Skip to content

Commit

Permalink
Cache Media Session artwork on download and play.
Browse files Browse the repository at this point in the history
  • Loading branch information
dero committed Oct 4, 2021
1 parent 534a465 commit d7ac820
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/js/pages/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default (routerContext) => {
playButton.addEventListener('click', (e) => {
const videoContainer = e.target.closest('.video-container');
const playerWrapper = videoContainer.querySelector('.video-container--player');
const videoPlayer = new VideoPlayer();
const videoPlayer = new VideoPlayer(downloader);

videoContainer.classList.add('has-player');
videoPlayer.render(currentVideoData);
Expand Down
36 changes: 0 additions & 36 deletions src/js/sw/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,9 @@ export default [
'/index.html',
'/styles.css',
'https://storage.googleapis.com/kino-assets/single-video/thumbnail.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/single-video/artwork-512x512.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/thumbnail.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/multiple-sources/artwork-512x512.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/thumbnail.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/using-webvtt/artwork-512x512.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/thumbnail.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/streaming-basics/artwork-512x512.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/thumbnail.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/efficient-formats/artwork-512x512.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/thumbnail.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-96x96.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-128x128.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-192x192.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-256x256.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-384x384.png',
'https://storage.googleapis.com/kino-assets/adaptive-streaming/artwork-512x512.png',
];
3 changes: 0 additions & 3 deletions src/js/utils/generateCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export default function generateCache() {
} else if (Object.prototype.toString.call(video.thumbnail) === '[object String]') {
assetsToCache.push(video.thumbnail);
}
if (Array.isArray(video['media-session-artwork'])) {
video['media-session-artwork'].forEach((artworkObject) => assetsToCache.push(artworkObject.src));
}
});

const data = `/*
Expand Down
29 changes: 25 additions & 4 deletions src/js/web-components/video-download/VideoDownloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ export default class VideoDownloader extends HTMLElement {
return subtitlesUrls;
}

/**
* Returns the artwork images URLs used by Media Session API
* to render the media popup / notification.
*
* @returns {string[]} URLs.
*/
getMediaSessionArtworkUrls() {
const artworkObjects = this.internal.videoData['media-session-artwork'] || [];
const artworkUrls = artworkObjects.map((artworkObject) => artworkObject.src);

return artworkUrls;
}

/**
* Saves assets to the specified cache using Cache API.
*
Expand All @@ -203,14 +216,22 @@ export default class VideoDownloader extends HTMLElement {

/**
* Downloads the current video and its assets to the cache and IDB.
*
* @param {object} opts Download options.
* @param {boolean} opts.assetsOnly Whether to cache only video assets: poster images,
* subtitles and Media Session API artowrk.
*/
async download() {
async download(opts = {}) {
const posterURLs = this.getPosterURLs();
const subtitlesURLs = this.getSubtitlesUrls();
const mediaSessionArtworkURLs = this.getMediaSessionArtworkUrls();

this.downloading = true;
this.saveToCache([...posterURLs, ...subtitlesURLs]);
this.runIDBDownloads();
this.saveToCache([...posterURLs, ...subtitlesURLs, ...mediaSessionArtworkURLs]);

if (!opts.assetsOnly) {
this.downloading = true;
this.runIDBDownloads();
}
}

/**
Expand Down
16 changes: 13 additions & 3 deletions src/js/web-components/video-player/VideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import selectSource from '../../utils/selectSource';
import { MEDIA_SESSION_DEFAULT_ARTWORK } from '../../constants';

export default class extends HTMLElement {
constructor() {
/**
* @param {VideoDownloader} downloader Video downloader associated with the current video.
*/
constructor(downloader) {
super();

this.internal = {};
this.internal.root = this.attachShadow({ mode: 'open' });
this.internal = {
downloader,
root: this.attachShadow({ mode: 'open' }),
};
}

/**
Expand Down Expand Up @@ -283,10 +288,15 @@ export default class extends HTMLElement {
*/
play() {
const HAVE_NOTHING = 0;

if (this.videoElement.readyState === HAVE_NOTHING) {
this.videoElement.addEventListener('loadeddata', this.videoElement.play, { once: true });
} else {
this.videoElement.play();
}

this.internal.downloader.download({
assetsOnly: true,
});
}
}

0 comments on commit d7ac820

Please sign in to comment.