Skip to content

Commit

Permalink
Account for missing preroll when converting adPodIndex to adGroupIndex
Browse files Browse the repository at this point in the history
IMA always starts midrolls at index 1. So if there is no preroll ad,
the ad group index in AdPlaybackState is off by 1 all the time, and
may also lead to ArrayIndexOutOfBoundsExceptions when trying to access
the last midroll ad

Issue: #1741
PiperOrigin-RevId: 682324368
  • Loading branch information
tonihei authored and copybara-github committed Oct 4, 2024
1 parent b5680e8 commit 47021c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
* Effect:
* Muxers:
* IMA extension:
* Fix bug where server-side inserted DAI streams without a preroll can
result in an `ArrayIndexOutOfBoundsException` when playing past the last
midroll ([#1741](https://github.com/androidx/media/issues/1741)).
* Session:
* Fix bug that caused custom commands sent from a `MediaBrowser` being
dispatched to the `MediaSessionCompat.Callback` instead of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,7 @@ private static long getAdDuration(double startTimeSeconds, double endTimeSeconds

private static AdPlaybackState setVodAdInPlaceholder(Ad ad, AdPlaybackState adPlaybackState) {
AdPodInfo adPodInfo = ad.getAdPodInfo();
// Handle post rolls that have a podIndex of -1.
int adGroupIndex =
adPodInfo.getPodIndex() == -1 ? adPlaybackState.adGroupCount - 1 : adPodInfo.getPodIndex();
int adGroupIndex = getAdGroupIndexFromAdPodInfo(adPodInfo, adPlaybackState);
AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(adGroupIndex);
int adIndexInAdGroup = adPodInfo.getAdPosition() - 1;
if (adGroup.count < adPodInfo.getTotalAds()) {
Expand All @@ -924,12 +922,27 @@ private static AdPlaybackState setVodAdInPlaceholder(Ad ad, AdPlaybackState adPl

private static AdPlaybackState skipAd(Ad ad, AdPlaybackState adPlaybackState) {
AdPodInfo adPodInfo = ad.getAdPodInfo();
int adGroupIndex = adPodInfo.getPodIndex();
int adGroupIndex = getAdGroupIndexFromAdPodInfo(adPodInfo, adPlaybackState);
// IMA SDK always returns index starting at 1.
int adIndexInAdGroup = adPodInfo.getAdPosition() - 1;
return adPlaybackState.withSkippedAd(adGroupIndex, adIndexInAdGroup);
}

private static int getAdGroupIndexFromAdPodInfo(
AdPodInfo adPodInfo, AdPlaybackState adPlaybackState) {
int adPodIndex = adPodInfo.getPodIndex();
if (adPodIndex == -1) {
// Post-roll
return adPlaybackState.adGroupCount - 1;
}
if (adPlaybackState.getAdGroup(0).timeUs == 0) {
// When a pre-roll exists, the index starts at zero.
return adPodIndex;
}
// Mid-rolls always start at 1.
return adPodIndex - 1;
}

private final class ComponentListener
implements AdEvent.AdEventListener, Player.Listener, AdPlaybackStateUpdater {

Expand Down

0 comments on commit 47021c8

Please sign in to comment.