Take fragment duration in Ms instead of Us

PiperOrigin-RevId: 621144165
This commit is contained in:
sheenachhabra 2024-04-02 05:39:34 -07:00 committed by Copybara-Service
parent 136baa148f
commit 308c4c4fbf
3 changed files with 22 additions and 21 deletions

View file

@ -59,25 +59,26 @@ import java.nio.ByteBuffer;
*/ */
@UnstableApi @UnstableApi
public final class FragmentedMp4Muxer implements Muxer { public final class FragmentedMp4Muxer implements Muxer {
private static final int DEFAULT_FRAGMENT_DURATION_US = 2_000_000; /** The default fragment duration. */
public static final long DEFAULT_FRAGMENT_DURATION_MS = 2_000;
private final FragmentedMp4Writer fragmentedMp4Writer; private final FragmentedMp4Writer fragmentedMp4Writer;
private final MetadataCollector metadataCollector; private final MetadataCollector metadataCollector;
/** Creates an instance with default fragment duration. */ /** Creates an instance with {@link #DEFAULT_FRAGMENT_DURATION_MS}. */
public FragmentedMp4Muxer(FileOutputStream fileOutputStream) { public FragmentedMp4Muxer(FileOutputStream fileOutputStream) {
this(fileOutputStream, DEFAULT_FRAGMENT_DURATION_US); this(fileOutputStream, DEFAULT_FRAGMENT_DURATION_MS);
} }
/** /**
* Creates an instance. * Creates an instance.
* *
* @param fileOutputStream The {@link FileOutputStream} to write the media data to. * @param fileOutputStream The {@link FileOutputStream} to write the media data to.
* @param fragmentDurationUs The fragment duration (in microseconds). The muxer will attempt to * @param fragmentDurationMs The fragment duration (in milliseconds). The muxer will attempt to
* create fragments of the given duration but the actual duration might be greater depending * create fragments of the given duration but the actual duration might be greater depending
* upon the frequency of sync samples. * upon the frequency of sync samples.
*/ */
public FragmentedMp4Muxer(FileOutputStream fileOutputStream, int fragmentDurationUs) { public FragmentedMp4Muxer(FileOutputStream fileOutputStream, long fragmentDurationMs) {
checkNotNull(fileOutputStream); checkNotNull(fileOutputStream);
metadataCollector = new MetadataCollector(); metadataCollector = new MetadataCollector();
Mp4MoovStructure moovStructure = Mp4MoovStructure moovStructure =
@ -85,7 +86,7 @@ public final class FragmentedMp4Muxer implements Muxer {
metadataCollector, Mp4Muxer.LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION); metadataCollector, Mp4Muxer.LAST_FRAME_DURATION_BEHAVIOR_DUPLICATE_PREV_DURATION);
fragmentedMp4Writer = fragmentedMp4Writer =
new FragmentedMp4Writer( new FragmentedMp4Writer(
fileOutputStream, moovStructure, AnnexBToAvccConverter.DEFAULT, fragmentDurationUs); fileOutputStream, moovStructure, AnnexBToAvccConverter.DEFAULT, fragmentDurationMs);
} }
@Override @Override

View file

@ -65,7 +65,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private final Mp4MoovStructure moovGenerator; private final Mp4MoovStructure moovGenerator;
private final AnnexBToAvccConverter annexBToAvccConverter; private final AnnexBToAvccConverter annexBToAvccConverter;
private final List<Track> tracks; private final List<Track> tracks;
private final int fragmentDurationUs; private final long fragmentDurationUs;
private @MonotonicNonNull Track videoTrack; private @MonotonicNonNull Track videoTrack;
private int currentFragmentSequenceNumber; private int currentFragmentSequenceNumber;
@ -77,13 +77,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
FileOutputStream outputStream, FileOutputStream outputStream,
Mp4MoovStructure moovGenerator, Mp4MoovStructure moovGenerator,
AnnexBToAvccConverter annexBToAvccConverter, AnnexBToAvccConverter annexBToAvccConverter,
int fragmentDurationUs) { long fragmentDurationMs) {
this.outputStream = outputStream; this.outputStream = outputStream;
this.output = outputStream.getChannel(); this.output = outputStream.getChannel();
this.moovGenerator = moovGenerator; this.moovGenerator = moovGenerator;
this.annexBToAvccConverter = annexBToAvccConverter; this.annexBToAvccConverter = annexBToAvccConverter;
tracks = new ArrayList<>(); tracks = new ArrayList<>();
this.fragmentDurationUs = fragmentDurationUs; this.fragmentDurationUs = fragmentDurationMs * 1_000;
minInputPresentationTimeUs = Long.MAX_VALUE; minInputPresentationTimeUs = Long.MAX_VALUE;
currentFragmentSequenceNumber = 1; currentFragmentSequenceNumber = 1;
} }

View file

@ -63,11 +63,11 @@ public final class InAppMuxer implements Muxer {
public static final class Builder { public static final class Builder {
private @Nullable MetadataProvider metadataProvider; private @Nullable MetadataProvider metadataProvider;
private boolean outputFragmentedMp4; private boolean outputFragmentedMp4;
private int fragmentDurationUs; private long fragmentDurationMs;
/** Creates a {@link Builder} instance with default values. */ /** Creates a {@link Builder} instance with default values. */
public Builder() { public Builder() {
fragmentDurationUs = C.LENGTH_UNSET; fragmentDurationMs = C.TIME_UNSET;
} }
/** /**
@ -92,18 +92,18 @@ public final class InAppMuxer implements Muxer {
} }
/** /**
* Sets the fragment duration if the output file is {@link #setOutputFragmentedMp4(boolean) * Sets the fragment duration (in milliseconds) if the output file is {@link
* fragmented}. * #setOutputFragmentedMp4(boolean) fragmented}.
*/ */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setFragmentDurationUs(int fragmentDurationUs) { public Builder setFragmentDurationMs(long fragmentDurationMs) {
this.fragmentDurationUs = fragmentDurationUs; this.fragmentDurationMs = fragmentDurationMs;
return this; return this;
} }
/** Builds a {@link Factory} instance. */ /** Builds a {@link Factory} instance. */
public Factory build() { public Factory build() {
return new Factory(metadataProvider, outputFragmentedMp4, fragmentDurationUs); return new Factory(metadataProvider, outputFragmentedMp4, fragmentDurationMs);
} }
} }
@ -117,15 +117,15 @@ public final class InAppMuxer implements Muxer {
private final @Nullable MetadataProvider metadataProvider; private final @Nullable MetadataProvider metadataProvider;
private final boolean outputFragmentedMp4; private final boolean outputFragmentedMp4;
private final int fragmentDurationUs; private final long fragmentDurationMs;
private Factory( private Factory(
@Nullable MetadataProvider metadataProvider, @Nullable MetadataProvider metadataProvider,
boolean outputFragmentedMp4, boolean outputFragmentedMp4,
int fragmentDurationUs) { long fragmentDurationMs) {
this.metadataProvider = metadataProvider; this.metadataProvider = metadataProvider;
this.outputFragmentedMp4 = outputFragmentedMp4; this.outputFragmentedMp4 = outputFragmentedMp4;
this.fragmentDurationUs = fragmentDurationUs; this.fragmentDurationMs = fragmentDurationMs;
} }
@Override @Override
@ -139,8 +139,8 @@ public final class InAppMuxer implements Muxer {
androidx.media3.muxer.Muxer muxer = androidx.media3.muxer.Muxer muxer =
outputFragmentedMp4 outputFragmentedMp4
? fragmentDurationUs != C.LENGTH_UNSET ? fragmentDurationMs != C.TIME_UNSET
? new FragmentedMp4Muxer(outputStream, fragmentDurationUs) ? new FragmentedMp4Muxer(outputStream, fragmentDurationMs)
: new FragmentedMp4Muxer(outputStream) : new FragmentedMp4Muxer(outputStream)
: new Mp4Muxer.Builder(outputStream).build(); : new Mp4Muxer.Builder(outputStream).build();
return new InAppMuxer(muxer, metadataProvider); return new InAppMuxer(muxer, metadataProvider);