Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
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 <array>
#include <bitset>
#include <string_view>
#include <vector>
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::audio_ctx_t
 Shared audio device state used while streams are active. More...
struct  audio::config_t
 Audio capture and encoder settings for a stream. More...
struct  audio::opus_stream_config_t
 Static Opus encoder layout for one advertised audio mode. More...
struct  audio::stream_params_t
 Custom Opus channel layout supplied by configuration. More...

Typedefs

using audio::audio_ctx_ref_t = safe::shared_t<audio_ctx_t>::ptr_t
 Shared mailbox reference to the global audio context.
using audio::buffer_t = util::buffer_t<std::uint8_t>
 Byte buffer used for encoded audio packet payloads.
using audio::packet_t = std::pair<stream::session_t *, buffer_t>
 Encoded audio packet paired with its owning stream session.

Enumerations

enum class  audio::dolby_codec_e { opus , ac3 , eac3 }
 Audio codec selected by RTSP Dolby offer/accept negotiation. More...
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
}
 Supported Opus channel layouts advertised to Moonlight clients. More...

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

Detailed Description

Declarations for audio capture and encoding.

Enumeration Type Documentation

◆ dolby_codec_e

enum class audio::dolby_codec_e
strong

Audio codec selected by RTSP Dolby offer/accept negotiation.

Enumerator
opus 

Existing Opus audio path.

ac3 

Dolby Digital AC-3 bitstream.

eac3 

Dolby Digital Plus E-AC-3 bitstream.

◆ stream_config_e

Supported Opus channel layouts advertised to Moonlight clients.

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.

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.

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