Skip to content

Commit

Permalink
Fix access to stale ByteBuffer in FfmpegAudioDecoder
Browse files Browse the repository at this point in the history
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 androidx@8750ed8.

PiperOrigin-RevId: 578799862
  • Loading branch information
tonihei authored and copybara-github committed Nov 2, 2023
1 parent d4e5ab2 commit ae6f83d
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit ae6f83d

Please sign in to comment.