Skip to content

Multi period content support for AdsMediaSource #2501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Special case for post-roll ads
  • Loading branch information
kotucz committed Jun 3, 2025
commit eb0467b31e9f25625a57090f7b4fc76caf29887b
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import androidx.annotation.VisibleForTesting;
import androidx.media3.common.AdPlaybackState;
import androidx.media3.common.C;
import androidx.media3.common.Timeline;
import androidx.media3.common.util.Assertions;
import androidx.media3.exoplayer.source.ForwardingTimeline;
Expand Down Expand Up @@ -50,7 +51,7 @@ public MultiPeriodAdTimeline(Timeline contentTimeline, AdPlaybackState adPlaybac
timeline.getPeriod(periodIndex, period);
final long periodDurationUs = period.durationUs;
adPlaybackStates[periodIndex] = forPeriod(adPlaybackState, periodStartOffsetUs,
periodDurationUs);
periodDurationUs, periodIndex == periodCount - 1);
periodStartOffsetUs += periodDurationUs;
}
}
Expand All @@ -74,23 +75,30 @@ public Period getPeriod(int periodIndex, Period period, boolean setIds) {
* @param adPlaybackState original state is immutable always new modified copy is created
* @param periodStartOffsetUs period start time offset from start of timeline (microseconds)
* @param periodDurationUs period duration (microseconds)
* @param isLast true if this is the last period
* @return adPlaybackState modified for period
*/
private AdPlaybackState forPeriod(
AdPlaybackState adPlaybackState,
long periodStartOffsetUs,
long periodDurationUs
) {
long periodDurationUs,
boolean isLast) {
final long periodEndUs = periodStartOffsetUs + periodDurationUs;
for (int adGroupIndex = 0; adGroupIndex < adPlaybackState.adGroupCount; adGroupIndex++) {
final long adGroupTimeUs = adPlaybackState.getAdGroup(adGroupIndex).timeUs;
if (periodEndUs < adGroupTimeUs) {
// this cue point belongs to next periods
adPlaybackState = adPlaybackState.withSkippedAdGroup(adGroupIndex);
if (adGroupTimeUs == C.TIME_END_OF_SOURCE) {
if (!isLast) {
adPlaybackState = adPlaybackState.withSkippedAdGroup(adGroupIndex);
}
} else {
if (periodEndUs < adGroupTimeUs) {
// this cue point belongs to next periods
adPlaybackState = adPlaybackState.withSkippedAdGroup(adGroupIndex);
}
// start time relative to period start
adPlaybackState = adPlaybackState.withAdGroupTimeUs(adGroupIndex,
adGroupTimeUs - periodStartOffsetUs);
}
// start time relative to period start
adPlaybackState = adPlaybackState.withAdGroupTimeUs(adGroupIndex,
adGroupTimeUs - periodStartOffsetUs);
}
return adPlaybackState;
}
Expand Down