Merge dead branch 'glitch-free'
[platform/upstream/pulseaudio.git] / src / pulsecore / protocol-simple.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
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 <stdlib.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.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 #include <pulsecore/atomic.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/core-util.h>
45
46 #include "protocol-simple.h"
47
48 /* Don't allow more than this many concurrent connections */
49 #define MAX_CONNECTIONS 10
50
51 typedef struct connection {
52     pa_msgobject parent;
53     pa_protocol_simple *protocol;
54     pa_iochannel *io;
55     pa_sink_input *sink_input;
56     pa_source_output *source_output;
57     pa_client *client;
58     pa_memblockq *input_memblockq, *output_memblockq;
59
60     pa_bool_t dead;
61
62     struct {
63         pa_memblock *current_memblock;
64         size_t memblock_index;
65         pa_atomic_t missing;
66         pa_bool_t underrun;
67     } playback;
68 } connection;
69
70 PA_DECLARE_CLASS(connection);
71 #define CONNECTION(o) (connection_cast(o))
72 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
73
74 struct pa_protocol_simple {
75     pa_module *module;
76     pa_core *core;
77     pa_socket_server*server;
78     pa_idxset *connections;
79
80     enum {
81         RECORD = 1,
82         PLAYBACK = 2,
83         DUPLEX = 3
84     } mode;
85
86     pa_sample_spec sample_spec;
87     char *source_name, *sink_name;
88 };
89
90 enum {
91     SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
92     SINK_INPUT_MESSAGE_DISABLE_PREBUF /* disabled prebuf, get playback started. */
93 };
94
95 enum {
96     CONNECTION_MESSAGE_REQUEST_DATA,      /* data requested from sink input from the main loop */
97     CONNECTION_MESSAGE_POST_DATA,         /* data from source output to main loop */
98     CONNECTION_MESSAGE_UNLINK_CONNECTION    /* Please drop a aconnection now */
99 };
100
101
102 #define PLAYBACK_BUFFER_SECONDS (.5)
103 #define PLAYBACK_BUFFER_FRAGMENTS (10)
104 #define RECORD_BUFFER_SECONDS (5)
105 #define DEFAULT_SINK_LATENCY (300*PA_USEC_PER_MSEC)
106 #define DEFAULT_SOURCE_LATENCY (300*PA_USEC_PER_MSEC)
107
108 static void connection_unlink(connection *c) {
109     pa_assert(c);
110
111     if (!c->protocol)
112         return;
113
114     if (c->sink_input) {
115         pa_sink_input_unlink(c->sink_input);
116         pa_sink_input_unref(c->sink_input);
117         c->sink_input = NULL;
118     }
119
120     if (c->source_output) {
121         pa_source_output_unlink(c->source_output);
122         pa_source_output_unref(c->source_output);
123         c->source_output = NULL;
124     }
125
126     if (c->client) {
127         pa_client_free(c->client);
128         c->client = NULL;
129     }
130
131     if (c->io) {
132         pa_iochannel_free(c->io);
133         c->io = NULL;
134     }
135
136     pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
137     c->protocol = NULL;
138     connection_unref(c);
139 }
140
141 static void connection_free(pa_object *o) {
142     connection *c = CONNECTION(o);
143     pa_assert(c);
144
145     if (c->playback.current_memblock)
146         pa_memblock_unref(c->playback.current_memblock);
147
148     if (c->input_memblockq)
149         pa_memblockq_free(c->input_memblockq);
150     if (c->output_memblockq)
151         pa_memblockq_free(c->output_memblockq);
152
153     pa_xfree(c);
154 }
155
156 static int do_read(connection *c) {
157     pa_memchunk chunk;
158     ssize_t r;
159     size_t l;
160     void *p;
161     size_t space;
162
163     connection_assert_ref(c);
164
165     if (!c->sink_input || (l = pa_atomic_load(&c->playback.missing)) <= 0)
166         return 0;
167
168     if (c->playback.current_memblock) {
169
170         space = pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index;
171
172         if (space <= 0) {
173             pa_memblock_unref(c->playback.current_memblock);
174             c->playback.current_memblock = NULL;
175         }
176     }
177
178     if (!c->playback.current_memblock) {
179         pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, 0));
180         c->playback.memblock_index = 0;
181
182         space = pa_memblock_get_length(c->playback.current_memblock);
183     }
184
185     if (l > space)
186         l = space;
187
188     p = pa_memblock_acquire(c->playback.current_memblock);
189     r = pa_iochannel_read(c->io, (uint8_t*) p + c->playback.memblock_index, l);
190     pa_memblock_release(c->playback.current_memblock);
191
192     if (r <= 0) {
193
194         if (r < 0 && (errno == EINTR || errno == EAGAIN))
195             return 0;
196
197         pa_log_debug("read(): %s", r == 0 ? "EOF" : pa_cstrerror(errno));
198         return -1;
199     }
200
201     chunk.memblock = c->playback.current_memblock;
202     chunk.index = c->playback.memblock_index;
203     chunk.length = r;
204
205     c->playback.memblock_index += r;
206
207     pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
208     pa_atomic_sub(&c->playback.missing, r);
209
210     return 0;
211 }
212
213 static int do_write(connection *c) {
214     pa_memchunk chunk;
215     ssize_t r;
216     void *p;
217
218     connection_assert_ref(c);
219
220     if (!c->source_output)
221         return 0;
222
223     if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0) {
224 /*         pa_log("peek failed"); */
225         return 0;
226     }
227
228     pa_assert(chunk.memblock);
229     pa_assert(chunk.length);
230
231     p = pa_memblock_acquire(chunk.memblock);
232     r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
233     pa_memblock_release(chunk.memblock);
234
235     pa_memblock_unref(chunk.memblock);
236
237     if (r < 0) {
238
239         if (errno == EINTR || errno == EAGAIN)
240             return 0;
241
242         pa_log("write(): %s", pa_cstrerror(errno));
243         return -1;
244     }
245
246     pa_memblockq_drop(c->output_memblockq, r);
247
248     return 0;
249 }
250
251 static void do_work(connection *c) {
252     connection_assert_ref(c);
253
254     if (c->dead)
255         return;
256
257     if (pa_iochannel_is_readable(c->io))
258         if (do_read(c) < 0)
259             goto fail;
260
261     if (!c->sink_input && pa_iochannel_is_hungup(c->io))
262         goto fail;
263
264     if (pa_iochannel_is_writable(c->io))
265         if (do_write(c) < 0)
266             goto fail;
267
268     return;
269
270 fail:
271
272     if (c->sink_input) {
273
274         /* If there is a sink input, we first drain what we already have read before shutting down the connection */
275         c->dead = TRUE;
276
277         pa_iochannel_free(c->io);
278         c->io = NULL;
279
280         pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
281     } else
282         connection_unlink(c);
283 }
284
285 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
286     connection *c = CONNECTION(o);
287     connection_assert_ref(c);
288
289     switch (code) {
290         case CONNECTION_MESSAGE_REQUEST_DATA:
291             do_work(c);
292             break;
293
294         case CONNECTION_MESSAGE_POST_DATA:
295 /*             pa_log("got data %u", chunk->length); */
296             pa_memblockq_push_align(c->output_memblockq, chunk);
297             do_work(c);
298             break;
299
300         case CONNECTION_MESSAGE_UNLINK_CONNECTION:
301             connection_unlink(c);
302             break;
303     }
304
305     return 0;
306 }
307
308 /*** sink_input callbacks ***/
309
310 /* Called from thread context */
311 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
312     pa_sink_input *i = PA_SINK_INPUT(o);
313     connection*c;
314
315     pa_sink_input_assert_ref(i);
316     c = CONNECTION(i->userdata);
317     connection_assert_ref(c);
318
319     switch (code) {
320
321         case SINK_INPUT_MESSAGE_POST_DATA: {
322             pa_assert(chunk);
323
324             /* New data from the main loop */
325             pa_memblockq_push_align(c->input_memblockq, chunk);
326
327             if (pa_memblockq_is_readable(c->input_memblockq) && c->playback.underrun) {
328                 pa_log_debug("Requesting rewind due to end of underrun.");
329                 pa_sink_input_request_rewind(c->sink_input, 0, FALSE, TRUE);
330             }
331
332 /*             pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
333
334             return 0;
335         }
336
337         case SINK_INPUT_MESSAGE_DISABLE_PREBUF:
338             pa_memblockq_prebuf_disable(c->input_memblockq);
339             return 0;
340
341         case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
342             pa_usec_t *r = userdata;
343
344             *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
345
346             /* Fall through, the default handler will add in the extra
347              * latency added by the resampler */
348         }
349
350         default:
351             return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
352     }
353 }
354
355 /* Called from thread context */
356 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
357     connection *c;
358
359     pa_sink_input_assert_ref(i);
360     c = CONNECTION(i->userdata);
361     connection_assert_ref(c);
362     pa_assert(chunk);
363
364     if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
365
366         c->playback.underrun = TRUE;
367
368         if (c->dead && pa_sink_input_safe_to_remove(i))
369             pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
370
371         return -1;
372     } else {
373         size_t m;
374
375         chunk->length = PA_MIN(length, chunk->length);
376
377         c->playback.underrun = FALSE;
378
379         pa_memblockq_drop(c->input_memblockq, chunk->length);
380         m = pa_memblockq_pop_missing(c->input_memblockq);
381
382         if (m > 0)
383             if (pa_atomic_add(&c->playback.missing, m) <= 0)
384                 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
385
386         return 0;
387     }
388 }
389
390 /* Called from thread context */
391 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
392     connection *c;
393
394     pa_sink_input_assert_ref(i);
395     c = CONNECTION(i->userdata);
396     connection_assert_ref(c);
397
398     /* If we are in an underrun, then we don't rewind */
399     if (i->thread_info.underrun_for > 0)
400         return;
401
402     pa_memblockq_rewind(c->input_memblockq, nbytes);
403 }
404
405 /* Called from thread context */
406 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
407     connection *c;
408
409     pa_sink_input_assert_ref(i);
410     c = CONNECTION(i->userdata);
411     connection_assert_ref(c);
412
413     pa_memblockq_set_maxrewind(c->input_memblockq, nbytes);
414 }
415
416 /* Called from main context */
417 static void sink_input_kill_cb(pa_sink_input *i) {
418     pa_sink_input_assert_ref(i);
419
420     connection_unlink(CONNECTION(i->userdata));
421 }
422
423 /*** source_output callbacks ***/
424
425 /* Called from thread context */
426 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
427     connection *c;
428
429     pa_source_output_assert_ref(o);
430     c = CONNECTION(o->userdata);
431     pa_assert(c);
432     pa_assert(chunk);
433
434     pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
435 }
436
437 /* Called from main context */
438 static void source_output_kill_cb(pa_source_output *o) {
439     pa_source_output_assert_ref(o);
440
441     connection_unlink(CONNECTION(o->userdata));
442 }
443
444 /* Called from main context */
445 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
446     connection*c;
447
448     pa_source_output_assert_ref(o);
449     c = CONNECTION(o->userdata);
450     pa_assert(c);
451
452     return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
453 }
454
455 /*** client callbacks ***/
456
457 static void client_kill_cb(pa_client *client) {
458     connection*c;
459
460     pa_assert(client);
461     c = CONNECTION(client->userdata);
462     pa_assert(c);
463
464     connection_unlink(c);
465 }
466
467 /*** pa_iochannel callbacks ***/
468
469 static void io_callback(pa_iochannel*io, void *userdata) {
470     connection *c = CONNECTION(userdata);
471
472     connection_assert_ref(c);
473     pa_assert(io);
474
475     do_work(c);
476 }
477
478 /*** socket_server callbacks ***/
479
480 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
481     pa_protocol_simple *p = userdata;
482     connection *c = NULL;
483     char cname[256], pname[128];
484
485     pa_assert(s);
486     pa_assert(io);
487     pa_assert(p);
488
489     if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
490         pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
491         pa_iochannel_free(io);
492         return;
493     }
494
495     c = pa_msgobject_new(connection);
496     c->parent.parent.free = connection_free;
497     c->parent.process_msg = connection_process_msg;
498     c->io = io;
499     c->sink_input = NULL;
500     c->source_output = NULL;
501     c->input_memblockq = c->output_memblockq = NULL;
502     c->protocol = p;
503     c->playback.current_memblock = NULL;
504     c->playback.memblock_index = 0;
505     c->dead = FALSE;
506     c->playback.underrun = TRUE;
507     pa_atomic_store(&c->playback.missing, 0);
508
509     pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
510     pa_snprintf(cname, sizeof(cname), "Simple client (%s)", pname);
511     pa_assert_se(c->client = pa_client_new(p->core, __FILE__, cname));
512     pa_proplist_sets(c->client->proplist, "simple-protocol.peer", pname);
513     c->client->module = p->module;
514     c->client->kill = client_kill_cb;
515     c->client->userdata = c;
516
517     if (p->mode & PLAYBACK) {
518         pa_sink_input_new_data data;
519         size_t l;
520         pa_sink *sink;
521
522         if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, TRUE))) {
523             pa_log("Failed to get sink.");
524             goto fail;
525         }
526
527         pa_sink_input_new_data_init(&data);
528         data.driver = __FILE__;
529         data.module = p->module;
530         data.client = c->client;
531         data.sink = sink;
532         pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
533         pa_sink_input_new_data_set_sample_spec(&data, &p->sample_spec);
534
535         c->sink_input = pa_sink_input_new(p->core, &data, 0);
536         pa_sink_input_new_data_done(&data);
537
538         if (!c->sink_input) {
539             pa_log("Failed to create sink input.");
540             goto fail;
541         }
542
543         c->sink_input->parent.process_msg = sink_input_process_msg;
544         c->sink_input->pop = sink_input_pop_cb;
545         c->sink_input->process_rewind = sink_input_process_rewind_cb;
546         c->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
547         c->sink_input->kill = sink_input_kill_cb;
548         c->sink_input->userdata = c;
549
550         pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY);
551
552         l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
553         c->input_memblockq = pa_memblockq_new(
554                 0,
555                 l,
556                 l,
557                 pa_frame_size(&p->sample_spec),
558                 (size_t) -1,
559                 l/PLAYBACK_BUFFER_FRAGMENTS,
560                 0,
561                 NULL);
562         pa_iochannel_socket_set_rcvbuf(io, l);
563
564         pa_atomic_store(&c->playback.missing, pa_memblockq_missing(c->input_memblockq));
565
566         pa_sink_input_put(c->sink_input);
567     }
568
569     if (p->mode & RECORD) {
570         pa_source_output_new_data data;
571         size_t l;
572         pa_source *source;
573
574         if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, TRUE))) {
575             pa_log("Failed to get source.");
576             goto fail;
577         }
578
579         pa_source_output_new_data_init(&data);
580         data.driver = __FILE__;
581         data.module = p->module;
582         data.client = c->client;
583         data.source = source;
584         pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
585         pa_source_output_new_data_set_sample_spec(&data, &p->sample_spec);
586
587         c->source_output = pa_source_output_new(p->core, &data, 0);
588         pa_source_output_new_data_done(&data);
589
590         if (!c->source_output) {
591             pa_log("Failed to create source output.");
592             goto fail;
593         }
594         c->source_output->push = source_output_push_cb;
595         c->source_output->kill = source_output_kill_cb;
596         c->source_output->get_latency = source_output_get_latency_cb;
597         c->source_output->userdata = c;
598
599         pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY);
600
601         l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
602         c->output_memblockq = pa_memblockq_new(
603                 0,
604                 l,
605                 0,
606                 pa_frame_size(&p->sample_spec),
607                 1,
608                 0,
609                 0,
610                 NULL);
611         pa_iochannel_socket_set_sndbuf(io, l);
612
613         pa_source_output_put(c->source_output);
614     }
615
616     pa_iochannel_set_callback(c->io, io_callback, c);
617     pa_idxset_put(p->connections, c, NULL);
618
619     return;
620
621 fail:
622     if (c)
623         connection_unlink(c);
624 }
625
626 pa_protocol_simple* pa_protocol_simple_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
627     pa_protocol_simple* p = NULL;
628     pa_bool_t enable;
629
630     pa_assert(core);
631     pa_assert(server);
632     pa_assert(m);
633     pa_assert(ma);
634
635     p = pa_xnew0(pa_protocol_simple, 1);
636     p->module = m;
637     p->core = core;
638     p->server = pa_socket_server_ref(server);
639     p->connections = pa_idxset_new(NULL, NULL);
640
641     p->sample_spec = core->default_sample_spec;
642     if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
643         pa_log("Failed to parse sample type specification.");
644         goto fail;
645     }
646
647     p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
648     p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
649
650     enable = FALSE;
651     if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
652         pa_log("record= expects a numeric argument.");
653         goto fail;
654     }
655     p->mode = enable ? RECORD : 0;
656
657     enable = TRUE;
658     if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
659         pa_log("playback= expects a numeric argument.");
660         goto fail;
661     }
662     p->mode |= enable ? PLAYBACK : 0;
663
664     if ((p->mode & (RECORD|PLAYBACK)) == 0) {
665         pa_log("neither playback nor recording enabled for protocol.");
666         goto fail;
667     }
668
669     pa_socket_server_set_callback(p->server, on_connection, p);
670
671     return p;
672
673 fail:
674     if (p)
675         pa_protocol_simple_free(p);
676
677     return NULL;
678 }
679
680
681 void pa_protocol_simple_free(pa_protocol_simple *p) {
682     connection *c;
683     pa_assert(p);
684
685     if (p->connections) {
686         while((c = pa_idxset_first(p->connections, NULL)))
687             connection_unlink(c);
688
689         pa_idxset_free(p->connections, NULL, NULL);
690     }
691
692     if (p->server)
693         pa_socket_server_unref(p->server);
694
695     pa_xfree(p);
696 }