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 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 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"
50 #define DEFAULT_COOKIE_FILE ".esd_auth"
52 #define PLAYBACK_BUFFER_SECONDS (.5)
53 #define PLAYBACK_BUFFER_FRAGMENTS (10)
54 #define RECORD_BUFFER_SECONDS (5)
55 #define RECORD_BUFFER_FRAGMENTS (100)
57 #define MAX_CACHE_SAMPLE_SIZE (1024000)
59 #define SCACHE_PREFIX "esound."
61 /* This is heavily based on esound's code */
65 struct pa_protocol_esound
*protocol
;
66 struct pa_iochannel
*io
;
67 struct pa_client
*client
;
68 int authorized
, swap_byte_order
;
70 size_t write_data_alloc
, write_data_index
, write_data_length
;
72 size_t read_data_alloc
, read_data_length
;
74 esd_client_state_t state
;
75 struct pa_sink_input
*sink_input
;
76 struct pa_source_output
*source_output
;
77 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
78 struct pa_defer_event
*defer_event
;
80 struct pa_memblock
*current_memblock
;
81 size_t memblock_index
, fragment_size
;
84 struct pa_memchunk scache_memchunk
;
86 struct pa_sample_spec scache_sample_spec
;
89 struct pa_protocol_esound
{
91 struct pa_module
*module
;
93 struct pa_socket_server
*server
;
94 struct pa_idxset
*connections
;
95 char *sink_name
, *source_name
;
97 uint8_t esd_key
[ESD_KEY_LEN
];
100 typedef struct proto_handler
{
102 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
103 const char *description
;
104 } esd_proto_handler_info_t
;
106 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
107 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
108 static void sink_input_kill_cb(struct pa_sink_input
*i
);
109 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
111 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
112 static void source_output_kill_cb(struct pa_source_output
*o
);
114 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
115 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
116 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
117 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
118 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
119 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
120 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
121 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
122 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
123 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
125 /* the big map of protocol handler info */
126 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
127 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
128 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
129 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
131 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
132 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
133 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
135 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" },
136 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
137 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" },
138 { sizeof(int), NULL
, "sample loop" },
139 { sizeof(int), NULL
, "sample stop" },
140 { -1, NULL
, "TODO: sample kill" },
142 { ESD_KEY_LEN
+ sizeof(int), NULL
, "standby" },
143 { ESD_KEY_LEN
+ sizeof(int), NULL
, "resume" },
145 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" },
146 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
148 { sizeof(int), esd_proto_server_info
, "server info" },
149 { sizeof(int), esd_proto_all_info
, "all info" },
150 { -1, NULL
, "TODO: subscribe" },
151 { -1, NULL
, "TODO: unsubscribe" },
153 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
154 { 3 * sizeof(int), NULL
, "sample pan" },
156 { sizeof(int), NULL
, "standby mode" },
157 { 0, esd_proto_get_latency
, "get latency" }
161 static void connection_free(struct connection
*c
) {
163 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
165 if (c
->state
== ESD_STREAMING_DATA
)
166 c
->protocol
->n_player
--;
168 pa_client_free(c
->client
);
171 pa_sink_input_free(c
->sink_input
);
172 if (c
->source_output
)
173 pa_source_output_free(c
->source_output
);
174 if (c
->input_memblockq
)
175 pa_memblockq_free(c
->input_memblockq
);
176 if (c
->output_memblockq
)
177 pa_memblockq_free(c
->output_memblockq
);
179 if (c
->playback
.current_memblock
)
180 pa_memblock_unref(c
->playback
.current_memblock
);
182 pa_xfree(c
->read_data
);
183 pa_xfree(c
->write_data
);
185 pa_iochannel_free(c
->io
);
188 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
190 if (c
->scache_memchunk
.memblock
)
191 pa_memblock_unref(c
->scache_memchunk
.memblock
);
192 pa_xfree(c
->scache_name
);
197 static void* connection_write(struct connection
*c
, size_t length
) {
201 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
202 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
204 t
= c
->write_data_length
+length
;
206 if (c
->write_data_alloc
< t
)
207 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
209 assert(c
->write_data
);
211 i
= c
->write_data_length
;
212 c
->write_data_length
+= length
;
214 return (uint8_t*) c
->write_data
+i
;
217 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
220 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
221 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
224 static int format_native2esd(struct pa_sample_spec
*ss
) {
227 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
228 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
233 /*** esound commands ***/
235 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
238 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
240 if (!c
->authorized
) {
241 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
242 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
249 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
250 if (ekey
== ESD_ENDIAN_KEY
)
251 c
->swap_byte_order
= 0;
252 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
253 c
->swap_byte_order
= 1;
255 pa_log(__FILE__
": client sent invalid endian key\n");
259 ok
= connection_write(c
, sizeof(int));
265 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
266 char name
[ESD_NAME_MAX
];
268 struct pa_sink
*sink
;
269 struct pa_sample_spec ss
;
271 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
273 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
274 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
277 format_esd2native(format
, &ss
);
279 if (!pa_sample_spec_valid(&ss
))
282 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
283 pa_log(__FILE__
": No output sink\n");
287 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
288 name
[sizeof(name
)-1] = 0;
290 pa_client_rename(c
->client
, name
);
292 assert(!c
->input_memblockq
);
294 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
295 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
296 assert(c
->input_memblockq
);
297 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
298 c
->playback
.fragment_size
= l
/10;
300 assert(!c
->sink_input
);
301 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
);
302 assert(c
->sink_input
);
304 c
->sink_input
->owner
= c
->protocol
->module
;
305 c
->sink_input
->client
= c
->client
;
306 c
->sink_input
->peek
= sink_input_peek_cb
;
307 c
->sink_input
->drop
= sink_input_drop_cb
;
308 c
->sink_input
->kill
= sink_input_kill_cb
;
309 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
310 c
->sink_input
->userdata
= c
;
312 c
->state
= ESD_STREAMING_DATA
;
314 c
->protocol
->n_player
++;
319 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
320 char name
[ESD_NAME_MAX
];
322 struct pa_source
*source
;
323 struct pa_sample_spec ss
;
325 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
327 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
328 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
331 format_esd2native(format
, &ss
);
333 if (!pa_sample_spec_valid(&ss
))
336 if (request
== ESD_PROTO_STREAM_MON
) {
337 struct pa_sink
* sink
;
339 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
342 if (!(source
= sink
->monitor_source
))
345 assert(request
== ESD_PROTO_STREAM_REC
);
347 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1)))
351 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
352 name
[sizeof(name
)-1] = 0;
354 pa_client_rename(c
->client
, name
);
356 assert(!c
->output_memblockq
);
358 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
359 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
360 assert(c
->output_memblockq
);
361 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
363 assert(!c
->source_output
);
364 c
->source_output
= pa_source_output_new(source
, name
, &ss
);
365 assert(c
->source_output
);
367 c
->source_output
->owner
= c
->protocol
->module
;
368 c
->source_output
->client
= c
->client
;
369 c
->source_output
->push
= source_output_push_cb
;
370 c
->source_output
->kill
= source_output_kill_cb
;
371 c
->source_output
->userdata
= c
;
373 c
->state
= ESD_STREAMING_DATA
;
375 c
->protocol
->n_player
++;
380 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
381 struct pa_sink
*sink
;
383 assert(c
&& !data
&& length
== 0);
385 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
388 double usec
= pa_sink_get_latency(sink
);
389 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
390 latency
= (int) ((usec
*44100)/1000000);
393 lag
= connection_write(c
, sizeof(int));
395 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
399 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
400 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
402 struct pa_sink
*sink
;
403 assert(c
&& data
&& length
== sizeof(int));
405 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
406 rate
= sink
->sample_spec
.rate
;
407 format
= format_native2esd(&sink
->sample_spec
);
410 response
= connection_write(c
, sizeof(int)*3);
413 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
414 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
418 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
421 struct connection
*conn
;
422 size_t index
= PA_IDXSET_INVALID
;
424 assert(c
&& data
&& length
== sizeof(int));
426 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
429 k
= sizeof(int)*5+ESD_NAME_MAX
;
430 s
= sizeof(int)*6+ESD_NAME_MAX
;
431 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
432 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
435 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
436 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
438 if (conn
->state
!= ESD_STREAMING_DATA
)
443 if (conn
->sink_input
) {
444 rate
= conn
->sink_input
->sample_spec
.rate
;
445 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
446 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
450 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
451 response
+= sizeof(int);
454 assert(conn
->client
);
455 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
456 response
+= ESD_NAME_MAX
;
459 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
460 response
+= sizeof(int);
463 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
464 response
+= sizeof(int);
467 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
468 response
+= sizeof(int);
471 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
472 response
+= sizeof(int);
477 assert(t
== s
*(nsamples
+1)+k
);
478 memset(response
, 0, k
);
483 struct pa_scache_entry
*ce
;
485 index
= PA_IDXSET_INVALID
;
486 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
490 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
491 response
+= sizeof(int);
494 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
495 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
497 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
498 response
+= ESD_NAME_MAX
;
501 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
502 response
+= sizeof(int);
505 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
506 response
+= sizeof(int);
509 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
510 response
+= sizeof(int);
513 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
514 response
+= sizeof(int);
517 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
518 response
+= sizeof(int);
525 memset(response
, 0, s
);
530 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
532 uint32_t index
, volume
;
533 struct connection
*conn
;
534 assert(c
&& data
&& length
== sizeof(int)*3);
536 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
537 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
538 volume
= (volume
*0x100)/0xFF;
540 ok
= connection_write(c
, sizeof(int));
543 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
544 assert(conn
->sink_input
);
545 conn
->sink_input
->volume
= volume
;
553 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
554 struct pa_sample_spec ss
;
559 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
560 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
562 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
563 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
566 format_esd2native(format
, &ss
);
568 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
570 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
573 strcpy(name
, SCACHE_PREFIX
);
574 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
575 name
[sizeof(name
)-1] = 0;
577 assert(!c
->scache_memchunk
.memblock
);
578 c
->scache_memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
579 c
->scache_memchunk
.index
= 0;
580 c
->scache_memchunk
.length
= sc_length
;
581 c
->scache_sample_spec
= ss
;
582 assert(!c
->scache_name
);
583 c
->scache_name
= pa_xstrdup(name
);
585 c
->state
= ESD_CACHING_SAMPLE
;
587 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, NULL
, NULL
, &index
);
589 ok
= connection_write(c
, sizeof(int));
597 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
600 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
601 assert(c
&& data
&& length
== ESD_NAME_MAX
);
603 ok
= connection_write(c
, sizeof(int));
608 strcpy(name
, SCACHE_PREFIX
);
609 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
610 name
[sizeof(name
)-1] = 0;
612 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
613 *ok
= (int) index
+1;
618 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
622 assert(c
&& data
&& length
== sizeof(int));
624 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
626 ok
= connection_write(c
, sizeof(int));
631 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
632 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
633 struct pa_sink
*sink
;
635 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
636 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
639 assert(request
== ESD_PROTO_SAMPLE_FREE
);
641 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
649 /*** client callbacks ***/
651 static void client_kill_cb(struct pa_client
*c
) {
652 assert(c
&& c
->userdata
);
653 connection_free(c
->userdata
);
656 /*** pa_iochannel callbacks ***/
658 static int do_read(struct connection
*c
) {
661 if (c
->state
== ESD_NEXT_REQUEST
) {
663 assert(c
->read_data_length
< sizeof(c
->request
));
665 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
666 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
670 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
671 struct proto_handler
*handler
;
673 if (c
->swap_byte_order
)
674 c
->request
= swap_endian_32(c
->request
);
676 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
677 pa_log(__FILE__
": recieved invalid request.\n");
681 handler
= proto_map
+c
->request
;
683 if (!handler
->proc
) {
684 pa_log(__FILE__
": recieved unimplemented request.\n");
688 if (handler
->data_length
== 0) {
689 c
->read_data_length
= 0;
691 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
695 if (c
->read_data_alloc
< handler
->data_length
)
696 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
697 assert(c
->read_data
);
699 c
->state
= ESD_NEEDS_REQDATA
;
700 c
->read_data_length
= 0;
704 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
706 struct proto_handler
*handler
= proto_map
+c
->request
;
708 assert(handler
->proc
);
710 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
712 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
713 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
717 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
718 size_t l
= c
->read_data_length
;
719 assert(handler
->proc
);
721 c
->state
= ESD_NEXT_REQUEST
;
722 c
->read_data_length
= 0;
724 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
727 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
730 assert(c
->scache_memchunk
.memblock
&& c
->scache_name
&& c
->scache_memchunk
.index
< c
->scache_memchunk
.length
);
732 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) {
733 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
737 c
->scache_memchunk
.index
+= r
;
738 assert(c
->scache_memchunk
.index
<= c
->scache_memchunk
.length
);
740 if (c
->scache_memchunk
.index
== c
->scache_memchunk
.length
) {
744 c
->scache_memchunk
.index
= 0;
745 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, &c
->scache_sample_spec
, &c
->scache_memchunk
, &index
);
747 pa_memblock_unref(c
->scache_memchunk
.memblock
);
748 c
->scache_memchunk
.memblock
= NULL
;
749 c
->scache_memchunk
.index
= c
->scache_memchunk
.length
= 0;
751 pa_xfree(c
->scache_name
);
752 c
->scache_name
= NULL
;
754 c
->state
= ESD_NEXT_REQUEST
;
756 ok
= connection_write(c
, sizeof(int));
761 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
762 struct pa_memchunk chunk
;
766 assert(c
->input_memblockq
);
768 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
771 if (l
> c
->playback
.fragment_size
)
772 l
= c
->playback
.fragment_size
;
774 if (c
->playback
.current_memblock
)
775 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
776 pa_memblock_unref(c
->playback
.current_memblock
);
777 c
->playback
.current_memblock
= NULL
;
778 c
->playback
.memblock_index
= 0;
781 if (!c
->playback
.current_memblock
) {
782 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
783 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
784 c
->playback
.memblock_index
= 0;
787 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
788 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
792 chunk
.memblock
= c
->playback
.current_memblock
;
793 chunk
.index
= c
->playback
.memblock_index
;
795 assert(chunk
.memblock
);
797 c
->playback
.memblock_index
+= r
;
799 assert(c
->input_memblockq
);
800 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
801 assert(c
->sink_input
);
802 pa_sink_notify(c
->sink_input
->sink
);
808 static int do_write(struct connection
*c
) {
811 if (c
->write_data_length
) {
814 assert(c
->write_data_index
< c
->write_data_length
);
815 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) {
816 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
820 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
821 c
->write_data_length
= c
->write_data_index
= 0;
823 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
824 struct pa_memchunk chunk
;
827 assert(c
->output_memblockq
);
828 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
831 assert(chunk
.memblock
&& chunk
.length
);
833 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
834 pa_memblock_unref(chunk
.memblock
);
835 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
839 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
840 pa_memblock_unref(chunk
.memblock
);
846 static void do_work(struct connection
*c
) {
849 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
850 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
852 if (pa_iochannel_is_hungup(c
->io
))
855 if (pa_iochannel_is_writable(c
->io
))
859 if (pa_iochannel_is_readable(c
->io
))
869 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
870 struct connection
*c
= userdata
;
871 assert(io
&& c
&& c
->io
== io
);
876 /*** defer callback ***/
878 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
879 struct connection
*c
= userdata
;
880 assert(a
&& c
&& c
->defer_event
== e
);
885 /*** sink_input callbacks ***/
887 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
889 assert(i
&& i
->userdata
&& chunk
);
892 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
898 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
899 struct connection
*c
= i
->userdata
;
900 assert(i
&& c
&& length
);
902 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
905 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
906 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
908 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
911 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
912 assert(i
&& i
->userdata
);
913 connection_free((struct connection
*) i
->userdata
);
917 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
918 struct connection
*c
= i
->userdata
;
920 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
923 /*** source_output callbacks ***/
925 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
926 struct connection
*c
= o
->userdata
;
927 assert(o
&& c
&& chunk
);
929 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
932 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
933 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
936 static void source_output_kill_cb(struct pa_source_output
*o
) {
937 assert(o
&& o
->userdata
);
938 connection_free((struct connection
*) o
->userdata
);
941 /*** socket server callback ***/
943 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
944 struct connection
*c
;
946 assert(s
&& io
&& userdata
);
948 c
= pa_xmalloc(sizeof(struct connection
));
949 c
->protocol
= userdata
;
951 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
953 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
954 assert(c
->protocol
->core
);
955 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
957 c
->client
->owner
= c
->protocol
->module
;
958 c
->client
->kill
= client_kill_cb
;
959 c
->client
->userdata
= c
;
961 c
->authorized
= c
->protocol
->public;
962 c
->swap_byte_order
= 0;
964 c
->read_data_length
= 0;
965 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
967 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
968 c
->write_data
= NULL
;
970 c
->state
= ESD_NEEDS_REQDATA
;
971 c
->request
= ESD_PROTO_CONNECT
;
973 c
->sink_input
= NULL
;
974 c
->input_memblockq
= NULL
;
976 c
->source_output
= NULL
;
977 c
->output_memblockq
= NULL
;
979 c
->playback
.current_memblock
= NULL
;
980 c
->playback
.memblock_index
= 0;
981 c
->playback
.fragment_size
= 0;
983 c
->scache_memchunk
.length
= c
->scache_memchunk
.index
= 0;
984 c
->scache_memchunk
.memblock
= NULL
;
985 c
->scache_name
= NULL
;
987 c
->defer_event
= c
->protocol
->core
->mainloop
->defer_new(c
->protocol
->core
->mainloop
, defer_callback
, c
);
988 assert(c
->defer_event
);
989 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
991 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
994 /*** entry points ***/
996 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
) {
997 struct pa_protocol_esound
*p
;
999 assert(core
&& server
&& ma
);
1001 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1003 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1004 pa_log(__FILE__
": public= expects a boolean argument.\n");
1008 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1016 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1018 p
->connections
= pa_idxset_new(NULL
, NULL
);
1019 assert(p
->connections
);
1021 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1022 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1028 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1029 struct connection
*c
;
1032 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1035 pa_idxset_free(p
->connections
, NULL
, NULL
);
1036 pa_socket_server_unref(p
->server
);