Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
video.cpp File Reference

Definitions for video. More...

#include <atomic>
#include <bitset>
#include <list>
#include <mutex>
#include <thread>
#include <boost/pointer_cast.hpp>
#include <libavutil/imgutils.h>
#include <libavutil/mastering_display_metadata.h>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#include "process.h"
#include "annexb_filler.h"
#include "cbs.h"
#include "config.h"
#include "display_device.h"
#include "globals.h"
#include "input.h"
#include "logging.h"
#include "nvenc/nvenc_base.h"
#include "platform/common.h"
#include "sync.h"
#include "video.h"
#include "platform/linux/misc.h"
Include dependency graph for video.cpp:

Go to the source code of this file.

Classes

class  video::avcodec_software_encode_device_t
class  video::avcodec_encode_session_t
class  video::nvenc_encode_session_t
struct  video::sync_session_ctx_t
struct  video::sync_session_t
struct  video::capture_ctx_t
struct  video::capture_thread_async_ctx_t
struct  video::capture_thread_sync_ctx_t
struct  video::pipeline_metrics_accum_t

Typedefs

using video::encode_session_ctx_queue_t = safe::queue_t<sync_session_ctx_t>
using video::encode_e = platf::capture_e
typedef int(*) video::vaapi_init_avcodec_hardware_input_buffer_fn(platf::avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf)

Enumerations

enum class  video::nv::profile_h264_e : int { high = 2 , high_444p = 3 }
enum class  video::nv::profile_hevc_e : int { main = 0 , main_10 = 1 , rext = 2 }
enum  video::flag_e : uint32_t {
  video::DEFAULT = 0 , video::PARALLEL_ENCODING = 1 << 1 , video::H264_ONLY = 1 << 2 , video::LIMITED_GOP_SIZE = 1 << 3 ,
  video::SINGLE_SLICE_ONLY = 1 << 4 , video::CBR_WITH_VBR = 1 << 5 , video::RELAXED_COMPLIANCE = 1 << 6 , video::NO_RC_BUF_LIMIT = 1 << 7 ,
  video::REF_FRAMES_INVALIDATION = 1 << 8 , video::ALWAYS_REPROBE = 1 << 9 , video::YUV444_SUPPORT = 1 << 10 , video::ASYNC_TEARDOWN = 1 << 11
}
enum  video::validate_flag_e { video::VUI_PARAMS = 0x01 }

Functions

bool video::display_present ()
 Whether the host has a display to capture without creating one.
void video::free_ctx (AVCodecContext *ctx)
void video::free_frame (AVFrame *frame)
void video::free_buffer (AVBufferRef *ref)
util::Either< avcodec_buffer_t, int > video::vaapi_init_avcodec_hardware_input_buffer (platf::avcodec_encode_device_t *)
util::Either< avcodec_buffer_t, int > video::cuda_init_avcodec_hardware_input_buffer (platf::avcodec_encode_device_t *)
std::size_t video::pyrowave_frame_budget (const config_t &config)
 The most bytes one PyroWave frame may take.
bool video::pyrowave_available ()
int video::start_capture_sync (capture_thread_sync_ctx_t &ctx)
void video::end_capture_sync (capture_thread_sync_ctx_t &ctx)
int video::start_capture_async (capture_thread_async_ctx_t &ctx)
int video::start_capture_async_with_encoder (capture_thread_async_ctx_t &ctx, const encoder_t *encoder)
void video::end_capture_async (capture_thread_async_ctx_t &ctx)
encoder_status_t video::get_encoder_status ()
 Snapshot of the last encoder probe result (thread-safe copy).
void video::metrics_record_frame (double encode_ms, double capture_to_encode_ms, size_t packet_bytes)
 Record one encoded frame's timings and output size.
void video::metrics_record_drop ()
 Record one dropped frame (encoder error/timeout).
void video::metrics_reset ()
 Reset pipeline metrics at the start of a streaming session.
void video::metrics_set_resolution (int width, int height)
 Record the active stream resolution for diagnostics.
pipeline_metrics_t video::get_pipeline_metrics ()
 Snapshot of the live pipeline metrics (thread-safe copy).
void video::reset_display (std::shared_ptr< platf::display_t > &disp, const platf::mem_type_e &type, const std::string &display_name, const config_t &config)
void video::refresh_displays (platf::mem_type_e dev_type, std::vector< std::string > &display_names, int &current_display_index, std::string &preferred_display_name)
 Update the list of display names before or during a stream.
void video::refresh_displays (platf::mem_type_e dev_type, std::vector< std::string > &display_names, int &current_display_index)
void video::captureThread (std::shared_ptr< safe::queue_t< capture_ctx_t > > capture_ctx_queue, sync_util::sync_t< std::weak_ptr< platf::display_t > > &display_wp, safe::signal_t &reinit_event, const encoder_t &encoder)
int video::encode_avcodec (int64_t frame_nr, avcodec_encode_session_t &session, safe::mail_raw_t::queue_t< packet_t > &packets, void *channel_data, std::optional< std::chrono::steady_clock::time_point > frame_timestamp)
int video::encode_nvenc (int64_t frame_nr, nvenc_encode_session_t &session, safe::mail_raw_t::queue_t< packet_t > &packets, void *channel_data, std::optional< std::chrono::steady_clock::time_point > frame_timestamp)
int video::encode (int64_t frame_nr, encode_session_t &session, safe::mail_raw_t::queue_t< packet_t > &packets, void *channel_data, std::optional< std::chrono::steady_clock::time_point > frame_timestamp)
std::unique_ptr< avcodec_encode_session_t > video::make_avcodec_encode_session (platf::display_t *disp, const encoder_t &encoder, const config_t &config, int width, int height, std::unique_ptr< platf::avcodec_encode_device_t > encode_device)
std::unique_ptr< nvenc_encode_session_t > video::make_nvenc_encode_session (const config_t &client_config, std::unique_ptr< platf::nvenc_encode_device_t > encode_device)
std::unique_ptr< encode_session_t > video::make_encode_session (platf::display_t *disp, const encoder_t &encoder, const config_t &config, int width, int height, std::unique_ptr< platf::encode_device_t > encode_device)
void video::encode_run (int &frame_nr, safe::mail_t mail, img_event_t images, config_t config, std::shared_ptr< platf::display_t > disp, std::unique_ptr< platf::encode_device_t > encode_device, safe::signal_t &reinit_event, const encoder_t &encoder, void *channel_data)
input::touch_port_t video::make_port (platf::display_t *display, const config_t &config)
std::unique_ptr< platf::encode_device_t > video::make_encode_device (platf::display_t &disp, const encoder_t &encoder, const config_t &config)
std::optional< sync_session_t > video::make_synced_session (platf::display_t *disp, const encoder_t &encoder, platf::img_t &img, sync_session_ctx_t &ctx)
encode_e video::encode_run_sync (std::vector< std::unique_ptr< sync_session_ctx_t > > &synced_session_ctxs, encode_session_ctx_queue_t &encode_session_ctx_queue, std::vector< std::string > &display_names, int &display_p)
void video::captureThreadSync ()
void video::capture_async_with_context (capture_thread_async_ctx_t &capture_context, safe::mail_t mail, config_t &config, void *channel_data)
void video::capture_async (safe::mail_t mail, config_t &config, void *channel_data)
void video::capture (safe::mail_t mail, config_t config, void *channel_data)
int video::validate_config (std::shared_ptr< platf::display_t > disp, const encoder_t &encoder, const config_t &config)
bool video::validate_encoder (encoder_t &encoder, bool expect_failure)
std::optional< capture_readiness_mode_t > video::choose_capture_readiness_mode (bool client_hdr_requested, bool display_hdr, bool hevc_hdr_supported, bool av1_hdr_supported)
 Choose a readiness mode without ever encoding PQ scanout as SDR.
int video::check_capture_path (const config_t &stream, std::string &failure, const std::string &client={})
 Run one real frame through the path a stream will use.
capture_readiness_t video::last_capture_readiness ()
 The result of the last check_capture_path().
void video::forget_capture_readiness (const std::string &client)
 Forget the last check if it was for client's launch: a later launch of theirs ran without one, so it says nothing about that stream.
int video::probe_encoders_without_display ()
 Probe encoders without opening any screen.
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.
platf::mem_type_e video::map_base_dev_type (AVHWDeviceType type)
platf::pix_fmt_e video::map_pix_fmt (AVPixelFormat fmt)

Variables

auto video::capture_thread_async = safe::make_shared<capture_thread_async_ctx_t>(start_capture_async, end_capture_async)
auto video::capture_thread_sync = safe::make_shared<capture_thread_sync_ctx_t>(start_capture_sync, end_capture_sync)
encoder_t video::nvenc
encoder_t video::software
encoder_t video::vaapi
int video::active_hevc_mode
int video::active_av1_mode
bool video::last_encoder_probe_supported_ref_frames_invalidation = false
std::array< bool, 3 > video::last_encoder_probe_supported_yuv444_for_codec
bool video::probe_without_display = false

Detailed Description

Definitions for video.

Definition in file video.cpp.

Typedef Documentation

◆ encode_e

Definition at line 592 of file video.cpp.

◆ encode_session_ctx_queue_t

using video::encode_session_ctx_queue_t = safe::queue_t<sync_session_ctx_t>

Definition at line 591 of file video.cpp.

◆ vaapi_init_avcodec_hardware_input_buffer_fn

typedef int(*) video::vaapi_init_avcodec_hardware_input_buffer_fn(platf::avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf)

Definition at line 3463 of file video.cpp.

Enumeration Type Documentation

◆ flag_e

enum video::flag_e : uint32_t
Enumerator
DEFAULT 

Default flags.

PARALLEL_ENCODING 

Capture and encoding can run concurrently on separate threads.

H264_ONLY 

When HEVC is too heavy.

LIMITED_GOP_SIZE 

Some encoders don't like it when you have an infinite GOP_SIZE. e.g. VAAPI.

SINGLE_SLICE_ONLY 

Never use multiple slices. Older intel iGPU's ruin it for everyone else.

CBR_WITH_VBR 

Use a VBR rate control mode to simulate CBR.

RELAXED_COMPLIANCE 

Use FF_COMPLIANCE_UNOFFICIAL compliance mode.

NO_RC_BUF_LIMIT 

Don't set rc_buffer_size.

REF_FRAMES_INVALIDATION 

Support reference frames invalidation.

ALWAYS_REPROBE 

This is an encoder of last resort and we want to aggressively probe for a better one.

YUV444_SUPPORT 

Encoder may support 4:4:4 chroma sampling depending on hardware.

ASYNC_TEARDOWN 

Encoder supports async teardown on a different thread.

Definition at line 260 of file video.cpp.

◆ profile_h264_e

enum class video::nv::profile_h264_e : int
strong
Enumerator
high 

High profile.

high_444p 

High 4:4:4 Predictive profile.

Definition at line 80 of file video.cpp.

◆ profile_hevc_e

enum class video::nv::profile_hevc_e : int
strong
Enumerator
main 

Main profile.

main_10 

Main 10 profile.

rext 

Rext profile.

Definition at line 85 of file video.cpp.

◆ validate_flag_e

Enumerator
VUI_PARAMS 

VUI parameters.

Definition at line 2747 of file video.cpp.

Function Documentation

◆ capture()

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

Definition at line 2713 of file video.cpp.

◆ capture_async()

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

Definition at line 2687 of file video.cpp.

◆ capture_async_with_context()

void video::capture_async_with_context ( capture_thread_async_ctx_t & capture_context,
safe::mail_t mail,
config_t & config,
void * channel_data )

Definition at line 2587 of file video.cpp.

◆ captureThread()

void video::captureThread ( std::shared_ptr< safe::queue_t< capture_ctx_t > > capture_ctx_queue,
sync_util::sync_t< std::weak_ptr< platf::display_t > > & display_wp,
safe::signal_t & reinit_event,
const encoder_t & encoder )

Definition at line 1031 of file video.cpp.

◆ captureThreadSync()

void video::captureThreadSync ( )

Definition at line 2559 of file video.cpp.

◆ check_capture_path()

int video::check_capture_path ( const config_t & stream,
std::string & failure,
const std::string & client = {} )

Run one real frame through the path a stream will use.

Opens the display, captures a frame from it, converts it on the encoder's device and encodes it, with the encoder already chosen. Done before a client is told the stream is ready, so a broken step is named then rather than shown as a black stream.

Parameters
streamThe stream's capture settings: display_name (empty for the process-wide display), frame rate and client HDR request. The check chooses its own size and a codec matching the effective display color.
failureSet to the failed step and its reason.
clientThe paired client whose launch this check is for.
Returns
0 when a frame went all the way through, or when the display produced no new frame in time (logged; a static screen may not); -1 when a step failed.

Definition at line 3199 of file video.cpp.

◆ choose_capture_readiness_mode()

std::optional< capture_readiness_mode_t > video::choose_capture_readiness_mode ( bool client_hdr_requested,
bool display_hdr,
bool hevc_hdr_supported,
bool av1_hdr_supported )

Choose a readiness mode without ever encoding PQ scanout as SDR.

Returns
No mode when an SDR client encounters HDR output, or when the encoder has no supported HDR codec for an HDR output.

Definition at line 3001 of file video.cpp.

◆ cuda_init_avcodec_hardware_input_buffer()

util::Either< avcodec_buffer_t, int > video::cuda_init_avcodec_hardware_input_buffer ( platf::avcodec_encode_device_t * encode_device)

Definition at line 3489 of file video.cpp.

◆ display_present()

bool video::display_present ( )

Whether the host has a display to capture without creating one.

False means a virtual display has to stand in, both to probe the encoders and to stream. The kernel reports whether any connector lights a monitor. This answers yes whenever no virtual display could stand in or one already does.

Definition at line 52 of file video.cpp.

◆ encode()

int video::encode ( int64_t frame_nr,
encode_session_t & session,
safe::mail_raw_t::queue_t< packet_t > & packets,
void * channel_data,
std::optional< std::chrono::steady_clock::time_point > frame_timestamp )

Definition at line 1566 of file video.cpp.

◆ encode_avcodec()

int video::encode_avcodec ( int64_t frame_nr,
avcodec_encode_session_t & session,
safe::mail_raw_t::queue_t< packet_t > & packets,
void * channel_data,
std::optional< std::chrono::steady_clock::time_point > frame_timestamp )

Definition at line 1311 of file video.cpp.

◆ encode_nvenc()

int video::encode_nvenc ( int64_t frame_nr,
nvenc_encode_session_t & session,
safe::mail_raw_t::queue_t< packet_t > & packets,
void * channel_data,
std::optional< std::chrono::steady_clock::time_point > frame_timestamp )

Definition at line 1433 of file video.cpp.

◆ encode_run()

void video::encode_run ( int & frame_nr,
safe::mail_t mail,
img_event_t images,
config_t config,
std::shared_ptr< platf::display_t > disp,
std::unique_ptr< platf::encode_device_t > encode_device,
safe::signal_t & reinit_event,
const encoder_t & encoder,
void * channel_data )

Definition at line 2104 of file video.cpp.

◆ encode_run_sync()

encode_e video::encode_run_sync ( std::vector< std::unique_ptr< sync_session_ctx_t > > & synced_session_ctxs,
encode_session_ctx_queue_t & encode_session_ctx_queue,
std::vector< std::string > & display_names,
int & display_p )

Definition at line 2408 of file video.cpp.

◆ end_capture_async()

void video::end_capture_async ( capture_thread_async_ctx_t & ctx)

Definition at line 3523 of file video.cpp.

◆ end_capture_sync()

void video::end_capture_sync ( capture_thread_sync_ctx_t & ctx)

Definition at line 3534 of file video.cpp.

◆ forget_capture_readiness()

void video::forget_capture_readiness ( const std::string & client)

Forget the last check if it was for client's launch: a later launch of theirs ran without one, so it says nothing about that stream.

Definition at line 3217 of file video.cpp.

◆ free_buffer()

void video::free_buffer ( AVBufferRef * ref)

Definition at line 74 of file video.cpp.

◆ free_ctx()

void video::free_ctx ( AVCodecContext * ctx)

Definition at line 66 of file video.cpp.

◆ free_frame()

void video::free_frame ( AVFrame * frame)

Definition at line 70 of file video.cpp.

◆ get_encoder_status()

encoder_status_t video::get_encoder_status ( )

Snapshot of the last encoder probe result (thread-safe copy).

Definition at line 852 of file video.cpp.

◆ get_pipeline_metrics()

pipeline_metrics_t video::get_pipeline_metrics ( )

Snapshot of the live pipeline metrics (thread-safe copy).

Definition at line 926 of file video.cpp.

◆ last_capture_readiness()

capture_readiness_t video::last_capture_readiness ( )

The result of the last check_capture_path().

Definition at line 3212 of file video.cpp.

◆ make_avcodec_encode_session()

std::unique_ptr< avcodec_encode_session_t > video::make_avcodec_encode_session ( platf::display_t * disp,
const encoder_t & encoder,
const config_t & config,
int width,
int height,
std::unique_ptr< platf::avcodec_encode_device_t > encode_device )

Definition at line 1580 of file video.cpp.

◆ make_encode_device()

std::unique_ptr< platf::encode_device_t > video::make_encode_device ( platf::display_t & disp,
const encoder_t & encoder,
const config_t & config )

Definition at line 2303 of file video.cpp.

◆ make_encode_session()

std::unique_ptr< encode_session_t > video::make_encode_session ( platf::display_t * disp,
const encoder_t & encoder,
const config_t & config,
int width,
int height,
std::unique_ptr< platf::encode_device_t > encode_device )

Definition at line 2082 of file video.cpp.

◆ make_nvenc_encode_session()

std::unique_ptr< nvenc_encode_session_t > video::make_nvenc_encode_session ( const config_t & client_config,
std::unique_ptr< platf::nvenc_encode_device_t > encode_device )

Definition at line 1978 of file video.cpp.

◆ make_port()

input::touch_port_t video::make_port ( platf::display_t * display,
const config_t & config )

Definition at line 2264 of file video.cpp.

◆ make_synced_session()

std::optional< sync_session_t > video::make_synced_session ( platf::display_t * disp,
const encoder_t & encoder,
platf::img_t & img,
sync_session_ctx_t & ctx )

Definition at line 2368 of file video.cpp.

◆ map_base_dev_type()

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

Definition at line 3537 of file video.cpp.

◆ map_pix_fmt()

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

Definition at line 3552 of file video.cpp.

◆ metrics_record_drop()

void video::metrics_record_drop ( )

Record one dropped frame (encoder error/timeout).

Definition at line 904 of file video.cpp.

◆ metrics_record_frame()

void video::metrics_record_frame ( double encode_ms,
double capture_to_encode_ms,
size_t packet_bytes )

Record one encoded frame's timings and output size.

Parameters
encode_msTime spent in the encoder for this frame.
capture_to_encode_msLatency from capture to encode start.
packet_bytesEncoded packet size, for the measured bitrate.

Definition at line 874 of file video.cpp.

◆ metrics_reset()

void video::metrics_reset ( )

Reset pipeline metrics at the start of a streaming session.

Definition at line 910 of file video.cpp.

◆ metrics_set_resolution()

void video::metrics_set_resolution ( int width,
int height )

Record the active stream resolution for diagnostics.

Called by the encode loop once the session resolution is known (after the RTSP layer has already reset the counters). Survives the per-window republish so it remains reported for the life of the session.

Parameters
widthActive stream width in pixels.
heightActive stream height in pixels.

Definition at line 915 of file video.cpp.

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

Definition at line 3232 of file video.cpp.

◆ probe_encoders_without_display()

int video::probe_encoders_without_display ( )

Probe encoders without opening any screen.

For when no screen can be captured - every monitor asleep, say. Creating a virtual output instead would wake every monitor the compositor has, so the encoders are validated on a display with nothing behind it.

Returns
0 on success, -1 if no encoder works.

Definition at line 3224 of file video.cpp.

◆ pyrowave_available()

bool video::pyrowave_available ( )

Definition at line 363 of file video.cpp.

◆ pyrowave_frame_budget()

std::size_t video::pyrowave_frame_budget ( const config_t & config)

The most bytes one PyroWave frame may take.

PyroWave's rate control is a per-frame ceiling, so the client's bitrate is spread evenly over its frame rate. It never exceeds an uncompressed 4:2:0 frame, which the encoder has no use for, and never drops below 16 KiB, where every frame would be a smear.

Definition at line 355 of file video.cpp.

◆ refresh_displays() [1/2]

void video::refresh_displays ( platf::mem_type_e dev_type,
std::vector< std::string > & display_names,
int & current_display_index )

Definition at line 1026 of file video.cpp.

◆ refresh_displays() [2/2]

void video::refresh_displays ( platf::mem_type_e dev_type,
std::vector< std::string > & display_names,
int & current_display_index,
std::string & preferred_display_name )

Update the list of display names before or during a stream.

This will attempt to keep current_display_index pointing at the same display.

Parameters
dev_typeThe encoder device type used for display lookup.
display_namesThe list of display names to repopulate.
current_display_indexThe current display index or -1 if not yet known.

Definition at line 975 of file video.cpp.

◆ reset_display()

void video::reset_display ( std::shared_ptr< platf::display_t > & disp,
const platf::mem_type_e & type,
const std::string & display_name,
const config_t & config )

Definition at line 950 of file video.cpp.

◆ start_capture_async()

int video::start_capture_async ( capture_thread_async_ctx_t & ctx)

Definition at line 3502 of file video.cpp.

◆ start_capture_async_with_encoder()

int video::start_capture_async_with_encoder ( capture_thread_async_ctx_t & ctx,
const encoder_t * encoder )

Definition at line 3506 of file video.cpp.

◆ start_capture_sync()

int video::start_capture_sync ( capture_thread_sync_ctx_t & ctx)

Definition at line 3529 of file video.cpp.

◆ vaapi_init_avcodec_hardware_input_buffer()

util::Either< avcodec_buffer_t, int > video::vaapi_init_avcodec_hardware_input_buffer ( platf::avcodec_encode_device_t * encode_device)

Definition at line 3465 of file video.cpp.

◆ validate_config()

int video::validate_config ( std::shared_ptr< platf::display_t > disp,
const encoder_t & encoder,
const config_t & config )

Definition at line 2751 of file video.cpp.

◆ validate_encoder()

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

Definition at line 2803 of file video.cpp.

Variable Documentation

◆ active_av1_mode

int video::active_av1_mode

Definition at line 845 of file video.cpp.

◆ active_hevc_mode

int video::active_hevc_mode

Definition at line 844 of file video.cpp.

◆ capture_thread_async

auto video::capture_thread_async = safe::make_shared<capture_thread_async_ctx_t>(start_capture_async, end_capture_async)

Definition at line 619 of file video.cpp.

◆ capture_thread_sync

auto video::capture_thread_sync = safe::make_shared<capture_thread_sync_ctx_t>(start_capture_sync, end_capture_sync)

Definition at line 620 of file video.cpp.

◆ last_encoder_probe_supported_ref_frames_invalidation

bool video::last_encoder_probe_supported_ref_frames_invalidation = false

Definition at line 940 of file video.cpp.

◆ last_encoder_probe_supported_yuv444_for_codec

std::array< bool, 3 > video::last_encoder_probe_supported_yuv444_for_codec
Initial value:
= {
true,
true,
true
}

Definition at line 941 of file video.cpp.

◆ nvenc

encoder_t video::nvenc

Definition at line 622 of file video.cpp.

◆ probe_without_display

bool video::probe_without_display = false

Definition at line 948 of file video.cpp.

◆ software

encoder_t video::software

Definition at line 710 of file video.cpp.

◆ vaapi

encoder_t video::vaapi

Definition at line 780 of file video.cpp.