Implemented Media Session API for video components#10476
Implemented Media Session API for video components#10476aghassemi merged 7 commits intoampproject:masterfrom
Conversation
6cb9cfc to
e47397c
Compare
| /** | ||
| * Returns video's meta data (poster, artist, album, etc.) | ||
| * @return {!VideoMetaDef} metadata | ||
| */ |
There was a problem hiding this comment.
getMetaData() for consistency
src/video-interface.js
Outdated
| export let VideoAnalyticsDetailsDef; | ||
|
|
||
| /** | ||
| * @typedef {{ |
There was a problem hiding this comment.
define on top of the file
| */ | ||
| hideControls() {} | ||
|
|
||
| /** |
There was a problem hiding this comment.
mention it will be used for session API
| } | ||
|
|
||
| } | ||
|
|
There was a problem hiding this comment.
let's not do default. it doesn't really add value and just makes the JS files bigger
| * inferred meta-data to update the video's Media Session notification. | ||
| * | ||
| * @return {boolean} | ||
| */ |
There was a problem hiding this comment.
preimplementsMediaSession sounded better
src/service/video-manager-impl.js
Outdated
| album: this.metaData_.album, | ||
| artwork: [ | ||
| { | ||
| src: this.metaData_.posterUrl, |
src/service/video-manager-impl.js
Outdated
| ], | ||
| }); | ||
|
|
||
| navigator.mediaSession.setActionHandler('play', function() { |
There was a problem hiding this comment.
play(/*isAutoplay*/ false)
src/service/video-manager-impl.js
Outdated
| if (!metaData) { | ||
| metaData = {}; | ||
| } | ||
| if (!metaData.artist) { |
There was a problem hiding this comment.
we should not mutate their object. Use map() from object.js to create a map using their object as initial data and then mutate (and cache) the map
| } | ||
| if (!metaData.artist) { | ||
| metaData.artist = 'No artist'; | ||
| } |
There was a problem hiding this comment.
poster is very amp-video specific. We should just expect poster to be in meta data, manager can't do anything reasonable without it (except for a default which I argue we should not have)
There was a problem hiding this comment.
(oh unless do fallback to AMP specific thumbnail in the schema.org metadata and then to the favicon)
src/service/video-manager-impl.js
Outdated
| || this.internalElement_.getAttribute('aria-label') | ||
| || this.ampdoc_.win.document.title; | ||
| } | ||
| if (!metaData.album) { |
There was a problem hiding this comment.
what happens if we don't set it? we don't have good localization story for bultin text like "no album" yet.
5c90a36 to
dbadc6b
Compare
a494d11 to
22cfa6c
Compare
|
@aghassemi should be ready for a final review :) . If we want to implement next/previous from |
| } | ||
|
|
||
| /** @override */ | ||
| getMetaData() { |
There was a problem hiding this comment.
let's allow these to be just
getMetadata() {
/// Not implemented
}
and handle undefined/null in the video manager
src/mediasession-helper.js
Outdated
| * Updates the Media Session API's metadata | ||
| * @param {!./ampdoc-impl.AmpDoc} ampdoc | ||
| * @param {!./video-interface.VideoMetaDef} metaData | ||
| * @param {function} playHandler |
src/mediasession-helper.js
Outdated
| // Clear mediaSession (required to fix a bug when switching between two | ||
| // videos) | ||
| navigator.mediaSession.metadata = new win.MediaMetadata({ | ||
| title: '', |
There was a problem hiding this comment.
nit: as a constant variable defined outside and typed as video-interface.VideoMetaDef
src/mediasession-helper.js
Outdated
| const schema = doc.querySelector('script[type="application/ld+json"]'); | ||
| if (!schema) { | ||
| // No schema element found | ||
| return undefined; |
src/mediasession-helper.js
Outdated
|
|
||
| // Image definition in schema could be one of : | ||
| if (schemaJson.image['@list'] | ||
| && schemaJson.image['@list'][0] |
There was a problem hiding this comment.
undefined/null check is already covered by the typeof check
src/mediasession-helper.js
Outdated
| ] | ||
| }); | ||
| // Add metaData | ||
| navigator.mediaSession.metadata = new win.MediaMetadata(metaData); |
There was a problem hiding this comment.
nit: do a sweep of variable naming for this PR so we always do (M/m)etadata instead of (M/m)etaData to match native naming.
|
|
||
| /** @override */ | ||
| getMetaData() { | ||
| return { |
There was a problem hiding this comment.
amp-video should do the lookups for poster instead of manager.
src/service/video-manager-impl.js
Outdated
| } | ||
|
|
||
| if (!this.metaData_.artwork || this.metaData_.artwork.length == 0) { | ||
| const posterUrl = this.video.element.getAttribute('poster') |
There was a problem hiding this comment.
ditto comment about poster being the responsibility of amp-video
|
|
||
| if (!this.metaData_.title) { | ||
| const title = this.video.element.getAttribute('title') | ||
| || this.video.element.getAttribute('aria-label') |
There was a problem hiding this comment.
probably should not look into internalElement (there shouldn't be any cases where inner element has has but not the custom element wrapper of it)
| import {ActionTrust} from './action-trust'; /* eslint no-unused-vars: 0 */ | ||
|
|
||
| /** | ||
| * @typedef {{ |
5e95027 to
8221fd2
Compare
|
@erwinmombay for the |
src/mediasession-helper.js
Outdated
| // 1. "image": "http://..", | ||
| return schemaJson['image']; | ||
| } else if (schemaJson['image']['@list'] | ||
| && schemaJson['image']['@list'][0] |
There was a problem hiding this comment.
ditto: unnecessary check here and 2 more places below
There was a problem hiding this comment.
Didn't pass when I removed them :(
|
|
||
| /** | ||
| * Returns video's meta data (artwork, title, artist, album, etc.) for use | ||
| * with the Media Session API |
…ter default posters
This implements the Media Session API inside the video manager as described in #7272
Changes
video-interfaceshould expose :metaDatawhich containsposterUrl(512x512 png image),title,album,artist.metaData(see to-do)metaDatawhen the video loads as well as when the video starts playing (so that we override the media session when switching between videos on the same page)preimplementsMediaSessionfor players already implementing the Media Session API (amp-youtubefor example) so that we don't override their implementationpreimplementsMediaSessiononamp-youtubeto opt-outDeciding whether these other sources might be useful:
Excluded players list
List of video players that have a good implementation of the Media Session API already built-in.
amp-vimeoamp-youtubeTo-do
amp-youtubeCloses #7272 , Checks off an item on #4154