Extract HlsMediaChunk#feedDataToExtractor method

Deduplicates the call to Extractor#read and sorrounding logic.

PiperOrigin-RevId: 235165499
This commit is contained in:
aquilescanta 2019-02-22 11:34:44 +00:00 committed by Oliver Woodman
parent 325029d58e
commit 6682580ad7

View File

@ -203,7 +203,8 @@ import java.util.concurrent.atomic.AtomicInteger;
private Extractor extractor; private Extractor extractor;
private boolean isExtractorReusable; private boolean isExtractorReusable;
private HlsSampleStreamWrapper output; private HlsSampleStreamWrapper output;
private int initSegmentBytesLoaded; // nextLoadPosition refers to the init segment if initDataLoadRequired is true.
// Otherwise, nextLoadPosition refers to the media segment.
private int nextLoadPosition; private int nextLoadPosition;
private boolean initDataLoadRequired; private boolean initDataLoadRequired;
private volatile boolean loadCanceled; private volatile boolean loadCanceled;
@ -307,54 +308,42 @@ import java.util.concurrent.atomic.AtomicInteger;
if (!initDataLoadRequired) { if (!initDataLoadRequired) {
return; return;
} }
DataSpec initSegmentDataSpec; feedDataToExtractor(initDataSource, initDataSpec, initSegmentEncrypted);
boolean skipLoadedBytes; nextLoadPosition = 0;
if (initSegmentEncrypted) {
initSegmentDataSpec = initDataSpec;
skipLoadedBytes = initSegmentBytesLoaded != 0;
} else {
initSegmentDataSpec = initDataSpec.subrange(initSegmentBytesLoaded);
skipLoadedBytes = false;
}
try {
DefaultExtractorInput input = prepareExtraction(initDataSource, initSegmentDataSpec);
if (skipLoadedBytes) {
input.skipFully(initSegmentBytesLoaded);
}
try {
int result = Extractor.RESULT_CONTINUE;
while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
result = extractor.read(input, null);
}
} finally {
initSegmentBytesLoaded = (int) (input.getPosition() - initDataSpec.absoluteStreamPosition);
}
} finally {
Util.closeQuietly(initDataSource);
}
initDataLoadRequired = false; initDataLoadRequired = false;
} }
private void loadMedia() throws IOException, InterruptedException { private void loadMedia() throws IOException, InterruptedException {
if (!isMasterTimestampSource) {
timestampAdjuster.waitUntilInitialized();
} else if (timestampAdjuster.getFirstSampleTimestampUs() == TimestampAdjuster.DO_NOT_OFFSET) {
// We're the master and we haven't set the desired first sample timestamp yet.
timestampAdjuster.setFirstSampleTimestampUs(startTimeUs);
}
feedDataToExtractor(dataSource, dataSpec, mediaSegmentEncrypted);
}
/**
* Attempts to feed the given {@code dataSpec} to {@code this.extractor}. Whenever the operation
* concludes (because of a thrown exception or because the operation finishes), the number of fed
* bytes is written to {@code nextLoadPosition}.
*/
private void feedDataToExtractor(
DataSource dataSource, DataSpec dataSpec, boolean dataIsEncrypted)
throws IOException, InterruptedException {
// If we previously fed part of this chunk to the extractor, we need to skip it this time. For // If we previously fed part of this chunk to the extractor, we need to skip it this time. For
// encrypted content we need to skip the data by reading it through the source, so as to ensure // encrypted content we need to skip the data by reading it through the source, so as to ensure
// correct decryption of the remainder of the chunk. For clear content, we can request the // correct decryption of the remainder of the chunk. For clear content, we can request the
// remainder of the chunk directly. // remainder of the chunk directly.
DataSpec loadDataSpec; DataSpec loadDataSpec;
boolean skipLoadedBytes; boolean skipLoadedBytes;
if (mediaSegmentEncrypted) { if (dataIsEncrypted) {
loadDataSpec = dataSpec; loadDataSpec = dataSpec;
skipLoadedBytes = nextLoadPosition != 0; skipLoadedBytes = nextLoadPosition != 0;
} else { } else {
loadDataSpec = dataSpec.subrange(nextLoadPosition); loadDataSpec = dataSpec.subrange(nextLoadPosition);
skipLoadedBytes = false; skipLoadedBytes = false;
} }
if (!isMasterTimestampSource) {
timestampAdjuster.waitUntilInitialized();
} else if (timestampAdjuster.getFirstSampleTimestampUs() == TimestampAdjuster.DO_NOT_OFFSET) {
// We're the master and we haven't set the desired first sample timestamp yet.
timestampAdjuster.setFirstSampleTimestampUs(startTimeUs);
}
try { try {
ExtractorInput input = prepareExtraction(dataSource, loadDataSpec); ExtractorInput input = prepareExtraction(dataSource, loadDataSpec);
if (skipLoadedBytes) { if (skipLoadedBytes) {
@ -363,7 +352,7 @@ import java.util.concurrent.atomic.AtomicInteger;
try { try {
int result = Extractor.RESULT_CONTINUE; int result = Extractor.RESULT_CONTINUE;
while (result == Extractor.RESULT_CONTINUE && !loadCanceled) { while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
result = extractor.read(input, null); result = extractor.read(input, /* seekPosition= */ null);
} }
} finally { } finally {
nextLoadPosition = (int) (input.getPosition() - dataSpec.absoluteStreamPosition); nextLoadPosition = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);