Skip to content

Commit

Permalink
Remove deprecated setOutputMimeType
Browse files Browse the repository at this point in the history
This is to prepare Muxer to become public

PiperOrigin-RevId: 481893842
(cherry picked from commit bd9181e)
  • Loading branch information
kim-vde authored and microkatz committed Oct 18, 2022
1 parent 5de37d2 commit 66d56be
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,18 @@ public Factory() {
}

@Override
public Muxer create(String path, String outputMimeType) throws IOException {
return new DefaultMuxer(muxerFactory.create(path, outputMimeType));
public Muxer create(String path) throws IOException {
return new DefaultMuxer(muxerFactory.create(path));
}

@Override
public Muxer create(ParcelFileDescriptor parcelFileDescriptor, String outputMimeType)
throws IOException {
return new DefaultMuxer(muxerFactory.create(parcelFileDescriptor, outputMimeType));
public Muxer create(ParcelFileDescriptor parcelFileDescriptor) throws IOException {
return new DefaultMuxer(muxerFactory.create(parcelFileDescriptor));
}

@Override
public boolean supportsOutputMimeType(String mimeType) {
return muxerFactory.supportsOutputMimeType(mimeType);
}

@Override
public ImmutableList<String> getSupportedSampleMimeTypes(
@C.TrackType int trackType, String containerMimeType) {
return muxerFactory.getSupportedSampleMimeTypes(trackType, containerMimeType);
public ImmutableList<String> getSupportedSampleMimeTypes(@C.TrackType int trackType) {
return muxerFactory.getSupportedSampleMimeTypes(trackType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
Expand All @@ -42,70 +41,42 @@
/* package */ final class FrameworkMuxer implements Muxer {

// MediaMuxer supported sample formats are documented in MediaMuxer.addTrack(MediaFormat).
private static final ImmutableMap<String, ImmutableList<String>>
SUPPORTED_CONTAINER_TO_VIDEO_SAMPLE_MIME_TYPES =
ImmutableMap.of(
MimeTypes.VIDEO_MP4,
Util.SDK_INT >= 24
? ImmutableList.of(
MimeTypes.VIDEO_H263,
MimeTypes.VIDEO_H264,
MimeTypes.VIDEO_MP4V,
MimeTypes.VIDEO_H265)
: ImmutableList.of(
MimeTypes.VIDEO_H263, MimeTypes.VIDEO_H264, MimeTypes.VIDEO_MP4V),
MimeTypes.VIDEO_WEBM,
Util.SDK_INT >= 24
? ImmutableList.of(MimeTypes.VIDEO_VP8, MimeTypes.VIDEO_VP9)
: ImmutableList.of(MimeTypes.VIDEO_VP8));

private static final ImmutableMap<String, ImmutableList<String>>
SUPPORTED_CONTAINER_TO_AUDIO_SAMPLE_MIME_TYPES =
ImmutableMap.of(
MimeTypes.VIDEO_MP4,
ImmutableList.of(MimeTypes.AUDIO_AAC, MimeTypes.AUDIO_AMR_NB, MimeTypes.AUDIO_AMR_WB),
MimeTypes.VIDEO_WEBM,
ImmutableList.of(MimeTypes.AUDIO_VORBIS));
private static final ImmutableList<String> SUPPORTED_VIDEO_SAMPLE_MIME_TYPES =
Util.SDK_INT >= 24
? ImmutableList.of(
MimeTypes.VIDEO_H263,
MimeTypes.VIDEO_H264,
MimeTypes.VIDEO_MP4V,
MimeTypes.VIDEO_H265)
: ImmutableList.of(MimeTypes.VIDEO_H263, MimeTypes.VIDEO_H264, MimeTypes.VIDEO_MP4V);

private static final ImmutableList<String> SUPPORTED_AUDIO_SAMPLE_MIME_TYPES =
ImmutableList.of(MimeTypes.AUDIO_AAC, MimeTypes.AUDIO_AMR_NB, MimeTypes.AUDIO_AMR_WB);

/** {@link Muxer.Factory} for {@link FrameworkMuxer}. */
public static final class Factory implements Muxer.Factory {
@Override
public FrameworkMuxer create(String path, String outputMimeType) throws IOException {
MediaMuxer mediaMuxer = new MediaMuxer(path, mimeTypeToMuxerOutputFormat(outputMimeType));
public FrameworkMuxer create(String path) throws IOException {
MediaMuxer mediaMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
return new FrameworkMuxer(mediaMuxer);
}

@RequiresApi(26)
@Override
public FrameworkMuxer create(ParcelFileDescriptor parcelFileDescriptor, String outputMimeType)
throws IOException {
public FrameworkMuxer create(ParcelFileDescriptor parcelFileDescriptor) throws IOException {
MediaMuxer mediaMuxer =
new MediaMuxer(
parcelFileDescriptor.getFileDescriptor(),
mimeTypeToMuxerOutputFormat(outputMimeType));
MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
return new FrameworkMuxer(mediaMuxer);
}

@Override
public boolean supportsOutputMimeType(String mimeType) {
try {
mimeTypeToMuxerOutputFormat(mimeType);
} catch (IllegalArgumentException e) {
return false;
}
return true;
}

@Override
public ImmutableList<String> getSupportedSampleMimeTypes(
@C.TrackType int trackType, String containerMimeType) {
// MediaMuxer supported sample formats are documented in MediaMuxer.addTrack(MediaFormat).
public ImmutableList<String> getSupportedSampleMimeTypes(@C.TrackType int trackType) {
if (trackType == C.TRACK_TYPE_VIDEO) {
return SUPPORTED_CONTAINER_TO_VIDEO_SAMPLE_MIME_TYPES.getOrDefault(
containerMimeType, ImmutableList.of());
return SUPPORTED_VIDEO_SAMPLE_MIME_TYPES;
} else if (trackType == C.TRACK_TYPE_AUDIO) {
return SUPPORTED_CONTAINER_TO_AUDIO_SAMPLE_MIME_TYPES.getOrDefault(
containerMimeType, ImmutableList.of());
return SUPPORTED_AUDIO_SAMPLE_MIME_TYPES;
}
return ImmutableList.of();
}
Expand Down Expand Up @@ -212,25 +183,6 @@ public void release(boolean forCancellation) throws MuxerException {
}
}

/**
* Converts a {@linkplain MimeTypes MIME type} into a {@linkplain MediaMuxer.OutputFormat
* MediaMuxer output format}.
*
* @param mimeType The {@linkplain MimeTypes MIME type} to convert.
* @return The corresponding {@linkplain MediaMuxer.OutputFormat MediaMuxer output format}.
* @throws IllegalArgumentException If the {@linkplain MimeTypes MIME type} is not supported as
* output format.
*/
private static int mimeTypeToMuxerOutputFormat(String mimeType) {
if (mimeType.equals(MimeTypes.VIDEO_MP4)) {
return MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4;
} else if (SDK_INT >= 21 && mimeType.equals(MimeTypes.VIDEO_WEBM)) {
return MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM;
} else {
throw new IllegalArgumentException("Unsupported output MIME type: " + mimeType);
}
}

// Accesses MediaMuxer state via reflection to ensure that muxer resources can be released even
// if stopping fails.
@SuppressLint("PrivateApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
/**
* Abstracts media muxing operations.
*
* <p>Query whether {@linkplain Factory#supportsOutputMimeType(String) container MIME type} and
* {@linkplain Factory#getSupportedSampleMimeTypes(int, String)} sample MIME types} are supported
* and {@linkplain #addTrack(Format) add all tracks}, then {@linkplain #writeSampleData(int,
* ByteBuffer, boolean, long) write sample data} to mux samples. Once any sample data has been
* written, it is not possible to add tracks. After writing all sample data, {@linkplain
* #release(boolean) release} the instance to finish writing to the output and return any resources
* to the system.
* <p>Query whether {@linkplain Factory#getSupportedSampleMimeTypes(int)} sample MIME types} are
* supported and {@linkplain #addTrack(Format) add all tracks}, then {@linkplain
* #writeSampleData(int, ByteBuffer, boolean, long) write sample data} to mux samples. Once any
* sample data has been written, it is not possible to add tracks. After writing all sample data,
* {@linkplain #release(boolean) release} the instance to finish writing to the output and return
* any resources to the system.
*/
/* package */ interface Muxer {

Expand All @@ -55,11 +54,10 @@ interface Factory {
* Returns a new muxer writing to a file.
*
* @param path The path to the output file.
* @param outputMimeType The container {@linkplain MimeTypes MIME type} of the output file.
* @throws IllegalArgumentException If the path is invalid or the MIME type is not supported.
* @throws IllegalArgumentException If the path is invalid.
* @throws IOException If an error occurs opening the output file for writing.
*/
Muxer create(String path, String outputMimeType) throws IOException;
Muxer create(String path) throws IOException;

/**
* Returns a new muxer writing to a file descriptor.
Expand All @@ -68,25 +66,16 @@ interface Factory {
* output. The file referenced by this ParcelFileDescriptor should not be used before the
* muxer is released. It is the responsibility of the caller to close the
* ParcelFileDescriptor. This can be done after this method returns.
* @param outputMimeType The {@linkplain MimeTypes MIME type} of the output.
* @throws IllegalArgumentException If the file descriptor is invalid or the MIME type is not
* supported.
* @throws IllegalArgumentException If the file descriptor is invalid.
* @throws IOException If an error occurs opening the output file descriptor for writing.
*/
Muxer create(ParcelFileDescriptor parcelFileDescriptor, String outputMimeType)
throws IOException;

/**
* Returns whether the {@linkplain MimeTypes MIME type} provided is a supported output format.
*/
boolean supportsOutputMimeType(String mimeType);
Muxer create(ParcelFileDescriptor parcelFileDescriptor) throws IOException;

/**
* Returns the supported sample {@linkplain MimeTypes MIME types} for the given {@link
* C.TrackType} and container {@linkplain MimeTypes MIME type}.
* C.TrackType}.
*/
ImmutableList<String> getSupportedSampleMimeTypes(
@C.TrackType int trackType, String containerMimeType);
ImmutableList<String> getSupportedSampleMimeTypes(@C.TrackType int trackType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,16 @@
private final SparseIntArray trackTypeToSampleCount;
private final SparseLongArray trackTypeToTimeUs;
private final SparseLongArray trackTypeToBytesWritten;
private final String containerMimeType;

private int trackCount;
private int trackFormatCount;
private boolean isReady;
private @C.TrackType int previousTrackType;
private long minTrackTimeUs;

public MuxerWrapper(Muxer muxer, Muxer.Factory muxerFactory, String containerMimeType) {
public MuxerWrapper(Muxer muxer, Muxer.Factory muxerFactory) {
this.muxer = muxer;
this.muxerFactory = muxerFactory;
this.containerMimeType = containerMimeType;

trackTypeToIndex = new SparseIntArray();
trackTypeToSampleCount = new SparseIntArray();
Expand Down Expand Up @@ -97,7 +95,7 @@ public boolean supportsSampleMimeType(@Nullable String mimeType) {
* track type}.
*/
public ImmutableList<String> getSupportedSampleMimeTypes(@C.TrackType int trackType) {
return muxerFactory.getSupportedSampleMimeTypes(trackType, containerMimeType);
return muxerFactory.getSupportedSampleMimeTypes(trackType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public static final class Builder {
private Muxer.Factory muxerFactory;
private boolean removeAudio;
private boolean removeVideo;
private String containerMimeType;
private TransformationRequest transformationRequest;
private ImmutableList<Effect> videoEffects;
private FrameProcessor.Factory frameProcessorFactory;
Expand All @@ -131,7 +130,6 @@ public Builder(Context context) {
encoderFactory = new DefaultEncoderFactory.Builder(this.context).build();
decoderFactory = new DefaultDecoderFactory(this.context);
debugViewProvider = DebugViewProvider.NONE;
containerMimeType = MimeTypes.VIDEO_MP4;
transformationRequest = new TransformationRequest.Builder().build();
videoEffects = ImmutableList.of();
frameProcessorFactory = new GlEffectsFrameProcessor.Factory();
Expand All @@ -144,7 +142,6 @@ private Builder(Transformer transformer) {
this.muxerFactory = transformer.muxerFactory;
this.removeAudio = transformer.removeAudio;
this.removeVideo = transformer.removeVideo;
this.containerMimeType = transformer.containerMimeType;
this.transformationRequest = transformer.transformationRequest;
this.videoEffects = transformer.videoEffects;
this.frameProcessorFactory = transformer.frameProcessorFactory;
Expand Down Expand Up @@ -276,17 +273,6 @@ public Builder setFlattenForSlowMotion(boolean flattenForSlowMotion) {
return this;
}

/**
* @deprecated This feature will be removed in a following release and the MIME type of the
* output will always be MP4.
*/
@CanIgnoreReturnValue
@Deprecated
public Builder setOutputMimeType(String outputMimeType) {
this.containerMimeType = outputMimeType;
return this;
}

/**
* @deprecated Use {@link #addListener(Listener)}, {@link #removeListener(Listener)} or {@link
* #removeAllListeners()} instead.
Expand Down Expand Up @@ -454,9 +440,6 @@ public Transformer build() {
}
mediaSourceFactory = new DefaultMediaSourceFactory(context, defaultExtractorsFactory);
}
checkState(
muxerFactory.supportsOutputMimeType(containerMimeType),
"Unsupported container MIME type: " + containerMimeType);
if (transformationRequest.audioMimeType != null) {
checkSampleMimeType(transformationRequest.audioMimeType);
}
Expand All @@ -469,7 +452,6 @@ public Transformer build() {
muxerFactory,
removeAudio,
removeVideo,
containerMimeType,
transformationRequest,
videoEffects,
frameProcessorFactory,
Expand All @@ -484,13 +466,9 @@ public Transformer build() {
private void checkSampleMimeType(String sampleMimeType) {
checkState(
muxerFactory
.getSupportedSampleMimeTypes(
MimeTypes.getTrackType(sampleMimeType), containerMimeType)
.getSupportedSampleMimeTypes(MimeTypes.getTrackType(sampleMimeType))
.contains(sampleMimeType),
"Unsupported sample MIME type "
+ sampleMimeType
+ " for container MIME type "
+ containerMimeType);
"Unsupported sample MIME type " + sampleMimeType);
}
}

Expand Down Expand Up @@ -582,7 +560,6 @@ default void onFallbackApplied(
private final Muxer.Factory muxerFactory;
private final boolean removeAudio;
private final boolean removeVideo;
private final String containerMimeType;
private final TransformationRequest transformationRequest;
private final ImmutableList<Effect> videoEffects;
private final FrameProcessor.Factory frameProcessorFactory;
Expand All @@ -606,7 +583,6 @@ private Transformer(
Muxer.Factory muxerFactory,
boolean removeAudio,
boolean removeVideo,
String containerMimeType,
TransformationRequest transformationRequest,
ImmutableList<Effect> videoEffects,
FrameProcessor.Factory frameProcessorFactory,
Expand All @@ -622,7 +598,6 @@ private Transformer(
this.muxerFactory = muxerFactory;
this.removeAudio = removeAudio;
this.removeVideo = removeVideo;
this.containerMimeType = containerMimeType;
this.transformationRequest = transformationRequest;
this.videoEffects = videoEffects;
this.frameProcessorFactory = frameProcessorFactory;
Expand Down Expand Up @@ -711,7 +686,7 @@ public void startTransformation(MediaItem mediaItem, String path) throws IOExcep
}
this.outputPath = path;
this.outputParcelFileDescriptor = null;
startTransformation(mediaItem, muxerFactory.create(path, containerMimeType));
startTransformation(mediaItem, muxerFactory.create(path));
}

/**
Expand Down Expand Up @@ -741,15 +716,15 @@ public void startTransformation(MediaItem mediaItem, ParcelFileDescriptor parcel
throws IOException {
this.outputParcelFileDescriptor = parcelFileDescriptor;
this.outputPath = null;
startTransformation(mediaItem, muxerFactory.create(parcelFileDescriptor, containerMimeType));
startTransformation(mediaItem, muxerFactory.create(parcelFileDescriptor));
}

private void startTransformation(MediaItem mediaItem, Muxer muxer) {
verifyApplicationThread();
if (player != null) {
throw new IllegalStateException("There is already a transformation in progress.");
}
MuxerWrapper muxerWrapper = new MuxerWrapper(muxer, muxerFactory, containerMimeType);
MuxerWrapper muxerWrapper = new MuxerWrapper(muxer, muxerFactory);
this.muxerWrapper = muxerWrapper;
DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
trackSelector.setParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public final class TestMuxer implements Muxer, Dumper.Dumpable {
private final List<Dumper.Dumpable> dumpables;

/** Creates a new test muxer. */
public TestMuxer(String path, String outputMimeType, Muxer.Factory muxerFactory)
throws IOException {
muxer = muxerFactory.create(path, outputMimeType);
public TestMuxer(String path, Muxer.Factory muxerFactory) throws IOException {
muxer = muxerFactory.create(path);
dumpables = new ArrayList<>();
dumpables.add(dumper -> dumper.add("containerMimeType", outputMimeType));
}

// Muxer implementation.
Expand Down
Loading

0 comments on commit 66d56be

Please sign in to comment.