From e1167f407b966b36ae22db45b57f51154be0253c Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 27 Feb 2022 22:15:49 +0100 Subject: [PATCH] unify code --- Cargo.lock | 358 +++++++++++++++++++++++++-------------- Cargo.toml | 4 +- src/filter/mod.rs | 14 +- src/main.rs | 18 +- src/output/desktop.rs | 41 ++++- src/utils/folder.rs | 171 +++++++++++-------- src/utils/json_reader.rs | 21 +-- src/utils/mod.rs | 61 ++++++- src/utils/playlist.rs | 37 ++-- 9 files changed, 464 insertions(+), 261 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec8e5365..571d3346 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -41,10 +41,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "cfg-if" -version = "0.1.10" +name = "bytes" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" [[package]] name = "cfg-if" @@ -62,7 +62,7 @@ dependencies = [ "num-integer", "num-traits", "time", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -101,7 +101,27 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e5bed1f1c269533fa816a0a5492b3545209a205ca1a54842be180eb63a16a6" +dependencies = [ + "cfg-if", + "lazy_static", ] [[package]] @@ -120,6 +140,7 @@ dependencies = [ "serde_json", "serde_yaml", "simplelog", + "tokio", "walkdir", ] @@ -149,10 +170,10 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", - "winapi 0.3.9", + "winapi", ] [[package]] @@ -161,54 +182,28 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e6988e897c1c9c485f43b47a529cef42fde0547f9d8d41a7062518f1d8fc53f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crc32fast", "libc", "miniz_oxide", ] -[[package]] -name = "fsevent" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" -dependencies = [ - "bitflags", - "fsevent-sys", -] - [[package]] name = "fsevent-sys" -version = "2.0.1" +version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" +checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" dependencies = [ "libc", ] -[[package]] -name = "fuchsia-zircon" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" -dependencies = [ - "bitflags", - "fuchsia-zircon-sys", -] - -[[package]] -name = "fuchsia-zircon-sys" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" - [[package]] name = "getrandom" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi", ] @@ -246,9 +241,9 @@ dependencies = [ [[package]] name = "inotify" -version = "0.7.1" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f" +checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff" dependencies = [ "bitflags", "inotify-sys", @@ -264,15 +259,6 @@ dependencies = [ "libc", ] -[[package]] -name = "iovec" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" -dependencies = [ - "libc", -] - [[package]] name = "itoa" version = "1.0.1" @@ -280,13 +266,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] -name = "kernel32-sys" -version = "0.2.2" +name = "kqueue" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +checksum = "058a107a784f8be94c7d35c1300f4facced2e93d2fbe5b1452b44e905ddca4a9" dependencies = [ - "winapi 0.2.8", - "winapi-build", + "kqueue-sys", + "libc", +] + +[[package]] +name = "kqueue-sys" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8367585489f01bc55dd27404dcf56b95e6da061a256a666ab23be9ba96a2e587" +dependencies = [ + "bitflags", + "libc", ] [[package]] @@ -295,12 +291,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lazycell" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" - [[package]] name = "libc" version = "0.2.119" @@ -313,13 +303,22 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +[[package]] +name = "lock_api" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -340,74 +339,65 @@ dependencies = [ [[package]] name = "mio" -version = "0.6.23" +version = "0.7.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4" +checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" dependencies = [ - "cfg-if 0.1.10", - "fuchsia-zircon", - "fuchsia-zircon-sys", - "iovec", - "kernel32-sys", "libc", "log", "miow", - "net2", - "slab", - "winapi 0.2.8", + "ntapi", + "winapi", ] [[package]] -name = "mio-extras" -version = "2.0.6" +name = "mio" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" +checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" dependencies = [ - "lazycell", + "libc", "log", - "mio", - "slab", + "miow", + "ntapi", + "winapi", ] [[package]] name = "miow" -version = "0.2.2" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ - "kernel32-sys", - "net2", - "winapi 0.2.8", - "ws2_32-sys", -] - -[[package]] -name = "net2" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "winapi 0.3.9", + "winapi", ] [[package]] name = "notify" -version = "4.0.17" +version = "5.0.0-pre.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae03c8c853dba7bfd23e571ff0cff7bc9dceb40a4cd684cd1681824183f45257" +checksum = "245d358380e2352c2d020e8ee62baac09b3420f1f6c012a31326cfced4ad487d" dependencies = [ "bitflags", + "crossbeam-channel", "filetime", - "fsevent", "fsevent-sys", "inotify", + "kqueue", "libc", - "mio", - "mio-extras", + "mio 0.7.14", + "serde", "walkdir", - "winapi 0.3.9", + "winapi", +] + +[[package]] +name = "ntapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f" +dependencies = [ + "winapi", ] [[package]] @@ -429,6 +419,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + [[package]] name = "os_str_bytes" version = "6.0.0" @@ -444,6 +450,35 @@ version = "1.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c69d19a208bba8b94bd27d4b7a06ad153cddc6b88cb2149a668e23ce7bdb67d5" +[[package]] +name = "parking_lot" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" + [[package]] name = "ppv-lite86" version = "0.2.16" @@ -563,6 +598,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "serde" version = "1.0.136" @@ -606,6 +647,15 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "simplelog" version = "0.11.2" @@ -619,10 +669,20 @@ dependencies = [ ] [[package]] -name = "slab" -version = "0.4.5" +name = "smallvec" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] [[package]] name = "strsim" @@ -663,7 +723,38 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" dependencies = [ "libc", - "winapi 0.3.9", + "winapi", +] + +[[package]] +name = "tokio" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +dependencies = [ + "bytes", + "libc", + "memchr", + "mio 0.8.0", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -685,7 +776,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", - "winapi 0.3.9", + "winapi", "winapi-util", ] @@ -695,12 +786,6 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" -[[package]] -name = "winapi" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" - [[package]] name = "winapi" version = "0.3.9" @@ -711,12 +796,6 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -729,7 +808,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi 0.3.9", + "winapi", ] [[package]] @@ -739,15 +818,48 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] -name = "ws2_32-sys" -version = "0.2.1" +name = "windows-sys" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" +checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6" dependencies = [ - "winapi 0.2.8", - "winapi-build", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", ] +[[package]] +name = "windows_aarch64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" + +[[package]] +name = "windows_i686_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" + +[[package]] +name = "windows_i686_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index 7e4b4824..fde013c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,16 +11,16 @@ clap = { version = "3.0.14", features = ["derive"] } ffprobe = "0.3" file-rotate = "0.6.0" log = "0.4.14" -notify = "4.0.0" +notify = { version = "5.0.0-pre.13", features = ["serde"] } rand = "0.8.5" regex = "1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.8" simplelog = { version = "^0.11.2", features = ["paris"] } +tokio = { version = "1.16.1", features = ["full"] } walkdir = "2" -# tokio = { version = "1.16.1", features = ["full"] } [[bin]] name = "ffplayout" path = "src/main.rs" diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 3cd4770e..e3abf99c 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::path::Path; -use crate::utils::{is_close, Config, Program}; +use crate::utils::{is_close, Config, Media}; #[derive(Debug, Serialize, Deserialize, Clone)] struct Filters { @@ -117,7 +117,7 @@ fn scale(width: i64, height: i64, aspect: f64, chain: &mut Filters, config: &Con } } -fn fade(node: &mut Program, chain: &mut Filters, codec_type: String) { +fn fade(node: &mut Media, chain: &mut Filters, codec_type: String) { let mut t = "".to_string(); if codec_type == "audio".to_string() { @@ -136,7 +136,7 @@ fn fade(node: &mut Program, chain: &mut Filters, codec_type: String) { } } -fn overlay(node: &mut Program, chain: &mut Filters, config: &Config, last_ad: bool, next_ad: bool) { +fn overlay(node: &mut Media, chain: &mut Filters, config: &Config, last_ad: bool, next_ad: bool) { if config.processing.add_logo && Path::new(&config.processing.logo).is_file() && node.category != "advertisement".to_string() @@ -165,7 +165,7 @@ fn overlay(node: &mut Program, chain: &mut Filters, config: &Config, last_ad: bo } } -fn extend_video(node: &mut Program, chain: &mut Filters) { +fn extend_video(node: &mut Media, chain: &mut Filters) { let video_streams = node.probe.clone().unwrap().video_streams.unwrap(); if video_streams.len() > 0 { let video_duration = &video_streams[0].duration; @@ -180,7 +180,7 @@ fn extend_video(node: &mut Program, chain: &mut Filters) { } } -fn add_audio(node: &mut Program, chain: &mut Filters) { +fn add_audio(node: &mut Media, chain: &mut Filters) { let audio_streams = node.probe.clone().unwrap().audio_streams.unwrap(); if audio_streams.len() == 0 { println!("Clip: '{}' has no audio!", node.source); @@ -192,7 +192,7 @@ fn add_audio(node: &mut Program, chain: &mut Filters) { } } -fn extend_audio(node: &mut Program, chain: &mut Filters) { +fn extend_audio(node: &mut Media, chain: &mut Filters) { let audio_streams = node.probe.clone().unwrap().audio_streams.unwrap(); if audio_streams.len() > 0 { let audio_duration = &audio_streams[0].duration; @@ -213,7 +213,7 @@ fn audio_volume(chain: &mut Filters, config: &Config) { } } -pub fn filter_chains(node: &mut Program, config: &Config, last: bool, next: bool) -> Vec { +pub fn filter_chains(node: &mut Media, config: &Config, last: bool, next: bool) -> Vec { let mut filters = Filters::new(); let probe = node.probe.clone(); diff --git a/src/main.rs b/src/main.rs index f8f56954..249a288e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,22 +16,8 @@ fn main() { CombinedLogger::init(logging).unwrap(); - warn!("this is a warning"); - error!("this is a error"); - - // folder::walk(&config.storage.path, config.storage.shuffle, &config.storage.extensions); - - // read_json(&config); - - // let args = arg_parse::get_args(); - - // println!("{:#?}", args); - // println!("{:#?}", args.config.is_some()); - // // println!("{:#?}", args.config.unwrap()); - // //println!("{:?}", config.general.stop_threshold); - - // println!("{:#?}", utils::get_sec()); - // println!("{:#?}", utils::get_timestamp()); + // warn!("this is a warning"); + // error!("this is a error"); desktop::play(config); } diff --git a/src/output/desktop.rs b/src/output/desktop.rs index eda3dbda..4291c3bb 100644 --- a/src/output/desktop.rs +++ b/src/output/desktop.rs @@ -1,16 +1,51 @@ use std::{ io::{prelude::*, Read}, + path::Path, + process, process::{Command, Stdio}, + sync::Arc, thread::sleep, time::Duration, }; +use tokio::sync::Mutex; +use tokio::runtime::Builder; + use simplelog::*; -use crate::utils::{sec_to_time, Config, CurrentProgram}; +use crate::utils::{sec_to_time, watch_folder, Config, CurrentProgram, Media, Source}; pub fn play(config: Config) { - let get_source = CurrentProgram::new(config.clone()); + let get_source = match config.processing.mode.clone().as_str() { + "folder" => { + let path = config.storage.path.clone(); + if !Path::new(&path).exists() { + error!("Folder path not exists: '{path}'"); + process::exit(0x0100); + } + + let runtime = Builder::new_multi_thread() + .worker_threads(1) + .enable_all() + .build() + .unwrap(); + + let folder_source = Source::new(config.clone()); + let mut folder_sync = Arc::new(Mutex::new(folder_source.clone())); + + runtime.spawn(watch_folder(&path, &mut folder_sync)); + + Box::new(folder_source) as Box> + } + "playlist" => { + Box::new(CurrentProgram::new(config.clone())) as Box> + } + _ => { + error!("Process Mode not exists!"); + process::exit(0x0100); + } + }; + // let get_source = CurrentProgram::new(config.clone()); let dec_settings = config.processing.settings.unwrap(); let ff_log_format = format!("level+{}", config.logging.ffmpeg_level); let mut enc_cmd = vec![ @@ -54,7 +89,7 @@ pub fn play(config: Config) { for node in get_source { // println!("Node begin: {:?}", sec_to_time(node.begin.unwrap())); - info!( + info!( "Play for {}: {}", sec_to_time(node.out - node.seek), node.source diff --git a/src/utils/folder.rs b/src/utils/folder.rs index 1f58b4c3..9d22e009 100644 --- a/src/utils/folder.rs +++ b/src/utils/folder.rs @@ -1,58 +1,117 @@ use notify::DebouncedEvent::{Create, Remove, Rename}; use notify::{watcher, RecursiveMode, Watcher}; -use rand::(seq::SliceRandom, thread_rng); +use rand::{seq::SliceRandom, thread_rng}; use std::{ ffi::OsStr, path::Path, - process, - sync::mpsc::{channel, Receiver, TryRecvError}, - thread, time, + sync::{mpsc::channel, Arc}, time::Duration, }; + +use tokio::sync::Mutex; + use walkdir::WalkDir; -#[derive(Clone)] -struct Source { - files: Vec, +use simplelog::*; + +use crate::utils::{Config, Media}; + +#[derive(Debug, Clone)] +pub struct Source { + config: Config, + nodes: Vec, + index: usize, } impl Source { - fn new(path: &String, extensions: &Vec) -> Self { + pub fn new(config: Config) -> Self { let mut file_list = vec![]; - for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) { + for entry in WalkDir::new(config.storage.path.clone()) + .into_iter() + .filter_map(|e| e.ok()) + { if entry.path().is_file() { let ext = file_extension(entry.path()); - if ext.is_some() && extensions.contains(&ext.unwrap().to_lowercase()) { + if ext.is_some() + && config + .storage + .extensions + .clone() + .contains(&ext.unwrap().to_lowercase()) + { file_list.push(entry.path().display().to_string()); } } } - Self { files: file_list } + if config.storage.shuffle { + info!("Shuffle files"); + let mut rng = thread_rng(); + file_list.shuffle(&mut rng); + } else { + file_list.sort(); + } + + Self { + config: config, + nodes: file_list, + index: 0, + } } fn push(&mut self, file: String) { - self.files.push(file) + self.nodes.push(file) } fn rm(&mut self, file: String) { - self.files.retain(|x| x != &file); + self.nodes.retain(|x| x != &file); } fn mv(&mut self, old_file: String, new_file: String) { - let i = self.files.iter().position(|x| *x == old_file).unwrap(); - self.files[i] = new_file; + let i = self.nodes.iter().position(|x| *x == old_file).unwrap(); + self.nodes[i] = new_file; } fn shuffle(&mut self) { let mut rng = thread_rng(); - self.files.shuffle(&mut rng); + self.nodes.shuffle(&mut rng); } fn sort(&mut self) { - self.files.sort(); + self.nodes.sort(); + } +} + +impl Iterator for Source { + type Item = Media; + + fn next(&mut self) -> Option { + if self.index < self.nodes.len() { + let current_file = self.nodes[self.index].clone(); + let mut media = Media::new(self.index, current_file); + media.add_probe(); + media.add_filter(&self.config, false, false); + self.index += 1; + + Some(media) + } else { + if self.config.storage.shuffle { + info!("Shuffle files"); + self.shuffle(); + } else { + self.sort(); + } + + let current_file = self.nodes[0].clone(); + let mut media = Media::new(self.index, current_file); + media.add_probe(); + media.add_filter(&self.config, false, false); + self.index = 1; + + Some(media) + } } } @@ -60,61 +119,39 @@ fn file_extension(filename: &Path) -> Option<&str> { filename.extension().and_then(OsStr::to_str) } -fn watch_folder(source: &mut Source, receiver: &Receiver) { - match receiver.try_recv() { - Ok(event) => match event { - Create(new_path) => { - println!("Create new file: {:?}", new_path); - source.push(new_path.display().to_string()); - } - Remove(old_path) => { - println!("Remove file: {:?}", old_path); - source.rm(old_path.display().to_string()); - } - Rename(old_path, new_path) => { - println!("Rename file: {:?} to {:?}", old_path, new_path); - source.mv( - old_path.display().to_string(), - new_path.display().to_string(), - ); - } - _ => (), - }, - Err(err) => { - if err == TryRecvError::Disconnected { - println!("Folder watch error: {:?}", err) - } - } - } -} - -pub fn walk(path: &String, shuffle: bool, extensions: &Vec) { - if !Path::new(path).exists() { - println!("Folder path not exists: '{path}'"); - process::exit(0x0100); - } - let mut source = Source::new(path, extensions); - let mut index: usize = 0; - +pub async fn watch_folder(path: &String, source: &mut Arc>) { + // let mut source = Source::new(); let (sender, receiver) = channel(); + let mut watcher = watcher(sender, Duration::from_secs(2)).unwrap(); watcher.watch(path, RecursiveMode::Recursive).unwrap(); + println!("watch path: '{}'", path); + loop { - if shuffle { - println!("Shuffle files in folder: '{path}'"); - source.shuffle(); - } else { - source.sort(); + match receiver.recv() { + Ok(event) => match event { + Create(new_path) => { + println!("Create new file: {:?}", new_path); + let mut lock = source.lock().await; + lock.push(new_path.display().to_string()); + } + Remove(old_path) => { + println!("Remove file: {:?}", old_path); + let mut lock = source.lock().await; + lock.rm(old_path.display().to_string()); + } + Rename(old_path, new_path) => { + println!("Rename file: {:?} to {:?}", old_path, new_path); + let mut lock = source.lock().await; + lock.mv( + old_path.display().to_string(), + new_path.display().to_string(), + ); + } + _ => (), + }, + Err(e) => println!("watch error: {:?}", e), } - - while index < source.files.len() { - watch_folder(&mut source, &receiver); - println!("Play file {index}: {:?}", source.files[index]); - index += 1; - - thread::sleep(time::Duration::from_secs(1)); - } - index = 0 } } diff --git a/src/utils/json_reader.rs b/src/utils/json_reader.rs index 0d20d001..a2b611e1 100644 --- a/src/utils/json_reader.rs +++ b/src/utils/json_reader.rs @@ -3,7 +3,7 @@ use std::{fs::File, path::Path}; use simplelog::*; -use crate::utils::{get_date, get_sec, modified_time, time_to_sec, Config, MediaProbe}; +use crate::utils::{get_date, get_sec, modified_time, time_to_sec, Config, Media}; #[derive(Debug, Serialize, Deserialize)] pub struct Playlist { @@ -11,24 +11,7 @@ pub struct Playlist { pub current_file: Option, pub start_index: Option, pub modified: Option, - pub program: Vec, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Program { - pub begin: Option, - pub index: Option, - #[serde(rename = "in")] - pub seek: f64, - pub out: f64, - pub duration: f64, - pub category: String, - pub source: String, - pub cmd: Option>, - pub filter: Option>, - pub probe: Option, - pub last_ad: Option, - pub next_ad: Option, + pub program: Vec, } pub fn read_json(config: &Config, seek: bool) -> Playlist { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index aaa65565..5dee87df 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -6,17 +6,72 @@ use std::{fs::metadata, process, time, time::UNIX_EPOCH}; mod arg_parse; mod config; +mod folder; mod json_reader; mod logging; mod playlist; pub use arg_parse::get_args; pub use config::{get_config, Config}; -// pub use folder::walk; -pub use json_reader::{read_json, Program}; +pub use folder::{watch_folder, Source}; +pub use json_reader::read_json; pub use logging::init_logging; pub use playlist::CurrentProgram; +use crate::filter::filter_chains; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Media { + pub begin: Option, + pub index: Option, + #[serde(rename = "in")] + pub seek: f64, + pub out: f64, + pub duration: f64, + pub category: String, + pub source: String, + pub cmd: Option>, + pub filter: Option>, + pub probe: Option, + pub last_ad: Option, + pub next_ad: Option, +} + +impl Media { + fn new(index: usize, src: String) -> Self { + let probe = MediaProbe::new(src.clone()); + + let duration: f64 = match &probe.clone().format.unwrap().duration { + Some(dur) => dur.parse().unwrap(), + None => 0.0 + }; + + Self { + begin: None, + index: Some(index), + seek: 0.0, + out: duration, + duration: duration, + category: "".to_string(), + source: src.clone(), + cmd: Some(vec!["-i".to_string(), src]), + filter: Some(vec![]), + probe: Some(probe), + last_ad: Some(false), + next_ad: Some(false), + } + } + + fn add_probe(&mut self) { + self.probe = Some(MediaProbe::new(self.source.clone())) + } + + fn add_filter(&mut self, config: &Config, last: bool, next: bool) { + let mut node = self.clone(); + self.filter = Some(filter_chains(&mut node, &config, last, next)); + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct MediaProbe { pub format: Option, @@ -120,7 +175,7 @@ pub fn modified_time(path: String) -> Option> { pub fn time_to_sec(time_str: &String) -> f64 { if ["now", "", "none"].contains(&time_str.as_str()) || !time_str.contains(":") { - return get_sec() + return get_sec(); } let t: Vec<&str> = time_str.split(':').collect(); diff --git a/src/utils/playlist.rs b/src/utils/playlist.rs index 3af4dd63..5af0df9d 100644 --- a/src/utils/playlist.rs +++ b/src/utils/playlist.rs @@ -3,18 +3,15 @@ use std::path::Path; use simplelog::*; use crate::utils::{ - check_sync, gen_dummy, get_delta, - json_reader::{read_json, Program}, - modified_time, Config, MediaProbe, + check_sync, gen_dummy, get_delta, json_reader::read_json, modified_time, Config, Media, }; -use crate::filter::filter_chains; - +#[derive(Debug)] pub struct CurrentProgram { config: Config, json_mod: String, json_path: String, - nodes: Vec, + nodes: Vec, init: bool, idx: usize, } @@ -44,18 +41,10 @@ impl CurrentProgram { self.nodes = json.program.into(); } } - - fn append_probe(&mut self, node: &mut Program) { - node.probe = Some(MediaProbe::new(node.source.clone())) - } - - fn add_filter(&mut self, node: &mut Program, last: bool, next: bool) { - node.filter = Some(filter_chains(node, &self.config, last, next)); - } } impl Iterator for CurrentProgram { - type Item = Program; + type Item = Media; fn next(&mut self) -> Option { if self.idx < self.nodes.len() { @@ -68,8 +57,14 @@ impl Iterator for CurrentProgram { last = true } - debug!("Last: {}", self.nodes[self.idx - 1].source); - debug!("Next: {}", self.nodes[self.idx + 1].source); + debug!( + "Last: {}", + self.nodes[self.idx - 1].source + ); + debug!( + "Next: {}", + self.nodes[self.idx + 1].source + ); self.idx += 1; @@ -85,8 +80,8 @@ impl Iterator for CurrentProgram { } if Path::new(¤t.source).is_file() { - self.append_probe(&mut current); - self.add_filter(&mut current, last, next); + current.add_probe(); + current.add_filter(&self.config, last, next); } else { error!("File not found: {}", current.source); let dummy = gen_dummy(current.out - current.seek, &self.config); @@ -124,8 +119,8 @@ impl Iterator for CurrentProgram { } if Path::new(¤t.source).is_file() { - self.append_probe(&mut current); - self.add_filter(&mut current, last, next); + current.add_probe(); + current.add_filter(&self.config, last, next); } else { error!("File not found: {}", current.source); let dummy = gen_dummy(current.out - current.seek, &self.config);