mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
gstreamer-backend: rustfmt.
This commit is contained in:
parent
1e9a52bd6e
commit
e7093cb0bc
2 changed files with 35 additions and 19 deletions
|
@ -1,14 +1,14 @@
|
|||
use super::{Open, Sink};
|
||||
use std::{io, thread};
|
||||
use std::sync::mpsc::{sync_channel, SyncSender};
|
||||
use gst::prelude::*;
|
||||
use gst::*;
|
||||
use std::sync::mpsc::{sync_channel, SyncSender};
|
||||
use std::{io, thread};
|
||||
use zerocopy::*;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct GstreamerSink {
|
||||
tx: SyncSender<Vec<u8>>,
|
||||
pipeline: gst::Pipeline
|
||||
pipeline: gst::Pipeline,
|
||||
}
|
||||
|
||||
impl Open for GstreamerSink {
|
||||
|
@ -16,25 +16,35 @@ impl Open for GstreamerSink {
|
|||
gst::init().expect("Failed to init gstreamer!");
|
||||
let pipeline_str_preamble = r#"appsrc caps="audio/x-raw,format=S16LE,layout=interleaved,channels=2,rate=44100" block=true max-bytes=4096 name=appsrc0 "#;
|
||||
let pipeline_str_rest = r#" ! audioconvert ! autoaudiosink"#;
|
||||
let pipeline_str : String = match device {
|
||||
let pipeline_str: String = match device {
|
||||
Some(x) => format!("{}{}", pipeline_str_preamble, x),
|
||||
None => format!("{}{}", pipeline_str_preamble, pipeline_str_rest)
|
||||
None => format!("{}{}", pipeline_str_preamble, pipeline_str_rest),
|
||||
};
|
||||
println!("Pipeline: {}", pipeline_str);
|
||||
|
||||
gst::init().unwrap();
|
||||
let pipelinee = gst::parse_launch(&*pipeline_str).expect("New Pipeline error");
|
||||
let pipeline = pipelinee.dynamic_cast::<gst::Pipeline>().expect("Couldn't cast pipeline element at runtime!");
|
||||
let pipeline = pipelinee
|
||||
.dynamic_cast::<gst::Pipeline>()
|
||||
.expect("Couldn't cast pipeline element at runtime!");
|
||||
let bus = pipeline.get_bus().expect("Couldn't get bus from pipeline");
|
||||
let mainloop = glib::MainLoop::new(None, false);
|
||||
let appsrce : gst::Element = pipeline.get_by_name("appsrc0").expect("Couldn't get appsrc from pipeline");
|
||||
let appsrc : gst_app::AppSrc = appsrce.dynamic_cast::<gst_app::AppSrc>().expect("Couldn't cast AppSrc element at runtime!");
|
||||
let appsrce: gst::Element = pipeline
|
||||
.get_by_name("appsrc0")
|
||||
.expect("Couldn't get appsrc from pipeline");
|
||||
let appsrc: gst_app::AppSrc = appsrce
|
||||
.dynamic_cast::<gst_app::AppSrc>()
|
||||
.expect("Couldn't cast AppSrc element at runtime!");
|
||||
let bufferpool = gst::BufferPool::new();
|
||||
let appsrc_caps = appsrc.get_caps().expect("get appsrc caps failed");
|
||||
let mut conf = bufferpool.get_config();
|
||||
conf.set_params(Some(&appsrc_caps), 8192, 0, 0);
|
||||
bufferpool.set_config(conf).expect("Couldn't configure the buffer pool");
|
||||
bufferpool.set_active(true).expect("Couldn't activate buffer pool");
|
||||
bufferpool
|
||||
.set_config(conf)
|
||||
.expect("Couldn't configure the buffer pool");
|
||||
bufferpool
|
||||
.set_active(true)
|
||||
.expect("Couldn't activate buffer pool");
|
||||
|
||||
let (tx, rx) = sync_channel::<Vec<u8>>(128);
|
||||
thread::spawn(move || {
|
||||
|
@ -44,7 +54,9 @@ impl Open for GstreamerSink {
|
|||
let mut okbuffer = buffer.unwrap();
|
||||
let mutbuf = okbuffer.make_mut();
|
||||
mutbuf.set_size(data.len());
|
||||
mutbuf.copy_from_slice(0, data.as_bytes()).expect("Failed to copy from slice");
|
||||
mutbuf
|
||||
.copy_from_slice(0, data.as_bytes())
|
||||
.expect("Failed to copy from slice");
|
||||
let _eat = appsrc.push_buffer(okbuffer);
|
||||
}
|
||||
}
|
||||
|
@ -67,18 +79,20 @@ impl Open for GstreamerSink {
|
|||
}
|
||||
_ => (),
|
||||
};
|
||||
|
||||
|
||||
glib::Continue(true)
|
||||
})
|
||||
.expect("Failed to add bus watch");
|
||||
thread_mainloop.run();
|
||||
});
|
||||
|
||||
pipeline.set_state(gst::State::Playing).expect("Unable to set the pipeline to the `Playing` state");
|
||||
pipeline
|
||||
.set_state(gst::State::Playing)
|
||||
.expect("Unable to set the pipeline to the `Playing` state");
|
||||
|
||||
GstreamerSink {
|
||||
tx: tx,
|
||||
pipeline: pipeline
|
||||
pipeline: pipeline,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,8 +106,10 @@ impl Sink for GstreamerSink {
|
|||
}
|
||||
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
||||
// Copy expensively (in to_vec()) to avoid thread synchronization
|
||||
let deighta : &[u8] = data.as_bytes();
|
||||
self.tx.send(deighta.to_vec()).expect("tx send failed in write function");
|
||||
let deighta: &[u8] = data.as_bytes();
|
||||
self.tx
|
||||
.send(deighta.to_vec())
|
||||
.expect("tx send failed in write function");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,13 +17,13 @@ extern crate libpulse_sys;
|
|||
#[cfg(feature = "jackaudio-backend")]
|
||||
extern crate jack;
|
||||
|
||||
#[cfg(feature = "gstreamer-backend")]
|
||||
extern crate glib;
|
||||
#[cfg(feature = "gstreamer-backend")]
|
||||
extern crate gstreamer as gst;
|
||||
#[cfg(feature = "gstreamer-backend")]
|
||||
extern crate gstreamer_app as gst_app;
|
||||
#[cfg(feature = "gstreamer-backend")]
|
||||
extern crate glib;
|
||||
#[cfg(feature = "gstreamer-backend")]
|
||||
extern crate zerocopy;
|
||||
|
||||
#[cfg(feature = "sdl-backend")]
|
||||
|
|
Loading…
Reference in a new issue