Search in sources :

Example 1 with AdMediaInfo

use of com.google.ads.interactivemedia.v3.api.player.AdMediaInfo in project ExoPlayer by google.

the class AdTagLoader method handleAdPrepareError.

private void handleAdPrepareError(int adGroupIndex, int adIndexInAdGroup, Exception exception) {
    if (configuration.debugModeEnabled) {
        Log.d(TAG, "Prepare error for ad " + adIndexInAdGroup + " in group " + adGroupIndex, exception);
    }
    if (adsManager == null) {
        Log.w(TAG, "Ignoring ad prepare error after release");
        return;
    }
    if (imaAdState == IMA_AD_STATE_NONE) {
        // Send IMA a content position at the ad group so that it will try to play it, at which point
        // we can notify that it failed to load.
        fakeContentProgressElapsedRealtimeMs = SystemClock.elapsedRealtime();
        fakeContentProgressOffsetMs = Util.usToMs(adPlaybackState.getAdGroup(adGroupIndex).timeUs);
        if (fakeContentProgressOffsetMs == C.TIME_END_OF_SOURCE) {
            fakeContentProgressOffsetMs = contentDurationMs;
        }
        pendingAdPrepareErrorAdInfo = new AdInfo(adGroupIndex, adIndexInAdGroup);
    } else {
        AdMediaInfo adMediaInfo = checkNotNull(imaAdMediaInfo);
        // We're already playing an ad.
        if (adIndexInAdGroup > playingAdIndexInAdGroup) {
            // which means that the ad after will load (if any).
            for (int i = 0; i < adCallbacks.size(); i++) {
                adCallbacks.get(i).onEnded(adMediaInfo);
            }
        }
        playingAdIndexInAdGroup = adPlaybackState.getAdGroup(adGroupIndex).getFirstAdIndexToPlay();
        for (int i = 0; i < adCallbacks.size(); i++) {
            adCallbacks.get(i).onError(checkNotNull(adMediaInfo));
        }
    }
    adPlaybackState = adPlaybackState.withAdLoadError(adGroupIndex, adIndexInAdGroup);
    updateAdPlaybackState();
}
Also used : AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo)

Example 2 with AdMediaInfo

use of com.google.ads.interactivemedia.v3.api.player.AdMediaInfo in project ExoPlayer by google.

the class AdTagLoader method updateAdProgress.

private void updateAdProgress() {
    VideoProgressUpdate videoProgressUpdate = getAdVideoProgressUpdate();
    if (configuration.debugModeEnabled) {
        Log.d(TAG, "Ad progress: " + ImaUtil.getStringForVideoProgressUpdate(videoProgressUpdate));
    }
    AdMediaInfo adMediaInfo = checkNotNull(imaAdMediaInfo);
    for (int i = 0; i < adCallbacks.size(); i++) {
        adCallbacks.get(i).onAdProgress(adMediaInfo, videoProgressUpdate);
    }
    handler.removeCallbacks(updateAdProgressRunnable);
    handler.postDelayed(updateAdProgressRunnable, AD_PROGRESS_UPDATE_INTERVAL_MS);
}
Also used : AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo) VideoProgressUpdate(com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate)

Example 3 with AdMediaInfo

use of com.google.ads.interactivemedia.v3.api.player.AdMediaInfo in project ExoPlayer by google.

the class AdTagLoader method handlePrepareComplete.

/**
 * Notifies the IMA SDK that the specified ad has been prepared for playback.
 */
public void handlePrepareComplete(int adGroupIndex, int adIndexInAdGroup) {
    AdInfo adInfo = new AdInfo(adGroupIndex, adIndexInAdGroup);
    if (configuration.debugModeEnabled) {
        Log.d(TAG, "Prepared ad " + adInfo);
    }
    @Nullable AdMediaInfo adMediaInfo = adInfoByAdMediaInfo.inverse().get(adInfo);
    if (adMediaInfo != null) {
        for (int i = 0; i < adCallbacks.size(); i++) {
            adCallbacks.get(i).onLoaded(adMediaInfo);
        }
    } else {
        Log.w(TAG, "Unexpected prepared ad " + adInfo);
    }
}
Also used : AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo) Nullable(androidx.annotation.Nullable)

Example 4 with AdMediaInfo

use of com.google.ads.interactivemedia.v3.api.player.AdMediaInfo in project ExoPlayer by google.

the class AdTagLoader method handleTimelineOrPositionChanged.

private void handleTimelineOrPositionChanged() {
    @Nullable Player player = this.player;
    if (adsManager == null || player == null) {
        return;
    }
    if (!playingAd && !player.isPlayingAd()) {
        ensureSentContentCompleteIfAtEndOfStream();
        if (!sentContentComplete && !timeline.isEmpty()) {
            long positionMs = getContentPeriodPositionMs(player, timeline, period);
            timeline.getPeriod(player.getCurrentPeriodIndex(), period);
            int newAdGroupIndex = period.getAdGroupIndexForPositionUs(Util.msToUs(positionMs));
            if (newAdGroupIndex != C.INDEX_UNSET) {
                sentPendingContentPositionMs = false;
                pendingContentPositionMs = positionMs;
            }
        }
    }
    boolean wasPlayingAd = playingAd;
    int oldPlayingAdIndexInAdGroup = playingAdIndexInAdGroup;
    playingAd = player.isPlayingAd();
    playingAdIndexInAdGroup = playingAd ? player.getCurrentAdIndexInAdGroup() : C.INDEX_UNSET;
    boolean adFinished = wasPlayingAd && playingAdIndexInAdGroup != oldPlayingAdIndexInAdGroup;
    if (adFinished) {
        // IMA is waiting for the ad playback to finish so invoke the callback now.
        // Either CONTENT_RESUME_REQUESTED will be passed next, or playAd will be called again.
        @Nullable AdMediaInfo adMediaInfo = imaAdMediaInfo;
        if (adMediaInfo == null) {
            Log.w(TAG, "onEnded without ad media info");
        } else {
            @Nullable AdInfo adInfo = adInfoByAdMediaInfo.get(adMediaInfo);
            if (playingAdIndexInAdGroup == C.INDEX_UNSET || (adInfo != null && adInfo.adIndexInAdGroup < playingAdIndexInAdGroup)) {
                for (int i = 0; i < adCallbacks.size(); i++) {
                    adCallbacks.get(i).onEnded(adMediaInfo);
                }
                if (configuration.debugModeEnabled) {
                    Log.d(TAG, "VideoAdPlayerCallback.onEnded in onTimelineChanged/onPositionDiscontinuity");
                }
            }
        }
    }
    if (!sentContentComplete && !wasPlayingAd && playingAd && imaAdState == IMA_AD_STATE_NONE) {
        AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(player.getCurrentAdGroupIndex());
        if (adGroup.timeUs == C.TIME_END_OF_SOURCE) {
            sendContentComplete();
        } else {
            // IMA hasn't called playAd yet, so fake the content position.
            fakeContentProgressElapsedRealtimeMs = SystemClock.elapsedRealtime();
            fakeContentProgressOffsetMs = Util.usToMs(adGroup.timeUs);
            if (fakeContentProgressOffsetMs == C.TIME_END_OF_SOURCE) {
                fakeContentProgressOffsetMs = contentDurationMs;
            }
        }
    }
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo) Nullable(androidx.annotation.Nullable)

Example 5 with AdMediaInfo

use of com.google.ads.interactivemedia.v3.api.player.AdMediaInfo in project ExoPlayer by google.

the class AdTagLoader method handlePlayerStateChanged.

private void handlePlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
    if (playingAd && imaAdState == IMA_AD_STATE_PLAYING) {
        if (!bufferingAd && playbackState == Player.STATE_BUFFERING) {
            bufferingAd = true;
            AdMediaInfo adMediaInfo = checkNotNull(imaAdMediaInfo);
            for (int i = 0; i < adCallbacks.size(); i++) {
                adCallbacks.get(i).onBuffering(adMediaInfo);
            }
            stopUpdatingAdProgress();
        } else if (bufferingAd && playbackState == Player.STATE_READY) {
            bufferingAd = false;
            updateAdProgress();
        }
    }
    if (imaAdState == IMA_AD_STATE_NONE && playbackState == Player.STATE_BUFFERING && playWhenReady) {
        ensureSentContentCompleteIfAtEndOfStream();
    } else if (imaAdState != IMA_AD_STATE_NONE && playbackState == Player.STATE_ENDED) {
        @Nullable AdMediaInfo adMediaInfo = imaAdMediaInfo;
        if (adMediaInfo == null) {
            Log.w(TAG, "onEnded without ad media info");
        } else {
            for (int i = 0; i < adCallbacks.size(); i++) {
                adCallbacks.get(i).onEnded(adMediaInfo);
            }
        }
        if (configuration.debugModeEnabled) {
            Log.d(TAG, "VideoAdPlayerCallback.onEnded in onPlaybackStateChanged");
        }
    }
}
Also used : AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo)

Aggregations

AdMediaInfo (com.google.ads.interactivemedia.v3.api.player.AdMediaInfo)5 Nullable (androidx.annotation.Nullable)2 VideoAdPlayer (com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer)1 VideoProgressUpdate (com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate)1 Player (com.google.android.exoplayer2.Player)1 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)1