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 (.5)
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_log("disconnect\n");
190 pa_sink_input_unref(c
->sink_input
);
193 if (c
->source_output
) {
194 pa_source_output_disconnect(c
->source_output
);
195 pa_source_output_unref(c
->source_output
);
198 if (c
->input_memblockq
)
199 pa_memblockq_free(c
->input_memblockq
);
200 if (c
->output_memblockq
)
201 pa_memblockq_free(c
->output_memblockq
);
203 if (c
->playback
.current_memblock
)
204 pa_memblock_unref(c
->playback
.current_memblock
);
206 pa_xfree(c
->read_data
);
207 pa_xfree(c
->write_data
);
210 pa_iochannel_free(c
->io
);
213 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
215 if (c
->scache
.memchunk
.memblock
)
216 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
217 pa_xfree(c
->scache
.name
);
219 if (c
->auth_timeout_event
)
220 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
225 static void* connection_write(struct connection
*c
, size_t length
) {
229 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
230 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
232 t
= c
->write_data_length
+length
;
234 if (c
->write_data_alloc
< t
)
235 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
237 assert(c
->write_data
);
239 i
= c
->write_data_length
;
240 c
->write_data_length
+= length
;
242 return (uint8_t*) c
->write_data
+i
;
245 static void format_esd2native(int format
, int swap_bytes
, pa_sample_spec
*ss
) {
248 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
249 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
250 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
252 ss
->format
= PA_SAMPLE_U8
;
255 static int format_native2esd(pa_sample_spec
*ss
) {
258 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
259 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
264 /*** esound commands ***/
266 static int esd_proto_connect(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
269 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
271 if (!c
->authorized
) {
272 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
273 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
278 if (c
->auth_timeout_event
) {
279 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
280 c
->auth_timeout_event
= NULL
;
284 ekey
= *(const uint32_t*)((const uint8_t*) data
+ESD_KEY_LEN
);
285 if (ekey
== ESD_ENDIAN_KEY
)
286 c
->swap_byte_order
= 0;
287 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
288 c
->swap_byte_order
= 1;
290 pa_log(__FILE__
": client sent invalid endian key\n");
294 ok
= connection_write(c
, sizeof(int));
300 static int esd_proto_stream_play(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
301 char name
[ESD_NAME_MAX
];
306 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
308 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
309 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
312 format_esd2native(format
, c
->swap_byte_order
, &ss
);
314 if (!pa_sample_spec_valid(&ss
)) {
315 pa_log(__FILE__
": invalid sample specification\n");
319 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
320 pa_log(__FILE__
": no such sink\n");
324 strncpy(name
, (const char*) data
+ sizeof(int)*2, sizeof(name
));
325 name
[sizeof(name
)-1] = 0;
327 pa_client_set_name(c
->client
, name
);
329 assert(!c
->sink_input
&& !c
->input_memblockq
);
331 if (!(c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, &ss
, NULL
, 0, -1))) {
332 pa_log(__FILE__
": failed to create sink input.\n");
336 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
337 c
->input_memblockq
= pa_memblockq_new(
343 l
/PLAYBACK_BUFFER_FRAGMENTS
,
345 c
->protocol
->core
->memblock_stat
);
346 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
347 c
->playback
.fragment_size
= l
/10;
349 c
->sink_input
->owner
= c
->protocol
->module
;
350 c
->sink_input
->client
= c
->client
;
351 c
->sink_input
->peek
= sink_input_peek_cb
;
352 c
->sink_input
->drop
= sink_input_drop_cb
;
353 c
->sink_input
->kill
= sink_input_kill_cb
;
354 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
355 c
->sink_input
->userdata
= c
;
357 c
->state
= ESD_STREAMING_DATA
;
359 c
->protocol
->n_player
++;
364 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
365 char name
[ESD_NAME_MAX
];
370 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
372 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
373 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
376 format_esd2native(format
, c
->swap_byte_order
, &ss
);
378 if (!pa_sample_spec_valid(&ss
)) {
379 pa_log(__FILE__
": invalid sample specification.\n");
383 if (request
== ESD_PROTO_STREAM_MON
) {
386 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
387 pa_log(__FILE__
": no such sink.\n");
391 if (!(source
= sink
->monitor_source
)) {
392 pa_log(__FILE__
": no such monitor source.\n");
396 assert(request
== ESD_PROTO_STREAM_REC
);
398 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
399 pa_log(__FILE__
": no such source.\n");
404 strncpy(name
, (const char*) data
+ sizeof(int)*2, sizeof(name
));
405 name
[sizeof(name
)-1] = 0;
407 pa_client_set_name(c
->client
, name
);
409 assert(!c
->output_memblockq
&& !c
->source_output
);
411 if (!(c
->source_output
= pa_source_output_new(source
, __FILE__
, name
, &ss
, NULL
, -1))) {
412 pa_log(__FILE__
": failed to create source output\n");
416 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
417 c
->output_memblockq
= pa_memblockq_new(
425 c
->protocol
->core
->memblock_stat
);
426 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
428 c
->source_output
->owner
= c
->protocol
->module
;
429 c
->source_output
->client
= c
->client
;
430 c
->source_output
->push
= source_output_push_cb
;
431 c
->source_output
->kill
= source_output_kill_cb
;
432 c
->source_output
->get_latency
= source_output_get_latency_cb
;
433 c
->source_output
->userdata
= c
;
435 c
->state
= ESD_STREAMING_DATA
;
437 c
->protocol
->n_player
++;
442 static int esd_proto_get_latency(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
445 assert(c
&& !data
&& length
== 0);
447 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
450 double usec
= pa_sink_get_latency(sink
);
451 latency
= (int) ((usec
*44100)/1000000);
454 lag
= connection_write(c
, sizeof(int));
456 *lag
= MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
460 static int esd_proto_server_info(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
461 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
464 assert(c
&& data
&& length
== sizeof(int));
466 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
467 rate
= sink
->sample_spec
.rate
;
468 format
= format_native2esd(&sink
->sample_spec
);
471 response
= connection_write(c
, sizeof(int)*3);
474 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
475 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
479 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
482 struct connection
*conn
;
483 size_t idx
= PA_IDXSET_INVALID
;
485 assert(c
&& data
&& length
== sizeof(int));
487 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
490 k
= sizeof(int)*5+ESD_NAME_MAX
;
491 s
= sizeof(int)*6+ESD_NAME_MAX
;
492 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
493 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
496 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
497 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
499 if (conn
->state
!= ESD_STREAMING_DATA
)
504 if (conn
->sink_input
) {
505 rate
= conn
->sink_input
->sample_spec
.rate
;
506 lvolume
= (conn
->sink_input
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
507 rvolume
= (conn
->sink_input
->volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
508 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
512 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (conn
->index
+1));
513 response
+= sizeof(int);
516 assert(conn
->client
);
517 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
518 response
+= ESD_NAME_MAX
;
521 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
522 response
+= sizeof(int);
525 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
526 response
+= sizeof(int);
529 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
530 response
+= sizeof(int);
533 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
534 response
+= sizeof(int);
539 assert(t
== s
*(nsamples
+1)+k
);
540 memset(response
, 0, k
);
547 idx
= PA_IDXSET_INVALID
;
548 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
552 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
553 response
+= sizeof(int);
556 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
557 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
559 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
560 response
+= ESD_NAME_MAX
;
563 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
564 response
+= sizeof(int);
567 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
568 response
+= sizeof(int);
571 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
572 response
+= sizeof(int);
575 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
576 response
+= sizeof(int);
579 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
580 response
+= sizeof(int);
587 memset(response
, 0, s
);
592 static int esd_proto_stream_pan(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
595 pa_volume_t lvolume
, rvolume
;
596 struct connection
*conn
;
597 assert(c
&& data
&& length
== sizeof(int)*3);
599 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *(const uint32_t*)data
)-1;
600 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 1));
601 lvolume
= (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
602 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 2));
603 rvolume
= (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
605 ok
= connection_write(c
, sizeof(int));
608 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
))) {
609 assert(conn
->sink_input
);
610 conn
->sink_input
->volume
.values
[0] = lvolume
;
611 conn
->sink_input
->volume
.values
[1] = rvolume
;
612 conn
->sink_input
->volume
.channels
= 2;
620 static int esd_proto_sample_cache(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
626 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
627 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
629 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
630 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
633 format_esd2native(format
, c
->swap_byte_order
, &ss
);
635 sc_length
= (size_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, (*((const int*)data
+ 2)));
637 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
640 strcpy(name
, SCACHE_PREFIX
);
641 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (const char*) data
+3*sizeof(int), ESD_NAME_MAX
);
642 name
[sizeof(name
)-1] = 0;
644 assert(!c
->scache
.memchunk
.memblock
);
645 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
646 c
->scache
.memchunk
.index
= 0;
647 c
->scache
.memchunk
.length
= sc_length
;
648 c
->scache
.sample_spec
= ss
;
649 assert(!c
->scache
.name
);
650 c
->scache
.name
= pa_xstrdup(name
);
652 c
->state
= ESD_CACHING_SAMPLE
;
654 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, &idx
);
656 ok
= connection_write(c
, sizeof(int));
664 static int esd_proto_sample_get_id(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
667 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
668 assert(c
&& data
&& length
== ESD_NAME_MAX
);
670 ok
= connection_write(c
, sizeof(int));
675 strcpy(name
, SCACHE_PREFIX
);
676 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
677 name
[sizeof(name
)-1] = 0;
679 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
685 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
689 assert(c
&& data
&& length
== sizeof(int));
691 idx
= (uint32_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
)-1;
693 ok
= connection_write(c
, sizeof(int));
698 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
699 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
702 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
703 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, NULL
) >= 0)
706 assert(request
== ESD_PROTO_SAMPLE_FREE
);
708 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
716 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
) {
718 ok
= connection_write(c
, sizeof(int)*2);
725 /*** client callbacks ***/
727 static void client_kill_cb(pa_client
*c
) {
728 assert(c
&& c
->userdata
);
729 connection_free(c
->userdata
);
732 /*** pa_iochannel callbacks ***/
734 static int do_read(struct connection
*c
) {
737 /* pa_log("READ\n"); */
739 if (c
->state
== ESD_NEXT_REQUEST
) {
741 assert(c
->read_data_length
< sizeof(c
->request
));
743 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
744 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
748 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
749 struct proto_handler
*handler
;
751 c
->request
= MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
753 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
754 pa_log(__FILE__
": recieved invalid request.\n");
758 handler
= proto_map
+c
->request
;
760 /* pa_log(__FILE__": executing request #%u\n", c->request); */
762 if (!handler
->proc
) {
763 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
767 if (handler
->data_length
== 0) {
768 c
->read_data_length
= 0;
770 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
774 if (c
->read_data_alloc
< handler
->data_length
)
775 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
776 assert(c
->read_data
);
778 c
->state
= ESD_NEEDS_REQDATA
;
779 c
->read_data_length
= 0;
783 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
785 struct proto_handler
*handler
= proto_map
+c
->request
;
787 assert(handler
->proc
);
789 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
791 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
792 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
796 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
797 size_t l
= c
->read_data_length
;
798 assert(handler
->proc
);
800 c
->state
= ESD_NEXT_REQUEST
;
801 c
->read_data_length
= 0;
803 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
806 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
809 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
811 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) {
812 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
816 c
->scache
.memchunk
.index
+= r
;
817 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
819 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
823 c
->scache
.memchunk
.index
= 0;
824 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, &idx
);
826 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
827 c
->scache
.memchunk
.memblock
= NULL
;
828 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
830 pa_xfree(c
->scache
.name
);
831 c
->scache
.name
= NULL
;
833 c
->state
= ESD_NEXT_REQUEST
;
835 ok
= connection_write(c
, sizeof(int));
840 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
845 assert(c
->input_memblockq
);
847 /* pa_log("STREAMING_DATA\n"); */
849 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
852 if (l
> c
->playback
.fragment_size
)
853 l
= c
->playback
.fragment_size
;
855 if (c
->playback
.current_memblock
)
856 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
857 pa_memblock_unref(c
->playback
.current_memblock
);
858 c
->playback
.current_memblock
= NULL
;
859 c
->playback
.memblock_index
= 0;
862 if (!c
->playback
.current_memblock
) {
863 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
864 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
865 c
->playback
.memblock_index
= 0;
868 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
869 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
873 chunk
.memblock
= c
->playback
.current_memblock
;
874 chunk
.index
= c
->playback
.memblock_index
;
876 assert(chunk
.memblock
);
878 c
->playback
.memblock_index
+= r
;
880 assert(c
->input_memblockq
);
881 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
882 assert(c
->sink_input
);
883 pa_sink_notify(c
->sink_input
->sink
);
889 static int do_write(struct connection
*c
) {
892 /* pa_log("WRITE\n"); */
894 if (c
->write_data_length
) {
897 assert(c
->write_data_index
< c
->write_data_length
);
898 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) {
899 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
903 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
904 c
->write_data_length
= c
->write_data_index
= 0;
906 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
910 assert(c
->output_memblockq
);
911 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
914 assert(chunk
.memblock
&& chunk
.length
);
916 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
917 pa_memblock_unref(chunk
.memblock
);
918 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
922 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
923 pa_memblock_unref(chunk
.memblock
);
925 pa_source_notify(c
->source_output
->source
);
931 static void do_work(struct connection
*c
) {
934 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
935 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
940 if (pa_iochannel_is_readable(c
->io
)) {
943 } else if (pa_iochannel_is_hungup(c
->io
))
946 if (pa_iochannel_is_writable(c
->io
))
954 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
957 pa_iochannel_free(c
->io
);
960 pa_memblockq_prebuf_disable(c
->input_memblockq
);
961 pa_sink_notify(c
->sink_input
->sink
);
967 static void io_callback(pa_iochannel
*io
, void *userdata
) {
968 struct connection
*c
= userdata
;
969 assert(io
&& c
&& c
->io
== io
);
971 /* pa_log("IO\n"); */
976 /*** defer callback ***/
978 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
979 struct connection
*c
= userdata
;
980 assert(a
&& c
&& c
->defer_event
== e
);
982 /* pa_log("DEFER\n"); */
987 /*** sink_input callbacks ***/
989 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
991 assert(i
&& i
->userdata
&& chunk
);
994 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1005 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
1006 struct connection
*c
= i
->userdata
;
1007 assert(i
&& c
&& length
);
1009 /* pa_log("DROP\n"); */
1011 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
1014 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1017 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1019 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1022 static void sink_input_kill_cb(pa_sink_input
*i
) {
1023 assert(i
&& i
->userdata
);
1024 connection_free((struct connection
*) i
->userdata
);
1027 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
1028 struct connection
*c
= i
->userdata
;
1030 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1033 /*** source_output callbacks ***/
1035 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1036 struct connection
*c
= o
->userdata
;
1037 assert(o
&& c
&& chunk
);
1039 pa_memblockq_push(c
->output_memblockq
, chunk
);
1042 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1045 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1048 static void source_output_kill_cb(pa_source_output
*o
) {
1049 assert(o
&& o
->userdata
);
1050 connection_free((struct connection
*) o
->userdata
);
1053 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1054 struct connection
*c
= o
->userdata
;
1056 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1059 /*** socket server callback ***/
1061 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1062 struct connection
*c
= userdata
;
1063 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1069 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1070 struct connection
*c
;
1071 pa_protocol_esound
*p
= userdata
;
1073 assert(s
&& io
&& p
);
1075 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1076 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
1077 pa_iochannel_free(io
);
1081 c
= pa_xmalloc(sizeof(struct connection
));
1084 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1086 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1088 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1090 c
->client
->owner
= p
->module
;
1091 c
->client
->kill
= client_kill_cb
;
1092 c
->client
->userdata
= c
;
1094 c
->authorized
= p
->public;
1095 c
->swap_byte_order
= 0;
1098 c
->read_data_length
= 0;
1099 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1101 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1102 c
->write_data
= NULL
;
1104 c
->state
= ESD_NEEDS_REQDATA
;
1105 c
->request
= ESD_PROTO_CONNECT
;
1107 c
->sink_input
= NULL
;
1108 c
->input_memblockq
= NULL
;
1110 c
->source_output
= NULL
;
1111 c
->output_memblockq
= NULL
;
1113 c
->playback
.current_memblock
= NULL
;
1114 c
->playback
.memblock_index
= 0;
1115 c
->playback
.fragment_size
= 0;
1117 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1118 c
->scache
.memchunk
.memblock
= NULL
;
1119 c
->scache
.name
= NULL
;
1121 if (!c
->authorized
) {
1123 pa_gettimeofday(&tv
);
1124 tv
.tv_sec
+= AUTH_TIMEOUT
;
1125 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1127 c
->auth_timeout_event
= NULL
;
1129 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1130 assert(c
->defer_event
);
1131 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1133 pa_idxset_put(p
->connections
, c
, &c
->index
);
1136 /*** entry points ***/
1138 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1139 pa_protocol_esound
*p
;
1141 assert(core
&& server
&& ma
);
1143 p
= pa_xmalloc(sizeof(pa_protocol_esound
));
1145 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1146 pa_log(__FILE__
": public= expects a boolean argument.\n");
1150 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1158 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1160 p
->connections
= pa_idxset_new(NULL
, NULL
);
1161 assert(p
->connections
);
1163 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1164 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1170 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1171 struct connection
*c
;
1174 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1177 pa_idxset_free(p
->connections
, NULL
, NULL
);
1178 pa_socket_server_unref(p
->server
);