Commit 8d47209a by andrewlewis Committed by Oliver Woodman

Make video playback smoother at the beginning

Video renderers assume that the player position is advancing linearly while in
the started state. MediaCodecVideoRenderer schedules frames for rendering in the
future in the expectation that the player position is advancing.

This assumption is not currently true when using a position from the AudioTrack:
the player position can be fixed for (in the worst case) up to about 100 ms
before it starts increasing. This leads to an effect where the first frame is
rendered then a few other frames are rendered, then there's a pause before
frames start being rendered smoothly.

Work around this issue by checking whether the position has started advancing
before scheduling frames to be rendered in the future.

It might be preferable to make the audio renderer only become ready when its
timestamp can start advancing, but this is likely to be complicated.

Issue: #3841

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190937429
parent 78204970
...@@ -127,6 +127,7 @@ public class LibvpxVideoRenderer extends BaseRenderer { ...@@ -127,6 +127,7 @@ public class LibvpxVideoRenderer extends BaseRenderer {
private Bitmap bitmap; private Bitmap bitmap;
private boolean renderedFirstFrame; private boolean renderedFirstFrame;
private long initialPositionUs;
private long joiningDeadlineMs; private long joiningDeadlineMs;
private Surface surface; private Surface surface;
private VpxOutputBufferRenderer outputBufferRenderer; private VpxOutputBufferRenderer outputBufferRenderer;
...@@ -303,6 +304,7 @@ public class LibvpxVideoRenderer extends BaseRenderer { ...@@ -303,6 +304,7 @@ public class LibvpxVideoRenderer extends BaseRenderer {
inputStreamEnded = false; inputStreamEnded = false;
outputStreamEnded = false; outputStreamEnded = false;
clearRenderedFirstFrame(); clearRenderedFirstFrame();
initialPositionUs = C.TIME_UNSET;
consecutiveDroppedFrameCount = 0; consecutiveDroppedFrameCount = 0;
if (decoder != null) { if (decoder != null) {
flushDecoder(); flushDecoder();
...@@ -809,6 +811,10 @@ public class LibvpxVideoRenderer extends BaseRenderer { ...@@ -809,6 +811,10 @@ public class LibvpxVideoRenderer extends BaseRenderer {
*/ */
private boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs) private boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs)
throws ExoPlaybackException { throws ExoPlaybackException {
if (initialPositionUs == C.TIME_UNSET) {
initialPositionUs = positionUs;
}
long earlyUs = outputBuffer.timeUs - positionUs; long earlyUs = outputBuffer.timeUs - positionUs;
if (outputMode == VpxDecoder.OUTPUT_MODE_NONE) { if (outputMode == VpxDecoder.OUTPUT_MODE_NONE) {
// Skip frames in sync with playback, so we'll be at the right frame if the mode changes. // Skip frames in sync with playback, so we'll be at the right frame if the mode changes.
...@@ -828,7 +834,7 @@ public class LibvpxVideoRenderer extends BaseRenderer { ...@@ -828,7 +834,7 @@ public class LibvpxVideoRenderer extends BaseRenderer {
return true; return true;
} }
if (!isStarted) { if (!isStarted || positionUs == initialPositionUs) {
return false; return false;
} }
......
...@@ -100,6 +100,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer { ...@@ -100,6 +100,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
@C.VideoScalingMode @C.VideoScalingMode
private int scalingMode; private int scalingMode;
private boolean renderedFirstFrame; private boolean renderedFirstFrame;
private long initialPositionUs;
private long joiningDeadlineMs; private long joiningDeadlineMs;
private long droppedFrameAccumulationStartTimeMs; private long droppedFrameAccumulationStartTimeMs;
private int droppedFrames; private int droppedFrames;
...@@ -276,6 +277,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer { ...@@ -276,6 +277,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException { protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
super.onPositionReset(positionUs, joining); super.onPositionReset(positionUs, joining);
clearRenderedFirstFrame(); clearRenderedFirstFrame();
initialPositionUs = C.TIME_UNSET;
consecutiveDroppedFrameCount = 0; consecutiveDroppedFrameCount = 0;
if (pendingOutputStreamOffsetCount != 0) { if (pendingOutputStreamOffsetCount != 0) {
outputStreamOffsetUs = pendingOutputStreamOffsetsUs[pendingOutputStreamOffsetCount - 1]; outputStreamOffsetUs = pendingOutputStreamOffsetsUs[pendingOutputStreamOffsetCount - 1];
...@@ -532,6 +534,10 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer { ...@@ -532,6 +534,10 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
protected boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs, MediaCodec codec, protected boolean processOutputBuffer(long positionUs, long elapsedRealtimeUs, MediaCodec codec,
ByteBuffer buffer, int bufferIndex, int bufferFlags, long bufferPresentationTimeUs, ByteBuffer buffer, int bufferIndex, int bufferFlags, long bufferPresentationTimeUs,
boolean shouldSkip) throws ExoPlaybackException { boolean shouldSkip) throws ExoPlaybackException {
if (initialPositionUs == C.TIME_UNSET) {
initialPositionUs = positionUs;
}
while (pendingOutputStreamOffsetCount != 0 while (pendingOutputStreamOffsetCount != 0
&& bufferPresentationTimeUs >= pendingOutputStreamOffsetsUs[0]) { && bufferPresentationTimeUs >= pendingOutputStreamOffsetsUs[0]) {
outputStreamOffsetUs = pendingOutputStreamOffsetsUs[0]; outputStreamOffsetUs = pendingOutputStreamOffsetsUs[0];
...@@ -569,7 +575,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer { ...@@ -569,7 +575,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
return true; return true;
} }
if (!isStarted) { if (!isStarted || positionUs == initialPositionUs) {
return false; return false;
} }
......
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