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 /*** esound commands ***/
265 static int esd_proto_connect(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
268 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
270 if (!c
->authorized
) {
271 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
272 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
277 if (c
->auth_timeout_event
) {
278 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
279 c
->auth_timeout_event
= NULL
;
283 ekey
= *(const uint32_t*)((const uint8_t*) data
+ESD_KEY_LEN
);
284 if (ekey
== ESD_ENDIAN_KEY
)
285 c
->swap_byte_order
= 0;
286 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
287 c
->swap_byte_order
= 1;
289 pa_log(__FILE__
": client sent invalid endian key\n");
293 ok
= connection_write(c
, sizeof(int));
299 static int esd_proto_stream_play(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
300 char name
[ESD_NAME_MAX
];
305 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
307 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
308 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
311 format_esd2native(format
, c
->swap_byte_order
, &ss
);
313 if (!pa_sample_spec_valid(&ss
)) {
314 pa_log(__FILE__
": invalid sample specification\n");
318 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
319 pa_log(__FILE__
": no such sink\n");
323 strncpy(name
, (const char*) data
+ sizeof(int)*2, sizeof(name
));
324 name
[sizeof(name
)-1] = 0;
326 pa_client_set_name(c
->client
, name
);
328 assert(!c
->sink_input
&& !c
->input_memblockq
);
330 if (!(c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, name
, &ss
, NULL
, 0, -1))) {
331 pa_log(__FILE__
": failed to create sink input.\n");
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 if (!pa_sample_spec_valid(&ss
)) {
378 pa_log(__FILE__
": invalid sample specification.\n");
382 if (request
== ESD_PROTO_STREAM_MON
) {
385 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
386 pa_log(__FILE__
": no such sink.\n");
390 if (!(source
= sink
->monitor_source
)) {
391 pa_log(__FILE__
": no such monitor source.\n");
395 assert(request
== ESD_PROTO_STREAM_REC
);
397 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
398 pa_log(__FILE__
": no such source.\n");
403 strncpy(name
, (const char*) data
+ sizeof(int)*2, sizeof(name
));
404 name
[sizeof(name
)-1] = 0;
406 pa_client_set_name(c
->client
, name
);
408 assert(!c
->output_memblockq
&& !c
->source_output
);
410 if (!(c
->source_output
= pa_source_output_new(source
, __FILE__
, name
, &ss
, NULL
, -1))) {
411 pa_log(__FILE__
": failed to create source output\n");
415 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
416 c
->output_memblockq
= pa_memblockq_new(
424 c
->protocol
->core
->memblock_stat
);
425 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
427 c
->source_output
->owner
= c
->protocol
->module
;
428 c
->source_output
->client
= c
->client
;
429 c
->source_output
->push
= source_output_push_cb
;
430 c
->source_output
->kill
= source_output_kill_cb
;
431 c
->source_output
->get_latency
= source_output_get_latency_cb
;
432 c
->source_output
->userdata
= c
;
434 c
->state
= ESD_STREAMING_DATA
;
436 c
->protocol
->n_player
++;
441 static int esd_proto_get_latency(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
444 assert(c
&& !data
&& length
== 0);
446 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
449 double usec
= pa_sink_get_latency(sink
);
450 latency
= (int) ((usec
*44100)/1000000);
453 lag
= connection_write(c
, sizeof(int));
455 *lag
= MAYBE_INT32_SWAP(c
->swap_byte_order
, latency
);
459 static int esd_proto_server_info(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
460 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
463 assert(c
&& data
&& length
== sizeof(int));
465 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
466 rate
= sink
->sample_spec
.rate
;
467 format
= format_native2esd(&sink
->sample_spec
);
470 response
= connection_write(c
, sizeof(int)*3);
473 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
474 *(response
++) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
478 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
481 struct connection
*conn
;
482 size_t idx
= PA_IDXSET_INVALID
;
484 assert(c
&& data
&& length
== sizeof(int));
486 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
489 k
= sizeof(int)*5+ESD_NAME_MAX
;
490 s
= sizeof(int)*6+ESD_NAME_MAX
;
491 nsamples
= c
->protocol
->core
->scache
? pa_idxset_size(c
->protocol
->core
->scache
) : 0;
492 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
495 for (conn
= pa_idxset_first(c
->protocol
->connections
, &idx
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &idx
)) {
496 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, lvolume
= ESD_VOLUME_BASE
, rvolume
= ESD_VOLUME_BASE
;
498 if (conn
->state
!= ESD_STREAMING_DATA
)
503 if (conn
->sink_input
) {
504 rate
= conn
->sink_input
->sample_spec
.rate
;
505 lvolume
= (conn
->sink_input
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
506 rvolume
= (conn
->sink_input
->volume
.values
[1]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
;
507 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
511 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (conn
->index
+1));
512 response
+= sizeof(int);
515 assert(conn
->client
);
516 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
517 response
+= ESD_NAME_MAX
;
520 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rate
);
521 response
+= sizeof(int);
524 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, lvolume
);
525 response
+= sizeof(int);
528 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, rvolume
);
529 response
+= sizeof(int);
532 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format
);
533 response
+= sizeof(int);
538 assert(t
== s
*(nsamples
+1)+k
);
539 memset(response
, 0, k
);
546 idx
= PA_IDXSET_INVALID
;
547 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &idx
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &idx
)) {
551 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) (ce
->index
+1));
552 response
+= sizeof(int);
555 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
556 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
558 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
559 response
+= ESD_NAME_MAX
;
562 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, ce
->sample_spec
.rate
);
563 response
+= sizeof(int);
566 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
567 response
+= sizeof(int);
570 *((uint32_t*) response
) = MAYBE_UINT32_SWAP(c
->swap_byte_order
, (ce
->volume
.values
[0]*ESD_VOLUME_BASE
)/PA_VOLUME_NORM
);
571 response
+= sizeof(int);
574 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
575 response
+= sizeof(int);
578 *((int*) response
) = MAYBE_INT32_SWAP(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
579 response
+= sizeof(int);
586 memset(response
, 0, s
);
591 static int esd_proto_stream_pan(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
594 pa_volume_t lvolume
, rvolume
;
595 struct connection
*conn
;
596 assert(c
&& data
&& length
== sizeof(int)*3);
598 idx
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *(const uint32_t*)data
)-1;
599 lvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 1));
600 lvolume
= (lvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
601 rvolume
= MAYBE_UINT32_SWAP(c
->swap_byte_order
, *((const uint32_t*)data
+ 2));
602 rvolume
= (rvolume
*PA_VOLUME_NORM
)/ESD_VOLUME_BASE
;
604 ok
= connection_write(c
, sizeof(int));
607 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, idx
))) {
608 assert(conn
->sink_input
);
609 conn
->sink_input
->volume
.values
[0] = lvolume
;
610 conn
->sink_input
->volume
.values
[1] = rvolume
;
611 conn
->sink_input
->volume
.channels
= 2;
619 static int esd_proto_sample_cache(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
625 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
626 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
628 format
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
);
629 rate
= MAYBE_INT32_SWAP(c
->swap_byte_order
, *((const int*)data
+ 1));
632 format_esd2native(format
, c
->swap_byte_order
, &ss
);
634 sc_length
= (size_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, (*((const int*)data
+ 2)));
636 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
639 strcpy(name
, SCACHE_PREFIX
);
640 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (const char*) data
+3*sizeof(int), ESD_NAME_MAX
);
641 name
[sizeof(name
)-1] = 0;
643 assert(!c
->scache
.memchunk
.memblock
);
644 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
645 c
->scache
.memchunk
.index
= 0;
646 c
->scache
.memchunk
.length
= sc_length
;
647 c
->scache
.sample_spec
= ss
;
648 assert(!c
->scache
.name
);
649 c
->scache
.name
= pa_xstrdup(name
);
651 c
->state
= ESD_CACHING_SAMPLE
;
653 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, NULL
, &idx
);
655 ok
= connection_write(c
, sizeof(int));
663 static int esd_proto_sample_get_id(struct connection
*c
, PA_GCC_UNUSED esd_proto_t request
, const void *data
, size_t length
) {
666 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
667 assert(c
&& data
&& length
== ESD_NAME_MAX
);
669 ok
= connection_write(c
, sizeof(int));
674 strcpy(name
, SCACHE_PREFIX
);
675 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
676 name
[sizeof(name
)-1] = 0;
678 if ((idx
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
684 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
688 assert(c
&& data
&& length
== sizeof(int));
690 idx
= (uint32_t) MAYBE_INT32_SWAP(c
->swap_byte_order
, *(const int*)data
)-1;
692 ok
= connection_write(c
, sizeof(int));
697 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, idx
))) {
698 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
701 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
702 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, NULL
) >= 0)
705 assert(request
== ESD_PROTO_SAMPLE_FREE
);
707 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
715 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
) {
717 ok
= connection_write(c
, sizeof(int)*2);
724 /*** client callbacks ***/
726 static void client_kill_cb(pa_client
*c
) {
727 assert(c
&& c
->userdata
);
728 connection_free(c
->userdata
);
731 /*** pa_iochannel callbacks ***/
733 static int do_read(struct connection
*c
) {
736 /* pa_log("READ\n"); */
738 if (c
->state
== ESD_NEXT_REQUEST
) {
740 assert(c
->read_data_length
< sizeof(c
->request
));
742 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
743 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
747 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
748 struct proto_handler
*handler
;
750 c
->request
= MAYBE_INT32_SWAP(c
->swap_byte_order
, c
->request
);
752 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
753 pa_log(__FILE__
": recieved invalid request.\n");
757 handler
= proto_map
+c
->request
;
759 /* pa_log(__FILE__": executing request #%u\n", c->request); */
761 if (!handler
->proc
) {
762 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
766 if (handler
->data_length
== 0) {
767 c
->read_data_length
= 0;
769 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
773 if (c
->read_data_alloc
< handler
->data_length
)
774 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
775 assert(c
->read_data
);
777 c
->state
= ESD_NEEDS_REQDATA
;
778 c
->read_data_length
= 0;
782 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
784 struct proto_handler
*handler
= proto_map
+c
->request
;
786 assert(handler
->proc
);
788 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
790 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
791 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
795 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
796 size_t l
= c
->read_data_length
;
797 assert(handler
->proc
);
799 c
->state
= ESD_NEXT_REQUEST
;
800 c
->read_data_length
= 0;
802 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
805 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
808 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
810 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) {
811 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
815 c
->scache
.memchunk
.index
+= r
;
816 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
818 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
822 c
->scache
.memchunk
.index
= 0;
823 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, NULL
, &c
->scache
.memchunk
, &idx
);
825 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
826 c
->scache
.memchunk
.memblock
= NULL
;
827 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
829 pa_xfree(c
->scache
.name
);
830 c
->scache
.name
= NULL
;
832 c
->state
= ESD_NEXT_REQUEST
;
834 ok
= connection_write(c
, sizeof(int));
839 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
844 assert(c
->input_memblockq
);
846 /* pa_log("STREAMING_DATA\n"); */
848 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
851 if (l
> c
->playback
.fragment_size
)
852 l
= c
->playback
.fragment_size
;
854 if (c
->playback
.current_memblock
)
855 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
856 pa_memblock_unref(c
->playback
.current_memblock
);
857 c
->playback
.current_memblock
= NULL
;
858 c
->playback
.memblock_index
= 0;
861 if (!c
->playback
.current_memblock
) {
862 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
863 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
864 c
->playback
.memblock_index
= 0;
867 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
868 pa_log_debug(__FILE__
": read() failed: %s\n", r
< 0 ? strerror(errno
) : "EOF");
872 chunk
.memblock
= c
->playback
.current_memblock
;
873 chunk
.index
= c
->playback
.memblock_index
;
875 assert(chunk
.memblock
);
877 c
->playback
.memblock_index
+= r
;
879 assert(c
->input_memblockq
);
880 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
881 assert(c
->sink_input
);
882 pa_sink_notify(c
->sink_input
->sink
);
888 static int do_write(struct connection
*c
) {
891 /* pa_log("WRITE\n"); */
893 if (c
->write_data_length
) {
896 assert(c
->write_data_index
< c
->write_data_length
);
897 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) {
898 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
902 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
903 c
->write_data_length
= c
->write_data_index
= 0;
905 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
909 assert(c
->output_memblockq
);
910 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
913 assert(chunk
.memblock
&& chunk
.length
);
915 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
916 pa_memblock_unref(chunk
.memblock
);
917 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
921 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
922 pa_memblock_unref(chunk
.memblock
);
924 pa_source_notify(c
->source_output
->source
);
930 static void do_work(struct connection
*c
) {
933 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
934 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
939 if (pa_iochannel_is_readable(c
->io
)) {
942 } else if (pa_iochannel_is_hungup(c
->io
))
945 if (pa_iochannel_is_writable(c
->io
))
953 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
956 pa_iochannel_free(c
->io
);
959 pa_memblockq_prebuf_disable(c
->input_memblockq
);
960 pa_sink_notify(c
->sink_input
->sink
);
966 static void io_callback(pa_iochannel
*io
, void *userdata
) {
967 struct connection
*c
= userdata
;
968 assert(io
&& c
&& c
->io
== io
);
970 /* pa_log("IO\n"); */
975 /*** defer callback ***/
977 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
978 struct connection
*c
= userdata
;
979 assert(a
&& c
&& c
->defer_event
== e
);
981 /* pa_log("DEFER\n"); */
986 /*** sink_input callbacks ***/
988 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
990 assert(i
&& i
->userdata
&& chunk
);
993 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
1004 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
1005 struct connection
*c
= i
->userdata
;
1006 assert(i
&& c
&& length
);
1008 /* pa_log("DROP\n"); */
1010 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
1013 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1016 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1018 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1021 static void sink_input_kill_cb(pa_sink_input
*i
) {
1022 assert(i
&& i
->userdata
);
1023 connection_free((struct connection
*) i
->userdata
);
1026 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
1027 struct connection
*c
= i
->userdata
;
1029 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1032 /*** source_output callbacks ***/
1034 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
1035 struct connection
*c
= o
->userdata
;
1036 assert(o
&& c
&& chunk
);
1038 pa_memblockq_push(c
->output_memblockq
, chunk
);
1041 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1044 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1047 static void source_output_kill_cb(pa_source_output
*o
) {
1048 assert(o
&& o
->userdata
);
1049 connection_free((struct connection
*) o
->userdata
);
1052 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
1053 struct connection
*c
= o
->userdata
;
1055 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1058 /*** socket server callback ***/
1060 static void auth_timeout(pa_mainloop_api
*m
, pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1061 struct connection
*c
= userdata
;
1062 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1068 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
1069 struct connection
*c
;
1070 pa_protocol_esound
*p
= userdata
;
1072 assert(s
&& io
&& p
);
1074 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
1075 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
1076 pa_iochannel_free(io
);
1080 c
= pa_xmalloc(sizeof(struct connection
));
1083 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1085 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1087 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
1089 c
->client
->owner
= p
->module
;
1090 c
->client
->kill
= client_kill_cb
;
1091 c
->client
->userdata
= c
;
1093 c
->authorized
= p
->public;
1094 c
->swap_byte_order
= 0;
1097 c
->read_data_length
= 0;
1098 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1100 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1101 c
->write_data
= NULL
;
1103 c
->state
= ESD_NEEDS_REQDATA
;
1104 c
->request
= ESD_PROTO_CONNECT
;
1106 c
->sink_input
= NULL
;
1107 c
->input_memblockq
= NULL
;
1109 c
->source_output
= NULL
;
1110 c
->output_memblockq
= NULL
;
1112 c
->playback
.current_memblock
= NULL
;
1113 c
->playback
.memblock_index
= 0;
1114 c
->playback
.fragment_size
= 0;
1116 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1117 c
->scache
.memchunk
.memblock
= NULL
;
1118 c
->scache
.name
= NULL
;
1120 if (!c
->authorized
) {
1122 pa_gettimeofday(&tv
);
1123 tv
.tv_sec
+= AUTH_TIMEOUT
;
1124 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1126 c
->auth_timeout_event
= NULL
;
1128 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1129 assert(c
->defer_event
);
1130 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1132 pa_idxset_put(p
->connections
, c
, &c
->index
);
1135 /*** entry points ***/
1137 pa_protocol_esound
* pa_protocol_esound_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
1138 pa_protocol_esound
*p
;
1140 assert(core
&& server
&& ma
);
1142 p
= pa_xmalloc(sizeof(pa_protocol_esound
));
1144 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1145 pa_log(__FILE__
": public= expects a boolean argument.\n");
1149 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1157 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1159 p
->connections
= pa_idxset_new(NULL
, NULL
);
1160 assert(p
->connections
);
1162 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1163 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1169 void pa_protocol_esound_free(pa_protocol_esound
*p
) {
1170 struct connection
*c
;
1173 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1176 pa_idxset_free(p
->connections
, NULL
, NULL
);
1177 pa_socket_server_unref(p
->server
);