Handle preload callbacks asynchronously in PreloadMediaSource

When there is an exception thrown from the `LoadTask`, the `Loader` will call `Loader.Callback.onLoadError`. Some implementations of `onLoadError` method may call `MediaPeriod.onContinueLoadingRequested`, and in the `PreloadMediaSource`, its `PreloadMediaPeriodCallback` will be triggered and then it can further call `continueLoading` if it finds needed. However the above process is currently done synchronously, which will cause problem. By calling `continueLoading`, the `Loader` is set with a `currentTask`, and when that long sync logic in `Loader.Callback.onLoadError` ends, the `Loader` will immediately retry, and then a non-null `currentTask` will cause the `IllegalStateException`.

Issue: androidx/media#1568

PiperOrigin-RevId: 662550622
(cherry picked from commit cd532c5fb2)
This commit is contained in:
tianyifeng 2024-08-13 09:43:07 -07:00 committed by Tianyi Feng
parent 07e9c659d7
commit f139d709c7
2 changed files with 64 additions and 53 deletions

View file

@ -4,6 +4,8 @@
* Common Library: * Common Library:
* ExoPlayer: * ExoPlayer:
* Handle preload callbacks asynchronously in `PreloadMediaSource`
([#1568](https://github.com/androidx/media/issues/1568)).
* Transformer: * Transformer:
* Track Selection: * Track Selection:
* Extractors: * Extractors:

View file

@ -293,25 +293,28 @@ public final class PreloadMediaSource extends WrappingMediaSource {
protected void onChildSourceInfoRefreshed(Timeline newTimeline) { protected void onChildSourceInfoRefreshed(Timeline newTimeline) {
this.timeline = newTimeline; this.timeline = newTimeline;
refreshSourceInfo(newTimeline); refreshSourceInfo(newTimeline);
if (isUsedByPlayer() || onSourcePreparedNotified) { preloadHandler.post(
return; () -> {
} if (isUsedByPlayer() || onSourcePreparedNotified) {
onSourcePreparedNotified = true; return;
if (!preloadControl.onSourcePrepared(this)) { }
return; onSourcePreparedNotified = true;
} if (!preloadControl.onSourcePrepared(this)) {
Pair<Object, Long> periodPosition = return;
newTimeline.getPeriodPositionUs( }
new Timeline.Window(), Pair<Object, Long> periodPosition =
new Timeline.Period(), newTimeline.getPeriodPositionUs(
/* windowIndex= */ 0, new Timeline.Window(),
/* windowPositionUs= */ startPositionUs); new Timeline.Period(),
MediaPeriodId mediaPeriodId = new MediaPeriodId(periodPosition.first); /* windowIndex= */ 0,
PreloadMediaPeriod mediaPeriod = /* windowPositionUs= */ startPositionUs);
PreloadMediaSource.this.createPeriod(mediaPeriodId, allocator, periodPosition.second); MediaPeriodId mediaPeriodId = new MediaPeriodId(periodPosition.first);
mediaPeriod.preload( PreloadMediaPeriod mediaPeriod =
new PreloadMediaPeriodCallback(periodPosition.second), PreloadMediaSource.this.createPeriod(mediaPeriodId, allocator, periodPosition.second);
/* positionUs= */ periodPosition.second); mediaPeriod.preload(
new PreloadMediaPeriodCallback(periodPosition.second),
/* positionUs= */ periodPosition.second);
});
} }
@Override @Override
@ -405,44 +408,50 @@ public final class PreloadMediaSource extends WrappingMediaSource {
@Override @Override
public void onPrepared(MediaPeriod mediaPeriod) { public void onPrepared(MediaPeriod mediaPeriod) {
prepared = true; prepared = true;
if (isUsedByPlayer()) { preloadHandler.post(
return; () -> {
} if (isUsedByPlayer()) {
PreloadMediaPeriod preloadMediaPeriod = (PreloadMediaPeriod) mediaPeriod; return;
TrackGroupArray trackGroups = preloadMediaPeriod.getTrackGroups(); }
@Nullable TrackSelectorResult trackSelectorResult = null; PreloadMediaPeriod preloadMediaPeriod = (PreloadMediaPeriod) mediaPeriod;
MediaPeriodKey key = checkNotNull(preloadingMediaPeriodAndKey).second; TrackGroupArray trackGroups = preloadMediaPeriod.getTrackGroups();
try { @Nullable TrackSelectorResult trackSelectorResult = null;
trackSelectorResult = MediaPeriodKey key = checkNotNull(preloadingMediaPeriodAndKey).second;
trackSelector.selectTracks( try {
rendererCapabilities, trackGroups, key.mediaPeriodId, checkNotNull(timeline)); trackSelectorResult =
} catch (ExoPlaybackException e) { trackSelector.selectTracks(
Log.e(TAG, "Failed to select tracks", e); rendererCapabilities, trackGroups, key.mediaPeriodId, checkNotNull(timeline));
} } catch (ExoPlaybackException e) {
if (trackSelectorResult != null) { Log.e(TAG, "Failed to select tracks", e);
preloadMediaPeriod.selectTracksForPreloading( }
trackSelectorResult.selections, periodStartPositionUs); if (trackSelectorResult != null) {
if (preloadControl.onTracksSelected(PreloadMediaSource.this)) { preloadMediaPeriod.selectTracksForPreloading(
preloadMediaPeriod.continueLoading( trackSelectorResult.selections, periodStartPositionUs);
new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build()); if (preloadControl.onTracksSelected(PreloadMediaSource.this)) {
} preloadMediaPeriod.continueLoading(
} new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build());
}
}
});
} }
@Override @Override
public void onContinueLoadingRequested(MediaPeriod mediaPeriod) { public void onContinueLoadingRequested(MediaPeriod mediaPeriod) {
if (isUsedByPlayer()) { preloadHandler.post(
return; () -> {
} if (isUsedByPlayer()) {
PreloadMediaPeriod preloadMediaPeriod = (PreloadMediaPeriod) mediaPeriod; return;
if (prepared && mediaPeriod.getBufferedPositionUs() == C.TIME_END_OF_SOURCE) { }
preloadControl.onLoadedToTheEndOfSource(PreloadMediaSource.this); PreloadMediaPeriod preloadMediaPeriod = (PreloadMediaPeriod) mediaPeriod;
} else if (!prepared if (prepared && mediaPeriod.getBufferedPositionUs() == C.TIME_END_OF_SOURCE) {
|| preloadControl.onContinueLoadingRequested( preloadControl.onLoadedToTheEndOfSource(PreloadMediaSource.this);
PreloadMediaSource.this, preloadMediaPeriod.getBufferedPositionUs())) { } else if (!prepared
preloadMediaPeriod.continueLoading( || preloadControl.onContinueLoadingRequested(
new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build()); PreloadMediaSource.this, preloadMediaPeriod.getBufferedPositionUs())) {
} preloadMediaPeriod.continueLoading(
new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build());
}
});
} }
} }