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_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
351 c
= CONNECTION(i
->userdata
);
352 connection_assert_ref(c
);
355 r
= pa_memblockq_peek(c
->input_memblockq
, chunk
);
357 /* pa_log("peeked %u %i", r >= 0 ? chunk->length: 0, r); */
359 if (c
->dead
&& r
< 0)
360 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_UNLINK_CONNECTION
, NULL
, 0, NULL
, NULL
);
365 /* Called from thread context */
366 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
371 c
= CONNECTION(i
->userdata
);
372 connection_assert_ref(c
);
375 old
= pa_memblockq_missing(c
->input_memblockq
);
376 pa_memblockq_drop(c
->input_memblockq
, length
);
377 new = pa_memblockq_missing(c
->input_memblockq
);
380 if (pa_atomic_add(&c
->playback
.missing
, new - old
) <= 0)
381 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
385 /* Called from main context */
386 static void sink_input_kill_cb(pa_sink_input
*i
) {
387 pa_sink_input_assert_ref(i
);
389 connection_unlink(CONNECTION(i
->userdata
));
392 /*** source_output callbacks ***/
394 /* Called from thread context */
395 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
399 c
= CONNECTION(o
->userdata
);
403 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
406 /* Called from main context */
407 static void source_output_kill_cb(pa_source_output
*o
) {
408 pa_source_output_assert_ref(o
);
410 connection_unlink(CONNECTION(o
->userdata
));
413 /* Called from main context */
414 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
418 c
= CONNECTION(o
->userdata
);
421 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
424 /*** client callbacks ***/
426 static void client_kill_cb(pa_client
*client
) {
430 c
= CONNECTION(client
->userdata
);
433 connection_unlink(c
);
436 /*** pa_iochannel callbacks ***/
438 static void io_callback(pa_iochannel
*io
, void *userdata
) {
439 connection
*c
= CONNECTION(userdata
);
441 connection_assert_ref(c
);
447 /*** socket_server callbacks ***/
449 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
450 pa_protocol_simple
*p
= userdata
;
451 connection
*c
= NULL
;
458 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
459 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
460 pa_iochannel_free(io
);
464 c
= pa_msgobject_new(connection
);
465 c
->parent
.parent
.free
= connection_free
;
466 c
->parent
.process_msg
= connection_process_msg
;
468 c
->sink_input
= NULL
;
469 c
->source_output
= NULL
;
470 c
->input_memblockq
= c
->output_memblockq
= NULL
;
472 c
->playback
.current_memblock
= NULL
;
473 c
->playback
.memblock_index
= 0;
474 c
->playback
.fragment_size
= 0;
476 pa_atomic_store(&c
->playback
.missing
, 0);
478 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
479 pa_assert_se(c
->client
= pa_client_new(p
->core
, __FILE__
, cname
));
480 c
->client
->owner
= p
->module
;
481 c
->client
->kill
= client_kill_cb
;
482 c
->client
->userdata
= c
;
484 if (p
->mode
& PLAYBACK
) {
485 pa_sink_input_new_data data
;
488 pa_sink_input_new_data_init(&data
);
489 data
.driver
= __FILE__
;
490 data
.name
= c
->client
->name
;
491 pa_sink_input_new_data_set_sample_spec(&data
, &p
->sample_spec
);
492 data
.module
= p
->module
;
493 data
.client
= c
->client
;
495 if (!(c
->sink_input
= pa_sink_input_new(p
->core
, &data
, 0))) {
496 pa_log("Failed to create sink input.");
500 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
501 c
->sink_input
->peek
= sink_input_peek_cb
;
502 c
->sink_input
->drop
= sink_input_drop_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
,
515 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
516 c
->playback
.fragment_size
= l
/PLAYBACK_BUFFER_FRAGMENTS
;
518 pa_atomic_store(&c
->playback
.missing
, pa_memblockq_missing(c
->input_memblockq
));
520 pa_sink_input_put(c
->sink_input
);
523 if (p
->mode
& RECORD
) {
524 pa_source_output_new_data data
;
527 pa_source_output_new_data_init(&data
);
528 data
.driver
= __FILE__
;
529 data
.name
= c
->client
->name
;
530 pa_source_output_new_data_set_sample_spec(&data
, &p
->sample_spec
);
531 data
.module
= p
->module
;
532 data
.client
= c
->client
;
534 if (!(c
->source_output
= pa_source_output_new(p
->core
, &data
, 0))) {
535 pa_log("Failed to create source output.");
538 c
->source_output
->push
= source_output_push_cb
;
539 c
->source_output
->kill
= source_output_kill_cb
;
540 c
->source_output
->get_latency
= source_output_get_latency_cb
;
541 c
->source_output
->userdata
= c
;
543 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
544 c
->output_memblockq
= pa_memblockq_new(
548 pa_frame_size(&p
->sample_spec
),
552 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
554 pa_source_output_put(c
->source_output
);
557 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
558 pa_idxset_put(p
->connections
, c
, NULL
);
564 connection_unlink(c
);
567 pa_protocol_simple
* pa_protocol_simple_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
568 pa_protocol_simple
* p
= NULL
;
575 p
= pa_xnew0(pa_protocol_simple
, 1);
579 p
->connections
= pa_idxset_new(NULL
, NULL
);
581 p
->sample_spec
= core
->default_sample_spec
;
582 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
583 pa_log("Failed to parse sample type specification.");
587 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
588 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
591 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
592 pa_log("record= expects a numeric argument.");
595 p
->mode
= enable
? RECORD
: 0;
598 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
599 pa_log("playback= expects a numeric argument.");
602 p
->mode
|= enable
? PLAYBACK
: 0;
604 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
605 pa_log("neither playback nor recording enabled for protocol.");
609 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
615 pa_protocol_simple_free(p
);
621 void pa_protocol_simple_free(pa_protocol_simple
*p
) {
625 if (p
->connections
) {
626 while((c
= pa_idxset_first(p
->connections
, NULL
)))
627 connection_unlink(c
);
629 pa_idxset_free(p
->connections
, NULL
, NULL
);
633 pa_socket_server_unref(p
->server
);