Compare commits

..

14 Commits

Author SHA1 Message Date
dec05eba
57caf13d65 5.12.3 2026-01-28 01:39:09 +01:00
dec05eba
bdf1950ca2 Dont use dbus_bus_request_name 2026-01-28 01:37:50 +01:00
Victor Nova
144b481526 Add -write-first-frame-ts switch
Add -write-first-frame-ts switch that creates a .ts file next to the output
file with values from CLOCK_MONOTONIC and CLOCK_REALTIME corresponding to the
first frame to be able to synchronize video with other timestamped data.
2026-01-27 17:43:41 +01:00
dec05eba
f4ee71a094 Remove fixed TODO 2026-01-26 15:18:26 +01:00
dec05eba
2dce92d82f Use constant bitrate mode in example scripts 2026-01-25 03:16:56 +01:00
Mroik
933911bdde Install example scripts and add fix man example 2026-01-25 03:08:15 +01:00
dec05eba
01d0df500c Revert "Test dont set environment variables"
This reverts commit 95415f7ac7.
2026-01-24 15:41:31 +01:00
dec05eba
95415f7ac7 Test dont set environment variables 2026-01-24 15:19:31 +01:00
dec05eba
a39dad1c02 Remove capture source property clamp, cleanup manpage 2026-01-23 01:39:49 +01:00
dec05eba
c1af143406 5.12.2 2026-01-23 00:41:44 +01:00
dec05eba
4cebc3f0ee Fix negative relative position not working (incorrectly clamped to 0, 100) 2026-01-23 00:10:11 +01:00
dec05eba
f6013d094d Lock around get packet data 2026-01-21 18:29:41 +01:00
dec05eba
45daccff84 Clone replay buffer immediately, only one short mutex lock 2026-01-21 17:29:03 +01:00
dec05eba
ede1e46503 Fix possible replay race condition, move mutex out of replay 2026-01-21 02:24:30 +01:00
17 changed files with 189 additions and 161 deletions

6
TODO
View File

@@ -312,8 +312,6 @@ Check if region capture works properly with fractional scaling on wayland.
Add option to specify medium/high/very high/ultra for -bm cbr as well, which should automatically pick bitrate based on resolution and framerate.
This should also be reflected in gsr ui.
Create a manpage and move --help text there and mention the manpage command to view it (and make it work in flatpak, maybe with man <link-to-manpage-file>).
Implement webcam support by using mjpeg with v4l2 and use ffmpeg mjpeg decoder.
After adding rpc, making recording while in replay/streaming work differently. Start recording should take audio as an argument, to optionally specify different audio for recording than replay/stream.
@@ -398,4 +396,6 @@ Return the max resolution of each codec in --info to display an error in the UI
Should -low-power option also use vaapi/vulkan low power, if available?
Should capture option x=bla;y=bla be scaled by -s (output resolution scale)? width and height is.
Should capture option x=bla;y=bla be scaled by -s (output resolution scale)? width and height is.
Certain webcam resolutions yuyv resolutions dont work (on amd at least), such as 800x600. Maybe it's because of alignment issue, 600 isn't divisible by 16.

View File

@@ -105,18 +105,18 @@ These are the available options for all capture sources (optional):
.RS
.IP \(bu 3
.B x
- The X position in pixels. If the number ends with % and is a number between 0 and 100 then it's a position relative to the video size
- The X position in pixels. If the number ends with % then this sets the X position relative to the video width (integer percentage where 100 = 100%)
.IP \(bu 3
.B y
- The Y position in pixels. If the number ends with % and is a number between 0 and 100 then it's a position relative to the video size
- The Y position in pixels. If the number ends with % then this sets the Y position relative to the video height (integer percentage where 100 = 100%)
.IP \(bu 3
.B width
- The width in pixels. If the number ends with % and is a number between 0 and 100 then it's a size relative to the video size.
- The width in pixels. If the number ends with % then this sets the width relative to the video width (integer percentage where 100 = 100%).
A value of 0 means to not scale the capture source and instead use the original width.
.IP \(bu 3
.B height
- The height in pixels. If the number ends with % and is a number between 0 and 100 then it's a size relative to the video size
- The height in pixels. If the number ends with % then this sets the height relative to the video height (integer percentage where 100 = 100%).
A value of 0 means to not scale the capture source and instead use the original height.
.IP \(bu 3
@@ -372,6 +372,14 @@ It's recommended to also use the option
when this is set to
.B yes
to only encode frames when the screen content updates to lower GPU and video encoding usage when the system is idle.
.TP
.BI \-write\-first\-frame\-ts " yes|no"
When enabled, writes a timestamp file with extra extension \fI.ts\fR next to the output video containing:
.nf
monotonic_microsec realtime_microsec
<monotonic_microsec> <realtime_microsec>
.fi
(default: no). Ignored for livestreaming and when output is piped.
.SS Output Options
.TP
.BI \-o " output"
@@ -467,7 +475,7 @@ Instant replay (last 60 seconds):
.PP
.nf
.RS
gpu-screen-recorder -w screen -f 60 -c mkv -r 60 -o ~/Videos
gpu-screen-recorder -w screen -c mkv -r 60 -o ~/Videos
.RE
.fi
.PP
@@ -491,15 +499,15 @@ Instant replay and launch a script when saving replay:
.PP
.nf
.RS
gpu-screen-recorder -w screen -f 60 -c mkv -r 60 -sc ./script.sh -o ~/Videos
gpu-screen-recorder -w screen -c mkv -r 60 -sc ./script.sh -o ~/Videos
.RE
.fi
.PP
Stream to Twitch:
Stream to Twitch with constant bitrate mode:
.PP
.nf
.RS
gpu-screen-recorder -w screen -f 60 -a default_output -o "rtmp://live.twitch.tv/app/stream_key"
gpu-screen-recorder -w screen -c flv -a default_output -bm cbr -q 8000 -o "rtmp://live.twitch.tv/app/stream_key"
.RE
.fi
.PP

View File

@@ -8,7 +8,7 @@
typedef struct gsr_egl gsr_egl;
#define NUM_ARGS 36
#define NUM_ARGS 37
typedef enum {
GSR_CAPTURE_SOURCE_TYPE_WINDOW,
@@ -97,6 +97,7 @@ typedef struct {
bool restore_portal_session;
bool restart_replay_on_save;
bool overclock;
bool write_first_frame_ts;
bool is_livestream;
bool is_output_piped;
bool low_latency_recording;

View File

@@ -20,12 +20,18 @@ typedef struct {
AVStream *stream;
int64_t start_pts;
bool has_received_keyframe;
char *first_frame_ts_filepath;
bool first_frame_ts_written;
} gsr_encoder_recording_destination;
typedef struct {
gsr_replay_buffer *replay_buffer;
pthread_mutex_t file_write_mutex;
bool mutex_created;
bool file_write_mutex_created;
pthread_mutex_t replay_mutex;
bool replay_mutex_created;
gsr_encoder_recording_destination recording_destinations[GSR_MAX_RECORDING_DESTINATIONS];
size_t num_recording_destinations;
@@ -39,5 +45,6 @@ void gsr_encoder_receive_packets(gsr_encoder *self, AVCodecContext *codec_contex
/* Returns the id to the recording destination, or -1 on error */
size_t gsr_encoder_add_recording_destination(gsr_encoder *self, AVCodecContext *codec_context, AVFormatContext *format_context, AVStream *stream, int64_t start_pts);
bool gsr_encoder_remove_recording_destination(gsr_encoder *self, size_t id);
bool gsr_encoder_set_recording_destination_first_frame_ts_filepath(gsr_encoder *self, size_t id, const char *filepath);
#endif /* GSR_ENCODER_H */

View File

@@ -2,7 +2,6 @@
#define GSR_REPLAY_BUFFER_H
#include "../defs.h"
#include <pthread.h>
#include <stdbool.h>
#include <libavcodec/packet.h>
@@ -27,17 +26,11 @@ struct gsr_replay_buffer {
/* Returns {-1, 0} if not found */
gsr_replay_buffer_iterator (*find_keyframe)(gsr_replay_buffer *self, gsr_replay_buffer_iterator start_iterator, int stream_index, bool invert_stream_index);
bool (*iterator_next)(gsr_replay_buffer *self, gsr_replay_buffer_iterator *iterator);
pthread_mutex_t mutex;
bool mutex_initialized;
gsr_replay_buffer *original_replay_buffer;
};
gsr_replay_buffer* gsr_replay_buffer_create(gsr_replay_storage replay_storage, const char *replay_directory, double replay_buffer_time, size_t replay_buffer_num_packets);
void gsr_replay_buffer_destroy(gsr_replay_buffer *self);
void gsr_replay_buffer_lock(gsr_replay_buffer *self);
void gsr_replay_buffer_unlock(gsr_replay_buffer *self);
bool gsr_replay_buffer_append(gsr_replay_buffer *self, const AVPacket *av_packet, double timestamp);
void gsr_replay_buffer_clear(gsr_replay_buffer *self);
AVPacket* gsr_replay_buffer_iterator_get_packet(gsr_replay_buffer *self, gsr_replay_buffer_iterator iterator);

View File

@@ -1,4 +1,4 @@
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.12.1', default_options : ['warning_level=2'])
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.12.3', default_options : ['warning_level=2'])
add_project_arguments('-Wshadow', language : ['c', 'cpp'])
if get_option('buildtype') == 'debug'
@@ -117,6 +117,7 @@ executable('gpu-screen-recorder', src, dependencies : dep, install : true)
install_headers('plugin/plugin.h', install_dir : 'include/gsr')
install_man('gpu-screen-recorder.1', 'gsr-kms-server.1')
install_subdir('scripts', install_dir: 'share/gpu-screen-recorder')
if get_option('systemd') == true
install_data(files('extra/gpu-screen-recorder.service'), install_dir : 'lib/systemd/user')

View File

@@ -1,7 +1,7 @@
[package]
name = "gpu-screen-recorder"
type = "executable"
version = "5.12.1"
version = "5.12.3"
platforms = ["posix"]
[config]

View File

@@ -4,4 +4,4 @@
[ "$#" -ne 4 ] && echo "usage: twitch-stream-local-copy.sh <window_id> <fps> <livestream_key> <local_file>" && exit 1
active_sink=default_output
gpu-screen-recorder -w "$1" -c flv -f "$2" -q high -a "$active_sink" | tee -- "$4" | ffmpeg -i pipe:0 -c copy -f flv -- "rtmp://live.twitch.tv/app/$3"
gpu-screen-recorder -w "$1" -c flv -f "$2" -bm cbr -q 8000 -a "$active_sink" | tee -- "$4" | ffmpeg -i pipe:0 -c copy -f flv -- "rtmp://live.twitch.tv/app/$3"

View File

@@ -2,4 +2,4 @@
[ "$#" -ne 3 ] && echo "usage: twitch-stream.sh <window_id> <fps> <livestream_key>" && exit 1
active_sink=default_output
gpu-screen-recorder -w "$1" -c flv -f "$2" -q high -a "$active_sink" -o "rtmp://live.twitch.tv/app/$3"
gpu-screen-recorder -w "$1" -c flv -f "$2" -bm cbr -q 8000 -a "$active_sink" -o "rtmp://live.twitch.tv/app/$3"

View File

@@ -2,4 +2,4 @@
[ "$#" -ne 3 ] && echo "usage: youtube-hls-stream.sh <window_id> <fps> <livestream_key>" && exit 1
active_sink=default_output
gpu-screen-recorder -w "$1" -c hls -f "$2" -q high -a "$active_sink" -ac aac -o "https://a.upload.youtube.com/http_upload_hls?cid=$3&copy=0&file=stream.m3u8"
gpu-screen-recorder -w "$1" -c hls -f "$2" -bm cbr -q 8000 -a "$active_sink" -ac aac -o "https://a.upload.youtube.com/http_upload_hls?cid=$3&copy=0&file=stream.m3u8"

View File

@@ -196,7 +196,7 @@ static void usage_header(void) {
"[-bm auto|qp|vbr|cbr] [-cr limited|full] [-tune performance|quality] [-df yes|no] [-sc <script_path>] [-p <plugin_path>] "
"[-cursor yes|no] [-keyint <value>] [-restore-portal-session yes|no] [-portal-session-token-filepath filepath] [-encoder gpu|cpu] "
"[-fallback-cpu-encoding yes|no] [-o <output_file>] [-ro <output_directory>] [-ffmpeg-opts <options>] [--list-capture-options [card_path]] "
"[--list-audio-devices] [--list-application-audio] [--list-v4l2-devices] [-low-power yes|no] [-v yes|no] [-gl-debug yes|no] [--version] [-h|--help]\n", program_name);
"[--list-audio-devices] [--list-application-audio] [--list-v4l2-devices] [-write-first-frame-ts yes|no] [-low-power yes|no] [-v yes|no] [-gl-debug yes|no] [--version] [-h|--help]\n", program_name);
fflush(stdout);
}
@@ -255,6 +255,7 @@ static bool args_parser_set_values(args_parser *self) {
self->restart_replay_on_save = args_get_boolean_by_key(self->args, NUM_ARGS, "-restart-replay-on-save", false);
self->overclock = args_get_boolean_by_key(self->args, NUM_ARGS, "-oc", false);
self->fallback_cpu_encoding = args_get_boolean_by_key(self->args, NUM_ARGS, "-fallback-cpu-encoding", false);
self->write_first_frame_ts = args_get_boolean_by_key(self->args, NUM_ARGS, "-write-first-frame-ts", false);
self->low_power = args_get_boolean_by_key(self->args, NUM_ARGS, "-low-power", false);
self->audio_bitrate = args_get_i64_by_key(self->args, NUM_ARGS, "-ab", 0);
@@ -432,6 +433,10 @@ static bool args_parser_set_values(args_parser *self) {
self->is_output_piped = strcmp(self->filename, "/dev/stdout") == 0;
self->low_latency_recording = self->is_livestream || self->is_output_piped;
if(self->write_first_frame_ts && (self->is_livestream || self->is_output_piped)) {
fprintf(stderr, "gsr warning: -write-first-frame-ts is ignored for livestreaming or when output is piped\n");
self->write_first_frame_ts = false;
}
self->replay_recording_directory = args_get_value_by_key(self->args, NUM_ARGS, "-ro");
@@ -536,6 +541,7 @@ bool args_parser_parse(args_parser *self, int argc, char **argv, const args_hand
self->args[arg_index++] = (Arg){ .key = "-ffmpeg-opts", .optional = true, .list = false, .type = ARG_TYPE_STRING };
self->args[arg_index++] = (Arg){ .key = "-ffmpeg-video-opts", .optional = true, .list = false, .type = ARG_TYPE_STRING };
self->args[arg_index++] = (Arg){ .key = "-ffmpeg-audio-opts", .optional = true, .list = false, .type = ARG_TYPE_STRING };
self->args[arg_index++] = (Arg){ .key = "-write-first-frame-ts", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
self->args[arg_index++] = (Arg){ .key = "-low-power", .optional = true, .list = false, .type = ARG_TYPE_BOOLEAN };
assert(arg_index == NUM_ARGS);

View File

@@ -78,14 +78,6 @@ bool gsr_dbus_init(gsr_dbus *self, const char *screencast_restore_token) {
return false;
}
/* TODO: Check the name */
const int ret = dbus_bus_request_name(self->con, "com.dec05eba.gpu_screen_recorder", DBUS_NAME_FLAG_REPLACE_EXISTING, &self->err);
if(dbus_error_is_set(&self->err)) {
fprintf(stderr, "gsr error: gsr_dbus_init: dbus_bus_request_name failed with error: %s\n", self->err.message);
gsr_dbus_deinit(self);
return false;
}
if(screencast_restore_token) {
self->screencast_restore_token = strdup(screencast_restore_token);
if(!self->screencast_restore_token) {
@@ -95,12 +87,6 @@ bool gsr_dbus_init(gsr_dbus *self, const char *screencast_restore_token) {
}
}
(void)ret;
// if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
// fprintf(stderr, "gsr error: gsr_capture_portal_setup_dbus: dbus_bus_request_name failed to get primary owner\n");
// return false;
// }
return true;
}
@@ -119,8 +105,6 @@ void gsr_dbus_deinit(gsr_dbus *self) {
if(self->con) {
dbus_error_free(&self->err);
dbus_bus_release_name(self->con, "com.dec05eba.gpu_screen_recorder", NULL);
// Apparently shouldn't be used when a connection is setup by using dbus_bus_get
//dbus_connection_close(self->con);
dbus_connection_unref(self->con);

View File

@@ -3,10 +3,37 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include <errno.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
static uint64_t clock_gettime_microseconds(clockid_t clock_id) {
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;
clock_gettime(clock_id, &ts);
return (uint64_t)ts.tv_sec * 1000000ULL + (uint64_t)ts.tv_nsec / 1000ULL;
}
static void gsr_write_first_frame_timestamp_file(const char *filepath) {
const uint64_t evdev_compatible_ts = clock_gettime_microseconds(CLOCK_MONOTONIC);
const uint64_t unix_time_microsec = clock_gettime_microseconds(CLOCK_REALTIME);
FILE *file = fopen(filepath, "w");
if(!file) {
fprintf(stderr, "gsr warning: failed to open timestamp file '%s': %s\n", filepath, strerror(errno));
return;
}
fputs("monotonic_microsec\trealtime_microsec\n", file);
fprintf(file, "%" PRIu64 "\t%" PRIu64 "\n", evdev_compatible_ts, unix_time_microsec);
fclose(file);
}
bool gsr_encoder_init(gsr_encoder *self, gsr_replay_storage replay_storage, size_t replay_buffer_num_packets, double replay_buffer_time, const char *replay_directory) {
memset(self, 0, sizeof(*self));
self->num_recording_destinations = 0;
@@ -14,9 +41,17 @@ bool gsr_encoder_init(gsr_encoder *self, gsr_replay_storage replay_storage, size
if(pthread_mutex_init(&self->file_write_mutex, NULL) != 0) {
fprintf(stderr, "gsr error: gsr_encoder_init: failed to create mutex\n");
gsr_encoder_deinit(self);
return false;
}
self->mutex_created = true;
self->file_write_mutex_created = true;
if(pthread_mutex_init(&self->replay_mutex, NULL) != 0) {
fprintf(stderr, "gsr error: gsr_encoder_init: failed to create mutex\n");
gsr_encoder_deinit(self);
return false;
}
self->replay_mutex_created = true;
if(replay_buffer_num_packets > 0) {
self->replay_buffer = gsr_replay_buffer_create(replay_storage, replay_directory, replay_buffer_time, replay_buffer_num_packets);
@@ -31,14 +66,31 @@ bool gsr_encoder_init(gsr_encoder *self, gsr_replay_storage replay_storage, size
}
void gsr_encoder_deinit(gsr_encoder *self) {
if(self->mutex_created) {
self->mutex_created = false;
if(self->file_write_mutex_created)
pthread_mutex_lock(&self->file_write_mutex);
for(size_t i = 0; i < self->num_recording_destinations; ++i) {
free(self->recording_destinations[i].first_frame_ts_filepath);
self->recording_destinations[i].first_frame_ts_filepath = NULL;
self->recording_destinations[i].first_frame_ts_written = false;
}
if(self->file_write_mutex_created)
pthread_mutex_unlock(&self->file_write_mutex);
if(self->replay_buffer) {
pthread_mutex_lock(&self->replay_mutex);
gsr_replay_buffer_destroy(self->replay_buffer);
self->replay_buffer = NULL;
pthread_mutex_unlock(&self->replay_mutex);
}
if(self->file_write_mutex_created) {
self->file_write_mutex_created = false;
pthread_mutex_destroy(&self->file_write_mutex);
}
if(self->replay_buffer) {
gsr_replay_buffer_destroy(self->replay_buffer);
self->replay_buffer = NULL;
if(self->replay_mutex_created) {
self->replay_mutex_created = false;
pthread_mutex_destroy(&self->replay_mutex);
}
self->num_recording_destinations = 0;
@@ -60,9 +112,11 @@ void gsr_encoder_receive_packets(gsr_encoder *self, AVCodecContext *codec_contex
av_packet->dts = pts;
if(self->replay_buffer) {
pthread_mutex_lock(&self->replay_mutex);
const double time_now = clock_get_monotonic_seconds();
if(!gsr_replay_buffer_append(self->replay_buffer, av_packet, time_now))
fprintf(stderr, "gsr error: gsr_encoder_receive_packets: failed to add replay buffer data\n");
pthread_mutex_unlock(&self->replay_mutex);
}
pthread_mutex_lock(&self->file_write_mutex);
@@ -77,6 +131,11 @@ void gsr_encoder_receive_packets(gsr_encoder *self, AVCodecContext *codec_contex
else if(!recording_destination->has_received_keyframe)
continue;
if(recording_destination->first_frame_ts_filepath && !recording_destination->first_frame_ts_written) {
gsr_write_first_frame_timestamp_file(recording_destination->first_frame_ts_filepath);
recording_destination->first_frame_ts_written = true;
}
av_packet->pts = pts - recording_destination->start_pts;
av_packet->dts = pts - recording_destination->start_pts;
@@ -131,6 +190,8 @@ size_t gsr_encoder_add_recording_destination(gsr_encoder *self, AVCodecContext *
recording_destination->stream = stream;
recording_destination->start_pts = start_pts;
recording_destination->has_received_keyframe = false;
recording_destination->first_frame_ts_filepath = NULL;
recording_destination->first_frame_ts_written = false;
++self->recording_destination_id_counter;
++self->num_recording_destinations;
@@ -144,6 +205,9 @@ bool gsr_encoder_remove_recording_destination(gsr_encoder *self, size_t id) {
pthread_mutex_lock(&self->file_write_mutex);
for(size_t i = 0; i < self->num_recording_destinations; ++i) {
if(self->recording_destinations[i].id == id) {
free(self->recording_destinations[i].first_frame_ts_filepath);
self->recording_destinations[i].first_frame_ts_filepath = NULL;
self->recording_destinations[i].first_frame_ts_written = false;
self->recording_destinations[i] = self->recording_destinations[self->num_recording_destinations - 1];
--self->num_recording_destinations;
found = true;
@@ -153,3 +217,26 @@ bool gsr_encoder_remove_recording_destination(gsr_encoder *self, size_t id) {
pthread_mutex_unlock(&self->file_write_mutex);
return found;
}
bool gsr_encoder_set_recording_destination_first_frame_ts_filepath(gsr_encoder *self, size_t id, const char *filepath) {
if(!filepath)
return false;
bool found = false;
pthread_mutex_lock(&self->file_write_mutex);
for(size_t i = 0; i < self->num_recording_destinations; ++i) {
if(self->recording_destinations[i].id == id) {
char *filepath_copy = strdup(filepath);
if(!filepath_copy)
break;
free(self->recording_destinations[i].first_frame_ts_filepath);
self->recording_destinations[i].first_frame_ts_filepath = filepath_copy;
self->recording_destinations[i].first_frame_ts_written = false;
found = true;
break;
}
}
pthread_mutex_unlock(&self->file_write_mutex);
return found;
}

View File

@@ -1293,48 +1293,60 @@ struct AudioPtsOffset {
int stream_index = 0;
};
static void save_replay_async(AVCodecContext *video_codec_context, int video_stream_index, const std::vector<AudioTrack> &audio_tracks, gsr_replay_buffer *replay_buffer, const args_parser &arg_parser, const std::string &file_extension, bool date_folders, bool hdr, std::vector<VideoSource> &video_sources, int current_save_replay_seconds) {
static void save_replay_async(AVCodecContext *video_codec_context, int video_stream_index, const std::vector<AudioTrack> &audio_tracks, gsr_encoder *encoder, const args_parser &arg_parser, const std::string &file_extension, bool date_folders, bool hdr, std::vector<VideoSource> &video_sources, int current_save_replay_seconds) {
if(save_replay_thread.valid())
return;
const gsr_replay_buffer_iterator search_start_iterator = current_save_replay_seconds == save_replay_seconds_full ? gsr_replay_buffer_iterator{0, 0} : gsr_replay_buffer_find_packet_index_by_time_passed(replay_buffer, current_save_replay_seconds);
const gsr_replay_buffer_iterator video_start_iterator = gsr_replay_buffer_find_keyframe(replay_buffer, search_start_iterator, video_stream_index, false);
if(video_start_iterator.packet_index == (size_t)-1) {
fprintf(stderr, "gsr error: failed to save replay: failed to find a video keyframe. perhaps replay was saved too fast, before anything has been recorded\n");
return;
}
const int64_t video_pts_offset = gsr_replay_buffer_iterator_get_packet(replay_buffer, video_start_iterator)->pts;
std::vector<AudioPtsOffset> audio_pts_offsets;
audio_pts_offsets.reserve(audio_tracks.size());
for(const AudioTrack &audio_track : audio_tracks) {
const gsr_replay_buffer_iterator audio_start_iterator = gsr_replay_buffer_find_keyframe(replay_buffer, video_start_iterator, audio_track.stream_index, false);
const int64_t audio_pts_offset = audio_start_iterator.packet_index == (size_t)-1 ? 0 : gsr_replay_buffer_iterator_get_packet(replay_buffer, audio_start_iterator)->pts;
audio_pts_offsets.push_back(AudioPtsOffset{audio_pts_offset, audio_track.stream_index});
}
gsr_replay_buffer *cloned_replay_buffer = gsr_replay_buffer_clone(replay_buffer);
pthread_mutex_lock(&encoder->replay_mutex);
gsr_replay_buffer *cloned_replay_buffer = gsr_replay_buffer_clone(encoder->replay_buffer);
pthread_mutex_unlock(&encoder->replay_mutex);
if(!cloned_replay_buffer) {
// TODO: Return this error to mark the replay as failed
fprintf(stderr, "gsr error: failed to save replay: failed to clone replay buffer\n");
return;
}
const gsr_replay_buffer_iterator search_start_iterator = current_save_replay_seconds == save_replay_seconds_full ? gsr_replay_buffer_iterator{0, 0} : gsr_replay_buffer_find_packet_index_by_time_passed(cloned_replay_buffer, current_save_replay_seconds);
const gsr_replay_buffer_iterator video_start_iterator = gsr_replay_buffer_find_keyframe(cloned_replay_buffer, search_start_iterator, video_stream_index, false);
if(video_start_iterator.packet_index == (size_t)-1) {
fprintf(stderr, "gsr error: failed to save replay: failed to find a video keyframe. perhaps replay was saved too fast, before anything has been recorded\n");
pthread_mutex_lock(&encoder->replay_mutex);
gsr_replay_buffer_destroy(cloned_replay_buffer);
pthread_mutex_unlock(&encoder->replay_mutex);
return;
}
const int64_t video_pts_offset = gsr_replay_buffer_iterator_get_packet(cloned_replay_buffer, video_start_iterator)->pts;
std::vector<AudioPtsOffset> audio_pts_offsets;
audio_pts_offsets.reserve(audio_tracks.size());
for(const AudioTrack &audio_track : audio_tracks) {
const gsr_replay_buffer_iterator audio_start_iterator = gsr_replay_buffer_find_keyframe(cloned_replay_buffer, video_start_iterator, audio_track.stream_index, false);
const int64_t audio_pts_offset = audio_start_iterator.packet_index == (size_t)-1 ? 0 : gsr_replay_buffer_iterator_get_packet(cloned_replay_buffer, audio_start_iterator)->pts;
audio_pts_offsets.push_back(AudioPtsOffset{audio_pts_offset, audio_track.stream_index});
}
std::string output_filepath = create_new_recording_filepath_from_timestamp(arg_parser.filename, "Replay", file_extension, date_folders);
RecordingStartResult recording_start_result = start_recording_create_streams(output_filepath.c_str(), arg_parser, video_codec_context, audio_tracks, hdr, video_sources);
if(!recording_start_result.av_format_context)
if(!recording_start_result.av_format_context) {
pthread_mutex_lock(&encoder->replay_mutex);
gsr_replay_buffer_destroy(cloned_replay_buffer);
pthread_mutex_unlock(&encoder->replay_mutex);
return;
}
save_replay_output_filepath = std::move(output_filepath);
save_replay_thread = std::async(std::launch::async, [video_stream_index, recording_start_result, video_start_iterator, video_pts_offset, audio_pts_offsets{std::move(audio_pts_offsets)}, video_codec_context, cloned_replay_buffer]() mutable {
save_replay_thread = std::async(std::launch::async, [video_stream_index, recording_start_result, video_start_iterator, video_pts_offset, audio_pts_offsets{std::move(audio_pts_offsets)}, video_codec_context, cloned_replay_buffer, encoder]() mutable {
gsr_replay_buffer_iterator replay_iterator = video_start_iterator;
for(;;) {
AVPacket *replay_packet = gsr_replay_buffer_iterator_get_packet(cloned_replay_buffer, replay_iterator);
uint8_t *replay_packet_data = NULL;
if(replay_packet)
if(replay_packet) {
pthread_mutex_lock(&encoder->replay_mutex);
replay_packet_data = gsr_replay_buffer_iterator_get_packet_data(cloned_replay_buffer, replay_iterator);
pthread_mutex_unlock(&encoder->replay_mutex);
}
if(!replay_packet) {
fprintf(stderr, "gsr error: save_replay_async: no replay packet\n");
@@ -1397,7 +1409,10 @@ static void save_replay_async(AVCodecContext *video_codec_context, int video_str
}
stop_recording_close_streams(recording_start_result.av_format_context);
pthread_mutex_lock(&encoder->replay_mutex);
gsr_replay_buffer_destroy(cloned_replay_buffer);
pthread_mutex_unlock(&encoder->replay_mutex);
});
}
@@ -2254,6 +2269,7 @@ static std::string region_get_data(gsr_egl *egl, vec2i *region_size, vec2i *regi
} else {
region_position->x -= monitor_pos.x;
region_position->y -= monitor_pos.y;
// Match drm plane coordinate space (1x scaling) to wayland coordinate space (which may have scaling set by user)
region_position->x *= monitor_scale_inverted;
region_position->y *= monitor_scale_inverted;
@@ -2784,15 +2800,6 @@ static bool string_to_bool(const char *str, size_t len, bool *value) {
}
}
static int clamp_scalar(int value) {
if(value < 0)
return 0;
else if(value > 100)
return 100;
else
return value;
}
static void parse_capture_source_options(const std::string &capture_source_str, CaptureSource &capture_source) {
bool is_first_column = true;
@@ -2814,9 +2821,6 @@ static void parse_capture_source_options(const std::string &capture_source_str,
fprintf(stderr, "gsr error: invalid capture target value for option x: \"%.*s\", expected a number\n", (int)size, sub);
_exit(1);
}
if(capture_source.pos.x_type == VVEC2I_TYPE_SCALAR)
capture_source.pos.x = clamp_scalar(capture_source.pos.x);
} else if(string_starts_with(sub, size, "y=")) {
capture_source.pos.y_type = sub[size - 1] == '%' ? VVEC2I_TYPE_SCALAR : VVEC2I_TYPE_PIXELS;
sub += 2;
@@ -2826,8 +2830,6 @@ static void parse_capture_source_options(const std::string &capture_source_str,
_exit(1);
}
if(capture_source.pos.y_type == VVEC2I_TYPE_SCALAR)
capture_source.pos.y = clamp_scalar(capture_source.pos.y);
} else if(string_starts_with(sub, size, "width=")) {
capture_source.size.x_type = sub[size - 1] == '%' ? VVEC2I_TYPE_SCALAR : VVEC2I_TYPE_PIXELS;
sub += 6;
@@ -2836,9 +2838,6 @@ static void parse_capture_source_options(const std::string &capture_source_str,
fprintf(stderr, "gsr error: invalid capture target value for option width: \"%.*s\", expected a number\n", (int)size, sub);
_exit(1);
}
if(capture_source.size.x_type == VVEC2I_TYPE_SCALAR)
capture_source.size.x = clamp_scalar(capture_source.size.x);
} else if(string_starts_with(sub, size, "height=")) {
capture_source.size.y_type = sub[size - 1] == '%' ? VVEC2I_TYPE_SCALAR : VVEC2I_TYPE_PIXELS;
sub += 7;
@@ -2847,9 +2846,6 @@ static void parse_capture_source_options(const std::string &capture_source_str,
fprintf(stderr, "gsr error: invalid capture target value for option height: \"%.*s\", expected a number\n", (int)size, sub);
_exit(1);
}
if(capture_source.size.y_type == VVEC2I_TYPE_SCALAR)
capture_source.size.y = clamp_scalar(capture_source.size.y);
} else if(string_starts_with(sub, size, "halign=")) {
sub += 7;
size -= 7;
@@ -3992,7 +3988,11 @@ int main(int argc, char **argv) {
if(video_stream) {
avcodec_parameters_from_context(video_stream->codecpar, video_codec_context);
gsr_encoder_add_recording_destination(&encoder, video_codec_context, av_format_context, video_stream, 0);
const size_t video_destination_id = gsr_encoder_add_recording_destination(&encoder, video_codec_context, av_format_context, video_stream, 0);
if(arg_parser.write_first_frame_ts && video_destination_id != (size_t)-1) {
std::string ts_filepath = std::string(arg_parser.filename) + ".ts";
gsr_encoder_set_recording_destination_first_frame_ts_filepath(&encoder, video_destination_id, ts_filepath.c_str());
}
}
int audio_max_frame_size = 1024;
@@ -4531,6 +4531,11 @@ int main(int argc, char **argv) {
replay_recording_start_result = start_recording_create_streams(replay_recording_filepath.c_str(), arg_parser, video_codec_context, audio_tracks, hdr, video_sources);
if(replay_recording_start_result.av_format_context) {
const size_t video_recording_destination_id = gsr_encoder_add_recording_destination(&encoder, video_codec_context, replay_recording_start_result.av_format_context, replay_recording_start_result.video_stream, video_frame->pts);
if(arg_parser.write_first_frame_ts && video_recording_destination_id != (size_t)-1) {
std::string ts_filepath = replay_recording_filepath + ".ts";
gsr_encoder_set_recording_destination_first_frame_ts_filepath(&encoder, video_recording_destination_id, ts_filepath.c_str());
}
if(video_recording_destination_id != (size_t)-1)
replay_recording_items.push_back(video_recording_destination_id);
@@ -4590,10 +4595,13 @@ int main(int argc, char **argv) {
save_replay_seconds = 0;
save_replay_output_filepath.clear();
save_replay_async(video_codec_context, VIDEO_STREAM_INDEX, audio_tracks, encoder.replay_buffer, arg_parser, file_extension, arg_parser.date_folders, hdr, video_sources, current_save_replay_seconds);
save_replay_async(video_codec_context, VIDEO_STREAM_INDEX, audio_tracks, &encoder, arg_parser, file_extension, arg_parser.date_folders, hdr, video_sources, current_save_replay_seconds);
if(arg_parser.restart_replay_on_save && current_save_replay_seconds == save_replay_seconds_full)
if(arg_parser.restart_replay_on_save && current_save_replay_seconds == save_replay_seconds_full) {
pthread_mutex_lock(&encoder.replay_mutex);
gsr_replay_buffer_clear(encoder.replay_buffer);
pthread_mutex_unlock(&encoder.replay_mutex);
}
}
const double time_at_frame_end = clock_get_monotonic_seconds() - paused_time_offset;

View File

@@ -16,48 +16,14 @@ gsr_replay_buffer* gsr_replay_buffer_create(gsr_replay_storage replay_storage, c
replay_buffer = gsr_replay_buffer_disk_create(replay_directory, replay_buffer_time);
break;
}
replay_buffer->mutex_initialized = false;
replay_buffer->original_replay_buffer = NULL;
if(pthread_mutex_init(&replay_buffer->mutex, NULL) != 0) {
gsr_replay_buffer_destroy(replay_buffer);
return NULL;
}
replay_buffer->mutex_initialized = true;
return replay_buffer;
}
void gsr_replay_buffer_destroy(gsr_replay_buffer *self) {
self->destroy(self);
if(self->mutex_initialized && !self->original_replay_buffer) {
pthread_mutex_destroy(&self->mutex);
self->mutex_initialized = false;
}
self->original_replay_buffer = NULL;
free(self);
}
void gsr_replay_buffer_lock(gsr_replay_buffer *self) {
if(self->original_replay_buffer) {
gsr_replay_buffer_lock(self->original_replay_buffer);
return;
}
if(self->mutex_initialized)
pthread_mutex_lock(&self->mutex);
}
void gsr_replay_buffer_unlock(gsr_replay_buffer *self) {
if(self->original_replay_buffer) {
gsr_replay_buffer_unlock(self->original_replay_buffer);
return;
}
if(self->mutex_initialized)
pthread_mutex_unlock(&self->mutex);
}
bool gsr_replay_buffer_append(gsr_replay_buffer *self, const AVPacket *av_packet, double timestamp) {
return self->append(self, av_packet, timestamp);
}

View File

@@ -94,7 +94,6 @@ static void gsr_replay_buffer_file_unref(gsr_replay_buffer_file *self, const cha
static void gsr_replay_buffer_disk_clear(gsr_replay_buffer *replay_buffer) {
gsr_replay_buffer_disk *self = (gsr_replay_buffer_disk*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
for(size_t i = 0; i < self->num_files; ++i) {
gsr_replay_buffer_file_unref(self->files[i], self->replay_directory);
@@ -107,7 +106,6 @@ static void gsr_replay_buffer_disk_clear(gsr_replay_buffer *replay_buffer) {
}
self->storage_num_bytes_written = 0;
gsr_replay_buffer_unlock(&self->replay_buffer);
}
static void gsr_replay_buffer_disk_destroy(gsr_replay_buffer *replay_buffer) {
@@ -197,7 +195,6 @@ static void gsr_replay_buffer_disk_remove_first_file(gsr_replay_buffer_disk *sel
static bool gsr_replay_buffer_disk_append(gsr_replay_buffer *replay_buffer, const AVPacket *av_packet, double timestamp) {
gsr_replay_buffer_disk *self = (gsr_replay_buffer_disk*)replay_buffer;
bool success = false;
gsr_replay_buffer_lock(&self->replay_buffer);
if(self->storage_fd <= 0) {
if(!gsr_replay_buffer_disk_create_next_file(self, timestamp))
@@ -215,7 +212,6 @@ static bool gsr_replay_buffer_disk_append(gsr_replay_buffer *replay_buffer, cons
success = data_written;
done:
gsr_replay_buffer_unlock(&self->replay_buffer);
return success;
}
@@ -265,11 +261,7 @@ static gsr_replay_buffer* gsr_replay_buffer_disk_clone(gsr_replay_buffer *replay
return NULL;
gsr_replay_buffer_disk_set_impl_funcs(destination);
gsr_replay_buffer_lock(&self->replay_buffer);
destination->replay_buffer.original_replay_buffer = replay_buffer;
destination->replay_buffer.mutex = self->replay_buffer.mutex;
destination->replay_buffer.mutex_initialized = self->replay_buffer.mutex_initialized;
destination->replay_buffer_time = self->replay_buffer_time;
destination->storage_counter = self->storage_counter;
destination->storage_num_bytes_written = self->storage_num_bytes_written;
@@ -283,7 +275,6 @@ static gsr_replay_buffer* gsr_replay_buffer_disk_clone(gsr_replay_buffer *replay
snprintf(destination->replay_directory, sizeof(destination->replay_directory), "%s", self->replay_directory);
destination->owns_directory = false;
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer*)destination;
}
@@ -319,11 +310,9 @@ static size_t gsr_replay_buffer_file_find_packet_index_by_time_passed(const gsr_
/* Binary search */
static gsr_replay_buffer_iterator gsr_replay_buffer_disk_find_file_index_by_time_passed(gsr_replay_buffer *replay_buffer, int seconds) {
gsr_replay_buffer_disk *self = (gsr_replay_buffer_disk*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
const double now = clock_get_monotonic_seconds();
if(self->num_files == 0) {
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer_iterator){0, 0};
}
@@ -352,14 +341,12 @@ static gsr_replay_buffer_iterator gsr_replay_buffer_disk_find_file_index_by_time
const gsr_replay_buffer_file *file = self->files[file_index];
const size_t packet_index = gsr_replay_buffer_file_find_packet_index_by_time_passed(file, seconds);
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer_iterator){packet_index, file_index};
}
static gsr_replay_buffer_iterator gsr_replay_buffer_disk_find_keyframe(gsr_replay_buffer *replay_buffer, gsr_replay_buffer_iterator start_iterator, int stream_index, bool invert_stream_index) {
gsr_replay_buffer_disk *self = (gsr_replay_buffer_disk*)replay_buffer;
gsr_replay_buffer_iterator keyframe_iterator = {(size_t)-1, 0};
gsr_replay_buffer_lock(&self->replay_buffer);
size_t packet_index = start_iterator.packet_index;
for(size_t file_index = start_iterator.file_index; file_index < self->num_files; ++file_index) {
const gsr_replay_buffer_file *file = self->files[file_index];
@@ -374,7 +361,6 @@ static gsr_replay_buffer_iterator gsr_replay_buffer_disk_find_keyframe(gsr_repla
packet_index = 0;
}
done:
gsr_replay_buffer_unlock(&self->replay_buffer);
return keyframe_iterator;
}

View File

@@ -54,7 +54,6 @@ static void gsr_av_packet_ram_unref(gsr_av_packet_ram *self) {
static void gsr_replay_buffer_ram_destroy(gsr_replay_buffer *replay_buffer) {
gsr_replay_buffer_ram *self = (gsr_replay_buffer_ram*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
for(size_t i = 0; i < self->num_packets; ++i) {
if(self->packets[i]) {
gsr_av_packet_ram_unref(self->packets[i]);
@@ -62,7 +61,6 @@ static void gsr_replay_buffer_ram_destroy(gsr_replay_buffer *replay_buffer) {
}
}
self->num_packets = 0;
gsr_replay_buffer_unlock(&self->replay_buffer);
if(self->packets) {
free(self->packets);
@@ -75,12 +73,9 @@ static void gsr_replay_buffer_ram_destroy(gsr_replay_buffer *replay_buffer) {
static bool gsr_replay_buffer_ram_append(gsr_replay_buffer *replay_buffer, const AVPacket *av_packet, double timestamp) {
gsr_replay_buffer_ram *self = (gsr_replay_buffer_ram*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
gsr_av_packet_ram *packet = gsr_av_packet_ram_create(av_packet, timestamp);
if(!packet) {
gsr_replay_buffer_unlock(&self->replay_buffer);
if(!packet)
return false;
}
if(self->packets[self->index]) {
gsr_av_packet_ram_unref(self->packets[self->index]);
@@ -93,13 +88,11 @@ static bool gsr_replay_buffer_ram_append(gsr_replay_buffer *replay_buffer, const
if(self->num_packets > self->capacity_num_packets)
self->num_packets = self->capacity_num_packets;
gsr_replay_buffer_unlock(&self->replay_buffer);
return true;
}
static void gsr_replay_buffer_ram_clear(gsr_replay_buffer *replay_buffer) {
gsr_replay_buffer_ram *self = (gsr_replay_buffer_ram*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
for(size_t i = 0; i < self->num_packets; ++i) {
if(self->packets[i]) {
gsr_av_packet_ram_unref(self->packets[i]);
@@ -108,7 +101,6 @@ static void gsr_replay_buffer_ram_clear(gsr_replay_buffer *replay_buffer) {
}
self->num_packets = 0;
self->index = 0;
gsr_replay_buffer_unlock(&self->replay_buffer);
}
static gsr_av_packet_ram* gsr_replay_buffer_ram_get_packet_at_index(gsr_replay_buffer *replay_buffer, size_t index) {
@@ -141,17 +133,12 @@ static gsr_replay_buffer* gsr_replay_buffer_ram_clone(gsr_replay_buffer *replay_
return NULL;
gsr_replay_buffer_ram_set_impl_funcs(destination);
gsr_replay_buffer_lock(&self->replay_buffer);
destination->replay_buffer.original_replay_buffer = replay_buffer;
destination->replay_buffer.mutex = self->replay_buffer.mutex;
destination->replay_buffer.mutex_initialized = self->replay_buffer.mutex_initialized;
destination->capacity_num_packets = self->capacity_num_packets;
destination->index = self->index;
destination->packets = calloc(destination->capacity_num_packets, sizeof(gsr_av_packet_ram*));
if(!destination->packets) {
free(destination);
gsr_replay_buffer_unlock(&self->replay_buffer);
return NULL;
}
@@ -160,18 +147,15 @@ static gsr_replay_buffer* gsr_replay_buffer_ram_clone(gsr_replay_buffer *replay_
destination->packets[i] = gsr_av_packet_ram_ref(self->packets[i]);
}
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer*)destination;
}
/* Binary search */
static gsr_replay_buffer_iterator gsr_replay_buffer_ram_find_packet_index_by_time_passed(gsr_replay_buffer *replay_buffer, int seconds) {
gsr_replay_buffer_ram *self = (gsr_replay_buffer_ram*)replay_buffer;
gsr_replay_buffer_lock(&self->replay_buffer);
const double now = clock_get_monotonic_seconds();
if(self->num_packets == 0) {
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer_iterator){0, 0};
}
@@ -194,14 +178,12 @@ static gsr_replay_buffer_iterator gsr_replay_buffer_ram_find_packet_index_by_tim
}
}
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer_iterator){index, 0};
}
static gsr_replay_buffer_iterator gsr_replay_buffer_ram_find_keyframe(gsr_replay_buffer *replay_buffer, gsr_replay_buffer_iterator start_iterator, int stream_index, bool invert_stream_index) {
gsr_replay_buffer_ram *self = (gsr_replay_buffer_ram*)replay_buffer;
size_t keyframe_index = (size_t)-1;
gsr_replay_buffer_lock(&self->replay_buffer);
for(size_t i = start_iterator.packet_index; i < self->num_packets; ++i) {
const gsr_av_packet_ram *packet = gsr_replay_buffer_ram_get_packet_at_index(replay_buffer, i);
if((packet->packet.flags & AV_PKT_FLAG_KEY) && (invert_stream_index ? packet->packet.stream_index != stream_index : packet->packet.stream_index == stream_index)) {
@@ -209,7 +191,6 @@ static gsr_replay_buffer_iterator gsr_replay_buffer_ram_find_keyframe(gsr_replay
break;
}
}
gsr_replay_buffer_unlock(&self->replay_buffer);
return (gsr_replay_buffer_iterator){keyframe_index, 0};
}