diff options
Diffstat (limited to 'src/recorder/recorder.c')
| -rw-r--r-- | src/recorder/recorder.c | 826 |
1 files changed, 826 insertions, 0 deletions
diff --git a/src/recorder/recorder.c b/src/recorder/recorder.c new file mode 100644 index 0000000..27cc1ad --- /dev/null +++ b/src/recorder/recorder.c @@ -0,0 +1,826 @@ +#include "../../include/recorder/recorder.h" +#include "../../include/recorder/error.h" +#include "../../include/recorder/audio_codec.h" +#include "../../include/recorder/video_codec.h" +#include "../../include/recorder/codec_select.h" +#include "../../include/recorder/muxer.h" +#include "../../include/recorder/replay_save.h" +#include "../../include/recorder/audio_capture.h" +#include "../../include/recorder/recording_clock.h" +#include "../../include/recorder/screenshot.h" +#include "../../include/encoder/encoder.h" +#include "../../include/encoder/video/video.h" +#include "../../include/window/window.h" +#include "../../include/color_conversion.h" +#include "../../include/damage.h" +#include "../../include/cursor.h" +#include "../../include/plugins.h" +#include "../../include/utils.h" +#include "../../include/ffmpeg_utils.h" +#include "../../include/log.h" + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <assert.h> +#include <limits.h> +#include <unistd.h> + +#include <libavutil/time.h> +#include <libavformat/avformat.h> + +#include <X11/Xlib.h> + +#define GSR_VIDEO_STREAM_INDEX 0 + +struct gsr_recorder { + gsr_recorder_settings settings; + gsr_recorder_callbacks callbacks; + gsr_windowing *windowing; + gsr_egl *egl; + gsr_window *window; + gsr_capture_deps *capture_deps; + gsr_capture_sources *capture_sources; + gsr_audio_input_tracks *audio_input_tracks; + + char file_extension[32]; + bool force_no_audio_offset; + double target_fps; + bool uses_amix; + bool hdr; + bool low_power; + vec2i video_size; + + AVFormatContext *av_format_context; + AVStream *video_stream; + AVCodecContext *video_codec_context; + AVFrame *video_frame; + gsr_video_sources video_sources_data; + gsr_video_sources *video_sources; + gsr_encoder encoder; + bool encoder_initialized; + gsr_video_encoder *video_encoder; + gsr_color_conversion color_conversion; + bool color_conversion_initialized; + gsr_color_conversion *output_color_conversion; + gsr_plugins plugins; + gsr_recording_clock *recording_clock; + gsr_audio_capture audio_capture; + bool audio_capture_initialized; + gsr_replay_save replay_save; + + gsr_recording_output replay_recording_output; + size_t replay_recording_items[GSR_MAX_RECORDING_DESTINATIONS]; + size_t num_replay_recording_items; + char replay_recording_filepath[PATH_MAX]; + bool replay_recording; + + volatile sig_atomic_t running; + volatile sig_atomic_t toggle_pause; + volatile sig_atomic_t toggle_replay_recording; + volatile sig_atomic_t save_replay_seconds; + bool should_stop_error; + bool force_iframe_frame; + int audio_max_frame_size; + bool use_damage_tracking; + gsr_damage damage; + const char **plugin_filepaths; + int num_plugin_filepaths; +#ifdef GSR_APP_AUDIO + gsr_pipewire_audio *pipewire_audio; +#endif +}; + +static void gsr_recorder_stop_recording(gsr_recorder *self); + +static int64_t gsr_max_int64(int64_t a, int64_t b) { + return a > b ? a : b; +} + +static int64_t gsr_min_int64(int64_t a, int64_t b) { + return a < b ? a : b; +} + +gsr_recorder* gsr_recorder_create(const gsr_recorder_params *params, const gsr_recorder_callbacks *callbacks, int *error) { + *error = GSR_ERROR_GENERIC; + + gsr_recorder *self = calloc(1, sizeof(gsr_recorder)); + if(!self) { + gsr_log(GSR_LOG_LEVEL_ERROR, "gsr_recorder_create: failed to allocate recorder"); + return NULL; + } + + self->settings = *params->settings; + if(callbacks) + self->callbacks = *callbacks; + self->windowing = params->windowing; + self->egl = ¶ms->windowing->egl; + self->window = params->windowing->window; + self->capture_deps = params->capture_deps; + self->capture_sources = params->capture_sources; + self->audio_input_tracks = params->audio_input_tracks; + self->running = 1; + self->audio_max_frame_size = 1024; + self->hdr = video_codec_is_hdr(params->settings->video_codec); + self->plugin_filepaths = params->plugin_filepaths; + self->num_plugin_filepaths = params->num_plugin_filepaths; +#ifdef GSR_APP_AUDIO + self->pipewire_audio = params->pipewire_audio; +#endif + + + // The output format is automatically guessed by the file extension + avformat_alloc_output_context2(&self->av_format_context, NULL, self->settings.container_format, self->settings.filename); + if (!self->av_format_context) { + if(self->settings.container_format) { + gsr_log(GSR_LOG_LEVEL_ERROR, "Container format '%s' (argument -c) is not valid", self->settings.container_format); + } else { + gsr_log(GSR_LOG_LEVEL_ERROR, "Failed to deduce container format from file extension. Use the '-c' option to specify container format"); + args_parser_print_usage(); + _exit(1); + } + _exit(1); + } + + set_format_context_options(self->av_format_context); + + const AVOutputFormat *output_format = self->av_format_context->oformat; + + const char *file_extensions = output_format->extensions ? output_format->extensions : ""; + const char *file_extension_end = strchr(file_extensions, ','); + if(file_extension_end) + snprintf(self->file_extension, sizeof(self->file_extension), "%.*s", (int)(file_extension_end - file_extensions), file_extensions); + else + snprintf(self->file_extension, sizeof(self->file_extension), "%s", file_extensions); + + if(self->file_extension[0] == '\0') + snprintf(self->file_extension, sizeof(self->file_extension), "%s", self->settings.container_format ? self->settings.container_format : ""); + + self->force_no_audio_offset = self->settings.is_livestream || self->settings.is_output_piped || (strcmp(self->file_extension, "mp4") != 0 && strcmp(self->file_extension, "mkv") != 0 && strcmp(self->file_extension, "webm") != 0); + self->target_fps = 1.0 / (double)self->settings.fps; + + self->uses_amix = gsr_audio_input_tracks_should_use_amix(self->audio_input_tracks); + self->settings.audio_codec = select_audio_codec_with_fallback(self->settings.audio_codec, self->file_extension, self->uses_amix); + + self->video_size = (vec2i){0, 0}; + const int video_sources_result = gsr_video_sources_create(&self->video_sources_data, &self->settings, self->egl, self->capture_deps, false, self->capture_sources, &self->video_size); + if(video_sources_result != GSR_ERROR_OK) + _exit(gsr_error_to_exit_code(video_sources_result)); + + self->video_sources = &self->video_sources_data; + + // (Some?) livestreaming services require at least one audio track to work. + // If not audio is provided then create one silent audio track. + if(self->settings.is_livestream && self->audio_input_tracks->num_items == 0) { + gsr_log(GSR_LOG_LEVEL_INFO, "live streaming but no audio track was added. Adding a silent audio track"); + gsr_merged_audio_inputs silent_audio_track; + memset(&silent_audio_track, 0, sizeof(silent_audio_track)); + gsr_audio_input silent_audio_input; + memset(&silent_audio_input, 0, sizeof(silent_audio_input)); + if(!gsr_merged_audio_inputs_add(&silent_audio_track, &silent_audio_input) || !gsr_audio_input_tracks_add(self->audio_input_tracks, &silent_audio_track)) + _exit(1); + } + + self->video_stream = NULL; + + if(self->settings.video_encoder == GSR_VIDEO_ENCODER_HW_CPU && self->settings.video_codec != (gsr_video_codec)GSR_VIDEO_CODEC_AUTO && self->settings.video_codec != GSR_VIDEO_CODEC_H264) { + gsr_log(GSR_LOG_LEVEL_ERROR, "-self->encoder cpu was specified but a codec other than h264 was specified. -self->encoder cpu supports only h264 at the moment"); + _exit(1); + } + + self->low_power = false; + const AVCodec *video_codec_f = NULL; + const int select_video_codec_result = select_video_codec_with_fallback(self->video_size, &self->settings, self->file_extension, self->egl, &self->low_power, &video_codec_f); + if(select_video_codec_result != GSR_ERROR_OK) + _exit(gsr_error_to_exit_code(select_video_codec_result)); + + const enum AVPixelFormat video_pix_fmt = get_pixel_format(self->settings.video_codec, self->egl->gpu_info.vendor, self->settings.video_encoder == GSR_VIDEO_ENCODER_HW_CPU); + self->video_codec_context = create_video_codec_context(video_pix_fmt, video_codec_f, self->egl, &self->settings, self->video_size.x, self->video_size.y); + if(!self->settings.is_replaying) + self->video_stream = create_stream(self->av_format_context, self->video_codec_context); + + self->video_frame = av_frame_alloc(); + if(!self->video_frame) { + gsr_log(GSR_LOG_LEVEL_ERROR, "Failed to allocate video frame"); + _exit(1); + } + self->video_frame->format = self->video_codec_context->pix_fmt; + self->video_frame->width = self->video_size.x; + self->video_frame->height = self->video_size.y; + self->video_frame->color_range = self->video_codec_context->color_range; + self->video_frame->color_primaries = self->video_codec_context->color_primaries; + self->video_frame->color_trc = self->video_codec_context->color_trc; + self->video_frame->colorspace = self->video_codec_context->colorspace; + self->video_frame->chroma_location = self->video_codec_context->chroma_sample_location; + + const size_t estimated_replay_buffer_packets = calculate_estimated_replay_buffer_packets(self->settings.replay_buffer_size_secs, self->settings.fps, self->settings.audio_codec, self->audio_input_tracks); + self->recording_clock = gsr_recording_clock_create(); + if(!self->recording_clock) + _exit(1); + + if(!gsr_encoder_init(&self->encoder, self->settings.replay_storage, estimated_replay_buffer_packets, self->settings.replay_buffer_size_secs, self->settings.filename)) { + gsr_log(GSR_LOG_LEVEL_ERROR, "failed to create self->encoder"); + _exit(1); + } + + self->video_encoder = create_video_encoder(self->egl, &self->settings); + if(!self->video_encoder) { + gsr_log(GSR_LOG_LEVEL_ERROR, "failed to create video self->encoder"); + _exit(1); + } + + if(!gsr_video_encoder_start(self->video_encoder, self->video_codec_context, self->video_frame)) { + gsr_log(GSR_LOG_LEVEL_ERROR, "failed to start video self->encoder"); + _exit(1); + } + + self->video_size.x = self->video_codec_context->width; + self->video_size.y = self->video_codec_context->height; + gsr_video_sources_update_with_real_video_size(self->video_sources, self->video_size); + + memset(&self->plugins, 0, sizeof(self->plugins)); + + if(gsr_load_plugins(&self->plugins, self->plugin_filepaths, self->num_plugin_filepaths, &self->settings, self->egl, self->video_size) != GSR_ERROR_OK) + _exit(1); + + gsr_color_conversion_params color_conversion_params; + memset(&color_conversion_params, 0, sizeof(color_conversion_params)); + color_conversion_params.color_range = self->settings.color_range; + color_conversion_params.egl = self->egl; + color_conversion_params.load_external_image_shader = gsr_video_sources_uses_external_image(self->video_sources); + gsr_video_encoder_get_textures(self->video_encoder, color_conversion_params.destination_textures, color_conversion_params.destination_textures_size, &color_conversion_params.num_destination_textures, &color_conversion_params.destination_color); + + if(gsr_color_conversion_init(&self->color_conversion, &color_conversion_params) != 0) { + gsr_log(GSR_LOG_LEVEL_ERROR, "main: failed to create color conversion"); + _exit(1); + } + + gsr_color_conversion_clear(&self->color_conversion); + + self->output_color_conversion = self->plugins.num_plugins > 0 ? &self->plugins.color_conversion : &self->color_conversion; + + if(self->settings.video_encoder == GSR_VIDEO_ENCODER_HW_CPU) { + if(!open_video_software(self->video_codec_context, &self->settings)) + _exit(1); + } else { + if(!open_video_hardware(self->video_codec_context, self->low_power, self->egl, &self->settings)) + _exit(1); + } + + if(self->video_stream) { + avcodec_parameters_from_context(self->video_stream->codecpar, self->video_codec_context); + const size_t video_destination_id = gsr_encoder_add_recording_destination(&self->encoder, self->video_codec_context, self->av_format_context, self->video_stream, 0); + if(self->settings.write_first_frame_ts && video_destination_id != (size_t)-1) { + char ts_filepath[PATH_MAX]; + snprintf(ts_filepath, sizeof(ts_filepath), "%s.ts", self->settings.filename); + gsr_encoder_set_recording_destination_first_frame_ts_filepath(&self->encoder, video_destination_id, ts_filepath); + } + } + + if(gsr_audio_capture_init(&self->audio_capture, &self->encoder, self->recording_clock, &self->running) != GSR_ERROR_OK) + _exit(1); + + + int audio_stream_index = GSR_VIDEO_STREAM_INDEX + 1; + for(size_t audio_track_index = 0; audio_track_index < self->audio_input_tracks->num_items; ++audio_track_index) { + const gsr_merged_audio_inputs *merged_audio_inputs = &self->audio_input_tracks->items[audio_track_index]; + const bool use_amix = gsr_audio_inputs_should_use_amix(merged_audio_inputs); + AVCodecContext *audio_codec_context = create_audio_codec_context(self->settings.fps, self->settings.audio_codec, use_amix, self->settings.audio_bitrate); + if(!audio_codec_context) + _exit(1); + + AVStream *audio_stream = NULL; + if(!self->settings.is_replaying) { + audio_stream = create_stream(self->av_format_context, audio_codec_context); + if(gsr_encoder_add_recording_destination(&self->encoder, audio_codec_context, self->av_format_context, audio_stream, 0) == (size_t)-1) + gsr_log(GSR_LOG_LEVEL_ERROR, "added too many audio sources"); + } + + if(audio_stream && merged_audio_inputs->track_name[0] != '\0' && !self->settings.exclude_metadata) + av_dict_set(&audio_stream->metadata, "title", merged_audio_inputs->track_name, 0); + + if(!open_audio(audio_codec_context, self->settings.ffmpeg_audio_opts)) + _exit(1); + if(audio_stream) + avcodec_parameters_from_context(audio_stream->codecpar, audio_codec_context); + + #if LIBAVCODEC_VERSION_MAJOR < 60 + const int num_channels = audio_codec_context->channels; + #else + const int num_channels = audio_codec_context->ch_layout.nb_channels; + #endif + + //audio_frame->sample_rate = audio_codec_context->sample_rate; + + AVFilterContext *src_filter_ctx[GSR_MAX_AUDIO_SOURCES_PER_TRACK]; + AVFilterGraph *graph = NULL; + AVFilterContext *sink = NULL; + if(use_amix) { + if(merged_audio_inputs->num_items > GSR_MAX_AUDIO_SOURCES_PER_TRACK) { + gsr_log(GSR_LOG_LEVEL_ERROR, "too many audio sources for one audio track, the maximum is %d", GSR_MAX_AUDIO_SOURCES_PER_TRACK); + _exit(1); + } + + if(gsr_audio_init_filter_graph(audio_codec_context, &graph, &sink, src_filter_ctx, merged_audio_inputs->num_items) < 0) { + gsr_log(GSR_LOG_LEVEL_ERROR, "failed to create audio filter"); + _exit(1); + } + } + + // TODO: Cleanup above + + const double audio_fps = (double)audio_codec_context->sample_rate / (double)audio_codec_context->frame_size; + const double timeout_sec = 1000.0 / audio_fps / 1000.0; + + const double audio_startup_time_seconds = self->force_no_audio_offset ? 0 : audio_codec_get_desired_delay(self->settings.audio_codec, self->settings.fps);// * ((double)audio_codec_context->frame_size / 1024.0); + const double num_audio_frames_shift = audio_startup_time_seconds / timeout_sec; + + gsr_audio_track audio_track; + memset(&audio_track, 0, sizeof(audio_track)); + snprintf(audio_track.name, sizeof(audio_track.name), "%s", merged_audio_inputs->track_name); + audio_track.codec_context = audio_codec_context; + audio_track.graph = graph; + audio_track.sink = sink; + audio_track.stream_index = audio_stream_index; + audio_track.pts = -audio_codec_context->frame_size * num_audio_frames_shift; + + int audio_track_result = GSR_ERROR_OK; + if(gsr_audio_inputs_has_app_audio(merged_audio_inputs)) { + assert(!use_amix); +#ifdef GSR_APP_AUDIO + audio_track_result = gsr_audio_track_init_application_input(&audio_track, merged_audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, self->pipewire_audio); +#endif + } else { + audio_track_result = gsr_audio_track_init_device_inputs(&audio_track, merged_audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, src_filter_ctx, use_amix); + } + + if(audio_track_result != GSR_ERROR_OK) + _exit(gsr_error_to_exit_code(audio_track_result)); + + if(!gsr_audio_capture_add_track(&self->audio_capture, &audio_track)) + _exit(1); + ++audio_stream_index; + + if(audio_codec_context->frame_size > self->audio_max_frame_size) + self->audio_max_frame_size = audio_codec_context->frame_size; + } + + //av_dump_format(self->av_format_context, 0, filename, 1); + + if(!self->settings.is_replaying && !(self->av_format_context->oformat->flags & AVFMT_NOFILE)) { + const int ret = avio_open(&self->av_format_context->pb, self->settings.filename, AVIO_FLAG_WRITE); + if(ret < 0) { + gsr_log(GSR_LOG_LEVEL_ERROR, "Could not open '%s': %s", self->settings.filename, gsr_av_error_to_string(ret)); + _exit(1); + } + } + + if(!self->settings.is_replaying) + av_write_header(self->av_format_context, self->settings.ffmpeg_opts); + + *error = GSR_ERROR_OK; + return self; +} + +int gsr_recorder_run(gsr_recorder *self) { + double fps_start_time = clock_get_monotonic_seconds(); + //double frame_timer_start = fps_start_time; + int fps_counter = 0; + int damage_fps_counter = 0; + + bool paused = false; + self->replay_recording = false; + + memset(&self->replay_recording_output, 0, sizeof(self->replay_recording_output)); + + gsr_replay_save_init(&self->replay_save); + + + self->force_iframe_frame = false; + + gsr_recording_clock_start(self->recording_clock); + const double record_start_time = gsr_recording_clock_get_start_time(self->recording_clock); + + if(gsr_audio_capture_start(&self->audio_capture, self->audio_max_frame_size, self->uses_amix) != GSR_ERROR_OK) + _exit(1); + + // Set update_fps to 24 to test if duplicate/delayed frames cause video/audio desync or too fast/slow video. + //const double update_fps = fps + 190; + self->should_stop_error = false; + + int64_t video_pts_counter = 0; + int64_t video_prev_pts = 0; + + bool hdr_metadata_set = false; + self->hdr = video_codec_is_hdr(self->settings.video_codec); + + + memset(&self->damage, 0, sizeof(self->damage)); + if(self->settings.framerate_mode == GSR_FRAMERATE_MODE_CONTENT && gsr_capture_sources_has_damage_tracked_target(self->capture_sources)) { + if(gsr_window_get_display_server(self->window) == GSR_DISPLAY_SERVER_X11) { + gsr_damage_init(&self->damage, self->egl, &self->capture_deps->x11_cursor, self->settings.record_cursor); + self->use_damage_tracking = true; + + for(size_t i = 0; i < self->capture_sources->num_items; ++i) { + const gsr_capture_source *capture_source = &self->capture_sources->items[i]; + switch(capture_source->type) { + case GSR_CAPTURE_SOURCE_TYPE_WINDOW: + gsr_damage_start_tracking_window(&self->damage, capture_source->window_id); + break; + case GSR_CAPTURE_SOURCE_TYPE_MONITOR: + case GSR_CAPTURE_SOURCE_TYPE_REGION: + // TODO: When capturing a region only track damage in that region + gsr_damage_start_tracking_monitor(&self->damage, capture_source->name); + break; + default: + break; + } + } + } else if(gsr_capture_sources_has_monitor_or_region(self->capture_sources)) { + gsr_log(GSR_LOG_LEVEL_WARNING, "\"-fm content\" has no effect on Wayland when recording a monitor. Either record a monitor on X11 or capture with desktop portal instead (-w portal)"); + } + } + + while(self->running) { + while(gsr_window_process_event(self->window)) { + if(self->capture_deps->x11_cursor_display && self->settings.record_cursor) + gsr_cursor_on_event(&self->capture_deps->x11_cursor, gsr_window_get_event_data(self->window)); + + gsr_damage_on_event(&self->damage, gsr_window_get_event_data(self->window)); + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + gsr_capture_on_event(video_source->capture, self->egl); + } + } + + if(self->capture_deps->x11_cursor_display && self->settings.record_cursor) + gsr_cursor_tick(&self->capture_deps->x11_cursor, DefaultRootWindow(self->capture_deps->x11_cursor_display)); + + gsr_damage_tick(&self->damage); + + self->should_stop_error = false; + bool damaged = false; + + if(self->use_damage_tracking) + damaged = gsr_damage_is_damaged(&self->damage); + + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + gsr_capture_tick(video_source->capture); + + if(gsr_capture_should_stop(video_source->capture, &self->should_stop_error)) { + self->running = 0; + break; + } + + if(video_source->capture_source->type == GSR_CAPTURE_SOURCE_TYPE_FOCUSED_WINDOW) { + assert(video_source->capture->get_window_id); + const Window damage_target_window = video_source->capture->get_window_id(video_source->capture); + + if((int64_t)damage_target_window != video_source->capture_source->window_id) { + gsr_damage_stop_tracking_window(&self->damage, video_source->capture_source->window_id); + if(damage_target_window != 0) + gsr_damage_start_tracking_window(&self->damage, damage_target_window); + } + + video_source->capture_source->window_id = damage_target_window; + } + + if(video_source->capture->is_damaged) + damaged |= video_source->capture->is_damaged(video_source->capture); + else if(!self->use_damage_tracking) + damaged = true; + } + + damaged |= gsr_plugins_is_damaged(&self->plugins); + + // TODO: Readd wayland sync warning when removing this + if(self->settings.framerate_mode != GSR_FRAMERATE_MODE_CONTENT) + damaged = true; + + if(damaged) + ++damage_fps_counter; + + ++fps_counter; + const double time_now = clock_get_monotonic_seconds(); + //const double frame_timer_elapsed = time_now - frame_timer_start; + const double elapsed = time_now - fps_start_time; + if (elapsed >= 1.0) { + if(self->settings.verbose) { + fprintf(stderr, "update fps: %d, damage fps: %d\n", fps_counter, damage_fps_counter); + } + fps_start_time = time_now; + fps_counter = 0; + damage_fps_counter = 0; + } + + const double this_video_frame_time = gsr_recording_clock_get_time(self->recording_clock); + const int64_t expected_frames = floor((this_video_frame_time - record_start_time) / self->target_fps); + const int64_t num_missed_frames = expected_frames - video_pts_counter; + + if(damaged && num_missed_frames >= 1 && !paused) { + // TODO: Dont do this if no damage? + self->egl->glClear(0); + + gsr_damage_clear(&self->damage); + gsr_plugins_clear_damage(&self->plugins); + gsr_capture_deps_cleanup_kms_fds(self->capture_deps); + + gsr_capture_deps_update_kms(self->capture_deps); + + bool capture_has_synchronous_task = false; + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + if(video_source->capture->clear_damage) + video_source->capture->clear_damage(video_source->capture); + + if(video_source->capture->capture_has_synchronous_task) { + capture_has_synchronous_task = video_source->capture->capture_has_synchronous_task(video_source->capture); + if(capture_has_synchronous_task) { + paused = true; + gsr_recording_clock_set_paused(self->recording_clock, true); + } + } + } + + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + if(video_source->capture->pre_capture) + video_source->capture->pre_capture(video_source->capture, &video_source->metadata, self->output_color_conversion); + } + + if(self->output_color_conversion->schedule_clear) { + self->output_color_conversion->schedule_clear = false; + gsr_color_conversion_clear(self->output_color_conversion); + } + + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + gsr_capture_capture(video_source->capture, &video_source->metadata, self->output_color_conversion); + } + + gsr_capture_deps_cleanup_kms_fds(self->capture_deps); + + if(self->plugins.num_plugins > 0) { + gsr_plugins_draw(&self->plugins); + gsr_color_conversion_draw(&self->color_conversion, self->plugins.texture, + (vec2i){0, 0}, self->video_size, + (vec2i){0, 0}, self->video_size, + self->video_size, GSR_ROT_0, GSR_FLIP_NONE, GSR_SOURCE_COLOR_RGB, false); + } + + if(capture_has_synchronous_task) { + paused = false; + gsr_recording_clock_set_paused(self->recording_clock, false); + } + + gsr_egl_swap_buffers(self->egl); + gsr_video_encoder_copy_textures_to_frame(self->video_encoder, self->video_frame, self->output_color_conversion); + + for(size_t video_source_index = 0; video_source_index < self->video_sources->num_items; ++video_source_index) { + gsr_video_source *video_source = &self->video_sources->items[video_source_index]; + if(self->hdr && !hdr_metadata_set && !self->settings.is_replaying && add_hdr_metadata_to_video_stream(video_source->capture, self->video_stream)) + hdr_metadata_set = true; + } + + // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again + const int num_frames_to_encode = self->settings.framerate_mode == GSR_FRAMERATE_MODE_CONSTANT ? num_missed_frames : 1; + for(int i = 0; i < num_frames_to_encode; ++i) { + if(self->settings.framerate_mode == GSR_FRAMERATE_MODE_CONSTANT) { + self->video_frame->pts = video_pts_counter + i; + } else { + self->video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE; + const bool same_pts = self->video_frame->pts == video_prev_pts; + video_prev_pts = self->video_frame->pts; + if(same_pts) + continue; + } + + if(self->force_iframe_frame) { + self->video_frame->pict_type = AV_PICTURE_TYPE_I; + } + + int ret = avcodec_send_frame(self->video_codec_context, self->video_frame); + if(ret == 0) { + // TODO: Move to separate thread because this could write to network (for example when livestreaming) + gsr_encoder_receive_packets(&self->encoder, self->video_codec_context, self->video_frame->pts, GSR_VIDEO_STREAM_INDEX); + } else { + gsr_log(GSR_LOG_LEVEL_ERROR, "avcodec_send_frame failed, error: %s", gsr_av_error_to_string(ret)); + } + + if(self->force_iframe_frame) { + self->force_iframe_frame = false; + self->video_frame->pict_type = AV_PICTURE_TYPE_NONE; + } + } + + video_pts_counter += num_missed_frames; + } + + if(self->toggle_pause == 1 && !self->settings.is_replaying) { + paused = !paused; + gsr_recording_clock_set_paused(self->recording_clock, paused); + gsr_log(GSR_LOG_LEVEL_INFO, paused ? "Paused" : "Unpaused"); + self->toggle_pause = 0; + } + + if(self->toggle_replay_recording && !self->settings.replay_recording_directory) { + self->toggle_replay_recording = 0; + if(self->callbacks.recording_started) + self->callbacks.recording_started(NULL, self->callbacks.userdata); + } + + if(self->toggle_replay_recording && self->settings.replay_recording_directory) { + self->toggle_replay_recording = 0; + const bool new_replay_recording_state = !self->replay_recording; + if(new_replay_recording_state) { + gsr_audio_capture_lock_filter(&self->audio_capture); + self->num_replay_recording_items = 0; + const bool filepath_created = gsr_create_new_recording_filepath_from_timestamp(self->replay_recording_filepath, sizeof(self->replay_recording_filepath), self->settings.replay_recording_directory, "Video", self->file_extension, self->settings.date_folders); + if(filepath_created && gsr_recording_output_start(&self->replay_recording_output, self->replay_recording_filepath, &self->settings, self->video_codec_context, &self->audio_capture, self->hdr, self->video_sources)) { + const size_t video_recording_destination_id = gsr_encoder_add_recording_destination(&self->encoder, self->video_codec_context, self->replay_recording_output.av_format_context, self->replay_recording_output.video_stream, self->video_frame->pts); + if(self->settings.write_first_frame_ts && video_recording_destination_id != (size_t)-1) { + char ts_filepath[PATH_MAX]; + snprintf(ts_filepath, sizeof(ts_filepath), "%s.ts", self->replay_recording_filepath); + gsr_encoder_set_recording_destination_first_frame_ts_filepath(&self->encoder, video_recording_destination_id, ts_filepath); + } + + if(video_recording_destination_id != (size_t)-1 && self->num_replay_recording_items < GSR_MAX_RECORDING_DESTINATIONS) { + self->replay_recording_items[self->num_replay_recording_items] = video_recording_destination_id; + ++self->num_replay_recording_items; + } + + for(size_t i = 0; i < self->replay_recording_output.num_audio_streams; ++i) { + const gsr_recording_audio_stream *audio_stream = &self->replay_recording_output.audio_streams[i]; + const size_t audio_recording_destination_id = gsr_encoder_add_recording_destination(&self->encoder, audio_stream->audio_track->codec_context, self->replay_recording_output.av_format_context, audio_stream->stream, audio_stream->audio_track->pts); + if(audio_recording_destination_id != (size_t)-1 && self->num_replay_recording_items < GSR_MAX_RECORDING_DESTINATIONS) { + self->replay_recording_items[self->num_replay_recording_items] = audio_recording_destination_id; + ++self->num_replay_recording_items; + } + } + + self->replay_recording = true; + self->force_iframe_frame = true; + gsr_log(GSR_LOG_LEVEL_INFO, "Started recording"); + if(self->callbacks.recording_started) + self->callbacks.recording_started(self->replay_recording_filepath, self->callbacks.userdata); + } else { + if(self->callbacks.recording_started) + self->callbacks.recording_started(NULL, self->callbacks.userdata); + } + gsr_audio_capture_unlock_filter(&self->audio_capture); + } else if(self->replay_recording_output.av_format_context) { + for(size_t i = 0; i < self->num_replay_recording_items; ++i) { + gsr_encoder_remove_recording_destination(&self->encoder, self->replay_recording_items[i]); + } + self->num_replay_recording_items = 0; + + if(gsr_recording_output_stop(&self->replay_recording_output)) { + gsr_log(GSR_LOG_LEVEL_INFO, "Stopped recording"); + if(self->callbacks.recording_stopped) + self->callbacks.recording_stopped(self->replay_recording_filepath, self->callbacks.userdata); + } else { + if(self->callbacks.recording_stopped) + self->callbacks.recording_stopped(NULL, self->callbacks.userdata); + } + + self->replay_recording = false; + self->replay_recording_filepath[0] = '\0'; + } + } + + bool replay_save_result = false; + const char *replay_save_output_filepath = NULL; + if(gsr_replay_save_poll(&self->replay_save, &replay_save_result, &replay_save_output_filepath)) { + if(self->callbacks.replay_saved) + self->callbacks.replay_saved(replay_save_output_filepath[0] == '\0' || !replay_save_result ? NULL : replay_save_output_filepath, self->callbacks.userdata); + } + + if(self->save_replay_seconds != 0 && !gsr_replay_save_is_running(&self->replay_save) && self->settings.is_replaying) { + int current_save_replay_seconds = self->save_replay_seconds; + if(current_save_replay_seconds > 0) + current_save_replay_seconds += self->settings.keyint; + + self->save_replay_seconds = 0; + const bool replay_start_result = gsr_replay_save_start(&self->replay_save, self->video_codec_context, GSR_VIDEO_STREAM_INDEX, &self->audio_capture, &self->encoder, &self->settings, self->file_extension, self->hdr, self->video_sources, current_save_replay_seconds); + if(!replay_start_result && self->callbacks.replay_saved) + self->callbacks.replay_saved(NULL, self->callbacks.userdata); + + if(self->settings.restart_replay_on_save && current_save_replay_seconds == GSR_SAVE_REPLAY_SECONDS_FULL) { + pthread_mutex_lock(&self->encoder.replay_mutex); + gsr_replay_buffer_clear(self->encoder.replay_buffer); + pthread_mutex_unlock(&self->encoder.replay_mutex); + } + } + + const double time_at_frame_end = gsr_recording_clock_get_time(self->recording_clock); + const double time_elapsed_total = time_at_frame_end - record_start_time; + const int64_t frames_elapsed = floor(time_elapsed_total / self->target_fps); + const double time_at_next_frame = (frames_elapsed + 1) * self->target_fps; + double time_to_next_frame = time_at_next_frame - time_elapsed_total; + if(time_to_next_frame > self->target_fps) + time_to_next_frame = self->target_fps; + const int64_t end_num_missed_frames = frames_elapsed - video_pts_counter; + + if(time_to_next_frame > 0.0 && end_num_missed_frames <= 0) + av_usleep(time_to_next_frame * 1000.0 * 1000.0); + else { + if(paused) + av_usleep(20.0 * 1000.0); // 20 milliseconds + else if(self->settings.framerate_mode == GSR_FRAMERATE_MODE_CONTENT) + av_usleep(2.8 * 1000.0); // 2.8 milliseconds + } + } + + gsr_recorder_stop_recording(self); + return self->should_stop_error ? GSR_ERROR_CAPTURE_FAILED : GSR_ERROR_OK; +} + +static void gsr_recorder_stop_recording(gsr_recorder *self) { + self->running = 0; + + bool final_replay_save_result = false; + const char *final_replay_save_output_filepath = NULL; + if(gsr_replay_save_join(&self->replay_save, &final_replay_save_result, &final_replay_save_output_filepath)) { + if(final_replay_save_output_filepath[0] != '\0' && self->callbacks.replay_saved) + self->callbacks.replay_saved(final_replay_save_output_filepath, self->callbacks.userdata); + } + + gsr_plugins_deinit(&self->plugins); + + if(self->replay_recording_output.av_format_context) { + for(size_t i = 0; i < self->num_replay_recording_items; ++i) { + gsr_encoder_remove_recording_destination(&self->encoder, self->replay_recording_items[i]); + } + self->num_replay_recording_items = 0; + + if(gsr_recording_output_stop(&self->replay_recording_output)) { + gsr_log(GSR_LOG_LEVEL_INFO, "Stopped recording"); + if(self->callbacks.recording_stopped) + self->callbacks.recording_stopped(self->replay_recording_filepath, self->callbacks.userdata); + } else { + if(self->callbacks.recording_stopped) + self->callbacks.recording_stopped(NULL, self->callbacks.userdata); + } + } + + gsr_audio_capture_join_threads(&self->audio_capture); + + // TODO: Replace this with start_recording_create_steams + if(!self->settings.is_replaying && av_write_trailer(self->av_format_context) != 0) { + //fprintf(stderr, "Failed to write trailer\n"); + } + + if(!self->settings.is_replaying && !(self->av_format_context->oformat->flags & AVFMT_NOFILE)) { + avio_close(self->av_format_context->pb); + avformat_free_context(self->av_format_context); + } + + gsr_damage_deinit(&self->damage); + gsr_color_conversion_deinit(&self->color_conversion); + gsr_video_encoder_destroy(self->video_encoder, self->video_codec_context); + gsr_encoder_deinit(&self->encoder); + gsr_video_sources_deinit(self->video_sources); +#ifdef GSR_APP_AUDIO + gsr_pipewire_audio_deinit(self->pipewire_audio); +#endif + gsr_capture_deps_deinit(self->capture_deps); + + if(!self->settings.is_replaying && self->callbacks.recording_stopped) + self->callbacks.recording_stopped(self->settings.filename, self->callbacks.userdata); + + if(self->windowing->display) { + // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? + //XCloseDisplay(dpy); + } + + //gsr_egl_unload(self->egl); + //gsr_window_destroy(&window); + + //av_frame_free(&self->video_frame); +} + +void gsr_recorder_destroy(gsr_recorder *self) { + if(!self) + return; + + gsr_recording_clock_destroy(self->recording_clock); + free(self); +} + +void gsr_recorder_stop(gsr_recorder *self) { + self->running = 0; +} + +void gsr_recorder_toggle_pause(gsr_recorder *self) { + self->toggle_pause = 1; +} + +void gsr_recorder_toggle_replay_recording(gsr_recorder *self) { + self->toggle_replay_recording = 1; +} + +void gsr_recorder_save_replay(gsr_recorder *self, int seconds) { + self->save_replay_seconds = seconds; +} |
