Implement Muxer interface in MP4 and Fragmented MP4 muxers

PiperOrigin-RevId: 755945054
This commit is contained in:
sheenachhabra 2025-05-07 11:50:26 -07:00 committed by Copybara-Service
parent 38ac6ba711
commit 2e18e5073c
2 changed files with 13 additions and 29 deletions

View File

@ -84,7 +84,7 @@ import java.nio.ByteBuffer;
* </ul> * </ul>
*/ */
@UnstableApi @UnstableApi
public final class FragmentedMp4Muxer implements AutoCloseable { public final class FragmentedMp4Muxer implements Muxer {
/** The default fragment duration. */ /** The default fragment duration. */
public static final long DEFAULT_FRAGMENT_DURATION_MS = 2_000; public static final long DEFAULT_FRAGMENT_DURATION_MS = 2_000;
@ -186,14 +186,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable {
trackIdToTrack = new SparseArray<>(); trackIdToTrack = new SparseArray<>();
} }
/** @Override
* Adds a track of the given media format.
*
* <p>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}.
*/
public int addTrack(Format format) { public int addTrack(Format format) {
Track track = fragmentedMp4Writer.addTrack(/* sortKey= */ 1, format); Track track = fragmentedMp4Writer.addTrack(/* sortKey= */ 1, format);
trackIdToTrack.append(track.id, track); trackIdToTrack.append(track.id, track);
@ -201,7 +194,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable {
} }
/** /**
* Writes encoded sample data. * {@inheritDoc}
* *
* <p>Samples are written to the disk in batches. If {@link * <p>Samples are written to the disk in batches. If {@link
* Builder#setSampleCopyingEnabled(boolean) sample copying} is disabled, the {@code byteBuffer} * 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. * @param bufferInfo The {@link BufferInfo} related to this sample.
* @throws MuxerException If there is any error while writing data to the disk. * @throws MuxerException If there is any error while writing data to the disk.
*/ */
@Override
public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo) public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo)
throws MuxerException { throws MuxerException {
try { try {
@ -232,7 +226,7 @@ public final class FragmentedMp4Muxer implements AutoCloseable {
} }
/** /**
* Adds {@linkplain Metadata.Entry metadata} about the output file. * {@inheritDoc}
* *
* <p>List of supported {@linkplain Metadata.Entry metadata entries}: * <p>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 * IllegalArgumentException} is thrown if the {@linkplain Metadata.Entry metadata} is not
* supported. * supported.
*/ */
@Override
public void addMetadataEntry(Metadata.Entry metadataEntry) { public void addMetadataEntry(Metadata.Entry metadataEntry) {
checkArgument(MuxerUtil.isMetadataSupported(metadataEntry), "Unsupported metadata"); checkArgument(MuxerUtil.isMetadataSupported(metadataEntry), "Unsupported metadata");
metadataCollector.addMetadata(metadataEntry); metadataCollector.addMetadata(metadataEntry);
} }
/**
* Closes the file.
*
* <p>The muxer cannot be used anymore once this method returns.
*
* @throws MuxerException If the muxer fails to finish writing the output.
*/
@Override @Override
public void close() throws MuxerException { public void close() throws MuxerException {
try { try {

View File

@ -109,7 +109,7 @@ import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
* </ul> * </ul>
*/ */
@UnstableApi @UnstableApi
public final class Mp4Muxer implements AutoCloseable { public final class Mp4Muxer implements Muxer {
/** Parameters for {@link #FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION}. */ /** Parameters for {@link #FILE_FORMAT_MP4_WITH_AUXILIARY_TRACKS_EXTENSION}. */
public static final class Mp4AtFileParameters { public static final class Mp4AtFileParameters {
/** Provides temporary cache files to be used by the muxer. */ /** 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}
* *
* <p>Tracks can be added at any point before the muxer is closed, even after writing samples to * <p>Tracks can be added at any point before the muxer is closed, even after writing samples to
* other tracks. * other tracks.
@ -438,6 +438,7 @@ public final class Mp4Muxer implements AutoCloseable {
* #writeSampleData}. * #writeSampleData}.
* @throws MuxerException If an error occurs while adding track. * @throws MuxerException If an error occurs while adding track.
*/ */
@Override
public int addTrack(Format format) throws MuxerException { public int addTrack(Format format) throws MuxerException {
return addTrack(/* sortKey= */ 1, format); 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 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 * @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. * @param bufferInfo The {@link BufferInfo} related to this sample.
* @throws MuxerException If an error occurs while writing data to the output file. * @throws MuxerException If an error occurs while writing data to the output file.
*/ */
@Override
public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo) public void writeSampleData(int trackId, ByteBuffer byteBuffer, BufferInfo bufferInfo)
throws MuxerException { throws MuxerException {
Track track = trackIdToTrack.get(trackId); 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}
* *
* <p>List of supported {@linkplain Metadata.Entry metadata entries}: * <p>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 * IllegalArgumentException} is thrown if the {@linkplain Metadata.Entry metadata} is not
* supported. * supported.
*/ */
@Override
public void addMetadataEntry(Metadata.Entry metadataEntry) { public void addMetadataEntry(Metadata.Entry metadataEntry) {
checkArgument(isMetadataSupported(metadataEntry), "Unsupported metadata"); checkArgument(isMetadataSupported(metadataEntry), "Unsupported metadata");
metadataCollector.addMetadata(metadataEntry); metadataCollector.addMetadata(metadataEntry);
} }
/**
* Closes the file.
*
* <p>The muxer cannot be used anymore once this method returns.
*
* @throws MuxerException If the muxer fails to finish writing the output.
*/
@Override @Override
public void close() throws MuxerException { public void close() throws MuxerException {
@Nullable MuxerException exception = null; @Nullable MuxerException exception = null;