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>
45 #include "protocol-simple.h"
47 /* Don't allow more than this many concurrent connections */
48 #define MAX_CONNECTIONS 10
50 typedef struct connection
{
52 pa_protocol_simple
*protocol
;
54 pa_sink_input
*sink_input
;
55 pa_source_output
*source_output
;
57 pa_memblockq
*input_memblockq
, *output_memblockq
;
62 pa_memblock
*current_memblock
;
63 size_t memblock_index
, fragment_size
;
68 PA_DECLARE_CLASS(connection
);
69 #define CONNECTION(o) (connection_cast(o))
70 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
72 struct pa_protocol_simple
{
75 pa_socket_server
*server
;
76 pa_idxset
*connections
;
84 pa_sample_spec sample_spec
;
85 char *source_name
, *sink_name
;
89 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
90 SINK_INPUT_MESSAGE_DISABLE_PREBUF
/* disabled prebuf, get playback started. */
94 CONNECTION_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
95 CONNECTION_MESSAGE_POST_DATA
, /* data from source output to main loop */
96 CONNECTION_MESSAGE_DROP_CONNECTION
/* Please drop a aconnection now */
100 #define PLAYBACK_BUFFER_SECONDS (.5)
101 #define PLAYBACK_BUFFER_FRAGMENTS (10)
102 #define RECORD_BUFFER_SECONDS (5)
103 #define RECORD_BUFFER_FRAGMENTS (100)
105 static void connection_unlink(connection
*c
) {
112 pa_sink_input_disconnect(c
->sink_input
);
113 pa_sink_input_unref(c
->sink_input
);
114 c
->sink_input
= NULL
;
117 if (c
->source_output
) {
118 pa_source_output_disconnect(c
->source_output
);
119 pa_source_output_unref(c
->source_output
);
120 c
->source_output
= NULL
;
124 pa_client_free(c
->client
);
128 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
133 static void connection_free(pa_object
*o
) {
134 connection
*c
= CONNECTION(o
);
139 if (c
->playback
.current_memblock
)
140 pa_memblock_unref(c
->playback
.current_memblock
);
143 pa_iochannel_free(c
->io
);
144 if (c
->input_memblockq
)
145 pa_memblockq_free(c
->input_memblockq
);
146 if (c
->output_memblockq
)
147 pa_memblockq_free(c
->output_memblockq
);
152 static int do_read(connection
*c
) {
160 if (!c
->sink_input
|| (l
= pa_atomic_load(&c
->playback
.missing
)) <= 0)
163 if (l
> c
->playback
.fragment_size
)
164 l
= c
->playback
.fragment_size
;
166 if (c
->playback
.current_memblock
)
167 if (pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
< l
) {
168 pa_memblock_unref(c
->playback
.current_memblock
);
169 c
->playback
.current_memblock
= NULL
;
170 c
->playback
.memblock_index
= 0;
173 if (!c
->playback
.current_memblock
) {
174 pa_assert_se(c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, l
));
175 c
->playback
.memblock_index
= 0;
178 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
179 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+ c
->playback
.memblock_index
, l
);
180 pa_memblock_release(c
->playback
.current_memblock
);
184 if (errno
== EINTR
|| errno
== EAGAIN
)
187 pa_log_debug("read(): %s", r
== 0 ? "EOF" : pa_cstrerror(errno
));
191 chunk
.memblock
= c
->playback
.current_memblock
;
192 chunk
.index
= c
->playback
.memblock_index
;
195 c
->playback
.memblock_index
+= r
;
197 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, &chunk
, NULL
);
198 pa_atomic_sub(&c
->playback
.missing
, r
);
203 static int do_write(connection
*c
) {
210 if (!c
->source_output
)
213 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0) {
214 /* pa_log("peek failed"); */
218 pa_assert(chunk
.memblock
);
219 pa_assert(chunk
.length
);
221 p
= pa_memblock_acquire(chunk
.memblock
);
222 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
223 pa_memblock_release(chunk
.memblock
);
225 pa_memblock_unref(chunk
.memblock
);
229 if (errno
== EINTR
|| errno
== EAGAIN
)
232 pa_log("write(): %s", pa_cstrerror(errno
));
236 pa_memblockq_drop(c
->output_memblockq
, r
);
241 static void do_work(connection
*c
) {
247 if (pa_iochannel_is_readable(c
->io
)) {
250 } else if (pa_iochannel_is_hungup(c
->io
))
253 if (pa_iochannel_is_writable(c
->io
)) {
264 /* If there is a sink input, we first drain what we already have read before shutting down the connection */
267 pa_iochannel_free(c
->io
);
270 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_DISABLE_PREBUF
, NULL
, 0, NULL
, NULL
);
272 connection_unlink(c
);
275 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
276 connection
*c
= CONNECTION(o
);
277 connection_assert_ref(c
);
280 case CONNECTION_MESSAGE_REQUEST_DATA
:
284 case CONNECTION_MESSAGE_POST_DATA
:
285 /* pa_log("got data %u", chunk->length); */
286 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
290 case CONNECTION_MESSAGE_DROP_CONNECTION
:
291 connection_unlink(c
);
298 /*** sink_input callbacks ***/
300 /* Called from thread context */
301 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
302 pa_sink_input
*i
= PA_SINK_INPUT(o
);
305 pa_sink_input_assert_ref(i
);
306 c
= CONNECTION(i
->userdata
);
307 connection_assert_ref(c
);
311 case SINK_INPUT_MESSAGE_POST_DATA
: {
314 /* New data from the main loop */
315 pa_memblockq_push_align(c
->input_memblockq
, chunk
);
317 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
322 case SINK_INPUT_MESSAGE_DISABLE_PREBUF
: {
323 pa_memblockq_prebuf_disable(c
->input_memblockq
);
327 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
328 pa_usec_t
*r
= userdata
;
330 *r
= pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
332 /* Fall through, the default handler will add in the extra
333 * latency added by the resampler */
337 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
341 /* Called from thread context */
342 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
351 r
= pa_memblockq_peek(c
->input_memblockq
, chunk
);
353 /* pa_log("peeked %u %i", r >= 0 ? chunk->length: 0, r); */
355 if (c
->dead
&& r
< 0)
356 pa_asyncmsgq_post(c
->protocol
->core
->asyncmsgq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_DROP_CONNECTION
, NULL
, 0, NULL
, NULL
);
361 /* Called from thread context */
362 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
363 connection
*c
= i
->userdata
;
370 old
= pa_memblockq_missing(c
->input_memblockq
);
371 pa_memblockq_drop(c
->input_memblockq
, length
);
372 new = pa_memblockq_missing(c
->input_memblockq
);
375 if (pa_atomic_add(&c
->playback
.missing
, new - old
) <= 0)
376 pa_asyncmsgq_post(c
->protocol
->core
->asyncmsgq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
380 /* Called from main context */
381 static void sink_input_kill_cb(pa_sink_input
*i
) {
382 pa_sink_input_assert_ref(i
);
384 connection_unlink(CONNECTION(i
->userdata
));
387 /*** source_output callbacks ***/
389 /* Called from thread context */
390 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
398 pa_asyncmsgq_post(c
->protocol
->core
->asyncmsgq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
401 /* Called from main context */
402 static void source_output_kill_cb(pa_source_output
*o
) {
403 pa_source_output_assert_ref(o
);
405 connection_unlink(CONNECTION(o
->userdata
));
408 /* Called from main context */
409 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
413 c
= CONNECTION(o
->userdata
);
416 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
419 /*** client callbacks ***/
421 static void client_kill_cb(pa_client
*client
) {
425 c
= CONNECTION(client
->userdata
);
428 connection_unlink(c
);
431 /*** pa_iochannel callbacks ***/
433 static void io_callback(pa_iochannel
*io
, void *userdata
) {
434 connection
*c
= CONNECTION(userdata
);
442 /*** socket_server callbacks ***/
444 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
445 pa_protocol_simple
*p
= userdata
;
446 connection
*c
= NULL
;
453 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
454 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
455 pa_iochannel_free(io
);
459 c
= pa_msgobject_new(connection
);
460 c
->parent
.parent
.free
= connection_free
;
461 c
->parent
.process_msg
= connection_process_msg
;
463 c
->sink_input
= NULL
;
464 c
->source_output
= NULL
;
465 c
->input_memblockq
= c
->output_memblockq
= NULL
;
467 c
->playback
.current_memblock
= NULL
;
468 c
->playback
.memblock_index
= 0;
469 c
->playback
.fragment_size
= 0;
471 pa_atomic_store(&c
->playback
.missing
, 0);
473 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
474 pa_assert_se(c
->client
= pa_client_new(p
->core
, __FILE__
, cname
));
475 c
->client
->owner
= p
->module
;
476 c
->client
->kill
= client_kill_cb
;
477 c
->client
->userdata
= c
;
479 if (p
->mode
& PLAYBACK
) {
480 pa_sink_input_new_data data
;
483 pa_sink_input_new_data_init(&data
);
484 data
.driver
= __FILE__
;
485 data
.name
= c
->client
->name
;
486 pa_sink_input_new_data_set_sample_spec(&data
, &p
->sample_spec
);
487 data
.module
= p
->module
;
488 data
.client
= c
->client
;
490 if (!(c
->sink_input
= pa_sink_input_new(p
->core
, &data
, 0))) {
491 pa_log("Failed to create sink input.");
495 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
496 c
->sink_input
->peek
= sink_input_peek_cb
;
497 c
->sink_input
->drop
= sink_input_drop_cb
;
498 c
->sink_input
->kill
= sink_input_kill_cb
;
499 c
->sink_input
->userdata
= c
;
501 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
502 c
->input_memblockq
= pa_memblockq_new(
506 pa_frame_size(&p
->sample_spec
),
508 l
/PLAYBACK_BUFFER_FRAGMENTS
,
510 pa_assert(c
->input_memblockq
);
511 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
512 c
->playback
.fragment_size
= l
/PLAYBACK_BUFFER_FRAGMENTS
;
514 pa_atomic_store(&c
->playback
.missing
, pa_memblockq_missing(c
->input_memblockq
));
516 pa_sink_input_put(c
->sink_input
);
519 if (p
->mode
& RECORD
) {
520 pa_source_output_new_data data
;
523 pa_source_output_new_data_init(&data
);
524 data
.driver
= __FILE__
;
525 data
.name
= c
->client
->name
;
526 pa_source_output_new_data_set_sample_spec(&data
, &p
->sample_spec
);
527 data
.module
= p
->module
;
528 data
.client
= c
->client
;
530 if (!(c
->source_output
= pa_source_output_new(p
->core
, &data
, 0))) {
531 pa_log("Failed to create source output.");
534 c
->source_output
->push
= source_output_push_cb
;
535 c
->source_output
->kill
= source_output_kill_cb
;
536 c
->source_output
->get_latency
= source_output_get_latency_cb
;
537 c
->source_output
->userdata
= c
;
539 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
540 c
->output_memblockq
= pa_memblockq_new(
544 pa_frame_size(&p
->sample_spec
),
548 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
550 pa_source_output_put(c
->source_output
);
553 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
554 pa_idxset_put(p
->connections
, c
, NULL
);
560 connection_unlink(c
);
563 pa_protocol_simple
* pa_protocol_simple_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
564 pa_protocol_simple
* p
= NULL
;
571 p
= pa_xnew0(pa_protocol_simple
, 1);
575 p
->connections
= pa_idxset_new(NULL
, NULL
);
577 p
->sample_spec
= core
->default_sample_spec
;
578 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
579 pa_log("Failed to parse sample type specification.");
583 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
584 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
587 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
588 pa_log("record= expects a numeric argument.");
591 p
->mode
= enable
? RECORD
: 0;
594 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
595 pa_log("playback= expects a numeric argument.");
598 p
->mode
|= enable
? PLAYBACK
: 0;
600 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
601 pa_log("neither playback nor recording enabled for protocol.");
605 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
611 pa_protocol_simple_free(p
);
617 void pa_protocol_simple_free(pa_protocol_simple
*p
) {
621 if (p
->connections
) {
622 while((c
= pa_idxset_first(p
->connections
, NULL
)))
623 connection_unlink(c
);
625 pa_idxset_free(p
->connections
, NULL
, NULL
);
629 pa_socket_server_unref(p
->server
);