Skip to content

Commit ae6f83d

Browse files
toniheicopybara-github
authored andcommitted
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 8750ed8. PiperOrigin-RevId: 578799862
1 parent d4e5ab2 commit ae6f83d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libraries/decoder_ffmpeg/src/main/java/androidx/media3/decoder/ffmpeg/FfmpegAudioDecoder.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package androidx.media3.decoder.ffmpeg;
1717

18+
import static androidx.media3.common.util.Assertions.checkNotNull;
19+
1820
import androidx.annotation.Nullable;
1921
import androidx.media3.common.C;
2022
import androidx.media3.common.Format;
2123
import androidx.media3.common.MimeTypes;
22-
import androidx.media3.common.util.Assertions;
2324
import androidx.media3.common.util.ParsableByteArray;
2425
import androidx.media3.common.util.Util;
2526
import androidx.media3.decoder.DecoderInputBuffer;
@@ -59,8 +60,8 @@ public FfmpegAudioDecoder(
5960
if (!FfmpegLibrary.isAvailable()) {
6061
throw new FfmpegDecoderException("Failed to load decoder native libraries.");
6162
}
62-
Assertions.checkNotNull(format.sampleMimeType);
63-
codecName = Assertions.checkNotNull(FfmpegLibrary.getCodecName(format.sampleMimeType));
63+
checkNotNull(format.sampleMimeType);
64+
codecName = checkNotNull(FfmpegLibrary.getCodecName(format.sampleMimeType));
6465
extraData = getExtraData(format.sampleMimeType, format.initializationData);
6566
encoding = outputFloat ? C.ENCODING_PCM_FLOAT : C.ENCODING_PCM_16BIT;
6667
outputBufferSize =
@@ -128,7 +129,7 @@ protected FfmpegDecoderException decode(
128129
channelCount = ffmpegGetChannelCount(nativeContext);
129130
sampleRate = ffmpegGetSampleRate(nativeContext);
130131
if (sampleRate == 0 && "alac".equals(codecName)) {
131-
Assertions.checkNotNull(extraData);
132+
checkNotNull(extraData);
132133
// ALAC decoder did not set the sample rate in earlier versions of FFmpeg. See
133134
// https://trac.ffmpeg.org/ticket/6096.
134135
ParsableByteArray parsableExtraData = new ParsableByteArray(extraData);
@@ -137,6 +138,9 @@ protected FfmpegDecoderException decode(
137138
}
138139
hasOutputFormat = true;
139140
}
141+
// Get a new reference to the output ByteBuffer in case the native decode method reallocated the
142+
// buffer to grow its size.
143+
outputData = checkNotNull(outputBuffer.data);
140144
outputData.position(0);
141145
outputData.limit(result);
142146
return null;

0 commit comments

Comments
 (0)