From 2e18e5073c866adca87ecee4eb248fe559bd0806 Mon Sep 17 00:00:00 2001 From: sheenachhabra Date: Wed, 7 May 2025 11:50:26 -0700 Subject: [PATCH] Implement `Muxer` interface in MP4 and Fragmented MP4 muxers PiperOrigin-RevId: 755945054 --- .../media3/muxer/FragmentedMp4Muxer.java | 24 +++++-------------- .../java/androidx/media3/muxer/Mp4Muxer.java | 18 ++++++-------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/libraries/muxer/src/main/java/androidx/media3/muxer/FragmentedMp4Muxer.java b/libraries/muxer/src/main/java/androidx/media3/muxer/FragmentedMp4Muxer.java index c64490531b..2bacb4b960 100644 --- a/libraries/muxer/src/main/java/androidx/media3/muxer/FragmentedMp4Muxer.java +++ b/libraries/muxer/src/main/java/androidx/media3/muxer/FragmentedMp4Muxer.java @@ -84,7 +84,7 @@ import java.nio.ByteBuffer; * */ @UnstableApi -public final class FragmentedMp4Muxer implements AutoCloseable { +public final class FragmentedMp4Muxer implements Muxer { /** The default fragment duration. */ public static final long DEFAULT_FRAGMENT_DURATION_MS = 2_000; @@ -186,14 +186,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable { trackIdToTrack = new SparseArray<>(); } - /** - * Adds a track of the given media format. - * - *

All tracks must be added before {@linkplain #writeSampleData writing any samples}. - * - * @param format The {@link Format} of the track. - * @return A track id for this track, which should be passed to {@link #writeSampleData}. - */ + @Override public int addTrack(Format format) { Track track = fragmentedMp4Writer.addTrack(/* sortKey= */ 1, format); trackIdToTrack.append(track.id, track); @@ -201,7 +194,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable { } /** - * Writes encoded sample data. + * {@inheritDoc} * *

Samples are written to the disk in batches. If {@link * Builder#setSampleCopyingEnabled(boolean) sample copying} is disabled, the {@code byteBuffer} @@ -217,6 +210,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable { * @param bufferInfo The {@link BufferInfo} related to this sample. * @throws MuxerException If there is any error while writing data to the disk. */ + @Override public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo) throws MuxerException { try { @@ -232,7 +226,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable { } /** - * Adds {@linkplain Metadata.Entry metadata} about the output file. + * {@inheritDoc} * *

List of supported {@linkplain Metadata.Entry metadata entries}: * @@ -250,18 +244,12 @@ public final class FragmentedMp4Muxer implements AutoCloseable { * IllegalArgumentException} is thrown if the {@linkplain Metadata.Entry metadata} is not * supported. */ + @Override public void addMetadataEntry(Metadata.Entry metadataEntry) { checkArgument(MuxerUtil.isMetadataSupported(metadataEntry), "Unsupported metadata"); metadataCollector.addMetadata(metadataEntry); } - /** - * Closes the file. - * - *

The muxer cannot be used anymore once this method returns. - * - * @throws MuxerException If the muxer fails to finish writing the output. - */ @Override public void close() throws MuxerException { try { diff --git a/libraries/muxer/src/main/java/androidx/media3/muxer/Mp4Muxer.java b/libraries/muxer/src/main/java/androidx/media3/muxer/Mp4Muxer.java index f710904007..7553ba2b02 100644 --- a/libraries/muxer/src/main/java/androidx/media3/muxer/Mp4Muxer.java +++ b/libraries/muxer/src/main/java/androidx/media3/muxer/Mp4Muxer.java @@ -109,7 +109,7 @@ import org.checkerframework.checker.nullness.qual.EnsuresNonNull; * */ @UnstableApi -public final class Mp4Muxer implements AutoCloseable { +public final class Mp4Muxer implements Muxer { /** Parameters for {@link #FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION}. */ public static final class Mp4AtFileParameters { /** Provides temporary cache files to be used by the muxer. */ @@ -426,7 +426,7 @@ public final class Mp4Muxer implements AutoCloseable { } /** - * Adds a track of the given media format. + * {@inheritDoc} * *

Tracks can be added at any point before the muxer is closed, even after writing samples to * other tracks. @@ -438,6 +438,7 @@ public final class Mp4Muxer implements AutoCloseable { * #writeSampleData}. * @throws MuxerException If an error occurs while adding track. */ + @Override public int addTrack(Format format) throws MuxerException { return addTrack(/* sortKey= */ 1, format); } @@ -483,7 +484,7 @@ public final class Mp4Muxer implements AutoCloseable { } /** - * Writes encoded sample data. + * {@inheritDoc} * * @param trackId The track id for which this sample is being written. * @param byteBuffer The encoded sample. The muxer takes ownership of the buffer if {@link @@ -492,6 +493,7 @@ public final class Mp4Muxer implements AutoCloseable { * @param bufferInfo The {@link BufferInfo} related to this sample. * @throws MuxerException If an error occurs while writing data to the output file. */ + @Override public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo) throws MuxerException { Track track = trackIdToTrack.get(trackId); @@ -512,7 +514,7 @@ public final class Mp4Muxer implements AutoCloseable { } /** - * Adds {@linkplain Metadata.Entry metadata} about the output file. + * {@inheritDoc} * *

List of supported {@linkplain Metadata.Entry metadata entries}: * @@ -530,18 +532,12 @@ public final class Mp4Muxer implements AutoCloseable { * IllegalArgumentException} is thrown if the {@linkplain Metadata.Entry metadata} is not * supported. */ + @Override public void addMetadataEntry(Metadata.Entry metadataEntry) { checkArgument(isMetadataSupported(metadataEntry), "Unsupported metadata"); metadataCollector.addMetadata(metadataEntry); } - /** - * Closes the file. - * - *

The muxer cannot be used anymore once this method returns. - * - * @throws MuxerException If the muxer fails to finish writing the output. - */ @Override public void close() throws MuxerException { @Nullable MuxerException exception = null;