diff --git a/Cargo.lock b/Cargo.lock index c59b960..6df3499 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -323,7 +323,7 @@ dependencies = [ "base64ct", "clap", "cool_asserts", - "serde 1.0.193", + "serde 1.0.209", "sha2", "sqlx", "thiserror", @@ -474,7 +474,7 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" dependencies = [ - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -608,7 +608,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", - "serde 1.0.193", + "serde 1.0.209", "wasm-bindgen", "windows-targets 0.48.5", ] @@ -622,7 +622,7 @@ dependencies = [ "chrono", "chrono-tz-build", "phf 0.11.2", - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -723,7 +723,7 @@ version = "0.1.0" dependencies = [ "config-derive", "cool_asserts", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "thiserror", ] @@ -972,7 +972,7 @@ dependencies = [ "libadwaita", "memorycache", "reqwest", - "serde 1.0.193", + "serde 1.0.209", "serde_derive", "serde_json", "tokio", @@ -1041,7 +1041,7 @@ checksum = "2517b0555262aeeda0d107a40ecfbbcf185921180ffb4acf316ebe0887467e26" dependencies = [ "generic-array 0.11.2", "num-traits", - "serde 1.0.193", + "serde 1.0.209", "typenum", ] @@ -1053,7 +1053,7 @@ checksum = "a0b0a86c5d31c93238ff4b694fa31f3acdf67440770dc314c57d90e433914397" dependencies = [ "generic-array 0.14.7", "num-traits", - "serde 1.0.193", + "serde 1.0.209", "typenum", ] @@ -1080,7 +1080,7 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" dependencies = [ - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -1119,7 +1119,7 @@ dependencies = [ "chrono", "chrono-tz", "dimensioned 0.7.0", - "serde 1.0.193", + "serde 1.0.209", "serde_derive", "serde_json", "tempfile", @@ -1274,7 +1274,7 @@ dependencies = [ "mime 0.3.17", "mime_guess 2.0.4", "pretty_env_logger", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "sha2", "tempdir", @@ -1475,7 +1475,7 @@ dependencies = [ "chrono-tz", "dimensioned 0.8.0", "emseries", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "tempfile", ] @@ -1875,7 +1875,7 @@ dependencies = [ "glib-build-tools 0.16.3", "gtk4", "libadwaita", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "tokio", ] @@ -1885,6 +1885,10 @@ name = "gm-dash" version = "0.1.0" dependencies = [ "pipewire", + "serde 1.0.209", + "serde_json", + "tokio", + "warp", ] [[package]] @@ -2097,9 +2101,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.3.3" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "hex" @@ -2319,7 +2323,7 @@ name = "ifc" version = "0.1.0" dependencies = [ "chrono", - "serde 1.0.193", + "serde 1.0.209", "thiserror", ] @@ -2731,7 +2735,7 @@ version = "0.1.0" dependencies = [ "chrono", "futures", - "serde 1.0.193", + "serde 1.0.209", "serde_derive", "tokio", ] @@ -2810,13 +2814,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.10" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ + "hermit-abi", "libc", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -3091,7 +3096,7 @@ dependencies = [ "cool_asserts", "grid", "nary_tree", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "sgf", "thiserror", @@ -3851,7 +3856,7 @@ dependencies = [ "once_cell", "percent-encoding 2.3.1", "pin-project-lite", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "serde_urlencoded", "system-configuration", @@ -4019,15 +4024,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rustls-pemfile" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" -dependencies = [ - "base64 0.21.5", -] - [[package]] name = "rusty-fork" version = "0.3.0" @@ -4154,18 +4150,18 @@ checksum = "34b623917345a631dc9608d5194cc206b3fe6c3554cd1c75b937e55e285254af" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ "proc-macro2", "quote", @@ -4174,13 +4170,14 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" dependencies = [ "itoa", + "memchr", "ryu", - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -4189,7 +4186,7 @@ version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" dependencies = [ - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -4201,7 +4198,7 @@ dependencies = [ "form_urlencoded", "itoa", "ryu", - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -4212,7 +4209,7 @@ dependencies = [ "cool_asserts", "nary_tree", "nom", - "serde 1.0.193", + "serde 1.0.209", "thiserror", "typeshare", "uuid 0.8.2", @@ -4415,7 +4412,7 @@ dependencies = [ "once_cell", "paste", "percent-encoding 2.3.1", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "sha2", "smallvec", @@ -4454,7 +4451,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "sha2", "sqlx-core", @@ -4497,7 +4494,7 @@ dependencies = [ "percent-encoding 2.3.1", "rand 0.8.5", "rsa", - "serde 1.0.193", + "serde 1.0.209", "sha1", "sha2", "smallvec", @@ -4535,7 +4532,7 @@ dependencies = [ "memchr", "once_cell", "rand 0.8.5", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "sha1", "sha2", @@ -4563,7 +4560,7 @@ dependencies = [ "libsqlite3-sys", "log 0.4.20", "percent-encoding 2.3.1", - "serde 1.0.193", + "serde 1.0.209", "sqlx-core", "tracing", "url 2.5.0", @@ -4755,7 +4752,7 @@ dependencies = [ "deranged", "itoa", "powerfmt", - "serde 1.0.193", + "serde 1.0.209", "time-core", "time-macros", ] @@ -4809,28 +4806,27 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.35.1" +version = "1.39.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104" +checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2 0.5.5", "tokio-macros", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", @@ -4860,9 +4856,9 @@ dependencies = [ [[package]] name = "tokio-tungstenite" -version = "0.20.1" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "212d5dcb2a1ce06d81107c3d0ffa3121fe974b73f068c8282cb1c32328113b6c" +checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" dependencies = [ "futures-util", "log 0.4.20", @@ -4890,7 +4886,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" dependencies = [ - "serde 1.0.193", + "serde 1.0.209", "serde_spanned", "toml_datetime", "toml_edit 0.20.2", @@ -4902,7 +4898,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" dependencies = [ - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -4923,7 +4919,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" dependencies = [ "indexmap", - "serde 1.0.193", + "serde 1.0.209", "serde_spanned", "toml_datetime", "winnow", @@ -4985,14 +4981,14 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tungstenite" -version = "0.20.1" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" +checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" dependencies = [ "byteorder", "bytes", "data-encoding", - "http 0.2.11", + "http 1.0.0", "httparse", "log 0.4.20", "rand 0.8.5", @@ -5039,7 +5035,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f44d1a2f454cb35fbe05b218c410792697e76bd868f48d3a418f2cd1a7d527d6" dependencies = [ "chrono", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "typeshare-annotation", ] @@ -5207,7 +5203,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ "getrandom", - "serde 1.0.193", + "serde 1.0.209", ] [[package]] @@ -5261,7 +5257,7 @@ version = "0.1.0" dependencies = [ "authdb", "http 1.0.0", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "tokio", "warp", @@ -5308,9 +5304,9 @@ dependencies = [ [[package]] name = "warp" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e92e22e03ff1230c03a1a8ee37d2f89cd489e2e541b7550d6afad96faed169" +checksum = "4378d202ff965b011c64817db11d5829506d3404edeadb61f190d111da3f231c" dependencies = [ "bytes", "futures-channel", @@ -5324,13 +5320,11 @@ dependencies = [ "multer", "percent-encoding 2.3.1", "pin-project", - "rustls-pemfile", "scoped-tls", - "serde 1.0.193", + "serde 1.0.209", "serde_json", "serde_urlencoded", "tokio", - "tokio-stream", "tokio-tungstenite", "tokio-util", "tower-service", diff --git a/gm-dash/server/Cargo.toml b/gm-dash/server/Cargo.toml index 53a078a..f07ad89 100644 --- a/gm-dash/server/Cargo.toml +++ b/gm-dash/server/Cargo.toml @@ -7,3 +7,7 @@ edition = "2021" [dependencies] pipewire = "0.8.0" +serde = { version = "1.0.209", features = ["alloc", "derive"] } +serde_json = "1.0.127" +tokio = { version = "1.39.3", features = ["full"] } +warp = "0.3.7" diff --git a/gm-dash/server/src/main.rs b/gm-dash/server/src/main.rs new file mode 100644 index 0000000..c80aec8 --- /dev/null +++ b/gm-dash/server/src/main.rs @@ -0,0 +1,109 @@ +use pipewire::{context::Context, main_loop::MainLoop}; +use std::{ + net::{Ipv6Addr, SocketAddrV6}, + sync::{Arc, RwLock}, +}; +use tokio::task::spawn_blocking; +use warp::{serve, Filter}; + +struct State_ { + device_list: Vec, +} + +#[derive(Clone)] +struct State { + internal: Arc>, +} + +impl State { + fn new() -> State { + let internal = State_ { + device_list: vec![], + }; + State { + internal: Arc::new(RwLock::new(internal)), + } + } + + fn add_audio(&self, device: String) { + let mut st = self.internal.write().unwrap(); + (*st).device_list.push(device); + } + + fn audio_devices(&self) -> Vec { + let st = self.internal.read().unwrap(); + (*st).device_list.clone() + } +} + +impl Default for State { + fn default() -> State { + State::new() + } +} + +async fn server_main(state: State) { + let localhost: Ipv6Addr = "::1".parse().unwrap(); + let server_addr = SocketAddrV6::new(localhost, 3001, 0, 0); + + let root = warp::path!().map(|| "ok".to_string()); + let list_output_devices = warp::path!("output_devices").map({ + let state = state.clone(); + move || { + let devices = state.audio_devices(); + serde_json::to_string(&devices).unwrap() + } + }); + + let routes = root.or(list_output_devices); + + serve(routes).run(server_addr).await; +} + +fn handle_add_audio_device(state: State, props: &pipewire::spa::utils::dict::DictRef) +{ + if props.get("media.class") == Some("Audio/Sink") { + if let Some(device_name) = props.get("node.description") { + state.add_audio(device_name.to_owned()); + } + } +} + +fn pipewire_loop(state: State) -> Result<(), Box> { + let mainloop = MainLoop::new(None)?; + let context = Context::new(&mainloop)?; + let core = context.connect(None)?; + let registry = core.get_registry()?; + + let _listener = registry + .add_listener_local() + .global({ + let state = state.clone(); + move |global_data| { + if let Some(props) = global_data.props { + handle_add_audio_device(state.clone(), props); + } + } + }) + .register(); + + mainloop.run(); + + Ok(()) +} + +fn pipewire_main(state: State) { + pipewire_loop(state).expect("pipewire should not error"); +} + +#[tokio::main] +async fn main() { + let state = State::default(); + + spawn_blocking({ + let state = state.clone(); + move || pipewire_main(state) + }); + + server_main(state.clone()).await; +}