Commit 34007364 by andrewlewis Committed by Ian Baker

Avoid attempting to mux out of order pre-API 25

Calling `MediaMuxer.writeSampleData` can block indefinitely on old API versions. It is better not to call this method to fail quickly with an exception rather than getting stuck.

Based on on-device testing media muxer doesn't generally handle out of order samples before API 25. There are a small number of devices where this does succeed but it seems preferable to turn this off everywhere to keep the code simple and have consistent behavior. Once we switch to in-app muxing this limitation will no longer apply.

#mse-bug-week

PiperOrigin-RevId: 429070255
parent e296bf91
......@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.android.exoplayer2.util.Util.SDK_INT;
import static com.google.android.exoplayer2.util.Util.castNonNull;
......@@ -24,6 +25,7 @@ import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.ParcelFileDescriptor;
import android.util.SparseLongArray;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C;
......@@ -119,12 +121,14 @@ import java.nio.ByteBuffer;
private final MediaMuxer mediaMuxer;
private final MediaCodec.BufferInfo bufferInfo;
private final SparseLongArray trackIndexToLastPresentationTimeUs;
private boolean isStarted;
private FrameworkMuxer(MediaMuxer mediaMuxer) {
this.mediaMuxer = mediaMuxer;
bufferInfo = new MediaCodec.BufferInfo();
trackIndexToLastPresentationTimeUs = new SparseLongArray();
}
@Override
......@@ -172,7 +176,17 @@ import java.nio.ByteBuffer;
int size = data.limit() - offset;
int flags = isKeyFrame ? C.BUFFER_FLAG_KEY_FRAME : 0;
bufferInfo.set(offset, size, presentationTimeUs, flags);
long lastSamplePresentationTimeUs = trackIndexToLastPresentationTimeUs.get(trackIndex);
try {
// writeSampleData blocks on old API versions, so check here to avoid calling the method.
checkState(
Util.SDK_INT > 24 || presentationTimeUs >= lastSamplePresentationTimeUs,
"Samples not in presentation order ("
+ presentationTimeUs
+ " < "
+ lastSamplePresentationTimeUs
+ ") unsupported on this API version");
trackIndexToLastPresentationTimeUs.put(trackIndex, presentationTimeUs);
mediaMuxer.writeSampleData(trackIndex, data, bufferInfo);
} catch (RuntimeException e) {
throw new MuxerException(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment