Factor out VideoFrameProcessor logic

PiperOrigin-RevId: 559780905
This commit is contained in:
claincly 2023-08-24 09:39:34 -07:00 committed by Copybara-Service
parent cd0b7d9e29
commit 28fd43617e
2 changed files with 141 additions and 102 deletions

View file

@ -45,7 +45,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
/** Processes decoded video frames from one single input. */
/* package */ final class SingleInputVideoGraph implements GraphInput {
/* package */ final class SingleInputVideoGraph {
/**
* Listener for video frame processing events.
@ -68,11 +68,7 @@ import java.util.concurrent.atomic.AtomicLong;
void onEnded(long finalFramePresentationTimeUs);
}
private final VideoFrameProcessor videoFrameProcessor;
private final AtomicLong mediaItemOffsetUs;
private final ColorInfo inputColorInfo;
@Nullable final Presentation presentation;
private final VideoFrameProcessingWrapper videoFrameProcessingWrapper;
private volatile boolean hasProducedFrameWithTimestampZero;
@ -105,17 +101,14 @@ import java.util.concurrent.atomic.AtomicLong;
boolean renderFramesAutomatically,
@Nullable Presentation presentation)
throws VideoFrameProcessingException {
this.mediaItemOffsetUs = new AtomicLong();
this.inputColorInfo = inputColorInfo;
this.presentation = presentation;
videoFrameProcessor =
videoFrameProcessorFactory.create(
videoFrameProcessingWrapper =
new VideoFrameProcessingWrapper(
context,
debugViewProvider,
videoFrameProcessorFactory,
inputColorInfo,
outputColorInfo,
renderFramesAutomatically,
debugViewProvider,
listenerExecutor,
new VideoFrameProcessor.Listener() {
private long lastProcessedFramePresentationTimeUs;
@ -124,14 +117,12 @@ import java.util.concurrent.atomic.AtomicLong;
public void onInputStreamRegistered(
@VideoFrameProcessor.InputType int inputType,
List<Effect> effects,
FrameInfo frameInfo) {
// Do nothing.
}
FrameInfo frameInfo) {}
@Override
public void onOutputSizeChanged(int width, int height) {
// TODO: b/289986435 - Allow setting output surface info on VideoGraph.
checkNotNull(videoFrameProcessor)
checkNotNull(videoFrameProcessingWrapper)
.setOutputSurfaceInfo(listener.onOutputSizeChanged(width, height));
}
@ -154,7 +145,54 @@ import java.util.concurrent.atomic.AtomicLong;
public void onEnded() {
listener.onEnded(lastProcessedFramePresentationTimeUs);
}
});
},
renderFramesAutomatically,
presentation);
}
/** Returns the {@link GraphInput}. */
public GraphInput getInput() {
return videoFrameProcessingWrapper;
}
/* package */ boolean hasProducedFrameWithTimestampZero() {
return hasProducedFrameWithTimestampZero;
}
public void release() {
videoFrameProcessingWrapper.release();
}
private static final class VideoFrameProcessingWrapper implements GraphInput {
private final VideoFrameProcessor videoFrameProcessor;
private final AtomicLong mediaItemOffsetUs;
private final ColorInfo inputColorInfo;
@Nullable private final Presentation presentation;
public VideoFrameProcessingWrapper(
Context context,
VideoFrameProcessor.Factory videoFrameProcessorFactory,
ColorInfo inputColorInfo,
ColorInfo outputColorInfo,
DebugViewProvider debugViewProvider,
Executor listenerExecutor,
VideoFrameProcessor.Listener listener,
boolean renderFramesAutomatically,
@Nullable Presentation presentation)
throws VideoFrameProcessingException {
this.videoFrameProcessor =
videoFrameProcessorFactory.create(
context,
debugViewProvider,
inputColorInfo,
outputColorInfo,
renderFramesAutomatically,
listenerExecutor,
listener);
this.mediaItemOffsetUs = new AtomicLong();
this.inputColorInfo = inputColorInfo;
this.presentation = presentation;
}
@Override
@ -216,8 +254,8 @@ import java.util.concurrent.atomic.AtomicLong;
videoFrameProcessor.signalEndOfInput();
}
/* package */ boolean hasProducedFrameWithTimestampZero() {
return hasProducedFrameWithTimestampZero;
public void setOutputSurfaceInfo(@Nullable SurfaceInfo outputSurfaceInfo) {
videoFrameProcessor.setOutputSurfaceInfo(outputSurfaceInfo);
}
public void release() {
@ -253,4 +291,5 @@ import java.util.concurrent.atomic.AtomicLong;
effectsWithPresentationBuilder.addAll(effects).add(presentation);
return effectsWithPresentationBuilder.build();
}
}
}

View file

@ -179,7 +179,7 @@ import org.checkerframework.dataflow.qual.Pure;
@Override
public GraphInput getInput(EditedMediaItem item, Format format) {
return singleInputVideoGraph;
return singleInputVideoGraph.getInput();
}
@Override