Skip to content

Commit d2c4f74

Browse files
rohitjoinsmicrokatz
authored andcommitted
Merge pull request #10618 from vishnuchilakala:fix/do_not_send_content_complete_if_midroll_skipped
PiperOrigin-RevId: 482481703 (cherry picked from commit a413b47)
1 parent b97a959 commit d2c4f74

File tree

1 file changed

+29
-17
lines changed
  • extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima

1 file changed

+29
-17
lines changed

extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/AdTagLoader.java

+29-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.android.exoplayer2.ext.ima.ImaUtil.getImaLooper;
2323
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
2424
import static com.google.android.exoplayer2.util.Assertions.checkState;
25+
import static com.google.android.exoplayer2.util.Util.msToUs;
2526
import static java.lang.Math.max;
2627
import static java.lang.annotation.ElementType.TYPE_USE;
2728

@@ -50,6 +51,7 @@
5051
import com.google.ads.interactivemedia.v3.api.player.AdMediaInfo;
5152
import com.google.ads.interactivemedia.v3.api.player.ContentProgressProvider;
5253
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
54+
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer.VideoAdPlayerCallback;
5355
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
5456
import com.google.android.exoplayer2.C;
5557
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
@@ -355,7 +357,7 @@ public void activate(Player player) {
355357
long contentPositionMs = getContentPeriodPositionMs(player, timeline, period);
356358
int adGroupForPositionIndex =
357359
adPlaybackState.getAdGroupIndexForPositionUs(
358-
Util.msToUs(contentPositionMs), Util.msToUs(contentDurationMs));
360+
msToUs(contentPositionMs), msToUs(contentDurationMs));
359361
if (adGroupForPositionIndex != C.INDEX_UNSET
360362
&& imaAdInfo != null
361363
&& imaAdInfo.adGroupIndex != adGroupForPositionIndex) {
@@ -379,7 +381,7 @@ public void deactivate() {
379381
}
380382
adPlaybackState =
381383
adPlaybackState.withAdResumePositionUs(
382-
playingAd ? Util.msToUs(player.getCurrentPosition()) : 0);
384+
playingAd ? msToUs(player.getCurrentPosition()) : 0);
383385
}
384386
lastVolumePercent = getPlayerVolumePercent();
385387
lastAdProgress = getAdVideoProgressUpdate();
@@ -609,11 +611,10 @@ private AdsRenderingSettings setupAdsRendering(long contentPositionMs, long cont
609611
// Skip ads based on the start position as required.
610612
int adGroupForPositionIndex =
611613
adPlaybackState.getAdGroupIndexForPositionUs(
612-
Util.msToUs(contentPositionMs), Util.msToUs(contentDurationMs));
614+
msToUs(contentPositionMs), msToUs(contentDurationMs));
613615
if (adGroupForPositionIndex != C.INDEX_UNSET) {
614616
boolean playAdWhenStartingPlayback =
615-
adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs
616-
== Util.msToUs(contentPositionMs)
617+
adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs == msToUs(contentPositionMs)
617618
|| configuration.playAdBeforeStartPosition;
618619
if (!playAdWhenStartingPlayback) {
619620
adGroupForPositionIndex++;
@@ -865,7 +866,7 @@ private void handleTimelineOrPositionChanged() {
865866
if (!sentContentComplete && !timeline.isEmpty()) {
866867
long positionMs = getContentPeriodPositionMs(player, timeline, period);
867868
timeline.getPeriod(player.getCurrentPeriodIndex(), period);
868-
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(Util.msToUs(positionMs));
869+
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(msToUs(positionMs));
869870
if (newAdGroupIndex != C.INDEX_UNSET) {
870871
sentPendingContentPositionMs = false;
871872
pendingContentPositionMs = positionMs;
@@ -1157,14 +1158,26 @@ private void handleAdPrepareError(int adGroupIndex, int adIndexInAdGroup, Except
11571158
}
11581159

11591160
private void ensureSentContentCompleteIfAtEndOfStream() {
1160-
if (!sentContentComplete
1161-
&& contentDurationMs != C.TIME_UNSET
1162-
&& pendingContentPositionMs == C.TIME_UNSET
1163-
&& getContentPeriodPositionMs(checkNotNull(player), timeline, period)
1164-
+ THRESHOLD_END_OF_CONTENT_MS
1165-
>= contentDurationMs) {
1166-
sendContentComplete();
1161+
if (sentContentComplete
1162+
|| contentDurationMs == C.TIME_UNSET
1163+
|| pendingContentPositionMs != C.TIME_UNSET) {
1164+
return;
1165+
}
1166+
long contentPeriodPositionMs =
1167+
getContentPeriodPositionMs(checkNotNull(player), timeline, period);
1168+
if (contentPeriodPositionMs + THRESHOLD_END_OF_CONTENT_MS < contentDurationMs) {
1169+
return;
11671170
}
1171+
int pendingAdGroupIndex =
1172+
adPlaybackState.getAdGroupIndexForPositionUs(
1173+
msToUs(contentPeriodPositionMs), msToUs(contentDurationMs));
1174+
if (pendingAdGroupIndex != C.INDEX_UNSET
1175+
&& adPlaybackState.getAdGroup(pendingAdGroupIndex).timeUs != C.TIME_END_OF_SOURCE
1176+
&& adPlaybackState.getAdGroup(pendingAdGroupIndex).shouldPlayAdGroup()) {
1177+
// Pending mid-roll ad that needs to be played before marking the content complete.
1178+
return;
1179+
}
1180+
sendContentComplete();
11681181
}
11691182

11701183
private void sendContentComplete() {
@@ -1233,14 +1246,13 @@ private int getLoadingAdGroupIndex() {
12331246
if (player == null) {
12341247
return C.INDEX_UNSET;
12351248
}
1236-
long playerPositionUs = Util.msToUs(getContentPeriodPositionMs(player, timeline, period));
1249+
long playerPositionUs = msToUs(getContentPeriodPositionMs(player, timeline, period));
12371250
int adGroupIndex =
1238-
adPlaybackState.getAdGroupIndexForPositionUs(
1239-
playerPositionUs, Util.msToUs(contentDurationMs));
1251+
adPlaybackState.getAdGroupIndexForPositionUs(playerPositionUs, msToUs(contentDurationMs));
12401252
if (adGroupIndex == C.INDEX_UNSET) {
12411253
adGroupIndex =
12421254
adPlaybackState.getAdGroupIndexAfterPositionUs(
1243-
playerPositionUs, Util.msToUs(contentDurationMs));
1255+
playerPositionUs, msToUs(contentDurationMs));
12441256
}
12451257
return adGroupIndex;
12461258
}

0 commit comments

Comments
 (0)