Skip to content

Commit 427245e

Browse files
committed
Add compat support for FFmpeg < 5.1
The new chlayout API has been introduced in FFmpeg 5.1. Use the old channel_layout API on older versions. PR #3757 <Genymobile/scrcpy#3757>
1 parent 2a6df62 commit 427245e

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

app/src/audio_player.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,19 @@ static bool
9393
sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
9494
const AVCodecContext *ctx) {
9595
struct sc_audio_player *ap = DOWNCAST(sink);
96+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
97+
assert(ctx->ch_layout.nb_channels > 0);
98+
unsigned nb_channels = ctx->ch_layout.nb_channels;
99+
#else
100+
int tmp = av_get_channel_layout_nb_channels(ctx->channel_layout);
101+
assert(tmp > 0);
102+
unsigned nb_channels = tmp;
103+
#endif
96104

97105
SDL_AudioSpec desired = {
98106
.freq = ctx->sample_rate,
99107
.format = SC_SDL_SAMPLE_FMT,
100-
.channels = ctx->ch_layout.nb_channels,
108+
.channels = nb_channels,
101109
.samples = SC_AUDIO_OUTPUT_BUFFER_SAMPLES,
102110
.callback = sc_audio_player_sdl_callback,
103111
.userdata = ap,
@@ -118,13 +126,20 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
118126
ap->swr_ctx = swr_ctx;
119127

120128
assert(ctx->sample_rate > 0);
121-
assert(ctx->ch_layout.nb_channels > 0);
122129
assert(!av_sample_fmt_is_planar(SC_AV_SAMPLE_FMT));
130+
123131
int out_bytes_per_sample = av_get_bytes_per_sample(SC_AV_SAMPLE_FMT);
124132
assert(out_bytes_per_sample > 0);
125133

134+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
126135
av_opt_set_chlayout(swr_ctx, "in_chlayout", &ctx->ch_layout, 0);
127136
av_opt_set_chlayout(swr_ctx, "out_chlayout", &ctx->ch_layout, 0);
137+
#else
138+
av_opt_set_channel_layout(swr_ctx, "in_channel_layout",
139+
ctx->channel_layout, 0);
140+
av_opt_set_channel_layout(swr_ctx, "out_channel_layout",
141+
ctx->channel_layout, 0);
142+
#endif
128143

129144
av_opt_set_int(swr_ctx, "in_sample_rate", ctx->sample_rate, 0);
130145
av_opt_set_int(swr_ctx, "out_sample_rate", ctx->sample_rate, 0);
@@ -139,7 +154,7 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
139154
}
140155

141156
ap->sample_rate = ctx->sample_rate;
142-
ap->nb_channels = ctx->ch_layout.nb_channels;
157+
ap->nb_channels = nb_channels;
143158
ap->out_bytes_per_sample = out_bytes_per_sample;
144159

145160
size_t bytebuf_size = samples_to_bytes(ap, SC_BYTEBUF_SIZE_IN_SAMPLES);

app/src/compat.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
# define SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
3838
#endif
3939

40+
// Not documented in ffmpeg/doc/APIchanges, but the channel_layout API
41+
// has been replaced by chlayout in FFmpeg commit
42+
// f423497b455da06c1337846902c770028760e094.
43+
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 23, 100)
44+
# define SCRCPY_LAVU_HAS_CHLAYOUT
45+
#endif
46+
4047
#if SDL_VERSION_ATLEAST(2, 0, 6)
4148
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
4249
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS

app/src/decoder.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
5353
decoder->codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
5454
} else {
5555
// Hardcoded audio properties
56+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
5657
decoder->codec_ctx->ch_layout =
5758
(AVChannelLayout) AV_CHANNEL_LAYOUT_STEREO;
59+
#else
60+
decoder->codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
61+
decoder->codec_ctx->channels = 2;
62+
#endif
5863
decoder->codec_ctx->sample_rate = 48000;
5964
}
6065

app/src/recorder.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ sc_recorder_wait_audio_stream(struct sc_recorder *recorder) {
216216

217217
stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
218218
stream->codecpar->codec_id = codec->id;
219+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
219220
stream->codecpar->ch_layout.nb_channels = 2;
221+
#else
222+
stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
223+
stream->codecpar->channels = 2;
224+
#endif
220225
stream->codecpar->sample_rate = 48000;
221226

222227
recorder->audio_stream_index = stream->index;

0 commit comments

Comments
 (0)