diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 1270 |
1 files changed, 716 insertions, 554 deletions
diff --git a/src/main.cpp b/src/main.cpp index 81b325c..7d4a47e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,9 +13,12 @@ extern "C" { #include "../include/encoder/video/vaapi.h" #include "../include/encoder/video/vulkan.h" #include "../include/encoder/video/software.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" @@ -110,7 +113,9 @@ enum class VideoCodec { VP8, VP9, H264_VULKAN, - HEVC_VULKAN + HEVC_VULKAN, + JPEG, + PNG }; enum class AudioCodec { @@ -214,6 +219,16 @@ static bool video_codec_is_vulkan(VideoCodec video_codec) { } } +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; @@ -578,7 +593,22 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt, if (codec_context->codec_id == AV_CODEC_ID_MPEG1VIDEO) codec_context->mb_decision = 2; - if(!use_software_video_encoder && vendor != GSR_GPU_VENDOR_NVIDIA && bitrate_mode != BitrateMode::CBR) { + 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) { @@ -713,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) { @@ -1067,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] [-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>] [-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 @@ -1075,182 +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.\n"); - fprintf(stderr, " 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\n"); - fprintf(stderr, " 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, " Run GPU Screen Recorder with the --list-capture-options option to list valid values for this option.\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 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"); - fprintf(stderr, " 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"); - fprintf(stderr, " 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"); - 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 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"); - 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 sources 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, " The audio name can also be prefixed with \"device:\", for example: -a \"device:alsa_output.pci-0000_00_1b.0.analog-stereo.monitor\".\n"); -#ifdef GSR_APP_AUDIO - fprintf(stderr, " To record audio from an application then prefix the audio name with \"app:\", for example: -a \"app:Brave\".\n"); - fprintf(stderr, " To record audio from all applications except the provided use prefix the audio name with \"app-inverse:\", for example: -a \"app-inverse:Brave\".\n"); - fprintf(stderr, " \"app:\" and \"app-inverse:\" can't be mixed in one audio track.\n"); - fprintf(stderr, " 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"); - fprintf(stderr, " Recording application audio is only possible when the sound server on the system is PipeWire.\n"); -#endif - fprintf(stderr, " If the audio name is an empty string then the argument is ignored.\n"); - fprintf(stderr, " Optional, no audio track is added by default.\n"); - fprintf(stderr, " Run GPU Screen Recorder with the --list-audio-devices option to list valid audio device names.\n"); - fprintf(stderr, " 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"); - fprintf(stderr, " for example when trying to record audio from an application that hasn't started yet.\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -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"); - fprintf(stderr, " 'high' is the recommended option when live streaming or when you have a slower harddrive.\n"); - fprintf(stderr, " When using '-bm cbr' option then this is option is instead used to specify the video bitrate in kbps.\n"); - fprintf(stderr, " Optional when using '-bm qp' or '-bm vbr' options, set to 'very_high' be default.\n"); - fprintf(stderr, " Required when using '-bm cbr' option.\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' or 'av1_10bit'.\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 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"); - fprintf(stderr, " Optional, by default the bitrate is 128kbps for opus and flac and 160kbps 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), 'vbr' (variable bitrate) or 'cbr' (constant 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, " Note: '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'. 'cpu' option 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. 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 (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-capture-options\n"); - fprintf(stderr, " List available capture options. Lists capture options in the following format (prints them to stdout and exits):\n"); - fprintf(stderr, " <option>\n"); - fprintf(stderr, " <monitor_name>|<resolution>\n"); - fprintf(stderr, " For example:\n"); - fprintf(stderr, " window\n"); - fprintf(stderr, " DP-1|1920x1080\n"); - fprintf(stderr, " The <option> and <monitor_name> is the name that can be passed to GPU Screen Recorder with the -w option.\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " --list-audio-devices\n"); - fprintf(stderr, " List audio devices. 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, " alsa_output.pci-0000_0c_00.4.iec958-stereo|Monitor of Starship/Matisse HD Audio Controller Digital Stereo (IEC958)\n"); - fprintf(stderr, " The <audio_device_name> is the name that can be passed to GPU Screen Recorder with the -a option.\n"); - fprintf(stderr, "\n"); -#ifdef GSR_APP_AUDIO - fprintf(stderr, " --list-application-audio\n"); - fprintf(stderr, " Lists application that you can record from (with the -aa or -aai option) (prints them to stdout and exits), for example:\n"); - fprintf(stderr, " firefox\n"); - fprintf(stderr, " csgo\n"); - fprintf(stderr, " These names are the application audio names that can be passed to GPU Screen Recorder with the -aa option.\n"); - fprintf(stderr, "\n"); -#endif - 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 fps and damage info once per second. 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 -a default_input -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); - fprintf(stderr, " %s -w screen -f 60 -a default_output -bm cbr -q 15000 -o \"$HOME/Videos/video.mp4\"\n", program_name); -#ifdef GSR_APP_AUDIO - fprintf(stderr, " %s -w screen -f 60 -a \"app:firefox|app:csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name); - fprintf(stderr, " %s -w screen -f 60 -a \"app-inverse:firefox|app-inverse:csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name); - fprintf(stderr, " %s -w screen -f 60 -a \"default-input|app-inverse:Brave\" -o \"$HOME/Videos/video.mp4\"\n", program_name); -#endif + 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); } @@ -1307,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() { @@ -1346,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; @@ -1400,21 +1447,21 @@ 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; AVFrame *frame = nullptr; std::thread thread; // TODO: Instead of having a thread for each track, have one thread for all threads and read the data with non-blocking read - std::string combined_sink_name; }; // 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; @@ -1434,17 +1481,17 @@ 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; } @@ -1463,10 +1510,10 @@ static bool add_hdr_metadata_to_video_stream(gsr_capture *cap, AVStream *video_s #endif if(!content_light_level_added) - av_freep(light_metadata); + av_freep(&light_metadata); if(!mastering_display_metadata_added) - av_freep(mastering_display_metadata); + av_freep(&mastering_display_metadata); // Return true even on failure because we dont want to retry adding hdr metadata on failure return true; @@ -1479,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; @@ -1500,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; @@ -1538,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; } @@ -1634,65 +1683,63 @@ static bool string_starts_with(const std::string &str, const char *substr) { return (int)str.size() >= len && memcmp(str.data(), substr, len) == 0; } -static const AudioInput* get_audio_device_by_name(const std::vector<AudioInput> &audio_inputs, const std::string &name) { - for(const auto &audio_input : audio_inputs) { - if(audio_input.name == name) - return &audio_input; +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 std::vector<AudioInput> parse_audio_input_arg(const char *str, const AudioDevices &audio_devices) { - std::vector<AudioInput> audio_inputs; +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); 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.description = audio_input.name; audio_input.type = AudioInputType::APPLICATION; audio_input.inverted = false; - audio_inputs.push_back(std::move(audio_input)); + 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.description = audio_input.name; audio_input.type = AudioInputType::APPLICATION; audio_input.inverted = true; - audio_inputs.push_back(std::move(audio_input)); + 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; - audio_inputs.push_back(std::move(audio_input)); + result.audio_inputs.push_back(std::move(audio_input)); return true; } else { - const bool name_is_existing_audio_device = get_audio_device_by_name(audio_devices.audio_inputs, audio_input.name); - const size_t index = audio_input.name.find('/'); - if(!name_is_existing_audio_device && 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); - } audio_input.type = AudioInputType::DEVICE; - audio_inputs.push_back(std::move(audio_input)); + result.audio_inputs.push_back(std::move(audio_input)); return true; } }); - return audio_inputs; -} -static std::vector<AudioInput> parse_app_audio_input_arg(const char *str) { - std::vector<AudioInput> audio_inputs; - split_string(str, '|', [&](const char *sub, size_t size) { - AudioInput audio_input; - audio_input.name.assign(sub, size); - audio_input.description = audio_input.name; - audio_input.type = AudioInputType::APPLICATION; - audio_inputs.push_back(std::move(audio_input)); - return true; - }); - return audio_inputs; + return result; } // TODO: Does this match all livestreaming cases? @@ -1714,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 @@ -1749,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]; @@ -1803,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(¶ms); + return video_encoder; + } + if(use_software_video_encoder) { gsr_video_encoder_software_params params; params.egl = egl; @@ -1901,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() { @@ -1909,6 +1990,14 @@ 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) { @@ -1916,11 +2005,13 @@ static void list_system_info(bool wayland) { bool supports_app_audio = false; #ifdef GSR_APP_AUDIO supports_app_audio = pulseaudio_server_is_pipewire(); - gsr_pipewire_audio audio; - if(gsr_pipewire_audio_init(&audio)) - gsr_pipewire_audio_deinit(&audio); - else - supports_app_audio = false; + 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"); } @@ -1937,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) { @@ -1959,6 +2051,10 @@ static const AVCodec* get_ffmpeg_video_codec(VideoCodec video_codec, gsr_gpu_ven 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; } @@ -2029,26 +2125,29 @@ static void list_supported_video_codecs(gsr_egl *egl, bool wayland) { puts("vp8"); if(supported_video_codecs.vp9.supported) puts("vp9"); + 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); @@ -2057,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 @@ -2115,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; } } @@ -2138,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); @@ -2179,10 +2284,12 @@ static bool app_audio_query_callback(const char *app_name, void*) { static void list_application_audio_command() { #ifdef GSR_APP_AUDIO - 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); + 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 @@ -2190,7 +2297,9 @@ static void list_application_audio_command() { _exit(0); } -static void list_capture_options_command() { +// |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) { @@ -2212,36 +2321,90 @@ static void list_capture_options_command() { disable_prime_run(); } - gsr_egl egl; - if(!gsr_egl_load(&egl, dpy, wayland, false)) { - fprintf(stderr, "gsr error: failed to load opengl\n"); + gsr_window *window = gsr_window_create(dpy, wayland); + if(!window) { + fprintf(stderr, "Error: failed to create window\n"); _exit(1); } - egl.card_path[0] = '\0'; - if(monitor_capture_use_drm(&egl, wayland)) { - // TODO: Allow specifying another card, and in other places - if(!gsr_get_valid_card_path(&egl, egl.card_path, false)) { - fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected\n"); - _exit(23); + 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); } - } - av_log_set_level(AV_LOG_FATAL); - list_supported_capture_options(&egl, wayland); + 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 use_software_video_encoder, bool restore_portal_session, const char *portal_session_token_filepath, + bool record_cursor, bool restore_portal_session, const char *portal_session_token_filepath, gsr_color_depth color_depth) { Window src_window_id = None; @@ -2284,59 +2447,13 @@ static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_re _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; @@ -2349,7 +2466,6 @@ static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_re 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) @@ -2370,7 +2486,7 @@ static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_re } } 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); } @@ -2400,7 +2516,10 @@ static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_re } 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_is_vulkan(video_codec)) @@ -2410,10 +2529,19 @@ static AVPixelFormat get_pixel_format(VideoCodec video_codec, gsr_gpu_vendor ven } } +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()) @@ -2448,43 +2576,42 @@ static void match_app_audio_input_to_available_apps(const std::vector<AudioInput // 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, const Arg &app_audio_input_arg, const Arg &app_audio_input_inverted_arg) { +static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &audio_devices, const Arg &audio_input_arg) { std::vector<MergedAudioInputs> requested_audio_inputs; 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, audio_devices)}); + 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; - } - - const AudioInput* existing_audio_input = get_audio_device_by_name(audio_devices.audio_inputs, request_audio_input.name); - if(existing_audio_input) { - if(request_audio_input.description.empty()) - request_audio_input.description = "gsr-" + existing_audio_input->description; match = true; + } 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; } 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()) @@ -2492,28 +2619,11 @@ static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &aud 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); } } } - for(const char *app_audio_input : app_audio_input_arg.values) { - if(!app_audio_input || app_audio_input[0] == '\0') - continue; - - requested_audio_inputs.push_back({parse_app_audio_input_arg(app_audio_input)}); - } - - for(const char *app_audio_input : app_audio_input_inverted_arg.values) { - if(!app_audio_input || app_audio_input[0] == '\0') - continue; - - requested_audio_inputs.push_back({parse_app_audio_input_arg(app_audio_input)}); - for(auto &audio_input : requested_audio_inputs.back().audio_inputs) { - audio_input.inverted = true; - } - } - return requested_audio_inputs; } @@ -2621,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"; @@ -2633,12 +2743,14 @@ static const char* video_codec_to_string(VideoCodec video_codec) { 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 bool video_codec_only_supports_low_power_mode(const gsr_supported_video_codecs &supported_video_codecs, VideoCodec video_codec) { - switch(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; @@ -2650,6 +2762,8 @@ static bool video_codec_only_supports_low_power_mode(const gsr_supported_video_c 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; } @@ -2725,6 +2839,11 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo 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) { @@ -2786,6 +2905,12 @@ static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bo video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor); break; } + case VideoCodec::JPEG: + case VideoCodec::PNG: { + // TODO: + //assert(false); + break; + } } } @@ -2864,15 +2989,15 @@ static const AVCodec* select_video_codec_with_fallback(VideoCodec *video_codec, 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<AudioDevice> 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<AudioDevice> audio_track_audio_devices; +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]; - AudioDevice audio_device; + AudioDeviceData audio_device; audio_device.audio_input = audio_input; audio_device.src_filter_ctx = src_ctx; @@ -2880,7 +3005,8 @@ static std::vector<AudioDevice> create_device_audio_inputs(const std::vector<Aud 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) { + 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); } @@ -2895,28 +3021,35 @@ static std::vector<AudioDevice> create_device_audio_inputs(const std::vector<Aud } #ifdef GSR_APP_AUDIO -static AudioDevice 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) { - AudioDevice audio_device; +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: ailed to generate random string\n"); + 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); } - audio_device.combined_sink_name = "gsr-combined-"; - audio_device.combined_sink_name.append(random_str, sizeof(random_str)); - if(sound_device_create_combined_sink_connect(&audio_device.sound_device, audio_device.combined_sink_name.c_str(), num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) { + 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_sources; + std::vector<const char*> audio_devices_sources; for(const auto &audio_input : merged_audio_inputs.audio_inputs) { if(audio_input.type == AudioInputType::DEVICE) - audio_sources.push_back(audio_input.name.c_str()); + audio_devices_sources.push_back(audio_input.name.c_str()); } bool app_audio_inverted = false; @@ -2928,20 +3061,20 @@ static AudioDevice create_application_audio_audio_input(const MergedAudioInputs } } - if(!audio_sources.empty()) { - if(!gsr_pipewire_audio_add_link_from_sources_to_sink(pipewire_audio, audio_sources.data(), audio_sources.size(), audio_device.combined_sink_name.c_str())) { + 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(), audio_device.combined_sink_name.c_str())) { + 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(), audio_device.combined_sink_name.c_str())) { + 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); } @@ -2951,6 +3084,31 @@ static AudioDevice create_application_audio_audio_input(const MergedAudioInputs } #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 @@ -2979,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(); @@ -3000,9 +3163,25 @@ int main(int argc, char **argv) { _exit(0); } - if(argc == 2 && strcmp(argv[1], "--list-capture-options") == 0) { - list_capture_options_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) { @@ -3012,60 +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 } }, -#ifdef GSR_APP_AUDIO - { "-aa", Arg { {}, true, true } }, // TODO: Remove soon since this is deprecated. User should use -a with app: instead - { "-aai", Arg { {}, true, true } }, // TODO: Remove soon since this is deprecated. User should use -a with app-inverse: instead -#endif - { "-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 } }, - { "-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(); } } @@ -3172,75 +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 = "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) { @@ -3280,20 +3410,12 @@ int main(int argc, char **argv) { } const Arg &audio_input_arg = args["-a"]; - const Arg &app_audio_input_arg = args["-aa"]; - const Arg &app_audio_input_inverted_arg = args["-aai"]; AudioDevices audio_devices; if(!audio_input_arg.values.empty()) audio_devices = get_pulseaudio_inputs(); - - if(!app_audio_input_arg.values.empty()) - fprintf(stderr, "gsr warning: argument -aa is deprecated, use -a with app: prefix instead, for example: -a \"app:Brave\"\n"); - if(!app_audio_input_inverted_arg.values.empty()) - fprintf(stderr, "gsr warning: argument -aai is deprecated, use -a with app-inverse: prefix instead, for example: -a \"app-inverse:Brave\"\n"); - - std::vector<MergedAudioInputs> requested_audio_inputs = parse_audio_inputs(audio_devices, audio_input_arg, app_audio_input_arg, app_audio_input_inverted_arg); + 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; @@ -3301,13 +3423,13 @@ int main(int argc, char **argv) { gsr_pipewire_audio pipewire_audio; memset(&pipewire_audio, 0, sizeof(pipewire_audio)); if(uses_app_audio) { - if(!gsr_pipewire_audio_init(&pipewire_audio)) { - fprintf(stderr, "gsr error: failed to setup PipeWire audio for application audio capture. The likely reason for this failure is that your sound server is not PipeWire. Application audio is only available when running PipeWire audio server.\n"); + 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(!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"); + if(!gsr_pipewire_audio_init(&pipewire_audio)) { + fprintf(stderr, "gsr error: failed to setup PipeWire audio for application audio capture\n"); _exit(2); } @@ -3337,8 +3459,8 @@ int main(int argc, char **argv) { 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 @@ -3372,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(); @@ -3382,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); } @@ -3412,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"); @@ -3601,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); @@ -3626,25 +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); - } - const bool uses_amix = merged_audio_inputs_should_use_amix(requested_audio_inputs); - audio_codec = select_audio_codec_with_fallback(audio_codec, file_extension, uses_amix); + 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, output_resolution, 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)); } @@ -3709,7 +3844,9 @@ 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, video_codec, low_power); @@ -3727,6 +3864,9 @@ int main(int argc, char **argv) { 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); @@ -3758,7 +3898,7 @@ 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; + 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 @@ -3769,6 +3909,7 @@ int main(int argc, char **argv) { } 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); @@ -3795,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); @@ -3819,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; @@ -3831,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 @@ -3937,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"); } @@ -3969,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"); } @@ -4003,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"); } @@ -4033,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; } @@ -4043,12 +4187,13 @@ int main(int argc, char **argv) { 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); @@ -4149,7 +4294,11 @@ int main(int argc, char **argv) { 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); + 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)); } @@ -4178,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(); } @@ -4185,6 +4335,13 @@ 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; + } } const double frame_end = clock_get_monotonic_seconds(); @@ -4226,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); } @@ -4239,8 +4396,10 @@ 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); @@ -4258,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, |