mirror of
https://repo.dec05eba.com/gpu-screen-recorder
synced 2026-04-05 19:16:36 +09:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
015570ca75 | ||
|
|
2de33ded99 | ||
|
|
a8b26621d4 | ||
|
|
1b5cde0789 | ||
|
|
62bbdd7c30 | ||
|
|
b250731b1c | ||
|
|
ac5003dea6 | ||
|
|
e869b55878 | ||
|
|
38f1ef0f9b | ||
|
|
d217aec053 | ||
|
|
d088586296 | ||
|
|
ddc3871b27 | ||
|
|
3b3d8e893d | ||
|
|
a3b9b89a7f | ||
|
|
86df5a580e | ||
|
|
1b8d3b3f56 | ||
|
|
2ee6c9dc92 | ||
|
|
7babffaa01 | ||
|
|
4ac5da0c1c | ||
|
|
1cb9066dbb | ||
|
|
31ca53540a | ||
|
|
26e9029579 | ||
|
|
cafcda1022 | ||
|
|
450bc0ac4a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -23,3 +23,6 @@ gsr-kms-server
|
||||
*.mov
|
||||
*.webm
|
||||
*.ts
|
||||
*.jpg
|
||||
*.jpeg
|
||||
*.png
|
||||
|
||||
12
README.md
12
README.md
@@ -7,6 +7,8 @@ similar to shadowplay on windows. This is the fastest screen recording tool for
|
||||
This screen recorder can be used for recording your desktop offline, for live streaming and for nvidia shadowplay-like instant replay,
|
||||
where only the last few minutes are saved.
|
||||
|
||||
This software can also take screenshots.
|
||||
|
||||
This is a cli-only tool, if you want an UI for this check out [GPU Screen Recorder GTK](https://git.dec05eba.com/gpu-screen-recorder-gtk/) or if you prefer a ShadowPlay-like UI then check out [GPU Screen Recorder UI](https://git.dec05eba.com/gpu-screen-recorder-ui/).
|
||||
|
||||
Supported video codecs:
|
||||
@@ -20,7 +22,10 @@ Supported audio codecs:
|
||||
* Opus (default)
|
||||
* AAC
|
||||
|
||||
## Note
|
||||
Supported image formats:
|
||||
* JPEG
|
||||
* PNG
|
||||
|
||||
This software works on X11 and Wayland on AMD, Intel and NVIDIA.
|
||||
### TEMPORARY ISSUES
|
||||
1) Videos are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player. You can try saving the video into a .mkv file instead as some software may have better support for .mkv files (such as kdenlive). You can use the "-fm cfr" option to to use constant framerate mode.
|
||||
@@ -29,7 +34,7 @@ This software works on X11 and Wayland on AMD, Intel and NVIDIA.
|
||||
When recording a window or when using the `-w portal` option under AMD/Intel no special user permission is required,
|
||||
however when recording a monitor (or when using wayland) the program needs root permission (to access KMS).\
|
||||
This is safe in GPU Screen Recorder as the part that needs root access has been moved to its own small program that only does one thing.\
|
||||
For you as a user this only means that if you installed GPU Screen Recorder as a flatpak then a prompt asking for root password will show up when you start recording.
|
||||
For you as a user this only means that if you installed GPU Screen Recorder as a flatpak then a prompt asking for root password will show up once when you start recording.
|
||||
# Performance
|
||||
On a system with a i5 4690k CPU and a GTX 1080 GPU:\
|
||||
When recording Legend of Zelda Breath of the Wild at 4k, fps drops from 30 to 7 when using OBS Studio + nvenc, however when using this screen recorder the fps remains at 30.\
|
||||
@@ -181,4 +186,5 @@ You can record with desktop portal option (`-w portal`) instead which ignores ni
|
||||
## Kdenlive says that the video is not usable for editing because it has variable frame rate
|
||||
To fix this you can either record the video in .mkv format or constant frame rate (-fm cfr).
|
||||
## Colors look incorrect when recording HDR (with hevc_hdr/av1_hdr) or using an ICC profile
|
||||
The latest version of KDE Plasma breaks HDR and ICC profiles for recording applications. Wayland in general doesn't properly support recording HDR yet. Use desktop portal option (`-w portal`) for now to turn HDR recording into SDR and to be able to record with correct colors when using an ICC profile.
|
||||
KDE Plasma version 6.2 broke HDR and ICC profiles for screen recorders. This was changed in KDE plasma version 6.3 and recording HDR works now, as long as you set HDR brightness to 100% (which means setting "Maximum SDR Brightness" in KDE plasma display settings to 203). If you want to convert HDR to SDR then record with desktop portal option (`-w portal`) instead.
|
||||
I don't know how well recording HDR works in wayland compositors other than KDE plasma.
|
||||
|
||||
15
TODO
15
TODO
@@ -194,8 +194,7 @@ Add option to record audio from the recorded window only.
|
||||
|
||||
Add option to automatically select best video codec available. Add -k best, -k best_10bit and -k best_hdr.
|
||||
|
||||
HDR is broken on kde plasma > 6.2 because of change to how HDR metadata works. See https://github.com/dec05eba/gpu-screen-recorder-issues/issues/60.
|
||||
Use wayland color management protocol when it's available: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/14.
|
||||
Use wayland color management protocol when it's available: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/14.
|
||||
|
||||
Use different exit codes for different errors. Use one for invalid -w option, another one for invalid -a option for audio devices, etc. This is to make UI error reporting better.
|
||||
Document these exit codes in an exit code .md file, or finally create a manpage where this can be documented.
|
||||
@@ -204,6 +203,7 @@ Ffmpeg fixed black bars in videos on amd when using hevc and when recording at s
|
||||
https://github.com/FFmpeg/FFmpeg/commit/bcfbf2bac8f9eeeedc407b40596f5c7aaa0d5b47
|
||||
https://github.com/FFmpeg/FFmpeg/commit/d0facac679faf45d3356dff2e2cb382580d7a521
|
||||
Disable gpu screen recorder black bar handling when using hevc on amd when the libavcodec version is the one that comes after those commits.
|
||||
Also consider the mesa version, to see if the gpu supports this.
|
||||
|
||||
Use opengl compute shader instead of graphics shader. This might allow for better performance when games are using 100% of graphics unit which might fix issue with 100% gpu usage causing gpu screen recorder to run slow when not using vaapi to convert rgb to nv12(?).
|
||||
|
||||
@@ -219,3 +219,14 @@ Allow flv av1 if recent ffmpeg version and streaming to youtube (and twitch?) an
|
||||
Use explicit sync in pipewire video code: https://docs.pipewire.org/page_dma_buf.html.
|
||||
|
||||
Support vaapi rotation. Support for it is added in mesa here: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32919.
|
||||
|
||||
Replay (and recording?) fails to save properly sometimes (especially for long videos). This is noticable with mp4 files since they get corrupt and become unplayable.
|
||||
The entire video does seem to get saved (it's a large video file) and it seems to have the correct headers but it's not playable.
|
||||
|
||||
Make it possible to save a shorter replay clip remotely. Maybe implement ipc first, to then also allow starting recording/stream while a replay is running.
|
||||
|
||||
Add an option to pass http headers when streaming. Some streaming services require streaming keys to be passed in a http header instead of in the url as a parameter.
|
||||
|
||||
When adding vulkan video support add VK_VIDEO_ENCODE_TUNING_MODE_LOW_LATENCY_KHR.
|
||||
|
||||
Implement screenshot without invoking opengl (which is slow to start on some systems).
|
||||
|
||||
@@ -14,7 +14,6 @@ typedef struct {
|
||||
gsr_color_depth color_depth;
|
||||
gsr_color_range color_range;
|
||||
bool record_cursor;
|
||||
bool use_software_video_encoder;
|
||||
vec2i output_resolution;
|
||||
} gsr_capture_nvfbc_params;
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ typedef enum {
|
||||
|
||||
typedef enum {
|
||||
GSR_DESTINATION_COLOR_NV12, /* YUV420, BT709, 8-bit */
|
||||
GSR_DESTINATION_COLOR_P010 /* YUV420, BT2020, 10-bit */
|
||||
GSR_DESTINATION_COLOR_P010, /* YUV420, BT2020, 10-bit */
|
||||
GSR_DESTINATION_COLOR_RGB8
|
||||
} gsr_destination_color;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -104,11 +104,13 @@ typedef void(*__GLXextFuncPtr)(void);
|
||||
#define GL_RG 0x8227
|
||||
#define GL_RGB 0x1907
|
||||
#define GL_RGBA 0x1908
|
||||
#define GL_RGB8 0x8051
|
||||
#define GL_RGBA8 0x8058
|
||||
#define GL_R8 0x8229
|
||||
#define GL_RG8 0x822B
|
||||
#define GL_R16 0x822A
|
||||
#define GL_RG16 0x822C
|
||||
#define GL_RGB16 0x8054
|
||||
#define GL_UNSIGNED_BYTE 0x1401
|
||||
#define GL_COLOR_BUFFER_BIT 0x00004000
|
||||
#define GL_TEXTURE_WRAP_S 0x2802
|
||||
|
||||
15
include/encoder/video/image.h
Normal file
15
include/encoder/video/image.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef GSR_ENCODER_VIDEO_IMAGE_H
|
||||
#define GSR_ENCODER_VIDEO_IMAGE_H
|
||||
|
||||
#include "video.h"
|
||||
|
||||
typedef struct gsr_egl gsr_egl;
|
||||
|
||||
typedef struct {
|
||||
gsr_egl *egl;
|
||||
gsr_color_depth color_depth;
|
||||
} gsr_video_encoder_image_params;
|
||||
|
||||
gsr_video_encoder* gsr_video_encoder_image_create(const gsr_video_encoder_image_params *params);
|
||||
|
||||
#endif /* GSR_ENCODER_VIDEO_IMAGE_H */
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <spa/param/video/format.h>
|
||||
|
||||
#define GSR_PIPEWIRE_VIDEO_MAX_MODIFIERS 1024
|
||||
#define GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS 6
|
||||
#define GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS 12
|
||||
#define GSR_PIPEWIRE_VIDEO_DMABUF_MAX_PLANES 4
|
||||
|
||||
typedef struct gsr_egl gsr_egl;
|
||||
@@ -82,7 +82,7 @@ typedef struct {
|
||||
uint32_t width, height;
|
||||
} crop;
|
||||
|
||||
gsr_video_format supported_video_formats[GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS];
|
||||
gsr_video_format supported_video_formats[GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS];
|
||||
|
||||
gsr_pipewire_video_data_version server_version;
|
||||
gsr_pipewire_video_video_info video_info;
|
||||
|
||||
@@ -50,6 +50,7 @@ drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count
|
||||
uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count);
|
||||
|
||||
bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info);
|
||||
bool version_greater_than(int major, int minor, int patch, int other_major, int other_minor, int other_patch);
|
||||
bool gl_driver_version_greater_than(const gsr_gpu_info *gpu_info, int major, int minor, int patch);
|
||||
|
||||
bool try_card_has_valid_plane(const char *card_path);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.0.3', default_options : ['warning_level=2'])
|
||||
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.1.1', default_options : ['warning_level=2'])
|
||||
|
||||
add_project_arguments('-Wshadow', language : ['c', 'cpp'])
|
||||
if get_option('buildtype') == 'debug'
|
||||
@@ -18,6 +18,7 @@ src = [
|
||||
'src/encoder/video/vaapi.c',
|
||||
'src/encoder/video/vulkan.c',
|
||||
'src/encoder/video/software.c',
|
||||
'src/encoder/video/image.c',
|
||||
'src/codec_query/nvenc.c',
|
||||
'src/codec_query/vaapi.c',
|
||||
'src/codec_query/vulkan.c',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "gpu-screen-recorder"
|
||||
type = "executable"
|
||||
version = "5.0.3"
|
||||
version = "5.1.1"
|
||||
platforms = ["posix"]
|
||||
|
||||
[config]
|
||||
|
||||
@@ -236,6 +236,11 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c
|
||||
if(self->fast_path_failed)
|
||||
fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n");
|
||||
|
||||
//if(self->params.hdr) {
|
||||
// self->fast_path_failed = true;
|
||||
// fprintf(stderr, "gsr warning: gsr_capture_kms_start: recording with hdr requires shader color conversion which might be slow. If this is an issue record with -w portal instead (which converts HDR to SDR)\n");
|
||||
//}
|
||||
|
||||
self->mesa_supports_compute_only_vaapi_copy = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 3, 6);
|
||||
|
||||
frame->width = video_codec_context->width;
|
||||
@@ -756,8 +761,8 @@ static bool gsr_capture_kms_set_hdr_metadata(gsr_capture *cap, AVMasteringDispla
|
||||
mastering_display_metadata->min_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.min_display_mastering_luminance, 10000);
|
||||
mastering_display_metadata->max_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.max_display_mastering_luminance, 1);
|
||||
|
||||
mastering_display_metadata->has_primaries = mastering_display_metadata->display_primaries[0][0].num > 0;
|
||||
mastering_display_metadata->has_luminance = mastering_display_metadata->max_luminance.num > 0;
|
||||
mastering_display_metadata->has_primaries = true;
|
||||
mastering_display_metadata->has_luminance = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_
|
||||
/* Disable vsync */
|
||||
self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0);
|
||||
if(window_texture_init(&self->window_texture, self->display, self->window, self->params.egl) != 0 && !self->params.follow_focused) {
|
||||
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", self->window);
|
||||
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", (long)self->window);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#define MAX_SHADERS 4
|
||||
#define MAX_FRAMEBUFFERS 2
|
||||
#define EXTERNAL_TEXTURE_SHADER_OFFSET 2
|
||||
|
||||
static float abs_f(float v) {
|
||||
return v >= 0.0f ? v : -v;
|
||||
@@ -69,6 +70,8 @@ static const char* color_format_range_get_transform_matrix(gsr_destination_color
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8:
|
||||
return "";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
@@ -93,6 +96,12 @@ static int load_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *u
|
||||
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.x = (RGBtoYUV * vec4(pixel.rgb, 1.0)).x; \n"
|
||||
" FragColor.w = pixel.a; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
@@ -106,10 +115,8 @@ static int load_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *u
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.x = (RGBtoYUV * vec4(pixel.rgb, 1.0)).x; \n"
|
||||
" FragColor.w = pixel.a; \n"
|
||||
"} \n", color_transform_matrix);
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
@@ -120,10 +127,8 @@ static int load_shader_y(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *u
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.x = (RGBtoYUV * vec4(pixel.rgb, 1.0)).x; \n"
|
||||
" FragColor.w = pixel.a; \n"
|
||||
"} \n", color_transform_matrix);
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||
@@ -145,7 +150,7 @@ static unsigned int load_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
"in vec2 pos; \n"
|
||||
"in vec2 texcoords; \n"
|
||||
"out vec2 texcoords_out; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform float rotation; \n"
|
||||
ROTATE_Z
|
||||
"void main() \n"
|
||||
@@ -154,6 +159,12 @@ static unsigned int load_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
" gl_Position = (vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0)) * vec4(0.5, 0.5, 1.0, 1.0) - vec4(0.5, 0.5, 0.0, 0.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.xy = (RGBtoYUV * vec4(pixel.rgb, 1.0)).yz; \n"
|
||||
" FragColor.w = pixel.a; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
@@ -167,10 +178,8 @@ static unsigned int load_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.xy = (RGBtoYUV * vec4(pixel.rgb, 1.0)).yz; \n"
|
||||
" FragColor.w = pixel.a; \n"
|
||||
"} \n", color_transform_matrix);
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
@@ -181,10 +190,66 @@ static unsigned int load_shader_uv(gsr_shader *shader, gsr_egl *egl, gsr_color_u
|
||||
"%s"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", color_transform_matrix, main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||
return -1;
|
||||
|
||||
gsr_shader_bind_attribute_location(shader, "pos", 0);
|
||||
gsr_shader_bind_attribute_location(shader, "texcoords", 1);
|
||||
uniforms->offset = egl->glGetUniformLocation(shader->program_id, "offset");
|
||||
uniforms->rotation = egl->glGetUniformLocation(shader->program_id, "rotation");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int load_shader_rgb(gsr_shader *shader, gsr_egl *egl, gsr_color_uniforms *uniforms, bool external_texture) {
|
||||
char vertex_shader[2048];
|
||||
snprintf(vertex_shader, sizeof(vertex_shader),
|
||||
"#version 300 es \n"
|
||||
"in vec2 pos; \n"
|
||||
"in vec2 texcoords; \n"
|
||||
"out vec2 texcoords_out; \n"
|
||||
"uniform vec2 offset; \n"
|
||||
"uniform float rotation; \n"
|
||||
ROTATE_Z
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" texcoords_out = (vec4(texcoords.x - 0.5, texcoords.y - 0.5, 0.0, 0.0) * rotate_z(rotation)).xy + vec2(0.5, 0.5); \n"
|
||||
" gl_Position = vec4(offset.x, offset.y, 0.0, 0.0) + vec4(pos.x, pos.y, 0.0, 1.0); \n"
|
||||
"} \n");
|
||||
|
||||
const char *main_code =
|
||||
main_code =
|
||||
" vec4 pixel = texture(tex1, texcoords_out); \n"
|
||||
" FragColor.xy = (RGBtoYUV * vec4(pixel.rgb, 1.0)).yz; \n"
|
||||
" FragColor.w = pixel.a; \n"
|
||||
"} \n", color_transform_matrix);
|
||||
" FragColor = pixel; \n";
|
||||
|
||||
char fragment_shader[2048];
|
||||
if(external_texture) {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"#extension GL_OES_EGL_image_external : enable \n"
|
||||
"#extension GL_OES_EGL_image_external_essl3 : require \n"
|
||||
"precision mediump float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform samplerExternalOES tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", main_code);
|
||||
} else {
|
||||
snprintf(fragment_shader, sizeof(fragment_shader),
|
||||
"#version 300 es \n"
|
||||
"precision mediump float; \n"
|
||||
"in vec2 texcoords_out; \n"
|
||||
"uniform sampler2D tex1; \n"
|
||||
"out vec4 FragColor; \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
"%s"
|
||||
"} \n", main_code);
|
||||
}
|
||||
|
||||
if(gsr_shader_init(shader, egl, vertex_shader, fragment_shader) != 0)
|
||||
@@ -272,18 +337,37 @@ int gsr_color_conversion_init(gsr_color_conversion *self, const gsr_color_conver
|
||||
}
|
||||
|
||||
if(self->params.load_external_image_shader) {
|
||||
if(load_shader_y(&self->shaders[2], self->params.egl, &self->uniforms[2], params->destination_color, params->color_range, true) != 0) {
|
||||
if(load_shader_y(&self->shaders[EXTERNAL_TEXTURE_SHADER_OFFSET], self->params.egl, &self->uniforms[EXTERNAL_TEXTURE_SHADER_OFFSET], params->destination_color, params->color_range, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(load_shader_uv(&self->shaders[3], self->params.egl, &self->uniforms[3], params->destination_color, params->color_range, true) != 0) {
|
||||
if(load_shader_uv(&self->shaders[EXTERNAL_TEXTURE_SHADER_OFFSET + 1], self->params.egl, &self->uniforms[EXTERNAL_TEXTURE_SHADER_OFFSET + 1], params->destination_color, params->color_range, true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load UV shader\n");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
if(self->params.num_destination_textures != 1) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: expected 1 destination textures for destination color RGB8, got %d destination texture(s)\n", self->params.num_destination_textures);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(load_shader_rgb(&self->shaders[0], self->params.egl, &self->uniforms[0], false) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y shader\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if(self->params.load_external_image_shader) {
|
||||
if(load_shader_rgb(&self->shaders[EXTERNAL_TEXTURE_SHADER_OFFSET], self->params.egl, &self->uniforms[EXTERNAL_TEXTURE_SHADER_OFFSET], true) != 0) {
|
||||
fprintf(stderr, "gsr error: gsr_color_conversion_init: failed to load Y shader\n");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(load_framebuffers(self) != 0)
|
||||
@@ -415,7 +499,7 @@ void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
|
||||
|
||||
const int shader_index = external_texture ? 2 : 0;
|
||||
const int shader_index = external_texture ? EXTERNAL_TEXTURE_SHADER_OFFSET : 0;
|
||||
gsr_shader_use(&self->shaders[shader_index]);
|
||||
self->params.egl->glUniform1f(self->uniforms[shader_index].rotation, rotation);
|
||||
self->params.egl->glUniform2f(self->uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||
@@ -426,7 +510,7 @@ void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]);
|
||||
//cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
const int shader_index = external_texture ? 3 : 1;
|
||||
const int shader_index = external_texture ? EXTERNAL_TEXTURE_SHADER_OFFSET + 1 : 1;
|
||||
gsr_shader_use(&self->shaders[shader_index]);
|
||||
self->params.egl->glUniform1f(self->uniforms[shader_index].rotation, rotation);
|
||||
self->params.egl->glUniform2f(self->uniforms[shader_index].offset, pos_norm.x, pos_norm.y);
|
||||
@@ -454,6 +538,13 @@ void gsr_color_conversion_clear(gsr_color_conversion *self) {
|
||||
color2[3] = 1.0f;
|
||||
break;
|
||||
}
|
||||
case GSR_DESTINATION_COLOR_RGB8: {
|
||||
color2[0] = 0.0f;
|
||||
color2[1] = 0.0f;
|
||||
color2[2] = 0.0f;
|
||||
color2[3] = 1.0f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
|
||||
|
||||
130
src/encoder/video/image.c
Normal file
130
src/encoder/video/image.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "../../../include/encoder/video/image.h"
|
||||
#include "../../../include/egl.h"
|
||||
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavutil/frame.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LINESIZE_ALIGNMENT 4
|
||||
|
||||
typedef struct {
|
||||
gsr_video_encoder_image_params params;
|
||||
|
||||
unsigned int target_texture;
|
||||
} gsr_video_encoder_image;
|
||||
|
||||
static unsigned int gl_create_texture(gsr_egl *egl, int width, int height, int internal_format, unsigned int format) {
|
||||
unsigned int texture_id = 0;
|
||||
egl->glGenTextures(1, &texture_id);
|
||||
egl->glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
egl->glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
|
||||
|
||||
egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
|
||||
egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return texture_id;
|
||||
}
|
||||
|
||||
static bool gsr_video_encoder_image_setup_textures(gsr_video_encoder_image *self, AVCodecContext *video_codec_context, AVFrame *frame) {
|
||||
int res = av_frame_get_buffer(frame, LINESIZE_ALIGNMENT);
|
||||
if(res < 0) {
|
||||
fprintf(stderr, "gsr error: gsr_video_encoder_image_setup_textures: av_frame_get_buffer failed: %d\n", res);
|
||||
return false;
|
||||
}
|
||||
|
||||
res = av_frame_make_writable(frame);
|
||||
if(res < 0) {
|
||||
fprintf(stderr, "gsr error: gsr_video_encoder_image_setup_textures: av_frame_make_writable failed: %d\n", res);
|
||||
return false;
|
||||
}
|
||||
|
||||
self->target_texture = gl_create_texture(self->params.egl, video_codec_context->width, video_codec_context->height, self->params.color_depth == GSR_COLOR_DEPTH_8_BITS ? GL_RGB8 : GL_RGB16, GL_RGB);
|
||||
if(self->target_texture == 0) {
|
||||
fprintf(stderr, "gsr error: gsr_capture_kms_setup_cuda_textures: failed to create opengl texture\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void gsr_video_encoder_image_stop(gsr_video_encoder_image *self, AVCodecContext *video_codec_context);
|
||||
|
||||
static bool gsr_video_encoder_image_start(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame) {
|
||||
gsr_video_encoder_image *self = encoder->priv;
|
||||
|
||||
video_codec_context->width = FFALIGN(video_codec_context->width, LINESIZE_ALIGNMENT);
|
||||
video_codec_context->height = FFALIGN(video_codec_context->height, 2);
|
||||
|
||||
frame->width = video_codec_context->width;
|
||||
frame->height = video_codec_context->height;
|
||||
|
||||
if(!gsr_video_encoder_image_setup_textures(self, video_codec_context, frame)) {
|
||||
gsr_video_encoder_image_stop(self, video_codec_context);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void gsr_video_encoder_image_stop(gsr_video_encoder_image *self, AVCodecContext *video_codec_context) {
|
||||
(void)video_codec_context;
|
||||
self->params.egl->glDeleteTextures(1, &self->target_texture);
|
||||
self->target_texture = 0;
|
||||
}
|
||||
|
||||
static void gsr_video_encoder_image_copy_textures_to_frame(gsr_video_encoder *encoder, AVFrame *frame, gsr_color_conversion *color_conversion) {
|
||||
gsr_video_encoder_image *self = encoder->priv;
|
||||
// TODO: hdr support
|
||||
self->params.egl->glBindTexture(GL_TEXTURE_2D, self->target_texture);
|
||||
// We could use glGetTexSubImage and then we wouldn't have to use a specific linesize (LINESIZE_ALIGNMENT) that adds padding,
|
||||
// but glGetTexSubImage is only available starting from opengl 4.5.
|
||||
self->params.egl->glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, frame->data[0]);
|
||||
self->params.egl->glBindTexture(GL_TEXTURE_2D, 0);
|
||||
// cap_kms->kms.base.egl->eglSwapBuffers(cap_kms->kms.base.egl->egl_display, cap_kms->kms.base.egl->egl_surface);
|
||||
|
||||
self->params.egl->glFlush();
|
||||
self->params.egl->glFinish();
|
||||
}
|
||||
|
||||
static void gsr_video_encoder_image_get_textures(gsr_video_encoder *encoder, unsigned int *textures, int *num_textures, gsr_destination_color *destination_color) {
|
||||
gsr_video_encoder_image *self = encoder->priv;
|
||||
textures[0] = self->target_texture;
|
||||
*num_textures = 1;
|
||||
// TODO: 10-bit support
|
||||
//*destination_color = self->params.color_depth == GSR_COLOR_DEPTH_10_BITS ? GSR_DESTINATION_COLOR_P010 : GSR_DESTINATION_COLOR_NV12;
|
||||
*destination_color = GSR_DESTINATION_COLOR_RGB8;
|
||||
}
|
||||
|
||||
static void gsr_video_encoder_image_destroy(gsr_video_encoder *encoder, AVCodecContext *video_codec_context) {
|
||||
gsr_video_encoder_image_stop(encoder->priv, video_codec_context);
|
||||
free(encoder->priv);
|
||||
free(encoder);
|
||||
}
|
||||
|
||||
gsr_video_encoder* gsr_video_encoder_image_create(const gsr_video_encoder_image_params *params) {
|
||||
gsr_video_encoder *encoder = calloc(1, sizeof(gsr_video_encoder));
|
||||
if(!encoder)
|
||||
return NULL;
|
||||
|
||||
gsr_video_encoder_image *encoder_image = calloc(1, sizeof(gsr_video_encoder_image));
|
||||
if(!encoder_image) {
|
||||
free(encoder);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
encoder_image->params = *params;
|
||||
|
||||
*encoder = (gsr_video_encoder) {
|
||||
.start = gsr_video_encoder_image_start,
|
||||
.copy_textures_to_frame = gsr_video_encoder_image_copy_textures_to_frame,
|
||||
.get_textures = gsr_video_encoder_image_get_textures,
|
||||
.destroy = gsr_video_encoder_image_destroy,
|
||||
.priv = encoder_image
|
||||
};
|
||||
|
||||
return encoder;
|
||||
}
|
||||
483
src/main.cpp
483
src/main.cpp
@@ -13,6 +13,7 @@ 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"
|
||||
@@ -112,7 +113,9 @@ enum class VideoCodec {
|
||||
VP8,
|
||||
VP9,
|
||||
H264_VULKAN,
|
||||
HEVC_VULKAN
|
||||
HEVC_VULKAN,
|
||||
JPEG,
|
||||
PNG
|
||||
};
|
||||
|
||||
enum class AudioCodec {
|
||||
@@ -216,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;
|
||||
@@ -580,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) {
|
||||
@@ -715,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) {
|
||||
@@ -1197,7 +1234,7 @@ static void usage_full() {
|
||||
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 (if supported)).\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");
|
||||
@@ -1257,6 +1294,7 @@ static void usage_full() {
|
||||
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);
|
||||
@@ -1315,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() {
|
||||
@@ -1354,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;
|
||||
@@ -1442,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;
|
||||
}
|
||||
|
||||
@@ -1471,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;
|
||||
@@ -1487,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;
|
||||
@@ -1508,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;
|
||||
@@ -1644,6 +1683,12 @@ static bool string_starts_with(const std::string &str, const char *substr) {
|
||||
return (int)str.size() >= len && memcmp(str.data(), substr, len) == 0;
|
||||
}
|
||||
|
||||
static bool string_ends_with(const char *str, const char *substr) {
|
||||
int str_len = strlen(str);
|
||||
int substr_len = strlen(substr);
|
||||
return str_len >= substr_len && memcmp(str + str_len - substr_len, substr, substr_len) == 0;
|
||||
}
|
||||
|
||||
static const AudioDevice* get_audio_device_by_name(const std::vector<AudioDevice> &audio_devices, const char *name) {
|
||||
for(const auto &audio_device : audio_devices) {
|
||||
if(strcmp(audio_device.name.c_str(), name) == 0)
|
||||
@@ -1716,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
|
||||
@@ -1751,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];
|
||||
@@ -1805,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;
|
||||
@@ -1972,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;
|
||||
}
|
||||
@@ -2042,6 +2125,10 @@ 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)
|
||||
@@ -2317,7 +2404,7 @@ static void validate_monitor_get_valid(const gsr_egl *egl, std::string &window_s
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -2379,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)
|
||||
@@ -2430,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))
|
||||
@@ -2440,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())
|
||||
@@ -2633,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";
|
||||
@@ -2645,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;
|
||||
@@ -2662,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;
|
||||
}
|
||||
@@ -2737,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) {
|
||||
@@ -2798,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2971,6 +3084,31 @@ static AudioDeviceData create_application_audio_audio_input(const MergedAudioInp
|
||||
}
|
||||
#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
|
||||
|
||||
@@ -3053,53 +3191,68 @@ int main(int argc, char **argv) {
|
||||
|
||||
//av_log_set_level(AV_LOG_TRACE);
|
||||
|
||||
const bool is_optional = true;
|
||||
const bool is_list = true;
|
||||
std::map<std::string, Arg> args = {
|
||||
{ "-w", Arg { {}, false, false } },
|
||||
{ "-c", Arg { {}, true, false } },
|
||||
{ "-f", Arg { {}, false, false } },
|
||||
{ "-s", Arg { {}, true, false } },
|
||||
{ "-a", Arg { {}, true, true } },
|
||||
{ "-q", Arg { {}, true, false } },
|
||||
{ "-o", Arg { {}, true, false } },
|
||||
{ "-r", Arg { {}, true, false } },
|
||||
{ "-restart-replay-on-save", 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 } },
|
||||
{ "-gl-debug", 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, "Error: 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, "Error: 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, "Error: 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) {
|
||||
@@ -3211,89 +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 gl_debug = false;
|
||||
const char *gl_debug_str = args["-gl-debug"].value();
|
||||
if(!gl_debug_str)
|
||||
gl_debug_str = "no";
|
||||
|
||||
if(strcmp(gl_debug_str, "yes") == 0) {
|
||||
gl_debug = true;
|
||||
} else if(strcmp(gl_debug_str, "no") == 0) {
|
||||
gl_debug = false;
|
||||
} else {
|
||||
fprintf(stderr, "Error: -gl-debug should either be either 'yes' or 'no', got: '%s'\n", gl_debug_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) {
|
||||
@@ -3382,27 +3459,13 @@ 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
|
||||
}
|
||||
|
||||
bool restart_replay_on_save = false;
|
||||
const char *restart_replay_on_save_str = args["-restart-replay-on-save"].value();
|
||||
if(!restart_replay_on_save_str)
|
||||
restart_replay_on_save_str = "no";
|
||||
|
||||
if(strcmp(restart_replay_on_save_str, "yes") == 0) {
|
||||
restart_replay_on_save = true;
|
||||
} else if(strcmp(restart_replay_on_save_str, "no") == 0) {
|
||||
restart_replay_on_save = false;
|
||||
} else {
|
||||
fprintf(stderr, "Error: -restart-replay-on-save should either be either 'yes' or 'no', got: '%s'\n", restart_replay_on_save_str);
|
||||
usage();
|
||||
}
|
||||
|
||||
std::string window_str = args["-w"].value();
|
||||
const bool is_portal_capture = strcmp(window_str.c_str(), "portal") == 0;
|
||||
|
||||
@@ -3447,6 +3510,11 @@ 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, window, is_monitor_capture, gl_debug)) {
|
||||
@@ -3666,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);
|
||||
@@ -3691,18 +3765,14 @@ 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.
|
||||
@@ -3774,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);
|
||||
@@ -3864,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);
|
||||
@@ -4113,6 +4187,7 @@ 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();
|
||||
@@ -4220,6 +4295,10 @@ int main(int argc, char **argv) {
|
||||
// TODO: Move to separate thread because this could write to network (for example when livestreaming)
|
||||
receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context,
|
||||
replay_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
|
||||
if(is_image_output) {
|
||||
running = 0;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret));
|
||||
}
|
||||
@@ -4251,16 +4330,18 @@ int main(int argc, char **argv) {
|
||||
|
||||
std::lock_guard<std::mutex> lock(write_output_mutex);
|
||||
save_replay_packets.clear();
|
||||
if(restart_replay_on_save) {
|
||||
frame_data_queue.clear();
|
||||
frames_erased = true;
|
||||
replay_start_time = clock_get_monotonic_seconds() - paused_time_offset;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -4315,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);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
/* This code is partially based on xr-video-player pipewire implementation which is based on obs-studio's pipewire implementation */
|
||||
|
||||
/* TODO: Make gsr_pipewire_video_init asynchronous */
|
||||
/* TODO: Support 10-bit capture (hdr) when pipewire supports it */
|
||||
/* TODO: Support hdr when pipewire supports it */
|
||||
/* TODO: Test all of the image formats */
|
||||
|
||||
#ifndef SPA_POD_PROP_FLAG_DONT_FIXATE
|
||||
@@ -65,14 +65,20 @@ static void on_core_done_cb(void *user_data, uint32_t id, int seq) {
|
||||
|
||||
static bool is_cursor_format_supported(const enum spa_video_format format) {
|
||||
switch(format) {
|
||||
case SPA_VIDEO_FORMAT_RGBx: return true;
|
||||
case SPA_VIDEO_FORMAT_BGRx: return true;
|
||||
case SPA_VIDEO_FORMAT_xRGB: return true;
|
||||
case SPA_VIDEO_FORMAT_xBGR: return true;
|
||||
case SPA_VIDEO_FORMAT_RGBA: return true;
|
||||
case SPA_VIDEO_FORMAT_BGRA: return true;
|
||||
case SPA_VIDEO_FORMAT_ARGB: return true;
|
||||
case SPA_VIDEO_FORMAT_ABGR: return true;
|
||||
case SPA_VIDEO_FORMAT_RGBx: return true;
|
||||
case SPA_VIDEO_FORMAT_BGRx: return true;
|
||||
case SPA_VIDEO_FORMAT_RGBA: return true;
|
||||
case SPA_VIDEO_FORMAT_BGRA: return true;
|
||||
case SPA_VIDEO_FORMAT_RGB: return true;
|
||||
case SPA_VIDEO_FORMAT_BGR: return true;
|
||||
case SPA_VIDEO_FORMAT_ARGB: return true;
|
||||
case SPA_VIDEO_FORMAT_ABGR: return true;
|
||||
#if PW_CHECK_VERSION(0, 3, 41)
|
||||
case SPA_VIDEO_FORMAT_xRGB_210LE: return true;
|
||||
case SPA_VIDEO_FORMAT_xBGR_210LE: return true;
|
||||
case SPA_VIDEO_FORMAT_ARGB_210LE: return true;
|
||||
case SPA_VIDEO_FORMAT_ABGR_210LE: return true;
|
||||
#endif
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
@@ -338,24 +344,46 @@ static inline struct spa_pod *build_format(struct spa_pod_builder *b,
|
||||
/* For some reason gstreamer formats are in opposite order to drm formats */
|
||||
static int64_t spa_video_format_to_drm_format(const enum spa_video_format format) {
|
||||
switch(format) {
|
||||
case SPA_VIDEO_FORMAT_RGBx: return DRM_FORMAT_XBGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGRx: return DRM_FORMAT_XRGB8888;
|
||||
case SPA_VIDEO_FORMAT_RGBA: return DRM_FORMAT_ABGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGRA: return DRM_FORMAT_ARGB8888;
|
||||
case SPA_VIDEO_FORMAT_RGB: return DRM_FORMAT_XBGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGR: return DRM_FORMAT_XRGB8888;
|
||||
case SPA_VIDEO_FORMAT_RGBx: return DRM_FORMAT_XBGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGRx: return DRM_FORMAT_XRGB8888;
|
||||
case SPA_VIDEO_FORMAT_RGBA: return DRM_FORMAT_ABGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGRA: return DRM_FORMAT_ARGB8888;
|
||||
case SPA_VIDEO_FORMAT_RGB: return DRM_FORMAT_XBGR8888;
|
||||
case SPA_VIDEO_FORMAT_BGR: return DRM_FORMAT_XRGB8888;
|
||||
case SPA_VIDEO_FORMAT_ARGB: return DRM_FORMAT_XRGB8888;
|
||||
case SPA_VIDEO_FORMAT_ABGR: return DRM_FORMAT_XRGB8888;
|
||||
#if PW_CHECK_VERSION(0, 3, 41)
|
||||
case SPA_VIDEO_FORMAT_xRGB_210LE: return DRM_FORMAT_XRGB2101010;
|
||||
case SPA_VIDEO_FORMAT_xBGR_210LE: return DRM_FORMAT_XBGR2101010;
|
||||
case SPA_VIDEO_FORMAT_ARGB_210LE: return DRM_FORMAT_ARGB2101010;
|
||||
case SPA_VIDEO_FORMAT_ABGR_210LE: return DRM_FORMAT_ABGR2101010;
|
||||
#endif
|
||||
default: break;
|
||||
}
|
||||
return DRM_FORMAT_INVALID;
|
||||
}
|
||||
|
||||
static const enum spa_video_format video_formats[] = {
|
||||
#if PW_CHECK_VERSION(0, 3, 41)
|
||||
#define GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS
|
||||
#else
|
||||
#define GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS 8
|
||||
#endif
|
||||
|
||||
static const enum spa_video_format video_formats[GSR_PIPEWIRE_VIDEO_MAX_VIDEO_FORMATS] = {
|
||||
SPA_VIDEO_FORMAT_BGRA,
|
||||
SPA_VIDEO_FORMAT_BGRx,
|
||||
SPA_VIDEO_FORMAT_BGR,
|
||||
SPA_VIDEO_FORMAT_RGBx,
|
||||
SPA_VIDEO_FORMAT_RGBA,
|
||||
SPA_VIDEO_FORMAT_RGB,
|
||||
SPA_VIDEO_FORMAT_ARGB,
|
||||
SPA_VIDEO_FORMAT_ABGR,
|
||||
#if PW_CHECK_VERSION(0, 3, 41)
|
||||
SPA_VIDEO_FORMAT_xRGB_210LE,
|
||||
SPA_VIDEO_FORMAT_xBGR_210LE,
|
||||
SPA_VIDEO_FORMAT_ARGB_210LE,
|
||||
SPA_VIDEO_FORMAT_ABGR_210LE
|
||||
#endif
|
||||
};
|
||||
|
||||
static bool gsr_pipewire_video_build_format_params(gsr_pipewire_video *self, struct spa_pod_builder *pod_builder, struct spa_pod **params, uint32_t *num_params) {
|
||||
@@ -367,7 +395,7 @@ static bool gsr_pipewire_video_build_format_params(gsr_pipewire_video *self, str
|
||||
for(size_t i = 0; i < GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS; i++) {
|
||||
if(self->supported_video_formats[i].modifiers_size == 0)
|
||||
continue;
|
||||
params[i] = build_format(pod_builder, &self->video_info, self->supported_video_formats[i].format, self->modifiers + self->supported_video_formats[i].modifiers_index, self->supported_video_formats[i].modifiers_size);
|
||||
params[*num_params] = build_format(pod_builder, &self->video_info, self->supported_video_formats[i].format, self->modifiers + self->supported_video_formats[i].modifiers_index, self->supported_video_formats[i].modifiers_size);
|
||||
++(*num_params);
|
||||
}
|
||||
|
||||
@@ -382,7 +410,7 @@ static void renegotiate_format(void *data, uint64_t expirations) {
|
||||
|
||||
struct spa_pod *params[GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS];
|
||||
uint32_t num_video_formats = 0;
|
||||
uint8_t params_buffer[2048];
|
||||
uint8_t params_buffer[4096];
|
||||
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
|
||||
if (!gsr_pipewire_video_build_format_params(self, &pod_builder, params, &num_video_formats)) {
|
||||
pw_thread_loop_unlock(self->thread_loop);
|
||||
@@ -413,6 +441,11 @@ static bool spa_video_format_get_modifiers(gsr_pipewire_video *self, const enum
|
||||
}
|
||||
|
||||
const int64_t drm_format = spa_video_format_to_drm_format(format);
|
||||
if(drm_format == DRM_FORMAT_INVALID) {
|
||||
fprintf(stderr, "gsr error: spa_video_format_get_modifiers: unsupported format: %d\n", (int)format);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!self->egl->eglQueryDmaBufModifiersEXT(self->egl->egl_display, drm_format, max_modifiers, modifiers, NULL, num_modifiers)) {
|
||||
fprintf(stderr, "gsr error: spa_video_format_get_modifiers: eglQueryDmaBufModifiersEXT failed with drm format %d, %" PRIi64 "\n", (int)format, drm_format);
|
||||
//modifiers[0] = DRM_FORMAT_MOD_LINEAR;
|
||||
@@ -443,7 +476,7 @@ static void gsr_pipewire_video_init_modifiers(gsr_pipewire_video *self) {
|
||||
static bool gsr_pipewire_video_setup_stream(gsr_pipewire_video *self) {
|
||||
struct spa_pod *params[GSR_PIPEWIRE_VIDEO_NUM_VIDEO_FORMATS];
|
||||
uint32_t num_video_formats = 0;
|
||||
uint8_t params_buffer[2048];
|
||||
uint8_t params_buffer[4096];
|
||||
struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
|
||||
|
||||
self->thread_loop = pw_thread_loop_new("gsr screen capture", NULL);
|
||||
|
||||
@@ -413,7 +413,7 @@ bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info) {
|
||||
return supported;
|
||||
}
|
||||
|
||||
static bool version_greater_than(int major, int minor, int patch, int other_major, int other_minor, int other_patch) {
|
||||
bool version_greater_than(int major, int minor, int patch, int other_major, int other_minor, int other_patch) {
|
||||
return (major > other_major) || (major == other_major && minor > other_minor) || (major == other_major && minor == other_minor && patch > other_patch);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user