4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/sink-input.h>
37 #include <pulsecore/source-output.h>
38 #include <pulsecore/client.h>
39 #include <pulsecore/sample-util.h>
40 #include <pulsecore/namereg.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/atomic.h>
44 #include <pulsecore/thread-mq.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, fragment_size;
69 PA_DECLARE_CLASS(connection);
70 #define CONNECTION(o) (connection_cast(o))
71 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
73 struct pa_protocol_simple {
76 pa_socket_server*server;
77 pa_idxset *connections;
85 pa_sample_spec sample_spec;
86 char *source_name, *sink_name;
90 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
91 SINK_INPUT_MESSAGE_DISABLE_PREBUF /* disabled prebuf, get playback started. */
95 CONNECTION_MESSAGE_REQUEST_DATA, /* data requested from sink input from the main loop */
96 CONNECTION_MESSAGE_POST_DATA, /* data from source output to main loop */
97 CONNECTION_MESSAGE_UNLINK_CONNECTION /* Please drop a aconnection now */
101 #define PLAYBACK_BUFFER_SECONDS (.5)
102 #define PLAYBACK_BUFFER_FRAGMENTS (10)
103 #define RECORD_BUFFER_SECONDS (5)
104 #define RECORD_BUFFER_FRAGMENTS (100)
106 static void connection_unlink(connection *c) {
113 pa_sink_input_unlink(c->sink_input);
114 pa_sink_input_unref(c->sink_input);
115 c->sink_input = NULL;
118 if (c->source_output) {
119 pa_source_output_unlink(c->source_output);
120 pa_source_output_unref(c->source_output);
121 c->source_output = NULL;
125 pa_client_free(c->client);
130 pa_iochannel_free(c->io);
134 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
139 static void connection_free(pa_object *o) {
140 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) {
162 connection_assert_ref(c);
164 if (!c->sink_input || (l = pa_atomic_load(&c->playback.missing)) <= 0)
167 if (l > c->playback.fragment_size)
168 l = c->playback.fragment_size;
170 if (c->playback.current_memblock)
171 if (pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index < l) {
172 pa_memblock_unref(c->playback.current_memblock);
173 c->playback.current_memblock = NULL;
174 c->playback.memblock_index = 0;
177 if (!c->playback.current_memblock) {
178 pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, l));
179 c->playback.memblock_index = 0;
182 p = pa_memblock_acquire(c->playback.current_memblock);
183 r = pa_iochannel_read(c->io, (uint8_t*) p + c->playback.memblock_index, l);
184 pa_memblock_release(c->playback.current_memblock);
188 if (r < 0 && (errno == EINTR || errno == EAGAIN))
191 pa_log_debug("read(): %s", r == 0 ? "EOF" : pa_cstrerror(errno));
195 chunk.memblock = c->playback.current_memblock;
196 chunk.index = c->playback.memblock_index;
199 c->playback.memblock_index += r;
201 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
202 pa_atomic_sub(&c->playback.missing, r);
207 static int do_write(connection *c) {
212 connection_assert_ref(c);
214 if (!c->source_output)
217 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0) {
218 /* pa_log("peek failed"); */
222 pa_assert(chunk.memblock);
223 pa_assert(chunk.length);
225 p = pa_memblock_acquire(chunk.memblock);
226 r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
227 pa_memblock_release(chunk.memblock);
229 pa_memblock_unref(chunk.memblock);
233 if (errno == EINTR || errno == EAGAIN)
236 pa_log("write(): %s", pa_cstrerror(errno));
240 pa_memblockq_drop(c->output_memblockq, r);
245 static void do_work(connection *c) {
246 connection_assert_ref(c);
251 if (pa_iochannel_is_readable(c->io)) {
254 } else if (pa_iochannel_is_hungup(c->io))
257 if (pa_iochannel_is_writable(c->io)) {
268 /* If there is a sink input, we first drain what we already have read before shutting down the connection */
271 pa_iochannel_free(c->io);
274 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
276 connection_unlink(c);
279 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
280 connection *c = CONNECTION(o);
281 connection_assert_ref(c);
284 case CONNECTION_MESSAGE_REQUEST_DATA:
288 case CONNECTION_MESSAGE_POST_DATA:
289 /* pa_log("got data %u", chunk->length); */
290 pa_memblockq_push_align(c->output_memblockq, chunk);
294 case CONNECTION_MESSAGE_UNLINK_CONNECTION:
295 connection_unlink(c);
302 /*** sink_input callbacks ***/
304 /* Called from thread context */
305 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
306 pa_sink_input *i = PA_SINK_INPUT(o);
309 pa_sink_input_assert_ref(i);
310 c = CONNECTION(i->userdata);
311 connection_assert_ref(c);
315 case SINK_INPUT_MESSAGE_POST_DATA: {
318 /* New data from the main loop */
319 pa_memblockq_push_align(c->input_memblockq, chunk);
321 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
326 case SINK_INPUT_MESSAGE_DISABLE_PREBUF: {
327 pa_memblockq_prebuf_disable(c->input_memblockq);
331 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
332 pa_usec_t *r = userdata;
334 *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
336 /* Fall through, the default handler will add in the extra
337 * latency added by the resampler */
341 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
345 /* Called from thread context */
346 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
351 c = CONNECTION(i->userdata);
352 connection_assert_ref(c);
355 if ((r = pa_memblockq_peek(c->input_memblockq, chunk)) < 0) {
357 if (c->dead && pa_sink_input_safe_to_remove(i))
358 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
363 old = pa_memblockq_missing(c->input_memblockq);
364 pa_memblockq_drop(c->input_memblockq, chunk->length);
365 new = pa_memblockq_missing(c->input_memblockq);
368 if (pa_atomic_add(&c->playback.missing, new - old) <= 0)
369 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
376 /* Called from main context */
377 static void sink_input_kill_cb(pa_sink_input *i) {
378 pa_sink_input_assert_ref(i);
380 connection_unlink(CONNECTION(i->userdata));
383 /*** source_output callbacks ***/
385 /* Called from thread context */
386 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
390 c = CONNECTION(o->userdata);
394 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
397 /* Called from main context */
398 static void source_output_kill_cb(pa_source_output *o) {
399 pa_source_output_assert_ref(o);
401 connection_unlink(CONNECTION(o->userdata));
404 /* Called from main context */
405 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
409 c = CONNECTION(o->userdata);
412 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
415 /*** client callbacks ***/
417 static void client_kill_cb(pa_client *client) {
421 c = CONNECTION(client->userdata);
424 connection_unlink(c);
427 /*** pa_iochannel callbacks ***/
429 static void io_callback(pa_iochannel*io, void *userdata) {
430 connection *c = CONNECTION(userdata);
432 connection_assert_ref(c);
438 /*** socket_server callbacks ***/
440 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
441 pa_protocol_simple *p = userdata;
442 connection *c = NULL;
449 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
450 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
451 pa_iochannel_free(io);
455 c = pa_msgobject_new(connection);
456 c->parent.parent.free = connection_free;
457 c->parent.process_msg = connection_process_msg;
459 c->sink_input = NULL;
460 c->source_output = NULL;
461 c->input_memblockq = c->output_memblockq = NULL;
463 c->playback.current_memblock = NULL;
464 c->playback.memblock_index = 0;
465 c->playback.fragment_size = 0;
467 pa_atomic_store(&c->playback.missing, 0);
469 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
470 pa_assert_se(c->client = pa_client_new(p->core, __FILE__, cname));
471 c->client->module = p->module;
472 c->client->kill = client_kill_cb;
473 c->client->userdata = c;
475 if (p->mode & PLAYBACK) {
476 pa_sink_input_new_data data;
480 if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, TRUE))) {
481 pa_log("Failed to get sink.");
485 pa_sink_input_new_data_init(&data);
486 data.driver = __FILE__;
487 data.module = p->module;
488 data.client = c->client;
490 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
491 pa_sink_input_new_data_set_sample_spec(&data, &p->sample_spec);
493 c->sink_input = pa_sink_input_new(p->core, &data, 0);
494 pa_sink_input_new_data_done(&data);
496 if (!c->sink_input) {
497 pa_log("Failed to create sink input.");
501 c->sink_input->parent.process_msg = sink_input_process_msg;
502 c->sink_input->pop = sink_input_pop_cb;
503 c->sink_input->kill = sink_input_kill_cb;
504 c->sink_input->userdata = c;
506 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
507 c->input_memblockq = pa_memblockq_new(
511 pa_frame_size(&p->sample_spec),
513 l/PLAYBACK_BUFFER_FRAGMENTS,
516 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
517 c->playback.fragment_size = l/PLAYBACK_BUFFER_FRAGMENTS;
519 pa_atomic_store(&c->playback.missing, pa_memblockq_missing(c->input_memblockq));
521 pa_sink_input_put(c->sink_input);
524 if (p->mode & RECORD) {
525 pa_source_output_new_data data;
529 if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, TRUE))) {
530 pa_log("Failed to get source.");
534 pa_source_output_new_data_init(&data);
535 data.driver = __FILE__;
536 data.module = p->module;
537 data.client = c->client;
538 data.source = source;
539 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
540 pa_source_output_new_data_set_sample_spec(&data, &p->sample_spec);
542 c->source_output = pa_source_output_new(p->core, &data, 0);
543 pa_source_output_new_data_done(&data);
545 if (!c->source_output) {
546 pa_log("Failed to create source output.");
549 c->source_output->push = source_output_push_cb;
550 c->source_output->kill = source_output_kill_cb;
551 c->source_output->get_latency = source_output_get_latency_cb;
552 c->source_output->userdata = c;
554 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
555 c->output_memblockq = pa_memblockq_new(
559 pa_frame_size(&p->sample_spec),
564 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
566 pa_source_output_put(c->source_output);
569 pa_iochannel_set_callback(c->io, io_callback, c);
570 pa_idxset_put(p->connections, c, NULL);
576 connection_unlink(c);
579 pa_protocol_simple* pa_protocol_simple_new(pa_core *core, pa_socket_server *server, pa_module *m, pa_modargs *ma) {
580 pa_protocol_simple* p = NULL;
587 p = pa_xnew0(pa_protocol_simple, 1);
591 p->connections = pa_idxset_new(NULL, NULL);
593 p->sample_spec = core->default_sample_spec;
594 if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
595 pa_log("Failed to parse sample type specification.");
599 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
600 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
603 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
604 pa_log("record= expects a numeric argument.");
607 p->mode = enable ? RECORD : 0;
610 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
611 pa_log("playback= expects a numeric argument.");
614 p->mode |= enable ? PLAYBACK : 0;
616 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
617 pa_log("neither playback nor recording enabled for protocol.");
621 pa_socket_server_set_callback(p->server, on_connection, p);
627 pa_protocol_simple_free(p);
633 void pa_protocol_simple_free(pa_protocol_simple *p) {
637 if (p->connections) {
638 while((c = pa_idxset_first(p->connections, NULL)))
639 connection_unlink(c);
641 pa_idxset_free(p->connections, NULL, NULL);
645 pa_socket_server_unref(p->server);