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_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(struct connection
*c
, size_t length
) {
228 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
229 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
231 t
= c
->write_data_length
+length
;
233 if (c
->write_data_alloc
< t
)
234 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
236 assert(c
->write_data
);
238 i
= c
->write_data_length
;
239 c
->write_data_length
+= length
;
241 return (uint8_t*) c
->write_data
+i
;
244 static void format_esd2native(int format
, int swap_bytes
, pa_sample_spec
*ss
) {
247 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
248 if ((format
& ESD_MASK_BITS
) == ESD_BITS16
)
249 ss
->format
= swap_bytes
? PA_SAMPLE_S16RE
: PA_SAMPLE_S16NE
;
251 ss
->format
= PA_SAMPLE_U8
;
254 static int format_native2esd(pa_sample_spec
*ss
) {
257 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
258 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
263 #define CHECK_VALIDITY(expression, string) do { \
264 if (!(expression)) { \
265 pa_log_warn(__FILE__ ": " string); \
270 /*** esound commands ***/
272 static int esd_proto_connect(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
275 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
277 if (!c
->authorized
) {
278 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
279 pa_log(__FILE__
": kicked client with invalid authorization key.");
284 if (c
->auth_timeout_event
) {
285 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
286 c
->auth_timeout_event
= NULL
;
290 ekey
= *(const uint32_t*)((const uint8_t*) data
+ESD_KEY_LEN
);
291 if (ekey
== ESD_ENDIAN_KEY
)
292 c
->swap_byte_order
= 0;
293 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
294 c
->swap_byte_order
= 1;
296 pa_log(__FILE__
": client sent invalid endian key");
300 ok
= connection_write(c
, sizeof(int));
306 static int esd_proto_stream_play(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
307 char name
[ESD_NAME_MAX
];
312 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
314 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
315 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
318 format_esd2native(format
, c
->swap_byte_order
, &ss
);
320 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification");
321 sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1);
322 CHECK_VALIDITY(sink
, "No such sink");
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 c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, &ss
, NULL
, 0, -1);
333 CHECK_VALIDITY(c
->sink_input
, "Failed to create sink input.");
335 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
336 c
->input_memblockq
= pa_memblockq_new(
342 l
/PLAYBACK_BUFFER_FRAGMENTS
,
344 c
->protocol
->core
->memblock_stat
);
345 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
346 c
->playback
.fragment_size
= l
/10;
348 c
->sink_input
->owner
= c
->protocol
->module
;
349 c
->sink_input
->client
= c
->client
;
350 c
->sink_input
->peek
= sink_input_peek_cb
;
351 c
->sink_input
->drop
= sink_input_drop_cb
;
352 c
->sink_input
->kill
= sink_input_kill_cb
;
353 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
354 c
->sink_input
->userdata
= c
;
356 c
->state
= ESD_STREAMING_DATA
;
358 c
->protocol
->n_player
++;
363 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
364 char name
[ESD_NAME_MAX
];
369 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
371 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
372 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
375 format_esd2native(format
, c
->swap_byte_order
, &ss
);
377 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
379 if (request
== ESD_PROTO_STREAM_MON
) {
382 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
383 pa_log(__FILE__
": no such sink.");
387 if (!(source
= sink
->monitor_source
)) {
388 pa_log(__FILE__
": no such monitor source.");
392 assert(request
== ESD_PROTO_STREAM_REC
);
394 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
395 pa_log(__FILE__
": no such source.");
400 strncpy(name
, (const char*) data
+ sizeof(int)*2, sizeof(name
));
401 name
[sizeof(name
)-1] = 0;
403 pa_client_set_name(c
->client
, name
);
405 assert(!c
->output_memblockq
&& !c
->source_output
);
407 if (!(c
->source_output
= pa_source_output_new(source
, __FILE__
, name
, &ss
, NULL
, -1))) {
408 pa_log(__FILE__
": failed to create source output");
412 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
413 c
->output_memblockq
= pa_memblockq_new(
421 c
->protocol
->core
->memblock_stat
);
422 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
424 c
->source_output
->owner
= c
->protocol
->module
;
425 c
->source_output
->client
= c
->client
;
426 c
->source_output
->push
= source_output_push_cb
;
427 c
->source_output
->kill
= source_output_kill_cb
;
428 c
->source_output
->get_latency
= source_output_get_latency_cb
;
429 c
->source_output
->userdata
= c
;
431 c
->state
= ESD_STREAMING_DATA
;
433 c
->protocol
->n_player
++;
438 static int esd_proto_get_latency(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
441 assert(c
&& !data
&& length
== 0);
443 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
446 double usec
= pa_sink_get_latency(sink
);
447 latency
= (int) ((usec
*44100)/1000000);
450 lag
= connection_write(c
, sizeof(int));
452 *lag
= MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
456 static int esd_proto_server_info(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
457 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
460 assert(c
&& data
&& length
== sizeof(int));
462 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
463 rate
= sink
->sample_spec
.rate
;
464 format
= format_native2esd(&sink
->sample_spec
);
467 response
= connection_write(c
, sizeof(int)*3);
470 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
471 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
475 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
478 struct connection
*conn
;
479 uint32_t idx
= PA_IDXSET_INVALID
;
481 assert(c
&& data
&& length
== sizeof(int));
483 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
486 k
= sizeof(int)*5+ESD_NAME_MAX
;
487 s
= sizeof(int)*6+ESD_NAME_MAX
;
488 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
489 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
492 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
493 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
495 if (conn
->state
!= ESD_STREAMING_DATA
)
500 if (conn
->sink_input
) {
501 rate
= conn
->sink_input
->sample_spec
.rate
;
502 lvolume
= (conn
->sink_input
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
503 rvolume
= (conn
->sink_input
->volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
504 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
508 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (conn
->index
+1));
509 response
+= sizeof(int);
512 assert(conn
->client
);
513 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
514 response
+= ESD_NAME_MAX
;
517 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
518 response
+= sizeof(int);
521 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
522 response
+= sizeof(int);
525 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
526 response
+= sizeof(int);
529 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
530 response
+= sizeof(int);
535 assert(t
== s
*(nsamples
+1)+k
);
536 memset(response
, 0, k
);
543 idx
= PA_IDXSET_INVALID
;
544 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
548 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
549 response
+= sizeof(int);
552 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
553 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
555 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
556 response
+= ESD_NAME_MAX
;
559 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
560 response
+= sizeof(int);
563 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
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 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
572 response
+= sizeof(int);
575 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
576 response
+= sizeof(int);
583 memset(response
, 0, s
);
588 static int esd_proto_stream_pan(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
591 pa_volume_t lvolume
, rvolume
;
592 struct connection
*conn
;
593 assert(c
&& data
&& length
== sizeof(int)*3);
595 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *(const uint32_t*)data
)-1;
596 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 1));
597 lvolume
= (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
598 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 2));
599 rvolume
= (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
601 ok
= connection_write(c
, sizeof(int));
604 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
))) {
605 assert(conn
->sink_input
);
606 conn
->sink_input
->volume
.values
[0] = lvolume
;
607 conn
->sink_input
->volume
.values
[1] = rvolume
;
608 conn
->sink_input
->volume
.channels
= 2;
616 static int esd_proto_sample_cache(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
622 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
623 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
625 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
626 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
629 format_esd2native(format
, c
->swap_byte_order
, &ss
);
631 CHECK_VALIDITY(pa_sample_spec_valid(&ss
), "Invalid sample specification.");
633 sc_length
= (size_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, (*((const int*)data
+ 2)));
635 CHECK_VALIDITY(sc_length
<= MAX_CACHE_SAMPLE_SIZE
, "Sample too large.");
637 strcpy(name
, SCACHE_PREFIX
);
638 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (const char*) data
+3*sizeof(int), ESD_NAME_MAX
);
639 name
[sizeof(name
)-1] = 0;
641 assert(!c
->scache
.memchunk
.memblock
);
642 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
643 c
->scache
.memchunk
.index
= 0;
644 c
->scache
.memchunk
.length
= sc_length
;
645 c
->scache
.sample_spec
= ss
;
646 assert(!c
->scache
.name
);
647 c
->scache
.name
= pa_xstrdup(name
);
649 c
->state
= ESD_CACHING_SAMPLE
;
651 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, &idx
);
653 ok
= connection_write(c
, sizeof(int));
661 static int esd_proto_sample_get_id(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
664 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
665 assert(c
&& data
&& length
== ESD_NAME_MAX
);
667 ok
= connection_write(c
, sizeof(int));
672 strcpy(name
, SCACHE_PREFIX
);
673 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
674 name
[sizeof(name
)-1] = 0;
676 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
682 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
686 assert(c
&& data
&& length
== sizeof(int));
688 idx
= (uint32_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
)-1;
690 ok
= connection_write(c
, sizeof(int));
695 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
696 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
699 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
700 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, NULL
) >= 0)
703 assert(request
== ESD_PROTO_SAMPLE_FREE
);
705 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
713 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
) {
715 ok
= connection_write(c
, sizeof(int)*2);
722 /*** client callbacks ***/
724 static void client_kill_cb(pa_client
*c
) {
725 assert(c
&& c
->userdata
);
726 connection_free(c
->userdata
);
729 /*** pa_iochannel callbacks ***/
731 static int do_read(struct connection
*c
) {
734 /* pa_log("READ"); */
736 if (c
->state
== ESD_NEXT_REQUEST
) {
738 assert(c
->read_data_length
< sizeof(c
->request
));
740 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
741 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
745 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
746 struct proto_handler
*handler
;
748 c
->request
= MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
750 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
751 pa_log(__FILE__
": recieved invalid request.");
755 handler
= proto_map
+c
->request
;
757 /* pa_log(__FILE__": executing request #%u", c->request); */
759 if (!handler
->proc
) {
760 pa_log(__FILE__
": recieved unimplemented request #%u.", c
->request
);
764 if (handler
->data_length
== 0) {
765 c
->read_data_length
= 0;
767 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
771 if (c
->read_data_alloc
< handler
->data_length
)
772 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
773 assert(c
->read_data
);
775 c
->state
= ESD_NEEDS_REQDATA
;
776 c
->read_data_length
= 0;
780 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
782 struct proto_handler
*handler
= proto_map
+c
->request
;
784 assert(handler
->proc
);
786 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
788 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
789 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
793 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
794 size_t l
= c
->read_data_length
;
795 assert(handler
->proc
);
797 c
->state
= ESD_NEXT_REQUEST
;
798 c
->read_data_length
= 0;
800 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
803 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
806 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
808 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) {
809 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
813 c
->scache
.memchunk
.index
+= r
;
814 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
816 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
820 c
->scache
.memchunk
.index
= 0;
821 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, &idx
);
823 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
824 c
->scache
.memchunk
.memblock
= NULL
;
825 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
827 pa_xfree(c
->scache
.name
);
828 c
->scache
.name
= NULL
;
830 c
->state
= ESD_NEXT_REQUEST
;
832 ok
= connection_write(c
, sizeof(int));
837 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
842 assert(c
->input_memblockq
);
844 /* pa_log("STREAMING_DATA"); */
846 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
849 if (l
> c
->playback
.fragment_size
)
850 l
= c
->playback
.fragment_size
;
852 if (c
->playback
.current_memblock
)
853 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
854 pa_memblock_unref(c
->playback
.current_memblock
);
855 c
->playback
.current_memblock
= NULL
;
856 c
->playback
.memblock_index
= 0;
859 if (!c
->playback
.current_memblock
) {
860 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
861 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
862 c
->playback
.memblock_index
= 0;
865 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
866 pa_log_debug(__FILE__
": read() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
870 chunk
.memblock
= c
->playback
.current_memblock
;
871 chunk
.index
= c
->playback
.memblock_index
;
873 assert(chunk
.memblock
);
875 c
->playback
.memblock_index
+= r
;
877 assert(c
->input_memblockq
);
878 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
879 assert(c
->sink_input
);
880 pa_sink_notify(c
->sink_input
->sink
);
886 static int do_write(struct connection
*c
) {
889 /* pa_log("WRITE"); */
891 if (c
->write_data_length
) {
894 assert(c
->write_data_index
< c
->write_data_length
);
895 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) {
896 pa_log(__FILE__
": write() failed: %s", strerror(errno
));
900 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
901 c
->write_data_length
= c
->write_data_index
= 0;
903 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
907 assert(c
->output_memblockq
);
908 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
911 assert(chunk
.memblock
&& chunk
.length
);
913 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
914 pa_memblock_unref(chunk
.memblock
);
915 pa_log(__FILE__
": write(): %s", strerror(errno
));
919 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
920 pa_memblock_unref(chunk
.memblock
);
922 pa_source_notify(c
->source_output
->source
);
928 static void do_work(struct connection
*c
) {
931 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
932 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
937 if (pa_iochannel_is_readable(c
->io
)) {
940 } else if (pa_iochannel_is_hungup(c
->io
))
943 if (pa_iochannel_is_writable(c
->io
))
951 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
954 pa_iochannel_free(c
->io
);
957 pa_memblockq_prebuf_disable(c
->input_memblockq
);
958 pa_sink_notify(c
->sink_input
->sink
);
964 static void io_callback(pa_iochannel
*io
, void *userdata
) {
965 struct connection
*c
= userdata
;
966 assert(io
&& c
&& c
->io
== io
);
973 /*** defer callback ***/
975 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
976 struct connection
*c
= userdata
;
977 assert(a
&& c
&& c
->defer_event
== e
);
979 /* pa_log("DEFER"); */
984 /*** sink_input callbacks ***/
986 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
988 assert(i
&& i
->userdata
&& chunk
);
991 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1002 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
1003 struct connection
*c
= i
->userdata
;
1004 assert(i
&& c
&& length
);
1006 /* pa_log("DROP"); */
1008 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
1011 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1014 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1016 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1019 static void sink_input_kill_cb(pa_sink_input
*i
) {
1020 assert(i
&& i
->userdata
);
1021 connection_free((struct connection
*) i
->userdata
);
1024 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
1025 struct connection
*c
= i
->userdata
;
1027 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1030 /*** source_output callbacks ***/
1032 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1033 struct connection
*c
= o
->userdata
;
1034 assert(o
&& c
&& chunk
);
1036 pa_memblockq_push(c
->output_memblockq
, chunk
);
1039 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1042 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1045 static void source_output_kill_cb(pa_source_output
*o
) {
1046 assert(o
&& o
->userdata
);
1047 connection_free((struct connection
*) o
->userdata
);
1050 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1051 struct connection
*c
= o
->userdata
;
1053 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1056 /*** socket server callback ***/
1058 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1059 struct connection
*c
= userdata
;
1060 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1066 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1067 struct connection
*c
;
1068 pa_protocol_esound
*p
= userdata
;
1070 assert(s
&& io
&& p
);
1072 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1073 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
1074 pa_iochannel_free(io
);
1078 c
= pa_xnew(struct connection
, 1);
1081 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1083 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1085 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1087 c
->client
->owner
= p
->module
;
1088 c
->client
->kill
= client_kill_cb
;
1089 c
->client
->userdata
= c
;
1091 c
->authorized
= p
->public;
1092 c
->swap_byte_order
= 0;
1095 c
->read_data_length
= 0;
1096 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1098 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1099 c
->write_data
= NULL
;
1101 c
->state
= ESD_NEEDS_REQDATA
;
1102 c
->request
= ESD_PROTO_CONNECT
;
1104 c
->sink_input
= NULL
;
1105 c
->input_memblockq
= NULL
;
1107 c
->source_output
= NULL
;
1108 c
->output_memblockq
= NULL
;
1110 c
->playback
.current_memblock
= NULL
;
1111 c
->playback
.memblock_index
= 0;
1112 c
->playback
.fragment_size
= 0;
1114 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1115 c
->scache
.memchunk
.memblock
= NULL
;
1116 c
->scache
.name
= NULL
;
1118 if (!c
->authorized
) {
1120 pa_gettimeofday(&tv
);
1121 tv
.tv_sec
+= AUTH_TIMEOUT
;
1122 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1124 c
->auth_timeout_event
= NULL
;
1126 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1127 assert(c
->defer_event
);
1128 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1130 pa_idxset_put(p
->connections
, c
, &c
->index
);
1133 /*** entry points ***/
1135 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1136 pa_protocol_esound
*p
;
1138 assert(core
&& server
&& ma
);
1140 p
= pa_xnew(pa_protocol_esound
, 1);
1142 if (pa_modargs_get_value_boolean(ma
, "auth-anonymous", &public) < 0) {
1143 pa_log(__FILE__
": auth-anonymous= expects a boolean argument.");
1147 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1155 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1157 p
->connections
= pa_idxset_new(NULL
, NULL
);
1158 assert(p
->connections
);
1160 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1161 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1167 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1168 struct connection
*c
;
1171 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1174 pa_idxset_free(p
->connections
, NULL
, NULL
);
1175 pa_socket_server_unref(p
->server
);