aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp2068
1 files changed, 1428 insertions, 640 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 4b84f78..7d4a47e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,13 +6,19 @@ extern "C" {
#include "../include/capture/portal.h"
#include "../include/dbus.h"
#endif
-#include "../include/encoder/video/cuda.h"
+#ifdef GSR_APP_AUDIO
+#include "../include/pipewire_audio.h"
+#endif
+#include "../include/encoder/video/nvenc.h"
#include "../include/encoder/video/vaapi.h"
#include "../include/encoder/video/vulkan.h"
#include "../include/encoder/video/software.h"
-#include "../include/codec_query/cuda.h"
+#include "../include/encoder/video/image.h"
+#include "../include/codec_query/nvenc.h"
#include "../include/codec_query/vaapi.h"
#include "../include/codec_query/vulkan.h"
+#include "../include/window/window_x11.h"
+#include "../include/window/window_wayland.h"
#include "../include/egl.h"
#include "../include/utils.h"
#include "../include/damage.h"
@@ -32,6 +38,7 @@ extern "C" {
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
+#include <inttypes.h>
#include <libgen.h>
#include "../include/sound.hpp"
@@ -105,7 +112,10 @@ enum class VideoCodec {
AV1_10BIT,
VP8,
VP9,
- H264_VULKAN
+ H264_VULKAN,
+ HEVC_VULKAN,
+ JPEG,
+ PNG
};
enum class AudioCodec {
@@ -127,7 +137,8 @@ enum class FramerateMode {
enum class BitrateMode {
QP,
- VBR
+ VBR,
+ CBR
};
static int x11_error_handler(Display*, XErrorEvent*) {
@@ -139,6 +150,7 @@ static int x11_io_error_handler(Display*) {
}
static bool video_codec_is_hdr(VideoCodec video_codec) {
+ // TODO: Vulkan
switch(video_codec) {
case VideoCodec::HEVC_HDR:
case VideoCodec::AV1_HDR:
@@ -149,6 +161,7 @@ static bool video_codec_is_hdr(VideoCodec video_codec) {
}
static VideoCodec hdr_video_codec_to_sdr_video_codec(VideoCodec video_codec) {
+ // TODO: Vulkan
switch(video_codec) {
case VideoCodec::HEVC_HDR:
return VideoCodec::HEVC;
@@ -160,6 +173,7 @@ static VideoCodec hdr_video_codec_to_sdr_video_codec(VideoCodec video_codec) {
}
static gsr_color_depth video_codec_to_bit_depth(VideoCodec video_codec) {
+ // TODO: Vulkan
switch(video_codec) {
case VideoCodec::HEVC_HDR:
case VideoCodec::HEVC_10BIT:
@@ -172,6 +186,7 @@ static gsr_color_depth video_codec_to_bit_depth(VideoCodec video_codec) {
}
// static bool video_codec_is_hevc(VideoCodec video_codec) {
+// TODO: Vulkan
// switch(video_codec) {
// case VideoCodec::HEVC:
// case VideoCodec::HEVC_HDR:
@@ -183,6 +198,7 @@ static gsr_color_depth video_codec_to_bit_depth(VideoCodec video_codec) {
// }
static bool video_codec_is_av1(VideoCodec video_codec) {
+ // TODO: Vulkan
switch(video_codec) {
case VideoCodec::AV1:
case VideoCodec::AV1_HDR:
@@ -193,6 +209,26 @@ static bool video_codec_is_av1(VideoCodec video_codec) {
}
}
+static bool video_codec_is_vulkan(VideoCodec video_codec) {
+ switch(video_codec) {
+ case VideoCodec::H264_VULKAN:
+ case VideoCodec::HEVC_VULKAN:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool video_codec_is_image(VideoCodec video_codec) {
+ switch(video_codec) {
+ case VideoCodec::JPEG:
+ case VideoCodec::PNG:
+ return true;
+ default:
+ return false;
+ }
+}
+
struct PacketData {
PacketData() {}
PacketData(const PacketData&) = delete;
@@ -384,7 +420,7 @@ static AVSampleFormat audio_format_to_sample_format(const AudioFormat audio_form
return AV_SAMPLE_FMT_S16;
}
-static AVCodecContext* create_audio_codec_context(int fps, AudioCodec audio_codec, bool mix_audio, int audio_bitrate) {
+static AVCodecContext* create_audio_codec_context(int fps, AudioCodec audio_codec, bool mix_audio, int64_t audio_bitrate) {
(void)fps;
const AVCodec *codec = avcodec_find_encoder(audio_codec_get_id(audio_codec));
if (!codec) {
@@ -471,7 +507,7 @@ static int vbr_get_quality_parameter(AVCodecContext *codec_context, VideoQuality
static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
VideoQuality video_quality,
int fps, const AVCodec *codec, bool low_latency, gsr_gpu_vendor vendor, FramerateMode framerate_mode,
- bool hdr, gsr_color_range color_range, float keyint, bool use_software_video_encoder, BitrateMode bitrate_mode) {
+ bool hdr, gsr_color_range color_range, float keyint, bool use_software_video_encoder, BitrateMode bitrate_mode, VideoCodec video_codec, int64_t bitrate) {
AVCodecContext *codec_context = avcodec_alloc_context3(codec);
@@ -515,7 +551,13 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
if(codec->id == AV_CODEC_ID_HEVC)
codec_context->codec_tag = MKTAG('h', 'v', 'c', '1'); // QuickTime on MacOS requires this or the video wont be playable
- if(bitrate_mode == BitrateMode::VBR) {
+ if(bitrate_mode == BitrateMode::CBR) {
+ codec_context->bit_rate = bitrate;
+ codec_context->rc_max_rate = codec_context->bit_rate;
+ //codec_context->rc_min_rate = codec_context->bit_rate;
+ codec_context->rc_buffer_size = codec_context->bit_rate;//codec_context->bit_rate / 10;
+ codec_context->rc_initial_buffer_occupancy = 0;//codec_context->bit_rate;//codec_context->bit_rate * 1000;
+ } else if(bitrate_mode == BitrateMode::VBR) {
const int quality = vbr_get_quality_parameter(codec_context, video_quality, hdr);
switch(video_quality) {
case VideoQuality::MEDIUM:
@@ -541,15 +583,32 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
}
codec_context->rc_max_rate = codec_context->bit_rate;
- codec_context->rc_min_rate = codec_context->bit_rate;
+ //codec_context->rc_min_rate = codec_context->bit_rate;
codec_context->rc_buffer_size = codec_context->bit_rate;//codec_context->bit_rate / 10;
- codec_context->rc_initial_buffer_occupancy = 100000;//codec_context->bit_rate * 1000;
+ codec_context->rc_initial_buffer_occupancy = codec_context->bit_rate;//codec_context->bit_rate * 1000;
+ } else {
+ //codec_context->rc_buffer_size = 50000 * 1000;
}
//codec_context->profile = FF_PROFILE_H264_MAIN;
if (codec_context->codec_id == AV_CODEC_ID_MPEG1VIDEO)
codec_context->mb_decision = 2;
- if(!use_software_video_encoder && vendor != GSR_GPU_VENDOR_NVIDIA) {
+ if(video_codec_is_image(video_codec)) {
+ switch(video_quality) {
+ case VideoQuality::MEDIUM:
+ codec_context->compression_level = 8;
+ break;
+ case VideoQuality::HIGH:
+ codec_context->compression_level = 6;
+ break;
+ case VideoQuality::VERY_HIGH:
+ codec_context->compression_level = 4;
+ break;
+ case VideoQuality::ULTRA:
+ codec_context->compression_level = 2;
+ break;
+ }
+ } else if(!use_software_video_encoder && vendor != GSR_GPU_VENDOR_NVIDIA && bitrate_mode != BitrateMode::CBR) {
// 8 bit / 10 bit = 80%, and increase it even more
const float quality_multiply = hdr ? (8.0f/10.0f * 0.7f) : 1.0f;
if(codec_context->codec_id == AV_CODEC_ID_AV1 || codec_context->codec_id == AV_CODEC_ID_H264 || codec_context->codec_id == AV_CODEC_ID_HEVC) {
@@ -561,7 +620,7 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
codec_context->global_quality = 120 * quality_multiply;
break;
case VideoQuality::VERY_HIGH:
- codec_context->global_quality = 100 * quality_multiply;
+ codec_context->global_quality = 115 * quality_multiply;
break;
case VideoQuality::ULTRA:
codec_context->global_quality = 90 * quality_multiply;
@@ -576,7 +635,7 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
codec_context->global_quality = 30 * quality_multiply;
break;
case VideoQuality::VERY_HIGH:
- codec_context->global_quality = 20 * quality_multiply;
+ codec_context->global_quality = 25 * quality_multiply;
break;
case VideoQuality::ULTRA:
codec_context->global_quality = 10 * quality_multiply;
@@ -591,7 +650,7 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
codec_context->global_quality = 30 * quality_multiply;
break;
case VideoQuality::VERY_HIGH:
- codec_context->global_quality = 20 * quality_multiply;
+ codec_context->global_quality = 25 * quality_multiply;
break;
case VideoQuality::ULTRA:
codec_context->global_quality = 10 * quality_multiply;
@@ -606,10 +665,35 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
if(vendor != GSR_GPU_VENDOR_NVIDIA) {
// TODO: More options, better options
//codec_context->bit_rate = codec_context->width * codec_context->height;
- if(bitrate_mode == BitrateMode::QP)
- av_opt_set(codec_context->priv_data, "rc_mode", "CQP", 0);
- else
- av_opt_set(codec_context->priv_data, "rc_mode", "VBR", 0);
+ switch(bitrate_mode) {
+ case BitrateMode::QP: {
+ if(video_codec_is_vulkan(video_codec))
+ av_opt_set(codec_context->priv_data, "rc_mode", "cqp", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_opt_set(codec_context->priv_data, "rc", "constqp", 0);
+ else
+ av_opt_set(codec_context->priv_data, "rc_mode", "CQP", 0);
+ break;
+ }
+ case BitrateMode::VBR: {
+ if(video_codec_is_vulkan(video_codec))
+ av_opt_set(codec_context->priv_data, "rc_mode", "vbr", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_opt_set(codec_context->priv_data, "rc", "vbr", 0);
+ else
+ av_opt_set(codec_context->priv_data, "rc_mode", "VBR", 0);
+ break;
+ }
+ case BitrateMode::CBR: {
+ if(video_codec_is_vulkan(video_codec))
+ av_opt_set(codec_context->priv_data, "rc_mode", "cbr", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_opt_set(codec_context->priv_data, "rc", "cbr", 0);
+ else
+ av_opt_set(codec_context->priv_data, "rc_mode", "CBR", 0);
+ break;
+ }
+ }
//codec_context->global_quality = 4;
//codec_context->compression_level = 2;
}
@@ -659,6 +743,15 @@ static AVFrame* create_audio_frame(AVCodecContext *audio_codec_context) {
return frame;
}
+static void open_video_image(AVCodecContext *codec_context) {
+ AVDictionary *options = nullptr;
+ int ret = avcodec_open2(codec_context, codec_context->codec, &options);
+ if (ret < 0) {
+ fprintf(stderr, "Error: Could not open video codec: %s\n", av_error_to_string(ret));
+ _exit(1);
+ }
+}
+
static void dict_set_profile(AVCodecContext *codec_context, gsr_gpu_vendor vendor, gsr_color_depth color_depth, AVDictionary **options) {
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 17, 100)
if(codec_context->codec_id == AV_CODEC_ID_H264) {
@@ -730,10 +823,10 @@ static void video_software_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 23 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
- av_dict_set_int(options, "qp", 20 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
break;
}
} else {
@@ -761,10 +854,9 @@ static void open_video_software(AVCodecContext *codec_context, VideoQuality vide
if(bitrate_mode == BitrateMode::QP)
video_software_set_qp(codec_context, video_quality, hdr, &options);
- av_dict_set(&options, "preset", "medium", 0);
+ av_dict_set(&options, "preset", "veryfast", 0);
+ av_dict_set(&options, "tune", "film", 0);
dict_set_profile(codec_context, GSR_GPU_VENDOR_INTEL, color_depth, &options);
- // TODO: If streaming or piping output set this to zerolatency
- av_dict_set(&options, "tune", "fastdecode", 0);
if(codec_context->codec_id == AV_CODEC_ID_H264) {
av_dict_set(&options, "coder", "cabac", 0); // TODO: cavlc is faster than cabac but worse compression. Which to use?
@@ -779,6 +871,38 @@ static void open_video_software(AVCodecContext *codec_context, VideoQuality vide
}
}
+static void video_set_rc(VideoCodec video_codec, gsr_gpu_vendor vendor, BitrateMode bitrate_mode, AVDictionary **options) {
+ switch(bitrate_mode) {
+ case BitrateMode::QP: {
+ if(video_codec_is_vulkan(video_codec))
+ av_dict_set(options, "rc_mode", "cqp", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_dict_set(options, "rc", "constqp", 0);
+ else
+ av_dict_set(options, "rc_mode", "CQP", 0);
+ break;
+ }
+ case BitrateMode::VBR: {
+ if(video_codec_is_vulkan(video_codec))
+ av_dict_set(options, "rc_mode", "vbr", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_dict_set(options, "rc", "vbr", 0);
+ else
+ av_dict_set(options, "rc_mode", "VBR", 0);
+ break;
+ }
+ case BitrateMode::CBR: {
+ if(video_codec_is_vulkan(video_codec))
+ av_dict_set(options, "rc_mode", "cbr", 0);
+ else if(vendor == GSR_GPU_VENDOR_NVIDIA)
+ av_dict_set(options, "rc", "cbr", 0);
+ else
+ av_dict_set(options, "rc_mode", "CBR", 0);
+ break;
+ }
+ }
+}
+
static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality video_quality, gsr_gpu_vendor vendor, bool hdr, AVDictionary **options) {
// 8 bit / 10 bit = 80%
const float qp_multiply = hdr ? 8.0f/10.0f : 1.0f;
@@ -793,7 +917,7 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
@@ -802,16 +926,16 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
} else if(codec_context->codec_id == AV_CODEC_ID_H264) {
switch(video_quality) {
case VideoQuality::MEDIUM:
- av_dict_set_int(options, "qp", 34 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
break;
case VideoQuality::HIGH:
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 23 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
- av_dict_set_int(options, "qp", 20 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
break;
}
} else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
@@ -823,7 +947,7 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
@@ -838,31 +962,29 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
break;
}
}
-
- av_dict_set(options, "rc", "constqp", 0);
} else {
if(codec_context->codec_id == AV_CODEC_ID_AV1) {
// Using global_quality option
} else if(codec_context->codec_id == AV_CODEC_ID_H264) {
switch(video_quality) {
case VideoQuality::MEDIUM:
- av_dict_set_int(options, "qp", 34 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
break;
case VideoQuality::HIGH:
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 23 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
- av_dict_set_int(options, "qp", 20 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
break;
}
} else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
@@ -874,7 +996,7 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
@@ -889,35 +1011,38 @@ static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality vi
av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
break;
case VideoQuality::VERY_HIGH:
- av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
+ av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
break;
case VideoQuality::ULTRA:
av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
break;
}
}
-
- av_dict_set(options, "rc_mode", "CQP", 0);
}
}
-static void open_video_hardware(AVCodecContext *codec_context, VideoQuality video_quality, bool very_old_gpu, gsr_gpu_vendor vendor, PixelFormat pixel_format, bool hdr, gsr_color_depth color_depth, BitrateMode bitrate_mode) {
+static void open_video_hardware(AVCodecContext *codec_context, VideoQuality video_quality, bool very_old_gpu, gsr_gpu_vendor vendor, PixelFormat pixel_format, bool hdr, gsr_color_depth color_depth, BitrateMode bitrate_mode, VideoCodec video_codec, bool low_power) {
(void)very_old_gpu;
AVDictionary *options = nullptr;
- if(bitrate_mode == BitrateMode::QP) {
+ if(bitrate_mode == BitrateMode::QP)
video_hardware_set_qp(codec_context, video_quality, vendor, hdr, &options);
- } else {
- if(vendor == GSR_GPU_VENDOR_NVIDIA) {
- av_dict_set(&options, "rc", "vbr", 0);
- } else {
- av_dict_set(&options, "rc_mode", "VBR", 0);
- }
- }
+
+ video_set_rc(video_codec, vendor, bitrate_mode, &options);
// TODO: Enable multipass
+ // TODO: Set "usage" option to "record"/"stream" and "content" option to "rendered" for vulkan encoding
+
if(vendor == GSR_GPU_VENDOR_NVIDIA) {
+ // TODO: These dont seem to be necessary
+ // av_dict_set_int(&options, "zerolatency", 1, 0);
+ // if(codec_context->codec_id == AV_CODEC_ID_AV1) {
+ // av_dict_set(&options, "tune", "ll", 0);
+ // } else if(codec_context->codec_id == AV_CODEC_ID_H264 || codec_context->codec_id == AV_CODEC_ID_HEVC) {
+ // av_dict_set(&options, "preset", "llhq", 0);
+ // av_dict_set(&options, "tune", "ll", 0);
+ // }
av_dict_set(&options, "tune", "hq", 0);
dict_set_profile(codec_context, vendor, color_depth, &options);
@@ -947,7 +1072,8 @@ static void open_video_hardware(AVCodecContext *codec_context, VideoQuality vide
}
} else {
// TODO: More quality options
- //av_dict_set_int(&options, "low_power", 1, 0);
+ if(low_power)
+ av_dict_set_int(&options, "low_power", 1, 0);
// Improves performance but increases vram
//av_dict_set_int(&options, "async_depth", 8, 0);
@@ -980,7 +1106,8 @@ static void open_video_hardware(AVCodecContext *codec_context, VideoQuality vide
static void usage_header() {
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
const char *program_name = inside_flatpak ? "flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder" : "gpu-screen-recorder";
- fprintf(stderr, "usage: %s -w <window_id|monitor|focused|portal> [-c <container_format>] [-s WxH] -f <fps> [-a <audio_input>] [-q <quality>] [-r <replay_buffer_size_sec>] [-k h264|hevc|av1|vp8|vp9|hevc_hdr|av1_hdr|hevc_10bit|av1_10bit|h264_vulkan] [-ac aac|opus|flac] [-ab <bitrate>] [-oc yes|no] [-fm cfr|vfr|content] [-bm auto|qp|vbr] [-cr limited|full] [-df yes|no] [-sc <script_path>] [-cursor yes|no] [-keyint <value>] [-restore-portal-session yes|no] [-portal-session-token-filepath filepath] [-encoder gpu|cpu] [-o <output_file>] [-v yes|no] [--version] [-h|--help]\n", program_name);
+ printf("usage: %s -w <window_id|monitor|focused|portal> [-c <container_format>] [-s WxH] -f <fps> [-a <audio_input>] [-q <quality>] [-r <replay_buffer_size_sec>] [-restart-replay-on-save yes|no] [-k h264|hevc|av1|vp8|vp9|hevc_hdr|av1_hdr|hevc_10bit|av1_10bit] [-ac aac|opus|flac] [-ab <bitrate>] [-oc yes|no] [-fm cfr|vfr|content] [-bm auto|qp|vbr|cbr] [-cr limited|full] [-df yes|no] [-sc <script_path>] [-cursor yes|no] [-keyint <value>] [-restore-portal-session yes|no] [-portal-session-token-filepath filepath] [-encoder gpu|cpu] [-o <output_file>] [--list-capture-options [card_path] [vendor]] [--list-audio-devices] [--list-application-audio] [-v yes|no] [-gl-debug yes|no] [--version] [-h|--help]\n", program_name);
+ fflush(stdout);
}
// TODO: Update with portal info
@@ -988,137 +1115,188 @@ static void usage_full() {
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
const char *program_name = inside_flatpak ? "flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder" : "gpu-screen-recorder";
usage_header();
- fprintf(stderr, "\n");
- fprintf(stderr, "OPTIONS:\n");
- fprintf(stderr, " -w Window id to record, a display (monitor name), \"screen\", \"screen-direct-force\", \"focused\" or \"portal\".\n");
- fprintf(stderr, " If this is \"portal\" then xdg desktop screencast portal with pipewire will be used. Portal option is only available on Wayland.\n");
- fprintf(stderr, " If you select to save the session (token) in the desktop portal capture popup then the session will be saved for the next time you use \"portal\",\n");
- fprintf(stderr, " but the session will be ignored unless you run GPU Screen Recorder with the '-restore-portal-session yes' option.\n");
- fprintf(stderr, " If this is \"screen\" or \"screen-direct-force\" then all monitors are recorded on Nvidia X11. On AMD/Intel or wayland \"screen\" will record the first monitor found.\n");
- fprintf(stderr, " \"screen-direct-force\" is not recommended unless you use a VRR (G-SYNC) monitor on Nvidia X11 and you are aware that using this option can cause games to freeze/crash or other issues because of Nvidia driver issues.\n");
- fprintf(stderr, " \"screen-direct-force\" option is only available on Nvidia X11. VRR works without this option on other systems.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -c Container format for output file, for example mp4, or flv. Only required if no output file is specified or if recording in replay buffer mode.\n");
- fprintf(stderr, " If an output file is specified and -c is not used then the container format is determined from the output filename extension.\n");
- fprintf(stderr, " Only containers that support h264, hevc, av1, vp8 or vp9 are supported, which means that only mp4, mkv, flv, webm (and some others) are supported.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -s The size (area) to record at in the format WxH, for example 1920x1080. This option is only supported (and required) when -w is \"focused\".\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -f Frame rate to record at. Recording will only capture frames at this target frame rate.\n");
- fprintf(stderr, " For constant frame rate mode this option is the frame rate every frame will be captured at and if the capture frame rate is below this target frame rate then the frames will be duplicated.\n");
- fprintf(stderr, " For variable frame rate mode this option is the max frame rate and if the capture frame rate is below this target frame rate then frames will not be duplicated.\n");
- fprintf(stderr, " Content frame rate is similar to variable frame rate mode, except the frame rate will match the frame rate of the captured content when possible, but not capturing above the frame rate set in this -f option.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -a Audio device to record from (pulse audio device). Can be specified multiple times. Each time this is specified a new audio track is added for the specified audio device.\n");
- fprintf(stderr, " A name can be given to the audio input device by prefixing the audio input with <name>/, for example \"dummy/alsa_output.pci-0000_00_1b.0.analog-stereo.monitor\".\n");
- fprintf(stderr, " Multiple audio devices can be merged into one audio track by using \"|\" as a separator into one -a argument, for example: -a \"alsa_output1|alsa_output2\".\n");
- fprintf(stderr, " The audio device can also be \"default_output\" in which case the default output device is used, or \"default_input\" in which case the default input device is used.\n");
- fprintf(stderr, " If the audio device is an empty string then the audio device is ignored.\n");
- fprintf(stderr, " Optional, no audio track is added by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -q Video quality. Should be either 'medium', 'high', 'very_high' or 'ultra'. 'high' is the recommended option when live streaming or when you have a slower harddrive.\n");
- fprintf(stderr, " Optional, set to 'very_high' be default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -r Replay buffer size in seconds. If this is set, then only the last seconds as set by this option will be stored\n");
- fprintf(stderr, " and the video will only be saved when the gpu-screen-recorder is closed. This feature is similar to Nvidia's instant replay feature.\n");
- fprintf(stderr, " This option has be between 5 and 1200. Note that the replay buffer size will not always be precise, because of keyframes. Optional, disabled by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -k Video codec to use. Should be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit', 'av1_10bit' or 'h264_vulkan'.\n");
- fprintf(stderr, " Optional, set to 'auto' by default which defaults to 'h264'. Forcefully set to 'h264' if the file container type is 'flv'.\n");
- fprintf(stderr, " 'hevc_hdr' and 'av1_hdr' option is not available on X11 nor when using the portal capture option.\n");
- fprintf(stderr, " 'hevc_10bit' and 'av1_10bit' options allow you to select 10 bit color depth which can reduce banding and improve quality in darker areas, but not all video players support 10 bit color depth\n");
- fprintf(stderr, " and if you upload the video to a website the website might reduce 10 bit to 8 bit.\n");
- fprintf(stderr, " Note that when using 'hevc_hdr' or 'av1_hdr' the color depth is also 10 bits.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -ac Audio codec to use. Should be either 'aac', 'opus' or 'flac'. Optional, set to 'opus' for .mp4/.mkv files, otherwise set to 'aac'.\n");
- fprintf(stderr, " 'opus' and 'flac' is only supported by .mp4/.mkv files. 'opus' is recommended for best performance and smallest audio size.\n");
- fprintf(stderr, " Flac audio codec is option is disable at the moment because of a temporary issue.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -ab Audio bitrate to use. If this is set to 0 then it's the same as if it's absent, in which case the bitrate is determined automatically depending on the audio codec.\n");
- fprintf(stderr, " Optional, by default the bitrate is 128000 for opus and flac and 160000 for aac.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -oc Overclock memory transfer rate to the maximum performance level. This only applies to NVIDIA on X11 and exists to overcome a bug in NVIDIA driver where performance level\n");
- fprintf(stderr, " is dropped when you record a game. Only needed if you are recording a game that is bottlenecked by GPU. The same issue exists on Wayland but overclocking is not possible on Wayland.\n");
- fprintf(stderr, " Works only if your have \"Coolbits\" set to \"12\" in NVIDIA X settings, see README for more information. Note! use at your own risk! Optional, disabled by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -fm Framerate mode. Should be either 'cfr' (constant frame rate), 'vfr' (variable frame rate) or 'content'. Optional, set to 'vfr' by default.\n");
- fprintf(stderr, " 'vfr' is recommended for recording for less issue with very high system load but some applications such as video editors may not support it properly.\n");
- fprintf(stderr, " 'content' is currently only supported on X11 or when using portal capture option. The 'content' option matches the recording frame rate to the captured content.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -bm Bitrate mode. Should be either 'auto', 'qp' (constant quality) or 'vbr' (variable bitrate). Optional, set to 'auto' by default which defaults to 'qp' on all devices\n");
- fprintf(stderr, " except steam deck that has broken drivers and doesn't support qp.\n");
- fprintf(stderr, " 'vbr' option is not supported when using '-encoder cpu' option.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -cr Color range. Should be either 'limited' (aka mpeg) or 'full' (aka jpeg). Optional, set to 'limited' by default.\n");
- fprintf(stderr, " Limited color range means that colors are in range 16-235 (4112-60395 for hdr) while full color range means that colors are in range 0-255 (0-65535 for hdr).\n");
- fprintf(stderr, " Note that some buggy video players (such as vlc) are unable to correctly display videos in full color range and when upload the video to websites the website\n");
- fprintf(stderr, " might re-encoder the video to make the video limited color range.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -df Organise replays in folders based on the current date.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -sc Run a script on the saved video file (asynchronously). The first argument to the script is the filepath to the saved video file and the second argument is the recording type (either \"regular\" or \"replay\").\n");
- fprintf(stderr, " Not applicable for live streams.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -cursor\n");
- fprintf(stderr, " Record cursor. Optional, set to 'yes' by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -keyint\n");
- fprintf(stderr, " Specifies the keyframe interval in seconds, the max amount of time to wait to generate a keyframe. Keyframes can be generated more often than this.\n");
- fprintf(stderr, " This also affects seeking in the video and may affect how the replay video is cut. If this is set to 10 for example then you can only seek in 10-second chunks in the video.\n");
- fprintf(stderr, " Setting this to a higher value reduces the video file size if you are ok with the previously described downside. This option is expected to be a floating point number.\n");
- fprintf(stderr, " By default this value is set to 2.0.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -restore-portal-session\n");
- fprintf(stderr, " If GPU Screen Recorder should use the same capture option as the last time. Using this option removes the popup asking what you want to record the next time you record with '-w portal' if you selected the option to save session (token) in the desktop portal screencast popup.\n");
- fprintf(stderr, " This option may not have any effect on your Wayland compositor and your systems desktop portal needs to support ScreenCast version 5 or later. Optional, set to 'no' by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -portal-session-token-filepath\n");
- fprintf(stderr, " This option is used together with -restore-portal-session option to specify the file path to save/restore the portal session token to/from.\n");
- fprintf(stderr, " This can be used to remember different portal capture options depending on different recording option (such as recording/replay).\n");
- fprintf(stderr, " Optional, set to \"$XDG_CONFIG_HOME/gpu-screen-recorder/restore_token\" by default ($XDG_CONFIG_HOME defaults to \"$HOME/.config\").\n");
- fprintf(stderr, " Note: the directory to the portal session token file is created automatically if it doesn't exist.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -encoder\n");
- fprintf(stderr, " Which device should be used for video encoding. Should either be 'gpu' or 'cpu'. Does currently only work with h264 codec option (-k).\n");
- fprintf(stderr, " Optional, set to 'gpu' by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " --info\n");
- fprintf(stderr, " List info about the system (for use by GPU Screen Recorder UI). Lists the following information (prints them to stdout and exits):\n");
- fprintf(stderr, " Supported video codecs (h264, h264_software, hevc, hevc_hdr, hevc_10bit, av1, av1_hdr, av1_10bit, vp8, vp9, h264_vulkan (if supported)).\n");
- fprintf(stderr, " Supported capture options (window, focused, screen, monitors and portal, if supported by the system).\n");
- fprintf(stderr, " If opengl initialization fails then the program exits with 22, if no usable drm device is found then it exits with 23. On success it exits with 0.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " --list-audio-devices\n");
- fprintf(stderr, " List audio devices (for use by GPU Screen Recorder UI). Lists audio devices in the following format (prints them to stdout and exits):\n");
- fprintf(stderr, " <audio_device_name>|<audio_device_name_in_human_readable_format>\n");
- fprintf(stderr, " For example:\n");
- fprintf(stderr, " bluez_input.88:C9:E8:66:A2:27|WH-1000XM4\n");
- fprintf(stderr, " The <audio_device_name> is the name to pass to GPU Screen Recorder in a -a option.\n");
- fprintf(stderr, " --version\n");
- fprintf(stderr, " Print version (%s) and exit\n", GSR_VERSION);
- fprintf(stderr, "\n");
+ printf("\n");
+ printf("OPTIONS:\n");
+ printf(" -w Window id to record, a display (monitor name), \"screen\", \"screen-direct\", \"focused\" or \"portal\".\n");
+ printf(" If this is \"portal\" then xdg desktop screencast portal with PipeWire will be used. Portal option is only available on Wayland.\n");
+ printf(" If you select to save the session (token) in the desktop portal capture popup then the session will be saved for the next time you use \"portal\",\n");
+ printf(" but the session will be ignored unless you run GPU Screen Recorder with the '-restore-portal-session yes' option.\n");
+ printf(" If this is \"screen\" then the first monitor found is recorded.\n");
+ printf(" \"screen-direct\" can only be used on Nvidia X11, to allow recording without breaking VRR (G-SYNC). This also records all of your monitors.\n");
+ printf(" Using this \"screen-direct\" option is not recommended unless you use VRR (G-SYNC) as there are Nvidia driver issues that can cause your system or games to freeze/crash.\n");
+ printf(" The \"screen-direct\" option is not needed on AMD, Intel nor Nvidia on Wayland as VRR works properly in those cases.\n");
+ printf(" Run GPU Screen Recorder with the --list-capture-options option to list valid values for this option.\n");
+ printf("\n");
+ printf(" -c Container format for output file, for example mp4, or flv. Only required if no output file is specified or if recording in replay buffer mode.\n");
+ printf(" If an output file is specified and -c is not used then the container format is determined from the output filename extension.\n");
+ printf(" Only containers that support h264, hevc, av1, vp8 or vp9 are supported, which means that only mp4, mkv, flv, webm (and some others) are supported.\n");
+ printf("\n");
+ printf(" -s The output resolution limit of the video in the format WxH, for example 1920x1080. If this is 0x0 then the original resolution is used. Optional, except when -w is \"focused\".\n");
+ printf(" Note: the captured content is scaled to this size. The output resolution might not be exactly as specified by this option. The original aspect ratio is respected so the resolution will match that.\n");
+ printf(" The video encoder might also need to add padding, which will result in black bars on the sides of the video. This is especially an issue on AMD.\n");
+ printf("\n");
+ printf(" -f Frame rate to record at. Recording will only capture frames at this target frame rate.\n");
+ printf(" For constant frame rate mode this option is the frame rate every frame will be captured at and if the capture frame rate is below this target frame rate then the frames will be duplicated.\n");
+ printf(" For variable frame rate mode this option is the max frame rate and if the capture frame rate is below this target frame rate then frames will not be duplicated.\n");
+ printf(" Content frame rate is similar to variable frame rate mode, except the frame rate will match the frame rate of the captured content when possible, but not capturing above the frame rate set in this -f option.\n");
+ printf("\n");
+ printf(" -a Audio device or application to record from (pulse audio device). Can be specified multiple times. Each time this is specified a new audio track is added for the specified audio device or application.\n");
+ printf(" The audio device can also be \"default_output\" in which case the default output device is used, or \"default_input\" in which case the default input device is used.\n");
+ printf(" Multiple audio sources can be merged into one audio track by using \"|\" as a separator into one -a argument, for example: -a \"default_output|default_input\".\n");
+ printf(" A name can be given to the audio track by prefixing the audio with <name>/, for example \"track name/default_output\" or \"track name/default_output|default_input\".\n");
+ printf(" The audio name can also be prefixed with \"device:\", for example: -a \"device:default_output\".\n");
+ printf(" To record audio from an application then prefix the audio name with \"app:\", for example: -a \"app:Brave\". The application name is case-insensitive.\n");
+ printf(" To record audio from all applications except the provided ones prefix the audio name with \"app-inverse:\", for example: -a \"app-inverse:Brave\".\n");
+ printf(" \"app:\" and \"app-inverse:\" can't be mixed in one audio track.\n");
+ printf(" One audio track can contain both audio devices and application audio, for example: -a \"default_output|device:alsa_output.pci-0000_00_1b.0.analog-stereo.monitor|app:Brave\".\n");
+ printf(" Recording application audio is only possible when the sound server on the system is PipeWire.\n");
+ printf(" If the audio name is an empty string then the argument is ignored.\n");
+ printf(" Optional, no audio track is added by default.\n");
+ printf(" Run GPU Screen Recorder with the --list-audio-devices option to list valid audio device names.\n");
+ printf(" Run GPU Screen Recorder with the --list-application-audio option to list valid application names. It's possible to use an application name that is not listed in --list-application-audio,\n");
+ printf(" for example when trying to record audio from an application that hasn't started yet.\n");
+ printf("\n");
+ printf(" -q Video quality. Should be either 'medium', 'high', 'very_high' or 'ultra' when using '-bm qp' or '-bm vbr' options, and '-bm qp' is the default option used.\n");
+ printf(" 'high' is the recommended option when live streaming or when you have a slower harddrive.\n");
+ printf(" When using '-bm cbr' option then this is option is instead used to specify the video bitrate in kbps.\n");
+ printf(" Optional when using '-bm qp' or '-bm vbr' options, set to 'very_high' be default.\n");
+ printf(" Required when using '-bm cbr' option.\n");
+ printf("\n");
+ printf(" -r Replay buffer time in seconds. If this is set, then only the last seconds as set by this option will be stored\n");
+ printf(" and the video will only be saved when the gpu-screen-recorder is closed. This feature is similar to Nvidia's instant replay feature This option has be between 5 and 1200.\n");
+ printf(" Note that the video data is stored in RAM, so don't use too long replay buffer time and use constant bitrate option (-bm cbr) to prevent RAM usage from going too high in busy scenes.\n");
+ printf(" Optional, disabled by default.\n");
+ printf("\n");
+ printf(" -restart-replay-on-save\n");
+ printf(" Restart replay on save. For example if this is set to 'no' and replay time (-r) is set to 60 seconds and a replay is saved once then the first replay video is 60 seconds long\n");
+ printf(" and if a replay is saved 10 seconds later then the second replay video will also be 60 seconds long and contain 50 seconds of the previous video as well.\n");
+ printf(" If this is set to 'yes' then after a replay is saved the replay buffer data is cleared and the second replay will start from that point onward.\n");
+ printf(" Optional, set to 'no' by default.\n");
+ printf("\n");
+ printf(" -k Video codec to use. Should be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit' or 'av1_10bit'.\n");
+ printf(" Optional, set to 'auto' by default which defaults to 'h264'. Forcefully set to 'h264' if the file container type is 'flv'.\n");
+ printf(" 'hevc_hdr' and 'av1_hdr' option is not available on X11 nor when using the portal capture option.\n");
+ printf(" 'hevc_10bit' and 'av1_10bit' options allow you to select 10 bit color depth which can reduce banding and improve quality in darker areas, but not all video players support 10 bit color depth\n");
+ printf(" and if you upload the video to a website the website might reduce 10 bit to 8 bit.\n");
+ printf(" Note that when using 'hevc_hdr' or 'av1_hdr' the color depth is also 10 bits.\n");
+ printf("\n");
+ printf(" -ac Audio codec to use. Should be either 'aac', 'opus' or 'flac'. Optional, set to 'opus' for .mp4/.mkv files, otherwise set to 'aac'.\n");
+ printf(" 'opus' and 'flac' is only supported by .mp4/.mkv files. 'opus' is recommended for best performance and smallest audio size.\n");
+ printf(" Flac audio codec is option is disable at the moment because of a temporary issue.\n");
+ printf("\n");
+ printf(" -ab Audio bitrate in kbps. If this is set to 0 then it's the same as if it's absent, in which case the bitrate is determined automatically depending on the audio codec.\n");
+ printf(" Optional, by default the bitrate is 128kbps for opus and flac and 160kbps for aac.\n");
+ printf("\n");
+ printf(" -oc Overclock memory transfer rate to the maximum performance level. This only applies to NVIDIA on X11 and exists to overcome a bug in NVIDIA driver where performance level\n");
+ printf(" is dropped when you record a game. Only needed if you are recording a game that is bottlenecked by GPU. The same issue exists on Wayland but overclocking is not possible on Wayland.\n");
+ printf(" Works only if your have \"Coolbits\" set to \"12\" in NVIDIA X settings, see README for more information. Note! use at your own risk! Optional, disabled by default.\n");
+ printf("\n");
+ printf(" -fm Framerate mode. Should be either 'cfr' (constant frame rate), 'vfr' (variable frame rate) or 'content'. Optional, set to 'vfr' by default.\n");
+ printf(" 'vfr' is recommended for recording for less issue with very high system load but some applications such as video editors may not support it properly.\n");
+ printf(" 'content' is currently only supported on X11 or when using portal capture option. The 'content' option matches the recording frame rate to the captured content.\n");
+ printf("\n");
+ printf(" -bm Bitrate mode. Should be either 'auto', 'qp' (constant quality), 'vbr' (variable bitrate) or 'cbr' (constant bitrate). Optional, set to 'auto' by default which defaults to 'qp' on all devices\n");
+ printf(" except steam deck that has broken drivers and doesn't support qp.\n");
+ printf(" Note: 'vbr' option is not supported when using '-encoder cpu' option.\n");
+ printf("\n");
+ printf(" -cr Color range. Should be either 'limited' (aka mpeg) or 'full' (aka jpeg). Optional, set to 'limited' by default.\n");
+ printf(" Limited color range means that colors are in range 16-235 (4112-60395 for hdr) while full color range means that colors are in range 0-255 (0-65535 for hdr).\n");
+ printf(" Note that some buggy video players (such as vlc) are unable to correctly display videos in full color range and when upload the video to websites the website\n");
+ printf(" might re-encoder the video to make the video limited color range.\n");
+ printf("\n");
+ printf(" -df Organise replays in folders based on the current date.\n");
+ printf("\n");
+ printf(" -sc Run a script on the saved video file (asynchronously). The first argument to the script is the filepath to the saved video file and the second argument is the recording type (either \"regular\" or \"replay\").\n");
+ printf(" Not applicable for live streams.\n");
+ printf("\n");
+ printf(" -cursor\n");
+ printf(" Record cursor. Optional, set to 'yes' by default.\n");
+ printf("\n");
+ printf(" -keyint\n");
+ printf(" Specifies the keyframe interval in seconds, the max amount of time to wait to generate a keyframe. Keyframes can be generated more often than this.\n");
+ printf(" This also affects seeking in the video and may affect how the replay video is cut. If this is set to 10 for example then you can only seek in 10-second chunks in the video.\n");
+ printf(" Setting this to a higher value reduces the video file size if you are ok with the previously described downside. This option is expected to be a floating point number.\n");
+ printf(" By default this value is set to 2.0.\n");
+ printf("\n");
+ printf(" -restore-portal-session\n");
+ printf(" If GPU Screen Recorder should use the same capture option as the last time. Using this option removes the popup asking what you want to record the next time you record with '-w portal' if you selected the option to save session (token) in the desktop portal screencast popup.\n");
+ printf(" This option may not have any effect on your Wayland compositor and your systems desktop portal needs to support ScreenCast version 5 or later. Optional, set to 'no' by default.\n");
+ printf("\n");
+ printf(" -portal-session-token-filepath\n");
+ printf(" This option is used together with -restore-portal-session option to specify the file path to save/restore the portal session token to/from.\n");
+ printf(" This can be used to remember different portal capture options depending on different recording option (such as recording/replay).\n");
+ printf(" Optional, set to \"$XDG_CONFIG_HOME/gpu-screen-recorder/restore_token\" by default ($XDG_CONFIG_HOME defaults to \"$HOME/.config\").\n");
+ printf(" Note: the directory to the portal session token file is created automatically if it doesn't exist.\n");
+ printf("\n");
+ printf(" -encoder\n");
+ printf(" Which device should be used for video encoding. Should either be 'gpu' or 'cpu'. 'cpu' option currently only work with h264 codec option (-k).\n");
+ printf(" Optional, set to 'gpu' by default.\n");
+ printf("\n");
+ printf(" --info\n");
+ printf(" List info about the system. Lists the following information (prints them to stdout and exits):\n");
+ printf(" Supported video codecs (h264, h264_software, hevc, hevc_hdr, hevc_10bit, av1, av1_hdr, av1_10bit, vp8, vp9) and image codecs (jpeg, png) (if supported).\n");
+ printf(" Supported capture options (window, focused, screen, monitors and portal, if supported by the system).\n");
+ printf(" If opengl initialization fails then the program exits with 22, if no usable drm device is found then it exits with 23. On success it exits with 0.\n");
+ printf("\n");
+ printf(" --list-capture-options\n");
+ printf(" List available capture options. Lists capture options in the following format (prints them to stdout and exits):\n");
+ printf(" <option>\n");
+ printf(" <monitor_name>|<resolution>\n");
+ printf(" For example:\n");
+ printf(" window\n");
+ printf(" DP-1|1920x1080\n");
+ printf(" The <option> and <monitor_name> is the name that can be passed to GPU Screen Recorder with the -w option.\n");
+ printf(" --list-capture-options optionally accepts a card path (\"/dev/dri/cardN\") and vendor (\"amd\", \"intel\" or \"nvidia\") which can improve the performance of running this command.\n");
+ printf("\n");
+ printf(" --list-audio-devices\n");
+ printf(" List audio devices. Lists audio devices in the following format (prints them to stdout and exits):\n");
+ printf(" <audio_device_name>|<audio_device_name_in_human_readable_format>\n");
+ printf(" For example:\n");
+ printf(" bluez_input.88:C9:E8:66:A2:27|WH-1000XM4\n");
+ printf(" alsa_output.pci-0000_0c_00.4.iec958-stereo|Monitor of Starship/Matisse HD Audio Controller Digital Stereo (IEC958)\n");
+ printf(" The <audio_device_name> is the name that can be passed to GPU Screen Recorder with the -a option.\n");
+ printf("\n");
+ printf(" --list-application-audio\n");
+ printf(" Lists applications that you can record from (prints them to stdout and exits), for example:\n");
+ printf(" firefox\n");
+ printf(" csgo\n");
+ printf(" These names are the application audio names that can be passed to GPU Screen Recorder with the -a option.\n");
+ printf("\n");
+ printf(" --version\n");
+ printf(" Print version (%s) and exit\n", GSR_VERSION);
+ printf("\n");
//fprintf(stderr, " -pixfmt The pixel format to use for the output video. yuv420 is the most common format and is best supported, but the color is compressed, so colors can look washed out and certain colors of text can look bad. Use yuv444 for no color compression, but the video may not work everywhere and it may not work with hardware video decoding. Optional, set to 'yuv420' by default\n");
- fprintf(stderr, " -o The output file path. If omitted then the encoded data is sent to stdout. Required in replay mode (when using -r).\n");
- fprintf(stderr, " In replay mode this has to be a directory instead of a file.\n");
- fprintf(stderr, " Note: the directory to the file is created automatically if it doesn't already exist.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -v Prints per second, fps updates. Optional, set to 'yes' by default.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, " -h, --help\n");
- fprintf(stderr, " Show this help.\n");
- fprintf(stderr, "\n");
- fprintf(stderr, "NOTES:\n");
- fprintf(stderr, " Send signal SIGINT to gpu-screen-recorder (Ctrl+C, or killall -SIGINT gpu-screen-recorder) to stop and save the recording. When in replay mode this stops recording without saving.\n");
- fprintf(stderr, " Send signal SIGUSR1 to gpu-screen-recorder (killall -SIGUSR1 gpu-screen-recorder) to save a replay (when in replay mode).\n");
- fprintf(stderr, " Send signal SIGUSR2 to gpu-screen-recorder (killall -SIGUSR2 gpu-screen-recorder) to pause/unpause recording. Only applicable and useful when recording (not streaming nor replay).\n");
- fprintf(stderr, "\n");
- fprintf(stderr, "EXAMPLES:\n");
- fprintf(stderr, " %s -w screen -f 60 -a default_output -o \"$HOME/Videos/video.mp4\"\n", program_name);
- fprintf(stderr, " %s -w screen -f 60 -a \"default_output|default_input\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
- fprintf(stderr, " %s -w screen -f 60 -a default_output -c mkv -r 60 -o \"$HOME/Videos\"\n", program_name);
- fprintf(stderr, " %s -w screen -f 60 -a default_output -c mkv -sc script.sh -r 60 -o \"$HOME/Videos\"\n", program_name);
- fprintf(stderr, " %s -w portal -f 60 -a default_output -restore-portal-session yes -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" -o The output file path. If omitted then the encoded data is sent to stdout. Required in replay mode (when using -r).\n");
+ printf(" In replay mode this has to be a directory instead of a file.\n");
+ printf(" Note: the directory to the file is created automatically if it doesn't already exist.\n");
+ printf("\n");
+ printf(" -v Prints fps and damage info once per second. Optional, set to 'yes' by default.\n");
+ printf("\n");
+ printf(" -gl-debug\n");
+ printf(" Print opengl debug output. Optional, set to 'no' by default.\n");
+ printf("\n");
+ printf(" -h, --help\n");
+ printf(" Show this help.\n");
+ printf("\n");
+ printf("NOTES:\n");
+ printf(" Send signal SIGINT to gpu-screen-recorder (Ctrl+C, or killall -SIGINT gpu-screen-recorder) to stop and save the recording. When in replay mode this stops recording without saving.\n");
+ printf(" Send signal SIGUSR1 to gpu-screen-recorder (killall -SIGUSR1 gpu-screen-recorder) to save a replay (when in replay mode).\n");
+ printf(" Send signal SIGUSR2 to gpu-screen-recorder (killall -SIGUSR2 gpu-screen-recorder) to pause/unpause recording. Only applicable and useful when recording (not streaming nor replay).\n");
+ printf("\n");
+ printf("EXAMPLES:\n");
+ printf(" %s -w screen -f 60 -a default_output -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a default_output -a default_input -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a \"default_output|default_input\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a default_output -c mkv -r 60 -o \"$HOME/Videos\"\n", program_name);
+ printf(" %s -w screen -f 60 -a default_output -c mkv -sc script.sh -r 60 -o \"$HOME/Videos\"\n", program_name);
+ printf(" %s -w portal -f 60 -a default_output -restore-portal-session yes -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a default_output -bm cbr -q 15000 -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a \"app:firefox|app:csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a \"app-inverse:firefox|app-inverse:csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -a \"default-input|app-inverse:Brave\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
+ printf(" %s -w screen -f 60 -o \"$HOME/Pictures/image.jpg\"\n", program_name);
//fprintf(stderr, " gpu-screen-recorder -w screen -f 60 -q ultra -pixfmt yuv444 -o video.mp4\n");
+ fflush(stdout);
_exit(1);
}
@@ -1175,7 +1353,7 @@ static std::string get_date_str() {
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(str, sizeof(str)-1, "%Y-%m-%d_%H-%M-%S", t);
- return str;
+ return str;
}
static std::string get_date_only_str() {
@@ -1214,16 +1392,17 @@ static void run_recording_saved_script_async(const char *script_file, const char
return;
}
- const char *args[6];
+ const char *args[7];
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
if(inside_flatpak) {
args[0] = "flatpak-spawn";
args[1] = "--host";
- args[2] = script_file_full;
- args[3] = video_file;
- args[4] = type;
- args[5] = NULL;
+ args[2] = "--";
+ args[3] = script_file_full;
+ args[4] = video_file;
+ args[5] = type;
+ args[6] = NULL;
} else {
args[0] = script_file_full;
args[1] = video_file;
@@ -1268,7 +1447,7 @@ static double audio_codec_get_desired_delay(AudioCodec audio_codec, int fps) {
return std::max(0.0, base - fps_inv);
}
-struct AudioDevice {
+struct AudioDeviceData {
SoundDevice sound_device;
AudioInput audio_input;
AVFilterContext *src_filter_ctx = nullptr;
@@ -1278,10 +1457,11 @@ struct AudioDevice {
// TODO: Cleanup
struct AudioTrack {
+ std::string name;
AVCodecContext *codec_context = nullptr;
AVStream *stream = nullptr;
- std::vector<AudioDevice> audio_devices;
+ std::vector<AudioDeviceData> audio_devices;
AVFilterGraph *graph = nullptr;
AVFilterContext *sink = nullptr;
int stream_index = 0;
@@ -1301,34 +1481,41 @@ static bool add_hdr_metadata_to_video_stream(gsr_capture *cap, AVStream *video_s
if(!light_metadata || !mastering_display_metadata) {
if(light_metadata)
- av_freep(light_metadata);
+ av_freep(&light_metadata);
if(mastering_display_metadata)
- av_freep(mastering_display_metadata);
+ av_freep(&mastering_display_metadata);
return false;
}
if(!gsr_capture_set_hdr_metadata(cap, mastering_display_metadata, light_metadata)) {
- av_freep(light_metadata);
- av_freep(mastering_display_metadata);
+ av_freep(&light_metadata);
+ av_freep(&mastering_display_metadata);
return false;
}
// TODO: More error checking
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
- av_stream_add_side_data(video_stream, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, (uint8_t*)light_metadata, light_metadata_size);
+ const bool content_light_level_added = av_stream_add_side_data(video_stream, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, (uint8_t*)light_metadata, light_metadata_size) == 0;
#else
- av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, light_metadata, light_metadata_size, 0);
+ const bool content_light_level_added = av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, light_metadata, light_metadata_size, 0) != NULL;
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
- av_stream_add_side_data(video_stream, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, (uint8_t*)mastering_display_metadata, mastering_display_metadata_size);
+ const bool mastering_display_metadata_added = av_stream_add_side_data(video_stream, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, (uint8_t*)mastering_display_metadata, mastering_display_metadata_size) == 0;
#else
- av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, mastering_display_metadata, mastering_display_metadata_size, 0);
+ const bool mastering_display_metadata_added = av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, mastering_display_metadata, mastering_display_metadata_size, 0) != NULL;
#endif
+ if(!content_light_level_added)
+ av_freep(&light_metadata);
+
+ if(!mastering_display_metadata_added)
+ av_freep(&mastering_display_metadata);
+
+ // Return true even on failure because we dont want to retry adding hdr metadata on failure
return true;
}
@@ -1339,7 +1526,7 @@ static std::string save_replay_output_filepath;
static void save_replay_async(AVCodecContext *video_codec_context, int video_stream_index, std::vector<AudioTrack> &audio_tracks, std::deque<std::shared_ptr<PacketData>> &frame_data_queue, bool frames_erased, std::string output_dir, const char *container_format, const std::string &file_extension, std::mutex &write_output_mutex, bool date_folders, bool hdr, gsr_capture *capture) {
if(save_replay_thread.valid())
return;
-
+
size_t start_index = (size_t)-1;
int64_t video_pts_offset = 0;
int64_t audio_pts_offset = 0;
@@ -1360,7 +1547,7 @@ static void save_replay_async(AVCodecContext *video_codec_context, int video_str
if(frames_erased) {
video_pts_offset = frame_data_queue[start_index]->data.pts;
-
+
// Find the next audio packet to use as audio pts offset
for(size_t i = start_index; i < frame_data_queue.size(); ++i) {
const AVPacket &av_packet = frame_data_queue[i]->data;
@@ -1398,6 +1585,8 @@ static void save_replay_async(AVCodecContext *video_codec_context, int video_str
for(AudioTrack &audio_track : audio_tracks) {
stream_index_to_audio_track_map[audio_track.stream_index] = &audio_track;
AVStream *audio_stream = create_stream(av_format_context, audio_track.codec_context);
+ if(!audio_track.name.empty())
+ av_dict_set(&audio_stream->metadata, "title", audio_track.name.c_str(), 0);
avcodec_parameters_from_context(audio_stream->codecpar, audio_track.codec_context);
audio_track.stream = audio_stream;
}
@@ -1489,20 +1678,68 @@ static void split_string(const std::string &str, char delimiter, std::function<b
}
}
-static std::vector<AudioInput> parse_audio_input_arg(const char *str) {
- std::vector<AudioInput> audio_inputs;
- split_string(str, '|', [&audio_inputs](const char *sub, size_t size) {
+static bool string_starts_with(const std::string &str, const char *substr) {
+ int len = strlen(substr);
+ return (int)str.size() >= len && memcmp(str.data(), substr, len) == 0;
+}
+
+static bool string_ends_with(const char *str, const char *substr) {
+ int str_len = strlen(str);
+ int substr_len = strlen(substr);
+ return str_len >= substr_len && memcmp(str + str_len - substr_len, substr, substr_len) == 0;
+}
+
+static const AudioDevice* get_audio_device_by_name(const std::vector<AudioDevice> &audio_devices, const char *name) {
+ for(const auto &audio_device : audio_devices) {
+ if(strcmp(audio_device.name.c_str(), name) == 0)
+ return &audio_device;
+ }
+ return nullptr;
+}
+
+static MergedAudioInputs parse_audio_input_arg(const char *str, const AudioDevices &audio_devices) {
+ MergedAudioInputs result;
+ const bool name_is_existing_audio_device = get_audio_device_by_name(audio_devices.audio_inputs, str) != nullptr;
+ if(name_is_existing_audio_device) {
+ result.audio_inputs.push_back({str, AudioInputType::DEVICE, false});
+ return result;
+ }
+
+ const char *track_name_sep_ptr = strchr(str, '/');
+ if(track_name_sep_ptr) {
+ result.track_name.assign(str, track_name_sep_ptr - str);
+ str = track_name_sep_ptr + 1;
+ }
+
+ split_string(str, '|', [&](const char *sub, size_t size) {
AudioInput audio_input;
audio_input.name.assign(sub, size);
- const size_t index = audio_input.name.find('/');
- if(index != std::string::npos) {
- audio_input.description = audio_input.name.substr(0, index);
- audio_input.name.erase(audio_input.name.begin(), audio_input.name.begin() + index + 1);
+
+ if(string_starts_with(audio_input.name.c_str(), "app:")) {
+ audio_input.name.erase(audio_input.name.begin(), audio_input.name.begin() + 4);
+ audio_input.type = AudioInputType::APPLICATION;
+ audio_input.inverted = false;
+ result.audio_inputs.push_back(std::move(audio_input));
+ return true;
+ } else if(string_starts_with(audio_input.name.c_str(), "app-inverse:")) {
+ audio_input.name.erase(audio_input.name.begin(), audio_input.name.begin() + 12);
+ audio_input.type = AudioInputType::APPLICATION;
+ audio_input.inverted = true;
+ result.audio_inputs.push_back(std::move(audio_input));
+ return true;
+ } else if(string_starts_with(audio_input.name.c_str(), "device:")) {
+ audio_input.name.erase(audio_input.name.begin(), audio_input.name.begin() + 7);
+ audio_input.type = AudioInputType::DEVICE;
+ result.audio_inputs.push_back(std::move(audio_input));
+ return true;
+ } else {
+ audio_input.type = AudioInputType::DEVICE;
+ result.audio_inputs.push_back(std::move(audio_input));
+ return true;
}
- audio_inputs.push_back(std::move(audio_input));
- return true;
});
- return audio_inputs;
+
+ return result;
}
// TODO: Does this match all livestreaming cases?
@@ -1524,31 +1761,46 @@ static bool is_livestream_path(const char *str) {
return false;
}
-// TODO: Proper cleanup
-static int init_filter_graph(AVCodecContext *audio_codec_context, AVFilterGraph **graph, AVFilterContext **sink, std::vector<AVFilterContext*> &src_filter_ctx, size_t num_sources) {
+static int init_filter_graph(AVCodecContext* audio_codec_context, AVFilterGraph** graph, AVFilterContext** sink, std::vector<AVFilterContext*>& src_filter_ctx, size_t num_sources) {
char ch_layout[64];
int err = 0;
ch_layout[0] = '\0';
-
- AVFilterGraph *filter_graph = avfilter_graph_alloc();
+
+ // C89-style variable declaration to
+ // avoid problems because of goto
+ AVFilterGraph* filter_graph = nullptr;
+ AVFilterContext* mix_ctx = nullptr;
+
+ const AVFilter* mix_filter = nullptr;
+ const AVFilter* abuffersink = nullptr;
+ AVFilterContext* abuffersink_ctx = nullptr;
+ char args[512] = { 0 };
+#if LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(7, 107, 100)
+ bool normalize = false;
+#endif
+
+ filter_graph = avfilter_graph_alloc();
if (!filter_graph) {
fprintf(stderr, "Unable to create filter graph.\n");
- return AVERROR(ENOMEM);
+ err = AVERROR(ENOMEM);
+ goto fail;
}
-
+
for(size_t i = 0; i < num_sources; ++i) {
const AVFilter *abuffer = avfilter_get_by_name("abuffer");
if (!abuffer) {
fprintf(stderr, "Could not find the abuffer filter.\n");
- return AVERROR_FILTER_NOT_FOUND;
+ err = AVERROR_FILTER_NOT_FOUND;
+ goto fail;
}
-
+
AVFilterContext *abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, NULL);
if (!abuffer_ctx) {
fprintf(stderr, "Could not allocate the abuffer instance.\n");
- return AVERROR(ENOMEM);
+ err = AVERROR(ENOMEM);
+ goto fail;
}
-
+
#if LIBAVCODEC_VERSION_MAJOR < 60
av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, AV_CH_LAYOUT_STEREO);
#else
@@ -1559,50 +1811,56 @@ static int init_filter_graph(AVCodecContext *audio_codec_context, AVFilterGraph
av_opt_set_q (abuffer_ctx, "time_base", audio_codec_context->time_base, AV_OPT_SEARCH_CHILDREN);
av_opt_set_int(abuffer_ctx, "sample_rate", audio_codec_context->sample_rate, AV_OPT_SEARCH_CHILDREN);
av_opt_set_int(abuffer_ctx, "bit_rate", audio_codec_context->bit_rate, AV_OPT_SEARCH_CHILDREN);
-
+
err = avfilter_init_str(abuffer_ctx, NULL);
if (err < 0) {
fprintf(stderr, "Could not initialize the abuffer filter.\n");
- return err;
+ goto fail;
}
src_filter_ctx.push_back(abuffer_ctx);
}
- const AVFilter *mix_filter = avfilter_get_by_name("amix");
+ mix_filter = avfilter_get_by_name("amix");
if (!mix_filter) {
av_log(NULL, AV_LOG_ERROR, "Could not find the mix filter.\n");
- return AVERROR_FILTER_NOT_FOUND;
+ err = AVERROR_FILTER_NOT_FOUND;
+ goto fail;
}
-
- char args[512];
+
+#if LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(7, 107, 100)
+ snprintf(args, sizeof(args), "inputs=%d:normalize=%s", (int)num_sources, normalize ? "true" : "false");
+#else
snprintf(args, sizeof(args), "inputs=%d", (int)num_sources);
-
- AVFilterContext *mix_ctx;
+ fprintf(stderr, "Warning: your ffmpeg version doesn't support disabling normalizing of mixed audio. Volume might be lower than expected\n");
+#endif
+
err = avfilter_graph_create_filter(&mix_ctx, mix_filter, "amix", args, NULL, filter_graph);
if (err < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create audio amix filter\n");
- return err;
+ goto fail;
}
-
- const AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
+
+ abuffersink = avfilter_get_by_name("abuffersink");
if (!abuffersink) {
fprintf(stderr, "Could not find the abuffersink filter.\n");
- return AVERROR_FILTER_NOT_FOUND;
+ err = AVERROR_FILTER_NOT_FOUND;
+ goto fail;
}
-
- AVFilterContext *abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
+
+ abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
if (!abuffersink_ctx) {
fprintf(stderr, "Could not allocate the abuffersink instance.\n");
- return AVERROR(ENOMEM);
+ err = AVERROR(ENOMEM);
+ goto fail;
}
-
+
err = avfilter_init_str(abuffersink_ctx, NULL);
if (err < 0) {
fprintf(stderr, "Could not initialize the abuffersink instance.\n");
- return err;
+ goto fail;
}
-
+
err = 0;
for(size_t i = 0; i < src_filter_ctx.size(); ++i) {
AVFilterContext *src_ctx = src_filter_ctx[i];
@@ -1613,24 +1871,37 @@ static int init_filter_graph(AVCodecContext *audio_codec_context, AVFilterGraph
err = avfilter_link(mix_ctx, 0, abuffersink_ctx, 0);
if (err < 0) {
av_log(NULL, AV_LOG_ERROR, "Error connecting filters\n");
- return err;
+ goto fail;
}
-
+
err = avfilter_graph_config(filter_graph, NULL);
if (err < 0) {
av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
- return err;
+ goto fail;
}
-
+
*graph = filter_graph;
- *sink = abuffersink_ctx;
-
+ *sink = abuffersink_ctx;
+
return 0;
+
+fail:
+ avfilter_graph_free(&filter_graph);
+ src_filter_ctx.clear(); // possibly unnecessary?
+ return err;
}
static gsr_video_encoder* create_video_encoder(gsr_egl *egl, bool overclock, gsr_color_depth color_depth, bool use_software_video_encoder, VideoCodec video_codec) {
gsr_video_encoder *video_encoder = nullptr;
+ if(video_codec_is_image(video_codec)) {
+ gsr_video_encoder_image_params params;
+ params.egl = egl;
+ params.color_depth = color_depth;
+ video_encoder = gsr_video_encoder_image_create(&params);
+ return video_encoder;
+ }
+
if(use_software_video_encoder) {
gsr_video_encoder_software_params params;
params.egl = egl;
@@ -1639,7 +1910,7 @@ static gsr_video_encoder* create_video_encoder(gsr_egl *egl, bool overclock, gsr
return video_encoder;
}
- if(video_codec == VideoCodec::H264_VULKAN) {
+ if(video_codec_is_vulkan(video_codec)) {
gsr_video_encoder_vulkan_params params;
params.egl = egl;
params.color_depth = color_depth;
@@ -1657,11 +1928,11 @@ static gsr_video_encoder* create_video_encoder(gsr_egl *egl, bool overclock, gsr
break;
}
case GSR_GPU_VENDOR_NVIDIA: {
- gsr_video_encoder_cuda_params params;
+ gsr_video_encoder_nvenc_params params;
params.egl = egl;
params.overclock = overclock;
params.color_depth = color_depth;
- video_encoder = gsr_video_encoder_cuda_create(&params);
+ video_encoder = gsr_video_encoder_nvenc_create(&params);
break;
}
}
@@ -1673,11 +1944,11 @@ static bool get_supported_video_codecs(gsr_egl *egl, VideoCodec video_codec, boo
memset(video_codecs, 0, sizeof(*video_codecs));
if(use_software_video_encoder) {
- video_codecs->h264 = true;
+ video_codecs->h264.supported = true;
return true;
}
- if(video_codec == VideoCodec::H264_VULKAN)
+ if(video_codec_is_vulkan(video_codec))
return gsr_get_supported_video_codecs_vulkan(video_codecs, egl->card_path, cleanup);
switch(egl->gpu_info.vendor) {
@@ -1711,7 +1982,7 @@ static bool is_xwayland(Display *display) {
static bool is_using_prime_run() {
const char *prime_render_offload = getenv("__NV_PRIME_RENDER_OFFLOAD");
- return prime_render_offload && strcmp(prime_render_offload, "1") == 0;
+ return (prime_render_offload && strcmp(prime_render_offload, "1") == 0) || getenv("DRI_PRIME");
}
static void disable_prime_run() {
@@ -1719,10 +1990,30 @@ static void disable_prime_run() {
unsetenv("__NV_PRIME_RENDER_OFFLOAD_PROVIDER");
unsetenv("__GLX_VENDOR_LIBRARY_NAME");
unsetenv("__VK_LAYER_NV_optimus");
+ unsetenv("DRI_PRIME");
+}
+
+static gsr_window* gsr_window_create(Display *display, bool wayland) {
+ if(wayland)
+ return gsr_window_wayland_create();
+ else
+ return gsr_window_x11_create(display);
}
static void list_system_info(bool wayland) {
printf("display_server|%s\n", wayland ? "wayland" : "x11");
+ bool supports_app_audio = false;
+#ifdef GSR_APP_AUDIO
+ supports_app_audio = pulseaudio_server_is_pipewire();
+ if(supports_app_audio) {
+ gsr_pipewire_audio audio;
+ if(gsr_pipewire_audio_init(&audio))
+ gsr_pipewire_audio_deinit(&audio);
+ else
+ supports_app_audio = false;
+ }
+#endif
+ printf("supports_app_audio|%s\n", supports_app_audio ? "yes" : "no");
}
static void list_gpu_info(gsr_egl *egl) {
@@ -1737,6 +2028,7 @@ static void list_gpu_info(gsr_egl *egl) {
printf("vendor|nvidia\n");
break;
}
+ printf("card_path|%s\n", egl->card_path);
}
static const AVCodec* get_ffmpeg_video_codec(VideoCodec video_codec, gsr_gpu_vendor vendor) {
@@ -1757,33 +2049,49 @@ static const AVCodec* get_ffmpeg_video_codec(VideoCodec video_codec, gsr_gpu_ven
return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "vp9_nvenc" : "vp9_vaapi");
case VideoCodec::H264_VULKAN:
return avcodec_find_encoder_by_name("h264_vulkan");
+ case VideoCodec::HEVC_VULKAN:
+ return avcodec_find_encoder_by_name("hevc_vulkan");
+ case VideoCodec::JPEG:
+ return avcodec_find_encoder_by_name("libopenjpeg");
+ case VideoCodec::PNG:
+ return avcodec_find_encoder_by_name("png");
}
return nullptr;
}
-static void set_supported_video_codecs_ffmpeg(gsr_supported_video_codecs *supported_video_codecs, gsr_gpu_vendor vendor) {
+static void set_supported_video_codecs_ffmpeg(gsr_supported_video_codecs *supported_video_codecs, gsr_supported_video_codecs *supported_video_codecs_vulkan, gsr_gpu_vendor vendor) {
if(!get_ffmpeg_video_codec(VideoCodec::H264, vendor)) {
- supported_video_codecs->h264 = false;
+ supported_video_codecs->h264.supported = false;
}
if(!get_ffmpeg_video_codec(VideoCodec::HEVC, vendor)) {
- supported_video_codecs->hevc = false;
- supported_video_codecs->hevc_hdr = false;
- supported_video_codecs->hevc_10bit = false;
+ supported_video_codecs->hevc.supported = false;
+ supported_video_codecs->hevc_hdr.supported = false;
+ supported_video_codecs->hevc_10bit.supported = false;
}
if(!get_ffmpeg_video_codec(VideoCodec::AV1, vendor)) {
- supported_video_codecs->av1 = false;
- supported_video_codecs->av1_hdr = false;
- supported_video_codecs->av1_10bit = false;
+ supported_video_codecs->av1.supported = false;
+ supported_video_codecs->av1_hdr.supported = false;
+ supported_video_codecs->av1_10bit.supported = false;
}
if(!get_ffmpeg_video_codec(VideoCodec::VP8, vendor)) {
- supported_video_codecs->vp8 = false;
+ supported_video_codecs->vp8.supported = false;
}
if(!get_ffmpeg_video_codec(VideoCodec::VP9, vendor)) {
- supported_video_codecs->vp9 = false;
+ supported_video_codecs->vp9.supported = false;
+ }
+
+ if(!get_ffmpeg_video_codec(VideoCodec::H264_VULKAN, vendor)) {
+ supported_video_codecs_vulkan->h264.supported = false;
+ }
+
+ if(!get_ffmpeg_video_codec(VideoCodec::HEVC_VULKAN, vendor)) {
+ supported_video_codecs_vulkan->hevc.supported = false;
+ supported_video_codecs_vulkan->hevc_hdr.supported = false;
+ supported_video_codecs_vulkan->hevc_10bit.supported = false;
}
}
@@ -1791,51 +2099,55 @@ static void list_supported_video_codecs(gsr_egl *egl, bool wayland) {
// Dont clean it up on purpose to increase shutdown speed
gsr_supported_video_codecs supported_video_codecs;
get_supported_video_codecs(egl, VideoCodec::H264, false, false, &supported_video_codecs);
- set_supported_video_codecs_ffmpeg(&supported_video_codecs, egl->gpu_info.vendor);
gsr_supported_video_codecs supported_video_codecs_vulkan;
get_supported_video_codecs(egl, VideoCodec::H264_VULKAN, false, false, &supported_video_codecs_vulkan);
- if(!get_ffmpeg_video_codec(VideoCodec::H264_VULKAN, egl->gpu_info.vendor))
- memset(&supported_video_codecs_vulkan, 0, sizeof(supported_video_codecs_vulkan));
- if(supported_video_codecs.h264)
+ set_supported_video_codecs_ffmpeg(&supported_video_codecs, &supported_video_codecs_vulkan, egl->gpu_info.vendor);
+
+ if(supported_video_codecs.h264.supported)
puts("h264");
if(avcodec_find_encoder_by_name("libx264"))
puts("h264_software");
- if(supported_video_codecs.hevc)
+ if(supported_video_codecs.hevc.supported)
puts("hevc");
- if(supported_video_codecs.hevc_hdr && wayland)
+ if(supported_video_codecs.hevc_hdr.supported && wayland)
puts("hevc_hdr");
- if(supported_video_codecs.hevc_10bit)
+ if(supported_video_codecs.hevc_10bit.supported)
puts("hevc_10bit");
- if(supported_video_codecs.av1)
+ if(supported_video_codecs.av1.supported)
puts("av1");
- if(supported_video_codecs.av1_hdr && wayland)
+ if(supported_video_codecs.av1_hdr.supported && wayland)
puts("av1_hdr");
- if(supported_video_codecs.av1_10bit)
+ if(supported_video_codecs.av1_10bit.supported)
puts("av1_10bit");
- if(supported_video_codecs.vp8)
+ if(supported_video_codecs.vp8.supported)
puts("vp8");
- if(supported_video_codecs.vp9)
+ if(supported_video_codecs.vp9.supported)
puts("vp9");
- if(supported_video_codecs_vulkan.h264)
- puts("h264_vulkan");
+ if(avcodec_find_encoder_by_name("libopenjpeg"))
+ puts("jpeg");
+ if(avcodec_find_encoder_by_name("png"))
+ puts("png");
+ //if(supported_video_codecs_vulkan.h264.supported)
+ // puts("h264_vulkan");
+ //if(supported_video_codecs_vulkan.hevc.supported)
+ // puts("hevc_vulkan"); // TODO: hdr, 10 bit
}
-static bool monitor_capture_use_drm(gsr_egl *egl, bool wayland) {
- return wayland || egl->gpu_info.vendor != GSR_GPU_VENDOR_NVIDIA;
+static bool monitor_capture_use_drm(const gsr_window *window, gsr_gpu_vendor vendor) {
+ return gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_WAYLAND || vendor != GSR_GPU_VENDOR_NVIDIA;
}
typedef struct {
- bool wayland;
- gsr_egl *egl;
+ const gsr_window *window;
} capture_options_callback;
static void output_monitor_info(const gsr_monitor *monitor, void *userdata) {
const capture_options_callback *options = (capture_options_callback*)userdata;
- if(options->wayland && monitor_capture_use_drm(options->egl, options->wayland)) {
+ if(gsr_window_get_display_server(options->window) == GSR_DISPLAY_SERVER_WAYLAND) {
vec2i monitor_size = monitor->size;
- const gsr_monitor_rotation rot = drm_monitor_get_display_server_rotation(options->egl, monitor);
+ const gsr_monitor_rotation rot = drm_monitor_get_display_server_rotation(options->window, monitor);
if(rot == GSR_MONITOR_ROT_90 || rot == GSR_MONITOR_ROT_270)
std::swap(monitor_size.x, monitor_size.y);
printf("%.*s|%dx%d\n", monitor->name_len, monitor->name, monitor_size.x, monitor_size.y);
@@ -1844,22 +2156,19 @@ static void output_monitor_info(const gsr_monitor *monitor, void *userdata) {
}
}
-static void list_supported_capture_options(gsr_egl *egl, bool wayland) {
+static void list_supported_capture_options(const gsr_window *window, const char *card_path, bool list_monitors) {
+ const bool wayland = gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_WAYLAND;
if(!wayland) {
puts("window");
puts("focused");
}
- capture_options_callback options;
- options.wayland = wayland;
- options.egl = egl;
- if(monitor_capture_use_drm(egl, wayland)) {
- const bool is_x11 = gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_X11;
+ if(list_monitors) {
+ capture_options_callback options;
+ options.window = window;
+ const bool is_x11 = gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_X11;
const gsr_connection_type connection_type = is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
- for_each_active_monitor_output(egl, connection_type, output_monitor_info, &options);
- } else {
- puts("screen"); // All monitors in one, only available on Nvidia X11
- for_each_active_monitor_output(egl, GSR_CONNECTION_X11, output_monitor_info, &options);
+ for_each_active_monitor_output(window, card_path, connection_type, output_monitor_info, &options);
}
#ifdef GSR_PORTAL
@@ -1902,18 +2211,25 @@ static void info_command() {
disable_prime_run();
}
+ gsr_window *window = gsr_window_create(dpy, wayland);
+ if(!window) {
+ fprintf(stderr, "Error: failed to create window\n");
+ _exit(1);
+ }
+
gsr_egl egl;
- if(!gsr_egl_load(&egl, dpy, wayland, false)) {
+ if(!gsr_egl_load(&egl, window, false, false)) {
fprintf(stderr, "gsr error: failed to load opengl\n");
_exit(22);
}
+ bool list_monitors = true;
egl.card_path[0] = '\0';
- if(monitor_capture_use_drm(&egl, wayland)) {
+ if(monitor_capture_use_drm(window, egl.gpu_info.vendor)) {
// TODO: Allow specifying another card, and in other places
- if(!gsr_get_valid_card_path(&egl, egl.card_path, false)) {
+ if(!gsr_get_valid_card_path(&egl, egl.card_path, true)) {
fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected\n");
- _exit(23);
+ list_monitors = false;
}
}
@@ -1925,17 +2241,19 @@ static void info_command() {
puts("is_steam_deck|yes");
else
puts("is_steam_deck|no");
+ printf("gsr_version|%s\n", GSR_VERSION);
puts("section=gpu_info");
list_gpu_info(&egl);
puts("section=video_codecs");
list_supported_video_codecs(&egl, wayland);
puts("section=capture_options");
- list_supported_capture_options(&egl, wayland);
+ list_supported_capture_options(window, egl.card_path, list_monitors);
fflush(stdout);
// Not needed as this will just slow down shutdown
//gsr_egl_unload(&egl);
+ //gsr_window_destroy(&window);
//if(dpy)
// XCloseDisplay(dpy);
@@ -1959,11 +2277,136 @@ static void list_audio_devices_command() {
_exit(0);
}
-static gsr_capture* create_capture_impl(std::string &window_str, const char *screen_region, bool wayland, gsr_egl *egl, int fps, VideoCodec video_codec, gsr_color_range color_range,
- bool record_cursor, bool use_software_video_encoder, bool restore_portal_session, const char *portal_session_token_filepath,
+static bool app_audio_query_callback(const char *app_name, void*) {
+ puts(app_name);
+ return true;
+}
+
+static void list_application_audio_command() {
+#ifdef GSR_APP_AUDIO
+ if(pulseaudio_server_is_pipewire()) {
+ gsr_pipewire_audio audio;
+ if(gsr_pipewire_audio_init(&audio)) {
+ gsr_pipewire_audio_for_each_app(&audio, app_audio_query_callback, NULL);
+ gsr_pipewire_audio_deinit(&audio);
+ }
+ }
+#endif
+
+ fflush(stdout);
+ _exit(0);
+}
+
+// |card_path| can be NULL. If not NULL then |vendor| has to be valid
+static void list_capture_options_command(const char *card_path, gsr_gpu_vendor vendor) {
+ (void)vendor;
+ bool wayland = false;
+ Display *dpy = XOpenDisplay(nullptr);
+ if (!dpy) {
+ wayland = true;
+ fprintf(stderr, "Warning: failed to connect to the X server. Assuming wayland is running without Xwayland\n");
+ }
+
+ XSetErrorHandler(x11_error_handler);
+ XSetIOErrorHandler(x11_io_error_handler);
+
+ if(!wayland)
+ wayland = is_xwayland(dpy);
+
+ if(!wayland && is_using_prime_run()) {
+ // Disable prime-run and similar options as it doesn't work, the monitor to capture has to be run on the same device.
+ // This is fine on wayland since nvidia uses drm interface there and the monitor query checks the monitors connected
+ // to the drm device.
+ fprintf(stderr, "Warning: use of prime-run on X11 is not supported. Disabling prime-run\n");
+ disable_prime_run();
+ }
+
+ gsr_window *window = gsr_window_create(dpy, wayland);
+ if(!window) {
+ fprintf(stderr, "Error: failed to create window\n");
+ _exit(1);
+ }
+
+ if(card_path) {
+ list_supported_capture_options(window, card_path, true);
+ } else {
+ gsr_egl egl;
+ if(!gsr_egl_load(&egl, window, false, false)) {
+ fprintf(stderr, "gsr error: failed to load opengl\n");
+ _exit(1);
+ }
+
+ bool list_monitors = true;
+ egl.card_path[0] = '\0';
+ if(monitor_capture_use_drm(window, egl.gpu_info.vendor)) {
+ // TODO: Allow specifying another card, and in other places
+ if(!gsr_get_valid_card_path(&egl, egl.card_path, true)) {
+ fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected\n");
+ list_monitors = false;
+ }
+ }
+ list_supported_capture_options(window, egl.card_path, list_monitors);
+ }
+
+ fflush(stdout);
+
+ // Not needed as this will just slow down shutdown
+ //gsr_egl_unload(&egl);
+ //gsr_window_destroy(&window);
+ //if(dpy)
+ // XCloseDisplay(dpy);
+
+ _exit(0);
+}
+
+static bool gpu_vendor_from_string(const char *vendor_str, gsr_gpu_vendor *vendor) {
+ if(strcmp(vendor_str, "amd") == 0) {
+ *vendor = GSR_GPU_VENDOR_AMD;
+ return true;
+ } else if(strcmp(vendor_str, "intel") == 0) {
+ *vendor = GSR_GPU_VENDOR_INTEL;
+ return true;
+ } else if(strcmp(vendor_str, "nvidia") == 0) {
+ *vendor = GSR_GPU_VENDOR_NVIDIA;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+static void validate_monitor_get_valid(const gsr_egl *egl, std::string &window_str) {
+ const bool is_x11 = gsr_window_get_display_server(egl->window) == GSR_DISPLAY_SERVER_X11;
+ const gsr_connection_type connection_type = is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
+ const bool capture_use_drm = monitor_capture_use_drm(egl->window, egl->gpu_info.vendor);
+
+ if(strcmp(window_str.c_str(), "screen") == 0) {
+ FirstOutputCallback first_output;
+ first_output.output_name = NULL;
+ for_each_active_monitor_output(egl->window, egl->card_path, connection_type, get_first_output, &first_output);
+
+ if(first_output.output_name) {
+ window_str = first_output.output_name;
+ } else {
+ fprintf(stderr, "Error: no usable output found\n");
+ _exit(51);
+ }
+ } else if(capture_use_drm || (strcmp(window_str.c_str(), "screen-direct") != 0 && strcmp(window_str.c_str(), "screen-direct-force") != 0)) {
+ gsr_monitor gmon;
+ if(!get_monitor_by_name(egl, connection_type, window_str.c_str(), &gmon)) {
+ fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str.c_str());
+ fprintf(stderr, " \"screen\"\n");
+ if(!capture_use_drm)
+ fprintf(stderr, " \"screen-direct\"\n");
+ for_each_active_monitor_output(egl->window, egl->card_path, connection_type, monitor_output_callback_print, NULL);
+ _exit(51);
+ }
+ }
+}
+
+static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_resolution, bool wayland, gsr_egl *egl, int fps, VideoCodec video_codec, gsr_color_range color_range,
+ bool record_cursor, bool restore_portal_session, const char *portal_session_token_filepath,
gsr_color_depth color_depth)
{
- vec2i region_size = { 0, 0 };
Window src_window_id = None;
bool follow_focused = false;
@@ -1974,18 +2417,8 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
_exit(2);
}
- if(!screen_region) {
- fprintf(stderr, "Error: option -s is required when using -w focused\n");
- usage();
- }
-
- if(sscanf(screen_region, "%dx%d", &region_size.x, &region_size.y) != 2) {
- fprintf(stderr, "Error: invalid value for option -s '%s', expected a value in format WxH\n", screen_region);
- usage();
- }
-
- if(region_size.x <= 0 || region_size.y <= 0) {
- fprintf(stderr, "Error: invalud value for option -s '%s', expected width and height to be greater than 0\n", screen_region);
+ if(output_resolution.x <= 0 || output_resolution.y <= 0) {
+ fprintf(stderr, "Error: invalid value for option -s '%dx%d' when using -w focused option. expected width and height to be greater than 0\n", output_resolution.x, output_resolution.y);
usage();
}
@@ -2005,67 +2438,22 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
portal_params.record_cursor = record_cursor;
portal_params.restore_portal_session = restore_portal_session;
portal_params.portal_session_token_filepath = portal_session_token_filepath;
+ portal_params.output_resolution = output_resolution;
capture = gsr_capture_portal_create(&portal_params);
if(!capture)
_exit(1);
#else
- fprintf(stderr, "Error: option '-w portal' used but GPU Screen Recorder was compiled without desktop portal support\n");
+ fprintf(stderr, "Error: option '-w portal' used but GPU Screen Recorder was compiled without desktop portal support. Please recompile GPU Screen recorder with the -Dportal=true option\n");
_exit(2);
#endif
} else if(contains_non_hex_number(window_str.c_str())) {
- if(monitor_capture_use_drm(egl, wayland)) {
- const bool is_x11 = gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_X11;
- const gsr_connection_type connection_type = is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
-
- if(strcmp(window_str.c_str(), "screen") == 0) {
- FirstOutputCallback first_output;
- first_output.output_name = NULL;
- for_each_active_monitor_output(egl, connection_type, get_first_output, &first_output);
-
- if(first_output.output_name) {
- window_str = first_output.output_name;
- } else {
- fprintf(stderr, "Error: no usable output found\n");
- _exit(1);
- }
- } else {
- gsr_monitor gmon;
- if(!get_monitor_by_name(egl, connection_type, window_str.c_str(), &gmon)) {
- fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str.c_str());
- fprintf(stderr, " \"screen\"\n");
- for_each_active_monitor_output(egl, connection_type, monitor_output_callback_print, NULL);
- _exit(1);
- }
- }
- } else {
- if(strcmp(window_str.c_str(), "screen") != 0 && strcmp(window_str.c_str(), "screen-direct") != 0 && strcmp(window_str.c_str(), "screen-direct-force") != 0) {
- gsr_monitor gmon;
- if(!get_monitor_by_name(egl, GSR_CONNECTION_X11, window_str.c_str(), &gmon)) {
- const int screens_width = XWidthOfScreen(DefaultScreenOfDisplay(egl->x11.dpy));
- const int screens_height = XWidthOfScreen(DefaultScreenOfDisplay(egl->x11.dpy));
- fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str.c_str());
- fprintf(stderr, " \"screen\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
- fprintf(stderr, " \"screen-direct\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
- fprintf(stderr, " \"screen-direct-force\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
- for_each_active_monitor_output(egl, GSR_CONNECTION_X11, monitor_output_callback_print, NULL);
- _exit(1);
- }
- }
- }
-
- if(egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && !wayland) {
+ validate_monitor_get_valid(egl, window_str);
+ if(!monitor_capture_use_drm(egl->window, egl->gpu_info.vendor)) {
const char *capture_target = window_str.c_str();
- bool direct_capture = strcmp(window_str.c_str(), "screen-direct") == 0;
+ const bool direct_capture = strcmp(window_str.c_str(), "screen-direct") == 0 || strcmp(window_str.c_str(), "screen-direct-force") == 0;
if(direct_capture) {
capture_target = "screen";
- // TODO: Temporary disable direct capture because push model causes stuttering when it's direct capturing. This might be a nvfbc bug. This does not happen when using a compositor.
- direct_capture = false;
- fprintf(stderr, "Warning: screen-direct has temporary been disabled as it causes stuttering. This is likely a NvFBC bug. Falling back to \"screen\".\n");
- }
-
- if(strcmp(window_str.c_str(), "screen-direct-force") == 0) {
- direct_capture = true;
- capture_target = "screen";
+ fprintf(stderr, "Warning: %s capture option is not recommended unless you use G-SYNC as Nvidia has driver issues that can cause your system or games to freeze/crash.\n", window_str.c_str());
}
gsr_capture_nvfbc_params nvfbc_params;
@@ -2078,7 +2466,7 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
nvfbc_params.color_depth = color_depth;
nvfbc_params.color_range = color_range;
nvfbc_params.record_cursor = record_cursor;
- nvfbc_params.use_software_video_encoder = use_software_video_encoder;
+ nvfbc_params.output_resolution = output_resolution;
capture = gsr_capture_nvfbc_create(&nvfbc_params);
if(!capture)
_exit(1);
@@ -2091,13 +2479,14 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
kms_params.record_cursor = record_cursor;
kms_params.hdr = video_codec_is_hdr(video_codec);
kms_params.fps = fps;
+ kms_params.output_resolution = output_resolution;
capture = gsr_capture_kms_create(&kms_params);
if(!capture)
_exit(1);
}
} else {
if(wayland) {
- fprintf(stderr, "Error: GPU Screen Recorder window capture only works in a pure X11 session. Xwayland is not supported. You can record a monitor instead on wayland\n");
+ fprintf(stderr, "Error: GPU Screen Recorder window capture only works in a pure X11 session. Xwayland is not supported. You can record a monitor instead on wayland or use -w portal option which supports window capture if your wayland compositor supports window capture\n");
_exit(2);
}
@@ -2114,10 +2503,10 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
xcomposite_params.egl = egl;
xcomposite_params.window = src_window_id;
xcomposite_params.follow_focused = follow_focused;
- xcomposite_params.region_size = region_size;
xcomposite_params.color_range = color_range;
xcomposite_params.record_cursor = record_cursor;
xcomposite_params.color_depth = color_depth;
+ xcomposite_params.output_resolution = output_resolution;
capture = gsr_capture_xcomposite_create(&xcomposite_params);
if(!capture)
_exit(1);
@@ -2127,20 +2516,32 @@ static gsr_capture* create_capture_impl(std::string &window_str, const char *scr
}
static AVPixelFormat get_pixel_format(VideoCodec video_codec, gsr_gpu_vendor vendor, bool use_software_video_encoder) {
- if(use_software_video_encoder) {
+ if(video_codec_is_image(video_codec)) {
+ // TODO: hdr
+ return AV_PIX_FMT_RGB24;
+ } else if(use_software_video_encoder) {
return AV_PIX_FMT_NV12;
} else {
- if(video_codec == VideoCodec::H264_VULKAN)
+ if(video_codec_is_vulkan(video_codec))
return AV_PIX_FMT_VULKAN;
else
return vendor == GSR_GPU_VENDOR_NVIDIA ? AV_PIX_FMT_CUDA : AV_PIX_FMT_VAAPI;
}
}
+enum class ArgType {
+ STRING,
+ BOOLEAN
+};
+
struct Arg {
std::vector<const char*> values;
bool optional = false;
bool list = false;
+ ArgType arg_type = ArgType::STRING;
+ union {
+ bool boolean = false;
+ } typed_value;
const char* value() const {
if(values.empty())
@@ -2149,58 +2550,76 @@ struct Arg {
}
};
+static void match_app_audio_input_to_available_apps(const std::vector<AudioInput> &requested_audio_inputs, const std::vector<std::string> &app_audio_names) {
+ for(const AudioInput &request_audio_input : requested_audio_inputs) {
+ if(request_audio_input.type != AudioInputType::APPLICATION || request_audio_input.inverted)
+ continue;
+
+ bool match = false;
+ for(const std::string &app_name : app_audio_names) {
+ if(strcasecmp(app_name.c_str(), request_audio_input.name.c_str()) == 0) {
+ match = true;
+ break;
+ }
+ }
+
+ if(!match) {
+ fprintf(stderr, "gsr warning: no audio application with the name \"%s\" was found, expected one of the following:\n", request_audio_input.name.c_str());
+ for(const std::string &app_name : app_audio_names) {
+ fprintf(stderr, " * %s\n", app_name.c_str());
+ }
+ fprintf(stderr, " assuming this is intentional (if you are trying to record audio for applications that haven't started yet).\n");
+ }
+ }
+}
+
// Manually check if the audio inputs we give exist. This is only needed for pipewire, not pulseaudio.
// Pipewire instead DEFAULTS TO THE DEFAULT AUDIO INPUT. THAT'S RETARDED.
// OH, YOU MISSPELLED THE AUDIO INPUT? FUCK YOU
-static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &audio_devices, const Arg &audio_input_arg, bool &uses_amix) {
+static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &audio_devices, const Arg &audio_input_arg) {
std::vector<MergedAudioInputs> requested_audio_inputs;
- uses_amix = false;
for(const char *audio_input : audio_input_arg.values) {
if(!audio_input || audio_input[0] == '\0')
continue;
- requested_audio_inputs.push_back({parse_audio_input_arg(audio_input)});
- if(requested_audio_inputs.back().audio_inputs.size() > 1)
- uses_amix = true;
-
+ requested_audio_inputs.push_back(parse_audio_input_arg(audio_input, audio_devices));
for(AudioInput &request_audio_input : requested_audio_inputs.back().audio_inputs) {
+ if(request_audio_input.type != AudioInputType::DEVICE)
+ continue;
+
bool match = false;
- if(!audio_devices.default_output.empty() && request_audio_input.name == "default_output") {
+ if(request_audio_input.name == "default_output") {
+ if(audio_devices.default_output.empty()) {
+ fprintf(stderr, "Error: -a default_output was specified but no default audio output is specified in the audio server\n");
+ _exit(2);
+ }
request_audio_input.name = audio_devices.default_output;
- if(request_audio_input.description.empty())
- request_audio_input.description = "gsr-Default output";
match = true;
- }
-
- if(!audio_devices.default_input.empty() && request_audio_input.name == "default_input") {
+ } else if(request_audio_input.name == "default_input") {
+ if(audio_devices.default_input.empty()) {
+ fprintf(stderr, "Error: -a default_input was specified but no default audio input is specified in the audio server\n");
+ _exit(2);
+ }
request_audio_input.name = audio_devices.default_input;
- if(request_audio_input.description.empty())
- request_audio_input.description = "gsr-Default input";
match = true;
- }
-
- for(const auto &existing_audio_input : audio_devices.audio_inputs) {
- if(request_audio_input.name == existing_audio_input.name) {
- if(request_audio_input.description.empty())
- request_audio_input.description = "gsr-" + existing_audio_input.description;
-
+ } else {
+ const bool name_is_existing_audio_device = get_audio_device_by_name(audio_devices.audio_inputs, request_audio_input.name.c_str()) != nullptr;
+ if(name_is_existing_audio_device)
match = true;
- break;
- }
}
if(!match) {
- fprintf(stderr, "Error: Audio input device '%s' is not a valid audio device, expected one of:\n", request_audio_input.name.c_str());
+ fprintf(stderr, "Error: Audio device '%s' is not a valid audio device, expected one of:\n", request_audio_input.name.c_str());
if(!audio_devices.default_output.empty())
fprintf(stderr, " default_output (Default output)\n");
if(!audio_devices.default_input.empty())
fprintf(stderr, " default_input (Default input)\n");
- for(const auto &existing_audio_input : audio_devices.audio_inputs) {
- fprintf(stderr, " %s (%s)\n", existing_audio_input.name.c_str(), existing_audio_input.description.c_str());
+ for(const auto &audio_device_input : audio_devices.audio_inputs) {
+ fprintf(stderr, " %s (%s)\n", audio_device_input.name.c_str(), audio_device_input.description.c_str());
}
- _exit(2);
+ _exit(50);
}
}
}
@@ -2208,7 +2627,69 @@ static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &aud
return requested_audio_inputs;
}
-static AudioCodec select_audio_codec_with_fallback(AudioCodec audio_codec, const std::string &file_extension,bool uses_amix) {
+static bool audio_inputs_has_app_audio(const std::vector<AudioInput> &audio_inputs) {
+ for(const auto &audio_input : audio_inputs) {
+ if(audio_input.type == AudioInputType::APPLICATION)
+ return true;
+ }
+ return false;
+}
+
+static bool merged_audio_inputs_has_app_audio(const std::vector<MergedAudioInputs> &merged_audio_inputs) {
+ for(const auto &merged_audio_input : merged_audio_inputs) {
+ if(audio_inputs_has_app_audio(merged_audio_input.audio_inputs))
+ return true;
+ }
+ return false;
+}
+
+// Should use amix if more than 1 audio device and 0 application audio, merged
+static bool audio_inputs_should_use_amix(const std::vector<AudioInput> &audio_inputs) {
+ int num_audio_devices = 0;
+ int num_app_audio = 0;
+
+ for(const auto &audio_input : audio_inputs) {
+ if(audio_input.type == AudioInputType::DEVICE)
+ ++num_audio_devices;
+ else if(audio_input.type == AudioInputType::APPLICATION)
+ ++num_app_audio;
+ }
+
+ return num_audio_devices > 1 && num_app_audio == 0;
+}
+
+static bool merged_audio_inputs_should_use_amix(const std::vector<MergedAudioInputs> &merged_audio_inputs) {
+ for(const auto &merged_audio_input : merged_audio_inputs) {
+ if(audio_inputs_should_use_amix(merged_audio_input.audio_inputs))
+ return true;
+ }
+ return false;
+}
+
+static void validate_merged_audio_inputs_app_audio(const std::vector<MergedAudioInputs> &merged_audio_inputs, const std::vector<std::string> &app_audio_names) {
+ for(const auto &merged_audio_input : merged_audio_inputs) {
+ int num_app_audio = 0;
+ int num_app_inverted_audio = 0;
+
+ for(const auto &audio_input : merged_audio_input.audio_inputs) {
+ if(audio_input.type == AudioInputType::APPLICATION) {
+ if(audio_input.inverted)
+ ++num_app_inverted_audio;
+ else
+ ++num_app_audio;
+ }
+ }
+
+ match_app_audio_input_to_available_apps(merged_audio_input.audio_inputs, app_audio_names);
+
+ if(num_app_audio > 0 && num_app_inverted_audio > 0) {
+ fprintf(stderr, "gsr error: argument -a was provided with both app: and app-inverse:, only one of them can be used for one audio track\n");
+ _exit(2);
+ }
+ }
+}
+
+static AudioCodec select_audio_codec_with_fallback(AudioCodec audio_codec, const std::string &file_extension, bool uses_amix) {
switch(audio_codec) {
case AudioCodec::AAC: {
if(file_extension == "webm") {
@@ -2250,7 +2731,7 @@ static AudioCodec select_audio_codec_with_fallback(AudioCodec audio_codec, const
}
static const char* video_codec_to_string(VideoCodec video_codec) {
- switch(video_codec) {
+ switch(video_codec) {
case VideoCodec::H264: return "h264";
case VideoCodec::HEVC: return "hevc";
case VideoCodec::HEVC_HDR: return "hevc_hdr";
@@ -2261,12 +2742,35 @@ static const char* video_codec_to_string(VideoCodec video_codec) {
case VideoCodec::VP8: return "vp8";
case VideoCodec::VP9: return "vp9";
case VideoCodec::H264_VULKAN: return "h264_vulkan";
+ case VideoCodec::HEVC_VULKAN: return "hevc_vulkan";
+ case VideoCodec::JPEG: return "jpeg";
+ case VideoCodec::PNG: return "png";
}
return "";
}
-static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bool use_software_video_encoder, bool video_codec_auto, const char *video_codec_to_use, bool is_flv) {
+static bool video_codec_only_supports_low_power_mode(const gsr_supported_video_codecs &supported_video_codecs, VideoCodec video_codec) {
+ switch(video_codec) {
+ case VideoCodec::H264: return supported_video_codecs.h264.low_power;
+ case VideoCodec::HEVC: return supported_video_codecs.hevc.low_power;
+ case VideoCodec::HEVC_HDR: return supported_video_codecs.hevc_hdr.low_power;
+ case VideoCodec::HEVC_10BIT: return supported_video_codecs.hevc_10bit.low_power;
+ case VideoCodec::AV1: return supported_video_codecs.av1.low_power;
+ case VideoCodec::AV1_HDR: return supported_video_codecs.av1_hdr.low_power;
+ case VideoCodec::AV1_10BIT: return supported_video_codecs.av1_10bit.low_power;
+ case VideoCodec::VP8: return supported_video_codecs.vp8.low_power;
+ case VideoCodec::VP9: return supported_video_codecs.vp9.low_power;
+ case VideoCodec::H264_VULKAN: return supported_video_codecs.h264.low_power;
+ case VideoCodec::HEVC_VULKAN: return supported_video_codecs.hevc.low_power; // TODO: hdr, 10 bit
+ case VideoCodec::JPEG: return false;
+ case VideoCodec::PNG: return false;
+ }
+ return false;
+}
+
+static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bool use_software_video_encoder, bool video_codec_auto, const char *video_codec_to_use, bool is_flv, bool *low_power) {
// TODO: software encoder for hevc, av1, vp8 and vp9
+ *low_power = false;
gsr_supported_video_codecs supported_video_codecs;
if(!get_supported_video_codecs(egl, *video_codec, use_software_video_encoder, true, &supported_video_codecs)) {
@@ -2280,55 +2784,66 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
case VideoCodec::H264: {
if(use_software_video_encoder)
video_codec_f = avcodec_find_encoder_by_name("libx264");
- else if(supported_video_codecs.h264)
+ else if(supported_video_codecs.h264.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::HEVC: {
- if(supported_video_codecs.hevc)
+ if(supported_video_codecs.hevc.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::HEVC_HDR: {
- if(supported_video_codecs.hevc_hdr)
+ if(supported_video_codecs.hevc_hdr.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::HEVC_10BIT: {
- if(supported_video_codecs.hevc_10bit)
+ if(supported_video_codecs.hevc_10bit.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::AV1: {
- if(supported_video_codecs.av1)
+ if(supported_video_codecs.av1.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::AV1_HDR: {
- if(supported_video_codecs.av1_hdr)
+ if(supported_video_codecs.av1_hdr.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::AV1_10BIT: {
- if(supported_video_codecs.av1_10bit)
+ if(supported_video_codecs.av1_10bit.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::VP8: {
- if(supported_video_codecs.vp8)
+ if(supported_video_codecs.vp8.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::VP9: {
- if(supported_video_codecs.vp9)
+ if(supported_video_codecs.vp9.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
case VideoCodec::H264_VULKAN: {
- if(supported_video_codecs.h264)
+ if(supported_video_codecs.h264.supported)
+ video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
+ break;
+ }
+ case VideoCodec::HEVC_VULKAN: {
+ // TODO: hdr, 10 bit
+ if(supported_video_codecs.hevc.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
+ case VideoCodec::JPEG:
+ case VideoCodec::PNG: {
+ video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
+ break;
+ }
}
if(!video_codec_auto && !video_codec_f && !is_flv) {
@@ -2336,7 +2851,7 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
case VideoCodec::H264: {
fprintf(stderr, "Warning: selected video codec h264 is not supported, trying hevc instead\n");
video_codec_to_use = "hevc";
- if(supported_video_codecs.hevc)
+ if(supported_video_codecs.hevc.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
@@ -2346,7 +2861,7 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
fprintf(stderr, "Warning: selected video codec hevc is not supported, trying h264 instead\n");
video_codec_to_use = "h264";
*video_codec = VideoCodec::H264;
- if(supported_video_codecs.h264)
+ if(supported_video_codecs.h264.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
@@ -2356,7 +2871,7 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
fprintf(stderr, "Warning: selected video codec av1 is not supported, trying h264 instead\n");
video_codec_to_use = "h264";
*video_codec = VideoCodec::H264;
- if(supported_video_codecs.h264)
+ if(supported_video_codecs.h264.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
@@ -2373,10 +2888,29 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
fprintf(stderr, "Error: failed to query for supported video codecs\n");
_exit(11);
}
- if(supported_video_codecs.h264)
+ if(supported_video_codecs.h264.supported)
+ video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
+ break;
+ }
+ case VideoCodec::HEVC_VULKAN: {
+ fprintf(stderr, "Warning: selected video codec hevc_vulkan is not supported, trying hevc instead\n");
+ video_codec_to_use = "hevc";
+ *video_codec = VideoCodec::HEVC;
+ // Need to do a query again because this time it's without vulkan
+ if(!get_supported_video_codecs(egl, *video_codec, use_software_video_encoder, true, &supported_video_codecs)) {
+ fprintf(stderr, "Error: failed to query for supported video codecs\n");
+ _exit(11);
+ }
+ if(supported_video_codecs.hevc.supported)
video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
break;
}
+ case VideoCodec::JPEG:
+ case VideoCodec::PNG: {
+ // TODO:
+ //assert(false);
+ break;
+ }
}
}
@@ -2397,10 +2931,12 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo
_exit(2);
}
+ *low_power = video_codec_only_supports_low_power_mode(supported_video_codecs, *video_codec);
+
return video_codec_f;
}
-static const AVCodec* select_video_codec_with_fallback(VideoCodec *video_codec, const char *video_codec_to_use, const char *file_extension, bool use_software_video_encoder, gsr_egl *egl) {
+static const AVCodec* select_video_codec_with_fallback(VideoCodec *video_codec, const char *video_codec_to_use, const char *file_extension, bool use_software_video_encoder, gsr_egl *egl, bool *low_power) {
const bool video_codec_auto = strcmp(video_codec_to_use, "auto") == 0;
if(video_codec_auto) {
if(strcmp(file_extension, "webm") == 0) {
@@ -2450,10 +2986,132 @@ static const AVCodec* select_video_codec_with_fallback(VideoCodec *video_codec,
usage();
}
- return pick_video_codec(video_codec, egl, use_software_video_encoder, video_codec_auto, video_codec_to_use, is_flv);
+ return pick_video_codec(video_codec, egl, use_software_video_encoder, video_codec_auto, video_codec_to_use, is_flv, low_power);
+}
+
+static std::vector<AudioDeviceData> create_device_audio_inputs(const std::vector<AudioInput> &audio_inputs, AVCodecContext *audio_codec_context, int num_channels, double num_audio_frames_shift, std::vector<AVFilterContext*> &src_filter_ctx, bool use_amix) {
+ std::vector<AudioDeviceData> audio_track_audio_devices;
+ for(size_t i = 0; i < audio_inputs.size(); ++i) {
+ const auto &audio_input = audio_inputs[i];
+ AVFilterContext *src_ctx = nullptr;
+ if(use_amix)
+ src_ctx = src_filter_ctx[i];
+
+ AudioDeviceData audio_device;
+ audio_device.audio_input = audio_input;
+ audio_device.src_filter_ctx = src_ctx;
+
+ if(audio_input.name.empty()) {
+ audio_device.sound_device.handle = NULL;
+ audio_device.sound_device.frames = 0;
+ } else {
+ const std::string description = "gsr-" + audio_input.name;
+ if(sound_device_get_by_name(&audio_device.sound_device, audio_input.name.c_str(), description.c_str(), num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) {
+ fprintf(stderr, "Error: failed to get \"%s\" audio device\n", audio_input.name.c_str());
+ _exit(1);
+ }
+ }
+
+ audio_device.frame = create_audio_frame(audio_codec_context);
+ audio_device.frame->pts = -audio_codec_context->frame_size * num_audio_frames_shift;
+
+ audio_track_audio_devices.push_back(std::move(audio_device));
+ }
+ return audio_track_audio_devices;
+}
+
+#ifdef GSR_APP_AUDIO
+static AudioDeviceData create_application_audio_audio_input(const MergedAudioInputs &merged_audio_inputs, AVCodecContext *audio_codec_context, int num_channels, double num_audio_frames_shift, gsr_pipewire_audio *pipewire_audio) {
+ AudioDeviceData audio_device;
+ audio_device.frame = create_audio_frame(audio_codec_context);
+ audio_device.frame->pts = -audio_codec_context->frame_size * num_audio_frames_shift;
+
+ char random_str[8];
+ if(!generate_random_characters_standard_alphabet(random_str, sizeof(random_str))) {
+ fprintf(stderr, "gsr error: failed to generate random string\n");
+ _exit(1);
+ }
+ std::string combined_sink_name = "gsr-combined-";
+ combined_sink_name.append(random_str, sizeof(random_str));
+
+ if(!gsr_pipewire_audio_create_virtual_sink(pipewire_audio, combined_sink_name.c_str())) {
+ fprintf(stderr, "gsr error: failed to create virtual sink for application audio\n");
+ _exit(1);
+ }
+
+ combined_sink_name += ".monitor";
+
+ if(sound_device_get_by_name(&audio_device.sound_device, combined_sink_name.c_str(), "gpu-screen-recorder", num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) {
+ fprintf(stderr, "Error: failed to setup audio recording to combined sink\n");
+ _exit(1);
+ }
+
+ std::vector<const char*> audio_devices_sources;
+ for(const auto &audio_input : merged_audio_inputs.audio_inputs) {
+ if(audio_input.type == AudioInputType::DEVICE)
+ audio_devices_sources.push_back(audio_input.name.c_str());
+ }
+
+ bool app_audio_inverted = false;
+ std::vector<const char*> app_names;
+ for(const auto &audio_input : merged_audio_inputs.audio_inputs) {
+ if(audio_input.type == AudioInputType::APPLICATION) {
+ app_names.push_back(audio_input.name.c_str());
+ app_audio_inverted = audio_input.inverted;
+ }
+ }
+
+ if(!audio_devices_sources.empty()) {
+ if(!gsr_pipewire_audio_add_link_from_sources_to_sink(pipewire_audio, audio_devices_sources.data(), audio_devices_sources.size(), combined_sink_name.c_str())) {
+ fprintf(stderr, "gsr error: failed to add application audio link\n");
+ _exit(1);
+ }
+ }
+
+ if(app_audio_inverted) {
+ if(!gsr_pipewire_audio_add_link_from_apps_to_sink_inverted(pipewire_audio, app_names.data(), app_names.size(), combined_sink_name.c_str())) {
+ fprintf(stderr, "gsr error: failed to add application audio link\n");
+ _exit(1);
+ }
+ } else {
+ if(!gsr_pipewire_audio_add_link_from_apps_to_sink(pipewire_audio, app_names.data(), app_names.size(), combined_sink_name.c_str())) {
+ fprintf(stderr, "gsr error: failed to add application audio link\n");
+ _exit(1);
+ }
+ }
+
+ return audio_device;
+}
+#endif
+
+static void set_video_codec_for_image_output(const char *filename, VideoCodec *video_codec, const char **video_codec_to_use) {
+ const bool video_codec_auto = strcmp(*video_codec_to_use, "auto") == 0;
+ if(string_ends_with(filename, ".jpg") || string_ends_with(filename, ".jpeg")) {
+ if(!video_codec_auto)
+ fprintf(stderr, "Warning: expected -k option to be set to 'auto' (or not specified) for jpeg output\n");
+ *video_codec = VideoCodec::JPEG;
+ *video_codec_to_use = "jpeg";
+ } else if(string_ends_with(filename, ".png")) {
+ if(!video_codec_auto)
+ fprintf(stderr, "Warning: expected -k option to be set to 'auto' (or not specified) for png output\n");
+ *video_codec = VideoCodec::PNG;
+ *video_codec_to_use = "png";
+ }
+}
+
+static bool arg_get_boolean_value(std::map<std::string, Arg> &args, const char *arg_name, bool default_value) {
+ auto it = args.find(arg_name);
+ if(it == args.end() || !it->second.value()) {
+ return default_value;
+ } else {
+ assert(it->second.arg_type == ArgType::BOOLEAN);
+ return it->second.typed_value.boolean;
+ }
}
int main(int argc, char **argv) {
+ setlocale(LC_ALL, "C"); // Sigh... stupid C
+
signal(SIGINT, stop_handler);
signal(SIGUSR1, save_replay_handler);
signal(SIGUSR2, toggle_pause_handler);
@@ -2463,6 +3121,13 @@ int main(int argc, char **argv) {
// If this is set to 1 then cuGraphicsGLRegisterImage will fail for egl context with error: invalid OpenGL or DirectX context,
// so we overwrite it
setenv("__GL_THREADED_OPTIMIZATIONS", "0", true);
+ // Forces low latency encoding mode. Use this environment variable until vaapi supports setting this as a parameter.
+ // The downside of this is that it always uses maximum power, which is not ideal for replay mode that runs on system startup.
+ // This option was added in mesa 24.1.4, released in july 17, 2024.
+ // TODO: Add an option to enable/disable this?
+ // Seems like the performance issue is not in encoding, but rendering the frame.
+ // Some frames end up taking 10 times longer. Seems to be an issue with amd gpu power management when letting the application sleep on the cpu side?
+ setenv("AMD_DEBUG", "lowlatencyenc", true);
// Some people set this to nvidia (for nvdec) or vdpau (for nvidia vdpau), which breaks gpu screen recorder since
// nvidia doesn't support vaapi and nvidia-vaapi-driver doesn't support encoding yet.
// Let vaapi find the match vaapi driver instead of forcing a specific one.
@@ -2472,6 +3137,11 @@ int main(int argc, char **argv) {
// Same as above, but for amd/intel
unsetenv("vblank_mode");
+ if(geteuid() == 0) {
+ fprintf(stderr, "Error: don't run gpu-screen-recorder as the root user\n");
+ _exit(1);
+ }
+
if(argc <= 1)
usage_full();
@@ -2488,6 +3158,32 @@ int main(int argc, char **argv) {
_exit(0);
}
+ if(argc == 2 && strcmp(argv[1], "--list-application-audio") == 0) {
+ list_application_audio_command();
+ _exit(0);
+ }
+
+ if(strcmp(argv[1], "--list-capture-options") == 0) {
+ if(argc == 2) {
+ list_capture_options_command(nullptr, GSR_GPU_VENDOR_AMD);
+ _exit(0);
+ } else if(argc == 4) {
+ const char *card_path = argv[2];
+ const char *vendor_str = argv[3];
+ gsr_gpu_vendor vendor;
+ if(!gpu_vendor_from_string(vendor_str, &vendor)) {
+ fprintf(stderr, "Error: \"%s\" is not a valid vendor, expected \"amd\", \"intel\" or \"nvidia\"\n", vendor_str);
+ _exit(1);
+ }
+
+ list_capture_options_command(card_path, vendor);
+ _exit(0);
+ } else {
+ fprintf(stderr, "Error: expected --list-capture-options to be called with either no extra arguments or 2 extra arguments (card path and vendor)\n");
+ _exit(1);
+ }
+ }
+
if(argc == 2 && strcmp(argv[1], "--version") == 0) {
puts(GSR_VERSION);
_exit(0);
@@ -2495,57 +3191,73 @@ int main(int argc, char **argv) {
//av_log_set_level(AV_LOG_TRACE);
+ const bool is_optional = true;
+ const bool is_list = true;
std::map<std::string, Arg> args = {
- { "-w", Arg { {}, false, false } },
- { "-c", Arg { {}, true, false } },
- { "-f", Arg { {}, false, false } },
- { "-s", Arg { {}, true, false } },
- { "-a", Arg { {}, true, true } },
- { "-q", Arg { {}, true, false } },
- { "-o", Arg { {}, true, false } },
- { "-r", Arg { {}, true, false } },
- { "-k", Arg { {}, true, false } },
- { "-ac", Arg { {}, true, false } },
- { "-ab", Arg { {}, true, false } },
- { "-oc", Arg { {}, true, false } },
- { "-fm", Arg { {}, true, false } },
- { "-bm", Arg { {}, true, false } },
- { "-pixfmt", Arg { {}, true, false } },
- { "-v", Arg { {}, true, false } },
- { "-mf", Arg { {}, true, false } }, // TODO: Remove, this exists for backwards compatibility. -df should be used instead
- { "-df", Arg { {}, true, false } },
- { "-sc", Arg { {}, true, false } },
- { "-cr", Arg { {}, true, false } },
- { "-cursor", Arg { {}, true, false } },
- { "-keyint", Arg { {}, true, false } },
- { "-restore-portal-session", Arg { {}, true, false } },
- { "-portal-session-token-filepath", Arg { {}, true, false } },
- { "-encoder", Arg { {}, true, false } },
+ { "-w", Arg { {}, !is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-c", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-f", Arg { {}, !is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-s", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-a", Arg { {}, is_optional, is_list, ArgType::STRING, {false} } },
+ { "-q", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-o", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-r", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-restart-replay-on-save", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-k", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-ac", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-ab", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-oc", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-fm", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-bm", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-pixfmt", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-v", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-gl-debug", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-df", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-sc", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-cr", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-cursor", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-keyint", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
+ { "-restore-portal-session", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-portal-session-token-filepath", Arg { {}, is_optional, !is_list, ArgType::BOOLEAN, {false} } },
+ { "-encoder", Arg { {}, is_optional, !is_list, ArgType::STRING, {false} } },
};
for(int i = 1; i < argc; i += 2) {
- auto it = args.find(argv[i]);
+ const char *arg_name = argv[i];
+ auto it = args.find(arg_name);
if(it == args.end()) {
- fprintf(stderr, "Invalid argument '%s'\n", argv[i]);
+ fprintf(stderr, "Error: invalid argument '%s'\n", arg_name);
usage();
}
if(!it->second.values.empty() && !it->second.list) {
- fprintf(stderr, "Expected argument '%s' to only be specified once\n", argv[i]);
+ fprintf(stderr, "Error: expected argument '%s' to only be specified once\n", arg_name);
usage();
}
if(i + 1 >= argc) {
- fprintf(stderr, "Missing value for argument '%s'\n", argv[i]);
+ fprintf(stderr, "Error: missing value for argument '%s'\n", arg_name);
usage();
}
- it->second.values.push_back(argv[i + 1]);
+ const char *arg_value = argv[i + 1];
+ if(it->second.arg_type == ArgType::BOOLEAN) {
+ if(strcmp(arg_value, "yes") == 0) {
+ it->second.typed_value.boolean = true;
+ } else if(strcmp(arg_value, "no") == 0) {
+ it->second.typed_value.boolean = false;
+ } else {
+ fprintf(stderr, "Error: %s should either be 'yes' or 'no', got: '%s'\n", arg_name, arg_value);
+ usage();
+ }
+ }
+
+ it->second.values.push_back(arg_value);
}
for(auto &it : args) {
if(!it.second.optional && !it.second.value()) {
- fprintf(stderr, "Missing argument '%s'\n", it.first.c_str());
+ fprintf(stderr, "Error: missing argument '%s'\n", it.first.c_str());
usage();
}
}
@@ -2573,10 +3285,12 @@ int main(int argc, char **argv) {
video_codec = VideoCodec::VP8;
} else if(strcmp(video_codec_to_use, "vp9") == 0) {
video_codec = VideoCodec::VP9;
- } else if(strcmp(video_codec_to_use, "h264_vulkan") == 0) {
- video_codec = VideoCodec::H264_VULKAN;
+ //} else if(strcmp(video_codec_to_use, "h264_vulkan") == 0) {
+ // video_codec = VideoCodec::H264_VULKAN;
+ //} else if(strcmp(video_codec_to_use, "hevc_vulkan") == 0) {
+ // video_codec = VideoCodec::HEVC_VULKAN;
} else if(strcmp(video_codec_to_use, "auto") != 0) {
- fprintf(stderr, "Error: -k should either be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit', 'av1_10bit' or 'h264_vulkan', got: '%s'\n", video_codec_to_use);
+ fprintf(stderr, "Error: -k should either be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit' or 'av1_10bit', got: '%s'\n", video_codec_to_use);
usage();
}
@@ -2602,13 +3316,25 @@ int main(int argc, char **argv) {
audio_codec = AudioCodec::OPUS;
}
- int audio_bitrate = 0;
+ int64_t audio_bitrate = 0;
const char *audio_bitrate_str = args["-ab"].value();
if(audio_bitrate_str) {
- if(sscanf(audio_bitrate_str, "%d", &audio_bitrate) != 1) {
+ if(sscanf(audio_bitrate_str, "%" PRIi64, &audio_bitrate) != 1) {
fprintf(stderr, "Error: -ab argument \"%s\" is not an integer\n", audio_bitrate_str);
usage();
}
+
+ if(audio_bitrate < 0) {
+ fprintf(stderr, "Error: -ab is expected to be 0 or larger, got %" PRIi64 "\n", audio_bitrate);
+ usage();
+ }
+
+ if(audio_bitrate > 50000) {
+ fprintf(stderr, "Error: audio bitrate %" PRIi64 "is too high. It's expected to be in kbps, normally in the range 54-300\n", audio_bitrate);
+ usage();
+ }
+
+ audio_bitrate *= 1000LL;
}
float keyint = 2.0;
@@ -2620,7 +3346,7 @@ int main(int argc, char **argv) {
}
if(keyint < 0) {
- fprintf(stderr, "Error: -keyint is expected to be 0 or larger\n");
+ fprintf(stderr, "Error: -keyint is expected to be 0 or larger, got %f\n", keyint);
usage();
}
}
@@ -2638,80 +3364,13 @@ int main(int argc, char **argv) {
}
}
- bool overclock = false;
- const char *overclock_str = args["-oc"].value();
- if(!overclock_str)
- overclock_str = "no";
-
- if(strcmp(overclock_str, "yes") == 0) {
- overclock = true;
- } else if(strcmp(overclock_str, "no") == 0) {
- overclock = false;
- } else {
- fprintf(stderr, "Error: -oc should either be either 'yes' or 'no', got: '%s'\n", overclock_str);
- usage();
- }
-
- bool verbose = true;
- const char *verbose_str = args["-v"].value();
- if(!verbose_str)
- verbose_str = "yes";
-
- if(strcmp(verbose_str, "yes") == 0) {
- verbose = true;
- } else if(strcmp(verbose_str, "no") == 0) {
- verbose = false;
- } else {
- fprintf(stderr, "Error: -v should either be either 'yes' or 'no', got: '%s'\n", verbose_str);
- usage();
- }
-
- bool record_cursor = true;
- const char *record_cursor_str = args["-cursor"].value();
- if(!record_cursor_str)
- record_cursor_str = "yes";
-
- if(strcmp(record_cursor_str, "yes") == 0) {
- record_cursor = true;
- } else if(strcmp(record_cursor_str, "no") == 0) {
- record_cursor = false;
- } else {
- fprintf(stderr, "Error: -cursor should either be either 'yes' or 'no', got: '%s'\n", record_cursor_str);
- usage();
- }
-
- bool date_folders = false;
- const char *date_folders_str = args["-df"].value();
- if(!date_folders_str) {
- date_folders_str = args["-mf"].value();
- if(date_folders_str)
- fprintf(stderr, "Warning: -mf is deprecated, use -df instead\n");
- }
- if(!date_folders_str)
- date_folders_str = "no";
-
- if(strcmp(date_folders_str, "yes") == 0) {
- date_folders = true;
- } else if(strcmp(date_folders_str, "no") == 0) {
- date_folders = false;
- } else {
- fprintf(stderr, "Error: -df should either be either 'yes' or 'no', got: '%s'\n", date_folders_str);
- usage();
- }
-
- bool restore_portal_session = false;
- const char *restore_portal_session_str = args["-restore-portal-session"].value();
- if(!restore_portal_session_str)
- restore_portal_session_str = "no";
-
- if(strcmp(restore_portal_session_str, "yes") == 0) {
- restore_portal_session = true;
- } else if(strcmp(restore_portal_session_str, "no") == 0) {
- restore_portal_session = false;
- } else {
- fprintf(stderr, "Error: -restore-portal-session should either be either 'yes' or 'no', got: '%s'\n", restore_portal_session_str);
- usage();
- }
+ bool overclock = arg_get_boolean_value(args, "-oc", false);
+ const bool verbose = arg_get_boolean_value(args, "-v", true);
+ const bool gl_debug = arg_get_boolean_value(args, "-gl-debug", false);
+ const bool record_cursor = arg_get_boolean_value(args, "-cursor", true);
+ const bool date_folders = arg_get_boolean_value(args, "-df", false);
+ const bool restore_portal_session = arg_get_boolean_value(args, "-restore-portal-session", false);
+ const bool restart_replay_on_save = arg_get_boolean_value(args, "-restart-replay-on-save", false);
const char *portal_session_token_filepath = args["-portal-session-token-filepath"].value();
if(portal_session_token_filepath) {
@@ -2751,12 +3410,38 @@ int main(int argc, char **argv) {
}
const Arg &audio_input_arg = args["-a"];
+
AudioDevices audio_devices;
if(!audio_input_arg.values.empty())
audio_devices = get_pulseaudio_inputs();
- bool uses_amix = false;
- std::vector<MergedAudioInputs> requested_audio_inputs = parse_audio_inputs(audio_devices, audio_input_arg, uses_amix);
+ std::vector<MergedAudioInputs> requested_audio_inputs = parse_audio_inputs(audio_devices, audio_input_arg);
+
+ const bool uses_app_audio = merged_audio_inputs_has_app_audio(requested_audio_inputs);
+ std::vector<std::string> app_audio_names;
+#ifdef GSR_APP_AUDIO
+ gsr_pipewire_audio pipewire_audio;
+ memset(&pipewire_audio, 0, sizeof(pipewire_audio));
+ if(uses_app_audio) {
+ if(!pulseaudio_server_is_pipewire()) {
+ fprintf(stderr, "gsr error: your sound server is not PipeWire. Application audio is only available when running PipeWire audio server\n");
+ _exit(2);
+ }
+
+ if(!gsr_pipewire_audio_init(&pipewire_audio)) {
+ fprintf(stderr, "gsr error: failed to setup PipeWire audio for application audio capture\n");
+ _exit(2);
+ }
+
+ gsr_pipewire_audio_for_each_app(&pipewire_audio, [](const char *app_name, void *userdata) {
+ std::vector<std::string> *app_audio_names = (std::vector<std::string>*)userdata;
+ app_audio_names->push_back(app_name);
+ return true;
+ }, &app_audio_names);
+ }
+#endif
+
+ validate_merged_audio_inputs_app_audio(requested_audio_inputs, app_audio_names);
const char *container_format = args["-c"].value();
if(container_format && strcmp(container_format, "mkv") == 0)
@@ -2770,30 +3455,12 @@ int main(int argc, char **argv) {
if(fps < 1)
fps = 1;
- VideoQuality quality = VideoQuality::VERY_HIGH;
- const char *quality_str = args["-q"].value();
- if(!quality_str)
- quality_str = "very_high";
-
- if(strcmp(quality_str, "medium") == 0) {
- quality = VideoQuality::MEDIUM;
- } else if(strcmp(quality_str, "high") == 0) {
- quality = VideoQuality::HIGH;
- } else if(strcmp(quality_str, "very_high") == 0) {
- quality = VideoQuality::VERY_HIGH;
- } else if(strcmp(quality_str, "ultra") == 0) {
- quality = VideoQuality::ULTRA;
- } else {
- fprintf(stderr, "Error: -q should either be either 'medium', 'high', 'very_high' or 'ultra', got: '%s'\n", quality_str);
- usage();
- }
-
int replay_buffer_size_secs = -1;
const char *replay_buffer_size_secs_str = args["-r"].value();
if(replay_buffer_size_secs_str) {
replay_buffer_size_secs = atoi(replay_buffer_size_secs_str);
- if(replay_buffer_size_secs < 5 || replay_buffer_size_secs > 1200) {
- fprintf(stderr, "Error: option -r has to be between 5 and 1200, was: %s\n", replay_buffer_size_secs_str);
+ if(replay_buffer_size_secs < 2 || replay_buffer_size_secs > 10800) {
+ fprintf(stderr, "Error: option -r has to be between 2 and 10800, was: %s\n", replay_buffer_size_secs_str);
_exit(1);
}
replay_buffer_size_secs += std::ceil(keyint); // Add a few seconds to account of lost packets because of non-keyframe packets skipped
@@ -2827,6 +3494,12 @@ int main(int argc, char **argv) {
disable_prime_run();
}
+ gsr_window *window = gsr_window_create(dpy, wayland);
+ if(!window) {
+ fprintf(stderr, "Error: failed to create window\n");
+ _exit(1);
+ }
+
if(is_portal_capture && is_using_prime_run()) {
fprintf(stderr, "Warning: use of prime-run with -w portal option is currently not supported. Disabling prime-run\n");
disable_prime_run();
@@ -2837,9 +3510,14 @@ int main(int argc, char **argv) {
_exit(1);
}
+ if(video_codec_is_hdr(video_codec) && is_portal_capture) {
+ fprintf(stderr, "Warning: portal capture option doesn't support hdr yet (PipeWire doesn't support hdr), the video will be tonemapped from hdr to sdr\n");
+ video_codec = hdr_video_codec_to_sdr_video_codec(video_codec);
+ }
+
const bool is_monitor_capture = strcmp(window_str.c_str(), "focused") != 0 && !is_portal_capture && contains_non_hex_number(window_str.c_str());
gsr_egl egl;
- if(!gsr_egl_load(&egl, dpy, wayland, is_monitor_capture)) {
+ if(!gsr_egl_load(&egl, window, is_monitor_capture, gl_debug)) {
fprintf(stderr, "gsr error: failed to load opengl\n");
_exit(1);
}
@@ -2867,7 +3545,7 @@ int main(int argc, char **argv) {
}
egl.card_path[0] = '\0';
- if(monitor_capture_use_drm(&egl, wayland)) {
+ if(monitor_capture_use_drm(window, egl.gpu_info.vendor)) {
// TODO: Allow specifying another card, and in other places
if(!gsr_get_valid_card_path(&egl, egl.card_path, is_monitor_capture)) {
fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected or record a single window instead on X11 or record with the -w portal option\n");
@@ -2913,8 +3591,10 @@ int main(int argc, char **argv) {
bitrate_mode = BitrateMode::QP;
} else if(strcmp(bitrate_mode_str, "vbr") == 0) {
bitrate_mode = BitrateMode::VBR;
+ } else if(strcmp(bitrate_mode_str, "cbr") == 0) {
+ bitrate_mode = BitrateMode::CBR;
} else if(strcmp(bitrate_mode_str, "auto") != 0) {
- fprintf(stderr, "Error: -bm should either be either 'auto', 'qp', 'vbr', got: '%s'\n", bitrate_mode_str);
+ fprintf(stderr, "Error: -bm should either be either 'auto', 'qp', 'vbr' or 'cbr', got: '%s'\n", bitrate_mode_str);
usage();
}
@@ -2923,11 +3603,55 @@ int main(int argc, char **argv) {
bitrate_mode = egl.gpu_info.is_steam_deck ? BitrateMode::VBR : BitrateMode::QP;
}
- if(use_software_video_encoder && bitrate_mode != BitrateMode::QP) {
+ if(egl.gpu_info.is_steam_deck && bitrate_mode == BitrateMode::QP) {
+ fprintf(stderr, "Warning: qp bitrate mode is not supported on Steam Deck because of Steam Deck driver bugs. Using vbr instead\n");
+ bitrate_mode = BitrateMode::VBR;
+ }
+
+ if(use_software_video_encoder && bitrate_mode == BitrateMode::VBR) {
fprintf(stderr, "Warning: bitrate mode has been forcefully set to qp because software encoding option doesn't support vbr option\n");
bitrate_mode = BitrateMode::QP;
}
+ const char *quality_str = args["-q"].value();
+ VideoQuality quality = VideoQuality::VERY_HIGH;
+ int64_t video_bitrate = 0;
+
+ if(bitrate_mode == BitrateMode::CBR) {
+ if(!quality_str) {
+ fprintf(stderr, "Error: option '-q' is required when using '-bm cbr' option\n");
+ usage();
+ }
+
+ if(sscanf(quality_str, "%" PRIi64, &video_bitrate) != 1) {
+ fprintf(stderr, "Error: -q argument \"%s\" is not an integer value. When using '-bm cbr' option '-q' is expected to be an integer value\n", quality_str);
+ usage();
+ }
+
+ if(video_bitrate < 0) {
+ fprintf(stderr, "Error: -q is expected to be 0 or larger, got %" PRIi64 "\n", video_bitrate);
+ usage();
+ }
+
+ video_bitrate *= 1000LL;
+ } else {
+ if(!quality_str)
+ quality_str = "very_high";
+
+ if(strcmp(quality_str, "medium") == 0) {
+ quality = VideoQuality::MEDIUM;
+ } else if(strcmp(quality_str, "high") == 0) {
+ quality = VideoQuality::HIGH;
+ } else if(strcmp(quality_str, "very_high") == 0) {
+ quality = VideoQuality::VERY_HIGH;
+ } else if(strcmp(quality_str, "ultra") == 0) {
+ quality = VideoQuality::ULTRA;
+ } else {
+ fprintf(stderr, "Error: -q should either be either 'medium', 'high', 'very_high' or 'ultra', got: '%s'\n", quality_str);
+ usage();
+ }
+ }
+
gsr_color_range color_range = GSR_COLOR_RANGE_LIMITED;
const char *color_range_str = args["-cr"].value();
if(!color_range_str)
@@ -2942,13 +3666,25 @@ int main(int argc, char **argv) {
usage();
}
- const char *screen_region = args["-s"].value();
-
- if(screen_region && strcmp(window_str.c_str(), "focused") != 0) {
- fprintf(stderr, "Error: option -s is only available when using -w focused\n");
+ const char *output_resolution_str = args["-s"].value();
+ if(!output_resolution_str && strcmp(window_str.c_str(), "focused") == 0) {
+ fprintf(stderr, "Error: option -s is required when using -w focused option\n");
usage();
}
+ vec2i output_resolution = {0, 0};
+ if(output_resolution_str) {
+ if(sscanf(output_resolution_str, "%dx%d", &output_resolution.x, &output_resolution.y) != 2) {
+ fprintf(stderr, "Error: invalid value for option -s '%s', expected a value in format WxH\n", output_resolution_str);
+ usage();
+ }
+
+ if(output_resolution.x < 0 || output_resolution.y < 0) {
+ fprintf(stderr, "Error: invalud value for option -s '%s', expected width and height to be greater or equal to 0\n", output_resolution_str);
+ usage();
+ }
+ }
+
bool is_livestream = false;
const char *filename = args["-o"].value();
if(filename) {
@@ -2998,6 +3734,12 @@ int main(int argc, char **argv) {
const bool is_output_piped = strcmp(filename, "/dev/stdout") == 0;
+ set_video_codec_for_image_output(filename, &video_codec, &video_codec_to_use);
+ if(video_codec_is_image(video_codec) && !audio_input_arg.values.empty()) {
+ fprintf(stderr, "Error: can't record audio (-a) when taking a screenshot\n");
+ _exit(1);
+ }
+
AVFormatContext *av_format_context;
// The output format is automatically guessed by the file extension
avformat_alloc_output_context2(&av_format_context, nullptr, container_format, filename);
@@ -3023,23 +3765,21 @@ int main(int argc, char **argv) {
const bool force_no_audio_offset = is_livestream || is_output_piped || (file_extension != "mp4" && file_extension != "mkv" && file_extension != "webm");
const double target_fps = 1.0 / (double)fps;
- if(video_codec_is_hdr(video_codec) && is_portal_capture) {
- fprintf(stderr, "Warning: portal capture option doesn't support hdr yet (pipewire doesn't support hdr), the video will be tonemapped from hdr to sdr\n");
- video_codec = hdr_video_codec_to_sdr_video_codec(video_codec);
- }
-
- audio_codec = select_audio_codec_with_fallback(audio_codec, file_extension, uses_amix);
- const AVCodec *video_codec_f = select_video_codec_with_fallback(&video_codec, video_codec_to_use, file_extension.c_str(), use_software_video_encoder, &egl);
+ const bool uses_amix = merged_audio_inputs_should_use_amix(requested_audio_inputs);
+ if(!video_codec_is_image(video_codec))
+ audio_codec = select_audio_codec_with_fallback(audio_codec, file_extension, uses_amix);
+ bool low_power = false;
+ const AVCodec *video_codec_f = select_video_codec_with_fallback(&video_codec, video_codec_to_use, file_extension.c_str(), use_software_video_encoder, &egl, &low_power);
const gsr_color_depth color_depth = video_codec_to_bit_depth(video_codec);
- gsr_capture *capture = create_capture_impl(window_str, screen_region, wayland, &egl, fps, video_codec, color_range, record_cursor, use_software_video_encoder, restore_portal_session, portal_session_token_filepath, color_depth);
+ gsr_capture *capture = create_capture_impl(window_str, output_resolution, wayland, &egl, fps, video_codec, color_range, record_cursor, restore_portal_session, portal_session_token_filepath, color_depth);
// (Some?) livestreaming services require at least one audio track to work.
// If not audio is provided then create one silent audio track.
if(is_livestream && requested_audio_inputs.empty()) {
fprintf(stderr, "Info: live streaming but no audio track was added. Adding a silent audio track\n");
MergedAudioInputs mai;
- mai.audio_inputs.push_back({ "", "gsr-silent" });
+ mai.audio_inputs.push_back({""});
requested_audio_inputs.push_back(std::move(mai));
}
@@ -3054,7 +3794,7 @@ int main(int argc, char **argv) {
const bool low_latency_recording = is_livestream || is_output_piped;
const enum AVPixelFormat video_pix_fmt = get_pixel_format(video_codec, egl.gpu_info.vendor, use_software_video_encoder);
- AVCodecContext *video_codec_context = create_video_codec_context(video_pix_fmt, quality, fps, video_codec_f, low_latency_recording, egl.gpu_info.vendor, framerate_mode, hdr, color_range, keyint, use_software_video_encoder, bitrate_mode);
+ AVCodecContext *video_codec_context = create_video_codec_context(video_pix_fmt, quality, fps, video_codec_f, low_latency_recording, egl.gpu_info.vendor, framerate_mode, hdr, color_range, keyint, use_software_video_encoder, bitrate_mode, video_codec, video_bitrate);
if(replay_buffer_size_secs == -1)
video_stream = create_stream(av_format_context, video_codec_context);
@@ -3093,7 +3833,6 @@ int main(int argc, char **argv) {
memset(&color_conversion_params, 0, sizeof(color_conversion_params));
color_conversion_params.color_range = color_range;
color_conversion_params.egl = &egl;
- color_conversion_params.source_color = gsr_capture_get_source_color(capture);
color_conversion_params.load_external_image_shader = gsr_capture_uses_external_image(capture);
gsr_video_encoder_get_textures(video_encoder, color_conversion_params.destination_textures, &color_conversion_params.num_destination_textures, &color_conversion_params.destination_color);
@@ -3105,10 +3844,12 @@ int main(int argc, char **argv) {
gsr_color_conversion_clear(&color_conversion);
- if(use_software_video_encoder) {
+ if(video_codec_is_image(video_codec)) {
+ open_video_image(video_codec_context);
+ } else if(use_software_video_encoder) {
open_video_software(video_codec_context, quality, pixel_format, hdr, color_depth, bitrate_mode);
} else {
- open_video_hardware(video_codec_context, quality, very_old_gpu, egl.gpu_info.vendor, pixel_format, hdr, color_depth, bitrate_mode);
+ open_video_hardware(video_codec_context, quality, very_old_gpu, egl.gpu_info.vendor, pixel_format, hdr, color_depth, bitrate_mode, video_codec, low_power);
}
if(video_stream)
avcodec_parameters_from_context(video_stream->codecpar, video_codec_context);
@@ -3116,13 +3857,16 @@ int main(int argc, char **argv) {
int audio_max_frame_size = 1024;
int audio_stream_index = VIDEO_STREAM_INDEX + 1;
for(const MergedAudioInputs &merged_audio_inputs : requested_audio_inputs) {
- const bool use_amix = merged_audio_inputs.audio_inputs.size() > 1;
+ const bool use_amix = audio_inputs_should_use_amix(merged_audio_inputs.audio_inputs);
AVCodecContext *audio_codec_context = create_audio_codec_context(fps, audio_codec, use_amix, audio_bitrate);
AVStream *audio_stream = nullptr;
if(replay_buffer_size_secs == -1)
audio_stream = create_stream(av_format_context, audio_codec_context);
+ if(audio_stream && !merged_audio_inputs.track_name.empty())
+ av_dict_set(&audio_stream->metadata, "title", merged_audio_inputs.track_name.c_str(), 0);
+
open_audio(audio_codec_context);
if(audio_stream)
avcodec_parameters_from_context(audio_stream->codecpar, audio_codec_context);
@@ -3154,34 +3898,18 @@ int main(int argc, char **argv) {
const double audio_startup_time_seconds = force_no_audio_offset ? 0 : audio_codec_get_desired_delay(audio_codec, fps);// * ((double)audio_codec_context->frame_size / 1024.0);
const double num_audio_frames_shift = audio_startup_time_seconds / timeout_sec;
- std::vector<AudioDevice> audio_track_audio_devices;
- for(size_t i = 0; i < merged_audio_inputs.audio_inputs.size(); ++i) {
- auto &audio_input = merged_audio_inputs.audio_inputs[i];
- AVFilterContext *src_ctx = nullptr;
- if(use_amix)
- src_ctx = src_filter_ctx[i];
-
- AudioDevice audio_device;
- audio_device.audio_input = audio_input;
- audio_device.src_filter_ctx = src_ctx;
-
- if(audio_input.name.empty()) {
- audio_device.sound_device.handle = NULL;
- audio_device.sound_device.frames = 0;
- } else {
- if(sound_device_get_by_name(&audio_device.sound_device, audio_input.name.c_str(), audio_input.description.c_str(), num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) {
- fprintf(stderr, "Error: failed to get \"%s\" sound device\n", audio_input.name.c_str());
- _exit(1);
- }
- }
-
- audio_device.frame = create_audio_frame(audio_codec_context);
- audio_device.frame->pts = -audio_codec_context->frame_size * num_audio_frames_shift;
-
- audio_track_audio_devices.push_back(std::move(audio_device));
+ std::vector<AudioDeviceData> audio_track_audio_devices;
+ if(audio_inputs_has_app_audio(merged_audio_inputs.audio_inputs)) {
+ assert(!use_amix);
+#ifdef GSR_APP_AUDIO
+ audio_track_audio_devices.push_back(create_application_audio_audio_input(merged_audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, &pipewire_audio));
+#endif
+ } else {
+ audio_track_audio_devices = create_device_audio_inputs(merged_audio_inputs.audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, src_filter_ctx, use_amix);
}
AudioTrack audio_track;
+ audio_track.name = merged_audio_inputs.track_name;
audio_track.codec_context = audio_codec_context;
audio_track.stream = audio_stream;
audio_track.audio_devices = std::move(audio_track_audio_devices);
@@ -3208,6 +3936,8 @@ int main(int argc, char **argv) {
if(replay_buffer_size_secs == -1) {
AVDictionary *options = nullptr;
av_dict_set(&options, "strict", "experimental", 0);
+ if(video_codec_is_image(video_codec))
+ av_dict_set(&options, "update", "true", 0);
//av_dict_set_int(&av_format_context->metadata, "video_full_range_flag", 1, 0);
int ret = avformat_write_header(av_format_context, &options);
@@ -3220,7 +3950,7 @@ int main(int argc, char **argv) {
}
double fps_start_time = clock_get_monotonic_seconds();
- double frame_timer_start = fps_start_time;
+ //double frame_timer_start = fps_start_time;
int fps_counter = 0;
int damage_fps_counter = 0;
@@ -3232,6 +3962,7 @@ int main(int argc, char **argv) {
std::mutex audio_filter_mutex;
const double record_start_time = clock_get_monotonic_seconds();
+ std::atomic<double> replay_start_time(record_start_time);
std::deque<std::shared_ptr<PacketData>> frame_data_queue;
bool frames_erased = false;
@@ -3244,7 +3975,7 @@ int main(int argc, char **argv) {
memset(empty_audio, 0, audio_buffer_size);
for(AudioTrack &audio_track : audio_tracks) {
- for(AudioDevice &audio_device : audio_track.audio_devices) {
+ for(AudioDeviceData &audio_device : audio_track.audio_devices) {
audio_device.thread = std::thread([&]() mutable {
const AVSampleFormat sound_device_sample_format = audio_format_to_sample_format(audio_codec_context_get_audio_format(audio_track.codec_context));
// TODO: Always do conversion for now. This fixes issue with stuttering audio on pulseaudio with opus + multiple audio sources merged
@@ -3350,7 +4081,7 @@ int main(int argc, char **argv) {
ret = avcodec_send_frame(audio_track.codec_context, audio_device.frame);
if(ret >= 0) {
// TODO: Move to separate thread because this could write to network (for example when livestreaming)
- receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
+ receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, replay_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
} else {
fprintf(stderr, "Failed to encode audio!\n");
}
@@ -3382,7 +4113,7 @@ int main(int argc, char **argv) {
ret = avcodec_send_frame(audio_track.codec_context, audio_device.frame);
if(ret >= 0) {
// TODO: Move to separate thread because this could write to network (for example when livestreaming)
- receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
+ receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, replay_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
} else {
fprintf(stderr, "Failed to encode audio!\n");
}
@@ -3416,7 +4147,7 @@ int main(int argc, char **argv) {
err = avcodec_send_frame(audio_track.codec_context, aframe);
if(err >= 0){
// TODO: Move to separate thread because this could write to network (for example when livestreaming)
- receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, aframe->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
+ receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, aframe->pts, av_format_context, replay_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
} else {
fprintf(stderr, "Failed to encode audio!\n");
}
@@ -3432,7 +4163,7 @@ int main(int argc, char **argv) {
}
// Set update_fps to 24 to test if duplicate/delayed frames cause video/audio desync or too fast/slow video.
- const double update_fps = fps + 190;
+ //const double update_fps = fps + 190;
bool should_stop_error = false;
int64_t video_pts_counter = 0;
@@ -3446,7 +4177,7 @@ int main(int argc, char **argv) {
bool use_damage_tracking = false;
gsr_damage damage;
memset(&damage, 0, sizeof(damage));
- if(gsr_egl_get_display_server(&egl) == GSR_DISPLAY_SERVER_X11) {
+ if(gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_X11) {
gsr_damage_init(&damage, &egl, record_cursor);
use_damage_tracking = true;
}
@@ -3454,11 +4185,15 @@ int main(int argc, char **argv) {
if(is_monitor_capture)
gsr_damage_set_target_monitor(&damage, window_str.c_str());
+ double last_capture_seconds = record_start_time;
+ bool wait_until_frame_time_elapsed = false;
+ const bool is_image_output = video_codec_is_image(video_codec);
+
while(running) {
const double frame_start = clock_get_monotonic_seconds();
- while(gsr_egl_process_event(&egl)) {
- gsr_damage_on_event(&damage, gsr_egl_get_event_data(&egl));
+ while(gsr_window_process_event(window)) {
+ gsr_damage_on_event(&damage, gsr_window_get_event_data(window));
gsr_capture_on_event(capture, &egl);
}
gsr_damage_tick(&damage);
@@ -3496,7 +4231,7 @@ int main(int argc, char **argv) {
++fps_counter;
const double time_now = clock_get_monotonic_seconds();
- const double frame_timer_elapsed = time_now - frame_timer_start;
+ //const double frame_timer_elapsed = time_now - frame_timer_start;
const double elapsed = time_now - fps_start_time;
if (elapsed >= 1.0) {
if(verbose) {
@@ -3507,51 +4242,69 @@ int main(int argc, char **argv) {
damage_fps_counter = 0;
}
- double frame_time_overflow = frame_timer_elapsed - target_fps;
- if ((frame_time_overflow >= 0.0 || video_pts_counter == 0) && damaged) {
+ const double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset;
+ const double time_since_last_frame_captured_seconds = this_video_frame_time - last_capture_seconds;
+ double frame_time_overflow = time_since_last_frame_captured_seconds - target_fps;
+ const bool frame_timeout = frame_time_overflow >= 0.0;
+
+ bool force_frame_capture = wait_until_frame_time_elapsed && frame_timeout;
+ bool allow_capture = !wait_until_frame_time_elapsed || force_frame_capture;
+ if(framerate_mode == FramerateMode::CONTENT) {
+ force_frame_capture = false;
+ allow_capture = frame_timeout;
+ }
+
+ bool frame_captured = false;
+ if((damaged || force_frame_capture) && allow_capture && !paused) {
+ frame_captured = true;
+ frame_time_overflow = std::min(std::max(0.0, frame_time_overflow), target_fps);
+ last_capture_seconds = this_video_frame_time - frame_time_overflow;
+ wait_until_frame_time_elapsed = false;
+
gsr_damage_clear(&damage);
if(capture->clear_damage)
capture->clear_damage(capture);
- frame_time_overflow = std::min(std::max(0.0, frame_time_overflow), target_fps);
- frame_timer_start = time_now - frame_time_overflow;
- const double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset;
+ // TODO: Dont do this if no damage?
+ egl.glClear(0);
+ gsr_capture_capture(capture, video_frame, &color_conversion);
+ gsr_egl_swap_buffers(&egl);
+ gsr_video_encoder_copy_textures_to_frame(video_encoder, video_frame, &color_conversion);
+
+ if(hdr && !hdr_metadata_set && replay_buffer_size_secs == -1 && add_hdr_metadata_to_video_stream(capture, video_stream))
+ hdr_metadata_set = true;
+
const int64_t expected_frames = std::round((this_video_frame_time - record_start_time) / target_fps);
- const int num_frames = framerate_mode == FramerateMode::CONSTANT ? std::max((int64_t)0LL, expected_frames - video_pts_counter) : 1;
-
- if(num_frames > 0 && !paused) {
- egl.glClear(0);
- gsr_capture_capture(capture, video_frame, &color_conversion);
- gsr_egl_swap_buffers(&egl);
- gsr_video_encoder_copy_textures_to_frame(video_encoder, video_frame, &color_conversion);
-
- if(hdr && !hdr_metadata_set && replay_buffer_size_secs == -1 && add_hdr_metadata_to_video_stream(capture, video_stream))
- hdr_metadata_set = true;
-
- // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again
- for(int i = 0; i < num_frames; ++i) {
- if(framerate_mode == FramerateMode::CONSTANT) {
- video_frame->pts = video_pts_counter + i;
- } else {
- video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE;
- const bool same_pts = video_frame->pts == video_prev_pts;
- video_prev_pts = video_frame->pts;
- if(same_pts)
- continue;
- }
+ const int num_missed_frames = std::max((int64_t)1LL, expected_frames - video_pts_counter);
- int ret = avcodec_send_frame(video_codec_context, video_frame);
- if(ret == 0) {
- // TODO: Move to separate thread because this could write to network (for example when livestreaming)
- receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context,
- record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
- } else {
- fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret));
- }
+ // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again
+ const int num_frames_to_encode = framerate_mode == FramerateMode::CONSTANT ? num_missed_frames : 1;
+ for(int i = 0; i < num_frames_to_encode; ++i) {
+ if(framerate_mode == FramerateMode::CONSTANT) {
+ video_frame->pts = video_pts_counter + i;
+ } else {
+ video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE;
+ const bool same_pts = video_frame->pts == video_prev_pts;
+ video_prev_pts = video_frame->pts;
+ if(same_pts)
+ continue;
}
- video_pts_counter += num_frames;
+ int ret = avcodec_send_frame(video_codec_context, video_frame);
+ if(ret == 0) {
+ // TODO: Move to separate thread because this could write to network (for example when livestreaming)
+ receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context,
+ replay_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
+ if(is_image_output) {
+ running = 0;
+ break;
+ }
+ } else {
+ fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret));
+ }
}
+
+ video_pts_counter += num_frames_to_encode;
}
if(toggle_pause == 1) {
@@ -3574,6 +4327,7 @@ int main(int argc, char **argv) {
fflush(stdout);
if(recording_saved_script)
run_recording_saved_script_async(recording_saved_script, save_replay_output_filepath.c_str(), "replay");
+
std::lock_guard<std::mutex> lock(write_output_mutex);
save_replay_packets.clear();
}
@@ -3581,13 +4335,39 @@ int main(int argc, char **argv) {
if(save_replay == 1 && !save_replay_thread.valid() && replay_buffer_size_secs != -1) {
save_replay = 0;
save_replay_async(video_codec_context, VIDEO_STREAM_INDEX, audio_tracks, frame_data_queue, frames_erased, filename, container_format, file_extension, write_output_mutex, date_folders, hdr, capture);
+
+ std::lock_guard<std::mutex> lock(write_output_mutex);
+ if(restart_replay_on_save) {
+ frame_data_queue.clear();
+ frames_erased = true;
+ replay_start_time = clock_get_monotonic_seconds() - paused_time_offset;
+ }
}
- double frame_end = clock_get_monotonic_seconds();
- double frame_sleep_fps = 1.0 / update_fps;
- double sleep_time = frame_sleep_fps - (frame_end - frame_start);
- if(sleep_time > 0.0)
- av_usleep(sleep_time * 1000.0 * 1000.0);
+ const double frame_end = clock_get_monotonic_seconds();
+ const double time_at_frame_end = frame_end - paused_time_offset;
+ const double time_elapsed_total = time_at_frame_end - record_start_time;
+ const int64_t frames_elapsed = (int64_t)(time_elapsed_total / target_fps);
+ const double time_at_next_frame = (frames_elapsed + 1) * target_fps;
+ double time_to_next_frame = time_at_next_frame - time_elapsed_total;
+ if(time_to_next_frame > target_fps*1.1)
+ time_to_next_frame = target_fps;
+
+ const double frame_time = frame_end - frame_start;
+ const bool frame_deadline_missed = frame_time > target_fps;
+ if(time_to_next_frame >= 0.0 && !frame_deadline_missed && frame_captured)
+ av_usleep(time_to_next_frame * 1000.0 * 1000.0);
+ else {
+ if(paused)
+ av_usleep(20.0 * 1000.0); // 20 milliseconds
+ else if(frame_deadline_missed)
+ {}
+ else if(framerate_mode == FramerateMode::CONTENT || !frame_captured)
+ av_usleep(2.8 * 1000.0); // 2.8 milliseconds
+ else if(!frame_captured)
+ av_usleep(1.0 * 1000.0); // 1 milliseconds
+ wait_until_frame_time_elapsed = true;
+ }
}
running = 0;
@@ -3603,7 +4383,7 @@ int main(int argc, char **argv) {
}
for(AudioTrack &audio_track : audio_tracks) {
- for(AudioDevice &audio_device : audio_track.audio_devices) {
+ for(auto &audio_device : audio_track.audio_devices) {
audio_device.thread.join();
sound_device_close(&audio_device.sound_device);
}
@@ -3616,13 +4396,18 @@ int main(int argc, char **argv) {
fprintf(stderr, "Failed to write trailer\n");
}
- if(replay_buffer_size_secs == -1 && !(output_format->flags & AVFMT_NOFILE))
+ if(replay_buffer_size_secs == -1 && !(output_format->flags & AVFMT_NOFILE)) {
avio_close(av_format_context->pb);
+ avformat_free_context(av_format_context);
+ }
gsr_damage_deinit(&damage);
gsr_color_conversion_deinit(&color_conversion);
gsr_video_encoder_destroy(video_encoder, video_codec_context);
gsr_capture_destroy(capture, video_codec_context);
+#ifdef GSR_APP_AUDIO
+ gsr_pipewire_audio_deinit(&pipewire_audio);
+#endif
if(replay_buffer_size_secs == -1 && recording_saved_script)
run_recording_saved_script_async(recording_saved_script, filename, "regular");
@@ -3632,6 +4417,9 @@ int main(int argc, char **argv) {
//XCloseDisplay(dpy);
}
+ //gsr_egl_unload(&egl);
+ //gsr_window_destroy(&window);
+
//av_frame_free(&video_frame);
free(empty_audio);
// We do an _exit here because cuda uses at_exit to do _something_ that causes the program to freeze,