1 /* 2 * Copyright (C) 2016 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 androidx.annotation.Nullable; 19 import com.google.android.exoplayer2.C; 20 import com.google.android.exoplayer2.Format; 21 import com.google.android.exoplayer2.upstream.DataSource; 22 import com.google.android.exoplayer2.upstream.DataSpec; 23 import com.google.android.exoplayer2.util.Assertions; 24 import org.checkerframework.checker.nullness.qual.MonotonicNonNull; 25 26 /** 27 * A base implementation of {@link MediaChunk} that outputs to a {@link BaseMediaChunkOutput}. 28 */ 29 public abstract class BaseMediaChunk extends MediaChunk { 30 31 /** 32 * The time from which output will begin, or {@link C#TIME_UNSET} if output will begin from the 33 * start of the chunk. 34 */ 35 public final long clippedStartTimeUs; 36 /** 37 * The time from which output will end, or {@link C#TIME_UNSET} if output will end at the end of 38 * the chunk. 39 */ 40 public final long clippedEndTimeUs; 41 42 private @MonotonicNonNull BaseMediaChunkOutput output; 43 private int @MonotonicNonNull [] firstSampleIndices; 44 45 /** 46 * @param dataSource The source from which the data should be loaded. 47 * @param dataSpec Defines the data to be loaded. 48 * @param trackFormat See {@link #trackFormat}. 49 * @param trackSelectionReason See {@link #trackSelectionReason}. 50 * @param trackSelectionData See {@link #trackSelectionData}. 51 * @param startTimeUs The start time of the media contained by the chunk, in microseconds. 52 * @param endTimeUs The end time of the media contained by the chunk, in microseconds. 53 * @param clippedStartTimeUs The time in the chunk from which output will begin, or {@link 54 * C#TIME_UNSET} to output from the start of the chunk. 55 * @param clippedEndTimeUs The time in the chunk from which output will end, or {@link 56 * C#TIME_UNSET} to output to the end of the chunk. 57 * @param chunkIndex The index of the chunk, or {@link C#INDEX_UNSET} if it is not known. 58 */ BaseMediaChunk( DataSource dataSource, DataSpec dataSpec, Format trackFormat, int trackSelectionReason, @Nullable Object trackSelectionData, long startTimeUs, long endTimeUs, long clippedStartTimeUs, long clippedEndTimeUs, long chunkIndex)59 public BaseMediaChunk( 60 DataSource dataSource, 61 DataSpec dataSpec, 62 Format trackFormat, 63 int trackSelectionReason, 64 @Nullable Object trackSelectionData, 65 long startTimeUs, 66 long endTimeUs, 67 long clippedStartTimeUs, 68 long clippedEndTimeUs, 69 long chunkIndex) { 70 super(dataSource, dataSpec, trackFormat, trackSelectionReason, trackSelectionData, startTimeUs, 71 endTimeUs, chunkIndex); 72 this.clippedStartTimeUs = clippedStartTimeUs; 73 this.clippedEndTimeUs = clippedEndTimeUs; 74 } 75 76 /** 77 * Initializes the chunk for loading, setting the {@link BaseMediaChunkOutput} that will receive 78 * samples as they are loaded. 79 * 80 * @param output The output that will receive the loaded media samples. 81 */ init(BaseMediaChunkOutput output)82 public void init(BaseMediaChunkOutput output) { 83 this.output = output; 84 firstSampleIndices = output.getWriteIndices(); 85 } 86 87 /** 88 * Returns the index of the first sample in the specified track of the output that will originate 89 * from this chunk. 90 */ getFirstSampleIndex(int trackIndex)91 public final int getFirstSampleIndex(int trackIndex) { 92 return Assertions.checkStateNotNull(firstSampleIndices)[trackIndex]; 93 } 94 95 /** 96 * Returns the output most recently passed to {@link #init(BaseMediaChunkOutput)}. 97 */ getOutput()98 protected final BaseMediaChunkOutput getOutput() { 99 return Assertions.checkStateNotNull(output); 100 } 101 102 } 103