2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
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>
46 #include "protocol-simple.h"
48 /* Don't allow more than this many concurrent connections */
49 #define MAX_CONNECTIONS 10
51 typedef struct connection {
53 pa_protocol_simple *protocol;
55 pa_sink_input *sink_input;
56 pa_source_output *source_output;
58 pa_memblockq *input_memblockq, *output_memblockq;
63 pa_memblock *current_memblock;
64 size_t memblock_index;
70 PA_DECLARE_CLASS(connection);
71 #define CONNECTION(o) (connection_cast(o))
72 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
74 struct pa_protocol_simple {
77 pa_socket_server*server;
78 pa_idxset *connections;
86 pa_sample_spec sample_spec;
87 char *source_name, *sink_name;
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. */
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 */
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)
108 static void connection_unlink(connection *c) {
115 pa_sink_input_unlink(c->sink_input);
116 pa_sink_input_unref(c->sink_input);
117 c->sink_input = NULL;
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;
127 pa_client_free(c->client);
132 pa_iochannel_free(c->io);
136 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
141 static void connection_free(pa_object *o) {
142 connection *c = CONNECTION(o);
145 if (c->playback.current_memblock)
146 pa_memblock_unref(c->playback.current_memblock);
148 if (c->input_memblockq)
149 pa_memblockq_free(c->input_memblockq);
150 if (c->output_memblockq)
151 pa_memblockq_free(c->output_memblockq);
156 static int do_read(connection *c) {
163 connection_assert_ref(c);
165 if (!c->sink_input || (l = pa_atomic_load(&c->playback.missing)) <= 0)
168 if (c->playback.current_memblock) {
170 space = pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index;
173 pa_memblock_unref(c->playback.current_memblock);
174 c->playback.current_memblock = NULL;
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;
182 space = pa_memblock_get_length(c->playback.current_memblock);
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);
194 if (r < 0 && (errno == EINTR || errno == EAGAIN))
197 pa_log_debug("read(): %s", r == 0 ? "EOF" : pa_cstrerror(errno));
201 chunk.memblock = c->playback.current_memblock;
202 chunk.index = c->playback.memblock_index;
205 c->playback.memblock_index += r;
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);
213 static int do_write(connection *c) {
218 connection_assert_ref(c);
220 if (!c->source_output)
223 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0) {
224 /* pa_log("peek failed"); */
228 pa_assert(chunk.memblock);
229 pa_assert(chunk.length);
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);
235 pa_memblock_unref(chunk.memblock);
239 if (errno == EINTR || errno == EAGAIN)
242 pa_log("write(): %s", pa_cstrerror(errno));
246 pa_memblockq_drop(c->output_memblockq, r);
251 static void do_work(connection *c) {
252 connection_assert_ref(c);
257 if (pa_iochannel_is_readable(c->io))
261 if (!c->sink_input && pa_iochannel_is_hungup(c->io))
264 if (pa_iochannel_is_writable(c->io))
274 /* If there is a sink input, we first drain what we already have read before shutting down the connection */
277 pa_iochannel_free(c->io);
280 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
282 connection_unlink(c);
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);
290 case CONNECTION_MESSAGE_REQUEST_DATA:
294 case CONNECTION_MESSAGE_POST_DATA:
295 /* pa_log("got data %u", chunk->length); */
296 pa_memblockq_push_align(c->output_memblockq, chunk);
300 case CONNECTION_MESSAGE_UNLINK_CONNECTION:
301 connection_unlink(c);
308 /*** sink_input callbacks ***/
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);
315 pa_sink_input_assert_ref(i);
316 c = CONNECTION(i->userdata);
317 connection_assert_ref(c);
321 case SINK_INPUT_MESSAGE_POST_DATA: {
324 /* New data from the main loop */
325 pa_memblockq_push_align(c->input_memblockq, chunk);
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);
332 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
337 case SINK_INPUT_MESSAGE_DISABLE_PREBUF:
338 pa_memblockq_prebuf_disable(c->input_memblockq);
341 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
342 pa_usec_t *r = userdata;
344 *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
346 /* Fall through, the default handler will add in the extra
347 * latency added by the resampler */
351 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
355 /* Called from thread context */
356 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
359 pa_sink_input_assert_ref(i);
360 c = CONNECTION(i->userdata);
361 connection_assert_ref(c);
364 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
366 c->playback.underrun = TRUE;
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);
375 chunk->length = PA_MIN(length, chunk->length);
377 c->playback.underrun = FALSE;
379 pa_memblockq_drop(c->input_memblockq, chunk->length);
380 m = pa_memblockq_pop_missing(c->input_memblockq);
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);
390 /* Called from thread context */
391 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
394 pa_sink_input_assert_ref(i);
395 c = CONNECTION(i->userdata);
396 connection_assert_ref(c);
398 /* If we are in an underrun, then we don't rewind */
399 if (i->thread_info.underrun_for > 0)
402 pa_memblockq_rewind(c->input_memblockq, nbytes);
405 /* Called from thread context */
406 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
409 pa_sink_input_assert_ref(i);
410 c = CONNECTION(i->userdata);
411 connection_assert_ref(c);
413 pa_memblockq_set_maxrewind(c->input_memblockq, nbytes);
416 /* Called from main context */
417 static void sink_input_kill_cb(pa_sink_input *i) {
418 pa_sink_input_assert_ref(i);
420 connection_unlink(CONNECTION(i->userdata));
423 /*** source_output callbacks ***/
425 /* Called from thread context */
426 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
429 pa_source_output_assert_ref(o);
430 c = CONNECTION(o->userdata);
434 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
437 /* Called from main context */
438 static void source_output_kill_cb(pa_source_output *o) {
439 pa_source_output_assert_ref(o);
441 connection_unlink(CONNECTION(o->userdata));
444 /* Called from main context */
445 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
448 pa_source_output_assert_ref(o);
449 c = CONNECTION(o->userdata);
452 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
455 /*** client callbacks ***/
457 static void client_kill_cb(pa_client *client) {
461 c = CONNECTION(client->userdata);
464 connection_unlink(c);
467 /*** pa_iochannel callbacks ***/
469 static void io_callback(pa_iochannel*io, void *userdata) {
470 connection *c = CONNECTION(userdata);
472 connection_assert_ref(c);
478 /*** socket_server callbacks ***/
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];
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);
495 c = pa_msgobject_new(connection);
496 c->parent.parent.free = connection_free;
497 c->parent.process_msg = connection_process_msg;
499 c->sink_input = NULL;
500 c->source_output = NULL;
501 c->input_memblockq = c->output_memblockq = NULL;
503 c->playback.current_memblock = NULL;
504 c->playback.memblock_index = 0;
506 c->playback.underrun = TRUE;
507 pa_atomic_store(&c->playback.missing, 0);
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;
517 if (p->mode & PLAYBACK) {
518 pa_sink_input_new_data data;
522 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, TRUE))) {
523 pa_log("Failed to get sink.");
527 pa_sink_input_new_data_init(&data);
528 data.driver = __FILE__;
529 data.module = p->module;
530 data.client = c->client;
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);
535 c->sink_input = pa_sink_input_new(p->core, &data, 0);
536 pa_sink_input_new_data_done(&data);
538 if (!c->sink_input) {
539 pa_log("Failed to create sink input.");
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;
550 pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY);
552 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
553 c->input_memblockq = pa_memblockq_new(
557 pa_frame_size(&p->sample_spec),
559 l/PLAYBACK_BUFFER_FRAGMENTS,
562 pa_iochannel_socket_set_rcvbuf(io, l);
564 pa_atomic_store(&c->playback.missing, pa_memblockq_missing(c->input_memblockq));
566 pa_sink_input_put(c->sink_input);
569 if (p->mode & RECORD) {
570 pa_source_output_new_data data;
574 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, TRUE))) {
575 pa_log("Failed to get source.");
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);
587 c->source_output = pa_source_output_new(p->core, &data, 0);
588 pa_source_output_new_data_done(&data);
590 if (!c->source_output) {
591 pa_log("Failed to create source output.");
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;
599 pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY);
601 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
602 c->output_memblockq = pa_memblockq_new(
606 pa_frame_size(&p->sample_spec),
611 pa_iochannel_socket_set_sndbuf(io, l);
613 pa_source_output_put(c->source_output);
616 pa_iochannel_set_callback(c->io, io_callback, c);
617 pa_idxset_put(p->connections, c, NULL);
623 connection_unlink(c);
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;
635 p = pa_xnew0(pa_protocol_simple, 1);
638 p->server = pa_socket_server_ref(server);
639 p->connections = pa_idxset_new(NULL, NULL);
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.");
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));
651 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
652 pa_log("record= expects a numeric argument.");
655 p->mode = enable ? RECORD : 0;
658 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
659 pa_log("playback= expects a numeric argument.");
662 p->mode |= enable ? PLAYBACK : 0;
664 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
665 pa_log("neither playback nor recording enabled for protocol.");
669 pa_socket_server_set_callback(p->server, on_connection, p);
675 pa_protocol_simple_free(p);
681 void pa_protocol_simple_free(pa_protocol_simple *p) {
685 if (p->connections) {
686 while((c = pa_idxset_first(p->connections, NULL)))
687 connection_unlink(c);
689 pa_idxset_free(p->connections, NULL, NULL);
693 pa_socket_server_unref(p->server);