Change muxer video duration unit to microseconds

PiperOrigin-RevId: 644402109
This commit is contained in:
kimvde 2024-06-18 08:53:31 -07:00 committed by Copybara-Service
parent ada7271974
commit 048d71e392
3 changed files with 48 additions and 17 deletions

View File

@ -20,9 +20,10 @@ import androidx.media3.common.C;
import androidx.media3.common.Format; import androidx.media3.common.Format;
import androidx.media3.common.Metadata; import androidx.media3.common.Metadata;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.muxer.Muxer; import androidx.media3.muxer.Muxer;
import androidx.media3.muxer.Muxer.TrackToken;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** A default {@link Muxer} implementation. */ /** A default {@link Muxer} implementation. */
@ -31,22 +32,37 @@ public final class DefaultMuxer implements Muxer {
/** A {@link Muxer.Factory} for {@link DefaultMuxer}. */ /** A {@link Muxer.Factory} for {@link DefaultMuxer}. */
public static final class Factory implements Muxer.Factory { public static final class Factory implements Muxer.Factory {
private final Muxer.Factory muxerFactory; private final FrameworkMuxer.Factory muxerFactory;
/** Creates an instance with {@code videoDurationMs} set to {@link C#TIME_UNSET}. */ /** Creates an instance. */
public Factory() { public Factory() {
this(/* videoDurationMs= */ C.TIME_UNSET); this.muxerFactory = new FrameworkMuxer.Factory();
} }
/** /**
* Creates an instance. * @deprecated Use {@link #setVideoDurationUs(long)} instead. Note that a conversion from
* milliseconds to microseconds is required to migrate to {@link #setVideoDurationUs(long)}.
*/
@Deprecated
public Factory(long videoDurationMs) {
this.muxerFactory =
new FrameworkMuxer.Factory().setVideoDurationUs(Util.msToUs(videoDurationMs));
}
/**
* Sets the duration of the video track (in microseconds) to enforce in the output.
* *
* @param videoDurationMs The duration of the video track (in milliseconds) to enforce in the * <p>The default is {@link C#TIME_UNSET}.
*
* @param videoDurationUs The duration of the video track (in microseconds) to enforce in the
* output, or {@link C#TIME_UNSET} to not enforce. Only applicable when a video track is * output, or {@link C#TIME_UNSET} to not enforce. Only applicable when a video track is
* {@linkplain #addTrack(Format) added}. * {@linkplain #addTrack(Format) added}.
* @return This factory.
*/ */
public Factory(long videoDurationMs) { @CanIgnoreReturnValue
this.muxerFactory = new FrameworkMuxer.Factory(videoDurationMs); public Factory setVideoDurationUs(long videoDurationUs) {
muxerFactory.setVideoDurationUs(videoDurationUs);
return this;
} }
@Override @Override

View File

@ -34,6 +34,7 @@ import androidx.media3.common.util.Util;
import androidx.media3.container.Mp4LocationData; import androidx.media3.container.Mp4LocationData;
import androidx.media3.muxer.Muxer; import androidx.media3.muxer.Muxer;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -52,10 +53,26 @@ import java.util.Map;
/** {@link Muxer.Factory} for {@link FrameworkMuxer}. */ /** {@link Muxer.Factory} for {@link FrameworkMuxer}. */
public static final class Factory implements Muxer.Factory { public static final class Factory implements Muxer.Factory {
private final long videoDurationMs; private long videoDurationUs;
public Factory(long videoDurationMs) { public Factory() {
this.videoDurationMs = videoDurationMs; this.videoDurationUs = C.TIME_UNSET;
}
/**
* Sets the duration of the video track (in microseconds) to enforce in the output.
*
* <p>The default is {@link C#TIME_UNSET}.
*
* @param videoDurationUs The duration of the video track (in microseconds) to enforce in the
* output, or {@link C#TIME_UNSET} to not enforce. Only applicable when a video track is
* {@linkplain #addTrack(Format) added}.
* @return This factory.
*/
@CanIgnoreReturnValue
public Factory setVideoDurationUs(long videoDurationUs) {
this.videoDurationUs = videoDurationUs;
return this;
} }
@Override @Override
@ -66,7 +83,7 @@ import java.util.Map;
} catch (IOException e) { } catch (IOException e) {
throw new MuxerException("Error creating muxer", e); throw new MuxerException("Error creating muxer", e);
} }
return new FrameworkMuxer(mediaMuxer, videoDurationMs); return new FrameworkMuxer(mediaMuxer, videoDurationUs);
} }
@Override @Override
@ -90,9 +107,9 @@ import java.util.Map;
private boolean isStarted; private boolean isStarted;
private boolean isReleased; private boolean isReleased;
private FrameworkMuxer(MediaMuxer mediaMuxer, long videoDurationMs) { private FrameworkMuxer(MediaMuxer mediaMuxer, long videoDurationUs) {
this.mediaMuxer = mediaMuxer; this.mediaMuxer = mediaMuxer;
this.videoDurationUs = Util.msToUs(videoDurationMs); this.videoDurationUs = videoDurationUs;
trackTokenToLastPresentationTimeUs = new HashMap<>(); trackTokenToLastPresentationTimeUs = new HashMap<>();
trackTokenToPresentationTimeOffsetUs = new HashMap<>(); trackTokenToPresentationTimeOffsetUs = new HashMap<>();
} }

View File

@ -1567,9 +1567,7 @@ public final class MediaItemExportTest {
// Do not use CapturingMuxer.Factory(), as this test checks for a workaround in // Do not use CapturingMuxer.Factory(), as this test checks for a workaround in
// FrameworkMuxer. // FrameworkMuxer.
Transformer transformer = Transformer transformer =
createTransformerBuilder( createTransformerBuilder(new FrameworkMuxer.Factory(), /* enableFallback= */ false).build();
new FrameworkMuxer.Factory(C.TIME_UNSET), /* enableFallback= */ false)
.build();
MediaItem mediaItem = MediaItem.fromUri(ASSET_URI_PREFIX + FILE_AUDIO_ELST_SKIP_500MS); MediaItem mediaItem = MediaItem.fromUri(ASSET_URI_PREFIX + FILE_AUDIO_ELST_SKIP_500MS);
transformer.start(mediaItem, outputDir.newFile().getPath()); transformer.start(mediaItem, outputDir.newFile().getPath());