From 048efbd851fa454bc21a53035ceeecaa75b602c1 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 21 Sep 2024 16:12:39 +0200 Subject: frame test --- src/main.cpp | 97 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 46 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index ef53935..dd5c916 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3158,7 +3158,6 @@ int main(int argc, char **argv) { } double fps_start_time = clock_get_monotonic_seconds(); - double frame_timer_start = fps_start_time - target_fps; // We want to capture the first frame immediately int fps_counter = 0; int damage_fps_counter = 0; @@ -3369,8 +3368,6 @@ int main(int argc, char **argv) { }); } - // Set update_fps to 24 to test if duplicate/delayed frames cause video/audio desync or too fast/slow video. - const double update_fps = fps + 190; bool should_stop_error = false; int64_t video_pts_counter = 0; @@ -3392,6 +3389,8 @@ int main(int argc, char **argv) { if(is_monitor_capture) gsr_damage_set_target_monitor(&damage, window_str.c_str()); + double last_capture_seconds = record_start_time; + while(running) { const double frame_start = clock_get_monotonic_seconds(); @@ -3434,7 +3433,6 @@ int main(int argc, char **argv) { ++fps_counter; const double time_now = clock_get_monotonic_seconds(); - const double frame_timer_elapsed = time_now - frame_timer_start; const double elapsed = time_now - fps_start_time; if (elapsed >= 1.0) { if(verbose) { @@ -3445,52 +3443,52 @@ int main(int argc, char **argv) { damage_fps_counter = 0; } - double frame_time_overflow = frame_timer_elapsed - target_fps; - if (frame_time_overflow >= 0.0 && damaged) { + const double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset; + const double time_since_last_frame_captured_seconds = this_video_frame_time - last_capture_seconds; + const bool force_frame_capture = time_since_last_frame_captured_seconds >= damage_timeout_seconds; + if((damaged || force_frame_capture) && !paused) { + last_capture_seconds = this_video_frame_time; + gsr_damage_clear(&damage); if(capture->clear_damage) capture->clear_damage(capture); - frame_time_overflow = std::min(frame_time_overflow, target_fps); - frame_timer_start = time_now - frame_time_overflow; - const double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset; + // TODO: Dont do this if no damage? + egl.glClear(0); + gsr_capture_capture(capture, video_frame, &color_conversion); + gsr_egl_swap_buffers(&egl); + gsr_video_encoder_copy_textures_to_frame(video_encoder, video_frame); + + if(hdr && !hdr_metadata_set && replay_buffer_size_secs == -1 && add_hdr_metadata_to_video_stream(capture, video_stream)) + hdr_metadata_set = true; + const int64_t expected_frames = std::round((this_video_frame_time - record_start_time) / target_fps); - const int num_frames = framerate_mode == FramerateMode::CONSTANT ? std::max((int64_t)0LL, expected_frames - video_pts_counter) : 1; - - if(num_frames > 0 && !paused) { - egl.glClear(0); - gsr_capture_capture(capture, video_frame, &color_conversion); - gsr_egl_swap_buffers(&egl); - - gsr_video_encoder_copy_textures_to_frame(video_encoder, video_frame); - - if(hdr && !hdr_metadata_set && replay_buffer_size_secs == -1 && add_hdr_metadata_to_video_stream(capture, video_stream)) - hdr_metadata_set = true; - - // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again - for(int i = 0; i < num_frames; ++i) { - if(framerate_mode == FramerateMode::CONSTANT) { - video_frame->pts = video_pts_counter + i; - } else { - video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE; - const bool same_pts = video_frame->pts == video_prev_pts; - video_prev_pts = video_frame->pts; - if(same_pts) - continue; - } + const int num_missed_frames = std::max((int64_t)1LL, expected_frames - video_pts_counter); - int ret = avcodec_send_frame(video_codec_context, video_frame); - if(ret == 0) { - // TODO: Move to separate thread because this could write to network (for example when livestreaming) - receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context, - record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset); - } else { - fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret)); - } + // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again + const int num_frames_to_encode = framerate_mode == FramerateMode::CONSTANT ? num_missed_frames : 1; + for(int i = 0; i < num_frames_to_encode; ++i) { + if(framerate_mode == FramerateMode::CONSTANT) { + video_frame->pts = video_pts_counter + i; + } else { + video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE; + const bool same_pts = video_frame->pts == video_prev_pts; + video_prev_pts = video_frame->pts; + if(same_pts) + continue; } - video_pts_counter += num_frames; + int ret = avcodec_send_frame(video_codec_context, video_frame); + if(ret == 0) { + // TODO: Move to separate thread because this could write to network (for example when livestreaming) + receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context, + record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset); + } else { + fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret)); + } } + + video_pts_counter += num_frames_to_encode; } if(toggle_pause == 1) { @@ -3522,11 +3520,18 @@ int main(int argc, char **argv) { 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); } - double frame_end = clock_get_monotonic_seconds(); - double frame_sleep_fps = 1.0 / update_fps; - double sleep_time = frame_sleep_fps - (frame_end - frame_start); - if(sleep_time > 0.0) - av_usleep(sleep_time * 1000.0 * 1000.0); + const double time_at_frame_end = clock_get_monotonic_seconds() - paused_time_offset; + const double time_elapsed_total = time_at_frame_end - record_start_time; + const int64_t frames_elapsed = std::round(time_elapsed_total / target_fps); + const double time_at_next_frame = (frames_elapsed + 1) * target_fps; + const double time_to_next_frame = time_at_next_frame - time_elapsed_total; + + const double frame_end = clock_get_monotonic_seconds(); + const double frame_time = frame_end - frame_start; + if(time_to_next_frame > 0.0) + av_usleep(time_to_next_frame * 1000.0 * 1000.0); + else if(frame_time < target_fps && !damaged) + usleep(2.8 * 1000.0); // 2.8 milliseconds } running = 0; -- cgit v1.2.3