Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,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;
private static final int MAX_SEEK_HEAD_HOPS = 16;
private final long[] pendingSeekHeadPositions = new long[MAX_SEEK_HEAD_HOPS];
private int pendingSeekHeadCount;
private final long[] visitedSeekHeadPositions = new long[MAX_SEEK_HEAD_HOPS];
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 @@ -642,6 +648,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 @@ -661,11 +675,31 @@ public final int read(ExtractorInput input, PositionHolder seekPosition) throws
continueReading = reader.read(input);
if (continueReading
&& (maybeSeekForTracks(seekPosition, input.getPosition())
|| maybeSeekForSecondarySeekHead(seekPosition, input.getPosition())
|| maybeSeekForCues(seekPosition, input.getPosition()))) {
return Extractor.RESULT_SEEK;
}
}
if (!continueReading) {
if (seekPositionAfterBuildingCues != C.INDEX_UNSET) {
checkNotNull(extractorOutput).seekMap(new SeekMap.Unseekable(durationUs));
sentSeekMap = true;
outputTrackFormats();
maybeEndTracks();
seekPosition.position = seekPositionAfterBuildingCues;
seekPositionAfterBuildingCues = C.INDEX_UNSET;
seekForCues = false;
seekForSecondarySeekHead = false;
cuesContentPosition = C.INDEX_UNSET;
pendingSeekHeadCount = 0;
return Extractor.RESULT_SEEK;
}
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 @@ -908,15 +942,38 @@ 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 {
checkNotNull(extractorOutput).seekMap(new SeekMap.Unseekable(durationUs));
sentSeekMap = true;
outputTrackFormats();
maybeEndTracks();
}
}
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_SEEK_HEAD_HOPS) {
pendingSeekHeadPositions[pendingSeekHeadCount++] = seekEntryPosition;
}
}
break;
case ID_CUES:
Expand Down Expand Up @@ -946,14 +1003,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 @@ -1080,7 +1130,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 @@ -1101,14 +1153,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 @@ -1128,6 +1172,7 @@ protected void endMasterElement(int id) throws ParserException {
maybePrepareSeekMap();
}
if (maybeSendFormatsEarly) {
outputTrackFormats();
maybeEndTracks();
}
break;
Expand Down Expand Up @@ -1161,7 +1206,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 @@ -2179,6 +2228,45 @@ private int writeToOutput(ExtractorInput input, TrackOutput output, int length)
return bytesWritten;
}

private boolean maybeSeekForSecondarySeekHead(
PositionHolder seekPosition, long currentPosition) {
if (seekForSecondarySeekHead) {
seekForSecondarySeekHead = false;
if (pendingSeekHeadCount > 0) {
if (seekPositionAfterBuildingCues == C.INDEX_UNSET) {
seekPositionAfterBuildingCues = currentPosition;
}
long targetPosition = removePendingSeekHeadPosition();
if (visitedSeekHeadCount < MAX_SEEK_HEAD_HOPS) {
visitedSeekHeadPositions[visitedSeekHeadCount++] = targetPosition;
}
seekPosition.position = targetPosition;
return true;
}
}
return false;
}

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
Expand All @@ -2190,17 +2278,27 @@ private int writeToOutput(ExtractorInput input, TrackOutput output, int length)
*/
private boolean maybeSeekForCues(PositionHolder seekPosition, long currentPosition) {
if (seekForCues) {
seekPositionAfterBuildingCues = currentPosition;
if (seekPositionAfterBuildingCues == C.INDEX_UNSET) {
seekPositionAfterBuildingCues = currentPosition;
}
seekPosition.position = cuesContentPosition;
if (visitedSeekHeadCount < MAX_SEEK_HEAD_HOPS) {
visitedSeekHeadPositions[visitedSeekHeadCount++] = cuesContentPosition;
}
cuesContentPosition = C.INDEX_UNSET;
seekForCues = false;
return true;
}
// 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 @@ -2295,10 +2393,24 @@ private void maybePrepareSeekMap() {
if (sentSeekMap) {
return; // Already evaluated
}
if (seekPositionAfterBuildingCues != C.INDEX_UNSET) {
checkNotNull(extractorOutput).seekMap(new SeekMap.Unseekable(durationUs));
sentSeekMap = true;
outputTrackFormats();
maybeEndTracks();
cuesContentPosition = C.INDEX_UNSET;
pendingSeekHeadCount = 0;
seekForCues = false;
seekForSecondarySeekHead = false;
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 @@ -2307,6 +2419,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
Loading