forked from tisogai/module-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-router.c
1951 lines (1808 loc) · 73.2 KB
/
module-router.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* @file: module-router.c
*
* The file contains the implementation of the hooks and the dbus-callbacks
* for pulseaduio router module.
*
* @component: PulseAudio router module
*
* @author: Toshiaki Isogai <[email protected]>
* Kapildev Patel <[email protected]>
*
* @copyright (c) 2016 Advanced Driver Information Technology.
* This code is developed by Advanced Driver Information Technology.
* Copyright of Advanced Driver Information Technology, Bosch, and DENSO.
* All rights reserved.
*
*****************************************************************************/
#include <pulsecore/pulsecore-config.h>
#include <pulsecore/core.h>
#include <pulsecore/module.h>
#include <pulsecore/modargs.h>
#include <pulsecore/sink.h>
#include <pulsecore/sink-input.h>
#include <pulse/version.h>
#include <router-userdata.h>
#include <router-dbusif.h>
#define GENIVI_DBUS_PLUGIN 1
#define MODULE_ROUTER_EXTRA_LOGS 0
#if GENIVI_DBUS_PLUGIN
#define PULSE_ROUTER_DBUS_RETURN_INTERFACE_NAME "org.genivi.audiomanager.routinginterface"
#else
#define PULSE_ROUTER_DBUS_RETURN_INTERFACE_NAME "pulseaudio"
#endif
#define PULSE_ROUTER_DBUS_NAME "org.genivi.audiomanager.routing.pulseaudio"
#define PULSE_ROUTER_INTERFACE_NAME "org.genivi.audiomanager.routing.pulseaudio"
#define PULSE_ROUTER_DBUS_PATH "/org/genivi/audiomanager/routing/pulseaudio"
#define AM_COMMAND_DBUS_NAME "org.genivi.audiomanager"
#define AM_COMMAND_DBUS_INTERFACE_NAME "org.genivi.audiomanager.commandinterface"
#define AM_COMMAND_DBUS_PATH "/org/genivi/audiomanager/commandinterface"
#define AM_ROUTING_DBUS_NAME "org.genivi.audiomanager"
#define AM_ROUTING_DBUS_INTERFACE_NAME "org.genivi.audiomanager.routinginterface"
#define AM_ROUTING_DBUS_PATH "/org/genivi/audiomanager/routinginterface"
#define AM_COMMAND_SIGNAL_WATCH_RULE "type='signal',interface='org.genivi.audiomanager.commandinterface'"
#define DS_UNKNOWN 0
#define DS_CONTROLLED 1
#define SS_ON 1
#define SS_OFF 2
#define SS_PAUSED 3
#define A_AVAILABLE 1
#define A_UNAVAILABLE 2
PA_MODULE_AUTHOR("Advanced Driver Information Technology");
PA_MODULE_DESCRIPTION("PulseAudio router plug-in");
PA_MODULE_VERSION( PACKAGE_VERSION);
PA_MODULE_LOAD_ONCE( true);
PA_MODULE_USAGE("");
struct router_hooks {
pa_hook_slot *hook_slot_sink_input_put;
pa_hook_slot *hook_slot_sink_input_unlink;
pa_hook_slot *hook_slot_source_output_put;
pa_hook_slot *hook_slot_source_output_unlink;
pa_hook_slot *hook_slot_sink_new;
pa_hook_slot *hook_slot_sink_input_new;
pa_hook_slot *hook_slot_source_new;
pa_hook_slot *hook_slot_source_output_new;
};
/**
* @brief This function is registered with the dbus interface module, it gets called when command side
* Notification cbNewMainConnection is received.
* @param u: The pointer to the userdata structure
* connection: The pointer to the main connection data structure.
* @return void
*/
static void cb_new_main_connection(struct userdata *u, am_main_connection_t *connection) {
ROUTER_FUNCTION_ENTRY;
ROUTER_FUNCTION_EXIT;
}
/**
* @brief This function is registered with the dbus interface module, it gets called when command side
* Notification cbRemovedMainConnection is received.
* @param u: The pointer to the userdata structure
* id: The id of the main connection which is removed.
* @return void
*/
static void cb_removed_main_connection(struct userdata *u, uint16_t id) {
ROUTER_FUNCTION_ENTRY;
pa_log_debug("Main Connection removed ID = %d", id);
pa_hashmap_remove(u->main_connection_map, (void*) (intptr_t) id);
ROUTER_FUNCTION_EXIT;
}
/**
* @brief This function is registered with the dbus interface module, it gets called when command side
* Notification cbMainConnectionStateChanged is received.
* @param u: The pointer to the userdata structure
* id: The main connection id
* state: The main connection state
* @return void
*/
static void cb_main_connection_state_changed(struct userdata *u, uint16_t id, int32_t state) {
ROUTER_FUNCTION_ENTRY;
pa_log_debug("Main Connection state changed ID = %d state = %d", id, state);
am_main_connection_t* main_connection = pa_hashmap_get(u->main_connection_map, (void*) (intptr_t) id);
if ( main_connection != NULL ) {
main_connection->state = state;
}
ROUTER_FUNCTION_EXIT;
}
#if MODULE_ROUTER_EXTRA_LOGS
/**
* @brief This function prints all the data present in the router module. It is used only for the debug purpose.
* @param u: The pointer to the userdata structure, which has all the data related to the module.
* @return void
*/
static void print_maps(struct userdata* u)
{
int16_t index;
pa_log_debug("print_maps: SinkMaps");
for(index = 0;index < 10;index++)
{
pa_log_debug("sink ID=%d, builtin=%d, source_state=%d, ptr=%p, name=%s",
u->sink_map[index].id,u->sink_map[index].builtin,u->sink_map[index].source_state,
u->sink_map[index].data,u->sink_map[index].name);
}
pa_log_debug("print_maps: SourceMaps");
for(index = 0;index < 10;index++)
{
pa_log_debug("Source ID=%d, builtin=%d, source_state=%d, ptr=%p, name=%s",
u->source_map[index].id,u->source_map[index].builtin,u->source_map[index].source_state,
u->source_map[index].data,u->source_map[index].name);
}
pa_log_debug("print_maps: ConenctionMaps");
am_connect_t *c;
void *s;
PA_HASHMAP_FOREACH(c, u->connection_map, s)
{
pa_log_debug("Connection ID=%d, sourceID = %d, sinkID=%d",c->connection_id, c->source_id,c->sink_id);
}
pa_log_debug("print_maps: MainConenctionMaps");
am_main_connection_t *mainConnection;
PA_HASHMAP_FOREACH(mainConnection, u->main_connection_map, s)
{
pa_log_debug("Connection ID=%d, sourceID = %d, sinkID=%d state=%d",mainConnection->connection_id, mainConnection->source_id,mainConnection->sink_id,mainConnection->state);
}
}
#endif
/**
* @brief This function gets the free index in the map. The map can be source or sink map.
* @param map: The pointer to the map
* @return int16_t: The index of the free entry in the map.
*/
static int16_t get_free_map_index(name_id_map *map) {
int16_t index = -1;
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].name[0] == '\0' ) {
index = i;
break;
}
}
pa_log_debug("get_free_map_index index=%d", index);
return index;
}
/**
* @brief This function returns the index of the map entry from the id.
* @param id: The id for which the map index is needed
* map: pointer to the map either source/sink map
* @return int16_t: The index of the map.
*/
static int16_t get_map_index_from_id(const uint16_t id, const name_id_map *map) {
int16_t index = -1;
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].id == id ) {
index = i;
break;
}
}
pa_log_debug("get_map_index_from_id id=%d, index = %d", id, index);
return index;
}
/**
* @brief This function returns the map index which matches the name
* @param name: The pointer to the name for which map index is needed.
* map: pointer to the map either source/sink map
* @return int16_t: The index of the map.
*/
static int16_t get_map_index_from_name(const char* name, const name_id_map *map) {
int16_t index = -1;
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( strcmp(map[i].name, name) == 0 ) {
index = i;
break;
}
}
pa_log_debug("get_map_index_from_name name=%s, index = %d", name, index);
return index;
}
/**
* @brief This function returns the connection from the source id
* @param u: The pointer to the userdata.
* source_id: The source id of which the connection is to be searched.
* @return am_connect_t: The pointer to the am_connect structure.
*/
static am_connect_t* get_connection_from_source(struct userdata *u, uint16_t source_id) {
am_connect_t *c = NULL;
am_connect_t *ret_val = NULL;
void *s;
PA_HASHMAP_FOREACH(c, u->connection_map, s)
{
if ( c->source_id == source_id ) {
ret_val = c;
break;
}
}
pa_log_debug("get_connection_from_source() sourceID = %d", source_id);
return ret_val;
}
/**
* @brief This function returns the connection from the source and sink.
* @param u: The pointer to the userdata.
* source_id: The source id of which the connection is to be searched.
* sink_id: The sink id of which connection is to be searched
* @return am_connect_t: The pointer to the am_connect structure.
*/
static am_connect_t* get_connection_from_src_sink(struct userdata *u, uint16_t source_id, uint16_t sink_id) {
am_connect_t *c = NULL;
am_connect_t *ret_val = NULL;
void *s;
PA_HASHMAP_FOREACH(c, u->connection_map, s)
{
if ( (c->source_id == source_id) && (c->sink_id == sink_id) ) {
ret_val = c;
break;
}
}
pa_log_debug("get_connection_from_src_sink ()sourceID = %d sinkID=%d", source_id, sink_id);
return ret_val;
}
/**
* @brief This function returns the id from the sink_input pointer
* @param sink_input: The pointer to the sink input.
* map: The pointer to the sink input map
* @return uint16_t: The id of the source
*/
static uint16_t get_id_from_sink_input(pa_sink_input* sink_input, const name_id_map* map) {
uint16_t id = 0;
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].data == sink_input ) {
id = map[i].id;
break;
}
}
pa_log_debug("get_id_from_sink_input id=%d", id);
return id;
}
/**
* @brief This function removes the entry of source/sink from the respective map.
* @param id: The id of source/sink which needs to be removed.
* map: The pointer to the source/sink map.
* @return None
*/
static void remove_pa_pointer(uint16_t id, name_id_map* map) {
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].id == id ) {
map[i].data = NULL;
break;
}
}
return;
}
/**
* @brief This function returns the pulseaudio source/sink pointer from the audiomanager ID.
* @param id: The id of source/sink.
* map: The pointer to the source/sink map.
* @return void*: The pointer to the pulseaudio source/sinks.
*/
static void* get_pa_pointer_from_id(uint16_t id, const name_id_map* map) {
int16_t i = 0;
void* pa_ptr = NULL;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].id == id ) {
pa_ptr = map[i].data;
break;
}
}
return pa_ptr;
}
/**
* @brief This function returns the map index from the audio manager name.
* @param: name: The audio manager name.
* map: The pointer to the source/sink map.
* @return int16_t: The index of the map for which the name matches.
*/
static int16_t am_name_to_map_index(const char *name, const name_id_map *map) {
int16_t index = -1;
int16_t i = 0;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( strcmp(name, map[i].name) == 0 ) {
index = i;
break;
}
}
return index;
}
/**
* @brief This function returns the audiomanager ID from name.
* @param: name: The audio manager name.
* map: The pointer to the source/sink map.
* @return int16_t: The id for which the name matches in the map.
*/
static uint16_t am_name_to_id(const char *name, const name_id_map *map) {
uint16_t id = 0;
if ( name != NULL ) {
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( strcmp(map[i].name, name) == 0 ) {
id = map[i].id;
break;
}
}
}
pa_log_debug("am_name_to_id name:%s id:%d", name, id);
return id;
}
/**
* @brief This function returns the audiomanager ID from name.
* @param: name: The audio manager name.
* map: The pointer to the source/sink map.
* @return int16_t: The id for which the name matches in the map.
*/
static char* id_to_am_name(const uint16_t id, name_id_map *map) {
char *name = NULL;
for ( int i = 0 ; i < AM_MAX_SOURCE_SINK ; i++ ) {
if ( map[i].id == id ) {
name = map[i].name;
break;
}
}
return name;
}
/**
* @brief This function returns the pulseaudio property description from a given id.
* @param: id: The audio manager id.
* map: The pointer to the source/sink map.
* @return char*: The description of the source/sink.
*/
static char* id_to_pulse_description(const uint16_t id, const name_id_map *map) {
char* description = NULL;
for ( const name_id_map *m = map ; m->name[0] != '\0' ; m++ ) {
if ( m->id == id ) {
description = (char*) m->description;
break;
}
}
return description;
}
/**
* @brief This function returns the audiomanager ID from pulseaudio description
* @param: description: The pulseaudio description.
* map: The pointer to the source/sink map.
* @return uint16_t: The id from the pulseaudio description.
*/
static uint16_t pulse_description_to_am_id(const char *description, const name_id_map *map) {
uint16_t id = 0;
if ( description ) {
for ( const name_id_map *m = map ; m->name[0] != '\0' ; m++ ) {
if ( (m->description) && (strcmp(m->description, description) == 0) ) {
id = m->id;
break;
}
}
}
return id;
}
/**
* @brief This function checks if the source/sink is built-in i.e. not application source/sink.
* @param: id: The id of the source/sink
* map: The pointer to the source/sink map.
* @return bool: true if source/sink is built-in and vice versa.
*/
static bool is_source_sink_builtin(const uint16_t id, const name_id_map *map) {
bool builtin = false;
if ( id ) {
for ( const name_id_map *m = map ; m->name[0] != '\0' ; m++ ) {
if ( m->id == id ) {
builtin = m->builtin;
break;
}
}
}
return builtin;
}
/**
* @brief This function returns the sink input pointer from the audiomanager id.
* @param: u: The user data pointer.
* sink_id: The sink id.
* @return pa_sink_input*: The pointer to the pulse audio sink input.
*/
static pa_sink_input* am_id_to_loopbacked_sink_input(struct userdata*u, uint16_t sink_id) {
pa_sink_input* sink_input = NULL;
pa_sink_input* return_sink_input = NULL;
char* sink_name = id_to_am_name(sink_id, u->sink_map);
uint32_t index;
PA_IDXSET_FOREACH(sink_input, u->core->sink_inputs, index)
{
if ( !strcmp(sink_name, sink_input->sink->name) ) {
return_sink_input = sink_input;
break;
}
}
return return_sink_input;
}
/**
* @brief This function returns the sink input pointer from the audiomanager id.
* @param: u: The user data pointer.
* sink_id: The sink id.
* @return pa_sink_input*: The pointer to the pulse audio sink input.
*/
static pa_source* am_name_to_pa_source(struct userdata*u, char* source_name) {
pa_source* source = NULL;
pa_source* output_source = NULL;
uint32_t index;
PA_IDXSET_FOREACH(source, u->core->sources, index)
{
if ( !strcmp(source_name, source->name) ) {
output_source = source;
break;
}
}
return output_source;
}
/**
* @brief This function returns the source pointer from the pulseaudio description of the sink.
* @param: u: The user data pointer.
* description: The pulseaudio description of the sink.
* @return pa_source*: The pointer to the pulse audio source.
*/
static pa_source* pulse_description_to_loopbacked_source(const struct userdata* u, const char *description) {
pa_source* pulse_source = NULL;
pa_source* iterator;
uint32_t index;
if ( description ) {
PA_IDXSET_FOREACH(iterator, u->core->sources, index)
{
if ( !strcmp(description, pa_proplist_gets(iterator->proplist, PA_PROP_DEVICE_DESCRIPTION)) ) {
pulse_source = iterator;
break;
}
}
}
return pulse_source;
}
/**
* @brief This function returns the sink pointer from the pulseaudio description of the source.
* @param: u: The user data pointer.
* description: The pulseaudio description of the source.
* @return pa_sink*: The pointer to the pulse audio sink.
*/
static pa_sink* pulse_description_to_loopbacked_sink(const struct userdata* u, const char *description) {
pa_sink* pulse_sink = NULL;
pa_sink* iterator;
uint32_t index;
if ( description ) {
PA_IDXSET_FOREACH(iterator, u->core->sinks, index)
{
if ( !strcmp(description, pa_proplist_gets(iterator->proplist, PA_PROP_DEVICE_DESCRIPTION)) ) {
pulse_sink = iterator;
break;
}
}
}
return pulse_sink;
}
/**
* @brief This function returns the source output pointer from the pulseaudio description.
* @param: u: The user data pointer.
* description: The pulseaudio description property.
* @return pa_source_output*: The pointer to the pulse audio source output.
*/
static pa_source_output* pulse_description_to_loopbacked_source_output(const struct userdata* u,
const char *description) {
pa_source_output* pulse_source_output = NULL;
pa_source_output* iterator;
uint32_t index;
char Loopback_description[1024];
sprintf(Loopback_description, "Loopback to %s", description);
if ( description ) {
PA_IDXSET_FOREACH(iterator, u->core->source_outputs, index)
{
const char* proplist_description = pa_proplist_gets(iterator->proplist, PA_PROP_MEDIA_NAME);
if ( (proplist_description != NULL) && !(strcmp(Loopback_description, proplist_description)) ) {
pulse_source_output = iterator;
break;
}
}
}
return pulse_source_output;
}
/**
* @brief This function returns the sink output pointer from the pulseaudio description.
* @param: u: The user data pointer.
* description: The pulseaudio description property.
* @return pa_sink_input*: The pointer to the pulse audio sink input.
*/
static pa_sink_input* pulse_description_to_loopbacked_sink_input(const struct userdata* u, const char *description) {
pa_sink_input* pulse_sink_input = NULL;
pa_sink_input* iterator;
uint32_t index;
char Loopback_description[1024];
sprintf(Loopback_description, "Loopback from %s", description);
if ( description ) {
PA_IDXSET_FOREACH(iterator, u->core->sink_inputs, index)
{
const char* proplist_description = pa_proplist_gets(iterator->proplist, PA_PROP_MEDIA_NAME);
if ( (proplist_description != NULL) && !(strcmp(Loopback_description, proplist_description)) ) {
pulse_sink_input = iterator;
break;
}
}
}
return pulse_sink_input;
}
/**
* @brief This function returns the pulseaudio module pointer from the source and sink id.
* @param: u: The user data pointer.
* source_id: The source id.
* sink_id: The sink id.
* @return pa_module*: The pointer to the pulse audio loopback module.
*/
static pa_module* get_loopback_module(struct userdata *u, uint16_t source_id, uint16_t sink_id) {
pa_module* loopback_module = NULL;
void* map_source_id;
void* map_sink_id;
void* state;
char* description = id_to_pulse_description(source_id, u->source_map);
pa_sink_input* sink_input = pulse_description_to_loopbacked_sink_input(u, description);
description = id_to_pulse_description(sink_id, u->sink_map);
pa_source_output* source_output = pulse_description_to_loopbacked_source_output(u, description);
if ( sink_input && source_output ) {
if ( sink_input->module == source_output->module ) {
loopback_module = sink_input->module;
}
}
return loopback_module;
}
/**
* @brief This function loads the loopback module between the source and sink.
* @param: u: The user data pointer.
* source_id: The source id.
* sink_id: The sink id.
* @return pa_module*: The pointer to the pulse audio loopback module.
*/
static pa_module* load_loopback_module(struct userdata*u, uint16_t source_id, uint16_t sink_id) {
pa_module* loopback_module = NULL;
char* source_name = id_to_am_name(source_id, u->source_map);
char* sink_name = id_to_am_name(sink_id, u->sink_map);
char arguments[1024];
if ( false == pa_module_exists("module-loopback") ) {
/* No operation */
} else {
sprintf(arguments, "source=%s", source_name);
loopback_module = pa_module_load(u->core, "module-loopback", arguments);
if ( loopback_module != NULL ) {
// get the sink input connected to the sink
pa_sink_input* sink_input = am_id_to_loopbacked_sink_input(u, sink_id);
if ( sink_input != NULL ) {
pa_sink_input_cork(sink_input, true);
}
} else {
pa_log_error("Failed to load the Loopback module");
}
}
return loopback_module;
}
/**
* @brief This function replaces the whitespaces with the hash '#' character. These is needed because of
* limitation in the controller topology descrption which cant parse whitespaces at present.
* @param: char*: The string to replace the whitespaces.
* @return void.
*/
static void replace_whitespace_with_hash(char* string) {
int counter = 0;
pa_log_error("%s", string);
while ( string[counter] != '\0' ) {
if ( string[counter] == ' ' ) {
string[counter] = '#';
}
counter++;
}
pa_log_error("%s", string);
}
/**
* @brief This function sets the volume in a pulseaudio volume structure.
* @param: destchannelVolume: The pointer to the pulseaudio volume structure.
* num_channels: The number of channels to be set.
* volume: volume value
* @return void.
*/
static void set_pa_volume(pa_cvolume* destchannelVolume, int num_channels, uint32_t volume) {
int i;
destchannelVolume->channels = num_channels;
for ( i = 0; i < num_channels ; i++ ) {
destchannelVolume->values[i] = volume;
}
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever any sink_input is connected
* to a sink.
* @param: c: The pointer to pulseaudio core.
* sink_input: The sink input pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_sink_input_put(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
pa_assert(c);
pa_assert(u);
pa_assert(sink_input);
bool corked = false;
pa_sink_input_state_t state;
pa_sink_input_set_mute(sink_input, true, false);
pa_sink_input_cork(sink_input, true);
char *source_name_dup = (char*) pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_NAME);
char source_name[AM_MAX_NAME_LENGTH];
strncpy(source_name, source_name_dup, AM_MAX_NAME_LENGTH);
pa_assert(source_name);
replace_whitespace_with_hash(source_name);
char* sink_name_dup = (char*) pa_proplist_gets(sink_input->sink->proplist, PA_PROP_DEVICE_DESCRIPTION);
char sink_name[AM_MAX_NAME_LENGTH];
strncpy(sink_name, sink_name_dup, AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(sink_name);
pa_log_debug("hook_callback_sink_input_put source Name=%s sink_name=%s", source_name, sink_name);
uint16_t source_id = am_name_to_id(source_name, u->source_map);
uint16_t sink_id = am_name_to_id(sink_name, u->sink_map);
int source_index = get_map_index_from_id(source_id, u->source_map);
if ( source_index != -1 ) {
u->source_map[source_index].data = (void*) sink_input;
/* set the sink input volume */
if ( ((u->source_map[source_index].builtin == false)) && (u->source_map[source_index].volume_valid == true) ) {
pa_cvolume channelVolume;
set_pa_volume(&channelVolume, sink_input->volume.channels, (uint32_t) (u->source_map[source_index].volume));
pa_sink_input_set_volume(sink_input, &channelVolume, false, false);
}
}
if ( (source_id != 0) && (sink_id != 0) ) {
am_connect_t* con = get_connection_from_src_sink(u, source_id, sink_id);
if ( (con != NULL) && (source_index != -1) ) {
if ( u->source_map[source_index].source_state == SS_ON ) {
if ( sink_input->muted == true ) {
pa_sink_input_set_mute(sink_input, false, false);
}
pa_sink_input_cork(sink_input, false);
}
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
}
if ( source_id != 0 ) {
//Check if the source is connected to any other sink then the requested one in put request.
am_connect_t* con = get_connection_from_source(u, source_id);
if ( con != NULL ) {
int sink_index = get_map_index_from_id(con->sink_id, u->sink_map);
if ( sink_index != -1 ) {
pa_sink* sink = (pa_sink*) (u->sink_map[sink_index].data);
if ( sink != NULL ) {
int return_code = pa_sink_input_move_to(sink_input, sink, false);
pa_log_debug("sink Input move return=%d", return_code);
}
if ( u->source_map[source_index].source_state == SS_ON ) {
pa_sink_input_set_mute(sink_input, false, false);
pa_sink_input_cork(sink_input, false);
}
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
}
}
/*
* send connect request to audio manager
*/
am_main_connection_t connection_data;
connection_data.connection_id = 0;
connection_data.source_id = source_id;
connection_data.sink_id = sink_id;
if ( connection_data.source_id == 0 ) {
if ( (source_name != NULL) && (strstr(source_name, "Loopback from") != NULL) ) {
char description[256];
memset(description, 0, sizeof(description));
sscanf(source_name, "Loopback#from#%s", description);
pa_log_debug("Loopback from -> %s", description);
connection_data.source_id = pulse_description_to_am_id(description, u->source_map);
pa_log_debug("source id = %d", connection_data.source_id);
pa_source* source = am_name_to_pa_source(u, id_to_am_name(connection_data.source_id, u->source_map));
if ( source != NULL ) {
pa_source_suspend(source, true, PA_SUSPEND_INTERNAL);
}
}
}
connection_data.delay = 0;
connection_data.state = 0;
if ( (connection_data.source_id != 0) && (connection_data.sink_id != 0) ) {
router_dbusif_command_connect(u, &connection_data);
}
#if MODULE_ROUTER_EXTRA_LOGS
print_maps(u);
#endif
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever any source_output is connected
* to a source.
* @param: c: The pointer to pulseaudio core.
* source_output: The source output pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_source_output_put(pa_core *c, pa_source_output *source_output, struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
pa_assert(c);
pa_assert(source_output);
pa_assert(u);
bool corked = false;
pa_source_output_state_t state;
char *sink_name_dup = (char*) pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_NAME);
char sink_name[AM_MAX_NAME_LENGTH];
strncpy(sink_name, sink_name_dup, AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(sink_name);
// in case of implicit loopback module loading this event should be ignored.
if ( strstr(sink_name, "Loopback#to") == NULL ) {
pa_assert(sink_name);
pa_log_debug("sink name = %s", sink_name);
state = pa_source_output_get_state(source_output);
corked = (state == PA_SOURCE_OUTPUT_CORKED);
if ( (!corked) && (!source_output->muted) ) {
pa_source_output_set_mute(source_output, true, false);
pa_source_output_cork(source_output, true);
}
am_main_connection_t connection_data;
connection_data.connection_id = 0;
//TODO : why this is hardcoded????
connection_data.source_id = am_name_to_id("Mic", u->source_map);
connection_data.sink_id = am_name_to_id(sink_name, u->sink_map);
connection_data.delay = 0;
connection_data.state = 0;
if ( (connection_data.source_id != 0) && (connection_data.sink_id != 0) ) {
int index = get_map_index_from_id(connection_data.sink_id, u->sink_map);
if ( index != -1 ) {
u->sink_map[index].data = source_output;
}
router_dbusif_command_connect(u, &connection_data);
}
}
#if MODULE_ROUTER_EXTRA_LOGS
print_maps(u);
#endif
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever any sink_input is unconnected
* to a sink.
* @param: c: The pointer to pulseaudio core.
* sink_input: The sink input pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_sink_input_unlink(pa_core *c, pa_sink_input *sink_input, struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
pa_assert(c);
pa_assert(sink_input);
pa_assert(u);
bool corked = false;
char *source_name_dup = (char*) pa_proplist_gets(sink_input->proplist, PA_PROP_MEDIA_NAME);
pa_assert(source_name_dup);
char source_name[AM_MAX_NAME_LENGTH];
strncpy(source_name, source_name_dup, AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(source_name);
uint16_t source_id = get_id_from_sink_input(sink_input, u->source_map);
pa_log_debug("source name = %s source id: %d", source_name, source_id);
if ( (source_name != NULL) && (source_id == 0) && strstr(source_name, "Loopback from") != NULL ) {
char description[256];
memset(description, 0, sizeof(description));
sscanf(source_name, "Loopback from %s", description);
source_id = pulse_description_to_am_id(description, u->source_map);
}
/*
* send disconnect request to the Audiomanager.
*/
am_main_connection_t* conn;
void *s;
bool found = false;
PA_HASHMAP_FOREACH(conn, u->main_connection_map, s)
{
if ( conn->source_id == source_id ) {
found = true;
break;
}
}
if ( found ) {
am_disconnect_t disconnectData;
disconnectData.connection_id = conn->connection_id;
router_dbusif_command_disconnect(u, &disconnectData);
}
int source_index = get_map_index_from_id(source_id, u->source_map);
if ( (source_index != -1) && (u->source_map[source_index].builtin == false) ) {
remove_pa_pointer(source_id, u->source_map);
}
#if MODULE_ROUTER_EXTRA_LOGS
print_maps(u);
#endif
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever any source_output is unconnected
* from a source.
* @param: c: The pointer to pulseaudio core.
* source_output: The source output pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_source_output_unlink(pa_core *c, pa_source_output *source_output,
struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
pa_assert(c);
pa_assert(source_output);
pa_assert(u);
bool corked = false;
char *sink_name_dup = (char*) pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_NAME);
pa_assert(sink_name_dup);
char sink_name[AM_MAX_NAME_LENGTH];
strncpy(sink_name, sink_name_dup, AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(sink_name);
pa_log_debug("hook_callback_source_output_unlink sink name = %s", sink_name);
if ( strstr(sink_name, "Loopback to") == NULL ) {
uint16_t sink_id = am_name_to_id(sink_name, u->sink_map);
am_main_connection_t* conn;
void *s;
bool found = false;
PA_HASHMAP_FOREACH(conn, u->main_connection_map, s)
{
if ( conn->sink_id == sink_id ) {
found = true;
break;
}
}
if ( found ) {
am_disconnect_t disconnectData;
disconnectData.connection_id = conn->connection_id;
router_dbusif_command_disconnect(u, &disconnectData);
}
int sink_index = get_map_index_from_id(sink_id, u->sink_map);
if ( (sink_index != -1) && (u->sink_map[sink_index].builtin == false) ) {
remove_pa_pointer(sink_id, u->sink_map);
}
}
#if MODULE_ROUTER_EXTRA_LOGS
print_maps(u);
#endif
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever a new sink appears in the system.
* @param: c: The pointer to pulseaudio core.
* sink: The sink pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_sink_new(pa_core *c, pa_sink *sink, struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
am_sink_register_t sink_register;
pa_assert(c);
pa_assert(sink);
pa_assert(u);
memset(&sink_register, 0, sizeof(am_sink_register_t));
strncpy(sink_register.name, pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION), AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(sink_register.name);
if ( 0 == am_name_to_id(sink_register.name, u->sink_map) ) {
pa_log_debug("sink Name=%s", sink_register.name);
sink_register.domain_id = ((am_domain_register_t*) u->domain)->domain_id;
sink_register.available = A_AVAILABLE;
sink_register.sink_class_id = 1;
sink_register.mute_state = 2;
sink_register.mute_state = SS_OFF;
sink_register.sink_id = 0;
sink_register.visible = true;
int index = get_free_map_index(u->sink_map);
if ( index != -1 ) {
strncpy(u->sink_map[index].name, sink_register.name, AM_MAX_NAME_LENGTH);
strncpy(u->sink_map[index].description, sink_register.name, AM_MAX_NAME_LENGTH);
u->sink_map[index].id = 0;
u->sink_map[index].builtin = true;
u->sink_map[index].data = sink;
pa_cvolume* sink_volume = (pa_cvolume*) pa_sink_get_volume(sink, true);
int16_t audiomanagervolume;
audiomanagervolume = ((0.04577706569008926527809567406729) * sink_volume->values[0] ) - 3000;
sink_register.volume = audiomanagervolume;
/*
* Convert the range [0-100] -> [0-65535]
*/
sink_register.main_volume = sink_volume->values[0] * 100 / 65535;
pa_log_info("sink volume=%d , main_volume=%d",sink_register.volume,sink_register.main_volume );
router_dbusif_routing_register_sink(u, &sink_register);
}
}
#if MODULE_ROUTER_EXTRA_LOGS
print_maps(u);
#endif
ROUTER_FUNCTION_EXIT;
return PA_HOOK_OK;
}
/**
* @brief The hook/callback function called from the pulseaudio main loop whenever any source appears in system.
* @param: c: The pointer to pulseaudio core.
* source: The sink input pointer.
* u: The pointer to the user data.
* @return pa_hook_result: The result of the hook function.
*/
static pa_hook_result_t hook_callback_source_new(pa_core *c, pa_source *source, struct userdata *u) {
ROUTER_FUNCTION_ENTRY;
am_source_register_t source_register;
pa_assert(c);
pa_assert(source);
pa_assert(u);
memset(&source_register, 0, sizeof(am_source_register_t));
strncpy(source_register.name, pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION), AM_MAX_NAME_LENGTH);
replace_whitespace_with_hash(source_register.name);
if ( 0 != am_name_to_id(source_register.name, u->source_map) ) {
pa_log_debug("source name=%s", source_register.name);
source_register.domain_id = ((am_domain_register_t*) (u->domain))->domain_id;
source_register.availability_reason = 0;
source_register.available = A_AVAILABLE;
source_register.interrupt_state = 0;
source_register.source_class_id = 1;
source_register.source_id = 0;
source_register.source_state = SS_OFF;
source_register.visible = true;