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"
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 */
66 struct pa_protocol_esound
*protocol
;
67 struct pa_iochannel
*io
;
68 struct pa_client
*client
;
69 int authorized
, swap_byte_order
;
71 size_t write_data_alloc
, write_data_index
, write_data_length
;
73 size_t read_data_alloc
, read_data_length
;
75 esd_client_state_t state
;
76 struct pa_sink_input
*sink_input
;
77 struct pa_source_output
*source_output
;
78 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
79 struct pa_defer_event
*defer_event
;
82 struct pa_memblock
*current_memblock
;
83 size_t memblock_index
, fragment_size
;
87 struct pa_memchunk memchunk
;
89 struct pa_sample_spec sample_spec
;
93 struct pa_protocol_esound
{
95 struct pa_module
*module
;
97 struct pa_socket_server
*server
;
98 struct pa_idxset
*connections
;
99 char *sink_name
, *source_name
;
101 uint8_t esd_key
[ESD_KEY_LEN
];
104 typedef struct proto_handler
{
106 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
107 const char *description
;
108 } esd_proto_handler_info_t
;
110 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
111 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
112 static void sink_input_kill_cb(struct pa_sink_input
*i
);
113 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
114 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
116 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
117 static void source_output_kill_cb(struct pa_source_output
*o
);
119 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
120 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
121 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
122 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
123 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
124 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
125 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
126 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
127 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
128 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
129 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
131 /* the big map of protocol handler info */
132 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
133 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
134 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
135 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
137 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
138 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
139 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
141 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
142 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
143 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
144 { sizeof(int), NULL
, "sample loop" },
145 { sizeof(int), NULL
, "sample stop" },
146 { -1, NULL
, "TODO: sample kill" },
148 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
149 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
151 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
152 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
154 { sizeof(int), esd_proto_server_info
, "server info" },
155 { sizeof(int), esd_proto_all_info
, "all info" },
156 { -1, NULL
, "TODO: subscribe" },
157 { -1, NULL
, "TODO: unsubscribe" },
159 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
160 { 3 * sizeof(int), NULL
, "sample pan" },
162 { sizeof(int), NULL
, "standby mode" },
163 { 0, esd_proto_get_latency
, "get latency" }
167 static void connection_free(struct connection
*c
) {
169 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
171 if (c
->state
== ESD_STREAMING_DATA
)
172 c
->protocol
->n_player
--;
174 pa_client_free(c
->client
);
177 pa_sink_input_disconnect(c
->sink_input
);
178 pa_sink_input_unref(c
->sink_input
);
181 if (c
->source_output
) {
182 pa_source_output_disconnect(c
->source_output
);
183 pa_source_output_unref(c
->source_output
);
186 if (c
->input_memblockq
)
187 pa_memblockq_free(c
->input_memblockq
);
188 if (c
->output_memblockq
)
189 pa_memblockq_free(c
->output_memblockq
);
191 if (c
->playback
.current_memblock
)
192 pa_memblock_unref(c
->playback
.current_memblock
);
194 pa_xfree(c
->read_data
);
195 pa_xfree(c
->write_data
);
198 pa_iochannel_free(c
->io
);
201 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
203 if (c
->scache
.memchunk
.memblock
)
204 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
205 pa_xfree(c
->scache
.name
);
210 static void* connection_write(struct connection
*c
, size_t length
) {
214 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
215 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
217 t
= c
->write_data_length
+length
;
219 if (c
->write_data_alloc
< t
)
220 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
222 assert(c
->write_data
);
224 i
= c
->write_data_length
;
225 c
->write_data_length
+= length
;
227 return (uint8_t*) c
->write_data
+i
;
230 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
233 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
234 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
237 static int format_native2esd(struct pa_sample_spec
*ss
) {
240 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
241 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
246 /*** esound commands ***/
248 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
251 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
253 if (!c
->authorized
) {
254 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
255 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
262 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
263 if (ekey
== ESD_ENDIAN_KEY
)
264 c
->swap_byte_order
= 0;
265 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
266 c
->swap_byte_order
= 1;
268 pa_log(__FILE__
": client sent invalid endian key\n");
272 ok
= connection_write(c
, sizeof(int));
278 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
279 char name
[ESD_NAME_MAX
];
281 struct pa_sink
*sink
;
282 struct pa_sample_spec ss
;
284 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
286 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
287 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
290 format_esd2native(format
, &ss
);
292 if (!pa_sample_spec_valid(&ss
))
295 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
296 pa_log(__FILE__
": No output sink\n");
300 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
301 name
[sizeof(name
)-1] = 0;
303 pa_client_set_name(c
->client
, name
);
305 assert(!c
->input_memblockq
);
307 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
308 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
309 assert(c
->input_memblockq
);
310 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
311 c
->playback
.fragment_size
= l
/10;
313 assert(!c
->sink_input
);
314 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
, 0, -1);
315 assert(c
->sink_input
);
317 c
->sink_input
->owner
= c
->protocol
->module
;
318 c
->sink_input
->client
= c
->client
;
319 c
->sink_input
->peek
= sink_input_peek_cb
;
320 c
->sink_input
->drop
= sink_input_drop_cb
;
321 c
->sink_input
->kill
= sink_input_kill_cb
;
322 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
323 c
->sink_input
->userdata
= c
;
325 c
->state
= ESD_STREAMING_DATA
;
327 c
->protocol
->n_player
++;
332 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
333 char name
[ESD_NAME_MAX
];
335 struct pa_source
*source
;
336 struct pa_sample_spec ss
;
338 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
340 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
341 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
344 format_esd2native(format
, &ss
);
346 if (!pa_sample_spec_valid(&ss
))
349 if (request
== ESD_PROTO_STREAM_MON
) {
350 struct pa_sink
* sink
;
352 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
355 if (!(source
= sink
->monitor_source
))
358 assert(request
== ESD_PROTO_STREAM_REC
);
360 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1)))
364 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
365 name
[sizeof(name
)-1] = 0;
367 pa_client_set_name(c
->client
, name
);
369 assert(!c
->output_memblockq
);
371 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
372 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
373 assert(c
->output_memblockq
);
374 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
376 assert(!c
->source_output
);
377 c
->source_output
= pa_source_output_new(source
, name
, &ss
, -1);
378 assert(c
->source_output
);
380 c
->source_output
->owner
= c
->protocol
->module
;
381 c
->source_output
->client
= c
->client
;
382 c
->source_output
->push
= source_output_push_cb
;
383 c
->source_output
->kill
= source_output_kill_cb
;
384 c
->source_output
->get_latency
= source_output_get_latency_cb
;
385 c
->source_output
->userdata
= c
;
387 c
->state
= ESD_STREAMING_DATA
;
389 c
->protocol
->n_player
++;
394 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
395 struct pa_sink
*sink
;
397 assert(c
&& !data
&& length
== 0);
399 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
402 double usec
= pa_sink_get_latency(sink
);
403 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
404 latency
= (int) ((usec
*44100)/1000000);
407 lag
= connection_write(c
, sizeof(int));
409 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
413 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
414 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
416 struct pa_sink
*sink
;
417 assert(c
&& data
&& length
== sizeof(int));
419 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
420 rate
= sink
->sample_spec
.rate
;
421 format
= format_native2esd(&sink
->sample_spec
);
424 response
= connection_write(c
, sizeof(int)*3);
427 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
428 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
432 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
435 struct connection
*conn
;
436 size_t index
= PA_IDXSET_INVALID
;
438 assert(c
&& data
&& length
== sizeof(int));
440 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
443 k
= sizeof(int)*5+ESD_NAME_MAX
;
444 s
= sizeof(int)*6+ESD_NAME_MAX
;
445 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
446 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
449 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
450 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
452 if (conn
->state
!= ESD_STREAMING_DATA
)
457 if (conn
->sink_input
) {
458 rate
= conn
->sink_input
->sample_spec
.rate
;
459 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
460 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
464 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
465 response
+= sizeof(int);
468 assert(conn
->client
);
469 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
470 response
+= ESD_NAME_MAX
;
473 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
474 response
+= sizeof(int);
477 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
478 response
+= sizeof(int);
481 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
482 response
+= sizeof(int);
485 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
486 response
+= sizeof(int);
491 assert(t
== s
*(nsamples
+1)+k
);
492 memset(response
, 0, k
);
497 struct pa_scache_entry
*ce
;
499 index
= PA_IDXSET_INVALID
;
500 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
504 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
505 response
+= sizeof(int);
508 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
509 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
511 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
512 response
+= ESD_NAME_MAX
;
515 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
516 response
+= sizeof(int);
519 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
520 response
+= sizeof(int);
523 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
524 response
+= sizeof(int);
527 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
528 response
+= sizeof(int);
531 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
532 response
+= sizeof(int);
539 memset(response
, 0, s
);
544 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
546 uint32_t index
, volume
;
547 struct connection
*conn
;
548 assert(c
&& data
&& length
== sizeof(int)*3);
550 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
551 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
552 volume
= (volume
*0x100)/0xFF;
554 ok
= connection_write(c
, sizeof(int));
557 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
558 assert(conn
->sink_input
);
559 conn
->sink_input
->volume
= volume
;
567 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
568 struct pa_sample_spec ss
;
573 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
574 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
576 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
577 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
580 format_esd2native(format
, &ss
);
582 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
584 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
587 strcpy(name
, SCACHE_PREFIX
);
588 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
589 name
[sizeof(name
)-1] = 0;
591 assert(!c
->scache
.memchunk
.memblock
);
592 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
593 c
->scache
.memchunk
.index
= 0;
594 c
->scache
.memchunk
.length
= sc_length
;
595 c
->scache
.sample_spec
= ss
;
596 assert(!c
->scache
.name
);
597 c
->scache
.name
= pa_xstrdup(name
);
599 c
->state
= ESD_CACHING_SAMPLE
;
601 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, &index
);
603 ok
= connection_write(c
, sizeof(int));
611 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
614 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
615 assert(c
&& data
&& length
== ESD_NAME_MAX
);
617 ok
= connection_write(c
, sizeof(int));
622 strcpy(name
, SCACHE_PREFIX
);
623 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
624 name
[sizeof(name
)-1] = 0;
626 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
627 *ok
= (int) index
+1;
632 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
636 assert(c
&& data
&& length
== sizeof(int));
638 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
640 ok
= connection_write(c
, sizeof(int));
645 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
646 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
647 struct pa_sink
*sink
;
649 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
650 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
653 assert(request
== ESD_PROTO_SAMPLE_FREE
);
655 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
663 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
665 ok
= connection_write(c
, sizeof(int)*2);
672 /*** client callbacks ***/
674 static void client_kill_cb(struct pa_client
*c
) {
675 assert(c
&& c
->userdata
);
676 connection_free(c
->userdata
);
679 /*** pa_iochannel callbacks ***/
681 static int do_read(struct connection
*c
) {
684 /* pa_log("READ\n"); */
686 if (c
->state
== ESD_NEXT_REQUEST
) {
688 assert(c
->read_data_length
< sizeof(c
->request
));
690 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
691 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
695 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
696 struct proto_handler
*handler
;
698 if (c
->swap_byte_order
)
699 c
->request
= swap_endian_32(c
->request
);
701 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
702 pa_log(__FILE__
": recieved invalid request.\n");
706 handler
= proto_map
+c
->request
;
708 /* pa_log(__FILE__": executing request #%u\n", c->request); */
710 if (!handler
->proc
) {
711 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
715 if (handler
->data_length
== 0) {
716 c
->read_data_length
= 0;
718 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
722 if (c
->read_data_alloc
< handler
->data_length
)
723 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
724 assert(c
->read_data
);
726 c
->state
= ESD_NEEDS_REQDATA
;
727 c
->read_data_length
= 0;
731 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
733 struct proto_handler
*handler
= proto_map
+c
->request
;
735 assert(handler
->proc
);
737 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
739 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
740 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
744 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
745 size_t l
= c
->read_data_length
;
746 assert(handler
->proc
);
748 c
->state
= ESD_NEXT_REQUEST
;
749 c
->read_data_length
= 0;
751 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
754 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
757 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
759 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) {
760 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
764 c
->scache
.memchunk
.index
+= r
;
765 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
767 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
771 c
->scache
.memchunk
.index
= 0;
772 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, &c
->scache
.memchunk
, &index
);
774 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
775 c
->scache
.memchunk
.memblock
= NULL
;
776 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
778 pa_xfree(c
->scache
.name
);
779 c
->scache
.name
= NULL
;
781 c
->state
= ESD_NEXT_REQUEST
;
783 ok
= connection_write(c
, sizeof(int));
788 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
789 struct pa_memchunk chunk
;
793 assert(c
->input_memblockq
);
795 /* pa_log("STREAMING_DATA\n"); */
797 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
800 if (l
> c
->playback
.fragment_size
)
801 l
= c
->playback
.fragment_size
;
803 if (c
->playback
.current_memblock
)
804 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
805 pa_memblock_unref(c
->playback
.current_memblock
);
806 c
->playback
.current_memblock
= NULL
;
807 c
->playback
.memblock_index
= 0;
810 if (!c
->playback
.current_memblock
) {
811 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
812 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
813 c
->playback
.memblock_index
= 0;
816 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
817 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
821 /* pa_log(__FILE__": read %u\n", r); */
823 chunk
.memblock
= c
->playback
.current_memblock
;
824 chunk
.index
= c
->playback
.memblock_index
;
826 assert(chunk
.memblock
);
828 c
->playback
.memblock_index
+= r
;
830 assert(c
->input_memblockq
);
831 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
832 assert(c
->sink_input
);
833 pa_sink_notify(c
->sink_input
->sink
);
839 static int do_write(struct connection
*c
) {
842 /* pa_log("WRITE\n"); */
844 if (c
->write_data_length
) {
847 assert(c
->write_data_index
< c
->write_data_length
);
848 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) {
849 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
853 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
854 c
->write_data_length
= c
->write_data_index
= 0;
856 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
857 struct pa_memchunk chunk
;
860 assert(c
->output_memblockq
);
861 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
864 assert(chunk
.memblock
&& chunk
.length
);
866 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
867 pa_memblock_unref(chunk
.memblock
);
868 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
872 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
873 pa_memblock_unref(chunk
.memblock
);
879 static void do_work(struct connection
*c
) {
882 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
883 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
885 /* pa_log("DOWORK\n"); */
887 if (c
->dead
|| !c
->io
)
890 if (pa_iochannel_is_readable(c
->io
))
894 if (pa_iochannel_is_writable(c
->io
))
902 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
904 pa_memblockq_prebuf_disable(c
->input_memblockq
);
906 pa_iochannel_free(c
->io
);
913 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
914 struct connection
*c
= userdata
;
915 assert(io
&& c
&& c
->io
== io
);
917 /* pa_log("IO\n"); */
922 /*** defer callback ***/
924 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
925 struct connection
*c
= userdata
;
926 assert(a
&& c
&& c
->defer_event
== e
);
928 /* pa_log("DEFER\n"); */
933 /*** sink_input callbacks ***/
935 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
937 assert(i
&& i
->userdata
&& chunk
);
940 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
951 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
952 struct connection
*c
= i
->userdata
;
953 assert(i
&& c
&& length
);
955 /* pa_log("DROP\n"); */
957 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
960 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
963 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
965 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
968 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
969 assert(i
&& i
->userdata
);
970 connection_free((struct connection
*) i
->userdata
);
973 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
974 struct connection
*c
= i
->userdata
;
976 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
979 /*** source_output callbacks ***/
981 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
982 struct connection
*c
= o
->userdata
;
983 assert(o
&& c
&& chunk
);
985 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
988 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
991 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
994 static void source_output_kill_cb(struct pa_source_output
*o
) {
995 assert(o
&& o
->userdata
);
996 connection_free((struct connection
*) o
->userdata
);
999 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
1000 struct connection
*c
= o
->userdata
;
1002 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1005 /*** socket server callback ***/
1007 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1008 struct connection
*c
;
1010 assert(s
&& io
&& userdata
);
1012 c
= pa_xmalloc(sizeof(struct connection
));
1013 c
->protocol
= userdata
;
1015 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1017 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1018 assert(c
->protocol
->core
);
1019 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
1021 c
->client
->owner
= c
->protocol
->module
;
1022 c
->client
->kill
= client_kill_cb
;
1023 c
->client
->userdata
= c
;
1025 c
->authorized
= c
->protocol
->public;
1026 c
->swap_byte_order
= 0;
1029 c
->read_data_length
= 0;
1030 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1032 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1033 c
->write_data
= NULL
;
1035 c
->state
= ESD_NEEDS_REQDATA
;
1036 c
->request
= ESD_PROTO_CONNECT
;
1038 c
->sink_input
= NULL
;
1039 c
->input_memblockq
= NULL
;
1041 c
->source_output
= NULL
;
1042 c
->output_memblockq
= NULL
;
1044 c
->playback
.current_memblock
= NULL
;
1045 c
->playback
.memblock_index
= 0;
1046 c
->playback
.fragment_size
= 0;
1048 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1049 c
->scache
.memchunk
.memblock
= NULL
;
1050 c
->scache
.name
= NULL
;
1052 c
->defer_event
= c
->protocol
->core
->mainloop
->defer_new(c
->protocol
->core
->mainloop
, defer_callback
, c
);
1053 assert(c
->defer_event
);
1054 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1056 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
1059 /*** entry points ***/
1061 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
) {
1062 struct pa_protocol_esound
*p
;
1064 assert(core
&& server
&& ma
);
1066 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1068 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1069 pa_log(__FILE__
": public= expects a boolean argument.\n");
1073 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1081 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1083 p
->connections
= pa_idxset_new(NULL
, NULL
);
1084 assert(p
->connections
);
1086 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1087 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1093 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1094 struct connection
*c
;
1097 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1100 pa_idxset_free(p
->connections
, NULL
, NULL
);
1101 pa_socket_server_unref(p
->server
);