From 24125f73492a8ed8ddd8ea09596c0467294646fa Mon Sep 17 00:00:00 2001 From: bachinger Date: Wed, 8 Mar 2023 19:27:39 +0000 Subject: [PATCH] Use the ad group time as content position while playing an ad The value returned by `player.getContentPosition()` is calculated in the timeline based on the position of the first period in the window. In a single period live stream this position is advanced when the live window advances on timeline refresh. This calculation has produced slightly varying values below 1000 us which are likely caused by us/ms truncations for public API values that we use in the IMASSAIMediaSource. However, `AdGroup.timeUs` is the (recorded) content position at the moment when the first ad of the an ad group has been inserted. While playing an ad, we can always use this value instead of `getContentPosition()` to not require recalculation. #minor-release PiperOrigin-RevId: 515093177 --- .../ext/ima/ImaServerSideAdInsertionMediaSource.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaServerSideAdInsertionMediaSource.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaServerSideAdInsertionMediaSource.java index 3f3f7e6747..c1d9051be2 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaServerSideAdInsertionMediaSource.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaServerSideAdInsertionMediaSource.java @@ -1338,16 +1338,18 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou } AdPlaybackState newAdPlaybackState = adPlaybackState; Timeline timeline = player.getCurrentTimeline(); + Timeline.Period currentPeriod = new Timeline.Period(); long positionInWindowUs = - timeline.getPeriod(player.getCurrentPeriodIndex(), new Timeline.Period()) - .positionInWindowUs; - long currentContentPeriodPositionUs = - msToUs(player.getContentPosition()) - positionInWindowUs; + timeline.getPeriod(player.getCurrentPeriodIndex(), currentPeriod).positionInWindowUs; + long contentPositionUs = + player.isPlayingAd() + ? currentPeriod.getAdGroupTimeUs(player.getCurrentAdGroupIndex()) + : msToUs(player.getContentPosition()); Ad ad = event.getAd(); AdPodInfo adPodInfo = ad.getAdPodInfo(); newAdPlaybackState = addLiveAdBreak( - currentContentPeriodPositionUs, + /* currentContentPeriodPositionUs= */ contentPositionUs - positionInWindowUs, /* adDurationUs= */ secToUsRounded(ad.getDuration()), /* adPositionInAdPod= */ adPodInfo.getAdPosition(), /* totalAdDurationUs= */ secToUsRounded(adPodInfo.getMaxDuration()),