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>
38 #include <pulsecore/esound.h>
39 #include <pulsecore/memblock.h>
40 #include <pulsecore/client.h>
41 #include <pulsecore/sink-input.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/source-output.h>
44 #include <pulsecore/source.h>
45 #include <pulsecore/core-scache.h>
46 #include <pulsecore/sample-util.h>
47 #include <pulsecore/authkey.h>
48 #include <pulsecore/namereg.h>
49 #include <pulsecore/log.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/core-error.h>
52 #include <pulsecore/ipacl.h>
53 #include <pulsecore/macro.h>
54 #include <pulsecore/thread-mq.h>
56 #include "endianmacros.h"
58 #include "protocol-esound.h"
60 /* Don't accept more connection than this */
61 #define MAX_CONNECTIONS 64
63 /* Kick a client if it doesn't authenticate within this time */
64 #define AUTH_TIMEOUT 5
66 #define DEFAULT_COOKIE_FILE ".esd_auth"
68 #define PLAYBACK_BUFFER_SECONDS (.25)
69 #define PLAYBACK_BUFFER_FRAGMENTS (10)
70 #define RECORD_BUFFER_SECONDS (5)
72 #define MAX_CACHE_SAMPLE_SIZE (2048000)
74 #define DEFAULT_SINK_LATENCY (150*PA_USEC_PER_MSEC)
75 #define DEFAULT_SOURCE_LATENCY (150*PA_USEC_PER_MSEC)
77 #define SCACHE_PREFIX "esound."
79 /* This is heavily based on esound's code */
81 typedef struct connection
{
86 pa_protocol_esound
*protocol
;
89 pa_bool_t authorized
, swap_byte_order
;
91 size_t write_data_alloc
, write_data_index
, write_data_length
;
93 size_t read_data_alloc
, read_data_length
;
95 esd_client_state_t state
;
96 pa_sink_input
*sink_input
;
97 pa_source_output
*source_output
;
98 pa_memblockq
*input_memblockq
, *output_memblockq
;
99 pa_defer_event
*defer_event
;
104 pa_memblock
*current_memblock
;
105 size_t memblock_index
;
111 pa_memchunk memchunk
;
113 pa_sample_spec sample_spec
;
116 pa_time_event
*auth_timeout_event
;
119 PA_DECLARE_CLASS(connection
);
120 #define CONNECTION(o) (connection_cast(o))
121 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
123 struct pa_protocol_esound
{
127 pa_socket_server
*server
;
128 pa_idxset
*connections
;
130 char *sink_name
, *source_name
;
132 uint8_t esd_key
[ESD_KEY_LEN
];
133 pa_ip_acl
*auth_ip_acl
;
137 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
138 SINK_INPUT_MESSAGE_DISABLE_PREBUF
142 CONNECTION_MESSAGE_REQUEST_DATA
,
143 CONNECTION_MESSAGE_POST_DATA
,
144 CONNECTION_MESSAGE_UNLINK_CONNECTION
147 typedef struct proto_handler
{
149 int (*proc
)(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
150 const char *description
;
151 } esd_proto_handler_info_t
;
153 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
154 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
155 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
156 static void sink_input_kill_cb(pa_sink_input
*i
);
157 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
158 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
160 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
161 static void source_output_kill_cb(pa_source_output
*o
);
163 static int esd_proto_connect(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
164 static int esd_proto_stream_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
165 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
166 static int esd_proto_get_latency(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
167 static int esd_proto_server_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
168 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
169 static int esd_proto_stream_pan(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
170 static int esd_proto_sample_cache(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
171 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
172 static int esd_proto_sample_get_id(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
173 static int esd_proto_standby_or_resume(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
175 /* the big map of protocol handler info */
176 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
177 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
178 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
179 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
181 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
182 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
183 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
185 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
186 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
187 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
188 { sizeof(int), NULL
, "sample loop" },
189 { sizeof(int), NULL
, "sample stop" },
190 { -1, NULL
, "TODO: sample kill" },
192 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
193 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
195 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
196 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
198 { sizeof(int), esd_proto_server_info
, "server info" },
199 { sizeof(int), esd_proto_all_info
, "all info" },
200 { -1, NULL
, "TODO: subscribe" },
201 { -1, NULL
, "TODO: unsubscribe" },
203 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
204 { 3 * sizeof(int), NULL
, "sample pan" },
206 { sizeof(int), NULL
, "standby mode" },
207 { 0, esd_proto_get_latency
, "get latency" }
210 static void connection_unlink(connection
*c
) {
217 pa_sink_input_unlink(c
->sink_input
);
218 pa_sink_input_unref(c
->sink_input
);
219 c
->sink_input
= NULL
;
222 if (c
->source_output
) {
223 pa_source_output_unlink(c
->source_output
);
224 pa_source_output_unref(c
->source_output
);
225 c
->source_output
= NULL
;
229 pa_client_free(c
->client
);
233 if (c
->state
== ESD_STREAMING_DATA
)
234 c
->protocol
->n_player
--;
237 pa_iochannel_free(c
->io
);
241 if (c
->defer_event
) {
242 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
243 c
->defer_event
= NULL
;
246 if (c
->auth_timeout_event
) {
247 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
248 c
->auth_timeout_event
= NULL
;
251 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
256 static void connection_free(pa_object
*obj
) {
257 connection
*c
= CONNECTION(obj
);
260 if (c
->input_memblockq
)
261 pa_memblockq_free(c
->input_memblockq
);
262 if (c
->output_memblockq
)
263 pa_memblockq_free(c
->output_memblockq
);
265 if (c
->playback
.current_memblock
)
266 pa_memblock_unref(c
->playback
.current_memblock
);
268 pa_xfree(c
->read_data
);
269 pa_xfree(c
->write_data
);
271 if (c
->scache
.memchunk
.memblock
)
272 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
273 pa_xfree(c
->scache
.name
);
275 pa_xfree(c
->original_name
);
279 static void connection_write_prepare(connection
*c
, size_t length
) {
283 t
= c
->write_data_length
+length
;
285 if (c
->write_data_alloc
< t
)
286 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
288 pa_assert(c
->write_data
);
291 static void connection_write(connection
*c
, const void *data
, size_t length
) {
295 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
297 connection_write_prepare(c
, length
);
299 pa_assert(c
->write_data
);
301 i
= c
->write_data_length
;
302 c
->write_data_length
+= length
;
304 memcpy((uint8_t*) c
->write_data
+ i
, data
, length
);
307 static void format_esd2native(int format
, pa_bool_t swap_bytes
, pa_sample_spec
*ss
) {
310 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
311 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
312 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
314 ss
->format
= PA_SAMPLE_U8
;
317 static int format_native2esd(pa_sample_spec
*ss
) {
320 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
321 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
326 #define CHECK_VALIDITY(expression, ...) do { \
327 if (!(expression)) { \
328 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
333 /*** esound commands ***/
335 static int esd_proto_connect(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
339 connection_assert_ref(c
);
341 pa_assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
343 if (!c
->authorized
) {
344 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
345 pa_log("kicked client with invalid authorization key.");
349 c
->authorized
= TRUE
;
350 if (c
->auth_timeout_event
) {
351 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
352 c
->auth_timeout_event
= NULL
;
356 data
= (const char*)data
+ ESD_KEY_LEN
;
358 memcpy(&ekey
, data
, sizeof(uint32_t));
359 if (ekey
== ESD_ENDIAN_KEY
)
360 c
->swap_byte_order
= FALSE
;
361 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
362 c
->swap_byte_order
= TRUE
;
364 pa_log_warn("Client sent invalid endian key");
369 connection_write(c
, &ok
, sizeof(int));
373 static int esd_proto_stream_play(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
374 char name
[ESD_NAME_MAX
], *utf8_name
;
375 int32_t format
, rate
;
378 pa_sink
*sink
= NULL
;
379 pa_sink_input_new_data sdata
;
381 connection_assert_ref(c
);
383 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
385 memcpy(&format
, data
, sizeof(int32_t));
386 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
387 data
= (const char*) data
+ sizeof(int32_t);
389 memcpy(&rate
, data
, sizeof(int32_t));
390 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
391 data
= (const char*) data
+ sizeof(int32_t);
394 format_esd2native(format
, c
->swap_byte_order
, &ss
);
396 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
398 if (c
->protocol
->sink_name
) {
399 sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1);
400 CHECK_VALIDITY(sink
, "No such sink: %s", c
->protocol
->sink_name
);
403 pa_strlcpy(name
, data
, sizeof(name
));
405 utf8_name
= pa_utf8_filter(name
);
406 pa_client_set_name(c
->client
, utf8_name
);
409 c
->original_name
= pa_xstrdup(name
);
411 pa_assert(!c
->sink_input
&& !c
->input_memblockq
);
413 pa_sink_input_new_data_init(&sdata
);
414 sdata
.driver
= __FILE__
;
415 sdata
.module
= c
->protocol
->module
;
416 sdata
.client
= c
->client
;
418 pa_proplist_update(sdata
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
419 pa_sink_input_new_data_set_sample_spec(&sdata
, &ss
);
421 c
->sink_input
= pa_sink_input_new(c
->protocol
->core
, &sdata
, 0);
422 pa_sink_input_new_data_done(&sdata
);
424 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
426 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
427 c
->input_memblockq
= pa_memblockq_new(
433 l
/PLAYBACK_BUFFER_FRAGMENTS
,
436 pa_iochannel_socket_set_rcvbuf(c
->io
, l
);
438 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
439 c
->sink_input
->pop
= sink_input_pop_cb
;
440 c
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
441 c
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
442 c
->sink_input
->kill
= sink_input_kill_cb
;
443 c
->sink_input
->userdata
= c
;
445 pa_sink_input_set_requested_latency(c
->sink_input
, DEFAULT_SINK_LATENCY
);
447 c
->state
= ESD_STREAMING_DATA
;
449 c
->protocol
->n_player
++;
451 pa_atomic_store(&c
->playback
.missing
, pa_memblockq_missing(c
->input_memblockq
));
453 pa_sink_input_put(c
->sink_input
);
458 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
459 char name
[ESD_NAME_MAX
], *utf8_name
;
460 int32_t format
, rate
;
461 pa_source
*source
= NULL
;
464 pa_source_output_new_data sdata
;
466 connection_assert_ref(c
);
468 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
470 memcpy(&format
, data
, sizeof(int32_t));
471 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
472 data
= (const char*) data
+ sizeof(int32_t);
474 memcpy(&rate
, data
, sizeof(int32_t));
475 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
476 data
= (const char*) data
+ sizeof(int32_t);
479 format_esd2native(format
, c
->swap_byte_order
, &ss
);
481 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
483 if (request
== ESD_PROTO_STREAM_MON
) {
486 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
487 pa_log("no such sink.");
491 if (!(source
= sink
->monitor_source
)) {
492 pa_log("no such monitor source.");
496 pa_assert(request
== ESD_PROTO_STREAM_REC
);
498 if (c
->protocol
->source_name
) {
499 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
500 pa_log("no such source.");
506 pa_strlcpy(name
, data
, sizeof(name
));
508 utf8_name
= pa_utf8_filter(name
);
509 pa_client_set_name(c
->client
, utf8_name
);
512 c
->original_name
= pa_xstrdup(name
);
514 pa_assert(!c
->output_memblockq
&& !c
->source_output
);
516 pa_source_output_new_data_init(&sdata
);
517 sdata
.driver
= __FILE__
;
518 sdata
.module
= c
->protocol
->module
;
519 sdata
.client
= c
->client
;
520 sdata
.source
= source
;
521 pa_proplist_update(sdata
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
522 pa_source_output_new_data_set_sample_spec(&sdata
, &ss
);
524 c
->source_output
= pa_source_output_new(c
->protocol
->core
, &sdata
, 0);
525 pa_source_output_new_data_done(&sdata
);
527 CHECK_VALIDITY(c
->source_output
, "Failed to create source output.");
529 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
530 c
->output_memblockq
= pa_memblockq_new(
539 pa_iochannel_socket_set_sndbuf(c
->io
, l
);
541 c
->source_output
->push
= source_output_push_cb
;
542 c
->source_output
->kill
= source_output_kill_cb
;
543 c
->source_output
->get_latency
= source_output_get_latency_cb
;
544 c
->source_output
->userdata
= c
;
546 pa_source_output_set_requested_latency(c
->source_output
, DEFAULT_SOURCE_LATENCY
);
548 c
->state
= ESD_STREAMING_DATA
;
550 c
->protocol
->n_player
++;
552 pa_source_output_put(c
->source_output
);
557 static int esd_proto_get_latency(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
563 pa_assert(length
== 0);
565 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
568 double usec
= pa_sink_get_latency(sink
);
569 latency
= (int) ((usec
*44100)/1000000);
572 latency
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
573 connection_write(c
, &latency
, sizeof(int32_t));
577 static int esd_proto_server_info(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
578 int32_t rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
584 pa_assert(length
== sizeof(int32_t));
586 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
587 rate
= sink
->sample_spec
.rate
;
588 format
= format_native2esd(&sink
->sample_spec
);
591 connection_write_prepare(c
, sizeof(int32_t) * 3);
594 connection_write(c
, &response
, sizeof(int32_t));
595 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
596 connection_write(c
, &rate
, sizeof(int32_t));
597 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
598 connection_write(c
, &format
, sizeof(int32_t));
603 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
606 uint32_t idx
= PA_IDXSET_INVALID
;
608 char terminator
[sizeof(int32_t)*6+ESD_NAME_MAX
];
612 pa_assert(length
== sizeof(int32_t));
614 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
617 k
= sizeof(int32_t)*5+ESD_NAME_MAX
;
618 s
= sizeof(int32_t)*6+ESD_NAME_MAX
;
619 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
620 t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1);
622 connection_write_prepare(c
, t
);
624 memset(terminator
, 0, sizeof(terminator
));
626 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
627 int32_t id
, format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
628 char name
[ESD_NAME_MAX
];
630 if (conn
->state
!= ESD_STREAMING_DATA
)
633 pa_assert(t
>= k
*2+s
);
635 if (conn
->sink_input
) {
636 pa_cvolume volume
= *pa_sink_input_get_volume(conn
->sink_input
);
637 rate
= conn
->sink_input
->sample_spec
.rate
;
638 lvolume
= (volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
639 rvolume
= (volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
640 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
644 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) (conn
->index
+1));
645 connection_write(c
, &id
, sizeof(int32_t));
648 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
649 if (conn
->original_name
)
650 strncpy(name
, conn
->original_name
, ESD_NAME_MAX
);
651 else if (conn
->client
&& pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
))
652 strncpy(name
, pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
), ESD_NAME_MAX
);
653 connection_write(c
, name
, ESD_NAME_MAX
);
656 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
657 connection_write(c
, &rate
, sizeof(int32_t));
660 lvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
661 connection_write(c
, &lvolume
, sizeof(int32_t));
664 rvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
665 connection_write(c
, &rvolume
, sizeof(int32_t));
668 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
669 connection_write(c
, &format
, sizeof(int32_t));
674 pa_assert(t
== s
*(nsamples
+1)+k
);
677 connection_write(c
, terminator
, k
);
682 idx
= PA_IDXSET_INVALID
;
683 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
684 int32_t id
, rate
, lvolume
, rvolume
, format
, len
;
685 char name
[ESD_NAME_MAX
];
690 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
691 connection_write(c
, &id
, sizeof(int32_t));
694 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
695 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
696 strncpy(name
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
698 pa_snprintf(name
, ESD_NAME_MAX
, "native.%s", ce
->name
);
699 connection_write(c
, name
, ESD_NAME_MAX
);
702 rate
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
703 connection_write(c
, &rate
, sizeof(int32_t));
706 lvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
707 connection_write(c
, &lvolume
, sizeof(int32_t));
710 rvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
711 connection_write(c
, &rvolume
, sizeof(int32_t));
714 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
715 connection_write(c
, &format
, sizeof(int32_t));
718 len
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
719 connection_write(c
, &len
, sizeof(int32_t));
727 connection_write(c
, terminator
, s
);
732 static int esd_proto_stream_pan(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
734 uint32_t idx
, lvolume
, rvolume
;
737 connection_assert_ref(c
);
739 pa_assert(length
== sizeof(int32_t)*3);
741 memcpy(&idx
, data
, sizeof(uint32_t));
742 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
743 data
= (const char*)data
+ sizeof(uint32_t);
745 memcpy(&lvolume
, data
, sizeof(uint32_t));
746 lvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, lvolume
);
747 data
= (const char*)data
+ sizeof(uint32_t);
749 memcpy(&rvolume
, data
, sizeof(uint32_t));
750 rvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, rvolume
);
751 data
= (const char*)data
+ sizeof(uint32_t);
753 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
)) && conn
->sink_input
) {
755 volume
.values
[0] = (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
756 volume
.values
[1] = (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
758 pa_sink_input_set_volume(conn
->sink_input
, &volume
);
763 connection_write(c
, &ok
, sizeof(int32_t));
768 static int esd_proto_sample_cache(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
770 int32_t format
, rate
, sc_length
;
772 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
774 connection_assert_ref(c
);
776 pa_assert(length
== (ESD_NAME_MAX
+3*sizeof(int32_t)));
778 memcpy(&format
, data
, sizeof(int32_t));
779 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
780 data
= (const char*)data
+ sizeof(int32_t);
782 memcpy(&rate
, data
, sizeof(int32_t));
783 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
784 data
= (const char*)data
+ sizeof(int32_t);
787 format_esd2native(format
, c
->swap_byte_order
, &ss
);
789 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
791 memcpy(&sc_length
, data
, sizeof(int32_t));
792 sc_length
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, sc_length
);
793 data
= (const char*)data
+ sizeof(int32_t);
795 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large (%d bytes).", (int)sc_length
);
797 strcpy(name
, SCACHE_PREFIX
);
798 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
800 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
802 pa_assert(!c
->scache
.memchunk
.memblock
);
803 c
->scache
.memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, sc_length
);
804 c
->scache
.memchunk
.index
= 0;
805 c
->scache
.memchunk
.length
= sc_length
;
806 c
->scache
.sample_spec
= ss
;
807 pa_assert(!c
->scache
.name
);
808 c
->scache
.name
= pa_xstrdup(name
);
810 c
->state
= ESD_CACHING_SAMPLE
;
812 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, c
->client
->proplist
, &idx
);
815 connection_write(c
, &idx
, sizeof(uint32_t));
820 static int esd_proto_sample_get_id(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
823 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
825 connection_assert_ref(c
);
827 pa_assert(length
== ESD_NAME_MAX
);
829 strcpy(name
, SCACHE_PREFIX
);
830 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
832 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
835 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
838 connection_write(c
, &ok
, sizeof(int32_t));
843 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
848 connection_assert_ref(c
);
850 pa_assert(length
== sizeof(int32_t));
852 memcpy(&idx
, data
, sizeof(uint32_t));
853 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
857 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
858 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
861 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
862 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
, c
->client
->proplist
, NULL
) >= 0)
865 pa_assert(request
== ESD_PROTO_SAMPLE_FREE
);
867 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
872 connection_write(c
, &ok
, sizeof(int32_t));
877 static int esd_proto_standby_or_resume(connection
*c
, PA_GCC_UNUSED esd_proto_t request
, PA_GCC_UNUSED
const void *data
, PA_GCC_UNUSED
size_t length
) {
880 connection_assert_ref(c
);
882 connection_write_prepare(c
, sizeof(int32_t) * 2);
885 connection_write(c
, &ok
, sizeof(int32_t));
886 connection_write(c
, &ok
, sizeof(int32_t));
891 /*** client callbacks ***/
893 static void client_kill_cb(pa_client
*c
) {
896 connection_unlink(CONNECTION(c
->userdata
));
899 /*** pa_iochannel callbacks ***/
901 static int do_read(connection
*c
) {
902 connection_assert_ref(c
);
904 /* pa_log("READ"); */
906 if (c
->state
== ESD_NEXT_REQUEST
) {
908 pa_assert(c
->read_data_length
< sizeof(c
->request
));
910 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
911 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
915 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
916 struct proto_handler
*handler
;
918 c
->request
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
920 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
921 pa_log("recieved invalid request.");
925 handler
= proto_map
+c
->request
;
927 /* pa_log("executing request #%u", c->request); */
929 if (!handler
->proc
) {
930 pa_log("recieved unimplemented request #%u.", c
->request
);
934 if (handler
->data_length
== 0) {
935 c
->read_data_length
= 0;
937 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
941 if (c
->read_data_alloc
< handler
->data_length
)
942 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
943 pa_assert(c
->read_data
);
945 c
->state
= ESD_NEEDS_REQDATA
;
946 c
->read_data_length
= 0;
950 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
952 struct proto_handler
*handler
= proto_map
+c
->request
;
954 pa_assert(handler
->proc
);
956 pa_assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
958 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
959 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
962 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
966 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
967 size_t l
= c
->read_data_length
;
968 pa_assert(handler
->proc
);
970 c
->state
= ESD_NEXT_REQUEST
;
971 c
->read_data_length
= 0;
973 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
976 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
980 pa_assert(c
->scache
.memchunk
.memblock
);
981 pa_assert(c
->scache
.name
);
982 pa_assert(c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
984 p
= pa_memblock_acquire(c
->scache
.memchunk
.memblock
);
985 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->scache
.memchunk
.index
, c
->scache
.memchunk
.length
-c
->scache
.memchunk
.index
);
986 pa_memblock_release(c
->scache
.memchunk
.memblock
);
989 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
992 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
996 c
->scache
.memchunk
.index
+= r
;
997 pa_assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
999 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
1002 c
->scache
.memchunk
.index
= 0;
1003 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, c
->client
->proplist
, &idx
);
1005 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
1006 c
->scache
.memchunk
.memblock
= NULL
;
1007 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
1009 pa_xfree(c
->scache
.name
);
1010 c
->scache
.name
= NULL
;
1012 c
->state
= ESD_NEXT_REQUEST
;
1015 connection_write(c
, &idx
, sizeof(uint32_t));
1018 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1025 pa_assert(c
->input_memblockq
);
1027 /* pa_log("STREAMING_DATA"); */
1029 if (!(l
= pa_atomic_load(&c
->playback
.missing
)))
1032 if (c
->playback
.current_memblock
) {
1034 space
= pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
;
1037 pa_memblock_unref(c
->playback
.current_memblock
);
1038 c
->playback
.current_memblock
= NULL
;
1042 if (!c
->playback
.current_memblock
) {
1043 pa_assert_se(c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, (size_t) -1));
1044 c
->playback
.memblock_index
= 0;
1046 space
= pa_memblock_get_length(c
->playback
.current_memblock
);
1052 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
1053 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->playback
.memblock_index
, l
);
1054 pa_memblock_release(c
->playback
.current_memblock
);
1058 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1061 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
1065 chunk
.memblock
= c
->playback
.current_memblock
;
1066 chunk
.index
= c
->playback
.memblock_index
;
1069 c
->playback
.memblock_index
+= r
;
1071 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, &chunk
, NULL
);
1072 pa_atomic_sub(&c
->playback
.missing
, r
);
1078 static int do_write(connection
*c
) {
1079 connection_assert_ref(c
);
1081 /* pa_log("WRITE"); */
1083 if (c
->write_data_length
) {
1086 pa_assert(c
->write_data_index
< c
->write_data_length
);
1087 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) {
1089 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1092 pa_log("write(): %s", pa_cstrerror(errno
));
1096 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
1097 c
->write_data_length
= c
->write_data_index
= 0;
1099 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
1104 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
1107 pa_assert(chunk
.memblock
);
1108 pa_assert(chunk
.length
);
1110 p
= pa_memblock_acquire(chunk
.memblock
);
1111 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
1112 pa_memblock_release(chunk
.memblock
);
1114 pa_memblock_unref(chunk
.memblock
);
1118 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1121 pa_log("write(): %s", pa_cstrerror(errno
));
1125 pa_memblockq_drop(c
->output_memblockq
, r
);
1131 static void do_work(connection
*c
) {
1132 connection_assert_ref(c
);
1134 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1139 if (pa_iochannel_is_readable(c
->io
))
1143 if (c
->state
== ESD_STREAMING_DATA
&& !c
->sink_input
&& pa_iochannel_is_hungup(c
->io
))
1144 /* In case we are in capture mode we will never call read()
1145 * on the socket, hence we need to detect the hangup manually
1146 * here, instead of simply waiting for read() to return 0. */
1149 if (pa_iochannel_is_writable(c
->io
))
1150 if (do_write(c
) < 0)
1157 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1160 pa_iochannel_free(c
->io
);
1163 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_DISABLE_PREBUF
, NULL
, 0, NULL
, NULL
);
1165 connection_unlink(c
);
1168 static void io_callback(pa_iochannel
*io
, void *userdata
) {
1169 connection
*c
= CONNECTION(userdata
);
1171 connection_assert_ref(c
);
1177 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
1178 connection
*c
= CONNECTION(userdata
);
1180 connection_assert_ref(c
);
1186 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1187 connection
*c
= CONNECTION(o
);
1188 connection_assert_ref(c
);
1191 case CONNECTION_MESSAGE_REQUEST_DATA
:
1195 case CONNECTION_MESSAGE_POST_DATA
:
1196 /* pa_log("got data %u", chunk->length); */
1197 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
1201 case CONNECTION_MESSAGE_UNLINK_CONNECTION
:
1202 connection_unlink(c
);
1209 /*** sink_input callbacks ***/
1211 /* Called from thread context */
1212 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1213 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1216 pa_sink_input_assert_ref(i
);
1217 c
= CONNECTION(i
->userdata
);
1218 connection_assert_ref(c
);
1222 case SINK_INPUT_MESSAGE_POST_DATA
: {
1225 /* New data from the main loop */
1226 pa_memblockq_push_align(c
->input_memblockq
, chunk
);
1228 if (pa_memblockq_is_readable(c
->input_memblockq
) && c
->playback
.underrun
) {
1229 pa_log_debug("Requesting rewind due to end of underrun.");
1230 pa_sink_input_request_rewind(c
->sink_input
, 0, FALSE
, TRUE
);
1233 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1238 case SINK_INPUT_MESSAGE_DISABLE_PREBUF
:
1239 pa_memblockq_prebuf_disable(c
->input_memblockq
);
1242 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1243 pa_usec_t
*r
= userdata
;
1245 *r
= pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1247 /* Fall through, the default handler will add in the extra
1248 * latency added by the resampler */
1252 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1256 /* Called from thread context */
1257 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
1260 pa_sink_input_assert_ref(i
);
1261 c
= CONNECTION(i
->userdata
);
1262 connection_assert_ref(c
);
1265 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1267 c
->playback
.underrun
= TRUE
;
1269 if (c
->dead
&& pa_sink_input_safe_to_remove(i
))
1270 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_UNLINK_CONNECTION
, NULL
, 0, NULL
, NULL
);
1276 chunk
->length
= PA_MIN(length
, chunk
->length
);
1278 c
->playback
.underrun
= FALSE
;
1280 pa_memblockq_drop(c
->input_memblockq
, chunk
->length
);
1281 m
= pa_memblockq_pop_missing(c
->input_memblockq
);
1284 if (pa_atomic_add(&c
->playback
.missing
, m
) <= 0)
1285 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1291 /* Called from thread context */
1292 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1295 pa_sink_input_assert_ref(i
);
1296 c
= CONNECTION(i
->userdata
);
1297 connection_assert_ref(c
);
1299 /* If we are in an underrun, then we don't rewind */
1300 if (i
->thread_info
.underrun_for
> 0)
1303 pa_memblockq_rewind(c
->input_memblockq
, nbytes
);
1306 /* Called from thread context */
1307 static void sink_input_update_max_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 pa_memblockq_set_maxrewind(c
->input_memblockq
, nbytes
);
1317 static void sink_input_kill_cb(pa_sink_input
*i
) {
1318 pa_sink_input_assert_ref(i
);
1320 connection_unlink(CONNECTION(i
->userdata
));
1323 /*** source_output callbacks ***/
1325 /* Called from thread context */
1326 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1329 pa_source_output_assert_ref(o
);
1330 c
= CONNECTION(o
->userdata
);
1334 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1337 static void source_output_kill_cb(pa_source_output
*o
) {
1338 pa_source_output_assert_ref(o
);
1340 connection_unlink(CONNECTION(o
->userdata
));
1343 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1346 pa_source_output_assert_ref(o
);
1347 c
= CONNECTION(o
->userdata
);
1350 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1353 /*** socket server callback ***/
1355 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1356 connection
*c
= CONNECTION(userdata
);
1360 connection_assert_ref(c
);
1361 pa_assert(c
->auth_timeout_event
== e
);
1364 connection_unlink(c
);
1367 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1369 pa_protocol_esound
*p
= userdata
;
1370 char cname
[256], pname
[128];
1376 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1377 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1378 pa_iochannel_free(io
);
1382 c
= pa_msgobject_new(connection
);
1383 c
->parent
.parent
.free
= connection_free
;
1384 c
->parent
.process_msg
= connection_process_msg
;
1387 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1389 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
1390 pa_snprintf(cname
, sizeof(cname
), "EsounD client (%s)", pname
);
1391 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1392 pa_proplist_sets(c
->client
->proplist
, "esound-protocol.peer", pname
);
1393 c
->client
->module
= p
->module
;
1394 c
->client
->kill
= client_kill_cb
;
1395 c
->client
->userdata
= c
;
1397 c
->authorized
= !!p
->public;
1398 c
->swap_byte_order
= FALSE
;
1401 c
->read_data_length
= 0;
1402 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1404 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1405 c
->write_data
= NULL
;
1407 c
->state
= ESD_NEEDS_REQDATA
;
1408 c
->request
= ESD_PROTO_CONNECT
;
1410 c
->sink_input
= NULL
;
1411 c
->input_memblockq
= NULL
;
1413 c
->source_output
= NULL
;
1414 c
->output_memblockq
= NULL
;
1416 c
->playback
.current_memblock
= NULL
;
1417 c
->playback
.memblock_index
= 0;
1418 c
->playback
.underrun
= TRUE
;
1419 pa_atomic_store(&c
->playback
.missing
, 0);
1421 pa_memchunk_reset(&c
->scache
.memchunk
);
1422 c
->scache
.name
= NULL
;
1424 c
->original_name
= NULL
;
1426 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
1427 pa_log_info("Client authenticated by IP ACL.");
1428 c
->authorized
= TRUE
;
1431 if (!c
->authorized
) {
1433 pa_gettimeofday(&tv
);
1434 tv
.tv_sec
+= AUTH_TIMEOUT
;
1435 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1437 c
->auth_timeout_event
= NULL
;
1439 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1440 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1442 pa_idxset_put(p
->connections
, c
, &c
->index
);
1445 /*** entry points ***/
1447 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1448 pa_protocol_esound
*p
= NULL
;
1449 pa_bool_t
public = FALSE
;
1457 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
1458 pa_log("auth-anonymous= expects a boolean argument.");
1462 p
= pa_xnew(pa_protocol_esound
, 1);
1464 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0)
1467 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
1469 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
1470 pa_log("Failed to parse IP ACL '%s'", acl
);
1474 p
->auth_ip_acl
= NULL
;
1479 p
->server
= pa_socket_server_ref(server
);
1480 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1481 p
->connections
= pa_idxset_new(NULL
, NULL
);
1483 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1484 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1494 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1498 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1499 connection_unlink(c
);
1500 pa_idxset_free(p
->connections
, NULL
, NULL
);
1503 pa_socket_server_unref(p
->server
);
1506 pa_ip_acl_free(p
->auth_ip_acl
);
1508 pa_xfree(p
->sink_name
);
1509 pa_xfree(p
->source_name
);