Skip to content

Commit 61b9b41

Browse files
committed
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 <Genymobile/scrcpy#3757>
1 parent 5828b85 commit 61b9b41

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

app/scrcpy.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Encode the audio at the given bit\-rate, expressed in bits/s. Unit suffixes are
2525

2626
Default is 196K (196000).
2727

28+
.TP
29+
.BI "\-\-audio\-buffer ms
30+
Add a buffering delay (in milliseconds) before playing audio. This increases latency to compensate for jitter.
31+
32+
Default is 0 (no buffering).
33+
2834
.TP
2935
.BI "\-\-audio\-codec " name
3036
Select an audio codec (opus or aac).

app/src/cli.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum {
7070
OPT_LIST_ENCODERS,
7171
OPT_LIST_DISPLAYS,
7272
OPT_REQUIRE_AUDIO,
73+
OPT_AUDIO_BUFFER,
7374
};
7475

7576
struct sc_option {
@@ -119,6 +120,14 @@ static const struct sc_option options[] = {
119120
"Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
120121
"Default is 196K (196000).",
121122
},
123+
{
124+
.longopt_id = OPT_AUDIO_BUFFER,
125+
.longopt = "audio-buffer",
126+
.argdesc = "ms",
127+
.text = "Add a buffering delay (in milliseconds) before playing audio. "
128+
"This increases latency to compensate for jitter.\n"
129+
"Default is 0 (no buffering).",
130+
},
122131
{
123132
.longopt_id = OPT_AUDIO_CODEC,
124133
.longopt = "audio-codec",
@@ -1812,6 +1821,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
18121821
case OPT_REQUIRE_AUDIO:
18131822
opts->require_audio = true;
18141823
break;
1824+
case OPT_AUDIO_BUFFER:
1825+
if (!parse_buffering_time(optarg, &opts->audio_buffer)) {
1826+
return false;
1827+
}
1828+
break;
18151829
default:
18161830
// getopt prints the error message on stderr
18171831
return false;

app/src/options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const struct scrcpy_options scrcpy_options_default = {
4343
.display_id = 0,
4444
.display_buffer = 0,
4545
.v4l2_buffer = 0,
46+
.audio_buffer = 0,
4647
#ifdef HAVE_USB
4748
.otg = false,
4849
#endif

app/src/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ struct scrcpy_options {
125125
uint32_t display_id;
126126
sc_tick display_buffer;
127127
sc_tick v4l2_buffer;
128+
sc_tick audio_buffer;
128129
#ifdef HAVE_USB
129130
bool otg;
130131
#endif

app/src/scrcpy.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct scrcpy {
4343
struct sc_server server;
4444
struct sc_screen screen;
4545
struct sc_audio_player audio_player;
46+
struct sc_delay_buffer audio_buffer;
4647
struct sc_demuxer video_demuxer;
4748
struct sc_demuxer audio_demuxer;
4849
struct sc_decoder video_decoder;
@@ -694,9 +695,16 @@ scrcpy(struct scrcpy_options *options) {
694695
sc_frame_source_add_sink(src, &s->screen.frame_sink);
695696

696697
if (options->audio) {
698+
struct sc_frame_source *src = &s->audio_decoder.frame_source;
699+
if (options->audio_buffer) {
700+
sc_delay_buffer_init(&s->audio_buffer, options->audio_buffer,
701+
false);
702+
sc_frame_source_add_sink(src, &s->audio_buffer.frame_sink);
703+
src = &s->audio_buffer.frame_source;
704+
}
705+
697706
sc_audio_player_init(&s->audio_player);
698-
sc_frame_source_add_sink(&s->audio_decoder.frame_source,
699-
&s->audio_player.frame_sink);
707+
sc_frame_source_add_sink(src, &s->audio_player.frame_sink);
700708
}
701709
}
702710

0 commit comments

Comments
 (0)