]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-simple.c
rework memory block management to be thread-safe and mostly lock-free.
[pulseaudio] / src / pulsecore / protocol-simple.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/xmalloc.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/source-output.h>
37 #include <pulsecore/client.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-error.h>
42
43 #include "protocol-simple.h"
44
45 /* Don't allow more than this many concurrent connections */
46 #define MAX_CONNECTIONS 10
47
48 struct connection {
49 pa_protocol_simple *protocol;
50 pa_iochannel *io;
51 pa_sink_input *sink_input;
52 pa_source_output *source_output;
53 pa_client *client;
54 pa_memblockq *input_memblockq, *output_memblockq;
55 pa_defer_event *defer_event;
56
57 int dead;
58
59 struct {
60 pa_memblock *current_memblock;
61 size_t memblock_index, fragment_size;
62 } playback;
63 };
64
65 struct pa_protocol_simple {
66 pa_module *module;
67 pa_core *core;
68 pa_socket_server*server;
69 pa_idxset *connections;
70 enum {
71 RECORD = 1,
72 PLAYBACK = 2,
73 DUPLEX = 3
74 } mode;
75 pa_sample_spec sample_spec;
76 char *source_name, *sink_name;
77 };
78
79 #define PLAYBACK_BUFFER_SECONDS (.5)
80 #define PLAYBACK_BUFFER_FRAGMENTS (10)
81 #define RECORD_BUFFER_SECONDS (5)
82 #define RECORD_BUFFER_FRAGMENTS (100)
83
84 static void connection_free(struct connection *c) {
85 assert(c);
86
87 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
88
89 if (c->playback.current_memblock)
90 pa_memblock_unref(c->playback.current_memblock);
91 if (c->sink_input) {
92 pa_sink_input_disconnect(c->sink_input);
93 pa_sink_input_unref(c->sink_input);
94 }
95 if (c->source_output) {
96 pa_source_output_disconnect(c->source_output);
97 pa_source_output_unref(c->source_output);
98 }
99 if (c->client)
100 pa_client_free(c->client);
101 if (c->io)
102 pa_iochannel_free(c->io);
103 if (c->input_memblockq)
104 pa_memblockq_free(c->input_memblockq);
105 if (c->output_memblockq)
106 pa_memblockq_free(c->output_memblockq);
107 if (c->defer_event)
108 c->protocol->core->mainloop->defer_free(c->defer_event);
109 pa_xfree(c);
110 }
111
112 static int do_read(struct connection *c) {
113 pa_memchunk chunk;
114 ssize_t r;
115 size_t l;
116 void *p;
117
118 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
119 return 0;
120
121 if (l > c->playback.fragment_size)
122 l = c->playback.fragment_size;
123
124 if (c->playback.current_memblock)
125 if (pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index < l) {
126 pa_memblock_unref(c->playback.current_memblock);
127 c->playback.current_memblock = NULL;
128 c->playback.memblock_index = 0;
129 }
130
131 if (!c->playback.current_memblock) {
132 c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, c->playback.fragment_size*2);
133 assert(c->playback.current_memblock);
134 assert(pa_memblock_get_length(c->playback.current_memblock) >= l);
135 c->playback.memblock_index = 0;
136 }
137
138 p = pa_memblock_acquire(c->playback.current_memblock);
139
140 if ((r = pa_iochannel_read(c->io, (uint8_t*) p + c->playback.memblock_index, l)) <= 0) {
141 pa_memblock_release(c->playback.current_memblock);
142 pa_log_debug("read(): %s", r == 0 ? "EOF" : pa_cstrerror(errno));
143 return -1;
144 }
145
146 pa_memblock_release(c->playback.current_memblock);
147 chunk.memblock = c->playback.current_memblock;
148 chunk.index = c->playback.memblock_index;
149 chunk.length = r;
150 assert(chunk.memblock);
151
152 c->playback.memblock_index += r;
153
154 assert(c->input_memblockq);
155 pa_memblockq_push_align(c->input_memblockq, &chunk);
156 assert(c->sink_input);
157 pa_sink_notify(c->sink_input->sink);
158
159 return 0;
160 }
161
162 static int do_write(struct connection *c) {
163 pa_memchunk chunk;
164 ssize_t r;
165 void *p;
166
167 if (!c->source_output)
168 return 0;
169
170 assert(c->output_memblockq);
171 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
172 return 0;
173
174 assert(chunk.memblock && chunk.length);
175
176 p = pa_memblock_acquire(chunk.memblock);
177
178 if ((r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length)) < 0) {
179 pa_memblock_release(chunk.memblock);
180 pa_memblock_unref(chunk.memblock);
181 pa_log("write(): %s", pa_cstrerror(errno));
182 return -1;
183 }
184
185 pa_memblock_release(chunk.memblock);
186
187 pa_memblockq_drop(c->output_memblockq, &chunk, r);
188 pa_memblock_unref(chunk.memblock);
189
190 pa_source_notify(c->source_output->source);
191
192 return 0;
193 }
194
195 static void do_work(struct connection *c) {
196 assert(c);
197
198 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
199 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
200
201 if (c->dead)
202 return;
203
204 if (pa_iochannel_is_readable(c->io)) {
205 if (do_read(c) < 0)
206 goto fail;
207 } else if (pa_iochannel_is_hungup(c->io))
208 goto fail;
209
210 if (pa_iochannel_is_writable(c->io)) {
211 if (do_write(c) < 0)
212 goto fail;
213 }
214
215 return;
216
217 fail:
218
219 if (c->sink_input) {
220 c->dead = 1;
221
222 pa_iochannel_free(c->io);
223 c->io = NULL;
224
225 pa_memblockq_prebuf_disable(c->input_memblockq);
226 pa_sink_notify(c->sink_input->sink);
227 } else
228 connection_free(c);
229 }
230
231 /*** sink_input callbacks ***/
232
233 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
234 struct connection*c;
235 assert(i && i->userdata && chunk);
236 c = i->userdata;
237
238 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
239
240 if (c->dead)
241 connection_free(c);
242
243 return -1;
244 }
245
246 return 0;
247 }
248
249 static void sink_input_drop_cb(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
250 struct connection*c = i->userdata;
251 assert(i && c && length);
252
253 pa_memblockq_drop(c->input_memblockq, chunk, length);
254
255 /* do something */
256 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
257 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
258 }
259
260 static void sink_input_kill_cb(pa_sink_input *i) {
261 assert(i && i->userdata);
262 connection_free((struct connection *) i->userdata);
263 }
264
265
266 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
267 struct connection*c = i->userdata;
268 assert(i && c);
269 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
270 }
271
272 /*** source_output callbacks ***/
273
274 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
275 struct connection *c = o->userdata;
276 assert(o && c && chunk);
277
278 pa_memblockq_push(c->output_memblockq, chunk);
279
280 /* do something */
281 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
282 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
283 }
284
285 static void source_output_kill_cb(pa_source_output *o) {
286 assert(o && o->userdata);
287 connection_free((struct connection *) o->userdata);
288 }
289
290 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
291 struct connection*c = o->userdata;
292 assert(o && c);
293 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
294 }
295
296 /*** client callbacks ***/
297
298 static void client_kill_cb(pa_client *c) {
299 assert(c && c->userdata);
300 connection_free((struct connection *) c->userdata);
301 }
302
303 /*** pa_iochannel callbacks ***/
304
305 static void io_callback(pa_iochannel*io, void *userdata) {
306 struct connection *c = userdata;
307 assert(io && c && c->io == io);
308
309 do_work(c);
310 }
311
312 /*** fixed callback ***/
313
314 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
315 struct connection *c = userdata;
316 assert(a && c && c->defer_event == e);
317
318 do_work(c);
319 }
320
321 /*** socket_server callbacks ***/
322
323 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
324 pa_protocol_simple *p = userdata;
325 struct connection *c = NULL;
326 char cname[256];
327 assert(s && io && p);
328
329 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
330 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
331 pa_iochannel_free(io);
332 return;
333 }
334
335 c = pa_xmalloc(sizeof(struct connection));
336 c->io = io;
337 c->sink_input = NULL;
338 c->source_output = NULL;
339 c->defer_event = NULL;
340 c->input_memblockq = c->output_memblockq = NULL;
341 c->protocol = p;
342 c->playback.current_memblock = NULL;
343 c->playback.memblock_index = 0;
344 c->playback.fragment_size = 0;
345 c->dead = 0;
346
347 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
348 c->client = pa_client_new(p->core, __FILE__, cname);
349 assert(c->client);
350 c->client->owner = p->module;
351 c->client->kill = client_kill_cb;
352 c->client->userdata = c;
353
354 if (p->mode & PLAYBACK) {
355 pa_sink_input_new_data data;
356 size_t l;
357
358 pa_sink_input_new_data_init(&data);
359 data.driver = __FILE__;
360 data.name = c->client->name;
361 pa_sink_input_new_data_set_sample_spec(&data, &p->sample_spec);
362 data.module = p->module;
363 data.client = c->client;
364
365 if (!(c->sink_input = pa_sink_input_new(p->core, &data, 0))) {
366 pa_log("Failed to create sink input.");
367 goto fail;
368 }
369
370 c->sink_input->peek = sink_input_peek_cb;
371 c->sink_input->drop = sink_input_drop_cb;
372 c->sink_input->kill = sink_input_kill_cb;
373 c->sink_input->get_latency = sink_input_get_latency_cb;
374 c->sink_input->userdata = c;
375
376 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
377 c->input_memblockq = pa_memblockq_new(
378 0,
379 l,
380 0,
381 pa_frame_size(&p->sample_spec),
382 (size_t) -1,
383 l/PLAYBACK_BUFFER_FRAGMENTS,
384 NULL);
385 assert(c->input_memblockq);
386 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
387 c->playback.fragment_size = l/10;
388
389 pa_sink_notify(c->sink_input->sink);
390 }
391
392 if (p->mode & RECORD) {
393 pa_source_output_new_data data;
394 size_t l;
395
396 pa_source_output_new_data_init(&data);
397 data.driver = __FILE__;
398 data.name = c->client->name;
399 pa_source_output_new_data_set_sample_spec(&data, &p->sample_spec);
400 data.module = p->module;
401 data.client = c->client;
402
403 if (!(c->source_output = pa_source_output_new(p->core, &data, 0))) {
404 pa_log("Failed to create source output.");
405 goto fail;
406 }
407 c->source_output->push = source_output_push_cb;
408 c->source_output->kill = source_output_kill_cb;
409 c->source_output->get_latency = source_output_get_latency_cb;
410 c->source_output->userdata = c;
411
412 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
413 c->output_memblockq = pa_memblockq_new(
414 0,
415 l,
416 0,
417 pa_frame_size(&p->sample_spec),
418 1,
419 0,
420 NULL);
421 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
422 pa_source_notify(c->source_output->source);
423 }
424
425 pa_iochannel_set_callback(c->io, io_callback, c);
426 pa_idxset_put(p->connections, c, NULL);
427
428 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
429 assert(c->defer_event);
430 p->core->mainloop->defer_enable(c->defer_event, 0);
431
432 return;
433
434 fail:
435 if (c)
436 connection_free(c);
437 }
438
439 pa_protocol_simple* pa_protocol_simple_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
440 pa_protocol_simple* p = NULL;
441 int enable;
442 assert(core && server && ma);
443
444 p = pa_xmalloc0(sizeof(pa_protocol_simple));
445 p->module = m;
446 p->core = core;
447 p->server = server;
448 p->connections = pa_idxset_new(NULL, NULL);
449
450 p->sample_spec = core->default_sample_spec;
451 if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
452 pa_log("Failed to parse sample type specification.");
453 goto fail;
454 }
455
456 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
457 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
458
459 enable = 0;
460 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
461 pa_log("record= expects a numeric argument.");
462 goto fail;
463 }
464 p->mode = enable ? RECORD : 0;
465
466 enable = 1;
467 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
468 pa_log("playback= expects a numeric argument.");
469 goto fail;
470 }
471 p->mode |= enable ? PLAYBACK : 0;
472
473 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
474 pa_log("neither playback nor recording enabled for protocol.");
475 goto fail;
476 }
477
478 pa_socket_server_set_callback(p->server, on_connection, p);
479
480 return p;
481
482 fail:
483 if (p)
484 pa_protocol_simple_free(p);
485 return NULL;
486 }
487
488
489 void pa_protocol_simple_free(pa_protocol_simple *p) {
490 struct connection *c;
491 assert(p);
492
493 if (p->connections) {
494 while((c = pa_idxset_first(p->connections, NULL)))
495 connection_free(c);
496
497 pa_idxset_free(p->connections, NULL, NULL);
498 }
499
500 if (p->server)
501 pa_socket_server_unref(p->server);
502 pa_xfree(p);
503 }
504