aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-09-21 15:43:19 +0200
committerdec05eba <dec05eba@protonmail.com>2024-09-21 15:43:19 +0200
commiteddb07ac408fe80af0ffd88e85880f813c57642a (patch)
tree0039022efb2d145d2c7e2a757135dba954f3c14e /src
parentff6d46871387ebae99904602b51d619f8d6ae957 (diff)
Revert recent time framing and performance changes as it affects slower devices badly
Diffstat (limited to 'src')
-rw-r--r--src/capture/kms.c23
-rw-r--r--src/capture/portal.c3
-rw-r--r--src/capture/xcomposite.c3
-rw-r--r--src/main.cpp101
4 files changed, 51 insertions, 79 deletions
diff --git a/src/capture/kms.c b/src/capture/kms.c
index fcf2058..7a61962 100644
--- a/src/capture/kms.c
+++ b/src/capture/kms.c
@@ -570,15 +570,6 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c
if(!capture_is_combined_plane)
capture_pos = (vec2i){drm_fd->x, drm_fd->y};
- // TODO: Hack!! cursor flickers without this when using vaapi copy on wayland.
- // There is probably some sync issue between opengl and vaapi. It flickers more when capture fps is lower.
- // Remove this when that has been figured out. Same for the below glFlush && glFinish
- const int hack_iterations = max_int(1, roundf(1000.0 / (double)self->params.fps));
- for(int i = 0; i < hack_iterations; ++i) {
- self->params.egl->glFlush();
- self->params.egl->glFinish();
- }
-
/* Fast opengl free path */
if(self->monitor_rotation == GSR_MONITOR_ROT_0 && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) {
int fds[4];
@@ -599,20 +590,12 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c
self->params.egl->eglDestroyImage(self->params.egl->egl_display, image);
}
- self->params.egl->glFlush();
- self->params.egl->glFinish();
-
gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id,
target_pos, self->capture_size,
capture_pos, self->capture_size,
texture_rotation, self->external_texture_fallback);
}
- for(int i = 0; i < hack_iterations; ++i) {
- self->params.egl->glFlush();
- self->params.egl->glFinish();
- }
-
if(self->params.record_cursor) {
gsr_kms_response_item *cursor_drm_fd = find_cursor_drm_if_on_monitor(self, drm_fd->connector_id, capture_is_combined_plane);
// The cursor is handled by x11 on x11 instead of using the cursor drm plane because on prime systems with a dedicated nvidia gpu
@@ -625,10 +608,8 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c
}
}
- for(int i = 0; i < hack_iterations; ++i) {
- self->params.egl->glFlush();
- self->params.egl->glFinish();
- }
+ self->params.egl->glFlush();
+ self->params.egl->glFinish();
gsr_capture_kms_cleanup_kms_fds(self);
diff --git a/src/capture/portal.c b/src/capture/portal.c
index 40e4581..58d16d9 100644
--- a/src/capture/portal.c
+++ b/src/capture/portal.c
@@ -333,9 +333,6 @@ static int gsr_capture_portal_capture(gsr_capture *cap, AVFrame *frame, gsr_colo
} else {
return 0;
}
-
- self->params.egl->glFlush();
- self->params.egl->glFinish();
const vec2i target_pos = { max_int(0, frame->width / 2 - self->capture_size.x / 2), max_int(0, frame->height / 2 - self->capture_size.y / 2) };
diff --git a/src/capture/xcomposite.c b/src/capture/xcomposite.c
index 9d053bb..31d0b7e 100644
--- a/src/capture/xcomposite.c
+++ b/src/capture/xcomposite.c
@@ -258,9 +258,6 @@ static int gsr_capture_xcomposite_capture(gsr_capture *cap, AVFrame *frame, gsr_
const vec2i target_pos = { max_int(0, frame->width / 2 - self->texture_size.x / 2), max_int(0, frame->height / 2 - self->texture_size.y / 2) };
- self->params.egl->glFlush();
- self->params.egl->glFinish();
-
/* Fast opengl free path */
if(video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) {
vaapi_copy_egl_image_to_video_surface(self->params.egl, self->window_texture.image, (vec2i){0, 0}, self->texture_size, target_pos, self->texture_size, self->video_codec_context, frame);
diff --git a/src/main.cpp b/src/main.cpp
index ba5550c..ef53935 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -944,7 +944,7 @@ static void open_video_hardware(AVCodecContext *codec_context, VideoQuality vide
// TODO: More quality options
//av_dict_set_int(&options, "low_power", 1, 0);
// Improves performance but increases vram
- av_dict_set_int(&options, "async_depth", 8, 0);
+ //av_dict_set_int(&options, "async_depth", 8, 0);
if(codec_context->codec_id == AV_CODEC_ID_H264) {
// Removed because it causes stutter in games for some people
@@ -3158,6 +3158,7 @@ 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;
@@ -3368,6 +3369,8 @@ 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;
@@ -3389,8 +3392,6 @@ int main(int argc, char **argv) {
if(is_monitor_capture)
gsr_damage_set_target_monitor(&damage, window_str.c_str());
- double last_capture_seconds = clock_get_monotonic_seconds();
-
while(running) {
const double frame_start = clock_get_monotonic_seconds();
@@ -3425,13 +3426,15 @@ int main(int argc, char **argv) {
damaged = true;
// TODO: Readd wayland sync warning when removing this
- damaged = true;
+ if(framerate_mode != FramerateMode::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(verbose) {
@@ -3442,52 +3445,52 @@ int main(int argc, char **argv) {
damage_fps_counter = 0;
}
- 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;
-
+ double frame_time_overflow = frame_timer_elapsed - target_fps;
+ if (frame_time_overflow >= 0.0 && damaged) {
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;
- // 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 double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset;
const int64_t expected_frames = std::round((this_video_frame_time - record_start_time) / target_fps);
- const int num_missed_frames = std::max((int64_t)1LL, expected_frames - video_pts_counter);
+ 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;
+ }
- // 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;
+ 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));
+ }
}
- 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;
}
-
- video_pts_counter += num_frames_to_encode;
}
if(toggle_pause == 1) {
@@ -3519,17 +3522,11 @@ 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);
}
- 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 double time_at_next_frame = (video_pts_counter + 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)
- usleep(2.8 * 1000.0); // 2.8 milliseconds
+ 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);
}
running = 0;