Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import androidx.media3.extractor.text.SubtitleParser;
import androidx.media3.extractor.text.SubtitleTranscodingExtractorOutput;
import com.google.common.collect.ImmutableList;
import java.io.EOFException;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -503,9 +504,15 @@ public static ExtractorsFactory newFactory(SubtitleParser.Factory subtitleParser
private long currentCueClusterPosition = C.INDEX_UNSET;
private long currentCueRelativePosition = C.INDEX_UNSET;
private int primarySeekTrackNumber = C.INDEX_UNSET;
/* package */ static final int MAX_INDEX_SEEKS = 16;
private final long[] pendingSeekHeadPositions = new long[MAX_INDEX_SEEKS];
private int pendingSeekHeadCount;
private final long[] visitedSeekHeadPositions = new long[MAX_INDEX_SEEKS];
private int visitedSeekHeadCount;
private boolean seekForCues;
private long cuesContentPosition = C.INDEX_UNSET;
private long seekPositionAfterBuildingCues = C.INDEX_UNSET;
private boolean seekForSecondarySeekHead;
private long tracksContentPosition = C.INDEX_UNSET;
private boolean seekForTracks;
private long seekPositionAfterReadingTracks = C.INDEX_UNSET;
Expand Down Expand Up @@ -646,6 +653,14 @@ public void seek(long position, long timeUs) {
// Cues elements will be ignored by the parsing logic.
if (!sentSeekMap) {
perTrackCues.clear();
seekForCues = false;
seekForSecondarySeekHead = false;
seekPositionAfterBuildingCues = C.INDEX_UNSET;
if (position == 0) {
cuesContentPosition = C.INDEX_UNSET;
pendingSeekHeadCount = 0;
visitedSeekHeadCount = 0;
}
}
for (int i = 0; i < tracks.size(); i++) {
tracks.valueAt(i).reset();
Expand All @@ -665,14 +680,33 @@ public final int read(ExtractorInput input, PositionHolder seekPosition) throws
haveOutputSample = false;
boolean continueReading = true;
while (continueReading && !haveOutputSample) {
continueReading = reader.read(input);
try {
continueReading = reader.read(input);
} catch (EOFException e) {
if (seekPositionAfterBuildingCues == C.INDEX_UNSET) {
throw e;
}
seekBackAfterAbandoningIndexResolution(seekPosition);
return Extractor.RESULT_SEEK;
}
if (continueReading
&& (maybeSeekForTracks(seekPosition, input.getPosition())
|| maybeSeekForCues(seekPosition, input.getPosition()))) {
|| maybeSeekForSecondarySeekHead(seekPosition, input)
|| maybeSeekForCues(seekPosition, input))) {
return Extractor.RESULT_SEEK;
}
}
if (!continueReading) {
if (seekPositionAfterBuildingCues != C.INDEX_UNSET) {
seekBackAfterAbandoningIndexResolution(seekPosition);
return Extractor.RESULT_SEEK;
Comment thread
TharathielCB marked this conversation as resolved.
Resolved
}
if (!sentSeekMap) {
checkNotNull(extractorOutput).seekMap(new SeekMap.Unseekable(durationUs));
sentSeekMap = true;
outputTrackFormats();
maybeEndTracks();
}
for (int i = 0; i < tracks.size(); i++) {
Track track = tracks.valueAt(i);
track.assertOutputInitialized();
Expand Down Expand Up @@ -924,15 +958,35 @@ protected void endMasterElement(int id) throws ParserException {
durationUs = scaleTimecodeToUs(durationTimecode);
}
break;
case ID_SEEK_HEAD:
if (seekPositionAfterBuildingCues != C.INDEX_UNSET) {
if (cuesContentPosition != C.INDEX_UNSET) {
seekForCues = true;
} else if (pendingSeekHeadCount > 0) {
seekForSecondarySeekHead = true;
} else {
abandonIndexResolution();
}
}
break;
case ID_SEEK:
if (seekEntryId == UNSET_ENTRY_ID || seekEntryPosition == C.INDEX_UNSET) {
throw ParserException.createForMalformedContainer(
"Mandatory element SeekID or SeekPosition not found", /* cause= */ null);
}
if (seekEntryId == ID_CUES) {
cuesContentPosition = seekEntryPosition;
if (!arrayContains(visitedSeekHeadPositions, visitedSeekHeadCount, seekEntryPosition)) {
cuesContentPosition = seekEntryPosition;
pendingSeekHeadCount = 0;
}
} else if (seekEntryId == ID_TRACKS) {
tracksContentPosition = seekEntryPosition;
} else if (seekEntryId == ID_SEEK_HEAD && cuesContentPosition == C.INDEX_UNSET) {
if (!arrayContains(visitedSeekHeadPositions, visitedSeekHeadCount, seekEntryPosition)
&& !arrayContains(pendingSeekHeadPositions, pendingSeekHeadCount, seekEntryPosition)
&& visitedSeekHeadCount + pendingSeekHeadCount < MAX_INDEX_SEEKS) {
pendingSeekHeadPositions[pendingSeekHeadCount++] = seekEntryPosition;
}
}
break;
case ID_CUES:
Expand Down Expand Up @@ -962,14 +1016,7 @@ protected void endMasterElement(int id) throws ParserException {
}
sentSeekMap = true;
inCuesElement = false;
for (int i = 0; i < tracks.size(); i++) {
Track track = tracks.valueAt(i);
if (!track.waitingForDtsAnalysis) {
updateTrackFormatWithMetadata(track);
track.assertOutputInitialized();
track.output.format(checkNotNull(track.format));
}
}
outputTrackFormats();
maybeEndTracks();
}
break;
Expand Down Expand Up @@ -1096,7 +1143,9 @@ protected void endMasterElement(int id) throws ParserException {
// If we're not going to seek for cues, or we have already processed them, output the
// formats immediately.
boolean maybeSendFormatsEarly =
!seekForCuesEnabled || cuesContentPosition == C.INDEX_UNSET || sentSeekMap;
!seekForCuesEnabled
|| (cuesContentPosition == C.INDEX_UNSET && pendingSeekHeadCount == 0)
|| sentSeekMap;

for (int i = 0; i < tracks.size(); i++) {
Track trackItem = tracks.valueAt(i);
Expand All @@ -1117,14 +1166,6 @@ protected void endMasterElement(int id) throws ParserException {
firstAudioTrackNumber = trackItem.number;
}
}

if (maybeSendFormatsEarly) {
if (!trackItem.waitingForDtsAnalysis) {
updateTrackFormatWithMetadata(trackItem);
trackItem.assertOutputInitialized();
trackItem.output.format(checkNotNull(trackItem.format));
}
}
}

if (defaultVideoTrackNumber != C.INDEX_UNSET) {
Expand All @@ -1144,6 +1185,7 @@ protected void endMasterElement(int id) throws ParserException {
maybePrepareSeekMap();
}
if (maybeSendFormatsEarly) {
outputTrackFormats();
maybeEndTracks();
}
break;
Expand Down Expand Up @@ -1177,7 +1219,11 @@ protected void integerElement(int id, long value) throws ParserException {
case ID_SEEK_POSITION:
// Seek Position is the relative offset beginning from the Segment. So to get absolute
// offset from the beginning of the file, we need to add segmentContentPosition to it.
seekEntryPosition = value + segmentContentPosition;
if (value < 0 || segmentContentPosition == C.INDEX_UNSET) {
seekEntryPosition = C.INDEX_UNSET;
} else {
seekEntryPosition = value + segmentContentPosition;
}
break;
case ID_TIMECODE_SCALE:
timecodeScale = value;
Expand Down Expand Up @@ -2195,28 +2241,120 @@ private int writeToOutput(ExtractorInput input, TrackOutput output, int length)
return bytesWritten;
}

private boolean maybeSeekForSecondarySeekHead(
PositionHolder seekPosition, ExtractorInput input) {
if (!seekForSecondarySeekHead) {
return false;
}
seekForSecondarySeekHead = false;
while (pendingSeekHeadCount > 0) {
long targetPosition = removePendingSeekHeadPosition();
if (maybeStartIndexSeek(seekPosition, input, targetPosition)) {
return true;
}
if (visitedSeekHeadCount >= MAX_INDEX_SEEKS) {
break;
}
}
abandonIndexResolution();
return false;
}

private boolean maybeStartIndexSeek(
PositionHolder seekPosition, ExtractorInput input, long targetPosition) {
if (visitedSeekHeadCount >= MAX_INDEX_SEEKS
|| !isIndexSeekTargetInBounds(targetPosition, input.getLength())) {
return false;
}
if (seekPositionAfterBuildingCues == C.INDEX_UNSET) {
seekPositionAfterBuildingCues = input.getPosition();
}
visitedSeekHeadPositions[visitedSeekHeadCount++] = targetPosition;
seekPosition.position = targetPosition;
return true;
}

private boolean isIndexSeekTargetInBounds(long position, long inputLength) {
if (position == C.INDEX_UNSET
|| segmentContentPosition == C.INDEX_UNSET
|| position < segmentContentPosition
|| position - segmentContentPosition >= segmentContentSize) {
return false;
}
return inputLength == C.LENGTH_UNSET || position < inputLength;
}

private void abandonIndexResolution() {
seekForCues = false;
seekForSecondarySeekHead = false;
cuesContentPosition = C.INDEX_UNSET;
pendingSeekHeadCount = 0;
if (!sentSeekMap) {
checkNotNull(extractorOutput).seekMap(new SeekMap.Unseekable(durationUs));
sentSeekMap = true;
outputTrackFormats();
maybeEndTracks();
}
}

private void seekBackAfterAbandoningIndexResolution(PositionHolder seekPosition) {
inCuesElement = false;
abandonIndexResolution();
reader.reset();
seekPosition.position = seekPositionAfterBuildingCues;
seekPositionAfterBuildingCues = C.INDEX_UNSET;
}

private long removePendingSeekHeadPosition() {
if (pendingSeekHeadCount == 0) {
return C.INDEX_UNSET;
}
long position = pendingSeekHeadPositions[0];
pendingSeekHeadCount--;
System.arraycopy(
pendingSeekHeadPositions, 1, pendingSeekHeadPositions, 0, pendingSeekHeadCount);
return position;
}

private static boolean arrayContains(long[] array, int count, long value) {
for (int i = 0; i < count; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}

/**
* Updates the position of the holder to Cues element's position if the extractor configuration
* permits use of master seek entry. After building Cues sets the holder's position back to where
* it was before.
*
* @param seekPosition The holder whose position will be updated.
* @param currentPosition Current position of the input.
* @param input The current input.
* @return Whether the seek position was updated.
*/
private boolean maybeSeekForCues(PositionHolder seekPosition, long currentPosition) {
private boolean maybeSeekForCues(PositionHolder seekPosition, ExtractorInput input) {
long currentPosition = input.getPosition();
if (seekForCues) {
seekPositionAfterBuildingCues = currentPosition;
seekPosition.position = cuesContentPosition;
long targetPosition = cuesContentPosition;
cuesContentPosition = C.INDEX_UNSET;
seekForCues = false;
return true;
if (maybeStartIndexSeek(seekPosition, input, targetPosition)) {
return true;
}
abandonIndexResolution();
}
// After parsing Cues, seek back to original position if available. We will not do this unless
// we seeked to get to the Cues in the first place.
// After parsing Cues or concluding index resolution, seek back to original position if
// available. We will not do this unless we seeked to get to the Cues or secondary SeekHead in
// the first place.
if (sentSeekMap && seekPositionAfterBuildingCues != C.INDEX_UNSET) {
seekPosition.position = seekPositionAfterBuildingCues;
if (seekPositionAfterBuildingCues != currentPosition) {
seekPosition.position = seekPositionAfterBuildingCues;
seekPositionAfterBuildingCues = C.INDEX_UNSET;
return true;
}
seekPositionAfterBuildingCues = C.INDEX_UNSET;
return true;
}
return false;
}
Expand Down Expand Up @@ -2311,10 +2449,17 @@ private void maybePrepareSeekMap() {
if (sentSeekMap) {
return; // Already evaluated
}
if (seekPositionAfterBuildingCues != C.INDEX_UNSET) {
abandonIndexResolution();
return;
}
// We need to build cues before parsing the cluster.
if (seekForCuesEnabled && cuesContentPosition != C.INDEX_UNSET) {
// We know where the Cues element is located. Seek to request it.
seekForCues = true;
} else if (seekForCuesEnabled && pendingSeekHeadCount > 0) {
// We know where the secondary SeekHead is located. Seek to read it for cues.
seekForSecondarySeekHead = true;
} else {
// We don't know where the Cues element is located. It's most likely omitted. Allow
// playback, but disable seeking.
Expand All @@ -2323,6 +2468,17 @@ private void maybePrepareSeekMap() {
}
}

private void outputTrackFormats() {
for (int i = 0; i < tracks.size(); i++) {
Track track = tracks.valueAt(i);
if (!track.waitingForDtsAnalysis) {
updateTrackFormatWithMetadata(track);
track.assertOutputInitialized();
track.output.format(checkNotNull(track.format));
}
}
}

private void maybeEndTracks() {
if (!pendingEndTracks) {
return;
Expand Down
Loading