From a43ffa8898df6f09f5c1381df76f9e1617a4a8e6 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 22 Apr 2024 12:06:43 -0700 Subject: [PATCH] Add replaceAdTagParameters() to ImaServerSideAdInsertion.AdsLoader. PiperOrigin-RevId: 627114711 --- RELEASENOTES.md | 3 ++ api.txt | 1 + .../ImaServerSideAdInsertionMediaSource.java | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 8bae8d4804..3ff8429290 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -35,6 +35,9 @@ * Promote API that is required for apps to play [DAI ad streams](https://developers.google.com/ad-manager/dynamic-ad-insertion/full-service) to stable. + * Add `replaceAdTagParameters(Map )` to + `ImaServerSideAdInsertionMediaSource.AdLoader` that allows replacing ad + tag parameters at runtime. * Session: * Hide seekbar in the media notification for live streams by not setting the duration into the platform session metadata diff --git a/api.txt b/api.txt index 66a509138d..ca98427537 100644 --- a/api.txt +++ b/api.txt @@ -1393,6 +1393,7 @@ package androidx.media3.exoplayer.ima { public static final class ImaServerSideAdInsertionMediaSource.AdsLoader { method public androidx.media3.exoplayer.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State release(); + method public void replaceAdTagParameters(java.util.Map); method public void setPlayer(androidx.media3.common.Player); } diff --git a/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java b/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java index 09e1ed6e5d..cefbc70084 100644 --- a/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java +++ b/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java @@ -472,6 +472,36 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou this.player = player; } + /** + * Replaces all the ad tag parameters used for the upcoming ad requests for a live stream. + * + * @see StreamManager#replaceAdTagParameters(Map) + */ + public void replaceAdTagParameters(Map adTagParameters) { + if (player == null) { + return; + } + if (player.getPlaybackState() != Player.STATE_IDLE + && player.getPlaybackState() != Player.STATE_ENDED + && player.getMediaItemCount() > 0) { + int currentPeriodIndex = player.getCurrentPeriodIndex(); + Object adsId = + player + .getCurrentTimeline() + .getPeriod(currentPeriodIndex, new Timeline.Period()) + .getAdsId(); + if (adsId instanceof String) { + MediaSourceResourceHolder mediaSourceResourceHolder = mediaSourceResources.get(adsId); + if (mediaSourceResourceHolder != null + && mediaSourceResourceHolder.imaServerSideAdInsertionMediaSource.streamManager + != null) { + mediaSourceResourceHolder.imaServerSideAdInsertionMediaSource.streamManager + .replaceAdTagParameters(adTagParameters); + } + } + } + } + /** * Puts the focus on the skip button, if a skip button is present and an ad is playing. *