Skip to content

Display and control your Android device

Create video regulator mutex/cond from init

Romain Vimontec3331431d66
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 5e756319..b6885430 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -355,7 +355,9 @@ scrcpy(struct scrcpy_options *options) {
bool recorder_started = false;
#ifdef HAVE_V4L2
bool v4l2_sink_initialized = false;
+ bool v4l2_regulator_initialized = false;
#endif
+ bool video_regulator_initialized = false;
bool video_demuxer_started = false;
bool audio_demuxer_started = false;
bool video_decoder_initialized = false;
@@ -844,8 +846,11 @@ aoa_complete:
if (options->video_playback) {
struct sc_frame_source *src = &s->video_decoder.frame_source;
if (options->video_buffer) {
- sc_video_regulator_init(&s->video_regulator,
- options->video_buffer, true);
+ if (!sc_video_regulator_init(&s->video_regulator,
+ options->video_buffer, true)) {
+ goto end;
+ }
+ video_regulator_initialized = true;
sc_frame_source_add_sink(src, &s->video_regulator.frame_sink);
src = &s->video_regulator.frame_source;
}
@@ -869,8 +874,11 @@ aoa_complete:
struct sc_frame_source *src = &s->video_decoder.frame_source;
if (options->v4l2_buffer) {
- sc_video_regulator_init(&s->v4l2_regulator, options->v4l2_buffer,
- true);
+ if (!sc_video_regulator_init(&s->v4l2_regulator,
+ options->v4l2_buffer, true)) {
+ goto end;
+ }
+ v4l2_regulator_initialized = true;
sc_frame_source_add_sink(src, &s->v4l2_regulator.frame_sink);
src = &s->v4l2_regulator.frame_source;
}
@@ -1036,7 +1044,14 @@ end:
sc_demuxer_join(&s->audio_demuxer);
}
+ if (video_regulator_initialized) {
+ sc_video_regulator_destroy(&s->video_regulator);
+ }
+
#ifdef HAVE_V4L2
+ if (v4l2_regulator_initialized) {
+ sc_video_regulator_destroy(&s->v4l2_regulator);
+ }
if (v4l2_sink_initialized) {
sc_v4l2_sink_destroy(&s->v4l2_sink);
}
diff --git a/app/src/video_regulator.c b/app/src/video_regulator.c
index 6c0b0796..1d147f55 100644
--- a/app/src/video_regulator.c
+++ b/app/src/video_regulator.c
@@ -138,47 +138,22 @@ sc_video_regulator_frame_sink_open(struct sc_frame_sink *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;
+ return false;
}
- ok = sc_thread_create(&vr->thread, run_buffering, "scrcpy-vruf", vr);
+ bool ok = sc_thread_create(&vr->thread, run_buffering, "scrcpy-vruf", vr);
if (!ok) {
LOGE("Could not start buffering thread");
- goto error_close_sinks;
+ sc_frame_source_sinks_close(&vr->frame_source);
+ return false;
}
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
@@ -196,9 +171,6 @@ sc_video_regulator_frame_sink_close(struct sc_frame_sink *sink) {
sc_frame_source_sinks_close(&vr->frame_source);
sc_vecdeque_destroy(&vr->queue);
- sc_cond_destroy(&vr->wait_cond);
- sc_cond_destroy(&vr->queue_cond);
- sc_mutex_destroy(&vr->mutex);
}
static enum sc_sink_result
@@ -286,11 +258,26 @@ sc_video_regulator_frame_sink_push_session(struct sc_frame_sink *sink,
return SC_SINK_OK;
}
-void
+bool
sc_video_regulator_init(struct sc_video_regulator *vr, sc_tick delay,
bool first_frame_asap) {
assert(delay > 0);
+ 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;
+ }
+
vr->delay = delay;
vr->first_frame_asap = first_frame_asap;
@@ -304,4 +291,20 @@ sc_video_regulator_init(struct sc_video_regulator *vr, sc_tick delay,
};
vr->frame_sink.ops = &ops;
+
+ return true;
+
+error_destroy_queue_cond:
+ sc_cond_destroy(&vr->queue_cond);
+error_destroy_mutex:
+ sc_mutex_destroy(&vr->mutex);
+
+ return false;
+}
+
+void
+sc_video_regulator_destroy(struct sc_video_regulator *vr) {
+ sc_cond_destroy(&vr->wait_cond);
+ sc_cond_destroy(&vr->queue_cond);
+ sc_mutex_destroy(&vr->mutex);
}
diff --git a/app/src/video_regulator.h b/app/src/video_regulator.h
index 77c70c0e..43333c86 100644
--- a/app/src/video_regulator.h
+++ b/app/src/video_regulator.h
@@ -65,8 +65,11 @@ struct sc_video_regulator_callbacks {
* \param first_frame_asap if true, do not delay the first frame (useful for
a video stream).
*/
-void
+bool
sc_video_regulator_init(struct sc_video_regulator *vr, sc_tick delay,
bool first_frame_asap);
+void
+sc_video_regulator_destroy(struct sc_video_regulator *vr);
+
#endif