22
22
import static com .google .android .exoplayer2 .ext .ima .ImaUtil .getImaLooper ;
23
23
import static com .google .android .exoplayer2 .util .Assertions .checkNotNull ;
24
24
import static com .google .android .exoplayer2 .util .Assertions .checkState ;
25
+ import static com .google .android .exoplayer2 .util .Util .msToUs ;
25
26
import static java .lang .Math .max ;
26
27
import static java .lang .annotation .ElementType .TYPE_USE ;
27
28
50
51
import com .google .ads .interactivemedia .v3 .api .player .AdMediaInfo ;
51
52
import com .google .ads .interactivemedia .v3 .api .player .ContentProgressProvider ;
52
53
import com .google .ads .interactivemedia .v3 .api .player .VideoAdPlayer ;
54
+ import com .google .ads .interactivemedia .v3 .api .player .VideoAdPlayer .VideoAdPlayerCallback ;
53
55
import com .google .ads .interactivemedia .v3 .api .player .VideoProgressUpdate ;
54
56
import com .google .android .exoplayer2 .C ;
55
57
import com .google .android .exoplayer2 .ExoPlayerLibraryInfo ;
@@ -355,7 +357,7 @@ public void activate(Player player) {
355
357
long contentPositionMs = getContentPeriodPositionMs (player , timeline , period );
356
358
int adGroupForPositionIndex =
357
359
adPlaybackState .getAdGroupIndexForPositionUs (
358
- Util . msToUs (contentPositionMs ), Util . msToUs (contentDurationMs ));
360
+ msToUs (contentPositionMs ), msToUs (contentDurationMs ));
359
361
if (adGroupForPositionIndex != C .INDEX_UNSET
360
362
&& imaAdInfo != null
361
363
&& imaAdInfo .adGroupIndex != adGroupForPositionIndex ) {
@@ -379,7 +381,7 @@ public void deactivate() {
379
381
}
380
382
adPlaybackState =
381
383
adPlaybackState .withAdResumePositionUs (
382
- playingAd ? Util . msToUs (player .getCurrentPosition ()) : 0 );
384
+ playingAd ? msToUs (player .getCurrentPosition ()) : 0 );
383
385
}
384
386
lastVolumePercent = getPlayerVolumePercent ();
385
387
lastAdProgress = getAdVideoProgressUpdate ();
@@ -609,11 +611,10 @@ private AdsRenderingSettings setupAdsRendering(long contentPositionMs, long cont
609
611
// Skip ads based on the start position as required.
610
612
int adGroupForPositionIndex =
611
613
adPlaybackState .getAdGroupIndexForPositionUs (
612
- Util . msToUs (contentPositionMs ), Util . msToUs (contentDurationMs ));
614
+ msToUs (contentPositionMs ), msToUs (contentDurationMs ));
613
615
if (adGroupForPositionIndex != C .INDEX_UNSET ) {
614
616
boolean playAdWhenStartingPlayback =
615
- adPlaybackState .getAdGroup (adGroupForPositionIndex ).timeUs
616
- == Util .msToUs (contentPositionMs )
617
+ adPlaybackState .getAdGroup (adGroupForPositionIndex ).timeUs == msToUs (contentPositionMs )
617
618
|| configuration .playAdBeforeStartPosition ;
618
619
if (!playAdWhenStartingPlayback ) {
619
620
adGroupForPositionIndex ++;
@@ -865,7 +866,7 @@ private void handleTimelineOrPositionChanged() {
865
866
if (!sentContentComplete && !timeline .isEmpty ()) {
866
867
long positionMs = getContentPeriodPositionMs (player , timeline , period );
867
868
timeline .getPeriod (player .getCurrentPeriodIndex (), period );
868
- int newAdGroupIndex = period .getAdGroupIndexForPositionUs (Util . msToUs (positionMs ));
869
+ int newAdGroupIndex = period .getAdGroupIndexForPositionUs (msToUs (positionMs ));
869
870
if (newAdGroupIndex != C .INDEX_UNSET ) {
870
871
sentPendingContentPositionMs = false ;
871
872
pendingContentPositionMs = positionMs ;
@@ -1157,14 +1158,26 @@ private void handleAdPrepareError(int adGroupIndex, int adIndexInAdGroup, Except
1157
1158
}
1158
1159
1159
1160
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 ;
1167
1170
}
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 ();
1168
1181
}
1169
1182
1170
1183
private void sendContentComplete () {
@@ -1233,14 +1246,13 @@ private int getLoadingAdGroupIndex() {
1233
1246
if (player == null ) {
1234
1247
return C .INDEX_UNSET ;
1235
1248
}
1236
- long playerPositionUs = Util . msToUs (getContentPeriodPositionMs (player , timeline , period ));
1249
+ long playerPositionUs = msToUs (getContentPeriodPositionMs (player , timeline , period ));
1237
1250
int adGroupIndex =
1238
- adPlaybackState .getAdGroupIndexForPositionUs (
1239
- playerPositionUs , Util .msToUs (contentDurationMs ));
1251
+ adPlaybackState .getAdGroupIndexForPositionUs (playerPositionUs , msToUs (contentDurationMs ));
1240
1252
if (adGroupIndex == C .INDEX_UNSET ) {
1241
1253
adGroupIndex =
1242
1254
adPlaybackState .getAdGroupIndexAfterPositionUs (
1243
- playerPositionUs , Util . msToUs (contentDurationMs ));
1255
+ playerPositionUs , msToUs (contentDurationMs ));
1244
1256
}
1245
1257
return adGroupIndex ;
1246
1258
}
0 commit comments