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