Use Track instead of TrackToken internally

The TrackToken is primarily for public API.
Using Track object internally will remove unnecessary
type casting at various places.

PiperOrigin-RevId: 662564224
This commit is contained in:
sheenachhabra 2024-08-13 10:21:49 -07:00 committed by Copybara-Service
parent cd532c5fb2
commit 68393832b4
3 changed files with 16 additions and 20 deletions

View File

@ -287,7 +287,7 @@ public final class Mp4Muxer implements Muxer {
@Nullable private final CacheFileProvider cacheFileProvider; @Nullable private final CacheFileProvider cacheFileProvider;
private final MetadataCollector metadataCollector; private final MetadataCollector metadataCollector;
private final Mp4Writer mp4Writer; private final Mp4Writer mp4Writer;
private final List<TrackToken> editableVideoTracks; private final List<Track> editableVideoTracks;
@Nullable private String cacheFilePath; @Nullable private String cacheFilePath;
@Nullable private FileOutputStream cacheFileOutputStream; @Nullable private FileOutputStream cacheFileOutputStream;
@ -361,9 +361,9 @@ public final class Mp4Muxer implements Muxer {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new MuxerException("Cache file not found", e); throw new MuxerException("Cache file not found", e);
} }
TrackToken trackToken = editableVideoMp4Writer.addTrack(sortKey, format); Track track = editableVideoMp4Writer.addTrack(sortKey, format);
editableVideoTracks.add(trackToken); editableVideoTracks.add(track);
return trackToken; return track;
} }
return mp4Writer.addTrack(sortKey, format); return mp4Writer.addTrack(sortKey, format);
} }
@ -386,11 +386,13 @@ public final class Mp4Muxer implements Muxer {
@Override @Override
public void writeSampleData(TrackToken trackToken, ByteBuffer byteBuffer, BufferInfo bufferInfo) public void writeSampleData(TrackToken trackToken, ByteBuffer byteBuffer, BufferInfo bufferInfo)
throws MuxerException { throws MuxerException {
checkState(trackToken instanceof Track);
Track track = (Track) trackToken;
try { try {
if (editableVideoTracks.contains(trackToken)) { if (editableVideoTracks.contains(trackToken)) {
checkNotNull(editableVideoMp4Writer).writeSampleData(trackToken, byteBuffer, bufferInfo); checkNotNull(editableVideoMp4Writer).writeSampleData(track, byteBuffer, bufferInfo);
} else { } else {
mp4Writer.writeSampleData(trackToken, byteBuffer, bufferInfo); mp4Writer.writeSampleData(track, byteBuffer, bufferInfo);
} }
} catch (IOException e) { } catch (IOException e) {
throw new MuxerException( throw new MuxerException(

View File

@ -15,7 +15,6 @@
*/ */
package androidx.media3.muxer; package androidx.media3.muxer;
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;
import static androidx.media3.muxer.AnnexBUtils.doesSampleContainAnnexBNalUnits; import static androidx.media3.muxer.AnnexBUtils.doesSampleContainAnnexBNalUnits;
@ -27,7 +26,6 @@ import static java.lang.Math.min;
import android.media.MediaCodec.BufferInfo; import android.media.MediaCodec.BufferInfo;
import androidx.media3.common.Format; import androidx.media3.common.Format;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.muxer.Muxer.TrackToken;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -101,9 +99,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
* *
* @param sortKey The key used for sorting the track list. * @param sortKey The key used for sorting the track list.
* @param format The {@link Format} for the track. * @param format The {@link Format} for the track.
* @return A unique {@link TrackToken}. It should be used in {@link #writeSampleData}. * @return A unique {@link Track}. It should be used in {@link #writeSampleData}.
*/ */
public TrackToken addTrack(int sortKey, Format format) { public Track addTrack(int sortKey, Format format) {
Track track = new Track(format, sortKey, sampleCopyEnabled); Track track = new Track(format, sortKey, sampleCopyEnabled);
tracks.add(track); tracks.add(track);
Collections.sort(tracks, (a, b) -> Integer.compare(a.sortKey, b.sortKey)); Collections.sort(tracks, (a, b) -> Integer.compare(a.sortKey, b.sortKey));
@ -113,15 +111,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* Writes encoded sample data. * Writes encoded sample data.
* *
* @param token The {@link TrackToken} for which this sample is being written. * @param track The {@link Track} for which this sample is being written.
* @param byteBuffer The encoded sample. * @param byteBuffer The encoded sample.
* @param bufferInfo The {@link BufferInfo} related to this sample. * @param bufferInfo The {@link BufferInfo} related to this sample.
* @throws IOException If there is any error while writing data to the output {@link FileChannel}. * @throws IOException If there is any error while writing data to the output {@link FileChannel}.
*/ */
public void writeSampleData(TrackToken token, ByteBuffer byteBuffer, BufferInfo bufferInfo) public void writeSampleData(Track track, ByteBuffer byteBuffer, BufferInfo bufferInfo)
throws IOException { throws IOException {
checkArgument(token instanceof Track); track.writeSampleData(byteBuffer, bufferInfo);
((Track) token).writeSampleData(byteBuffer, bufferInfo);
doInterleave(); doInterleave();
} }

View File

@ -15,7 +15,6 @@
*/ */
package androidx.media3.muxer; package androidx.media3.muxer;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.container.MdtaMetadataEntry.EDITABLE_TRACKS_SAMPLES_LOCATION_INTERLEAVED; import static androidx.media3.container.MdtaMetadataEntry.EDITABLE_TRACKS_SAMPLES_LOCATION_INTERLEAVED;
import static androidx.media3.container.MdtaMetadataEntry.EDITABLE_TRACKS_SAMPLES_LOCATION_IN_EDIT_DATA_MP4; import static androidx.media3.container.MdtaMetadataEntry.EDITABLE_TRACKS_SAMPLES_LOCATION_IN_EDIT_DATA_MP4;
import static androidx.media3.container.MdtaMetadataEntry.TYPE_INDICATOR_8_BIT_UNSIGNED_INT; import static androidx.media3.container.MdtaMetadataEntry.TYPE_INDICATOR_8_BIT_UNSIGNED_INT;
@ -105,7 +104,7 @@ public final class MuxerUtil {
MetadataCollector metadataCollector, MetadataCollector metadataCollector,
Mp4TimestampData timestampData, Mp4TimestampData timestampData,
boolean samplesInterleaved, boolean samplesInterleaved,
List<Muxer.TrackToken> editableVideoTracks) { List<Track> editableVideoTracks) {
metadataCollector.addMetadata(timestampData); metadataCollector.addMetadata(timestampData);
metadataCollector.addMetadata(getEditableTracksSamplesLocationMetadata(samplesInterleaved)); metadataCollector.addMetadata(getEditableTracksSamplesLocationMetadata(samplesInterleaved));
metadataCollector.addMetadata(getEditableTracksMapMetadata(editableVideoTracks)); metadataCollector.addMetadata(getEditableTracksMapMetadata(editableVideoTracks));
@ -123,8 +122,7 @@ public final class MuxerUtil {
TYPE_INDICATOR_8_BIT_UNSIGNED_INT); TYPE_INDICATOR_8_BIT_UNSIGNED_INT);
} }
private static MdtaMetadataEntry getEditableTracksMapMetadata( private static MdtaMetadataEntry getEditableTracksMapMetadata(List<Track> editableVideoTracks) {
List<Muxer.TrackToken> editableVideoTracks) {
// 1 byte version + 1 byte track count (n) + n bytes track types. // 1 byte version + 1 byte track count (n) + n bytes track types.
int totalTracks = editableVideoTracks.size(); int totalTracks = editableVideoTracks.size();
int dataSize = 2 + totalTracks; int dataSize = 2 + totalTracks;
@ -132,8 +130,7 @@ public final class MuxerUtil {
data[0] = 1; // version data[0] = 1; // version
data[1] = (byte) totalTracks; // track count data[1] = (byte) totalTracks; // track count
for (int i = 0; i < totalTracks; i++) { for (int i = 0; i < totalTracks; i++) {
checkState(editableVideoTracks.get(i) instanceof Track); Track track = editableVideoTracks.get(i);
Track track = (Track) editableVideoTracks.get(i);
int trackType; int trackType;
switch (track.format.auxiliaryTrackType) { switch (track.format.auxiliaryTrackType) {
case C.AUXILIARY_TRACK_TYPE_ORIGINAL: case C.AUXILIARY_TRACK_TYPE_ORIGINAL: