From df55bc2683e2ca2aec105d6e435cb0b170037ed7 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 2 Mar 2023 23:14:01 +0100 Subject: [PATCH] Add --audio-buffer Expose an option to add a buffering delay (in milliseconds) before playing audio. This is similar to the options --display-buffer and --v4l2-buffer for video frames. PR #3757 --- app/scrcpy.1 | 8 ++++++++ app/src/cli.c | 15 +++++++++++++++ app/src/options.c | 1 + app/src/options.h | 1 + app/src/scrcpy.c | 2 +- 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/scrcpy.1 b/app/scrcpy.1 index 91258414..120ea192 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -25,6 +25,14 @@ Encode the audio at the given bit\-rate, expressed in bits/s. Unit suffixes are Default is 128K (128000). +.TP +.BI "\-\-audio\-buffer ms +Configure the audio buffering delay (in milliseconds). + +Lower values decrease the latency, but increase the likelyhood of buffer underrun (causing audio glitches). + +Default is 50. + .TP .BI "\-\-audio\-codec " name Select an audio codec (opus or aac). diff --git a/app/src/cli.c b/app/src/cli.c index 18f3b83b..122a5891 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -71,6 +71,7 @@ enum { OPT_LIST_ENCODERS, OPT_LIST_DISPLAYS, OPT_REQUIRE_AUDIO, + OPT_AUDIO_BUFFER, }; struct sc_option { @@ -120,6 +121,15 @@ static const struct sc_option options[] = { "Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n" "Default is 128K (128000).", }, + { + .longopt_id = OPT_AUDIO_BUFFER, + .longopt = "audio-buffer", + .argdesc = "ms", + .text = "Configure the audio buffering delay (in milliseconds).\n" + "Lower values decrease the latency, but increase the " + "likelyhood of buffer underrun (causing audio glitches).\n" + "Default is 50.", + }, { .longopt_id = OPT_AUDIO_CODEC, .longopt = "audio-codec", @@ -1822,6 +1832,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[], case OPT_REQUIRE_AUDIO: opts->require_audio = true; break; + case OPT_AUDIO_BUFFER: + if (!parse_buffering_time(optarg, &opts->audio_buffer)) { + return false; + } + break; default: // getopt prints the error message on stderr return false; diff --git a/app/src/options.c b/app/src/options.c index 5dd655ce..68c16d53 100644 --- a/app/src/options.c +++ b/app/src/options.c @@ -43,6 +43,7 @@ const struct scrcpy_options scrcpy_options_default = { .display_id = 0, .display_buffer = 0, .v4l2_buffer = 0, + .audio_buffer = SC_TICK_FROM_MS(50), #ifdef HAVE_USB .otg = false, #endif diff --git a/app/src/options.h b/app/src/options.h index 5fcaf016..d9c2d228 100644 --- a/app/src/options.h +++ b/app/src/options.h @@ -125,6 +125,7 @@ struct scrcpy_options { uint32_t display_id; sc_tick display_buffer; sc_tick v4l2_buffer; + sc_tick audio_buffer; #ifdef HAVE_USB bool otg; #endif diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 3f3a34f0..ce045c97 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -687,7 +687,7 @@ aoa_hid_end: sc_frame_source_add_sink(src, &s->screen.frame_sink); if (options->audio) { - sc_audio_player_init(&s->audio_player, SC_TICK_FROM_MS(50)); + sc_audio_player_init(&s->audio_player, options->audio_buffer); sc_frame_source_add_sink(&s->audio_decoder.frame_source, &s->audio_player.frame_sink); }