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