use of androidx.media3.exoplayer.source.MediaPeriod in project media by androidx.
the class ServerSideAdInsertionMediaSource method createPeriod.
@Override
public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
@Nullable SharedMediaPeriod sharedPeriod = null;
Pair<Long, Object> 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);
}
lastUsedMediaPeriod = null;
}
if (sharedPeriod == null) {
@Nullable SharedMediaPeriod lastExistingPeriod = Iterables.getLast(mediaPeriods.get(sharedMediaPeriodKey), /* defaultValue= */
null);
if (lastExistingPeriod != null && lastExistingPeriod.canReuseMediaPeriod(id, startPositionUs)) {
sharedPeriod = lastExistingPeriod;
} else {
AdPlaybackState adPlaybackState = checkNotNull(adPlaybackStates.get(id.periodUid));
long streamPositionUs = getStreamPositionUs(startPositionUs, id, adPlaybackState);
sharedPeriod = new SharedMediaPeriod(mediaSource.createPeriod(new MediaPeriodId(id.periodUid, id.windowSequenceNumber), allocator, streamPositionUs), id.periodUid, adPlaybackState);
mediaPeriods.put(sharedMediaPeriodKey, sharedPeriod);
}
}
MediaPeriodImpl mediaPeriod = new MediaPeriodImpl(sharedPeriod, id, createEventDispatcher(id), createDrmEventDispatcher(id));
sharedPeriod.add(mediaPeriod);
if (reusedSharedPeriod && sharedPeriod.trackSelections.length > 0) {
mediaPeriod.seekToUs(startPositionUs);
}
return mediaPeriod;
}
Aggregations