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
|
/*
Copyright 2011 Canonical Ltd.
Authors:
Conor Curran <conor.curran@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**Notes
*
* Approach now is to set up the communication channels then query the server
* fetch its default sink. If this fails then fetch the list of sinks and take
* the first one which is not the auto-null sink.
* TODO: need to handle the situation where one chink in this linear chain breaks
* i.e. start off the process again and count the attempts (note different to
reconnect attempts)
*/
#include <pulse/gccmacro.h>
#include <pulse/glib-mainloop.h>
#include <pulse/error.h>
#include "pulseaudio-mgr.h"
#define RECONNECT_DELAY 5
static void pm_context_state_callback(pa_context *c, void *userdata);
static void pm_subscribed_events_callback (pa_context *c,
enum pa_subscription_event_type t,
uint32_t index,
void* userdata);
static void pm_server_info_callback (pa_context *c,
const pa_server_info *info,
void *userdata);
static void pm_default_sink_info_callback (pa_context *c,
const pa_sink_info *info,
int eol,
void *userdata);
static void pm_default_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata);
static void pm_sink_info_callback (pa_context *c,
const pa_sink_info *sink,
int eol,
void *userdata);
static void pm_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata);
static void pm_update_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata);
static void pm_sink_input_info_callback (pa_context *c,
const pa_sink_input_info *info,
int eol,
void *userdata);
static void pm_update_active_sink (pa_context *c,
const pa_sink_info *info,
int eol,
void *userdata);
static void pm_toggle_mute_for_every_sink_callback (pa_context *c,
const pa_sink_info *sink,
int eol,
void* userdata);
static gboolean reconnect_to_pulse (gpointer user_data);
static gint connection_attempts = 0;
static gint reconnect_idle_id = 0;
static pa_context *pulse_context = NULL;
static pa_glib_mainloop *pa_main_loop = NULL;
/**
Entry Point
**/
void
pm_establish_pulse_connection (ActiveSink* active_sink)
{
pa_main_loop = pa_glib_mainloop_new (g_main_context_default ());
g_assert (pa_main_loop);
reconnect_to_pulse ((gpointer)active_sink);
}
/**
close_pulse_activites()
Gracefully close our connection with the Pulse async library.
**/
void close_pulse_activites()
{
if (pulse_context != NULL) {
pa_context_unref(pulse_context);
pulse_context = NULL;
}
pa_glib_mainloop_free(pa_main_loop);
pa_main_loop = NULL;
}
/**
reconnect_to_pulse (gpointer user_data)
Method which connects to the pulse server and is used to track reconnects.
*/
static gboolean
reconnect_to_pulse (gpointer user_data)
{
g_debug("Attempt to reconnect to pulse");
// reset
connection_attempts += 1;
if (pulse_context != NULL) {
pa_context_unref(pulse_context);
pulse_context = NULL;
}
pulse_context = pa_context_new( pa_glib_mainloop_get_api( pa_main_loop ),
"com.canonical.indicators.sound" );
g_assert(pulse_context);
pa_context_set_state_callback (pulse_context,
pm_context_state_callback,
user_data);
int result = pa_context_connect (pulse_context,
NULL,
PA_CONTEXT_NOFAIL,
user_data);
if (result < 0) {
g_warning ("Failed to connect context: %s",
pa_strerror (pa_context_errno (pulse_context)));
}
if (connection_attempts > 5){
return FALSE;
}
else{
return TRUE;
}
}
void
pm_update_volume (gint sink_index, pa_cvolume new_volume)
{
pa_operation_unref (pa_context_set_sink_volume_by_index (pulse_context,
sink_index,
&new_volume,
NULL,
NULL) );
}
void
pm_update_mute (gboolean update)
{
pa_operation_unref (pa_context_get_sink_info_list (pulse_context,
pm_toggle_mute_for_every_sink_callback,
GINT_TO_POINTER (update)));
}
/**********************************************************************************************************************/
// Pulse-Audio asychronous call-backs
/**********************************************************************************************************************/
static void
pm_subscribed_events_callback (pa_context *c,
enum pa_subscription_event_type t,
uint32_t index,
void* userdata)
{
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_critical ("subscribed events callback - our userdata is not what we think it should be");
return;
}
ActiveSink* sink = ACTIVE_SINK (userdata);
switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
case PA_SUBSCRIPTION_EVENT_SINK:
// We don't care about any other sink other than the active one.
if (index != active_sink_get_index (sink))
return;
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
active_sink_deactivate (sink);
}
else{
pa_operation_unref (pa_context_get_sink_info_by_index (c,
index,
pm_update_active_sink,
userdata) );
}
break;
case PA_SUBSCRIPTION_EVENT_SOURCE:
g_debug ("Looks like source event of some description");
// We don't care about any other sink other than the active one.
if (index != active_sink_get_source_index (sink))
return;
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
active_sink_deactivate_voip_source (sink);
}
else{
pa_operation_unref (pa_context_get_source_info_by_index (c,
index,
pm_update_source_info_callback,
userdata) );
}
break;
case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
// We don't care about sink input removals.
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
g_debug ("Just saw a sink input removal event");
}
else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
// Determine if its a VOIP app or a maybe blocking state.
pa_operation_unref (pa_context_get_sink_input_info (c,
index,
pm_sink_input_info_callback, userdata));
}
break;
case PA_SUBSCRIPTION_EVENT_SERVER:
g_debug("PA_SUBSCRIPTION_EVENT_SERVER event triggered.");
pa_operation *o;
if (!(o = pa_context_get_server_info (c, pm_server_info_callback, userdata))) {
g_warning("subscribed_events_callback - pa_context_get_server_info() failed");
return;
}
pa_operation_unref(o);
break;
}
}
static void
pm_context_state_callback (pa_context *c, void *userdata)
{
switch (pa_context_get_state(c)) {
case PA_CONTEXT_UNCONNECTED:
g_debug("unconnected");
break;
case PA_CONTEXT_CONNECTING:
g_debug("connecting - waiting for the server to become available");
break;
case PA_CONTEXT_AUTHORIZING:
break;
case PA_CONTEXT_SETTING_NAME:
break;
case PA_CONTEXT_FAILED:
g_warning("PA_CONTEXT_FAILED - Is PulseAudio Daemon running ?");
active_sink_deactivate (ACTIVE_SINK (userdata));
if (reconnect_idle_id == 0){
reconnect_idle_id = g_timeout_add_seconds (RECONNECT_DELAY,
reconnect_to_pulse,
userdata);
}
break;
case PA_CONTEXT_TERMINATED:
break;
case PA_CONTEXT_READY:
connection_attempts = 0;
g_debug("PA_CONTEXT_READY");
if (reconnect_idle_id != 0){
g_source_remove (reconnect_idle_id);
reconnect_idle_id = 0;
}
pa_operation *o;
pa_context_set_subscribe_callback(c, pm_subscribed_events_callback, userdata);
if (!(o = pa_context_subscribe (c, (pa_subscription_mask_t)
(PA_SUBSCRIPTION_MASK_SINK|
PA_SUBSCRIPTION_MASK_SOURCE|
PA_SUBSCRIPTION_MASK_SINK_INPUT|
PA_SUBSCRIPTION_MASK_SERVER), NULL, NULL))) {
g_warning("pa_context_subscribe() failed");
}
if (!(o = pa_context_get_server_info (c, pm_server_info_callback, userdata))) {
g_warning("Initial - pa_context_get_server_info() failed");
}
pa_operation_unref(o);
break;
}
}
/**
After startup we go straight for the server info to see if it has details of
the default sink and source. Normally these are valid, if there is none set
fetch the list of each and try to determine the sink.
**/
static void
pm_server_info_callback (pa_context *c,
const pa_server_info *info,
void *userdata)
{
pa_operation *operation;
g_debug ("server info callback");
if (info == NULL) {
g_warning("No PA server - get the hell out of here");
active_sink_deactivate (ACTIVE_SINK (userdata));
return;
}
// Go for the default sink
if (info->default_sink_name != NULL) {
g_debug ("default sink name from the server ain't null'");
if (!(operation = pa_context_get_sink_info_by_name (c,
info->default_sink_name,
pm_default_sink_info_callback,
userdata) )) {
g_warning("pa_context_get_sink_info_by_namet() failed");
active_sink_deactivate (ACTIVE_SINK (userdata));
pa_operation_unref(operation);
return;
}
} // If there is no default sink, try to determine a sink from the list of sinks
else if (!(operation = pa_context_get_sink_info_list(c,
pm_sink_info_callback,
userdata))) {
g_warning("pa_context_get_sink_info_list() failed");
active_sink_deactivate (ACTIVE_SINK (userdata));
pa_operation_unref(operation);
return;
}
// And the source
if (info->default_source_name != NULL) {
g_debug ("default source name from the server is not null'");
if (!(operation = pa_context_get_source_info_by_name (c,
info->default_source_name,
pm_default_source_info_callback,
userdata) )) {
g_warning("pa_context_get_default_source_info() failed");
// TODO: call some input deactivate method on active sink
pa_operation_unref(operation);
return;
}
}
else if (!(operation = pa_context_get_source_info_list(c,
pm_source_info_callback,
userdata))) {
g_warning("pa_context_get_sink_info_list() failed");
// TODO: call some input deactivate method for the source
}
pa_operation_unref(operation);
}
// If the server doesn't have a default sink to give us
// we should attempt to pick up the first of the list of sinks which doesn't have
// the name 'auto_null' (that was all really I was doing before)
static void
pm_sink_info_callback (pa_context *c,
const pa_sink_info *sink,
int eol,
void* userdata)
{
if (eol > 0) {
return;
}
else {
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("sink info callback - our user data is not what we think it should be");
return;
}
ActiveSink* a_sink = ACTIVE_SINK (userdata);
if (active_sink_is_populated (a_sink) == FALSE &&
g_ascii_strncasecmp("auto_null", sink->name, 9) != 0){
active_sink_populate (a_sink, sink);
}
}
}
static void
pm_default_sink_info_callback (pa_context *c,
const pa_sink_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else {
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("Default sink info callback - our user data is not what we think it should be");
return;
}
g_debug ("server has handed us a default sink");
active_sink_populate (ACTIVE_SINK (userdata), info);
}
}
static void
pm_sink_input_info_callback (pa_context *c,
const pa_sink_input_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else {
if (info == NULL) {
// TODO: watch this carefully - PA async api should not be doing this . . .
g_warning("\n Sink input info callback : SINK INPUT INFO IS NULL BUT EOL was not POSITIVE!!!");
return;
}
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("sink input info callback - our user data is not what we think it should be");
return;
}
// Check if this is Voip sink input
gint result = pa_proplist_contains (info->proplist, PA_PROP_MEDIA_ROLE);
ActiveSink* a_sink = ACTIVE_SINK (userdata);
if (result == 1){
g_debug ("Sink input info has media role property");
const char* value = pa_proplist_gets (info->proplist, PA_PROP_MEDIA_ROLE);
g_debug ("prop role = %s", value);
if (g_strcmp0 (value, "phone") == 0) {
g_debug ("And yes its a VOIP app ...");
active_sink_activate_voip_item (a_sink);
// TODO to start with we will assume our source is the same as what this 'client'
// is pointing at. This should probably be more intelligent :
// query for the list of source output info's and going on the name of the client
// from the sink input ensure our voip item is using the right source.
}
}
// And finally check for the mute blocking state
if (active_sink_get_index (a_sink) == info->sink){
active_sink_determine_blocking_state (a_sink);
}
}
}
static void
pm_update_active_sink (pa_context *c,
const pa_sink_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else{
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("update_active_sink - our user data is not what we think it should be");
return;
}
active_sink_update (ACTIVE_SINK(userdata), info);
}
}
static void
pm_toggle_mute_for_every_sink_callback (pa_context *c,
const pa_sink_info *sink,
int eol,
void* userdata)
{
if (eol > 0) {
return;
}
else {
pa_operation_unref (pa_context_set_sink_mute_by_index (c,
sink->index,
GPOINTER_TO_INT(userdata),
NULL,
NULL));
}
}
// Source info related callbacks
static void
pm_default_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else {
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("Default sink info callback - our user data is not what we think it should be");
return;
}
g_debug ("server has handed us a default source");
active_sink_update_voip_input_source (ACTIVE_SINK (userdata), info);
}
}
static void
pm_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else {
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("Default sink info callback - our user data is not what we think it should be");
return;
}
// For now we will take the first available
if (active_sink_is_voip_source_populated (ACTIVE_SINK (userdata)) == FALSE){
active_sink_update_voip_input_source (ACTIVE_SINK (userdata), info);
}
}
}
static void
pm_update_source_info_callback (pa_context *c,
const pa_source_info *info,
int eol,
void *userdata)
{
if (eol > 0) {
return;
}
else {
if (IS_ACTIVE_SINK (userdata) == FALSE){
g_warning ("Default sink info callback - our user data is not what we think it should be");
return;
}
g_debug ("Got a source update for %s , index %i", info->name, info->index);
active_sink_update_voip_input_source (ACTIVE_SINK (userdata), info);
}
}
|