diff --git a/Cargo.lock b/Cargo.lock index 053136f..73f3d64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,6 +110,12 @@ version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" +[[package]] +name = "byte-slice-cast" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c1bf4a04a88c54f589125563643d773f3254b5c38571395e2b591c693bbc81" + [[package]] name = "cfb-mode" version = "0.6.0" @@ -303,21 +309,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" -[[package]] -name = "futures" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adc00f486adfc9ce99f77d717836f0c5aa84965eb0b4f051f4e83f7cab53f8b" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - [[package]] name = "futures-channel" version = "0.3.16" @@ -325,7 +316,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74ed2411805f6e4e3d9bc904c95d5d423b89b3b25dc0250aa74729de20629ff9" dependencies = [ "futures-core", - "futures-sink", ] [[package]] @@ -351,19 +341,6 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b0e06c393068f3a6ef246c75cdca793d6a46347e75286933e5e75fd2fd11582" -[[package]] -name = "futures-macro" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c54913bae956fb8df7f4dc6fc90362aa72e69148e3f39041fbe8742d21e0ac57" -dependencies = [ - "autocfg", - "proc-macro-hack", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "futures-sink" version = "0.3.16" @@ -383,17 +360,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67eb846bfd58e44a8481a00049e82c43e0ccb5d61f8dc071057cb19249dd4d78" dependencies = [ "autocfg", - "futures-channel", "futures-core", - "futures-io", - "futures-macro", - "futures-sink", "futures-task", - "memchr", "pin-project-lite", "pin-utils", - "proc-macro-hack", - "proc-macro-nested", "slab", ] @@ -409,20 +379,16 @@ dependencies = [ [[package]] name = "gio" -version = "0.9.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fb60242bfff700772dae5d9e3a1f7aa2e4ebccf18b89662a16acb2822568561" +checksum = "86c6823b39d46d22cac2466de261f28d7f049ebc18f7b35296a42c7ed8a88325" dependencies = [ "bitflags", - "futures", "futures-channel", "futures-core", "futures-io", - "futures-util", "gio-sys", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "libc", "once_cell", "thiserror", @@ -430,36 +396,17 @@ dependencies = [ [[package]] name = "gio-sys" -version = "0.10.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e24fb752f8f5d2cf6bbc2c606fd2bc989c81c5e2fe321ab974d54f8b6344eac" +checksum = "c0a41df66e57fcc287c4bcf74fc26b884f31901ea9792ec75607289b456f48fa" dependencies = [ - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", + "gobject-sys", "libc", - "system-deps 1.3.2", + "system-deps", "winapi", ] -[[package]] -name = "glib" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5" -dependencies = [ - "bitflags", - "futures-channel", - "futures-core", - "futures-executor", - "futures-task", - "futures-util", - "glib-macros 0.10.1", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", - "libc", - "once_cell", -] - [[package]] name = "glib" version = "0.14.2" @@ -471,30 +418,14 @@ dependencies = [ "futures-core", "futures-executor", "futures-task", - "glib-macros 0.14.1", - "glib-sys 0.14.0", - "gobject-sys 0.14.0", + "glib-macros", + "glib-sys", + "gobject-sys", "libc", "once_cell", "smallvec", ] -[[package]] -name = "glib-macros" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41486a26d1366a8032b160b59065a59fb528530a46a49f627e7048fb8c064039" -dependencies = [ - "anyhow", - "heck", - "itertools 0.9.0", - "proc-macro-crate 0.1.5", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "glib-macros" version = "0.14.1" @@ -503,23 +434,13 @@ checksum = "2aad66361f66796bfc73f530c51ef123970eb895ffba991a234fcf7bea89e518" dependencies = [ "anyhow", "heck", - "proc-macro-crate 1.0.0", + "proc-macro-crate", "proc-macro-error", "proc-macro2", "quote", "syn", ] -[[package]] -name = "glib-sys" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1" -dependencies = [ - "libc", - "system-deps 1.3.2", -] - [[package]] name = "glib-sys" version = "0.14.0" @@ -527,18 +448,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c1d60554a212445e2a858e42a0e48cece1bd57b311a19a9468f70376cf554ae" dependencies = [ "libc", - "system-deps 3.2.0", -] - -[[package]] -name = "gobject-sys" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "952133b60c318a62bf82ee75b93acc7e84028a093e06b9e27981c2b6fe68218c" -dependencies = [ - "glib-sys 0.10.1", - "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] @@ -547,28 +457,27 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa92cae29759dae34ab5921d73fff5ad54b3d794ab842c117e36cafc7994c3f5" dependencies = [ - "glib-sys 0.14.0", + "glib-sys", "libc", - "system-deps 3.2.0", + "system-deps", ] [[package]] name = "gstreamer" -version = "0.16.7" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ff5d0f7ff308ae37e6eb47b6ded17785bdea06e438a708cd09e0288c1862f33" +checksum = "1f71e8c163945a3e0140e6b1ed10d1507da7e71196fedec568cbad92db63d762" dependencies = [ "bitflags", "cfg-if 1.0.0", "futures-channel", "futures-core", "futures-util", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer-sys", "libc", "muldiv", + "num-integer", "num-rational", "once_cell", "paste", @@ -578,207 +487,183 @@ dependencies = [ [[package]] name = "gstreamer-app" -version = "0.16.5" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc80888271338c3ede875d8cafc452eb207476ff5539dcbe0018a8f5b827af0e" +checksum = "d2ed9c3e9ce3a406ea5026c2d4e094acd8efd61084bb107f8ff97d13f739b542" dependencies = [ "bitflags", "futures-core", "futures-sink", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer", "gstreamer-app-sys", "gstreamer-base", - "gstreamer-sys", "libc", "once_cell", ] [[package]] name = "gstreamer-app-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "813f64275c9e7b33b828b9efcf9dfa64b95996766d4de996e84363ac65b87e3d" +checksum = "41865cfb8a5ddfa1161734a0d068dcd4689da852be0910b40484206408cfeafa" dependencies = [ - "glib-sys 0.10.1", + "glib-sys", "gstreamer-base-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-base" -version = "0.16.5" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bafd01c56f59cb10f4b5a10f97bb4bdf8c2b2784ae5b04da7e2d400cf6e6afcf" +checksum = "5cf50118e1933d9e9e043036f2306d0b9660182ea61a75f266cd9fb3607b7a19" dependencies = [ "bitflags", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "cfg-if 1.0.0", + "glib", "gstreamer", "gstreamer-base-sys", - "gstreamer-sys", "libc", ] [[package]] name = "gstreamer-base-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4b7b6dc2d6e160a1ae28612f602bd500b3fa474ce90bf6bb2f08072682beef5" +checksum = "28169a7b58edb93ad8ac766f0fa12dcd36a2af4257a97ee10194c7103baf3e27" dependencies = [ - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", + "gobject-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-net" -version = "0.16.5" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ff4b9468ce5fc3d366374d6e156f4e66f316ef2580d4e6c6167a5a090f48996" +checksum = "46e71e9301725be01dcb2e260a531ab62fd42f3d4586de3da7060220b4f675a3" dependencies = [ "gio", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer", "gstreamer-net-sys", - "gstreamer-sys", ] [[package]] name = "gstreamer-net-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5317f292e00d1e7b966a3876ca7568abdab3d0559b2c050d1dd3bbbef9c5b99f" +checksum = "6a36633587f9f1c34d5ed3ba5e7e2b4d89739fea2b9ee5ac101bb0685c35ad6b" dependencies = [ "gio-sys", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-rtsp" -version = "0.16.5" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46370552813cfdf431f409dd5ffefa24ae80f3189a034ec098123f4cf1286389" +checksum = "398187f5b704e30e90f6bd2b75f0cee537b7c299dd5111d06b6bf7a8bd4305a2" dependencies = [ "bitflags", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer", "gstreamer-rtsp-sys", "gstreamer-sdp", - "gstreamer-sdp-sys", - "gstreamer-sys", "libc", ] [[package]] name = "gstreamer-rtsp-server" -version = "0.16.5" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb39e0061d9aa519948276813e45249ae1cf41d098a04da490b085a1f2180466" +checksum = "2ca2a54cc0094a0d7926c12ae58de190f5883605bbe5ff7fb78b4a3832ca85f6" dependencies = [ "bitflags", "gio", - "gio-sys", - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer", "gstreamer-net", - "gstreamer-net-sys", "gstreamer-rtsp", "gstreamer-rtsp-server-sys", - "gstreamer-rtsp-sys", "gstreamer-sdp", - "gstreamer-sdp-sys", - "gstreamer-sys", "libc", "once_cell", ] [[package]] name = "gstreamer-rtsp-server-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "372f48e74995eb4855bac56210ea97d8cf34ef5b328c225f35bdbf3fdedd0d06" +checksum = "3ac39d406e3e7dd40b9dd6332b6193b1a40d2c9d7f4795579707589c951e32a6" dependencies = [ "gio-sys", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", + "gobject-sys", "gstreamer-net-sys", "gstreamer-rtsp-sys", "gstreamer-sdp-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-rtsp-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0938ef76af0226e3d9488957b5bfdd3359d01adc1d3b4792e166bc2cec86f1be" +checksum = "244405aaa835f94f35cffed30bde7f2bc59bef5822ad747109921d429b7fb371" dependencies = [ "gio-sys", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", + "gobject-sys", "gstreamer-sdp-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-sdp" -version = "0.16.6" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2583808102240bf3e94b4dc5d099ee0ad36557211279c8b90b82c45c2b76e8" +checksum = "4fb915df42663a982c64d65c36e7de7963c96733ea591338c648cc124fc0b5c9" dependencies = [ - "glib 0.10.3", - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib", "gstreamer", "gstreamer-sdp-sys", - "gstreamer-sys", ] [[package]] name = "gstreamer-sdp-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "405adf64f61c427c1c3cd6d928f0ca891bde2614b7bd31c3b3670ae0da3ac79a" +checksum = "e37750f58494510976ac50e6c2ba1757e2270aca1086239bf02f576d63496201" dependencies = [ - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", "gstreamer-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] name = "gstreamer-sys" -version = "0.9.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc1f154082d01af5718c5f8a8eb4f565a4ea5586ad8833a8fc2c2aa6844b601d" +checksum = "8188ba998999a4a16005c3984812807ff882a87f5f3457c3d5bbbfcbdf631ebd" dependencies = [ - "glib-sys 0.10.1", - "gobject-sys 0.10.0", + "glib-sys", + "gobject-sys", "libc", - "system-deps 1.3.2", + "system-deps", ] [[package]] @@ -938,22 +823,21 @@ dependencies = [ [[package]] name = "muldiv" -version = "0.2.1" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204" +checksum = "b5136edda114182728ccdedb9f5eda882781f35fa6e80cc360af12a8932507f3" [[package]] name = "neolink" version = "0.4.0" dependencies = [ "aes", + "byte-slice-cast", "cfb-mode", "cookie-factory", "crossbeam", "env_logger", "err-derive", - "gio", - "glib 0.14.2", "gstreamer", "gstreamer-app", "gstreamer-rtsp", @@ -1022,9 +906,9 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.3.2" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a" dependencies = [ "autocfg", "num-integer", @@ -1088,15 +972,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131" -[[package]] -name = "proc-macro-crate" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" -dependencies = [ - "toml", -] - [[package]] name = "proc-macro-crate" version = "1.0.0" @@ -1137,12 +1012,6 @@ version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" -[[package]] -name = "proc-macro-nested" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" - [[package]] name = "proc-macro2" version = "1.0.28" @@ -1380,30 +1249,12 @@ dependencies = [ "syn", ] -[[package]] -name = "strum" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" - [[package]] name = "strum" version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aaf86bbcfd1fa9670b7a129f64fc0c9fcbbfe4f1bc4210e9e98fe71ffc12cde2" -[[package]] -name = "strum_macros" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "strum_macros" version = "0.21.1" @@ -1439,21 +1290,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "system-deps" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b" -dependencies = [ - "heck", - "pkg-config", - "strum 0.18.0", - "strum_macros 0.18.0", - "thiserror", - "toml", - "version-compare 0.0.10", -] - [[package]] name = "system-deps" version = "3.2.0" @@ -1465,11 +1301,11 @@ dependencies = [ "heck", "itertools 0.10.1", "pkg-config", - "strum 0.21.0", - "strum_macros 0.21.1", + "strum", + "strum_macros", "thiserror", "toml", - "version-compare 0.0.11", + "version-compare", ] [[package]] @@ -1674,12 +1510,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" -[[package]] -name = "version-compare" -version = "0.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" - [[package]] name = "version-compare" version = "0.0.11" diff --git a/Cargo.toml b/Cargo.toml index 74e4a8d..3a5d3de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,10 @@ cookie-factory = "0.3" crossbeam = "0.7" err-derive = "0.2" env_logger = "*" -glib = ">=0.10.3" -gio = "0.9" -gstreamer = "0.16" -gstreamer-app = "0.16" -gstreamer-rtsp = "0.16" -gstreamer-rtsp-server = { version = "0.16", features = ["v1_12", "v1_14"]} +gstreamer = "0.17" +gstreamer-app = "0.17" +gstreamer-rtsp = "0.17" +gstreamer-rtsp-server = { version = "0.17", features = ["v1_12", "v1_14"]} lazy_static = "1.4" log = { version = "0.4" } itertools = "0.9" @@ -40,3 +38,4 @@ yaserde_derive = "0.3.16" xml-rs = "0.8" validator = "0.10" validator_derive = "0.10" +byte-slice-cast = "1.0.0" diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index f61db6e..32de4cf 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -48,15 +48,28 @@ where Err(e) => return Err(e.into()), }; - if 0 == (&mut rdr) - .take(to_read.get() as u64) - .read_to_end(&mut input)? - { - return Err(std::io::Error::new( - std::io::ErrorKind::UnexpectedEof, - "Read returned 0 bytes", - ) - .into()); + loop { + match (&mut rdr) + .take(to_read.get() as u64) + .read_to_end(&mut input) + { + Ok(0) => { + return Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "Read returned 0 bytes", + ) + .into()); + } + Ok(_) => break, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + // This is a temporaily unavaliable resource + // We should just wait and try again + } + Err(e) => { + log::error!("HERE {:?}", e); + return Err(e.into()); + } + } } } } diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 41c1fa2..588e282 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -10,6 +10,10 @@ pub const MSG_ID_LOGIN: u32 = 1; pub const MSG_ID_LOGOUT: u32 = 2; /// Video and Audio Streams messages have this ID pub const MSG_ID_VIDEO: u32 = 3; +/// TalkAbility messages have this ID +pub const MSG_ID_TALKABILITY: u32 = 10; +/// TalkReset messages have this ID +pub const MSG_ID_TALKRESET: u32 = 11; /// Reboot messages have this ID pub const MSG_ID_REBOOT: u32 = 23; /// Version messages have this ID diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index edfca2b..1aea64a 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -63,6 +63,9 @@ pub struct BcXml { /// Sent as part of the TalkConfig to prepare the camera for audio talk-back #[yaserde(rename = "TalkConfig")] pub talk_config: Option, + /// Revieced as part of the TalkAbility request + #[yaserde(rename = "TalkAbility")] + pub talk_ability: Option, } impl BcXml { @@ -314,13 +317,16 @@ pub struct TalkConfig { #[yaserde(rename = "audioStreamMode")] pub audio_stream_mode: String, /// AudioConfig contans the details of the audio to follow + #[yaserde(rename = "audioConfig")] pub audio_config: AudioConfig, } /// audioConfig xml -#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)] #[yaserde(rename = "audioConfig")] pub struct AudioConfig { + /// Unknown only sent during TalkAbility request from the camera + pub priority: Option, /// Audio type known values are `"adpcm"` /// /// Do not expect camera to support anything else. @@ -339,7 +345,47 @@ pub struct AudioConfig { /// /// Do not expect camera to support anything else #[yaserde(rename = "soundTrack")] - pub soundTrack: String, + pub sound_track: String, +} + +/// TalkAbility xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct TalkAbility { + /// XML Version + #[yaserde(attribute)] + pub version: String, + /// Duplexes known values `"FDX"` + #[yaserde(rename = "duplexList")] + pub duplex_list: Vec, + /// audioStreamModes known values `"followVideoStream"` + #[yaserde(rename = "audioStreamModeList")] + pub audio_stream_mode_list: Vec, + /// AudioConfigs contans the details of the audio to follow + #[yaserde(rename = "audioConfigList")] + pub audio_config_list: Vec, +} + +/// duplexList xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct DuplexList { + /// The supported duplex known values are "FBX" + pub duplex: String, +} + +/// audioStreamModeList xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct AudioStreamModeList { + /// The supported audio stream mode + #[yaserde(rename = "audioStreamMode")] + pub audio_stream_mode: String, +} + +/// audioConfigList xml +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)] +pub struct AudioConfigList { + /// The supported audio configs + #[yaserde(rename = "audioConfig")] + pub audio_config: AudioConfig, } /// Convience function to return the xml version used throughout the library diff --git a/crates/core/src/bc_protocol/errors.rs b/crates/core/src/bc_protocol/errors.rs index d0b532d..ef79712 100644 --- a/crates/core/src/bc_protocol/errors.rs +++ b/crates/core/src/bc_protocol/errors.rs @@ -58,6 +58,10 @@ pub enum Error { #[error(display = "Failed to translate camera address")] AddrResolutionError, + /// Raised non adpcm data is sent to the talk command + #[error(display = "Talk data is not ADPCM")] + UnknownTalkEncoding, + /// A generic catch all error #[error(display = "Other error")] Other(&'static str), diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index 425b265..343e73b 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -1,39 +1,133 @@ use super::{BcCamera, Error, Result, RX_TIMEOUT}; use crate::{bc::model::*, bc::xml::*, bcmedia::model::*}; +use std::io::{BufRead, Error as IoError, ErrorKind, Read}; +use std::sync::mpsc::Receiver; + +type IoResult = std::result::Result; impl BcCamera { + /// + /// Finish Talk + /// + /// The send the talk finish to the camera. + /// + /// It is also sent when the request for talk config returns status code 422 + /// + pub fn talk_stop(&self) -> Result<()> { + let connection = self.connection.as_ref().expect("Must be connected"); + + let sub = connection.subscribe(MSG_ID_TALKRESET)?; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALKRESET, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: None, + }), + }; + + sub.send(msg)?; + let msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + } else { + return Err(Error::UnintelligibleReply { + reply: msg, + why: "The camera did not accept the talk stop command.", + }); + } + + Ok(()) + } + + /// + /// Requests the [`TalkAbility`] xml + /// + pub fn talk_ability(&self) -> Result { + let connection = self + .connection + .as_ref() + .expect("Must be connected to get time"); + let sub_get = connection.subscribe(MSG_ID_TALKABILITY)?; + let get = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALKABILITY, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: None, + }), + }; + + sub_get.send(get)?; + let msg = sub_get.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + talk_ability: Some(talk_ability), + .. + })), + .. + }) = msg.body + { + Ok(talk_ability) + } else { + Err(Error::UnintelligibleReply { + reply: msg, + why: "Expected TalkAbility xml but it was not recieved", + }) + } + } + /// /// Send sound to the camera /// - /// long description + /// The data should be in the format as described in `` xml + /// This method assumes that you have set up the data in the desired format + /// in the `` xml + /// + /// It also checks that it is ADPCM as the code is written to accept only that /// /// # Parameters /// /// * `adpcm` - Data must be adpcm in DVI-4 format /// - /// * `block_size` - is block-align used to encode the adpcm - /// - /// * `sample_rate` - sample rate of the audio + /// * `talk_config` - The talk config that describes the adpcm data /// /// - pub fn talk(&self, adpcm: &[u8], block_size: u16, sample_rate: u16) -> Result<()> { - let connection = self.connection.as_ref().expect("Must be connected to ping"); + pub fn talk(&self, adpcm: &[u8], talk_config: TalkConfig) -> Result<()> { + let connection = self.connection.as_ref().expect("Must be connected"); let sub = connection.subscribe(MSG_ID_TALKCONFIG)?; - let talk_config = TalkConfig { - channel_id: self.channel_id, - duplex: "FDX".to_string(), - audio_stream_mode: "followVideoStream".to_string(), - audio_config: AudioConfig { - audio_type: "adpcm".to_string(), - sample_rate, - sample_precision: 16, - length_per_encoder: block_size * 2, - soundTrack: "mono".to_string(), - }, - ..Default::default() - }; + if &talk_config.audio_config.audio_type != "adpcm" { + return Err(Error::UnknownTalkEncoding); + } + + let block_size = talk_config.audio_config.length_per_encoder / 2; + let sample_rate = talk_config.audio_config.sample_rate; let msg = Bc { meta: BcMeta { @@ -57,7 +151,17 @@ impl BcCamera { }; sub.send(msg)?; - let msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + let mut msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 442, .. + } = msg.meta + { + self.talk_stop()?; + // Retry + sub.send(msg)?; + msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + } if let BcMeta { response_code: 200, .. @@ -66,18 +170,26 @@ impl BcCamera { } else { return Err(Error::UnintelligibleReply { reply: msg, - why: "The camera did not except the TalkConfig xml", + why: + "The camera did not accept the TalkConfig xml. Audio format is likely incorrect", }); } let full_block_size = block_size + 4; // Block size + predictor state let sub = connection.subscribe(MSG_ID_TALK)?; - for bytes in adpcm.chunks(full_block_size as usize) { - let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { - data: bytes.to_vec(), - }); - let bcmedia_adpcm_data = bcmedia_adpcm.serialize(vec![])?; + const BLOCK_PER_PAYLOAD: usize = 4; + const BLOCK_HEADER_SIZE: usize = 4; + const SAMPLES_PER_BYTE: usize = 2; + + for payload_bytes in adpcm.chunks(full_block_size as usize * BLOCK_PER_PAYLOAD) { + let mut payload = vec![]; + for bytes in payload_bytes.chunks(full_block_size as usize) { + let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { + data: bytes.to_vec(), + }); + payload = bcmedia_adpcm.serialize(payload)?; + } let msg = Bc { meta: BcMeta { @@ -94,13 +206,253 @@ impl BcCamera { binary_data: Some(1), ..Default::default() }), - payload: Some(BcPayloads::Binary(bcmedia_adpcm_data)), + payload: Some(BcPayloads::Binary(payload)), }), }; sub.send(msg)?; + + let adpcm_len = payload_bytes.len(); + // There are two samples per byte + // + // To calculate the bytes we subtract the block headers from the len + // + // There is 1 initial sample stored in the block header so we add that in the end + // + let samples_sent = (adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD) + * SAMPLES_PER_BYTE + + BLOCK_PER_PAYLOAD; + + // Time to play the sample in seconds + let play_length = samples_sent as f32 / sample_rate as f32; + std::thread::sleep(std::time::Duration::from_secs_f32(play_length)); } + self.talk_stop()?; + + Ok(()) + } + + /// + /// Send sound to the camera through a channel + /// + /// This is similar to [`talk`] except it uses a channel to receive data + /// + /// The data should be in the format as described in `` xml + /// This method assumes that you have set up the data in the desired format + /// in the `` xml + /// + /// It also checks that it is ADPCM as the code is written to accept only that + /// + /// # Parameters + /// + /// * `adpcm` - Data must be adpcm in DVI-4 format + /// + /// * `talk_config` - The talk config that describes the adpcm data + /// + /// + pub fn talk_stream(&self, rx: Receiver>, talk_config: TalkConfig) -> Result<()> { + let connection = self.connection.as_ref().expect("Must be connected"); + + let sub = connection.subscribe(MSG_ID_TALKCONFIG)?; + + if &talk_config.audio_config.audio_type != "adpcm" { + return Err(Error::UnknownTalkEncoding); + } + + let block_size = talk_config.audio_config.length_per_encoder / 2; + let sample_rate = talk_config.audio_config.sample_rate; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALKCONFIG, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + ..Default::default() + }), + payload: Some(BcPayloads::BcXml(BcXml { + talk_config: Some(talk_config), + ..Default::default() + })), + }), + }; + + sub.send(msg)?; + let mut msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 442, .. + } = msg.meta + { + self.talk_stop()?; + // Retry + sub.send(msg)?; + msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + } + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + } else { + return Err(Error::UnintelligibleReply { + reply: msg, + why: + "The camera did not accept the TalkConfig xml. Audio format is likely incorrect", + }); + } + + let full_block_size = block_size + 4; // Block size + predictor state + let sub = connection.subscribe(MSG_ID_TALK)?; + + const BLOCK_PER_PAYLOAD: usize = 1; + const BLOCK_HEADER_SIZE: usize = 4; + const SAMPLES_PER_BYTE: usize = 2; + + let mut buffered_recv = BufferedStream::from_rx(rx); + + let target_chunks = full_block_size as usize * BLOCK_PER_PAYLOAD; + + let mut payload_bytes = vec![]; + let mut end_of_stream = false; + while !end_of_stream { + while payload_bytes.len() < target_chunks { + let mut buffer = vec![255; target_chunks - payload_bytes.len()]; + if let Ok(read) = buffered_recv.read(&mut buffer) { + payload_bytes.extend(&buffer[..read]); + } else { + // Error should occur if the channel is dropped + // and all bytes are consumed + end_of_stream = true; + } + if end_of_stream { + break; + } + } + + let mut payload = vec![]; + for block_bytes in payload_bytes.chunks(full_block_size as usize) { + let bytes: Vec = block_bytes.to_vec(); + let bcmedia_adpcm = BcMedia::Adpcm(BcMediaAdpcm { data: bytes }); + payload = bcmedia_adpcm.serialize(payload)?; + } + + let adpcm_len = payload_bytes.len(); + + // There are two samples per byte + // + // To calculate the bytes we subtract the block headers from the len + // + // There is 1 initial sample stored in the block header so we add that in the end + // + let samples_sent = if adpcm_len >= BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD { + (adpcm_len - BLOCK_HEADER_SIZE * BLOCK_PER_PAYLOAD) * SAMPLES_PER_BYTE + + BLOCK_PER_PAYLOAD + } else { + // Zero samples in this block + break; + }; + + payload_bytes = vec![]; + + // Time to play the sample in seconds + let play_length = samples_sent as f32 / sample_rate as f32; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_TALK, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: Some(Extension { + channel_id: Some(self.channel_id), + binary_data: Some(1), + ..Default::default() + }), + payload: Some(BcPayloads::Binary(payload)), + }), + }; + + sub.send(msg)?; + + std::thread::sleep(std::time::Duration::from_secs_f32(play_length * 0.95)); + } + + self.talk_stop()?; + Ok(()) } } + +struct BufferedStream { + rx: Receiver>, + buffer: Vec, + consumed: usize, +} + +impl BufferedStream { + pub fn from_rx(rx: Receiver>) -> BufferedStream { + BufferedStream { + rx, + buffer: vec![], + consumed: 0, + } + } +} + +impl Read for BufferedStream { + fn read(&mut self, buf: &mut [u8]) -> IoResult { + let buffer = self.fill_buf()?; + let amt = std::cmp::min(buf.len(), buffer.len()); + + // First check if the amount of bytes we want to read is small: + // `copy_from_slice` will generally expand to a call to `memcpy`, and + // for a single byte the overhead is significant. + if amt == 1 { + buf[0] = buffer[0]; + } else { + buf[..amt].copy_from_slice(&buffer[..amt]); + } + + self.consume(amt); + + Ok(amt) + } +} + +impl BufRead for BufferedStream { + fn fill_buf(&mut self) -> IoResult<&[u8]> { + const CLEAR_CONSUMED_AT: usize = 1024; + // This is a trade off between caching too much dead memory + // and calling the drain method too often + if self.consumed > CLEAR_CONSUMED_AT { + let _ = self.buffer.drain(0..self.consumed).collect::>(); + self.consumed = 0; + } + while self.buffer.len() <= self.consumed { + let data = self + .rx + .recv() + .map_err(|err| IoError::new(ErrorKind::ConnectionReset, err))?; + self.buffer.extend(data); + } + + Ok(&self.buffer.as_slice()[self.consumed..]) + } + + fn consume(&mut self, amt: usize) { + assert!(self.consumed + amt <= self.buffer.len()); + self.consumed += amt; + } +} diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index 6dca3ee..e57bc2b 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -282,10 +282,14 @@ fn bcmedia_aac(buf: &[u8]) -> IResult<&[u8], BcMediaAac> { } fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> { + const BLOCK_HEADER_SIZE: u16 = 4; + let (buf, payload_size) = le_u16(buf)?; let (buf, _payload_size_b) = le_u16(buf)?; let (buf, _magic) = verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA)(buf)?; - let (buf, block_size) = le_u16(buf)?; + // This `sample_block_size` is bigendian for some reason. It is block size - BLOCK_HEADER_SIZE + let (buf, sample_block_size) = be_u16(buf)?; + let block_size = sample_block_size + BLOCK_HEADER_SIZE; let (buf, data_slice) = take!(buf, block_size)?; let pad_size = match payload_size as u32 % PAD_SIZE { 0 => 0, diff --git a/crates/core/src/bcmedia/model.rs b/crates/core/src/bcmedia/model.rs index f657e7d..0127b67 100644 --- a/crates/core/src/bcmedia/model.rs +++ b/crates/core/src/bcmedia/model.rs @@ -166,12 +166,12 @@ pub struct BcMediaAdpcm { // Size of payload after header in bytes exactly the same as before // pub payload_size_b: u16, // more_magic: MAGIC_HEADER_BCMEDIA_ADPCM_DATA - // Adpcm block size in bytes (there are twice this in samples) + // Adpcm sample_block_size in bytes // // These bytes (and the MAGIC_HEADER_BCMEDIA_ADPCM_DATA) are included as // part of the payload_size. It may be more prudent to sealise them to // another structure. - // pub block_size: u16, + // pub sample_block_size: u16, /// The raw adpcm data in DVI-4 layout. /// /// One `data` should contain 4 bytes of the adpcm predictor state then one block diff --git a/crates/core/src/bcmedia/ser.rs b/crates/core/src/bcmedia/ser.rs index e5bb801..9173589 100644 --- a/crates/core/src/bcmedia/ser.rs +++ b/crates/core/src/bcmedia/ser.rs @@ -183,6 +183,6 @@ fn bcmedia_adpcm(payload: &BcMediaAdpcm) -> impl SerializeFn { le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size le_u16((payload.data.len() + 4) as u16), // Payload + 2 byte magic + 2byte block size le_u16(MAGIC_HEADER_BCMEDIA_ADPCM_DATA), // magic - le_u16((payload.data.len()) as u16), // block size + predictor state size (4bytes) + be_u16((payload.data.len() - 4) as u16), // sample_block_size = len - predictor state size (4bytes) )) } diff --git a/src/cmdline.rs b/src/cmdline.rs index b584623..80a65a0 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -13,4 +13,5 @@ pub enum Command { Rtsp(super::rtsp::Opt), StatusLight(super::statusled::Opt), Reboot(super::reboot::Opt), + Talk(super::talk::Opt), } diff --git a/src/errors.rs b/src/errors.rs index 10b62cf..009115e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -9,4 +9,6 @@ pub enum Error { StatusLight(#[error(source)] super::statusled::Error), #[error(display = "Reboot Error")] Reboot(#[error(source)] super::reboot::Error), + #[error(display = "Talk Error")] + Talk(#[error(source)] super::talk::Error), } diff --git a/src/main.rs b/src/main.rs index 78b9b5c..475475b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod errors; mod reboot; mod rtsp; mod statusled; +mod talk; use cmdline::{Command, Opt}; use errors::Error; @@ -41,6 +42,9 @@ fn main() -> Result<(), Error> { Command::Reboot(opts) => { reboot::main(opts)?; } + Command::Talk(opts) => { + talk::main(opts)?; + } } Ok(()) diff --git a/src/rtsp/errors.rs b/src/rtsp/errors.rs index 162e91f..19d0c68 100644 --- a/src/rtsp/errors.rs +++ b/src/rtsp/errors.rs @@ -17,7 +17,7 @@ pub enum Error { /// Raised when the config file fails validataion #[error(display = "Validation error")] Validation(#[error(source)] validator::ValidationErrors), - /// Raised when there is an ADPCM decoding error - #[error(display = "ADPCM Decoding Error")] - AdpcmDecoding(&'static str), + // Raised when there is an ADPCM decoding error + // #[error(display = "ADPCM Decoding Error")] + // AdpcmDecoding(&'static str), } diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 7b69022..2955c07 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -1,14 +1,15 @@ //! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it //! data using an ordinary std::io::Write interface. pub(crate) use self::maybe_app_src::MaybeAppSrc; -use super::adpcm::adpcm_to_pcm; -use super::errors::Error; +// use super::adpcm::adpcm_to_pcm; +// use super::errors::Error; use gstreamer::prelude::Cast; use gstreamer::{Bin, Structure}; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; -use gio::{TlsAuthenticationMode, TlsCertificate}; use gstreamer_rtsp::RTSPAuthMethod; +pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate}; +use gstreamer_rtsp_server::glib; use gstreamer_rtsp_server::prelude::*; use gstreamer_rtsp_server::{ RTSPAuth, RTSPMediaFactory, RTSPServer as GstRTSPServer, RTSPToken, @@ -79,15 +80,8 @@ impl StreamOutput for GstOutputs { self.audsrc.write_all(&payload.data)?; } BcMedia::Adpcm(payload) => { - self.set_format(Some(StreamFormat::Adpcm((payload.data.len() - 4) as u16))); - let pcm = adpcm_to_pcm(&payload.data).map_err(|e| { - if let Error::AdpcmDecoding(msg) = e { - neolink_core::Error::OtherString(format!("ADPCM decoding error: {}", msg)) - } else { - neolink_core::Error::Other("Generic error during ADPCM decoding") - } - })?; - self.audsrc.write_all(&pcm)?; + self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16))); + self.audsrc.write_all(&payload.data)?; } _ => { //Ignore other BcMedia like InfoV1 and InfoV2 @@ -181,7 +175,7 @@ impl RtspServer { ) -> Result { let mounts = self .server - .get_mount_points() + .mount_points() .expect("The server should have mountpoints"); // Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is @@ -209,19 +203,19 @@ impl RtspServer { factory.connect_media_configure(move |_factory, media| { debug!("RTSP: media was configured"); let bin = media - .get_element() + .element() .expect("Media should have an element") .dynamic_cast::() .expect("Media source's element should be a bin"); let app_src = bin - .get_by_name_recurse_up("vidsrc") + .by_name_recurse_up("vidsrc") .expect("write_src must be present in created bin") .dynamic_cast::() .expect("Source element is expected to be an appsrc!"); let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so let app_src_aud = bin - .get_by_name_recurse_up("audsrc") + .by_name_recurse_up("audsrc") .expect("write_src must be present in created bin") .dynamic_cast::() .expect("Source element is expected to be an appsrc!"); @@ -272,7 +266,7 @@ impl RtspServer { } pub(crate) fn set_credentials(&self, credentials: &[(&str, &str)]) -> Result<()> { - let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new); + let auth = self.server.auth().unwrap_or_else(RTSPAuth::new); auth.set_supported_methods(RTSPAuthMethod::Basic); let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]); @@ -296,7 +290,7 @@ impl RtspServer { client_auth: TlsAuthenticationMode, ) -> Result<()> { debug!("Setting up TLS using {}", cert_file); - let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new); + let auth = self.server.auth().unwrap_or_else(RTSPAuth::new); // We seperate reading the file and changing to a PEM so that we get different error messages. let cert_contents = fs::read_to_string(cert_file).expect("TLS file not found"); @@ -312,7 +306,7 @@ impl RtspServer { self.server.set_address(bind_addr); self.server.set_service(&format!("{}", bind_port)); // Attach server to default Glib context - self.server.attach(None); + let _ = self.server.attach(None); // Run the Glib main loop. let main_loop = glib::MainLoop::new(None, false); diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 210bdc2..120b614 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -28,7 +28,6 @@ /// neolink rtsp --config=config.toml /// ``` /// -use gio::TlsAuthenticationMode; use log::*; use neolink_core::bc_protocol::BcCamera; use neolink_core::Never; @@ -38,7 +37,7 @@ use std::sync::Arc; use std::time::Duration; use validator::Validate; -mod adpcm; +// mod adpcm; /// The command line parameters for this subcommand mod cmdline; mod config; @@ -49,7 +48,7 @@ mod gst; pub(crate) use cmdline::Opt; use config::{CameraConfig, Config, UserConfig}; pub(crate) use errors::Error; -use gst::{GstOutputs, RtspServer}; +use gst::{GstOutputs, RtspServer, TlsAuthenticationMode}; /// Entry point for the rtsp subcommand /// @@ -141,7 +140,7 @@ fn camera_loop( return Err(cam_err.err.into()); } _ => error!( - "Error streaming from camera {}, will retry in {}s: {}", + "Error streaming from camera {}, will retry in {}s: {:?}", camera_config.name, current_backoff.as_secs(), cam_err.err diff --git a/src/talk/cmdline.rs b/src/talk/cmdline.rs new file mode 100644 index 0000000..807d677 --- /dev/null +++ b/src/talk/cmdline.rs @@ -0,0 +1,27 @@ +use std::path::PathBuf; +use structopt::StructOpt; + +/// The talk command will send audio for the camera to say +/// +/// This data should be encoded in a way that gstreamer can understand. +/// This should be ok with most common formats. +/// +/// `gst-launch` can be used to prepare this data +#[derive(StructOpt, Debug)] +pub struct Opt { + /// The path to the config file + #[structopt(short, long, parse(from_os_str))] + pub config: PathBuf, + /// The name of the camera to change the lights of. Must be a name in the config + pub camera: String, + /// The path to the audio file. + #[structopt(short, long, parse(from_os_str), conflicts_with = "microphone")] + pub file_path: Option, + /// Use the microphone as the source. Which microphone depends + /// on [gstraemer](https://gstreamer.freedesktop.org/documentation/autodetect/autoaudiosrc.html?gi-language=c#autoaudiosrc-page) + #[structopt(short, long, conflicts_with = "file_path")] + pub microphone: bool, + /// Use to change the volume of the input + #[structopt(short, long, default_value = "1.0")] + pub volume: f32, +} diff --git a/src/talk/config.rs b/src/talk/config.rs new file mode 100644 index 0000000..de60ef2 --- /dev/null +++ b/src/talk/config.rs @@ -0,0 +1,29 @@ +use serde::Deserialize; +use std::clone::Clone; +use validator::Validate; +use validator_derive::Validate; + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct Config { + #[validate] + pub(crate) cameras: Vec, +} + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct CameraConfig { + pub(crate) name: String, + + #[serde(rename = "address")] + pub(crate) camera_addr: String, + + pub(crate) username: String, + pub(crate) password: Option, + + #[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))] + #[serde(default = "default_channel_id")] + pub(crate) channel_id: u8, +} + +fn default_channel_id() -> u8 { + 0 +} diff --git a/src/talk/errors.rs b/src/talk/errors.rs new file mode 100644 index 0000000..af0f3b0 --- /dev/null +++ b/src/talk/errors.rs @@ -0,0 +1,42 @@ +use err_derive::Error; +use gstreamer::glib; + +/// The main error for the status-light subcommand +#[derive(Debug, Error)] +#[allow(clippy::large_enum_variant)] +pub enum Error { + /// Raised when the config file fails to deserlize + #[error(display = "Configuration parsing error")] + Config(#[error(source)] toml::de::Error), + /// Raised when `neolink_core` raises an error + #[error(display = "Communication error")] + Protocol(#[error(source)] neolink_core::Error), + /// Raised when there is an IO error such as unable to find + /// config file + #[error(display = "I/O error")] + Io(#[error(source)] std::io::Error), + /// Raised when the config file fails validataion + #[error(display = "Validation error")] + Validation(#[error(source)] validator::ValidationErrors), + /// Raised when gstreamer fails to init + #[error(display = "Gstreamer init failed")] + GstreamerInit(#[error(source)] glib::Error), + /// Raised when a needed part of the gsteamer pipeline is not found + /// + /// Usually this means missing pluings + #[error(display = "Gstreamer element not supported. Check your gstreamer plugins")] + GstreamerElement(gstreamer::Element), + /// Raised where there is an issue starting a audio playback in gstraemer + #[error(display = "Gstreamer Unable to play the file")] + GstreamerPlayback(#[error(source)] gstreamer::StateChangeError), + /// Generic gstreamer error + #[error(display = "Gstreamer raised an error")] + Gstreamer { + error: String, + debug: Option, + }, + + /// Raised when talk back is unsupported + #[error(display = "This camera does not support talkback")] + TalkUnsupported, +} diff --git a/src/talk/gst.rs b/src/talk/gst.rs new file mode 100644 index 0000000..a469295 --- /dev/null +++ b/src/talk/gst.rs @@ -0,0 +1,191 @@ +use gstreamer::{ + element_error, parse_launch, prelude::*, Caps, ClockTime, FlowError, FlowSuccess, MessageView, + Pipeline, ResourceError, State, +}; +use gstreamer_app::{AppSink, AppSinkCallbacks}; +use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; + +use super::errors::Error; +use byte_slice_cast::*; + +pub(super) fn file_input( + filename: &str, + volume: f32, + block_align: u16, + sample_rate: u16, +) -> Result>, Error> { + let pipeline = create_pipeline( + &format!("filesrc location={}", filename), + volume, + block_align, + sample_rate, + )?; + input(pipeline) +} + +pub(super) fn mic_input( + volume: f32, + block_align: u16, + sample_rate: u16, +) -> Result>, Error> { + let pipeline = create_pipeline("autoaudiosrc", volume, block_align, sample_rate)?; + input(pipeline) +} + +fn input(pipeline: Pipeline) -> Result>, Error> { + let appsink = get_sink(&pipeline)?; + let (tx, rx) = sync_channel(30); + + set_data_channel(&appsink, tx); + + std::thread::spawn(move || { + let _ = start_pipeline(pipeline); + }); + + Ok(rx) +} + +fn start_pipeline(pipeline: Pipeline) -> Result<(), Error> { + pipeline.set_state(State::Playing)?; + + let bus = pipeline + .bus() + .expect("Pipeline without bus. Shouldn't happen!"); + + for msg in bus.iter_timed(ClockTime::NONE) { + match msg.view() { + MessageView::Eos(..) => break, + MessageView::Error(err) => { + pipeline.set_state(State::Null)?; + log::warn!( + "{:?}", + Error::Gstreamer { + error: err.error().to_string(), + debug: err.debug(), + } + ); + } + _ => (), + } + } + + pipeline.set_state(State::Null)?; + + Ok(()) +} + +fn get_sink(pipeline: &Pipeline) -> Result { + let sink = pipeline + .by_name("thesink") + .expect("There shoud be a `thesink`"); + sink.dynamic_cast::() + .map_err(Error::GstreamerElement) +} + +fn set_data_channel(appsink: &AppSink, tx: SyncSender>) { + // Getting data out of the appsink is done by setting callbacks on it. + // The appsink will then call those handlers, as soon as data is available. + appsink.set_callbacks( + AppSinkCallbacks::builder() + // Add a handler to the "new-sample" signal. + .new_sample(move |appsink| { + // Pull the sample in question out of the appsink's buffer. + let sample = appsink.pull_sample().map_err(|_| FlowError::Eos)?; + let buffer = sample.buffer().ok_or_else(|| { + element_error!( + appsink, + ResourceError::Failed, + ("Failed to get buffer from appsink") + ); + + FlowError::Error + })?; + + // At this point, buffer is only a reference to an existing memory region somewhere. + // When we want to access its content, we have to map it while requesting the required + // mode of access (read, read/write). + // This type of abstraction is necessary, because the buffer in question might not be + // on the machine's main memory itself, but rather in the GPU's memory. + // So mapping the buffer makes the underlying memory region accessible to us. + // See: https://gstreamer.freedesktop.org/documentation/plugin-development/advanced/allocation.html + let map = buffer.map_readable().map_err(|_| { + element_error!( + appsink, + ResourceError::Failed, + ("Failed to map buffer readable") + ); + + FlowError::Error + })?; + + // We know what format the data in the memory region has, since we requested + // it by setting the appsink's caps. So what we do here is interpret the + // memory region we mapped as an array of signed 16 bit integers. + let samples = map.as_slice_of::().map_err(|_| { + element_error!( + appsink, + ResourceError::Failed, + ("Failed to interprete buffer as u8 ADPCM") + ); + + FlowError::Error + })?; + + // Ready! + let _ = tx.send(samples.to_vec()); + + Ok(FlowSuccess::Ok) + }) + .build(), + ); +} + +fn create_pipeline( + source: &str, + volume: f32, + block_align: u16, + sample_rate: u16, +) -> Result { + gstreamer::init()?; + + let launch_str = format!( + "{} \ + ! decodebin \ + ! audioconvert \ + ! audioresample \ + ! audio/x-raw,rate={},channels=1 \ + ! volume volume={:.2} \ + ! queue \ + ! adpcmenc blockalign={} layout=dvi \ + ! appsink name=thesink", + source, sample_rate, volume, block_align + ); + + log::info!("{}", launch_str); + + // Parse the pipeline we want to probe from a static in-line string. + // Here we give our audiotestsrc a name, so we can retrieve that element + // from the resulting pipeline. + let pipeline = parse_launch(&launch_str)?; + let pipeline = pipeline + .dynamic_cast::() + .map_err(Error::GstreamerElement)?; + + let appsink = get_sink(&pipeline)?; + + // Tell the appsink what format we want. It will then be the audiotestsrc's job to + // provide the format we request. + // This can be set after linking the two objects, because format negotiation between + // both elements will happen during pre-rolling of the pipeline. + appsink.set_caps(Some(&Caps::new_simple( + "audio/x-adpcm", + &[ + ("layout", &"dvi"), + ("block_align", &(block_align as i32)), + ("channels", &(1i32)), + ("rate", &(sample_rate as i32)), + ], + ))); + + Ok(pipeline) +} diff --git a/src/talk/mod.rs b/src/talk/mod.rs new file mode 100644 index 0000000..ef1131e --- /dev/null +++ b/src/talk/mod.rs @@ -0,0 +1,104 @@ +/// +/// # Neolink Talk +/// +/// This module can be used to send adpcm data for the camera to play +/// +/// The adpcm data needs to be in DVI-4 layout +/// +/// # Usage +/// +/// ```bash +/// neolink status-light --config=config.toml --adpcm-file=data.adpcm --sample-rate=16000 --block-size=512 CameraName +/// ``` +/// +use log::*; +use neolink_core::{bc::xml::TalkConfig, bc_protocol::BcCamera}; +use std::fs; +use validator::Validate; + +mod cmdline; +mod config; +mod errors; +mod gst; + +pub(crate) use cmdline::Opt; +use config::Config; +pub(crate) use errors::Error; + +/// Entry point for the talk subcommand +/// +/// Opt is the command line options +pub fn main(opt: Opt) -> Result<(), Error> { + let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + + config.validate()?; + + let mut cam_found = false; + for camera_config in &config.cameras { + if opt.camera == camera_config.name { + cam_found = true; + info!( + "{}: Connecting to camera at {}", + camera_config.name, camera_config.camera_addr + ); + + let mut camera = + BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?; + + info!("{}: Logging in", camera_config.name); + camera.login(&camera_config.username, camera_config.password.as_deref())?; + + info!("{}: Connected and logged in", camera_config.name); + + let talk_ability = camera.talk_ability()?; + if talk_ability.duplex_list.is_empty() + || talk_ability.audio_stream_mode_list.is_empty() + || talk_ability.audio_config_list.is_empty() + { + return Err(Error::TalkUnsupported); + } + + // Just copy that data from the first talk ability in the config have never seen more + // than one ability + let config = 0; + + let talk_config = TalkConfig { + channel_id: camera_config.channel_id, + duplex: talk_ability.duplex_list[config].duplex.clone(), + audio_stream_mode: talk_ability.audio_stream_mode_list[config] + .audio_stream_mode + .clone(), + audio_config: talk_ability.audio_config_list[config].audio_config.clone(), + ..Default::default() + }; + + let block_size = (talk_config.audio_config.length_per_encoder / 2) + 4; + let sample_rate = talk_config.audio_config.sample_rate; + if block_size == 0 || sample_rate == 0 { + return Err(Error::TalkUnsupported); + } + + let rx = match (&opt.file_path, &opt.microphone) { + (Some(path), false) => gst::file_input( + path.to_str().expect("File path not UTF8 complient"), + opt.volume, + block_size, + sample_rate, + )?, + (None, true) => gst::mic_input(opt.volume, block_size, sample_rate)?, + _ => unreachable!(), + }; + + camera.talk_stream(rx, talk_config)?; + } + } + + if !cam_found { + error!( + "No camera with the name {} was found in the config", + opt.camera + ); + } + + Ok(()) +}