Skip to content

Commit

Permalink
Add compat support for FFmpeg < 5.1
Browse files Browse the repository at this point in the history
The new chlayout API has been introduced in FFmpeg 5.1. Use the old
channel_layout API on older versions.
  • Loading branch information
rom1v committed Feb 27, 2023
1 parent 10e62a2 commit 6e948fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions app/src/audio_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ static bool
sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
const AVCodecContext *ctx) {
struct sc_audio_player *ap = DOWNCAST(sink);
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
assert(ctx->ch_layout.nb_channels > 0);
unsigned nb_channels = ctx->ch_layout.nb_channels;
#else
int tmp = av_get_channel_layout_nb_channels(ctx->channel_layout);
assert(tmp > 0);
unsigned nb_channels = tmp;
#endif

SDL_AudioSpec desired = {
.freq = ctx->sample_rate,
.format = SC_SDL_SAMPLE_FMT,
.channels = ctx->ch_layout.nb_channels,
.channels = nb_channels,
.samples = SC_AUDIO_OUTPUT_BUFFER_SAMPLES,
.callback = sc_audio_player_sdl_callback,
.userdata = ap,
Expand All @@ -131,13 +139,20 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink,
ap->swr_ctx = swr_ctx;

assert(ctx->sample_rate > 0);
assert(ctx->ch_layout.nb_channels > 0);
assert(!av_sample_fmt_is_planar(SC_AV_SAMPLE_FMT));

int out_bytes_per_sample = av_get_bytes_per_sample(SC_AV_SAMPLE_FMT);
assert(out_bytes_per_sample > 0);

#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
av_opt_set_chlayout(swr_ctx, "in_chlayout", &ctx->ch_layout, 0);
av_opt_set_chlayout(swr_ctx, "out_chlayout", &ctx->ch_layout, 0);
#else
av_opt_set_channel_layout(swr_ctx, "in_channel_layout",
ctx->channel_layout, 0);
av_opt_set_channel_layout(swr_ctx, "out_channel_layout",
ctx->channel_layout, 0);
#endif

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

ap->sample_rate = ctx->sample_rate;
ap->nb_channels = ctx->ch_layout.nb_channels;
ap->nb_channels = nb_channels;
ap->out_bytes_per_sample = out_bytes_per_sample;

size_t bytebuf_size =
Expand Down
7 changes: 7 additions & 0 deletions app/src/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
# define SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
#endif

// Not documented in ffmpeg/doc/APIchanges, but the channel_layout API
// has been replaced by chlayout in FFmpeg commit
// f423497b455da06c1337846902c770028760e094.
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 23, 100)
# define SCRCPY_LAVU_HAS_CHLAYOUT
#endif

#if SDL_VERSION_ATLEAST(2, 0, 6)
// <https://github.com/libsdl-org/SDL/commit/d7a318de563125e5bb465b1000d6bc9576fbc6fc>
# define SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
Expand Down
5 changes: 5 additions & 0 deletions app/src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ sc_decoder_open(struct sc_decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
} else {
// Hardcoded audio properties
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
decoder->codec_ctx->ch_layout =
(AVChannelLayout) AV_CHANNEL_LAYOUT_STEREO;
#else
decoder->codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
decoder->codec_ctx->channels = 2;
#endif
decoder->codec_ctx->sample_rate = 48000;
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ sc_recorder_wait_audio_stream(struct sc_recorder *recorder) {

stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
stream->codecpar->codec_id = codec->id;
#ifdef SCRCPY_LAVU_HAS_CHLAYOUT
stream->codecpar->ch_layout.nb_channels = 2;
#else
stream->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
stream->codecpar->channels = 2;
#endif
stream->codecpar->sample_rate = 48000;

recorder->audio_stream_index = stream->index;
Expand Down

0 comments on commit 6e948fe

Please sign in to comment.