From fe52a486f47331a2c898802a2b2da5ac8ca645de Mon Sep 17 00:00:00 2001 From: Oliver Woodman Date: Wed, 15 Jul 2015 11:05:15 +0100 Subject: [PATCH] Inject Allocator into ExtractorSampleSource. --- .../demo/player/ExtractorRendererBuilder.java | 9 +++- .../exoplayer/demo/webm/VideoPlayer.java | 8 +++- .../extractor/ExtractorSampleSource.java | 43 ++++++++++++++++--- .../android/exoplayer/upstream/Allocator.java | 9 ++++ .../exoplayer/upstream/DefaultAllocator.java | 13 +++--- 5 files changed, 64 insertions(+), 18 deletions(-) diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/player/ExtractorRendererBuilder.java b/demo/src/main/java/com/google/android/exoplayer/demo/player/ExtractorRendererBuilder.java index b7d4eb3458..68f0645750 100644 --- a/demo/src/main/java/com/google/android/exoplayer/demo/player/ExtractorRendererBuilder.java +++ b/demo/src/main/java/com/google/android/exoplayer/demo/player/ExtractorRendererBuilder.java @@ -24,7 +24,9 @@ import com.google.android.exoplayer.extractor.Extractor; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.text.TextTrackRenderer; import com.google.android.exoplayer.text.tx3g.Tx3gParser; +import com.google.android.exoplayer.upstream.Allocator; import com.google.android.exoplayer.upstream.DataSource; +import com.google.android.exoplayer.upstream.DefaultAllocator; import com.google.android.exoplayer.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer.upstream.DefaultUriDataSource; @@ -37,7 +39,8 @@ import android.net.Uri; */ public class ExtractorRendererBuilder implements RendererBuilder { - private static final int BUFFER_SIZE = 10 * 1024 * 1024; + private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; + private static final int BUFFER_SEGMENT_COUNT = 160; private final Context context; private final String userAgent; @@ -53,12 +56,14 @@ public class ExtractorRendererBuilder implements RendererBuilder { @Override public void buildRenderers(DemoPlayer player, RendererBuilderCallback callback) { + Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); + // Build the video and audio renderers. DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(player.getMainHandler(), null); DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, extractor, - BUFFER_SIZE); + allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE); MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource, null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000, null, player.getMainHandler(), player, 50); diff --git a/demo_misc/webm_sw_decoder/src/main/java/com/google/android/exoplayer/demo/webm/VideoPlayer.java b/demo_misc/webm_sw_decoder/src/main/java/com/google/android/exoplayer/demo/webm/VideoPlayer.java index 60dfe79f78..3d449d0078 100644 --- a/demo_misc/webm_sw_decoder/src/main/java/com/google/android/exoplayer/demo/webm/VideoPlayer.java +++ b/demo_misc/webm_sw_decoder/src/main/java/com/google/android/exoplayer/demo/webm/VideoPlayer.java @@ -25,6 +25,7 @@ import com.google.android.exoplayer.ext.vp9.VpxDecoderException; import com.google.android.exoplayer.ext.vp9.VpxVideoSurfaceView; import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.webm.WebmExtractor; +import com.google.android.exoplayer.upstream.DefaultAllocator; import com.google.android.exoplayer.upstream.DefaultUriDataSource; import com.google.android.exoplayer.util.PlayerControl; import com.google.android.exoplayer.util.Util; @@ -56,7 +57,9 @@ public class VideoPlayer extends Activity implements OnClickListener, public static final String USE_OPENGL_ID_EXTRA = "use_opengl"; private static final int FILE_PICKER_REQUEST = 1; - private static final int EXTRACTOR_BUFFER_SIZE = 10 * 1024 * 1024; + private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; + private static final int BUFFER_SEGMENT_COUNT = 160; + private boolean isDash; private String manifestUrl; @@ -161,7 +164,8 @@ public class VideoPlayer extends Activity implements OnClickListener, ExtractorSampleSource sampleSource = new ExtractorSampleSource( Uri.fromFile(new File(filename)), new DefaultUriDataSource(this, Util.getUserAgent(this, "ExoPlayerExtWebMDemo")), - new WebmExtractor(), EXTRACTOR_BUFFER_SIZE); + new WebmExtractor(), new DefaultAllocator(BUFFER_SEGMENT_SIZE), + BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT); TrackRenderer videoRenderer = new LibvpxVideoTrackRenderer(sampleSource, true, handler, this, 50); if (useOpenGL) { 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 68ff1b4966..c17e3f30ad 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 @@ -54,12 +54,11 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader, */ public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE = 6; - private static final int BUFFER_FRAGMENT_LENGTH = 256 * 1024; private static final int MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA = -1; private static final int NO_RESET_PENDING = -1; private final Extractor extractor; - private final DefaultAllocator allocator; + private final Allocator allocator; private final int requestedBufferSize; private final SparseArray sampleQueues; private final int minLoadableRetryCount; @@ -106,9 +105,24 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader, * @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. */ + @Deprecated public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, int requestedBufferSize) { - this(uri, dataSource, extractor, requestedBufferSize, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA); + this(uri, dataSource, extractor, new DefaultAllocator(64 * 1024), requestedBufferSize); + } + + /** + * @param uri The {@link Uri} of the media stream. + * @param dataSource A data source to read the media stream. + * @param extractor An {@link Extractor} to extract 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. + */ + public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, + Allocator allocator, int requestedBufferSize) { + this(uri, dataSource, extractor, allocator, requestedBufferSize, + MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA); } /** @@ -120,15 +134,32 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader, * @param minLoadableRetryCount The minimum number of times that the sample source will retry * if a loading error occurs. */ + @Deprecated public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, int requestedBufferSize, int minLoadableRetryCount) { + this(uri, dataSource, extractor, new DefaultAllocator(64 * 1024), requestedBufferSize, + minLoadableRetryCount); + } + + /** + * @param uri The {@link Uri} of the media stream. + * @param dataSource A data source to read the media stream. + * @param extractor An {@link Extractor} to extract 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. + */ + public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, + Allocator allocator, int requestedBufferSize, int minLoadableRetryCount) { this.uri = uri; this.dataSource = dataSource; this.extractor = extractor; + this.allocator = allocator; this.requestedBufferSize = requestedBufferSize; this.minLoadableRetryCount = minLoadableRetryCount; sampleQueues = new SparseArray<>(); - allocator = new DefaultAllocator(BUFFER_FRAGMENT_LENGTH); pendingResetPositionUs = NO_RESET_PENDING; frameAccurateSeeking = true; extractor.init(this); @@ -561,7 +592,7 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader, private final Uri uri; private final DataSource dataSource; private final Extractor extractor; - private final DefaultAllocator allocator; + private final Allocator allocator; private final int requestedBufferSize; private final PositionHolder positionHolder; @@ -570,7 +601,7 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader, private boolean pendingExtractorSeek; public ExtractingLoadable(Uri uri, DataSource dataSource, Extractor extractor, - DefaultAllocator allocator, int requestedBufferSize, long position) { + Allocator allocator, int requestedBufferSize, long position) { this.uri = Assertions.checkNotNull(uri); this.dataSource = Assertions.checkNotNull(dataSource); this.extractor = Assertions.checkNotNull(extractor); diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java b/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java index b51217f591..db575441b6 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java @@ -45,6 +45,15 @@ public interface Allocator { */ void trim(int targetSize); + /** + * Blocks execution until the number of bytes allocated is not greater than the limit, or the + * thread is interrupted. + * + * @param limit The limit in bytes. + * @throws InterruptedException If the thread is interrupted. + */ + void blockWhileTotalBytesAllocatedExceeds(int limit) throws InterruptedException; + /** * Returns the total number of bytes currently allocated. */ diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java b/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java index a92f7f42e0..2327e909f1 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java @@ -143,14 +143,6 @@ public final class DefaultAllocator implements Allocator { } @Override - public int getIndividualAllocationLength() { - return individualAllocationSize; - } - - /** - * Blocks execution until the allocated number of bytes allocated is not greater than the - * threshold, or the thread is interrupted. - */ public synchronized void blockWhileTotalBytesAllocatedExceeds(int limit) throws InterruptedException { while (getTotalBytesAllocated() > limit) { @@ -158,4 +150,9 @@ public final class DefaultAllocator implements Allocator { } } + @Override + public int getIndividualAllocationLength() { + return individualAllocationSize; + } + }