From ae6f83d298424bd405372803bb8b206fc95a2d0f Mon Sep 17 00:00:00 2001 From: tonihei Date: Thu, 2 Nov 2023 03:53:19 -0700 Subject: [PATCH] Fix access to stale ByteBuffer in FfmpegAudioDecoder The native code can now reallocate the buffer if it needs to grow its size, so we have to reacquire a reference in the Java code to avoid accessing a stale instance. This fixes a bug introduced by https://github.com/androidx/media/pull/746/commits/8750ed8de6469dc818007f2eb254df9ddbd52cc5. PiperOrigin-RevId: 578799862 --- .../media3/decoder/ffmpeg/FfmpegAudioDecoder.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libraries/decoder_ffmpeg/src/main/java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java b/libraries/decoder_ffmpeg/src/main/java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java index d3b876403b1..8182dad297c 100644 --- a/libraries/decoder_ffmpeg/src/main/java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java +++ b/libraries/decoder_ffmpeg/src/main/java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java @@ -15,11 +15,12 @@ */ package androidx.media3.decoder.ffmpeg; +import static androidx.media3.common.util.Assertions.checkNotNull; + import androidx.annotation.Nullable; import androidx.media3.common.C; import androidx.media3.common.Format; import androidx.media3.common.MimeTypes; -import androidx.media3.common.util.Assertions; import androidx.media3.common.util.ParsableByteArray; import androidx.media3.common.util.Util; import androidx.media3.decoder.DecoderInputBuffer; @@ -59,8 +60,8 @@ public FfmpegAudioDecoder( if (!FfmpegLibrary.isAvailable()) { throw new FfmpegDecoderException("Failed to load decoder native libraries."); } - Assertions.checkNotNull(format.sampleMimeType); - codecName = Assertions.checkNotNull(FfmpegLibrary.getCodecName(format.sampleMimeType)); + checkNotNull(format.sampleMimeType); + codecName = checkNotNull(FfmpegLibrary.getCodecName(format.sampleMimeType)); extraData = getExtraData(format.sampleMimeType, format.initializationData); encoding = outputFloat ? C.ENCODING_PCM_FLOAT : C.ENCODING_PCM_16BIT; outputBufferSize = @@ -128,7 +129,7 @@ protected FfmpegDecoderException decode( channelCount = ffmpegGetChannelCount(nativeContext); sampleRate = ffmpegGetSampleRate(nativeContext); if (sampleRate == 0 && "alac".equals(codecName)) { - Assertions.checkNotNull(extraData); + checkNotNull(extraData); // ALAC decoder did not set the sample rate in earlier versions of FFmpeg. See // https://trac.ffmpeg.org/ticket/6096. ParsableByteArray parsableExtraData = new ParsableByteArray(extraData); @@ -137,6 +138,9 @@ protected FfmpegDecoderException decode( } hasOutputFormat = true; } + // Get a new reference to the output ByteBuffer in case the native decode method reallocated the + // buffer to grow its size. + outputData = checkNotNull(outputBuffer.data); outputData.position(0); outputData.limit(result); return null;