Skip to content

Commit bc66199

Browse files
committed
blocks[sound[pipewire]]: Set vol/mute with node proxy too
1 parent 38ac28f commit bc66199

File tree

1 file changed

+86
-11
lines changed

1 file changed

+86
-11
lines changed

src/pipewire.rs

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl TryFrom<u32> for Direction {
7979

8080
#[derive(Debug)]
8181
pub(crate) struct Node {
82+
proxy_id: u32,
8283
pub device_id: Option<u32>,
8384
pub name: String,
8485
pub nick: Option<String>,
@@ -95,8 +96,9 @@ pub(crate) struct Node {
9596
}
9697

9798
impl Node {
98-
fn new(global_id: u32, global_props: &DictRef) -> Self {
99+
fn new(global_id: u32, global_props: &DictRef, proxy_id: u32) -> Self {
99100
Self {
101+
proxy_id,
100102
device_id: global_props
101103
.get(&keys::DEVICE_ID)
102104
.and_then(|s| s.parse().ok()),
@@ -219,7 +221,7 @@ impl<T: ProxyT + 'static> Proxies<T> {
219221
&mut self,
220222
proxy: T,
221223
listener: impl Listener + 'static,
222-
proxies: &std::rc::Rc<RefCell<Self>>,
224+
proxies: &Rc<RefCell<Self>>,
223225
) -> u32 {
224226
let listener_spe = Box::new(listener);
225227

@@ -286,17 +288,87 @@ pub(crate) enum CommandKind {
286288
}
287289

288290
impl CommandKind {
289-
fn execute(self, client: &Client, proxies: Rc<RefCell<Proxies<DeviceProxy>>>) {
291+
fn execute(
292+
self,
293+
client: &Client,
294+
node_proxies: Rc<RefCell<Proxies<NodeProxy>>>,
295+
device_proxies: Rc<RefCell<Proxies<DeviceProxy>>>,
296+
) {
297+
debug!("Executing command: {:?}", self);
290298
use CommandKind::*;
291299
let id = match self {
292300
SetVolume(id, _) | Mute(id, _) => id,
293301
};
294302
let client_data = client.data.lock().unwrap();
295303
if let Some(node) = client_data.nodes.get(&id) {
304+
if let Some(node_proxy) = node_proxies.borrow_mut().proxies_t.get(&node.proxy_id) {
305+
let mut pod_data = Vec::new();
306+
let mut pod_builder = PodBuilder::new(&mut pod_data);
307+
308+
let mut object_param_props_frame = MaybeUninit::uninit();
309+
310+
// Safety: Frames must be popped in the reverse order they were pushed.
311+
// push_object initializes the frame.
312+
// object_param_props_frame is frame 1
313+
unsafe {
314+
pod_builder.push_object(
315+
&mut object_param_props_frame,
316+
SpaTypes::ObjectParamProps.as_raw(),
317+
ParamType::Props.as_raw(),
318+
)
319+
}
320+
.expect("Could not push object");
321+
322+
match &self {
323+
SetVolume(_, volume) => {
324+
pod_builder
325+
.add_prop(SPA_PROP_channelVolumes, 0)
326+
.expect("Could not add prop");
327+
328+
let mut array_frame = MaybeUninit::uninit();
329+
330+
// Safety: push_array initializes the frame.
331+
// array_frame is frame 2
332+
unsafe { pod_builder.push_array(&mut array_frame) }
333+
.expect("Could not push object");
334+
335+
for vol in volume {
336+
let vol = vol / NORMAL;
337+
pod_builder
338+
.add_float(vol * vol * vol)
339+
.expect("Could not add bool");
340+
}
341+
342+
// Safety: array_frame is popped here, which is frame 2
343+
unsafe {
344+
PodBuilder::pop(&mut pod_builder, array_frame.assume_init_mut());
345+
}
346+
}
347+
Mute(_, mute) => {
348+
pod_builder
349+
.add_prop(SPA_PROP_mute, 0)
350+
.expect("Could not add prop");
351+
pod_builder.add_bool(*mute).expect("Could not add bool");
352+
}
353+
}
354+
355+
// Safety: object_param_props_frame is popped here, which is frame 1
356+
unsafe {
357+
PodBuilder::pop(&mut pod_builder, object_param_props_frame.assume_init_mut());
358+
}
359+
360+
debug!(
361+
"Setting Node Props param: {:?}",
362+
PodDeserializer::deserialize_from::<Value>(&pod_data)
363+
);
364+
let pod = Pod::from_bytes(&pod_data).expect("Unable to construct pod");
365+
node_proxy.set_param(ParamType::Props, 0, pod);
366+
}
367+
296368
if let (Some(device_id), Some(direction)) = (node.device_id, node.direction) {
297369
if let Some(directed_routes) = client_data.directed_routes.get(&device_id) {
298370
if let Some(route) = directed_routes.get_route(direction) {
299-
if let Some(device_proxy) = proxies
371+
if let Some(device_proxy) = device_proxies
300372
.borrow_mut()
301373
.proxies_t
302374
.get(&directed_routes.proxy_id)
@@ -348,7 +420,7 @@ impl CommandKind {
348420
}
349421
.expect("Could not push object");
350422

351-
match self {
423+
match &self {
352424
SetVolume(_, volume) => {
353425
pod_builder
354426
.add_prop(SPA_PROP_channelVolumes, 0)
@@ -380,7 +452,7 @@ impl CommandKind {
380452
pod_builder
381453
.add_prop(SPA_PROP_mute, 0)
382454
.expect("Could not add prop");
383-
pod_builder.add_bool(mute).expect("Could not add bool");
455+
pod_builder.add_bool(*mute).expect("Could not add bool");
384456
}
385457
}
386458

@@ -406,7 +478,7 @@ impl CommandKind {
406478
}
407479

408480
debug!(
409-
"Setting param: {:?}",
481+
"Setting Device Route param: {:?}",
410482
PodDeserializer::deserialize_from::<Value>(&pod_data)
411483
);
412484
let pod = Pod::from_bytes(&pod_data).expect("Unable to construct pod");
@@ -460,13 +532,16 @@ impl Client {
460532

461533
// Proxies and their listeners need to stay alive so store them here
462534
let node_proxies = Rc::new(RefCell::new(Proxies::<NodeProxy>::new()));
535+
let node_proxies_weak = Rc::downgrade(&node_proxies);
463536
let device_proxies = Rc::new(RefCell::new(Proxies::<DeviceProxy>::new()));
464537
let device_proxies_weak = Rc::downgrade(&device_proxies);
465538
let metadata_proxies = Rc::new(RefCell::new(Proxies::<MetadataProxy>::new()));
466539

467540
let _receiver = command_receiver.attach(main_loop.loop_(), move |command: CommandKind| {
468-
if let Some(device_proxies) = device_proxies_weak.upgrade() {
469-
command.execute(client, device_proxies.clone());
541+
if let (Some(node_proxies), Some(device_proxies)) =
542+
(node_proxies_weak.upgrade(), device_proxies_weak.upgrade())
543+
{
544+
command.execute(client, node_proxies.clone(), device_proxies.clone());
470545
}
471546
});
472547

@@ -562,7 +637,7 @@ impl Client {
562637
})
563638
.register();
564639

565-
node_proxies.borrow_mut().add_proxy(
640+
let proxy_id = node_proxies.borrow_mut().add_proxy(
566641
node_proxy,
567642
node_listener,
568643
&node_proxies,
@@ -573,7 +648,7 @@ impl Client {
573648
.lock()
574649
.unwrap()
575650
.nodes
576-
.insert(global_id, Node::new(global_id, global_props));
651+
.insert(global_id, Node::new(global_id, global_props, proxy_id));
577652
update_copy.replace_with(|v| *v | EventKind::NODE_ADDED);
578653
}
579654
ObjectType::Link => {

0 commit comments

Comments
 (0)