mirror of
https://github.com/samsonjs/media.git
synced 2026-04-04 11:05:47 +00:00
Fix DownloadHelper for some HLS streams
onContinueLoadingRequested can occur during preparation, so MediaPreparer needs to handle it. PiperOrigin-RevId: 232507267
This commit is contained in:
parent
589af35c65
commit
6b81d9e7a4
1 changed files with 21 additions and 10 deletions
|
|
@ -744,6 +744,7 @@ public final class DownloadHelper {
|
|||
|
||||
private static final int MESSAGE_PREPARE_SOURCE = 0;
|
||||
private static final int MESSAGE_CHECK_FOR_FAILURE = 1;
|
||||
private static final int MESSAGE_CONTINUE_LOADING = 2;
|
||||
|
||||
private final MediaSource mediaSource;
|
||||
private final DownloadHelper downloadHelper;
|
||||
|
|
@ -755,7 +756,7 @@ public final class DownloadHelper {
|
|||
public @MonotonicNonNull Timeline timeline;
|
||||
public MediaPeriod @MonotonicNonNull [] mediaPeriods;
|
||||
|
||||
private int pendingPreparations;
|
||||
private final ArrayList<MediaPeriod> pendingMediaPeriods;
|
||||
|
||||
public MediaPreparer(MediaSource mediaSource, DownloadHelper downloadHelper) {
|
||||
this.mediaSource = mediaSource;
|
||||
|
|
@ -765,6 +766,7 @@ public final class DownloadHelper {
|
|||
mediaSourceThread.start();
|
||||
mediaSourceHandler = Util.createHandler(mediaSourceThread.getLooper(), /* callback= */ this);
|
||||
mediaSourceHandler.sendEmptyMessage(MESSAGE_PREPARE_SOURCE);
|
||||
pendingMediaPeriods = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void release() {
|
||||
|
|
@ -791,8 +793,8 @@ public final class DownloadHelper {
|
|||
if (mediaPeriods == null) {
|
||||
mediaSource.maybeThrowSourceInfoRefreshError();
|
||||
} else {
|
||||
for (MediaPeriod mediaPeriod : mediaPeriods) {
|
||||
mediaPeriod.maybeThrowPrepareError();
|
||||
for (int i = 0; i < pendingMediaPeriods.size(); i++) {
|
||||
pendingMediaPeriods.get(i).maybeThrowPrepareError();
|
||||
}
|
||||
}
|
||||
mediaSourceHandler.sendEmptyMessageDelayed(
|
||||
|
|
@ -801,6 +803,12 @@ public final class DownloadHelper {
|
|||
downloadHelper.onMediaPreparationFailed(e);
|
||||
}
|
||||
return true;
|
||||
case MESSAGE_CONTINUE_LOADING:
|
||||
MediaPeriod mediaPeriod = (MediaPeriod) msg.obj;
|
||||
if (pendingMediaPeriods.contains(mediaPeriod)) {
|
||||
mediaPeriod.continueLoading(/* positionUs= */ 0);
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
|
@ -818,14 +826,15 @@ public final class DownloadHelper {
|
|||
this.timeline = timeline;
|
||||
this.manifest = manifest;
|
||||
mediaPeriods = new MediaPeriod[timeline.getPeriodCount()];
|
||||
pendingPreparations = mediaPeriods.length;
|
||||
for (int i = 0; i < mediaPeriods.length; i++) {
|
||||
mediaPeriods[i] =
|
||||
MediaPeriod mediaPeriod =
|
||||
mediaSource.createPeriod(
|
||||
new MediaPeriodId(timeline.getUidOfPeriod(/* periodIndex= */ i)),
|
||||
allocator,
|
||||
/* startPositionUs= */ 0);
|
||||
mediaPeriods[i].prepare(/* callback= */ this, /* positionUs= */ 0);
|
||||
mediaPeriods[i] = mediaPeriod;
|
||||
pendingMediaPeriods.add(mediaPeriod);
|
||||
mediaPeriod.prepare(/* callback= */ this, /* positionUs= */ 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -833,16 +842,18 @@ public final class DownloadHelper {
|
|||
|
||||
@Override
|
||||
public void onPrepared(MediaPeriod mediaPeriod) {
|
||||
pendingPreparations--;
|
||||
if (pendingPreparations == 0) {
|
||||
pendingMediaPeriods.remove(mediaPeriod);
|
||||
if (pendingMediaPeriods.isEmpty()) {
|
||||
mediaSourceHandler.removeMessages(MESSAGE_CHECK_FOR_FAILURE);
|
||||
downloadHelper.onMediaPrepared();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContinueLoadingRequested(MediaPeriod source) {
|
||||
// Ignore.
|
||||
public void onContinueLoadingRequested(MediaPeriod mediaPeriod) {
|
||||
if (pendingMediaPeriods.contains(mediaPeriod)) {
|
||||
mediaSourceHandler.obtainMessage(MESSAGE_CONTINUE_LOADING, mediaPeriod).sendToTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue