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 "protocol-esound.h"
37 #include "sink-input.h"
39 #include "source-output.h"
43 #include "sample-util.h"
49 /* Don't accept more connection than this */
50 #define MAX_CONNECTIONS 10
52 /* Kick a client if it doesn't authenticate within this time */
53 #define AUTH_TIMEOUT 5
55 #define DEFAULT_COOKIE_FILE ".esd_auth"
57 #define PLAYBACK_BUFFER_SECONDS (.5)
58 #define PLAYBACK_BUFFER_FRAGMENTS (10)
59 #define RECORD_BUFFER_SECONDS (5)
60 #define RECORD_BUFFER_FRAGMENTS (100)
62 #define MAX_CACHE_SAMPLE_SIZE (1024000)
64 #define SCACHE_PREFIX "esound."
66 /* This is heavily based on esound's code */
71 struct pa_protocol_esound
*protocol
;
72 struct pa_iochannel
*io
;
73 struct pa_client
*client
;
74 int authorized
, swap_byte_order
;
76 size_t write_data_alloc
, write_data_index
, write_data_length
;
78 size_t read_data_alloc
, read_data_length
;
80 esd_client_state_t state
;
81 struct pa_sink_input
*sink_input
;
82 struct pa_source_output
*source_output
;
83 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
84 struct pa_defer_event
*defer_event
;
87 struct pa_memblock
*current_memblock
;
88 size_t memblock_index
, fragment_size
;
92 struct pa_memchunk memchunk
;
94 struct pa_sample_spec sample_spec
;
97 struct pa_time_event
*auth_timeout_event
;
100 struct pa_protocol_esound
{
102 struct pa_module
*module
;
103 struct pa_core
*core
;
104 struct pa_socket_server
*server
;
105 struct pa_idxset
*connections
;
106 char *sink_name
, *source_name
;
108 uint8_t esd_key
[ESD_KEY_LEN
];
111 typedef struct proto_handler
{
113 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
114 const char *description
;
115 } esd_proto_handler_info_t
;
117 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
118 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
119 static void sink_input_kill_cb(struct pa_sink_input
*i
);
120 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
121 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
123 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
124 static void source_output_kill_cb(struct pa_source_output
*o
);
126 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
127 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
128 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
129 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
130 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
131 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
132 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
133 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
134 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
135 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
136 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
138 /* the big map of protocol handler info */
139 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
140 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
141 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
142 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
144 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
145 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
146 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
148 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
149 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
150 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
151 { sizeof(int), NULL
, "sample loop" },
152 { sizeof(int), NULL
, "sample stop" },
153 { -1, NULL
, "TODO: sample kill" },
155 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
156 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
158 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
159 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
161 { sizeof(int), esd_proto_server_info
, "server info" },
162 { sizeof(int), esd_proto_all_info
, "all info" },
163 { -1, NULL
, "TODO: subscribe" },
164 { -1, NULL
, "TODO: unsubscribe" },
166 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
167 { 3 * sizeof(int), NULL
, "sample pan" },
169 { sizeof(int), NULL
, "standby mode" },
170 { 0, esd_proto_get_latency
, "get latency" }
174 static void connection_free(struct connection
*c
) {
176 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
178 if (c
->state
== ESD_STREAMING_DATA
)
179 c
->protocol
->n_player
--;
181 pa_client_free(c
->client
);
184 pa_sink_input_disconnect(c
->sink_input
);
185 pa_sink_input_unref(c
->sink_input
);
188 if (c
->source_output
) {
189 pa_source_output_disconnect(c
->source_output
);
190 pa_source_output_unref(c
->source_output
);
193 if (c
->input_memblockq
)
194 pa_memblockq_free(c
->input_memblockq
);
195 if (c
->output_memblockq
)
196 pa_memblockq_free(c
->output_memblockq
);
198 if (c
->playback
.current_memblock
)
199 pa_memblock_unref(c
->playback
.current_memblock
);
201 pa_xfree(c
->read_data
);
202 pa_xfree(c
->write_data
);
205 pa_iochannel_free(c
->io
);
208 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
210 if (c
->scache
.memchunk
.memblock
)
211 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
212 pa_xfree(c
->scache
.name
);
214 if (c
->auth_timeout_event
)
215 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
220 static void* connection_write(struct connection
*c
, size_t length
) {
224 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
225 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
227 t
= c
->write_data_length
+length
;
229 if (c
->write_data_alloc
< t
)
230 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
232 assert(c
->write_data
);
234 i
= c
->write_data_length
;
235 c
->write_data_length
+= length
;
237 return (uint8_t*) c
->write_data
+i
;
240 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
243 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
244 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
247 static int format_native2esd(struct pa_sample_spec
*ss
) {
250 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
251 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
256 /*** esound commands ***/
258 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
261 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
263 if (!c
->authorized
) {
264 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
265 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
270 if (c
->auth_timeout_event
) {
271 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
272 c
->auth_timeout_event
= NULL
;
276 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
277 if (ekey
== ESD_ENDIAN_KEY
)
278 c
->swap_byte_order
= 0;
279 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
280 c
->swap_byte_order
= 1;
282 pa_log(__FILE__
": client sent invalid endian key\n");
286 ok
= connection_write(c
, sizeof(int));
292 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
293 char name
[ESD_NAME_MAX
];
295 struct pa_sink
*sink
;
296 struct pa_sample_spec ss
;
298 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
300 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
301 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
304 format_esd2native(format
, &ss
);
306 if (!pa_sample_spec_valid(&ss
)) {
307 pa_log(__FILE__
": invalid sample specification\n");
311 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
312 pa_log(__FILE__
": no such sink\n");
316 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
317 name
[sizeof(name
)-1] = 0;
319 pa_client_set_name(c
->client
, name
);
321 assert(!c
->sink_input
&& !c
->input_memblockq
);
323 if (!(c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
, 0, -1))) {
324 pa_log(__FILE__
": failed to create sink input.\n");
328 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
329 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
330 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
331 c
->playback
.fragment_size
= l
/10;
333 c
->sink_input
->owner
= c
->protocol
->module
;
334 c
->sink_input
->client
= c
->client
;
335 c
->sink_input
->peek
= sink_input_peek_cb
;
336 c
->sink_input
->drop
= sink_input_drop_cb
;
337 c
->sink_input
->kill
= sink_input_kill_cb
;
338 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
339 c
->sink_input
->userdata
= c
;
341 c
->state
= ESD_STREAMING_DATA
;
343 c
->protocol
->n_player
++;
348 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
349 char name
[ESD_NAME_MAX
];
351 struct pa_source
*source
;
352 struct pa_sample_spec ss
;
354 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
356 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
357 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
360 format_esd2native(format
, &ss
);
362 if (!pa_sample_spec_valid(&ss
)) {
363 pa_log(__FILE__
": invalid sample specification.\n");
367 if (request
== ESD_PROTO_STREAM_MON
) {
368 struct pa_sink
* sink
;
370 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
371 pa_log(__FILE__
": no such sink.\n");
375 if (!(source
= sink
->monitor_source
)) {
376 pa_log(__FILE__
": no such monitor source.\n");
380 assert(request
== ESD_PROTO_STREAM_REC
);
382 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
383 pa_log(__FILE__
": no such source.\n");
388 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
389 name
[sizeof(name
)-1] = 0;
391 pa_client_set_name(c
->client
, name
);
393 assert(!c
->output_memblockq
&& !c
->source_output
);
395 if (!(c
->source_output
= pa_source_output_new(source
, name
, &ss
, -1))) {
396 pa_log(__FILE__
": failed to create source output\n");
400 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
401 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
402 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
404 c
->source_output
->owner
= c
->protocol
->module
;
405 c
->source_output
->client
= c
->client
;
406 c
->source_output
->push
= source_output_push_cb
;
407 c
->source_output
->kill
= source_output_kill_cb
;
408 c
->source_output
->get_latency
= source_output_get_latency_cb
;
409 c
->source_output
->userdata
= c
;
411 c
->state
= ESD_STREAMING_DATA
;
413 c
->protocol
->n_player
++;
418 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
419 struct pa_sink
*sink
;
421 assert(c
&& !data
&& length
== 0);
423 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
426 double usec
= pa_sink_get_latency(sink
);
427 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
428 latency
= (int) ((usec
*44100)/1000000);
431 lag
= connection_write(c
, sizeof(int));
433 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
437 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
438 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
440 struct pa_sink
*sink
;
441 assert(c
&& data
&& length
== sizeof(int));
443 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
444 rate
= sink
->sample_spec
.rate
;
445 format
= format_native2esd(&sink
->sample_spec
);
448 response
= connection_write(c
, sizeof(int)*3);
451 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
452 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
456 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
459 struct connection
*conn
;
460 size_t index
= PA_IDXSET_INVALID
;
462 assert(c
&& data
&& length
== sizeof(int));
464 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
467 k
= sizeof(int)*5+ESD_NAME_MAX
;
468 s
= sizeof(int)*6+ESD_NAME_MAX
;
469 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
470 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
473 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
474 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
476 if (conn
->state
!= ESD_STREAMING_DATA
)
481 if (conn
->sink_input
) {
482 rate
= conn
->sink_input
->sample_spec
.rate
;
483 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
484 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
488 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
489 response
+= sizeof(int);
492 assert(conn
->client
);
493 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
494 response
+= ESD_NAME_MAX
;
497 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
498 response
+= sizeof(int);
501 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
502 response
+= sizeof(int);
505 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
506 response
+= sizeof(int);
509 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
510 response
+= sizeof(int);
515 assert(t
== s
*(nsamples
+1)+k
);
516 memset(response
, 0, k
);
521 struct pa_scache_entry
*ce
;
523 index
= PA_IDXSET_INVALID
;
524 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
528 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
529 response
+= sizeof(int);
532 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
533 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
535 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
536 response
+= ESD_NAME_MAX
;
539 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
540 response
+= sizeof(int);
543 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
544 response
+= sizeof(int);
547 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
548 response
+= sizeof(int);
551 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
552 response
+= sizeof(int);
555 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
556 response
+= sizeof(int);
563 memset(response
, 0, s
);
568 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
570 uint32_t index
, volume
;
571 struct connection
*conn
;
572 assert(c
&& data
&& length
== sizeof(int)*3);
574 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
575 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
576 volume
= (volume
*0x100)/0xFF;
578 ok
= connection_write(c
, sizeof(int));
581 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
582 assert(conn
->sink_input
);
583 conn
->sink_input
->volume
= volume
;
591 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
592 struct pa_sample_spec ss
;
597 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
598 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
600 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
601 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
604 format_esd2native(format
, &ss
);
606 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
608 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
611 strcpy(name
, SCACHE_PREFIX
);
612 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
613 name
[sizeof(name
)-1] = 0;
615 assert(!c
->scache
.memchunk
.memblock
);
616 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
617 c
->scache
.memchunk
.index
= 0;
618 c
->scache
.memchunk
.length
= sc_length
;
619 c
->scache
.sample_spec
= ss
;
620 assert(!c
->scache
.name
);
621 c
->scache
.name
= pa_xstrdup(name
);
623 c
->state
= ESD_CACHING_SAMPLE
;
625 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, &index
);
627 ok
= connection_write(c
, sizeof(int));
635 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
638 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
639 assert(c
&& data
&& length
== ESD_NAME_MAX
);
641 ok
= connection_write(c
, sizeof(int));
646 strcpy(name
, SCACHE_PREFIX
);
647 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
648 name
[sizeof(name
)-1] = 0;
650 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
651 *ok
= (int) index
+1;
656 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
660 assert(c
&& data
&& length
== sizeof(int));
662 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
664 ok
= connection_write(c
, sizeof(int));
669 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
670 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
671 struct pa_sink
*sink
;
673 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
674 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
677 assert(request
== ESD_PROTO_SAMPLE_FREE
);
679 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
687 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
689 ok
= connection_write(c
, sizeof(int)*2);
696 /*** client callbacks ***/
698 static void client_kill_cb(struct pa_client
*c
) {
699 assert(c
&& c
->userdata
);
700 connection_free(c
->userdata
);
703 /*** pa_iochannel callbacks ***/
705 static int do_read(struct connection
*c
) {
708 /* pa_log("READ\n"); */
710 if (c
->state
== ESD_NEXT_REQUEST
) {
712 assert(c
->read_data_length
< sizeof(c
->request
));
714 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
715 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
719 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
720 struct proto_handler
*handler
;
722 if (c
->swap_byte_order
)
723 c
->request
= swap_endian_32(c
->request
);
725 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
726 pa_log(__FILE__
": recieved invalid request.\n");
730 handler
= proto_map
+c
->request
;
732 /* pa_log(__FILE__": executing request #%u\n", c->request); */
734 if (!handler
->proc
) {
735 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
739 if (handler
->data_length
== 0) {
740 c
->read_data_length
= 0;
742 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
746 if (c
->read_data_alloc
< handler
->data_length
)
747 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
748 assert(c
->read_data
);
750 c
->state
= ESD_NEEDS_REQDATA
;
751 c
->read_data_length
= 0;
755 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
757 struct proto_handler
*handler
= proto_map
+c
->request
;
759 assert(handler
->proc
);
761 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
763 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
764 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
768 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
769 size_t l
= c
->read_data_length
;
770 assert(handler
->proc
);
772 c
->state
= ESD_NEXT_REQUEST
;
773 c
->read_data_length
= 0;
775 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
778 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
781 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
783 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) {
784 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
788 c
->scache
.memchunk
.index
+= r
;
789 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
791 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
795 c
->scache
.memchunk
.index
= 0;
796 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, &c
->scache
.memchunk
, &index
);
798 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
799 c
->scache
.memchunk
.memblock
= NULL
;
800 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
802 pa_xfree(c
->scache
.name
);
803 c
->scache
.name
= NULL
;
805 c
->state
= ESD_NEXT_REQUEST
;
807 ok
= connection_write(c
, sizeof(int));
812 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
813 struct pa_memchunk chunk
;
817 assert(c
->input_memblockq
);
819 /* pa_log("STREAMING_DATA\n"); */
821 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
824 if (l
> c
->playback
.fragment_size
)
825 l
= c
->playback
.fragment_size
;
827 if (c
->playback
.current_memblock
)
828 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
829 pa_memblock_unref(c
->playback
.current_memblock
);
830 c
->playback
.current_memblock
= NULL
;
831 c
->playback
.memblock_index
= 0;
834 if (!c
->playback
.current_memblock
) {
835 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
836 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
837 c
->playback
.memblock_index
= 0;
840 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
841 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
845 /* pa_log(__FILE__": read %u\n", r); */
847 chunk
.memblock
= c
->playback
.current_memblock
;
848 chunk
.index
= c
->playback
.memblock_index
;
850 assert(chunk
.memblock
);
852 c
->playback
.memblock_index
+= r
;
854 assert(c
->input_memblockq
);
855 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
856 assert(c
->sink_input
);
857 pa_sink_notify(c
->sink_input
->sink
);
863 static int do_write(struct connection
*c
) {
866 /* pa_log("WRITE\n"); */
868 if (c
->write_data_length
) {
871 assert(c
->write_data_index
< c
->write_data_length
);
872 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) {
873 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
877 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
878 c
->write_data_length
= c
->write_data_index
= 0;
880 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
881 struct pa_memchunk chunk
;
884 assert(c
->output_memblockq
);
885 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
888 assert(chunk
.memblock
&& chunk
.length
);
890 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
891 pa_memblock_unref(chunk
.memblock
);
892 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
896 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
897 pa_memblock_unref(chunk
.memblock
);
903 static void do_work(struct connection
*c
) {
906 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
907 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
909 /* pa_log("DOWORK %i\n", pa_iochannel_is_hungup(c->io)); */
911 if (!c
->dead
&& pa_iochannel_is_readable(c
->io
))
915 if (!c
->dead
&& pa_iochannel_is_writable(c
->io
))
919 /* In case the line was hungup, make sure to rerun this function
920 as soon as possible, until all data has been read. */
922 if (!c
->dead
&& pa_iochannel_is_hungup(c
->io
))
923 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
929 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
931 pa_memblockq_prebuf_disable(c
->input_memblockq
);
933 pa_iochannel_free(c
->io
);
940 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
941 struct connection
*c
= userdata
;
942 assert(io
&& c
&& c
->io
== io
);
944 /* pa_log("IO\n"); */
949 /*** defer callback ***/
951 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
952 struct connection
*c
= userdata
;
953 assert(a
&& c
&& c
->defer_event
== e
);
955 /* pa_log("DEFER\n"); */
960 /*** sink_input callbacks ***/
962 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
964 assert(i
&& i
->userdata
&& chunk
);
967 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
978 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
979 struct connection
*c
= i
->userdata
;
980 assert(i
&& c
&& length
);
982 /* pa_log("DROP\n"); */
984 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
987 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
990 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
992 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
995 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
996 assert(i
&& i
->userdata
);
997 connection_free((struct connection
*) i
->userdata
);
1000 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
1001 struct connection
*c
= i
->userdata
;
1003 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1006 /*** source_output callbacks ***/
1008 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
1009 struct connection
*c
= o
->userdata
;
1010 assert(o
&& c
&& chunk
);
1012 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
1015 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1018 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1021 static void source_output_kill_cb(struct pa_source_output
*o
) {
1022 assert(o
&& o
->userdata
);
1023 connection_free((struct connection
*) o
->userdata
);
1026 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
1027 struct connection
*c
= o
->userdata
;
1029 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1032 /*** socket server callback ***/
1034 static void auth_timeout(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1035 struct connection
*c
= userdata
;
1036 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1042 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1043 struct connection
*c
;
1044 struct pa_protocol_esound
*p
= userdata
;
1046 assert(s
&& io
&& p
);
1048 if (pa_idxset_ncontents(p
->connections
)+1 > MAX_CONNECTIONS
) {
1049 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
1050 pa_iochannel_free(io
);
1054 c
= pa_xmalloc(sizeof(struct connection
));
1057 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1059 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1061 c
->client
= pa_client_new(p
->core
, "ESOUND", cname
);
1063 c
->client
->owner
= p
->module
;
1064 c
->client
->kill
= client_kill_cb
;
1065 c
->client
->userdata
= c
;
1067 c
->authorized
= p
->public;
1068 c
->swap_byte_order
= 0;
1071 c
->read_data_length
= 0;
1072 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1074 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1075 c
->write_data
= NULL
;
1077 c
->state
= ESD_NEEDS_REQDATA
;
1078 c
->request
= ESD_PROTO_CONNECT
;
1080 c
->sink_input
= NULL
;
1081 c
->input_memblockq
= NULL
;
1083 c
->source_output
= NULL
;
1084 c
->output_memblockq
= NULL
;
1086 c
->playback
.current_memblock
= NULL
;
1087 c
->playback
.memblock_index
= 0;
1088 c
->playback
.fragment_size
= 0;
1090 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1091 c
->scache
.memchunk
.memblock
= NULL
;
1092 c
->scache
.name
= NULL
;
1094 if (!c
->authorized
) {
1096 gettimeofday(&tv
, NULL
);
1097 tv
.tv_sec
+= AUTH_TIMEOUT
;
1098 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1100 c
->auth_timeout_event
= NULL
;
1102 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1103 assert(c
->defer_event
);
1104 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1106 pa_idxset_put(p
->connections
, c
, &c
->index
);
1109 /*** entry points ***/
1111 struct pa_protocol_esound
* pa_protocol_esound_new(struct pa_core
*core
, struct pa_socket_server
*server
, struct pa_module
*m
, struct pa_modargs
*ma
) {
1112 struct pa_protocol_esound
*p
;
1114 assert(core
&& server
&& ma
);
1116 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1118 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1119 pa_log(__FILE__
": public= expects a boolean argument.\n");
1123 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1131 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1133 p
->connections
= pa_idxset_new(NULL
, NULL
);
1134 assert(p
->connections
);
1136 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1137 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1143 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1144 struct connection
*c
;
1147 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1150 pa_idxset_free(p
->connections
, NULL
, NULL
);
1151 pa_socket_server_unref(p
->server
);