Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
audio.cpp File Reference

Definitions for audio capture and encoding. More...

#include <algorithm>
#include <array>
#include <thread>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/frame.h>
#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:

Classes

class  audio::ffmpeg_audio_encoder_t
 Encode one interleaved stereo or 5.1 float PCM frame as an AC-3-family access unit. More...

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

bool audio::ac3_available ()
 Check whether the linked FFmpeg build provides the AC-3 encoder.
void audio::capture (safe::mail_t mail, config_t config, stream::session_t *channel_data)
 Run the capture loop for this backend.
dolby_codec_e audio::configure_dolby_audio (config_t &config, std::string_view offered, std::string_view accepted, bool ac3_encoder_available, bool eac3_encoder_available, bool high_quality)
 Configure a Dolby stream only for an exact, supported client acceptance.
int audio::dolby_bitrate_kbps (bool eac3, bool high_quality, int channels)
 Select a channel-aware bitrate for Dolby audio encoding.
std::string_view audio::dolby_codec_name (dolby_codec_e codec)
 Return the codec label for the RTSP audio-codec response header.
bool audio::eac3_available ()
 Check whether the linked FFmpeg build provides the E-AC-3 encoder.
void audio::encodeThread (sample_queue_t samples, config_t config, stream::session_t *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.
const std::array< opus_stream_config_t, MAX_STREAM_CONFIG > audio::stream_configs
 Opus stream layouts and bitrates advertised to clients.

Detailed Description

Definitions for audio capture and encoding.

Function Documentation

◆ ac3_available()

bool audio::ac3_available ( )

Check whether the linked FFmpeg build provides the AC-3 encoder.

Check whether a compatible optional FFmpeg AC-3 encoder is available.

Returns
True when the AC-3 encoder is available in this build.
True when the required FFmpeg DLLs and AC-3 encoder are available.

◆ capture()

void audio::capture ( safe::mail_t mail,
config_t config,
stream::session_t * 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_dataStream session that receives the encoded packets.

◆ configure_dolby_audio()

dolby_codec_e audio::configure_dolby_audio ( config_t & config,
std::string_view offered,
std::string_view accepted,
bool ac3_encoder_available,
bool eac3_encoder_available,
bool high_quality )

Configure a Dolby stream only for an exact, supported client acceptance.

Parameters
configAudio configuration to update for the negotiated stream.
offeredCodec offered by Sunshine, or empty when using Opus.
acceptedCodec explicitly accepted by the client.
ac3_encoder_availableWhether the AC-3 encoder is available.
eac3_encoder_availableWhether the E-AC-3 encoder is available.
high_qualityWhether the higher Dolby bitrate was requested.
Returns
Selected codec, falling back to Opus when negotiation is unsupported.

◆ dolby_bitrate_kbps()

int audio::dolby_bitrate_kbps ( bool eac3,
bool high_quality,
int channels )

Select a channel-aware bitrate for Dolby audio encoding.

Parameters
eac3True to select E-AC-3; false to select AC-3.
high_qualityWhether to use the higher Dolby bitrate.
channelsNumber of output audio channels (stereo or 5.1).
Returns
The selected bitrate in kbit/s, or 0 for unsupported channel layouts.

◆ dolby_codec_name()

std::string_view audio::dolby_codec_name ( dolby_codec_e codec)

Return the codec label for the RTSP audio-codec response header.

Parameters
codecSelected audio codec.
Returns
RTSP codec label.

◆ eac3_available()

bool audio::eac3_available ( )

Check whether the linked FFmpeg build provides the E-AC-3 encoder.

Check whether a compatible optional FFmpeg E-AC-3 encoder is available.

Returns
True when the E-AC-3 encoder is available in this build.
True when the required FFmpeg DLLs and E-AC-3 encoder are available.

◆ encodeThread()

void audio::encodeThread ( sample_queue_t samples,
config_t config,
stream::session_t * 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_dataStream session carried with each encoded packet.

◆ 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.

Variable Documentation

◆ stream_configs

const std::array< opus_stream_config_t, MAX_STREAM_CONFIG > audio::stream_configs

Opus stream layouts and bitrates advertised to clients.

Opus layouts and bitrates advertised for each supported stream mode.