due to YouTube API issues
var videoId = parseId$1(source);
var id = generateId(player.provider); // Get poster, if already set
var poster = player.poster; // Replace media element
var container = createElement('div', {
id: id,
poster: poster
});
player.media = replaceElement(container, player.media); // Id to poster wrapper
var posterSrc = function posterSrc(format$$1) {
return "https://img.youtube.com/vi/".concat(videoId, "/").concat(format$$1, "default.jpg");
}; // Check thumbnail images in order of quality, but reject fallback thumbnails (120px wide)
loadImage(posterSrc('maxres'), 121) // Higest quality and unpadded
.catch(function () {
return loadImage(posterSrc('sd'), 121);
}) // 480p padded 4:3
.catch(function () {
return loadImage(posterSrc('hq'));
}) // 360p padded 4:3. Always exists
.then(function (image) {
return ui.setPoster.call(player, image.src);
}).then(function (posterSrc) {
// If the image is padded, use background-size "cover" instead (like youtube does too with their posters)
if (!posterSrc.includes('maxres')) {
player.elements.poster.style.backgroundSize = 'cover';
}
}).catch(function () {}); // Setup instance
// https://developers.google.com/youtube/iframe_api_reference
player.embed = new window.YT.Player(id, {
videoId: videoId,
playerVars: {
autoplay: player.config.autoplay ? 1 : 0,
// Autoplay
hl: player.config.hl,
// iframe interface language
controls: player.supported.ui ? 0 : 1,
// Only show controls if not fully supported
rel: 0,
// No related vids
showinfo: 0,
// Hide info
iv_load_policy: 3,
// Hide annotations
modestbranding: 1,
// Hide logos as much as possible (they still show one in the corner when paused)
disablekb: 1,
// Disable keyboard as we handle it
playsinline: 1,
// Allow iOS inline playback
// Tracking for stats
// origin: window ? `${window.location.protocol}//${window.location.host}` : null,
widget_referrer: window ? window.location.href : null,
// Captions are flaky on YouTube
cc_load_policy: player.captions.active ? 1 : 0,
cc_lang_pref: player.config.captions.language
},
events: {
onError: function onError(event) {
// YouTube may fire onError twice, so only handle it once
if (!player.media.error) {
var code = event.data; // Messages copied from https://developers.google.com/youtube/iframe_api_reference#onError
var message = {
2: 'The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.',
5: 'The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.',
100: 'The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.',
101: 'The owner of the requested video does not allow it to be played in embedded players.',
150: 'The owner of the requested video does not allow it to be played in embedded players.'
}[code] || 'An unknown error occured';
player.media.error = {
code: code,
message: message
};
triggerEvent.call(player, player.media, 'error');
}
},
onPlaybackRateChange: function onPlaybackRateChange(event) {
// Get the instance
var instance = event.target; // Get current speed
player.media.playbackRate = instance.getPlaybackRate();
triggerEvent.call(player, player.media, 'ratechange');
},
onReady: function onReady(event) {
// Bail if onReady has already been called. See issue #1108
if (is.function(player.media.play)) {
return;
} // Get the instance
var instance = event.target; // Get the title
youtube.getTitle.call(player, videoId); // Create a faux HTML5 API using the YouTube API
player.media.play = function () {
assurePlaybackState$1.call(player, true);
instance.playVideo();
};
player.media.pause = function () {
assurePlaybackState$1.call(player, false);
instance.pauseVideo();
};
player.media.stop = function () {
instance.stopVideo();
};
player.media.duration = instance.getDuration();
player.media.paused = true; // Seeking
player.media.currentTime = 0;
Object.defineProperty(player.media, 'currentTime', {
get: function get() {
return Number(instance.getCurrentTime());
},
set: function set(time) {
// If paused and never played, mute audio preventively (YouTube starts playing on seek if the video hasn't been played yet).
if (player.paused && !player.embed.hasPlayed) {
player.embed.mute();
} // Set seeking state and trigger event
player.media.seeking = true;
triggerEvent.call(player, player.media, 'seeking'); // Seek after events sent
instance.seekTo(time);
}
}); // Playback speed
Object.defineProperty(player.media, 'playbackRate', {
get: function get() {
return instance.getPlaybackRate();
},
set: function set(input) {
instance.setPlaybackRate(input);
}
}); // Volume
var volume = player.config.volume;
Object.defineProperty(player.media, 'volume', {
get: function get() {
return volume;
},
set: function set(input) {
volume = input;
instance.setVolume(volume * 100);
triggerEvent.call(player, player.media, 'volumechange');
}
}); // Muted
var muted = player.config.muted;
Object.defineProperty(player.media, 'muted', {
get: function get() {
return muted;
},
set: function set(input) {
var toggle = is.boolean(input) ? input : muted;
muted = toggle;
instance[toggle ? 'mute' : 'unMute']();
triggerEvent.call(player, player.media, 'volumechange');
}
}); // Source
Object.defineProperty(player.media, 'currentSrc', {
get: function get() {
return instance.getVideoUrl();
}
}); // Ended
Object.defineProperty(player.media, 'ended', {
get: function get() {
return player.currentTime === player.duration;
}
}); // Get available speeds
player.options.speed = instance.getAvailablePlaybackRates(); // Set the tabindex to avoid focus entering iframe
if (player.supported.ui) {
player.media.setAttribute('tabindex', -1);
}
triggerEvent.call(player, player.media, 'timeupdate');
triggerEvent.call(player, player.media, 'durationchange'); // Reset timer
clearInterval(player.timers.buffering); // Setup buffering
player.timers.buffering = setInterval(function () {
// Get loaded % from YouTube
player.media.buffered = instance.getVideoLoadedFraction(); // Trigger progress only when we actually buffer something
if (player.media.lastBuffered === null || player.media.lastBuffered < player.media.buffered) {
triggerEvent.call(player, player.media, 'progress');
} // Set last buffer point
player.media.lastBuffered = player.media.buffered; // Bail if we're at 100%
if (player.media.buffered === 1) {
clearInterval(player.timers.buffering); // Trigger event
triggerEvent.call(player, player.media, 'canplaythrough');
}
}, 200); // Rebuild UI
setTimeout(function () {
return ui.build.call(player);
}, 50);
},
onStateChange: function onStateChange(event) {
// Get the instance
var instance = event.target; // Reset timer
clearInterval(player.timers.playing);
var seeked = player.media.seeking && [1, 2].includes(event.data);
if (seeked) {
// Unset seeking and fire seeked event
player.media.seeking = false;
triggerEvent.call(player, player.media, 'seeked');
} // Handle events
// -1 Unstarted
// 0 Ended
// 1 Playing
// 2 Paused
// 3 Buffering
// 5 Video cued
switch (event.data) {
case -1:
// Update scrubber
triggerEvent.call(player, player.media, 'timeupdate'); // Get loaded % from YouTube
player.media.buffered = instance.getVideoLoadedFraction();
triggerEvent.call(player, player.media, 'progress');
break;
case 0:
assurePlaybackState$1.call(player, false); // YouTube doesn't support loop for a single video, so mimick it.
if (player.media.loop) {
// YouTube needs a call to `stopVideo` before playing again
instance.stopVideo();
instance.playVideo();
} else {
triggerEvent.call(player, player.media, 'ended');
}
break;
case 1:
// Restore paused state (YouTube starts playing on seek if the video hasn't been played yet)
if (player.media.paused && !player.embed.hasPlayed) {
player.media.pause();
} else {
assurePlaybackState$1.call(player, true);
triggerEvent.call(player, player.media, 'playing'); // Poll to get playback progress
player.timers.playing = setInterval(function () {
triggerEvent.call(player, player.media, 'timeupdate');
}, 50); // Check duration again due to YouTube bug
// https://github.com/sampotts/plyr/issues/374
// https://code.google.com/p/gdata-issues/issues/detail?id=8690
if (player.media.duration !== instance.getDuration()) {
player.media.duration = instance.getDuration();
triggerEvent.call(player, player.media, 'durationchange');
}
}
break;
case 2:
// Restore audio (YouTube starts playing on seek if the video hasn't been played yet)
if (!player.muted) {
player.embed.unMute();
}
assurePlaybackState$1.call(player, false);
break;
default:
break;
}
triggerEvent.call(player, player.elements.container, 'statechange', false, {
code: event.data
});
}
}
});
}
};
// ==========================================================================
var media = {
// Setup media
setup: function setup() {
// If there's no media, bail
if (!this.media) {
this.debug.warn('No media element found!');
return;
} // Add type class
toggleClass(this.elements.container, this.config.classNames.type.replace('{0}', this.type), true); // Add provider class
toggleClass(this.elements.container, this.config.classNames.provider.replace('{0}', this.provider), true); // Add video class for embeds
// This will require changes if audio embeds are added
if (this.isEmbed) {
toggleClass(this.elements.container, this.config.classNames.type.replace('{0}', 'video'), true);
} // Inject the player wrapper
if (this.isVideo) {
// Create the wrapper div
this.elements.wrapper = createElement('div', {
class: this.config.classNames.video
}); // Wrap the video in a container
wrap(this.media, this.elements.wrapper); // Faux poster container
this.elements.poster = createElement('div', {
class: this.config.classNames.poster
});
this.elements.wrapper.appendChild(this.elements.poster);
}
if (this.isHTML5) {
html5.extend.call(this);
} else if (this.isYouTube) {
youtube.setup.call(this);
} else if (this.isVimeo) {
vimeo.setup.call(this);
}
}
};
var Ads =
/*#__PURE__*/
function () {
/**
* Ads constructor.
* @param {object} player
* @return {Ads}
*/
function Ads(player) {
var _this = this;
_classCallCheck(this, Ads);
this.player = player;
this.publisherId = player.config.ads.publisherId;
this.playing = false;
this.initialized = false;
this.elements = {
container: null,
displayContainer: null
};
this.manager = null;
this.loader = null;
this.cuePoints = null;
this.events = {};
this.safetyTimer = null;
this.countdownTimer = null; // Setup a promise to resolve when the IMA manager is ready
this.managerPromise = new Promise(function (resolve, reject) {
// The ad is loaded and ready
_this.on('loaded', resolve); // Ads failed
_this.on('error', reject);
});
this.load();
}
_createClass(Ads, [{
key: "load",
/**
* Load the IMA SDK
*/
value: function load() {
var _this2 = this;
if (this.enabled) {
// Check if the Google IMA3 SDK is loaded or load it ourselves
if (!is.object(window.google) || !is.object(window.google.ima)) {
loadScript(this.player.config.urls.googleIMA.sdk).then(function () {
_this2.ready();
}).catch(function () {
// Script failed to load or is blocked
_this2.trigger('error', new Error('Google IMA SDK failed to load'));
});
} else {
this.ready();
}
}
}
/**
* Get the ads instance ready
*/
}, {
key: "ready",
value: function ready$$1() {
var _this3 = this;
// Start ticking our safety timer. If the whole advertisement
// thing doesn't resolve within our set time; we bail
this.startSafetyTimer(12000, 'ready()'); // Clear the safety timer
this.managerPromise.then(function () {
_this3.clearSafetyTimer('onAdsManagerLoaded()');
}); // Set listeners on the Plyr instance
this.listeners(); // Setup the IMA SDK
this.setupIMA();
} // Build the default tag URL
}, {
key: "setupIMA",
/**
* In order for the SDK to display ads for our video, we need to tell it where to put them,
* so here we define our ad container. This div is set up to render on top of the video player.
* Using the code below, we tell the SDK to render ads within that div. We also provide a
* handle to the content video player - the SDK will poll the current time of our player to
* properly place mid-rolls. After we create the ad display container, we initialize it. On
* mobile devices, this initialization is done as the result of a user action.
*/
value: function setupIMA() {
// Create the container for our advertisements
this.elements.container = createElement('div', {
class: this.player.config.classNames.ads
});
this.player.elements.container.appendChild(this.elements.container); // So we can run VPAID2
google.ima.settings.setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.ENABLED); // Set language
google.ima.settings.setLocale(this.player.config.ads.language); // We assume the adContainer is the video container of the plyr element
// that will house the ads
this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container); // Request video ads to be pre-loaded
this.requestAds();
}
/**
* Request advertisements
*/
}, {
key: "requestAds",
value: function requestAds() {
var _this4 = this;
var container = this.player.elements.container;
try {
// Create ads loader
this.loader = new google.ima.AdsLoader(this.elements.displayContainer); // Listen and respond to ads loaded and error events
this.loader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, function (event) {
return _this4.onAdsManagerLoaded(event);
}, false);
this.loader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, function (error) {
return _this4.onAdError(error);
}, false); // Request video ads
var request = new google.ima.AdsRequest();
request.adTagUrl = this.tagUrl; // Specify the linear and nonlinear slot sizes. This helps the SDK
// to select the correct creative if multiple are returned
request.linearAdSlotWidth = container.offsetWidth;
request.linearAdSlotHeight = container.offsetHeight;
request.nonLinearAdSlotWidth = container.offsetWidth;
request.nonLinearAdSlotHeight = container.offsetHeight; // We only overlay ads as we only support video.
request.forceNonLinearFullSlot = false; // Mute based on current state
request.setAdWillPlayMuted(!this.player.muted);
this.loader.requestAds(request);
} catch (e) {
this.onAdError(e);
}
}
/**
* Update the ad countdown
* @param {boolean} start
*/
}, {
key: "pollCountdown",
value: function pollCountdown() {
var _this5 = this;
var start = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (!start) {
clearInterval(this.countdownTimer);
this.elements.container.removeAttribute('data-badge-text');
return;
}
var update = function update() {
var time = formatTime(Math.max(_this5.manager.getRemainingTime(), 0));
var label = "".concat(i18n.get('advertisement', _this5.player.config), " - ").concat(time);
_this5.elements.container.setAttribute('data-badge-text', label);
};
this.countdownTimer = setInterval(update, 100);
}
/**
* This method is called whenever the ads are ready inside the AdDisplayContainer
* @param {Event} adsManagerLoadedEvent
*/
}, {
key: "onAdsManagerLoaded",
value: function onAdsManagerLoaded(event) {
var _this6 = this;
// Load could occur after a source change (race condition)
if (!this.enabled) {
return;
} // Get the ads manager
var settings = new google.ima.AdsRenderingSettings(); // Tell the SDK to save and restore content video state on our behalf
settings.restoreCustomPlaybackStateOnAdBreakComplete = true;
settings.enablePreloading = true; // The SDK is polling currentTime on the contentPlayback. And needs a duration
// so it can determine when to start the mid- and post-roll
this.manager = event.getAdsManager(this.player, settings); // Get the cue points for any mid-rolls by filtering out the pre- and post-roll
this.cuePoints = this.manager.getCuePoints(); // Add advertisement cue's within the time line if available
if (!is.empty(this.cuePoints)) {
this.cuePoints.forEach(function (cuePoint) {
if (cuePoint !== 0 && cuePoint !== -1 && cuePoint < _this6.player.duration) {
var seekElement = _this6.player.elements.progress;
if (is.element(seekElement)) {
var cuePercentage = 100 / _this6.player.duration * cuePoint;
var cue = createElement('span', {
class: _this6.player.config.classNames.cues
});
cue.style.left = "".concat(cuePercentage.toString(), "%");
seekElement.appendChild(cue);
}
}
});
} // Set volume to match player
this.manager.setVolume(this.player.volume); // Add listeners to the required events
// Advertisement error events
this.manager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, function (error) {
return _this6.onAdError(error);
}); // Advertisement regular events
Object.keys(google.ima.AdEvent.Type).forEach(function (type) {
_this6.manager.addEventListener(google.ima.AdEvent.Type[type], function (event) {
return _this6.onAdEvent(event);
});
}); // Resolve our adsManager
this.trigger('loaded');
}
/**
* This is where all the event handling takes place. Retrieve the ad from the event. Some
* events (e.g. ALL_ADS_COMPLETED) don't have the ad object associated
* https://developers.google.com/interactive-media-ads/docs/sdks/html5/v3/apis#ima.AdEvent.Type
* @param {Event} event
*/
}, {
key: "onAdEvent",
value: function onAdEvent(event) {
var _this7 = this;
var container = this.player.elements.container; // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
// don't have ad object associated
var ad = event.getAd(); // Proxy event
var dispatchEvent = function dispatchEvent(type) {
var event = "ads".concat(type.replace(/_/g, '').toLowerCase());
triggerEvent.call(_this7.player, _this7.player.media, event);
};
switch (event.type) {
case google.ima.AdEvent.Type.LOADED:
// This is the first event sent for an ad - it is possible to determine whether the
// ad is a video ad or an overlay
this.trigger('loaded'); // Bubble event
dispatchEvent(event.type); // Start countdown
this.pollCountdown(true);
if (!ad.isLinear()) {
// Position AdDisplayContainer correctly for overlay
ad.width = container.offsetWidth;
ad.height = container.offsetHeight;
} // console.info('Ad type: ' + event.getAd().getAdPodInfo().getPodIndex());
// console.info('Ad time: ' + event.getAd().getAdPodInfo().getTimeOffset());
break;
case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
// All ads for the current videos are done. We can now request new advertisements
// in case the video is re-played
// Fire event
dispatchEvent(event.type); // TODO: Example for what happens when a next video in a playlist would be loaded.
// So here we load a new video when all ads are done.
// Then we load new ads within a new adsManager. When the video
// Is started - after - the ads are loaded, then we get ads.
// You can also easily test cancelling and reloading by running
// player.ads.cancel() and player.ads.play from the console I guess.
// this.player.source = {
// type: 'video',
// title: 'View From A Blue Moon',
// sources: [{
// src:
// 'https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.mp4', type:
// 'video/mp4', }], poster:
// 'https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg', tracks:
// [ { kind: 'captions', label: 'English', srclang: 'en', src:
// 'https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.en.vtt',
// default: true, }, { kind: 'captions', label: 'French', srclang: 'fr', src:
// 'https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.fr.vtt', }, ],
// };
// TODO: So there is still this thing where a video should only be allowed to start
// playing when the IMA SDK is ready or has failed
this.loadAds();
break;
case google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED:
// This event indicates the ad has started - the video player can adjust the UI,
// for example display a pause button and remaining time. Fired when content should
// be paused. This usually happens right before an ad is about to cover the content
dispatchEvent(event.type);
this.pauseContent();
break;
case google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED:
// This event indicates the ad has finished - the video player can perform
// appropriate UI actions, such as removing the timer for remaining time detection.
// Fired when content should be resumed. This usually happens when an ad finishes
// or collapses
dispatchEvent(event.type);
this.pollCountdown();
this.resumeContent();
break;
case google.ima.AdEvent.Type.STARTED:
case google.ima.AdEvent.Type.MIDPOINT:
case google.ima.AdEvent.Type.COMPLETE:
case google.ima.AdEvent.Type.IMPRESSION:
case google.ima.AdEvent.Type.CLICK:
dispatchEvent(event.type);
break;
default:
break;
}
}
/**
* Any ad error handling comes through here
* @param {Event} event
*/
}, {
key: "onAdError",
value: function onAdError(event) {
this.cancel();
this.player.debug.warn('Ads error', event);
}
/**
* Setup hooks for Plyr and window events. This ensures
* the mid- and post-roll launch at the correct time. And
* resize the advertisement when the player resizes
*/
}, {
key: "listeners",
value: function listeners() {
var _this8 = this;
var container = this.player.elements.container;
var time; // Add listeners to the required events
this.player.on('ended', function () {
_this8.loader.contentComplete();
});
this.player.on('seeking', function () {
time = _this8.player.currentTime;
return time;
});
this.player.on('seeked', function () {
var seekedTime = _this8.player.currentTime;
if (is.empty(_this8.cuePoints)) {
return;
}
_this8.cuePoints.forEach(function (cuePoint, index) {
if (time < cuePoint && cuePoint < seekedTime) {
_this8.manager.discardAdBreak();
_this8.cuePoints.splice(index, 1);
}
});
}); // Listen to the resizing of the window. And resize ad accordingly
// TODO: eventually implement ResizeObserver
window.addEventListener('resize', function () {
if (_this8.manager) {
_this8.manager.resize(container.offsetWidth, container.offsetHeight, google.ima.ViewMode.NORMAL);
}
});
}
/**
* Initialize the adsManager and start playing advertisements
*/
}, {
key: "play",
value: function play() {
var _this9 = this;
var container = this.player.elements.container;
if (!this.managerPromise) {
this.resumeContent();
} // Play the requested advertisement whenever the adsManager is ready
this.managerPromise.then(function () {
// Initialize the container. Must be done via a user action on mobile devices
_this9.elements.displayContainer.initialize();
try {
if (!_this9.initialized) {
// Initialize the ads manager. Ad rules playlist will start at this time
_this9.manager.init(container.offsetWidth, container.offsetHeight, google.ima.ViewMode.NORMAL); // Call play to start showing the ad. Single video and overlay ads will
// start at this time; the call will be ignored for ad rules
_this9.manager.start();
}
_this9.initialized = true;
} catch (adError) {
// An error may be thrown if there was a problem with the
// VAST response
_this9.onAdError(adError);
}
}).catch(function () {});
}
/**
* Resume our video
*/
}, {
key: "resumeContent",
value: function resumeContent() {
// Hide the advertisement container
this.elements.container.style.zIndex = ''; // Ad is stopped
this.playing = false; // Play our video
if (this.player.currentTime < this.player.duration) {
this.player.play();
}
}
/**
* Pause our video
*/
}, {
key: "pauseContent",
value: function pauseContent() {
// Show the advertisement container
this.elements.container.style.zIndex = 3; // Ad is playing.
this.playing = true; // Pause our video.
this.player.pause();
}
/**
* Destroy the adsManager so we can grab new ads after this. If we don't then we're not
* allowed to call new ads based on google policies, as they interpret this as an accidental
* video requests. https://developers.google.com/interactive-
* media-ads/docs/sdks/android/faq#8
*/
}, {
key: "cancel",
value: function cancel() {
// Pause our video
if (this.initialized) {
this.resumeContent();
} // Tell our instance that we're done for now
this.trigger('error'); // Re-create our adsManager
this.loadAds();
}
/**
* Re-create our adsManager
*/
}, {
key: "loadAds",
value: function loadAds() {
var _this10 = this;
// Tell our adsManager to go bye bye
this.managerPromise.then(function () {
// Destroy our adsManager
if (_this10.manager) {
_this10.manager.destroy();
} // Re-set our adsManager promises
_this10.managerPromise = new Promise(function (resolve) {
_this10.on('loaded', resolve);
_this10.player.debug.log(_this10.manager);
}); // Now request some new advertisements
_this10.requestAds();
}).catch(function () {});
}
/**
* Handles callbacks after an ad event was invoked
* @param {string} event - Event type
*/
}, {
key: "trigger",
value: function trigger(event) {
var _this11 = this;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var handlers = this.events[event];
if (is.array(handlers)) {
handlers.forEach(function (handler) {
if (is.function(handler)) {
handler.apply(_this11, args);
}
});
}
}
/**
* Add event listeners
* @param {string} event - Event type
* @param {function} callback - Callback for when event occurs
* @return {Ads}
*/
}, {
key: "on",
value: function on$$1(event, callback) {
if (!is.array(this.events[event])) {
this.events[event] = [];
}
this.events[event].push(callback);
return this;
}
/**
* Setup a safety timer for when the ad network doesn't respond for whatever reason.
* The advertisement has 12 seconds to get its things together. We stop this timer when the
* advertisement is playing, or when a user action is required to start, then we clear the
* timer on ad ready
* @param {number} time
* @param {string} from
*/
}, {
key: "startSafetyTimer",
value: function startSafetyTimer(time, from) {
var _this12 = this;
this.player.debug.log("Safety timer invoked from: ".concat(from));
this.safetyTimer = setTimeout(function () {
_this12.cancel();
_this12.clearSafetyTimer('startSafetyTimer()');
}, time);
}
/**
* Clear our safety timer(s)
* @param {string} from
*/
}, {
key: "clearSafetyTimer",
value: function clearSafetyTimer(from) {
if (!is.nullOrUndefined(this.safetyTimer)) {
this.player.debug.log("Safety timer cleared from: ".concat(from));
clearTimeout(this.safetyTimer);
this.safetyTimer = null;
}
}
}, {
key: "enabled",
get: function get() {
return this.player.isHTML5 && this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId);
}
}, {
key: "tagUrl",
get: function get() {
var params = {
AV_PUBLISHERID: '58c25bb0073ef448b1087ad6',
AV_CHANNELID: '5a0458dc28a06145e4519d21',
AV_URL: window.location.hostname,
cb: Date.now(),
AV_WIDTH: 640,
AV_HEIGHT: 480,
AV_CDIM2: this.publisherId
};
var base = 'https://go.aniview.com/api/adserver6/vast/';
return "".concat(base, "?").concat(buildUrlParams(params));
}
}]);
return Ads;
}();
var source = {
// Add elements to HTML5 media (source, tracks, etc)
insertElements: function insertElements(type, attributes) {
var _this = this;
if (is.string(attributes)) {
insertElement(type, this.media, {
src: attributes
});
} else if (is.array(attributes)) {
attributes.forEach(function (attribute) {
insertElement(type, _this.media, attribute);
});
}
},
// Update source
// Sources are not checked for support so be careful
change: function change(input) {
var _this2 = this;
if (!getDeep(input, 'sources.length')) {
this.debug.warn('Invalid source format');
return;
} // Cancel current network requests
html5.cancelRequests.call(this); // Destroy instance and re-setup
this.destroy.call(this, function () {
// Reset quality options
_this2.options.quality = []; // Remove elements
removeElement(_this2.media);
_this2.media = null; // Reset class name
if (is.element(_this2.elements.container)) {
_this2.elements.container.removeAttribute('class');
} // Set the type and provider
var sources = input.sources,
type = input.type;
var _sources = _slicedToArray(sources, 1),
_sources$ = _sources[0],
_sources$$provider = _sources$.provider,
provider = _sources$$provider === void 0 ? providers.html5 : _sources$$provider,
src = _sources$.src;
var tagName = provider === 'html5' ? type : 'div';
var attributes = provider === 'html5' ? {} : {
src: src
};
Object.assign(_this2, {
provider: provider,
type: type,
// Check for support
supported: support.check(type, provider, _this2.config.playsinline),
// Create new element
media: createElement(tagName, attributes)
}); // Inject the new element
_this2.elements.container.appendChild(_this2.media); // Autoplay the new source?
if (is.boolean(input.autoplay)) {
_this2.config.autoplay = input.autoplay;
} // Set attributes for audio and video
if (_this2.isHTML5) {
if (_this2.config.crossorigin) {
_this2.media.setAttribute('crossorigin', '');
}
if (_this2.config.autoplay) {
_this2.media.setAttribute('autoplay', '');
}
if (!is.empty(input.poster)) {
_this2.poster = input.poster;
}
if (_this2.config.loop.active) {
_this2.media.setAttribute('loop', '');
}
if (_this2.config.muted) {
_this2.media.setAttribute('muted', '');
}
if (_this2.config.playsinline) {
_this2.media.setAttribute('playsinline', '');
}
} // Restore class hook
ui.addStyleHook.call(_this2); // Set new sources for html5
if (_this2.isHTML5) {
source.insertElements.call(_this2, 'source', sources);
} // Set video title
_this2.config.title = input.title; // Set up from scratch
media.setup.call(_this2); // HTML5 stuff
if (_this2.isHTML5) {
// Setup captions
if (Object.keys(input).includes('tracks')) {
source.insertElements.call(_this2, 'track', input.tracks);
}
} // If HTML5 or embed but not fully supported, setupInterface and call ready now
if (_this2.isHTML5 || _this2.isEmbed && !_this2.supported.ui) {
// Setup interface
ui.build.call(_this2);
}
if (_this2.isHTML5) {
// Load HTML5 sources
_this2.media.load();
} // Update the fullscreen support
_this2.fullscreen.update();
}, true);
}
};
// TODO: Use a WeakMap for private globals
// const globals = new WeakMap();
// Plyr instance
var Plyr =
/*#__PURE__*/
function () {
function Plyr(target, options) {
var _this = this;
_classCallCheck(this, Plyr);
this.timers = {}; // State
this.ready = false;
this.loading = false;
this.failed = false; // Touch device
this.touch = support.touch; // Set the media element
this.media = target; // String selector passed
if (is.string(this.media)) {
this.media = document.querySelectorAll(this.media);
} // jQuery, NodeList or Array passed, use first element
if (window.jQuery && this.media instanceof jQuery || is.nodeList(this.media) || is.array(this.media)) {
// eslint-disable-next-line
this.media = this.media[0];
} // Set config
this.config = extend({}, defaults, Plyr.defaults, options || {}, function () {
try {
return JSON.parse(_this.media.getAttribute('data-plyr-config'));
} catch (e) {
return {};
}
}()); // Elements cache
this.elements = {
container: null,
captions: null,
buttons: {},
display: {},
progress: {},
inputs: {},
settings: {
popup: null,
menu: null,
panels: {},
buttons: {}
}
}; // Captions
this.captions = {
active: null,
currentTrack: -1,
meta: new WeakMap()
}; // Fullscreen
this.fullscreen = {
active: false
}; // Options
this.options = {
speed: [],
quality: []
}; // Debugging
// TODO: move to globals
this.debug = new Console(this.config.debug); // Log config options and support
this.debug.log('Config', this.config);
this.debug.log('Support', support); // We need an element to setup
if (is.nullOrUndefined(this.media) || !is.element(this.media)) {
this.debug.error('Setup failed: no suitable element passed');
return;
} // Bail if the element is initialized
if (this.media.plyr) {
this.debug.warn('Target already setup');
return;
} // Bail if not enabled
if (!this.config.enabled) {
this.debug.error('Setup failed: disabled by config');
return;
} // Bail if disabled or no basic support
// You may want to disable certain UAs etc
if (!support.check().api) {
this.debug.error('Setup failed: no support');
return;
} // Cache original element state for .destroy()
var clone = this.media.cloneNode(true);
clone.autoplay = false;
this.elements.original = clone; // Set media type based on tag or data attribute
// Supported: video, audio, vimeo, youtube
var type = this.media.tagName.toLowerCase(); // Embed properties
var iframe = null;
var url = null; // Different setup based on type
switch (type) {
case 'div':
// Find the frame
iframe = this.media.querySelector('iframe'); //