From 18a15fb9959efeebada8d50b4b88917387708883 Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Wed, 17 Nov 2021 16:08:53 +0000 Subject: [PATCH] Transformer: Rename setOutputMimeType() to setContainerMimeType(). This mime type is technically for the Muxer, and determines the container used. In the context of the transformer, this can be thought of more as a container mime type, to avoid confusion with the video mime type and audio mime type. Deprecates setOutputMimeType(). PiperOrigin-RevId: 410530707 --- docs/transforming-media.md | 2 +- .../transformer/TranscodingTransformer.java | 49 +++++++++++++------ .../transformer/Transformation.java | 6 +-- .../exoplayer2/transformer/Transformer.java | 45 ++++++++++++----- .../transformer/TransformerBuilderTest.java | 4 +- 5 files changed, 72 insertions(+), 34 deletions(-) diff --git a/docs/transforming-media.md b/docs/transforming-media.md index 84dba92fe3..4d180260dd 100644 --- a/docs/transforming-media.md +++ b/docs/transforming-media.md @@ -37,7 +37,7 @@ Transformer transformer = new Transformer.Builder() .setContext(context) .setRemoveAudio(true) - .setOutputMimeType(MimeTypes.VIDEO_WEBM) + .setContainerMimeType(MimeTypes.VIDEO_WEBM) .setListener(transformerListener) .build(); // Start the transformation. diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TranscodingTransformer.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TranscodingTransformer.java index 131d72ce5a..9e0b1d45f6 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TranscodingTransformer.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TranscodingTransformer.java @@ -100,7 +100,7 @@ public final class TranscodingTransformer { private boolean removeVideo; private boolean flattenForSlowMotion; private int outputHeight; - private String outputMimeType; + private String containerMimeType; @Nullable private String audioMimeType; @Nullable private String videoMimeType; private TranscodingTransformer.Listener listener; @@ -111,7 +111,7 @@ public final class TranscodingTransformer { public Builder() { muxerFactory = new FrameworkMuxer.Factory(); outputHeight = Transformation.NO_VALUE; - outputMimeType = MimeTypes.VIDEO_MP4; + containerMimeType = MimeTypes.VIDEO_MP4; listener = new Listener() {}; looper = Util.getCurrentOrMainLooper(); clock = Clock.DEFAULT; @@ -126,7 +126,7 @@ public final class TranscodingTransformer { this.removeVideo = transcodingTransformer.transformation.removeVideo; this.flattenForSlowMotion = transcodingTransformer.transformation.flattenForSlowMotion; this.outputHeight = transcodingTransformer.transformation.outputHeight; - this.outputMimeType = transcodingTransformer.transformation.outputMimeType; + this.containerMimeType = transcodingTransformer.transformation.containerMimeType; this.audioMimeType = transcodingTransformer.transformation.audioMimeType; this.videoMimeType = transcodingTransformer.transformation.videoMimeType; this.listener = transcodingTransformer.listener; @@ -258,11 +258,30 @@ public final class TranscodingTransformer { *
  • {@link MimeTypes#VIDEO_WEBM} from API level 21 * * - * @param outputMimeType The MIME type of the output. + * @param outputMimeType The MIME type of the container. + * @return This builder. + * @deprecated Use {@link #setContainerMimeType} instead. + */ + @Deprecated + public Builder setOutputMimeType(String outputMimeType) { + this.containerMimeType = outputMimeType; + return this; + } + + /** + * Sets the MIME type of the output container. The default value is {@link MimeTypes#VIDEO_MP4}. + * Supported values are: + * + * + * + * @param containerMimeType The MIME type of the container. * @return This builder. */ - public Builder setOutputMimeType(String outputMimeType) { - this.outputMimeType = outputMimeType; + public Builder setContainerMimeType(String containerMimeType) { + this.containerMimeType = containerMimeType; return this; } @@ -377,7 +396,7 @@ public final class TranscodingTransformer { * @throws IllegalStateException If the {@link Context} has not been provided. * @throws IllegalStateException If both audio and video have been removed (otherwise the output * would not contain any samples). - * @throws IllegalStateException If the muxer doesn't support the requested output MIME type. + * @throws IllegalStateException If the muxer doesn't support the requested container MIME type. * @throws IllegalStateException If the muxer doesn't support the requested audio MIME type. */ public TranscodingTransformer build() { @@ -390,8 +409,8 @@ public final class TranscodingTransformer { mediaSourceFactory = new DefaultMediaSourceFactory(context, defaultExtractorsFactory); } checkState( - muxerFactory.supportsOutputMimeType(outputMimeType), - "Unsupported output MIME type: " + outputMimeType); + muxerFactory.supportsOutputMimeType(containerMimeType), + "Unsupported container MIME type: " + containerMimeType); if (audioMimeType != null) { checkSampleMimeType(audioMimeType); } @@ -404,7 +423,7 @@ public final class TranscodingTransformer { removeVideo, flattenForSlowMotion, outputHeight, - outputMimeType, + containerMimeType, audioMimeType, videoMimeType); return new TranscodingTransformer( @@ -413,11 +432,11 @@ public final class TranscodingTransformer { private void checkSampleMimeType(String sampleMimeType) { checkState( - muxerFactory.supportsSampleMimeType(sampleMimeType, outputMimeType), + muxerFactory.supportsSampleMimeType(sampleMimeType, containerMimeType), "Unsupported sample MIME type " + sampleMimeType + " for container MIME type " - + outputMimeType); + + containerMimeType); } } @@ -540,7 +559,7 @@ public final class TranscodingTransformer { * @throws IOException If an error occurs opening the output file for writing. */ public void startTransformation(MediaItem mediaItem, String path) throws IOException { - startTransformation(mediaItem, muxerFactory.create(path, transformation.outputMimeType)); + startTransformation(mediaItem, muxerFactory.create(path, transformation.containerMimeType)); } /** @@ -571,7 +590,7 @@ public final class TranscodingTransformer { public void startTransformation(MediaItem mediaItem, ParcelFileDescriptor parcelFileDescriptor) throws IOException { startTransformation( - mediaItem, muxerFactory.create(parcelFileDescriptor, transformation.outputMimeType)); + mediaItem, muxerFactory.create(parcelFileDescriptor, transformation.containerMimeType)); } private void startTransformation(MediaItem mediaItem, Muxer muxer) { @@ -581,7 +600,7 @@ public final class TranscodingTransformer { } MuxerWrapper muxerWrapper = - new MuxerWrapper(muxer, muxerFactory, transformation.outputMimeType); + new MuxerWrapper(muxer, muxerFactory, transformation.containerMimeType); this.muxerWrapper = muxerWrapper; DefaultTrackSelector trackSelector = new DefaultTrackSelector(context); trackSelector.setParameters( diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformation.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformation.java index e08a1c39b4..b1baee9001 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformation.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformation.java @@ -28,7 +28,7 @@ import androidx.annotation.Nullable; public final boolean removeVideo; public final boolean flattenForSlowMotion; public final int outputHeight; - public final String outputMimeType; + public final String containerMimeType; @Nullable public final String audioMimeType; @Nullable public final String videoMimeType; @@ -37,14 +37,14 @@ import androidx.annotation.Nullable; boolean removeVideo, boolean flattenForSlowMotion, int outputHeight, - String outputMimeType, + String containerMimeType, @Nullable String audioMimeType, @Nullable String videoMimeType) { this.removeAudio = removeAudio; this.removeVideo = removeVideo; this.flattenForSlowMotion = flattenForSlowMotion; this.outputHeight = outputHeight; - this.outputMimeType = outputMimeType; + this.containerMimeType = containerMimeType; this.audioMimeType = audioMimeType; this.videoMimeType = videoMimeType; } diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformer.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformer.java index 4e906480ff..c5d9f5ec95 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformer.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/Transformer.java @@ -95,7 +95,7 @@ public final class Transformer { private boolean removeAudio; private boolean removeVideo; private boolean flattenForSlowMotion; - private String outputMimeType; + private String containerMimeType; private Transformer.Listener listener; private Looper looper; private Clock clock; @@ -103,7 +103,7 @@ public final class Transformer { /** Creates a builder with default values. */ public Builder() { muxerFactory = new FrameworkMuxer.Factory(); - outputMimeType = MimeTypes.VIDEO_MP4; + containerMimeType = MimeTypes.VIDEO_MP4; listener = new Listener() {}; looper = Util.getCurrentOrMainLooper(); clock = Clock.DEFAULT; @@ -117,7 +117,7 @@ public final class Transformer { this.removeAudio = transformer.transformation.removeAudio; this.removeVideo = transformer.transformation.removeVideo; this.flattenForSlowMotion = transformer.transformation.flattenForSlowMotion; - this.outputMimeType = transformer.transformation.outputMimeType; + this.containerMimeType = transformer.transformation.containerMimeType; this.listener = transformer.listener; this.looper = transformer.looper; this.clock = transformer.clock; @@ -216,11 +216,30 @@ public final class Transformer { *
  • {@link MimeTypes#VIDEO_WEBM} from API level 21 * * - * @param outputMimeType The MIME type of the output. + * @param outputMimeType The MIME type of the container. + * @return This builder. + * @deprecated Use {@link #setContainerMimeType} instead. + */ + @Deprecated + public Builder setOutputMimeType(String outputMimeType) { + this.containerMimeType = outputMimeType; + return this; + } + + /** + * Sets the MIME type of the output container. The default value is {@link MimeTypes#VIDEO_MP4}. + * Supported values are: + * + * + * + * @param containerMimeType The MIME type of the output. * @return This builder. */ - public Builder setOutputMimeType(String outputMimeType) { - this.outputMimeType = outputMimeType; + public Builder setContainerMimeType(String containerMimeType) { + this.containerMimeType = containerMimeType; return this; } @@ -283,7 +302,7 @@ public final class Transformer { * @throws IllegalStateException If the {@link Context} has not been provided. * @throws IllegalStateException If both audio and video have been removed (otherwise the output * would not contain any samples). - * @throws IllegalStateException If the muxer doesn't support the requested output MIME type. + * @throws IllegalStateException If the muxer doesn't support the requested container MIME type. */ public Transformer build() { checkStateNotNull(context); @@ -295,15 +314,15 @@ public final class Transformer { mediaSourceFactory = new DefaultMediaSourceFactory(context, defaultExtractorsFactory); } checkState( - muxerFactory.supportsOutputMimeType(outputMimeType), - "Unsupported output MIME type: " + outputMimeType); + muxerFactory.supportsOutputMimeType(containerMimeType), + "Unsupported container MIME type: " + containerMimeType); Transformation transformation = new Transformation( removeAudio, removeVideo, flattenForSlowMotion, /* outputHeight= */ Transformation.NO_VALUE, - outputMimeType, + containerMimeType, /* audioMimeType= */ null, /* videoMimeType= */ null); return new Transformer( @@ -428,7 +447,7 @@ public final class Transformer { * @throws IOException If an error occurs opening the output file for writing. */ public void startTransformation(MediaItem mediaItem, String path) throws IOException { - startTransformation(mediaItem, muxerFactory.create(path, transformation.outputMimeType)); + startTransformation(mediaItem, muxerFactory.create(path, transformation.containerMimeType)); } /** @@ -459,7 +478,7 @@ public final class Transformer { public void startTransformation(MediaItem mediaItem, ParcelFileDescriptor parcelFileDescriptor) throws IOException { startTransformation( - mediaItem, muxerFactory.create(parcelFileDescriptor, transformation.outputMimeType)); + mediaItem, muxerFactory.create(parcelFileDescriptor, transformation.containerMimeType)); } private void startTransformation(MediaItem mediaItem, Muxer muxer) { @@ -469,7 +488,7 @@ public final class Transformer { } MuxerWrapper muxerWrapper = - new MuxerWrapper(muxer, muxerFactory, transformation.outputMimeType); + new MuxerWrapper(muxer, muxerFactory, transformation.containerMimeType); this.muxerWrapper = muxerWrapper; DefaultTrackSelector trackSelector = new DefaultTrackSelector(context); trackSelector.setParameters( diff --git a/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/TransformerBuilderTest.java b/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/TransformerBuilderTest.java index 8cfba3156d..73454b27ae 100644 --- a/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/TransformerBuilderTest.java +++ b/library/transformer/src/test/java/com/google/android/exoplayer2/transformer/TransformerBuilderTest.java @@ -30,10 +30,10 @@ import org.junit.runner.RunWith; public class TransformerBuilderTest { @Test - public void setOutputMimeType_unsupportedMimeType_throws() { + public void setContainerMimeType_unsupportedMimeType_throws() { assertThrows( IllegalStateException.class, - () -> new Transformer.Builder().setOutputMimeType(MimeTypes.VIDEO_FLV).build()); + () -> new Transformer.Builder().setContainerMimeType(MimeTypes.VIDEO_FLV).build()); } @Test