]> code.delx.au - pulseaudio/blob - polyp/protocol-simple.c
add support for automatic termination of the daemon after the last client quit
[pulseaudio] / polyp / protocol-simple.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 <assert.h>
27 #include <stdlib.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32
33 #include "sink-input.h"
34 #include "source-output.h"
35 #include "protocol-simple.h"
36 #include "client.h"
37 #include "sample-util.h"
38 #include "namereg.h"
39 #include "xmalloc.h"
40
41 struct connection {
42 struct pa_protocol_simple *protocol;
43 struct pa_iochannel *io;
44 struct pa_sink_input *sink_input;
45 struct pa_source_output *source_output;
46 struct pa_client *client;
47 struct pa_memblockq *input_memblockq, *output_memblockq;
48 struct pa_defer_event *defer_event;
49
50 struct {
51 struct pa_memblock *current_memblock;
52 size_t memblock_index, fragment_size;
53 } playback;
54 };
55
56 struct pa_protocol_simple {
57 struct pa_module *module;
58 struct pa_core *core;
59 struct pa_socket_server*server;
60 struct pa_idxset *connections;
61 enum {
62 RECORD = 1,
63 PLAYBACK = 2,
64 DUPLEX = 3
65 } mode;
66 struct pa_sample_spec sample_spec;
67 char *source_name, *sink_name;
68 };
69
70 #define PLAYBACK_BUFFER_SECONDS (.5)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
73 #define RECORD_BUFFER_FRAGMENTS (100)
74
75 static void connection_free(struct connection *c) {
76 assert(c);
77
78 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
79
80 if (c->playback.current_memblock)
81 pa_memblock_unref(c->playback.current_memblock);
82 if (c->sink_input)
83 pa_sink_input_free(c->sink_input);
84 if (c->source_output)
85 pa_source_output_free(c->source_output);
86 if (c->client)
87 pa_client_free(c->client);
88 if (c->io)
89 pa_iochannel_free(c->io);
90 if (c->input_memblockq)
91 pa_memblockq_free(c->input_memblockq);
92 if (c->output_memblockq)
93 pa_memblockq_free(c->output_memblockq);
94 if (c->defer_event)
95 c->protocol->core->mainloop->defer_free(c->defer_event);
96 pa_xfree(c);
97 }
98
99 static int do_read(struct connection *c) {
100 struct pa_memchunk chunk;
101 ssize_t r;
102 size_t l;
103
104 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
105 return 0;
106
107 if (l > c->playback.fragment_size)
108 l = c->playback.fragment_size;
109
110 if (c->playback.current_memblock)
111 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
112 pa_memblock_unref(c->playback.current_memblock);
113 c->playback.current_memblock = NULL;
114 c->playback.memblock_index = 0;
115 }
116
117 if (!c->playback.current_memblock) {
118 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2, c->protocol->core->memblock_stat);
119 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
120 c->playback.memblock_index = 0;
121 }
122
123 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
124 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
125 return -1;
126 }
127
128 chunk.memblock = c->playback.current_memblock;
129 chunk.index = c->playback.memblock_index;
130 chunk.length = r;
131 assert(chunk.memblock);
132
133 c->playback.memblock_index += r;
134
135 assert(c->input_memblockq);
136 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
137 assert(c->sink_input);
138 pa_sink_notify(c->sink_input->sink);
139
140 return 0;
141 }
142
143 static int do_write(struct connection *c) {
144 struct pa_memchunk chunk;
145 ssize_t r;
146
147 if (!c->source_output)
148 return 0;
149
150 assert(c->output_memblockq);
151 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
152 return 0;
153
154 assert(chunk.memblock && chunk.length);
155
156 if ((r = pa_iochannel_write(c->io, (uint8_t*) chunk.memblock->data+chunk.index, chunk.length)) < 0) {
157 pa_memblock_unref(chunk.memblock);
158 fprintf(stderr, "write(): %s\n", strerror(errno));
159 return -1;
160 }
161
162 pa_memblockq_drop(c->output_memblockq, &chunk, r);
163 pa_memblock_unref(chunk.memblock);
164
165 return 0;
166 }
167
168
169 static void do_work(struct connection *c) {
170 assert(c);
171
172 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
173 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
174
175 if (pa_iochannel_is_hungup(c->io))
176 goto fail;
177
178 if (pa_iochannel_is_writable(c->io))
179 if (do_write(c) < 0)
180 goto fail;
181
182 if (pa_iochannel_is_readable(c->io))
183 if (do_read(c) < 0)
184 goto fail;
185
186 return;
187
188 fail:
189 connection_free(c);
190 }
191
192 /*** sink_input callbacks ***/
193
194 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
195 struct connection*c;
196 assert(i && i->userdata && chunk);
197 c = i->userdata;
198
199 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
200 return -1;
201
202 return 0;
203 }
204
205 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
206 struct connection*c = i->userdata;
207 assert(i && c && length);
208
209 pa_memblockq_drop(c->input_memblockq, chunk, length);
210
211 /* do something */
212 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
213 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
214 }
215
216 static void sink_input_kill_cb(struct pa_sink_input *i) {
217 assert(i && i->userdata);
218 connection_free((struct connection *) i->userdata);
219 }
220
221
222 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
223 struct connection*c = i->userdata;
224 assert(i && c);
225 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
226 }
227
228 /*** source_output callbacks ***/
229
230 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
231 struct connection *c = o->userdata;
232 assert(o && c && chunk);
233
234 pa_memblockq_push(c->output_memblockq, chunk, 0);
235
236 /* do something */
237 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
238 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
239 }
240
241 static void source_output_kill_cb(struct pa_source_output *o) {
242 assert(o && o->userdata);
243 connection_free((struct connection *) o->userdata);
244 }
245
246 /*** client callbacks ***/
247
248 static void client_kill_cb(struct pa_client *c) {
249 assert(c && c->userdata);
250 connection_free((struct connection *) c->userdata);
251 }
252
253 /*** pa_iochannel callbacks ***/
254
255 static void io_callback(struct pa_iochannel*io, void *userdata) {
256 struct connection *c = userdata;
257 assert(io && c && c->io == io);
258
259 do_work(c);
260 }
261
262 /*** fixed callback ***/
263
264 static void defer_callback(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata) {
265 struct connection *c = userdata;
266 assert(a && c && c->defer_event == e);
267
268 do_work(c);
269 }
270
271 /*** socket_server callbacks ***/
272
273 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
274 struct pa_protocol_simple *p = userdata;
275 struct connection *c = NULL;
276 char cname[256];
277 assert(s && io && p);
278
279 c = pa_xmalloc(sizeof(struct connection));
280 c->io = io;
281 c->sink_input = NULL;
282 c->source_output = NULL;
283 c->defer_event = NULL;
284 c->input_memblockq = c->output_memblockq = NULL;
285 c->protocol = p;
286 c->playback.current_memblock = NULL;
287 c->playback.memblock_index = 0;
288 c->playback.fragment_size = 0;
289
290 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
291 c->client = pa_client_new(p->core, "SIMPLE", cname);
292 assert(c->client);
293 c->client->owner = p->module;
294 c->client->kill = client_kill_cb;
295 c->client->userdata = c;
296
297 if (p->mode & PLAYBACK) {
298 struct pa_sink *sink;
299 size_t l;
300
301 if (!(sink = pa_namereg_get(p->core, p->sink_name, PA_NAMEREG_SINK, 1))) {
302 fprintf(stderr, "Failed to get sink.\n");
303 goto fail;
304 }
305
306 if (!(c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec))) {
307 fprintf(stderr, "Failed to create sink input.\n");
308 goto fail;
309 }
310
311 c->sink_input->owner = p->module;
312 c->sink_input->client = c->client;
313
314 c->sink_input->peek = sink_input_peek_cb;
315 c->sink_input->drop = sink_input_drop_cb;
316 c->sink_input->kill = sink_input_kill_cb;
317 c->sink_input->get_latency = sink_input_get_latency_cb;
318 c->sink_input->userdata = c;
319
320 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
321 c->input_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&p->sample_spec), l/2, l/PLAYBACK_BUFFER_FRAGMENTS, p->core->memblock_stat);
322 assert(c->input_memblockq);
323 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
324 c->playback.fragment_size = l/10;
325 }
326
327 if (p->mode & RECORD) {
328 struct pa_source *source;
329 size_t l;
330
331 if (!(source = pa_namereg_get(p->core, p->source_name, PA_NAMEREG_SOURCE, 1))) {
332 fprintf(stderr, "Failed to get source.\n");
333 goto fail;
334 }
335
336 c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
337 if (!c->source_output) {
338 fprintf(stderr, "Failed to create source output.\n");
339 goto fail;
340 }
341 c->source_output->owner = p->module;
342 c->source_output->client = c->client;
343
344 c->source_output->push = source_output_push_cb;
345 c->source_output->kill = source_output_kill_cb;
346 c->source_output->userdata = c;
347
348 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
349 c->output_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&p->sample_spec), 0, 0, p->core->memblock_stat);
350 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
351 }
352
353 pa_iochannel_set_callback(c->io, io_callback, c);
354 pa_idxset_put(p->connections, c, NULL);
355
356 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
357 assert(c->defer_event);
358 p->core->mainloop->defer_enable(c->defer_event, 0);
359
360 return;
361
362 fail:
363 if (c)
364 connection_free(c);
365 }
366
367 struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
368 struct pa_protocol_simple* p = NULL;
369 int enable;
370 assert(core && server && ma);
371
372 p = pa_xmalloc0(sizeof(struct pa_protocol_simple));
373 p->module = m;
374 p->core = core;
375 p->server = server;
376 p->connections = pa_idxset_new(NULL, NULL);
377
378 p->sample_spec = core->default_sample_spec;
379 if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
380 fprintf(stderr, "Failed to parse sample type specification.\n");
381 goto fail;
382 }
383
384 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
385 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
386
387 enable = 0;
388 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
389 fprintf(stderr, __FILE__": record= expects a numeric argument.\n");
390 goto fail;
391 }
392 p->mode = enable ? RECORD : 0;
393
394 enable = 1;
395 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
396 fprintf(stderr, __FILE__": playback= expects a numeric argument.\n");
397 goto fail;
398 }
399 p->mode |= enable ? PLAYBACK : 0;
400
401 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
402 fprintf(stderr, __FILE__": neither playback nor recording enabled for protocol.\n");
403 goto fail;
404 }
405
406 pa_socket_server_set_callback(p->server, on_connection, p);
407
408 return p;
409
410 fail:
411 if (p)
412 pa_protocol_simple_free(p);
413 return NULL;
414 }
415
416
417 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
418 struct connection *c;
419 assert(p);
420
421 if (p->connections) {
422 while((c = pa_idxset_first(p->connections, NULL)))
423 connection_free(c);
424
425 pa_idxset_free(p->connections, NULL, NULL);
426 }
427
428 if (p->server)
429 pa_socket_server_unref(p->server);
430 pa_xfree(p);
431 }
432