Post onPrepared so it runs after createPeriod has finished.

Issue: #1853

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134409897
This commit is contained in:
andrewlewis 2016-09-27 08:32:41 -07:00 committed by Oliver Woodman
parent 85b61adb32
commit 4a62b2688c
6 changed files with 35 additions and 12 deletions

View file

@ -55,9 +55,9 @@ public interface MediaSource {
/**
* Returns a {@link MediaPeriod} corresponding to the period at the specified index.
* <p>
* {@link Callback#onPrepared(MediaPeriod)} is called when the new period is prepared. If
* preparation fails, {@link MediaPeriod#maybeThrowPrepareError()} will throw an
* {@link IOException} if called on the returned instance.
* {@link Callback#onPrepared(MediaPeriod)} is called after this method has returned, when the new
* period is prepared. If preparation fails, {@link MediaPeriod#maybeThrowPrepareError()} will
* throw an {@link IOException} if called on the returned instance.
*
* @param index The index of the period.
* @param callback A callback to receive updates from the period.

View file

@ -19,7 +19,6 @@ import android.support.annotation.IntDef;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.MediaPeriod.Callback;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -125,7 +124,6 @@ public final class MergingMediaSource implements MediaSource {
MergingMediaPeriod mergingPeriod = new MergingMediaPeriod(callback, periods);
for (int i = 0; i < periods.length; i++) {
periods[i] = mediaSources[i].createPeriod(index, mergingPeriod, allocator, positionUs);
Assertions.checkState(periods[i] != null, "Child source must not return null period");
}
return mergingPeriod;
}

View file

@ -51,6 +51,7 @@ import java.util.Arrays;
private final int eventSourceId;
private final TrackGroupArray tracks;
private final ArrayList<SampleStreamImpl> sampleStreams;
private final Handler handler;
/* package */ final Loader loader;
/* package */ final Format format;
@ -60,7 +61,7 @@ import java.util.Arrays;
public SingleSampleMediaPeriod(Uri uri, DataSource.Factory dataSourceFactory, Format format,
int minLoadableRetryCount, Handler eventHandler, EventListener eventListener,
int eventSourceId) {
int eventSourceId, final Callback callback) {
this.uri = uri;
this.dataSourceFactory = dataSourceFactory;
this.format = format;
@ -70,12 +71,20 @@ import java.util.Arrays;
this.eventSourceId = eventSourceId;
tracks = new TrackGroupArray(new TrackGroup(format));
sampleStreams = new ArrayList<>();
handler = new Handler();
loader = new Loader("Loader:SingleSampleMediaPeriod");
sampleData = new byte[INITIAL_SAMPLE_SIZE];
handler.post(new Runnable() {
@Override
public void run() {
callback.onPrepared(SingleSampleMediaPeriod.this);
}
});
}
public void release() {
loader.release();
handler.removeCallbacksAndMessages(null);
}
@Override

View file

@ -98,10 +98,8 @@ public final class SingleSampleMediaSource implements MediaSource {
public MediaPeriod createPeriod(int index, Callback callback, Allocator allocator,
long positionUs) {
Assertions.checkArgument(index == 0);
MediaPeriod mediaPeriod = new SingleSampleMediaPeriod(uri, dataSourceFactory, format,
minLoadableRetryCount, eventHandler, eventListener, eventSourceId);
callback.onPrepared(mediaPeriod);
return mediaPeriod;
return new SingleSampleMediaPeriod(uri, dataSourceFactory, format, minLoadableRetryCount,
eventHandler, eventListener, eventSourceId, callback);
}
@Override

View file

@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.source.dash;
import android.os.Handler;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.source.AdaptiveMediaSourceEventListener.EventDispatcher;
@ -51,6 +52,7 @@ import java.util.List;
private final Callback callback;
private final Allocator allocator;
private final TrackGroupArray trackGroups;
private final Handler handler;
private ChunkSampleStream<DashChunkSource>[] sampleStreams;
private CompositeSequenceableLoader sequenceableLoader;
@ -76,7 +78,13 @@ import java.util.List;
sequenceableLoader = new CompositeSequenceableLoader(sampleStreams);
period = manifest.getPeriod(index);
trackGroups = buildTrackGroups(period);
callback.onPrepared(this);
handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
DashMediaPeriod.this.callback.onPrepared(DashMediaPeriod.this);
}
});
}
public void updateManifest(DashManifest manifest, int index) {
@ -95,6 +103,7 @@ import java.util.List;
for (ChunkSampleStream<DashChunkSource> sampleStream : sampleStreams) {
sampleStream.release();
}
handler.removeCallbacksAndMessages(null);
}
@Override

View file

@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.source.smoothstreaming;
import android.os.Handler;
import android.util.Base64;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.mp4.TrackEncryptionBox;
@ -50,6 +51,7 @@ import java.util.ArrayList;
private final Allocator allocator;
private final TrackGroupArray trackGroups;
private final TrackEncryptionBox[] trackEncryptionBoxes;
private final Handler handler;
private SsManifest manifest;
private ChunkSampleStream<SsChunkSource>[] sampleStreams;
@ -74,10 +76,16 @@ import java.util.ArrayList;
} else {
trackEncryptionBoxes = null;
}
handler = new Handler();
this.manifest = manifest;
sampleStreams = newSampleStreamArray(0);
sequenceableLoader = new CompositeSequenceableLoader(sampleStreams);
callback.onPrepared(this);
handler.post(new Runnable() {
@Override
public void run() {
SsMediaPeriod.this.callback.onPrepared(SsMediaPeriod.this);
}
});
}
public void updateManifest(SsManifest manifest) {
@ -92,6 +100,7 @@ import java.util.ArrayList;
for (ChunkSampleStream<SsChunkSource> sampleStream : sampleStreams) {
sampleStream.release();
}
handler.removeCallbacksAndMessages(null);
}
@Override