Skip to content

Commit 6e948fe

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.
1 parent 10e62a2 commit 6e948fe

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

app/src/audio_player.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,19 @@ static bool
106106
sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
107107
const AVCodecContext *ctx) {
108108
struct sc_audio_player *ap = DOWNCAST(sink);
109+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
110+
assert(ctx->ch_layout.nb_channels > 0);
111+
unsigned nb_channels = ctx->ch_layout.nb_channels;
112+
#else
113+
int tmp = av_get_channel_layout_nb_channels(ctx->channel_layout);
114+
assert(tmp > 0);
115+
unsigned nb_channels = tmp;
116+
#endif
109117

110118
SDL_AudioSpec desired = {
111119
.freq = ctx->sample_rate,
112120
.format = SC_SDL_SAMPLE_FMT,
113-
.channels = ctx->ch_layout.nb_channels,
121+
.channels = nb_channels,
114122
.samples = SC_AUDIO_OUTPUT_BUFFER_SAMPLES,
115123
.callback = sc_audio_player_sdl_callback,
116124
.userdata = ap,
@@ -131,13 +139,20 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
131139
ap->swr_ctx = swr_ctx;
132140

133141
assert(ctx->sample_rate > 0);
134-
assert(ctx->ch_layout.nb_channels > 0);
135142
assert(!av_sample_fmt_is_planar(SC_AV_SAMPLE_FMT));
143+
136144
int out_bytes_per_sample = av_get_bytes_per_sample(SC_AV_SAMPLE_FMT);
137145
assert(out_bytes_per_sample > 0);
138146

147+
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
139148
av_opt_set_chlayout(swr_ctx, "in_chlayout", &ctx->ch_layout, 0);
140149
av_opt_set_chlayout(swr_ctx, "out_chlayout", &ctx->ch_layout, 0);
150+
#else
151+
av_opt_set_channel_layout(swr_ctx, "in_channel_layout",
152+
ctx->channel_layout, 0);
153+
av_opt_set_channel_layout(swr_ctx, "out_channel_layout",
154+
ctx->channel_layout, 0);
155+
#endif
141156

142157
av_opt_set_int(swr_ctx, "in_sample_rate", ctx->sample_rate, 0);
143158
av_opt_set_int(swr_ctx, "out_sample_rate", ctx->sample_rate, 0);
@@ -152,7 +167,7 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
152167
}
153168

154169
ap->sample_rate = ctx->sample_rate;
155-
ap->nb_channels = ctx->ch_layout.nb_channels;
170+
ap->nb_channels = nb_channels;
156171
ap->out_bytes_per_sample = out_bytes_per_sample;
157172

158173
size_t bytebuf_size =

app/src/compat.h

+7
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

+5
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

+5
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)