Skip to content

Commit

Permalink
Merge pull request #10618 from vishnuchilakala:fix/do_not_send_conten…
Browse files Browse the repository at this point in the history
…t_complete_if_midroll_skipped

PiperOrigin-RevId: 482481703
(cherry picked from commit faa4302)
  • Loading branch information
rohitjoins authored and microkatz committed Oct 24, 2022
1 parent f56193b commit 3cb1d60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Expand Up @@ -91,6 +91,8 @@ Release notes
* Add timeout for loading ad information to handle cases where the IMA SDK
gets stuck loading an ad
([#10510](https://github.com/google/ExoPlayer/issues/10510)).
* Prevent skipping mid-roll ads when seeking to the end of the content
([#10685](https://github.com/google/ExoPlayer/issues/10685)).
* FFmpeg extension:
* Add newly required flags to link FFmpeg libraries with NDK 23.1.7779620
and above ([#9933](https://github.com/google/ExoPlayer/issues/9933)).
Expand Down
Expand Up @@ -18,6 +18,7 @@
import static androidx.media3.common.Player.COMMAND_GET_VOLUME;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.common.util.Util.msToUs;
import static androidx.media3.exoplayer.ima.ImaUtil.BITRATE_UNSET;
import static androidx.media3.exoplayer.ima.ImaUtil.TIMEOUT_UNSET;
import static androidx.media3.exoplayer.ima.ImaUtil.getAdGroupTimesUsForCuePoints;
Expand Down Expand Up @@ -63,6 +64,7 @@
import com.google.ads.interactivemedia.v3.api.player.AdMediaInfo;
import com.google.ads.interactivemedia.v3.api.player.ContentProgressProvider;
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer.VideoAdPlayerCallback;
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
Expand Down Expand Up @@ -355,7 +357,7 @@ public void activate(Player player) {
long contentPositionMs = getContentPeriodPositionMs(player, timeline, period);
int adGroupForPositionIndex =
adPlaybackState.getAdGroupIndexForPositionUs(
Util.msToUs(contentPositionMs), Util.msToUs(contentDurationMs));
msToUs(contentPositionMs), msToUs(contentDurationMs));
if (adGroupForPositionIndex != C.INDEX_UNSET
&& imaAdInfo != null
&& imaAdInfo.adGroupIndex != adGroupForPositionIndex) {
Expand All @@ -379,7 +381,7 @@ public void deactivate() {
}
adPlaybackState =
adPlaybackState.withAdResumePositionUs(
playingAd ? Util.msToUs(player.getCurrentPosition()) : 0);
playingAd ? msToUs(player.getCurrentPosition()) : 0);
}
lastVolumePercent = getPlayerVolumePercent();
lastAdProgress = getAdVideoProgressUpdate();
Expand Down Expand Up @@ -609,11 +611,10 @@ private AdsRenderingSettings setupAdsRendering(long contentPositionMs, long cont
// Skip ads based on the start position as required.
int adGroupForPositionIndex =
adPlaybackState.getAdGroupIndexForPositionUs(
Util.msToUs(contentPositionMs), Util.msToUs(contentDurationMs));
msToUs(contentPositionMs), msToUs(contentDurationMs));
if (adGroupForPositionIndex != C.INDEX_UNSET) {
boolean playAdWhenStartingPlayback =
adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs
== Util.msToUs(contentPositionMs)
adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs == msToUs(contentPositionMs)
|| configuration.playAdBeforeStartPosition;
if (!playAdWhenStartingPlayback) {
adGroupForPositionIndex++;
Expand Down Expand Up @@ -865,7 +866,7 @@ private void handleTimelineOrPositionChanged() {
if (!sentContentComplete && !timeline.isEmpty()) {
long positionMs = getContentPeriodPositionMs(player, timeline, period);
timeline.getPeriod(player.getCurrentPeriodIndex(), period);
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(Util.msToUs(positionMs));
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(msToUs(positionMs));
if (newAdGroupIndex != C.INDEX_UNSET) {
sentPendingContentPositionMs = false;
pendingContentPositionMs = positionMs;
Expand Down Expand Up @@ -1157,14 +1158,26 @@ private void handleAdPrepareError(int adGroupIndex, int adIndexInAdGroup, Except
}

private void ensureSentContentCompleteIfAtEndOfStream() {
if (!sentContentComplete
&& contentDurationMs != C.TIME_UNSET
&& pendingContentPositionMs == C.TIME_UNSET
&& getContentPeriodPositionMs(checkNotNull(player), timeline, period)
+ THRESHOLD_END_OF_CONTENT_MS
>= contentDurationMs) {
sendContentComplete();
if (sentContentComplete
|| contentDurationMs == C.TIME_UNSET
|| pendingContentPositionMs != C.TIME_UNSET) {
return;
}
long contentPeriodPositionMs =
getContentPeriodPositionMs(checkNotNull(player), timeline, period);
if (contentPeriodPositionMs + THRESHOLD_END_OF_CONTENT_MS < contentDurationMs) {
return;
}
int pendingAdGroupIndex =
adPlaybackState.getAdGroupIndexForPositionUs(
msToUs(contentPeriodPositionMs), msToUs(contentDurationMs));
if (pendingAdGroupIndex != C.INDEX_UNSET
&& adPlaybackState.getAdGroup(pendingAdGroupIndex).timeUs != C.TIME_END_OF_SOURCE
&& adPlaybackState.getAdGroup(pendingAdGroupIndex).shouldPlayAdGroup()) {
// Pending mid-roll ad that needs to be played before marking the content complete.
return;
}
sendContentComplete();
}

private void sendContentComplete() {
Expand Down Expand Up @@ -1233,14 +1246,13 @@ private int getLoadingAdGroupIndex() {
if (player == null) {
return C.INDEX_UNSET;
}
long playerPositionUs = Util.msToUs(getContentPeriodPositionMs(player, timeline, period));
long playerPositionUs = msToUs(getContentPeriodPositionMs(player, timeline, period));
int adGroupIndex =
adPlaybackState.getAdGroupIndexForPositionUs(
playerPositionUs, Util.msToUs(contentDurationMs));
adPlaybackState.getAdGroupIndexForPositionUs(playerPositionUs, msToUs(contentDurationMs));
if (adGroupIndex == C.INDEX_UNSET) {
adGroupIndex =
adPlaybackState.getAdGroupIndexAfterPositionUs(
playerPositionUs, Util.msToUs(contentDurationMs));
playerPositionUs, msToUs(contentDurationMs));
}
return adGroupIndex;
}
Expand Down

0 comments on commit 3cb1d60

Please sign in to comment.