Commit e6d55955 by kimvde Committed by Rohit Singh

Remove muxerFactory dependency on playerListener

This listener will need to be passed to the MuxerWrapper to throw when
the Transformer is stuck.

PiperOrigin-RevId: 482433552
parent 2c61dd9c
...@@ -104,19 +104,19 @@ public final class Transformer { ...@@ -104,19 +104,19 @@ public final class Transformer {
private final Context context; private final Context context;
// Optional fields. // Optional fields.
private MediaSource.@MonotonicNonNull Factory mediaSourceFactory;
private Muxer.Factory muxerFactory;
private boolean removeAudio;
private boolean removeVideo;
private TransformationRequest transformationRequest; private TransformationRequest transformationRequest;
private ImmutableList<Effect> videoEffects; private ImmutableList<Effect> videoEffects;
private FrameProcessor.Factory frameProcessorFactory; private boolean removeAudio;
private boolean removeVideo;
private ListenerSet<Transformer.Listener> listeners; private ListenerSet<Transformer.Listener> listeners;
private DebugViewProvider debugViewProvider; private MediaSource.@MonotonicNonNull Factory mediaSourceFactory;
private Codec.DecoderFactory decoderFactory;
private Codec.EncoderFactory encoderFactory;
private FrameProcessor.Factory frameProcessorFactory;
private Muxer.Factory muxerFactory;
private Looper looper; private Looper looper;
private DebugViewProvider debugViewProvider;
private Clock clock; private Clock clock;
private Codec.EncoderFactory encoderFactory;
private Codec.DecoderFactory decoderFactory;
/** /**
* Creates a builder with default values. * Creates a builder with default values.
...@@ -125,32 +125,32 @@ public final class Transformer { ...@@ -125,32 +125,32 @@ public final class Transformer {
*/ */
public Builder(Context context) { public Builder(Context context) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
transformationRequest = new TransformationRequest.Builder().build();
videoEffects = ImmutableList.of();
decoderFactory = new DefaultDecoderFactory(this.context);
encoderFactory = new DefaultEncoderFactory.Builder(this.context).build();
frameProcessorFactory = new GlEffectsFrameProcessor.Factory();
muxerFactory = new DefaultMuxer.Factory(); muxerFactory = new DefaultMuxer.Factory();
looper = Util.getCurrentOrMainLooper(); looper = Util.getCurrentOrMainLooper();
debugViewProvider = DebugViewProvider.NONE;
clock = Clock.DEFAULT; clock = Clock.DEFAULT;
listeners = new ListenerSet<>(looper, clock, (listener, flags) -> {}); listeners = new ListenerSet<>(looper, clock, (listener, flags) -> {});
encoderFactory = new DefaultEncoderFactory.Builder(this.context).build();
decoderFactory = new DefaultDecoderFactory(this.context);
debugViewProvider = DebugViewProvider.NONE;
transformationRequest = new TransformationRequest.Builder().build();
videoEffects = ImmutableList.of();
frameProcessorFactory = new GlEffectsFrameProcessor.Factory();
} }
/** Creates a builder with the values of the provided {@link Transformer}. */ /** Creates a builder with the values of the provided {@link Transformer}. */
private Builder(Transformer transformer) { private Builder(Transformer transformer) {
this.context = transformer.context; this.context = transformer.context;
this.mediaSourceFactory = transformer.mediaSourceFactory;
this.muxerFactory = transformer.muxerFactory;
this.removeAudio = transformer.removeAudio;
this.removeVideo = transformer.removeVideo;
this.transformationRequest = transformer.transformationRequest; this.transformationRequest = transformer.transformationRequest;
this.videoEffects = transformer.videoEffects; this.videoEffects = transformer.videoEffects;
this.frameProcessorFactory = transformer.frameProcessorFactory; this.removeAudio = transformer.removeAudio;
this.removeVideo = transformer.removeVideo;
this.listeners = transformer.listeners; this.listeners = transformer.listeners;
this.looper = transformer.looper; this.mediaSourceFactory = transformer.mediaSourceFactory;
this.encoderFactory = transformer.encoderFactory;
this.decoderFactory = transformer.decoderFactory; this.decoderFactory = transformer.decoderFactory;
this.encoderFactory = transformer.encoderFactory;
this.frameProcessorFactory = transformer.frameProcessorFactory;
this.muxerFactory = transformer.muxerFactory;
this.looper = transformer.looper;
this.debugViewProvider = transformer.debugViewProvider; this.debugViewProvider = transformer.debugViewProvider;
this.clock = transformer.clock; this.clock = transformer.clock;
} }
...@@ -195,41 +195,6 @@ public final class Transformer { ...@@ -195,41 +195,6 @@ public final class Transformer {
} }
/** /**
* Sets the {@link FrameProcessor.Factory} for the {@link FrameProcessor} to use when applying
* {@linkplain Effect effects} to the video frames.
*
* <p>This factory will be used to create the {@link FrameProcessor} used for applying the
* {@link Effect} instances passed to {@link #setVideoEffects(List)} and any additional {@link
* GlMatrixTransformation} instances derived from the {@link TransformationRequest} set using
* {@link #setTransformationRequest(TransformationRequest)}.
*
* <p>The default is {@link GlEffectsFrameProcessor.Factory}.
*
* @param frameProcessorFactory The {@link FrameProcessor.Factory} to use.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setFrameProcessorFactory(FrameProcessor.Factory frameProcessorFactory) {
this.frameProcessorFactory = frameProcessorFactory;
return this;
}
/**
* Sets the {@link MediaSource.Factory} to be used to retrieve the inputs to transform.
*
* <p>The default value is a {@link DefaultMediaSourceFactory} built with the context provided
* in {@linkplain #Builder(Context) the constructor}.
*
* @param mediaSourceFactory A {@link MediaSource.Factory}.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory) {
this.mediaSourceFactory = mediaSourceFactory;
return this;
}
/**
* Sets whether to remove the audio from the output. * Sets whether to remove the audio from the output.
* *
* <p>The default value is {@code false}. * <p>The default value is {@code false}.
...@@ -329,19 +294,31 @@ public final class Transformer { ...@@ -329,19 +294,31 @@ public final class Transformer {
} }
/** /**
* Sets the {@link Looper} that must be used for all calls to the transformer and that is used * Sets the {@link MediaSource.Factory} to be used to retrieve the inputs to transform.
* to call listeners on.
* *
* <p>The default value is the Looper of the thread that this builder was created on, or if that * <p>The default value is a {@link DefaultMediaSourceFactory} built with the context provided
* thread does not have a Looper, the Looper of the application's main thread. * in {@linkplain #Builder(Context) the constructor}.
* *
* @param looper A {@link Looper}. * @param mediaSourceFactory A {@link MediaSource.Factory}.
* @return This builder. * @return This builder.
*/ */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setLooper(Looper looper) { public Builder setMediaSourceFactory(MediaSource.Factory mediaSourceFactory) {
this.looper = looper; this.mediaSourceFactory = mediaSourceFactory;
this.listeners = listeners.copy(looper, (listener, flags) -> {}); return this;
}
/**
* Sets the {@link Codec.DecoderFactory} that will be used by the transformer.
*
* <p>The default value is a {@link DefaultDecoderFactory} instance.
*
* @param decoderFactory The {@link Codec.DecoderFactory} instance.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setDecoderFactory(Codec.DecoderFactory decoderFactory) {
this.decoderFactory = decoderFactory;
return this; return this;
} }
...@@ -360,16 +337,22 @@ public final class Transformer { ...@@ -360,16 +337,22 @@ public final class Transformer {
} }
/** /**
* Sets the {@link Codec.DecoderFactory} that will be used by the transformer. * Sets the {@link FrameProcessor.Factory} for the {@link FrameProcessor} to use when applying
* {@linkplain Effect effects} to the video frames.
* *
* <p>The default value is a {@link DefaultDecoderFactory} instance. * <p>This factory will be used to create the {@link FrameProcessor} used for applying the
* {@link Effect} instances passed to {@link #setVideoEffects(List)} and any additional {@link
* GlMatrixTransformation} instances derived from the {@link TransformationRequest} set using
* {@link #setTransformationRequest(TransformationRequest)}.
* *
* @param decoderFactory The {@link Codec.DecoderFactory} instance. * <p>The default is {@link GlEffectsFrameProcessor.Factory}.
*
* @param frameProcessorFactory The {@link FrameProcessor.Factory} to use.
* @return This builder. * @return This builder.
*/ */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setDecoderFactory(Codec.DecoderFactory decoderFactory) { public Builder setFrameProcessorFactory(FrameProcessor.Factory frameProcessorFactory) {
this.decoderFactory = decoderFactory; this.frameProcessorFactory = frameProcessorFactory;
return this; return this;
} }
...@@ -388,6 +371,23 @@ public final class Transformer { ...@@ -388,6 +371,23 @@ public final class Transformer {
} }
/** /**
* Sets the {@link Looper} that must be used for all calls to the transformer and that is used
* to call listeners on.
*
* <p>The default value is the Looper of the thread that this builder was created on, or if that
* thread does not have a Looper, the Looper of the application's main thread.
*
* @param looper A {@link Looper}.
* @return This builder.
*/
@CanIgnoreReturnValue
public Builder setLooper(Looper looper) {
this.looper = looper;
this.listeners = listeners.copy(looper, (listener, flags) -> {});
return this;
}
/**
* Sets a provider for views to show diagnostic information (if available) during * Sets a provider for views to show diagnostic information (if available) during
* transformation. * transformation.
* *
...@@ -434,6 +434,12 @@ public final class Transformer { ...@@ -434,6 +434,12 @@ public final class Transformer {
// TODO(huangdarwin): Remove this checkNotNull after deprecated {@link #setContext(Context)} // TODO(huangdarwin): Remove this checkNotNull after deprecated {@link #setContext(Context)}
// is removed. // is removed.
checkNotNull(context); checkNotNull(context);
if (transformationRequest.audioMimeType != null) {
checkSampleMimeType(transformationRequest.audioMimeType);
}
if (transformationRequest.videoMimeType != null) {
checkSampleMimeType(transformationRequest.videoMimeType);
}
if (mediaSourceFactory == null) { if (mediaSourceFactory == null) {
DefaultExtractorsFactory defaultExtractorsFactory = new DefaultExtractorsFactory(); DefaultExtractorsFactory defaultExtractorsFactory = new DefaultExtractorsFactory();
if (transformationRequest.flattenForSlowMotion) { if (transformationRequest.flattenForSlowMotion) {
...@@ -441,27 +447,21 @@ public final class Transformer { ...@@ -441,27 +447,21 @@ public final class Transformer {
} }
mediaSourceFactory = new DefaultMediaSourceFactory(context, defaultExtractorsFactory); mediaSourceFactory = new DefaultMediaSourceFactory(context, defaultExtractorsFactory);
} }
if (transformationRequest.audioMimeType != null) {
checkSampleMimeType(transformationRequest.audioMimeType);
}
if (transformationRequest.videoMimeType != null) {
checkSampleMimeType(transformationRequest.videoMimeType);
}
return new Transformer( return new Transformer(
context, context,
mediaSourceFactory,
muxerFactory,
removeAudio,
removeVideo,
transformationRequest, transformationRequest,
videoEffects, videoEffects,
frameProcessorFactory, removeAudio,
removeVideo,
listeners, listeners,
looper, mediaSourceFactory,
clock,
encoderFactory,
decoderFactory, decoderFactory,
debugViewProvider); encoderFactory,
frameProcessorFactory,
muxerFactory,
looper,
debugViewProvider,
clock);
} }
private void checkSampleMimeType(String sampleMimeType) { private void checkSampleMimeType(String sampleMimeType) {
...@@ -556,20 +556,21 @@ public final class Transformer { ...@@ -556,20 +556,21 @@ public final class Transformer {
/** Indicates that there is no current transformation. */ /** Indicates that there is no current transformation. */
public static final int PROGRESS_STATE_NO_TRANSFORMATION = 4; public static final int PROGRESS_STATE_NO_TRANSFORMATION = 4;
@VisibleForTesting /* package */ final Codec.DecoderFactory decoderFactory;
@VisibleForTesting /* package */ final Codec.EncoderFactory encoderFactory;
private final Context context; private final Context context;
private final MediaSource.Factory mediaSourceFactory;
private final Muxer.Factory muxerFactory;
private final boolean removeAudio;
private final boolean removeVideo;
private final TransformationRequest transformationRequest; private final TransformationRequest transformationRequest;
private final ImmutableList<Effect> videoEffects; private final ImmutableList<Effect> videoEffects;
private final boolean removeAudio;
private final boolean removeVideo;
private final ListenerSet<Transformer.Listener> listeners;
private final MediaSource.Factory mediaSourceFactory;
private final FrameProcessor.Factory frameProcessorFactory; private final FrameProcessor.Factory frameProcessorFactory;
private final Muxer.Factory muxerFactory;
private final Looper looper; private final Looper looper;
private final Clock clock;
private final DebugViewProvider debugViewProvider; private final DebugViewProvider debugViewProvider;
private final ListenerSet<Transformer.Listener> listeners; private final Clock clock;
@VisibleForTesting /* package */ final Codec.DecoderFactory decoderFactory;
@VisibleForTesting /* package */ final Codec.EncoderFactory encoderFactory;
@Nullable private MuxerWrapper muxerWrapper; @Nullable private MuxerWrapper muxerWrapper;
@Nullable private ExoPlayer player; @Nullable private ExoPlayer player;
...@@ -580,34 +581,34 @@ public final class Transformer { ...@@ -580,34 +581,34 @@ public final class Transformer {
private Transformer( private Transformer(
Context context, Context context,
MediaSource.Factory mediaSourceFactory,
Muxer.Factory muxerFactory,
boolean removeAudio,
boolean removeVideo,
TransformationRequest transformationRequest, TransformationRequest transformationRequest,
ImmutableList<Effect> videoEffects, ImmutableList<Effect> videoEffects,
FrameProcessor.Factory frameProcessorFactory, boolean removeAudio,
boolean removeVideo,
ListenerSet<Transformer.Listener> listeners, ListenerSet<Transformer.Listener> listeners,
Looper looper, MediaSource.Factory mediaSourceFactory,
Clock clock,
Codec.EncoderFactory encoderFactory,
Codec.DecoderFactory decoderFactory, Codec.DecoderFactory decoderFactory,
DebugViewProvider debugViewProvider) { Codec.EncoderFactory encoderFactory,
FrameProcessor.Factory frameProcessorFactory,
Muxer.Factory muxerFactory,
Looper looper,
DebugViewProvider debugViewProvider,
Clock clock) {
checkState(!removeAudio || !removeVideo, "Audio and video cannot both be removed."); checkState(!removeAudio || !removeVideo, "Audio and video cannot both be removed.");
this.context = context; this.context = context;
this.mediaSourceFactory = mediaSourceFactory;
this.muxerFactory = muxerFactory;
this.removeAudio = removeAudio;
this.removeVideo = removeVideo;
this.transformationRequest = transformationRequest; this.transformationRequest = transformationRequest;
this.videoEffects = videoEffects; this.videoEffects = videoEffects;
this.frameProcessorFactory = frameProcessorFactory; this.removeAudio = removeAudio;
this.removeVideo = removeVideo;
this.listeners = listeners; this.listeners = listeners;
this.looper = looper; this.mediaSourceFactory = mediaSourceFactory;
this.clock = clock;
this.encoderFactory = encoderFactory;
this.decoderFactory = decoderFactory; this.decoderFactory = decoderFactory;
this.encoderFactory = encoderFactory;
this.frameProcessorFactory = frameProcessorFactory;
this.muxerFactory = muxerFactory;
this.looper = looper;
this.debugViewProvider = debugViewProvider; this.debugViewProvider = debugViewProvider;
this.clock = clock;
progressState = PROGRESS_STATE_NO_TRANSFORMATION; progressState = PROGRESS_STATE_NO_TRANSFORMATION;
} }
...@@ -742,8 +743,7 @@ public final class Transformer { ...@@ -742,8 +743,7 @@ public final class Transformer {
DEFAULT_BUFFER_FOR_PLAYBACK_MS / 10, DEFAULT_BUFFER_FOR_PLAYBACK_MS / 10,
DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS / 10) DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS / 10)
.build(); .build();
TransformerPlayerListener playerListener = TransformerPlayerListener playerListener = new TransformerPlayerListener(mediaItem, looper);
new TransformerPlayerListener(mediaItem, muxerWrapper, looper);
ExoPlayer.Builder playerBuilder = ExoPlayer.Builder playerBuilder =
new ExoPlayer.Builder( new ExoPlayer.Builder(
context, context,
...@@ -973,13 +973,10 @@ public final class Transformer { ...@@ -973,13 +973,10 @@ public final class Transformer {
private final class TransformerPlayerListener implements Player.Listener, AsyncErrorListener { private final class TransformerPlayerListener implements Player.Listener, AsyncErrorListener {
private final MediaItem mediaItem; private final MediaItem mediaItem;
private final MuxerWrapper muxerWrapper;
private final Handler handler; private final Handler handler;
public TransformerPlayerListener( public TransformerPlayerListener(MediaItem mediaItem, Looper looper) {
MediaItem mediaItem, MuxerWrapper muxerWrapper, Looper looper) {
this.mediaItem = mediaItem; this.mediaItem = mediaItem;
this.muxerWrapper = muxerWrapper;
handler = new Handler(looper); handler = new Handler(looper);
} }
...@@ -1012,7 +1009,7 @@ public final class Transformer { ...@@ -1012,7 +1009,7 @@ public final class Transformer {
@Override @Override
public void onTracksChanged(Tracks tracks) { public void onTracksChanged(Tracks tracks) {
if (muxerWrapper.getTrackCount() == 0) { if (checkNotNull(muxerWrapper).getTrackCount() == 0) {
handleTransformationEnded( handleTransformationEnded(
TransformationException.createForUnexpected( TransformationException.createForUnexpected(
new IllegalStateException("The output does not contain any tracks."))); new IllegalStateException("The output does not contain any tracks.")));
...@@ -1039,6 +1036,7 @@ public final class Transformer { ...@@ -1039,6 +1036,7 @@ public final class Transformer {
} }
private void handleTransformationEnded(@Nullable TransformationException exception) { private void handleTransformationEnded(@Nullable TransformationException exception) {
MuxerWrapper muxerWrapper = Transformer.this.muxerWrapper;
@Nullable TransformationException resourceReleaseException = null; @Nullable TransformationException resourceReleaseException = null;
try { try {
releaseResources(/* forCancellation= */ false); releaseResources(/* forCancellation= */ false);
...@@ -1064,7 +1062,7 @@ public final class Transformer { ...@@ -1064,7 +1062,7 @@ public final class Transformer {
} else { } else {
TransformationResult result = TransformationResult result =
new TransformationResult.Builder() new TransformationResult.Builder()
.setDurationMs(muxerWrapper.getDurationMs()) .setDurationMs(checkNotNull(muxerWrapper).getDurationMs())
.setAverageAudioBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_AUDIO)) .setAverageAudioBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_AUDIO))
.setAverageVideoBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_VIDEO)) .setAverageVideoBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_VIDEO))
.setVideoFrameCount(muxerWrapper.getTrackSampleCount(C.TRACK_TYPE_VIDEO)) .setVideoFrameCount(muxerWrapper.getTrackSampleCount(C.TRACK_TYPE_VIDEO))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment