Display and control your Android device
Rename sc_delay_buffer to sc_video_regulator
Romain Vimont31e8c0ad89f5
diff --git a/app/meson.build b/app/meson.buildindex fd58098e..8816426f 100644--- a/app/meson.build+++ b/app/meson.build@@ -12,7 +12,6 @@ src = ['src/control_msg.c','src/controller.c','src/decoder.c',- 'src/delay_buffer.c','src/demuxer.c','src/device_msg.c','src/disconnect.c',@@ -36,6 +35,7 @@ src = ['src/server.c','src/texture.c','src/version.c',+ 'src/video_regulator.c','src/hid/hid_gamepad.c','src/hid/hid_keyboard.c','src/hid/hid_mouse.c',diff --git a/app/src/clock.h b/app/src/clock.hindex 0d34ab99..f73744b1 100644--- a/app/src/clock.h+++ b/app/src/clock.h@@ -20,7 +20,7 @@ struct sc_clock_point {* computer clock. It is expected to be very close to 1.** Since the clock is used to estimate very close points in the future (which- * are reestimated on every clock update, see delay_buffer), the error caused+ * are reestimated on every clock update, see video_regulator), the error caused* by clock drift is totally negligible, so it is better to assume that the* slope is 1 than to estimate it (the estimation error would be larger).*diff --git a/app/src/delay_buffer.c b/app/src/delay_buffer.cdeleted file mode 100644index aa73585e..00000000--- a/app/src/delay_buffer.c+++ /dev/null@@ -1,298 +0,0 @@-#include "delay_buffer.h"--#include <assert.h>-#include <stdlib.h>-#include <libavcodec/avcodec.h>--#include "util/log.h"--/** Downcast frame_sink to sc_delay_buffer */-#define DOWNCAST(SINK) container_of(SINK, struct sc_delay_buffer, frame_sink)--static bool-sc_delayed_packet_init_frame(struct sc_delayed_packet *dpacket,- const AVFrame *frame) {- dpacket->type = SC_DELAYED_PACKET_TYPE_FRAME;- dpacket->frame = av_frame_alloc();- if (!dpacket->frame) {- LOG_OOM();- return false;- }-- if (av_frame_ref(dpacket->frame, frame)) {- LOG_OOM();- av_frame_free(&dpacket->frame);- return false;- }-- return true;-}--static void-sc_delayed_packet_init_session(struct sc_delayed_packet *dpacket,- const struct sc_stream_session *session) {- dpacket->type = SC_DELAYED_PACKET_TYPE_SESSION;- dpacket->session = *session;-}--static void-sc_delayed_packet_destroy(struct sc_delayed_packet *dpacket) {- if (dpacket->type == SC_DELAYED_PACKET_TYPE_FRAME) {- av_frame_unref(dpacket->frame);- av_frame_free(&dpacket->frame);- }-}--static int-run_buffering(void *data) {- struct sc_delay_buffer *db = data;-- assert(db->delay > 0);-- for (;;) {- sc_mutex_lock(&db->mutex);-- while (!db->stopped && sc_vecdeque_is_empty(&db->queue)) {- sc_cond_wait(&db->queue_cond, &db->mutex);- }-- if (db->stopped) {- sc_mutex_unlock(&db->mutex);- goto stopped;- }-- struct sc_delayed_packet dpacket = sc_vecdeque_pop(&db->queue);-- bool ok;- if (dpacket.type == SC_DELAYED_PACKET_TYPE_FRAME) {- sc_tick max_deadline = sc_tick_now() + db->delay;- // PTS (written by the server) are expressed in microseconds- sc_tick pts = SC_TICK_FROM_US(dpacket.frame->pts);-- bool timed_out = false;- while (!db->stopped && !timed_out) {- sc_tick deadline = sc_clock_to_system_time(&db->clock, pts)- + db->delay;- if (deadline > max_deadline) {- deadline = max_deadline;- }-- timed_out =- !sc_cond_timedwait(&db->wait_cond, &db->mutex, deadline);- }-- bool stopped = db->stopped;- sc_mutex_unlock(&db->mutex);-- if (stopped) {- sc_delayed_packet_destroy(&dpacket);- goto stopped;- }--#ifdef SC_BUFFERING_DEBUG- LOGD("Buffering: %" PRItick ";%" PRItick ";%" PRItick,- pts, dframe.push_date, sc_tick_now());-#endif-- ok = sc_frame_source_sinks_push(&db->frame_source, dpacket.frame);- } else {- assert(dpacket.type == SC_DELAYED_PACKET_TYPE_SESSION);- sc_mutex_unlock(&db->mutex);- ok = sc_frame_source_sinks_push_session(&db->frame_source,- &dpacket.session);- }-- sc_delayed_packet_destroy(&dpacket);- if (!ok) {- LOGE("Delayed packet could not be pushed, stopping");- sc_mutex_lock(&db->mutex);- // Prevent to push any new packet- db->stopped = true;- sc_mutex_unlock(&db->mutex);- goto stopped;- }- }--stopped:- assert(db->stopped);-- // Flush queue- while (!sc_vecdeque_is_empty(&db->queue)) {- struct sc_delayed_packet *dpacket = sc_vecdeque_popref(&db->queue);- sc_delayed_packet_destroy(dpacket);- }-- LOGD("Buffering thread ended");-- return 0;-}--static bool-sc_delay_buffer_frame_sink_open(struct sc_frame_sink *sink,- const AVCodecContext *ctx,- const struct sc_stream_session *session) {- struct sc_delay_buffer *db = DOWNCAST(sink);- (void) ctx;- (void) session;-- bool ok = sc_mutex_init(&db->mutex);- if (!ok) {- return false;- }-- ok = sc_cond_init(&db->queue_cond);- if (!ok) {- goto error_destroy_mutex;- }-- ok = sc_cond_init(&db->wait_cond);- if (!ok) {- goto error_destroy_queue_cond;- }-- sc_clock_init(&db->clock);- sc_vecdeque_init(&db->queue);- db->stopped = false;-- if (!sc_frame_source_sinks_open(&db->frame_source, ctx, session)) {- goto error_destroy_wait_cond;- }-- ok = sc_thread_create(&db->thread, run_buffering, "scrcpy-dbuf", db);- if (!ok) {- LOGE("Could not start buffering thread");- goto error_close_sinks;- }-- return true;--error_close_sinks:- sc_frame_source_sinks_close(&db->frame_source);-error_destroy_wait_cond:- sc_cond_destroy(&db->wait_cond);-error_destroy_queue_cond:- sc_cond_destroy(&db->queue_cond);-error_destroy_mutex:- sc_mutex_destroy(&db->mutex);-- return false;-}--static void-sc_delay_buffer_frame_sink_close(struct sc_frame_sink *sink) {- struct sc_delay_buffer *db = DOWNCAST(sink);-- sc_mutex_lock(&db->mutex);- db->stopped = true;- sc_cond_signal(&db->queue_cond);- sc_cond_signal(&db->wait_cond);- sc_mutex_unlock(&db->mutex);-- sc_thread_join(&db->thread, NULL);-- sc_frame_source_sinks_close(&db->frame_source);-- sc_cond_destroy(&db->wait_cond);- sc_cond_destroy(&db->queue_cond);- sc_mutex_destroy(&db->mutex);-}--static bool-sc_delay_buffer_frame_sink_push(struct sc_frame_sink *sink,- const AVFrame *frame) {- struct sc_delay_buffer *db = DOWNCAST(sink);-- sc_mutex_lock(&db->mutex);-- if (db->stopped) {- sc_mutex_unlock(&db->mutex);- return false;- }-- sc_tick pts = SC_TICK_FROM_US(frame->pts);- sc_clock_update(&db->clock, sc_tick_now(), pts);- sc_cond_signal(&db->wait_cond);-- if (db->first_frame_asap && db->clock.range == 1) {- sc_mutex_unlock(&db->mutex);- return sc_frame_source_sinks_push(&db->frame_source, frame);- }-- struct sc_delayed_packet *dpacket =- sc_vecdeque_push_uninitialized(&db->queue);- if (!dpacket) {- sc_mutex_unlock(&db->mutex);- LOG_OOM();- return false;- }-- bool ok = sc_delayed_packet_init_frame(dpacket, frame);- if (!ok) {- sc_mutex_unlock(&db->mutex);- LOG_OOM();- return false;- }--#ifdef SC_BUFFERING_DEBUG- dpacket->push_date = sc_tick_now();-#endif-- sc_cond_signal(&db->queue_cond);-- sc_mutex_unlock(&db->mutex);-- return true;-}--static bool-sc_delay_buffer_frame_sink_push_session(struct sc_frame_sink *sink,- const struct sc_stream_session *session) {- struct sc_delay_buffer *db = DOWNCAST(sink);-- sc_mutex_lock(&db->mutex);-- if (db->stopped) {- sc_mutex_unlock(&db->mutex);- return false;- }-- struct sc_delayed_packet *dpacket =- sc_vecdeque_push_uninitialized(&db->queue);- if (!dpacket) {- sc_mutex_unlock(&db->mutex);- LOG_OOM();- return false;- }-- sc_delayed_packet_init_session(dpacket, session);--#ifdef SC_BUFFERING_DEBUG- dpacket->push_date = sc_tick_now();-#endif-- sc_cond_signal(&db->queue_cond);-- sc_mutex_unlock(&db->mutex);-- return true;-}--void-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);-- static const struct sc_frame_sink_ops ops = {- .open = sc_delay_buffer_frame_sink_open,- .close = sc_delay_buffer_frame_sink_close,- .push = sc_delay_buffer_frame_sink_push,- .push_session = sc_delay_buffer_frame_sink_push_session,- };-- db->frame_sink.ops = &ops;-}diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 304c5303..f5b644a8 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -17,7 +17,6 @@#include "audio_player.h"#include "controller.h"#include "decoder.h"-#include "delay_buffer.h"#include "demuxer.h"#include "events.h"#include "file_pusher.h"@@ -47,6 +46,7 @@#ifdef HAVE_V4L2# include "v4l2_sink.h"#endif+#include "video_regulator.h"struct scrcpy {struct sc_server server;@@ -57,10 +57,10 @@ struct scrcpy {struct sc_decoder video_decoder;struct sc_decoder audio_decoder;struct sc_recorder recorder;- struct sc_delay_buffer video_buffer;+ struct sc_video_regulator video_regulator;#ifdef HAVE_V4L2struct sc_v4l2_sink v4l2_sink;- struct sc_delay_buffer v4l2_buffer;+ struct sc_video_regulator v4l2_regulator;#endifstruct sc_controller controller;struct sc_file_pusher file_pusher;@@ -792,10 +792,10 @@ aoa_complete:if (options->video_playback) {struct sc_frame_source *src = &s->video_decoder.frame_source;if (options->video_buffer) {- sc_delay_buffer_init(&s->video_buffer,- options->video_buffer, true);- sc_frame_source_add_sink(src, &s->video_buffer.frame_sink);- src = &s->video_buffer.frame_source;+ sc_video_regulator_init(&s->video_regulator,+ options->video_buffer, true);+ sc_frame_source_add_sink(src, &s->video_regulator.frame_sink);+ src = &s->video_regulator.frame_source;}sc_frame_source_add_sink(src, &s->screen.frame_sink);@@ -817,9 +817,10 @@ aoa_complete:struct sc_frame_source *src = &s->video_decoder.frame_source;if (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;+ sc_video_regulator_init(&s->v4l2_regulator, options->v4l2_buffer,+ true);+ sc_frame_source_add_sink(src, &s->v4l2_regulator.frame_sink);+ src = &s->v4l2_regulator.frame_source;}sc_frame_source_add_sink(src, &s->v4l2_sink.frame_sink);diff --git a/app/src/video_regulator.c b/app/src/video_regulator.cnew file mode 100644index 00000000..69238d20--- /dev/null+++ b/app/src/video_regulator.c@@ -0,0 +1,298 @@+#include "video_regulator.h"++#include <assert.h>+#include <stdlib.h>+#include <libavcodec/avcodec.h>++#include "util/log.h"++/** Downcast frame_sink to sc_video_regulator */+#define DOWNCAST(SINK) container_of(SINK, struct sc_video_regulator, frame_sink)++static bool+sc_delayed_packet_init_frame(struct sc_delayed_packet *dpacket,+ const AVFrame *frame) {+ dpacket->type = SC_DELAYED_PACKET_TYPE_FRAME;+ dpacket->frame = av_frame_alloc();+ if (!dpacket->frame) {+ LOG_OOM();+ return false;+ }++ if (av_frame_ref(dpacket->frame, frame)) {+ LOG_OOM();+ av_frame_free(&dpacket->frame);+ return false;+ }++ return true;+}++static void+sc_delayed_packet_init_session(struct sc_delayed_packet *dpacket,+ const struct sc_stream_session *session) {+ dpacket->type = SC_DELAYED_PACKET_TYPE_SESSION;+ dpacket->session = *session;+}++static void+sc_delayed_packet_destroy(struct sc_delayed_packet *dpacket) {+ if (dpacket->type == SC_DELAYED_PACKET_TYPE_FRAME) {+ av_frame_unref(dpacket->frame);+ av_frame_free(&dpacket->frame);+ }+}++static int+run_buffering(void *data) {+ struct sc_video_regulator *vr = data;++ assert(vr->delay > 0);++ for (;;) {+ sc_mutex_lock(&vr->mutex);++ while (!vr->stopped && sc_vecdeque_is_empty(&vr->queue)) {+ sc_cond_wait(&vr->queue_cond, &vr->mutex);+ }++ if (vr->stopped) {+ sc_mutex_unlock(&vr->mutex);+ goto stopped;+ }++ struct sc_delayed_packet dpacket = sc_vecdeque_pop(&vr->queue);++ bool ok;+ if (dpacket.type == SC_DELAYED_PACKET_TYPE_FRAME) {+ sc_tick max_deadline = sc_tick_now() + vr->delay;+ // PTS (written by the server) are expressed in microseconds+ sc_tick pts = SC_TICK_FROM_US(dpacket.frame->pts);++ bool timed_out = false;+ while (!vr->stopped && !timed_out) {+ sc_tick deadline = sc_clock_to_system_time(&vr->clock, pts)+ + vr->delay;+ if (deadline > max_deadline) {+ deadline = max_deadline;+ }++ timed_out =+ !sc_cond_timedwait(&vr->wait_cond, &vr->mutex, deadline);+ }++ bool stopped = vr->stopped;+ sc_mutex_unlock(&vr->mutex);++ if (stopped) {+ sc_delayed_packet_destroy(&dpacket);+ goto stopped;+ }++#ifdef SC_BUFFERING_DEBUG+ LOGD("Buffering: %" PRItick ";%" PRItick ";%" PRItick,+ pts, dframe.push_date, sc_tick_now());+#endif++ ok = sc_frame_source_sinks_push(&vr->frame_source, dpacket.frame);+ } else {+ assert(dpacket.type == SC_DELAYED_PACKET_TYPE_SESSION);+ sc_mutex_unlock(&vr->mutex);+ ok = sc_frame_source_sinks_push_session(&vr->frame_source,+ &dpacket.session);+ }++ sc_delayed_packet_destroy(&dpacket);+ if (!ok) {+ LOGE("Delayed packet could not be pushed, stopping");+ sc_mutex_lock(&vr->mutex);+ // Prevent to push any new packet+ vr->stopped = true;+ sc_mutex_unlock(&vr->mutex);+ goto stopped;+ }+ }++stopped:+ assert(vr->stopped);++ // Flush queue+ while (!sc_vecdeque_is_empty(&vr->queue)) {+ struct sc_delayed_packet *dpacket = sc_vecdeque_popref(&vr->queue);+ sc_delayed_packet_destroy(dpacket);+ }++ LOGD("Buffering thread ended");++ return 0;+}++static bool+sc_video_regulator_frame_sink_open(struct sc_frame_sink *sink,+ const AVCodecContext *ctx,+ const struct sc_stream_session *session) {+ struct sc_video_regulator *vr = DOWNCAST(sink);+ (void) ctx;+ (void) session;++ bool ok = sc_mutex_init(&vr->mutex);+ if (!ok) {+ return false;+ }++ ok = sc_cond_init(&vr->queue_cond);+ if (!ok) {+ goto error_destroy_mutex;+ }++ ok = sc_cond_init(&vr->wait_cond);+ if (!ok) {+ goto error_destroy_queue_cond;+ }++ sc_clock_init(&vr->clock);+ sc_vecdeque_init(&vr->queue);+ vr->stopped = false;++ if (!sc_frame_source_sinks_open(&vr->frame_source, ctx, session)) {+ goto error_destroy_wait_cond;+ }++ ok = sc_thread_create(&vr->thread, run_buffering, "scrcpy-vruf", vr);+ if (!ok) {+ LOGE("Could not start buffering thread");+ goto error_close_sinks;+ }++ return true;++error_close_sinks:+ sc_frame_source_sinks_close(&vr->frame_source);+error_destroy_wait_cond:+ sc_cond_destroy(&vr->wait_cond);+error_destroy_queue_cond:+ sc_cond_destroy(&vr->queue_cond);+error_destroy_mutex:+ sc_mutex_destroy(&vr->mutex);++ return false;+}++static void+sc_video_regulator_frame_sink_close(struct sc_frame_sink *sink) {+ struct sc_video_regulator *vr = DOWNCAST(sink);++ sc_mutex_lock(&vr->mutex);+ vr->stopped = true;+ sc_cond_signal(&vr->queue_cond);+ sc_cond_signal(&vr->wait_cond);+ sc_mutex_unlock(&vr->mutex);++ sc_thread_join(&vr->thread, NULL);++ sc_frame_source_sinks_close(&vr->frame_source);++ sc_cond_destroy(&vr->wait_cond);+ sc_cond_destroy(&vr->queue_cond);+ sc_mutex_destroy(&vr->mutex);+}++static bool+sc_video_regulator_frame_sink_push(struct sc_frame_sink *sink,+ const AVFrame *frame) {+ struct sc_video_regulator *vr = DOWNCAST(sink);++ sc_mutex_lock(&vr->mutex);++ if (vr->stopped) {+ sc_mutex_unlock(&vr->mutex);+ return false;+ }++ sc_tick pts = SC_TICK_FROM_US(frame->pts);+ sc_clock_update(&vr->clock, sc_tick_now(), pts);+ sc_cond_signal(&vr->wait_cond);++ if (vr->first_frame_asap && vr->clock.range == 1) {+ sc_mutex_unlock(&vr->mutex);+ return sc_frame_source_sinks_push(&vr->frame_source, frame);+ }++ struct sc_delayed_packet *dpacket =+ sc_vecdeque_push_uninitialized(&vr->queue);+ if (!dpacket) {+ sc_mutex_unlock(&vr->mutex);+ LOG_OOM();+ return false;+ }++ bool ok = sc_delayed_packet_init_frame(dpacket, frame);+ if (!ok) {+ sc_mutex_unlock(&vr->mutex);+ LOG_OOM();+ return false;+ }++#ifdef SC_BUFFERING_DEBUG+ dpacket->push_date = sc_tick_now();+#endif++ sc_cond_signal(&vr->queue_cond);++ sc_mutex_unlock(&vr->mutex);++ return true;+}++static bool+sc_video_regulator_frame_sink_push_session(struct sc_frame_sink *sink,+ const struct sc_stream_session *session) {+ struct sc_video_regulator *vr = DOWNCAST(sink);++ sc_mutex_lock(&vr->mutex);++ if (vr->stopped) {+ sc_mutex_unlock(&vr->mutex);+ return false;+ }++ struct sc_delayed_packet *dpacket =+ sc_vecdeque_push_uninitialized(&vr->queue);+ if (!dpacket) {+ sc_mutex_unlock(&vr->mutex);+ LOG_OOM();+ return false;+ }++ sc_delayed_packet_init_session(dpacket, session);++#ifdef SC_BUFFERING_DEBUG+ dpacket->push_date = sc_tick_now();+#endif++ sc_cond_signal(&vr->queue_cond);++ sc_mutex_unlock(&vr->mutex);++ return true;+}++void+sc_video_regulator_init(struct sc_video_regulator *vr, sc_tick delay,+ bool first_frame_asap) {+ assert(delay > 0);++ vr->delay = delay;+ vr->first_frame_asap = first_frame_asap;++ sc_frame_source_init(&vr->frame_source);++ static const struct sc_frame_sink_ops ops = {+ .open = sc_video_regulator_frame_sink_open,+ .close = sc_video_regulator_frame_sink_close,+ .push = sc_video_regulator_frame_sink_push,+ .push_session = sc_video_regulator_frame_sink_push_session,+ };++ vr->frame_sink.ops = &ops;+}diff --git a/app/src/delay_buffer.h b/app/src/video_regulator.hsimilarity index 78%rename from app/src/delay_buffer.hrename to app/src/video_regulator.hindex 420684be..77c70c0e 100644--- a/app/src/delay_buffer.h+++ b/app/src/video_regulator.h@@ -1,5 +1,5 @@-#ifndef SC_DELAY_BUFFER_H-#define SC_DELAY_BUFFER_H+#ifndef SC_VIDEO_REGULATOR_H+#define SC_VIDEO_REGULATOR_H#include "common.h"@@ -36,7 +36,7 @@ struct sc_delayed_packet {struct sc_delayed_packet_queue SC_VECDEQUE(struct sc_delayed_packet);-struct sc_delay_buffer {+struct sc_video_regulator {struct sc_frame_source frame_source; // frame source traitstruct sc_frame_sink frame_sink; // frame sink trait@@ -53,20 +53,20 @@ struct sc_delay_buffer {bool stopped;};-struct sc_delay_buffer_callbacks {- bool (*on_new_frame)(struct sc_delay_buffer *db, const AVFrame *frame,+struct sc_video_regulator_callbacks {+ bool (*on_new_frame)(struct sc_video_regulator *vr, const AVFrame *frame,void *userdata);};/**- * Initialize a delay buffer.+ * Initialize a video regulator.** \param delay a (strictly) positive delay* \param first_frame_asap if true, do not delay the first frame (useful fora video stream).*/void-sc_delay_buffer_init(struct sc_delay_buffer *db, sc_tick delay,- bool first_frame_asap);+sc_video_regulator_init(struct sc_video_regulator *vr, sc_tick delay,+ bool first_frame_asap);#endif