4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <polyp/sample.h>
34 #include <polypcore/esound.h>
35 #include <polypcore/memblock.h>
36 #include <polypcore/client.h>
37 #include <polypcore/sink-input.h>
38 #include <polypcore/sink.h>
39 #include <polypcore/source-output.h>
40 #include <polypcore/source.h>
41 #include <polypcore/core-scache.h>
42 #include <polypcore/sample-util.h>
43 #include <polypcore/authkey.h>
44 #include <polypcore/namereg.h>
45 #include <polypcore/xmalloc.h>
46 #include <polypcore/log.h>
47 #include <polypcore/util.h>
49 #include "endianmacros.h"
51 #include "protocol-esound.h"
53 /* Don't accept more connection than this */
54 #define MAX_CONNECTIONS 10
56 /* Kick a client if it doesn't authenticate within this time */
57 #define AUTH_TIMEOUT 5
59 #define DEFAULT_COOKIE_FILE ".esd_auth"
61 #define PLAYBACK_BUFFER_SECONDS (.25)
62 #define PLAYBACK_BUFFER_FRAGMENTS (10)
63 #define RECORD_BUFFER_SECONDS (5)
64 #define RECORD_BUFFER_FRAGMENTS (100)
66 #define MAX_CACHE_SAMPLE_SIZE (1024000)
68 #define SCACHE_PREFIX "esound."
70 /* This is heavily based on esound's code */
75 pa_protocol_esound
*protocol
;
78 int authorized
, swap_byte_order
;
80 size_t write_data_alloc
, write_data_index
, write_data_length
;
82 size_t read_data_alloc
, read_data_length
;
84 esd_client_state_t state
;
85 pa_sink_input
*sink_input
;
86 pa_source_output
*source_output
;
87 pa_memblockq
*input_memblockq
, *output_memblockq
;
88 pa_defer_event
*defer_event
;
91 pa_memblock
*current_memblock
;
92 size_t memblock_index
, fragment_size
;
98 pa_sample_spec sample_spec
;
101 pa_time_event
*auth_timeout_event
;
104 struct pa_protocol_esound
{
108 pa_socket_server
*server
;
109 pa_idxset
*connections
;
110 char *sink_name
, *source_name
;
112 uint8_t esd_key
[ESD_KEY_LEN
];
115 typedef struct proto_handler
{
117 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
118 const char *description
;
119 } esd_proto_handler_info_t
;
121 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
);
122 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
);
123 static void sink_input_kill_cb(pa_sink_input
*i
);
124 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
);
125 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
);
127 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
);
128 static void source_output_kill_cb(pa_source_output
*o
);
130 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
131 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
132 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
133 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
134 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
135 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
136 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
137 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
138 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
139 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
140 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
142 /* the big map of protocol handler info */
143 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
144 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
145 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
146 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
148 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
149 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
150 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
152 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
153 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
154 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
155 { sizeof(int), NULL
, "sample loop" },
156 { sizeof(int), NULL
, "sample stop" },
157 { -1, NULL
, "TODO: sample kill" },
159 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
160 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
162 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
163 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
165 { sizeof(int), esd_proto_server_info
, "server info" },
166 { sizeof(int), esd_proto_all_info
, "all info" },
167 { -1, NULL
, "TODO: subscribe" },
168 { -1, NULL
, "TODO: unsubscribe" },
170 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
171 { 3 * sizeof(int), NULL
, "sample pan" },
173 { sizeof(int), NULL
, "standby mode" },
174 { 0, esd_proto_get_latency
, "get latency" }
178 static void connection_free(struct connection
*c
) {
180 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
182 if (c
->state
== ESD_STREAMING_DATA
)
183 c
->protocol
->n_player
--;
185 pa_client_free(c
->client
);
188 pa_sink_input_disconnect(c
->sink_input
);
189 pa_sink_input_unref(c
->sink_input
);
192 if (c
->source_output
) {
193 pa_source_output_disconnect(c
->source_output
);
194 pa_source_output_unref(c
->source_output
);
197 if (c
->input_memblockq
)
198 pa_memblockq_free(c
->input_memblockq
);
199 if (c
->output_memblockq
)
200 pa_memblockq_free(c
->output_memblockq
);
202 if (c
->playback
.current_memblock
)
203 pa_memblock_unref(c
->playback
.current_memblock
);
205 pa_xfree(c
->read_data
);
206 pa_xfree(c
->write_data
);
209 pa_iochannel_free(c
->io
);
212 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
214 if (c
->scache
.memchunk
.memblock
)
215 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
216 pa_xfree(c
->scache
.name
);
218 if (c
->auth_timeout_event
)
219 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
224 static void connection_write_prepare(struct connection
*c
, size_t length
) {
228 t
= c
->write_data_length
+length
;
230 if (c
->write_data_alloc
< t
)
231 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
233 assert(c
->write_data
);
236 static void connection_write(struct connection
*c
, const void *data
, size_t length
) {
240 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
241 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
243 connection_write_prepare(c
, length
);
245 assert(c
->write_data
);
247 i
= c
->write_data_length
;
248 c
->write_data_length
+= length
;
250 memcpy((char*)c
->write_data
+ i
, data
, length
);
253 static void format_esd2native(int format
, int swap_bytes
, pa_sample_spec
*ss
) {
256 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
257 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
258 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
260 ss
->format
= PA_SAMPLE_U8
;
263 static int format_native2esd(pa_sample_spec
*ss
) {
266 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
267 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
272 #define CHECK_VALIDITY(expression, string) do { \
273 if (!(expression)) { \
274 pa_log_warn(__FILE__ ": " string); \
279 /*** esound commands ***/
281 static int esd_proto_connect(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
285 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
287 if (!c
->authorized
) {
288 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
289 pa_log(__FILE__
": kicked client with invalid authorization key.");
294 if (c
->auth_timeout_event
) {
295 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
296 c
->auth_timeout_event
= NULL
;
300 data
= (const char*)data
+ ESD_KEY_LEN
;
302 memcpy(&ekey
, data
, sizeof(uint32_t));
303 if (ekey
== ESD_ENDIAN_KEY
)
304 c
->swap_byte_order
= 0;
305 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
306 c
->swap_byte_order
= 1;
308 pa_log(__FILE__
": client sent invalid endian key");
313 connection_write(c
, &ok
, sizeof(int));
317 static int esd_proto_stream_play(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
318 char name
[ESD_NAME_MAX
];
319 int32_t format
, rate
;
324 assert(c
&& length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
326 memcpy(&format
, data
, sizeof(int32_t));
327 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
328 data
= (const char*)data
+ sizeof(int32_t);
330 memcpy(&rate
, data
, sizeof(int32_t));
331 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
332 data
= (const char*)data
+ sizeof(int32_t);
335 format_esd2native(format
, c
->swap_byte_order
, &ss
);
337 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
338 sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1);
339 CHECK_VALIDITY(sink
, "No such sink");
341 strncpy(name
, data
, sizeof(name
));
342 name
[sizeof(name
)-1] = 0;
344 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in stream name");
346 pa_client_set_name(c
->client
, name
);
348 assert(!c
->sink_input
&& !c
->input_memblockq
);
350 c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, &ss
, NULL
, NULL
, 0, -1);
352 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
354 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
355 c
->input_memblockq
= pa_memblockq_new(
361 l
/PLAYBACK_BUFFER_FRAGMENTS
,
363 c
->protocol
->core
->memblock_stat
);
364 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
365 c
->playback
.fragment_size
= l
/10;
367 c
->sink_input
->owner
= c
->protocol
->module
;
368 c
->sink_input
->client
= c
->client
;
369 c
->sink_input
->peek
= sink_input_peek_cb
;
370 c
->sink_input
->drop
= sink_input_drop_cb
;
371 c
->sink_input
->kill
= sink_input_kill_cb
;
372 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
373 c
->sink_input
->userdata
= c
;
375 c
->state
= ESD_STREAMING_DATA
;
377 c
->protocol
->n_player
++;
382 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
383 char name
[ESD_NAME_MAX
];
384 int32_t format
, rate
;
389 assert(c
&& length
== (sizeof(int32_t)*2+ESD_NAME_MAX
));
391 memcpy(&format
, data
, sizeof(int32_t));
392 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
393 data
= (const char*)data
+ sizeof(int32_t);
395 memcpy(&rate
, data
, sizeof(int32_t));
396 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
397 data
= (const char*)data
+ sizeof(int32_t);
400 format_esd2native(format
, c
->swap_byte_order
, &ss
);
402 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
404 if (request
== ESD_PROTO_STREAM_MON
) {
407 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
408 pa_log(__FILE__
": no such sink.");
412 if (!(source
= sink
->monitor_source
)) {
413 pa_log(__FILE__
": no such monitor source.");
417 assert(request
== ESD_PROTO_STREAM_REC
);
419 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
420 pa_log(__FILE__
": no such source.");
425 strncpy(name
, data
, sizeof(name
));
426 name
[sizeof(name
)-1] = 0;
428 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in stream name.");
430 pa_client_set_name(c
->client
, name
);
432 assert(!c
->output_memblockq
&& !c
->source_output
);
434 if (!(c
->source_output
= pa_source_output_new(source
, __FILE__
, name
, &ss
, NULL
, -1))) {
435 pa_log(__FILE__
": failed to create source output");
439 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
440 c
->output_memblockq
= pa_memblockq_new(
448 c
->protocol
->core
->memblock_stat
);
449 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
451 c
->source_output
->owner
= c
->protocol
->module
;
452 c
->source_output
->client
= c
->client
;
453 c
->source_output
->push
= source_output_push_cb
;
454 c
->source_output
->kill
= source_output_kill_cb
;
455 c
->source_output
->get_latency
= source_output_get_latency_cb
;
456 c
->source_output
->userdata
= c
;
458 c
->state
= ESD_STREAMING_DATA
;
460 c
->protocol
->n_player
++;
465 static int esd_proto_get_latency(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
469 assert(c
&& !data
&& length
== 0);
471 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
474 double usec
= pa_sink_get_latency(sink
);
475 latency
= (int) ((usec
*44100)/1000000);
478 latency
= MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
479 connection_write(c
, &latency
, sizeof(int32_t));
483 static int esd_proto_server_info(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
484 int32_t rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
488 assert(c
&& data
&& length
== sizeof(int32_t));
490 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
491 rate
= sink
->sample_spec
.rate
;
492 format
= format_native2esd(&sink
->sample_spec
);
495 connection_write_prepare(c
, sizeof(int32_t) * 3);
498 connection_write(c
, &response
, sizeof(int32_t));
499 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
500 connection_write(c
, &rate
, sizeof(int32_t));
501 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
502 connection_write(c
, &format
, sizeof(int32_t));
507 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
510 struct connection
*conn
;
511 uint32_t idx
= PA_IDXSET_INVALID
;
513 char terminator
[sizeof(int32_t)*6+ESD_NAME_MAX
];
515 assert(c
&& data
&& length
== sizeof(int32_t));
517 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
520 k
= sizeof(int32_t)*5+ESD_NAME_MAX
;
521 s
= sizeof(int32_t)*6+ESD_NAME_MAX
;
522 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
523 t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1);
525 connection_write_prepare(c
, t
);
527 memset(terminator
, 0, sizeof(terminator
));
529 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
530 int32_t id
, format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
531 char name
[ESD_NAME_MAX
];
533 if (conn
->state
!= ESD_STREAMING_DATA
)
538 if (conn
->sink_input
) {
539 pa_cvolume volume
= *pa_sink_input_get_volume(conn
->sink_input
);
540 rate
= conn
->sink_input
->sample_spec
.rate
;
541 lvolume
= (volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
542 rvolume
= (volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
543 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
547 id
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int32_t) (conn
->index
+1));
548 connection_write(c
, &id
, sizeof(int32_t));
551 assert(conn
->client
);
552 strncpy(name
, conn
->client
->name
, ESD_NAME_MAX
);
553 connection_write(c
, name
, ESD_NAME_MAX
);
556 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
557 connection_write(c
, &rate
, sizeof(int32_t));
560 lvolume
= MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
561 connection_write(c
, &lvolume
, sizeof(int32_t));
564 rvolume
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
565 connection_write(c
, &rvolume
, sizeof(int32_t));
568 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
569 connection_write(c
, &format
, sizeof(int32_t));
574 assert(t
== s
*(nsamples
+1)+k
);
578 connection_write(c
, terminator
, k
);
583 idx
= PA_IDXSET_INVALID
;
584 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
585 int32_t id
, rate
, lvolume
, rvolume
, format
, len
;
586 char name
[ESD_NAME_MAX
];
591 id
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
592 connection_write(c
, &id
, sizeof(int32_t));
595 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
596 strncpy(name
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
598 snprintf(name
, ESD_NAME_MAX
, "native.%s", ce
->name
);
599 connection_write(c
, name
, ESD_NAME_MAX
);
602 rate
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
603 connection_write(c
, &rate
, sizeof(int32_t));
606 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
607 connection_write(c
, &lvolume
, sizeof(int32_t));
610 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
611 connection_write(c
, &rvolume
, sizeof(int32_t));
614 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
615 connection_write(c
, &format
, sizeof(int32_t));
618 len
= MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
619 connection_write(c
, &len
, sizeof(int32_t));
627 connection_write(c
, terminator
, s
);
632 static int esd_proto_stream_pan(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
634 uint32_t idx
, lvolume
, rvolume
;
635 struct connection
*conn
;
637 assert(c
&& data
&& length
== sizeof(int32_t)*3);
639 memcpy(&idx
, data
, sizeof(uint32_t));
640 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
641 data
= (const char*)data
+ sizeof(uint32_t);
643 memcpy(&lvolume
, data
, sizeof(uint32_t));
644 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, lvolume
);
645 data
= (const char*)data
+ sizeof(uint32_t);
647 memcpy(&rvolume
, data
, sizeof(uint32_t));
648 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, rvolume
);
649 data
= (const char*)data
+ sizeof(uint32_t);
651 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
)) && conn
->sink_input
) {
653 volume
.values
[0] = (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
654 volume
.values
[1] = (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
656 pa_sink_input_set_volume(conn
->sink_input
, &volume
);
661 connection_write(c
, &ok
, sizeof(int32_t));
666 static int esd_proto_sample_cache(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
668 int32_t format
, rate
, sc_length
;
670 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
672 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int32_t)));
674 memcpy(&format
, data
, sizeof(int32_t));
675 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
676 data
= (const char*)data
+ sizeof(int32_t);
678 memcpy(&rate
, data
, sizeof(int32_t));
679 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
680 data
= (const char*)data
+ sizeof(int32_t);
683 format_esd2native(format
, c
->swap_byte_order
, &ss
);
685 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
687 memcpy(&sc_length
, data
, sizeof(int32_t));
688 sc_length
= MAYBE_INT32_SWAP(c
->swap_byte_order
, sc_length
);
689 data
= (const char*)data
+ sizeof(int32_t);
691 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large.");
693 strcpy(name
, SCACHE_PREFIX
);
694 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
695 name
[sizeof(name
)-1] = 0;
697 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
699 assert(!c
->scache
.memchunk
.memblock
);
700 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
701 c
->scache
.memchunk
.index
= 0;
702 c
->scache
.memchunk
.length
= sc_length
;
703 c
->scache
.sample_spec
= ss
;
704 assert(!c
->scache
.name
);
705 c
->scache
.name
= pa_xstrdup(name
);
707 c
->state
= ESD_CACHING_SAMPLE
;
709 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, &idx
);
712 connection_write(c
, &idx
, sizeof(uint32_t));
717 static int esd_proto_sample_get_id(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
720 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
722 assert(c
&& data
&& length
== ESD_NAME_MAX
);
724 strcpy(name
, SCACHE_PREFIX
);
725 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
726 name
[sizeof(name
)-1] = 0;
728 CHECK_VALIDITY(pa_utf8_valid(name
), "Invalid UTF8 in sample name.");
731 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
734 connection_write(c
, &ok
, sizeof(int32_t));
739 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
744 assert(c
&& data
&& length
== sizeof(int32_t));
746 memcpy(&idx
, data
, sizeof(uint32_t));
747 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, idx
) - 1;
751 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
752 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
755 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
756 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
759 assert(request
== ESD_PROTO_SAMPLE_FREE
);
761 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
766 connection_write(c
, &ok
, sizeof(int32_t));
771 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
) {
774 connection_write_prepare(c
, sizeof(int32_t) * 2);
777 connection_write(c
, &ok
, sizeof(int32_t));
778 connection_write(c
, &ok
, sizeof(int32_t));
783 /*** client callbacks ***/
785 static void client_kill_cb(pa_client
*c
) {
786 assert(c
&& c
->userdata
);
787 connection_free(c
->userdata
);
790 /*** pa_iochannel callbacks ***/
792 static int do_read(struct connection
*c
) {
795 /* pa_log("READ"); */
797 if (c
->state
== ESD_NEXT_REQUEST
) {
799 assert(c
->read_data_length
< sizeof(c
->request
));
801 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
802 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
806 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
807 struct proto_handler
*handler
;
809 c
->request
= MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
811 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
812 pa_log(__FILE__
": recieved invalid request.");
816 handler
= proto_map
+c
->request
;
818 /* pa_log(__FILE__": executing request #%u", c->request); */
820 if (!handler
->proc
) {
821 pa_log(__FILE__
": recieved unimplemented request #%u.", c
->request
);
825 if (handler
->data_length
== 0) {
826 c
->read_data_length
= 0;
828 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
832 if (c
->read_data_alloc
< handler
->data_length
)
833 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
834 assert(c
->read_data
);
836 c
->state
= ESD_NEEDS_REQDATA
;
837 c
->read_data_length
= 0;
841 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
843 struct proto_handler
*handler
= proto_map
+c
->request
;
845 assert(handler
->proc
);
847 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
849 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
850 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
854 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
855 size_t l
= c
->read_data_length
;
856 assert(handler
->proc
);
858 c
->state
= ESD_NEXT_REQUEST
;
859 c
->read_data_length
= 0;
861 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
864 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
867 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
869 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) {
870 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
874 c
->scache
.memchunk
.index
+= r
;
875 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
877 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
880 c
->scache
.memchunk
.index
= 0;
881 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, &idx
);
883 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
884 c
->scache
.memchunk
.memblock
= NULL
;
885 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
887 pa_xfree(c
->scache
.name
);
888 c
->scache
.name
= NULL
;
890 c
->state
= ESD_NEXT_REQUEST
;
893 connection_write(c
, &idx
, sizeof(uint32_t));
896 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
901 assert(c
->input_memblockq
);
903 /* pa_log("STREAMING_DATA"); */
905 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
908 if (l
> c
->playback
.fragment_size
)
909 l
= c
->playback
.fragment_size
;
911 if (c
->playback
.current_memblock
)
912 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
913 pa_memblock_unref(c
->playback
.current_memblock
);
914 c
->playback
.current_memblock
= NULL
;
915 c
->playback
.memblock_index
= 0;
918 if (!c
->playback
.current_memblock
) {
919 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
920 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
921 c
->playback
.memblock_index
= 0;
924 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
925 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
929 chunk
.memblock
= c
->playback
.current_memblock
;
930 chunk
.index
= c
->playback
.memblock_index
;
932 assert(chunk
.memblock
);
934 c
->playback
.memblock_index
+= r
;
936 assert(c
->input_memblockq
);
937 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
938 assert(c
->sink_input
);
939 pa_sink_notify(c
->sink_input
->sink
);
945 static int do_write(struct connection
*c
) {
948 /* pa_log("WRITE"); */
950 if (c
->write_data_length
) {
953 assert(c
->write_data_index
< c
->write_data_length
);
954 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) {
955 pa_log(__FILE__
": write() failed: %s", strerror(errno
));
959 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
960 c
->write_data_length
= c
->write_data_index
= 0;
962 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
966 assert(c
->output_memblockq
);
967 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
970 assert(chunk
.memblock
&& chunk
.length
);
972 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
973 pa_memblock_unref(chunk
.memblock
);
974 pa_log(__FILE__
": write(): %s", strerror(errno
));
978 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
979 pa_memblock_unref(chunk
.memblock
);
981 pa_source_notify(c
->source_output
->source
);
987 static void do_work(struct connection
*c
) {
990 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
991 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
996 if (pa_iochannel_is_readable(c
->io
)) {
1001 if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
&& pa_iochannel_is_hungup(c
->io
))
1002 /* In case we are in capture mode we will never call read()
1003 * on the socket, hence we need to detect the hangup manually
1004 * here, instead of simply waiting for read() to return 0. */
1007 if (pa_iochannel_is_writable(c
->io
))
1008 if (do_write(c
) < 0)
1015 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
1018 pa_iochannel_free(c
->io
);
1021 pa_memblockq_prebuf_disable(c
->input_memblockq
);
1022 pa_sink_notify(c
->sink_input
->sink
);
1027 static void io_callback(pa_iochannel
*io
, void *userdata
) {
1028 struct connection
*c
= userdata
;
1029 assert(io
&& c
&& c
->io
== io
);
1034 /*** defer callback ***/
1036 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
1037 struct connection
*c
= userdata
;
1038 assert(a
&& c
&& c
->defer_event
== e
);
1040 /* pa_log("DEFER"); */
1045 /*** sink_input callbacks ***/
1047 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
1048 struct connection
*c
;
1049 assert(i
&& i
->userdata
&& chunk
);
1052 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1063 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
1064 struct connection
*c
= i
->userdata
;
1065 assert(i
&& c
&& length
);
1067 /* pa_log("DROP"); */
1069 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
1072 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1075 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1077 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1080 static void sink_input_kill_cb(pa_sink_input
*i
) {
1081 assert(i
&& i
->userdata
);
1082 connection_free((struct connection
*) i
->userdata
);
1085 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
1086 struct connection
*c
= i
->userdata
;
1088 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1091 /*** source_output callbacks ***/
1093 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1094 struct connection
*c
= o
->userdata
;
1095 assert(o
&& c
&& chunk
);
1097 pa_memblockq_push(c
->output_memblockq
, chunk
);
1100 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1103 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1106 static void source_output_kill_cb(pa_source_output
*o
) {
1107 assert(o
&& o
->userdata
);
1108 connection_free((struct connection
*) o
->userdata
);
1111 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1112 struct connection
*c
= o
->userdata
;
1114 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1117 /*** socket server callback ***/
1119 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1120 struct connection
*c
= userdata
;
1121 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1127 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1128 struct connection
*c
;
1129 pa_protocol_esound
*p
= userdata
;
1131 assert(s
&& io
&& p
);
1133 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1134 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1135 pa_iochannel_free(io
);
1139 c
= pa_xnew(struct connection
, 1);
1142 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1144 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1146 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1148 c
->client
->owner
= p
->module
;
1149 c
->client
->kill
= client_kill_cb
;
1150 c
->client
->userdata
= c
;
1152 c
->authorized
= p
->public;
1153 c
->swap_byte_order
= 0;
1156 c
->read_data_length
= 0;
1157 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1159 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1160 c
->write_data
= NULL
;
1162 c
->state
= ESD_NEEDS_REQDATA
;
1163 c
->request
= ESD_PROTO_CONNECT
;
1165 c
->sink_input
= NULL
;
1166 c
->input_memblockq
= NULL
;
1168 c
->source_output
= NULL
;
1169 c
->output_memblockq
= NULL
;
1171 c
->playback
.current_memblock
= NULL
;
1172 c
->playback
.memblock_index
= 0;
1173 c
->playback
.fragment_size
= 0;
1175 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1176 c
->scache
.memchunk
.memblock
= NULL
;
1177 c
->scache
.name
= NULL
;
1179 if (!c
->authorized
) {
1181 pa_gettimeofday(&tv
);
1182 tv
.tv_sec
+= AUTH_TIMEOUT
;
1183 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1185 c
->auth_timeout_event
= NULL
;
1187 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1188 assert(c
->defer_event
);
1189 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1191 pa_idxset_put(p
->connections
, c
, &c
->index
);
1194 /*** entry points ***/
1196 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1197 pa_protocol_esound
*p
;
1199 assert(core
&& server
&& ma
);
1201 p
= pa_xnew(pa_protocol_esound
, 1);
1203 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
1204 pa_log(__FILE__
": auth-anonymous= expects a boolean argument.");
1208 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1216 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1218 p
->connections
= pa_idxset_new(NULL
, NULL
);
1219 assert(p
->connections
);
1221 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1222 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1228 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1229 struct connection
*c
;
1232 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1235 pa_idxset_free(p
->connections
, NULL
, NULL
);
1236 pa_socket_server_unref(p
->server
);