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