use of com.google.android.exoplayer2.extractor.ChunkIndex in project ExoPlayer by google.
the class DashDownloader method getSegmentIndex.
@Nullable
private DashSegmentIndex getSegmentIndex(DataSource dataSource, int trackType, Representation representation, boolean removing) throws IOException, InterruptedException {
DashSegmentIndex index = representation.getIndex();
if (index != null) {
return index;
}
RunnableFutureTask<@NullableType ChunkIndex, IOException> runnable = new RunnableFutureTask<@NullableType ChunkIndex, IOException>() {
@Override
@NullableType
protected ChunkIndex doWork() throws IOException {
return DashUtil.loadChunkIndex(dataSource, trackType, representation);
}
};
@Nullable ChunkIndex seekMap = execute(runnable, removing);
return seekMap == null ? null : new DashWrappingSegmentIndex(seekMap, representation.presentationTimeOffsetUs);
}
use of com.google.android.exoplayer2.extractor.ChunkIndex in project ExoPlayer by google.
the class HlsSampleStreamWrapper method selectTracks.
public boolean selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags, SampleStream[] streams, boolean[] streamResetFlags, boolean isFirstTrackSelection) {
Assertions.checkState(prepared);
// Disable old tracks.
for (int i = 0; i < selections.length; i++) {
if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
int group = ((HlsSampleStream) streams[i]).group;
setTrackGroupEnabledState(group, false);
sampleQueues.valueAt(group).disable();
streams[i] = null;
}
}
// Enable new tracks.
TrackSelection primaryTrackSelection = null;
boolean selectedNewTracks = false;
for (int i = 0; i < selections.length; i++) {
if (streams[i] == null && selections[i] != null) {
TrackSelection selection = selections[i];
int group = trackGroups.indexOf(selection.getTrackGroup());
setTrackGroupEnabledState(group, true);
if (group == primaryTrackGroupIndex) {
primaryTrackSelection = selection;
chunkSource.selectTracks(selection);
}
streams[i] = new HlsSampleStream(this, group);
streamResetFlags[i] = true;
selectedNewTracks = true;
}
}
if (isFirstTrackSelection) {
// At the time of the first track selection all queues will be enabled, so we need to disable
// any that are no longer required.
int sampleQueueCount = sampleQueues.size();
for (int i = 0; i < sampleQueueCount; i++) {
if (!groupEnabledStates[i]) {
sampleQueues.valueAt(i).disable();
}
}
if (primaryTrackSelection != null && !mediaChunks.isEmpty()) {
primaryTrackSelection.updateSelectedTrack(0);
int chunkIndex = chunkSource.getTrackGroup().indexOf(mediaChunks.getLast().trackFormat);
if (primaryTrackSelection.getSelectedIndexInTrackGroup() != chunkIndex) {
// The loaded preparation chunk does match the selection. We discard it.
seekTo(lastSeekPositionUs);
}
}
}
// Cancel requests if necessary.
if (enabledTrackCount == 0) {
chunkSource.reset();
downstreamTrackFormat = null;
mediaChunks.clear();
if (loader.isLoading()) {
loader.cancelLoading();
}
}
return selectedNewTracks;
}
use of com.google.android.exoplayer2.extractor.ChunkIndex in project ExoPlayer by google.
the class DefaultSsChunkSource method getNextChunk.
@Override
public final void getNextChunk(MediaChunk previous, long playbackPositionUs, ChunkHolder out) {
if (fatalError != null) {
return;
}
long bufferedDurationUs = previous != null ? (previous.endTimeUs - playbackPositionUs) : 0;
trackSelection.updateSelectedTrack(bufferedDurationUs);
StreamElement streamElement = manifest.streamElements[elementIndex];
if (streamElement.chunkCount == 0) {
// There aren't any chunks for us to load.
out.endOfStream = !manifest.isLive;
return;
}
int chunkIndex;
if (previous == null) {
chunkIndex = streamElement.getChunkIndex(playbackPositionUs);
} else {
chunkIndex = previous.getNextChunkIndex() - currentManifestChunkOffset;
if (chunkIndex < 0) {
// This is before the first chunk in the current manifest.
fatalError = new BehindLiveWindowException();
return;
}
}
if (chunkIndex >= streamElement.chunkCount) {
// This is beyond the last chunk in the current manifest.
out.endOfStream = !manifest.isLive;
return;
}
long chunkStartTimeUs = streamElement.getStartTimeUs(chunkIndex);
long chunkEndTimeUs = chunkStartTimeUs + streamElement.getChunkDurationUs(chunkIndex);
int currentAbsoluteChunkIndex = chunkIndex + currentManifestChunkOffset;
int trackSelectionIndex = trackSelection.getSelectedIndex();
ChunkExtractorWrapper extractorWrapper = extractorWrappers[trackSelectionIndex];
int manifestTrackIndex = trackSelection.getIndexInTrackGroup(trackSelectionIndex);
Uri uri = streamElement.buildRequestUri(manifestTrackIndex, chunkIndex);
out.chunk = newMediaChunk(trackSelection.getSelectedFormat(), dataSource, uri, null, currentAbsoluteChunkIndex, chunkStartTimeUs, chunkEndTimeUs, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), extractorWrapper);
}
use of com.google.android.exoplayer2.extractor.ChunkIndex in project ExoPlayer by google.
the class DefaultSsChunkSource method newMediaChunk.
// Private methods.
private static MediaChunk newMediaChunk(Format format, DataSource dataSource, Uri uri, String cacheKey, int chunkIndex, long chunkStartTimeUs, long chunkEndTimeUs, int trackSelectionReason, Object trackSelectionData, ChunkExtractorWrapper extractorWrapper) {
DataSpec dataSpec = new DataSpec(uri, 0, C.LENGTH_UNSET, cacheKey);
// In SmoothStreaming each chunk contains sample timestamps relative to the start of the chunk.
// To convert them the absolute timestamps, we need to set sampleOffsetUs to chunkStartTimeUs.
long sampleOffsetUs = chunkStartTimeUs;
return new ContainerMediaChunk(dataSource, dataSpec, format, trackSelectionReason, trackSelectionData, chunkStartTimeUs, chunkEndTimeUs, chunkIndex, 1, sampleOffsetUs, extractorWrapper);
}
use of com.google.android.exoplayer2.extractor.ChunkIndex in project ExoPlayer by google.
the class DefaultDashChunkSource method onChunkLoadCompleted.
@Override
public void onChunkLoadCompleted(Chunk chunk) {
if (chunk instanceof InitializationChunk) {
InitializationChunk initializationChunk = (InitializationChunk) chunk;
int trackIndex = trackSelection.indexOf(initializationChunk.trackFormat);
RepresentationHolder representationHolder = representationHolders[trackIndex];
// where it does we should ignore it.
if (representationHolder.segmentIndex == null) {
@Nullable ChunkIndex chunkIndex = representationHolder.chunkExtractor.getChunkIndex();
if (chunkIndex != null) {
representationHolders[trackIndex] = representationHolder.copyWithNewSegmentIndex(new DashWrappingSegmentIndex(chunkIndex, representationHolder.representation.presentationTimeOffsetUs));
}
}
}
if (playerTrackEmsgHandler != null) {
playerTrackEmsgHandler.onChunkLoadCompleted(chunk);
}
}
Aggregations