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_disconnect(c
->sink_input
);
172 pa_sink_input_unref(c
->sink_input
);
175 if (c
->source_output
) {
176 pa_source_output_disconnect(c
->source_output
);
177 pa_source_output_unref(c
->source_output
);
180 if (c
->input_memblockq
)
181 pa_memblockq_free(c
->input_memblockq
);
182 if (c
->output_memblockq
)
183 pa_memblockq_free(c
->output_memblockq
);
185 if (c
->playback
.current_memblock
)
186 pa_memblock_unref(c
->playback
.current_memblock
);
188 pa_xfree(c
->read_data
);
189 pa_xfree(c
->write_data
);
191 pa_iochannel_free(c
->io
);
194 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
196 if (c
->scache_memchunk
.memblock
)
197 pa_memblock_unref(c
->scache_memchunk
.memblock
);
198 pa_xfree(c
->scache_name
);
203 static void* connection_write(struct connection
*c
, size_t length
) {
207 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
208 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
210 t
= c
->write_data_length
+length
;
212 if (c
->write_data_alloc
< t
)
213 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
215 assert(c
->write_data
);
217 i
= c
->write_data_length
;
218 c
->write_data_length
+= length
;
220 return (uint8_t*) c
->write_data
+i
;
223 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
226 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
227 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
230 static int format_native2esd(struct pa_sample_spec
*ss
) {
233 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
234 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
239 /*** esound commands ***/
241 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
244 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
246 if (!c
->authorized
) {
247 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
248 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
255 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
256 if (ekey
== ESD_ENDIAN_KEY
)
257 c
->swap_byte_order
= 0;
258 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
259 c
->swap_byte_order
= 1;
261 pa_log(__FILE__
": client sent invalid endian key\n");
265 ok
= connection_write(c
, sizeof(int));
271 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
272 char name
[ESD_NAME_MAX
];
274 struct pa_sink
*sink
;
275 struct pa_sample_spec ss
;
277 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
279 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
280 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
283 format_esd2native(format
, &ss
);
285 if (!pa_sample_spec_valid(&ss
))
288 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
289 pa_log(__FILE__
": No output sink\n");
293 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
294 name
[sizeof(name
)-1] = 0;
296 pa_client_set_name(c
->client
, name
);
298 assert(!c
->input_memblockq
);
300 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
301 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
302 assert(c
->input_memblockq
);
303 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
304 c
->playback
.fragment_size
= l
/10;
306 assert(!c
->sink_input
);
307 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
, 0);
308 assert(c
->sink_input
);
310 c
->sink_input
->owner
= c
->protocol
->module
;
311 c
->sink_input
->client
= c
->client
;
312 c
->sink_input
->peek
= sink_input_peek_cb
;
313 c
->sink_input
->drop
= sink_input_drop_cb
;
314 c
->sink_input
->kill
= sink_input_kill_cb
;
315 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
316 c
->sink_input
->userdata
= c
;
318 c
->state
= ESD_STREAMING_DATA
;
320 c
->protocol
->n_player
++;
325 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
326 char name
[ESD_NAME_MAX
];
328 struct pa_source
*source
;
329 struct pa_sample_spec ss
;
331 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
333 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
334 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
337 format_esd2native(format
, &ss
);
339 if (!pa_sample_spec_valid(&ss
))
342 if (request
== ESD_PROTO_STREAM_MON
) {
343 struct pa_sink
* sink
;
345 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
348 if (!(source
= sink
->monitor_source
))
351 assert(request
== ESD_PROTO_STREAM_REC
);
353 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1)))
357 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
358 name
[sizeof(name
)-1] = 0;
360 pa_client_set_name(c
->client
, name
);
362 assert(!c
->output_memblockq
);
364 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
365 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
366 assert(c
->output_memblockq
);
367 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
369 assert(!c
->source_output
);
370 c
->source_output
= pa_source_output_new(source
, name
, &ss
);
371 assert(c
->source_output
);
373 c
->source_output
->owner
= c
->protocol
->module
;
374 c
->source_output
->client
= c
->client
;
375 c
->source_output
->push
= source_output_push_cb
;
376 c
->source_output
->kill
= source_output_kill_cb
;
377 c
->source_output
->userdata
= c
;
379 c
->state
= ESD_STREAMING_DATA
;
381 c
->protocol
->n_player
++;
386 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
387 struct pa_sink
*sink
;
389 assert(c
&& !data
&& length
== 0);
391 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
394 double usec
= pa_sink_get_latency(sink
);
395 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
396 latency
= (int) ((usec
*44100)/1000000);
399 lag
= connection_write(c
, sizeof(int));
401 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
405 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
406 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
408 struct pa_sink
*sink
;
409 assert(c
&& data
&& length
== sizeof(int));
411 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
412 rate
= sink
->sample_spec
.rate
;
413 format
= format_native2esd(&sink
->sample_spec
);
416 response
= connection_write(c
, sizeof(int)*3);
419 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
420 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
424 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
427 struct connection
*conn
;
428 size_t index
= PA_IDXSET_INVALID
;
430 assert(c
&& data
&& length
== sizeof(int));
432 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
435 k
= sizeof(int)*5+ESD_NAME_MAX
;
436 s
= sizeof(int)*6+ESD_NAME_MAX
;
437 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
438 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
441 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
442 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
444 if (conn
->state
!= ESD_STREAMING_DATA
)
449 if (conn
->sink_input
) {
450 rate
= conn
->sink_input
->sample_spec
.rate
;
451 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
452 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
456 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
457 response
+= sizeof(int);
460 assert(conn
->client
);
461 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
462 response
+= ESD_NAME_MAX
;
465 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
466 response
+= sizeof(int);
469 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
470 response
+= sizeof(int);
473 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
474 response
+= sizeof(int);
477 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
478 response
+= sizeof(int);
483 assert(t
== s
*(nsamples
+1)+k
);
484 memset(response
, 0, k
);
489 struct pa_scache_entry
*ce
;
491 index
= PA_IDXSET_INVALID
;
492 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
496 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
497 response
+= sizeof(int);
500 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
501 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
503 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
504 response
+= ESD_NAME_MAX
;
507 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
508 response
+= sizeof(int);
511 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
512 response
+= sizeof(int);
515 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
516 response
+= sizeof(int);
519 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
520 response
+= sizeof(int);
523 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
524 response
+= sizeof(int);
531 memset(response
, 0, s
);
536 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
538 uint32_t index
, volume
;
539 struct connection
*conn
;
540 assert(c
&& data
&& length
== sizeof(int)*3);
542 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
543 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
544 volume
= (volume
*0x100)/0xFF;
546 ok
= connection_write(c
, sizeof(int));
549 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
550 assert(conn
->sink_input
);
551 conn
->sink_input
->volume
= volume
;
559 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
560 struct pa_sample_spec ss
;
565 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
566 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
568 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
569 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
572 format_esd2native(format
, &ss
);
574 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
576 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
579 strcpy(name
, SCACHE_PREFIX
);
580 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
581 name
[sizeof(name
)-1] = 0;
583 assert(!c
->scache_memchunk
.memblock
);
584 c
->scache_memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
585 c
->scache_memchunk
.index
= 0;
586 c
->scache_memchunk
.length
= sc_length
;
587 c
->scache_sample_spec
= ss
;
588 assert(!c
->scache_name
);
589 c
->scache_name
= pa_xstrdup(name
);
591 c
->state
= ESD_CACHING_SAMPLE
;
593 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, NULL
, NULL
, &index
);
595 ok
= connection_write(c
, sizeof(int));
603 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
606 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
607 assert(c
&& data
&& length
== ESD_NAME_MAX
);
609 ok
= connection_write(c
, sizeof(int));
614 strcpy(name
, SCACHE_PREFIX
);
615 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
616 name
[sizeof(name
)-1] = 0;
618 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
619 *ok
= (int) index
+1;
624 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
628 assert(c
&& data
&& length
== sizeof(int));
630 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
632 ok
= connection_write(c
, sizeof(int));
637 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
638 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
639 struct pa_sink
*sink
;
641 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
642 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
645 assert(request
== ESD_PROTO_SAMPLE_FREE
);
647 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
655 /*** client callbacks ***/
657 static void client_kill_cb(struct pa_client
*c
) {
658 assert(c
&& c
->userdata
);
659 connection_free(c
->userdata
);
662 /*** pa_iochannel callbacks ***/
664 static int do_read(struct connection
*c
) {
667 if (c
->state
== ESD_NEXT_REQUEST
) {
669 assert(c
->read_data_length
< sizeof(c
->request
));
671 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
672 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
676 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
677 struct proto_handler
*handler
;
679 if (c
->swap_byte_order
)
680 c
->request
= swap_endian_32(c
->request
);
682 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
683 pa_log(__FILE__
": recieved invalid request.\n");
687 handler
= proto_map
+c
->request
;
689 if (!handler
->proc
) {
690 pa_log(__FILE__
": recieved unimplemented request.\n");
694 if (handler
->data_length
== 0) {
695 c
->read_data_length
= 0;
697 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
701 if (c
->read_data_alloc
< handler
->data_length
)
702 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
703 assert(c
->read_data
);
705 c
->state
= ESD_NEEDS_REQDATA
;
706 c
->read_data_length
= 0;
710 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
712 struct proto_handler
*handler
= proto_map
+c
->request
;
714 assert(handler
->proc
);
716 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
718 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
719 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
723 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
724 size_t l
= c
->read_data_length
;
725 assert(handler
->proc
);
727 c
->state
= ESD_NEXT_REQUEST
;
728 c
->read_data_length
= 0;
730 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
733 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
736 assert(c
->scache_memchunk
.memblock
&& c
->scache_name
&& c
->scache_memchunk
.index
< c
->scache_memchunk
.length
);
738 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) {
739 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
743 c
->scache_memchunk
.index
+= r
;
744 assert(c
->scache_memchunk
.index
<= c
->scache_memchunk
.length
);
746 if (c
->scache_memchunk
.index
== c
->scache_memchunk
.length
) {
750 c
->scache_memchunk
.index
= 0;
751 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, &c
->scache_sample_spec
, &c
->scache_memchunk
, &index
);
753 pa_memblock_unref(c
->scache_memchunk
.memblock
);
754 c
->scache_memchunk
.memblock
= NULL
;
755 c
->scache_memchunk
.index
= c
->scache_memchunk
.length
= 0;
757 pa_xfree(c
->scache_name
);
758 c
->scache_name
= NULL
;
760 c
->state
= ESD_NEXT_REQUEST
;
762 ok
= connection_write(c
, sizeof(int));
767 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
768 struct pa_memchunk chunk
;
772 assert(c
->input_memblockq
);
774 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
777 if (l
> c
->playback
.fragment_size
)
778 l
= c
->playback
.fragment_size
;
780 if (c
->playback
.current_memblock
)
781 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
782 pa_memblock_unref(c
->playback
.current_memblock
);
783 c
->playback
.current_memblock
= NULL
;
784 c
->playback
.memblock_index
= 0;
787 if (!c
->playback
.current_memblock
) {
788 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
789 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
790 c
->playback
.memblock_index
= 0;
793 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
794 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
798 chunk
.memblock
= c
->playback
.current_memblock
;
799 chunk
.index
= c
->playback
.memblock_index
;
801 assert(chunk
.memblock
);
803 c
->playback
.memblock_index
+= r
;
805 assert(c
->input_memblockq
);
806 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
807 assert(c
->sink_input
);
808 pa_sink_notify(c
->sink_input
->sink
);
814 static int do_write(struct connection
*c
) {
817 if (c
->write_data_length
) {
820 assert(c
->write_data_index
< c
->write_data_length
);
821 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) {
822 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
826 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
827 c
->write_data_length
= c
->write_data_index
= 0;
829 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
830 struct pa_memchunk chunk
;
833 assert(c
->output_memblockq
);
834 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
837 assert(chunk
.memblock
&& chunk
.length
);
839 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
840 pa_memblock_unref(chunk
.memblock
);
841 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
845 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
846 pa_memblock_unref(chunk
.memblock
);
852 static void do_work(struct connection
*c
) {
855 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
856 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
858 if (pa_iochannel_is_hungup(c
->io
))
861 if (pa_iochannel_is_writable(c
->io
))
865 if (pa_iochannel_is_readable(c
->io
))
875 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
876 struct connection
*c
= userdata
;
877 assert(io
&& c
&& c
->io
== io
);
882 /*** defer callback ***/
884 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
885 struct connection
*c
= userdata
;
886 assert(a
&& c
&& c
->defer_event
== e
);
891 /*** sink_input callbacks ***/
893 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
895 assert(i
&& i
->userdata
&& chunk
);
898 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
904 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
905 struct connection
*c
= i
->userdata
;
906 assert(i
&& c
&& length
);
908 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
911 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
912 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
914 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
917 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
918 assert(i
&& i
->userdata
);
919 connection_free((struct connection
*) i
->userdata
);
923 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
924 struct connection
*c
= i
->userdata
;
926 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
929 /*** source_output callbacks ***/
931 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
932 struct connection
*c
= o
->userdata
;
933 assert(o
&& c
&& chunk
);
935 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
938 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
939 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
942 static void source_output_kill_cb(struct pa_source_output
*o
) {
943 assert(o
&& o
->userdata
);
944 connection_free((struct connection
*) o
->userdata
);
947 /*** socket server callback ***/
949 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
950 struct connection
*c
;
952 assert(s
&& io
&& userdata
);
954 c
= pa_xmalloc(sizeof(struct connection
));
955 c
->protocol
= userdata
;
957 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
959 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
960 assert(c
->protocol
->core
);
961 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
963 c
->client
->owner
= c
->protocol
->module
;
964 c
->client
->kill
= client_kill_cb
;
965 c
->client
->userdata
= c
;
967 c
->authorized
= c
->protocol
->public;
968 c
->swap_byte_order
= 0;
970 c
->read_data_length
= 0;
971 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
973 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
974 c
->write_data
= NULL
;
976 c
->state
= ESD_NEEDS_REQDATA
;
977 c
->request
= ESD_PROTO_CONNECT
;
979 c
->sink_input
= NULL
;
980 c
->input_memblockq
= NULL
;
982 c
->source_output
= NULL
;
983 c
->output_memblockq
= NULL
;
985 c
->playback
.current_memblock
= NULL
;
986 c
->playback
.memblock_index
= 0;
987 c
->playback
.fragment_size
= 0;
989 c
->scache_memchunk
.length
= c
->scache_memchunk
.index
= 0;
990 c
->scache_memchunk
.memblock
= NULL
;
991 c
->scache_name
= NULL
;
993 c
->defer_event
= c
->protocol
->core
->mainloop
->defer_new(c
->protocol
->core
->mainloop
, defer_callback
, c
);
994 assert(c
->defer_event
);
995 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
997 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
1000 /*** entry points ***/
1002 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
) {
1003 struct pa_protocol_esound
*p
;
1005 assert(core
&& server
&& ma
);
1007 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1009 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1010 pa_log(__FILE__
": public= expects a boolean argument.\n");
1014 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1022 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1024 p
->connections
= pa_idxset_new(NULL
, NULL
);
1025 assert(p
->connections
);
1027 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1028 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1034 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1035 struct connection
*c
;
1038 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1041 pa_idxset_free(p
->connections
, NULL
, NULL
);
1042 pa_socket_server_unref(p
->server
);