Sunshine master
Self-hosted game stream host for Moonlight.
audio.cpp File Reference

Definitions for audio capture and encoding. More...

#include <thread>
#include <opus/opus_multistream.h>
#include "audio.h"
#include "config.h"
#include "globals.h"
#include "logging.h"
#include "platform/common.h"
#include "thread_safe.h"
#include "utility.h"
Include dependency graph for audio.cpp:

Typedefs

using audio::opus_t = util::safe_ptr<OpusMSEncoder, opus_multistream_encoder_destroy>
 Owning pointer for an Opus multistream encoder.
 
using audio::sample_queue_t = std::shared_ptr<safe::queue_t<std::vector<float>>>
 Shared queue carrying captured PCM sample buffers to the encoder thread.
 

Functions

void audio::capture (safe::mail_t mail, config_t config, void *channel_data)
 Run the capture loop for this backend.
 
void audio::encodeThread (sample_queue_t samples, config_t config, void *channel_data)
 Encode captured PCM samples into Opus packets on the audio worker thread.
 
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.
 
int audio::map_stream (int channels, bool quality)
 Select the Opus stream configuration for a channel count and quality tier.
 

Variables

constexpr auto audio::SAMPLE_RATE = 48000
 Audio sample rate in hertz required by Opus.
 
opus_stream_config_t audio::stream_configs [MAX_STREAM_CONFIG]
 Opus stream layouts and bitrates advertised to clients.
 

Detailed Description

Definitions for audio capture and encoding.

Function Documentation

◆ capture()

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

Run the capture loop for this backend.

Capture, encode, and publish audio packets for a stream.

Parameters
mailMailbox used to publish encoded audio packets.
configAudio capture and encoder settings.
channel_dataPlatform-specific capture channel pointer.

◆ encodeThread()

void audio::encodeThread ( sample_queue_t samples,
config_t config,
void * channel_data )

Encode captured PCM samples into Opus packets on the audio worker thread.

Parameters
samplesQueue of captured PCM sample buffers to encode.
configAudio stream settings negotiated with the client.
channel_dataPlatform-specific audio capture context passed to packet metadata.

◆ 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()

◆ 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;
Parameters
ctxNative context object used by the operation or callback.

◆ map_stream()

int audio::map_stream ( int channels,
bool quality )

Select the Opus stream configuration for a channel count and quality tier.

Parameters
channelsNumber of audio channels in the stream.
qualityWhether the high-quality Opus layout should be selected.
Returns
Index into stream_configs for the requested layout.