From acb03603b6005574c35f544fdc2f0b895d5586a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Thu, 19 Jul 2018 11:51:10 +0200 Subject: [PATCH] Fire AudioScheduledSourceNode.onended when playback stops --- Cargo.lock | 9 +++--- components/atoms/static_atoms.txt | 1 + components/script/dom/audiobuffer.rs | 2 +- .../script/dom/audioscheduledsourcenode.rs | 32 ++++++++++++++++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fec507ba9c7..e2f9b8194544 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3076,7 +3076,7 @@ dependencies = [ [[package]] name = "servo-media" version = "0.1.0" -source = "git+https://github.com/servo/media#6403c703ac7411f5d3b18f5edc5889cf1cbceb09" +source = "git+https://github.com/servo/media#d08d03f9b78827cc1516d8a3b8e94ffc9394c748" dependencies = [ "servo-media-audio 0.1.0 (git+https://github.com/servo/media)", "servo-media-gstreamer 0.1.0 (git+https://github.com/servo/media)", @@ -3085,7 +3085,7 @@ dependencies = [ [[package]] name = "servo-media-audio" version = "0.1.0" -source = "git+https://github.com/servo/media#6403c703ac7411f5d3b18f5edc5889cf1cbceb09" +source = "git+https://github.com/servo/media#d08d03f9b78827cc1516d8a3b8e94ffc9394c748" dependencies = [ "byte-slice-cast 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3097,13 +3097,12 @@ dependencies = [ [[package]] name = "servo-media-gstreamer" version = "0.1.0" -source = "git+https://github.com/servo/media#6403c703ac7411f5d3b18f5edc5889cf1cbceb09" +source = "git+https://github.com/servo/media#d08d03f9b78827cc1516d8a3b8e94ffc9394c748" dependencies = [ "byte-slice-cast 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer-app 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", "gstreamer-audio 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "servo-media-audio 0.1.0 (git+https://github.com/servo/media)", "zip 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3204,7 +3203,7 @@ dependencies = [ [[package]] name = "servo_media_derive" version = "0.1.0" -source = "git+https://github.com/servo/media#6403c703ac7411f5d3b18f5edc5889cf1cbceb09" +source = "git+https://github.com/servo/media#d08d03f9b78827cc1516d8a3b8e94ffc9394c748" dependencies = [ "quote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/atoms/static_atoms.txt b/components/atoms/static_atoms.txt index cf8eb4603833..41ff80000737 100644 --- a/components/atoms/static_atoms.txt +++ b/components/atoms/static_atoms.txt @@ -19,6 +19,7 @@ datetime-local dir email emptied +ended error fantasy fetch diff --git a/components/script/dom/audiobuffer.rs b/components/script/dom/audiobuffer.rs index f8def4b73753..150a8d53ea44 100644 --- a/components/script/dom/audiobuffer.rs +++ b/components/script/dom/audiobuffer.rs @@ -16,8 +16,8 @@ use js::rust::CustomAutoRooterGuard; use js::typedarray::{CreateWith, Float32Array}; use servo_media::audio::buffer_source_node::AudioBuffer as ServoMediaAudioBuffer; use std::cmp::min; -use std::ptr::{self, NonNull}; use std::mem; +use std::ptr::{self, NonNull}; type JSAudioChannel = Heap<*mut JSObject>; diff --git a/components/script/dom/audioscheduledsourcenode.rs b/components/script/dom/audioscheduledsourcenode.rs index 45a091dcf186..c8ecf9a8fdaf 100644 --- a/components/script/dom/audioscheduledsourcenode.rs +++ b/components/script/dom/audioscheduledsourcenode.rs @@ -6,10 +6,15 @@ use dom::baseaudiocontext::BaseAudioContext; use dom::bindings::codegen::Bindings::AudioNodeBinding::AudioNodeOptions; use dom::bindings::codegen::Bindings::AudioScheduledSourceNodeBinding::AudioScheduledSourceNodeMethods; use dom::bindings::error::{Error, Fallible}; +use dom::bindings::inheritance::Castable; use dom::bindings::num::Finite; +use dom::bindings::refcounted::Trusted; +use dom::bindings::reflector::DomObject; use dom_struct::dom_struct; use servo_media::audio::node::{AudioNodeMessage, AudioNodeInit, AudioScheduledSourceNodeMessage}; +use servo_media::audio::node::OnEndedCallback; use std::cell::Cell; +use task_source::{TaskSource, TaskSourceName}; #[dom_struct] pub struct AudioScheduledSourceNode { @@ -51,7 +56,6 @@ impl AudioScheduledSourceNode { impl AudioScheduledSourceNodeMethods for AudioScheduledSourceNode { // https://webaudio.github.io/web-audio-api/#dom-audioscheduledsourcenode-onended - // XXX We should dispatch this when we reach the end. Depends on servo-media #82. event_handler!(ended, GetOnended, SetOnended); // https://webaudio.github.io/web-audio-api/#dom-audioscheduledsourcenode-start @@ -59,6 +63,32 @@ impl AudioScheduledSourceNodeMethods for AudioScheduledSourceNode { if self.started.get() || self.stopped.get() { return Err(Error::InvalidState); } + + let this = Trusted::new(self); + let global = self.global(); + let window = global.as_window(); + let task_source = window.dom_manipulation_task_source(); + let canceller = window.task_canceller(TaskSourceName::DOMManipulation); + let callback = OnEndedCallback::new(move || { + let _ = task_source.queue_with_canceller( + task!(ended: move || { + let this = this.root(); + let global = this.global(); + let window = global.as_window(); + window.dom_manipulation_task_source().queue_simple_event( + this.upcast(), + atom!("ended"), + &window + ); + }), + &canceller, + ); + }); + + self.node().message( + AudioNodeMessage::AudioScheduledSourceNode( + AudioScheduledSourceNodeMessage::RegisterOnEndedCallback(callback))); + self.started.set(true); self.node .message(AudioNodeMessage::AudioScheduledSourceNode(