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

Definitions for audio control on Linux. More...

#include <bitset>
#include <sstream>
#include <thread>
#include <boost/regex.hpp>
#include <pulse/error.h>
#include <pulse/pulseaudio.h>
#include <pulse/simple.h>
#include "src/config.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/thread_safe.h"
Include dependency graph for audio.cpp:

Classes

struct  platf::pa::add_const_helper< false, T >
 Template helper that leaves non-const inputs mutable. More...
 
struct  platf::pa::add_const_helper< true, T >
 Template helper that preserves constness for const inputs. More...
 
struct  platf::mic_attr_t
 PulseAudio recording stream and channel metadata. More...
 
class  platf::pa::server_t
 PulseAudio server controller that creates and removes Sunshine sinks. More...
 

Typedefs

template<class T >
using platf::pa::add_const_t = typename add_const_helper<std::is_pointer_v<T>, T>::type
 PulseAudio callback info type with pointer constness normalized.
 
template<class T >
using platf::pa::cb_simple_t = std::function<void(ctx_t::pointer, add_const_t<T> i)>
 Callback wrapper for PulseAudio introspection results without an end marker.
 
template<class T >
using platf::pa::cb_t = std::function<void(ctx_t::pointer, add_const_t<T> i, int eol)>
 Callback wrapper for PulseAudio introspection results with an end marker.
 
using platf::pa::ctx_t = util::safe_ptr<pa_context, pa_context_unref>
 Owning pointer for a PulseAudio context.
 
using platf::pa::loop_t = util::safe_ptr<pa_mainloop, pa_mainloop_free>
 Owning pointer for a PulseAudio mainloop.
 
using platf::pa::op_t = util::safe_ptr<pa_operation, pa_operation_unref>
 Owning pointer for a PulseAudio asynchronous operation.
 
using platf::pa::string_t = util::safe_ptr<char, pa_free<char>>
 Owning pointer for PulseAudio strings allocated with pa_xmalloc.
 

Functions

std::unique_ptr< audio_control_tplatf::audio_control ()
 Create the platform audio controller.
 
template<class T >
void platf::pa::cb (ctx_t::pointer ctx, add_const_t< T > i, int eol, void *userdata)
 Handle PulseAudio source introspection results.
 
template<class T >
void platf::pa::cb (ctx_t::pointer ctx, add_const_t< T > i, void *userdata)
 Handle PulseAudio sink-input introspection results.
 
void platf::pa::cb_i (ctx_t::pointer ctx, std::uint32_t i, void *userdata)
 Forward a PulseAudio integer callback value into a Sunshine alarm.
 
void platf::pa::ctx_state_cb (ctx_t::pointer ctx, void *userdata)
 Translate PulseAudio context state changes into server events.
 
std::unique_ptr< mic_tplatf::microphone (const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, std::string source_name)
 Create a microphone capture stream for the requested layout.
 
template<class T >
void platf::pa::pa_free (T *p)
 Release memory allocated by PulseAudio.
 
void platf::pa::success_cb (ctx_t::pointer ctx, int status, void *userdata)
 Record completion of a PulseAudio asynchronous operation.
 
std::string platf::to_string (const char *name, const std::uint8_t *mapping, int channels)
 Convert a PulseAudio operation result to a log string.
 

Variables

constexpr pa_channel_position_t platf::position_mapping []
 Position mapping.
 

Detailed Description

Definitions for audio control on Linux.

Function Documentation

◆ audio_control()

std::unique_ptr< audio_control_t > platf::audio_control ( )

Create the platform audio controller.

Returns
Platform audio controller, or nullptr when audio control is unavailable.

◆ cb() [1/2]

template<class T >
void platf::pa::cb ( ctx_t::pointer ctx,
add_const_t< T > i,
int eol,
void * userdata )

Handle PulseAudio source introspection results.

Parameters
ctxNative context object used by the operation or callback.
iPulseAudio introspection info supplied to the callback.
eolPulseAudio end-of-list marker.
userdataCaller-provided pointer passed through the callback.

◆ cb() [2/2]

template<class T >
void platf::pa::cb ( ctx_t::pointer ctx,
add_const_t< T > i,
void * userdata )

Handle PulseAudio sink-input introspection results.

Parameters
ctxNative context object used by the operation or callback.
iPulseAudio introspection info supplied to the callback.
userdataCaller-provided pointer passed through the callback.

◆ cb_i()

void platf::pa::cb_i ( ctx_t::pointer ctx,
std::uint32_t i,
void * userdata )

Forward a PulseAudio integer callback value into a Sunshine alarm.

Parameters
ctxPulseAudio context that emitted the callback.
iInteger value returned by the PulseAudio operation.
userdataCaller-provided pointer passed through the callback.

◆ ctx_state_cb()

void platf::pa::ctx_state_cb ( ctx_t::pointer ctx,
void * userdata )

Translate PulseAudio context state changes into server events.

Parameters
ctxNative context object used by the operation or callback.
userdataCaller-provided pointer passed through the callback.

◆ microphone()

std::unique_ptr< mic_t > platf::microphone ( const std::uint8_t * mapping,
int channels,
std::uint32_t sample_rate,
std::uint32_t frame_size,
std::string source_name )

Create a microphone capture stream for the requested layout.

Parameters
mappingOpus channel mapping table for the requested layout.
channelsNumber of audio channels in the stream.
sample_rateAudio sample rate in hertz.
frame_sizeNumber of samples captured per audio frame.
source_nameSource name.
Returns
Microphone capture object for the requested audio layout.

◆ pa_free()

template<class T >
void platf::pa::pa_free ( T * p)

Release memory allocated by PulseAudio.

Parameters
pPointer allocated by PulseAudio and released with pa_xfree.

◆ success_cb()

void platf::pa::success_cb ( ctx_t::pointer ctx,
int status,
void * userdata )

Record completion of a PulseAudio asynchronous operation.

Parameters
ctxNative context object used by the operation or callback.
statusNative status code returned by the platform API.
userdataCaller-provided pointer passed through the callback.

◆ to_string()

std::string platf::to_string ( const char * name,
const std::uint8_t * mapping,
int channels )

Convert a PulseAudio operation result to a log string.

Parameters
nameHuman-readable name to assign.
mappingOpus channel mapping table for the requested layout.
channelsNumber of audio channels in the stream.
Returns
Value converted to string.

Variable Documentation

◆ position_mapping

pa_channel_position_t platf::position_mapping[]
constexpr
Initial value:
{
PA_CHANNEL_POSITION_FRONT_LEFT,
PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER,
PA_CHANNEL_POSITION_LFE,
PA_CHANNEL_POSITION_REAR_LEFT,
PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_SIDE_LEFT,
PA_CHANNEL_POSITION_SIDE_RIGHT,
}

Position mapping.