Skip to content

Commit

Permalink
Default media session actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Nov 20, 2019
1 parent 68baabb commit 9da1dd3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
9 changes: 9 additions & 0 deletions components/script/dom/htmlmediaelement.rs
Expand Up @@ -1913,6 +1913,15 @@ impl HTMLMediaElement {
self.media_element_load_algorithm();
}
}

fn send_media_session_event(&self, event: MediaSessionEvent) {
let global = self.global();
let media_session = global.as_window().Navigator().MediaSession();

media_session.register_media_instance(&self);

media_session.send_event(event);
}
}

// XXX Placeholder for [https://github.com/servo/servo/issues/22293]
Expand Down
29 changes: 27 additions & 2 deletions components/script/dom/mediasession.rs
Expand Up @@ -2,8 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

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::MediaSessionBinding;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionAction;
use crate::dom::bindings::codegen::Bindings::MediaSessionBinding::MediaSessionActionHandler;
Expand Down Expand Up @@ -61,15 +63,38 @@ impl MediaSession {
)
}

pub fn register_media_instance(&self, media_instance: &HTMLMediaElement) {
self.media_instance.set(Some(media_instance));
}

pub fn handle_action(&self, action: MediaSessionActionType) {
println!("HANDLE ACTION {:?}", action);
debug!("Handle media session action {:?} {:?}", action);
if let Some(handler) = self.action_handlers.borrow().get(&action) {
if handler.Call__(ExceptionHandling::Report).is_err() {
warn!("Error calling MediaSessionActionHandler callback");
}
return;
}
// TODO default action.

// Default action.
if let Some(media) = self.media_instance.get() {
match action {
MediaSessionActionType::Play => {
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
media.Play(InCompartment::Already(&in_compartment_proof));
},
MediaSessionActionType::Pause => {
media.Pause();
},
MediaSessionActionType::SeekBackward => {},
MediaSessionActionType::SeekForward => {},
MediaSessionActionType::PreviousTrack => {},
MediaSessionActionType::NextTrack => {},
MediaSessionActionType::SkipAd => {},
MediaSessionActionType::Stop => {},
MediaSessionActionType::SeekTo => {},
}
}
}

pub fn send_event(&self, event: MediaSessionEvent) {
Expand Down
2 changes: 1 addition & 1 deletion components/script_traits/script_msg.rs
Expand Up @@ -256,7 +256,7 @@ pub enum ScriptMsg {
GetScreenAvailSize(IpcSender<DeviceIntSize>),
/// Notifies the constellation about media session events
/// (i.e. when there is metadata for the active media session, playback state changes...).
MediaSessionEvent(BrowsingContextId, MediaSessionEvent)
MediaSessionEvent(BrowsingContextId, MediaSessionEvent),
}

impl fmt::Debug for ScriptMsg {
Expand Down

0 comments on commit 9da1dd3

Please sign in to comment.