Move active media session logic to constellation

This commit is contained in:
Fernando Jiménez Moreno 2019-10-28 16:08:05 +01:00
parent 08f9f17ed3
commit 85ec66b43e
7 changed files with 46 additions and 51 deletions

View file

@ -112,6 +112,7 @@ use compositing::SendableFrameTree;
use crossbeam_channel::{after, never, unbounded, Receiver, Sender}; use crossbeam_channel::{after, never, unbounded, Receiver, Sender};
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg}; use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg};
use embedder_traits::{Cursor, EmbedderMsg, EmbedderProxy, EventLoopWaker}; use embedder_traits::{Cursor, EmbedderMsg, EmbedderProxy, EventLoopWaker};
use embedder_traits::{MediaSessionEvent, MediaSessionPlaybackState};
use euclid::{default::Size2D as UntypedSize2D, Size2D}; use euclid::{default::Size2D as UntypedSize2D, Size2D};
use gfx::font_cache_thread::FontCacheThread; use gfx::font_cache_thread::FontCacheThread;
use gfx_traits::Epoch; use gfx_traits::Epoch;
@ -475,8 +476,8 @@ pub struct Constellation<Message, LTF, STF> {
/// Mechanism to force the compositor to process events. /// Mechanism to force the compositor to process events.
event_loop_waker: Option<Box<dyn EventLoopWaker>>, event_loop_waker: Option<Box<dyn EventLoopWaker>>,
/// Browser ID of the active media session, if any. /// Browing context ID of the active media session.
active_media_session: Option<TopLevelBrowsingContextId>, active_media_session: Option<BrowsingContextId>,
} }
/// State needed to construct a constellation. /// State needed to construct a constellation.
@ -1778,6 +1779,30 @@ where
new_value, new_value,
); );
}, },
FromScriptMsg::MediaSessionEvent(browsing_context_id, event) => {
// Unlikely at this point, but we may receive events coming from
// different media sessions, so we set the active media session based
// on Playing events.
// The last media session claiming to be in playing state is set to
// the active media session.
// Events coming from inactive media sessions are discarded.
if self.active_media_session.is_some() {
match event {
MediaSessionEvent::PlaybackStateChange(ref state) => {
match state {
MediaSessionPlaybackState::Playing => (),
_ => return,
};
},
_ => (),
};
}
self.active_media_session = Some(browsing_context_id);
self.embedder_proxy.send((
Some(source_top_ctx_id),
EmbedderMsg::MediaSessionEvent(event),
));
},
} }
} }
@ -5042,8 +5067,7 @@ where
); );
}, },
}; };
let msg = let msg = ConstellationControlMsg::MediaSessionAction(browsing_context_id, action);
ConstellationControlMsg::MediaSessionAction(top_level_browsing_context_id, action);
let result = match self.pipelines.get(&pipeline_id) { let result = match self.pipelines.get(&pipeline_id) {
None => { None => {
return warn!( return warn!(

View file

@ -16,9 +16,10 @@ use crate::dom::mediametadata::MediaMetadata;
use crate::dom::window::Window; use crate::dom::window::Window;
use crate::script_thread::ScriptThread; use crate::script_thread::ScriptThread;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use embedder_traits::{EmbedderMsg, MediaSessionEvent}; use embedder_traits::MediaSessionEvent;
use msg::constellation_msg::TopLevelBrowsingContextId; use msg::constellation_msg::BrowsingContextId;
use script_traits::MediaSessionActionType; use script_traits::MediaSessionActionType;
use script_traits::ScriptMsg;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@ -39,7 +40,7 @@ pub struct MediaSession {
impl MediaSession { impl MediaSession {
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
fn new_inherited(browsing_context_id: TopLevelBrowsingContextId) -> MediaSession { fn new_inherited(browsing_context_id: BrowsingContextId) -> MediaSession {
let media_session = MediaSession { let media_session = MediaSession {
reflector_: Reflector::new(), reflector_: Reflector::new(),
metadata: Default::default(), metadata: Default::default(),
@ -52,7 +53,7 @@ impl MediaSession {
} }
pub fn new(window: &Window) -> DomRoot<MediaSession> { pub fn new(window: &Window) -> DomRoot<MediaSession> {
let browsing_context_id = window.window_proxy().top_level_browsing_context_id(); let browsing_context_id = window.window_proxy().browsing_context_id();
reflect_dom_object( reflect_dom_object(
Box::new(MediaSession::new_inherited(browsing_context_id)), Box::new(MediaSession::new_inherited(browsing_context_id)),
window, window,
@ -74,7 +75,8 @@ impl MediaSession {
pub fn send_event(&self, event: MediaSessionEvent) { pub fn send_event(&self, event: MediaSessionEvent) {
let global = self.global(); let global = self.global();
let window = global.as_window(); let window = global.as_window();
window.send_to_embedder(EmbedderMsg::MediaSessionEvent(event)); let browsing_context_id = window.window_proxy().browsing_context_id();
window.send_to_constellation(ScriptMsg::MediaSessionEvent(browsing_context_id, event));
} }
} }
@ -121,10 +123,7 @@ impl MediaSessionMethods for MediaSession {
impl Drop for MediaSession { impl Drop for MediaSession {
fn drop(&mut self) { fn drop(&mut self) {
let global = self.global(); let global = self.global();
let browsing_context_id = global let browsing_context_id = global.as_window().window_proxy().browsing_context_id();
.as_window()
.window_proxy()
.top_level_browsing_context_id();
ScriptThread::remove_media_session(browsing_context_id); ScriptThread::remove_media_session(browsing_context_id);
} }
} }

View file

@ -194,7 +194,7 @@ impl NavigatorMethods for Navigator {
/// https://w3c.github.io/mediasession/#dom-navigator-mediasession /// https://w3c.github.io/mediasession/#dom-navigator-mediasession
fn MediaSession(&self) -> DomRoot<MediaSession> { fn MediaSession(&self) -> DomRoot<MediaSession> {
self.mediasession.or_init(|| { self.mediasession.or_init(|| {
// There is a single MediaSession instance per top level browsing context // There is a single MediaSession instance per browsing context
// and only one active MediaSession globally. // and only one active MediaSession globally.
// //
// MediaSession creation can happen in two cases: // MediaSession creation can happen in two cases:
@ -206,7 +206,7 @@ impl NavigatorMethods for Navigator {
// the script thread. // the script thread.
let global = self.global(); let global = self.global();
let window = global.as_window(); let window = global.as_window();
let browsing_context_id = window.window_proxy().top_level_browsing_context_id(); let browsing_context_id = window.window_proxy().browsing_context_id();
match ScriptThread::get_media_session(browsing_context_id) { match ScriptThread::get_media_session(browsing_context_id) {
Some(session) => session, Some(session) => session,
None => MediaSession::new(window), None => MediaSession::new(window),

View file

@ -701,7 +701,7 @@ pub struct ScriptThread {
/// The MediaSessions known by this thread, if any. /// The MediaSessions known by this thread, if any.
/// There can only be one active MediaSession. /// There can only be one active MediaSession.
/// The constellation has the BrowsingContextId of the active MediaSession, if any. /// The constellation has the BrowsingContextId of the active MediaSession, if any.
media_sessions: DomRefCell<HashMap<TopLevelBrowsingContextId, Dom<MediaSession>>>, media_sessions: DomRefCell<HashMap<BrowsingContextId, Dom<MediaSession>>>,
} }
/// In the event of thread panic, all data on the stack runs its destructor. However, there /// In the event of thread panic, all data on the stack runs its destructor. However, there
@ -3938,7 +3938,7 @@ impl ScriptThread {
fn handle_media_session_action( fn handle_media_session_action(
&self, &self,
browsing_context_id: TopLevelBrowsingContextId, browsing_context_id: BrowsingContextId,
action: MediaSessionActionType, action: MediaSessionActionType,
) { ) {
match self.media_sessions.borrow().get(&browsing_context_id) { match self.media_sessions.borrow().get(&browsing_context_id) {
@ -3973,7 +3973,7 @@ impl ScriptThread {
pub fn register_media_session( pub fn register_media_session(
media_session: &MediaSession, media_session: &MediaSession,
browsing_context_id: TopLevelBrowsingContextId, browsing_context_id: BrowsingContextId,
) { ) {
SCRIPT_THREAD_ROOT.with(|root| { SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() }; let script_thread = unsafe { &*root.get().unwrap() };
@ -3984,7 +3984,7 @@ impl ScriptThread {
}) })
} }
pub fn remove_media_session(browsing_context_id: TopLevelBrowsingContextId) { pub fn remove_media_session(browsing_context_id: BrowsingContextId) {
SCRIPT_THREAD_ROOT.with(|root| { SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() }; let script_thread = unsafe { &*root.get().unwrap() };
script_thread script_thread
@ -3995,7 +3995,7 @@ impl ScriptThread {
} }
pub fn get_media_session( pub fn get_media_session(
browsing_context_id: TopLevelBrowsingContextId, browsing_context_id: BrowsingContextId,
) -> Option<DomRoot<MediaSession>> { ) -> Option<DomRoot<MediaSession>> {
SCRIPT_THREAD_ROOT.with(|root| { SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() }; let script_thread = unsafe { &*root.get().unwrap() };

View file

@ -389,7 +389,7 @@ pub enum ConstellationControlMsg {
/// Notifies the script thread about a new recorded paint metric. /// Notifies the script thread about a new recorded paint metric.
PaintMetric(PipelineId, ProgressiveWebMetricType, u64), PaintMetric(PipelineId, ProgressiveWebMetricType, u64),
/// Notifies the media session about a user requested media session action. /// Notifies the media session about a user requested media session action.
MediaSessionAction(TopLevelBrowsingContextId, MediaSessionActionType), MediaSessionAction(BrowsingContextId, MediaSessionActionType),
} }
impl fmt::Debug for ConstellationControlMsg { impl fmt::Debug for ConstellationControlMsg {

View file

@ -17,7 +17,7 @@ use crate::WorkerGlobalScopeInit;
use crate::WorkerScriptLoadOrigin; use crate::WorkerScriptLoadOrigin;
use canvas_traits::canvas::{CanvasId, CanvasMsg}; use canvas_traits::canvas::{CanvasId, CanvasMsg};
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId}; use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use embedder_traits::EmbedderMsg; use embedder_traits::{EmbedderMsg, MediaSessionEvent};
use euclid::default::Size2D as UntypedSize2D; use euclid::default::Size2D as UntypedSize2D;
use euclid::Size2D; use euclid::Size2D;
use gfx_traits::Epoch; use gfx_traits::Epoch;
@ -306,6 +306,7 @@ impl fmt::Debug for ScriptMsg {
GetClientWindow(..) => "GetClientWindow", GetClientWindow(..) => "GetClientWindow",
GetScreenSize(..) => "GetScreenSize", GetScreenSize(..) => "GetScreenSize",
GetScreenAvailSize(..) => "GetScreenAvailSize", GetScreenAvailSize(..) => "GetScreenAvailSize",
MediaSessionEvent(..) => "MediaSessionEvent",
}; };
write!(formatter, "ScriptMsg::{}", variant) write!(formatter, "ScriptMsg::{}", variant)
} }

View file

@ -83,7 +83,6 @@ use constellation::{Constellation, InitialConstellationState, UnprivilegedPipeli
use constellation::{FromCompositorLogger, FromScriptLogger}; use constellation::{FromCompositorLogger, FromScriptLogger};
use crossbeam_channel::{unbounded, Sender}; use crossbeam_channel::{unbounded, Sender};
use embedder_traits::{EmbedderMsg, EmbedderProxy, EmbedderReceiver, EventLoopWaker}; use embedder_traits::{EmbedderMsg, EmbedderProxy, EmbedderReceiver, EventLoopWaker};
use embedder_traits::{MediaSessionEvent, MediaSessionPlaybackState};
use env_logger::Builder as EnvLoggerBuilder; use env_logger::Builder as EnvLoggerBuilder;
use euclid::{Scale, Size2D}; use euclid::{Scale, Size2D};
#[cfg(all( #[cfg(all(
@ -272,8 +271,6 @@ pub struct Servo<Window: WindowMethods + 'static + ?Sized> {
embedder_events: Vec<(Option<BrowserId>, EmbedderMsg)>, embedder_events: Vec<(Option<BrowserId>, EmbedderMsg)>,
profiler_enabled: bool, profiler_enabled: bool,
webgl_thread_data: Option<Rc<WebGLMainThread>>, webgl_thread_data: Option<Rc<WebGLMainThread>>,
/// Browser ID of the active media session, if any.
active_media_session: Option<BrowserId>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -560,7 +557,6 @@ where
embedder_events: Vec::new(), embedder_events: Vec::new(),
profiler_enabled: false, profiler_enabled: false,
webgl_thread_data, webgl_thread_data,
active_media_session: None,
} }
} }
@ -750,31 +746,6 @@ where
self.embedder_events.push(event); self.embedder_events.push(event);
}, },
(EmbedderMsg::MediaSessionEvent(event), ShutdownState::NotShuttingDown) => {
// Unlikely at this point, but we may receive events coming from
// different media sessions, so we set the active media session based
// on Playing events.
// The last media session claiming to be in playing state is set to
// the active media session.
// Events coming from inactive media sessions are discarded.
if self.active_media_session.is_some() {
match event {
MediaSessionEvent::PlaybackStateChange(ref state) => {
match state {
MediaSessionPlaybackState::Playing => (),
_ => return,
};
},
_ => (),
};
}
self.active_media_session = top_level_browsing_context;
self.embedder_events.push((
top_level_browsing_context,
EmbedderMsg::MediaSessionEvent(event),
));
},
(msg, ShutdownState::NotShuttingDown) => { (msg, ShutdownState::NotShuttingDown) => {
self.embedder_events.push((top_level_browsing_context, msg)); self.embedder_events.push((top_level_browsing_context, msg));
}, },