Skip to content

Commit

Permalink
CompositionVideoSinkProvider: apply rotation effects per video
Browse files Browse the repository at this point in the history
There is a bug in CompositionVideoSinkProvider where the rotation
effect is applied only on the first video and the same effect
is applied on subsequent videos without checking if the next
items in the playlist must be rotated.

The bug applies only on devices with API < 21.

PiperOrigin-RevId: 589823797
  • Loading branch information
christosts authored and copybara-github committed Dec 11, 2023
1 parent 379cb3b commit 4c4c5f6
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ public void initialize(Format sourceFormat) throws VideoSink.VideoSinkException
/* compositionEffects= */ ImmutableList.of(),
/* initialTimestampOffsetUs= */ 0);
videoSinkImpl =
new VideoSinkImpl(
context, /* compositingVideoSinkProvider= */ this, videoGraph, sourceFormat);
new VideoSinkImpl(context, /* compositingVideoSinkProvider= */ this, videoGraph);
} catch (VideoFrameProcessingException e) {
throw new VideoSink.VideoSinkException(e, sourceFormat);
}
Expand Down Expand Up @@ -520,7 +519,7 @@ private static final class VideoSinkImpl implements VideoSink {
private final VideoFrameProcessor videoFrameProcessor;
private final int videoFrameProcessorMaxPendingFrameCount;
private final ArrayList<Effect> videoEffects;
@Nullable private final Effect rotationEffect;
@Nullable private Effect rotationEffect;

@Nullable private Format inputFormat;
private long inputStreamOffsetUs;
Expand All @@ -541,8 +540,7 @@ private static final class VideoSinkImpl implements VideoSink {
public VideoSinkImpl(
Context context,
CompositingVideoSinkProvider compositingVideoSinkProvider,
PreviewingVideoGraph videoGraph,
Format sourceFormat)
PreviewingVideoGraph videoGraph)
throws VideoFrameProcessingException {
this.context = context;
this.compositingVideoSinkProvider = compositingVideoSinkProvider;
Expand All @@ -556,11 +554,6 @@ public VideoSinkImpl(
videoFrameProcessor = videoGraph.getProcessor(videoGraphInputId);

videoEffects = new ArrayList<>();
// MediaCodec applies rotation after API 21.
rotationEffect =
Util.SDK_INT < 21 && sourceFormat.rotationDegrees != 0
? ScaleAndRotateAccessor.createRotationEffect(sourceFormat.rotationDegrees)
: null;
finalBufferPresentationTimeUs = C.TIME_UNSET;
lastBufferPresentationTimeUs = C.TIME_UNSET;
}
Expand Down Expand Up @@ -596,6 +589,20 @@ public void registerInputStream(@InputType int inputType, Format format) {
if (inputType != INPUT_TYPE_SURFACE) {
throw new UnsupportedOperationException("Unsupported input type " + inputType);
}
// MediaCodec applies rotation after API 21.
if (Util.SDK_INT < 21
&& format.rotationDegrees != Format.NO_VALUE
&& format.rotationDegrees != 0) {
// We must apply a rotation effect.
if (rotationEffect == null
|| this.inputFormat == null
|| this.inputFormat.rotationDegrees != format.rotationDegrees) {
rotationEffect = ScaleAndRotateAccessor.createRotationEffect(format.rotationDegrees);
} // Else, the rotation effect matches the previous format's rotation degrees, keep the same
// instance.
} else {
rotationEffect = null;
}
this.inputFormat = format;

if (!hasRegisteredFirstInputStream) {
Expand Down

0 comments on commit 4c4c5f6

Please sign in to comment.