From e1333f6f3b29fd68239f3991271e63bff316abc0 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 2 Mar 2023 22:33:31 +0100 Subject: [PATCH] Optionally do not delay the first frame A delay buffer delayed all the frames except the first one, to open the scrcpy window immediately and get a picture. Make this feature optional, so that the delay buffer might also be used for audio (especially for simulating a high delay for debugging). PR #3757 --- app/src/delay_buffer.c | 8 ++++---- app/src/delay_buffer.h | 6 +++++- app/src/scrcpy.c | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/delay_buffer.c b/app/src/delay_buffer.c index 360e2b66..9d4690a2 100644 --- a/app/src/delay_buffer.c +++ b/app/src/delay_buffer.c @@ -194,10 +194,8 @@ sc_delay_buffer_frame_sink_push(struct sc_frame_sink *sink, sc_clock_update(&db->clock, sc_tick_now(), pts); sc_cond_signal(&db->wait_cond); - if (db->clock.count == 1) { + if (db->first_frame_asap && db->clock.count == 1) { sc_mutex_unlock(&db->mutex); - // First frame, push it immediately, not to delay the opening of the - // scrcpy window return sc_frame_source_sinks_push(&db->frame_source, frame); } @@ -227,10 +225,12 @@ sc_delay_buffer_frame_sink_push(struct sc_frame_sink *sink, } void -sc_delay_buffer_init(struct sc_delay_buffer *db, sc_tick delay) { +sc_delay_buffer_init(struct sc_delay_buffer *db, sc_tick delay, + bool first_frame_asap) { assert(delay > 0); db->delay = delay; + db->first_frame_asap = first_frame_asap; sc_frame_source_init(&db->frame_source); diff --git a/app/src/delay_buffer.h b/app/src/delay_buffer.h index 96fbaa3d..53592372 100644 --- a/app/src/delay_buffer.h +++ b/app/src/delay_buffer.h @@ -29,6 +29,7 @@ struct sc_delay_buffer { struct sc_frame_sink frame_sink; // frame sink trait sc_tick delay; + bool first_frame_asap; sc_thread thread; sc_mutex mutex; @@ -49,8 +50,11 @@ struct sc_delay_buffer_callbacks { * Initialize a delay buffer. * * \param delay a (strictly) positive delay + * \param first_frame_asap if true, do not delay the first frame (useful for + a video stream). */ void -sc_delay_buffer_init(struct sc_delay_buffer *db, sc_tick delay); +sc_delay_buffer_init(struct sc_delay_buffer *db, sc_tick delay, + bool first_frame_asap); #endif diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 2688cab6..dba1bad9 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -669,7 +669,8 @@ aoa_hid_end: struct sc_frame_source *src = &s->video_decoder.frame_source; if (options->display_buffer) { - sc_delay_buffer_init(&s->display_buffer, options->display_buffer); + sc_delay_buffer_init(&s->display_buffer, options->display_buffer, + true); sc_frame_source_add_sink(src, &s->display_buffer.frame_sink); src = &s->display_buffer.frame_source; } @@ -686,7 +687,7 @@ aoa_hid_end: struct sc_frame_source *src = &s->video_decoder.frame_source; if (options->v4l2_buffer) { - sc_delay_buffer_init(&s->v4l2_buffer, options->v4l2_buffer); + sc_delay_buffer_init(&s->v4l2_buffer, options->v4l2_buffer, true); sc_frame_source_add_sink(src, &s->v4l2_buffer.frame_sink); src = &s->v4l2_buffer.frame_source; }