]> code.delx.au - pulseaudio/blob - src/utils/paplay.c
allow the user to specify an alternative channel map in paplay too
[pulseaudio] / src / utils / paplay.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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
35 #include <sndfile.h>
36
37 #include <polyp/polypaudio.h>
38 #include <polyp/mainloop.h>
39 #include <polyp/mainloop-signal.h>
40
41 #if PA_API_VERSION != 8
42 #error Invalid Polypaudio 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);
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 size_t k;
102 sf_count_t f, n;
103 void *data;
104 assert(s && length);
105
106 if (!sndfile)
107 return;
108
109 k = pa_frame_size(&sample_spec);
110
111 data = malloc(length);
112
113 n = length/k;
114
115 f = readf_function(sndfile, data, n);
116
117 if (f > 0)
118 pa_stream_write(s, data, f*k, free, 0, PA_SEEK_RELATIVE);
119
120 if (f < n) {
121 sf_close(sndfile);
122 sndfile = NULL;
123 pa_operation_unref(pa_stream_drain(s, stream_drain_complete, NULL));
124 }
125 }
126
127 /* This routine is called whenever the stream state changes */
128 static void stream_state_callback(pa_stream *s, void *userdata) {
129 assert(s);
130
131 switch (pa_stream_get_state(s)) {
132 case PA_STREAM_CREATING:
133 case PA_STREAM_TERMINATED:
134 break;
135
136 case PA_STREAM_READY:
137 if (verbose)
138 fprintf(stderr, "Stream successfully created\n");
139 break;
140
141 case PA_STREAM_FAILED:
142 default:
143 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
144 quit(1);
145 }
146 }
147
148 /* This is called whenever the context status changes */
149 static void context_state_callback(pa_context *c, void *userdata) {
150 assert(c);
151
152 switch (pa_context_get_state(c)) {
153 case PA_CONTEXT_CONNECTING:
154 case PA_CONTEXT_AUTHORIZING:
155 case PA_CONTEXT_SETTING_NAME:
156 break;
157
158 case PA_CONTEXT_READY: {
159 pa_cvolume cv;
160
161 assert(c && !stream);
162
163 if (verbose)
164 fprintf(stderr, "Connection established.\n");
165
166 stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL);
167 assert(stream);
168
169 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
170 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
171 pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL);
172
173 break;
174 }
175
176 case PA_CONTEXT_TERMINATED:
177 quit(0);
178 break;
179
180 case PA_CONTEXT_FAILED:
181 default:
182 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
183 quit(1);
184 }
185 }
186
187 /* UNIX signal to quit recieved */
188 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
189 if (verbose)
190 fprintf(stderr, "Got SIGINT, exiting.\n");
191 quit(0);
192
193 }
194
195 static void help(const char *argv0) {
196
197 printf("%s [options] [FILE]\n\n"
198 " -h, --help Show this help\n"
199 " --version Show version\n\n"
200 " -v, --verbose Enable verbose operations\n\n"
201 " -s, --server=SERVER The name of the server to connect to\n"
202 " -d, --device=DEVICE The name of the sink/source to connect to\n"
203 " -n, --client-name=NAME How to call this client on the server\n"
204 " --stream-name=NAME How to call this stream on the server\n"
205 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
206 " --channel-map=CHANNELMAP Set the channel map to the use\n",
207 argv0);
208 }
209
210 enum {
211 ARG_VERSION = 256,
212 ARG_STREAM_NAME,
213 ARG_VOLUME,
214 ARG_CHANNELMAP
215 };
216
217 int main(int argc, char *argv[]) {
218 pa_mainloop* m = NULL;
219 int ret = 1, r, c;
220 char *bn, *server = NULL;
221 const char *filename;
222 SF_INFO sfinfo;
223
224 static const struct option long_options[] = {
225 {"device", 1, NULL, 'd'},
226 {"server", 1, NULL, 's'},
227 {"client-name", 1, NULL, 'n'},
228 {"stream-name", 1, NULL, ARG_STREAM_NAME},
229 {"version", 0, NULL, ARG_VERSION},
230 {"help", 0, NULL, 'h'},
231 {"verbose", 0, NULL, 'v'},
232 {"volume", 1, NULL, ARG_VOLUME},
233 {"channel-map", 1, NULL, ARG_CHANNELMAP},
234 {NULL, 0, NULL, 0}
235 };
236
237 if (!(bn = strrchr(argv[0], '/')))
238 bn = argv[0];
239 else
240 bn++;
241
242 while ((c = getopt_long(argc, argv, "d:s:n:hv", long_options, NULL)) != -1) {
243
244 switch (c) {
245 case 'h' :
246 help(bn);
247 ret = 0;
248 goto quit;
249
250 case ARG_VERSION:
251 printf("paplay "PACKAGE_VERSION"\nCompiled with libpolyp %s\nLinked with libpolyp %s\n", pa_get_headers_version(), pa_get_library_version());
252 ret = 0;
253 goto quit;
254
255 case 'd':
256 free(device);
257 device = strdup(optarg);
258 break;
259
260 case 's':
261 free(server);
262 server = strdup(optarg);
263 break;
264
265 case 'n':
266 free(client_name);
267 client_name = strdup(optarg);
268 break;
269
270 case ARG_STREAM_NAME:
271 free(stream_name);
272 stream_name = strdup(optarg);
273 break;
274
275 case 'v':
276 verbose = 1;
277 break;
278
279 case ARG_VOLUME: {
280 int v = atoi(optarg);
281 volume = v < 0 ? 0 : v;
282 break;
283 }
284
285 case ARG_CHANNELMAP:
286 if (!pa_channel_map_parse(&channel_map, optarg)) {
287 fprintf(stderr, "Invalid channel map\n");
288 goto quit;
289 }
290
291 channel_map_set = 1;
292 break;
293
294 default:
295 goto quit;
296 }
297 }
298
299
300 filename = optind < argc ? argv[optind] : "STDIN";
301
302
303 if (!client_name)
304 client_name = strdup(bn);
305
306 if (!stream_name)
307 stream_name = strdup(filename);
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 switch (sfinfo.format & 0xFF) {
325 case SF_FORMAT_PCM_16:
326 case SF_FORMAT_PCM_U8:
327 case SF_FORMAT_ULAW:
328 case SF_FORMAT_ALAW:
329 sample_spec.format = PA_SAMPLE_S16NE;
330 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
331 break;
332 case SF_FORMAT_FLOAT:
333 default:
334 sample_spec.format = PA_SAMPLE_FLOAT32NE;
335 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
336 break;
337 }
338
339 assert(pa_sample_spec_valid(&sample_spec));
340
341 if (channel_map_set && channel_map.channels != sample_spec.channels) {
342 fprintf(stderr, "Channel map doesn't match file.\n");
343 goto quit;
344 }
345
346 if (verbose) {
347 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
348 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
349 fprintf(stderr, "Using sample spec '%s'\n", t);
350 }
351
352 /* Set up a new main loop */
353 if (!(m = pa_mainloop_new())) {
354 fprintf(stderr, "pa_mainloop_new() failed.\n");
355 goto quit;
356 }
357
358 mainloop_api = pa_mainloop_get_api(m);
359
360 r = pa_signal_init(mainloop_api);
361 assert(r == 0);
362 pa_signal_new(SIGINT, exit_signal_callback, NULL);
363 #ifdef SIGPIPE
364 signal(SIGPIPE, SIG_IGN);
365 #endif
366
367 /* Create a new connection context */
368 if (!(context = pa_context_new(mainloop_api, client_name))) {
369 fprintf(stderr, "pa_context_new() failed.\n");
370 goto quit;
371 }
372
373 pa_context_set_state_callback(context, context_state_callback, NULL);
374
375 /* Connect the context */
376 pa_context_connect(context, server, 0, NULL);
377
378 /* Run the main loop */
379 if (pa_mainloop_run(m, &ret) < 0) {
380 fprintf(stderr, "pa_mainloop_run() failed.\n");
381 goto quit;
382 }
383
384 quit:
385 if (stream)
386 pa_stream_unref(stream);
387
388 if (context)
389 pa_context_unref(context);
390
391 if (m) {
392 pa_signal_done();
393 pa_mainloop_free(m);
394 }
395
396 free(server);
397 free(device);
398 free(client_name);
399 free(stream_name);
400
401 if (sndfile)
402 sf_close(sndfile);
403
404 return ret;
405 }