diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorHolder.java b/library/core/src/main/java/com/google/android/exoplayer2/source/BundledExtractorsAdapter.java similarity index 65% rename from library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorHolder.java rename to library/core/src/main/java/com/google/android/exoplayer2/source/BundledExtractorsAdapter.java index 205c678798..4caf07d905 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorHolder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/BundledExtractorsAdapter.java @@ -29,8 +29,11 @@ import com.google.android.exoplayer2.util.Util; import java.io.EOFException; import java.io.IOException; -/** Stores a list of extractors and a selected extractor when the format has been detected. */ -/* package */ final class ExtractorHolder { +/** + * {@link ProgressiveMediaExtractor} built on top of {@link Extractor} instances, whose + * implementation classes are bundled in the app. + */ +/* package */ final class BundledExtractorsAdapter implements ProgressiveMediaExtractor { private final Extractor[] extractors; @@ -42,33 +45,11 @@ import java.io.IOException; * * @param extractors One or more extractors to choose from. */ - public ExtractorHolder(Extractor[] extractors) { + public BundledExtractorsAdapter(Extractor[] extractors) { this.extractors = extractors; } - /** - * Disables seeking in MP3 streams. - * - *
MP3 live streams commonly have seekable metadata, despite being unseekable. - */ - public void disableSeekingOnMp3Streams() { - if (extractor instanceof Mp3Extractor) { - ((Mp3Extractor) extractor).disableSeeking(); - } - } - - /** - * Initializes any necessary resources for extraction. - * - * @param dataSource The {@link DataSource} from which data should be read. - * @param position The initial position of the {@code dataSource} in the stream. - * @param length The length of the stream, or {@link C#LENGTH_UNSET} if it is unknown. - * @param output The {@link ExtractorOutput} that will be used to initialize the selected - * extractor. - * @throws UnrecognizedInputFormatException Thrown if the input format could not be detected. - * @throws IOException Thrown if the input could not be read. - * @throws InterruptedException Thrown if the thread was interrupted. - */ + @Override public void init(DataSource dataSource, long position, long length, ExtractorOutput output) throws IOException, InterruptedException { extractorInput = new DefaultExtractorInput(dataSource, position, length); @@ -101,39 +82,7 @@ import java.io.IOException; extractor.init(output); } - /** - * Returns the current read position in the input stream, or {@link C#POSITION_UNSET} if no input - * is available. - */ - public long getCurrentInputPosition() { - return extractorInput != null ? extractorInput.getPosition() : C.POSITION_UNSET; - } - - /** - * Notifies the underlying extractor that a seek has occurred. - * - * @param position The byte offset in the stream from which data will be provided. - * @param seekTimeUs The seek time in microseconds. - */ - public void seek(long position, long seekTimeUs) { - Assertions.checkNotNull(extractor).seek(position, seekTimeUs); - } - - /** - * Extracts data starting at the current input stream position. - * - * @param positionHolder If {@link Extractor#RESULT_SEEK} is returned, this holder is updated to - * hold the position of the required data. - * @return One of the {@link Extractor}{@code .RESULT_*} values. - * @throws IOException If an error occurred reading from the input. - * @throws InterruptedException If the thread was interrupted. - */ - public int read(PositionHolder positionHolder) throws IOException, InterruptedException { - return Assertions.checkNotNull(extractor) - .read(Assertions.checkNotNull(extractorInput), positionHolder); - } - - /** Releases any held resources. */ + @Override public void release() { if (extractor != null) { extractor.release(); @@ -141,4 +90,27 @@ import java.io.IOException; } extractorInput = null; } + + @Override + public void disableSeekingOnMp3Streams() { + if (extractor instanceof Mp3Extractor) { + ((Mp3Extractor) extractor).disableSeeking(); + } + } + + @Override + public long getCurrentInputPosition() { + return extractorInput != null ? extractorInput.getPosition() : C.POSITION_UNSET; + } + + @Override + public void seek(long position, long seekTimeUs) { + Assertions.checkNotNull(extractor).seek(position, seekTimeUs); + } + + @Override + public int read(PositionHolder positionHolder) throws IOException, InterruptedException { + return Assertions.checkNotNull(extractor) + .read(Assertions.checkNotNull(extractorInput), positionHolder); + } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaExtractor.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaExtractor.java new file mode 100644 index 0000000000..f7d26ac06c --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaExtractor.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.source; + +import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.extractor.Extractor; +import com.google.android.exoplayer2.extractor.ExtractorOutput; +import com.google.android.exoplayer2.extractor.PositionHolder; +import com.google.android.exoplayer2.upstream.DataSource; +import java.io.IOException; + +/** Extracts the contents of a container file from a progressive media stream. */ +/* package */ interface ProgressiveMediaExtractor { + + /** + * Initializes the underlying infrastructure for reading from the input. + * + * @param dataSource The {@link DataSource} from which data should be read. + * @param position The initial position of the {@code dataSource} in the stream. + * @param length The length of the stream, or {@link C#LENGTH_UNSET} if length is unknown. + * @param output The {@link ExtractorOutput} that will be used to initialize the selected + * extractor. + * @throws UnrecognizedInputFormatException Thrown if the input format could not be detected. + * @throws IOException Thrown if the input could not be read. + * @throws InterruptedException Thrown if the thread was interrupted. + */ + void init(DataSource dataSource, long position, long length, ExtractorOutput output) + throws IOException, InterruptedException; + + /** Releases any held resources. */ + void release(); + + /** + * Disables seeking in MP3 streams. + * + *
MP3 live streams commonly have seekable metadata, despite being unseekable. + */ + void disableSeekingOnMp3Streams(); + + /** + * Returns the current read position in the input stream, or {@link C#POSITION_UNSET} if no input + * is available. + */ + long getCurrentInputPosition(); + + /** + * Notifies the extracting infrastructure that a seek has occurred. + * + * @param position The byte offset in the stream from which data will be provided. + * @param seekTimeUs The seek time in microseconds. + */ + void seek(long position, long seekTimeUs); + + /** + * Extracts data starting at the current input stream position. + * + * @param positionHolder If {@link Extractor#RESULT_SEEK} is returned, this holder is updated to + * hold the position of the required data. + * @return One of the {@link Extractor}{@code .RESULT_*} values. + * @throws IOException If an error occurred reading from the input. + * @throws InterruptedException If the thread was interrupted. + */ + int read(PositionHolder positionHolder) throws IOException, InterruptedException; +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java index 11be045adc..4c4ac34dc3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java @@ -103,7 +103,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @Nullable private final String customCacheKey; private final long continueLoadingCheckIntervalBytes; private final Loader loader; - private final ExtractorHolder extractorHolder; + private final ProgressiveMediaExtractor progressiveMediaExtractor; private final ConditionVariable loadCondition; private final Runnable maybeFinishPrepareRunnable; private final Runnable onContinueLoadingRequestedRunnable; @@ -175,7 +175,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; this.customCacheKey = customCacheKey; this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes; loader = new Loader("Loader:ProgressiveMediaPeriod"); - extractorHolder = new ExtractorHolder(extractors); + progressiveMediaExtractor = new BundledExtractorsAdapter(extractors); loadCondition = new ConditionVariable(); maybeFinishPrepareRunnable = this::maybeFinishPrepare; onContinueLoadingRequestedRunnable = @@ -215,7 +215,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; for (SampleQueue sampleQueue : sampleQueues) { sampleQueue.release(); } - extractorHolder.release(); + progressiveMediaExtractor.release(); } @Override @@ -756,7 +756,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; private void startLoading() { ExtractingLoadable loadable = new ExtractingLoadable( - uri, dataSource, extractorHolder, /* extractorOutput= */ this, loadCondition); + uri, dataSource, progressiveMediaExtractor, /* extractorOutput= */ this, loadCondition); if (preparedState != null) { SeekMap seekMap = preparedState.seekMap; Assertions.checkState(isPendingReset()); @@ -909,7 +909,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; private final Uri uri; private final StatsDataSource dataSource; - private final ExtractorHolder extractorHolder; + private final ProgressiveMediaExtractor progressiveMediaExtractor; private final ExtractorOutput extractorOutput; private final ConditionVariable loadCondition; private final PositionHolder positionHolder; @@ -927,12 +927,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; public ExtractingLoadable( Uri uri, DataSource dataSource, - ExtractorHolder extractorHolder, + ProgressiveMediaExtractor progressiveMediaExtractor, ExtractorOutput extractorOutput, ConditionVariable loadCondition) { this.uri = uri; this.dataSource = new StatsDataSource(dataSource); - this.extractorHolder = extractorHolder; + this.progressiveMediaExtractor = progressiveMediaExtractor; this.extractorOutput = extractorOutput; this.loadCondition = loadCondition; this.positionHolder = new PositionHolder(); @@ -966,20 +966,20 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; icyTrackOutput = icyTrack(); icyTrackOutput.format(ICY_FORMAT); } - extractorHolder.init(extractorDataSource, position, length, extractorOutput); + progressiveMediaExtractor.init(extractorDataSource, position, length, extractorOutput); if (icyHeaders != null) { - extractorHolder.disableSeekingOnMp3Streams(); + progressiveMediaExtractor.disableSeekingOnMp3Streams(); } if (pendingExtractorSeek) { - extractorHolder.seek(position, seekTimeUs); + progressiveMediaExtractor.seek(position, seekTimeUs); pendingExtractorSeek = false; } while (result == Extractor.RESULT_CONTINUE && !loadCanceled) { loadCondition.block(); - result = extractorHolder.read(positionHolder); - long currentInputPosition = extractorHolder.getCurrentInputPosition(); + result = progressiveMediaExtractor.read(positionHolder); + long currentInputPosition = progressiveMediaExtractor.getCurrentInputPosition(); if (currentInputPosition > position + continueLoadingCheckIntervalBytes) { position = currentInputPosition; loadCondition.close(); @@ -989,8 +989,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; } finally { if (result == Extractor.RESULT_SEEK) { result = Extractor.RESULT_CONTINUE; - } else if (extractorHolder.getCurrentInputPosition() != C.POSITION_UNSET) { - positionHolder.position = extractorHolder.getCurrentInputPosition(); + } else if (progressiveMediaExtractor.getCurrentInputPosition() != C.POSITION_UNSET) { + positionHolder.position = progressiveMediaExtractor.getCurrentInputPosition(); } Util.closeQuietly(dataSource); }