Fix metadata update

This commit is contained in:
Fernando Jiménez Moreno 2019-11-18 11:36:21 +01:00
parent f65c400828
commit 35c4c35416
5 changed files with 62 additions and 29 deletions

View file

@ -213,9 +213,19 @@ pub struct MediaMetadata {
/// Title /// Title
pub title: String, pub title: String,
/// Artist /// Artist
pub artist: Option<String>, pub artist: String,
/// Album /// Album
pub album: Option<String>, pub album: String,
}
impl MediaMetadata {
pub fn new(title: String) -> Self {
Self {
title,
artist: "".to_owned(),
album: "".to_owned(),
}
}
} }
/// https://w3c.github.io/mediasession/#enumdef-mediasessionplaybackstate /// https://w3c.github.io/mediasession/#enumdef-mediasessionplaybackstate

View file

@ -67,7 +67,7 @@ use crate::script_thread::ScriptThread;
use crate::task_source::TaskSource; use crate::task_source::TaskSource;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use embedder_traits::resources::{self, Resource as EmbedderResource}; use embedder_traits::resources::{self, Resource as EmbedderResource};
use embedder_traits::{MediaMetadata, MediaSessionEvent, MediaSessionPlaybackState}; use embedder_traits::{MediaSessionEvent, MediaSessionPlaybackState};
use euclid::default::Size2D; use euclid::default::Size2D;
use headers::{ContentLength, ContentRange, HeaderMapExt}; use headers::{ContentLength, ContentRange, HeaderMapExt};
use html5ever::{LocalName, Prefix}; use html5ever::{LocalName, Prefix};
@ -1731,15 +1731,13 @@ impl HTMLMediaElement {
let global = self.global(); let global = self.global();
let window = global.as_window(); let window = global.as_window();
// Send a media session event with the obtained metadata. // Update the media session metadata title with the obtained metadata.
self.send_media_session_event(MediaSessionEvent::SetMetadata(MediaMetadata { window.Navigator().MediaSession().update_title(
title: metadata metadata
.title .title
.clone() .clone()
.unwrap_or(window.get_url().into_string()), .unwrap_or(window.get_url().into_string()),
artist: None, );
album: None,
}));
}, },
PlayerEvent::NeedData => { PlayerEvent::NeedData => {
// The player needs more data. // The player needs more data.

View file

@ -68,7 +68,7 @@ impl MediaMetadataMethods for MediaMetadata {
} }
/// https://w3c.github.io/mediasession/#dom-mediametadata-title /// https://w3c.github.io/mediasession/#dom-mediametadata-title
fn SetTitle(&self, value: DOMString) -> () { fn SetTitle(&self, value: DOMString) {
*self.title.borrow_mut() = value; *self.title.borrow_mut() = value;
self.queue_update_metadata_algorithm(); self.queue_update_metadata_algorithm();
} }
@ -79,7 +79,7 @@ impl MediaMetadataMethods for MediaMetadata {
} }
/// https://w3c.github.io/mediasession/#dom-mediametadata-artist /// https://w3c.github.io/mediasession/#dom-mediametadata-artist
fn SetArtist(&self, value: DOMString) -> () { fn SetArtist(&self, value: DOMString) {
*self.artist.borrow_mut() = value; *self.artist.borrow_mut() = value;
self.queue_update_metadata_algorithm(); self.queue_update_metadata_algorithm();
} }
@ -90,7 +90,7 @@ impl MediaMetadataMethods for MediaMetadata {
} }
/// https://w3c.github.io/mediasession/#dom-mediametadata-album /// https://w3c.github.io/mediasession/#dom-mediametadata-album
fn SetAlbum(&self, value: DOMString) -> () { fn SetAlbum(&self, value: DOMString) {
*self.album.borrow_mut() = value; *self.album.borrow_mut() = value;
self.queue_update_metadata_algorithm(); self.queue_update_metadata_algorithm();
} }

View file

@ -100,13 +100,6 @@ impl MediaSession {
} }
pub fn send_event(&self, event: MediaSessionEvent) { pub fn send_event(&self, event: MediaSessionEvent) {
match event {
MediaSessionEvent::SetMetadata(ref metadata) => {
*self.metadata.borrow_mut() = Some(metadata.clone());
},
_ => (),
}
let global = self.global(); let global = self.global();
let window = global.as_window(); let window = global.as_window();
let pipeline_id = window let pipeline_id = window
@ -114,6 +107,23 @@ impl MediaSession {
.expect("Cannot send media session event outside of a pipeline"); .expect("Cannot send media session event outside of a pipeline");
window.send_to_constellation(ScriptMsg::MediaSessionEvent(pipeline_id, event)); window.send_to_constellation(ScriptMsg::MediaSessionEvent(pipeline_id, event));
} }
pub fn update_title(&self, title: String) {
let mut metadata = self.metadata.borrow_mut();
if let Some(ref mut metadata) = *metadata {
// We only update the title with the data provided by the media
// player and iff the user did not provide a title.
if !metadata.title.is_empty() {
return;
}
metadata.title = title;
} else {
*metadata = Some(EmbedderMediaMetadata::new(title));
}
self.send_event(MediaSessionEvent::SetMetadata(
metadata.as_ref().unwrap().clone(),
));
}
} }
impl MediaSessionMethods for MediaSession { impl MediaSessionMethods for MediaSession {
@ -122,8 +132,8 @@ impl MediaSessionMethods for MediaSession {
if let Some(ref metadata) = *self.metadata.borrow() { if let Some(ref metadata) = *self.metadata.borrow() {
let mut init = MediaMetadataInit::empty(); let mut init = MediaMetadataInit::empty();
init.title = DOMString::from_string(metadata.title.clone()); init.title = DOMString::from_string(metadata.title.clone());
init.artist = DOMString::from_string(metadata.artist.clone().unwrap_or("".to_owned())); init.artist = DOMString::from_string(metadata.artist.clone());
init.album = DOMString::from_string(metadata.album.clone().unwrap_or("".to_owned())); init.album = DOMString::from_string(metadata.album.clone());
let global = self.global(); let global = self.global();
Some(MediaMetadata::new(&global.as_window(), &init)) Some(MediaMetadata::new(&global.as_window(), &init))
} else { } else {
@ -137,12 +147,27 @@ impl MediaSessionMethods for MediaSession {
metadata.set_session(self); metadata.set_session(self);
} }
let _metadata = metadata.map(|m| EmbedderMediaMetadata { let global = self.global();
title: m.Title().into(), let window = global.as_window();
artist: Some(m.Artist().into()), let _metadata = match metadata {
album: Some(m.Album().into()), Some(m) => {
}); let title = if m.Title().is_empty() {
*self.metadata.borrow_mut() = _metadata; window.get_url().into_string()
} else {
m.Title().into()
};
EmbedderMediaMetadata {
title,
artist: m.Artist().into(),
album: m.Album().into(),
}
},
None => EmbedderMediaMetadata::new(window.get_url().into_string()),
};
*self.metadata.borrow_mut() = Some(_metadata.clone());
self.send_event(MediaSessionEvent::SetMetadata(_metadata));
} }
/// https://w3c.github.io/mediasession/#dom-mediasession-playbackstate /// https://w3c.github.io/mediasession/#dom-mediasession-playbackstate

View file

@ -586,8 +586,8 @@ impl ServoGlue {
MediaSessionEvent::SetMetadata(metadata) => { MediaSessionEvent::SetMetadata(metadata) => {
self.callbacks.host_callbacks.on_media_session_metadata( self.callbacks.host_callbacks.on_media_session_metadata(
metadata.title, metadata.title,
metadata.artist.unwrap_or("".to_owned()), metadata.artist,
metadata.album.unwrap_or("".to_owned()), metadata.album,
) )
}, },
MediaSessionEvent::PlaybackStateChange(state) => self MediaSessionEvent::PlaybackStateChange(state) => self