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 8750ed8de6.

PiperOrigin-RevId: 578799862
This commit is contained in:
tonihei 2023-11-02 03:53:19 -07:00 committed by Copybara-Service
parent d4e5ab2c4d
commit ae6f83d298

View File

@ -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 @@ import java.util.List;
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 @@ import java.util.List;
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 @@ import java.util.List;
}
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;