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