Skip to content

Commit

Permalink
cancel adManagerLoad timer in case of error i.e timeout + prevent pla…
Browse files Browse the repository at this point in the history
…yer to play in case od LOG ERROR and multiple ads in pod (kaltura#206)

* prevent player to play in LOG ERROR and multiple ads in pod

* cancel adManagerLoad timer in case of error i.e timeout

* discard ad break if no ad at all

* print the cancel only if really done

* add removed code by mistake

* refactor call to prepare
preparePlayerOnAdLoadingFinished

* protect ape

* remove unneeded

* use ad position instead of pod index which is the counter of pods in the ad response
  • Loading branch information
giladna authored Apr 9, 2017
1 parent 335dea5 commit df462a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public Player.Settings getSettings() {
public void prepare(@NonNull PKMediaConfig mediaConfig) {

isNewEntry = isNewEntry(mediaConfig);
if (mediaConfig == null) {
log.e("No playable mediaConfig found, mediaConfig = null");
return;
}

this.mediaConfig = mediaConfig;

PKMediaSource source = SourceSelector.selectSource(mediaConfig.getMediaEntry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.ads.interactivemedia.v3.api.Ad;
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
import com.google.ads.interactivemedia.v3.api.AdPodInfo;
import com.google.ads.interactivemedia.v3.api.AdsLoader;
import com.google.ads.interactivemedia.v3.api.AdsManager;
import com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent;
Expand Down Expand Up @@ -147,7 +148,6 @@ protected void onLoad(Player player, Object config, final MessageBus messageBus,
log.e("Error, player instance is null.");
return;
}
this.mediaConfig = mediaConfig;
this.context = context;
this.messageBus = messageBus;
this.messageBus.listen(new PKEvent.Listener() {
Expand Down Expand Up @@ -203,10 +203,7 @@ public void onFinish() {
log.d("adManagerTimer.onFinish, adsManager=" + adsManager);
if (adsManager == null) {
log.d("adsManager is null, will play content");
if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
preparePlayerOnAdLoadingFinished();

messageBus.post(new AdEvent(AdEvent.Type.AD_BREAK_IGNORED));
if (isAdRequested) {
Expand All @@ -215,8 +212,10 @@ public void onFinish() {
}
}
};
adManagerTimer.start();
messageBus.post(new AdEvent(AD_LOAD_TIMEOUT_TIMER_STARTED));
if (adManagerTimer != null) {
adManagerTimer.start();
messageBus.post(new AdEvent(AD_LOAD_TIMEOUT_TIMER_STARTED));
}
}

adDisplayContainer = sdkFactory.createAdDisplayContainer();
Expand Down Expand Up @@ -334,10 +333,7 @@ protected void onDestroy() {
cancelAdDisplayedCheckTimer();
isContentPrepared = false;

if (adManagerTimer != null) {
adManagerTimer.cancel();
adManagerTimer = null;
}
cancelAdManagerTimer();

adTagCuePoints = null;
adPlaybackCancelled = false;
Expand All @@ -356,6 +352,14 @@ protected void onDestroy() {
removeAdProviderListener();
}

private void cancelAdManagerTimer() {
if (adManagerTimer != null) {
log.d("cancelAdManagerTimer");
adManagerTimer.cancel();
adManagerTimer = null;
}
}

////////Ads Plugin

@Override
Expand All @@ -372,8 +376,7 @@ private AdsLoader.AdsLoadedListener getAdsLoadedListener() {
public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {

log.d("AdsManager loaded");

adManagerTimer.cancel();
cancelAdManagerTimer();

// Ads were successfully loaded, so get the AdsManager instance. AdsManager has
// events for ad playback and errors.
Expand Down Expand Up @@ -620,20 +623,20 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {
adsManager.pause();
}


adInfo = createAdInfo(adEvent.getAd());
messageBus.post(new AdEvent.AdStartedEvent(adInfo));

if (!appIsInBackground) {
preparePlayerOnAdLoadingFinished();
}

if (adTagCuePoints == null) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
log.d("AD CUEPOINTS CHANGED TRIGGERED WITH DELAY");
if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}

adTagCuePoints = new AdCuePoints(getAdCuePoints());
messageBus.post(new AdEvent.AdCuePointsUpdateEvent(adTagCuePoints));
}
Expand Down Expand Up @@ -705,13 +708,23 @@ public void run() {
break;
case LOG:
//for this case no AD ERROR is fired need to show view {type=adLoadError, errorCode=1009, errorMessage=The response does not contain any valid ads.}
if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
preparePlayerOnAdLoadingFinished();
if (!isAdDisplayed) {
cancelAdDisplayedCheckTimer();
}
Ad adInfo = adEvent.getAd();
if (adInfo != null) {
//incase one ad in the pod fails to play we want next one to be played
AdPodInfo adPoidInfo = adInfo.getAdPodInfo();
log.d("adPoidInfo.getAdPosition() = " + adPoidInfo.getAdPosition() + " adPoidInfo.getTotalAds() = " + adPoidInfo.getTotalAds());
if (adPoidInfo != null && adPoidInfo.getTotalAds() > 1 && adPoidInfo.getAdPosition() < adPoidInfo.getTotalAds()) {
return;
} else {
adsManager.discardAdBreak();
}
} else {
adsManager.discardAdBreak();
}

log.e("Ad LogError - back to playback");
if (player != null && player.getView() != null) {
Expand Down Expand Up @@ -827,6 +840,7 @@ public void onAdError(AdErrorEvent adErrorEvent) {
isAdDisplayed = false;

cancelAdDisplayedCheckTimer();
cancelAdManagerTimer();
String errorMessage = adErrorEvent.getError().getMessage();
switch (adErrorEvent.getError().getErrorCode()) {
case INTERNAL_ERROR:
Expand Down Expand Up @@ -890,14 +904,18 @@ public void onAdError(AdErrorEvent adErrorEvent) {
messageBus.post(new AdError(AdError.Type.UNKNOWN_ERROR, errorMessage));
}

if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
preparePlayerOnAdLoadingFinished();

if (player != null && player.getView() != null) {
player.getView().showVideoSurface();
player.play();
}
}

private void preparePlayerOnAdLoadingFinished() {
if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
}
}

0 comments on commit df462a3

Please sign in to comment.