Skip to content

Commit

Permalink
Defer outputting the metadata sample when TimestampAdjuster isn't ini…
Browse files Browse the repository at this point in the history
…tialized

The sample timestamp carried by the emsg box can have a significant delta when comparing to the earliest presentation timestamp of the segment. Using this timestamp to intialize the timestamp offset in TimestampAdjuster will cause the media sample to have a wrong adjusted timestamp. So we should defer adjusting the metadata sample timestamp until the TimestampAdjuster is initialized with a real media sample.

PiperOrigin-RevId: 538172841
  • Loading branch information
tianyif authored and tof-tof committed Jun 6, 2023
1 parent 13df52b commit 08c189e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,13 @@ public TimestampAdjuster(long firstSampleTimestampUs) {
public synchronized void sharedInitializeOrWait(boolean canInitialize, long nextSampleTimestampUs)
throws InterruptedException {
Assertions.checkState(firstSampleTimestampUs == MODE_SHARED);
if (timestampOffsetUs != C.TIME_UNSET) {
// Already initialized.
if (isInitialized()) {
return;
} else if (canInitialize) {
this.nextSampleTimestampUs.set(nextSampleTimestampUs);
} else {
// Wait for another calling thread to complete initialization.
while (timestampOffsetUs == C.TIME_UNSET) {
while (!isInitialized()) {
wait();
}
}
Expand Down Expand Up @@ -194,7 +193,7 @@ public synchronized long adjustSampleTimestamp(long timeUs) {
if (timeUs == C.TIME_UNSET) {
return C.TIME_UNSET;
}
if (timestampOffsetUs == C.TIME_UNSET) {
if (!isInitialized()) {
long desiredSampleTimestampUs =
firstSampleTimestampUs == MODE_SHARED
? Assertions.checkNotNull(nextSampleTimestampUs.get())
Expand All @@ -207,6 +206,11 @@ public synchronized long adjustSampleTimestamp(long timeUs) {
return timeUs + timestampOffsetUs;
}

/** Returns whether the instance is initialized with a timestamp offset. */
public synchronized boolean isInitialized() {
return timestampOffsetUs != C.TIME_UNSET;
}

/**
* Converts a 90 kHz clock timestamp to a timestamp in microseconds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,13 @@ private void onEmsgLeafAtomRead(ParsableByteArray atom) {
pendingMetadataSampleInfos.addLast(
new MetadataSampleInfo(sampleTimeUs, /* sampleTimeIsRelative= */ false, sampleSize));
pendingMetadataSampleBytes += sampleSize;
} else if (timestampAdjuster != null && !timestampAdjuster.isInitialized()) {
// We also need to defer outputting metadata if the timestampAdjuster is not initialized,
// else we will set a wrong timestampOffsetUs in timestampAdjuster. See:
// https://github.com/androidx/media/issues/356.
pendingMetadataSampleInfos.addLast(
new MetadataSampleInfo(sampleTimeUs, /* sampleTimeIsRelative= */ false, sampleSize));
pendingMetadataSampleBytes += sampleSize;
} else {
// We can output the sample metadata immediately.
if (timestampAdjuster != null) {
Expand Down

0 comments on commit 08c189e

Please sign in to comment.