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>
55 #include <pulsecore/shared.h>
57 #include "endianmacros.h"
59 #include "protocol-esound.h"
61 /* Don't accept more connection than this */
62 #define MAX_CONNECTIONS 64
64 /* Kick a client if it doesn't authenticate within this time */
65 #define AUTH_TIMEOUT 5
67 #define DEFAULT_COOKIE_FILE ".esd_auth"
69 #define PLAYBACK_BUFFER_SECONDS (.25)
70 #define PLAYBACK_BUFFER_FRAGMENTS (10)
71 #define RECORD_BUFFER_SECONDS (5)
73 #define MAX_CACHE_SAMPLE_SIZE (2048000)
75 #define DEFAULT_SINK_LATENCY (150*PA_USEC_PER_MSEC)
76 #define DEFAULT_SOURCE_LATENCY (150*PA_USEC_PER_MSEC)
78 #define SCACHE_PREFIX "esound."
80 /* This is heavily based on esound's code */
82 typedef struct connection
{
87 pa_esound_protocol
*protocol
;
88 pa_esound_options
*options
;
91 pa_bool_t authorized
, swap_byte_order
;
93 size_t write_data_alloc
, write_data_index
, write_data_length
;
95 size_t read_data_alloc
, read_data_length
;
97 esd_client_state_t state
;
98 pa_sink_input
*sink_input
;
99 pa_source_output
*source_output
;
100 pa_memblockq
*input_memblockq
, *output_memblockq
;
101 pa_defer_event
*defer_event
;
106 pa_memblock
*current_memblock
;
107 size_t memblock_index
;
113 pa_memchunk memchunk
;
115 pa_sample_spec sample_spec
;
118 pa_time_event
*auth_timeout_event
;
121 PA_DECLARE_CLASS(connection
);
122 #define CONNECTION(o) (connection_cast(o))
123 static PA_DEFINE_CHECK_TYPE(connection
, pa_msgobject
);
125 struct pa_esound_protocol
{
129 pa_idxset
*connections
;
134 SINK_INPUT_MESSAGE_POST_DATA
= PA_SINK_INPUT_MESSAGE_MAX
, /* data from main loop to sink input */
135 SINK_INPUT_MESSAGE_DISABLE_PREBUF
139 CONNECTION_MESSAGE_REQUEST_DATA
,
140 CONNECTION_MESSAGE_POST_DATA
,
141 CONNECTION_MESSAGE_UNLINK_CONNECTION
144 typedef struct proto_handler
{
146 int (*proc
)(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
147 const char *description
;
148 } esd_proto_handler_info_t
;
150 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
);
151 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
152 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
);
153 static void sink_input_kill_cb(pa_sink_input
*i
);
154 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
);
155 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
157 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
158 static void source_output_kill_cb(pa_source_output
*o
);
160 static int esd_proto_connect(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
161 static int esd_proto_stream_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
162 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
163 static int esd_proto_get_latency(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
164 static int esd_proto_server_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
165 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
166 static int esd_proto_stream_pan(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
167 static int esd_proto_sample_cache(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
168 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
169 static int esd_proto_sample_get_id(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
170 static int esd_proto_standby_or_resume(connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
172 /* the big map of protocol handler info */
173 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
174 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
175 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
176 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
178 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
179 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
180 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
182 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
183 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
184 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
185 { sizeof(int), NULL
, "sample loop" },
186 { sizeof(int), NULL
, "sample stop" },
187 { (size_t) -1, NULL
, "TODO: sample kill" },
189 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
190 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
192 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
193 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
195 { sizeof(int), esd_proto_server_info
, "server info" },
196 { sizeof(int), esd_proto_all_info
, "all info" },
197 { (size_t) -1, NULL
, "TODO: subscribe" },
198 { (size_t) -1, NULL
, "TODO: unsubscribe" },
200 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
201 { 3 * sizeof(int), NULL
, "sample pan" },
203 { sizeof(int), NULL
, "standby mode" },
204 { 0, esd_proto_get_latency
, "get latency" }
207 static void connection_unlink(connection
*c
) {
214 pa_esound_options_unref(c
->options
);
219 pa_sink_input_unlink(c
->sink_input
);
220 pa_sink_input_unref(c
->sink_input
);
221 c
->sink_input
= NULL
;
224 if (c
->source_output
) {
225 pa_source_output_unlink(c
->source_output
);
226 pa_source_output_unref(c
->source_output
);
227 c
->source_output
= NULL
;
231 pa_client_free(c
->client
);
235 if (c
->state
== ESD_STREAMING_DATA
)
236 c
->protocol
->n_player
--;
239 pa_iochannel_free(c
->io
);
243 if (c
->defer_event
) {
244 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
245 c
->defer_event
= NULL
;
248 if (c
->auth_timeout_event
) {
249 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
250 c
->auth_timeout_event
= NULL
;
253 pa_assert_se(pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
) == c
);
258 static void connection_free(pa_object
*obj
) {
259 connection
*c
= CONNECTION(obj
);
262 if (c
->input_memblockq
)
263 pa_memblockq_free(c
->input_memblockq
);
264 if (c
->output_memblockq
)
265 pa_memblockq_free(c
->output_memblockq
);
267 if (c
->playback
.current_memblock
)
268 pa_memblock_unref(c
->playback
.current_memblock
);
270 pa_xfree(c
->read_data
);
271 pa_xfree(c
->write_data
);
273 if (c
->scache
.memchunk
.memblock
)
274 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
275 pa_xfree(c
->scache
.name
);
277 pa_xfree(c
->original_name
);
281 static void connection_write_prepare(connection
*c
, size_t length
) {
285 t
= c
->write_data_length
+length
;
287 if (c
->write_data_alloc
< t
)
288 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
290 pa_assert(c
->write_data
);
293 static void connection_write(connection
*c
, const void *data
, size_t length
) {
297 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
299 connection_write_prepare(c
, length
);
301 pa_assert(c
->write_data
);
303 i
= c
->write_data_length
;
304 c
->write_data_length
+= length
;
306 memcpy((uint8_t*) c
->write_data
+ i
, data
, length
);
309 static void format_esd2native(int format
, pa_bool_t swap_bytes
, pa_sample_spec
*ss
) {
312 ss
->channels
= (uint8_t) (((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1);
313 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
314 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
316 ss
->format
= PA_SAMPLE_U8
;
319 static int format_native2esd(pa_sample_spec
*ss
) {
322 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
323 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
328 #define CHECK_VALIDITY(expression, ...) do { \
329 if (!(expression)) { \
330 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
335 /*** esound commands ***/
337 static int esd_proto_connect(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
341 connection_assert_ref(c
);
343 pa_assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
345 if (!c
->authorized
&& c
->options
->auth_cookie
) {
348 if ((key
= pa_auth_cookie_read(c
->options
->auth_cookie
, ESD_KEY_LEN
)))
349 if (memcmp(data
, key
, ESD_KEY_LEN
) == 0)
350 c
->authorized
= TRUE
;
353 if (!c
->authorized
) {
354 pa_log("Kicked client with invalid authorization key.");
358 if (c
->auth_timeout_event
) {
359 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
360 c
->auth_timeout_event
= NULL
;
363 data
= (const char*)data
+ ESD_KEY_LEN
;
365 memcpy(&ekey
, data
, sizeof(uint32_t));
366 if (ekey
== ESD_ENDIAN_KEY
)
367 c
->swap_byte_order
= FALSE
;
368 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
369 c
->swap_byte_order
= TRUE
;
371 pa_log_warn("Client sent invalid endian key");
376 connection_write(c
, &ok
, sizeof(int));
380 static int esd_proto_stream_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
381 char name
[ESD_NAME_MAX
], *utf8_name
;
382 int32_t format
, rate
;
385 pa_sink
*sink
= NULL
;
386 pa_sink_input_new_data sdata
;
388 connection_assert_ref(c
);
390 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
392 memcpy(&format
, data
, sizeof(int32_t));
393 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
394 data
= (const char*) data
+ sizeof(int32_t);
396 memcpy(&rate
, data
, sizeof(int32_t));
397 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
398 data
= (const char*) data
+ sizeof(int32_t);
400 ss
.rate
= (uint32_t) rate
;
401 format_esd2native(format
, c
->swap_byte_order
, &ss
);
403 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
405 if (c
->options
->default_sink
) {
406 sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
, 1);
407 CHECK_VALIDITY(sink
, "No such sink: %s", c
->options
->default_sink
);
410 pa_strlcpy(name
, data
, sizeof(name
));
412 utf8_name
= pa_utf8_filter(name
);
413 pa_client_set_name(c
->client
, utf8_name
);
416 c
->original_name
= pa_xstrdup(name
);
418 pa_assert(!c
->sink_input
&& !c
->input_memblockq
);
420 pa_sink_input_new_data_init(&sdata
);
421 sdata
.driver
= __FILE__
;
422 sdata
.module
= c
->options
->module
;
423 sdata
.client
= c
->client
;
425 pa_proplist_update(sdata
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
426 pa_sink_input_new_data_set_sample_spec(&sdata
, &ss
);
428 c
->sink_input
= pa_sink_input_new(c
->protocol
->core
, &sdata
, 0);
429 pa_sink_input_new_data_done(&sdata
);
431 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
433 l
= (size_t) ((double) pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
434 c
->input_memblockq
= pa_memblockq_new(
440 l
/PLAYBACK_BUFFER_FRAGMENTS
,
443 pa_iochannel_socket_set_rcvbuf(c
->io
, l
);
445 c
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
446 c
->sink_input
->pop
= sink_input_pop_cb
;
447 c
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
448 c
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
449 c
->sink_input
->kill
= sink_input_kill_cb
;
450 c
->sink_input
->userdata
= c
;
452 pa_sink_input_set_requested_latency(c
->sink_input
, DEFAULT_SINK_LATENCY
);
454 c
->state
= ESD_STREAMING_DATA
;
456 c
->protocol
->n_player
++;
458 pa_atomic_store(&c
->playback
.missing
, (int) pa_memblockq_missing(c
->input_memblockq
));
460 pa_sink_input_put(c
->sink_input
);
465 static int esd_proto_stream_record(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
466 char name
[ESD_NAME_MAX
], *utf8_name
;
467 int32_t format
, rate
;
468 pa_source
*source
= NULL
;
471 pa_source_output_new_data sdata
;
473 connection_assert_ref(c
);
475 pa_assert(length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
477 memcpy(&format
, data
, sizeof(int32_t));
478 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
479 data
= (const char*) data
+ sizeof(int32_t);
481 memcpy(&rate
, data
, sizeof(int32_t));
482 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
483 data
= (const char*) data
+ sizeof(int32_t);
485 ss
.rate
= (uint32_t) rate
;
486 format_esd2native(format
, c
->swap_byte_order
, &ss
);
488 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
490 if (request
== ESD_PROTO_STREAM_MON
) {
493 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
, 1))) {
494 pa_log("no such sink.");
498 if (!(source
= sink
->monitor_source
)) {
499 pa_log("no such monitor source.");
503 pa_assert(request
== ESD_PROTO_STREAM_REC
);
505 if (c
->options
->default_source
) {
506 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_source
, PA_NAMEREG_SOURCE
, 1))) {
507 pa_log("no such source.");
513 pa_strlcpy(name
, data
, sizeof(name
));
515 utf8_name
= pa_utf8_filter(name
);
516 pa_client_set_name(c
->client
, utf8_name
);
519 c
->original_name
= pa_xstrdup(name
);
521 pa_assert(!c
->output_memblockq
&& !c
->source_output
);
523 pa_source_output_new_data_init(&sdata
);
524 sdata
.driver
= __FILE__
;
525 sdata
.module
= c
->options
->module
;
526 sdata
.client
= c
->client
;
527 sdata
.source
= source
;
528 pa_proplist_update(sdata
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
529 pa_source_output_new_data_set_sample_spec(&sdata
, &ss
);
531 c
->source_output
= pa_source_output_new(c
->protocol
->core
, &sdata
, 0);
532 pa_source_output_new_data_done(&sdata
);
534 CHECK_VALIDITY(c
->source_output
, "Failed to create source output.");
536 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
537 c
->output_memblockq
= pa_memblockq_new(
546 pa_iochannel_socket_set_sndbuf(c
->io
, l
);
548 c
->source_output
->push
= source_output_push_cb
;
549 c
->source_output
->kill
= source_output_kill_cb
;
550 c
->source_output
->get_latency
= source_output_get_latency_cb
;
551 c
->source_output
->userdata
= c
;
553 pa_source_output_set_requested_latency(c
->source_output
, DEFAULT_SOURCE_LATENCY
);
555 c
->state
= ESD_STREAMING_DATA
;
557 c
->protocol
->n_player
++;
559 pa_source_output_put(c
->source_output
);
564 static int esd_proto_get_latency(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
570 pa_assert(length
== 0);
572 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
, 1)))
575 double usec
= (double) pa_sink_get_latency(sink
);
576 latency
= (int) ((usec
*44100)/1000000);
579 latency
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
580 connection_write(c
, &latency
, sizeof(int32_t));
584 static int esd_proto_server_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
585 int32_t rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
591 pa_assert(length
== sizeof(int32_t));
593 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
, 1))) {
594 rate
= (int32_t) sink
->sample_spec
.rate
;
595 format
= format_native2esd(&sink
->sample_spec
);
598 connection_write_prepare(c
, sizeof(int32_t) * 3);
601 connection_write(c
, &response
, sizeof(int32_t));
602 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
603 connection_write(c
, &rate
, sizeof(int32_t));
604 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
605 connection_write(c
, &format
, sizeof(int32_t));
610 static int esd_proto_all_info(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
613 uint32_t idx
= PA_IDXSET_INVALID
;
615 char terminator
[sizeof(int32_t)*6+ESD_NAME_MAX
];
619 pa_assert(length
== sizeof(int32_t));
621 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
624 k
= sizeof(int32_t)*5+ESD_NAME_MAX
;
625 s
= sizeof(int32_t)*6+ESD_NAME_MAX
;
626 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
627 t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1);
629 connection_write_prepare(c
, t
);
631 memset(terminator
, 0, sizeof(terminator
));
633 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
634 int32_t id
, format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
635 char name
[ESD_NAME_MAX
];
637 if (conn
->state
!= ESD_STREAMING_DATA
)
640 pa_assert(t
>= k
*2+s
);
642 if (conn
->sink_input
) {
643 pa_cvolume volume
= *pa_sink_input_get_volume(conn
->sink_input
);
644 rate
= (int32_t) conn
->sink_input
->sample_spec
.rate
;
645 lvolume
= (int32_t) ((volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
646 rvolume
= (int32_t) ((volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
647 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
651 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) (conn
->index
+1));
652 connection_write(c
, &id
, sizeof(int32_t));
655 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
656 if (conn
->original_name
)
657 strncpy(name
, conn
->original_name
, ESD_NAME_MAX
);
658 else if (conn
->client
&& pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
))
659 strncpy(name
, pa_proplist_gets(conn
->client
->proplist
, PA_PROP_APPLICATION_NAME
), ESD_NAME_MAX
);
660 connection_write(c
, name
, ESD_NAME_MAX
);
663 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
664 connection_write(c
, &rate
, sizeof(int32_t));
667 lvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
668 connection_write(c
, &lvolume
, sizeof(int32_t));
671 rvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
672 connection_write(c
, &rvolume
, sizeof(int32_t));
675 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
676 connection_write(c
, &format
, sizeof(int32_t));
681 pa_assert(t
== s
*(nsamples
+1)+k
);
684 connection_write(c
, terminator
, k
);
689 idx
= PA_IDXSET_INVALID
;
690 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
691 int32_t id
, rate
, lvolume
, rvolume
, format
, len
;
692 char name
[ESD_NAME_MAX
];
697 id
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
698 connection_write(c
, &id
, sizeof(int32_t));
701 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
702 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
703 strncpy(name
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
705 pa_snprintf(name
, ESD_NAME_MAX
, "native.%s", ce
->name
);
706 connection_write(c
, name
, ESD_NAME_MAX
);
709 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ce
->sample_spec
.rate
);
710 connection_write(c
, &rate
, sizeof(int32_t));
713 lvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ((ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
));
714 connection_write(c
, &lvolume
, sizeof(int32_t));
717 rvolume
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) ((ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
));
718 connection_write(c
, &rvolume
, sizeof(int32_t));
721 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
722 connection_write(c
, &format
, sizeof(int32_t));
725 len
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
726 connection_write(c
, &len
, sizeof(int32_t));
734 connection_write(c
, terminator
, s
);
739 static int esd_proto_stream_pan(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
741 uint32_t idx
, lvolume
, rvolume
;
744 connection_assert_ref(c
);
746 pa_assert(length
== sizeof(int32_t)*3);
748 memcpy(&idx
, data
, sizeof(uint32_t));
749 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
750 data
= (const char*)data
+ sizeof(uint32_t);
752 memcpy(&lvolume
, data
, sizeof(uint32_t));
753 lvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, lvolume
);
754 data
= (const char*)data
+ sizeof(uint32_t);
756 memcpy(&rvolume
, data
, sizeof(uint32_t));
757 rvolume
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, rvolume
);
758 data
= (const char*)data
+ sizeof(uint32_t);
760 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
)) && conn
->sink_input
) {
762 volume
.values
[0] = (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
763 volume
.values
[1] = (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
765 pa_sink_input_set_volume(conn
->sink_input
, &volume
);
770 connection_write(c
, &ok
, sizeof(int32_t));
775 static int esd_proto_sample_cache(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
777 int32_t format
, rate
, sc_length
;
779 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
781 connection_assert_ref(c
);
783 pa_assert(length
== (ESD_NAME_MAX
+3*sizeof(int32_t)));
785 memcpy(&format
, data
, sizeof(int32_t));
786 format
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
787 data
= (const char*)data
+ sizeof(int32_t);
789 memcpy(&rate
, data
, sizeof(int32_t));
790 rate
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
791 data
= (const char*)data
+ sizeof(int32_t);
793 ss
.rate
= (uint32_t) rate
;
794 format_esd2native(format
, c
->swap_byte_order
, &ss
);
796 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
798 memcpy(&sc_length
, data
, sizeof(int32_t));
799 sc_length
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, sc_length
);
800 data
= (const char*)data
+ sizeof(int32_t);
802 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large (%d bytes).", (int)sc_length
);
804 strcpy(name
, SCACHE_PREFIX
);
805 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
807 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
809 pa_assert(!c
->scache
.memchunk
.memblock
);
810 c
->scache
.memchunk
.memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, (size_t) sc_length
);
811 c
->scache
.memchunk
.index
= 0;
812 c
->scache
.memchunk
.length
= (size_t) sc_length
;
813 c
->scache
.sample_spec
= ss
;
814 pa_assert(!c
->scache
.name
);
815 c
->scache
.name
= pa_xstrdup(name
);
817 c
->state
= ESD_CACHING_SAMPLE
;
819 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, c
->client
->proplist
, &idx
);
822 connection_write(c
, &idx
, sizeof(uint32_t));
827 static int esd_proto_sample_get_id(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
830 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
832 connection_assert_ref(c
);
834 pa_assert(length
== ESD_NAME_MAX
);
836 strcpy(name
, SCACHE_PREFIX
);
837 pa_strlcpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
839 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
842 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
843 ok
= (int32_t) idx
+ 1;
845 connection_write(c
, &ok
, sizeof(int32_t));
850 static int esd_proto_sample_free_or_play(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
855 connection_assert_ref(c
);
857 pa_assert(length
== sizeof(int32_t));
859 memcpy(&idx
, data
, sizeof(uint32_t));
860 idx
= PA_MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
864 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
865 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
868 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->options
->default_sink
, PA_NAMEREG_SINK
, 1)))
869 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
, c
->client
->proplist
, NULL
) >= 0)
870 ok
= (int32_t) idx
+ 1;
872 pa_assert(request
== ESD_PROTO_SAMPLE_FREE
);
874 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
875 ok
= (int32_t) idx
+ 1;
879 connection_write(c
, &ok
, sizeof(int32_t));
884 static int esd_proto_standby_or_resume(connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
887 connection_assert_ref(c
);
889 connection_write_prepare(c
, sizeof(int32_t) * 2);
892 connection_write(c
, &ok
, sizeof(int32_t));
893 connection_write(c
, &ok
, sizeof(int32_t));
898 /*** client callbacks ***/
900 static void client_kill_cb(pa_client
*c
) {
903 connection_unlink(CONNECTION(c
->userdata
));
906 /*** pa_iochannel callbacks ***/
908 static int do_read(connection
*c
) {
909 connection_assert_ref(c
);
911 /* pa_log("READ"); */
913 if (c
->state
== ESD_NEXT_REQUEST
) {
915 pa_assert(c
->read_data_length
< sizeof(c
->request
));
917 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
918 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
922 c
->read_data_length
+= (size_t) r
;
924 if (c
->read_data_length
>= sizeof(c
->request
)) {
925 struct proto_handler
*handler
;
927 c
->request
= PA_MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
929 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
930 pa_log("recieved invalid request.");
934 handler
= proto_map
+c
->request
;
936 /* pa_log("executing request #%u", c->request); */
938 if (!handler
->proc
) {
939 pa_log("recieved unimplemented request #%u.", c
->request
);
943 if (handler
->data_length
== 0) {
944 c
->read_data_length
= 0;
946 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
950 if (c
->read_data_alloc
< handler
->data_length
)
951 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
952 pa_assert(c
->read_data
);
954 c
->state
= ESD_NEEDS_REQDATA
;
955 c
->read_data_length
= 0;
959 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
961 struct proto_handler
*handler
= proto_map
+c
->request
;
963 pa_assert(handler
->proc
);
965 pa_assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
967 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
968 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
971 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
975 c
->read_data_length
+= (size_t) r
;
976 if (c
->read_data_length
>= handler
->data_length
) {
977 size_t l
= c
->read_data_length
;
978 pa_assert(handler
->proc
);
980 c
->state
= ESD_NEXT_REQUEST
;
981 c
->read_data_length
= 0;
983 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
986 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
990 pa_assert(c
->scache
.memchunk
.memblock
);
991 pa_assert(c
->scache
.name
);
992 pa_assert(c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
994 p
= pa_memblock_acquire(c
->scache
.memchunk
.memblock
);
995 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->scache
.memchunk
.index
, c
->scache
.memchunk
.length
-c
->scache
.memchunk
.index
);
996 pa_memblock_release(c
->scache
.memchunk
.memblock
);
999 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1002 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
1006 c
->scache
.memchunk
.index
+= (size_t) r
;
1007 pa_assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
1009 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
1012 c
->scache
.memchunk
.index
= 0;
1013 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, c
->client
->proplist
, &idx
);
1015 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
1016 c
->scache
.memchunk
.memblock
= NULL
;
1017 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
1019 pa_xfree(c
->scache
.name
);
1020 c
->scache
.name
= NULL
;
1022 c
->state
= ESD_NEXT_REQUEST
;
1025 connection_write(c
, &idx
, sizeof(uint32_t));
1028 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1035 pa_assert(c
->input_memblockq
);
1037 /* pa_log("STREAMING_DATA"); */
1039 if (!(l
= (size_t) pa_atomic_load(&c
->playback
.missing
)))
1042 if (c
->playback
.current_memblock
) {
1044 space
= pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
;
1047 pa_memblock_unref(c
->playback
.current_memblock
);
1048 c
->playback
.current_memblock
= NULL
;
1052 if (!c
->playback
.current_memblock
) {
1053 pa_assert_se(c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, (size_t) -1));
1054 c
->playback
.memblock_index
= 0;
1056 space
= pa_memblock_get_length(c
->playback
.current_memblock
);
1062 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
1063 r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+c
->playback
.memblock_index
, l
);
1064 pa_memblock_release(c
->playback
.current_memblock
);
1068 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1071 pa_log_debug("read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
1075 chunk
.memblock
= c
->playback
.current_memblock
;
1076 chunk
.index
= c
->playback
.memblock_index
;
1077 chunk
.length
= (size_t) r
;
1079 c
->playback
.memblock_index
+= (size_t) r
;
1081 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_POST_DATA
, NULL
, 0, &chunk
, NULL
);
1082 pa_atomic_sub(&c
->playback
.missing
, (int) r
);
1088 static int do_write(connection
*c
) {
1089 connection_assert_ref(c
);
1091 /* pa_log("WRITE"); */
1093 if (c
->write_data_length
) {
1096 pa_assert(c
->write_data_index
< c
->write_data_length
);
1097 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) {
1099 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1102 pa_log("write(): %s", pa_cstrerror(errno
));
1106 c
->write_data_index
+= (size_t) r
;
1107 if (c
->write_data_index
>= c
->write_data_length
)
1108 c
->write_data_length
= c
->write_data_index
= 0;
1110 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
1115 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
1118 pa_assert(chunk
.memblock
);
1119 pa_assert(chunk
.length
);
1121 p
= pa_memblock_acquire(chunk
.memblock
);
1122 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
1123 pa_memblock_release(chunk
.memblock
);
1125 pa_memblock_unref(chunk
.memblock
);
1129 if (r
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1132 pa_log("write(): %s", pa_cstrerror(errno
));
1136 pa_memblockq_drop(c
->output_memblockq
, (size_t) r
);
1142 static void do_work(connection
*c
) {
1143 connection_assert_ref(c
);
1145 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1150 if (pa_iochannel_is_readable(c
->io
))
1154 if (c
->state
== ESD_STREAMING_DATA
&& !c
->sink_input
&& pa_iochannel_is_hungup(c
->io
))
1155 /* In case we are in capture mode we will never call read()
1156 * on the socket, hence we need to detect the hangup manually
1157 * here, instead of simply waiting for read() to return 0. */
1160 if (pa_iochannel_is_writable(c
->io
))
1161 if (do_write(c
) < 0)
1168 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1171 pa_iochannel_free(c
->io
);
1174 pa_asyncmsgq_post(c
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(c
->sink_input
), SINK_INPUT_MESSAGE_DISABLE_PREBUF
, NULL
, 0, NULL
, NULL
);
1176 connection_unlink(c
);
1179 static void io_callback(pa_iochannel
*io
, void *userdata
) {
1180 connection
*c
= CONNECTION(userdata
);
1182 connection_assert_ref(c
);
1188 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
1189 connection
*c
= CONNECTION(userdata
);
1191 connection_assert_ref(c
);
1197 static int connection_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1198 connection
*c
= CONNECTION(o
);
1199 connection_assert_ref(c
);
1202 case CONNECTION_MESSAGE_REQUEST_DATA
:
1206 case CONNECTION_MESSAGE_POST_DATA
:
1207 /* pa_log("got data %u", chunk->length); */
1208 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
1212 case CONNECTION_MESSAGE_UNLINK_CONNECTION
:
1213 connection_unlink(c
);
1220 /*** sink_input callbacks ***/
1222 /* Called from thread context */
1223 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1224 pa_sink_input
*i
= PA_SINK_INPUT(o
);
1227 pa_sink_input_assert_ref(i
);
1228 c
= CONNECTION(i
->userdata
);
1229 connection_assert_ref(c
);
1233 case SINK_INPUT_MESSAGE_POST_DATA
: {
1236 /* New data from the main loop */
1237 pa_memblockq_push_align(c
->input_memblockq
, chunk
);
1239 if (pa_memblockq_is_readable(c
->input_memblockq
) && c
->playback
.underrun
) {
1240 pa_log_debug("Requesting rewind due to end of underrun.");
1241 pa_sink_input_request_rewind(c
->sink_input
, 0, FALSE
, TRUE
);
1244 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1249 case SINK_INPUT_MESSAGE_DISABLE_PREBUF
:
1250 pa_memblockq_prebuf_disable(c
->input_memblockq
);
1253 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
1254 pa_usec_t
*r
= userdata
;
1256 *r
= pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1258 /* Fall through, the default handler will add in the extra
1259 * latency added by the resampler */
1263 return pa_sink_input_process_msg(o
, code
, userdata
, offset
, chunk
);
1267 /* Called from thread context */
1268 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
1271 pa_sink_input_assert_ref(i
);
1272 c
= CONNECTION(i
->userdata
);
1273 connection_assert_ref(c
);
1276 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1278 c
->playback
.underrun
= TRUE
;
1280 if (c
->dead
&& pa_sink_input_safe_to_remove(i
))
1281 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_UNLINK_CONNECTION
, NULL
, 0, NULL
, NULL
);
1287 chunk
->length
= PA_MIN(length
, chunk
->length
);
1289 c
->playback
.underrun
= FALSE
;
1291 pa_memblockq_drop(c
->input_memblockq
, chunk
->length
);
1292 m
= pa_memblockq_pop_missing(c
->input_memblockq
);
1295 if (pa_atomic_add(&c
->playback
.missing
, (int) m
) <= 0)
1296 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_REQUEST_DATA
, NULL
, 0, NULL
, NULL
);
1302 /* Called from thread context */
1303 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1306 pa_sink_input_assert_ref(i
);
1307 c
= CONNECTION(i
->userdata
);
1308 connection_assert_ref(c
);
1310 /* If we are in an underrun, then we don't rewind */
1311 if (i
->thread_info
.underrun_for
> 0)
1314 pa_memblockq_rewind(c
->input_memblockq
, nbytes
);
1317 /* Called from thread context */
1318 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1321 pa_sink_input_assert_ref(i
);
1322 c
= CONNECTION(i
->userdata
);
1323 connection_assert_ref(c
);
1325 pa_memblockq_set_maxrewind(c
->input_memblockq
, nbytes
);
1328 static void sink_input_kill_cb(pa_sink_input
*i
) {
1329 pa_sink_input_assert_ref(i
);
1331 connection_unlink(CONNECTION(i
->userdata
));
1334 /*** source_output callbacks ***/
1336 /* Called from thread context */
1337 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1340 pa_source_output_assert_ref(o
);
1341 c
= CONNECTION(o
->userdata
);
1345 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(c
), CONNECTION_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
1348 static void source_output_kill_cb(pa_source_output
*o
) {
1349 pa_source_output_assert_ref(o
);
1351 connection_unlink(CONNECTION(o
->userdata
));
1354 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1357 pa_source_output_assert_ref(o
);
1358 c
= CONNECTION(o
->userdata
);
1361 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1364 /*** entry points ***/
1366 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1367 connection
*c
= CONNECTION(userdata
);
1371 connection_assert_ref(c
);
1372 pa_assert(c
->auth_timeout_event
== e
);
1375 connection_unlink(c
);
1378 void pa_esound_protocol_connect(pa_esound_protocol
*p
, pa_iochannel
*io
, pa_esound_options
*o
) {
1380 char cname
[256], pname
[128];
1386 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1387 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1388 pa_iochannel_free(io
);
1392 c
= pa_msgobject_new(connection
);
1393 c
->parent
.parent
.free
= connection_free
;
1394 c
->parent
.process_msg
= connection_process_msg
;
1397 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1399 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
1400 pa_snprintf(cname
, sizeof(cname
), "EsounD client (%s)", pname
);
1401 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1402 pa_proplist_sets(c
->client
->proplist
, "esound-protocol.peer", pname
);
1403 c
->client
->module
= o
->module
;
1404 c
->client
->kill
= client_kill_cb
;
1405 c
->client
->userdata
= c
;
1407 c
->options
= pa_esound_options_ref(o
);
1408 c
->authorized
= FALSE
;
1409 c
->swap_byte_order
= FALSE
;
1412 c
->read_data_length
= 0;
1413 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1415 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1416 c
->write_data
= NULL
;
1418 c
->state
= ESD_NEEDS_REQDATA
;
1419 c
->request
= ESD_PROTO_CONNECT
;
1421 c
->sink_input
= NULL
;
1422 c
->input_memblockq
= NULL
;
1424 c
->source_output
= NULL
;
1425 c
->output_memblockq
= NULL
;
1427 c
->playback
.current_memblock
= NULL
;
1428 c
->playback
.memblock_index
= 0;
1429 c
->playback
.underrun
= TRUE
;
1430 pa_atomic_store(&c
->playback
.missing
, 0);
1432 pa_memchunk_reset(&c
->scache
.memchunk
);
1433 c
->scache
.name
= NULL
;
1435 c
->original_name
= NULL
;
1437 if (o
->auth_anonymous
) {
1438 pa_log_info("Client authenticated anonymously.");
1439 c
->authorized
= TRUE
;
1442 if (!c
->authorized
&&
1444 pa_ip_acl_check(o
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
1446 pa_log_info("Client authenticated by IP ACL.");
1447 c
->authorized
= TRUE
;
1450 if (!c
->authorized
) {
1452 pa_gettimeofday(&tv
);
1453 tv
.tv_sec
+= AUTH_TIMEOUT
;
1454 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1456 c
->auth_timeout_event
= NULL
;
1458 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1459 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1461 pa_idxset_put(p
->connections
, c
, &c
->index
);
1464 void pa_esound_protocol_disconnect(pa_esound_protocol
*p
, pa_module
*m
) {
1471 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
1472 if (c
->options
->module
== m
)
1473 connection_unlink(c
);
1476 static pa_esound_protocol
* esound_protocol_new(pa_core
*c
) {
1477 pa_esound_protocol
*p
;
1481 p
= pa_xnew(pa_esound_protocol
, 1);
1484 p
->connections
= pa_idxset_new(NULL
, NULL
);
1487 pa_assert_se(pa_shared_set(c
, "esound-protocol", p
) >= 0);
1492 pa_esound_protocol
* pa_esound_protocol_get(pa_core
*c
) {
1493 pa_esound_protocol
*p
;
1495 if ((p
= pa_shared_get(c
, "esound-protocol")))
1496 return pa_esound_protocol_ref(p
);
1498 return esound_protocol_new(c
);
1501 pa_esound_protocol
* pa_esound_protocol_ref(pa_esound_protocol
*p
) {
1503 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
1510 void pa_esound_protocol_unref(pa_esound_protocol
*p
) {
1513 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
1515 if (PA_REFCNT_DEC(p
) > 0)
1518 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1519 connection_unlink(c
);
1521 pa_idxset_free(p
->connections
, NULL
, NULL
);
1523 pa_assert_se(pa_shared_remove(p
->core
, "esound-protocol") >= 0);
1528 pa_esound_options
* pa_esound_options_new(void) {
1529 pa_esound_options
*o
;
1531 o
= pa_xnew0(pa_esound_options
, 1);
1537 pa_esound_options
* pa_esound_options_ref(pa_esound_options
*o
) {
1539 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1546 void pa_esound_options_unref(pa_esound_options
*o
) {
1548 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1550 if (PA_REFCNT_DEC(o
) > 0)
1554 pa_ip_acl_free(o
->auth_ip_acl
);
1557 pa_auth_cookie_unref(o
->auth_cookie
);
1559 pa_xfree(o
->default_sink
);
1560 pa_xfree(o
->default_source
);
1565 int pa_esound_options_parse(pa_esound_options
*o
, pa_core
*c
, pa_modargs
*ma
) {
1570 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
1573 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &o
->auth_anonymous
) < 0) {
1574 pa_log("auth-anonymous= expects a boolean argument.");
1578 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
1581 if (!(ipa
= pa_ip_acl_new(acl
))) {
1582 pa_log("Failed to parse IP ACL '%s'", acl
);
1587 pa_ip_acl_free(o
->auth_ip_acl
);
1589 o
->auth_ip_acl
= ipa
;
1593 if (pa_modargs_get_value_boolean(ma
, "auth-cookie-enabled", &enabled
) < 0) {
1594 pa_log("auth-cookie-enabled= expects a boolean argument.");
1599 pa_auth_cookie_unref(o
->auth_cookie
);
1604 /* The new name for this is 'auth-cookie', for compat reasons
1605 * we check the old name too */
1606 if (!(cn
= pa_modargs_get_value(ma
, "auth-cookie", NULL
)))
1607 if (!(cn
= pa_modargs_get_value(ma
, "cookie", NULL
)))
1608 cn
= DEFAULT_COOKIE_FILE
;
1610 if (!(o
->auth_cookie
= pa_auth_cookie_get(c
, cn
, ESD_KEY_LEN
)))
1614 o
->auth_cookie
= NULL
;
1616 pa_xfree(o
->default_sink
);
1617 o
->default_sink
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1619 pa_xfree(o
->default_source
);
1620 o
->default_source
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));