1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.usbtuner.exoplayer; 17 18 import com.google.android.exoplayer.MediaFormat; 19 import com.google.android.exoplayer.MediaFormatHolder; 20 import com.google.android.exoplayer.SampleHolder; 21 import com.google.android.exoplayer.SampleSource; 22 import com.google.android.exoplayer.TrackRenderer; 23 24 import java.io.IOException; 25 26 /** 27 * Extractor for reading track metadata and samples stored in tracks. 28 * 29 * <p>Call {@link #prepare} until it returns {@code true}, then access track metadata via 30 * {@link #getTrackFormats} and {@link #getTrackMediaFormat}. 31 * 32 * <p>Pass indices of tracks to read from to {@link #selectTrack}. A track can later be deselected 33 * by calling {@link #deselectTrack}. It is safe to select/deselect tracks after reading sample 34 * data or seeking. Initially, all tracks are deselected. 35 * 36 * <p>Call {@link #release()} when the extractor is no longer needed to free resources. 37 */ 38 public interface SampleExtractor { 39 40 /** 41 * Prepares the extractor for reading track metadata and samples. 42 * 43 * @return whether the source is ready; if {@code false}, {@link #prepare()} must be called 44 * again 45 * @throws {@link IOException} thrown if the source can't be read 46 */ prepare()47 boolean prepare() throws IOException; 48 49 /** Returns track information about all tracks that can be selected. */ getTrackFormats()50 MediaFormat[] getTrackFormats(); 51 52 /** Selects the track at {@code index} for reading sample data. */ selectTrack(int index)53 void selectTrack(int index); 54 55 /** Deselects the track at {@code index}, so no more samples will be read from that track. */ deselectTrack(int index)56 void deselectTrack(int index); 57 58 /** 59 * Returns an estimate of the position up to which data is buffered. 60 * 61 * <p>This method should not be called until after the extractor has been successfully prepared. 62 * 63 * @return an estimate of the absolute position in microseconds up to which data is buffered, 64 * or {@link TrackRenderer#END_OF_TRACK_US} if data is buffered to the end of the stream, or 65 * {@link TrackRenderer#UNKNOWN_TIME_US} if no estimate is available. 66 */ getBufferedPositionUs()67 long getBufferedPositionUs(); 68 69 /** 70 * Seeks to the specified time in microseconds. 71 * 72 * <p>This method should not be called until after the extractor has been successfully prepared. 73 * 74 * @param positionUs the seek position in microseconds 75 */ seekTo(long positionUs)76 void seekTo(long positionUs); 77 78 /** Stores the {@link MediaFormat} of {@code track}. */ getTrackMediaFormat(int track, MediaFormatHolder outMediaFormatHolder)79 void getTrackMediaFormat(int track, MediaFormatHolder outMediaFormatHolder); 80 81 /** 82 * Reads the next sample in the track at index {@code track} into {@code sampleHolder}, returning 83 * {@link SampleSource#SAMPLE_READ} if it is available. 84 * 85 * <p>Advances to the next sample if a sample was read. 86 * 87 * @param track the index of the track from which to read a sample 88 * @param sampleHolder the holder for read sample data, if {@link SampleSource#SAMPLE_READ} is 89 * returned 90 * @return {@link SampleSource#SAMPLE_READ} if a sample was read into {@code sampleHolder}, or 91 * {@link SampleSource#END_OF_STREAM} if the last samples in all tracks have been read, or 92 * {@link SampleSource#NOTHING_READ} if the sample cannot be read immediately as it is not 93 * loaded. 94 */ readSample(int track, SampleHolder sampleHolder)95 int readSample(int track, SampleHolder sampleHolder); 96 97 /** Releases resources associated with this extractor. */ release()98 void release(); 99 100 /** Indicates to the source that it should still be buffering data. */ continueBuffering(long positionUs)101 boolean continueBuffering(long positionUs); 102 } 103