Skip to content

Commit

Permalink
Create intermediate storage for samples for formats without sync fram…
Browse files Browse the repository at this point in the history
…e guarantee
  • Loading branch information
hcgil committed Feb 22, 2023
1 parent 0c17605 commit 6f7151b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package androidx.media3.exoplayer.source;

import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.extractor.TrackOutput;

/* package */ final class SampleMetadata {
final long timeUs;

@C.BufferFlags
final int flags;

final int size;

final long absoluteOffset;

@Nullable
final TrackOutput.CryptoData cryptoData;

SampleMetadata(long timeUs, @C.BufferFlags int flags, int size, long absoluteOffset, @Nullable TrackOutput.CryptoData cryptoData) {
this.timeUs = timeUs;
this.flags = flags;
this.size = size;
this.absoluteOffset = absoluteOffset;
this.cryptoData = cryptoData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import androidx.media3.exoplayer.upstream.Allocator;
import androidx.media3.extractor.TrackOutput;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/** A queue of media samples. */
Expand Down Expand Up @@ -109,6 +111,8 @@ public interface UpstreamFormatChangedListener {
private long sampleOffsetUs;
private boolean pendingSplice;

private final Queue<SampleMetadata> metadataQueue;

/**
* Creates a sample queue without DRM resource management.
*
Expand Down Expand Up @@ -181,6 +185,8 @@ protected SampleQueue(
largestQueuedTimestampUs = Long.MIN_VALUE;
upstreamFormatRequired = true;
upstreamKeyframeRequired = true;

metadataQueue = new LinkedList<>();
}

// Called by the consuming thread when there is no loading thread.
Expand Down Expand Up @@ -642,7 +648,25 @@ public void sampleMetadata(
}

long absoluteOffset = sampleDataQueue.getTotalBytesWritten() - size - offset;
commitSample(timeUs, flags, absoluteOffset, size, cryptoData);
if (timeUs < startTimeUs) {
if (isKeyframe) {
// encountered sync frame before playback began - clear waiting frames
metadataQueue.clear();
}

SampleMetadata metadata = new SampleMetadata(timeUs, flags, size, absoluteOffset, cryptoData);

metadataQueue.add(metadata);
} else {
// encountered start time, flush all waiting commits
while (!metadataQueue.isEmpty()) {
SampleMetadata metadata = metadataQueue.poll();
Assertions.checkNotNull(metadata);
commitSample(metadata.timeUs, metadata.flags, metadata.absoluteOffset, metadata.size, metadata.cryptoData);
}

commitSample(timeUs, flags, absoluteOffset, size, cryptoData);
}
}

/**
Expand Down

0 comments on commit 6f7151b

Please sign in to comment.