Commit 8ec88402 by Oliver Woodman

Minor cleanup.

- Add constants class. Currently housing a single lonely variable,
which is used generally throughout the library, and so no longer
nicely fits into a specific class.

- Rename a few other constants to add clear units.

- Made minor tweak to ExoPlayer documentation.
parent 25a53265
Showing with 84 additions and 78 deletions
...@@ -98,12 +98,12 @@ import android.widget.TextView; ...@@ -98,12 +98,12 @@ import android.widget.TextView;
@Override @Override
protected long getDurationUs() { protected long getDurationUs() {
return TrackRenderer.MATCH_LONGEST; return TrackRenderer.MATCH_LONGEST_US;
} }
@Override @Override
protected long getBufferedPositionUs() { protected long getBufferedPositionUs() {
return TrackRenderer.END_OF_TRACK; return TrackRenderer.END_OF_TRACK_US;
} }
@Override @Override
......
...@@ -316,14 +316,16 @@ public interface ExoPlayer { ...@@ -316,14 +316,16 @@ public interface ExoPlayer {
public void seekTo(int positionMs); public void seekTo(int positionMs);
/** /**
* Stops playback. * Stops playback. Use {@code setPlayWhenReady(false)} rather than this method if the intention
* is to pause playback.
* <p> * <p>
* Calling this method will cause the playback state to transition to * Calling this method will cause the playback state to transition to
* {@link ExoPlayer#STATE_IDLE}. Note that the player instance can still be used, and that * {@link ExoPlayer#STATE_IDLE}. The player instance can still be used, and
* {@link ExoPlayer#release()} must still be called on the player should it no longer be required. * {@link ExoPlayer#release()} must still be called on the player if it's no longer required.
* <p> * <p>
* Use {@code setPlayWhenReady(false)} rather than this method if the intention is to pause * Calling this method does not reset the playback position. If this player instance will be used
* playback. * to play another video from its start, then {@code seekTo(0)} should be called after stopping
* the player and before preparing it for the next video.
*/ */
public void stop(); public void stop();
......
...@@ -95,8 +95,8 @@ import java.util.List; ...@@ -95,8 +95,8 @@ import java.util.List;
} }
this.state = ExoPlayer.STATE_IDLE; this.state = ExoPlayer.STATE_IDLE;
this.durationUs = TrackRenderer.UNKNOWN_TIME; this.durationUs = TrackRenderer.UNKNOWN_TIME_US;
this.bufferedPositionUs = TrackRenderer.UNKNOWN_TIME; this.bufferedPositionUs = TrackRenderer.UNKNOWN_TIME_US;
mediaClock = new MediaClock(); mediaClock = new MediaClock();
enabledRenderers = new ArrayList<TrackRenderer>(rendererEnabledFlags.length); enabledRenderers = new ArrayList<TrackRenderer>(rendererEnabledFlags.length);
...@@ -122,12 +122,12 @@ import java.util.List; ...@@ -122,12 +122,12 @@ import java.util.List;
} }
public int getBufferedPosition() { public int getBufferedPosition() {
return bufferedPositionUs == TrackRenderer.UNKNOWN_TIME ? ExoPlayer.UNKNOWN_TIME return bufferedPositionUs == TrackRenderer.UNKNOWN_TIME_US ? ExoPlayer.UNKNOWN_TIME
: (int) (bufferedPositionUs / 1000); : (int) (bufferedPositionUs / 1000);
} }
public int getDuration() { public int getDuration() {
return durationUs == TrackRenderer.UNKNOWN_TIME ? ExoPlayer.UNKNOWN_TIME return durationUs == TrackRenderer.UNKNOWN_TIME_US ? ExoPlayer.UNKNOWN_TIME
: (int) (durationUs / 1000); : (int) (durationUs / 1000);
} }
...@@ -287,14 +287,14 @@ import java.util.List; ...@@ -287,14 +287,14 @@ import java.util.List;
enabledRenderers.add(renderer); enabledRenderers.add(renderer);
isEnded = isEnded && renderer.isEnded(); isEnded = isEnded && renderer.isEnded();
allRenderersReadyOrEnded = allRenderersReadyOrEnded && rendererReadyOrEnded(renderer); allRenderersReadyOrEnded = allRenderersReadyOrEnded && rendererReadyOrEnded(renderer);
if (durationUs == TrackRenderer.UNKNOWN_TIME) { if (durationUs == TrackRenderer.UNKNOWN_TIME_US) {
// We've already encountered a track for which the duration is unknown, so the media // We've already encountered a track for which the duration is unknown, so the media
// duration is unknown regardless of the duration of this track. // duration is unknown regardless of the duration of this track.
} else { } else {
long trackDurationUs = renderer.getDurationUs(); long trackDurationUs = renderer.getDurationUs();
if (trackDurationUs == TrackRenderer.UNKNOWN_TIME) { if (trackDurationUs == TrackRenderer.UNKNOWN_TIME_US) {
durationUs = TrackRenderer.UNKNOWN_TIME; durationUs = TrackRenderer.UNKNOWN_TIME_US;
} else if (trackDurationUs == TrackRenderer.MATCH_LONGEST) { } else if (trackDurationUs == TrackRenderer.MATCH_LONGEST_US) {
// Do nothing. // Do nothing.
} else { } else {
durationUs = Math.max(durationUs, trackDurationUs); durationUs = Math.max(durationUs, trackDurationUs);
...@@ -331,11 +331,11 @@ import java.util.List; ...@@ -331,11 +331,11 @@ import java.util.List;
long rendererBufferedPositionUs = renderer.getBufferedPositionUs(); long rendererBufferedPositionUs = renderer.getBufferedPositionUs();
long minBufferDurationUs = rebuffering ? minRebufferUs : minBufferUs; long minBufferDurationUs = rebuffering ? minRebufferUs : minBufferUs;
return minBufferDurationUs <= 0 return minBufferDurationUs <= 0
|| rendererBufferedPositionUs == TrackRenderer.UNKNOWN_TIME || rendererBufferedPositionUs == TrackRenderer.UNKNOWN_TIME_US
|| rendererBufferedPositionUs == TrackRenderer.END_OF_TRACK || rendererBufferedPositionUs == TrackRenderer.END_OF_TRACK_US
|| rendererBufferedPositionUs >= positionUs + minBufferDurationUs || rendererBufferedPositionUs >= positionUs + minBufferDurationUs
|| (rendererDurationUs != TrackRenderer.UNKNOWN_TIME || (rendererDurationUs != TrackRenderer.UNKNOWN_TIME_US
&& rendererDurationUs != TrackRenderer.MATCH_LONGEST && rendererDurationUs != TrackRenderer.MATCH_LONGEST_US
&& rendererBufferedPositionUs >= rendererDurationUs); && rendererBufferedPositionUs >= rendererDurationUs);
} }
...@@ -384,7 +384,7 @@ import java.util.List; ...@@ -384,7 +384,7 @@ import java.util.List;
private void doSomeWork() throws ExoPlaybackException { private void doSomeWork() throws ExoPlaybackException {
TraceUtil.beginSection("doSomeWork"); TraceUtil.beginSection("doSomeWork");
long operationStartTimeMs = SystemClock.elapsedRealtime(); long operationStartTimeMs = SystemClock.elapsedRealtime();
long bufferedPositionUs = durationUs != TrackRenderer.UNKNOWN_TIME ? durationUs long bufferedPositionUs = durationUs != TrackRenderer.UNKNOWN_TIME_US ? durationUs
: Long.MAX_VALUE; : Long.MAX_VALUE;
boolean isEnded = true; boolean isEnded = true;
boolean allRenderersReadyOrEnded = true; boolean allRenderersReadyOrEnded = true;
...@@ -398,17 +398,17 @@ import java.util.List; ...@@ -398,17 +398,17 @@ import java.util.List;
isEnded = isEnded && renderer.isEnded(); isEnded = isEnded && renderer.isEnded();
allRenderersReadyOrEnded = allRenderersReadyOrEnded && rendererReadyOrEnded(renderer); allRenderersReadyOrEnded = allRenderersReadyOrEnded && rendererReadyOrEnded(renderer);
if (bufferedPositionUs == TrackRenderer.UNKNOWN_TIME) { if (bufferedPositionUs == TrackRenderer.UNKNOWN_TIME_US) {
// We've already encountered a track for which the buffered position is unknown. Hence the // We've already encountered a track for which the buffered position is unknown. Hence the
// media buffer position unknown regardless of the buffered position of this track. // media buffer position unknown regardless of the buffered position of this track.
} else { } else {
long rendererDurationUs = renderer.getDurationUs(); long rendererDurationUs = renderer.getDurationUs();
long rendererBufferedPositionUs = renderer.getBufferedPositionUs(); long rendererBufferedPositionUs = renderer.getBufferedPositionUs();
if (rendererBufferedPositionUs == TrackRenderer.UNKNOWN_TIME) { if (rendererBufferedPositionUs == TrackRenderer.UNKNOWN_TIME_US) {
bufferedPositionUs = TrackRenderer.UNKNOWN_TIME; bufferedPositionUs = TrackRenderer.UNKNOWN_TIME_US;
} else if (rendererBufferedPositionUs == TrackRenderer.END_OF_TRACK } else if (rendererBufferedPositionUs == TrackRenderer.END_OF_TRACK_US
|| (rendererDurationUs != TrackRenderer.UNKNOWN_TIME || (rendererDurationUs != TrackRenderer.UNKNOWN_TIME_US
&& rendererDurationUs != TrackRenderer.MATCH_LONGEST && rendererDurationUs != TrackRenderer.MATCH_LONGEST_US
&& rendererBufferedPositionUs >= rendererDurationUs)) { && rendererBufferedPositionUs >= rendererDurationUs)) {
// This track is fully buffered. // This track is fully buffered.
} else { } else {
......
...@@ -72,7 +72,7 @@ public final class FrameworkSampleSource implements SampleSource { ...@@ -72,7 +72,7 @@ public final class FrameworkSampleSource implements SampleSource {
for (int i = 0; i < trackStates.length; i++) { for (int i = 0; i < trackStates.length; i++) {
android.media.MediaFormat format = extractor.getTrackFormat(i); android.media.MediaFormat format = extractor.getTrackFormat(i);
long duration = format.containsKey(android.media.MediaFormat.KEY_DURATION) ? long duration = format.containsKey(android.media.MediaFormat.KEY_DURATION) ?
format.getLong(android.media.MediaFormat.KEY_DURATION) : TrackRenderer.UNKNOWN_TIME; format.getLong(android.media.MediaFormat.KEY_DURATION) : TrackRenderer.UNKNOWN_TIME_US;
String mime = format.getString(android.media.MediaFormat.KEY_MIME); String mime = format.getString(android.media.MediaFormat.KEY_MIME);
trackInfos[i] = new TrackInfo(mime, duration); trackInfos[i] = new TrackInfo(mime, duration);
} }
...@@ -188,7 +188,7 @@ public final class FrameworkSampleSource implements SampleSource { ...@@ -188,7 +188,7 @@ public final class FrameworkSampleSource implements SampleSource {
Assertions.checkState(prepared); Assertions.checkState(prepared);
long bufferedDurationUs = extractor.getCachedDuration(); long bufferedDurationUs = extractor.getCachedDuration();
if (bufferedDurationUs == -1) { if (bufferedDurationUs == -1) {
return TrackRenderer.UNKNOWN_TIME; return TrackRenderer.UNKNOWN_TIME_US;
} else { } else {
return extractor.getSampleTime() + bufferedDurationUs; return extractor.getSampleTime() + bufferedDurationUs;
} }
......
...@@ -340,7 +340,7 @@ public abstract class MediaCodecTrackRenderer extends TrackRenderer { ...@@ -340,7 +340,7 @@ public abstract class MediaCodecTrackRenderer extends TrackRenderer {
@Override @Override
protected long getBufferedPositionUs() { protected long getBufferedPositionUs() {
long sourceBufferedPosition = source.getBufferedPositionUs(); long sourceBufferedPosition = source.getBufferedPositionUs();
return sourceBufferedPosition == UNKNOWN_TIME || sourceBufferedPosition == END_OF_TRACK return sourceBufferedPosition == UNKNOWN_TIME_US || sourceBufferedPosition == END_OF_TRACK_US
? sourceBufferedPosition : Math.max(sourceBufferedPosition, getCurrentPositionUs()); ? sourceBufferedPosition : Math.max(sourceBufferedPosition, getCurrentPositionUs());
} }
......
...@@ -147,8 +147,8 @@ public interface SampleSource { ...@@ -147,8 +147,8 @@ public interface SampleSource {
* This method should not be called until after the source has been successfully prepared. * This method should not be called until after the source has been successfully prepared.
* *
* @return An estimate of the absolute position in micro-seconds up to which data is buffered, * @return An estimate of the absolute position in micro-seconds up to which data is buffered,
* or {@link TrackRenderer#END_OF_TRACK} if data is buffered to the end of the stream, or * or {@link TrackRenderer#END_OF_TRACK_US} if data is buffered to the end of the stream, or
* {@link TrackRenderer#UNKNOWN_TIME} if no estimate is available. * {@link TrackRenderer#UNKNOWN_TIME_US} if no estimate is available.
*/ */
public long getBufferedPositionUs(); public long getBufferedPositionUs();
......
...@@ -67,16 +67,16 @@ public abstract class TrackRenderer implements ExoPlayerComponent { ...@@ -67,16 +67,16 @@ public abstract class TrackRenderer implements ExoPlayerComponent {
/** /**
* Represents an unknown time or duration. * Represents an unknown time or duration.
*/ */
public static final long UNKNOWN_TIME = -1; public static final long UNKNOWN_TIME_US = -1;
/** /**
* Represents a time or duration that should match the duration of the longest track whose * Represents a time or duration that should match the duration of the longest track whose
* duration is known. * duration is known.
*/ */
public static final long MATCH_LONGEST = -2; public static final long MATCH_LONGEST_US = -2;
/** /**
* Represents the time of the end of the track. * Represents the time of the end of the track.
*/ */
public static final long END_OF_TRACK = -3; public static final long END_OF_TRACK_US = -3;
private int state; private int state;
...@@ -301,9 +301,9 @@ public abstract class TrackRenderer implements ExoPlayerComponent { ...@@ -301,9 +301,9 @@ public abstract class TrackRenderer implements ExoPlayerComponent {
* This method may be called when the renderer is in the following states: * This method may be called when the renderer is in the following states:
* {@link #STATE_PREPARED}, {@link #STATE_ENABLED}, {@link #STATE_STARTED} * {@link #STATE_PREPARED}, {@link #STATE_ENABLED}, {@link #STATE_STARTED}
* *
* @return The duration of the track in micro-seconds, or {@link #MATCH_LONGEST} if * @return The duration of the track in micro-seconds, or {@link #MATCH_LONGEST_US} if
* the track's duration should match that of the longest track whose duration is known, or * the track's duration should match that of the longest track whose duration is known, or
* or {@link #UNKNOWN_TIME} if the duration is not known. * or {@link #UNKNOWN_TIME_US} if the duration is not known.
*/ */
protected abstract long getDurationUs(); protected abstract long getDurationUs();
...@@ -324,8 +324,8 @@ public abstract class TrackRenderer implements ExoPlayerComponent { ...@@ -324,8 +324,8 @@ public abstract class TrackRenderer implements ExoPlayerComponent {
* {@link #STATE_ENABLED}, {@link #STATE_STARTED} * {@link #STATE_ENABLED}, {@link #STATE_STARTED}
* *
* @return An estimate of the absolute position in micro-seconds up to which data is buffered, * @return An estimate of the absolute position in micro-seconds up to which data is buffered,
* or {@link #END_OF_TRACK} if the track is fully buffered, or {@link #UNKNOWN_TIME} if no * or {@link #END_OF_TRACK_US} if the track is fully buffered, or {@link #UNKNOWN_TIME_US} if
* estimate is available. * no estimate is available.
*/ */
protected abstract long getBufferedPositionUs(); protected abstract long getBufferedPositionUs();
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.chunk; package com.google.android.exoplayer.chunk;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.Allocation; import com.google.android.exoplayer.upstream.Allocation;
import com.google.android.exoplayer.upstream.Allocator; import com.google.android.exoplayer.upstream.Allocator;
import com.google.android.exoplayer.upstream.DataSource; import com.google.android.exoplayer.upstream.DataSource;
...@@ -89,8 +90,8 @@ public abstract class Chunk implements Loadable { ...@@ -89,8 +90,8 @@ public abstract class Chunk implements Loadable {
/** /**
* Gets the length of the chunk in bytes. * Gets the length of the chunk in bytes.
* *
* @return The length of the chunk in bytes, or {@value DataSpec#LENGTH_UNBOUNDED} if the length * @return The length of the chunk in bytes, or {@link C#LENGTH_UNBOUNDED} if the length has yet
* has yet to be determined. * to be determined.
*/ */
public final long getLength() { public final long getLength() {
return dataSourceStream.getLength(); return dataSourceStream.getLength();
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.chunk; package com.google.android.exoplayer.chunk;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.FormatHolder; import com.google.android.exoplayer.FormatHolder;
import com.google.android.exoplayer.LoadControl; import com.google.android.exoplayer.LoadControl;
import com.google.android.exoplayer.MediaFormat; import com.google.android.exoplayer.MediaFormat;
...@@ -22,7 +23,6 @@ import com.google.android.exoplayer.SampleHolder; ...@@ -22,7 +23,6 @@ import com.google.android.exoplayer.SampleHolder;
import com.google.android.exoplayer.SampleSource; import com.google.android.exoplayer.SampleSource;
import com.google.android.exoplayer.TrackInfo; import com.google.android.exoplayer.TrackInfo;
import com.google.android.exoplayer.TrackRenderer; import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.upstream.DataSpec;
import com.google.android.exoplayer.upstream.Loader; import com.google.android.exoplayer.upstream.Loader;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
...@@ -383,14 +383,14 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener { ...@@ -383,14 +383,14 @@ public class ChunkSampleSource implements SampleSource, Loader.Listener {
if (currentLoadable != null && mediaChunk == currentLoadable) { if (currentLoadable != null && mediaChunk == currentLoadable) {
// Linearly interpolate partially-fetched chunk times. // Linearly interpolate partially-fetched chunk times.
long chunkLength = mediaChunk.getLength(); long chunkLength = mediaChunk.getLength();
if (chunkLength != DataSpec.LENGTH_UNBOUNDED) { if (chunkLength != C.LENGTH_UNBOUNDED) {
return mediaChunk.startTimeUs + ((mediaChunk.endTimeUs - mediaChunk.startTimeUs) * return mediaChunk.startTimeUs + ((mediaChunk.endTimeUs - mediaChunk.startTimeUs) *
mediaChunk.bytesLoaded()) / chunkLength; mediaChunk.bytesLoaded()) / chunkLength;
} else { } else {
return mediaChunk.startTimeUs; return mediaChunk.startTimeUs;
} }
} else if (mediaChunk.isLastChunk()) { } else if (mediaChunk.isLastChunk()) {
return TrackRenderer.END_OF_TRACK; return TrackRenderer.END_OF_TRACK_US;
} else { } else {
return mediaChunk.endTimeUs; return mediaChunk.endTimeUs;
} }
......
...@@ -230,7 +230,7 @@ public class TextTrackRenderer extends TrackRenderer implements Callback { ...@@ -230,7 +230,7 @@ public class TextTrackRenderer extends TrackRenderer implements Callback {
@Override @Override
protected long getBufferedPositionUs() { protected long getBufferedPositionUs() {
// Don't block playback whilst subtitles are loading. // Don't block playback whilst subtitles are loading.
return END_OF_TRACK; return END_OF_TRACK_US;
} }
@Override @Override
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
...@@ -29,7 +30,7 @@ public class ByteArrayDataSink implements DataSink { ...@@ -29,7 +30,7 @@ public class ByteArrayDataSink implements DataSink {
@Override @Override
public DataSink open(DataSpec dataSpec) throws IOException { public DataSink open(DataSpec dataSpec) throws IOException {
if (dataSpec.length == DataSpec.LENGTH_UNBOUNDED) { if (dataSpec.length == C.LENGTH_UNBOUNDED) {
stream = new ByteArrayOutputStream(); stream = new ByteArrayOutputStream();
} else { } else {
Assertions.checkArgument(dataSpec.length <= Integer.MAX_VALUE); Assertions.checkArgument(dataSpec.length <= Integer.MAX_VALUE);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import java.io.IOException; import java.io.IOException;
...@@ -36,14 +37,14 @@ public class ByteArrayDataSource implements DataSource { ...@@ -36,14 +37,14 @@ public class ByteArrayDataSource implements DataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
if (dataSpec.length == DataSpec.LENGTH_UNBOUNDED) { if (dataSpec.length == C.LENGTH_UNBOUNDED) {
Assertions.checkArgument(dataSpec.position < data.length); Assertions.checkArgument(dataSpec.position < data.length);
} else { } else {
Assertions.checkArgument(dataSpec.position + dataSpec.length <= data.length); Assertions.checkArgument(dataSpec.position + dataSpec.length <= data.length);
} }
readPosition = (int) dataSpec.position; readPosition = (int) dataSpec.position;
return (dataSpec.length == DataSpec.LENGTH_UNBOUNDED) return (dataSpec.length == C.LENGTH_UNBOUNDED) ? (data.length - dataSpec.position)
? (data.length - dataSpec.position) : dataSpec.length; : dataSpec.length;
} }
@Override @Override
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import java.io.IOException; import java.io.IOException;
/** /**
...@@ -34,9 +36,9 @@ public interface DataSource { ...@@ -34,9 +36,9 @@ public interface DataSource {
* @param dataSpec Defines the data to be read. * @param dataSpec Defines the data to be read.
* @throws IOException If an error occurs opening the source. * @throws IOException If an error occurs opening the source.
* @return The number of bytes that can be read from the opened source. For unbounded requests * @return The number of bytes that can be read from the opened source. For unbounded requests
* (i.e. requests where {@link DataSpec#length} equals {@link DataSpec#LENGTH_UNBOUNDED}) * (i.e. requests where {@link DataSpec#length} equals {@link C#LENGTH_UNBOUNDED}) this value
* this value is the resolved length of the request. For all other requests, the value * is the resolved length of the request. For all other requests, the value returned will be
* returned will be equal to the request's {@link DataSpec#length}. * equal to the request's {@link DataSpec#length}.
*/ */
public long open(DataSpec dataSpec) throws IOException; public long open(DataSpec dataSpec) throws IOException;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.Loader.Loadable; import com.google.android.exoplayer.upstream.Loader.Loadable;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.Util; import com.google.android.exoplayer.util.Util;
...@@ -67,7 +68,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream ...@@ -67,7 +68,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream
this.dataSource = dataSource; this.dataSource = dataSource;
this.dataSpec = dataSpec; this.dataSpec = dataSpec;
this.allocator = allocator; this.allocator = allocator;
resolvedLength = DataSpec.LENGTH_UNBOUNDED; resolvedLength = C.LENGTH_UNBOUNDED;
readHead = new ReadHead(); readHead = new ReadHead();
} }
...@@ -99,11 +100,11 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream ...@@ -99,11 +100,11 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream
/** /**
* Returns the length of the streamin bytes. * Returns the length of the streamin bytes.
* *
* @return The length of the stream in bytes, or {@value DataSpec#LENGTH_UNBOUNDED} if the length * @return The length of the stream in bytes, or {@value C#LENGTH_UNBOUNDED} if the length has
* has yet to be determined. * yet to be determined.
*/ */
public long getLength() { public long getLength() {
return resolvedLength != DataSpec.LENGTH_UNBOUNDED ? resolvedLength : dataSpec.length; return resolvedLength != C.LENGTH_UNBOUNDED ? resolvedLength : dataSpec.length;
} }
/** /**
...@@ -112,7 +113,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream ...@@ -112,7 +113,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream
* @return True if the stream has finished loading. False otherwise. * @return True if the stream has finished loading. False otherwise.
*/ */
public boolean isLoadFinished() { public boolean isLoadFinished() {
return resolvedLength != DataSpec.LENGTH_UNBOUNDED && loadPosition == resolvedLength; return resolvedLength != C.LENGTH_UNBOUNDED && loadPosition == resolvedLength;
} }
/** /**
...@@ -144,7 +145,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream ...@@ -144,7 +145,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream
@Override @Override
public boolean isEndOfStream() { public boolean isEndOfStream() {
return resolvedLength != DataSpec.LENGTH_UNBOUNDED && readHead.position == resolvedLength; return resolvedLength != C.LENGTH_UNBOUNDED && readHead.position == resolvedLength;
} }
@Override @Override
...@@ -233,7 +234,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream ...@@ -233,7 +234,7 @@ public final class DataSourceStream implements Loadable, NonBlockingInputStream
} }
try { try {
DataSpec loadDataSpec; DataSpec loadDataSpec;
if (resolvedLength == DataSpec.LENGTH_UNBOUNDED) { if (resolvedLength == C.LENGTH_UNBOUNDED) {
loadDataSpec = dataSpec; loadDataSpec = dataSpec;
resolvedLength = dataSource.open(loadDataSpec); resolvedLength = dataSource.open(loadDataSpec);
if (resolvedLength > Integer.MAX_VALUE) { if (resolvedLength > Integer.MAX_VALUE) {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import android.net.Uri; import android.net.Uri;
...@@ -25,13 +26,6 @@ import android.net.Uri; ...@@ -25,13 +26,6 @@ import android.net.Uri;
public final class DataSpec { public final class DataSpec {
/** /**
* A permitted value of {@link #length}. A {@link DataSpec} defined with this length represents
* the region of media data that starts at its {@link #position} and extends to the end of the
* data whose location is defined by its {@link #uri}.
*/
public static final int LENGTH_UNBOUNDED = -1;
/**
* Identifies the source from which data should be read. * Identifies the source from which data should be read.
*/ */
public final Uri uri; public final Uri uri;
...@@ -50,7 +44,7 @@ public final class DataSpec { ...@@ -50,7 +44,7 @@ public final class DataSpec {
*/ */
public final long position; public final long position;
/** /**
* The length of the data. Greater than zero, or equal to {@link #LENGTH_UNBOUNDED}. * The length of the data. Greater than zero, or equal to {@link C#LENGTH_UNBOUNDED}.
*/ */
public final long length; public final long length;
/** /**
...@@ -98,7 +92,7 @@ public final class DataSpec { ...@@ -98,7 +92,7 @@ public final class DataSpec {
boolean uriIsFullStream) { boolean uriIsFullStream) {
Assertions.checkArgument(absoluteStreamPosition >= 0); Assertions.checkArgument(absoluteStreamPosition >= 0);
Assertions.checkArgument(position >= 0); Assertions.checkArgument(position >= 0);
Assertions.checkArgument(length > 0 || length == LENGTH_UNBOUNDED); Assertions.checkArgument(length > 0 || length == C.LENGTH_UNBOUNDED);
Assertions.checkArgument(absoluteStreamPosition == position || !uriIsFullStream); Assertions.checkArgument(absoluteStreamPosition == position || !uriIsFullStream);
this.uri = uri; this.uri = uri;
this.uriIsFullStream = uriIsFullStream; this.uriIsFullStream = uriIsFullStream;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
...@@ -42,8 +44,7 @@ public final class FileDataSource implements DataSource { ...@@ -42,8 +44,7 @@ public final class FileDataSource implements DataSource {
try { try {
file = new RandomAccessFile(dataSpec.uri.getPath(), "r"); file = new RandomAccessFile(dataSpec.uri.getPath(), "r");
file.seek(dataSpec.position); file.seek(dataSpec.position);
bytesRemaining = dataSpec.length == DataSpec.LENGTH_UNBOUNDED bytesRemaining = dataSpec.length == C.LENGTH_UNBOUNDED ? file.length() - dataSpec.position
? file.length() - dataSpec.position
: dataSpec.length; : dataSpec.length;
return bytesRemaining; return bytesRemaining;
} catch (IOException e) { } catch (IOException e) {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.Predicate; import com.google.android.exoplayer.util.Predicate;
import com.google.android.exoplayer.util.Util; import com.google.android.exoplayer.util.Util;
...@@ -258,16 +259,16 @@ public class HttpDataSource implements DataSource { ...@@ -258,16 +259,16 @@ public class HttpDataSource implements DataSource {
} }
long contentLength = getContentLength(connection); long contentLength = getContentLength(connection);
dataLength = dataSpec.length == DataSpec.LENGTH_UNBOUNDED ? contentLength : dataSpec.length; dataLength = dataSpec.length == C.LENGTH_UNBOUNDED ? contentLength : dataSpec.length;
if (dataLength == DataSpec.LENGTH_UNBOUNDED) { if (dataLength == C.LENGTH_UNBOUNDED) {
// The DataSpec specified unbounded length and we failed to resolve a length from the // The DataSpec specified unbounded length and we failed to resolve a length from the
// response headers. // response headers.
throw new HttpDataSourceException( throw new HttpDataSourceException(
new UnexpectedLengthException(DataSpec.LENGTH_UNBOUNDED, DataSpec.LENGTH_UNBOUNDED), new UnexpectedLengthException(C.LENGTH_UNBOUNDED, C.LENGTH_UNBOUNDED),
dataSpec); dataSpec);
} }
if (dataSpec.length != DataSpec.LENGTH_UNBOUNDED && contentLength != DataSpec.LENGTH_UNBOUNDED if (dataSpec.length != C.LENGTH_UNBOUNDED && contentLength != C.LENGTH_UNBOUNDED
&& contentLength != dataSpec.length) { && contentLength != dataSpec.length) {
// The DataSpec specified a length and we resolved a length from the response headers, but // The DataSpec specified a length and we resolved a length from the response headers, but
// the two lengths do not match. // the two lengths do not match.
...@@ -394,14 +395,14 @@ public class HttpDataSource implements DataSource { ...@@ -394,14 +395,14 @@ public class HttpDataSource implements DataSource {
private String buildRangeHeader(DataSpec dataSpec) { private String buildRangeHeader(DataSpec dataSpec) {
String rangeRequest = "bytes=" + dataSpec.position + "-"; String rangeRequest = "bytes=" + dataSpec.position + "-";
if (dataSpec.length != DataSpec.LENGTH_UNBOUNDED) { if (dataSpec.length != C.LENGTH_UNBOUNDED) {
rangeRequest += (dataSpec.position + dataSpec.length - 1); rangeRequest += (dataSpec.position + dataSpec.length - 1);
} }
return rangeRequest; return rangeRequest;
} }
private long getContentLength(HttpURLConnection connection) { private long getContentLength(HttpURLConnection connection) {
long contentLength = DataSpec.LENGTH_UNBOUNDED; long contentLength = C.LENGTH_UNBOUNDED;
String contentLengthHeader = connection.getHeaderField("Content-Length"); String contentLengthHeader = connection.getHeaderField("Content-Length");
if (!TextUtils.isEmpty(contentLengthHeader)) { if (!TextUtils.isEmpty(contentLengthHeader)) {
try { try {
...@@ -435,7 +436,7 @@ public class HttpDataSource implements DataSource { ...@@ -435,7 +436,7 @@ public class HttpDataSource implements DataSource {
} }
} }
} }
if (contentLength == DataSpec.LENGTH_UNBOUNDED) { if (contentLength == C.LENGTH_UNBOUNDED) {
Log.w(TAG, "Unable to parse content length [" + contentLengthHeader + "] [" + Log.w(TAG, "Unable to parse content length [" + contentLengthHeader + "] [" +
contentRangeHeader + "]"); contentRangeHeader + "]");
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream; package com.google.android.exoplayer.upstream;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import java.io.IOException; import java.io.IOException;
...@@ -39,7 +40,7 @@ public final class TeeDataSource implements DataSource { ...@@ -39,7 +40,7 @@ public final class TeeDataSource implements DataSource {
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
long dataLength = upstream.open(dataSpec); long dataLength = upstream.open(dataSpec);
if (dataSpec.length == DataSpec.LENGTH_UNBOUNDED) { if (dataSpec.length == C.LENGTH_UNBOUNDED) {
// Reconstruct dataSpec in order to provide the resolved length to the sink. // Reconstruct dataSpec in order to provide the resolved length to the sink.
dataSpec = new DataSpec(dataSpec.uri, dataSpec.absoluteStreamPosition, dataLength, dataSpec = new DataSpec(dataSpec.uri, dataSpec.absoluteStreamPosition, dataLength,
dataSpec.key, dataSpec.position, dataSpec.uriIsFullStream); dataSpec.key, dataSpec.position, dataSpec.uriIsFullStream);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer.upstream.cache; package com.google.android.exoplayer.upstream.cache;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.DataSink; import com.google.android.exoplayer.upstream.DataSink;
import com.google.android.exoplayer.upstream.DataSource; import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec; import com.google.android.exoplayer.upstream.DataSpec;
...@@ -124,7 +125,7 @@ public final class CacheDataSource implements DataSource { ...@@ -124,7 +125,7 @@ public final class CacheDataSource implements DataSource {
Assertions.checkState(dataSpec.uriIsFullStream); Assertions.checkState(dataSpec.uriIsFullStream);
// TODO: Support caching for unbounded requests. This requires storing the source length // TODO: Support caching for unbounded requests. This requires storing the source length
// into the cache (the simplest approach is to incorporate it into each cache file's name). // into the cache (the simplest approach is to incorporate it into each cache file's name).
Assertions.checkState(dataSpec.length != DataSpec.LENGTH_UNBOUNDED); Assertions.checkState(dataSpec.length != C.LENGTH_UNBOUNDED);
try { try {
uri = dataSpec.uri; uri = dataSpec.uri;
key = dataSpec.key; key = dataSpec.key;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment