From 28b8ae70767d0c3ff358d1ef4349c788f10d6e9c Mon Sep 17 00:00:00 2001 From: bachinger Date: Mon, 14 Feb 2022 13:48:56 +0000 Subject: [PATCH] Seek to start position when reusing the last used shared period The last used shared media period is reused after all media periods have been released. In case the sample streams are already filled up, they need to be reset or they download samples from the current position up to the seek position. This causes long buffering states or load stuck exceptions. A seek when reusing the shared period takes care for reseting the period or internally seeks to the current position in the already available samples. #minor-release PiperOrigin-RevId: 428484187 --- .../source/ads/ServerSideAdInsertionMediaSource.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource.java index a0821d571e..e5e29d4c97 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionMediaSource.java @@ -264,10 +264,12 @@ public final class ServerSideAdInsertionMediaSource extends BaseMediaSource public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) { @Nullable SharedMediaPeriod sharedPeriod = null; Pair sharedMediaPeriodKey = new Pair<>(id.windowSequenceNumber, id.periodUid); + boolean reusedSharedPeriod = false; if (lastUsedMediaPeriod != null) { if (lastUsedMediaPeriod.periodUid.equals(id.periodUid)) { sharedPeriod = lastUsedMediaPeriod; mediaPeriods.put(sharedMediaPeriodKey, sharedPeriod); + reusedSharedPeriod = true; } else { lastUsedMediaPeriod.release(mediaSource); } @@ -298,6 +300,9 @@ public final class ServerSideAdInsertionMediaSource extends BaseMediaSource new MediaPeriodImpl( sharedPeriod, id, createEventDispatcher(id), createDrmEventDispatcher(id)); sharedPeriod.add(mediaPeriod); + if (reusedSharedPeriod && sharedPeriod.trackSelections.length > 0) { + mediaPeriod.seekToUs(startPositionUs); + } return mediaPeriod; }