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>
45 #include <pulsecore/shared.h>
47 #include "protocol-simple.h"
49 /* Don't allow more than this many concurrent connections */
50 #define MAX_CONNECTIONS 10
52 typedef struct connection
{
54 pa_simple_protocol
*protocol
;
55 pa_simple_options
*options
;
57 pa_sink_input
*sink_input
;
58 pa_source_output
*source_output
;
60 pa_memblockq
*input_memblockq
, *output_memblockq
;
65 pa_memblock
*current_memblock
;
66 size_t memblock_index
;
72 PA_DECLARE_CLASS(connection
);
73 #define CONNECTION(o) (connection_cast(o))
74 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
76 struct pa_simple_protocol
{
80 pa_idxset
*connections
;
84 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
85 SINK_INPUT_MESSAGE_DISABLE_PREBUF
/* disabled prebuf, get playback started. */
89 CONNECTION_MESSAGE_REQUEST_DATA
, /* data requested from sink input from the main loop */
90 CONNECTION_MESSAGE_POST_DATA
, /* data from source output to main loop */
91 CONNECTION_MESSAGE_UNLINK_CONNECTION
/* Please drop a aconnection now */
94 #define PLAYBACK_BUFFER_SECONDS (.5)
95 #define PLAYBACK_BUFFER_FRAGMENTS (10)
96 #define RECORD_BUFFER_SECONDS (5)
97 #define DEFAULT_SINK_LATENCY (300*PA_USEC_PER_MSEC)
98 #define DEFAULT_SOURCE_LATENCY (300*PA_USEC_PER_MSEC)
100 static void connection_unlink(connection
*c
) {
107 pa_simple_options_unref(c
->options
);
112 pa_sink_input_unlink(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_unlink(c
->source_output
);
119 pa_source_output_unref(c
->source_output
);
120 c
->source_output
= NULL
;
124 pa_client_free(c
->client
);
129 pa_iochannel_free(c
->io
);
133 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
138 static void connection_free(pa_object
*o
) {
139 connection
*c
= CONNECTION(o
);
142 if (c
->playback
.current_memblock
)
143 pa_memblock_unref(c
->playback
.current_memblock
);
145 if (c
->input_memblockq
)
146 pa_memblockq_free(c
->input_memblockq
);
147 if (c
->output_memblockq
)
148 pa_memblockq_free(c
->output_memblockq
);
153 static int do_read(connection
*c
) {
160 connection_assert_ref(c
);
162 if (!c
->sink_input
|| (l
= (size_t) pa_atomic_load(&c
->playback
.missing
)) <= 0)
165 if (c
->playback
.current_memblock
) {
167 space
= pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
;
170 pa_memblock_unref(c
->playback
.current_memblock
);
171 c
->playback
.current_memblock
= NULL
;
175 if (!c
->playback
.current_memblock
) {
176 pa_assert_se(c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, 0));
177 c
->playback
.memblock_index
= 0;
179 space
= pa_memblock_get_length(c
->playback
.current_memblock
);
185 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
186 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+ c
->playback
.memblock_index
, l
);
187 pa_memblock_release(c
->playback
.current_memblock
);
191 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
194 pa_log_debug("read(): %s", r
== 0 ? "EOF" : pa_cstrerror(errno
));
198 chunk
.memblock
= c
->playback
.current_memblock
;
199 chunk
.index
= c
->playback
.memblock_index
;
200 chunk
.length
= (size_t) r
;
202 c
->playback
.memblock_index
+= (size_t) r
;
204 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, &chunk
, NULL
);
205 pa_atomic_sub(&c
->playback
.missing
, (int) r
);
210 static int do_write(connection
*c
) {
215 connection_assert_ref(c
);
217 if (!c
->source_output
)
220 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0) {
221 /* pa_log("peek failed"); */
225 pa_assert(chunk
.memblock
);
226 pa_assert(chunk
.length
);
228 p
= pa_memblock_acquire(chunk
.memblock
);
229 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
230 pa_memblock_release(chunk
.memblock
);
232 pa_memblock_unref(chunk
.memblock
);
236 if (errno
== EINTR
|| errno
== EAGAIN
)
239 pa_log("write(): %s", pa_cstrerror(errno
));
243 pa_memblockq_drop(c
->output_memblockq
, (size_t) r
);
248 static void do_work(connection
*c
) {
249 connection_assert_ref(c
);
254 if (pa_iochannel_is_readable(c
->io
))
258 if (!c
->sink_input
&& pa_iochannel_is_hungup(c
->io
))
261 if (pa_iochannel_is_writable(c
->io
))
271 /* If there is a sink input, we first drain what we already have read before shutting down the connection */
274 pa_iochannel_free(c
->io
);
277 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_DISABLE_PREBUF
, NULL
, 0, NULL
, NULL
);
279 connection_unlink(c
);
282 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
283 connection
*c
= CONNECTION(o
);
284 connection_assert_ref(c
);
287 case CONNECTION_MESSAGE_REQUEST_DATA
:
291 case CONNECTION_MESSAGE_POST_DATA
:
292 /* pa_log("got data %u", chunk->length); */
293 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
297 case CONNECTION_MESSAGE_UNLINK_CONNECTION
:
298 connection_unlink(c
);
305 /*** sink_input callbacks ***/
307 /* Called from thread context */
308 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
309 pa_sink_input
*i
= PA_SINK_INPUT(o
);
312 pa_sink_input_assert_ref(i
);
313 c
= CONNECTION(i
->userdata
);
314 connection_assert_ref(c
);
318 case SINK_INPUT_MESSAGE_POST_DATA
: {
321 /* New data from the main loop */
322 pa_memblockq_push_align(c
->input_memblockq
, chunk
);
324 if (pa_memblockq_is_readable(c
->input_memblockq
) && c
->playback
.underrun
) {
325 pa_log_debug("Requesting rewind due to end of underrun.");
326 pa_sink_input_request_rewind(c
->sink_input
, 0, FALSE
, TRUE
);
329 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
334 case SINK_INPUT_MESSAGE_DISABLE_PREBUF
:
335 pa_memblockq_prebuf_disable(c
->input_memblockq
);
338 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
339 pa_usec_t
*r
= userdata
;
341 *r
= pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
343 /* Fall through, the default handler will add in the extra
344 * latency added by the resampler */
348 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
352 /* Called from thread context */
353 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
356 pa_sink_input_assert_ref(i
);
357 c
= CONNECTION(i
->userdata
);
358 connection_assert_ref(c
);
361 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
363 c
->playback
.underrun
= TRUE
;
365 if (c
->dead
&& pa_sink_input_safe_to_remove(i
))
366 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_UNLINK_CONNECTION
, NULL
, 0, NULL
, NULL
);
372 chunk
->length
= PA_MIN(length
, chunk
->length
);
374 c
->playback
.underrun
= FALSE
;
376 pa_memblockq_drop(c
->input_memblockq
, chunk
->length
);
377 m
= pa_memblockq_pop_missing(c
->input_memblockq
);
380 if (pa_atomic_add(&c
->playback
.missing
, (int) m
) <= 0)
381 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
387 /* Called from thread context */
388 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
391 pa_sink_input_assert_ref(i
);
392 c
= CONNECTION(i
->userdata
);
393 connection_assert_ref(c
);
395 /* If we are in an underrun, then we don't rewind */
396 if (i
->thread_info
.underrun_for
> 0)
399 pa_memblockq_rewind(c
->input_memblockq
, nbytes
);
402 /* Called from thread context */
403 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
406 pa_sink_input_assert_ref(i
);
407 c
= CONNECTION(i
->userdata
);
408 connection_assert_ref(c
);
410 pa_memblockq_set_maxrewind(c
->input_memblockq
, nbytes
);
413 /* Called from main context */
414 static void sink_input_kill_cb(pa_sink_input
*i
) {
415 pa_sink_input_assert_ref(i
);
417 connection_unlink(CONNECTION(i
->userdata
));
420 /*** source_output callbacks ***/
422 /* Called from thread context */
423 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
426 pa_source_output_assert_ref(o
);
427 c
= CONNECTION(o
->userdata
);
431 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
434 /* Called from main context */
435 static void source_output_kill_cb(pa_source_output
*o
) {
436 pa_source_output_assert_ref(o
);
438 connection_unlink(CONNECTION(o
->userdata
));
441 /* Called from main context */
442 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
445 pa_source_output_assert_ref(o
);
446 c
= CONNECTION(o
->userdata
);
449 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
452 /*** client callbacks ***/
454 static void client_kill_cb(pa_client
*client
) {
458 c
= CONNECTION(client
->userdata
);
461 connection_unlink(c
);
464 /*** pa_iochannel callbacks ***/
466 static void io_callback(pa_iochannel
*io
, void *userdata
) {
467 connection
*c
= CONNECTION(userdata
);
469 connection_assert_ref(c
);
475 /*** socket_server callbacks ***/
477 void pa_simple_protocol_connect(pa_simple_protocol
*p
, pa_iochannel
*io
, pa_simple_options
*o
) {
478 connection
*c
= NULL
;
479 char cname
[256], pname
[128];
485 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
486 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
487 pa_iochannel_free(io
);
491 c
= pa_msgobject_new(connection
);
492 c
->parent
.parent
.free
= connection_free
;
493 c
->parent
.process_msg
= connection_process_msg
;
495 c
->sink_input
= NULL
;
496 c
->source_output
= NULL
;
497 c
->input_memblockq
= c
->output_memblockq
= NULL
;
499 c
->options
= pa_simple_options_ref(o
);
500 c
->playback
.current_memblock
= NULL
;
501 c
->playback
.memblock_index
= 0;
503 c
->playback
.underrun
= TRUE
;
504 pa_atomic_store(&c
->playback
.missing
, 0);
506 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
507 pa_snprintf(cname
, sizeof(cname
), "Simple client (%s)", pname
);
508 pa_assert_se(c
->client
= pa_client_new(p
->core
, __FILE__
, cname
));
509 pa_proplist_sets(c
->client
->proplist
, "simple-protocol.peer", pname
);
510 c
->client
->module
= o
->module
;
511 c
->client
->kill
= client_kill_cb
;
512 c
->client
->userdata
= c
;
515 pa_sink_input_new_data data
;
519 if (!(sink
= pa_namereg_get(c
->protocol
->core
, o
->default_sink
, PA_NAMEREG_SINK
, TRUE
))) {
520 pa_log("Failed to get sink.");
524 pa_sink_input_new_data_init(&data
);
525 data
.driver
= __FILE__
;
526 data
.module
= o
->module
;
527 data
.client
= c
->client
;
529 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
530 pa_sink_input_new_data_set_sample_spec(&data
, &o
->sample_spec
);
532 c
->sink_input
= pa_sink_input_new(p
->core
, &data
, 0);
533 pa_sink_input_new_data_done(&data
);
535 if (!c
->sink_input
) {
536 pa_log("Failed to create sink input.");
540 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
541 c
->sink_input
->pop
= sink_input_pop_cb
;
542 c
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
543 c
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
544 c
->sink_input
->kill
= sink_input_kill_cb
;
545 c
->sink_input
->userdata
= c
;
547 pa_sink_input_set_requested_latency(c
->sink_input
, DEFAULT_SINK_LATENCY
);
549 l
= (size_t) ((double) pa_bytes_per_second(&o
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
550 c
->input_memblockq
= pa_memblockq_new(
554 pa_frame_size(&o
->sample_spec
),
556 l
/PLAYBACK_BUFFER_FRAGMENTS
,
559 pa_iochannel_socket_set_rcvbuf(io
, l
);
561 pa_atomic_store(&c
->playback
.missing
, (int) pa_memblockq_missing(c
->input_memblockq
));
563 pa_sink_input_put(c
->sink_input
);
567 pa_source_output_new_data data
;
571 if (!(source
= pa_namereg_get(c
->protocol
->core
, o
->default_source
, PA_NAMEREG_SOURCE
, TRUE
))) {
572 pa_log("Failed to get source.");
576 pa_source_output_new_data_init(&data
);
577 data
.driver
= __FILE__
;
578 data
.module
= o
->module
;
579 data
.client
= c
->client
;
580 data
.source
= source
;
581 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
582 pa_source_output_new_data_set_sample_spec(&data
, &o
->sample_spec
);
584 c
->source_output
= pa_source_output_new(p
->core
, &data
, 0);
585 pa_source_output_new_data_done(&data
);
587 if (!c
->source_output
) {
588 pa_log("Failed to create source output.");
591 c
->source_output
->push
= source_output_push_cb
;
592 c
->source_output
->kill
= source_output_kill_cb
;
593 c
->source_output
->get_latency
= source_output_get_latency_cb
;
594 c
->source_output
->userdata
= c
;
596 pa_source_output_set_requested_latency(c
->source_output
, DEFAULT_SOURCE_LATENCY
);
598 l
= (size_t) (pa_bytes_per_second(&o
->sample_spec
)*RECORD_BUFFER_SECONDS
);
599 c
->output_memblockq
= pa_memblockq_new(
603 pa_frame_size(&o
->sample_spec
),
608 pa_iochannel_socket_set_sndbuf(io
, l
);
610 pa_source_output_put(c
->source_output
);
613 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
614 pa_idxset_put(p
->connections
, c
, NULL
);
620 connection_unlink(c
);
623 void pa_simple_protocol_disconnect(pa_simple_protocol
*p
, pa_module
*m
) {
630 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
631 if (c
->options
->module
== m
)
632 connection_unlink(c
);
635 static pa_simple_protocol
* simple_protocol_new(pa_core
*c
) {
636 pa_simple_protocol
*p
;
640 p
= pa_xnew(pa_simple_protocol
, 1);
643 p
->connections
= pa_idxset_new(NULL
, NULL
);
645 pa_assert_se(pa_shared_set(c
, "simple-protocol", p
) >= 0);
650 pa_simple_protocol
* pa_simple_protocol_get(pa_core
*c
) {
651 pa_simple_protocol
*p
;
653 if ((p
= pa_shared_get(c
, "simple-protocol")))
654 return pa_simple_protocol_ref(p
);
656 return simple_protocol_new(c
);
659 pa_simple_protocol
* pa_simple_protocol_ref(pa_simple_protocol
*p
) {
661 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
668 void pa_simple_protocol_unref(pa_simple_protocol
*p
) {
671 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
673 if (PA_REFCNT_DEC(p
) > 0)
676 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
677 connection_unlink(c
);
679 pa_idxset_free(p
->connections
, NULL
, NULL
);
681 pa_assert_se(pa_shared_remove(p
->core
, "simple-protocol") >= 0);
686 pa_simple_options
* pa_simple_options_new(void) {
687 pa_simple_options
*o
;
689 o
= pa_xnew0(pa_simple_options
, 1);
695 pa_simple_options
* pa_simple_options_ref(pa_simple_options
*o
) {
697 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
704 void pa_simple_options_unref(pa_simple_options
*o
) {
706 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
708 if (PA_REFCNT_DEC(o
) > 0)
711 pa_xfree(o
->default_sink
);
712 pa_xfree(o
->default_source
);
717 int pa_simple_options_parse(pa_simple_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
721 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
724 o
->sample_spec
= c
->default_sample_spec
;
725 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &o
->sample_spec
, &o
->channel_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
726 pa_log("Failed to parse sample type specification.");
730 pa_xfree(o
->default_source
);
731 o
->default_source
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
733 pa_xfree(o
->default_sink
);
734 o
->default_sink
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
737 if (pa_modargs_get_value_boolean(ma
, "record", &enabled
) < 0) {
738 pa_log("record= expects a boolean argument.");
744 if (pa_modargs_get_value_boolean(ma
, "playback", &enabled
) < 0) {
745 pa_log("playback= expects a boolean argument.");
748 o
->playback
= enabled
;
750 if (!o
->playback
&& !o
->record
) {
751 pa_log("neither playback nor recording enabled for protocol.");