videojs-ima
Version:
[](https://travis-ci.org/googleads/videojs-ima)
1,586 lines (1,385 loc) • 83 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('video.js')) :
typeof define === 'function' && define.amd ? define(['video.js'], factory) :
(global.videojsIma = factory(global.videojs));
}(this, (function (videojs) { 'use strict';
videojs = videojs && videojs.hasOwnProperty('default') ? videojs['default'] : videojs;
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* IMA SDK integration plugin for Video.js. For more information see
* https://www.github.com/googleads/videojs-ima
*/
/**
* Wraps the video.js player for the plugin.
*
* @param {Object} player Video.js player instance.
* @param {Object} adsPluginSettings Settings for the contrib-ads plugin.
* @param {Controller} controller Reference to the parent controller.
*/
var PlayerWrapper = function PlayerWrapper(player, adsPluginSettings, controller) {
/**
* Instance of the video.js player.
*/
this.vjsPlayer = player;
/**
* Plugin controller.
*/
this.controller = controller;
/**
* Timer used to track content progress.
*/
this.contentTrackingTimer = null;
/**
* True if our content video has completed, false otherwise.
*/
this.contentComplete = false;
/**
* Handle to interval that repeatedly updates current time.
*/
this.updateTimeIntervalHandle = null;
/**
* Interval (ms) to check for player resize for fluid support.
*/
this.updateTimeInterval = 1000;
/**
* Handle to interval that repeatedly checks for seeking.
*/
this.seekCheckIntervalHandle = null;
/**
* Interval (ms) on which to check if the user is seeking through the
* content.
*/
this.seekCheckInterval = 1000;
/**
* Handle to interval that repeatedly checks for player resize.
*/
this.resizeCheckIntervalHandle = null;
/**
* Interval (ms) to check for player resize for fluid support.
*/
this.resizeCheckInterval = 250;
/**
* Threshold by which to judge user seeking. We check every 1000 ms to see
* if the user is seeking. In order for us to decide that they are *not*
* seeking, the content video playhead must only change by 900-1100 ms
* between checks. Any greater change and we assume the user is seeking
* through the video.
*/
this.seekThreshold = 100;
/**
* Content ended listeners passed by the publisher to the plugin. Publishers
* should allow the plugin to handle content ended to ensure proper support
* of custom ad playback.
*/
this.contentEndedListeners = [];
/**
* Stores the content source so we can re-populate it manually after a
* post-roll on iOS.
*/
this.contentSource = '';
/**
* Stores the content source type so we can re-populate it manually after a
* post-roll.
*/
this.contentSourceType = '';
/**
* Stores data for the content playhead tracker.
*/
this.contentPlayheadTracker = {
currentTime: 0,
previousTime: 0,
seeking: false,
duration: 0
};
/**
* Player dimensions. Used in our resize check.
*/
this.vjsPlayerDimensions = {
width: this.getPlayerWidth(),
height: this.getPlayerHeight()
};
/**
* Video.js control bar.
*/
this.vjsControls = this.vjsPlayer.getChild('controlBar');
/**
* Vanilla HTML5 video player underneath the video.js player.
*/
this.h5Player = null;
this.vjsPlayer.one('play', this.setUpPlayerIntervals.bind(this));
this.boundContentEndedListener = this.localContentEndedListener.bind(this);
this.vjsPlayer.on('contentended', this.boundContentEndedListener);
this.vjsPlayer.on('dispose', this.playerDisposedListener.bind(this));
this.vjsPlayer.on('readyforpreroll', this.onReadyForPreroll.bind(this));
this.vjsPlayer.ready(this.onPlayerReady.bind(this));
if (this.controller.getSettings().requestMode === 'onPlay') {
this.vjsPlayer.one('play', this.controller.requestAds.bind(this.controller));
}
this.vjsPlayer.ads(adsPluginSettings);
};
/**
* Set up the intervals we use on the player.
*/
PlayerWrapper.prototype.setUpPlayerIntervals = function () {
this.updateTimeIntervalHandle = setInterval(this.updateCurrentTime.bind(this), this.updateTimeInterval);
this.seekCheckIntervalHandle = setInterval(this.checkForSeeking.bind(this), this.seekCheckInterval);
this.resizeCheckIntervalHandle = setInterval(this.checkForResize.bind(this), this.resizeCheckInterval);
};
/**
* Updates the current time of the video
*/
PlayerWrapper.prototype.updateCurrentTime = function () {
if (!this.contentPlayheadTracker.seeking) {
this.contentPlayheadTracker.currentTime = this.vjsPlayer.currentTime();
}
};
/**
* Detects when the user is seeking through a video.
* This is used to prevent mid-rolls from playing while a user is seeking.
*
* There *is* a seeking property of the HTML5 video element, but it's not
* properly implemented on all platforms (e.g. mobile safari), so we have to
* check ourselves to be sure.
*/
PlayerWrapper.prototype.checkForSeeking = function () {
var tempCurrentTime = this.vjsPlayer.currentTime();
var diff = (tempCurrentTime - this.contentPlayheadTracker.previousTime) * 1000;
if (Math.abs(diff) > this.seekCheckInterval + this.seekThreshold) {
this.contentPlayheadTracker.seeking = true;
} else {
this.contentPlayheadTracker.seeking = false;
}
this.contentPlayheadTracker.previousTime = this.vjsPlayer.currentTime();
};
/**
* Detects when the player is resized (for fluid support) and resizes the
* ads manager to match.
*/
PlayerWrapper.prototype.checkForResize = function () {
var currentWidth = this.getPlayerWidth();
var currentHeight = this.getPlayerHeight();
if (currentWidth != this.vjsPlayerDimensions.width || currentHeight != this.vjsPlayerDimensions.height) {
this.vjsPlayerDimensions.width = currentWidth;
this.vjsPlayerDimensions.height = currentHeight;
this.controller.onPlayerResize(currentWidth, currentHeight);
}
};
/**
* Local content ended listener for contentComplete.
*/
PlayerWrapper.prototype.localContentEndedListener = function () {
if (!this.contentComplete) {
this.contentComplete = true;
this.controller.onContentComplete();
}
for (var index in this.contentEndedListeners) {
if (typeof this.contentEndedListeners[index] === 'function') {
this.contentEndedListeners[index]();
}
}
clearInterval(this.updateTimeIntervalHandle);
clearInterval(this.seekCheckIntervalHandle);
clearInterval(this.resizeCheckIntervalHandle);
if (this.vjsPlayer.el()) {
this.vjsPlayer.one('play', this.setUpPlayerIntervals.bind(this));
}
};
/**
* Called when it's time to play a post-roll but we don't have one to play.
*/
PlayerWrapper.prototype.onNoPostroll = function () {
this.vjsPlayer.trigger('nopostroll');
};
/**
* Detects when the video.js player has been disposed.
*/
PlayerWrapper.prototype.playerDisposedListener = function () {
this.contentEndedListeners = [];
this.controller.onPlayerDisposed();
this.contentComplete = true;
this.vjsPlayer.off('contentended', this.boundContentEndedListener);
// Bug fix: https://github.com/googleads/videojs-ima/issues/306
if (this.vjsPlayer.ads.adTimeoutTimeout) {
clearTimeout(this.vjsPlayer.ads.adTimeoutTimeout);
}
var intervalsToClear = [this.updateTimeIntervalHandle, this.seekCheckIntervalHandle, this.resizeCheckIntervalHandle];
for (var index in intervalsToClear) {
if (intervalsToClear[index]) {
clearInterval(intervalsToClear[index]);
}
}
};
/**
* Start ad playback, or content video playback in the absence of a
* pre-roll.
*/
PlayerWrapper.prototype.onReadyForPreroll = function () {
this.controller.onPlayerReadyForPreroll();
};
/**
* Called when the player fires its 'ready' event.
*/
PlayerWrapper.prototype.onPlayerReady = function () {
this.h5Player = document.getElementById(this.getPlayerId()).getElementsByClassName('vjs-tech')[0];
// Detect inline options
if (this.h5Player.hasAttribute('autoplay')) {
this.controller.setSetting('adWillAutoPlay', true);
}
// Sync ad volume with player volume.
this.onVolumeChange();
this.vjsPlayer.on('fullscreenchange', this.onFullscreenChange.bind(this));
this.vjsPlayer.on('volumechange', this.onVolumeChange.bind(this));
this.controller.onPlayerReady();
};
/**
* Listens for the video.js player to change its fullscreen status. This
* keeps the fullscreen-ness of the AdContainer in sync with the player.
*/
PlayerWrapper.prototype.onFullscreenChange = function () {
if (this.vjsPlayer.isFullscreen()) {
this.controller.onPlayerEnterFullscreen();
} else {
this.controller.onPlayerExitFullscreen();
}
};
/**
* Listens for the video.js player to change its volume. This keeps the ad
* volume in sync with the content volume if the volume of the player is
* changed while content is playing.
*/
PlayerWrapper.prototype.onVolumeChange = function () {
var newVolume = this.vjsPlayer.muted() ? 0 : this.vjsPlayer.volume();
this.controller.onPlayerVolumeChanged(newVolume);
};
/**
* Inject the ad container div into the DOM.
*
* @param{HTMLElement} adContainerDiv The ad container div.
*/
PlayerWrapper.prototype.injectAdContainerDiv = function (adContainerDiv) {
this.vjsControls.el().parentNode.appendChild(adContainerDiv);
};
/**
* @return {Object} The content player.
*/
PlayerWrapper.prototype.getContentPlayer = function () {
return this.h5Player;
};
/**
* @return {number} The volume, 0-1.
*/
PlayerWrapper.prototype.getVolume = function () {
return this.vjsPlayer.muted() ? 0 : this.vjsPlayer.volume();
};
/**
* Set the volume of the player. 0-1.
*
* @param {number} volume The new volume.
*/
PlayerWrapper.prototype.setVolume = function (volume) {
this.vjsPlayer.volume(volume);
if (volume == 0) {
this.vjsPlayer.muted(true);
} else {
this.vjsPlayer.muted(false);
}
};
/**
* Ummute the player.
*/
PlayerWrapper.prototype.unmute = function () {
this.vjsPlayer.muted(false);
};
/**
* Mute the player.
*/
PlayerWrapper.prototype.mute = function () {
this.vjsPlayer.muted(true);
};
/**
* Play the video.
*/
PlayerWrapper.prototype.play = function () {
this.vjsPlayer.play();
};
/**
* Get the player width.
*
* @return {number} The player's width.
*/
PlayerWrapper.prototype.getPlayerWidth = function () {
var width = (getComputedStyle(this.vjsPlayer.el()) || {}).width;
if (!width || parseFloat(width) === 0) {
width = (this.vjsPlayer.el().getBoundingClientRect() || {}).width;
}
return parseFloat(width) || this.vjsPlayer.width();
};
/**
* Get the player height.
*
* @return {number} The player's height.
*/
PlayerWrapper.prototype.getPlayerHeight = function () {
var height = (getComputedStyle(this.vjsPlayer.el()) || {}).height;
if (!height || parseFloat(height) === 0) {
height = (this.vjsPlayer.el().getBoundingClientRect() || {}).height;
}
return parseFloat(height) || this.vjsPlayer.height();
};
/**
* @return {Object} The vjs player's options object.
*/
PlayerWrapper.prototype.getPlayerOptions = function () {
return this.vjsPlayer.options_;
};
/**
* Returns the instance of the player id.
* @return {string} The player id.
*/
PlayerWrapper.prototype.getPlayerId = function () {
return this.vjsPlayer.id();
};
/**
* Toggle fullscreen state.
*/
PlayerWrapper.prototype.toggleFullscreen = function () {
if (this.vjsPlayer.isFullscreen()) {
this.vjsPlayer.exitFullscreen();
} else {
this.vjsPlayer.requestFullscreen();
}
};
/**
* Returns the content playhead tracker.
*
* @return {Object} The content playhead tracker.
*/
PlayerWrapper.prototype.getContentPlayheadTracker = function () {
return this.contentPlayheadTracker;
};
/**
* Handles ad errors.
*
* @param {Object} adErrorEvent The ad error event thrown by the IMA SDK.
*/
PlayerWrapper.prototype.onAdError = function (adErrorEvent) {
this.vjsControls.show();
var errorMessage = adErrorEvent.getError !== undefined ? adErrorEvent.getError() : adErrorEvent.stack;
this.vjsPlayer.trigger({ type: 'adserror', data: {
AdError: errorMessage,
AdErrorEvent: adErrorEvent
} });
};
/**
* Handles ad log messages.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the IMA SDK.
*/
PlayerWrapper.prototype.onAdLog = function (adEvent) {
var adData = adEvent.getAdData();
var errorMessage = adData['adError'] !== undefined ? adData['adError'].getMessage() : undefined;
this.vjsPlayer.trigger({ type: 'adslog', data: {
AdError: errorMessage,
AdEvent: adEvent
} });
};
/**
* Handles ad break starting.
*/
PlayerWrapper.prototype.onAdBreakStart = function () {
this.contentSource = this.vjsPlayer.currentSrc();
this.contentSourceType = this.vjsPlayer.currentType();
this.vjsPlayer.off('contentended', this.boundContentEndedListener);
this.vjsPlayer.ads.startLinearAdMode();
this.vjsControls.hide();
this.vjsPlayer.pause();
};
/**
* Handles ad break ending.
*/
PlayerWrapper.prototype.onAdBreakEnd = function () {
this.vjsPlayer.on('contentended', this.boundContentEndedListener);
if (this.vjsPlayer.ads.inAdBreak()) {
this.vjsPlayer.ads.endLinearAdMode();
}
this.vjsControls.show();
};
/**
* Handles an individual ad start.
*/
PlayerWrapper.prototype.onAdStart = function () {
this.vjsPlayer.trigger('ads-ad-started');
};
/**
* Handles when all ads have finished playing.
*/
PlayerWrapper.prototype.onAllAdsCompleted = function () {
if (this.contentComplete == true) {
// The null check on this.contentSource was added to fix
// an error when the post-roll was an empty VAST tag.
if (this.contentSource && this.vjsPlayer.currentSrc() != this.contentSource) {
this.vjsPlayer.src({
src: this.contentSource,
type: this.contentSourceType
});
}
this.controller.onContentAndAdsCompleted();
}
};
/**
* Triggers adsready for contrib-ads.
*/
PlayerWrapper.prototype.onAdsReady = function () {
this.vjsPlayer.trigger('adsready');
};
/**
* Changes the player source.
* @param {?string} contentSrc The URI for the content to be played. Leave
* blank to use the existing content.
*/
PlayerWrapper.prototype.changeSource = function (contentSrc) {
// Only try to pause the player when initialised with a source already
if (this.vjsPlayer.currentSrc()) {
this.vjsPlayer.currentTime(0);
this.vjsPlayer.pause();
}
if (contentSrc) {
this.vjsPlayer.src(contentSrc);
}
this.vjsPlayer.one('loadedmetadata', this.seekContentToZero.bind(this));
};
/**
* Seeks content to 00:00:00. This is used as an event handler for the
* loadedmetadata event, since seeking is not possible until that event has
* fired.
*/
PlayerWrapper.prototype.seekContentToZero = function () {
this.vjsPlayer.currentTime(0);
};
/**
* Triggers an event on the VJS player
* @param {string} name The event name.
* @param {Object} data The event data.
*/
PlayerWrapper.prototype.triggerPlayerEvent = function (name, data) {
this.vjsPlayer.trigger(name, data);
};
/**
* Listener JSDoc for ESLint. This listener can be passed to
* addContentEndedListener.
* @callback listener
*/
/**
* Adds a listener for the 'contentended' event of the video player. This should
* be used instead of setting an 'contentended' listener directly to ensure that
* the ima can do proper cleanup of the SDK before other event listeners are
* called.
* @param {listener} listener The listener to be called when content
* completes.
*/
PlayerWrapper.prototype.addContentEndedListener = function (listener) {
this.contentEndedListeners.push(listener);
};
/**
* Reset the player.
*/
PlayerWrapper.prototype.reset = function () {
// Attempts to remove the contentEndedListener before adding it.
// This is to prevent an error where an erroring video caused multiple
// contentEndedListeners to be added.
this.vjsPlayer.off('contentended', this.boundContentEndedListener);
this.vjsPlayer.on('contentended', this.boundContentEndedListener);
this.vjsControls.show();
if (this.vjsPlayer.ads.inAdBreak()) {
this.vjsPlayer.ads.endLinearAdMode();
}
// Reset the content time we give the SDK. Fixes an issue where requesting
// VMAP followed by VMAP would play the second mid-rolls as pre-rolls if
// the first playthrough of the video passed the second response's
// mid-roll time.
this.contentPlayheadTracker.currentTime = 0;
this.contentComplete = false;
};
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* IMA SDK integration plugin for Video.js. For more information see
* https://www.github.com/googleads/videojs-ima
*/
/**
* Ad UI implementation.
*
* @param {Controller} controller Plugin controller.
* @constructor
* @struct
* @final
*/
var AdUi = function AdUi(controller) {
/**
* Plugin controller.
*/
this.controller = controller;
/**
* Div used as an ad container.
*/
this.adContainerDiv = document.createElement('div');
/**
* Div used to display ad controls.
*/
this.controlsDiv = document.createElement('div');
/**
* Div used to display ad countdown timer.
*/
this.countdownDiv = document.createElement('div');
/**
* Div used to display add seek bar.
*/
this.seekBarDiv = document.createElement('div');
/**
* Div used to display ad progress (in seek bar).
*/
this.progressDiv = document.createElement('div');
/**
* Div used to display ad play/pause button.
*/
this.playPauseDiv = document.createElement('div');
/**
* Div used to display ad mute button.
*/
this.muteDiv = document.createElement('div');
/**
* Div used by the volume slider.
*/
this.sliderDiv = document.createElement('div');
/**
* Volume slider level visuals
*/
this.sliderLevelDiv = document.createElement('div');
/**
* Div used to display ad fullscreen button.
*/
this.fullscreenDiv = document.createElement('div');
/**
* Bound event handler for onMouseUp.
*/
this.boundOnMouseUp = this.onMouseUp.bind(this);
/**
* Bound event handler for onMouseMove.
*/
this.boundOnMouseMove = this.onMouseMove.bind(this);
/**
* Stores data for the ad playhead tracker.
*/
this.adPlayheadTracker = {
'currentTime': 0,
'duration': 0,
'isPod': false,
'adPosition': 0,
'totalAds': 0
};
/**
* Used to prefix videojs ima controls.
*/
this.controlPrefix = this.controller.getPlayerId() + '_';
/**
* Boolean flag to show or hide the ad countdown timer.
*/
this.showCountdown = true;
if (this.controller.getSettings().showCountdown === false) {
this.showCountdown = false;
}
this.createAdContainer();
};
/**
* Creates the ad container.
*/
AdUi.prototype.createAdContainer = function () {
this.assignControlAttributes(this.adContainerDiv, 'ima-ad-container');
this.adContainerDiv.style.position = 'absolute';
this.adContainerDiv.style.zIndex = 1111;
this.adContainerDiv.addEventListener('mouseenter', this.showAdControls.bind(this), false);
this.adContainerDiv.addEventListener('mouseleave', this.hideAdControls.bind(this), false);
this.createControls();
this.controller.injectAdContainerDiv(this.adContainerDiv);
};
/**
* Create the controls.
*/
AdUi.prototype.createControls = function () {
this.assignControlAttributes(this.controlsDiv, 'ima-controls-div');
this.controlsDiv.style.width = '100%';
if (!this.controller.getIsMobile) {
this.assignControlAttributes(this.countdownDiv, 'ima-countdown-div');
this.countdownDiv.innerHTML = this.controller.getSettings().adLabel;
this.countdownDiv.style.display = this.showCountdown ? 'block' : 'none';
} else {
this.countdownDiv.style.display = 'none';
}
this.assignControlAttributes(this.seekBarDiv, 'ima-seek-bar-div');
this.seekBarDiv.style.width = '100%';
this.assignControlAttributes(this.progressDiv, 'ima-progress-div');
this.assignControlAttributes(this.playPauseDiv, 'ima-play-pause-div');
this.addClass(this.playPauseDiv, 'ima-playing');
this.playPauseDiv.addEventListener('click', this.onAdPlayPauseClick.bind(this), false);
this.assignControlAttributes(this.muteDiv, 'ima-mute-div');
this.addClass(this.muteDiv, 'ima-non-muted');
this.muteDiv.addEventListener('click', this.onAdMuteClick.bind(this), false);
this.assignControlAttributes(this.sliderDiv, 'ima-slider-div');
this.sliderDiv.addEventListener('mousedown', this.onAdVolumeSliderMouseDown.bind(this), false);
// Hide volume slider controls on iOS as they aren't supported.
if (this.controller.getIsIos()) {
this.sliderDiv.style.display = 'none';
}
this.assignControlAttributes(this.sliderLevelDiv, 'ima-slider-level-div');
this.assignControlAttributes(this.fullscreenDiv, 'ima-fullscreen-div');
this.addClass(this.fullscreenDiv, 'ima-non-fullscreen');
this.fullscreenDiv.addEventListener('click', this.onAdFullscreenClick.bind(this), false);
this.adContainerDiv.appendChild(this.controlsDiv);
this.controlsDiv.appendChild(this.countdownDiv);
this.controlsDiv.appendChild(this.seekBarDiv);
this.controlsDiv.appendChild(this.playPauseDiv);
this.controlsDiv.appendChild(this.muteDiv);
this.controlsDiv.appendChild(this.sliderDiv);
this.controlsDiv.appendChild(this.fullscreenDiv);
this.seekBarDiv.appendChild(this.progressDiv);
this.sliderDiv.appendChild(this.sliderLevelDiv);
};
/**
* Listener for clicks on the play/pause button during ad playback.
*/
AdUi.prototype.onAdPlayPauseClick = function () {
this.controller.onAdPlayPauseClick();
};
/**
* Listener for clicks on the play/pause button during ad playback.
*/
AdUi.prototype.onAdMuteClick = function () {
this.controller.onAdMuteClick();
};
/**
* Listener for clicks on the fullscreen button during ad playback.
*/
AdUi.prototype.onAdFullscreenClick = function () {
this.controller.toggleFullscreen();
};
/**
* Show pause and hide play button
*/
AdUi.prototype.onAdsPaused = function () {
this.addClass(this.playPauseDiv, 'ima-paused');
this.removeClass(this.playPauseDiv, 'ima-playing');
this.showAdControls();
};
/**
* Show pause and hide play button
*/
AdUi.prototype.onAdsResumed = function () {
this.onAdsPlaying();
this.showAdControls();
};
/**
* Show play and hide pause button
*/
AdUi.prototype.onAdsPlaying = function () {
this.addClass(this.playPauseDiv, 'ima-playing');
this.removeClass(this.playPauseDiv, 'ima-paused');
};
/**
* Takes data from the controller to update the UI.
*
* @param {number} currentTime Current time of the ad.
* @param {number} remainingTime Remaining time of the ad.
* @param {number} duration Duration of the ad.
* @param {number} adPosition Index of the ad in the pod.
* @param {number} totalAds Total number of ads in the pod.
*/
AdUi.prototype.updateAdUi = function (currentTime, remainingTime, duration, adPosition, totalAds) {
// Update countdown timer data
var remainingMinutes = Math.floor(remainingTime / 60);
var remainingSeconds = Math.floor(remainingTime % 60);
if (remainingSeconds.toString().length < 2) {
remainingSeconds = '0' + remainingSeconds;
}
var podCount = ': ';
if (totalAds > 1) {
podCount = ' (' + adPosition + ' ' + this.controller.getSettings().adLabelNofN + ' ' + totalAds + '): ';
}
this.countdownDiv.innerHTML = this.controller.getSettings().adLabel + podCount + remainingMinutes + ':' + remainingSeconds;
// Update UI
var playProgressRatio = currentTime / duration;
var playProgressPercent = playProgressRatio * 100;
this.progressDiv.style.width = playProgressPercent + '%';
};
/**
* Handles UI changes when the ad is unmuted.
*/
AdUi.prototype.unmute = function () {
this.addClass(this.muteDiv, 'ima-non-muted');
this.removeClass(this.muteDiv, 'ima-muted');
this.sliderLevelDiv.style.width = this.controller.getPlayerVolume() * 100 + '%';
};
/**
* Handles UI changes when the ad is muted.
*/
AdUi.prototype.mute = function () {
this.addClass(this.muteDiv, 'ima-muted');
this.removeClass(this.muteDiv, 'ima-non-muted');
this.sliderLevelDiv.style.width = '0%';
};
/*
* Listener for mouse down events during ad playback. Used for volume.
*/
AdUi.prototype.onAdVolumeSliderMouseDown = function () {
document.addEventListener('mouseup', this.boundOnMouseUp, false);
document.addEventListener('mousemove', this.boundOnMouseMove, false);
};
/*
* Mouse movement listener used for volume slider.
*/
AdUi.prototype.onMouseMove = function (event) {
this.changeVolume(event);
};
/*
* Mouse release listener used for volume slider.
*/
AdUi.prototype.onMouseUp = function (event) {
this.changeVolume(event);
document.removeEventListener('mouseup', this.boundOnMouseUp);
document.removeEventListener('mousemove', this.boundOnMouseMove);
};
/*
* Utility function to set volume and associated UI
*/
AdUi.prototype.changeVolume = function (event) {
var percent = (event.clientX - this.sliderDiv.getBoundingClientRect().left) / this.sliderDiv.offsetWidth;
percent *= 100;
// Bounds value 0-100 if mouse is outside slider region.
percent = Math.min(Math.max(percent, 0), 100);
this.sliderLevelDiv.style.width = percent + '%';
if (this.percent == 0) {
this.addClass(this.muteDiv, 'ima-muted');
this.removeClass(this.muteDiv, 'ima-non-muted');
} else {
this.addClass(this.muteDiv, 'ima-non-muted');
this.removeClass(this.muteDiv, 'ima-muted');
}
this.controller.setVolume(percent / 100); // 0-1
};
/**
* Show the ad container.
*/
AdUi.prototype.showAdContainer = function () {
this.adContainerDiv.style.display = 'block';
};
/**
* Hide the ad container
*/
AdUi.prototype.hideAdContainer = function () {
this.adContainerDiv.style.display = 'none';
};
/**
* Resets the state of the ad ui.
*/
AdUi.prototype.reset = function () {
this.hideAdContainer();
};
/**
* Handles ad errors.
*/
AdUi.prototype.onAdError = function () {
this.hideAdContainer();
};
/**
* Handles ad break starting.
*
* @param {Object} adEvent The event fired by the IMA SDK.
*/
AdUi.prototype.onAdBreakStart = function (adEvent) {
this.showAdContainer();
var contentType = adEvent.getAd().getContentType();
if (contentType === 'application/javascript' && !this.controller.getSettings().showControlsForJSAds) {
this.controlsDiv.style.display = 'none';
} else {
this.controlsDiv.style.display = 'block';
}
this.onAdsPlaying();
// Start with the ad controls minimized.
this.hideAdControls();
};
/**
* Handles ad break ending.
*/
AdUi.prototype.onAdBreakEnd = function () {
var currentAd = this.controller.getCurrentAd();
if (currentAd == null || // hide for post-roll only playlist
currentAd.isLinear()) {
// don't hide for non-linear ads
this.hideAdContainer();
}
this.controlsDiv.style.display = 'none';
this.countdownDiv.innerHTML = '';
};
/**
* Handles when all ads have finished playing.
*/
AdUi.prototype.onAllAdsCompleted = function () {
this.hideAdContainer();
};
/**
* Handles when a linear ad starts.
*/
AdUi.prototype.onLinearAdStart = function () {
// Don't bump container when controls are shown
this.removeClass(this.adContainerDiv, 'bumpable-ima-ad-container');
};
/**
* Handles when a non-linear ad starts.
*/
AdUi.prototype.onNonLinearAdLoad = function () {
// For non-linear ads that show after a linear ad. For linear ads, we show the
// ad container in onAdBreakStart to prevent blinking in pods.
this.adContainerDiv.style.display = 'block';
// Bump container when controls are shown
this.addClass(this.adContainerDiv, 'bumpable-ima-ad-container');
};
AdUi.prototype.onPlayerEnterFullscreen = function () {
this.addClass(this.fullscreenDiv, 'ima-fullscreen');
this.removeClass(this.fullscreenDiv, 'ima-non-fullscreen');
};
AdUi.prototype.onPlayerExitFullscreen = function () {
this.addClass(this.fullscreenDiv, 'ima-non-fullscreen');
this.removeClass(this.fullscreenDiv, 'ima-fullscreen');
};
/**
* Called when the player volume changes.
*
* @param {number} volume The new player volume.
*/
AdUi.prototype.onPlayerVolumeChanged = function (volume) {
if (volume == 0) {
this.addClass(this.muteDiv, 'ima-muted');
this.removeClass(this.muteDiv, 'ima-non-muted');
this.sliderLevelDiv.style.width = '0%';
} else {
this.addClass(this.muteDiv, 'ima-non-muted');
this.removeClass(this.muteDiv, 'ima-muted');
this.sliderLevelDiv.style.width = volume * 100 + '%';
}
};
/**
* Shows ad controls on mouseover.
*/
AdUi.prototype.showAdControls = function () {
this.addClass(this.controlsDiv, 'ima-controls-div-showing');
};
/**
* Hide the ad controls.
*/
AdUi.prototype.hideAdControls = function () {
this.removeClass(this.controlsDiv, 'ima-controls-div-showing');
};
/**
* Assigns the unique id and class names to the given element as well as the
* style class.
* @param {HTMLElement} element Element that needs the controlName assigned.
* @param {string} controlName Control name to assign.
*/
AdUi.prototype.assignControlAttributes = function (element, controlName) {
element.id = this.controlPrefix + controlName;
element.className = this.controlPrefix + controlName + ' ' + controlName;
};
/**
* Returns a regular expression to test a string for the given className.
*
* @param {string} className The name of the class.
* @return {RegExp} The regular expression used to test for that class.
*/
AdUi.prototype.getClassRegexp = function (className) {
// Matches on
// (beginning of string OR NOT word char)
// classname
// (negative lookahead word char OR end of string)
return new RegExp('(^|[^A-Za-z-])' + className + '((?![A-Za-z-])|$)', 'gi');
};
/**
* Returns whether or not the provided element has the provied class in its
* className.
* @param {HTMLElement} element Element to tes.t
* @param {string} className Class to look for.
* @return {boolean} True if element has className in class list. False
* otherwise.
*/
AdUi.prototype.elementHasClass = function (element, className) {
var classRegexp = this.getClassRegexp(className);
return classRegexp.test(element.className);
};
/**
* Adds a class to the given element if it doesn't already have the class
* @param {HTMLElement} element Element to which the class will be added.
* @param {string} classToAdd Class to add.
*/
AdUi.prototype.addClass = function (element, classToAdd) {
element.className = element.className.trim() + ' ' + classToAdd;
};
/**
* Removes a class from the given element if it has the given class
*
* @param {HTMLElement} element Element from which the class will be removed.
* @param {string} classToRemove Class to remove.
*/
AdUi.prototype.removeClass = function (element, classToRemove) {
var classRegexp = this.getClassRegexp(classToRemove);
element.className = element.className.trim().replace(classRegexp, '');
};
/**
* @return {HTMLElement} The div for the ad container.
*/
AdUi.prototype.getAdContainerDiv = function () {
return this.adContainerDiv;
};
/**
* Changes the flag to show or hide the ad countdown timer.
*
* @param {boolean} showCountdownIn Show or hide the countdown timer.
*/
AdUi.prototype.setShowCountdown = function (showCountdownIn) {
this.showCountdown = showCountdownIn;
this.countdownDiv.style.display = this.showCountdown ? 'block' : 'none';
};
var name = "videojs-ima";
var version = "1.7.2";
var license = "Apache-2.0";
var main = "./dist/videojs.ima.js";
var module$1 = "./dist/videojs.ima.es.js";
var author = { "name": "Google Inc." };
var engines = { "node": ">=0.8.0" };
var scripts = { "contBuild": "watch 'npm run rollup:max' src", "predevServer": "echo \"Starting up server on localhost:8000.\"", "devServer": "npm-run-all -p testServer contBuild", "lint": "eslint \"src/*.js\"", "rollup": "npm-run-all rollup:*", "rollup:max": "rollup -c configs/rollup.config.js", "rollup:es": "rollup -c configs/rollup.config.es.js", "rollup:min": "rollup -c configs/rollup.config.min.js", "pretest": "npm run rollup", "start": "npm run devServer", "test": "npm-run-all test:*", "test:vjs5": "npm install video.js@5.19.2 --no-save && npm-run-all -p -r testServer webdriver", "test:vjs6": "npm install video.js@6 --no-save && npm-run-all -p -r testServer webdriver", "test:vjs7": "npm install video.js@7 --no-save && npm-run-all -p -r testServer webdriver", "testServer": "http-server --cors -p 8000 --silent", "preversion": "node scripts/preversion.js && npm run lint && npm test", "version": "node scripts/version.js", "postversion": "node scripts/postversion.js", "webdriver": "mocha test/webdriver/*.js --no-timeouts" };
var repository = { "type": "git", "url": "https://github.com/googleads/videojs-ima" };
var files = ["CHANGELOG.md", "LICENSE", "README.md", "dist/", "src/"];
var dependencies = { "can-autoplay": "^3.0.0", "cryptiles": "^4.1.2", "extend": ">=3.0.2", "lodash": ">=4.17.13", "lodash.template": ">=4.5.0", "video.js": "^5.19.2 || ^6 || ^7", "videojs-contrib-ads": "^6" };
var devDependencies = { "babel-core": "^6.26.3", "babel-preset-env": "^1.7.0", "child_process": "^1.0.2", "chromedriver": "^2.35.0", "conventional-changelog-cli": "^2.0.21", "conventional-changelog-videojs": "^3.0.0", "eslint": "^4.11.0", "eslint-config-google": "^0.9.1", "eslint-plugin-jsdoc": "^3.2.0", "geckodriver": "^1.16.2", "http-server": "^0.10.0", "mocha": "^5.0.3", "npm-run-all": "^4.1.2", "path": "^0.12.7", "rimraf": "^2.6.2", "rollup": "^0.51.8", "rollup-plugin-babel": "^3.0.3", "rollup-plugin-copy": "^0.2.3", "rollup-plugin-json": "^2.3.0", "rollup-plugin-uglify": "^2.0.1", "selenium-webdriver": "^3.6.0", "uglify-es": "^3.1.10", "watch": "^1.0.2" };
var keywords = ["videojs", "videojs-plugin"];
var pkg = {
name: name,
version: version,
license: license,
main: main,
module: module$1,
author: author,
engines: engines,
scripts: scripts,
repository: repository,
files: files,
dependencies: dependencies,
devDependencies: devDependencies,
keywords: keywords
};
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* IMA SDK integration plugin for Video.js. For more information see
* https://www.github.com/googleads/videojs-ima
*/
/**
* Implementation of the IMA SDK for the plugin.
*
* @param {Object} controller Reference to the parent controller.
*
* @constructor
* @struct
* @final
*/
var SdkImpl = function SdkImpl(controller) {
/**
* Plugin controller.
*/
this.controller = controller;
/**
* IMA SDK AdDisplayContainer.
*/
this.adDisplayContainer = null;
/**
* True if the AdDisplayContainer has been initialized. False otherwise.
*/
this.adDisplayContainerInitialized = false;
/**
* IMA SDK AdsLoader
*/
this.adsLoader = null;
/**
* IMA SDK AdsManager
*/
this.adsManager = null;
/**
* IMA SDK AdsRenderingSettings.
*/
this.adsRenderingSettings = null;
/**
* VAST, VMAP, or ad rules response. Used in lieu of fetching a response
* from an ad tag URL.
*/
this.adsResponse = null;
/**
* Current IMA SDK Ad.
*/
this.currentAd = null;
/**
* Timer used to track ad progress.
*/
this.adTrackingTimer = null;
/**
* True if ALL_ADS_COMPLETED has fired, false until then.
*/
this.allAdsCompleted = false;
/**
* True if ads are currently displayed, false otherwise.
* True regardless of ad pause state if an ad is currently being displayed.
*/
this.adsActive = false;
/**
* True if ad is currently playing, false if ad is paused or ads are not
* currently displayed.
*/
this.adPlaying = false;
/**
* True if the ad is muted, false otherwise.
*/
this.adMuted = false;
/**
* Listener to be called to trigger manual ad break playback.
*/
this.adBreakReadyListener = undefined;
/**
* Tracks whether or not we have already called adsLoader.contentComplete().
*/
this.contentCompleteCalled = false;
/**
* Stores the dimensions for the ads manager.
*/
this.adsManagerDimensions = {
width: 0,
height: 0
};
/**
* Boolean flag to enable manual ad break playback.
*/
this.autoPlayAdBreaks = true;
if (this.controller.getSettings().autoPlayAdBreaks === false) {
this.autoPlayAdBreaks = false;
}
// Set SDK settings from plugin settings.
if (this.controller.getSettings().locale) {
/* eslint no-undef: 'error' */
/* global google */
google.ima.settings.setLocale(this.controller.getSettings().locale);
}
if (this.controller.getSettings().disableFlashAds) {
google.ima.settings.setDisableFlashAds(this.controller.getSettings().disableFlashAds);
}
if (this.controller.getSettings().disableCustomPlaybackForIOS10Plus) {
google.ima.settings.setDisableCustomPlaybackForIOS10Plus(this.controller.getSettings().disableCustomPlaybackForIOS10Plus);
}
};
/**
* Creates and initializes the IMA SDK objects.
*/
SdkImpl.prototype.initAdObjects = function () {
this.adDisplayContainer = new google.ima.AdDisplayContainer(this.controller.getAdContainerDiv(), this.controller.getContentPlayer());
this.adsLoader = new google.ima.AdsLoader(this.adDisplayContainer);
this.adsLoader.getSettings().setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.ENABLED);
if (this.controller.getSettings().vpaidAllowed == false) {
this.adsLoader.getSettings().setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.DISABLED);
}
if (this.controller.getSettings().vpaidMode) {
this.adsLoader.getSettings().setVpaidMode(this.controller.getSettings().vpaidMode);
}
if (this.controller.getSettings().locale) {
this.adsLoader.getSettings().setLocale(this.controller.getSettings().locale);
}
if (this.controller.getSettings().numRedirects) {
this.adsLoader.getSettings().setNumRedirects(this.controller.getSettings().numRedirects);
}
this.adsLoader.getSettings().setPlayerType('videojs-ima');
this.adsLoader.getSettings().setPlayerVersion(pkg.version);
this.adsLoader.getSettings().setAutoPlayAdBreaks(this.autoPlayAdBreaks);
this.adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, this.onAdsManagerLoaded.bind(this), false);
this.adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onAdsLoaderError.bind(this), false);
this.controller.playerWrapper.vjsPlayer.trigger({
type: 'ads-loader',
adsLoader: this.adsLoader
});
};
/**
* Creates the AdsRequest and request ads through the AdsLoader.
*/
SdkImpl.prototype.requestAds = function () {
var adsRequest = new google.ima.AdsRequest();
if (this.controller.getSettings().adTagUrl) {
adsRequest.adTagUrl = this.controller.getSettings().adTagUrl;
} else {
adsRequest.adsResponse = this.controller.getSettings().adsResponse;
}
if (this.controller.getSettings().forceNonLinearFullSlot) {
adsRequest.forceNonLinearFullSlot = true;
}
if (this.controller.getSettings().vastLoadTimeout) {
adsRequest.vastLoadTimeout = this.controller.getSettings().vastLoadTimeout;
}
adsRequest.linearAdSlotWidth = this.controller.getPlayerWidth();
adsRequest.linearAdSlotHeight = this.controller.getPlayerHeight();
adsRequest.nonLinearAdSlotWidth = this.controller.getSettings().nonLinearWidth || this.controller.getPlayerWidth();
adsRequest.nonLinearAdSlotHeight = this.controller.getSettings().nonLinearHeight || this.controller.getPlayerHeight();
adsRequest.setAdWillAutoPlay(this.controller.adsWillAutoplay());
adsRequest.setAdWillPlayMuted(this.controller.adsWillPlayMuted());
// Populate the adsRequestproperties with those provided in the AdsRequest
// object in the settings.
var providedAdsRequest = this.controller.getSettings().adsRequest;
if (providedAdsRequest && (typeof providedAdsRequest === 'undefined' ? 'undefined' : _typeof(providedAdsRequest)) === 'object') {
Object.keys(providedAdsRequest).forEach(function (key) {
adsRequest[key] = providedAdsRequest[key];
});
}
this.adsLoader.requestAds(adsRequest);
this.controller.playerWrapper.vjsPlayer.trigger({
type: 'ads-request',
AdsRequest: adsRequest
});
};
/**
* Listener for the ADS_MANAGER_LOADED event. Creates the AdsManager,
* sets up event listeners, and triggers the 'adsready' event for
* videojs-ads-contrib.
*
* @param {google.ima.AdsManagerLoadedEvent} adsManagerLoadedEvent Fired when
* the AdsManager loads.
*/
SdkImpl.prototype.onAdsManagerLoaded = function (adsManagerLoadedEvent) {
this.createAdsRenderingSettings();
this.adsManager = adsManagerLoadedEvent.getAdsManager(this.controller.getContentPlayheadTracker(), this.adsRenderingSettings);
this.adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onAdError.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY, this.onAdBreakReady.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, this.onContentPauseRequested.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, this.onContentResumeRequested.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, this.onAllAdsCompleted.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, this.onAdLoaded.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, this.onAdStarted.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, this.onAdComplete.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, this.onAdComplete.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.LOG, this.onAdLog.bind(this));
if (this.controller.getIsMobile()) {
// Show/hide controls on pause and resume (triggered by tap).
this.adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED, this.onAdPaused.bind(this));
this.adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED, this.onAdResumed.bind(this));
}
this.controller.playerWrapper.vjsPlayer.trigger({
type: 'ads-manager',
adsManager: this.adsManager
});
if (!this.autoPlayAdBreaks) {
this.initAdsManager();
}
this.controller.onAdsReady();
if (this.controller.getSettings().adsManagerLoadedCallback) {
this.controller.getSettings().adsManagerLoadedCallback();
}
};
/**
* Listener for errors fired by the AdsLoader.
* @param {google.ima.AdErrorEvent} event The error event thrown by the
* AdsLoader. See
* https://developers.google.com/interactive-media-ads/docs/sdks/html5/v3/apis#ima.AdError.Type
*/
SdkImpl.prototype.onAdsLoaderError = function (event) {
window.console.warn('AdsLoader error: ' + event.getError());
this.controller.onErrorLoadingAds(event);
if (this.adsManager) {
this.adsManager.destroy();
}
};
/**
* Initialize the ads manager.
*/
SdkImpl.prototype.initAdsManager = function () {
try {
var initWidth = this.controller.getPlayerWidth();
var initHeight = this.controller.getPlayerHeight();
this.adsManagerDimensions.width = initWidth;
this.adsManagerDimensions.height = initHeight;
this.adsManager.init(initWidth, initHeight, google.ima.ViewMode.NORMAL);
this.adsManager.setVolume(this.controller.getPlayerVolume());
this.initializeAdDisplayContainer();
} catch (adError) {
this.onAdError(adError);
}
};
/**
* Create AdsRenderingSettings for the IMA SDK.
*/
SdkImpl.prototype.createAdsRenderingSettings = function () {
this.adsRenderingSettings = new google.ima.AdsRenderingSettings();
this.adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
if (this.controller.getSettings().adsRenderingSettings) {
for (var setting in this.controller.getSettings().adsRenderingSettings) {
if (setting !== '') {
this.adsRenderingSettings[setting] = this.controller.getSettings().adsRenderingSettings[setting];
}
}
}
};
/**
* Listener for errors thrown by the AdsManager.
* @param {google.ima.AdErrorEvent} adErrorEvent The error event thrown by
* the AdsManager.
*/
SdkImpl.prototype.onAdError = function (adErrorEvent) {
var errorMessage = adErrorEvent.getError !== undefined ? adErrorEvent.getError() : adErrorEvent.stack;
window.console.warn('Ad error: ' + errorMessage);
this.adsManager.destroy();
this.controller.onAdError(adErrorEvent);
// reset these so consumers don't think we are still in an ad break,
// but reset them after any prior cleanup happens
this.adsActive = false;
this.adPlaying = false;
};
/**
* Listener for AD_BREAK_READY. Passes event on to publisher's listener.
* @param {google.ima.AdEvent} adEvent AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAdBreakReady = function (adEvent) {
this.adBreakReadyListener(adEvent);
};
/**
* Pauses the content video and displays the ad container so ads can play.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onContentPauseRequested = function (adEvent) {
this.adsActive = true;
this.adPlaying = true;
this.controller.onAdBreakStart(adEvent);
};
/**
* Resumes content video and hides the ad container.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onContentResumeRequested = function (adEvent) {
this.adsActive = false;
this.adPlaying = false;
this.controller.onAdBreakEnd();
// Hide controls in case of future non-linear ads. They'll be unhidden in
// content_pause_requested.
};
/**
* Records that ads have completed and calls contentAndAdsEndedListeners
* if content is also complete.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAllAdsCompleted = function (adEvent) {
this.allAdsCompleted = true;
this.controller.onAllAdsCompleted();
};
/**
* Starts the content video when a non-linear ad is loaded.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAdLoaded = function (adEvent) {
if (!adEvent.getAd().isLinear()) {
this.controller.onNonLinearAdLoad();
this.controller.playContent();
}
};
/**
* Starts the interval timer to check the current ad time when an ad starts
* playing.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAdStarted = function (adEvent) {
this.currentAd = adEvent.getAd();
if (this.currentAd.isLinear()) {
this.adTrackingTimer = setInterval(this.onAdPlayheadTrackerInterval.bind(this), 250);
this.controller.onLinearAdStart();
} else {
this.controller.onNonLinearAdStart();
}
};
/**
* Handles an ad click. Puts the player UI in a paused state.
*/
SdkImpl.prototype.onAdPaused = function () {
this.controller.onAdsPaused();
};
/**
* Syncs controls when an ad resumes.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAdResumed = function (adEvent) {
this.controller.onAdsResumed();
};
/**
* Clears the interval timer for current ad time when an ad completes.
*/
SdkImpl.prototype.onAdComplete = function () {
if (this.currentAd.isLinear()) {
clearInterval(this.adTrackingTimer);
}
};
/**
* Handles ad log messages.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the AdsManager.
*/
SdkImpl.prototype.onAdLog = function (adEvent) {
this.controller.onAdLog(adEvent);
};
/**
* Gets the current time and duration of the ad and calls the method to
* update the ad UI.
*/
SdkImpl.prototype.onAdPlayheadTrackerInterval = function () {
if (this.adsManager === null) return;
var remainingTime = this.adsManager.getRemainingTime();
var duration = this.currentAd.getDuration();
var currentTime = duration - remainingTime;
currentTime = currentTime > 0 ? currentTime : 0;
var totalAds = 0;
var adPosition = void 0;
if (this.currentAd.getAdPodInfo()) {
adPosition = this.currentAd.getAdPodInfo().getAdPosition();
totalAds = this.currentAd.getAdPodInfo().getTotalAds();
}
this.controller.onAdPlayhe