8 #include "protocol-esound.h"
9 #include "esound-spec.h"
12 #include "sinkinput.h"
14 #include "sourceoutput.h"
20 #define COOKIE_FILE ".esd_auth"
22 #define PLAYBACK_BUFFER_SECONDS (.5)
23 #define PLAYBACK_BUFFER_FRAGMENTS (10)
24 #define RECORD_BUFFER_SECONDS (5)
25 #define RECORD_BUFFER_FRAGMENTS (100)
27 /* This is heavily based on esound's code */
31 struct pa_protocol_esound
*protocol
;
32 struct pa_iochannel
*io
;
33 struct pa_client
*client
;
34 int authorized
, swap_byte_order
;
36 size_t write_data_alloc
, write_data_index
, write_data_length
;
38 size_t read_data_alloc
, read_data_length
;
40 esd_client_state_t state
;
41 struct pa_sink_input
*sink_input
;
42 struct pa_source_output
*source_output
;
43 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
46 struct pa_memblock
*current_memblock
;
47 size_t memblock_index
, fragment_size
;
51 struct pa_protocol_esound
{
54 struct pa_socket_server
*server
;
55 struct pa_idxset
*connections
;
56 uint32_t sink_index
, source_index
;
58 uint8_t esd_key
[ESD_KEY_LEN
];
61 typedef struct proto_handler
{
63 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
64 const char *description
;
65 } esd_proto_handler_info_t
;
67 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
);
68 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
69 static void sink_input_kill_cb(struct pa_sink_input
*i
);
70 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
);
72 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
73 static void source_output_kill_cb(struct pa_source_output
*o
);
75 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
76 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
77 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
78 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
79 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
80 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
81 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
83 /* the big map of protocol handler info */
84 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
85 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
86 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
87 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
89 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
90 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
91 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
93 { ESD_NAME_MAX
+ 3 * sizeof(int), NULL
, "sample cache" },
94 { sizeof(int), NULL
, "sample free" },
95 { sizeof(int), NULL
, "sample play" },
96 { sizeof(int), NULL
, "sample loop" },
97 { sizeof(int), NULL
, "sample stop" },
98 { -1, NULL
, "TODO: sample kill" },
100 { ESD_KEY_LEN
+ sizeof(int), NULL
, "standby" },
101 { ESD_KEY_LEN
+ sizeof(int), NULL
, "resume" },
103 { ESD_NAME_MAX
, NULL
, "sample getid" },
104 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
106 { sizeof(int), esd_proto_server_info
, "server info" },
107 { sizeof(int), esd_proto_all_info
, "all info" },
108 { -1, NULL
, "TODO: subscribe" },
109 { -1, NULL
, "TODO: unsubscribe" },
111 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
112 { 3 * sizeof(int), NULL
, "sample pan" },
114 { sizeof(int), NULL
, "standby mode" },
115 { 0, esd_proto_get_latency
, "get latency" }
119 static void connection_free(struct connection
*c
) {
121 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
123 if (c
->state
== ESD_STREAMING_DATA
)
124 c
->protocol
->n_player
--;
126 pa_client_free(c
->client
);
129 pa_sink_input_free(c
->sink_input
);
130 if (c
->source_output
)
131 pa_source_output_free(c
->source_output
);
132 if (c
->input_memblockq
)
133 pa_memblockq_free(c
->input_memblockq
);
134 if (c
->output_memblockq
)
135 pa_memblockq_free(c
->output_memblockq
);
137 if (c
->playback
.current_memblock
)
138 pa_memblock_unref(c
->playback
.current_memblock
);
143 pa_iochannel_free(c
->io
);
146 c
->protocol
->core
->mainloop
->cancel_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
);
151 static struct pa_sink
* get_output_sink(struct pa_protocol_esound
*p
) {
155 if (!(s
= pa_idxset_get_by_index(p
->core
->sinks
, p
->sink_index
)))
156 s
= pa_sink_get_default(p
->core
);
158 p
->sink_index
= s
? s
->index
: PA_IDXSET_INVALID
;
162 static struct pa_source
* get_input_source(struct pa_protocol_esound
*p
) {
166 if (!(s
= pa_idxset_get_by_index(p
->core
->sources
, p
->sink_index
)))
167 s
= pa_source_get_default(p
->core
);
169 p
->source_index
= s
? s
->index
: PA_IDXSET_INVALID
;
173 static void* connection_write(struct connection
*c
, size_t length
) {
177 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
178 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 1);
180 t
= c
->write_data_length
+length
;
182 if (c
->write_data_alloc
< t
)
183 c
->write_data
= realloc(c
->write_data
, c
->write_data_alloc
= t
);
185 assert(c
->write_data
);
187 i
= c
->write_data_length
;
188 c
->write_data_length
+= length
;
190 return c
->write_data
+i
;
193 /*** esound commands ***/
195 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
198 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
200 if (!c
->authorized
) {
201 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
202 fprintf(stderr
, __FILE__
": kicked client with invalid authorization key.\n");
209 ekey
= *(uint32_t*)(data
+ESD_KEY_LEN
);
210 if (ekey
== ESD_ENDIAN_KEY
)
211 c
->swap_byte_order
= 0;
212 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
213 c
->swap_byte_order
= 1;
215 fprintf(stderr
, __FILE__
": client sent invalid endian key\n");
219 ok
= connection_write(c
, sizeof(int));
225 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
226 char name
[ESD_NAME_MAX
];
228 struct pa_sink
*sink
;
229 struct pa_sample_spec ss
;
231 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
233 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
234 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
237 ss
.channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
238 ss
.format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
240 if (!pa_sample_spec_valid(&ss
))
243 if (!(sink
= get_output_sink(c
->protocol
)))
246 strncpy(name
, data
+ sizeof(int)*2, sizeof(name
));
247 name
[sizeof(name
)-1] = 0;
249 pa_client_rename(c
->client
, name
);
251 assert(!c
->input_memblockq
);
253 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
254 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_sample_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
);
255 assert(c
->input_memblockq
);
256 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
257 c
->playback
.fragment_size
= l
/10;
259 assert(!c
->sink_input
);
260 c
->sink_input
= pa_sink_input_new(sink
, name
, &ss
);
261 assert(c
->sink_input
);
263 c
->sink_input
->peek
= sink_input_peek_cb
;
264 c
->sink_input
->drop
= sink_input_drop_cb
;
265 c
->sink_input
->kill
= sink_input_kill_cb
;
266 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
267 c
->sink_input
->userdata
= c
;
269 c
->state
= ESD_STREAMING_DATA
;
271 c
->protocol
->n_player
++;
276 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
277 char name
[ESD_NAME_MAX
];
279 struct pa_source
*source
;
280 struct pa_sample_spec ss
;
282 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
284 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
285 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
288 ss
.channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
289 ss
.format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
291 if (!pa_sample_spec_valid(&ss
))
294 if (request
== ESD_PROTO_STREAM_MON
) {
295 struct pa_sink
* sink
;
297 if (!(sink
= get_output_sink(c
->protocol
)))
300 if (!(source
= sink
->monitor_source
))
303 assert(request
== ESD_PROTO_STREAM_REC
);
305 if (!(source
= get_input_source(c
->protocol
)))
309 strncpy(name
, data
+ sizeof(int)*2, sizeof(name
));
310 name
[sizeof(name
)-1] = 0;
312 pa_client_rename(c
->client
, name
);
314 assert(!c
->output_memblockq
);
316 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
317 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_sample_size(&ss
), 0, 0);
318 assert(c
->output_memblockq
);
319 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
321 assert(!c
->source_output
);
322 c
->source_output
= pa_source_output_new(source
, name
, &ss
);
323 assert(c
->source_output
);
325 c
->source_output
->push
= source_output_push_cb
;
326 c
->source_output
->kill
= source_output_kill_cb
;
327 c
->source_output
->userdata
= c
;
329 c
->state
= ESD_STREAMING_DATA
;
331 c
->protocol
->n_player
++;
336 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
337 struct pa_sink
*sink
;
339 assert(c
&& !data
&& length
== 0);
341 if (!(sink
= get_output_sink(c
->protocol
)))
344 float usec
= pa_sink_get_latency(sink
);
345 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000*.9; /* A better estimation would be a good idea! */
346 latency
= (int) ((usec
*44100)/1000000);
349 lag
= connection_write(c
, sizeof(int));
351 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
355 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
356 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
358 struct pa_sink
*sink
;
359 assert(c
&& data
&& length
== sizeof(int));
361 if ((sink
= get_output_sink(c
->protocol
))) {
362 rate
= sink
->sample_spec
.rate
;
363 format
= (sink
->sample_spec
.format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
364 format
|= (sink
->sample_spec
.channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
367 response
= connection_write(c
, sizeof(int)*3);
370 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
371 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
375 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
378 struct connection
*conn
;
379 size_t index
= PA_IDXSET_INVALID
;
380 assert(c
&& data
&& length
== sizeof(int));
382 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
385 k
= sizeof(int)*5+ESD_NAME_MAX
;
386 s
= sizeof(int)*6+ESD_NAME_MAX
;
387 response
= connection_write(c
, (t
= s
+k
*(c
->protocol
->n_player
+1)));
390 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
391 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
393 if (conn
->state
!= ESD_STREAMING_DATA
)
398 if (conn
->sink_input
) {
399 rate
= conn
->sink_input
->sample_spec
.rate
;
400 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
401 format
= (conn
->sink_input
->sample_spec
.format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
402 format
|= (conn
->sink_input
->sample_spec
.channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
406 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) conn
->index
);
407 response
+= sizeof(int);
410 assert(conn
->client
);
411 strncpy(response
, conn
->client
->name
, ESD_NAME_MAX
);
412 response
+= ESD_NAME_MAX
;
415 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
416 response
+= sizeof(int);
419 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
420 response
+= sizeof(int);
423 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
424 response
+= sizeof(int);
427 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
428 response
+= sizeof(int);
434 memset(response
, 0, t
);
438 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
440 uint32_t index
, volume
;
441 struct connection
*conn
;
442 assert(c
&& data
&& length
== sizeof(int)*3);
444 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
445 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
446 volume
= (volume
*0x100)/0xFF;
448 ok
= connection_write(c
, sizeof(int));
451 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
452 assert(conn
->sink_input
);
453 conn
->sink_input
->volume
= volume
;
461 /*** client callbacks ***/
463 static void client_kill_cb(struct pa_client
*c
) {
464 assert(c
&& c
->userdata
);
465 connection_free(c
->userdata
);
468 /*** pa_iochannel callbacks ***/
470 static int do_read(struct connection
*c
) {
473 if (c
->state
== ESD_NEXT_REQUEST
) {
475 assert(c
->read_data_length
< sizeof(c
->request
));
477 if ((r
= pa_iochannel_read(c
->io
, ((void*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
478 fprintf(stderr
, "protocol-esound.c: read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
482 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
483 struct proto_handler
*handler
;
485 if (c
->swap_byte_order
)
486 c
->request
= swap_endian_32(c
->request
);
488 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
489 fprintf(stderr
, "protocol-esound.c: recieved invalid request.\n");
493 handler
= proto_map
+c
->request
;
495 if (!handler
->proc
) {
496 fprintf(stderr
, "protocol-sound.c: recieved unimplemented request.\n");
500 if (handler
->data_length
== 0) {
501 c
->read_data_length
= 0;
503 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
507 if (c
->read_data_alloc
< handler
->data_length
)
508 c
->read_data
= realloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
509 assert(c
->read_data
);
511 c
->state
= ESD_NEEDS_REQDATA
;
512 c
->read_data_length
= 0;
516 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
518 struct proto_handler
*handler
= proto_map
+c
->request
;
520 assert(handler
->proc
);
522 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
524 if ((r
= pa_iochannel_read(c
->io
, c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
525 fprintf(stderr
, "protocol-esound.c: read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
529 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
530 size_t l
= c
->read_data_length
;
531 assert(handler
->proc
);
533 c
->state
= ESD_NEXT_REQUEST
;
534 c
->read_data_length
= 0;
536 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
539 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
540 struct pa_memchunk chunk
;
544 assert(c
->input_memblockq
);
546 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
549 if (l
> c
->playback
.fragment_size
)
550 l
= c
->playback
.fragment_size
;
552 if (c
->playback
.current_memblock
)
553 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
554 pa_memblock_unref(c
->playback
.current_memblock
);
555 c
->playback
.current_memblock
= NULL
;
556 c
->playback
.memblock_index
= 0;
560 if (!c
->playback
.current_memblock
) {
561 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2);
562 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
563 c
->playback
.memblock_index
= 0;
566 if ((r
= pa_iochannel_read(c
->io
, c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
567 fprintf(stderr
, __FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
571 chunk
.memblock
= c
->playback
.current_memblock
;
572 chunk
.index
= c
->playback
.memblock_index
;
574 assert(chunk
.memblock
);
576 c
->playback
.memblock_index
+= r
;
578 assert(c
->input_memblockq
);
579 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
580 assert(c
->sink_input
);
581 pa_sink_notify(c
->sink_input
->sink
);
589 static int do_write(struct connection
*c
) {
592 if (c
->write_data_length
) {
595 assert(c
->write_data_index
< c
->write_data_length
);
596 if ((r
= pa_iochannel_write(c
->io
, c
->write_data
+c
->write_data_index
, c
->write_data_length
-c
->write_data_index
)) < 0) {
597 fprintf(stderr
, __FILE__
": write() failed: %s\n", strerror(errno
));
601 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
602 c
->write_data_length
= c
->write_data_index
= 0;
604 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
605 struct pa_memchunk chunk
;
608 assert(c
->output_memblockq
);
609 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
612 assert(chunk
.memblock
&& chunk
.length
);
614 if ((r
= pa_iochannel_write(c
->io
, chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
615 pa_memblock_unref(chunk
.memblock
);
616 fprintf(stderr
, __FILE__
": write(): %s\n", strerror(errno
));
620 pa_memblockq_drop(c
->output_memblockq
, r
);
621 pa_memblock_unref(chunk
.memblock
);
627 static void do_work(struct connection
*c
) {
630 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
631 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 0);
633 if (pa_iochannel_is_hungup(c
->io
))
636 if (pa_iochannel_is_writable(c
->io
))
640 if (pa_iochannel_is_readable(c
->io
))
650 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
651 struct connection
*c
= userdata
;
652 assert(io
&& c
&& c
->io
== io
);
657 /*** fixed callback ***/
659 void fixed_callback(struct pa_mainloop_api
*a
, void *id
, void *userdata
) {
660 struct connection
*c
= userdata
;
661 assert(a
&& c
&& c
->fixed_source
== id
);
666 /*** sink_input callbacks ***/
668 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
670 assert(i
&& i
->userdata
&& chunk
);
673 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
679 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
) {
680 struct connection
*c
= i
->userdata
;
681 assert(i
&& c
&& length
);
683 pa_memblockq_drop(c
->input_memblockq
, length
);
686 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
687 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 1);
690 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
691 assert(i
&& i
->userdata
);
692 connection_free((struct connection
*) i
->userdata
);
696 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
697 struct connection
*c
= i
->userdata
;
699 return pa_samples_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
702 /*** source_output callbacks ***/
704 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
705 struct connection
*c
= o
->userdata
;
706 assert(o
&& c
&& chunk
);
708 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
711 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
712 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 1);
715 static void source_output_kill_cb(struct pa_source_output
*o
) {
716 assert(o
&& o
->userdata
);
717 connection_free((struct connection
*) o
->userdata
);
720 /*** socket server callback ***/
722 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
723 struct connection
*c
;
725 assert(s
&& io
&& userdata
);
727 c
= malloc(sizeof(struct connection
));
729 c
->protocol
= userdata
;
731 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
733 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
734 assert(c
->protocol
->core
);
735 c
->client
= pa_client_new(c
->protocol
->core
, "ESOUND", cname
);
737 c
->client
->kill
= client_kill_cb
;
738 c
->client
->userdata
= c
;
740 c
->authorized
= c
->protocol
->public;
741 c
->swap_byte_order
= 0;
743 c
->read_data_length
= 0;
744 c
->read_data
= malloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
745 assert(c
->read_data
);
747 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
748 c
->write_data
= NULL
;
750 c
->state
= ESD_NEEDS_REQDATA
;
751 c
->request
= ESD_PROTO_CONNECT
;
753 c
->sink_input
= NULL
;
754 c
->input_memblockq
= NULL
;
756 c
->source_output
= NULL
;
757 c
->output_memblockq
= NULL
;
759 c
->playback
.current_memblock
= NULL
;
760 c
->playback
.memblock_index
= 0;
761 c
->playback
.fragment_size
= 0;
763 c
->fixed_source
= c
->protocol
->core
->mainloop
->source_fixed(c
->protocol
->core
->mainloop
, fixed_callback
, c
);
764 assert(c
->fixed_source
);
765 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 0);
767 pa_idxset_put(c
->protocol
->connections
, c
, &c
->index
);
770 /*** entry points ***/
772 struct pa_protocol_esound
* pa_protocol_esound_new(struct pa_core
*core
, struct pa_socket_server
*server
) {
773 struct pa_protocol_esound
*p
;
774 assert(core
&& server
);
776 p
= malloc(sizeof(struct pa_protocol_esound
));
779 if (pa_authkey_load_from_home(COOKIE_FILE
, p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
786 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
788 p
->connections
= pa_idxset_new(NULL
, NULL
);
789 assert(p
->connections
);
790 p
->sink_index
= p
->source_index
= PA_IDXSET_INVALID
;
796 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
797 struct connection
*c
;
800 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
803 pa_idxset_free(p
->connections
, NULL
, NULL
);
804 pa_socket_server_free(p
->server
);