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
);
110 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
112 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
113 static void source_output_kill_cb(struct pa_source_output
*o
);
115 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
116 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
117 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
118 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
119 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
120 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
121 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
122 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
123 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
124 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
126 /* the big map of protocol handler info */
127 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
128 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
129 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
130 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
132 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
133 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
134 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
136 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" },
137 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
138 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" },
139 { sizeof(int), NULL
, "sample loop" },
140 { sizeof(int), NULL
, "sample stop" },
141 { -1, NULL
, "TODO: sample kill" },
143 { ESD_KEY_LEN
+ sizeof(int), NULL
, "standby" },
144 { ESD_KEY_LEN
+ sizeof(int), NULL
, "resume" },
146 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" },
147 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
149 { sizeof(int), esd_proto_server_info
, "server info" },
150 { sizeof(int), esd_proto_all_info
, "all info" },
151 { -1, NULL
, "TODO: subscribe" },
152 { -1, NULL
, "TODO: unsubscribe" },
154 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
155 { 3 * sizeof(int), NULL
, "sample pan" },
157 { sizeof(int), NULL
, "standby mode" },
158 { 0, esd_proto_get_latency
, "get latency" }
162 static void connection_free(struct connection
*c
) {
164 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
166 if (c
->state
== ESD_STREAMING_DATA
)
167 c
->protocol
->n_player
--;
169 pa_client_free(c
->client
);
172 pa_sink_input_disconnect(c
->sink_input
);
173 pa_sink_input_unref(c
->sink_input
);
176 if (c
->source_output
) {
177 pa_source_output_disconnect(c
->source_output
);
178 pa_source_output_unref(c
->source_output
);
181 if (c
->input_memblockq
)
182 pa_memblockq_free(c
->input_memblockq
);
183 if (c
->output_memblockq
)
184 pa_memblockq_free(c
->output_memblockq
);
186 if (c
->playback
.current_memblock
)
187 pa_memblock_unref(c
->playback
.current_memblock
);
189 pa_xfree(c
->read_data
);
190 pa_xfree(c
->write_data
);
192 pa_iochannel_free(c
->io
);
195 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
197 if (c
->scache_memchunk
.memblock
)
198 pa_memblock_unref(c
->scache_memchunk
.memblock
);
199 pa_xfree(c
->scache_name
);
204 static void* connection_write(struct connection
*c
, size_t length
) {
208 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
209 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
211 t
= c
->write_data_length
+length
;
213 if (c
->write_data_alloc
< t
)
214 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
216 assert(c
->write_data
);
218 i
= c
->write_data_length
;
219 c
->write_data_length
+= length
;
221 return (uint8_t*) c
->write_data
+i
;
224 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
227 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
228 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
231 static int format_native2esd(struct pa_sample_spec
*ss
) {
234 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
235 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
240 /*** esound commands ***/
242 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
245 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
247 if (!c
->authorized
) {
248 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
249 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
256 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
257 if (ekey
== ESD_ENDIAN_KEY
)
258 c
->swap_byte_order
= 0;
259 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
260 c
->swap_byte_order
= 1;
262 pa_log(__FILE__
": client sent invalid endian key\n");
266 ok
= connection_write(c
, sizeof(int));
272 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
273 char name
[ESD_NAME_MAX
];
275 struct pa_sink
*sink
;
276 struct pa_sample_spec ss
;
278 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
280 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
281 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
284 format_esd2native(format
, &ss
);
286 if (!pa_sample_spec_valid(&ss
))
289 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
290 pa_log(__FILE__
": No output sink\n");
294 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
295 name
[sizeof(name
)-1] = 0;
297 pa_client_set_name(c
->client
, name
);
299 assert(!c
->input_memblockq
);
301 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
302 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
303 assert(c
->input_memblockq
);
304 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
305 c
->playback
.fragment_size
= l
/10;
307 assert(!c
->sink_input
);
308 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
, 0, -1);
309 assert(c
->sink_input
);
311 c
->sink_input
->owner
= c
->protocol
->module
;
312 c
->sink_input
->client
= c
->client
;
313 c
->sink_input
->peek
= sink_input_peek_cb
;
314 c
->sink_input
->drop
= sink_input_drop_cb
;
315 c
->sink_input
->kill
= sink_input_kill_cb
;
316 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
317 c
->sink_input
->userdata
= c
;
319 c
->state
= ESD_STREAMING_DATA
;
321 c
->protocol
->n_player
++;
326 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
327 char name
[ESD_NAME_MAX
];
329 struct pa_source
*source
;
330 struct pa_sample_spec ss
;
332 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
334 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
335 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
338 format_esd2native(format
, &ss
);
340 if (!pa_sample_spec_valid(&ss
))
343 if (request
== ESD_PROTO_STREAM_MON
) {
344 struct pa_sink
* sink
;
346 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
349 if (!(source
= sink
->monitor_source
))
352 assert(request
== ESD_PROTO_STREAM_REC
);
354 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1)))
358 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
359 name
[sizeof(name
)-1] = 0;
361 pa_client_set_name(c
->client
, name
);
363 assert(!c
->output_memblockq
);
365 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
366 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
367 assert(c
->output_memblockq
);
368 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
370 assert(!c
->source_output
);
371 c
->source_output
= pa_source_output_new(source
, name
, &ss
, -1);
372 assert(c
->source_output
);
374 c
->source_output
->owner
= c
->protocol
->module
;
375 c
->source_output
->client
= c
->client
;
376 c
->source_output
->push
= source_output_push_cb
;
377 c
->source_output
->kill
= source_output_kill_cb
;
378 c
->source_output
->get_latency
= source_output_get_latency_cb
;
379 c
->source_output
->userdata
= c
;
381 c
->state
= ESD_STREAMING_DATA
;
383 c
->protocol
->n_player
++;
388 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
389 struct pa_sink
*sink
;
391 assert(c
&& !data
&& length
== 0);
393 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
396 double usec
= pa_sink_get_latency(sink
);
397 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
398 latency
= (int) ((usec
*44100)/1000000);
401 lag
= connection_write(c
, sizeof(int));
403 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
407 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
408 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
410 struct pa_sink
*sink
;
411 assert(c
&& data
&& length
== sizeof(int));
413 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
414 rate
= sink
->sample_spec
.rate
;
415 format
= format_native2esd(&sink
->sample_spec
);
418 response
= connection_write(c
, sizeof(int)*3);
421 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
422 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
426 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
429 struct connection
*conn
;
430 size_t index
= PA_IDXSET_INVALID
;
432 assert(c
&& data
&& length
== sizeof(int));
434 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
437 k
= sizeof(int)*5+ESD_NAME_MAX
;
438 s
= sizeof(int)*6+ESD_NAME_MAX
;
439 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
440 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
443 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
444 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
446 if (conn
->state
!= ESD_STREAMING_DATA
)
451 if (conn
->sink_input
) {
452 rate
= conn
->sink_input
->sample_spec
.rate
;
453 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
454 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
458 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
459 response
+= sizeof(int);
462 assert(conn
->client
);
463 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
464 response
+= ESD_NAME_MAX
;
467 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
468 response
+= sizeof(int);
471 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
472 response
+= sizeof(int);
475 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
476 response
+= sizeof(int);
479 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
480 response
+= sizeof(int);
485 assert(t
== s
*(nsamples
+1)+k
);
486 memset(response
, 0, k
);
491 struct pa_scache_entry
*ce
;
493 index
= PA_IDXSET_INVALID
;
494 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
498 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
499 response
+= sizeof(int);
502 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
503 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
505 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
506 response
+= ESD_NAME_MAX
;
509 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
510 response
+= sizeof(int);
513 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
514 response
+= sizeof(int);
517 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
518 response
+= sizeof(int);
521 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
522 response
+= sizeof(int);
525 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
526 response
+= sizeof(int);
533 memset(response
, 0, s
);
538 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
540 uint32_t index
, volume
;
541 struct connection
*conn
;
542 assert(c
&& data
&& length
== sizeof(int)*3);
544 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
545 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
546 volume
= (volume
*0x100)/0xFF;
548 ok
= connection_write(c
, sizeof(int));
551 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
552 assert(conn
->sink_input
);
553 conn
->sink_input
->volume
= volume
;
561 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
562 struct pa_sample_spec ss
;
567 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
568 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
570 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
571 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
574 format_esd2native(format
, &ss
);
576 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
578 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
581 strcpy(name
, SCACHE_PREFIX
);
582 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
583 name
[sizeof(name
)-1] = 0;
585 assert(!c
->scache_memchunk
.memblock
);
586 c
->scache_memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
587 c
->scache_memchunk
.index
= 0;
588 c
->scache_memchunk
.length
= sc_length
;
589 c
->scache_sample_spec
= ss
;
590 assert(!c
->scache_name
);
591 c
->scache_name
= pa_xstrdup(name
);
593 c
->state
= ESD_CACHING_SAMPLE
;
595 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, NULL
, NULL
, &index
);
597 ok
= connection_write(c
, sizeof(int));
605 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
608 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
609 assert(c
&& data
&& length
== ESD_NAME_MAX
);
611 ok
= connection_write(c
, sizeof(int));
616 strcpy(name
, SCACHE_PREFIX
);
617 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
618 name
[sizeof(name
)-1] = 0;
620 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
621 *ok
= (int) index
+1;
626 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
630 assert(c
&& data
&& length
== sizeof(int));
632 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
634 ok
= connection_write(c
, sizeof(int));
639 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
640 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
641 struct pa_sink
*sink
;
643 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
644 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
647 assert(request
== ESD_PROTO_SAMPLE_FREE
);
649 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
657 /*** client callbacks ***/
659 static void client_kill_cb(struct pa_client
*c
) {
660 assert(c
&& c
->userdata
);
661 connection_free(c
->userdata
);
664 /*** pa_iochannel callbacks ***/
666 static int do_read(struct connection
*c
) {
669 if (c
->state
== ESD_NEXT_REQUEST
) {
671 assert(c
->read_data_length
< sizeof(c
->request
));
673 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
674 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
678 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
679 struct proto_handler
*handler
;
681 if (c
->swap_byte_order
)
682 c
->request
= swap_endian_32(c
->request
);
684 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
685 pa_log(__FILE__
": recieved invalid request.\n");
689 handler
= proto_map
+c
->request
;
691 if (!handler
->proc
) {
692 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
696 if (handler
->data_length
== 0) {
697 c
->read_data_length
= 0;
699 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
703 if (c
->read_data_alloc
< handler
->data_length
)
704 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
705 assert(c
->read_data
);
707 c
->state
= ESD_NEEDS_REQDATA
;
708 c
->read_data_length
= 0;
712 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
714 struct proto_handler
*handler
= proto_map
+c
->request
;
716 assert(handler
->proc
);
718 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
720 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
721 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
725 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
726 size_t l
= c
->read_data_length
;
727 assert(handler
->proc
);
729 c
->state
= ESD_NEXT_REQUEST
;
730 c
->read_data_length
= 0;
732 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
735 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
738 assert(c
->scache_memchunk
.memblock
&& c
->scache_name
&& c
->scache_memchunk
.index
< c
->scache_memchunk
.length
);
740 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) {
741 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
745 c
->scache_memchunk
.index
+= r
;
746 assert(c
->scache_memchunk
.index
<= c
->scache_memchunk
.length
);
748 if (c
->scache_memchunk
.index
== c
->scache_memchunk
.length
) {
752 c
->scache_memchunk
.index
= 0;
753 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, &c
->scache_sample_spec
, &c
->scache_memchunk
, &index
);
755 pa_memblock_unref(c
->scache_memchunk
.memblock
);
756 c
->scache_memchunk
.memblock
= NULL
;
757 c
->scache_memchunk
.index
= c
->scache_memchunk
.length
= 0;
759 pa_xfree(c
->scache_name
);
760 c
->scache_name
= NULL
;
762 c
->state
= ESD_NEXT_REQUEST
;
764 ok
= connection_write(c
, sizeof(int));
769 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
770 struct pa_memchunk chunk
;
774 assert(c
->input_memblockq
);
776 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
779 if (l
> c
->playback
.fragment_size
)
780 l
= c
->playback
.fragment_size
;
782 if (c
->playback
.current_memblock
)
783 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
784 pa_memblock_unref(c
->playback
.current_memblock
);
785 c
->playback
.current_memblock
= NULL
;
786 c
->playback
.memblock_index
= 0;
789 if (!c
->playback
.current_memblock
) {
790 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
791 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
792 c
->playback
.memblock_index
= 0;
795 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
796 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
800 chunk
.memblock
= c
->playback
.current_memblock
;
801 chunk
.index
= c
->playback
.memblock_index
;
803 assert(chunk
.memblock
);
805 c
->playback
.memblock_index
+= r
;
807 assert(c
->input_memblockq
);
808 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
809 assert(c
->sink_input
);
810 pa_sink_notify(c
->sink_input
->sink
);
816 static int do_write(struct connection
*c
) {
819 if (c
->write_data_length
) {
822 assert(c
->write_data_index
< c
->write_data_length
);
823 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) {
824 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
828 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
829 c
->write_data_length
= c
->write_data_index
= 0;
831 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
832 struct pa_memchunk chunk
;
835 assert(c
->output_memblockq
);
836 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
839 assert(chunk
.memblock
&& chunk
.length
);
841 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
842 pa_memblock_unref(chunk
.memblock
);
843 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
847 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
848 pa_memblock_unref(chunk
.memblock
);
854 static void do_work(struct connection
*c
) {
857 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
858 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
860 if (pa_iochannel_is_hungup(c
->io
))
863 if (pa_iochannel_is_writable(c
->io
))
867 if (pa_iochannel_is_readable(c
->io
))
877 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
878 struct connection
*c
= userdata
;
879 assert(io
&& c
&& c
->io
== io
);
884 /*** defer callback ***/
886 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
887 struct connection
*c
= userdata
;
888 assert(a
&& c
&& c
->defer_event
== e
);
893 /*** sink_input callbacks ***/
895 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
897 assert(i
&& i
->userdata
&& chunk
);
900 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
906 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
907 struct connection
*c
= i
->userdata
;
908 assert(i
&& c
&& length
);
910 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
913 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
914 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
916 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
919 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
920 assert(i
&& i
->userdata
);
921 connection_free((struct connection
*) i
->userdata
);
924 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
925 struct connection
*c
= i
->userdata
;
927 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
930 /*** source_output callbacks ***/
932 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
933 struct connection
*c
= o
->userdata
;
934 assert(o
&& c
&& chunk
);
936 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
939 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
940 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
943 static void source_output_kill_cb(struct pa_source_output
*o
) {
944 assert(o
&& o
->userdata
);
945 connection_free((struct connection
*) o
->userdata
);
948 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
949 struct connection
*c
= o
->userdata
;
951 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
954 /*** socket server callback ***/
956 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
957 struct connection
*c
;
959 assert(s
&& io
&& userdata
);
961 c
= pa_xmalloc(sizeof(struct connection
));
962 c
->protocol
= userdata
;
964 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
966 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
967 assert(c
->protocol
->core
);
968 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
970 c
->client
->owner
= c
->protocol
->module
;
971 c
->client
->kill
= client_kill_cb
;
972 c
->client
->userdata
= c
;
974 c
->authorized
= c
->protocol
->public;
975 c
->swap_byte_order
= 0;
977 c
->read_data_length
= 0;
978 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
980 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
981 c
->write_data
= NULL
;
983 c
->state
= ESD_NEEDS_REQDATA
;
984 c
->request
= ESD_PROTO_CONNECT
;
986 c
->sink_input
= NULL
;
987 c
->input_memblockq
= NULL
;
989 c
->source_output
= NULL
;
990 c
->output_memblockq
= NULL
;
992 c
->playback
.current_memblock
= NULL
;
993 c
->playback
.memblock_index
= 0;
994 c
->playback
.fragment_size
= 0;
996 c
->scache_memchunk
.length
= c
->scache_memchunk
.index
= 0;
997 c
->scache_memchunk
.memblock
= NULL
;
998 c
->scache_name
= NULL
;
1000 c
->defer_event
= c
->protocol
->core
->mainloop
->defer_new(c
->protocol
->core
->mainloop
, defer_callback
, c
);
1001 assert(c
->defer_event
);
1002 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1004 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
1007 /*** entry points ***/
1009 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
) {
1010 struct pa_protocol_esound
*p
;
1012 assert(core
&& server
&& ma
);
1014 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1016 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1017 pa_log(__FILE__
": public= expects a boolean argument.\n");
1021 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1029 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1031 p
->connections
= pa_idxset_new(NULL
, NULL
);
1032 assert(p
->connections
);
1034 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1035 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1041 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1042 struct connection
*c
;
1045 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1048 pa_idxset_free(p
->connections
, NULL
, NULL
);
1049 pa_socket_server_unref(p
->server
);