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

Definitions for the streaming protocols. More...

#include <fstream>
#include <future>
#include <queue>
#include <boost/endian/arithmetic.hpp>
#include <openssl/err.h>
#include <rs.h>
#include <moonlight-common-c/src/Limelight-internal.h>
#include "config.h"
#include "display_device.h"
#include "globals.h"
#include "input.h"
#include "logging.h"
#include "network.h"
#include "platform/common.h"
#include "process.h"
#include "stream.h"
#include "sync.h"
#include "system_tray.h"
#include "thread_safe.h"
#include "utility.h"
Include dependency graph for stream.cpp:

Classes

struct  stream::audio_fec_packet_t
 Packed RTP and FEC headers for an audio recovery packet. More...
 
struct  stream::audio_packet_t
 Packed RTP header for an audio packet. More...
 
struct  stream::broadcast_ctx_t
 UDP broadcast socket and target address state. More...
 
struct  stream::control_adaptive_triggers_t
 Control payload that configures DualSense adaptive triggers. More...
 
struct  stream::control_encrypted_t
 Packed encrypted control-channel envelope. More...
 
struct  stream::control_hdr_mode_t
 Control payload that toggles HDR mode and carries metadata. More...
 
struct  stream::control_header_v2
 Packed control-channel header used before control payloads. More...
 
struct  stream::control_rumble_t
 Control payload that sets controller rumble motors. More...
 
struct  stream::control_rumble_triggers_t
 Control payload that sets trigger rumble motors. More...
 
class  stream::control_server_t
 ENet control server that routes incoming control packets to stream sessions. More...
 
struct  stream::control_set_motion_event_t
 Control payload that enables or disables motion reports. More...
 
struct  stream::control_set_rgb_led_t
 Control payload that sets controller RGB LED color. More...
 
struct  stream::control_terminate_t
 Control-channel termination message payload. More...
 
struct  stream::fec::fec_t
 Reed-Solomon FEC encoder state for video packets. More...
 
struct  stream::session_t
 Runtime state for one audio/video streaming session. More...
 
struct  stream::video_packet_enc_prefix_t
 AES-GCM prefix written before encrypted video packet payloads. More...
 
struct  stream::video_packet_raw_t
 Packed RTP and video headers for an unencrypted video packet. More...
 
struct  stream::video_short_frame_header_t
 Packed short video frame header sent before video payload bytes. More...
 

Typedefs

using stream::audio_aes_t = std::array<char, round_to_pkcs7_padded(MAX_AUDIO_PACKET_SIZE)>
 AES key storage used for audio packet encryption.
 
using stream::av_session_id_t = std::variant<asio::ip::address, std::string>
 Audio/video session identifier carried by GameStream packets.
 
typedef struct stream::control_encrypted_tstream::control_encrypted_p
 Packed encrypted control-channel envelope.
 
using stream::message_queue_queue_t = std::shared_ptr<safe::queue_t<std::tuple<socket_e, av_session_id_t, message_queue_t>>>
 Shared queue set used to distribute packet queues to broadcast workers.
 
using stream::message_queue_t = std::shared_ptr<safe::queue_t<std::pair<udp::endpoint, std::string>>>
 Mail queue carrying encoded stream packets to sender threads.
 
using stream::fec::rs_t
 Owning pointer for a Reed-Solomon encoder instance.
 

Enumerations

enum class  stream::socket_e : int { video , audio }
 Enumerates supported socket options. More...
 

Functions

std::shared_ptr< session_tstream::session::alloc (config_t &config, rtsp_stream::launch_session_t &launch_session)
 Allocate and initialize platform input state for a stream.
 
void stream::audioBroadcastThread (udp::socket &sock)
 Run the broadcast audio sender thread.
 
void stream::audioThread (session_t *session)
 Run the session audio capture and encode thread.
 
const std::string & stream::session::client_cert (session_t &session)
 Return the paired client certificate for this session.
 
std::vector< uint8_t > stream::concat_and_insert (uint64_t insert_size, uint64_t slice_size, const std::string_view &data1, const std::string_view &data2)
 Combines two buffers and inserts new buffers at each slice boundary of the result.
 
void stream::controlBroadcastThread (control_server_t *server)
 Run the broadcast control-channel worker thread.
 
void stream::end_broadcast (broadcast_ctx_t &ctx)
 Stop broadcast processing.
 
void stream::session::join (session_t &session)
 Wait for worker threads owned by the session to exit.
 
int stream::recv_ping (session_t *session, decltype(broadcast)::ptr_t ref, socket_e type, std::string_view expected_payload, udp::endpoint &peer, std::chrono::milliseconds timeout)
 Receive ping data.
 
void stream::recvThread (broadcast_ctx_t &ctx)
 Receive thread data.
 
std::vector< uint8_t > stream::replace (const std::string_view &original, const std::string_view &old, const std::string_view &_new)
 Replace a byte sequence in an encoded packet.
 
constexpr std::size_t stream::round_to_pkcs7_padded (std::size_t size)
 Round a byte count up to the next PKCS#7 padding boundary.
 
int stream::send_feedback_msg (session_t *session, platf::gamepad_feedback_msg_t &msg)
 Pass gamepad feedback data back to the client.
 
int stream::send_hdr_mode (session_t *session, video::hdr_info_t hdr_info)
 Send the selected HDR mode to the connected client over the control channel.
 
int stream::session::start (session_t &session, const std::string &addr_string)
 Start the audio, video, and control workers for a streaming session.
 
int stream::start_broadcast (broadcast_ctx_t &ctx)
 Start periodic mDNS and service-discovery broadcasts.
 
state_e stream::session::state (session_t &session)
 Platform handle returned from stream setup.
 
void stream::session::stop (session_t &session)
 Stop the active streaming session and prevent new packets from being queued.
 
void stream::videoBroadcastThread (udp::socket &sock)
 Run the broadcast video sender thread.
 
void stream::videoThread (session_t *session)
 Run the session video capture and encode thread.
 

Variables

constexpr int IDX_ENCRYPTED = 10
 Control-stream message index for encrypted.
 
constexpr int IDX_HDR_MODE = 11
 Control-stream message index for hdr mode.
 
constexpr int IDX_INPUT_DATA = 5
 Control-stream message index for input data.
 
constexpr int IDX_INVALIDATE_REF_FRAMES = 2
 Control-stream message index for invalidate ref frames.
 
constexpr int IDX_LOSS_STATS = 3
 Control-stream message index for loss stats.
 
constexpr int IDX_PERIODIC_PING = 8
 Control-stream message index for periodic ping.
 
constexpr int IDX_REQUEST_IDR_FRAME = 9
 Control-stream message index for request idr frame.
 
constexpr int IDX_RUMBLE_DATA = 6
 Control-stream message index for rumble data.
 
constexpr int IDX_RUMBLE_TRIGGER_DATA = 12
 Control-stream message index for rumble trigger data.
 
constexpr int IDX_SET_ADAPTIVE_TRIGGERS = 15
 Control-stream message index for set adaptive triggers.
 
constexpr int IDX_SET_MOTION_EVENT = 13
 Control-stream message index for set motion event.
 
constexpr int IDX_SET_RGB_LED = 14
 Control-stream message index for set rgb led.
 
constexpr int IDX_START_A = 0
 Control-stream message index for the first stream-start packet.
 
constexpr int IDX_START_B = 1
 Control-stream message index for the second stream-start packet.
 
constexpr int IDX_TERMINATION = 7
 Control-stream message index for termination.
 
constexpr std::size_t stream::MAX_AUDIO_PACKET_SIZE = 1400
 Protocol or platform constant for max audio packet size.
 
std::atomic_uint stream::session::running_sessions
 Running sessions.
 

Detailed Description

Definitions for the streaming protocols.

Typedef Documentation

◆ control_encrypted_p

Packed encrypted control-channel envelope.

Alias for control encrypted p.

◆ rs_t

Initial value:
util::safe_ptr<reed_solomon, [](reed_solomon *rs) {
reed_solomon_release(rs);
}>
Unique pointer wrapper with customizable pointer and deleter types.
Definition utility.h:820

Owning pointer for a Reed-Solomon encoder instance.

Enumeration Type Documentation

◆ socket_e

enum class stream::socket_e : int
strong

Enumerates supported socket options.

Enumerator
video 

Video.

audio 

Audio.

Function Documentation

◆ alloc()

std::shared_ptr< session_t > stream::session::alloc ( config_t & config,
rtsp_stream::launch_session_t & launch_session )

Allocate and initialize platform input state for a stream.

Parameters
configConfiguration values to apply.
launch_sessionLaunch session.
Returns
Allocated object or identifier, or an error value on failure.

◆ audioBroadcastThread()

void stream::audioBroadcastThread ( udp::socket & sock)

Run the broadcast audio sender thread.

Parameters
sockSocket used to read or write the protocol message.

◆ audioThread()

void stream::audioThread ( session_t * session)

Run the session audio capture and encode thread.

Parameters
sessionActive streaming or pairing session for the request.

◆ client_cert()

const std::string & stream::session::client_cert ( session_t & session)

Return the paired client certificate for this session.

Return the paired client certificate for a stream session.

Parameters
sessionActive streaming or pairing session for the request.
Returns
PEM certificate associated with the session's client.

◆ concat_and_insert()

std::vector< uint8_t > stream::concat_and_insert ( uint64_t insert_size,
uint64_t slice_size,
const std::string_view & data1,
const std::string_view & data2 )

Combines two buffers and inserts new buffers at each slice boundary of the result.

Parameters
insert_sizeThe number of bytes to insert.
slice_sizeThe number of bytes between insertions.
data1The first data buffer.
data2The second data buffer.
Returns
Combined buffer with insert padding written at each slice boundary.

◆ controlBroadcastThread()

void stream::controlBroadcastThread ( control_server_t * server)

Run the broadcast control-channel worker thread.

Parameters
serverRTSP server instance handling the request.

◆ end_broadcast()

void stream::end_broadcast ( broadcast_ctx_t & ctx)

Stop broadcast processing.

Parameters
ctxNative context object used by the operation or callback.

◆ join()

void stream::session::join ( session_t & session)

Wait for worker threads owned by the session to exit.

Parameters
sessionActive streaming or pairing session for the request.

◆ recv_ping()

int stream::recv_ping ( session_t * session,
decltype(broadcast)::ptr_t ref,
socket_e type,
std::string_view expected_payload,
udp::endpoint & peer,
std::chrono::milliseconds timeout )

Receive ping data.

Parameters
sessionActive streaming or pairing session for the request.
refReference frame metadata used by the encoder.
typeProtocol, message, or resource type selector.
expected_payloadExpected payload.
peerRemote endpoint associated with the socket.
timeoutMaximum time to wait for the operation.
Returns
Network operation status.

◆ recvThread()

void stream::recvThread ( broadcast_ctx_t & ctx)

Receive thread data.

Parameters
ctxNative context object used by the operation or callback.

◆ replace()

std::vector< uint8_t > stream::replace ( const std::string_view & original,
const std::string_view & old,
const std::string_view & _new )

Replace a byte sequence in an encoded packet.

Parameters
originalOriginal text value used when reporting a parsing failure.
oldByte sequence to replace in encoded packets.
_newReplacement byte sequence inserted into encoded packets.
Returns
Copy of the original buffer with each matching byte sequence replaced.

◆ round_to_pkcs7_padded()

std::size_t stream::round_to_pkcs7_padded ( std::size_t size)
constexpr

Round a byte count up to the next PKCS#7 padding boundary.

Parameters
sizeNumber of bytes or elements requested.
Returns
size rounded up to the next PKCS#7 block boundary.

◆ send_feedback_msg()

int stream::send_feedback_msg ( session_t * session,
platf::gamepad_feedback_msg_t & msg )

Pass gamepad feedback data back to the client.

Parameters
sessionThe session object.
msgThe message to pass.
Returns
0 on success.

◆ send_hdr_mode()

int stream::send_hdr_mode ( session_t * session,
video::hdr_info_t hdr_info )

Send the selected HDR mode to the connected client over the control channel.

Parameters
sessionActive streaming or pairing session for the request.
hdr_infoHDR info.
Returns
0 when the control message is queued; nonzero when no control peer is ready.

◆ start()

int stream::session::start ( session_t & session,
const std::string & addr_string )

Start the audio, video, and control workers for a streaming session.

Start a streaming session for the supplied peer address.

Parameters
sessionActive streaming or pairing session for the request.
addr_stringAddr string.
Returns
Start status.

◆ start_broadcast()

int stream::start_broadcast ( broadcast_ctx_t & ctx)

Start periodic mDNS and service-discovery broadcasts.

Bind the GameStream UDP and control sockets used for a streaming session.

Parameters
ctxNative context object used by the operation or callback.
Returns
0 on success; nonzero when broadcast setup fails.

◆ state()

state_e stream::session::state ( session_t & session)

Platform handle returned from stream setup.

Parameters
sessionActive streaming or pairing session for the request.
Returns
Current lifecycle state for the stream session.

◆ stop()

void stream::session::stop ( session_t & session)

Stop the active streaming session and prevent new packets from being queued.

Stop a streaming session and prevent more packets from being queued.

Parameters
sessionActive streaming or pairing session for the request.

◆ videoBroadcastThread()

void stream::videoBroadcastThread ( udp::socket & sock)

Run the broadcast video sender thread.

Parameters
sockSocket used to read or write the protocol message.

◆ videoThread()

void stream::videoThread ( session_t * session)

Run the session video capture and encode thread.

Parameters
sessionActive streaming or pairing session for the request.