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

#cherrypick

PiperOrigin-RevId: 662550622
This commit is contained in:
tianyifeng 2024-08-13 09:43:07 -07:00 committed by Copybara-Service
parent 0b23285bae
commit cd532c5fb2
2 changed files with 74 additions and 63 deletions

View File

@ -32,6 +32,8 @@
zero. zero.
* Allow the user to select the built-in speaker for playback on Wear OS * Allow the user to select the built-in speaker for playback on Wear OS
API 35+ (where the device advertises support for this). API 35+ (where the device advertises support for this).
* Handle preload callbacks asynchronously in `PreloadMediaSource`
([#1568](https://github.com/androidx/media/issues/1568)).
* Transformer: * Transformer:
* Add `SurfaceAssetLoader`, which supports queueing video data to * Add `SurfaceAssetLoader`, which supports queueing video data to
Transformer via a `Surface`. Transformer via a `Surface`.

View File

@ -306,6 +306,8 @@ 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);
preloadHandler.post(
() -> {
if (isUsedByPlayer() || onSourcePreparedNotified) { if (isUsedByPlayer() || onSourcePreparedNotified) {
return; return;
} }
@ -326,6 +328,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
mediaPeriod.preload( mediaPeriod.preload(
new PreloadMediaPeriodCallback(periodPosition.second), new PreloadMediaPeriodCallback(periodPosition.second),
/* positionUs= */ periodPosition.second); /* positionUs= */ periodPosition.second);
});
} }
@Override @Override
@ -455,6 +458,8 @@ public final class PreloadMediaSource extends WrappingMediaSource {
@Override @Override
public void onPrepared(MediaPeriod mediaPeriod) { public void onPrepared(MediaPeriod mediaPeriod) {
prepared = true; prepared = true;
preloadHandler.post(
() -> {
if (isUsedByPlayer()) { if (isUsedByPlayer()) {
return; return;
} }
@ -481,10 +486,13 @@ public final class PreloadMediaSource extends WrappingMediaSource {
} }
preloadMediaPeriod.continueLoading( preloadMediaPeriod.continueLoading(
new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build()); new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build());
});
} }
@Override @Override
public void onContinueLoadingRequested(MediaPeriod mediaPeriod) { public void onContinueLoadingRequested(MediaPeriod mediaPeriod) {
preloadHandler.post(
() -> {
if (isUsedByPlayer()) { if (isUsedByPlayer()) {
return; return;
} }
@ -502,6 +510,7 @@ public final class PreloadMediaSource extends WrappingMediaSource {
} }
preloadMediaPeriod.continueLoading( preloadMediaPeriod.continueLoading(
new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build()); new LoadingInfo.Builder().setPlaybackPositionUs(periodStartPositionUs).build());
});
} }
} }