Add default method durationUs(long) in TrackOutput

The method allows extractors to set track duration when available. The `default` implementation does nothing, allowing implementers of `TrackOutput` to override it if they need to handle track duration.

Implemented it in `FakeExtractor` to print track duration in dump files.

PiperOrigin-RevId: 688110288
This commit is contained in:
rohks 2024-10-21 06:15:10 -07:00 committed by Copybara-Service
parent 457bc55a4d
commit 847c1252e2
3 changed files with 20 additions and 0 deletions

View File

@ -36,6 +36,11 @@ public class ForwardingTrackOutput implements TrackOutput {
this.trackOutput = trackOutput; this.trackOutput = trackOutput;
} }
@Override
public void durationUs(long durationUs) {
trackOutput.durationUs(durationUs);
}
@Override @Override
public void format(Format format) { public void format(Format format) {
trackOutput.format(format); trackOutput.format(format);

View File

@ -146,6 +146,13 @@ public interface TrackOutput {
*/ */
int SAMPLE_DATA_PART_SUPPLEMENTAL = 2; int SAMPLE_DATA_PART_SUPPLEMENTAL = 2;
/**
* Called to set the duration of the track in microseconds.
*
* @param durationUs The duration of the track in microseconds.
*/
default void durationUs(long durationUs) {}
/** /**
* Called when the {@link Format} of the track has been extracted from the stream. * Called when the {@link Format} of the track has been extracted from the stream.
* *

View File

@ -54,6 +54,7 @@ public final class FakeTrackOutput implements TrackOutput, Dumper.Dumpable {
private byte[] sampleData; private byte[] sampleData;
private int formatCount; private int formatCount;
private boolean receivedSampleInFormat; private boolean receivedSampleInFormat;
private long durationUs;
@Nullable public Format lastFormat; @Nullable public Format lastFormat;
@ -64,6 +65,7 @@ public final class FakeTrackOutput implements TrackOutput, Dumper.Dumpable {
sampleData = Util.EMPTY_BYTE_ARRAY; sampleData = Util.EMPTY_BYTE_ARRAY;
formatCount = 0; formatCount = 0;
receivedSampleInFormat = true; receivedSampleInFormat = true;
durationUs = C.TIME_UNSET;
} }
public void clear() { public void clear() {
@ -74,6 +76,11 @@ public final class FakeTrackOutput implements TrackOutput, Dumper.Dumpable {
receivedSampleInFormat = true; receivedSampleInFormat = true;
} }
@Override
public void durationUs(long durationUs) {
this.durationUs = durationUs;
}
@Override @Override
public void format(Format format) { public void format(Format format) {
if (!deduplicateConsecutiveFormats) { if (!deduplicateConsecutiveFormats) {
@ -188,6 +195,7 @@ public final class FakeTrackOutput implements TrackOutput, Dumper.Dumpable {
public void dump(Dumper dumper) { public void dump(Dumper dumper) {
dumper.add("total output bytes", sampleData.length); dumper.add("total output bytes", sampleData.length);
dumper.add("sample count", sampleInfos.size()); dumper.add("sample count", sampleInfos.size());
dumper.addIfNonDefault("track duration", durationUs, C.TIME_UNSET);
if (dumpables.isEmpty() && lastFormat != null) { if (dumpables.isEmpty() && lastFormat != null) {
new DumpableFormat(lastFormat, 0).dump(dumper); new DumpableFormat(lastFormat, 0).dump(dumper);
} }