mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Handle no supported encoder & muxer mime types in the Encoder factory.
PiperOrigin-RevId: 652825117
This commit is contained in:
parent
99679645fc
commit
1c3fe20826
@ -78,6 +78,7 @@ import org.checkerframework.dataflow.qual.Pure;
|
|||||||
.setCodecs(firstInputFormat.codecs)
|
.setCodecs(firstInputFormat.codecs)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
// TODO - b/324426022: Move logic for supported mime types to DefaultEncoderFactory.
|
||||||
encoder =
|
encoder =
|
||||||
encoderFactory.createForAudioEncoding(
|
encoderFactory.createForAudioEncoding(
|
||||||
requestedEncoderFormat
|
requestedEncoderFormat
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package androidx.media3.transformer;
|
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.checkArgument;
|
||||||
import static androidx.media3.common.util.Assertions.checkNotNull;
|
import static androidx.media3.common.util.Assertions.checkNotNull;
|
||||||
import static androidx.media3.common.util.Assertions.checkState;
|
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) {
|
if (format.bitrate == Format.NO_VALUE) {
|
||||||
format = format.buildUpon().setAverageBitrate(DEFAULT_AUDIO_BITRATE).build();
|
format = format.buildUpon().setAverageBitrate(DEFAULT_AUDIO_BITRATE).build();
|
||||||
}
|
}
|
||||||
checkNotNull(format.sampleMimeType);
|
if (format.sampleMimeType == null) {
|
||||||
|
throw createNoSupportedMimeTypeException(format, /* isVideo= */ false);
|
||||||
|
}
|
||||||
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
|
MediaFormat mediaFormat = createMediaFormatFromFormat(format);
|
||||||
ImmutableList<MediaCodecInfo> mediaCodecInfos =
|
ImmutableList<MediaCodecInfo> mediaCodecInfos =
|
||||||
EncoderUtil.getSupportedEncoders(format.sampleMimeType);
|
EncoderUtil.getSupportedEncoders(format.sampleMimeType);
|
||||||
@ -240,13 +243,16 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
if (format.frameRate == Format.NO_VALUE || deviceNeedsDefaultFrameRateWorkaround()) {
|
if (format.frameRate == Format.NO_VALUE || deviceNeedsDefaultFrameRateWorkaround()) {
|
||||||
format = format.buildUpon().setFrameRate(DEFAULT_FRAME_RATE).build();
|
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.width != Format.NO_VALUE);
|
||||||
checkArgument(format.height != Format.NO_VALUE);
|
checkArgument(format.height != Format.NO_VALUE);
|
||||||
// According to interface Javadoc, format.rotationDegrees should be 0. The video should always
|
// According to interface Javadoc, format.rotationDegrees should be 0. The video should always
|
||||||
// be encoded in landscape orientation.
|
// be encoded in landscape orientation.
|
||||||
checkArgument(format.height <= format.width);
|
checkArgument(format.height <= format.width);
|
||||||
checkArgument(format.rotationDegrees == 0);
|
checkArgument(format.rotationDegrees == 0);
|
||||||
checkNotNull(format.sampleMimeType);
|
|
||||||
checkStateNotNull(videoEncoderSelector);
|
checkStateNotNull(videoEncoderSelector);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -704,6 +710,22 @@ public final class DefaultEncoderFactory implements Codec.EncoderFactory {
|
|||||||
return (int) (width * height * frameRate * 0.07 * 2);
|
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")
|
@RequiresNonNull("#1.sampleMimeType")
|
||||||
private static ExportException createExportException(Format format, String errorString) {
|
private static ExportException createExportException(Format format, String errorString) {
|
||||||
return ExportException.createForCodec(
|
return ExportException.createForCodec(
|
||||||
|
@ -162,11 +162,11 @@ import java.util.List;
|
|||||||
* @param requestedFormat The {@link Format} requested.
|
* @param requestedFormat The {@link Format} requested.
|
||||||
* @param muxerSupportedMimeTypes The list of sample {@linkplain MimeTypes MIME types} that the
|
* @param muxerSupportedMimeTypes The list of sample {@linkplain MimeTypes MIME types} that the
|
||||||
* muxer supports.
|
* muxer supports.
|
||||||
* @return A supported {@linkplain MimeTypes MIME type}.
|
* @return A supported {@linkplain MimeTypes MIME type}, or {@code null} if none are supported.
|
||||||
* @throws ExportException If there are no supported {@linkplain MimeTypes MIME types}.
|
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
protected static String findSupportedMimeTypeForEncoderAndMuxer(
|
protected static String findSupportedMimeTypeForEncoderAndMuxer(
|
||||||
Format requestedFormat, List<String> muxerSupportedMimeTypes) throws ExportException {
|
Format requestedFormat, List<String> muxerSupportedMimeTypes) {
|
||||||
boolean isVideo = MimeTypes.isVideo(checkNotNull(requestedFormat.sampleMimeType));
|
boolean isVideo = MimeTypes.isVideo(checkNotNull(requestedFormat.sampleMimeType));
|
||||||
|
|
||||||
ImmutableSet.Builder<String> mimeTypesToCheckSetBuilder =
|
ImmutableSet.Builder<String> mimeTypesToCheckSetBuilder =
|
||||||
@ -193,22 +193,6 @@ import java.util.List;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw createNoSupportedMimeTypeException(requestedFormat);
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -315,6 +315,7 @@ import org.checkerframework.dataflow.qual.Pure;
|
|||||||
.setCodecs(inputFormat.codecs)
|
.setCodecs(inputFormat.codecs)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
// TODO - b/324426022: Move logic for supported mime types to DefaultEncoderFactory.
|
||||||
encoder =
|
encoder =
|
||||||
encoderFactory.createForVideoEncoding(
|
encoderFactory.createForVideoEncoding(
|
||||||
requestedEncoderFormat
|
requestedEncoderFormat
|
||||||
|
Loading…
x
Reference in New Issue
Block a user