Move HLS mapped track picking into a separate method

This helps reduce the amount of nesting in HlsSampleStreamWrapper.track()

PiperOrigin-RevId: 271983779
This commit is contained in:
ibaker 2019-09-30 16:07:44 +01:00 committed by Oliver Woodman
parent 9cc927bc64
commit f0723a27b7

View File

@ -814,24 +814,14 @@ import java.util.Set;
@Override @Override
public TrackOutput track(int id, int type) { public TrackOutput track(int id, int type) {
int trackCount = sampleQueues.length;
if (MAPPABLE_TYPES.contains(type)) { if (MAPPABLE_TYPES.contains(type)) {
// Track types in MAPPABLE_TYPES are handled manually to ignore IDs. // Track types in MAPPABLE_TYPES are handled manually to ignore IDs.
int sampleQueueIndex = sampleQueueIndicesByType.get(type, C.INDEX_UNSET); @Nullable TrackOutput mappedTrackOutput = getMappedTrackOutput(id, type);
if (sampleQueueIndex != C.INDEX_UNSET) { if (mappedTrackOutput != null) {
if (sampleQueueMappingDoneByType.contains(type)) { return mappedTrackOutput;
return sampleQueueTrackIds[sampleQueueIndex] == id
? sampleQueues[sampleQueueIndex]
: createDummyTrackOutput(id, type);
} else {
sampleQueueMappingDoneByType.add(type);
sampleQueueTrackIds[sampleQueueIndex] = id;
return sampleQueues[sampleQueueIndex];
}
} }
} else /* sparse track */ { } else /* sparse track */ {
for (int i = 0; i < trackCount; i++) { for (int i = 0; i < sampleQueues.length; i++) {
if (sampleQueueTrackIds[i] == id) { if (sampleQueueTrackIds[i] == id) {
return sampleQueues[i]; return sampleQueues[i];
} }
@ -840,6 +830,8 @@ import java.util.Set;
if (tracksEnded) { if (tracksEnded) {
return createDummyTrackOutput(id, type); return createDummyTrackOutput(id, type);
} }
int trackCount = sampleQueues.length;
SampleQueue trackOutput = new FormatAdjustingSampleQueue(allocator, overridingDrmInitData); SampleQueue trackOutput = new FormatAdjustingSampleQueue(allocator, overridingDrmInitData);
trackOutput.setSampleOffsetUs(sampleOffsetUs); trackOutput.setSampleOffsetUs(sampleOffsetUs);
trackOutput.sourceId(chunkUid); trackOutput.sourceId(chunkUid);
@ -865,6 +857,37 @@ import java.util.Set;
return trackOutput; return trackOutput;
} }
/**
* Returns the {@link TrackOutput} for the provided {@code type} and {@code id}, or null if none
* has been created yet.
*
* <p>If a {@link SampleQueue} for {@code type} has been created and is mapped, but it has a
* different ID, then return a {@link DummyTrackOutput} that does nothing.
*
* <p>If a {@link SampleQueue} for {@code type} has been created but is not mapped, then map it to
* this {@code id} and return it. This situation can happen after a call to {@link #init} with
* {@code reusingExtractor=false}.
*
* @param id The ID of the track.
* @param type The type of the track, must be one of {@link #MAPPABLE_TYPES}.
* @return The the mapped {@link TrackOutput}, or null if it's not been created yet.
*/
@Nullable
private TrackOutput getMappedTrackOutput(int id, int type) {
Assertions.checkArgument(MAPPABLE_TYPES.contains(type));
int sampleQueueIndex = sampleQueueIndicesByType.get(type, C.INDEX_UNSET);
if (sampleQueueIndex == C.INDEX_UNSET) {
return null;
}
if (sampleQueueMappingDoneByType.add(type)) {
sampleQueueTrackIds[sampleQueueIndex] = id;
}
return sampleQueueTrackIds[sampleQueueIndex] == id
? sampleQueues[sampleQueueIndex]
: createDummyTrackOutput(id, type);
}
@Override @Override
public void endTracks() { public void endTracks() {
tracksEnded = true; tracksEnded = true;