diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/UriSampleSourceProvider.java b/demo/src/main/java/com/google/android/exoplayer/demo/UriSampleSourceProvider.java index 51037917ae..85bd345eda 100644 --- a/demo/src/main/java/com/google/android/exoplayer/demo/UriSampleSourceProvider.java +++ b/demo/src/main/java/com/google/android/exoplayer/demo/UriSampleSourceProvider.java @@ -15,7 +15,6 @@ */ package com.google.android.exoplayer.demo; -import com.google.android.exoplayer.C; import com.google.android.exoplayer.SampleSource; import com.google.android.exoplayer.SampleSourceProvider; import com.google.android.exoplayer.SimpleExoPlayer; @@ -23,10 +22,7 @@ import com.google.android.exoplayer.dash.DashSampleSource; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.hls.HlsSampleSource; import com.google.android.exoplayer.smoothstreaming.SmoothStreamingSampleSource; -import com.google.android.exoplayer.upstream.Allocator; -import com.google.android.exoplayer.upstream.DataSource; import com.google.android.exoplayer.upstream.DataSourceFactory; -import com.google.android.exoplayer.upstream.DefaultAllocator; import com.google.android.exoplayer.util.Util; import android.net.Uri; @@ -116,10 +112,8 @@ public final class UriSampleSourceProvider implements SampleSourceProvider { return new HlsSampleSource(uri, dataSourceFactory, player.getBandwidthMeter(), handler, eventLogger); case Util.TYPE_OTHER: - Allocator allocator = new DefaultAllocator(C.DEFAULT_BUFFER_SEGMENT_SIZE); - DataSource dataSource = dataSourceFactory.createDataSource(player.getBandwidthMeter()); - return new ExtractorSampleSource(uri, dataSource, allocator, C.DEFAULT_MUXED_BUFFER_SIZE, - handler, eventLogger, 0, ExtractorSampleSource.newDefaultExtractors()); + return new ExtractorSampleSource(uri, dataSourceFactory, player.getBandwidthMeter(), + ExtractorSampleSource.newDefaultExtractors(), handler, eventLogger, 0); default: throw new IllegalStateException("Unsupported type: " + type); } diff --git a/extensions/flac/src/androidTest/java/com/google/android/exoplayer/ext/flac/FlacPlaybackTest.java b/extensions/flac/src/androidTest/java/com/google/android/exoplayer/ext/flac/FlacPlaybackTest.java index 848ca5e22f..ff0b6f9ddd 100644 --- a/extensions/flac/src/androidTest/java/com/google/android/exoplayer/ext/flac/FlacPlaybackTest.java +++ b/extensions/flac/src/androidTest/java/com/google/android/exoplayer/ext/flac/FlacPlaybackTest.java @@ -24,9 +24,7 @@ import com.google.android.exoplayer.TrackRenderer; import com.google.android.exoplayer.extractor.Extractor; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.mkv.MatroskaExtractor; -import com.google.android.exoplayer.upstream.DefaultAllocator; -import com.google.android.exoplayer.upstream.DefaultDataSource; -import com.google.android.exoplayer.util.Util; +import com.google.android.exoplayer.upstream.DefaultDataSourceFactory; import android.content.Context; import android.net.Uri; @@ -60,9 +58,6 @@ public class FlacPlaybackTest extends InstrumentationTestCase { private static class TestPlaybackThread extends Thread implements ExoPlayer.EventListener { - private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; - private static final int BUFFER_SEGMENT_COUNT = 16; - private final Context context; private final Uri uri; @@ -84,10 +79,9 @@ public class FlacPlaybackTest extends InstrumentationTestCase { player.addListener(this); ExtractorSampleSource sampleSource = new ExtractorSampleSource( uri, - new DefaultDataSource(context, null, Util.getUserAgent(context, "ExoPlayerExtFlacTest"), - false), - new DefaultAllocator(BUFFER_SEGMENT_SIZE), BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT, - new Extractor[] {new MatroskaExtractor()}); + new DefaultDataSourceFactory(context, "ExoPlayerExtFlacTest"), null, + new Extractor[] {new MatroskaExtractor()}, + null, null, 0); player.setSource(sampleSource); player.setPlayWhenReady(true); Looper.loop(); diff --git a/extensions/opus/src/androidTest/java/com/google/android/exoplayer/ext/opus/OpusPlaybackTest.java b/extensions/opus/src/androidTest/java/com/google/android/exoplayer/ext/opus/OpusPlaybackTest.java index b1f57e1361..00368af498 100644 --- a/extensions/opus/src/androidTest/java/com/google/android/exoplayer/ext/opus/OpusPlaybackTest.java +++ b/extensions/opus/src/androidTest/java/com/google/android/exoplayer/ext/opus/OpusPlaybackTest.java @@ -24,9 +24,7 @@ import com.google.android.exoplayer.TrackRenderer; import com.google.android.exoplayer.extractor.Extractor; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.mkv.MatroskaExtractor; -import com.google.android.exoplayer.upstream.DefaultAllocator; -import com.google.android.exoplayer.upstream.DefaultDataSource; -import com.google.android.exoplayer.util.Util; +import com.google.android.exoplayer.upstream.DefaultDataSourceFactory; import android.content.Context; import android.net.Uri; @@ -60,9 +58,6 @@ public class OpusPlaybackTest extends InstrumentationTestCase { private static class TestPlaybackThread extends Thread implements ExoPlayer.EventListener { - private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; - private static final int BUFFER_SEGMENT_COUNT = 16; - private final Context context; private final Uri uri; @@ -84,10 +79,9 @@ public class OpusPlaybackTest extends InstrumentationTestCase { player.addListener(this); ExtractorSampleSource sampleSource = new ExtractorSampleSource( uri, - new DefaultDataSource(context, null, Util.getUserAgent(context, "ExoPlayerExtOpusTest"), - false), - new DefaultAllocator(BUFFER_SEGMENT_SIZE), BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT, - new Extractor[] {new MatroskaExtractor()}); + new DefaultDataSourceFactory(context, "ExoPlayerExtOpusTest"), null, + new Extractor[] {new MatroskaExtractor()}, + null, null, 0); player.setSource(sampleSource); player.setPlayWhenReady(true); Looper.loop(); diff --git a/extensions/vp9/src/androidTest/java/com/google/android/exoplayer/ext/vp9/VpxPlaybackTest.java b/extensions/vp9/src/androidTest/java/com/google/android/exoplayer/ext/vp9/VpxPlaybackTest.java index 18c80be063..068bfec351 100644 --- a/extensions/vp9/src/androidTest/java/com/google/android/exoplayer/ext/vp9/VpxPlaybackTest.java +++ b/extensions/vp9/src/androidTest/java/com/google/android/exoplayer/ext/vp9/VpxPlaybackTest.java @@ -24,9 +24,7 @@ import com.google.android.exoplayer.TrackRenderer; import com.google.android.exoplayer.extractor.Extractor; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.mkv.MatroskaExtractor; -import com.google.android.exoplayer.upstream.DefaultAllocator; -import com.google.android.exoplayer.upstream.DefaultDataSource; -import com.google.android.exoplayer.util.Util; +import com.google.android.exoplayer.upstream.DefaultDataSourceFactory; import android.content.Context; import android.net.Uri; @@ -76,9 +74,6 @@ public class VpxPlaybackTest extends InstrumentationTestCase { private static class TestPlaybackThread extends Thread implements ExoPlayer.EventListener { - private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; - private static final int BUFFER_SEGMENT_COUNT = 16; - private final Context context; private final Uri uri; @@ -100,10 +95,9 @@ public class VpxPlaybackTest extends InstrumentationTestCase { player.addListener(this); ExtractorSampleSource sampleSource = new ExtractorSampleSource( uri, - new DefaultDataSource(context, null, Util.getUserAgent(context, "ExoPlayerExtVP9Test"), - false), - new DefaultAllocator(BUFFER_SEGMENT_SIZE), BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT, - new Extractor[] {new MatroskaExtractor()}); + new DefaultDataSourceFactory(context, "ExoPlayerExtVp9Test"), null, + new Extractor[] {new MatroskaExtractor()}, + null, null, 0); player.sendMessages(new ExoPlayer.ExoPlayerMessage(videoRenderer, LibvpxVideoTrackRenderer.MSG_SET_OUTPUT_BUFFER_RENDERER, new VpxVideoSurfaceView(context))); diff --git a/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java b/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java index 5aafba6b44..34f1d9a4f4 100644 --- a/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java +++ b/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java @@ -25,8 +25,11 @@ import com.google.android.exoplayer.TrackGroupArray; import com.google.android.exoplayer.TrackSelection; import com.google.android.exoplayer.TrackStream; import com.google.android.exoplayer.upstream.Allocator; +import com.google.android.exoplayer.upstream.BandwidthMeter; import com.google.android.exoplayer.upstream.DataSource; +import com.google.android.exoplayer.upstream.DataSourceFactory; import com.google.android.exoplayer.upstream.DataSpec; +import com.google.android.exoplayer.upstream.DefaultAllocator; import com.google.android.exoplayer.upstream.Loader; import com.google.android.exoplayer.upstream.Loader.Loadable; import com.google.android.exoplayer.util.Assertions; @@ -112,16 +115,15 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu // Lazily initialized default extractor classes in priority order. private static List> defaultExtractorClasses; - private final Loader loader; - private final ExtractorHolder extractorHolder; - private final Allocator allocator; - private final int requestedBufferSize; - private final int minLoadableRetryCount; private final Uri uri; - private final DataSource dataSource; + private final int minLoadableRetryCount; private final Handler eventHandler; private final EventListener eventListener; private final int eventSourceId; + private final DataSource dataSource; + private final Allocator allocator; + private final Loader loader; + private final ExtractorHolder extractorHolder; private volatile boolean tracksBuilt; private volatile SeekMap seekMap; @@ -144,91 +146,46 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu /** * @param uri The {@link Uri} of the media stream. - * @param dataSource A data source to read the media stream. - * @param allocator An {@link Allocator} from which to obtain memory allocations. - * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. - * The actual allocated size may exceed the value passed in if the implementation requires it. + * @param dataSourceFactory A factory for {@link DataSource}s to read the media. + * @param bandwidthMeter A {@link BandwidthMeter} to notify of loads performed by the source. * @param extractors {@link Extractor}s to process the media stream. Where the possible formats * are known, instantiate and inject only instances of the corresponding {@link Extractor}s. * Where this is not possible, {@link #newDefaultExtractors()} can be used to construct an * array of default extractors. - */ - public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, - int requestedBufferSize, Extractor[] extractors) { - this(uri, dataSource, allocator, requestedBufferSize, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, - extractors); - } - - /** - * @param uri The {@link Uri} of the media stream. - * @param dataSource A data source to read the media stream. - * @param allocator An {@link Allocator} from which to obtain memory allocations. - * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. - * The actual allocated size may exceed the value passed in if the implementation requires it. - * @param eventHandler A handler to use when delivering events to {@code eventListener}. May be - * null if delivery of events is not required. * @param eventListener A listener of events. May be null if delivery of events is not required. * @param eventSourceId An identifier that gets passed to {@code eventListener} methods. - * @param extractors {@link Extractor}s to process the media stream. Where the possible formats - * are known, instantiate and inject only instances of the corresponding {@link Extractor}s. - * Where this is not possible, {@link #newDefaultExtractors()} can be used to construct an - * array of default extractors. */ - public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, - int requestedBufferSize, Handler eventHandler, EventListener eventListener, - int eventSourceId, Extractor[] extractors) { - this(uri, dataSource, allocator, requestedBufferSize, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, - eventHandler, eventListener, eventSourceId, extractors); + public ExtractorSampleSource(Uri uri, DataSourceFactory dataSourceFactory, + BandwidthMeter bandwidthMeter, Extractor[] extractors, Handler eventHandler, + EventListener eventListener, int eventSourceId) { + this(uri, dataSourceFactory, bandwidthMeter, extractors, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, + eventHandler, eventListener, eventSourceId); } /** * @param uri The {@link Uri} of the media stream. - * @param dataSource A data source to read the media stream. - * @param allocator An {@link Allocator} from which to obtain memory allocations. - * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. - * The actual allocated size may exceed the value passed in if the implementation requires it. - * @param minLoadableRetryCount The minimum number of times that the sample source will retry - * if a loading error occurs. + * @param dataSourceFactory A factory for {@link DataSource}s to read the media. + * @param bandwidthMeter A {@link BandwidthMeter} to notify of loads performed by the source. * @param extractors {@link Extractor}s to process the media stream. Where the possible formats * are known, instantiate and inject only instances of the corresponding {@link Extractor}s. * Where this is not possible, {@link #newDefaultExtractors()} can be used to construct an * array of default extractors. - */ - public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, - int requestedBufferSize, int minLoadableRetryCount, Extractor[] extractors) { - this(uri, dataSource, allocator, requestedBufferSize, minLoadableRetryCount, null, null, 0, - extractors); - } - - /** - * @param uri The {@link Uri} of the media stream. - * @param dataSource A data source to read the media stream. - * @param allocator An {@link Allocator} from which to obtain memory allocations. - * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. - * The actual allocated size may exceed the value passed in if the implementation requires it. * @param minLoadableRetryCount The minimum number of times that the sample source will retry * if a loading error occurs. - * @param eventHandler A handler to use when delivering events to {@code eventListener}. May be - * null if delivery of events is not required. * @param eventListener A listener of events. May be null if delivery of events is not required. * @param eventSourceId An identifier that gets passed to {@code eventListener} methods. - * @param extractors {@link Extractor}s to process the media stream. Where the possible formats - * are known, instantiate and inject only instances of the corresponding {@link Extractor}s. - * Where this is not possible {@link #newDefaultExtractors()} can be used to construct an - * array of default extractors. */ - public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, - int requestedBufferSize, int minLoadableRetryCount, Handler eventHandler, - EventListener eventListener, int eventSourceId, Extractor[] extractors) { + public ExtractorSampleSource(Uri uri, DataSourceFactory dataSourceFactory, + BandwidthMeter bandwidthMeter, Extractor[] extractors, int minLoadableRetryCount, + Handler eventHandler, EventListener eventListener, int eventSourceId) { Assertions.checkState(extractors != null && extractors.length > 0); this.uri = uri; - this.dataSource = dataSource; + this.minLoadableRetryCount = minLoadableRetryCount; this.eventListener = eventListener; this.eventHandler = eventHandler; this.eventSourceId = eventSourceId; - this.allocator = allocator; - this.requestedBufferSize = requestedBufferSize; - this.minLoadableRetryCount = minLoadableRetryCount; + dataSource = dataSourceFactory.createDataSource(bandwidthMeter); + allocator = new DefaultAllocator(C.DEFAULT_BUFFER_SEGMENT_SIZE); loader = new Loader("Loader:ExtractorSampleSource"); extractorHolder = new ExtractorHolder(extractors, this); pendingResetPositionUs = C.UNSET_TIME_US; @@ -573,7 +530,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu private void startLoading() { ExtractingLoadable loadable = new ExtractingLoadable(uri, dataSource, extractorHolder, - allocator, requestedBufferSize); + allocator, C.DEFAULT_MUXED_BUFFER_SIZE); if (prepared) { Assertions.checkState(isPendingReset()); if (durationUs != C.UNSET_TIME_US && pendingResetPositionUs >= durationUs) {