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