diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 500122ba..2ea07b87 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -5,7 +5,7 @@ license = "MPL-2.0" [dependencies] rand = "0.5.0" -servo_media = { path = "../servo-media" } +servo_media = { path = "../servo-media", features = ["gst"] } [[bin]] name = "audio_decoder" @@ -27,11 +27,10 @@ path = "play.rs" name = "play_noise" path = "play_noise.rs" - [[bin]] name = "channels" path = "channels.rs" [[bin]] name = "channelsum" -path = "channelsum.rs" \ No newline at end of file +path = "channelsum.rs" diff --git a/servo-media/Cargo.toml b/servo-media/Cargo.toml index 46b443e5..6868fd9d 100644 --- a/servo-media/Cargo.toml +++ b/servo-media/Cargo.toml @@ -9,6 +9,8 @@ regex = "1.0" zip = "0.3.1" [dependencies] +byte-slice-cast = "0.1" +num-traits = "0.1" smallvec = "0.6.1" servo_media_derive = { path = "../servo-media-derive" } @@ -16,10 +18,6 @@ servo_media_derive = { path = "../servo-media-derive" } version = "0.4.12" features = ["stable_graph"] -[dependencies.byte-slice-cast] -optional = true -version = "0.1" - [dependencies.gstreamer] optional = true version = "0.11" @@ -32,13 +30,9 @@ version = "0.11.2" optional = true version = "0.11" -[dependencies.num-traits] -optional = true -version = "0.1" - [features] -default = ["gst"] -gst = ["gstreamer", "gstreamer-audio", "gstreamer-app", "byte-slice-cast", "num-traits"] +default = [] +gst = ["gstreamer", "gstreamer-audio", "gstreamer-app"] [lib] name = "servo_media" diff --git a/servo-media/src/audio/context.rs b/servo-media/src/audio/context.rs index 64d8a42b..5ff64e18 100644 --- a/servo-media/src/audio/context.rs +++ b/servo-media/src/audio/context.rs @@ -10,6 +10,9 @@ use std::thread::Builder; #[cfg(feature = "gst")] use backends::gstreamer::audio_decoder::GStreamerAudioDecoder; +#[cfg(not(feature = "gst"))] +use backends::dummy::audio_decoder::DummyAudioDecoder; + /// Describes the state of the audio context on the control thread. #[derive(Clone, Copy, Debug, PartialEq)] pub enum ProcessingState { @@ -224,6 +227,9 @@ impl AudioContext { Builder::new() .name("AudioDecoder".to_owned()) .spawn(move || { + #[cfg(not(feature = "gst"))] + let audio_decoder = DummyAudioDecoder {}; + #[cfg(feature = "gst")] let audio_decoder = GStreamerAudioDecoder::new(); diff --git a/servo-media/src/audio/render_thread.rs b/servo-media/src/audio/render_thread.rs index 45a9fda5..75eeab8b 100644 --- a/servo-media/src/audio/render_thread.rs +++ b/servo-media/src/audio/render_thread.rs @@ -4,9 +4,9 @@ use audio::channel_node::{ChannelMergerNode, ChannelSplitterNode}; use audio::context::{ProcessingState, StateChangeResult}; use audio::destination_node::DestinationNode; use audio::gain_node::GainNode; -use audio::graph::{AudioGraph, NodeId, PortId, InputPort, OutputPort}; +use audio::graph::{AudioGraph, InputPort, NodeId, OutputPort, PortId}; use audio::node::BlockInfo; -use audio::node::{AudioNodeEngine, AudioNodeMessage, AudioNodeInit}; +use audio::node::{AudioNodeEngine, AudioNodeInit, AudioNodeMessage}; use audio::oscillator_node::OscillatorNode; use audio::sink::AudioSink; use std::sync::mpsc::{Receiver, Sender}; @@ -14,6 +14,9 @@ use std::sync::mpsc::{Receiver, Sender}; #[cfg(feature = "gst")] use backends::gstreamer::audio_sink::GStreamerAudioSink; +#[cfg(not(feature = "gst"))] +use backends::dummy::audio_sink::DummyAudioSink; + #[derive(Debug)] pub enum AudioRenderThreadMsg { CreateNode(AudioNodeInit, Sender), @@ -48,7 +51,10 @@ impl AudioRenderThread { sender: Sender, sample_rate: f32, graph: AudioGraph, - ) -> Result<(), ()> { + ) -> Result<(), ()> { + #[cfg(not(feature = "gst"))] + let sink = DummyAudioSink {}; + #[cfg(feature = "gst")] let sink = GStreamerAudioSink::new()?; @@ -73,19 +79,22 @@ impl AudioRenderThread { fn create_node(&mut self, node_type: AudioNodeInit) -> NodeId { let node: Box = match node_type { - AudioNodeInit::AudioBufferSourceNode(options) => Box::new(AudioBufferSourceNode::new(options)), + AudioNodeInit::AudioBufferSourceNode(options) => { + Box::new(AudioBufferSourceNode::new(options)) + } AudioNodeInit::DestinationNode => Box::new(DestinationNode::new()), AudioNodeInit::GainNode(options) => Box::new(GainNode::new(options)), AudioNodeInit::OscillatorNode(options) => Box::new(OscillatorNode::new(options)), AudioNodeInit::ChannelMergerNode(options) => Box::new(ChannelMergerNode::new(options)), - AudioNodeInit::ChannelSplitterNode(options) => Box::new(ChannelSplitterNode::new(options)), + AudioNodeInit::ChannelSplitterNode(options) => { + Box::new(ChannelSplitterNode::new(options)) + } _ => unimplemented!(), }; self.graph.add_node(node) } - fn connect_ports(&mut self, output: PortId, - input: PortId) { + fn connect_ports(&mut self, output: PortId, input: PortId) { self.graph.add_edge(output, input) } @@ -133,9 +142,7 @@ impl AudioRenderThread { AudioRenderThreadMsg::DisconnectAllFrom(id) => { context.graph.disconnect_all_from(id) } - AudioRenderThreadMsg::DisconnectOutput(out) => { - context.graph.disconnect_output(out) - } + AudioRenderThreadMsg::DisconnectOutput(out) => context.graph.disconnect_output(out), AudioRenderThreadMsg::DisconnectBetween(from, to) => { context.graph.disconnect_between(from, to) } diff --git a/servo-media/src/backends/dummy/audio_decoder.rs b/servo-media/src/backends/dummy/audio_decoder.rs new file mode 100644 index 00000000..d101de13 --- /dev/null +++ b/servo-media/src/backends/dummy/audio_decoder.rs @@ -0,0 +1,7 @@ +use audio::decoder::{AudioDecoder, AudioDecoderCallbacks, AudioDecoderOptions}; + +pub struct DummyAudioDecoder {} + +impl AudioDecoder for DummyAudioDecoder { + fn decode(&self, _: Vec, _: AudioDecoderCallbacks, _: Option) {} +} diff --git a/servo-media/src/backends/dummy/audio_sink.rs b/servo-media/src/backends/dummy/audio_sink.rs new file mode 100644 index 00000000..438a27cf --- /dev/null +++ b/servo-media/src/backends/dummy/audio_sink.rs @@ -0,0 +1,24 @@ +use audio::block::Chunk; +use audio::render_thread::AudioRenderThreadMsg; +use audio::sink::AudioSink; +use std::sync::mpsc::Sender; + +pub struct DummyAudioSink {} + +impl AudioSink for DummyAudioSink { + fn init(&self, _: f32, _: Sender) -> Result<(), ()> { + Ok(()) + } + fn play(&self) -> Result<(), ()> { + Ok(()) + } + fn stop(&self) -> Result<(), ()> { + Ok(()) + } + fn has_enough_data(&self) -> bool { + true + } + fn push_data(&self, _: Chunk) -> Result<(), ()> { + Ok(()) + } +} diff --git a/servo-media/src/backends/dummy/mod.rs b/servo-media/src/backends/dummy/mod.rs new file mode 100644 index 00000000..488538a6 --- /dev/null +++ b/servo-media/src/backends/dummy/mod.rs @@ -0,0 +1,2 @@ +pub mod audio_decoder; +pub mod audio_sink; diff --git a/servo-media/src/backends/mod.rs b/servo-media/src/backends/mod.rs index f854ec87..c1cac4a6 100644 --- a/servo-media/src/backends/mod.rs +++ b/servo-media/src/backends/mod.rs @@ -1,2 +1,4 @@ +#[cfg(not(feature = "gst"))] +pub mod dummy; #[cfg(feature = "gst")] pub mod gstreamer; diff --git a/servo-media/src/lib.rs b/servo-media/src/lib.rs index 110091f2..7fd0df29 100644 --- a/servo-media/src/lib.rs +++ b/servo-media/src/lib.rs @@ -11,6 +11,7 @@ extern crate gstreamer as gst; extern crate byte_slice_cast; extern crate num_traits; + extern crate petgraph; extern crate smallvec;