Inject Allocator into ExtractorSampleSource.

This commit is contained in:
Oliver Woodman 2015-07-15 11:05:15 +01:00
parent b4879236b5
commit fe52a486f4
5 changed files with 64 additions and 18 deletions

View File

@ -24,7 +24,9 @@ import com.google.android.exoplayer.extractor.Extractor;
import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.ExtractorSampleSource;
import com.google.android.exoplayer.text.TextTrackRenderer; import com.google.android.exoplayer.text.TextTrackRenderer;
import com.google.android.exoplayer.text.tx3g.Tx3gParser; 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.DataSource;
import com.google.android.exoplayer.upstream.DefaultAllocator;
import com.google.android.exoplayer.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer.upstream.DefaultUriDataSource; import com.google.android.exoplayer.upstream.DefaultUriDataSource;
@ -37,7 +39,8 @@ import android.net.Uri;
*/ */
public class ExtractorRendererBuilder implements RendererBuilder { 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 Context context;
private final String userAgent; private final String userAgent;
@ -53,12 +56,14 @@ public class ExtractorRendererBuilder implements RendererBuilder {
@Override @Override
public void buildRenderers(DemoPlayer player, RendererBuilderCallback callback) { public void buildRenderers(DemoPlayer player, RendererBuilderCallback callback) {
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
// Build the video and audio renderers. // Build the video and audio renderers.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(player.getMainHandler(), DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(player.getMainHandler(),
null); null);
DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, extractor, ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, extractor,
BUFFER_SIZE); allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource, MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource,
null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000, null, player.getMainHandler(), null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000, null, player.getMainHandler(),
player, 50); player, 50);

View File

@ -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.ext.vp9.VpxVideoSurfaceView;
import com.google.android.exoplayer.extractor.ExtractorSampleSource; import com.google.android.exoplayer.extractor.ExtractorSampleSource;
import com.google.android.exoplayer.extractor.webm.WebmExtractor; 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.upstream.DefaultUriDataSource;
import com.google.android.exoplayer.util.PlayerControl; import com.google.android.exoplayer.util.PlayerControl;
import com.google.android.exoplayer.util.Util; 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"; public static final String USE_OPENGL_ID_EXTRA = "use_opengl";
private static final int FILE_PICKER_REQUEST = 1; 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 boolean isDash;
private String manifestUrl; private String manifestUrl;
@ -161,7 +164,8 @@ public class VideoPlayer extends Activity implements OnClickListener,
ExtractorSampleSource sampleSource = new ExtractorSampleSource( ExtractorSampleSource sampleSource = new ExtractorSampleSource(
Uri.fromFile(new File(filename)), Uri.fromFile(new File(filename)),
new DefaultUriDataSource(this, Util.getUserAgent(this, "ExoPlayerExtWebMDemo")), 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 = TrackRenderer videoRenderer =
new LibvpxVideoTrackRenderer(sampleSource, true, handler, this, 50); new LibvpxVideoTrackRenderer(sampleSource, true, handler, this, 50);
if (useOpenGL) { if (useOpenGL) {

View File

@ -54,12 +54,11 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader,
*/ */
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE = 6; 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 MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA = -1;
private static final int NO_RESET_PENDING = -1; private static final int NO_RESET_PENDING = -1;
private final Extractor extractor; private final Extractor extractor;
private final DefaultAllocator allocator; private final Allocator allocator;
private final int requestedBufferSize; private final int requestedBufferSize;
private final SparseArray<InternalTrackOutput> sampleQueues; private final SparseArray<InternalTrackOutput> sampleQueues;
private final int minLoadableRetryCount; 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. * @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. * The actual allocated size may exceed the value passed in if the implementation requires it.
*/ */
@Deprecated
public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor,
int requestedBufferSize) { 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 * @param minLoadableRetryCount The minimum number of times that the sample source will retry
* if a loading error occurs. * if a loading error occurs.
*/ */
@Deprecated
public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor, public ExtractorSampleSource(Uri uri, DataSource dataSource, Extractor extractor,
int requestedBufferSize, int minLoadableRetryCount) { 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.uri = uri;
this.dataSource = dataSource; this.dataSource = dataSource;
this.extractor = extractor; this.extractor = extractor;
this.allocator = allocator;
this.requestedBufferSize = requestedBufferSize; this.requestedBufferSize = requestedBufferSize;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
sampleQueues = new SparseArray<>(); sampleQueues = new SparseArray<>();
allocator = new DefaultAllocator(BUFFER_FRAGMENT_LENGTH);
pendingResetPositionUs = NO_RESET_PENDING; pendingResetPositionUs = NO_RESET_PENDING;
frameAccurateSeeking = true; frameAccurateSeeking = true;
extractor.init(this); extractor.init(this);
@ -561,7 +592,7 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader,
private final Uri uri; private final Uri uri;
private final DataSource dataSource; private final DataSource dataSource;
private final Extractor extractor; private final Extractor extractor;
private final DefaultAllocator allocator; private final Allocator allocator;
private final int requestedBufferSize; private final int requestedBufferSize;
private final PositionHolder positionHolder; private final PositionHolder positionHolder;
@ -570,7 +601,7 @@ public class ExtractorSampleSource implements SampleSource, SampleSourceReader,
private boolean pendingExtractorSeek; private boolean pendingExtractorSeek;
public ExtractingLoadable(Uri uri, DataSource dataSource, Extractor extractor, 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.uri = Assertions.checkNotNull(uri);
this.dataSource = Assertions.checkNotNull(dataSource); this.dataSource = Assertions.checkNotNull(dataSource);
this.extractor = Assertions.checkNotNull(extractor); this.extractor = Assertions.checkNotNull(extractor);

View File

@ -45,6 +45,15 @@ public interface Allocator {
*/ */
void trim(int targetSize); 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. * Returns the total number of bytes currently allocated.
*/ */

View File

@ -143,14 +143,6 @@ public final class DefaultAllocator implements Allocator {
} }
@Override @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) public synchronized void blockWhileTotalBytesAllocatedExceeds(int limit)
throws InterruptedException { throws InterruptedException {
while (getTotalBytesAllocated() > limit) { while (getTotalBytesAllocated() > limit) {
@ -158,4 +150,9 @@ public final class DefaultAllocator implements Allocator {
} }
} }
@Override
public int getIndividualAllocationLength() {
return individualAllocationSize;
}
} }