Clarify the size notification logic in MCVR

- Use a single `VideoSize` instance instead of four primitive fields.
- Clarify that the reported size is the decoded size, that is the encoded video
  size.

PiperOrigin-RevId: 494148190
This commit is contained in:
claincly 2022-12-09 13:38:31 +00:00 committed by Ian Baker
parent 6b15ace9aa
commit 8dbfa1d64d
2 changed files with 27 additions and 34 deletions

View File

@ -83,7 +83,7 @@ public final class VideoSize implements Bundleable {
} }
/** /**
* Creates a VideoSize. * Creates a new instance.
* *
* @param width The video width in pixels. * @param width The video width in pixels.
* @param height The video height in pixels. * @param height The video height in pixels.

View File

@ -154,10 +154,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
private long totalVideoFrameProcessingOffsetUs; private long totalVideoFrameProcessingOffsetUs;
private int videoFrameProcessingOffsetCount; private int videoFrameProcessingOffsetCount;
private int currentWidth; private VideoSize decodedVideoSize;
private int currentHeight;
private int currentUnappliedRotationDegrees;
private float currentPixelWidthHeightRatio;
@Nullable private VideoSize reportedVideoSize; @Nullable private VideoSize reportedVideoSize;
private boolean tunneling; private boolean tunneling;
@ -336,10 +333,8 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
eventDispatcher = new EventDispatcher(eventHandler, eventListener); eventDispatcher = new EventDispatcher(eventHandler, eventListener);
deviceNeedsNoPostProcessWorkaround = deviceNeedsNoPostProcessWorkaround(); deviceNeedsNoPostProcessWorkaround = deviceNeedsNoPostProcessWorkaround();
joiningDeadlineMs = C.TIME_UNSET; joiningDeadlineMs = C.TIME_UNSET;
currentWidth = Format.NO_VALUE;
currentHeight = Format.NO_VALUE;
currentPixelWidthHeightRatio = Format.NO_VALUE;
scalingMode = C.VIDEO_SCALING_MODE_DEFAULT; scalingMode = C.VIDEO_SCALING_MODE_DEFAULT;
decodedVideoSize = VideoSize.UNKNOWN;
tunnelingAudioSessionId = C.AUDIO_SESSION_ID_UNSET; tunnelingAudioSessionId = C.AUDIO_SESSION_ID_UNSET;
clearReportedVideoSize(); clearReportedVideoSize();
} }
@ -951,9 +946,14 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
// Must be applied each time the output format changes. // Must be applied each time the output format changes.
codec.setVideoScalingMode(scalingMode); codec.setVideoScalingMode(scalingMode);
} }
int width;
int height;
int unappliedRotationDegrees = 0;
float pixelWidthHeightRatio;
if (tunneling) { if (tunneling) {
currentWidth = format.width; width = format.width;
currentHeight = format.height; height = format.height;
} else { } else {
checkNotNull(mediaFormat); checkNotNull(mediaFormat);
boolean hasCrop = boolean hasCrop =
@ -961,30 +961,32 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
&& mediaFormat.containsKey(KEY_CROP_LEFT) && mediaFormat.containsKey(KEY_CROP_LEFT)
&& mediaFormat.containsKey(KEY_CROP_BOTTOM) && mediaFormat.containsKey(KEY_CROP_BOTTOM)
&& mediaFormat.containsKey(KEY_CROP_TOP); && mediaFormat.containsKey(KEY_CROP_TOP);
currentWidth = width =
hasCrop hasCrop
? mediaFormat.getInteger(KEY_CROP_RIGHT) - mediaFormat.getInteger(KEY_CROP_LEFT) + 1 ? mediaFormat.getInteger(KEY_CROP_RIGHT) - mediaFormat.getInteger(KEY_CROP_LEFT) + 1
: mediaFormat.getInteger(MediaFormat.KEY_WIDTH); : mediaFormat.getInteger(MediaFormat.KEY_WIDTH);
currentHeight = height =
hasCrop hasCrop
? mediaFormat.getInteger(KEY_CROP_BOTTOM) - mediaFormat.getInteger(KEY_CROP_TOP) + 1 ? mediaFormat.getInteger(KEY_CROP_BOTTOM) - mediaFormat.getInteger(KEY_CROP_TOP) + 1
: mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); : mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
} }
currentPixelWidthHeightRatio = format.pixelWidthHeightRatio; pixelWidthHeightRatio = format.pixelWidthHeightRatio;
if (Util.SDK_INT >= 21) { if (Util.SDK_INT >= 21) {
// On API level 21 and above the decoder applies the rotation when rendering to the surface. // On API level 21 and above the decoder applies the rotation when rendering to the surface.
// Hence currentUnappliedRotation should always be 0. For 90 and 270 degree rotations, we need // Hence currentUnappliedRotation should always be 0. For 90 and 270 degree rotations, we need
// to flip the width, height and pixel aspect ratio to reflect the rotation that was applied. // to flip the width, height and pixel aspect ratio to reflect the rotation that was applied.
if (format.rotationDegrees == 90 || format.rotationDegrees == 270) { if (format.rotationDegrees == 90 || format.rotationDegrees == 270) {
int rotatedHeight = currentWidth; int rotatedHeight = width;
currentWidth = currentHeight; width = height;
currentHeight = rotatedHeight; height = rotatedHeight;
currentPixelWidthHeightRatio = 1 / currentPixelWidthHeightRatio; pixelWidthHeightRatio = 1 / pixelWidthHeightRatio;
} }
} else { } else {
// On API level 20 and below the decoder does not apply the rotation. // On API level 20 and below the decoder does not apply the rotation.
currentUnappliedRotationDegrees = format.rotationDegrees; unappliedRotationDegrees = format.rotationDegrees;
} }
decodedVideoSize =
new VideoSize(width, height, unappliedRotationDegrees, pixelWidthHeightRatio);
frameReleaseHelper.onFormatChanged(format.frameRate); frameReleaseHelper.onFormatChanged(format.frameRate);
} }
@ -1169,7 +1171,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
/** Called when a buffer was processed in tunneling mode. */ /** Called when a buffer was processed in tunneling mode. */
protected void onProcessedTunneledBuffer(long presentationTimeUs) throws ExoPlaybackException { protected void onProcessedTunneledBuffer(long presentationTimeUs) throws ExoPlaybackException {
updateOutputFormatForTime(presentationTimeUs); updateOutputFormatForTime(presentationTimeUs);
maybeNotifyVideoSizeChanged(); maybeNotifyVideoSizeChanged(decodedVideoSize);
decoderCounters.renderedOutputBufferCount++; decoderCounters.renderedOutputBufferCount++;
maybeNotifyRenderedFirstFrame(); maybeNotifyRenderedFirstFrame();
onProcessedOutputBuffer(presentationTimeUs); onProcessedOutputBuffer(presentationTimeUs);
@ -1340,7 +1342,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
* @param presentationTimeUs The presentation time of the output buffer, in microseconds. * @param presentationTimeUs The presentation time of the output buffer, in microseconds.
*/ */
protected void renderOutputBuffer(MediaCodecAdapter codec, int index, long presentationTimeUs) { protected void renderOutputBuffer(MediaCodecAdapter codec, int index, long presentationTimeUs) {
maybeNotifyVideoSizeChanged(); maybeNotifyVideoSizeChanged(decodedVideoSize);
TraceUtil.beginSection("releaseOutputBuffer"); TraceUtil.beginSection("releaseOutputBuffer");
codec.releaseOutputBuffer(index, true); codec.releaseOutputBuffer(index, true);
TraceUtil.endSection(); TraceUtil.endSection();
@ -1362,7 +1364,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
@RequiresApi(21) @RequiresApi(21)
protected void renderOutputBufferV21( protected void renderOutputBufferV21(
MediaCodecAdapter codec, int index, long presentationTimeUs, long releaseTimeNs) { MediaCodecAdapter codec, int index, long presentationTimeUs, long releaseTimeNs) {
maybeNotifyVideoSizeChanged(); maybeNotifyVideoSizeChanged(decodedVideoSize);
TraceUtil.beginSection("releaseOutputBuffer"); TraceUtil.beginSection("releaseOutputBuffer");
codec.releaseOutputBuffer(index, releaseTimeNs); codec.releaseOutputBuffer(index, releaseTimeNs);
TraceUtil.endSection(); TraceUtil.endSection();
@ -1429,19 +1431,10 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
reportedVideoSize = null; reportedVideoSize = null;
} }
private void maybeNotifyVideoSizeChanged() { /** Notifies the new video size. */
if ((currentWidth != Format.NO_VALUE || currentHeight != Format.NO_VALUE) private void maybeNotifyVideoSizeChanged(VideoSize newOutputSize) {
&& (reportedVideoSize == null if (!newOutputSize.equals(VideoSize.UNKNOWN) && !newOutputSize.equals(reportedVideoSize)) {
|| reportedVideoSize.width != currentWidth reportedVideoSize = newOutputSize;
|| reportedVideoSize.height != currentHeight
|| reportedVideoSize.unappliedRotationDegrees != currentUnappliedRotationDegrees
|| reportedVideoSize.pixelWidthHeightRatio != currentPixelWidthHeightRatio)) {
reportedVideoSize =
new VideoSize(
currentWidth,
currentHeight,
currentUnappliedRotationDegrees,
currentPixelWidthHeightRatio);
eventDispatcher.videoSizeChanged(reportedVideoSize); eventDispatcher.videoSizeChanged(reportedVideoSize);
} }
} }