4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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>
54 #include "endianmacros.h"
56 #include "protocol-esound.h"
58 /* Don't accept more connection than this */
59 #define MAX_CONNECTIONS 64
61 /* Kick a client if it doesn't authenticate within this time */
62 #define AUTH_TIMEOUT 5
64 #define DEFAULT_COOKIE_FILE ".esd_auth"
66 #define PLAYBACK_BUFFER_SECONDS (.25)
67 #define PLAYBACK_BUFFER_FRAGMENTS (10)
68 #define RECORD_BUFFER_SECONDS (5)
69 #define RECORD_BUFFER_FRAGMENTS (100)
71 #define MAX_CACHE_SAMPLE_SIZE (1024000)
73 #define SCACHE_PREFIX "esound."
75 /* This is heavily based on esound's code */
80 pa_protocol_esound
*protocol
;
83 int authorized
, swap_byte_order
;
85 size_t write_data_alloc
, write_data_index
, write_data_length
;
87 size_t read_data_alloc
, read_data_length
;
89 esd_client_state_t state
;
90 pa_sink_input
*sink_input
;
91 pa_source_output
*source_output
;
92 pa_memblockq
*input_memblockq
, *output_memblockq
;
93 pa_defer_event
*defer_event
;
98 pa_memblock
*current_memblock
;
99 size_t memblock_index
, fragment_size
;
103 pa_memchunk memchunk
;
105 pa_sample_spec sample_spec
;
108 pa_time_event
*auth_timeout_event
;
111 struct pa_protocol_esound
{
115 pa_socket_server
*server
;
116 pa_idxset
*connections
;
117 char *sink_name
, *source_name
;
119 uint8_t esd_key
[ESD_KEY_LEN
];
120 pa_ip_acl
*auth_ip_acl
;
123 typedef struct proto_handler
{
125 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
126 const char *description
;
127 } esd_proto_handler_info_t
;
129 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
);
130 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
);
131 static void sink_input_kill_cb(pa_sink_input
*i
);
132 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
);
133 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
135 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
136 static void source_output_kill_cb(pa_source_output
*o
);
138 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
139 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
140 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
141 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
142 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
143 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
144 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
145 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
146 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
147 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
148 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
150 /* the big map of protocol handler info */
151 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
152 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
153 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
154 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
156 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
157 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
158 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
160 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
161 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
162 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
163 { sizeof(int), NULL
, "sample loop" },
164 { sizeof(int), NULL
, "sample stop" },
165 { -1, NULL
, "TODO: sample kill" },
167 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
168 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
170 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
171 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
173 { sizeof(int), esd_proto_server_info
, "server info" },
174 { sizeof(int), esd_proto_all_info
, "all info" },
175 { -1, NULL
, "TODO: subscribe" },
176 { -1, NULL
, "TODO: unsubscribe" },
178 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
179 { 3 * sizeof(int), NULL
, "sample pan" },
181 { sizeof(int), NULL
, "standby mode" },
182 { 0, esd_proto_get_latency
, "get latency" }
185 static void connection_free(struct connection
*c
) {
187 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
189 if (c
->state
== ESD_STREAMING_DATA
)
190 c
->protocol
->n_player
--;
192 pa_client_free(c
->client
);
195 pa_sink_input_disconnect(c
->sink_input
);
196 pa_sink_input_unref(c
->sink_input
);
199 if (c
->source_output
) {
200 pa_source_output_disconnect(c
->source_output
);
201 pa_source_output_unref(c
->source_output
);
204 if (c
->input_memblockq
)
205 pa_memblockq_free(c
->input_memblockq
);
206 if (c
->output_memblockq
)
207 pa_memblockq_free(c
->output_memblockq
);
209 if (c
->playback
.current_memblock
)
210 pa_memblock_unref(c
->playback
.current_memblock
);
212 pa_xfree(c
->read_data
);
213 pa_xfree(c
->write_data
);
216 pa_iochannel_free(c
->io
);
219 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
221 if (c
->scache
.memchunk
.memblock
)
222 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
223 pa_xfree(c
->scache
.name
);
225 if (c
->auth_timeout_event
)
226 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
228 pa_xfree(c
->original_name
);
232 static void connection_write_prepare(struct connection
*c
, size_t length
) {
236 t
= c
->write_data_length
+length
;
238 if (c
->write_data_alloc
< t
)
239 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
241 assert(c
->write_data
);
244 static void connection_write(struct connection
*c
, const void *data
, size_t length
) {
248 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
249 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
251 connection_write_prepare(c
, length
);
253 assert(c
->write_data
);
255 i
= c
->write_data_length
;
256 c
->write_data_length
+= length
;
258 memcpy((char*)c
->write_data
+ i
, data
, length
);
261 static void format_esd2native(int format
, int swap_bytes
, pa_sample_spec
*ss
) {
264 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
265 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
266 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
268 ss
->format
= PA_SAMPLE_U8
;
271 static int format_native2esd(pa_sample_spec
*ss
) {
274 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
275 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
280 #define CHECK_VALIDITY(expression, string) do { \
281 if (!(expression)) { \
282 pa_log_warn(__FILE__ ": " string); \
287 /*** esound commands ***/
289 static int esd_proto_connect(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
293 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
295 if (!c
->authorized
) {
296 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
297 pa_log(__FILE__
": kicked client with invalid authorization key.");
302 if (c
->auth_timeout_event
) {
303 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
304 c
->auth_timeout_event
= NULL
;
308 data
= (const char*)data
+ ESD_KEY_LEN
;
310 memcpy(&ekey
, data
, sizeof(uint32_t));
311 if (ekey
== ESD_ENDIAN_KEY
)
312 c
->swap_byte_order
= 0;
313 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
314 c
->swap_byte_order
= 1;
316 pa_log(__FILE__
": client sent invalid endian key");
321 connection_write(c
, &ok
, sizeof(int));
325 static int esd_proto_stream_play(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
326 char name
[ESD_NAME_MAX
], *utf8_name
;
327 int32_t format
, rate
;
331 pa_sink_input_new_data sdata
;
333 assert(c
&& length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
335 memcpy(&format
, data
, sizeof(int32_t));
336 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
337 data
= (const char*)data
+ sizeof(int32_t);
339 memcpy(&rate
, data
, sizeof(int32_t));
340 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
341 data
= (const char*)data
+ sizeof(int32_t);
344 format_esd2native(format
, c
->swap_byte_order
, &ss
);
346 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
347 sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1);
348 CHECK_VALIDITY(sink
, "No such sink");
350 strncpy(name
, data
, sizeof(name
));
351 name
[sizeof(name
)-1] = 0;
353 utf8_name
= pa_utf8_filter(name
);
354 pa_client_set_name(c
->client
, utf8_name
);
357 c
->original_name
= pa_xstrdup(name
);
359 assert(!c
->sink_input
&& !c
->input_memblockq
);
361 pa_sink_input_new_data_init(&sdata
);
363 sdata
.driver
= __FILE__
;
364 sdata
.name
= c
->client
->name
;
365 pa_sink_input_new_data_set_sample_spec(&sdata
, &ss
);
366 sdata
.module
= c
->protocol
->module
;
367 sdata
.client
= c
->client
;
369 c
->sink_input
= pa_sink_input_new(c
->protocol
->core
, &sdata
, 0);
370 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
372 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
373 c
->input_memblockq
= pa_memblockq_new(
379 l
/PLAYBACK_BUFFER_FRAGMENTS
,
381 c
->protocol
->core
->memblock_stat
);
382 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
383 c
->playback
.fragment_size
= l
/10;
385 c
->sink_input
->peek
= sink_input_peek_cb
;
386 c
->sink_input
->drop
= sink_input_drop_cb
;
387 c
->sink_input
->kill
= sink_input_kill_cb
;
388 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
389 c
->sink_input
->userdata
= c
;
391 c
->state
= ESD_STREAMING_DATA
;
393 c
->protocol
->n_player
++;
398 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
399 char name
[ESD_NAME_MAX
], *utf8_name
;
400 int32_t format
, rate
;
404 pa_source_output_new_data sdata
;
406 assert(c
&& length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
408 memcpy(&format
, data
, sizeof(int32_t));
409 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
410 data
= (const char*)data
+ sizeof(int32_t);
412 memcpy(&rate
, data
, sizeof(int32_t));
413 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
414 data
= (const char*)data
+ sizeof(int32_t);
417 format_esd2native(format
, c
->swap_byte_order
, &ss
);
419 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
421 if (request
== ESD_PROTO_STREAM_MON
) {
424 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
425 pa_log(__FILE__
": no such sink.");
429 if (!(source
= sink
->monitor_source
)) {
430 pa_log(__FILE__
": no such monitor source.");
434 assert(request
== ESD_PROTO_STREAM_REC
);
436 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
437 pa_log(__FILE__
": no such source.");
442 strncpy(name
, data
, sizeof(name
));
443 name
[sizeof(name
)-1] = 0;
445 utf8_name
= pa_utf8_filter(name
);
446 pa_client_set_name(c
->client
, utf8_name
);
449 c
->original_name
= pa_xstrdup(name
);
451 assert(!c
->output_memblockq
&& !c
->source_output
);
453 pa_source_output_new_data_init(&sdata
);
454 sdata
.source
= source
;
455 sdata
.driver
= __FILE__
;
456 sdata
.name
= c
->client
->name
;
457 pa_source_output_new_data_set_sample_spec(&sdata
, &ss
);
458 sdata
.module
= c
->protocol
->module
;
459 sdata
.client
= c
->client
;
461 c
->source_output
= pa_source_output_new(c
->protocol
->core
, &sdata
, 9);
462 CHECK_VALIDITY(c
->sink_input
, "Failed to create source_output.");
464 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
465 c
->output_memblockq
= pa_memblockq_new(
473 c
->protocol
->core
->memblock_stat
);
474 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
476 c
->source_output
->push
= source_output_push_cb
;
477 c
->source_output
->kill
= source_output_kill_cb
;
478 c
->source_output
->get_latency
= source_output_get_latency_cb
;
479 c
->source_output
->userdata
= c
;
481 c
->state
= ESD_STREAMING_DATA
;
483 c
->protocol
->n_player
++;
488 static int esd_proto_get_latency(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
492 assert(c
&& !data
&& length
== 0);
494 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
497 double usec
= pa_sink_get_latency(sink
);
498 latency
= (int) ((usec
*44100)/1000000);
501 latency
= MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
502 connection_write(c
, &latency
, sizeof(int32_t));
506 static int esd_proto_server_info(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
507 int32_t rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
511 assert(c
&& data
&& length
== sizeof(int32_t));
513 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
514 rate
= sink
->sample_spec
.rate
;
515 format
= format_native2esd(&sink
->sample_spec
);
518 connection_write_prepare(c
, sizeof(int32_t) * 3);
521 connection_write(c
, &response
, sizeof(int32_t));
522 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
523 connection_write(c
, &rate
, sizeof(int32_t));
524 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
525 connection_write(c
, &format
, sizeof(int32_t));
530 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
532 struct connection
*conn
;
533 uint32_t idx
= PA_IDXSET_INVALID
;
535 char terminator
[sizeof(int32_t)*6+ESD_NAME_MAX
];
537 assert(c
&& data
&& length
== sizeof(int32_t));
539 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
542 k
= sizeof(int32_t)*5+ESD_NAME_MAX
;
543 s
= sizeof(int32_t)*6+ESD_NAME_MAX
;
544 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
545 t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1);
547 connection_write_prepare(c
, t
);
549 memset(terminator
, 0, sizeof(terminator
));
551 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
552 int32_t id
, format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
553 char name
[ESD_NAME_MAX
];
555 if (conn
->state
!= ESD_STREAMING_DATA
)
560 if (conn
->sink_input
) {
561 pa_cvolume volume
= *pa_sink_input_get_volume(conn
->sink_input
);
562 rate
= conn
->sink_input
->sample_spec
.rate
;
563 lvolume
= (volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
564 rvolume
= (volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
565 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
569 id
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) (conn
->index
+1));
570 connection_write(c
, &id
, sizeof(int32_t));
573 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
574 if (conn
->original_name
)
575 strncpy(name
, conn
->original_name
, ESD_NAME_MAX
);
576 else if (conn
->client
&& conn
->client
->name
)
577 strncpy(name
, conn
->client
->name
, ESD_NAME_MAX
);
578 connection_write(c
, name
, ESD_NAME_MAX
);
581 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
582 connection_write(c
, &rate
, sizeof(int32_t));
585 lvolume
= MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
586 connection_write(c
, &lvolume
, sizeof(int32_t));
589 rvolume
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
590 connection_write(c
, &rvolume
, sizeof(int32_t));
593 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
594 connection_write(c
, &format
, sizeof(int32_t));
599 assert(t
== s
*(nsamples
+1)+k
);
602 connection_write(c
, terminator
, k
);
607 idx
= PA_IDXSET_INVALID
;
608 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
609 int32_t id
, rate
, lvolume
, rvolume
, format
, len
;
610 char name
[ESD_NAME_MAX
];
615 id
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
616 connection_write(c
, &id
, sizeof(int32_t));
619 memset(name
, 0, ESD_NAME_MAX
); /* don't leak old data */
620 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
621 strncpy(name
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
623 snprintf(name
, ESD_NAME_MAX
, "native.%s", ce
->name
);
624 connection_write(c
, name
, ESD_NAME_MAX
);
627 rate
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
628 connection_write(c
, &rate
, sizeof(int32_t));
631 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
632 connection_write(c
, &lvolume
, sizeof(int32_t));
635 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
636 connection_write(c
, &rvolume
, sizeof(int32_t));
639 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
640 connection_write(c
, &format
, sizeof(int32_t));
643 len
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
644 connection_write(c
, &len
, sizeof(int32_t));
652 connection_write(c
, terminator
, s
);
657 static int esd_proto_stream_pan(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
659 uint32_t idx
, lvolume
, rvolume
;
660 struct connection
*conn
;
662 assert(c
&& data
&& length
== sizeof(int32_t)*3);
664 memcpy(&idx
, data
, sizeof(uint32_t));
665 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
666 data
= (const char*)data
+ sizeof(uint32_t);
668 memcpy(&lvolume
, data
, sizeof(uint32_t));
669 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, lvolume
);
670 data
= (const char*)data
+ sizeof(uint32_t);
672 memcpy(&rvolume
, data
, sizeof(uint32_t));
673 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, rvolume
);
674 data
= (const char*)data
+ sizeof(uint32_t);
676 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
)) && conn
->sink_input
) {
678 volume
.values
[0] = (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
679 volume
.values
[1] = (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
681 pa_sink_input_set_volume(conn
->sink_input
, &volume
);
686 connection_write(c
, &ok
, sizeof(int32_t));
691 static int esd_proto_sample_cache(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
693 int32_t format
, rate
, sc_length
;
695 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
697 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int32_t)));
699 memcpy(&format
, data
, sizeof(int32_t));
700 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
701 data
= (const char*)data
+ sizeof(int32_t);
703 memcpy(&rate
, data
, sizeof(int32_t));
704 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
705 data
= (const char*)data
+ sizeof(int32_t);
708 format_esd2native(format
, c
->swap_byte_order
, &ss
);
710 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
712 memcpy(&sc_length
, data
, sizeof(int32_t));
713 sc_length
= MAYBE_INT32_SWAP(c
->swap_byte_order
, sc_length
);
714 data
= (const char*)data
+ sizeof(int32_t);
716 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large.");
718 strcpy(name
, SCACHE_PREFIX
);
719 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
720 name
[sizeof(name
)-1] = 0;
722 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
724 assert(!c
->scache
.memchunk
.memblock
);
725 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
726 c
->scache
.memchunk
.index
= 0;
727 c
->scache
.memchunk
.length
= sc_length
;
728 c
->scache
.sample_spec
= ss
;
729 assert(!c
->scache
.name
);
730 c
->scache
.name
= pa_xstrdup(name
);
732 c
->state
= ESD_CACHING_SAMPLE
;
734 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, &idx
);
737 connection_write(c
, &idx
, sizeof(uint32_t));
742 static int esd_proto_sample_get_id(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
745 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
747 assert(c
&& data
&& length
== ESD_NAME_MAX
);
749 strcpy(name
, SCACHE_PREFIX
);
750 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
751 name
[sizeof(name
)-1] = 0;
753 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
756 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
759 connection_write(c
, &ok
, sizeof(int32_t));
764 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
769 assert(c
&& data
&& length
== sizeof(int32_t));
771 memcpy(&idx
, data
, sizeof(uint32_t));
772 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
776 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
777 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
780 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
781 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
784 assert(request
== ESD_PROTO_SAMPLE_FREE
);
786 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
791 connection_write(c
, &ok
, sizeof(int32_t));
796 static int esd_proto_standby_or_resume(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, PA_GCC_UNUSED
const void *data
, PA_GCC_UNUSED
size_t length
) {
799 connection_write_prepare(c
, sizeof(int32_t) * 2);
802 connection_write(c
, &ok
, sizeof(int32_t));
803 connection_write(c
, &ok
, sizeof(int32_t));
808 /*** client callbacks ***/
810 static void client_kill_cb(pa_client
*c
) {
811 assert(c
&& c
->userdata
);
812 connection_free(c
->userdata
);
815 /*** pa_iochannel callbacks ***/
817 static int do_read(struct connection
*c
) {
820 /* pa_log("READ"); */
822 if (c
->state
== ESD_NEXT_REQUEST
) {
824 assert(c
->read_data_length
< sizeof(c
->request
));
826 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
827 pa_log_debug(__FILE__
": read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
831 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
832 struct proto_handler
*handler
;
834 c
->request
= MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
836 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
837 pa_log(__FILE__
": recieved invalid request.");
841 handler
= proto_map
+c
->request
;
843 /* pa_log(__FILE__": executing request #%u", c->request); */
845 if (!handler
->proc
) {
846 pa_log(__FILE__
": recieved unimplemented request #%u.", c
->request
);
850 if (handler
->data_length
== 0) {
851 c
->read_data_length
= 0;
853 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
857 if (c
->read_data_alloc
< handler
->data_length
)
858 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
859 assert(c
->read_data
);
861 c
->state
= ESD_NEEDS_REQDATA
;
862 c
->read_data_length
= 0;
866 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
868 struct proto_handler
*handler
= proto_map
+c
->request
;
870 assert(handler
->proc
);
872 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
874 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
875 pa_log_debug(__FILE__
": read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
879 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
880 size_t l
= c
->read_data_length
;
881 assert(handler
->proc
);
883 c
->state
= ESD_NEXT_REQUEST
;
884 c
->read_data_length
= 0;
886 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
889 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
892 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
894 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->scache
.memchunk
.memblock
->data
+c
->scache
.memchunk
.index
, c
->scache
.memchunk
.length
-c
->scache
.memchunk
.index
)) <= 0) {
895 pa_log_debug(__FILE__
": read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
899 c
->scache
.memchunk
.index
+= r
;
900 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
902 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
905 c
->scache
.memchunk
.index
= 0;
906 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, &idx
);
908 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
909 c
->scache
.memchunk
.memblock
= NULL
;
910 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
912 pa_xfree(c
->scache
.name
);
913 c
->scache
.name
= NULL
;
915 c
->state
= ESD_NEXT_REQUEST
;
918 connection_write(c
, &idx
, sizeof(uint32_t));
921 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
926 assert(c
->input_memblockq
);
928 /* pa_log("STREAMING_DATA"); */
930 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
933 if (l
> c
->playback
.fragment_size
)
934 l
= c
->playback
.fragment_size
;
936 if (c
->playback
.current_memblock
)
937 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
938 pa_memblock_unref(c
->playback
.current_memblock
);
939 c
->playback
.current_memblock
= NULL
;
940 c
->playback
.memblock_index
= 0;
943 if (!c
->playback
.current_memblock
) {
944 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
945 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
946 c
->playback
.memblock_index
= 0;
949 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
950 pa_log_debug(__FILE__
": read(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
954 chunk
.memblock
= c
->playback
.current_memblock
;
955 chunk
.index
= c
->playback
.memblock_index
;
957 assert(chunk
.memblock
);
959 c
->playback
.memblock_index
+= r
;
961 assert(c
->input_memblockq
);
962 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
963 assert(c
->sink_input
);
964 pa_sink_notify(c
->sink_input
->sink
);
970 static int do_write(struct connection
*c
) {
973 /* pa_log("WRITE"); */
975 if (c
->write_data_length
) {
978 assert(c
->write_data_index
< c
->write_data_length
);
979 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) {
980 pa_log(__FILE__
": write(): %s", pa_cstrerror(errno
));
984 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
985 c
->write_data_length
= c
->write_data_index
= 0;
987 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
991 assert(c
->output_memblockq
);
992 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
995 assert(chunk
.memblock
&& chunk
.length
);
997 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
998 pa_memblock_unref(chunk
.memblock
);
999 pa_log(__FILE__
": write(): %s", pa_cstrerror(errno
));
1003 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
1004 pa_memblock_unref(chunk
.memblock
);
1006 pa_source_notify(c
->source_output
->source
);
1012 static void do_work(struct connection
*c
) {
1015 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1016 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1021 if (pa_iochannel_is_readable(c
->io
)) {
1026 if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
&& pa_iochannel_is_hungup(c
->io
))
1027 /* In case we are in capture mode we will never call read()
1028 * on the socket, hence we need to detect the hangup manually
1029 * here, instead of simply waiting for read() to return 0. */
1032 if (pa_iochannel_is_writable(c
->io
))
1033 if (do_write(c
) < 0)
1040 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1043 pa_iochannel_free(c
->io
);
1046 pa_memblockq_prebuf_disable(c
->input_memblockq
);
1047 pa_sink_notify(c
->sink_input
->sink
);
1052 static void io_callback(pa_iochannel
*io
, void *userdata
) {
1053 struct connection
*c
= userdata
;
1054 assert(io
&& c
&& c
->io
== io
);
1059 /*** defer callback ***/
1061 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
1062 struct connection
*c
= userdata
;
1063 assert(a
&& c
&& c
->defer_event
== e
);
1065 /* pa_log("DEFER"); */
1070 /*** sink_input callbacks ***/
1072 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
1073 struct connection
*c
;
1074 assert(i
&& i
->userdata
&& chunk
);
1077 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1088 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
1089 struct connection
*c
= i
->userdata
;
1090 assert(i
&& c
&& length
);
1092 /* pa_log("DROP"); */
1094 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
1097 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1100 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1102 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1105 static void sink_input_kill_cb(pa_sink_input
*i
) {
1106 assert(i
&& i
->userdata
);
1107 connection_free((struct connection
*) i
->userdata
);
1110 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
1111 struct connection
*c
= i
->userdata
;
1113 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1116 /*** source_output callbacks ***/
1118 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1119 struct connection
*c
= o
->userdata
;
1120 assert(o
&& c
&& chunk
);
1122 pa_memblockq_push(c
->output_memblockq
, chunk
);
1125 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1128 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1131 static void source_output_kill_cb(pa_source_output
*o
) {
1132 assert(o
&& o
->userdata
);
1133 connection_free((struct connection
*) o
->userdata
);
1136 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1137 struct connection
*c
= o
->userdata
;
1139 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1142 /*** socket server callback ***/
1144 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1145 struct connection
*c
= userdata
;
1146 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1152 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1153 struct connection
*c
;
1154 pa_protocol_esound
*p
= userdata
;
1155 char cname
[256], pname
[128];
1156 assert(s
&& io
&& p
);
1158 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1159 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1160 pa_iochannel_free(io
);
1164 c
= pa_xnew(struct connection
, 1);
1167 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1169 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
1170 snprintf(cname
, sizeof(cname
), "EsounD client (%s)", pname
);
1172 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1174 c
->client
->owner
= p
->module
;
1175 c
->client
->kill
= client_kill_cb
;
1176 c
->client
->userdata
= c
;
1178 c
->authorized
= !!p
->public;
1179 c
->swap_byte_order
= 0;
1182 c
->read_data_length
= 0;
1183 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1185 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1186 c
->write_data
= NULL
;
1188 c
->state
= ESD_NEEDS_REQDATA
;
1189 c
->request
= ESD_PROTO_CONNECT
;
1191 c
->sink_input
= NULL
;
1192 c
->input_memblockq
= NULL
;
1194 c
->source_output
= NULL
;
1195 c
->output_memblockq
= NULL
;
1197 c
->playback
.current_memblock
= NULL
;
1198 c
->playback
.memblock_index
= 0;
1199 c
->playback
.fragment_size
= 0;
1201 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1202 c
->scache
.memchunk
.memblock
= NULL
;
1203 c
->scache
.name
= NULL
;
1205 c
->original_name
= NULL
;
1207 if (!c
->authorized
&& p
->auth_ip_acl
&& pa_ip_acl_check(p
->auth_ip_acl
, pa_iochannel_get_recv_fd(io
)) > 0) {
1208 pa_log_info(__FILE__
": Client authenticated by IP ACL.");
1212 if (!c
->authorized
) {
1214 pa_gettimeofday(&tv
);
1215 tv
.tv_sec
+= AUTH_TIMEOUT
;
1216 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1218 c
->auth_timeout_event
= NULL
;
1220 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1221 assert(c
->defer_event
);
1222 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1224 pa_idxset_put(p
->connections
, c
, &c
->index
);
1227 /*** entry points ***/
1229 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1230 pa_protocol_esound
*p
;
1239 p
= pa_xnew(pa_protocol_esound
, 1);
1241 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
1242 pa_log(__FILE__
": auth-anonymous= expects a boolean argument.");
1246 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0)
1249 if ((acl
= pa_modargs_get_value(ma
, "auth-ip-acl", NULL
))) {
1251 if (!(p
->auth_ip_acl
= pa_ip_acl_new(acl
))) {
1252 pa_log(__FILE__
": Failed to parse IP ACL '%s'", acl
);
1256 p
->auth_ip_acl
= NULL
;
1261 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1263 p
->connections
= pa_idxset_new(NULL
, NULL
);
1264 assert(p
->connections
);
1266 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1267 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1277 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1278 struct connection
*c
;
1281 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1284 pa_idxset_free(p
->connections
, NULL
, NULL
);
1285 pa_socket_server_unref(p
->server
);
1288 pa_ip_acl_free(p
->auth_ip_acl
);