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

Declarations for video. More...

#include <chrono>
#include "input.h"
#include "platform/common.h"
#include "thread_safe.h"
#include "video_colorspace.h"
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
Include dependency graph for video.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  video::encoder_t::codec_t
 Codec capabilities for AV1, HEVC, or H.264. More...
 
struct  video::config_t
 Encoding configuration requested by a remote client. More...
 
struct  video::encode_session_t
 Encoder session state shared by capture and encoding threads. More...
 
struct  video::encoder_platform_formats_avcodec
 AVCodec-specific pixel formats supported by a platform. More...
 
struct  video::encoder_platform_formats_nvenc
 NVENC-specific pixel formats supported by a platform. More...
 
struct  video::encoder_platform_formats_t
 Pixel formats supported by one encoder backend. More...
 
struct  video::encoder_t
 Encoder name and feature flags advertised by Sunshine. More...
 
struct  video::hdr_info_raw_t
 Raw HDR metadata fields parsed from the display backend. More...
 
struct  video::encoder_t::option_t
 Runtime encoder option exposed to configuration parsing. More...
 
struct  video::packet_raw_avcodec
 AVCodec packet wrapper with codec-specific metadata. More...
 
struct  video::packet_raw_generic
 Generic encoded packet bytes and metadata. More...
 
struct  video::packet_raw_t
 Encoded packet wrapper used by the streaming pipeline. More...
 
struct  video::packet_raw_t::replace_t
 Packet byte-range replacement descriptor. More...
 

Typedefs

using video::avcodec_buffer_t = util::safe_ptr<AVBufferRef, free_buffer>
 Owning pointer for an FFmpeg buffer reference.
 
using video::avcodec_ctx_t = util::safe_ptr<AVCodecContext, free_ctx>
 Owning pointer for an FFmpeg codec context.
 
using video::avcodec_frame_t = util::safe_ptr<AVFrame, free_frame>
 Owning pointer for an FFmpeg frame.
 
using video::hdr_info_t = std::unique_ptr<hdr_info_raw_t>
 Owning pointer to optional HDR metadata for a captured display.
 
using video::img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>
 Shared event that transports captured images between capture and encode threads.
 
using video::packet_t = std::unique_ptr<packet_raw_t>
 Owning pointer to an encoded packet abstraction.
 
using video::sws_t = util::safe_ptr<SwsContext, sws_freeContext>
 Owning pointer for an FFmpeg software-scaling context.
 

Functions

void video::capture (safe::mail_t mail, config_t config, void *channel_data)
 Capture and encode video for a streaming session.
 
std::chrono::nanoseconds video::capture_frame_interval (const config_t &config)
 Capture frame interval for the requested framerate. Uses the exact fractional rate when the client provided an X100 value.
 
AVRational video::framerate_to_rational (const config_t &config)
 Requested framerate as an exact rational. Uses the exact fractional rate when the client provided an X100 value, otherwise the integer framerate over 1.
 
AVRational video::framerateX100_to_rational (const int framerateX100)
 Convert a framerate stored as hundredths of Hz to an FFmpeg rational.
 
void video::free_buffer (AVBufferRef *ref)
 Release a backend buffer allocated for capture or conversion.
 
void video::free_ctx (AVCodecContext *ctx)
 Release context resources.
 
void video::free_frame (AVFrame *frame)
 Release an FFmpeg frame allocated by the capture or conversion backend.
 
platf::mem_type_e video::map_base_dev_type (AVHWDeviceType type)
 Map base dev type values.
 
platf::pix_fmt_e video::map_pix_fmt (AVPixelFormat fmt)
 Map pix fmt values.
 
int video::probe_encoders ()
 Probe encoders and select the preferred encoder. This is called once at startup and each time a stream is launched to ensure the best encoder is selected. Encoder availability can change at runtime due to all sorts of things from driver updates to eGPUs.
 
bool video::validate_encoder (encoder_t &encoder, bool expect_failure)
 Validate encoder before it is used.
 

Detailed Description

Declarations for video.

Function Documentation

◆ capture()

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

Capture and encode video for a streaming session.

Parameters
mailSession mail bus.
configVideo configuration.
channel_dataOpaque channel data passed to packets.

◆ capture_frame_interval()

std::chrono::nanoseconds video::capture_frame_interval ( const config_t & config)
inline

Capture frame interval for the requested framerate. Uses the exact fractional rate when the client provided an X100 value.

Parameters
configStream configuration carrying the requested framerate.
Returns
Duration of a single frame interval in nanoseconds.

◆ framerate_to_rational()

AVRational video::framerate_to_rational ( const config_t & config)
inline

Requested framerate as an exact rational. Uses the exact fractional rate when the client provided an X100 value, otherwise the integer framerate over 1.

Parameters
configStream configuration carrying the requested framerate.
Returns
Requested frame rate as a rational.

◆ framerateX100_to_rational()

AVRational video::framerateX100_to_rational ( const int framerateX100)
inline

Convert a framerate stored as hundredths of Hz to an FFmpeg rational.

Parameters
framerateX100Framerate multiplied by 100, such as 5994 for 59.94 Hz.
Returns
Rational frame rate preserving NTSC fractional rates when applicable.

◆ free_buffer()

void video::free_buffer ( AVBufferRef * ref)

Release a backend buffer allocated for capture or conversion.

Parameters
refFFmpeg buffer reference to unref and free.

◆ free_ctx()

void video::free_ctx ( AVCodecContext * ctx)

Release context resources.

Release an FFmpeg codec context.

Parameters
ctxCodec context allocated by FFmpeg.

◆ free_frame()

void video::free_frame ( AVFrame * frame)

Release an FFmpeg frame allocated by the capture or conversion backend.

Parameters
frameVideo or graphics frame being processed.

◆ map_base_dev_type()

platf::mem_type_e video::map_base_dev_type ( AVHWDeviceType type)

Map base dev type values.

Map an FFmpeg hardware device type to Sunshine's memory type.

Parameters
typeFFmpeg hardware device type reported by the encoder backend.
Returns
Sunshine memory type used by the capture and encode pipeline.

◆ map_pix_fmt()

platf::pix_fmt_e video::map_pix_fmt ( AVPixelFormat fmt)

Map pix fmt values.

Map an FFmpeg pixel format to Sunshine's pixel format enum.

Parameters
fmtFFmpeg pixel format to convert.
Returns
Sunshine pixel format used by display and encoder backends.

◆ probe_encoders()

int video::probe_encoders ( )

Probe encoders and select the preferred encoder. This is called once at startup and each time a stream is launched to ensure the best encoder is selected. Encoder availability can change at runtime due to all sorts of things from driver updates to eGPUs.

Warning
This is only safe to call when there is no client actively streaming.
Returns
0 when a usable encoder is selected; nonzero when probing fails.

◆ validate_encoder()

bool video::validate_encoder ( encoder_t & encoder,
bool expect_failure )

Validate encoder before it is used.

Parameters
encoderEncoder configuration or encoder instance.
expect_failureExpect failure.
Returns
True when encoder validation matches expect_failure.