Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
audio.h File Reference

Declarations for audio capture and encoding. More...

#include "platform/common.h"
#include "thread_safe.h"
#include "utility.h"
#include <bitset>
#include <memory>
#include <optional>
#include <string>
#include <opus/opus.h>
Include dependency graph for audio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  audio::opus_stream_config_t
struct  audio::stream_params_t
struct  audio::config_t
struct  audio::audio_ctx_t
class  audio::client_mic_t
 Decodes a client's Opus microphone packets into a session mic. More...

Typedefs

using audio::buffer_t = util::buffer_t<std::uint8_t>
using audio::packet_t = std::pair<void *, buffer_t>
using audio::audio_ctx_ref_t = safe::shared_t<audio_ctx_t>::ptr_t

Enumerations

enum  audio::stream_config_e : int {
  audio::STEREO , audio::HIGH_STEREO , audio::SURROUND51 , audio::HIGH_SURROUND51 ,
  audio::SURROUND71 , audio::HIGH_SURROUND71 , audio::MAX_STREAM_CONFIG
}

Functions

void audio::capture (safe::mail_t mail, config_t config, void *channel_data)
bool audio::create_session_sink (const std::string &name, int channels)
 Create the sink an isolated session records, and its own alone.
void audio::remove_session_sink (const std::string &name)
 Remove a sink made by create_session_sink(). Removing one that is already gone is not an error.
std::unique_ptr< client_mic_t > audio::open_client_mic (const std::string &source)
 nullptr if the session mic cannot be opened.
audio_ctx_ref_t audio::get_audio_ctx_ref ()
 Get the reference to the audio context.
bool audio::is_audio_ctx_sink_available (const audio_ctx_t &ctx)
 Check if the audio sink held by audio context is available.
bool audio::host_sink_restore_pending (const audio_ctx_t &ctx)
 Whether releasing the host sink still has something to wait for.
void audio::hold_host_sink ()
 Record the host's default sink before a display change can move it.
void audio::release_host_sink ()
 Let go of a hold_host_sink() hold and restore the sink it recorded.

Detailed Description

Declarations for audio capture and encoding.

Definition in file audio.h.

Typedef Documentation

◆ audio_ctx_ref_t

using audio::audio_ctx_ref_t = safe::shared_t<audio_ctx_t>::ptr_t

Definition at line 99 of file audio.h.

◆ buffer_t

using audio::buffer_t = util::buffer_t<std::uint8_t>

Definition at line 97 of file audio.h.

◆ packet_t

using audio::packet_t = std::pair<void *, buffer_t>

Definition at line 98 of file audio.h.

Enumeration Type Documentation

◆ stream_config_e

Enumerator
STEREO 

Stereo.

HIGH_STEREO 

High stereo.

SURROUND51 

Surround 5.1.

HIGH_SURROUND51 

High surround 5.1.

SURROUND71 

Surround 7.1.

HIGH_SURROUND71 

High surround 7.1.

MAX_STREAM_CONFIG 

Maximum audio stream configuration.

Definition at line 20 of file audio.h.

Function Documentation

◆ capture()

void audio::capture ( safe::mail_t mail,
config_t config,
void * channel_data )

Definition at line 137 of file audio.cpp.

◆ create_session_sink()

bool audio::create_session_sink ( const std::string & name,
int channels )

Create the sink an isolated session records, and its own alone.

Named after the session rather than after a channel layout, because there is one per session and its layout is whatever that client negotiated. Nothing is made anyone's default. Returns false where the platform has no virtual sinks, which leaves the caller to fall back to the shared ones.

Definition at line 483 of file audio.cpp.

◆ get_audio_ctx_ref()

audio_ctx_ref_t audio::get_audio_ctx_ref ( )

Get the reference to the audio context.

Returns
A shared pointer reference to audio context.
Note
Aside from the configuration purposes, it can be used to extend the audio sink lifetime to capture sink earlier and restore it later.

Examples

audio_ctx_ref_t audio = get_audio_ctx_ref()

Definition at line 315 of file audio.cpp.

◆ hold_host_sink()

void audio::hold_host_sink ( )

Record the host's default sink before a display change can move it.

A monitor's audio device belongs to the monitor: turn the output off - which is what the exclusive virtual display does to the local screens - and the sound server drops that sink and moves the default onto whatever is left. A session that reads the default after that has already missed the sink the user was listening on, and will faithfully restore the replacement when it ends.

Taking the audio context here reads the default while it is still the user's own, and holds the context open so the restore is timed by release_host_sink() rather than by whenever the last stream happens to end.

Holding a hold that is already held does nothing. A hold that is never released lasts until Hermes exits.

Definition at line 435 of file audio.cpp.

◆ host_sink_restore_pending()

bool audio::host_sink_restore_pending ( const audio_ctx_t & ctx)

Whether releasing the host sink still has something to wait for.

The question release_host_sink() asks between sleeps. False means restore now - either nothing was recorded to go back to, or what was recorded is present again. True means the recorded sink is still missing, and a restore issued now would name a sink the sound server cannot resolve: which is what happens for the seconds it takes a monitor coming out of an exclusive session to register its audio device again.

Definition at line 320 of file audio.cpp.

◆ is_audio_ctx_sink_available()

bool audio::is_audio_ctx_sink_available ( const audio_ctx_t & ctx)

Check if the audio sink held by audio context is available.

Returns
True if available (and can probably be restored), false otherwise.
Note
Useful for delaying the release of audio context shared pointer (which tries to restore original sink).

Examples

audio_ctx_ref_t audio = get_audio_ctx_ref()
if (audio.get()) {
return is_audio_ctx_sink_available(*audio.get());
}
return false;

Definition at line 554 of file audio.cpp.

◆ open_client_mic()

std::unique_ptr< client_mic_t > audio::open_client_mic ( const std::string & source)

nullptr if the session mic cannot be opened.

Definition at line 547 of file audio.cpp.

◆ release_host_sink()

void audio::release_host_sink ( )

Let go of a hold_host_sink() hold and restore the sink it recorded.

The sink a disabled output took with it does not come back the instant the monitor does, so the restore is not issued until the recorded sink is present again, or until the wait for it runs out after ten seconds. This returns straight away; the waiting is done on a thread of its own.

The wait is bounded that tightly because the host's default is still the virtual sink until it ends. A device that never comes back - a headset unplugged during the session - therefore costs that much quiet and no more, and then leaves the choice of default to the sound server, which is the right answer once the device the user picked is gone.

A hold taken again while that wait is running cancels it and keeps the context, which is what a second session starting before the first one's monitors are back should do.

Definition at line 461 of file audio.cpp.

◆ remove_session_sink()

void audio::remove_session_sink ( const std::string & name)

Remove a sink made by create_session_sink(). Removing one that is already gone is not an error.

Definition at line 496 of file audio.cpp.