2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/sample.h>
34 #include <pulse/timeval.h>
35 #include <pulse/utf8.h>
36 #include <pulse/xmalloc.h>
37 #include <pulse/proplist.h>
39 #include <pulsecore/esound.h>
40 #include <pulsecore/memblock.h>
41 #include <pulsecore/client.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/source-output.h>
45 #include <pulsecore/source.h>
46 #include <pulsecore/core-scache.h>
47 #include <pulsecore/sample-util.h>
48 #include <pulsecore/authkey.h>
49 #include <pulsecore/namereg.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/core-error.h>
53 #include <pulsecore/ipacl.h>
54 #include <pulsecore/macro.h>
55 #include <pulsecore/thread-mq.h>
56 #include <pulsecore/shared.h>
58 #include "endianmacros.h"
60 #include "protocol-esound.h"
62 /* Don't accept more connection than this */
63 #define MAX_CONNECTIONS 64
65 /* Kick a client if it doesn't authenticate within this time */
66 #define AUTH_TIMEOUT 5
68 #define DEFAULT_COOKIE_FILE ".esd_auth"
70 #define PLAYBACK_BUFFER_SECONDS (.25)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
74 #define MAX_CACHE_SAMPLE_SIZE (2048000)
76 #define DEFAULT_SINK_LATENCY (150*PA_USEC_PER_MSEC)
77 #define DEFAULT_SOURCE_LATENCY (150*PA_USEC_PER_MSEC)
79 #define SCACHE_PREFIX "esound."
81 /* This is heavily based on esound's code */
83 typedef struct connection
{
88 pa_esound_protocol
*protocol
;
89 pa_esound_options
*options
;
92 pa_bool_t authorized
, swap_byte_order
;
94 size_t write_data_alloc
, write_data_index
, write_data_length
;
96 size_t read_data_alloc
, read_data_length
;
98 esd_client_state_t state
;
99 pa_sink_input
*sink_input
;
100 pa_source_output
*source_output
;
101 pa_memblockq
*input_memblockq
, *output_memblockq
;
102 pa_defer_event
*defer_event
;
107 pa_memblock
*current_memblock
;
108 size_t memblock_index
;
114 pa_memchunk memchunk
;
116 pa_sample_spec sample_spec
;
119 pa_time_event
*auth_timeout_event
;
122 PA_DECLARE_CLASS(connection
);
123 #define CONNECTION(o) (connection_cast(o))
124 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
126 struct pa_esound_protocol
{
130 pa_idxset
*connections
;
135 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
136 SINK_INPUT_MESSAGE_DISABLE_PREBUF
140 CONNECTION_MESSAGE_REQUEST_DATA
,
141 CONNECTION_MESSAGE_POST_DATA
,
142 CONNECTION_MESSAGE_UNLINK_CONNECTION
145 typedef struct proto_handler
{
147 int (*proc
)(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
148 const char *description
;
149 } esd_proto_handler_info_t
;
151 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
152 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
153 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
154 static void sink_input_kill_cb(pa_sink_input
*i
);
155 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
156 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
158 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
159 static void source_output_kill_cb(pa_source_output
*o
);
161 static int esd_proto_connect(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
162 static int esd_proto_stream_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
163 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
164 static int esd_proto_get_latency(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
165 static int esd_proto_server_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
166 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
167 static int esd_proto_stream_pan(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
168 static int esd_proto_sample_cache(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
169 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
170 static int esd_proto_sample_get_id(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
171 static int esd_proto_standby_or_resume(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
173 /* the big map of protocol handler info */
174 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
175 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
176 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
177 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
179 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
180 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
181 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
183 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
184 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
185 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
186 { sizeof(int), NULL
, "sample loop" },
187 { sizeof(int), NULL
, "sample stop" },
188 { (size_t) -1, NULL
, "TODO: sample kill" },
190 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
191 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
193 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
194 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
196 { sizeof(int), esd_proto_server_info
, "server info" },
197 { sizeof(int), esd_proto_all_info
, "all info" },
198 { (size_t) -1, NULL
, "TODO: subscribe" },
199 { (size_t) -1, NULL
, "TODO: unsubscribe" },
201 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
202 { 3 * sizeof(int), NULL
, "sample pan" },
204 { sizeof(int), NULL
, "standby mode" },
205 { 0, esd_proto_get_latency
, "get latency" }
208 static void connection_unlink(connection
*c
) {
215 pa_esound_options_unref(c
->options
);
220 pa_sink_input_unlink(c
->sink_input
);
221 pa_sink_input_unref(c
->sink_input
);
222 c
->sink_input
= NULL
;
225 if (c
->source_output
) {
226 pa_source_output_unlink(c
->source_output
);
227 pa_source_output_unref(c
->source_output
);
228 c
->source_output
= NULL
;
232 pa_client_free(c
->client
);
236 if (c
->state
== ESD_STREAMING_DATA
)
237 c
->protocol
->n_player
--;
240 pa_iochannel_free(c
->io
);
244 if (c
->defer_event
) {
245 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
246 c
->defer_event
= NULL
;
249 if (c
->auth_timeout_event
) {
250 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
251 c
->auth_timeout_event
= NULL
;
254 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
259 static void connection_free(pa_object
*obj
) {
260 connection
*c
= CONNECTION(obj
);
263 if (c
->input_memblockq
)
264 pa_memblockq_free(c
->input_memblockq
);
265 if (c
->output_memblockq
)
266 pa_memblockq_free(c
->output_memblockq
);
268 if (c
->playback
.current_memblock
)
269 pa_memblock_unref(c
->playback
.current_memblock
);
271 pa_xfree(c
->read_data
);
272 pa_xfree(c
->write_data
);
274 if (c
->scache
.memchunk
.memblock
)
275 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
276 pa_xfree(c
->scache
.name
);
278 pa_xfree(c
->original_name
);
282 static void connection_write_prepare(connection
*c
, size_t length
) {
286 t
= c
->write_data_length
+length
;
288 if (c
->write_data_alloc
< t
)
289 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
291 pa_assert(c
->write_data
);
294 static void connection_write(connection
*c
, const void *data
, size_t length
) {
298 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
300 connection_write_prepare(c
, length
);
302 pa_assert(c
->write_data
);
304 i
= c
->write_data_length
;
305 c
->write_data_length
+= length
;
307 memcpy((uint8_t*) c
->write_data
+ i
, data
, length
);
310 static void format_esd2native(int format
, pa_bool_t swap_bytes
, pa_sample_spec
*ss
) {
313 ss
->channels
= (uint8_t) (((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1);
314 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
315 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
317 ss
->format
= PA_SAMPLE_U8
;
320 static int format_native2esd(pa_sample_spec
*ss
) {
323 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
324 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
329 #define CHECK_VALIDITY(expression, ...) do { \
330 if (!(expression)) { \
331 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
336 /*** esound commands ***/
338 static int esd_proto_connect(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
342 connection_assert_ref(c
);
344 pa_assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
346 if (!c
->authorized
&& c
->options
->auth_cookie
) {
349 if ((key
= pa_auth_cookie_read(c
->options
->auth_cookie
, ESD_KEY_LEN
)))
350 if (memcmp(data
, key
, ESD_KEY_LEN
) == 0)
351 c
->authorized
= TRUE
;
354 if (!c
->authorized
) {
355 pa_log("Kicked client with invalid authorization key.");
359 if (c
->auth_timeout_event
) {
360 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
361 c
->auth_timeout_event
= NULL
;
364 data
= (const char*)data
+ ESD_KEY_LEN
;
366 memcpy(&ekey
, data
, sizeof(uint32_t));
367 if (ekey
== ESD_ENDIAN_KEY
)
368 c
->swap_byte_order
= FALSE
;
369 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
370 c
->swap_byte_order
= TRUE
;
372 pa_log_warn("Client sent invalid endian key");
376 pa_proplist_sets(c
->client
->proplist
, "esound.byte_order", c
->swap_byte_order
? "reverse" : "native");
379 connection_write(c
, &ok
, sizeof(int));
383 static int esd_proto_stream_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
384 char name
[ESD_NAME_MAX
], *utf8_name
;
385 int32_t format
, rate
;
388 pa_sink
*sink
= NULL
;
389 pa_sink_input_new_data sdata
;
391 connection_assert_ref(c
);
393 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
395 memcpy(&format
, data
, sizeof(int32_t));
396 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
397 data
= (const char*) data
+ sizeof(int32_t);
399 memcpy(&rate
, data
, sizeof(int32_t));
400 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
401 data
= (const char*) data
+ sizeof(int32_t);
403 ss
.rate
= (uint32_t) rate
;
404 format_esd2native(format
, c
->swap_byte_order
, &ss
);
406 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
408 if (c
->options
->default_sink
) {
409 sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
);
410 CHECK_VALIDITY(sink
, "No such sink: %s", pa_strnull(c
->options
->default_sink
));
413 pa_strlcpy(name
, data
, sizeof(name
));
415 utf8_name
= pa_utf8_filter(name
);
416 pa_client_set_name(c
->client
, utf8_name
);
419 c
->original_name
= pa_xstrdup(name
);
421 pa_assert(!c
->sink_input
&& !c
->input_memblockq
);
423 pa_sink_input_new_data_init(&sdata
);
424 sdata
.driver
= __FILE__
;
425 sdata
.module
= c
->options
->module
;
426 sdata
.client
= c
->client
;
428 pa_sink_input_new_data_set_sample_spec(&sdata
, &ss
);
430 pa_sink_input_new(&c
->sink_input
, c
->protocol
->core
, &sdata
, 0);
431 pa_sink_input_new_data_done(&sdata
);
433 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
435 l
= (size_t) ((double) pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
436 c
->input_memblockq
= pa_memblockq_new(
442 l
/PLAYBACK_BUFFER_FRAGMENTS
,
445 pa_iochannel_socket_set_rcvbuf(c
->io
, l
);
447 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
448 c
->sink_input
->pop
= sink_input_pop_cb
;
449 c
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
450 c
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
451 c
->sink_input
->kill
= sink_input_kill_cb
;
452 c
->sink_input
->userdata
= c
;
454 pa_sink_input_set_requested_latency(c
->sink_input
, DEFAULT_SINK_LATENCY
);
456 c
->state
= ESD_STREAMING_DATA
;
458 c
->protocol
->n_player
++;
460 pa_atomic_store(&c
->playback
.missing
, (int) pa_memblockq_missing(c
->input_memblockq
));
462 pa_sink_input_put(c
->sink_input
);
467 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
468 char name
[ESD_NAME_MAX
], *utf8_name
;
469 int32_t format
, rate
;
470 pa_source
*source
= NULL
;
473 pa_source_output_new_data sdata
;
475 connection_assert_ref(c
);
477 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
479 memcpy(&format
, data
, sizeof(int32_t));
480 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
481 data
= (const char*) data
+ sizeof(int32_t);
483 memcpy(&rate
, data
, sizeof(int32_t));
484 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
485 data
= (const char*) data
+ sizeof(int32_t);
487 ss
.rate
= (uint32_t) rate
;
488 format_esd2native(format
, c
->swap_byte_order
, &ss
);
490 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
492 if (request
== ESD_PROTO_STREAM_MON
) {
495 sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
);
496 CHECK_VALIDITY(sink
, "No such sink: %s", pa_strnull(c
->options
->default_sink
));
498 source
= sink
->monitor_source
;
499 CHECK_VALIDITY(source
, "No such source.");
501 pa_assert(request
== ESD_PROTO_STREAM_REC
);
503 if (c
->options
->default_source
) {
504 source
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_source
, PA_NAMEREG_SOURCE
);
505 CHECK_VALIDITY(source
, "No such source: %s", pa_strnull(c
->options
->default_source
));
509 pa_strlcpy(name
, data
, sizeof(name
));
511 utf8_name
= pa_utf8_filter(name
);
512 pa_client_set_name(c
->client
, utf8_name
);
515 c
->original_name
= pa_xstrdup(name
);
517 pa_assert(!c
->output_memblockq
&& !c
->source_output
);
519 pa_source_output_new_data_init(&sdata
);
520 sdata
.driver
= __FILE__
;
521 sdata
.module
= c
->options
->module
;
522 sdata
.client
= c
->client
;
523 sdata
.source
= source
;
524 pa_source_output_new_data_set_sample_spec(&sdata
, &ss
);
526 pa_source_output_new(&c
->source_output
, c
->protocol
->core
, &sdata
, 0);
527 pa_source_output_new_data_done(&sdata
);
529 CHECK_VALIDITY(c
->source_output
, "Failed to create source output.");
531 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
532 c
->output_memblockq
= pa_memblockq_new(
541 pa_iochannel_socket_set_sndbuf(c
->io
, l
);
543 c
->source_output
->push
= source_output_push_cb
;
544 c
->source_output
->kill
= source_output_kill_cb
;
545 c
->source_output
->get_latency
= source_output_get_latency_cb
;
546 c
->source_output
->userdata
= c
;
548 pa_source_output_set_requested_latency(c
->source_output
, DEFAULT_SOURCE_LATENCY
);
550 c
->state
= ESD_STREAMING_DATA
;
552 c
->protocol
->n_player
++;
554 pa_source_output_put(c
->source_output
);
559 static int esd_proto_get_latency(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
565 pa_assert(length
== 0);
567 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
)))
570 double usec
= (double) pa_sink_get_latency(sink
);
571 latency
= (int) ((usec
*44100)/1000000);
574 latency
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
575 connection_write(c
, &latency
, sizeof(int32_t));
579 static int esd_proto_server_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
580 int32_t rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
586 pa_assert(length
== sizeof(int32_t));
588 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
))) {
589 rate
= (int32_t) sink
->sample_spec
.rate
;
590 format
= format_native2esd(&sink
->sample_spec
);
593 connection_write_prepare(c
, sizeof(int32_t) * 3);
596 connection_write(c
, &response
, sizeof(int32_t));
597 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
598 connection_write(c
, &rate
, sizeof(int32_t));
599 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
600 connection_write(c
, &format
, sizeof(int32_t));
605 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
608 uint32_t idx
= PA_IDXSET_INVALID
;
610 char terminator
[sizeof(int32_t)*6+ESD_NAME_MAX
];
614 pa_assert(length
== sizeof(int32_t));
616 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
619 k
= sizeof(int32_t)*5+ESD_NAME_MAX
;
620 s
= sizeof(int32_t)*6+ESD_NAME_MAX
;
621 nsamples
= pa_idxset_size(c
->protocol
->core
->scache
);
622 t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1);
624 connection_write_prepare(c
, t
);
626 memset(terminator
, 0, sizeof(terminator
));
628 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
629 int32_t id
, format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
630 char name
[ESD_NAME_MAX
];
632 if (conn
->state
!= ESD_STREAMING_DATA
)
635 pa_assert(t
>= k
*2+s
);
637 if (conn
->sink_input
) {
638 pa_cvolume volume
= *pa_sink_input_get_volume(conn
->sink_input
);
639 rate
= (int32_t) conn
->sink_input
->sample_spec
.rate
;
640 lvolume
= (int32_t) ((volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
641 rvolume
= (int32_t) ((volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
642 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
646 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) (conn
->index
+1));
647 connection_write(c
, &id
, sizeof(int32_t));
650 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
651 if (conn
->original_name
)
652 strncpy(name
, conn
->original_name
, ESD_NAME_MAX
);
653 else if (conn
->client
&& pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
))
654 strncpy(name
, pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
), ESD_NAME_MAX
);
655 connection_write(c
, name
, ESD_NAME_MAX
);
658 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
659 connection_write(c
, &rate
, sizeof(int32_t));
662 lvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
663 connection_write(c
, &lvolume
, sizeof(int32_t));
666 rvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
667 connection_write(c
, &rvolume
, sizeof(int32_t));
670 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
671 connection_write(c
, &format
, sizeof(int32_t));
676 pa_assert(t
== s
*(nsamples
+1)+k
);
679 connection_write(c
, terminator
, k
);
684 idx
= PA_IDXSET_INVALID
;
685 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
686 int32_t id
, rate
, lvolume
, rvolume
, format
, len
;
687 char name
[ESD_NAME_MAX
];
692 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
693 connection_write(c
, &id
, sizeof(int32_t));
696 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
697 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
698 strncpy(name
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
700 pa_snprintf(name
, ESD_NAME_MAX
, "native.%s", ce
->name
);
701 connection_write(c
, name
, ESD_NAME_MAX
);
704 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ce
->sample_spec
.rate
);
705 connection_write(c
, &rate
, sizeof(int32_t));
708 lvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ((ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
));
709 connection_write(c
, &lvolume
, sizeof(int32_t));
712 rvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ((ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
));
713 connection_write(c
, &rvolume
, sizeof(int32_t));
716 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
717 connection_write(c
, &format
, sizeof(int32_t));
720 len
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
721 connection_write(c
, &len
, sizeof(int32_t));
729 connection_write(c
, terminator
, s
);
734 static int esd_proto_stream_pan(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
736 uint32_t idx
, lvolume
, rvolume
;
739 connection_assert_ref(c
);
741 pa_assert(length
== sizeof(int32_t)*3);
743 memcpy(&idx
, data
, sizeof(uint32_t));
744 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
745 data
= (const char*)data
+ sizeof(uint32_t);
747 memcpy(&lvolume
, data
, sizeof(uint32_t));
748 lvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, lvolume
);
749 data
= (const char*)data
+ sizeof(uint32_t);
751 memcpy(&rvolume
, data
, sizeof(uint32_t));
752 rvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, rvolume
);
753 data
= (const char*)data
+ sizeof(uint32_t);
755 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
)) && conn
->sink_input
) {
757 volume
.values
[0] = (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
758 volume
.values
[1] = (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
760 pa_sink_input_set_volume(conn
->sink_input
, &volume
, TRUE
);
765 connection_write(c
, &ok
, sizeof(int32_t));
770 static int esd_proto_sample_cache(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
772 int32_t format
, rate
, sc_length
;
774 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
776 connection_assert_ref(c
);
778 pa_assert(length
== (ESD_NAME_MAX
+3*sizeof(int32_t)));
780 memcpy(&format
, data
, sizeof(int32_t));
781 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
782 data
= (const char*)data
+ sizeof(int32_t);
784 memcpy(&rate
, data
, sizeof(int32_t));
785 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
786 data
= (const char*)data
+ sizeof(int32_t);
788 ss
.rate
= (uint32_t) rate
;
789 format_esd2native(format
, c
->swap_byte_order
, &ss
);
791 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
793 memcpy(&sc_length
, data
, sizeof(int32_t));
794 sc_length
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, sc_length
);
795 data
= (const char*)data
+ sizeof(int32_t);
797 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large (%d bytes).", (int)sc_length
);
799 strcpy(name
, SCACHE_PREFIX
);
800 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
802 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
804 pa_assert(!c
->scache
.memchunk
.memblock
);
805 c
->scache
.memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, (size_t) sc_length
);
806 c
->scache
.memchunk
.index
= 0;
807 c
->scache
.memchunk
.length
= (size_t) sc_length
;
808 c
->scache
.sample_spec
= ss
;
809 pa_assert(!c
->scache
.name
);
810 c
->scache
.name
= pa_xstrdup(name
);
812 c
->state
= ESD_CACHING_SAMPLE
;
814 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, c
->client
->proplist
, &idx
);
817 connection_write(c
, &idx
, sizeof(uint32_t));
822 static int esd_proto_sample_get_id(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
825 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
827 connection_assert_ref(c
);
829 pa_assert(length
== ESD_NAME_MAX
);
831 strcpy(name
, SCACHE_PREFIX
);
832 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
834 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
837 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
838 ok
= (int32_t) idx
+ 1;
840 connection_write(c
, &ok
, sizeof(int32_t));
845 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
850 connection_assert_ref(c
);
852 pa_assert(length
== sizeof(int32_t));
854 memcpy(&idx
, data
, sizeof(uint32_t));
855 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
859 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
860 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
863 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
)))
864 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
, c
->client
->proplist
, NULL
) >= 0)
865 ok
= (int32_t) idx
+ 1;
867 pa_assert(request
== ESD_PROTO_SAMPLE_FREE
);
869 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
870 ok
= (int32_t) idx
+ 1;
874 connection_write(c
, &ok
, sizeof(int32_t));
879 static int esd_proto_standby_or_resume(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
882 connection_assert_ref(c
);
884 connection_write_prepare(c
, sizeof(int32_t) * 2);
887 connection_write(c
, &ok
, sizeof(int32_t));
888 connection_write(c
, &ok
, sizeof(int32_t));
893 /*** client callbacks ***/
895 static void client_kill_cb(pa_client
*c
) {
898 connection_unlink(CONNECTION(c
->userdata
));
901 /*** pa_iochannel callbacks ***/
903 static int do_read(connection
*c
) {
904 connection_assert_ref(c
);
906 /* pa_log("READ"); */
908 if (c
->state
== ESD_NEXT_REQUEST
) {
910 pa_assert(c
->read_data_length
< sizeof(c
->request
));
912 if ((r
= pa_iochannel_read(c
->io
,
913 ((uint8_t*) &c
->request
) + c
->read_data_length
,
914 sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
916 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
919 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
923 c
->read_data_length
+= (size_t) r
;
925 if (c
->read_data_length
>= sizeof(c
->request
)) {
926 struct proto_handler
*handler
;
928 c
->request
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
930 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
>= ESD_PROTO_MAX
) {
931 pa_log("recieved invalid request.");
935 handler
= proto_map
+c
->request
;
937 /* pa_log("executing request #%u", c->request); */
939 if (!handler
->proc
) {
940 pa_log("recieved unimplemented request #%u.", c
->request
);
944 if (handler
->data_length
== 0) {
945 c
->read_data_length
= 0;
947 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
951 if (c
->read_data_alloc
< handler
->data_length
)
952 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
953 pa_assert(c
->read_data
);
955 c
->state
= ESD_NEEDS_REQDATA
;
956 c
->read_data_length
= 0;
960 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
962 struct proto_handler
*handler
= proto_map
+c
->request
;
964 pa_assert(handler
->proc
);
966 pa_assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
968 if ((r
= pa_iochannel_read(c
->io
,
969 (uint8_t*) c
->read_data
+ c
->read_data_length
,
970 handler
->data_length
- c
->read_data_length
)) <= 0) {
972 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
975 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
979 c
->read_data_length
+= (size_t) r
;
980 if (c
->read_data_length
>= handler
->data_length
) {
981 size_t l
= c
->read_data_length
;
982 pa_assert(handler
->proc
);
984 c
->state
= ESD_NEXT_REQUEST
;
985 c
->read_data_length
= 0;
987 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
990 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
994 pa_assert(c
->scache
.memchunk
.memblock
);
995 pa_assert(c
->scache
.name
);
996 pa_assert(c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
998 p
= pa_memblock_acquire(c
->scache
.memchunk
.memblock
);
999 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->scache
.memchunk
.index
, c
->scache
.memchunk
.length
-c
->scache
.memchunk
.index
);
1000 pa_memblock_release(c
->scache
.memchunk
.memblock
);
1003 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1006 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
1010 c
->scache
.memchunk
.index
+= (size_t) r
;
1011 pa_assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
1013 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
1016 c
->scache
.memchunk
.index
= 0;
1017 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, c
->client
->proplist
, &idx
);
1019 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
1020 c
->scache
.memchunk
.memblock
= NULL
;
1021 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
1023 pa_xfree(c
->scache
.name
);
1024 c
->scache
.name
= NULL
;
1026 c
->state
= ESD_NEXT_REQUEST
;
1029 connection_write(c
, &idx
, sizeof(uint32_t));
1032 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1039 pa_assert(c
->input_memblockq
);
1041 /* pa_log("STREAMING_DATA"); */
1043 if (!(l
= (size_t) pa_atomic_load(&c
->playback
.missing
)))
1046 if (c
->playback
.current_memblock
) {
1048 space
= pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
;
1051 pa_memblock_unref(c
->playback
.current_memblock
);
1052 c
->playback
.current_memblock
= NULL
;
1056 if (!c
->playback
.current_memblock
) {
1057 pa_assert_se(c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, (size_t) -1));
1058 c
->playback
.memblock_index
= 0;
1060 space
= pa_memblock_get_length(c
->playback
.current_memblock
);
1066 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
1067 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->playback
.memblock_index
, l
);
1068 pa_memblock_release(c
->playback
.current_memblock
);
1072 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1075 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
1079 chunk
.memblock
= c
->playback
.current_memblock
;
1080 chunk
.index
= c
->playback
.memblock_index
;
1081 chunk
.length
= (size_t) r
;
1083 c
->playback
.memblock_index
+= (size_t) r
;
1085 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, &chunk
, NULL
);
1086 pa_atomic_sub(&c
->playback
.missing
, (int) r
);
1092 static int do_write(connection
*c
) {
1093 connection_assert_ref(c
);
1095 /* pa_log("WRITE"); */
1097 if (c
->write_data_length
) {
1100 pa_assert(c
->write_data_index
< c
->write_data_length
);
1101 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) c
->write_data
+c
->write_data_index
, c
->write_data_length
-c
->write_data_index
)) < 0) {
1103 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1106 pa_log("write(): %s", pa_cstrerror(errno
));
1110 c
->write_data_index
+= (size_t) r
;
1111 if (c
->write_data_index
>= c
->write_data_length
)
1112 c
->write_data_length
= c
->write_data_index
= 0;
1114 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
1119 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
1122 pa_assert(chunk
.memblock
);
1123 pa_assert(chunk
.length
);
1125 p
= pa_memblock_acquire(chunk
.memblock
);
1126 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
1127 pa_memblock_release(chunk
.memblock
);
1129 pa_memblock_unref(chunk
.memblock
);
1133 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1136 pa_log("write(): %s", pa_cstrerror(errno
));
1140 pa_memblockq_drop(c
->output_memblockq
, (size_t) r
);
1146 static void do_work(connection
*c
) {
1147 connection_assert_ref(c
);
1149 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1154 if (pa_iochannel_is_readable(c
->io
))
1158 if (c
->state
== ESD_STREAMING_DATA
&& !c
->sink_input
&& pa_iochannel_is_hungup(c
->io
))
1159 /* In case we are in capture mode we will never call read()
1160 * on the socket, hence we need to detect the hangup manually
1161 * here, instead of simply waiting for read() to return 0. */
1164 if (pa_iochannel_is_writable(c
->io
))
1165 if (do_write(c
) < 0)
1172 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1175 pa_iochannel_free(c
->io
);
1178 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_DISABLE_PREBUF
, NULL
, 0, NULL
, NULL
);
1180 connection_unlink(c
);
1183 static void io_callback(pa_iochannel
*io
, void *userdata
) {
1184 connection
*c
= CONNECTION(userdata
);
1186 connection_assert_ref(c
);
1192 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
1193 connection
*c
= CONNECTION(userdata
);
1195 connection_assert_ref(c
);
1201 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1202 connection
*c
= CONNECTION(o
);
1203 connection_assert_ref(c
);
1206 case CONNECTION_MESSAGE_REQUEST_DATA
:
1210 case CONNECTION_MESSAGE_POST_DATA
:
1211 /* pa_log("got data %u", chunk->length); */
1212 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
1216 case CONNECTION_MESSAGE_UNLINK_CONNECTION
:
1217 connection_unlink(c
);
1224 /*** sink_input callbacks ***/
1226 /* Called from thread context */
1227 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1228 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1231 pa_sink_input_assert_ref(i
);
1232 c
= CONNECTION(i
->userdata
);
1233 connection_assert_ref(c
);
1237 case SINK_INPUT_MESSAGE_POST_DATA
: {
1240 /* New data from the main loop */
1241 pa_memblockq_push_align(c
->input_memblockq
, chunk
);
1243 if (pa_memblockq_is_readable(c
->input_memblockq
) && c
->playback
.underrun
) {
1244 pa_log_debug("Requesting rewind due to end of underrun.");
1245 pa_sink_input_request_rewind(c
->sink_input
, 0, FALSE
, TRUE
, FALSE
);
1248 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1253 case SINK_INPUT_MESSAGE_DISABLE_PREBUF
:
1254 pa_memblockq_prebuf_disable(c
->input_memblockq
);
1257 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1258 pa_usec_t
*r
= userdata
;
1260 *r
= pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1262 /* Fall through, the default handler will add in the extra
1263 * latency added by the resampler */
1267 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1271 /* Called from thread context */
1272 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
1275 pa_sink_input_assert_ref(i
);
1276 c
= CONNECTION(i
->userdata
);
1277 connection_assert_ref(c
);
1280 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1282 c
->playback
.underrun
= TRUE
;
1284 if (c
->dead
&& pa_sink_input_safe_to_remove(i
))
1285 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_UNLINK_CONNECTION
, NULL
, 0, NULL
, NULL
);
1291 chunk
->length
= PA_MIN(length
, chunk
->length
);
1293 c
->playback
.underrun
= FALSE
;
1295 pa_memblockq_drop(c
->input_memblockq
, chunk
->length
);
1296 m
= pa_memblockq_pop_missing(c
->input_memblockq
);
1299 if (pa_atomic_add(&c
->playback
.missing
, (int) m
) <= 0)
1300 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1306 /* Called from thread context */
1307 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1310 pa_sink_input_assert_ref(i
);
1311 c
= CONNECTION(i
->userdata
);
1312 connection_assert_ref(c
);
1314 /* If we are in an underrun, then we don't rewind */
1315 if (i
->thread_info
.underrun_for
> 0)
1318 pa_memblockq_rewind(c
->input_memblockq
, nbytes
);
1321 /* Called from thread context */
1322 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1325 pa_sink_input_assert_ref(i
);
1326 c
= CONNECTION(i
->userdata
);
1327 connection_assert_ref(c
);
1329 pa_memblockq_set_maxrewind(c
->input_memblockq
, nbytes
);
1332 static void sink_input_kill_cb(pa_sink_input
*i
) {
1333 pa_sink_input_assert_ref(i
);
1335 connection_unlink(CONNECTION(i
->userdata
));
1338 /*** source_output callbacks ***/
1340 /* Called from thread context */
1341 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1344 pa_source_output_assert_ref(o
);
1345 c
= CONNECTION(o
->userdata
);
1349 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1352 static void source_output_kill_cb(pa_source_output
*o
) {
1353 pa_source_output_assert_ref(o
);
1355 connection_unlink(CONNECTION(o
->userdata
));
1358 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1361 pa_source_output_assert_ref(o
);
1362 c
= CONNECTION(o
->userdata
);
1365 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1368 /*** entry points ***/
1370 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1371 connection
*c
= CONNECTION(userdata
);
1375 connection_assert_ref(c
);
1376 pa_assert(c
->auth_timeout_event
== e
);
1379 connection_unlink(c
);
1382 void pa_esound_protocol_connect(pa_esound_protocol
*p
, pa_iochannel
*io
, pa_esound_options
*o
) {
1385 pa_client_new_data data
;
1392 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1393 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1394 pa_iochannel_free(io
);
1398 pa_client_new_data_init(&data
);
1399 data
.module
= o
->module
;
1400 data
.driver
= __FILE__
;
1401 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
1402 pa_proplist_setf(data
.proplist
, PA_PROP_APPLICATION_NAME
, "EsounD client (%s)", pname
);
1403 pa_proplist_sets(data
.proplist
, "esound-protocol.peer", pname
);
1404 client
= pa_client_new(p
->core
, &data
);
1405 pa_client_new_data_done(&data
);
1410 c
= pa_msgobject_new(connection
);
1411 c
->parent
.parent
.free
= connection_free
;
1412 c
->parent
.process_msg
= connection_process_msg
;
1415 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1418 c
->client
->kill
= client_kill_cb
;
1419 c
->client
->userdata
= c
;
1421 c
->options
= pa_esound_options_ref(o
);
1422 c
->authorized
= FALSE
;
1423 c
->swap_byte_order
= FALSE
;
1426 c
->read_data_length
= 0;
1427 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1429 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1430 c
->write_data
= NULL
;
1432 c
->state
= ESD_NEEDS_REQDATA
;
1433 c
->request
= ESD_PROTO_CONNECT
;
1435 c
->sink_input
= NULL
;
1436 c
->input_memblockq
= NULL
;
1438 c
->source_output
= NULL
;
1439 c
->output_memblockq
= NULL
;
1441 c
->playback
.current_memblock
= NULL
;
1442 c
->playback
.memblock_index
= 0;
1443 c
->playback
.underrun
= TRUE
;
1444 pa_atomic_store(&c
->playback
.missing
, 0);
1446 pa_memchunk_reset(&c
->scache
.memchunk
);
1447 c
->scache
.name
= NULL
;
1449 c
->original_name
= NULL
;
1451 if (o
->auth_anonymous
) {
1452 pa_log_info("Client authenticated anonymously.");
1453 c
->authorized
= TRUE
;
1456 if (!c
->authorized
&&
1458 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
1460 pa_log_info("Client authenticated by IP ACL.");
1461 c
->authorized
= TRUE
;
1464 if (!c
->authorized
) {
1466 pa_gettimeofday(&tv
);
1467 tv
.tv_sec
+= AUTH_TIMEOUT
;
1468 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1470 c
->auth_timeout_event
= NULL
;
1472 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1473 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1475 pa_idxset_put(p
->connections
, c
, &c
->index
);
1478 void pa_esound_protocol_disconnect(pa_esound_protocol
*p
, pa_module
*m
) {
1485 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
1486 if (c
->options
->module
== m
)
1487 connection_unlink(c
);
1490 static pa_esound_protocol
* esound_protocol_new(pa_core
*c
) {
1491 pa_esound_protocol
*p
;
1495 p
= pa_xnew(pa_esound_protocol
, 1);
1498 p
->connections
= pa_idxset_new(NULL
, NULL
);
1501 pa_assert_se(pa_shared_set(c
, "esound-protocol", p
) >= 0);
1506 pa_esound_protocol
* pa_esound_protocol_get(pa_core
*c
) {
1507 pa_esound_protocol
*p
;
1509 if ((p
= pa_shared_get(c
, "esound-protocol")))
1510 return pa_esound_protocol_ref(p
);
1512 return esound_protocol_new(c
);
1515 pa_esound_protocol
* pa_esound_protocol_ref(pa_esound_protocol
*p
) {
1517 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
1524 void pa_esound_protocol_unref(pa_esound_protocol
*p
) {
1527 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
1529 if (PA_REFCNT_DEC(p
) > 0)
1532 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1533 connection_unlink(c
);
1535 pa_idxset_free(p
->connections
, NULL
, NULL
);
1537 pa_assert_se(pa_shared_remove(p
->core
, "esound-protocol") >= 0);
1542 pa_esound_options
* pa_esound_options_new(void) {
1543 pa_esound_options
*o
;
1545 o
= pa_xnew0(pa_esound_options
, 1);
1551 pa_esound_options
* pa_esound_options_ref(pa_esound_options
*o
) {
1553 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1560 void pa_esound_options_unref(pa_esound_options
*o
) {
1562 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1564 if (PA_REFCNT_DEC(o
) > 0)
1568 pa_ip_acl_free(o
->auth_ip_acl
);
1571 pa_auth_cookie_unref(o
->auth_cookie
);
1573 pa_xfree(o
->default_sink
);
1574 pa_xfree(o
->default_source
);
1579 int pa_esound_options_parse(pa_esound_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
1584 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1587 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
1588 pa_log("auth-anonymous= expects a boolean argument.");
1592 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
1595 if (!(ipa
= pa_ip_acl_new(acl
))) {
1596 pa_log("Failed to parse IP ACL '%s'", acl
);
1601 pa_ip_acl_free(o
->auth_ip_acl
);
1603 o
->auth_ip_acl
= ipa
;
1607 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
1608 pa_log("auth-cookie-enabled= expects a boolean argument.");
1613 pa_auth_cookie_unref(o
->auth_cookie
);
1618 /* The new name for this is 'auth-cookie', for compat reasons
1619 * we check the old name too */
1620 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
1621 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
1622 cn
= DEFAULT_COOKIE_FILE
;
1624 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, ESD_KEY_LEN
)))
1628 o
->auth_cookie
= NULL
;
1630 pa_xfree(o
->default_sink
);
1631 o
->default_sink
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1633 pa_xfree(o
->default_source
);
1634 o
->default_source
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));