aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/plugins')
-rw-r--r--src/js/plugins/ads.js101
-rw-r--r--src/js/plugins/previewThumbnails.js639
-rw-r--r--src/js/plugins/vimeo.js75
-rw-r--r--src/js/plugins/youtube.js50
4 files changed, 744 insertions, 121 deletions
diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js
index 375fdc13..c9256b0e 100644
--- a/src/js/plugins/ads.js
+++ b/src/js/plugins/ads.js
@@ -17,12 +17,12 @@ import { buildUrlParams } from '../utils/urls';
class Ads {
/**
* Ads constructor.
- * @param {object} player
+ * @param {Object} player
* @return {Ads}
*/
constructor(player) {
this.player = player;
- this.publisherId = player.config.ads.publisherId;
+ this.config = player.config.ads;
this.playing = false;
this.initialized = false;
this.elements = {
@@ -49,8 +49,13 @@ class Ads {
}
get enabled() {
+ const { config } = this;
+
return (
- this.player.isHTML5 && this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId)
+ this.player.isHTML5 &&
+ this.player.isVideo &&
+ config.enabled &&
+ (!is.empty(config.publisherId) || is.url(config.tagUrl))
);
}
@@ -95,8 +100,14 @@ class Ads {
this.setupIMA();
}
- // Build the default tag URL
+ // Build the tag URL
get tagUrl() {
+ const { config } = this;
+
+ if (is.url(config.tagUrl)) {
+ return config.tagUrl;
+ }
+
const params = {
AV_PUBLISHERID: '58c25bb0073ef448b1087ad6',
AV_CHANNELID: '5a0458dc28a06145e4519d21',
@@ -125,6 +136,7 @@ class Ads {
this.elements.container = createElement('div', {
class: this.player.config.classNames.ads,
});
+
this.player.elements.container.appendChild(this.elements.container);
// So we can run VPAID2
@@ -133,9 +145,11 @@ class Ads {
// 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);
+ // Set playback for iOS10+
+ google.ima.settings.setDisableCustomPlaybackForIOS10Plus(this.player.config.playsinline);
+
+ // 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, this.player.media);
// Request video ads to be pre-loaded
this.requestAds();
@@ -184,7 +198,7 @@ class Ads {
/**
* Update the ad countdown
- * @param {boolean} start
+ * @param {Boolean} start
*/
pollCountdown(start = false) {
if (!start) {
@@ -226,6 +240,23 @@ class Ads {
// Get the cue points for any mid-rolls by filtering out the pre- and post-roll
this.cuePoints = this.manager.getCuePoints();
+ // 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, error => this.onAdError(error));
+
+ // Advertisement regular events
+ Object.keys(google.ima.AdEvent.Type).forEach(type => {
+ this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event));
+ });
+
+ // Resolve our adsManager
+ this.trigger('loaded');
+ }
+
+ addCuePoints() {
// Add advertisement cue's within the time line if available
if (!is.empty(this.cuePoints)) {
this.cuePoints.forEach(cuePoint => {
@@ -233,7 +264,7 @@ class Ads {
const seekElement = this.player.elements.progress;
if (is.element(seekElement)) {
- const cuePercentage = 100 / this.player.duration * cuePoint;
+ const cuePercentage = (100 / this.player.duration) * cuePoint;
const cue = createElement('span', {
class: this.player.config.classNames.cues,
});
@@ -244,21 +275,6 @@ class Ads {
}
});
}
-
- // 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, error => this.onAdError(error));
-
- // Advertisement regular events
- Object.keys(google.ima.AdEvent.Type).forEach(type => {
- this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event));
- });
-
- // Resolve our adsManager
- this.trigger('loaded');
}
/**
@@ -273,6 +289,7 @@ class Ads {
// Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
// don't have ad object associated
const ad = event.getAd();
+ const adData = event.getAdData();
// Proxy event
const dispatchEvent = type => {
@@ -368,6 +385,12 @@ class Ads {
dispatchEvent(event.type);
break;
+ case google.ima.AdEvent.Type.LOG:
+ if (adData.adError) {
+ this.player.debug.warn(`Non-fatal ad error: ${adData.adError.getMessage()}`);
+ }
+ break;
+
default:
break;
}
@@ -391,14 +414,16 @@ class Ads {
const { container } = this.player.elements;
let time;
- // Add listeners to the required events
+ this.player.on('canplay', () => {
+ this.addCuePoints();
+ });
+
this.player.on('ended', () => {
this.loader.contentComplete();
});
- this.player.on('seeking', () => {
+ this.player.on('timeupdate', () => {
time = this.player.currentTime;
- return time;
});
this.player.on('seeked', () => {
@@ -471,10 +496,8 @@ class Ads {
// Ad is stopped
this.playing = false;
- // Play our video
- if (this.player.currentTime < this.player.duration) {
- this.player.play();
- }
+ // Play video
+ this.player.media.play();
}
/**
@@ -484,11 +507,11 @@ class Ads {
// Show the advertisement container
this.elements.container.style.zIndex = 3;
- // Ad is playing.
+ // Ad is playing
this.playing = true;
// Pause our video.
- this.player.pause();
+ this.player.media.pause();
}
/**
@@ -536,7 +559,7 @@ class Ads {
/**
* Handles callbacks after an ad event was invoked
- * @param {string} event - Event type
+ * @param {String} event - Event type
*/
trigger(event, ...args) {
const handlers = this.events[event];
@@ -552,8 +575,8 @@ class Ads {
/**
* Add event listeners
- * @param {string} event - Event type
- * @param {function} callback - Callback for when event occurs
+ * @param {String} event - Event type
+ * @param {Function} callback - Callback for when event occurs
* @return {Ads}
*/
on(event, callback) {
@@ -571,8 +594,8 @@ class Ads {
* 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
+ * @param {Number} time
+ * @param {String} from
*/
startSafetyTimer(time, from) {
this.player.debug.log(`Safety timer invoked from: ${from}`);
@@ -585,7 +608,7 @@ class Ads {
/**
* Clear our safety timer(s)
- * @param {string} from
+ * @param {String} from
*/
clearSafetyTimer(from) {
if (!is.nullOrUndefined(this.safetyTimer)) {
diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js
new file mode 100644
index 00000000..bd7a6bbd
--- /dev/null
+++ b/src/js/plugins/previewThumbnails.js
@@ -0,0 +1,639 @@
+import { createElement } from '../utils/elements';
+import { once } from '../utils/events';
+import fetch from '../utils/fetch';
+import is from '../utils/is';
+import { formatTime } from '../utils/time';
+
+// Arg: vttDataString example: "WEBVTT\n\n1\n00:00:05.000 --> 00:00:10.000\n1080p-00001.jpg"
+const parseVtt = vttDataString => {
+ const processedList = [];
+ const frames = vttDataString.split(/\r\n\r\n|\n\n|\r\r/);
+
+ frames.forEach(frame => {
+ const result = {};
+ const lines = frame.split(/\r\n|\n|\r/);
+
+ lines.forEach(line => {
+ if (!is.number(result.startTime)) {
+ // The line with start and end times on it is the first line of interest
+ const matchTimes = line.match(
+ /([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})( ?--> ?)([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})/,
+ ); // Note that this currently ignores caption formatting directives that are optionally on the end of this line - fine for non-captions VTT
+
+ if (matchTimes) {
+ result.startTime =
+ Number(matchTimes[1]) * 60 * 60 +
+ Number(matchTimes[2]) * 60 +
+ Number(matchTimes[3]) +
+ Number(`0.${matchTimes[4]}`);
+ result.endTime =
+ Number(matchTimes[6]) * 60 * 60 +
+ Number(matchTimes[7]) * 60 +
+ Number(matchTimes[8]) +
+ Number(`0.${matchTimes[9]}`);
+ }
+ } else if (!is.empty(line.trim()) && is.empty(result.text)) {
+ // If we already have the startTime, then we're definitely up to the text line(s)
+ const lineSplit = line.trim().split('#xywh=');
+ [result.text] = lineSplit;
+
+ // If there's content in lineSplit[1], then we have sprites. If not, then it's just one frame per image
+ if (lineSplit[1]) {
+ [result.x, result.y, result.w, result.h] = lineSplit[1].split(',');
+ }
+ }
+ });
+
+ if (result.text) {
+ processedList.push(result);
+ }
+ });
+
+ return processedList;
+};
+
+/**
+ * Preview thumbnails for seek hover and scrubbing
+ * Seeking: Hover over the seek bar (desktop only): shows a small preview container above the seek bar
+ * Scrubbing: Click and drag the seek bar (desktop and mobile): shows the preview image over the entire video, as if the video is scrubbing at very high speed
+ *
+ * Notes:
+ * - Thumbs are set via JS settings on Plyr init, not HTML5 'track' property. Using the track property would be a bit gross, because it doesn't support custom 'kinds'. kind=metadata might be used for something else, and we want to allow multiple thumbnails tracks. Tracks must have a unique combination of 'kind' and 'label'. We would have to do something like kind=metadata,label=thumbnails1 / kind=metadata,label=thumbnails2. Square peg, round hole
+ * - VTT info: the image URL is relative to the VTT, not the current document. But if the url starts with a slash, it will naturally be relative to the current domain. https://support.jwplayer.com/articles/how-to-add-preview-thumbnails
+ * - This implementation uses multiple separate img elements. Other implementations use background-image on one element. This would be nice and simple, but Firefox and Safari have flickering issues with replacing backgrounds of larger images. It seems that YouTube perhaps only avoids this because they don't have the option for high-res previews (even the fullscreen ones, when mousedown/seeking). Images appear over the top of each other, and previous ones are discarded once the new ones have been rendered
+ */
+
+class PreviewThumbnails {
+ /**
+ * PreviewThumbnails constructor.
+ * @param {Plyr} player
+ * @return {PreviewThumbnails}
+ */
+ constructor(player) {
+ this.player = player;
+ this.thumbnails = [];
+ this.loaded = false;
+ this.lastMouseMoveTime = Date.now();
+ this.mouseDown = false;
+ this.loadedImages = [];
+
+ this.elements = {
+ thumb: {},
+ scrubbing: {},
+ };
+
+ this.load();
+ }
+
+ get enabled() {
+ return this.player.isHTML5 && this.player.isVideo && this.player.config.previewThumbnails.enabled;
+ }
+
+ load() {
+ // Togglethe regular seek tooltip
+ if (this.player.elements.display.seekTooltip) {
+ this.player.elements.display.seekTooltip.hidden = this.enabled;
+ }
+
+ if (!this.enabled) {
+ return;
+ }
+
+ this.getThumbnails().then(() => {
+ // Render DOM elements
+ this.render();
+
+ // Check to see if thumb container size was specified manually in CSS
+ this.determineContainerAutoSizing();
+
+ this.loaded = true;
+ });
+ }
+
+ // Download VTT files and parse them
+ getThumbnails() {
+ return new Promise(resolve => {
+ const { src } = this.player.config.previewThumbnails;
+
+ if (is.empty(src)) {
+ throw new Error('Missing previewThumbnails.src config attribute');
+ }
+
+ // If string, convert into single-element list
+ const urls = is.string(src) ? [src] : src;
+
+ // Loop through each src URL. Download and process the VTT file, storing the resulting data in this.thumbnails
+ const promises = urls.map(u => this.getThumbnail(u));
+
+ Promise.all(promises).then(() => {
+ // Sort smallest to biggest (e.g., [120p, 480p, 1080p])
+ this.thumbnails.sort((x, y) => x.height - y.height);
+
+ this.player.debug.log('Preview thumbnails', this.thumbnails);
+
+ resolve();
+ });
+ });
+ }
+
+ // Process individual VTT file
+ getThumbnail(url) {
+ return new Promise(resolve => {
+ fetch(url).then(response => {
+ const thumbnail = {
+ frames: parseVtt(response),
+ height: null,
+ urlPrefix: '',
+ };
+
+ // If the URLs don't start with '/', then we need to set their relative path to be the location of the VTT file
+ // If the URLs do start with '/', then they obviously don't need a prefix, so it will remain blank
+ if (!thumbnail.frames[0].text.startsWith('/')) {
+ thumbnail.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1);
+ }
+
+ // Download the first frame, so that we can determine/set the height of this thumbnailsDef
+ const tempImage = new Image();
+
+ tempImage.onload = () => {
+ thumbnail.height = tempImage.naturalHeight;
+ thumbnail.width = tempImage.naturalWidth;
+
+ this.thumbnails.push(thumbnail);
+
+ resolve();
+ };
+
+ tempImage.src = thumbnail.urlPrefix + thumbnail.frames[0].text;
+ });
+ });
+ }
+
+ startMove(event) {
+ if (!this.loaded) {
+ return;
+ }
+
+ if (!is.event(event) || !['touchmove', 'mousemove'].includes(event.type)) {
+ return;
+ }
+
+ // Wait until media has a duration
+ if (!this.player.media.duration) {
+ return;
+ }
+
+ if (event.type === 'touchmove') {
+ // Calculate seek hover position as approx video seconds
+ this.seekTime = this.player.media.duration * (this.player.elements.inputs.seek.value / 100);
+ } else {
+ // Calculate seek hover position as approx video seconds
+ const clientRect = this.player.elements.progress.getBoundingClientRect();
+ const percentage = (100 / clientRect.width) * (event.pageX - clientRect.left);
+ this.seekTime = this.player.media.duration * (percentage / 100);
+
+ if (this.seekTime < 0) {
+ // The mousemove fires for 10+px out to the left
+ this.seekTime = 0;
+ }
+
+ if (this.seekTime > this.player.media.duration - 1) {
+ // Took 1 second off the duration for safety, because different players can disagree on the real duration of a video
+ this.seekTime = this.player.media.duration - 1;
+ }
+
+ this.mousePosX = event.pageX;
+
+ // Set time text inside image container
+ this.elements.thumb.time.innerText = formatTime(this.seekTime);
+ }
+
+ // Download and show image
+ this.showImageAtCurrentTime();
+ }
+
+ endMove() {
+ this.toggleThumbContainer(false, true);
+ }
+
+ startScrubbing(event) {
+ // Only act on left mouse button (0), or touch device (event.button is false)
+ if (event.button === false || event.button === 0) {
+ this.mouseDown = true;
+
+ // Wait until media has a duration
+ if (this.player.media.duration) {
+ this.toggleScrubbingContainer(true);
+ this.toggleThumbContainer(false, true);
+
+ // Download and show image
+ this.showImageAtCurrentTime();
+ }
+ }
+ }
+
+ endScrubbing() {
+ this.mouseDown = false;
+
+ // Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview
+ if (Math.ceil(this.lastTime) === Math.ceil(this.player.media.currentTime)) {
+ // The video was already seeked/loaded at the chosen time - hide immediately
+ this.toggleScrubbingContainer(false);
+ } else {
+ // The video hasn't seeked yet. Wait for that
+ once.call(this.player, this.player.media, 'timeupdate', () => {
+ // Re-check mousedown - we might have already started scrubbing again
+ if (!this.mouseDown) {
+ this.toggleScrubbingContainer(false);
+ }
+ });
+ }
+ }
+
+ /**
+ * Setup hooks for Plyr and window events
+ */
+ listeners() {
+ // Hide thumbnail preview - on mouse click, mouse leave (in listeners.js for now), and video play/seek. All four are required, e.g., for buffering
+ this.player.on('play', () => {
+ this.toggleThumbContainer(false, true);
+ });
+
+ this.player.on('seeked', () => {
+ this.toggleThumbContainer(false);
+ });
+
+ this.player.on('timeupdate', () => {
+ this.lastTime = this.player.media.currentTime;
+ });
+ }
+
+ /**
+ * Create HTML elements for image containers
+ */
+ render() {
+ // Create HTML element: plyr__preview-thumbnail-container
+ this.elements.thumb.container = createElement('div', {
+ class: this.player.config.classNames.previewThumbnails.thumbContainer,
+ });
+
+ // Wrapper for the image for styling
+ this.elements.thumb.imageContainer = createElement('div', {
+ class: this.player.config.classNames.previewThumbnails.imageContainer,
+ });
+ this.elements.thumb.container.appendChild(this.elements.thumb.imageContainer);
+
+ // Create HTML element, parent+span: time text (e.g., 01:32:00)
+ const timeContainer = createElement('div', {
+ class: this.player.config.classNames.previewThumbnails.timeContainer,
+ });
+
+ this.elements.thumb.time = createElement('span', {}, '00:00');
+ timeContainer.appendChild(this.elements.thumb.time);
+
+ this.elements.thumb.container.appendChild(timeContainer);
+
+ // Inject the whole thumb
+ this.player.elements.progress.appendChild(this.elements.thumb.container);
+
+ // Create HTML element: plyr__preview-scrubbing-container
+ this.elements.scrubbing.container = createElement('div', {
+ class: this.player.config.classNames.previewThumbnails.scrubbingContainer,
+ });
+
+ this.player.elements.wrapper.appendChild(this.elements.scrubbing.container);
+ }
+
+ showImageAtCurrentTime() {
+ if (this.mouseDown) {
+ this.setScrubbingContainerSize();
+ } else {
+ this.setThumbContainerSizeAndPos();
+ }
+
+ // Find the desired thumbnail index
+ // TODO: Handle a video longer than the thumbs where thumbNum is null
+ const thumbNum = this.thumbnails[0].frames.findIndex(
+ frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime,
+ );
+ const hasThumb = thumbNum >= 0;
+ let qualityIndex = 0;
+
+ // Show the thumb container if we're not scrubbing
+ if (!this.mouseDown) {
+ this.toggleThumbContainer(hasThumb);
+ }
+
+ // No matching thumb found
+ if (!hasThumb) {
+ return;
+ }
+
+ // Check to see if we've already downloaded higher quality versions of this image
+ this.thumbnails.forEach((thumbnail, index) => {
+ if (this.loadedImages.includes(thumbnail.frames[thumbNum].text)) {
+ qualityIndex = index;
+ }
+ });
+
+ // Only proceed if either thumbnum or thumbfilename has changed
+ if (thumbNum !== this.showingThumb) {
+ this.showingThumb = thumbNum;
+ this.loadImage(qualityIndex);
+ }
+ }
+
+ // Show the image that's currently specified in this.showingThumb
+ loadImage(qualityIndex = 0) {
+ const thumbNum = this.showingThumb;
+ const thumbnail = this.thumbnails[qualityIndex];
+ const { urlPrefix } = thumbnail;
+ const frame = thumbnail.frames[thumbNum];
+ const thumbFilename = thumbnail.frames[thumbNum].text;
+ const thumbUrl = urlPrefix + thumbFilename;
+
+ if (!this.currentImageElement || this.currentImageElement.dataset.filename !== thumbFilename) {
+ // If we're already loading a previous image, remove its onload handler - we don't want it to load after this one
+ // Only do this if not using sprites. Without sprites we really want to show as many images as possible, as a best-effort
+ if (this.loadingImage && this.usingSprites) {
+ this.loadingImage.onload = null;
+ }
+
+ // We're building and adding a new image. In other implementations of similar functionality (YouTube), background image
+ // is instead used. But this causes issues with larger images in Firefox and Safari - switching between background
+ // images causes a flicker. Putting a new image over the top does not
+ const previewImage = new Image();
+ previewImage.src = thumbUrl;
+ previewImage.dataset.index = thumbNum;
+ previewImage.dataset.filename = thumbFilename;
+ this.showingThumbFilename = thumbFilename;
+
+ this.player.debug.log(`Loading image: ${thumbUrl}`);
+
+ // For some reason, passing the named function directly causes it to execute immediately. So I've wrapped it in an anonymous function...
+ previewImage.onload = () =>
+ this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true);
+ this.loadingImage = previewImage;
+ this.removeOldImages(previewImage);
+ } else {
+ // Update the existing image
+ this.showImage(this.currentImageElement, frame, qualityIndex, thumbNum, thumbFilename, false);
+ this.currentImageElement.dataset.index = thumbNum;
+ this.removeOldImages(this.currentImageElement);
+ }
+ }
+
+ showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) {
+ this.player.debug.log(
+ `Showing thumb: ${thumbFilename}. num: ${thumbNum}. qual: ${qualityIndex}. newimg: ${newImage}`,
+ );
+ this.setImageSizeAndOffset(previewImage, frame);
+
+ if (newImage) {
+ this.currentImageContainer.appendChild(previewImage);
+ this.currentImageElement = previewImage;
+
+ if (!this.loadedImages.includes(thumbFilename)) {
+ this.loadedImages.push(thumbFilename);
+ }
+ }
+
+ // Preload images before and after the current one
+ // Show higher quality of the same frame
+ // Each step here has a short time delay, and only continues if still hovering/seeking the same spot. This is to protect slow connections from overloading
+ this.preloadNearby(thumbNum, true)
+ .then(this.preloadNearby(thumbNum, false))
+ .then(this.getHigherQuality(qualityIndex, previewImage, frame, thumbFilename));
+ }
+
+ // Remove all preview images that aren't the designated current image
+ removeOldImages(currentImage) {
+ // Get a list of all images, convert it from a DOM list to an array
+ Array.from(this.currentImageContainer.children).forEach(image => {
+ if (image.tagName.toLowerCase() !== 'img') {
+ return;
+ }
+
+ const removeDelay = this.usingSprites ? 500 : 1000;
+
+ if (image.dataset.index !== currentImage.dataset.index && !image.dataset.deleting) {
+ // Wait 200ms, as the new image can take some time to show on certain browsers (even though it was downloaded before showing). This will prevent flicker, and show some generosity towards slower clients
+ // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function
+ image.dataset.deleting = true;
+ // This has to be set before the timeout - to prevent issues switching between hover and scrub
+ const { currentImageContainer } = this;
+
+ setTimeout(() => {
+ currentImageContainer.removeChild(image);
+ this.player.debug.log(`Removing thumb: ${image.dataset.filename}`);
+ }, removeDelay);
+ }
+ });
+ }
+
+ // Preload images before and after the current one. Only if the user is still hovering/seeking the same frame
+ // This will only preload the lowest quality
+ preloadNearby(thumbNum, forward = true) {
+ return new Promise(resolve => {
+ setTimeout(() => {
+ const oldThumbFilename = this.thumbnails[0].frames[thumbNum].text;
+
+ if (this.showingThumbFilename === oldThumbFilename) {
+ // Find the nearest thumbs with different filenames. Sometimes it'll be the next index, but in the case of sprites, it might be 100+ away
+ let thumbnailsClone;
+ if (forward) {
+ thumbnailsClone = this.thumbnails[0].frames.slice(thumbNum);
+ } else {
+ thumbnailsClone = this.thumbnails[0].frames.slice(0, thumbNum).reverse();
+ }
+
+ let foundOne = false;
+
+ thumbnailsClone.forEach(frame => {
+ const newThumbFilename = frame.text;
+
+ if (newThumbFilename !== oldThumbFilename) {
+ // Found one with a different filename. Make sure it hasn't already been loaded on this page visit
+ if (!this.loadedImages.includes(newThumbFilename)) {
+ foundOne = true;
+ this.player.debug.log(`Preloading thumb filename: ${newThumbFilename}`);
+
+ const { urlPrefix } = this.thumbnails[0];
+ const thumbURL = urlPrefix + newThumbFilename;
+
+ const previewImage = new Image();
+ previewImage.src = thumbURL;
+ previewImage.onload = () => {
+ this.player.debug.log(`Preloaded thumb filename: ${newThumbFilename}`);
+ if (!this.loadedImages.includes(newThumbFilename))
+ this.loadedImages.push(newThumbFilename);
+
+ // We don't resolve until the thumb is loaded
+ resolve();
+ };
+ }
+ }
+ });
+
+ // If there are none to preload then we want to resolve immediately
+ if (!foundOne) {
+ resolve();
+ }
+ }
+ }, 300);
+ });
+ }
+
+ // If user has been hovering current image for half a second, look for a higher quality one
+ getHigherQuality(currentQualityIndex, previewImage, frame, thumbFilename) {
+ if (currentQualityIndex < this.thumbnails.length - 1) {
+ // Only use the higher quality version if it's going to look any better - if the current thumb is of a lower pixel density than the thumbnail container
+ let previewImageHeight = previewImage.naturalHeight;
+
+ if (this.usingSprites) {
+ previewImageHeight = frame.h;
+ }
+
+ if (previewImageHeight < this.thumbContainerHeight) {
+ // Recurse back to the loadImage function - show a higher quality one, but only if the viewer is on this frame for a while
+ setTimeout(() => {
+ // Make sure the mouse hasn't already moved on and started hovering at another image
+ if (this.showingThumbFilename === thumbFilename) {
+ this.player.debug.log(`Showing higher quality thumb for: ${thumbFilename}`);
+ this.loadImage(currentQualityIndex + 1);
+ }
+ }, 300);
+ }
+ }
+ }
+
+ get currentImageContainer() {
+ if (this.mouseDown) {
+ return this.elements.scrubbing.container;
+ }
+
+ return this.elements.thumb.imageContainer;
+ }
+
+ get usingSprites() {
+ return Object.keys(this.thumbnails[0].frames[0]).includes('w');
+ }
+
+ get thumbAspectRatio() {
+ if (this.usingSprites) {
+ return this.thumbnails[0].frames[0].w / this.thumbnails[0].frames[0].h;
+ }
+
+ return this.thumbnails[0].width / this.thumbnails[0].height;
+ }
+
+ get thumbContainerHeight() {
+ if (this.mouseDown) {
+ // Can't use media.clientHeight - HTML5 video goes big and does black bars above and below
+ return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio);
+ }
+
+ return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio / 4);
+ }
+
+ get currentImageElement() {
+ if (this.mouseDown) {
+ return this.currentScrubbingImageElement;
+ }
+
+ return this.currentThumbnailImageElement;
+ }
+
+ set currentImageElement(element) {
+ if (this.mouseDown) {
+ this.currentScrubbingImageElement = element;
+ } else {
+ this.currentThumbnailImageElement = element;
+ }
+ }
+
+ toggleThumbContainer(toggle = false, clearShowing = false) {
+ const className = this.player.config.classNames.previewThumbnails.thumbContainerShown;
+ this.elements.thumb.container.classList.toggle(className, toggle);
+
+ if (!toggle && clearShowing) {
+ this.showingThumb = null;
+ this.showingThumbFilename = null;
+ }
+ }
+
+ toggleScrubbingContainer(toggle = false) {
+ const className = this.player.config.classNames.previewThumbnails.scrubbingContainerShown;
+ this.elements.scrubbing.container.classList.toggle(className, toggle);
+
+ if (!toggle) {
+ this.showingThumb = null;
+ this.showingThumbFilename = null;
+ }
+ }
+
+ determineContainerAutoSizing() {
+ if (this.elements.thumb.imageContainer.clientHeight > 20) {
+ // This will prevent auto sizing in this.setThumbContainerSizeAndPos()
+ this.sizeSpecifiedInCSS = true;
+ }
+ }
+
+ // Set the size to be about a quarter of the size of video. Unless option dynamicSize === false, in which case it needs to be set in CSS
+ setThumbContainerSizeAndPos() {
+ if (!this.sizeSpecifiedInCSS) {
+ const thumbWidth = Math.floor(this.thumbContainerHeight * this.thumbAspectRatio);
+ this.elements.thumb.imageContainer.style.height = `${this.thumbContainerHeight}px`;
+ this.elements.thumb.imageContainer.style.width = `${thumbWidth}px`;
+ }
+
+ this.setThumbContainerPos();
+ }
+
+ setThumbContainerPos() {
+ const seekbarRect = this.player.elements.progress.getBoundingClientRect();
+ const plyrRect = this.player.elements.container.getBoundingClientRect();
+ const { container } = this.elements.thumb;
+
+ // Find the lowest and highest desired left-position, so we don't slide out the side of the video container
+ const minVal = plyrRect.left - seekbarRect.left + 10;
+ const maxVal = plyrRect.right - seekbarRect.left - container.clientWidth - 10;
+
+ // Set preview container position to: mousepos, minus seekbar.left, minus half of previewContainer.clientWidth
+ let previewPos = this.mousePosX - seekbarRect.left - container.clientWidth / 2;
+
+ if (previewPos < minVal) {
+ previewPos = minVal;
+ }
+
+ if (previewPos > maxVal) {
+ previewPos = maxVal;
+ }
+
+ container.style.left = `${previewPos}px`;
+ }
+
+ // Can't use 100% width, in case the video is a different aspect ratio to the video container
+ setScrubbingContainerSize() {
+ this.elements.scrubbing.container.style.width = `${this.player.media.clientWidth}px`;
+ // Can't use media.clientHeight - html5 video goes big and does black bars above and below
+ this.elements.scrubbing.container.style.height = `${this.player.media.clientWidth / this.thumbAspectRatio}px`;
+ }
+
+ // Sprites need to be offset to the correct location
+ setImageSizeAndOffset(previewImage, frame) {
+ if (!this.usingSprites) {
+ return;
+ }
+
+ // Find difference between height and preview container height
+ const multiplier = this.thumbContainerHeight / frame.h;
+
+ previewImage.style.height = `${Math.floor(previewImage.naturalHeight * multiplier)}px`;
+ previewImage.style.width = `${Math.floor(previewImage.naturalWidth * multiplier)}px`;
+ previewImage.style.left = `-${frame.x * multiplier}px`;
+ previewImage.style.top = `-${frame.y * multiplier}px`;
+ }
+}
+
+export default PreviewThumbnails;
diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js
index 2d9ba6e2..a7664e73 100644
--- a/src/js/plugins/vimeo.js
+++ b/src/js/plugins/vimeo.js
@@ -10,7 +10,9 @@ import { triggerEvent } from '../utils/events';
import fetch from '../utils/fetch';
import is from '../utils/is';
import loadScript from '../utils/loadScript';
+import { extend } from '../utils/objects';
import { format, stripHTML } from '../utils/strings';
+import { setAspectRatio } from '../utils/style';
import { buildUrlParams } from '../utils/urls';
// Parse Vimeo ID from URL
@@ -27,13 +29,6 @@ function parseId(url) {
return url.match(regex) ? RegExp.$2 : url;
}
-// Get aspect ratio for dimensions
-function getAspectRatio(width, height) {
- const getRatio = (w, h) => (h === 0 ? w : getRatio(h, w % h));
- const ratio = getRatio(width, height);
- return `${width / ratio}:${height / ratio}`;
-}
-
// Set playback state and trigger change (only on actual change)
function assurePlaybackState(play) {
if (play && !this.embed.hasPlayed) {
@@ -51,7 +46,7 @@ const vimeo = {
toggleClass(this.elements.wrapper, this.config.classNames.embed, true);
// Set intial ratio
- vimeo.setAspectRatio.call(this);
+ setAspectRatio.call(this);
// Load the API if not already
if (!is.object(window.Vimeo)) {
@@ -67,40 +62,25 @@ const vimeo = {
}
},
- // Set aspect ratio
- // For Vimeo we have an extra 300% height <div> to hide the standard controls and UI
- setAspectRatio(input) {
- const [x, y] = (is.string(input) ? input : this.config.ratio).split(':').map(Number);
- const padding = (100 / x) * y;
- vimeo.padding = padding;
- this.elements.wrapper.style.paddingBottom = `${padding}%`;
-
- if (this.supported.ui) {
- const height = 240;
- const offset = (height - padding) / (height / 50);
-
- this.media.style.transform = `translateY(-${offset}%)`;
- }
- },
-
// API Ready
ready() {
const player = this;
+ const config = player.config.vimeo;
// Get Vimeo params for the iframe
- const options = {
- loop: player.config.loop.active,
- autoplay: player.autoplay,
- // muted: player.muted,
- byline: false,
- portrait: false,
- title: false,
- speed: true,
- transparent: 0,
- gesture: 'media',
- playsinline: !this.config.fullscreen.iosNative,
- };
- const params = buildUrlParams(options);
+ const params = buildUrlParams(
+ extend(
+ {},
+ {
+ loop: player.config.loop.active,
+ autoplay: player.autoplay,
+ muted: player.muted,
+ gesture: 'media',
+ playsinline: !this.config.fullscreen.iosNative,
+ },
+ config,
+ ),
+ );
// Get the source URL or ID
let source = player.media.getAttribute('src');
@@ -300,8 +280,9 @@ const vimeo = {
// Set aspect ratio based on video size
Promise.all([player.embed.getVideoWidth(), player.embed.getVideoHeight()]).then(dimensions => {
- vimeo.ratio = getAspectRatio(dimensions[0], dimensions[1]);
- vimeo.setAspectRatio.call(this, vimeo.ratio);
+ const [width, height] = dimensions;
+ player.embed.ratio = `${width}:${height}`;
+ setAspectRatio.call(this, player.embed.ratio);
});
// Set autopause
@@ -405,22 +386,6 @@ const vimeo = {
triggerEvent.call(player, player.media, 'error');
});
- // Set height/width on fullscreen
- player.on('enterfullscreen exitfullscreen', event => {
- const { target } = player.fullscreen;
-
- // Ignore for iOS native
- if (target !== player.elements.container) {
- return;
- }
-
- const toggle = event.type === 'enterfullscreen';
- const [x, y] = vimeo.ratio.split(':').map(Number);
- const dimension = x > y ? 'width' : 'height';
-
- target.style[dimension] = toggle ? `${vimeo.padding}%` : null;
- });
-
// Rebuild UI
setTimeout(() => ui.build.call(player), 0);
},
diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js
index 73175c14..d5972c80 100644
--- a/src/js/plugins/youtube.js
+++ b/src/js/plugins/youtube.js
@@ -9,7 +9,9 @@ import fetch from '../utils/fetch';
import is from '../utils/is';
import loadImage from '../utils/loadImage';
import loadScript from '../utils/loadScript';
+import { extend } from '../utils/objects';
import { format, generateId } from '../utils/strings';
+import { setAspectRatio } from '../utils/style';
// Parse YouTube ID from URL
function parseId(url) {
@@ -38,7 +40,7 @@ const youtube = {
toggleClass(this.elements.wrapper, this.config.classNames.embed, true);
// Set aspect ratio
- youtube.setAspectRatio.call(this);
+ setAspectRatio.call(this);
// Setup API
if (is.object(window.YT) && is.function(window.YT.Player)) {
@@ -98,12 +100,6 @@ const youtube = {
}
},
- // Set aspect ratio
- setAspectRatio() {
- const ratio = this.config.ratio.split(':');
- this.elements.wrapper.style.paddingBottom = `${100 / ratio[0] * ratio[1]}%`;
- },
-
// API ready
ready() {
const player = this;
@@ -134,7 +130,7 @@ const youtube = {
player.media = replaceElement(container, player.media);
// Id to poster wrapper
- const posterSrc = format => `https://img.youtube.com/vi/${videoId}/${format}default.jpg`;
+ const posterSrc = format => `https://i.ytimg.com/vi/${videoId}/${format}default.jpg`;
// Check thumbnail images in order of quality, but reject fallback thumbnails (120px wide)
loadImage(posterSrc('maxres'), 121) // Higest quality and unpadded
@@ -149,29 +145,29 @@ const youtube = {
})
.catch(() => {});
+ const config = player.config.youtube;
+
// Setup instance
// https://developers.google.com/youtube/iframe_api_reference
player.embed = new window.YT.Player(id, {
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,
- },
+ host: config.noCookie ? 'https://www.youtube-nocookie.com' : undefined,
+ playerVars: extend(
+ {},
+ {
+ 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
+ disablekb: 1, // Disable keyboard as we handle it
+ playsinline: !player.config.fullscreen.iosNative ? 1 : 0, // Allow iOS inline playback
+ // Captions are flaky on YouTube
+ cc_load_policy: player.captions.active ? 1 : 0,
+ cc_lang_pref: player.config.captions.language,
+ // Tracking for stats
+ widget_referrer: window ? window.location.href : null,
+ },
+ config,
+ ),
events: {
onError(event) {
// YouTube may fire onError twice, so only handle it once