1 /* 2 * Copyright (C) 2017 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.google.android.exoplayer2.source.chunk; 17 18 import com.google.android.exoplayer2.extractor.DummyTrackOutput; 19 import com.google.android.exoplayer2.extractor.TrackOutput; 20 import com.google.android.exoplayer2.source.SampleQueue; 21 import com.google.android.exoplayer2.source.chunk.ChunkExtractorWrapper.TrackOutputProvider; 22 import com.google.android.exoplayer2.util.Log; 23 24 /** 25 * A {@link TrackOutputProvider} that provides {@link TrackOutput TrackOutputs} based on a 26 * predefined mapping from track type to output. 27 */ 28 public final class BaseMediaChunkOutput implements TrackOutputProvider { 29 30 private static final String TAG = "BaseMediaChunkOutput"; 31 32 private final int[] trackTypes; 33 private final SampleQueue[] sampleQueues; 34 35 /** 36 * @param trackTypes The track types of the individual track outputs. 37 * @param sampleQueues The individual sample queues. 38 */ BaseMediaChunkOutput(int[] trackTypes, SampleQueue[] sampleQueues)39 public BaseMediaChunkOutput(int[] trackTypes, SampleQueue[] sampleQueues) { 40 this.trackTypes = trackTypes; 41 this.sampleQueues = sampleQueues; 42 } 43 44 @Override track(int id, int type)45 public TrackOutput track(int id, int type) { 46 for (int i = 0; i < trackTypes.length; i++) { 47 if (type == trackTypes[i]) { 48 return sampleQueues[i]; 49 } 50 } 51 Log.e(TAG, "Unmatched track of type: " + type); 52 return new DummyTrackOutput(); 53 } 54 55 /** 56 * Returns the current absolute write indices of the individual sample queues. 57 */ getWriteIndices()58 public int[] getWriteIndices() { 59 int[] writeIndices = new int[sampleQueues.length]; 60 for (int i = 0; i < sampleQueues.length; i++) { 61 writeIndices[i] = sampleQueues[i].getWriteIndex(); 62 } 63 return writeIndices; 64 } 65 66 /** 67 * Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples 68 * subsequently written to the sample queues. 69 */ setSampleOffsetUs(long sampleOffsetUs)70 public void setSampleOffsetUs(long sampleOffsetUs) { 71 for (SampleQueue sampleQueue : sampleQueues) { 72 sampleQueue.setSampleOffsetUs(sampleOffsetUs); 73 } 74 } 75 76 } 77