-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Media Session API for video components #10476
Conversation
6cb9cfc
to
e47397c
Compare
src/video-interface.js
Outdated
* Returns video's meta data (poster, artist, album, etc.) | ||
* @return {!VideoMetaDef} metadata | ||
*/ | ||
get metaData() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getMetaData()
for consistency
src/video-interface.js
Outdated
@@ -362,3 +379,13 @@ export const VideoAnalyticsType = { | |||
* }} | |||
*/ | |||
export let VideoAnalyticsDetailsDef; | |||
|
|||
/** | |||
* @typedef {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
define on top of the file
src/video-interface.js
Outdated
@@ -87,6 +87,23 @@ export class VideoInterface { | |||
hideControls() {} | |||
|
|||
/** | |||
* Returns video's meta data (poster, artist, album, etc.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mention it will be used for session API
src/service/video-manager-impl.js
Outdated
|
||
} | ||
|
||
getDefaultPoster_() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's not do default. it doesn't really add value and just makes the JS files bigger
src/video-interface.js
Outdated
* | ||
* @return {boolean} | ||
*/ | ||
optOutOfAutomaticMediaSessionAPI() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preimplementsMediaSession
sounded better
src/service/video-manager-impl.js
Outdated
artwork: [ | ||
{ | ||
src: this.metaData_.posterUrl, | ||
sizes: '512x512', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do they scale properly?
src/service/video-manager-impl.js
Outdated
}); | ||
|
||
navigator.mediaSession.setActionHandler('play', function() { | ||
this.video.play(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
play(/*isAutoplay*/ false)
src/service/video-manager-impl.js
Outdated
metaData = {}; | ||
} | ||
if (!metaData.artist) { | ||
metaData.artist = 'No artist'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
src/service/video-manager-impl.js
Outdated
if (!metaData.artist) { | ||
metaData.artist = 'No artist'; | ||
} | ||
if (!metaData.posterUrl) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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.ampdoc_.win.document.title; | ||
} | ||
if (!metaData.album) { | ||
metaData.album = 'No album'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
@@ -240,6 +240,21 @@ class Amp3QPlayer extends AMP.BaseElement { | |||
} | |||
|
|||
/** @override */ | |||
getMetaData() { | |||
return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function()=
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: just return;
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -305,6 +305,21 @@ class AmpVideo extends AMP.BaseElement { | |||
} | |||
|
|||
/** @override */ | |||
getMetaData() { | |||
return { | |||
'artwork': [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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') | ||
|| this.internalElement_.getAttribute('poster') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto comment about poster
being the responsibility of amp-video
src/service/video-manager-impl.js
Outdated
if (!this.metaData_.title) { | ||
const title = this.video.element.getAttribute('title') | ||
|| this.video.element.getAttribute('aria-label') | ||
|| this.internalElement_.getAttribute('title') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
src/video-interface.js
Outdated
@@ -17,6 +17,16 @@ | |||
import {ActionTrust} from './action-trust'; /* eslint no-unused-vars: 0 */ | |||
|
|||
/** | |||
* @typedef {{ | |||
* artwork: string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
artwork is array right?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto: unnecessary check here and 2 more places below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't pass when I removed them :(
src/video-interface.js
Outdated
@@ -105,6 +115,28 @@ export class VideoInterface { | |||
hideControls() {} | |||
|
|||
/** | |||
* Returns video's meta data (artwork, title, artist, album, etc.) for use | |||
* with the Media Session API | |||
* artwork (string): URL to the poster image (preferably a 512x512 PNG) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array for the artwork
…ter default posters
This implements the Media Session API inside the video manager as described in #7272
Changes
video-interface
should expose :metaData
which containsposterUrl
(512x512 png image),title
,album
,artist
.metaData
(see to-do)metaData
when 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)preimplementsMediaSession
for players already implementing the Media Session API (amp-youtube
for example) so that we don't override their implementationpreimplementsMediaSession
onamp-youtube
to 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-vimeo
amp-youtube
To-do
amp-youtube
Closes #7272 , Checks off an item on #4154