From 7ca74d598cedd5a826e3b6c9e37cee0f219f77ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Wed, 9 Oct 2019 18:06:27 +0200 Subject: [PATCH] MediaSession: refactor embedder API. Fix Android build --- components/embedder_traits/lib.rs | 3 ++- ports/glutin/browser.rs | 6 ++--- ports/libsimpleservo/api/src/lib.rs | 32 ++++++++++++++++++++------ ports/libsimpleservo/capi/src/lib.rs | 27 ++++++++++++++++++---- ports/libsimpleservo/jniapi/src/lib.rs | 14 ++++++++++- 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/components/embedder_traits/lib.rs b/components/embedder_traits/lib.rs index 7d967e46277..94ae2493b68 100644 --- a/components/embedder_traits/lib.rs +++ b/components/embedder_traits/lib.rs @@ -219,10 +219,11 @@ pub struct MediaMetadata { } /// https://w3c.github.io/mediasession/#enumdef-mediasessionplaybackstate +#[repr(i32)] #[derive(Clone, Debug, Deserialize, Serialize)] pub enum MediaSessionPlaybackState { /// The browsing context does not specify whether it’s playing or paused. - None_, + None_ = 1, /// The browsing context has paused media and it can be resumed. Playing, /// The browsing context is currently playing media and it can be paused. diff --git a/ports/glutin/browser.rs b/ports/glutin/browser.rs index 95bea1e72bb..976026a483b 100644 --- a/ports/glutin/browser.rs +++ b/ports/glutin/browser.rs @@ -449,9 +449,9 @@ where error!("Failed to store profile: {}", e); } }, - EmbedderMsg::MediaSession(_) => { - debug!("MediaSession received"); - // TODO(ferjm) + EmbedderMsg::MediaSessionEvent(_) => { + debug!("MediaSessionEvent received"); + // TODO(ferjm): MediaSession support for Glutin based browsers. }, } } diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index b7f0d3587b4..894928bc2b8 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -15,7 +15,7 @@ use servo::compositing::windowing::{ WindowMethods, }; use servo::embedder_traits::resources::{self, Resource, ResourceReaderMethods}; -use servo::embedder_traits::EmbedderMsg; +use servo::embedder_traits::{EmbedderMsg, MediaSessionEvent}; use servo::euclid::{Point2D, Rect, Scale, Size2D, Vector2D}; use servo::keyboard_types::{Key, KeyState, KeyboardEvent}; use servo::msg::constellation_msg::TraversalDirection; @@ -42,7 +42,6 @@ thread_local! { /// It will be called to notify embedder that some events are available, /// and that perform_updates need to be called pub use servo::embedder_traits::EventLoopWaker; -pub use servo::embedder_traits::MediaSessionEvent; pub struct InitOptions { pub args: Vec, @@ -127,12 +126,19 @@ pub trait HostTrait { fn on_shutdown_complete(&self); /// A text input is focused. fn on_ime_state_changed(&self, show: bool); - /// Gets sytem clipboard contents + /// Gets sytem clipboard contents. fn get_clipboard_contents(&self) -> Option; - /// Sets system clipboard contents + /// Sets system clipboard contents. fn set_clipboard_contents(&self, contents: String); - /// Called when there is a new media session event. - fn on_media_session_event(&self, event: MediaSessionEvent); + /// Called when we get the media session metadata/ + fn on_media_session_metadata( + &self, + title: String, + artist: Option, + album: Option, + ); + /// Called when the media sessoin playback state changes. + fn on_media_session_playback_state_change(&self, state: i32); } pub struct ServoGlue { @@ -585,7 +591,19 @@ impl ServoGlue { self.callbacks.host_callbacks.on_ime_state_changed(false); }, EmbedderMsg::MediaSessionEvent(event) => { - self.callbacks.host_callbacks.on_media_session_event(event); + match event { + MediaSessionEvent::SetMetadata(metadata) => { + self.callbacks.host_callbacks.on_media_session_metadata( + metadata.title, + metadata.artist, + metadata.album, + ) + }, + MediaSessionEvent::PlaybackStateChange(state) => self + .callbacks + .host_callbacks + .on_media_session_playback_state_change(state as i32), + }; }, EmbedderMsg::Status(..) | EmbedderMsg::SelectFiles(..) | diff --git a/ports/libsimpleservo/capi/src/lib.rs b/ports/libsimpleservo/capi/src/lib.rs index 57043407879..31a066d9416 100644 --- a/ports/libsimpleservo/capi/src/lib.rs +++ b/ports/libsimpleservo/capi/src/lib.rs @@ -216,8 +216,9 @@ pub struct CHostCallbacks { pub on_ime_state_changed: extern "C" fn(show: bool), pub get_clipboard_contents: extern "C" fn() -> *const c_char, pub set_clipboard_contents: extern "C" fn(contents: *const c_char), - // TODO(ferjm) pass C representation of media event. - pub on_media_session_event: extern "C" fn(), + pub on_media_session_metadata: + extern "C" fn(title: *const c_char, album: *const c_char, artist: *const c_char), + pub on_media_session_playback_state_change: extern "C" fn(state: i32), } /// Servo options @@ -711,8 +712,24 @@ impl HostTrait for HostCallbacks { (self.0.set_clipboard_contents)(contents.as_ptr()); } - fn on_media_session_event(&self, event: MediaSessionEvent) { - debug!("on_media_session_event (event: {:?})", event); - (self.0.on_media_session_event)(); + fn on_media_session_metadata( + &self, + title: String, + artist: Option, + album: Option, + ) { + debug!( + "on_media_session_metadata ({:?} {:?} {:?})", + title, artist, album + ); + let title = CString::new(title).expect("Can't create string"); + let artist = CString::new(artist.unwrap_or(String::new())).expect("Can't create string"); + let album = CString::new(album.unwrap_or(String::new())).expect("Can't create string"); + (self.0.on_media_session_metadata)(title.as_ptr(), artist.as_ptr(), album.as_ptr()); + } + + fn on_media_session_playback_state_change(&self, state: i32) { + debug!("on_media_session_playback_state_change {:?}", state); + (self.0.on_media_session_playback_state_change)(state); } } diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index 963e01dec6d..fe6538f9c2e 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -15,7 +15,9 @@ use jni::{errors, JNIEnv, JavaVM}; use libc::{dup2, pipe, read}; use log::Level; use simpleservo::{self, gl_glue, ServoGlue, SERVO}; -use simpleservo::{Coordinates, EventLoopWaker, HostTrait, InitOptions, VRInitOptions}; +use simpleservo::{ + Coordinates, EventLoopWaker, HostTrait, InitOptions, MediaSessionEvent, VRInitOptions, +}; use std::os::raw::{c_char, c_int, c_void}; use std::ptr::{null, null_mut}; use std::sync::Arc; @@ -508,6 +510,16 @@ impl HostTrait for HostCallbacks { } fn set_clipboard_contents(&self, _contents: String) {} + + fn on_media_session_metadata( + &self, + _title: String, + _artist: Option, + _album: Option, + ) { + } + + fn on_media_session_playback_state_change(&self, state: i32) {} } fn initialize_android_glue(env: &JNIEnv, activity: JObject) {