diff --git a/src/bin/dump.rs b/src/bin/dump.rs deleted file mode 100644 index 4512dc4..0000000 --- a/src/bin/dump.rs +++ /dev/null @@ -1,29 +0,0 @@ -extern crate webmetro; - -use std::env::args; -use std::fs::File; -use std::io::Read; -use std::path::Path; -use webmetro::webm::{ parse_webm, SimpleBlock }; -use webmetro::webm::WebmElement::*; - -pub fn main() { - let mut args = args(); - let _ = args.next(); - let filename = args.next().expect("Reading filename"); - - let mut buffer = Vec::new(); - let mut file = File::open(Path::new(&filename)).expect("Opening file"); - - file.read_to_end(&mut buffer).expect("Reading file contents"); - - for element in parse_webm(buffer.as_slice()) { - match element { - // suppress printing byte arrays - Tracks(slice) => println!("Tracks[{}]", slice.len()), - SimpleBlock(SimpleBlock {timecode, ..}) => println!("SimpleBlock@{}", timecode), - other => println!("{:?}", other) - } - } - -} diff --git a/src/commands/dump.rs b/src/commands/dump.rs new file mode 100644 index 0000000..af94cec --- /dev/null +++ b/src/commands/dump.rs @@ -0,0 +1,55 @@ +use std::{ + error::Error, + io::{self, prelude::*} +}; + +use clap::{App, AppSettings, ArgMatches, SubCommand}; +use futures::{ + Async, + stream::poll_fn +}; + +use webmetro::{ + stream_parser::StreamEbml, + webm::{ + SimpleBlock, + WebmElement::* + } +}; + +pub fn options() -> App<'static, 'static> { + SubCommand::with_name("dump") + .setting(AppSettings::Hidden) + .about("Dumps WebM parsing events from parsing stdin") +} + +pub fn run(_args: &ArgMatches) -> Result<(), Box> { + + let stdin = io::stdin(); + let mut buf_reader = stdin.lock(); + let mut read_bytes = 0; + + let mut events = poll_fn(|| { + buf_reader.consume(read_bytes); + buf_reader.fill_buf().map(|slice| { + read_bytes = slice.len(); + if read_bytes > 0 { + Async::Ready(Some(Into::>::into(slice))) + } else { + Async::Ready(None) + } + }) + }).parse_ebml(); + + // stdin is sync so Async::NotReady will never happen + while let Ok(Async::Ready(Some(element))) = events.poll_event() { + match element { + // suppress printing byte arrays + Tracks(slice) => println!("Tracks[{}]", slice.len()), + SimpleBlock(SimpleBlock {timecode, ..}) => println!("SimpleBlock@{}", timecode), + other => println!("{:?}", other) + } + } + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6193dd9..98a684e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1 +1,2 @@ +pub mod dump; pub mod relay; diff --git a/src/main.rs b/src/main.rs index 788cc7f..d91be28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ extern crate webmetro; mod commands; use clap::{App, AppSettings}; -use commands::{relay}; +use commands::{ + relay, + dump +}; fn options() -> App<'static, 'static> { App::new("webmetro") @@ -14,6 +17,7 @@ fn options() -> App<'static, 'static> { .about("Utilities for broadcasting & relaying live WebM video/audio streams") .setting(AppSettings::VersionlessSubcommands) .subcommand(relay::options()) + .subcommand(dump::options()) } fn main() { @@ -21,6 +25,7 @@ fn main() { match args.subcommand() { ("relay", Some(sub_args)) => relay::run(sub_args), + ("dump", Some(sub_args)) => dump::run(sub_args), _ => { options().print_help().unwrap(); println!("");