@@ -79,6 +79,7 @@ impl TryFrom<u32> for Direction {
79
79
80
80
#[ derive( Debug ) ]
81
81
pub ( crate ) struct Node {
82
+ proxy_id : u32 ,
82
83
pub device_id : Option < u32 > ,
83
84
pub name : String ,
84
85
pub nick : Option < String > ,
@@ -95,8 +96,9 @@ pub(crate) struct Node {
95
96
}
96
97
97
98
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 {
99
100
Self {
101
+ proxy_id,
100
102
device_id : global_props
101
103
. get ( & keys:: DEVICE_ID )
102
104
. and_then ( |s| s. parse ( ) . ok ( ) ) ,
@@ -219,7 +221,7 @@ impl<T: ProxyT + 'static> Proxies<T> {
219
221
& mut self ,
220
222
proxy : T ,
221
223
listener : impl Listener + ' static ,
222
- proxies : & std :: rc :: Rc < RefCell < Self > > ,
224
+ proxies : & Rc < RefCell < Self > > ,
223
225
) -> u32 {
224
226
let listener_spe = Box :: new ( listener) ;
225
227
@@ -286,17 +288,87 @@ pub(crate) enum CommandKind {
286
288
}
287
289
288
290
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 ) ;
290
298
use CommandKind :: * ;
291
299
let id = match self {
292
300
SetVolume ( id, _) | Mute ( id, _) => id,
293
301
} ;
294
302
let client_data = client. data . lock ( ) . unwrap ( ) ;
295
303
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
+
296
368
if let ( Some ( device_id) , Some ( direction) ) = ( node. device_id , node. direction ) {
297
369
if let Some ( directed_routes) = client_data. directed_routes . get ( & device_id) {
298
370
if let Some ( route) = directed_routes. get_route ( direction) {
299
- if let Some ( device_proxy) = proxies
371
+ if let Some ( device_proxy) = device_proxies
300
372
. borrow_mut ( )
301
373
. proxies_t
302
374
. get ( & directed_routes. proxy_id )
@@ -348,7 +420,7 @@ impl CommandKind {
348
420
}
349
421
. expect ( "Could not push object" ) ;
350
422
351
- match self {
423
+ match & self {
352
424
SetVolume ( _, volume) => {
353
425
pod_builder
354
426
. add_prop ( SPA_PROP_channelVolumes , 0 )
@@ -380,7 +452,7 @@ impl CommandKind {
380
452
pod_builder
381
453
. add_prop ( SPA_PROP_mute , 0 )
382
454
. 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" ) ;
384
456
}
385
457
}
386
458
@@ -406,7 +478,7 @@ impl CommandKind {
406
478
}
407
479
408
480
debug ! (
409
- "Setting param: {:?}" ,
481
+ "Setting Device Route param: {:?}" ,
410
482
PodDeserializer :: deserialize_from:: <Value >( & pod_data)
411
483
) ;
412
484
let pod = Pod :: from_bytes ( & pod_data) . expect ( "Unable to construct pod" ) ;
@@ -460,13 +532,16 @@ impl Client {
460
532
461
533
// Proxies and their listeners need to stay alive so store them here
462
534
let node_proxies = Rc :: new ( RefCell :: new ( Proxies :: < NodeProxy > :: new ( ) ) ) ;
535
+ let node_proxies_weak = Rc :: downgrade ( & node_proxies) ;
463
536
let device_proxies = Rc :: new ( RefCell :: new ( Proxies :: < DeviceProxy > :: new ( ) ) ) ;
464
537
let device_proxies_weak = Rc :: downgrade ( & device_proxies) ;
465
538
let metadata_proxies = Rc :: new ( RefCell :: new ( Proxies :: < MetadataProxy > :: new ( ) ) ) ;
466
539
467
540
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 ( ) ) ;
470
545
}
471
546
} ) ;
472
547
@@ -562,7 +637,7 @@ impl Client {
562
637
} )
563
638
. register ( ) ;
564
639
565
- node_proxies. borrow_mut ( ) . add_proxy (
640
+ let proxy_id = node_proxies. borrow_mut ( ) . add_proxy (
566
641
node_proxy,
567
642
node_listener,
568
643
& node_proxies,
@@ -573,7 +648,7 @@ impl Client {
573
648
. lock ( )
574
649
. unwrap ( )
575
650
. nodes
576
- . insert ( global_id, Node :: new ( global_id, global_props) ) ;
651
+ . insert ( global_id, Node :: new ( global_id, global_props, proxy_id ) ) ;
577
652
update_copy. replace_with ( |v| * v | EventKind :: NODE_ADDED ) ;
578
653
}
579
654
ObjectType :: Link => {
0 commit comments