Skip to content

Commit

Permalink
Post onPrepared so it runs after createPeriod has finished.
Browse files Browse the repository at this point in the history
Issue: #1853

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134409897
  • Loading branch information
andrewlewis authored and ojw28 committed Sep 28, 2016
1 parent 85b61ad commit 4a62b26
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ interface Listener {
/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand Down Expand Up @@ -125,7 +124,6 @@ public MediaPeriod createPeriod(int index, Callback callback, Allocator allocato
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;

Expand All @@ -60,7 +61,7 @@

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;
Expand All @@ -70,12 +71,20 @@ public SingleSampleMediaPeriod(Uri uri, DataSource.Factory dataSourceFactory, Fo
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ public void maybeThrowSourceInfoRefreshError() throws IOException {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +52,7 @@
private final Callback callback;
private final Allocator allocator;
private final TrackGroupArray trackGroups;
private final Handler handler;

private ChunkSampleStream<DashChunkSource>[] sampleStreams;
private CompositeSequenceableLoader sequenceableLoader;
Expand All @@ -76,7 +78,13 @@ public DashMediaPeriod(int id, DashManifest manifest, int index,
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) {
Expand All @@ -95,6 +103,7 @@ public void release() {
for (ChunkSampleStream<DashChunkSource> sampleStream : sampleStreams) {
sampleStream.release();
}
handler.removeCallbacksAndMessages(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,7 @@
private final Allocator allocator;
private final TrackGroupArray trackGroups;
private final TrackEncryptionBox[] trackEncryptionBoxes;
private final Handler handler;

private SsManifest manifest;
private ChunkSampleStream<SsChunkSource>[] sampleStreams;
Expand All @@ -74,10 +76,16 @@ public SsMediaPeriod(SsManifest manifest, SsChunkSource.Factory chunkSourceFacto
} 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) {
Expand All @@ -92,6 +100,7 @@ public void release() {
for (ChunkSampleStream<SsChunkSource> sampleStream : sampleStreams) {
sampleStream.release();
}
handler.removeCallbacksAndMessages(null);
}

@Override
Expand Down

0 comments on commit 4a62b26

Please sign in to comment.