Fixed blocking calls

This commit is contained in:
Sebastian 2017-09-12 01:29:22 +02:00
parent 68b920d2f0
commit d2bb513336
4 changed files with 171 additions and 13 deletions

129
Cargo.lock generated
View File

@ -4,11 +4,35 @@ version = "0.1.0"
dependencies = [
"gstreamer 0.7.2 (git+https://github.com/arturoc/gstreamer1.0-rs.git)",
"itertools 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "c_vec"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "conv"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "custom_derive"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "either"
version = "1.1.0"
@ -27,11 +51,101 @@ dependencies = [
"either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "magenta"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "magenta-sys"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-iter"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)",
"magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"c_vec 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2-sys"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "1.0.12"
@ -88,10 +202,25 @@ version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum c_vec 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "aa9e1d9f7d49e289f36f19effbf3d5a5e30163ecf9c7a3c9be94d5374dec5b9a"
"checksum conv 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "78ff10625fd0ac447827aa30ea8b861fead473bb60aeb73af6c1c58caf0d1299"
"checksum custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9"
"checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
"checksum gstreamer 0.7.2 (git+https://github.com/arturoc/gstreamer1.0-rs.git)" = "<none>"
"checksum itertools 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "22c285d60139cf413244894189ca52debcfd70b57966feed060da76802e415a0"
"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
"checksum libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)" = "2370ca07ec338939e356443dac2296f581453c35fe1e3a3ed06023c49435f915"
"checksum magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf0336886480e671965f794bc9b6fce88503563013d1bfb7a502c81fe3ac527"
"checksum magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40d014c7011ac470ae28e2f76a02bfea4a8480f73e701353b49ad7a8d75f4699"
"checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525"
"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"
"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"
"checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "eb250fd207a4729c976794d03db689c9be1d634ab5a1c9da9492a13d8fecbcdf"
"checksum sdl2 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "63066036ad426250ac56d23e38fd05063b38b661556acd596f4046cc92d98415"
"checksum sdl2-sys 0.30.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b48638b7882759f3421038fcd38ad5f1ea19b119d80c99f1601933004629e34d"
"checksum serde 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "7f61b753dd58ec5d4c735f794dbddde1f28b977f652afbcde89d75bc77902216"
"checksum serde_derive 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "2a169fa5384d751ada1da9f3992b81830151a03c875e40dcb37c9fb31aafc68f"
"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a"

View File

@ -9,3 +9,8 @@ serde = "*"
serde_derive = "*"
toml = "*"
gstreamer = { git = "https://github.com/arturoc/gstreamer1.0-rs.git" }
[dependencies.sdl2]
version = "*"
default-features = false
features = ["ttf","image","gfx"]

View File

@ -1,5 +1,5 @@
[audio]
alsa_device = "hw:1,0"
alsa_device = "hw:2,0"
rate = 96000
[output]

View File

@ -1,4 +1,6 @@
extern crate sdl2;
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate gst;
@ -6,9 +8,12 @@ extern crate itertools;
use std::fs::File;
use std::io::prelude::*;
use std::sync::mpsc;
use itertools::Itertools;
use sdl2::event::Event;
use sdl2::pixels;
#[derive(Debug, Deserialize)]
@ -30,7 +35,6 @@ struct OutputConfig {
}
fn read_config(path: &str) -> Config {
let mut config_toml = String::new();
@ -51,8 +55,8 @@ fn read_config(path: &str) -> Config {
}
fn handle_pipeline_events(bus : &mut gst::bus::Bus) -> bool {
let bus_receiver = bus.receiver();
fn handle_pipeline_events(bus_receiver : &mpsc::Receiver<gst::Message>) -> bool {
while let Ok(msg) = bus_receiver.try_recv() {
match msg.parse() {
gst::Message::StateChangedParsed { ref old, ref new, .. } => {
@ -74,8 +78,9 @@ fn handle_pipeline_events(bus : &mut gst::bus::Bus) -> bool {
}
fn get_max_samples(appsink : &gst::appsink::AppSink) -> Result<(f32, f32), &str>{
match appsink.recv(){
Ok(gst::appsink::Message::NewSample(sample)) | Ok(gst::appsink::Message::NewPreroll(sample)) => {
match appsink.try_recv() {
Ok(gst::appsink::Message::NewSample(sample))
| Ok(gst::appsink::Message::NewPreroll(sample)) => {
if let Some(buffer) = sample.buffer() {
let (max0, max1) = buffer.map_read(|mapping| {
mapping.iter::<f32>().tuples().fold((0.0f32, 0.0f32), |(acc0, acc1), (sample0, sample1)| {
@ -89,12 +94,17 @@ fn get_max_samples(appsink : &gst::appsink::AppSink) -> Result<(f32, f32), &str>
Ok(gst::appsink::Message::Eos) => {
return Err("Got no sample when polling. EOS");
}
Err(_) => {
return Err("Error, exiting");
Err(mpsc::TryRecvError::Empty) => {
return Ok((0.0f32,0.0f32));
}
Err(mpsc::TryRecvError::Disconnected) => {
return Err("Appsink got disconnected")
}
}
}
const SCREEN_WIDTH: u32 = 160;
const SCREEN_HEIGHT: u32 = 128;
fn main() {
gst::init();
@ -108,24 +118,39 @@ fn main() {
config.output.directory,
config.output.prefix);
println!("{}", pipeline_str);
let mut pipeline = gst::Pipeline::new_from_str(&pipeline_str).unwrap();
let mut mainloop = gst::MainLoop::new();
let mut bus = pipeline.bus().expect("Couldn't get bus from pipeline");
let bus_receiver = bus.receiver();
let appsink = pipeline
.get_by_name("appsink0")
.expect("Couldn't get appsink from pipeline");
let appsink = gst::AppSink::new_from_element(appsink);
/*
let sdl_context = sdl2::init().unwrap();
let video_subsys = sdl_context.video().unwrap();
let window = video_subsys.window("rascam", SCREEN_WIDTH, SCREEN_HEIGHT)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(pixels::Color::RGB(255, 255, 0));
canvas.clear();
canvas.present();
*/
mainloop.spawn();
pipeline.play();
loop {
if handle_pipeline_events(&mut bus) {
if handle_pipeline_events(&bus_receiver) {
break;
}
let result = get_max_samples(& appsink);
match result {
Ok((max0, max1)) => {
@ -136,7 +161,6 @@ fn main() {
break;
}
}
}