From f2d3072d1ac074f5c3125e51be4b93c0fb2e236f Mon Sep 17 00:00:00 2001 From: Googler Date: Sun, 7 Jul 2024 23:10:43 -0700 Subject: [PATCH] Refactor audioEsdsBox to esdsBox Since the muxer supported only AAC audio codec, the esdsBox was unconditionally created within the audioSampleEntry. This CL refactors the box creation logic by moving it to the codecSpecificBox method. This is to make adding support for new audio codecs easier. PiperOrigin-RevId: 650130935 (cherry picked from commit a2693553698bf75a6874f2a5bd7fc8b855232372) --- .../java/androidx/media3/muxer/Boxes.java | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/libraries/muxer/src/main/java/androidx/media3/muxer/Boxes.java b/libraries/muxer/src/main/java/androidx/media3/muxer/Boxes.java index 10851ca00d..8838828bb3 100644 --- a/libraries/muxer/src/main/java/androidx/media3/muxer/Boxes.java +++ b/libraries/muxer/src/main/java/androidx/media3/muxer/Boxes.java @@ -509,17 +509,11 @@ import java.util.Locale; /** Returns an audio sample entry box based on the MIME type. */ public static ByteBuffer audioSampleEntry(Format format) { - String mimeType = checkNotNull(format.sampleMimeType); - checkArgument(mimeType.equals(MimeTypes.AUDIO_AAC), "Unsupported audio format: " + mimeType); - String fourcc = "mp4a"; + String fourcc = codecSpecificFourcc(format); + ByteBuffer codecSpecificBox = codecSpecificBox(format); - checkArgument(!format.initializationData.isEmpty(), "csd-0 not found in the format."); - - byte[] csd0 = format.initializationData.get(0); - checkArgument(csd0.length > 0, "csd-0 is empty."); - - ByteBuffer csd0ByteBuffer = ByteBuffer.wrap(csd0); - ByteBuffer contents = ByteBuffer.allocate(csd0ByteBuffer.limit() + MAX_FIXED_LEAF_BOX_SIZE); + ByteBuffer contents = + ByteBuffer.allocate(codecSpecificBox.remaining() + MAX_FIXED_LEAF_BOX_SIZE); contents.putInt(0x00); // reserved contents.putShort((short) 0x0); // reserved @@ -536,7 +530,7 @@ import java.util.Locale; int sampleRate = format.sampleRate; contents.putInt(sampleRate << 16); - contents.put(audioEsdsBox(csd0ByteBuffer, format.peakBitrate, format.averageBitrate)); + contents.put(codecSpecificBox); contents.flip(); return BoxUtils.wrapIntoBox(fourcc, contents); @@ -546,14 +540,16 @@ import java.util.Locale; public static ByteBuffer codecSpecificBox(Format format) { String mimeType = checkNotNull(format.sampleMimeType); switch (mimeType) { - case "video/avc": + case MimeTypes.AUDIO_AAC: + return esdsBox(format); + case MimeTypes.VIDEO_H264: return avcCBox(format); - case "video/hevc": + case MimeTypes.VIDEO_H265: return hvcCBox(format); - case "video/av01": + case MimeTypes.VIDEO_AV1: return av1CBox(format); default: - throw new IllegalArgumentException("Unsupported video format: " + mimeType); + throw new IllegalArgumentException("Unsupported format: " + mimeType); } } @@ -1318,24 +1314,33 @@ import java.util.Locale; return BoxUtils.wrapIntoBox("colr", contents); } - /** Returns video codec specific fourcc. */ + /** Returns codec specific fourcc. */ private static String codecSpecificFourcc(Format format) { String mimeType = checkNotNull(format.sampleMimeType); switch (mimeType) { - case "video/avc": + case MimeTypes.AUDIO_AAC: + return "mp4a"; + case MimeTypes.VIDEO_H264: return "avc1"; - case "video/hevc": + case MimeTypes.VIDEO_H265: return "hvc1"; - case "video/av01": + case MimeTypes.VIDEO_AV1: return "av01"; default: - throw new IllegalArgumentException("Unsupported video format: " + mimeType); + throw new IllegalArgumentException("Unsupported format: " + mimeType); } } /** Returns the esds box. */ - private static ByteBuffer audioEsdsBox( - ByteBuffer csd0ByteBuffer, int peakBitrate, int averageBitrate) { + private static ByteBuffer esdsBox(Format format) { + checkArgument(!format.initializationData.isEmpty(), "csd-0 not found in the format."); + + byte[] csd0 = format.initializationData.get(0); + checkArgument(csd0.length > 0, "csd-0 is empty."); + + ByteBuffer csd0ByteBuffer = ByteBuffer.wrap(csd0); + int peakBitrate = format.peakBitrate; + int averageBitrate = format.averageBitrate; int csd0Size = csd0ByteBuffer.limit(); ByteBuffer contents = ByteBuffer.allocate(csd0Size + MAX_FIXED_LEAF_BOX_SIZE);