Set up a server that watches pipewire and adds devices as they appear #247

Merged
savanni merged 2 commits from gm-dash__pipewire into main 2024-08-26 14:55:28 +00:00
5 changed files with 373 additions and 72 deletions

303
Cargo.lock generated
View File

@ -66,6 +66,16 @@ dependencies = [
"libc",
]
[[package]]
name = "annotate-snippets"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccaf7e9dfbb6ab22c82e473cd1a8a7bd313c19a5b7e40970f3d89ef5a5c9e81e"
dependencies = [
"unicode-width",
"yansi-term",
]
[[package]]
name = "anstream"
version = "0.6.5"
@ -313,7 +323,7 @@ dependencies = [
"base64ct",
"clap",
"cool_asserts",
"serde 1.0.193",
"serde 1.0.209",
"sha2",
"sqlx",
"thiserror",
@ -404,6 +414,27 @@ dependencies = [
"rp-pico",
]
[[package]]
name = "bindgen"
version = "0.69.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0"
dependencies = [
"annotate-snippets",
"bitflags 2.4.1",
"cexpr",
"clang-sys",
"itertools 0.12.0",
"lazy_static",
"lazycell",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn 2.0.48",
]
[[package]]
name = "bit-set"
version = "0.5.3"
@ -443,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]]
@ -535,6 +566,15 @@ dependencies = [
"libc",
]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]]
name = "cfg-expr"
version = "0.15.5"
@ -568,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",
]
@ -582,7 +622,7 @@ dependencies = [
"chrono",
"chrono-tz-build",
"phf 0.11.2",
"serde 1.0.193",
"serde 1.0.209",
]
[[package]]
@ -596,6 +636,17 @@ dependencies = [
"phf_codegen 0.11.2",
]
[[package]]
name = "clang-sys"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "clap"
version = "4.4.11"
@ -672,7 +723,7 @@ version = "0.1.0"
dependencies = [
"config-derive",
"cool_asserts",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"thiserror",
]
@ -691,6 +742,15 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "convert_case"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "cookie"
version = "0.17.0"
@ -701,6 +761,15 @@ dependencies = [
"version_check 0.9.4",
]
[[package]]
name = "cookie-factory"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9885fa71e26b8ab7855e2ec7cae6e9b380edff76cd052e07c683a0319d51b3a2"
dependencies = [
"futures",
]
[[package]]
name = "cool_asserts"
version = "2.0.3"
@ -903,7 +972,7 @@ dependencies = [
"libadwaita",
"memorycache",
"reqwest",
"serde 1.0.193",
"serde 1.0.209",
"serde_derive",
"serde_json",
"tokio",
@ -972,7 +1041,7 @@ checksum = "2517b0555262aeeda0d107a40ecfbbcf185921180ffb4acf316ebe0887467e26"
dependencies = [
"generic-array 0.11.2",
"num-traits",
"serde 1.0.193",
"serde 1.0.209",
"typenum",
]
@ -984,7 +1053,7 @@ checksum = "a0b0a86c5d31c93238ff4b694fa31f3acdf67440770dc314c57d90e433914397"
dependencies = [
"generic-array 0.14.7",
"num-traits",
"serde 1.0.193",
"serde 1.0.209",
"typenum",
]
@ -1011,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]]
@ -1050,7 +1119,7 @@ dependencies = [
"chrono",
"chrono-tz",
"dimensioned 0.7.0",
"serde 1.0.193",
"serde 1.0.209",
"serde_derive",
"serde_json",
"tempfile",
@ -1205,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",
@ -1406,7 +1475,7 @@ dependencies = [
"chrono-tz",
"dimensioned 0.8.0",
"emseries",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"tempfile",
]
@ -1775,6 +1844,12 @@ dependencies = [
"system-deps",
]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "gloo-timers"
version = "0.2.6"
@ -1800,7 +1875,7 @@ dependencies = [
"glib-build-tools 0.16.3",
"gtk4",
"libadwaita",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"tokio",
]
@ -1808,6 +1883,13 @@ dependencies = [
[[package]]
name = "gm-dash"
version = "0.1.0"
dependencies = [
"pipewire",
"serde 1.0.209",
"serde_json",
"tokio",
"warp",
]
[[package]]
name = "gobject-sys"
@ -2019,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"
@ -2241,7 +2323,7 @@ name = "ifc"
version = "0.1.0"
dependencies = [
"chrono",
"serde 1.0.193",
"serde 1.0.209",
"thiserror",
]
@ -2446,6 +2528,12 @@ dependencies = [
"spin 0.5.2",
]
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "lebe"
version = "0.5.2"
@ -2490,12 +2578,50 @@ version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "libloading"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [
"cfg-if",
"windows-targets 0.52.0",
]
[[package]]
name = "libm"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]]
name = "libspa"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65f3a4b81b2a2d8c7f300643676202debd1b7c929dbf5c9bb89402ea11d19810"
dependencies = [
"bitflags 2.4.1",
"cc",
"convert_case",
"cookie-factory",
"libc",
"libspa-sys",
"nix",
"nom",
"system-deps",
]
[[package]]
name = "libspa-sys"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf0d9716420364790e85cbb9d3ac2c950bde16a7dd36f3209b7dfdfc4a24d01f"
dependencies = [
"bindgen",
"cc",
"system-deps",
]
[[package]]
name = "libsqlite3-sys"
version = "0.27.0"
@ -2609,7 +2735,7 @@ version = "0.1.0"
dependencies = [
"chrono",
"futures",
"serde 1.0.193",
"serde 1.0.209",
"serde_derive",
"tokio",
]
@ -2688,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]]
@ -2969,7 +3096,7 @@ dependencies = [
"cool_asserts",
"grid",
"nary_tree",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"sgf",
"thiserror",
@ -3223,6 +3350,34 @@ dependencies = [
"futures-io",
]
[[package]]
name = "pipewire"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08e645ba5c45109106d56610b3ee60eb13a6f2beb8b74f8dc8186cf261788dda"
dependencies = [
"anyhow",
"bitflags 2.4.1",
"libc",
"libspa",
"libspa-sys",
"nix",
"once_cell",
"pipewire-sys",
"thiserror",
]
[[package]]
name = "pipewire-sys"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "849e188f90b1dda88fe2bfe1ad31fe5f158af2c98f80fb5d13726c44f3f01112"
dependencies = [
"bindgen",
"libspa-sys",
"system-deps",
]
[[package]]
name = "pkcs1"
version = "0.7.5"
@ -3701,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",
@ -3869,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"
@ -4004,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",
@ -4024,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]]
@ -4039,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]]
@ -4051,7 +4198,7 @@ dependencies = [
"form_urlencoded",
"itoa",
"ryu",
"serde 1.0.193",
"serde 1.0.209",
]
[[package]]
@ -4062,7 +4209,7 @@ dependencies = [
"cool_asserts",
"nary_tree",
"nom",
"serde 1.0.193",
"serde 1.0.209",
"thiserror",
"typeshare",
"uuid 0.8.2",
@ -4090,6 +4237,12 @@ dependencies = [
"digest",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
@ -4259,7 +4412,7 @@ dependencies = [
"once_cell",
"paste",
"percent-encoding 2.3.1",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"sha2",
"smallvec",
@ -4298,7 +4451,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"sha2",
"sqlx-core",
@ -4341,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",
@ -4379,7 +4532,7 @@ dependencies = [
"memchr",
"once_cell",
"rand 0.8.5",
"serde 1.0.193",
"serde 1.0.209",
"serde_json",
"sha1",
"sha2",
@ -4407,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",
@ -4599,7 +4752,7 @@ dependencies = [
"deranged",
"itoa",
"powerfmt",
"serde 1.0.193",
"serde 1.0.209",
"time-core",
"time-macros",
]
@ -4653,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",
@ -4704,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",
@ -4734,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",
@ -4746,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]]
@ -4767,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",
@ -4829,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",
@ -4883,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",
]
@ -4967,6 +5119,12 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
[[package]]
name = "unicode_categories"
version = "0.1.1"
@ -5045,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]]
@ -5099,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",
@ -5146,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",
@ -5162,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",
@ -5466,6 +5622,15 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "yansi-term"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1"
dependencies = [
"winapi",
]
[[package]]
name = "zerocopy"
version = "0.7.31"

View File

@ -30,5 +30,5 @@ members = [
"sgf",
"timezone-testing",
"tree",
"visions/server", "gm-dash",
"visions/server", "gm-dash/server",
]

View File

@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[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"

View File

@ -0,0 +1,25 @@
use pipewire::{context::Context, main_loop::MainLoop};
fn main() -> Result<(), Box<dyn std::error::Error>> {
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(|global| {
if global.props.and_then(|p| p.get("media.class")) == Some("Audio/Sink"){
println!(
"\t{:?} {:?}",
global.props.and_then(|p| p.get("node.description")),
global.props.and_then(|p| p.get("media.class"))
);
}
})
.register();
mainloop.run();
Ok(())
}

View File

@ -1,3 +1,109 @@
fn main() {
println!("Hello, world!");
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<String>,
}
#[derive(Clone)]
struct State {
internal: Arc<RwLock<State_>>,
}
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<String> {
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<dyn std::error::Error>> {
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;
}