Add MediaChunkIterator interface and DASH implementation.

This will allow to provide available information about future chunks to the
track selection.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202940994
This commit is contained in:
tonihei 2018-07-02 06:19:33 -07:00 committed by Oliver Woodman
parent e50167ef54
commit 8ca9a32d8e
2 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,96 @@
/*
* Copyright (C) 2018 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.chunk;
import com.google.android.exoplayer2.upstream.DataSpec;
import java.util.NoSuchElementException;
/**
* Iterator for media chunk sequences.
*
* <p>The iterator initially points in front of the first available element. The first call to
* {@link #next()} moves the iterator to the first element. Check the return value of {@link
* #next()} or {@link #isEnded()} to determine whether the iterator reached the end of the available
* data.
*/
public interface MediaChunkIterator {
/** An empty media chunk iterator without available data. */
MediaChunkIterator EMPTY =
new MediaChunkIterator() {
@Override
public boolean isEnded() {
return true;
}
@Override
public boolean next() {
return false;
}
@Override
public DataSpec getDataSpec() {
throw new NoSuchElementException();
}
@Override
public long getChunkStartTimeUs() {
throw new NoSuchElementException();
}
@Override
public long getChunkEndTimeUs() {
throw new NoSuchElementException();
}
};
/** Returns whether the iteration has reached the end of the available data. */
boolean isEnded();
/**
* Moves the iterator to the next media chunk.
*
* <p>Check the return value or {@link #isEnded()} to determine whether the iterator reached the
* end of the available data.
*
* @return Whether the iterator points to a media chunk with available data.
*/
boolean next();
/**
* Returns the {@link DataSpec} used to load the media chunk.
*
* @throws java.util.NoSuchElementException If the method is called before the first call to
* {@link #next()} or when {@link #isEnded()} is true.
*/
DataSpec getDataSpec();
/**
* Returns the media start time of the chunk, in microseconds.
*
* @throws java.util.NoSuchElementException If the method is called before the first call to
* {@link #next()} or when {@link #isEnded()} is true.
*/
long getChunkStartTimeUs();
/**
* Returns the media end time of the chunk, in microseconds.
*
* @throws java.util.NoSuchElementException If the method is called before the first call to
* {@link #next()} or when {@link #isEnded()} is true.
*/
long getChunkEndTimeUs();
}

View File

@ -37,6 +37,7 @@ import com.google.android.exoplayer2.source.chunk.ChunkedTrackBlacklistUtil;
import com.google.android.exoplayer2.source.chunk.ContainerMediaChunk; import com.google.android.exoplayer2.source.chunk.ContainerMediaChunk;
import com.google.android.exoplayer2.source.chunk.InitializationChunk; import com.google.android.exoplayer2.source.chunk.InitializationChunk;
import com.google.android.exoplayer2.source.chunk.MediaChunk; import com.google.android.exoplayer2.source.chunk.MediaChunk;
import com.google.android.exoplayer2.source.chunk.MediaChunkIterator;
import com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk; import com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk;
import com.google.android.exoplayer2.source.dash.PlayerEmsgHandler.PlayerTrackEmsgHandler; import com.google.android.exoplayer2.source.dash.PlayerEmsgHandler.PlayerTrackEmsgHandler;
import com.google.android.exoplayer2.source.dash.manifest.AdaptationSet; import com.google.android.exoplayer2.source.dash.manifest.AdaptationSet;
@ -54,6 +55,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
/** /**
* A default {@link DashChunkSource} implementation. * A default {@link DashChunkSource} implementation.
@ -503,6 +505,70 @@ public class DefaultDashChunkSource implements DashChunkSource {
// Protected classes. // Protected classes.
/** {@link MediaChunkIterator} wrapping a {@link RepresentationHolder}. */
protected static final class RepresentationSegmentIterator implements MediaChunkIterator {
private final RepresentationHolder representationHolder;
private final long firstSegmentNum;
private final long lastAvailableSegmentNum;
private long segmentNum;
/**
* Creates iterator.
*
* @param representation The {@link RepresentationHolder} to wrap.
* @param segmentNum The number of the segment this iterator will be pointing to initially.
* @param lastAvailableSegmentNum The number of the last available segment.
*/
public RepresentationSegmentIterator(
RepresentationHolder representation, long segmentNum, long lastAvailableSegmentNum) {
this.representationHolder = representation;
this.firstSegmentNum = segmentNum;
this.segmentNum = segmentNum - 1;
this.lastAvailableSegmentNum = lastAvailableSegmentNum;
}
@Override
public boolean isEnded() {
return segmentNum > lastAvailableSegmentNum;
}
@Override
public boolean next() {
segmentNum++;
return segmentNum <= lastAvailableSegmentNum;
}
@Override
public @Nullable DataSpec getDataSpec() {
checkInBounds();
Representation representation = representationHolder.representation;
RangedUri segmentUri = representationHolder.getSegmentUrl(segmentNum);
Uri resolvedUri = segmentUri.resolveUri(representation.baseUrl);
String cacheKey = representation.getCacheKey();
return new DataSpec(resolvedUri, segmentUri.start, segmentUri.length, cacheKey);
}
@Override
public long getChunkStartTimeUs() {
checkInBounds();
return representationHolder.getSegmentStartTimeUs(segmentNum);
}
@Override
public long getChunkEndTimeUs() {
checkInBounds();
return representationHolder.getSegmentEndTimeUs(segmentNum);
}
private void checkInBounds() {
if (segmentNum < firstSegmentNum || segmentNum > lastAvailableSegmentNum) {
throw new NoSuchElementException();
}
}
}
/** Holds information about a snapshot of a single {@link Representation}. */ /** Holds information about a snapshot of a single {@link Representation}. */
protected static final class RepresentationHolder { protected static final class RepresentationHolder {