-
Notifications
You must be signed in to change notification settings - Fork 0
/
pactl.c
513 lines (405 loc) · 12.9 KB
/
pactl.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
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <locale.h>
#include <ctype.h>
#include <time.h>
#include <sndfile.h>
#include <pulse/pulseaudio.h>
#include <pulse/ext-device-restore.h>
#include <pulsecore/i18n.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/sndfile-util.h>
static void quit(int);
double active_sink_vol = 0.0f;
char active_sink_port[1000];
typedef struct node {
char node_id[1000]; // sink-index, or sink-input-index
bool is_sink;
uint64_t idx;
double vol;
char *node_desc;
FILE *logfile;
struct node *next;
} node;
node* ll_head = NULL;
node* new_node(const char* node_id, const char *fname, uint64_t idx, bool is_sink);
void log_node(node *n);
void remove_node(node *n);
node* get_node_with_id(const char *node_id);
void update_vol_with_node_id(const char *node_id, double vol);
void append_to_list(node *n);
void remove_from_list(const char *node_id);
void cleanup_all_nodes();
void remove_node(node *n) {
// mark vol = 0 for this node
n->vol = -1.0f;
log_node(n);
if (!fclose(n->logfile))
printf("Closed file for node# %s\n", n->node_id);
else {
printf("Failed to close file for node# %s", n->node_id);
quit(1);
}
free(n);
}
node* new_node(const char* node_id, const char *fname, uint64_t idx, bool is_sink) {
node* n;
n = (node*) malloc(sizeof(node));
strcpy(n->node_id, node_id);
n->vol = 0.0f;
n->is_sink = is_sink;
n->idx = idx;
n->next = NULL;
printf("Creating a new node id %s, fname %s\n", node_id, fname);
n->logfile = fopen(fname, "a+");
if (!n->logfile) {
printf("Failed to open file %s for node_id %s. \n", fname, node_id);
quit(1);
}
return n;
}
void log_node(node *n) {
// assumes that the file stream is opened at node creation and
// has not been closed
fprintf(n->logfile, "%lu,%lu,%s,%0.6f,%0.6f\n",
(unsigned long)time(NULL),
n->idx,
active_sink_port,
n->vol,
(n->is_sink) ? n->vol : n->vol * active_sink_vol
);
fflush(n->logfile);
}
void log_all_nodes() {
node *cur = ll_head;
while (cur != NULL) {
log_node(cur);
cur = cur->next;
}
}
void append_to_list(node* n) {
n->next = ll_head;
ll_head = n;
}
void remove_from_list(const char* node_id) {
node *cur, *prev;
cur = ll_head;
if (cur == NULL) {
printf("Failed to remove from an empty list.\n");
return;
}
prev = cur;
while (cur != NULL) {
if (strcmp(cur->node_id, node_id) == 0)
break;
prev = cur;
cur = cur->next;
}
if (cur == NULL) {
printf("Failed to remove node which doesn't exist.\n");
return;
}
// got the item at cur
// prev == cur : the first node
if (prev == cur) {
ll_head = cur->next;
}
prev->next = cur->next;
remove_node(cur);
}
node* get_node_with_id(const char* node_id) {
node *cur = ll_head;
while (cur != NULL) {
if (strcmp(node_id, cur->node_id) == 0) {
return cur;
}
cur = cur->next;
}
return cur;
}
void update_vol_with_node_id(const char* node_id, double vol) {
node *n = get_node_with_id(node_id);
if (n == NULL) {
printf("Node with id %s not found.\n", node_id);
return;
}
n->vol = vol;
log_node(n);
}
void cleanup_all_nodes() {
node *cur = ll_head;
while (cur != NULL) {
log_node(cur);
node *temp = cur;
cur = cur->next;
remove_node(temp);
}
}
static pa_context *context = NULL;
static pa_mainloop_api *mainloop_api = NULL;
static char
*list_type = NULL,
*sample_name = NULL,
*sink_name = NULL,
*source_name = NULL,
*module_args = NULL,
*card_name = NULL,
*profile_name = NULL,
*port_name = NULL,
*formats = NULL;
static bool short_list_format = false;
static pa_proplist *proplist = NULL;
static SNDFILE *sndfile = NULL;
static pa_stream *sample_stream = NULL;
/* This variable tracks the number of ongoing asynchronous operations. When a
* new operation begins, this is incremented simply with actions++, and when
* an operation finishes, this is decremented with the complete_action()
* function, which shuts down the program if actions reaches zero. */
static int actions = 0;
static void quit(int ret) {
pa_assert(mainloop_api);
mainloop_api->quit(mainloop_api, ret);
}
static void context_drain_complete(pa_context *c, void *userdata) {
pa_context_disconnect(c);
}
static void drain(void) {
pa_operation *o;
if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
pa_context_disconnect(context);
else
pa_operation_unref(o);
}
static void complete_action(void) {
pa_assert(actions > 0);
if (!(--actions))
drain();
}
static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
uint64_t vol_perc;
double vol;
if (is_last < 0) {
pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
/* quit(1); */
return;
}
if (is_last) {
complete_action();
return;
}
pa_assert(i);
vol_perc = ((uint64_t)pa_cvolume_avg(&i->volume) * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM;
vol = (i->mute || i->state != PA_SINK_RUNNING) ? 0.0f : vol_perc / 100.00f;
char fname[1000], node_id[1000];
sprintf(node_id, "sink-%u-%s", i->index, i->active_port->name);
sprintf(fname, "sink-%s.log", i->active_port->name);
if (i->state != PA_SINK_IDLE) {
strcpy(active_sink_port, i->active_port->name);
active_sink_vol = vol;
}
printf("%s %s %lf\n", node_id, fname, vol);
if (get_node_with_id(node_id) == NULL) {
printf("creating node with id %s\n", node_id);
node *n = new_node(node_id, fname, i->index, true);
append_to_list(n);
}
update_vol_with_node_id(node_id, vol);
log_all_nodes();
}
static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
uint64_t vol_perc;
double vol;
const char *application_name, *application_process_binary;
if (is_last < 0) {
pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
/* quit(1); */
return;
}
if (is_last) {
complete_action();
return;
}
pa_assert(i);
vol_perc = ((uint64_t)pa_cvolume_avg(&i->volume) * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM;
vol = (i->corked || i->mute) ? 0.0f : vol_perc / 100.00f;
application_process_binary = pa_proplist_gets(i->proplist, "application.process.binary");
application_name = pa_proplist_gets(i->proplist, "application.name");
// here we form a node and insert it to the list
char fname[1000], node_id[1000];
sprintf(node_id, "sink_input-%u", i->index);
sprintf(fname, "sink_input-%s.log", application_process_binary);
printf("%s %s %lf\n", node_id, fname, vol);
if (get_node_with_id(node_id) == NULL) {
printf("creating node with id %s\n", node_id);
node *n = new_node(node_id, fname, i->index, false);
append_to_list(n);
}
update_vol_with_node_id(node_id, vol);
}
/* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
#define MAX_FORMATS 256
static void context_subscribe_callback(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
pa_assert(c);
pa_operation *o = NULL;
switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
case PA_SUBSCRIPTION_EVENT_SINK:
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_REMOVE)
o = pa_context_get_sink_info_by_index(c, idx, get_sink_info_callback, NULL);
else {
// this doesn't happen on my machine, so no worries for now
log_all_nodes();
}
break;
case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_REMOVE) {
o = pa_context_get_sink_input_info(c, idx, get_sink_input_info_callback, NULL);
} else {
char node_id[1000];
sprintf(node_id, "sink_input-%u", idx);
printf("removing from list nodeid %s\n", node_id);
remove_from_list(node_id);
}
break;
}
if (o) {
pa_operation_unref(o);
actions++;
}
fflush(stdout);
}
static void context_state_callback(pa_context *c, void *userdata) {
pa_operation *o = NULL;
pa_assert(c);
switch (pa_context_get_state(c)) {
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
break;
case PA_CONTEXT_READY:
// first subscribe to sink and sink_input events
pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
o = pa_context_subscribe(c,
PA_SUBSCRIPTION_MASK_SINK|
PA_SUBSCRIPTION_MASK_SINK_INPUT,
NULL,
NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
// second list current sinks
o = pa_context_get_sink_info_list(c, get_sink_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
// third list current sink_inputs
o = pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL);
if (o) {
pa_operation_unref(o);
actions++;
}
if (actions < 3) {
pa_log("Operation failed: %s", pa_strerror(pa_context_errno(c)));
quit(1);
}
break;
case PA_CONTEXT_TERMINATED:
quit(0);
break;
case PA_CONTEXT_FAILED:
default:
pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c)));
quit(1);
}
}
static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
pa_log(_("Got SIGINT, exiting."));
cleanup_all_nodes();
quit(0);
}
enum {
ARG_VERSION = 256
};
int main(int argc, char *argv[]) {
pa_mainloop *m = NULL;
int ret = 1;
char *server = NULL;
setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
#endif
proplist = pa_proplist_new();
if (!(m = pa_mainloop_new())) {
pa_log(_("pa_mainloop_new() failed."));
goto quit;
}
mainloop_api = pa_mainloop_get_api(m);
pa_assert_se(pa_signal_init(mainloop_api) == 0);
pa_signal_new(SIGINT, exit_signal_callback, NULL);
pa_signal_new(SIGTERM, exit_signal_callback, NULL);
pa_disable_sigpipe();
if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
pa_log(_("pa_context_new() failed."));
goto quit;
}
pa_context_set_state_callback(context, context_state_callback, NULL);
if (pa_context_connect(context, server, 0, NULL) < 0) {
pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
goto quit;
}
if (pa_mainloop_run(m, &ret) < 0) {
pa_log(_("pa_mainloop_run() failed."));
goto quit;
}
// cleanup routine
quit:
if (sample_stream)
pa_stream_unref(sample_stream);
if (context)
pa_context_unref(context);
if (m) {
pa_signal_done();
pa_mainloop_free(m);
}
pa_xfree(server);
pa_xfree(list_type);
pa_xfree(sample_name);
pa_xfree(sink_name);
pa_xfree(source_name);
pa_xfree(module_args);
pa_xfree(card_name);
pa_xfree(profile_name);
pa_xfree(port_name);
pa_xfree(formats);
if (sndfile)
sf_close(sndfile);
if (proplist)
pa_proplist_free(proplist);
return ret;
}