From 51ed44c77d999daee20550d6177aa4d8f96734b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Tue, 17 Sep 2019 23:04:38 +0200 Subject: [PATCH 1/2] Register pipeline instead of browsing contexts for media instances --- components/script/dom/audiocontext.rs | 15 +++++++-------- components/script/dom/baseaudiocontext.rs | 10 ++++------ components/script/dom/htmlmediaelement.rs | 10 +++++----- components/script/dom/offlineaudiocontext.rs | 18 ++++++++---------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/components/script/dom/audiocontext.rs b/components/script/dom/audiocontext.rs index 9321bee1089..8f98586b3ea 100644 --- a/components/script/dom/audiocontext.rs +++ b/components/script/dom/audiocontext.rs @@ -23,7 +23,7 @@ use crate::dom::promise::Promise; use crate::dom::window::Window; use crate::task_source::TaskSource; use dom_struct::dom_struct; -use msg::constellation_msg::BrowsingContextId; +use msg::constellation_msg::PipelineId; use servo_media::audio::context::{LatencyCategory, ProcessingState, RealTimeAudioContextOptions}; use std::rc::Rc; @@ -40,14 +40,11 @@ pub struct AudioContext { impl AudioContext { #[allow(unrooted_must_root)] // https://webaudio.github.io/web-audio-api/#AudioContext-constructors - fn new_inherited( - options: &AudioContextOptions, - browsing_context_id: BrowsingContextId, - ) -> AudioContext { + fn new_inherited(options: &AudioContextOptions, pipeline_id: PipelineId) -> AudioContext { // Steps 1-3. let context = BaseAudioContext::new_inherited( BaseAudioContextOptions::AudioContext(options.into()), - browsing_context_id, + pipeline_id, ); // Step 4.1. @@ -70,8 +67,10 @@ impl AudioContext { #[allow(unrooted_must_root)] pub fn new(window: &Window, options: &AudioContextOptions) -> DomRoot { - let browsing_context_id = window.window_proxy().top_level_browsing_context_id().0; - let context = AudioContext::new_inherited(options, browsing_context_id); + let pipeline_id = window + .pipeline_id() + .expect("Cannot create AudioContext outside of a pipeline"); + let context = AudioContext::new_inherited(options, pipeline_id); let context = reflect_dom_object(Box::new(context), window, AudioContextBinding::Wrap); context.resume(); context diff --git a/components/script/dom/baseaudiocontext.rs b/components/script/dom/baseaudiocontext.rs index 74dd1863a75..925f5a9d27c 100644 --- a/components/script/dom/baseaudiocontext.rs +++ b/components/script/dom/baseaudiocontext.rs @@ -51,7 +51,7 @@ use crate::task_source::TaskSource; use dom_struct::dom_struct; use js::rust::CustomAutoRooterGuard; use js::typedarray::ArrayBuffer; -use msg::constellation_msg::BrowsingContextId; +use msg::constellation_msg::PipelineId; use servo_media::audio::context::{AudioContext, AudioContextOptions, ProcessingState}; use servo_media::audio::context::{OfflineAudioContextOptions, RealTimeAudioContextOptions}; use servo_media::audio::decoder::AudioDecoderCallbacks; @@ -109,7 +109,7 @@ impl BaseAudioContext { #[allow(unrooted_must_root)] pub fn new_inherited( options: BaseAudioContextOptions, - browsing_context_id: BrowsingContextId, + pipeline_id: PipelineId, ) -> BaseAudioContext { let (sample_rate, channel_count) = match options { BaseAudioContextOptions::AudioContext(ref opt) => (opt.sample_rate, 2), @@ -118,10 +118,8 @@ impl BaseAudioContext { }, }; - let client_context_id = ClientContextId::build( - browsing_context_id.namespace_id.0, - browsing_context_id.index.0.get(), - ); + let client_context_id = + ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get()); let context = BaseAudioContext { eventtarget: EventTarget::new_inherited(), audio_context_impl: ServoMedia::get() diff --git a/components/script/dom/htmlmediaelement.rs b/components/script/dom/htmlmediaelement.rs index ac44f43f3fa..a607c915802 100644 --- a/components/script/dom/htmlmediaelement.rs +++ b/components/script/dom/htmlmediaelement.rs @@ -1329,11 +1329,11 @@ impl HTMLMediaElement { HTMLMediaElementTypeId::HTMLVideoElement => Some(self.frame_renderer.clone()), }; - let browsing_context_id = window.window_proxy().top_level_browsing_context_id().0; - let client_context_id = ClientContextId::build( - browsing_context_id.namespace_id.0, - browsing_context_id.index.0.get(), - ); + let pipeline_id = window + .pipeline_id() + .expect("Cannot create player outside of a pipeline"); + let client_context_id = + ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get()); let player = ServoMedia::get().unwrap().create_player( &client_context_id, stream_type, diff --git a/components/script/dom/offlineaudiocontext.rs b/components/script/dom/offlineaudiocontext.rs index 8e3776817ab..d499e79c159 100644 --- a/components/script/dom/offlineaudiocontext.rs +++ b/components/script/dom/offlineaudiocontext.rs @@ -23,7 +23,7 @@ use crate::dom::promise::Promise; use crate::dom::window::Window; use crate::task_source::TaskSource; use dom_struct::dom_struct; -use msg::constellation_msg::BrowsingContextId; +use msg::constellation_msg::PipelineId; use servo_media::audio::context::OfflineAudioContextOptions as ServoMediaOfflineAudioContextOptions; use std::cell::Cell; use std::rc::Rc; @@ -47,7 +47,7 @@ impl OfflineAudioContext { channel_count: u32, length: u32, sample_rate: f32, - browsing_context_id: BrowsingContextId, + pipeline_id: PipelineId, ) -> OfflineAudioContext { let options = ServoMediaOfflineAudioContextOptions { channels: channel_count as u8, @@ -56,7 +56,7 @@ impl OfflineAudioContext { }; let context = BaseAudioContext::new_inherited( BaseAudioContextOptions::OfflineAudioContext(options), - browsing_context_id, + pipeline_id, ); OfflineAudioContext { context, @@ -82,13 +82,11 @@ impl OfflineAudioContext { { return Err(Error::NotSupported); } - let browsing_context_id = window.window_proxy().top_level_browsing_context_id().0; - let context = OfflineAudioContext::new_inherited( - channel_count, - length, - sample_rate, - browsing_context_id, - ); + let pipeline_id = window + .pipeline_id() + .expect("Cannot create audio context outside of a pipeline"); + let context = + OfflineAudioContext::new_inherited(channel_count, length, sample_rate, pipeline_id); Ok(reflect_dom_object( Box::new(context), window, From d7bebce53787c535bbd3511855a8633a12986daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Wed, 18 Sep 2019 09:34:43 +0200 Subject: [PATCH 2/2] Suspend/resume media activity based on document activity --- Cargo.lock | 24 ++++++++++++------------ components/script/dom/document.rs | 7 +++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e79a9832278..0dcf274cca2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4221,7 +4221,7 @@ dependencies = [ [[package]] name = "servo-media" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "servo-media-audio 0.1.0 (git+https://github.com/servo/media)", "servo-media-player 0.1.0 (git+https://github.com/servo/media)", @@ -4233,7 +4233,7 @@ dependencies = [ [[package]] name = "servo-media-audio" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "boxfnonce 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "byte-slice-cast 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4250,7 +4250,7 @@ dependencies = [ [[package]] name = "servo-media-dummy" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "boxfnonce 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4265,7 +4265,7 @@ dependencies = [ [[package]] name = "servo-media-gstreamer" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "boxfnonce 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "byte-slice-cast 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4301,7 +4301,7 @@ dependencies = [ [[package]] name = "servo-media-gstreamer-render" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "gstreamer 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer-video 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4311,7 +4311,7 @@ dependencies = [ [[package]] name = "servo-media-gstreamer-render-android" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "glib 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4324,7 +4324,7 @@ dependencies = [ [[package]] name = "servo-media-gstreamer-render-unix" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "glib 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4337,7 +4337,7 @@ dependencies = [ [[package]] name = "servo-media-player" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "ipc-channel 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4349,7 +4349,7 @@ dependencies = [ [[package]] name = "servo-media-streams" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4358,12 +4358,12 @@ dependencies = [ [[package]] name = "servo-media-traits" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" [[package]] name = "servo-media-webrtc" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "boxfnonce 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4462,7 +4462,7 @@ dependencies = [ [[package]] name = "servo_media_derive" version = "0.1.0" -source = "git+https://github.com/servo/media#8c072eb922d0c8cb431f8c1cf80b304e03fe66bb" +source = "git+https://github.com/servo/media#7776958f353b5881fa1d6ec97a236940f7a30d16" dependencies = [ "proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index fcacb4cedad..91f5e19dc7c 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -145,6 +145,7 @@ use script_traits::{ use servo_arc::Arc; use servo_atoms::Atom; use servo_config::pref; +use servo_media::{ClientContextId, ServoMedia}; use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl}; use std::borrow::ToOwned; use std::cell::{Cell, Ref, RefMut}; @@ -499,12 +500,17 @@ impl Document { // Set the document's activity level, reflow if necessary, and suspend or resume timers. if activity != self.activity.get() { self.activity.set(activity); + let media = ServoMedia::get().unwrap(); + let pipeline_id = self.window().pipeline_id().expect("doc with no pipeline"); + let client_context_id = + ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get()); if activity == DocumentActivity::FullyActive { self.title_changed(); self.dirty_all_nodes(); self.window() .reflow(ReflowGoal::Full, ReflowReason::CachedPageNeededReflow); self.window().resume(); + media.resume(&client_context_id); // html.spec.whatwg.org/multipage/#history-traversal // Step 4.6 if self.ready_state.get() == DocumentReadyState::Complete { @@ -544,6 +550,7 @@ impl Document { } } else { self.window().suspend(); + media.suspend(&client_context_id); } } }