Always reconfigure video sink for video after image

Before this CL, the video sink was not reconfigured for the second video
in a sequence with video-image-video. For example, the stream offset
and listener were not set for the second video.

PiperOrigin-RevId: 628065991
This commit is contained in:
kimvde 2024-04-25 07:16:26 -07:00 committed by Copybara-Service
parent 0e3b05c67d
commit 00ce572a4f

View File

@ -1068,65 +1068,58 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
ownsVideoSinkProvider && hasEffects && !hasInitializedPlayback; ownsVideoSinkProvider && hasEffects && !hasInitializedPlayback;
// We always use the video sink if the video sink provider is passed to the renderer. // We always use the video sink if the video sink provider is passed to the renderer.
boolean useVideoSink = enableEffectsForOwnSinkProvider || !ownsVideoSinkProvider; boolean useVideoSink = enableEffectsForOwnSinkProvider || !ownsVideoSinkProvider;
if (useVideoSink && videoSink == null) { if (useVideoSink) {
try { videoSinkProvider.setStreamOffsetUs(getOutputStreamOffsetUs());
videoSinkProvider.setStreamOffsetUs(getOutputStreamOffsetUs()); videoSink = videoSinkProvider.getSink();
videoSink = videoSinkProvider.getSink(); if (!videoSink.isInitialized()) {
if (!videoSink.isInitialized()) { try {
try { videoSink.initialize(format);
videoSink.initialize(format); } catch (VideoSink.VideoSinkException e) {
} catch (VideoSink.VideoSinkException e) { throw createRendererException(
throw createRendererException( e, format, PlaybackException.ERROR_CODE_VIDEO_FRAME_PROCESSOR_INIT_FAILED);
e, format, PlaybackException.ERROR_CODE_VIDEO_FRAME_PROCESSOR_INIT_FAILED);
}
} }
videoSink.setListener( }
new VideoSink.Listener() { videoSink.setListener(
@Override new VideoSink.Listener() {
public void onFirstFrameRendered(VideoSink videoSink) { @Override
checkStateNotNull(displaySurface); public void onFirstFrameRendered(VideoSink videoSink) {
notifyRenderedFirstFrame(); checkStateNotNull(displaySurface);
} notifyRenderedFirstFrame();
}
@Override @Override
public void onFrameDropped(VideoSink videoSink) { public void onFrameDropped(VideoSink videoSink) {
updateDroppedBufferCounters( updateDroppedBufferCounters(
/* droppedInputBufferCount= */ 0, /* droppedDecoderBufferCount= */ 1); /* droppedInputBufferCount= */ 0, /* droppedDecoderBufferCount= */ 1);
} }
@Override @Override
public void onVideoSizeChanged(VideoSink videoSink, VideoSize videoSize) { public void onVideoSizeChanged(VideoSink videoSink, VideoSize videoSize) {
// TODO: b/292111083 - Report video size change to app. Video size reporting is // TODO: b/292111083 - Report video size change to app. Video size reporting is
// removed at the moment to ensure the first frame is rendered, and the video is // removed at the moment to ensure the first frame is rendered, and the video is
// rendered after switching on/off the screen. // rendered after switching on/off the screen.
} }
@Override @Override
public void onError( public void onError(
VideoSink videoSink, VideoSink.VideoSinkException videoSinkException) { VideoSink videoSink, VideoSink.VideoSinkException videoSinkException) {
setPendingPlaybackException( setPendingPlaybackException(
createRendererException( createRendererException(
videoSinkException, videoSinkException,
videoSinkException.format, videoSinkException.format,
PlaybackException.ERROR_CODE_VIDEO_FRAME_PROCESSING_FAILED)); PlaybackException.ERROR_CODE_VIDEO_FRAME_PROCESSING_FAILED));
} }
}, },
// Pass a direct executor since the callback handling involves posting on the app looper // Pass a direct executor since the callback handling involves posting on the app looper
// again, so there's no need to do two hops. // again, so there's no need to do two hops.
directExecutor()); directExecutor());
if (frameMetadataListener != null) { if (frameMetadataListener != null) {
videoSink.setVideoFrameMetadataListener(frameMetadataListener); videoSink.setVideoFrameMetadataListener(frameMetadataListener);
}
if (enableEffectsForOwnSinkProvider) {
if (displaySurface != null && outputResolution != null) {
videoSinkProvider.setOutputSurfaceInfo(displaySurface, outputResolution);
} }
if (enableEffectsForOwnSinkProvider) {
if (displaySurface != null && outputResolution != null) {
videoSinkProvider.setOutputSurfaceInfo(displaySurface, outputResolution);
}
}
} catch (Exception e) {
// Set videoSink back to null so that, if the try block fails and the renderer retries the
// codec initialization, the try block is re-executed.
videoSink = null;
throw e;
} }
} }
hasInitializedPlayback = true; hasInitializedPlayback = true;