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
35 #include <pulse/xmalloc.h>
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>
45 #include "protocol-simple.h"
47 /* Don't allow more than this many concurrent connections */
48 #define MAX_CONNECTIONS 10
51 pa_protocol_simple *protocol;
53 pa_sink_input *sink_input;
54 pa_source_output *source_output;
56 pa_memblockq *input_memblockq, *output_memblockq;
57 pa_defer_event *defer_event;
62 pa_memblock *current_memblock;
63 size_t memblock_index, fragment_size;
67 struct pa_protocol_simple {
70 pa_socket_server*server;
71 pa_idxset *connections;
77 pa_sample_spec sample_spec;
78 char *source_name, *sink_name;
81 #define PLAYBACK_BUFFER_SECONDS (.5)
82 #define PLAYBACK_BUFFER_FRAGMENTS (10)
83 #define RECORD_BUFFER_SECONDS (5)
84 #define RECORD_BUFFER_FRAGMENTS (100)
86 static void connection_free(struct connection *c) {
89 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
91 if (c->playback.current_memblock)
92 pa_memblock_unref(c->playback.current_memblock);
94 pa_sink_input_disconnect(c->sink_input);
95 pa_sink_input_unref(c->sink_input);
97 if (c->source_output) {
98 pa_source_output_disconnect(c->source_output);
99 pa_source_output_unref(c->source_output);
102 pa_client_free(c->client);
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);
110 c->protocol->core->mainloop->defer_free(c->defer_event);
114 static int do_read(struct connection *c) {
119 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
122 if (l > c->playback.fragment_size)
123 l = c->playback.fragment_size;
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;
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;
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));
143 chunk.memblock = c->playback.current_memblock;
144 chunk.index = c->playback.memblock_index;
146 assert(chunk.memblock);
148 c->playback.memblock_index += r;
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);
158 static int do_write(struct connection *c) {
162 if (!c->source_output)
165 assert(c->output_memblockq);
166 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
169 assert(chunk.memblock && chunk.length);
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));
177 pa_memblockq_drop(c->output_memblockq, &chunk, r);
178 pa_memblock_unref(chunk.memblock);
180 pa_source_notify(c->source_output->source);
185 static void do_work(struct connection *c) {
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);
194 if (pa_iochannel_is_readable(c->io)) {
197 } else if (pa_iochannel_is_hungup(c->io))
200 if (pa_iochannel_is_writable(c->io)) {
212 pa_iochannel_free(c->io);
215 pa_memblockq_prebuf_disable(c->input_memblockq);
216 pa_sink_notify(c->sink_input->sink);
221 /*** sink_input callbacks ***/
223 static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
225 assert(i && i->userdata && chunk);
228 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
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);
243 pa_memblockq_drop(c->input_memblockq, chunk, length);
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);
250 static void sink_input_kill_cb(pa_sink_input *i) {
251 assert(i && i->userdata);
252 connection_free((struct connection *) i->userdata);
256 static pa_usec_t sink_input_get_latency_cb(pa_sink_input *i) {
257 struct connection*c = i->userdata;
259 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
262 /*** source_output callbacks ***/
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);
268 pa_memblockq_push(c->output_memblockq, chunk);
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);
275 static void source_output_kill_cb(pa_source_output *o) {
276 assert(o && o->userdata);
277 connection_free((struct connection *) o->userdata);
280 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
281 struct connection*c = o->userdata;
283 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
286 /*** client callbacks ***/
288 static void client_kill_cb(pa_client *c) {
289 assert(c && c->userdata);
290 connection_free((struct connection *) c->userdata);
293 /*** pa_iochannel callbacks ***/
295 static void io_callback(pa_iochannel*io, void *userdata) {
296 struct connection *c = userdata;
297 assert(io && c && c->io == io);
302 /*** fixed callback ***/
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);
311 /*** socket_server callbacks ***/
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;
317 assert(s && io && p);
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);
325 c = pa_xmalloc(sizeof(struct connection));
327 c->sink_input = NULL;
328 c->source_output = NULL;
329 c->defer_event = NULL;
330 c->input_memblockq = c->output_memblockq = NULL;
332 c->playback.current_memblock = NULL;
333 c->playback.memblock_index = 0;
334 c->playback.fragment_size = 0;
337 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
338 c->client = pa_client_new(p->core, __FILE__, cname);
340 c->client->owner = p->module;
341 c->client->kill = client_kill_cb;
342 c->client->userdata = c;
344 if (p->mode & PLAYBACK) {
345 pa_sink_input_new_data data;
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;
355 if (!(c->sink_input = pa_sink_input_new(p->core, &data, 0))) {
356 pa_log("Failed to create sink input.");
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;
366 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
367 c->input_memblockq = pa_memblockq_new(
371 pa_frame_size(&p->sample_spec),
373 l/PLAYBACK_BUFFER_FRAGMENTS,
375 assert(c->input_memblockq);
376 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
377 c->playback.fragment_size = l/10;
379 pa_sink_notify(c->sink_input->sink);
382 if (p->mode & RECORD) {
383 pa_source_output_new_data data;
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;
393 if (!(c->source_output = pa_source_output_new(p->core, &data, 0))) {
394 pa_log("Failed to create source output.");
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;
402 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
403 c->output_memblockq = pa_memblockq_new(
407 pa_frame_size(&p->sample_spec),
411 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
412 pa_source_notify(c->source_output->source);
415 pa_iochannel_set_callback(c->io, io_callback, c);
416 pa_idxset_put(p->connections, c, NULL);
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);
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;
432 assert(core && server && ma);
434 p = pa_xmalloc0(sizeof(pa_protocol_simple));
438 p->connections = pa_idxset_new(NULL, NULL);
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.");
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));
450 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
451 pa_log("record= expects a numeric argument.");
454 p->mode = enable ? RECORD : 0;
457 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
458 pa_log("playback= expects a numeric argument.");
461 p->mode |= enable ? PLAYBACK : 0;
463 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
464 pa_log("neither playback nor recording enabled for protocol.");
468 pa_socket_server_set_callback(p->server, on_connection, p);
474 pa_protocol_simple_free(p);
479 void pa_protocol_simple_free(pa_protocol_simple *p) {
480 struct connection *c;
483 if (p->connections) {
484 while((c = pa_idxset_first(p->connections, NULL)))
487 pa_idxset_free(p->connections, NULL, NULL);
491 pa_socket_server_unref(p->server);