use *futures* executor instead of glib

This commit is contained in:
2020-03-13 16:32:43 +01:00
parent fbd0b8b976
commit 1bdcebccb5

View File

@@ -21,7 +21,7 @@ fn gmake<T: IsA<Element>>(factory_name: &str) -> Result<T> {
Ok(res) Ok(res)
} }
fn get_paths(input: PathBuf, output: PathBuf) -> impl Iterator<Item = (PathBuf, PathBuf)> { fn get_path_pairs(input: PathBuf, output: PathBuf) -> impl Iterator<Item = (PathBuf, PathBuf)> {
walkdir::WalkDir::new(input.as_path()) walkdir::WalkDir::new(input.as_path())
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
@@ -51,30 +51,18 @@ fn get_paths(input: PathBuf, output: PathBuf) -> impl Iterator<Item = (PathBuf,
fn main() -> Result<()> { fn main() -> Result<()> {
gstreamer::init()?; gstreamer::init()?;
let ctx = glib::MainContext::default();
ctx.push_thread_default();
let glib_loop = glib::MainLoop::new(Some(&ctx), false);
let input = std::env::args().nth(1).expect("missing input"); let input = std::env::args().nth(1).expect("missing input");
let output = std::env::args().nth(2).expect("missing output"); let output = std::env::args().nth(2).expect("missing output");
futures::executor::block_on(
let it = get_paths(input.into(), output.into()); futures::stream::iter(get_path_pairs(input.into(), output.into())).for_each_concurrent(
num_cpus::get(),
let glib_loop_clone = glib_loop.clone(); |(src, dest)| async move {
let f = futures::stream::iter(it)
.for_each_concurrent(num_cpus::get(), |(src, dest)| async move {
if let Err(err) = transcode(src.as_path(), dest.as_path()).await { if let Err(err) = transcode(src.as_path(), dest.as_path()).await {
println!("err {} => {}:\n{:?}", src.display(), dest.display(), err); println!("err {} => {}:\n{:?}", src.display(), dest.display(), err);
} }
}) },
.then(move |_| { ),
// we're done, kill the loop );
glib_loop_clone.quit();
futures::future::ready(())
});
ctx.spawn_local(f);
glib_loop.run();
ctx.pop_thread_default();
Ok(()) Ok(())
} }