Skip to content
Permalink
Browse files

Update media session metadata according to media player

  • Loading branch information
ferjm committed Nov 14, 2019
1 parent caedc28 commit 6ee21af0c428ceff04f885f040ee98827207349d
Showing with 33 additions and 5 deletions.
  1. +2 −1 components/script/dom/bindings/trace.rs
  2. +31 −4 components/script/dom/mediasession.rs
@@ -57,7 +57,7 @@ use content_security_policy::CspList;
use crossbeam_channel::{Receiver, Sender};
use cssparser::RGBA;
use devtools_traits::{CSSError, TimelineMarkerType, WorkerId};
use embedder_traits::EventLoopWaker;
use embedder_traits::{EventLoopWaker, MediaMetadata};
use encoding_rs::{Decoder, Encoding};
use euclid::default::{Point2D, Rect, Rotation3D, Transform2D, Transform3D};
use euclid::Length as EuclidLength;
@@ -537,6 +537,7 @@ unsafe_no_jsmanaged_fields!(VideoFrame);
unsafe_no_jsmanaged_fields!(WebGLContextId);
unsafe_no_jsmanaged_fields!(Arc<Mutex<dyn AudioRenderer>>);
unsafe_no_jsmanaged_fields!(MediaSessionActionType);
unsafe_no_jsmanaged_fields!(MediaMetadata);

unsafe impl<'a> JSTraceable for &'a str {
#[inline]
@@ -6,17 +6,21 @@ use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::callback::ExceptionHandling;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::HTMLMediaElementBinding::HTMLMediaElementMethods;
use crate::dom::bindings::codegen::Bindings::MediaMetadataBinding::MediaMetadataInit;
use crate::dom::bindings::codegen::Bindings::MediaMetadataBinding::MediaMetadataMethods;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionAction;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionActionHandler;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionMethods;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionPlaybackState;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::htmlmediaelement::HTMLMediaElement;
use crate::dom::mediametadata::MediaMetadata;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use embedder_traits::MediaMetadata as EmbedderMediaMetadata;
use embedder_traits::MediaSessionEvent;
use script_traits::MediaSessionActionType;
use script_traits::ScriptMsg;
@@ -27,7 +31,8 @@ use std::rc::Rc;
pub struct MediaSession {
reflector_: Reflector,
/// https://w3c.github.io/mediasession/#dom-mediasession-metadata
metadata: MutNullableDom<MediaMetadata>,
#[ignore_malloc_size_of = "defined in embedder_traits"]
metadata: DomRefCell<Option<EmbedderMediaMetadata>>,
/// https://w3c.github.io/mediasession/#dom-mediasession-playbackstate
playback_state: DomRefCell<MediaSessionPlaybackState>,
/// https://w3c.github.io/mediasession/#supported-media-session-actions
@@ -43,7 +48,7 @@ impl MediaSession {
fn new_inherited() -> MediaSession {
let media_session = MediaSession {
reflector_: Reflector::new(),
metadata: Default::default(),
metadata: DomRefCell::new(None),
playback_state: DomRefCell::new(MediaSessionPlaybackState::None),
action_handlers: DomRefCell::new(HashMap::new()),
media_instance: Default::default(),
@@ -95,6 +100,13 @@ impl MediaSession {
}

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 window = global.as_window();
let pipeline_id = window
@@ -107,15 +119,30 @@ impl MediaSession {
impl MediaSessionMethods for MediaSession {
/// https://w3c.github.io/mediasession/#dom-mediasession-metadata
fn GetMetadata(&self) -> Option<DomRoot<MediaMetadata>> {
self.metadata.get()
if let Some(ref metadata) = *self.metadata.borrow() {
let mut init = MediaMetadataInit::empty();
init.title = DOMString::from_string(metadata.title.clone());
init.artist = DOMString::from_string(metadata.artist.clone().unwrap_or("".to_owned()));
init.album = DOMString::from_string(metadata.album.clone().unwrap_or("".to_owned()));
let global = self.global();
Some(MediaMetadata::new(&global.as_window(), &init))
} else {
None
}
}

/// https://w3c.github.io/mediasession/#dom-mediasession-metadata
fn SetMetadata(&self, metadata: Option<&MediaMetadata>) {
if let Some(ref metadata) = metadata {
metadata.set_session(self);
}
self.metadata.set(metadata);

let _metadata = metadata.map(|m| EmbedderMediaMetadata {
title: m.Title().into(),
artist: Some(m.Artist().into()),
album: Some(m.Album().into()),
});
*self.metadata.borrow_mut() = _metadata;
}

/// https://w3c.github.io/mediasession/#dom-mediasession-playbackstate

0 comments on commit 6ee21af

Please sign in to comment.
You can’t perform that action at this time.