Bring back setRenderTimeLimitMs

PiperOrigin-RevId: 333712782
This commit is contained in:
christosts 2020-09-25 13:10:35 +01:00 committed by kim-vde
parent 9b39268e0b
commit 397fe8f305

View file

@ -323,6 +323,7 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
@Nullable private DrmSession sourceDrmSession; @Nullable private DrmSession sourceDrmSession;
@Nullable private MediaCrypto mediaCrypto; @Nullable private MediaCrypto mediaCrypto;
private boolean mediaCryptoRequiresSecureDecoder; private boolean mediaCryptoRequiresSecureDecoder;
private long renderTimeLimitMs;
private float operatingRate; private float operatingRate;
@Nullable private MediaCodec codec; @Nullable private MediaCodec codec;
@Nullable private MediaCodecAdapter codecAdapter; @Nullable private MediaCodecAdapter codecAdapter;
@ -401,6 +402,7 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
decodeOnlyPresentationTimestamps = new ArrayList<>(); decodeOnlyPresentationTimestamps = new ArrayList<>();
outputBufferInfo = new MediaCodec.BufferInfo(); outputBufferInfo = new MediaCodec.BufferInfo();
operatingRate = 1f; operatingRate = 1f;
renderTimeLimitMs = C.TIME_UNSET;
pendingOutputStreamStartPositionsUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT]; pendingOutputStreamStartPositionsUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT];
pendingOutputStreamOffsetsUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT]; pendingOutputStreamOffsetsUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT];
pendingOutputStreamSwitchTimesUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT]; pendingOutputStreamSwitchTimesUs = new long[MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT];
@ -410,6 +412,19 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
resetCodecStateForRelease(); resetCodecStateForRelease();
} }
/**
* Set a limit on the time a single {@link #render(long, long)} call can spend draining and
* filling the decoder.
*
* <p>This method should be called right after creating an instance of this class.
*
* @param renderTimeLimitMs The render time limit in milliseconds, or {@link C#TIME_UNSET} for no
* limit.
*/
public void setRenderTimeLimitMs(long renderTimeLimitMs) {
this.renderTimeLimitMs = renderTimeLimitMs;
}
/** /**
* Enable asynchronous input buffer queueing. * Enable asynchronous input buffer queueing.
* *
@ -784,9 +799,11 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
while (bypassRender(positionUs, elapsedRealtimeUs)) {} while (bypassRender(positionUs, elapsedRealtimeUs)) {}
TraceUtil.endSection(); TraceUtil.endSection();
} else if (codec != null) { } else if (codec != null) {
long renderStartTimeMs = SystemClock.elapsedRealtime();
TraceUtil.beginSection("drainAndFeed"); TraceUtil.beginSection("drainAndFeed");
while (drainOutputBuffer(positionUs, elapsedRealtimeUs)) {} while (drainOutputBuffer(positionUs, elapsedRealtimeUs)
while (feedInputBuffer()) {} && shouldContinueRendering(renderStartTimeMs)) {}
while (feedInputBuffer() && shouldContinueRendering(renderStartTimeMs)) {}
TraceUtil.endSection(); TraceUtil.endSection();
} else { } else {
decoderCounters.skippedInputBufferCount += skipSource(positionUs); decoderCounters.skippedInputBufferCount += skipSource(positionUs);
@ -1110,6 +1127,11 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
onCodecInitialized(codecName, codecInitializedTimestamp, elapsed); onCodecInitialized(codecName, codecInitializedTimestamp, elapsed);
} }
private boolean shouldContinueRendering(long renderStartTimeMs) {
return renderTimeLimitMs == C.TIME_UNSET
|| SystemClock.elapsedRealtime() - renderStartTimeMs < renderTimeLimitMs;
}
private void getCodecBuffers(MediaCodec codec) { private void getCodecBuffers(MediaCodec codec) {
if (Util.SDK_INT < 21) { if (Util.SDK_INT < 21) {
inputBuffers = codec.getInputBuffers(); inputBuffers = codec.getInputBuffers();