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