]> code.delx.au - pulseaudio/blob - src/utils/pactl.c
Merge commit 'origin/master' into master-tx
[pulseaudio] / src / utils / pactl.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <getopt.h>
35 #include <locale.h>
36
37 #include <sndfile.h>
38
39 #include <pulse/i18n.h>
40 #include <pulse/pulseaudio.h>
41
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/sndfile-util.h>
46
47 #define BUFSIZE (16*1024)
48
49 static pa_context *context = NULL;
50 static pa_mainloop_api *mainloop_api = NULL;
51
52 static char
53 *device = NULL,
54 *sample_name = NULL,
55 *sink_name = NULL,
56 *source_name = NULL,
57 *module_name = NULL,
58 *module_args = NULL,
59 *card_name = NULL,
60 *profile_name = NULL,
61 *port_name = NULL;
62
63 static uint32_t
64 sink_input_idx = PA_INVALID_INDEX,
65 source_output_idx = PA_INVALID_INDEX;
66
67 static uint32_t module_index;
68 static pa_bool_t suspend;
69
70 static pa_proplist *proplist = NULL;
71
72 static SNDFILE *sndfile = NULL;
73 static pa_stream *sample_stream = NULL;
74 static pa_sample_spec sample_spec;
75 static pa_channel_map channel_map;
76 static size_t sample_length = 0;
77
78 static int actions = 1;
79
80 static pa_bool_t nl = FALSE;
81
82 static enum {
83 NONE,
84 EXIT,
85 STAT,
86 UPLOAD_SAMPLE,
87 PLAY_SAMPLE,
88 REMOVE_SAMPLE,
89 LIST,
90 MOVE_SINK_INPUT,
91 MOVE_SOURCE_OUTPUT,
92 LOAD_MODULE,
93 UNLOAD_MODULE,
94 SUSPEND_SINK,
95 SUSPEND_SOURCE,
96 SET_CARD_PROFILE,
97 SET_SINK_PORT,
98 SET_SOURCE_PORT
99 } action = NONE;
100
101 static void quit(int ret) {
102 pa_assert(mainloop_api);
103 mainloop_api->quit(mainloop_api, ret);
104 }
105
106 static void context_drain_complete(pa_context *c, void *userdata) {
107 pa_context_disconnect(c);
108 }
109
110 static void drain(void) {
111 pa_operation *o;
112 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
113 pa_context_disconnect(context);
114 else
115 pa_operation_unref(o);
116 }
117
118 static void complete_action(void) {
119 pa_assert(actions > 0);
120
121 if (!(--actions))
122 drain();
123 }
124
125 static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) {
126 char s[128];
127 if (!i) {
128 pa_log(_("Failed to get statistics: %s\n"), pa_strerror(pa_context_errno(c)));
129 quit(1);
130 return;
131 }
132
133 pa_bytes_snprint(s, sizeof(s), i->memblock_total_size);
134 printf(_("Currently in use: %u blocks containing %s bytes total.\n"), i->memblock_total, s);
135
136 pa_bytes_snprint(s, sizeof(s), i->memblock_allocated_size);
137 printf(_("Allocated during whole lifetime: %u blocks containing %s bytes total.\n"), i->memblock_allocated, s);
138
139 pa_bytes_snprint(s, sizeof(s), i->scache_size);
140 printf(_("Sample cache size: %s\n"), s);
141
142 complete_action();
143 }
144
145 static void get_server_info_callback(pa_context *c, const pa_server_info *i, void *useerdata) {
146 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
147
148 if (!i) {
149 pa_log(_("Failed to get server information: %s\n"), pa_strerror(pa_context_errno(c)));
150 quit(1);
151 return;
152 }
153
154 pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
155 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map);
156
157 printf(_("User name: %s\n"
158 "Host Name: %s\n"
159 "Server Name: %s\n"
160 "Server Version: %s\n"
161 "Default Sample Specification: %s\n"
162 "Default Channel Map: %s\n"
163 "Default Sink: %s\n"
164 "Default Source: %s\n"
165 "Cookie: %08x\n"),
166 i->user_name,
167 i->host_name,
168 i->server_name,
169 i->server_version,
170 ss,
171 cm,
172 i->default_sink_name,
173 i->default_source_name,
174 i->cookie);
175
176 complete_action();
177 }
178
179 static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
180
181 static const char *state_table[] = {
182 [1+PA_SINK_INVALID_STATE] = "n/a",
183 [1+PA_SINK_RUNNING] = "RUNNING",
184 [1+PA_SINK_IDLE] = "IDLE",
185 [1+PA_SINK_SUSPENDED] = "SUSPENDED"
186 };
187
188 char
189 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
190 cv[PA_CVOLUME_SNPRINT_MAX],
191 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
192 v[PA_VOLUME_SNPRINT_MAX],
193 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
194 cm[PA_CHANNEL_MAP_SNPRINT_MAX];
195 char *pl;
196
197 if (is_last < 0) {
198 pa_log(_("Failed to get sink information: %s\n"), pa_strerror(pa_context_errno(c)));
199 quit(1);
200 return;
201 }
202
203 if (is_last) {
204 complete_action();
205 return;
206 }
207
208 pa_assert(i);
209
210 if (nl)
211 printf("\n");
212 nl = TRUE;
213
214 printf(_("Sink #%u\n"
215 "\tState: %s\n"
216 "\tName: %s\n"
217 "\tDescription: %s\n"
218 "\tDriver: %s\n"
219 "\tSample Specification: %s\n"
220 "\tChannel Map: %s\n"
221 "\tOwner Module: %u\n"
222 "\tMute: %s\n"
223 "\tVolume: %s%s%s\n"
224 "\t balance %0.2f\n"
225 "\tBase Volume: %s%s%s\n"
226 "\tMonitor Source: %s\n"
227 "\tLatency: %0.0f usec, configured %0.0f usec\n"
228 "\tFlags: %s%s%s%s%s%s\n"
229 "\tProperties:\n\t\t%s\n"),
230 i->index,
231 state_table[1+i->state],
232 i->name,
233 pa_strnull(i->description),
234 pa_strnull(i->driver),
235 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
236 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
237 i->owner_module,
238 pa_yes_no(i->mute),
239 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
240 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
241 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
242 pa_cvolume_get_balance(&i->volume, &i->channel_map),
243 pa_volume_snprint(v, sizeof(v), i->base_volume),
244 i->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t " : "",
245 i->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
246 pa_strnull(i->monitor_source_name),
247 (double) i->latency, (double) i->configured_latency,
248 i->flags & PA_SINK_HARDWARE ? "HARDWARE " : "",
249 i->flags & PA_SINK_NETWORK ? "NETWORK " : "",
250 i->flags & PA_SINK_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
251 i->flags & PA_SINK_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
252 i->flags & PA_SINK_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
253 i->flags & PA_SINK_LATENCY ? "LATENCY " : "",
254 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
255
256 pa_xfree(pl);
257
258 if (i->ports) {
259 pa_sink_port_info **p;
260
261 printf(_("\tPorts:\n"));
262 for (p = i->ports; *p; p++)
263 printf("\t\t%s: %s (priority. %u)\n", (*p)->name, (*p)->description, (*p)->priority);
264 }
265
266 if (i->active_port)
267 printf(_("\tActive Port: %s\n"),
268 i->active_port->name);
269 }
270
271 static void get_source_info_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
272
273 static const char *state_table[] = {
274 [1+PA_SOURCE_INVALID_STATE] = "n/a",
275 [1+PA_SOURCE_RUNNING] = "RUNNING",
276 [1+PA_SOURCE_IDLE] = "IDLE",
277 [1+PA_SOURCE_SUSPENDED] = "SUSPENDED"
278 };
279
280 char
281 s[PA_SAMPLE_SPEC_SNPRINT_MAX],
282 cv[PA_CVOLUME_SNPRINT_MAX],
283 cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX],
284 v[PA_VOLUME_SNPRINT_MAX],
285 vdb[PA_SW_VOLUME_SNPRINT_DB_MAX],
286 cm[PA_CHANNEL_MAP_SNPRINT_MAX];
287 char *pl;
288
289 if (is_last < 0) {
290 pa_log(_("Failed to get source information: %s\n"), pa_strerror(pa_context_errno(c)));
291 quit(1);
292 return;
293 }
294
295 if (is_last) {
296 complete_action();
297 return;
298 }
299
300 pa_assert(i);
301
302 if (nl)
303 printf("\n");
304 nl = TRUE;
305
306 printf(_("Source #%u\n"
307 "\tState: %s\n"
308 "\tName: %s\n"
309 "\tDescription: %s\n"
310 "\tDriver: %s\n"
311 "\tSample Specification: %s\n"
312 "\tChannel Map: %s\n"
313 "\tOwner Module: %u\n"
314 "\tMute: %s\n"
315 "\tVolume: %s%s%s\n"
316 "\t balance %0.2f\n"
317 "\tBase Volume: %s%s%s\n"
318 "\tMonitor of Sink: %s\n"
319 "\tLatency: %0.0f usec, configured %0.0f usec\n"
320 "\tFlags: %s%s%s%s%s%s\n"
321 "\tProperties:\n\t\t%s\n"),
322 i->index,
323 state_table[1+i->state],
324 i->name,
325 pa_strnull(i->description),
326 pa_strnull(i->driver),
327 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
328 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
329 i->owner_module,
330 pa_yes_no(i->mute),
331 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
332 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
333 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume) : "",
334 pa_cvolume_get_balance(&i->volume, &i->channel_map),
335 pa_volume_snprint(v, sizeof(v), i->base_volume),
336 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "",
337 i->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), i->base_volume) : "",
338 i->monitor_of_sink_name ? i->monitor_of_sink_name : _("n/a"),
339 (double) i->latency, (double) i->configured_latency,
340 i->flags & PA_SOURCE_HARDWARE ? "HARDWARE " : "",
341 i->flags & PA_SOURCE_NETWORK ? "NETWORK " : "",
342 i->flags & PA_SOURCE_HW_MUTE_CTRL ? "HW_MUTE_CTRL " : "",
343 i->flags & PA_SOURCE_HW_VOLUME_CTRL ? "HW_VOLUME_CTRL " : "",
344 i->flags & PA_SOURCE_DECIBEL_VOLUME ? "DECIBEL_VOLUME " : "",
345 i->flags & PA_SOURCE_LATENCY ? "LATENCY " : "",
346 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
347
348 pa_xfree(pl);
349
350 if (i->ports) {
351 pa_source_port_info **p;
352
353 printf(_("\tPorts:\n"));
354 for (p = i->ports; *p; p++)
355 printf("\t\t%s: %s (priority. %u)\n", (*p)->name, (*p)->description, (*p)->priority);
356 }
357
358 if (i->active_port)
359 printf(_("\tActive Port: %s\n"),
360 i->active_port->name);
361 }
362
363 static void get_module_info_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
364 char t[32];
365 char *pl;
366
367 if (is_last < 0) {
368 pa_log(_("Failed to get module information: %s\n"), pa_strerror(pa_context_errno(c)));
369 quit(1);
370 return;
371 }
372
373 if (is_last) {
374 complete_action();
375 return;
376 }
377
378 pa_assert(i);
379
380 if (nl)
381 printf("\n");
382 nl = TRUE;
383
384 pa_snprintf(t, sizeof(t), "%u", i->n_used);
385
386 printf(_("Module #%u\n"
387 "\tName: %s\n"
388 "\tArgument: %s\n"
389 "\tUsage counter: %s\n"
390 "\tProperties:\n\t\t%s\n"),
391 i->index,
392 i->name,
393 i->argument ? i->argument : "",
394 i->n_used != PA_INVALID_INDEX ? t : _("n/a"),
395 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
396
397 pa_xfree(pl);
398 }
399
400 static void get_client_info_callback(pa_context *c, const pa_client_info *i, int is_last, void *userdata) {
401 char t[32];
402 char *pl;
403
404 if (is_last < 0) {
405 pa_log(_("Failed to get client information: %s\n"), pa_strerror(pa_context_errno(c)));
406 quit(1);
407 return;
408 }
409
410 if (is_last) {
411 complete_action();
412 return;
413 }
414
415 pa_assert(i);
416
417 if (nl)
418 printf("\n");
419 nl = TRUE;
420
421 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
422
423 printf(_("Client #%u\n"
424 "\tDriver: %s\n"
425 "\tOwner Module: %s\n"
426 "\tProperties:\n\t\t%s\n"),
427 i->index,
428 pa_strnull(i->driver),
429 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
430 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
431
432 pa_xfree(pl);
433 }
434
435 static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_last, void *userdata) {
436 char t[32];
437 char *pl;
438
439 if (is_last < 0) {
440 pa_log(_("Failed to get card information: %s\n"), pa_strerror(pa_context_errno(c)));
441 complete_action();
442 return;
443 }
444
445 if (is_last) {
446 complete_action();
447 return;
448 }
449
450 pa_assert(i);
451
452 if (nl)
453 printf("\n");
454 nl = TRUE;
455
456 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
457
458 printf(_("Card #%u\n"
459 "\tName: %s\n"
460 "\tDriver: %s\n"
461 "\tOwner Module: %s\n"
462 "\tProperties:\n\t\t%s\n"),
463 i->index,
464 i->name,
465 pa_strnull(i->driver),
466 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
467 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
468
469 if (i->profiles) {
470 pa_card_profile_info *p;
471
472 printf(_("\tProfiles:\n"));
473 for (p = i->profiles; p->name; p++)
474 printf("\t\t%s: %s (sinks: %u, sources: %u, priority. %u)\n", p->name, p->description, p->n_sinks, p->n_sources, p->priority);
475 }
476
477 if (i->active_profile)
478 printf(_("\tActive Profile: %s\n"),
479 i->active_profile->name);
480
481 pa_xfree(pl);
482 }
483
484 static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
485 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
486 char *pl;
487
488 if (is_last < 0) {
489 pa_log(_("Failed to get sink input information: %s\n"), pa_strerror(pa_context_errno(c)));
490 quit(1);
491 return;
492 }
493
494 if (is_last) {
495 complete_action();
496 return;
497 }
498
499 pa_assert(i);
500
501 if (nl)
502 printf("\n");
503 nl = TRUE;
504
505 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
506 pa_snprintf(k, sizeof(k), "%u", i->client);
507
508 printf(_("Sink Input #%u\n"
509 "\tDriver: %s\n"
510 "\tOwner Module: %s\n"
511 "\tClient: %s\n"
512 "\tSink: %u\n"
513 "\tSample Specification: %s\n"
514 "\tChannel Map: %s\n"
515 "\tMute: %s\n"
516 "\tVolume: %s\n"
517 "\t %s\n"
518 "\t balance %0.2f\n"
519 "\tBuffer Latency: %0.0f usec\n"
520 "\tSink Latency: %0.0f usec\n"
521 "\tResample method: %s\n"
522 "\tProperties:\n\t\t%s\n"),
523 i->index,
524 pa_strnull(i->driver),
525 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
526 i->client != PA_INVALID_INDEX ? k : _("n/a"),
527 i->sink,
528 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
529 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
530 pa_yes_no(i->mute),
531 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
532 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
533 pa_cvolume_get_balance(&i->volume, &i->channel_map),
534 (double) i->buffer_usec,
535 (double) i->sink_usec,
536 i->resample_method ? i->resample_method : _("n/a"),
537 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
538
539 pa_xfree(pl);
540 }
541
542 static void get_source_output_info_callback(pa_context *c, const pa_source_output_info *i, int is_last, void *userdata) {
543 char t[32], k[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
544 char *pl;
545
546 if (is_last < 0) {
547 pa_log(_("Failed to get source output information: %s\n"), pa_strerror(pa_context_errno(c)));
548 quit(1);
549 return;
550 }
551
552 if (is_last) {
553 complete_action();
554 return;
555 }
556
557 pa_assert(i);
558
559 if (nl)
560 printf("\n");
561 nl = TRUE;
562
563
564 pa_snprintf(t, sizeof(t), "%u", i->owner_module);
565 pa_snprintf(k, sizeof(k), "%u", i->client);
566
567 printf(_("Source Output #%u\n"
568 "\tDriver: %s\n"
569 "\tOwner Module: %s\n"
570 "\tClient: %s\n"
571 "\tSource: %u\n"
572 "\tSample Specification: %s\n"
573 "\tChannel Map: %s\n"
574 "\tBuffer Latency: %0.0f usec\n"
575 "\tSource Latency: %0.0f usec\n"
576 "\tResample method: %s\n"
577 "\tProperties:\n\t\t%s\n"),
578 i->index,
579 pa_strnull(i->driver),
580 i->owner_module != PA_INVALID_INDEX ? t : _("n/a"),
581 i->client != PA_INVALID_INDEX ? k : _("n/a"),
582 i->source,
583 pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec),
584 pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map),
585 (double) i->buffer_usec,
586 (double) i->source_usec,
587 i->resample_method ? i->resample_method : _("n/a"),
588 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
589
590 pa_xfree(pl);
591 }
592
593 static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int is_last, void *userdata) {
594 char t[32], s[PA_SAMPLE_SPEC_SNPRINT_MAX], cv[PA_CVOLUME_SNPRINT_MAX], cvdb[PA_SW_CVOLUME_SNPRINT_DB_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
595 char *pl;
596
597 if (is_last < 0) {
598 pa_log(_("Failed to get sample information: %s\n"), pa_strerror(pa_context_errno(c)));
599 quit(1);
600 return;
601 }
602
603 if (is_last) {
604 complete_action();
605 return;
606 }
607
608 pa_assert(i);
609
610 if (nl)
611 printf("\n");
612 nl = TRUE;
613
614 pa_bytes_snprint(t, sizeof(t), i->bytes);
615
616 printf(_("Sample #%u\n"
617 "\tName: %s\n"
618 "\tSample Specification: %s\n"
619 "\tChannel Map: %s\n"
620 "\tVolume: %s\n"
621 "\t %s\n"
622 "\t balance %0.2f\n"
623 "\tDuration: %0.1fs\n"
624 "\tSize: %s\n"
625 "\tLazy: %s\n"
626 "\tFilename: %s\n"
627 "\tProperties:\n\t\t%s\n"),
628 i->index,
629 i->name,
630 pa_sample_spec_valid(&i->sample_spec) ? pa_sample_spec_snprint(s, sizeof(s), &i->sample_spec) : _("n/a"),
631 pa_sample_spec_valid(&i->sample_spec) ? pa_channel_map_snprint(cm, sizeof(cm), &i->channel_map) : _("n/a"),
632 pa_cvolume_snprint(cv, sizeof(cv), &i->volume),
633 pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), &i->volume),
634 pa_cvolume_get_balance(&i->volume, &i->channel_map),
635 (double) i->duration/1000000.0,
636 t,
637 pa_yes_no(i->lazy),
638 i->filename ? i->filename : _("n/a"),
639 pl = pa_proplist_to_string_sep(i->proplist, "\n\t\t"));
640
641 pa_xfree(pl);
642 }
643
644 static void simple_callback(pa_context *c, int success, void *userdata) {
645 if (!success) {
646 pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
647 quit(1);
648 return;
649 }
650
651 complete_action();
652 }
653
654 static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
655 if (idx == PA_INVALID_INDEX) {
656 pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c)));
657 quit(1);
658 return;
659 }
660
661 printf("%u\n", idx);
662
663 complete_action();
664 }
665
666 static void stream_state_callback(pa_stream *s, void *userdata) {
667 pa_assert(s);
668
669 switch (pa_stream_get_state(s)) {
670 case PA_STREAM_CREATING:
671 case PA_STREAM_READY:
672 break;
673
674 case PA_STREAM_TERMINATED:
675 drain();
676 break;
677
678 case PA_STREAM_FAILED:
679 default:
680 pa_log(_("Failed to upload sample: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
681 quit(1);
682 }
683 }
684
685 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
686 sf_count_t l;
687 float *d;
688 pa_assert(s && length && sndfile);
689
690 d = pa_xmalloc(length);
691
692 pa_assert(sample_length >= length);
693 l = (sf_count_t) (length/pa_frame_size(&sample_spec));
694
695 if ((sf_readf_float(sndfile, d, l)) != l) {
696 pa_xfree(d);
697 pa_log(_("Premature end of file\n"));
698 quit(1);
699 return;
700 }
701
702 pa_stream_write(s, d, length, pa_xfree, 0, PA_SEEK_RELATIVE);
703
704 sample_length -= length;
705
706 if (sample_length <= 0) {
707 pa_stream_set_write_callback(sample_stream, NULL, NULL);
708 pa_stream_finish_upload(sample_stream);
709 }
710 }
711
712 static void context_state_callback(pa_context *c, void *userdata) {
713 pa_assert(c);
714 switch (pa_context_get_state(c)) {
715 case PA_CONTEXT_CONNECTING:
716 case PA_CONTEXT_AUTHORIZING:
717 case PA_CONTEXT_SETTING_NAME:
718 break;
719
720 case PA_CONTEXT_READY:
721 switch (action) {
722 case STAT:
723 actions = 2;
724 pa_operation_unref(pa_context_stat(c, stat_callback, NULL));
725 pa_operation_unref(pa_context_get_server_info(c, get_server_info_callback, NULL));
726 break;
727
728 case PLAY_SAMPLE:
729 pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL));
730 break;
731
732 case REMOVE_SAMPLE:
733 pa_operation_unref(pa_context_remove_sample(c, sample_name, simple_callback, NULL));
734 break;
735
736 case UPLOAD_SAMPLE:
737 sample_stream = pa_stream_new(c, sample_name, &sample_spec, NULL);
738 pa_assert(sample_stream);
739
740 pa_stream_set_state_callback(sample_stream, stream_state_callback, NULL);
741 pa_stream_set_write_callback(sample_stream, stream_write_callback, NULL);
742 pa_stream_connect_upload(sample_stream, sample_length);
743 break;
744
745 case EXIT:
746 pa_operation_unref(pa_context_exit_daemon(c, simple_callback, NULL));
747 break;
748
749 case LIST:
750 actions = 8;
751 pa_operation_unref(pa_context_get_module_info_list(c, get_module_info_callback, NULL));
752 pa_operation_unref(pa_context_get_sink_info_list(c, get_sink_info_callback, NULL));
753 pa_operation_unref(pa_context_get_source_info_list(c, get_source_info_callback, NULL));
754 pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, NULL));
755 pa_operation_unref(pa_context_get_source_output_info_list(c, get_source_output_info_callback, NULL));
756 pa_operation_unref(pa_context_get_client_info_list(c, get_client_info_callback, NULL));
757 pa_operation_unref(pa_context_get_sample_info_list(c, get_sample_info_callback, NULL));
758 pa_operation_unref(pa_context_get_card_info_list(c, get_card_info_callback, NULL));
759 break;
760
761 case MOVE_SINK_INPUT:
762 pa_operation_unref(pa_context_move_sink_input_by_name(c, sink_input_idx, sink_name, simple_callback, NULL));
763 break;
764
765 case MOVE_SOURCE_OUTPUT:
766 pa_operation_unref(pa_context_move_source_output_by_name(c, source_output_idx, source_name, simple_callback, NULL));
767 break;
768
769 case LOAD_MODULE:
770 pa_operation_unref(pa_context_load_module(c, module_name, module_args, index_callback, NULL));
771 break;
772
773 case UNLOAD_MODULE:
774 pa_operation_unref(pa_context_unload_module(c, module_index, simple_callback, NULL));
775 break;
776
777 case SUSPEND_SINK:
778 if (sink_name)
779 pa_operation_unref(pa_context_suspend_sink_by_name(c, sink_name, suspend, simple_callback, NULL));
780 else
781 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
782 break;
783
784 case SUSPEND_SOURCE:
785 if (source_name)
786 pa_operation_unref(pa_context_suspend_source_by_name(c, source_name, suspend, simple_callback, NULL));
787 else
788 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, suspend, simple_callback, NULL));
789 break;
790
791 case SET_CARD_PROFILE:
792 pa_operation_unref(pa_context_set_card_profile_by_name(c, card_name, profile_name, simple_callback, NULL));
793 break;
794
795 case SET_SINK_PORT:
796 pa_operation_unref(pa_context_set_sink_port_by_name(c, sink_name, port_name, simple_callback, NULL));
797 break;
798
799 case SET_SOURCE_PORT:
800 pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL));
801 break;
802
803 default:
804 pa_assert_not_reached();
805 }
806 break;
807
808 case PA_CONTEXT_TERMINATED:
809 quit(0);
810 break;
811
812 case PA_CONTEXT_FAILED:
813 default:
814 pa_log(_("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
815 quit(1);
816 }
817 }
818
819 static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
820 pa_log(_("Got SIGINT, exiting.\n"));
821 quit(0);
822 }
823
824 static void help(const char *argv0) {
825
826 printf(_("%s [options] stat\n"
827 "%s [options] list\n"
828 "%s [options] exit\n"
829 "%s [options] upload-sample FILENAME [NAME]\n"
830 "%s [options] play-sample NAME [SINK]\n"
831 "%s [options] remove-sample NAME\n"
832 "%s [options] move-sink-input ID SINK\n"
833 "%s [options] move-source-output ID SOURCE\n"
834 "%s [options] load-module NAME [ARGS ...]\n"
835 "%s [options] unload-module ID\n"
836 "%s [options] suspend-sink [SINK] 1|0\n"
837 "%s [options] suspend-source [SOURCE] 1|0\n"
838 "%s [options] set-card-profile [CARD] [PROFILE] \n"
839 "%s [options] set-sink-port [SINK] [PORT] \n"
840 "%s [options] set-source-port [SOURCE] [PORT] \n\n"
841 " -h, --help Show this help\n"
842 " --version Show version\n\n"
843 " -s, --server=SERVER The name of the server to connect to\n"
844 " -n, --client-name=NAME How to call this client on the server\n"),
845 argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0);
846 }
847
848 enum {
849 ARG_VERSION = 256
850 };
851
852 int main(int argc, char *argv[]) {
853 pa_mainloop* m = NULL;
854 int ret = 1, c;
855 char *server = NULL, *bn;
856
857 static const struct option long_options[] = {
858 {"server", 1, NULL, 's'},
859 {"client-name", 1, NULL, 'n'},
860 {"version", 0, NULL, ARG_VERSION},
861 {"help", 0, NULL, 'h'},
862 {NULL, 0, NULL, 0}
863 };
864
865 setlocale(LC_ALL, "");
866 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
867
868 bn = pa_path_get_filename(argv[0]);
869
870 proplist = pa_proplist_new();
871
872 while ((c = getopt_long(argc, argv, "s:n:h", long_options, NULL)) != -1) {
873 switch (c) {
874 case 'h' :
875 help(bn);
876 ret = 0;
877 goto quit;
878
879 case ARG_VERSION:
880 printf(_("pactl %s\n"
881 "Compiled with libpulse %s\n"
882 "Linked with libpulse %s\n"),
883 PACKAGE_VERSION,
884 pa_get_headers_version(),
885 pa_get_library_version());
886 ret = 0;
887 goto quit;
888
889 case 's':
890 pa_xfree(server);
891 server = pa_xstrdup(optarg);
892 break;
893
894 case 'n': {
895 char *t;
896
897 if (!(t = pa_locale_to_utf8(optarg)) ||
898 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) {
899
900 pa_log(_("Invalid client name '%s'\n"), t ? t : optarg);
901 pa_xfree(t);
902 goto quit;
903 }
904
905 pa_xfree(t);
906 break;
907 }
908
909 default:
910 goto quit;
911 }
912 }
913
914 if (optind < argc) {
915 if (pa_streq(argv[optind], "stat"))
916 action = STAT;
917 else if (pa_streq(argv[optind], "exit"))
918 action = EXIT;
919 else if (pa_streq(argv[optind], "list"))
920 action = LIST;
921 else if (pa_streq(argv[optind], "upload-sample")) {
922 struct SF_INFO sfi;
923 action = UPLOAD_SAMPLE;
924
925 if (optind+1 >= argc) {
926 pa_log(_("Please specify a sample file to load\n"));
927 goto quit;
928 }
929
930 if (optind+2 < argc)
931 sample_name = pa_xstrdup(argv[optind+2]);
932 else {
933 char *f = pa_path_get_filename(argv[optind+1]);
934 sample_name = pa_xstrndup(f, strcspn(f, "."));
935 }
936
937 pa_zero(sfi);
938 if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) {
939 pa_log(_("Failed to open sound file.\n"));
940 goto quit;
941 }
942
943 if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) {
944 pa_log(_("Failed to determine sample specification from file.\n"));
945 goto quit;
946 }
947 sample_spec.format = PA_SAMPLE_FLOAT32;
948
949 if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) {
950 if (sample_spec.channels > 2)
951 pa_log(_("Warning: Failed to determine sample specification from file.\n"));
952 pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
953 }
954
955 pa_assert(pa_channel_map_compatible(&channel_map, &sample_spec));
956 sample_length = (size_t) sfi.frames*pa_frame_size(&sample_spec);
957
958 } else if (pa_streq(argv[optind], "play-sample")) {
959 action = PLAY_SAMPLE;
960 if (argc != optind+2 && argc != optind+3) {
961 pa_log(_("You have to specify a sample name to play\n"));
962 goto quit;
963 }
964
965 sample_name = pa_xstrdup(argv[optind+1]);
966
967 if (optind+2 < argc)
968 device = pa_xstrdup(argv[optind+2]);
969
970 } else if (pa_streq(argv[optind], "remove-sample")) {
971 action = REMOVE_SAMPLE;
972 if (argc != optind+2) {
973 pa_log(_("You have to specify a sample name to remove\n"));
974 goto quit;
975 }
976
977 sample_name = pa_xstrdup(argv[optind+1]);
978
979 } else if (pa_streq(argv[optind], "move-sink-input")) {
980 action = MOVE_SINK_INPUT;
981 if (argc != optind+3) {
982 pa_log(_("You have to specify a sink input index and a sink\n"));
983 goto quit;
984 }
985
986 sink_input_idx = (uint32_t) atoi(argv[optind+1]);
987 sink_name = pa_xstrdup(argv[optind+2]);
988
989 } else if (pa_streq(argv[optind], "move-source-output")) {
990 action = MOVE_SOURCE_OUTPUT;
991 if (argc != optind+3) {
992 pa_log(_("You have to specify a source output index and a source\n"));
993 goto quit;
994 }
995
996 source_output_idx = (uint32_t) atoi(argv[optind+1]);
997 source_name = pa_xstrdup(argv[optind+2]);
998
999 } else if (pa_streq(argv[optind], "load-module")) {
1000 int i;
1001 size_t n = 0;
1002 char *p;
1003
1004 action = LOAD_MODULE;
1005
1006 if (argc <= optind+1) {
1007 pa_log(_("You have to specify a module name and arguments.\n"));
1008 goto quit;
1009 }
1010
1011 module_name = argv[optind+1];
1012
1013 for (i = optind+2; i < argc; i++)
1014 n += strlen(argv[i])+1;
1015
1016 if (n > 0) {
1017 p = module_args = pa_xmalloc(n);
1018
1019 for (i = optind+2; i < argc; i++)
1020 p += sprintf(p, "%s%s", p == module_args ? "" : " ", argv[i]);
1021 }
1022
1023 } else if (pa_streq(argv[optind], "unload-module")) {
1024 action = UNLOAD_MODULE;
1025
1026 if (argc != optind+2) {
1027 pa_log(_("You have to specify a module index\n"));
1028 goto quit;
1029 }
1030
1031 module_index = (uint32_t) atoi(argv[optind+1]);
1032
1033 } else if (pa_streq(argv[optind], "suspend-sink")) {
1034 action = SUSPEND_SINK;
1035
1036 if (argc > optind+3 || optind+1 >= argc) {
1037 pa_log(_("You may not specify more than one sink. You have to specify a boolean value.\n"));
1038 goto quit;
1039 }
1040
1041 suspend = pa_parse_boolean(argv[argc-1]);
1042
1043 if (argc > optind+2)
1044 sink_name = pa_xstrdup(argv[optind+1]);
1045
1046 } else if (pa_streq(argv[optind], "suspend-source")) {
1047 action = SUSPEND_SOURCE;
1048
1049 if (argc > optind+3 || optind+1 >= argc) {
1050 pa_log(_("You may not specify more than one source. You have to specify a boolean value.\n"));
1051 goto quit;
1052 }
1053
1054 suspend = pa_parse_boolean(argv[argc-1]);
1055
1056 if (argc > optind+2)
1057 source_name = pa_xstrdup(argv[optind+1]);
1058 } else if (pa_streq(argv[optind], "set-card-profile")) {
1059 action = SET_CARD_PROFILE;
1060
1061 if (argc != optind+3) {
1062 pa_log(_("You have to specify a card name/index and a profile name\n"));
1063 goto quit;
1064 }
1065
1066 card_name = pa_xstrdup(argv[optind+1]);
1067 profile_name = pa_xstrdup(argv[optind+2]);
1068
1069 } else if (pa_streq(argv[optind], "set-sink-port")) {
1070 action = SET_SINK_PORT;
1071
1072 if (argc != optind+3) {
1073 pa_log(_("You have to specify a sink name/index and a port name\n"));
1074 goto quit;
1075 }
1076
1077 sink_name = pa_xstrdup(argv[optind+1]);
1078 port_name = pa_xstrdup(argv[optind+2]);
1079
1080 } else if (pa_streq(argv[optind], "set-source-port")) {
1081 action = SET_SOURCE_PORT;
1082
1083 if (argc != optind+3) {
1084 pa_log(_("You have to specify a source name/index and a port name\n"));
1085 goto quit;
1086 }
1087
1088 source_name = pa_xstrdup(argv[optind+1]);
1089 port_name = pa_xstrdup(argv[optind+2]);
1090
1091 } else if (pa_streq(argv[optind], "help")) {
1092 help(bn);
1093 ret = 0;
1094 goto quit;
1095 }
1096 }
1097
1098 if (action == NONE) {
1099 pa_log(_("No valid command specified.\n"));
1100 goto quit;
1101 }
1102
1103 if (!(m = pa_mainloop_new())) {
1104 pa_log(_("pa_mainloop_new() failed.\n"));
1105 goto quit;
1106 }
1107
1108 mainloop_api = pa_mainloop_get_api(m);
1109
1110 pa_assert_se(pa_signal_init(mainloop_api) == 0);
1111 pa_signal_new(SIGINT, exit_signal_callback, NULL);
1112 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
1113 pa_disable_sigpipe();
1114
1115 if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) {
1116 pa_log(_("pa_context_new() failed.\n"));
1117 goto quit;
1118 }
1119
1120 pa_context_set_state_callback(context, context_state_callback, NULL);
1121 if (pa_context_connect(context, server, 0, NULL) < 0) {
1122 pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
1123 goto quit;
1124 }
1125
1126 if (pa_mainloop_run(m, &ret) < 0) {
1127 pa_log(_("pa_mainloop_run() failed.\n"));
1128 goto quit;
1129 }
1130
1131 quit:
1132 if (sample_stream)
1133 pa_stream_unref(sample_stream);
1134
1135 if (context)
1136 pa_context_unref(context);
1137
1138 if (m) {
1139 pa_signal_done();
1140 pa_mainloop_free(m);
1141 }
1142
1143 pa_xfree(server);
1144 pa_xfree(device);
1145 pa_xfree(sample_name);
1146 pa_xfree(sink_name);
1147 pa_xfree(source_name);
1148 pa_xfree(module_args);
1149 pa_xfree(card_name);
1150 pa_xfree(profile_name);
1151
1152 if (sndfile)
1153 sf_close(sndfile);
1154
1155 if (proplist)
1156 pa_proplist_free(proplist);
1157
1158 return ret;
1159 }