Handle no supported encoder & muxer mime types in the Encoder factory.

PiperOrigin-RevId: 652825117
This commit is contained in:
samrobinson 2024-07-16 07:00:18 -07:00 committed by Copybara-Service
parent 99679645fc
commit 1c3fe20826
4 changed files with 30 additions and 22 deletions

View File

@ -78,6 +78,7 @@ import org.checkerframework.dataflow.qual.Pure;
.setCodecs(firstInputFormat.codecs)
.build();
// TODO - b/324426022: Move logic for supported mime types to DefaultEncoderFactory.
encoder =
encoderFactory.createForAudioEncoding(
requestedEncoderFormat

View File

@ -16,6 +16,7 @@
package androidx.media3.transformer;
import static androidx.media3.common.ColorInfo.isTransferHdr;
import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
@ -211,7 +212,9 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
if (format.bitrate == Format.NO_VALUE) {
format = format.buildUpon().setAverageBitrate(DEFAULT_AUDIO_BITRATE).build();
}
checkNotNull(format.sampleMimeType);
if (format.sampleMimeType == null) {
throw createNoSupportedMimeTypeException(format, /* isVideo= */ false);
}
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
ImmutableList<MediaCodecInfo> mediaCodecInfos =
EncoderUtil.getSupportedEncoders(format.sampleMimeType);
@ -240,13 +243,16 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
if (format.frameRate == Format.NO_VALUE || deviceNeedsDefaultFrameRateWorkaround()) {
format = format.buildUpon().setFrameRate(DEFAULT_FRAME_RATE).build();
}
if (format.sampleMimeType == null) {
throw createNoSupportedMimeTypeException(format, /* isVideo= */ true);
}
checkArgument(format.width != Format.NO_VALUE);
checkArgument(format.height != Format.NO_VALUE);
// According to interface Javadoc, format.rotationDegrees should be 0. The video should always
// be encoded in landscape orientation.
checkArgument(format.height <= format.width);
checkArgument(format.rotationDegrees == 0);
checkNotNull(format.sampleMimeType);
checkStateNotNull(videoEncoderSelector);
@Nullable
@ -704,6 +710,22 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
return (int) (width * height * frameRate * 0.07 * 2);
}
private static ExportException createNoSupportedMimeTypeException(
Format format, boolean isVideo) {
String errorMessage = "No MIME type is supported by both encoder and muxer.";
int errorCode = ExportException.ERROR_CODE_ENCODING_FORMAT_UNSUPPORTED;
if (isVideo && isTransferHdr(format.colorInfo)) {
errorMessage += " Requested HDR colorInfo: " + format.colorInfo;
}
return ExportException.createForCodec(
new IllegalArgumentException(errorMessage),
errorCode,
new ExportException.CodecInfo(
format.toString(), isVideo, /* isDecoder= */ false, /* name= */ null));
}
@RequiresNonNull("#1.sampleMimeType")
private static ExportException createExportException(Format format, String errorString) {
return ExportException.createForCodec(

View File

@ -162,11 +162,11 @@ import java.util.List;
* @param requestedFormat The {@link Format} requested.
* @param muxerSupportedMimeTypes The list of sample {@linkplain MimeTypes MIME types} that the
* muxer supports.
* @return A supported {@linkplain MimeTypes MIME type}.
* @throws ExportException If there are no supported {@linkplain MimeTypes MIME types}.
* @return A supported {@linkplain MimeTypes MIME type}, or {@code null} if none are supported.
*/
@Nullable
protected static String findSupportedMimeTypeForEncoderAndMuxer(
Format requestedFormat, List<String> muxerSupportedMimeTypes) throws ExportException {
Format requestedFormat, List<String> muxerSupportedMimeTypes) {
boolean isVideo = MimeTypes.isVideo(checkNotNull(requestedFormat.sampleMimeType));
ImmutableSet.Builder<String> mimeTypesToCheckSetBuilder =
@ -193,22 +193,6 @@ import java.util.List;
}
}
throw createNoSupportedMimeTypeException(requestedFormat);
}
private static ExportException createNoSupportedMimeTypeException(Format format) {
String errorMessage = "No MIME type is supported by both encoder and muxer.";
int errorCode = ExportException.ERROR_CODE_ENCODING_FORMAT_UNSUPPORTED;
boolean isVideo = MimeTypes.isVideo(format.sampleMimeType);
if (isVideo && isTransferHdr(format.colorInfo)) {
errorMessage += " Requested HDR colorInfo: " + format.colorInfo;
}
return ExportException.createForCodec(
new IllegalArgumentException(errorMessage),
errorCode,
new ExportException.CodecInfo(
format.toString(), isVideo, /* isDecoder= */ false, /* name= */ null));
return null;
}
}

View File

@ -315,6 +315,7 @@ import org.checkerframework.dataflow.qual.Pure;
.setCodecs(inputFormat.codecs)
.build();
// TODO - b/324426022: Move logic for supported mime types to DefaultEncoderFactory.
encoder =
encoderFactory.createForVideoEncoding(
requestedEncoderFormat