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
);
125 static int esd_proto_noop(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
127 /* the big map of protocol handler info */
128 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
129 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
130 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
131 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
133 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
134 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
135 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
137 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" },
138 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
139 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" },
140 { sizeof(int), NULL
, "sample loop" },
141 { sizeof(int), NULL
, "sample stop" },
142 { -1, NULL
, "TODO: sample kill" },
144 { ESD_KEY_LEN
+ sizeof(int), esd_proto_noop
, "standby" }, /* NOOP! */
145 { ESD_KEY_LEN
+ sizeof(int), esd_proto_noop
, "resume" }, /* NOOP! */
147 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" },
148 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
150 { sizeof(int), esd_proto_server_info
, "server info" },
151 { sizeof(int), esd_proto_all_info
, "all info" },
152 { -1, NULL
, "TODO: subscribe" },
153 { -1, NULL
, "TODO: unsubscribe" },
155 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
156 { 3 * sizeof(int), NULL
, "sample pan" },
158 { sizeof(int), NULL
, "standby mode" },
159 { 0, esd_proto_get_latency
, "get latency" }
163 static void connection_free(struct connection
*c
) {
165 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
167 if (c
->state
== ESD_STREAMING_DATA
)
168 c
->protocol
->n_player
--;
170 pa_client_free(c
->client
);
173 pa_sink_input_disconnect(c
->sink_input
);
174 pa_sink_input_unref(c
->sink_input
);
177 if (c
->source_output
) {
178 pa_source_output_disconnect(c
->source_output
);
179 pa_source_output_unref(c
->source_output
);
182 if (c
->input_memblockq
)
183 pa_memblockq_free(c
->input_memblockq
);
184 if (c
->output_memblockq
)
185 pa_memblockq_free(c
->output_memblockq
);
187 if (c
->playback
.current_memblock
)
188 pa_memblock_unref(c
->playback
.current_memblock
);
190 pa_xfree(c
->read_data
);
191 pa_xfree(c
->write_data
);
193 pa_iochannel_free(c
->io
);
196 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
198 if (c
->scache_memchunk
.memblock
)
199 pa_memblock_unref(c
->scache_memchunk
.memblock
);
200 pa_xfree(c
->scache_name
);
205 static void* connection_write(struct connection
*c
, size_t length
) {
209 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
210 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
212 t
= c
->write_data_length
+length
;
214 if (c
->write_data_alloc
< t
)
215 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
217 assert(c
->write_data
);
219 i
= c
->write_data_length
;
220 c
->write_data_length
+= length
;
222 return (uint8_t*) c
->write_data
+i
;
225 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
228 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
229 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
232 static int format_native2esd(struct pa_sample_spec
*ss
) {
235 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
236 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
241 /*** esound commands ***/
243 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
246 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
248 if (!c
->authorized
) {
249 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
250 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
257 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
258 if (ekey
== ESD_ENDIAN_KEY
)
259 c
->swap_byte_order
= 0;
260 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
261 c
->swap_byte_order
= 1;
263 pa_log(__FILE__
": client sent invalid endian key\n");
267 ok
= connection_write(c
, sizeof(int));
273 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
274 char name
[ESD_NAME_MAX
];
276 struct pa_sink
*sink
;
277 struct pa_sample_spec ss
;
279 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
281 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
282 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
285 format_esd2native(format
, &ss
);
287 if (!pa_sample_spec_valid(&ss
))
290 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
291 pa_log(__FILE__
": No output sink\n");
295 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
296 name
[sizeof(name
)-1] = 0;
298 pa_client_set_name(c
->client
, name
);
300 assert(!c
->input_memblockq
);
302 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
303 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
304 assert(c
->input_memblockq
);
305 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
306 c
->playback
.fragment_size
= l
/10;
308 assert(!c
->sink_input
);
309 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
, 0, -1);
310 assert(c
->sink_input
);
312 c
->sink_input
->owner
= c
->protocol
->module
;
313 c
->sink_input
->client
= c
->client
;
314 c
->sink_input
->peek
= sink_input_peek_cb
;
315 c
->sink_input
->drop
= sink_input_drop_cb
;
316 c
->sink_input
->kill
= sink_input_kill_cb
;
317 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
318 c
->sink_input
->userdata
= c
;
320 c
->state
= ESD_STREAMING_DATA
;
322 c
->protocol
->n_player
++;
327 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
328 char name
[ESD_NAME_MAX
];
330 struct pa_source
*source
;
331 struct pa_sample_spec ss
;
333 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
335 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
336 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
339 format_esd2native(format
, &ss
);
341 if (!pa_sample_spec_valid(&ss
))
344 if (request
== ESD_PROTO_STREAM_MON
) {
345 struct pa_sink
* sink
;
347 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
350 if (!(source
= sink
->monitor_source
))
353 assert(request
== ESD_PROTO_STREAM_REC
);
355 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1)))
359 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
360 name
[sizeof(name
)-1] = 0;
362 pa_client_set_name(c
->client
, name
);
364 assert(!c
->output_memblockq
);
366 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
367 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
368 assert(c
->output_memblockq
);
369 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
371 assert(!c
->source_output
);
372 c
->source_output
= pa_source_output_new(source
, name
, &ss
, -1);
373 assert(c
->source_output
);
375 c
->source_output
->owner
= c
->protocol
->module
;
376 c
->source_output
->client
= c
->client
;
377 c
->source_output
->push
= source_output_push_cb
;
378 c
->source_output
->kill
= source_output_kill_cb
;
379 c
->source_output
->get_latency
= source_output_get_latency_cb
;
380 c
->source_output
->userdata
= c
;
382 c
->state
= ESD_STREAMING_DATA
;
384 c
->protocol
->n_player
++;
389 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
390 struct pa_sink
*sink
;
392 assert(c
&& !data
&& length
== 0);
394 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
397 double usec
= pa_sink_get_latency(sink
);
398 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
399 latency
= (int) ((usec
*44100)/1000000);
402 lag
= connection_write(c
, sizeof(int));
404 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
408 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
409 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
411 struct pa_sink
*sink
;
412 assert(c
&& data
&& length
== sizeof(int));
414 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
415 rate
= sink
->sample_spec
.rate
;
416 format
= format_native2esd(&sink
->sample_spec
);
419 response
= connection_write(c
, sizeof(int)*3);
422 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
423 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
427 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
430 struct connection
*conn
;
431 size_t index
= PA_IDXSET_INVALID
;
433 assert(c
&& data
&& length
== sizeof(int));
435 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
438 k
= sizeof(int)*5+ESD_NAME_MAX
;
439 s
= sizeof(int)*6+ESD_NAME_MAX
;
440 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
441 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
444 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
445 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
447 if (conn
->state
!= ESD_STREAMING_DATA
)
452 if (conn
->sink_input
) {
453 rate
= conn
->sink_input
->sample_spec
.rate
;
454 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
455 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
459 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
460 response
+= sizeof(int);
463 assert(conn
->client
);
464 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
465 response
+= ESD_NAME_MAX
;
468 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
469 response
+= sizeof(int);
472 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
473 response
+= sizeof(int);
476 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
477 response
+= sizeof(int);
480 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
481 response
+= sizeof(int);
486 assert(t
== s
*(nsamples
+1)+k
);
487 memset(response
, 0, k
);
492 struct pa_scache_entry
*ce
;
494 index
= PA_IDXSET_INVALID
;
495 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
499 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
500 response
+= sizeof(int);
503 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
504 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
506 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
507 response
+= ESD_NAME_MAX
;
510 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
511 response
+= sizeof(int);
514 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
515 response
+= sizeof(int);
518 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
519 response
+= sizeof(int);
522 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
523 response
+= sizeof(int);
526 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
527 response
+= sizeof(int);
534 memset(response
, 0, s
);
539 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
541 uint32_t index
, volume
;
542 struct connection
*conn
;
543 assert(c
&& data
&& length
== sizeof(int)*3);
545 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
546 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
547 volume
= (volume
*0x100)/0xFF;
549 ok
= connection_write(c
, sizeof(int));
552 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
553 assert(conn
->sink_input
);
554 conn
->sink_input
->volume
= volume
;
562 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
563 struct pa_sample_spec ss
;
568 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
569 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
571 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
572 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
575 format_esd2native(format
, &ss
);
577 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
579 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
582 strcpy(name
, SCACHE_PREFIX
);
583 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
584 name
[sizeof(name
)-1] = 0;
586 assert(!c
->scache_memchunk
.memblock
);
587 c
->scache_memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
588 c
->scache_memchunk
.index
= 0;
589 c
->scache_memchunk
.length
= sc_length
;
590 c
->scache_sample_spec
= ss
;
591 assert(!c
->scache_name
);
592 c
->scache_name
= pa_xstrdup(name
);
594 c
->state
= ESD_CACHING_SAMPLE
;
596 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, NULL
, NULL
, &index
);
598 ok
= connection_write(c
, sizeof(int));
606 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
609 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
610 assert(c
&& data
&& length
== ESD_NAME_MAX
);
612 ok
= connection_write(c
, sizeof(int));
617 strcpy(name
, SCACHE_PREFIX
);
618 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
619 name
[sizeof(name
)-1] = 0;
621 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
622 *ok
= (int) index
+1;
627 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
631 assert(c
&& data
&& length
== sizeof(int));
633 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
635 ok
= connection_write(c
, sizeof(int));
640 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
641 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
642 struct pa_sink
*sink
;
644 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
645 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
648 assert(request
== ESD_PROTO_SAMPLE_FREE
);
650 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
658 static int esd_proto_noop(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
660 ok
= connection_write(c
, sizeof(int));
666 /*** client callbacks ***/
668 static void client_kill_cb(struct pa_client
*c
) {
669 assert(c
&& c
->userdata
);
670 connection_free(c
->userdata
);
673 /*** pa_iochannel callbacks ***/
675 static int do_read(struct connection
*c
) {
678 if (c
->state
== ESD_NEXT_REQUEST
) {
680 assert(c
->read_data_length
< sizeof(c
->request
));
682 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
683 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
687 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
688 struct proto_handler
*handler
;
690 if (c
->swap_byte_order
)
691 c
->request
= swap_endian_32(c
->request
);
693 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
694 pa_log(__FILE__
": recieved invalid request.\n");
698 handler
= proto_map
+c
->request
;
700 if (!handler
->proc
) {
701 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
705 if (handler
->data_length
== 0) {
706 c
->read_data_length
= 0;
708 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
712 if (c
->read_data_alloc
< handler
->data_length
)
713 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
714 assert(c
->read_data
);
716 c
->state
= ESD_NEEDS_REQDATA
;
717 c
->read_data_length
= 0;
721 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
723 struct proto_handler
*handler
= proto_map
+c
->request
;
725 assert(handler
->proc
);
727 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
729 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
730 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
734 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
735 size_t l
= c
->read_data_length
;
736 assert(handler
->proc
);
738 c
->state
= ESD_NEXT_REQUEST
;
739 c
->read_data_length
= 0;
741 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
744 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
747 assert(c
->scache_memchunk
.memblock
&& c
->scache_name
&& c
->scache_memchunk
.index
< c
->scache_memchunk
.length
);
749 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) {
750 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
754 c
->scache_memchunk
.index
+= r
;
755 assert(c
->scache_memchunk
.index
<= c
->scache_memchunk
.length
);
757 if (c
->scache_memchunk
.index
== c
->scache_memchunk
.length
) {
761 c
->scache_memchunk
.index
= 0;
762 pa_scache_add_item(c
->protocol
->core
, c
->scache_name
, &c
->scache_sample_spec
, &c
->scache_memchunk
, &index
);
764 pa_memblock_unref(c
->scache_memchunk
.memblock
);
765 c
->scache_memchunk
.memblock
= NULL
;
766 c
->scache_memchunk
.index
= c
->scache_memchunk
.length
= 0;
768 pa_xfree(c
->scache_name
);
769 c
->scache_name
= NULL
;
771 c
->state
= ESD_NEXT_REQUEST
;
773 ok
= connection_write(c
, sizeof(int));
778 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
779 struct pa_memchunk chunk
;
783 assert(c
->input_memblockq
);
785 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
788 if (l
> c
->playback
.fragment_size
)
789 l
= c
->playback
.fragment_size
;
791 if (c
->playback
.current_memblock
)
792 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
793 pa_memblock_unref(c
->playback
.current_memblock
);
794 c
->playback
.current_memblock
= NULL
;
795 c
->playback
.memblock_index
= 0;
798 if (!c
->playback
.current_memblock
) {
799 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
800 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
801 c
->playback
.memblock_index
= 0;
804 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
805 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
809 chunk
.memblock
= c
->playback
.current_memblock
;
810 chunk
.index
= c
->playback
.memblock_index
;
812 assert(chunk
.memblock
);
814 c
->playback
.memblock_index
+= r
;
816 assert(c
->input_memblockq
);
817 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
818 assert(c
->sink_input
);
819 pa_sink_notify(c
->sink_input
->sink
);
825 static int do_write(struct connection
*c
) {
828 if (c
->write_data_length
) {
831 assert(c
->write_data_index
< c
->write_data_length
);
832 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) {
833 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
837 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
838 c
->write_data_length
= c
->write_data_index
= 0;
840 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
841 struct pa_memchunk chunk
;
844 assert(c
->output_memblockq
);
845 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
848 assert(chunk
.memblock
&& chunk
.length
);
850 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
851 pa_memblock_unref(chunk
.memblock
);
852 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
856 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
857 pa_memblock_unref(chunk
.memblock
);
863 static void do_work(struct connection
*c
) {
866 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
867 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
869 if (pa_iochannel_is_hungup(c
->io
))
872 if (pa_iochannel_is_writable(c
->io
))
876 if (pa_iochannel_is_readable(c
->io
))
886 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
887 struct connection
*c
= userdata
;
888 assert(io
&& c
&& c
->io
== io
);
893 /*** defer callback ***/
895 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
896 struct connection
*c
= userdata
;
897 assert(a
&& c
&& c
->defer_event
== e
);
902 /*** sink_input callbacks ***/
904 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
906 assert(i
&& i
->userdata
&& chunk
);
909 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
915 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
916 struct connection
*c
= i
->userdata
;
917 assert(i
&& c
&& length
);
919 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
922 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
923 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
925 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
928 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
929 assert(i
&& i
->userdata
);
930 connection_free((struct connection
*) i
->userdata
);
933 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
934 struct connection
*c
= i
->userdata
;
936 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
939 /*** source_output callbacks ***/
941 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
942 struct connection
*c
= o
->userdata
;
943 assert(o
&& c
&& chunk
);
945 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
948 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
949 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
952 static void source_output_kill_cb(struct pa_source_output
*o
) {
953 assert(o
&& o
->userdata
);
954 connection_free((struct connection
*) o
->userdata
);
957 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
958 struct connection
*c
= o
->userdata
;
960 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
963 /*** socket server callback ***/
965 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
966 struct connection
*c
;
968 assert(s
&& io
&& userdata
);
970 c
= pa_xmalloc(sizeof(struct connection
));
971 c
->protocol
= userdata
;
973 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
975 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
976 assert(c
->protocol
->core
);
977 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
979 c
->client
->owner
= c
->protocol
->module
;
980 c
->client
->kill
= client_kill_cb
;
981 c
->client
->userdata
= c
;
983 c
->authorized
= c
->protocol
->public;
984 c
->swap_byte_order
= 0;
986 c
->read_data_length
= 0;
987 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
989 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
990 c
->write_data
= NULL
;
992 c
->state
= ESD_NEEDS_REQDATA
;
993 c
->request
= ESD_PROTO_CONNECT
;
995 c
->sink_input
= NULL
;
996 c
->input_memblockq
= NULL
;
998 c
->source_output
= NULL
;
999 c
->output_memblockq
= NULL
;
1001 c
->playback
.current_memblock
= NULL
;
1002 c
->playback
.memblock_index
= 0;
1003 c
->playback
.fragment_size
= 0;
1005 c
->scache_memchunk
.length
= c
->scache_memchunk
.index
= 0;
1006 c
->scache_memchunk
.memblock
= NULL
;
1007 c
->scache_name
= NULL
;
1009 c
->defer_event
= c
->protocol
->core
->mainloop
->defer_new(c
->protocol
->core
->mainloop
, defer_callback
, c
);
1010 assert(c
->defer_event
);
1011 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1013 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
1016 /*** entry points ***/
1018 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
) {
1019 struct pa_protocol_esound
*p
;
1021 assert(core
&& server
&& ma
);
1023 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1025 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1026 pa_log(__FILE__
": public= expects a boolean argument.\n");
1030 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1038 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1040 p
->connections
= pa_idxset_new(NULL
, NULL
);
1041 assert(p
->connections
);
1043 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1044 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1050 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1051 struct connection
*c
;
1054 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1057 pa_idxset_free(p
->connections
, NULL
, NULL
);
1058 pa_socket_server_unref(p
->server
);