]> code.delx.au - pulseaudio/blob - src/utils/paplay.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / utils / paplay.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <signal.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <getopt.h>
37 #include <locale.h>
38
39 #include <sndfile.h>
40
41 #include <pulse/pulseaudio.h>
42
43 #if PA_API_VERSION < 9
44 #error Invalid PulseAudio API version
45 #endif
46
47 static pa_context *context = NULL;
48 static pa_stream *stream = NULL;
49 static pa_mainloop_api *mainloop_api = NULL;
50
51 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
52
53 static int verbose = 0;
54 static pa_volume_t volume = PA_VOLUME_NORM;
55
56 static SNDFILE* sndfile = NULL;
57
58 static pa_sample_spec sample_spec = { 0, 0, 0 };
59 static pa_channel_map channel_map;
60 static int channel_map_set = 0;
61
62 static sf_count_t (*readf_function)(SNDFILE *_sndfile, void *ptr, sf_count_t frames) = NULL;
63
64 /* A shortcut for terminating the application */
65 static void quit(int ret) {
66 assert(mainloop_api);
67 mainloop_api->quit(mainloop_api, ret);
68 }
69
70 /* Connection draining complete */
71 static void context_drain_complete(pa_context*c, void *userdata) {
72 pa_context_disconnect(c);
73 }
74
75 /* Stream draining complete */
76 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
77 pa_operation *o;
78
79 if (!success) {
80 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
81 quit(1);
82 }
83
84 if (verbose)
85 fprintf(stderr, "Playback stream drained.\n");
86
87 pa_stream_disconnect(stream);
88 pa_stream_unref(stream);
89 stream = NULL;
90
91 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
92 pa_context_disconnect(context);
93 else {
94 pa_operation_unref(o);
95
96 if (verbose)
97 fprintf(stderr, "Draining connection to server.\n");
98 }
99 }
100
101 /* This is called whenever new data may be written to the stream */
102 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
103 sf_count_t bytes;
104 void *data;
105 assert(s && length);
106
107 if (!sndfile)
108 return;
109
110 data = pa_xmalloc(length);
111
112 if (readf_function) {
113 size_t k = pa_frame_size(&sample_spec);
114
115 if ((bytes = readf_function(sndfile, data, length/k)) > 0)
116 bytes *= k;
117
118 } else
119 bytes = sf_read_raw(sndfile, data, length);
120
121 if (bytes > 0)
122 pa_stream_write(s, data, bytes, pa_xfree, 0, PA_SEEK_RELATIVE);
123 else
124 pa_xfree(data);
125
126 if (bytes < (sf_count_t) length) {
127 sf_close(sndfile);
128 sndfile = NULL;
129 pa_operation_unref(pa_stream_drain(s, stream_drain_complete, NULL));
130 }
131 }
132
133 /* This routine is called whenever the stream state changes */
134 static void stream_state_callback(pa_stream *s, void *userdata) {
135 assert(s);
136
137 switch (pa_stream_get_state(s)) {
138 case PA_STREAM_CREATING:
139 case PA_STREAM_TERMINATED:
140 break;
141
142 case PA_STREAM_READY:
143 if (verbose)
144 fprintf(stderr, "Stream successfully created\n");
145 break;
146
147 case PA_STREAM_FAILED:
148 default:
149 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
150 quit(1);
151 }
152 }
153
154 /* This is called whenever the context status changes */
155 static void context_state_callback(pa_context *c, void *userdata) {
156 assert(c);
157
158 switch (pa_context_get_state(c)) {
159 case PA_CONTEXT_CONNECTING:
160 case PA_CONTEXT_AUTHORIZING:
161 case PA_CONTEXT_SETTING_NAME:
162 break;
163
164 case PA_CONTEXT_READY: {
165 pa_cvolume cv;
166
167 assert(c && !stream);
168
169 if (verbose)
170 fprintf(stderr, "Connection established.\n");
171
172 stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL);
173 assert(stream);
174
175 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
176 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
177 pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL);
178
179 break;
180 }
181
182 case PA_CONTEXT_TERMINATED:
183 quit(0);
184 break;
185
186 case PA_CONTEXT_FAILED:
187 default:
188 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
189 quit(1);
190 }
191 }
192
193 /* UNIX signal to quit recieved */
194 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
195 if (verbose)
196 fprintf(stderr, "Got SIGINT, exiting.\n");
197 quit(0);
198
199 }
200
201 static void help(const char *argv0) {
202
203 printf("%s [options] [FILE]\n\n"
204 " -h, --help Show this help\n"
205 " --version Show version\n\n"
206 " -v, --verbose Enable verbose operations\n\n"
207 " -s, --server=SERVER The name of the server to connect to\n"
208 " -d, --device=DEVICE The name of the sink/source to connect to\n"
209 " -n, --client-name=NAME How to call this client on the server\n"
210 " --stream-name=NAME How to call this stream on the server\n"
211 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
212 " --channel-map=CHANNELMAP Set the channel map to the use\n",
213 argv0);
214 }
215
216 enum {
217 ARG_VERSION = 256,
218 ARG_STREAM_NAME,
219 ARG_VOLUME,
220 ARG_CHANNELMAP
221 };
222
223 int main(int argc, char *argv[]) {
224 pa_mainloop* m = NULL;
225 int ret = 1, r, c;
226 char *bn, *server = NULL;
227 const char *filename;
228 SF_INFO sfinfo;
229
230 static const struct option long_options[] = {
231 {"device", 1, NULL, 'd'},
232 {"server", 1, NULL, 's'},
233 {"client-name", 1, NULL, 'n'},
234 {"stream-name", 1, NULL, ARG_STREAM_NAME},
235 {"version", 0, NULL, ARG_VERSION},
236 {"help", 0, NULL, 'h'},
237 {"verbose", 0, NULL, 'v'},
238 {"volume", 1, NULL, ARG_VOLUME},
239 {"channel-map", 1, NULL, ARG_CHANNELMAP},
240 {NULL, 0, NULL, 0}
241 };
242
243 setlocale(LC_ALL, "");
244
245 if (!(bn = strrchr(argv[0], '/')))
246 bn = argv[0];
247 else
248 bn++;
249
250 while ((c = getopt_long(argc, argv, "d:s:n:hv", long_options, NULL)) != -1) {
251
252 switch (c) {
253 case 'h' :
254 help(bn);
255 ret = 0;
256 goto quit;
257
258 case ARG_VERSION:
259 printf("paplay "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
260 ret = 0;
261 goto quit;
262
263 case 'd':
264 pa_xfree(device);
265 device = pa_xstrdup(optarg);
266 break;
267
268 case 's':
269 pa_xfree(server);
270 server = pa_xstrdup(optarg);
271 break;
272
273 case 'n':
274 pa_xfree(client_name);
275 client_name = pa_xstrdup(optarg);
276 break;
277
278 case ARG_STREAM_NAME:
279 pa_xfree(stream_name);
280 stream_name = pa_xstrdup(optarg);
281 break;
282
283 case 'v':
284 verbose = 1;
285 break;
286
287 case ARG_VOLUME: {
288 int v = atoi(optarg);
289 volume = v < 0 ? 0 : v;
290 break;
291 }
292
293 case ARG_CHANNELMAP:
294 if (!pa_channel_map_parse(&channel_map, optarg)) {
295 fprintf(stderr, "Invalid channel map\n");
296 goto quit;
297 }
298
299 channel_map_set = 1;
300 break;
301
302 default:
303 goto quit;
304 }
305 }
306
307 filename = optind < argc ? argv[optind] : "STDIN";
308
309 memset(&sfinfo, 0, sizeof(sfinfo));
310
311 if (optind < argc)
312 sndfile = sf_open(filename, SFM_READ, &sfinfo);
313 else
314 sndfile = sf_open_fd(STDIN_FILENO, SFM_READ, &sfinfo, 0);
315
316 if (!sndfile) {
317 fprintf(stderr, "Failed to open file '%s'\n", filename);
318 goto quit;
319 }
320
321 sample_spec.rate = sfinfo.samplerate;
322 sample_spec.channels = sfinfo.channels;
323
324 readf_function = NULL;
325
326 switch (sfinfo.format & 0xFF) {
327 case SF_FORMAT_PCM_16:
328 case SF_FORMAT_PCM_U8:
329 case SF_FORMAT_PCM_S8:
330 sample_spec.format = PA_SAMPLE_S16NE;
331 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
332 break;
333
334 case SF_FORMAT_ULAW:
335 sample_spec.format = PA_SAMPLE_ULAW;
336 break;
337
338 case SF_FORMAT_ALAW:
339 sample_spec.format = PA_SAMPLE_ALAW;
340 break;
341
342 case SF_FORMAT_FLOAT:
343 case SF_FORMAT_DOUBLE:
344 default:
345 sample_spec.format = PA_SAMPLE_FLOAT32NE;
346 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
347 break;
348 }
349
350 assert(pa_sample_spec_valid(&sample_spec));
351
352 if (channel_map_set && channel_map.channels != sample_spec.channels) {
353 fprintf(stderr, "Channel map doesn't match file.\n");
354 goto quit;
355 }
356
357 if (!client_name) {
358 client_name = pa_locale_to_utf8(bn);
359 if (!client_name)
360 client_name = pa_utf8_filter(bn);
361 }
362
363 if (!stream_name) {
364 const char *n;
365
366 n = sf_get_string(sndfile, SF_STR_TITLE);
367
368 if (!n)
369 n = filename;
370
371 stream_name = pa_locale_to_utf8(n);
372 if (!stream_name)
373 stream_name = pa_utf8_filter(n);
374 }
375
376 if (verbose) {
377 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
378 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
379 fprintf(stderr, "Using sample spec '%s'\n", t);
380 }
381
382 /* Set up a new main loop */
383 if (!(m = pa_mainloop_new())) {
384 fprintf(stderr, "pa_mainloop_new() failed.\n");
385 goto quit;
386 }
387
388 mainloop_api = pa_mainloop_get_api(m);
389
390 r = pa_signal_init(mainloop_api);
391 assert(r == 0);
392 pa_signal_new(SIGINT, exit_signal_callback, NULL);
393 #ifdef SIGPIPE
394 signal(SIGPIPE, SIG_IGN);
395 #endif
396
397 /* Create a new connection context */
398 if (!(context = pa_context_new(mainloop_api, client_name))) {
399 fprintf(stderr, "pa_context_new() failed.\n");
400 goto quit;
401 }
402
403 pa_context_set_state_callback(context, context_state_callback, NULL);
404
405 /* Connect the context */
406 pa_context_connect(context, server, 0, NULL);
407
408 /* Run the main loop */
409 if (pa_mainloop_run(m, &ret) < 0) {
410 fprintf(stderr, "pa_mainloop_run() failed.\n");
411 goto quit;
412 }
413
414 quit:
415 if (stream)
416 pa_stream_unref(stream);
417
418 if (context)
419 pa_context_unref(context);
420
421 if (m) {
422 pa_signal_done();
423 pa_mainloop_free(m);
424 }
425
426 pa_xfree(server);
427 pa_xfree(device);
428 pa_xfree(client_name);
429 pa_xfree(stream_name);
430
431 if (sndfile)
432 sf_close(sndfile);
433
434 return ret;
435 }