Prevent microstutter in balanced mode when streaming at 60 FPS on a 120 Hz display

This commit is contained in:
Cameron Gutman 2022-05-14 20:08:41 -05:00
parent 6f8e719200
commit 226e580a30

View File

@ -86,6 +86,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
private LinkedBlockingQueue<Integer> outputBufferQueue = new LinkedBlockingQueue<>(); private LinkedBlockingQueue<Integer> outputBufferQueue = new LinkedBlockingQueue<>();
private static final int OUTPUT_BUFFER_QUEUE_LIMIT = 2; private static final int OUTPUT_BUFFER_QUEUE_LIMIT = 2;
private long lastRenderedFrameTimeNanos;
private int numSpsIn; private int numSpsIn;
private int numPpsIn; private int numPpsIn;
@ -419,6 +420,11 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
return; return;
} }
// Don't render unless a new frame is due. This prevents microstutter when streaming
// at a frame rate that doesn't match the display (such as 60 FPS on 120 Hz).
long actualFrameTimeDeltaNs = frameTimeNanos - lastRenderedFrameTimeNanos;
long expectedFrameTimeDeltaNs = 800000000 / refreshRate; // within 80% of the next frame
if (actualFrameTimeDeltaNs >= expectedFrameTimeDeltaNs) {
// Render up to one frame when in frame pacing mode. // Render up to one frame when in frame pacing mode.
// //
// NB: Since the queue limit is 2, we won't starve the decoder of output buffers // NB: Since the queue limit is 2, we won't starve the decoder of output buffers
@ -433,8 +439,10 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer implements C
videoDecoder.releaseOutputBuffer(nextOutputBuffer, true); videoDecoder.releaseOutputBuffer(nextOutputBuffer, true);
} }
lastRenderedFrameTimeNanos = frameTimeNanos;
activeWindowVideoStats.totalFramesRendered++; activeWindowVideoStats.totalFramesRendered++;
} }
}
// Request another callback for next frame // Request another callback for next frame
Choreographer.getInstance().postFrameCallback(this); Choreographer.getInstance().postFrameCallback(this);