Skip to content

Commit

Permalink
Fire AudioScheduledSourceNode.onended when playback stops
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jul 30, 2018
1 parent c9ff1b9 commit acb0360
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
9 changes: 4 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -19,6 +19,7 @@ datetime-local
dir
email
emptied
ended
error
fantasy
fetch
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/audiobuffer.rs
Expand Up @@ -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>;

Expand Down
32 changes: 31 additions & 1 deletion components/script/dom/audioscheduledsourcenode.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,14 +56,39 @@ 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
fn Start(&self, when: Finite<f64>) -> Fallible<()> {
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(
Expand Down

0 comments on commit acb0360

Please sign in to comment.