]> code.delx.au - pulseaudio/blob - polyp/pacat.c
new configuration subsystem
[pulseaudio] / polyp / pacat.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 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 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
34 #include <polyp/polyplib.h>
35 #include <polyp/polyplib-error.h>
36 #include <polyp/mainloop.h>
37 #include <polyp/mainloop-signal.h>
38
39 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
40
41 static struct pa_context *context = NULL;
42 static struct pa_stream *stream = NULL;
43 static struct pa_mainloop_api *mainloop_api = NULL;
44
45 static void *buffer = NULL;
46 static size_t buffer_length = 0, buffer_index = 0;
47
48 static struct pa_io_event* stdio_event = NULL;
49
50 /* A shortcut for terminating the application */
51 static void quit(int ret) {
52 assert(mainloop_api);
53 mainloop_api->quit(mainloop_api, ret);
54 }
55
56 /* Write some data to the stream */
57 static void do_stream_write(size_t length) {
58 size_t l;
59 assert(length);
60
61 if (!buffer || !buffer_length)
62 return;
63
64 l = length;
65 if (l > buffer_length)
66 l = buffer_length;
67
68 pa_stream_write(stream, (uint8_t*) buffer + buffer_index, l, NULL, 0);
69 buffer_length -= l;
70 buffer_index += l;
71
72 if (!buffer_length) {
73 free(buffer);
74 buffer = NULL;
75 buffer_index = buffer_length = 0;
76 }
77 }
78
79 /* This is called whenever new data may be written to the stream */
80 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
81 assert(s && length);
82
83 if (stdio_event)
84 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_INPUT);
85
86 if (!buffer)
87 return;
88
89 do_stream_write(length);
90 }
91
92 /* This is called whenever new data may is available */
93 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
94 assert(s && data && length);
95
96 if (stdio_event)
97 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);
98
99 if (buffer) {
100 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
101 return;
102 }
103
104 buffer = malloc(buffer_length = length);
105 assert(buffer);
106 memcpy(buffer, data, length);
107 buffer_index = 0;
108 }
109
110 /* This routine is called whenever the stream state changes */
111 static void stream_state_callback(struct pa_stream *s, void *userdata) {
112 assert(s);
113
114 switch (pa_stream_get_state(s)) {
115 case PA_STREAM_CREATING:
116 case PA_STREAM_TERMINATED:
117 break;
118
119 case PA_STREAM_READY:
120 fprintf(stderr, "Stream successfully created\n");
121 break;
122
123 case PA_STREAM_FAILED:
124 default:
125 fprintf(stderr, "Stream errror: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
126 quit(1);
127 }
128 }
129
130 /* This is called whenever the context status changes */
131 static void context_state_callback(struct pa_context *c, void *userdata) {
132 static const struct pa_sample_spec ss = {
133 .format = PA_SAMPLE_S16LE,
134 .rate = 44100,
135 .channels = 2
136 };
137
138 assert(c);
139
140 switch (pa_context_get_state(c)) {
141 case PA_CONTEXT_CONNECTING:
142 case PA_CONTEXT_AUTHORIZING:
143 case PA_CONTEXT_SETTING_NAME:
144 break;
145
146 case PA_CONTEXT_READY:
147
148 assert(c && !stream);
149 fprintf(stderr, "Connection established.\n");
150
151 stream = pa_stream_new(c, "pacat", &ss);
152 assert(stream);
153
154 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
155 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
156 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
157
158 if (mode == PLAYBACK)
159 pa_stream_connect_playback(stream, NULL, NULL);
160 else
161 pa_stream_connect_record(stream, NULL, NULL);
162
163 break;
164
165 case PA_CONTEXT_TERMINATED:
166 quit(0);
167 break;
168
169 case PA_CONTEXT_FAILED:
170 default:
171 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
172 quit(1);
173 }
174 }
175
176 /* Connection draining complete */
177 static void context_drain_complete(struct pa_context*c, void *userdata) {
178 pa_context_disconnect(c);
179 }
180
181 /* Stream draining complete */
182 static void stream_drain_complete(struct pa_stream*s, int success, void *userdata) {
183 struct pa_operation *o;
184
185 if (!success) {
186 fprintf(stderr, "Failed to drain stream: %s\n", pa_strerror(pa_context_errno(context)));
187 quit(1);
188 }
189
190 fprintf(stderr, "Playback stream drained.\n");
191
192 pa_stream_disconnect(stream);
193 pa_stream_unref(stream);
194 stream = NULL;
195
196 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
197 pa_context_disconnect(context);
198 else {
199 pa_operation_unref(o);
200 fprintf(stderr, "Draining connection to server.\n");
201 }
202 }
203
204 /* New data on STDIN **/
205 static void stdin_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
206 size_t l, w = 0;
207 ssize_t r;
208 assert(a == mainloop_api && e && stdio_event == e);
209
210 if (buffer) {
211 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
212 return;
213 }
214
215 if (!stream || pa_stream_get_state(stream) != PA_STREAM_READY || !(l = w = pa_stream_writable_size(stream)))
216 l = 4096;
217
218 buffer = malloc(l);
219 assert(buffer);
220 if ((r = read(fd, buffer, l)) <= 0) {
221 if (r == 0) {
222 fprintf(stderr, "Got EOF.\n");
223 pa_operation_unref(pa_stream_drain(stream, stream_drain_complete, NULL));
224 } else {
225 fprintf(stderr, "read() failed: %s\n", strerror(errno));
226 quit(1);
227 }
228
229 mainloop_api->io_free(stdio_event);
230 stdio_event = NULL;
231 return;
232 }
233
234 buffer_length = r;
235 buffer_index = 0;
236
237 if (w)
238 do_stream_write(w);
239 }
240
241 /* Some data may be written to STDOUT */
242 static void stdout_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
243 ssize_t r;
244 assert(a == mainloop_api && e && stdio_event == e);
245
246 if (!buffer) {
247 mainloop_api->io_enable(stdio_event, PA_IO_EVENT_NULL);
248 return;
249 }
250
251 assert(buffer_length);
252
253 if ((r = write(fd, (uint8_t*) buffer+buffer_index, buffer_length)) <= 0) {
254 fprintf(stderr, "write() failed: %s\n", strerror(errno));
255 quit(1);
256
257 mainloop_api->io_free(stdio_event);
258 stdio_event = NULL;
259 return;
260 }
261
262 buffer_length -= r;
263 buffer_index += r;
264
265 if (!buffer_length) {
266 free(buffer);
267 buffer = NULL;
268 buffer_length = buffer_index = 0;
269 }
270 }
271
272 /* UNIX signal to quit recieved */
273 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
274 fprintf(stderr, "Got SIGINT, exiting.\n");
275 quit(0);
276
277 }
278
279 /* Show the current playback latency */
280 static void stream_get_latency_callback(struct pa_stream *s, const struct pa_latency_info *i, void *userdata) {
281 assert(s);
282
283 if (!i) {
284 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
285 quit(1);
286 return;
287 }
288
289 fprintf(stderr, "Latency: buffer: %0.0f usec; sink: %0.0f usec; transport: %0.0f usec; total: %0.0f usec; synchronized clocks: %s.\n",
290 (float) i->buffer_usec, (float) i->sink_usec, (float) i->transport_usec,
291 (float) (i->buffer_usec+i->sink_usec+i->transport_usec),
292 i->synchronized_clocks ? "yes" : "no");
293 }
294
295 /* Someone requested that the latency is shown */
296 static void sigusr1_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
297 if (mode != PLAYBACK)
298 return;
299
300 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
301 pa_operation_unref(pa_stream_get_latency(stream, stream_get_latency_callback, NULL));
302 }
303
304 int main(int argc, char *argv[]) {
305 struct pa_mainloop* m = NULL;
306 int ret = 1, r;
307 char *bn;
308
309 if (!(bn = strrchr(argv[0], '/')))
310 bn = argv[0];
311 else
312 bn++;
313
314 if (strstr(bn, "rec") || strstr(bn, "mon"))
315 mode = RECORD;
316 else if (strstr(bn, "cat") || strstr(bn, "play"))
317 mode = PLAYBACK;
318
319 if (argc >= 2) {
320 if (!strcmp(argv[1], "-r"))
321 mode = RECORD;
322 else if (!strcmp(argv[1], "-p"))
323 mode = PLAYBACK;
324 else {
325 fprintf(stderr, "Invalid argument\n");
326 goto quit;
327 }
328 }
329
330 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
331
332 /* Set up a new main loop */
333 if (!(m = pa_mainloop_new())) {
334 fprintf(stderr, "pa_mainloop_new() failed.\n");
335 goto quit;
336 }
337
338 mainloop_api = pa_mainloop_get_api(m);
339
340 r = pa_signal_init(mainloop_api);
341 assert(r == 0);
342 pa_signal_new(SIGINT, exit_signal_callback, NULL);
343 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL);
344 signal(SIGPIPE, SIG_IGN);
345
346 if (!(stdio_event = mainloop_api->io_new(mainloop_api,
347 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
348 mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT,
349 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
350 fprintf(stderr, "source_io() failed.\n");
351 goto quit;
352 }
353
354 /* Create a new connection context */
355 if (!(context = pa_context_new(mainloop_api, bn))) {
356 fprintf(stderr, "pa_context_new() failed.\n");
357 goto quit;
358 }
359
360 pa_context_set_state_callback(context, context_state_callback, NULL);
361
362 /* Connect the context */
363 pa_context_connect_spawn(context, NULL, NULL, NULL);
364
365 /* Run the main loop */
366 if (pa_mainloop_run(m, &ret) < 0) {
367 fprintf(stderr, "pa_mainloop_run() failed.\n");
368 goto quit;
369 }
370
371 quit:
372 if (stream)
373 pa_stream_unref(stream);
374
375 if (context)
376 pa_context_unref(context);
377
378 if (stdio_event) {
379 assert(mainloop_api);
380 mainloop_api->io_free(stdio_event);
381 }
382
383 if (m) {
384 pa_signal_done();
385 pa_mainloop_free(m);
386 }
387
388 if (buffer)
389 free(buffer);
390
391 return ret;
392 }