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"
49 /* Don't accept more connection than this */
50 #define MAX_CONNECTIONS 10
52 /* Kick a client if it doesn't authenticate within this time */
53 #define AUTH_TIMEOUT 5
55 #define DEFAULT_COOKIE_FILE ".esd_auth"
57 #define PLAYBACK_BUFFER_SECONDS (.5)
58 #define PLAYBACK_BUFFER_FRAGMENTS (10)
59 #define RECORD_BUFFER_SECONDS (5)
60 #define RECORD_BUFFER_FRAGMENTS (100)
62 #define MAX_CACHE_SAMPLE_SIZE (1024000)
64 #define SCACHE_PREFIX "esound."
66 #define PA_TYPEID_ESOUND PA_TYPEID_MAKE('E', 'S', 'D', 'P')
68 /* This is heavily based on esound's code */
73 struct pa_protocol_esound
*protocol
;
74 struct pa_iochannel
*io
;
75 struct pa_client
*client
;
76 int authorized
, swap_byte_order
;
78 size_t write_data_alloc
, write_data_index
, write_data_length
;
80 size_t read_data_alloc
, read_data_length
;
82 esd_client_state_t state
;
83 struct pa_sink_input
*sink_input
;
84 struct pa_source_output
*source_output
;
85 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
86 struct pa_defer_event
*defer_event
;
89 struct pa_memblock
*current_memblock
;
90 size_t memblock_index
, fragment_size
;
94 struct pa_memchunk memchunk
;
96 struct pa_sample_spec sample_spec
;
99 struct pa_time_event
*auth_timeout_event
;
102 struct pa_protocol_esound
{
104 struct pa_module
*module
;
105 struct pa_core
*core
;
106 struct pa_socket_server
*server
;
107 struct pa_idxset
*connections
;
108 char *sink_name
, *source_name
;
110 uint8_t esd_key
[ESD_KEY_LEN
];
113 typedef struct proto_handler
{
115 int (*proc
)(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
116 const char *description
;
117 } esd_proto_handler_info_t
;
119 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
);
120 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
121 static void sink_input_kill_cb(struct pa_sink_input
*i
);
122 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
);
123 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
);
125 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
126 static void source_output_kill_cb(struct pa_source_output
*o
);
128 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
129 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
130 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
131 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
132 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
133 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
134 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
135 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
136 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
137 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
138 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
);
140 /* the big map of protocol handler info */
141 static struct proto_handler proto_map
[ESD_PROTO_MAX
] = {
142 { ESD_KEY_LEN
+ sizeof(int), esd_proto_connect
, "connect" },
143 { ESD_KEY_LEN
+ sizeof(int), NULL
, "lock" },
144 { ESD_KEY_LEN
+ sizeof(int), NULL
, "unlock" },
146 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_play
, "stream play" },
147 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream rec" },
148 { ESD_NAME_MAX
+ 2 * sizeof(int), esd_proto_stream_record
, "stream mon" },
150 { ESD_NAME_MAX
+ 3 * sizeof(int), esd_proto_sample_cache
, "sample cache" }, /* 6 */
151 { sizeof(int), esd_proto_sample_free_or_play
, "sample free" },
152 { sizeof(int), esd_proto_sample_free_or_play
, "sample play" }, /* 8 */
153 { sizeof(int), NULL
, "sample loop" },
154 { sizeof(int), NULL
, "sample stop" },
155 { -1, NULL
, "TODO: sample kill" },
157 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "standby" }, /* NOOP! */
158 { ESD_KEY_LEN
+ sizeof(int), esd_proto_standby_or_resume
, "resume" }, /* NOOP! */ /* 13 */
160 { ESD_NAME_MAX
, esd_proto_sample_get_id
, "sample getid" }, /* 14 */
161 { ESD_NAME_MAX
+ 2 * sizeof(int), NULL
, "stream filter" },
163 { sizeof(int), esd_proto_server_info
, "server info" },
164 { sizeof(int), esd_proto_all_info
, "all info" },
165 { -1, NULL
, "TODO: subscribe" },
166 { -1, NULL
, "TODO: unsubscribe" },
168 { 3 * sizeof(int), esd_proto_stream_pan
, "stream pan"},
169 { 3 * sizeof(int), NULL
, "sample pan" },
171 { sizeof(int), NULL
, "standby mode" },
172 { 0, esd_proto_get_latency
, "get latency" }
176 static void connection_free(struct connection
*c
) {
178 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
180 if (c
->state
== ESD_STREAMING_DATA
)
181 c
->protocol
->n_player
--;
183 pa_client_free(c
->client
);
186 pa_sink_input_disconnect(c
->sink_input
);
187 pa_sink_input_unref(c
->sink_input
);
190 if (c
->source_output
) {
191 pa_source_output_disconnect(c
->source_output
);
192 pa_source_output_unref(c
->source_output
);
195 if (c
->input_memblockq
)
196 pa_memblockq_free(c
->input_memblockq
);
197 if (c
->output_memblockq
)
198 pa_memblockq_free(c
->output_memblockq
);
200 if (c
->playback
.current_memblock
)
201 pa_memblock_unref(c
->playback
.current_memblock
);
203 pa_xfree(c
->read_data
);
204 pa_xfree(c
->write_data
);
207 pa_iochannel_free(c
->io
);
210 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
212 if (c
->scache
.memchunk
.memblock
)
213 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
214 pa_xfree(c
->scache
.name
);
216 if (c
->auth_timeout_event
)
217 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
222 static void* connection_write(struct connection
*c
, size_t length
) {
226 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
227 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
229 t
= c
->write_data_length
+length
;
231 if (c
->write_data_alloc
< t
)
232 c
->write_data
= pa_xrealloc(c
->write_data
, c
->write_data_alloc
= t
);
234 assert(c
->write_data
);
236 i
= c
->write_data_length
;
237 c
->write_data_length
+= length
;
239 return (uint8_t*) c
->write_data
+i
;
242 static void format_esd2native(int format
, struct pa_sample_spec
*ss
) {
245 ss
->channels
= ((format
& ESD_MASK_CHAN
) == ESD_STEREO
) ? 2 : 1;
246 ss
->format
= ((format
& ESD_MASK_BITS
) == ESD_BITS16
) ? PA_SAMPLE_S16NE
: PA_SAMPLE_U8
;
249 static int format_native2esd(struct pa_sample_spec
*ss
) {
252 format
= (ss
->format
== PA_SAMPLE_U8
) ? ESD_BITS8
: ESD_BITS16
;
253 format
|= (ss
->channels
>= 2) ? ESD_STEREO
: ESD_MONO
;
258 /*** esound commands ***/
260 static int esd_proto_connect(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
263 assert(length
== (ESD_KEY_LEN
+ sizeof(uint32_t)));
265 if (!c
->authorized
) {
266 if (memcmp(data
, c
->protocol
->esd_key
, ESD_KEY_LEN
) != 0) {
267 pa_log(__FILE__
": kicked client with invalid authorization key.\n");
272 if (c
->auth_timeout_event
) {
273 c
->protocol
->core
->mainloop
->time_free(c
->auth_timeout_event
);
274 c
->auth_timeout_event
= NULL
;
278 ekey
= *(uint32_t*)((uint8_t*) data
+ESD_KEY_LEN
);
279 if (ekey
== ESD_ENDIAN_KEY
)
280 c
->swap_byte_order
= 0;
281 else if (ekey
== ESD_SWAP_ENDIAN_KEY
)
282 c
->swap_byte_order
= 1;
284 pa_log(__FILE__
": client sent invalid endian key\n");
288 ok
= connection_write(c
, sizeof(int));
294 static int esd_proto_stream_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
295 char name
[ESD_NAME_MAX
];
297 struct pa_sink
*sink
;
298 struct pa_sample_spec ss
;
300 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
302 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
303 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
306 format_esd2native(format
, &ss
);
308 if (!pa_sample_spec_valid(&ss
)) {
309 pa_log(__FILE__
": invalid sample specification\n");
313 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
314 pa_log(__FILE__
": no such sink\n");
318 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
319 name
[sizeof(name
)-1] = 0;
321 pa_client_set_name(c
->client
, name
);
323 assert(!c
->sink_input
&& !c
->input_memblockq
);
325 if (!(c
->sink_input
= pa_sink_input_new(sink
, PA_TYPEID_ESOUND
, name
, &ss
, 0, -1))) {
326 pa_log(__FILE__
": failed to create sink input.\n");
330 l
= (size_t) (pa_bytes_per_second(&ss
)*PLAYBACK_BUFFER_SECONDS
);
331 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, c
->protocol
->core
->memblock_stat
);
332 pa_iochannel_socket_set_rcvbuf(c
->io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*2);
333 c
->playback
.fragment_size
= l
/10;
335 c
->sink_input
->owner
= c
->protocol
->module
;
336 c
->sink_input
->client
= c
->client
;
337 c
->sink_input
->peek
= sink_input_peek_cb
;
338 c
->sink_input
->drop
= sink_input_drop_cb
;
339 c
->sink_input
->kill
= sink_input_kill_cb
;
340 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
341 c
->sink_input
->userdata
= c
;
343 c
->state
= ESD_STREAMING_DATA
;
345 c
->protocol
->n_player
++;
350 static int esd_proto_stream_record(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
351 char name
[ESD_NAME_MAX
];
353 struct pa_source
*source
;
354 struct pa_sample_spec ss
;
356 assert(c
&& length
== (sizeof(int)*2+ESD_NAME_MAX
));
358 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
359 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
362 format_esd2native(format
, &ss
);
364 if (!pa_sample_spec_valid(&ss
)) {
365 pa_log(__FILE__
": invalid sample specification.\n");
369 if (request
== ESD_PROTO_STREAM_MON
) {
370 struct pa_sink
* sink
;
372 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
373 pa_log(__FILE__
": no such sink.\n");
377 if (!(source
= sink
->monitor_source
)) {
378 pa_log(__FILE__
": no such monitor source.\n");
382 assert(request
== ESD_PROTO_STREAM_REC
);
384 if (!(source
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
385 pa_log(__FILE__
": no such source.\n");
390 strncpy(name
, (char*) data
+ sizeof(int)*2, sizeof(name
));
391 name
[sizeof(name
)-1] = 0;
393 pa_client_set_name(c
->client
, name
);
395 assert(!c
->output_memblockq
&& !c
->source_output
);
397 if (!(c
->source_output
= pa_source_output_new(source
, PA_TYPEID_ESOUND
, name
, &ss
, -1))) {
398 pa_log(__FILE__
": failed to create source output\n");
402 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
403 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
404 pa_iochannel_socket_set_sndbuf(c
->io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
406 c
->source_output
->owner
= c
->protocol
->module
;
407 c
->source_output
->client
= c
->client
;
408 c
->source_output
->push
= source_output_push_cb
;
409 c
->source_output
->kill
= source_output_kill_cb
;
410 c
->source_output
->get_latency
= source_output_get_latency_cb
;
411 c
->source_output
->userdata
= c
;
413 c
->state
= ESD_STREAMING_DATA
;
415 c
->protocol
->n_player
++;
420 static int esd_proto_get_latency(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
421 struct pa_sink
*sink
;
423 assert(c
&& !data
&& length
== 0);
425 if (!(sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
428 double usec
= pa_sink_get_latency(sink
);
429 usec
+= PLAYBACK_BUFFER_SECONDS
*1000000; /* A better estimation would be a good idea! */
430 latency
= (int) ((usec
*44100)/1000000);
433 lag
= connection_write(c
, sizeof(int));
435 *lag
= c
->swap_byte_order
? swap_endian_32(latency
) : latency
;
439 static int esd_proto_server_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
440 int rate
= 44100, format
= ESD_STEREO
|ESD_BITS16
;
442 struct pa_sink
*sink
;
443 assert(c
&& data
&& length
== sizeof(int));
445 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1))) {
446 rate
= sink
->sample_spec
.rate
;
447 format
= format_native2esd(&sink
->sample_spec
);
450 response
= connection_write(c
, sizeof(int)*3);
453 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
454 *(response
++) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
458 static int esd_proto_all_info(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
461 struct connection
*conn
;
462 size_t index
= PA_IDXSET_INVALID
;
464 assert(c
&& data
&& length
== sizeof(int));
466 if (esd_proto_server_info(c
, request
, data
, length
) < 0)
469 k
= sizeof(int)*5+ESD_NAME_MAX
;
470 s
= sizeof(int)*6+ESD_NAME_MAX
;
471 nsamples
= c
->protocol
->core
->scache
? pa_idxset_ncontents(c
->protocol
->core
->scache
) : 0;
472 response
= connection_write(c
, (t
= s
*(nsamples
+1) + k
*(c
->protocol
->n_player
+1)));
475 for (conn
= pa_idxset_first(c
->protocol
->connections
, &index
); conn
; conn
= pa_idxset_next(c
->protocol
->connections
, &index
)) {
476 int format
= ESD_BITS16
| ESD_STEREO
, rate
= 44100, volume
= 0xFF;
478 if (conn
->state
!= ESD_STREAMING_DATA
)
483 if (conn
->sink_input
) {
484 rate
= conn
->sink_input
->sample_spec
.rate
;
485 volume
= (conn
->sink_input
->volume
*0xFF)/0x100;
486 format
= format_native2esd(&conn
->sink_input
->sample_spec
);
490 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (conn
->index
+1));
491 response
+= sizeof(int);
494 assert(conn
->client
);
495 strncpy((char*) response
, conn
->client
->name
, ESD_NAME_MAX
);
496 response
+= ESD_NAME_MAX
;
499 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, rate
);
500 response
+= sizeof(int);
503 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
504 response
+= sizeof(int);
507 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, volume
);
508 response
+= sizeof(int);
511 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format
);
512 response
+= sizeof(int);
517 assert(t
== s
*(nsamples
+1)+k
);
518 memset(response
, 0, k
);
523 struct pa_scache_entry
*ce
;
525 index
= PA_IDXSET_INVALID
;
526 for (ce
= pa_idxset_first(c
->protocol
->core
->scache
, &index
); ce
; ce
= pa_idxset_next(c
->protocol
->core
->scache
, &index
)) {
530 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) (ce
->index
+1));
531 response
+= sizeof(int);
534 if (strncmp(ce
->name
, SCACHE_PREFIX
, sizeof(SCACHE_PREFIX
)-1) == 0)
535 strncpy((char*) response
, ce
->name
+sizeof(SCACHE_PREFIX
)-1, ESD_NAME_MAX
);
537 snprintf((char*) response
, ESD_NAME_MAX
, "native.%s", ce
->name
);
538 response
+= ESD_NAME_MAX
;
541 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, ce
->sample_spec
.rate
);
542 response
+= sizeof(int);
545 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
546 response
+= sizeof(int);
549 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (ce
->volume
*0xFF)/0x100);
550 response
+= sizeof(int);
553 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, format_native2esd(&ce
->sample_spec
));
554 response
+= sizeof(int);
557 *((int*) response
) = maybe_swap_endian_32(c
->swap_byte_order
, (int) ce
->memchunk
.length
);
558 response
+= sizeof(int);
565 memset(response
, 0, s
);
570 static int esd_proto_stream_pan(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
572 uint32_t index
, volume
;
573 struct connection
*conn
;
574 assert(c
&& data
&& length
== sizeof(int)*3);
576 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
577 volume
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
578 volume
= (volume
*0x100)/0xFF;
580 ok
= connection_write(c
, sizeof(int));
583 if ((conn
= pa_idxset_get_by_index(c
->protocol
->connections
, index
))) {
584 assert(conn
->sink_input
);
585 conn
->sink_input
->volume
= volume
;
593 static int esd_proto_sample_cache(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
594 struct pa_sample_spec ss
;
599 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
600 assert(c
&& data
&& length
== (ESD_NAME_MAX
+3*sizeof(int)));
602 format
= maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
);
603 rate
= maybe_swap_endian_32(c
->swap_byte_order
, *((int*)data
+ 1));
606 format_esd2native(format
, &ss
);
608 sc_length
= (size_t) maybe_swap_endian_32(c
->swap_byte_order
, (*((int*)data
+ 2)));
610 if (sc_length
>= MAX_CACHE_SAMPLE_SIZE
)
613 strcpy(name
, SCACHE_PREFIX
);
614 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, (char*) data
+3*sizeof(int), ESD_NAME_MAX
);
615 name
[sizeof(name
)-1] = 0;
617 assert(!c
->scache
.memchunk
.memblock
);
618 c
->scache
.memchunk
.memblock
= pa_memblock_new(sc_length
, c
->protocol
->core
->memblock_stat
);
619 c
->scache
.memchunk
.index
= 0;
620 c
->scache
.memchunk
.length
= sc_length
;
621 c
->scache
.sample_spec
= ss
;
622 assert(!c
->scache
.name
);
623 c
->scache
.name
= pa_xstrdup(name
);
625 c
->state
= ESD_CACHING_SAMPLE
;
627 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, NULL
, NULL
, &index
);
629 ok
= connection_write(c
, sizeof(int));
637 static int esd_proto_sample_get_id(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
640 char name
[ESD_NAME_MAX
+sizeof(SCACHE_PREFIX
)-1];
641 assert(c
&& data
&& length
== ESD_NAME_MAX
);
643 ok
= connection_write(c
, sizeof(int));
648 strcpy(name
, SCACHE_PREFIX
);
649 strncpy(name
+sizeof(SCACHE_PREFIX
)-1, data
, ESD_NAME_MAX
);
650 name
[sizeof(name
)-1] = 0;
652 if ((index
= pa_scache_get_id_by_name(c
->protocol
->core
, name
)) != PA_IDXSET_INVALID
)
653 *ok
= (int) index
+1;
658 static int esd_proto_sample_free_or_play(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
662 assert(c
&& data
&& length
== sizeof(int));
664 index
= (uint32_t) maybe_swap_endian_32(c
->swap_byte_order
, *(int*)data
)-1;
666 ok
= connection_write(c
, sizeof(int));
671 if ((name
= pa_scache_get_name_by_id(c
->protocol
->core
, index
))) {
672 if (request
== ESD_PROTO_SAMPLE_PLAY
) {
673 struct pa_sink
*sink
;
675 if ((sink
= pa_namereg_get(c
->protocol
->core
, c
->protocol
->sink_name
, PA_NAMEREG_SINK
, 1)))
676 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, PA_VOLUME_NORM
) >= 0)
679 assert(request
== ESD_PROTO_SAMPLE_FREE
);
681 if (pa_scache_remove_item(c
->protocol
->core
, name
) >= 0)
689 static int esd_proto_standby_or_resume(struct connection
*c
, esd_proto_t request
, const void *data
, size_t length
) {
691 ok
= connection_write(c
, sizeof(int)*2);
698 /*** client callbacks ***/
700 static void client_kill_cb(struct pa_client
*c
) {
701 assert(c
&& c
->userdata
);
702 connection_free(c
->userdata
);
705 /*** pa_iochannel callbacks ***/
707 static int do_read(struct connection
*c
) {
710 /* pa_log("READ\n"); */
712 if (c
->state
== ESD_NEXT_REQUEST
) {
714 assert(c
->read_data_length
< sizeof(c
->request
));
716 if ((r
= pa_iochannel_read(c
->io
, ((uint8_t*) &c
->request
) + c
->read_data_length
, sizeof(c
->request
) - c
->read_data_length
)) <= 0) {
718 pa_log_warn(__FILE__
": read() failed: %s\n", strerror(errno
));
722 if ((c
->read_data_length
+= r
) >= sizeof(c
->request
)) {
723 struct proto_handler
*handler
;
725 if (c
->swap_byte_order
)
726 c
->request
= swap_endian_32(c
->request
);
728 if (c
->request
< ESD_PROTO_CONNECT
|| c
->request
> ESD_PROTO_MAX
) {
729 pa_log(__FILE__
": recieved invalid request.\n");
733 handler
= proto_map
+c
->request
;
735 /* pa_log(__FILE__": executing request #%u\n", c->request); */
737 if (!handler
->proc
) {
738 pa_log(__FILE__
": recieved unimplemented request #%u.\n", c
->request
);
742 if (handler
->data_length
== 0) {
743 c
->read_data_length
= 0;
745 if (handler
->proc(c
, c
->request
, NULL
, 0) < 0)
749 if (c
->read_data_alloc
< handler
->data_length
)
750 c
->read_data
= pa_xrealloc(c
->read_data
, c
->read_data_alloc
= handler
->data_length
);
751 assert(c
->read_data
);
753 c
->state
= ESD_NEEDS_REQDATA
;
754 c
->read_data_length
= 0;
758 } else if (c
->state
== ESD_NEEDS_REQDATA
) {
760 struct proto_handler
*handler
= proto_map
+c
->request
;
762 assert(handler
->proc
);
764 assert(c
->read_data
&& c
->read_data_length
< handler
->data_length
);
766 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->read_data
+ c
->read_data_length
, handler
->data_length
- c
->read_data_length
)) <= 0) {
768 pa_log_warn(__FILE__
": read() failed: %s\n", strerror(errno
));
772 if ((c
->read_data_length
+= r
) >= handler
->data_length
) {
773 size_t l
= c
->read_data_length
;
774 assert(handler
->proc
);
776 c
->state
= ESD_NEXT_REQUEST
;
777 c
->read_data_length
= 0;
779 if (handler
->proc(c
, c
->request
, c
->read_data
, l
) < 0)
782 } else if (c
->state
== ESD_CACHING_SAMPLE
) {
785 assert(c
->scache
.memchunk
.memblock
&& c
->scache
.name
&& c
->scache
.memchunk
.index
< c
->scache
.memchunk
.length
);
787 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) {
789 pa_log_warn(__FILE__
": read() failed: %s\n", strerror(errno
));
793 c
->scache
.memchunk
.index
+= r
;
794 assert(c
->scache
.memchunk
.index
<= c
->scache
.memchunk
.length
);
796 if (c
->scache
.memchunk
.index
== c
->scache
.memchunk
.length
) {
800 c
->scache
.memchunk
.index
= 0;
801 pa_scache_add_item(c
->protocol
->core
, c
->scache
.name
, &c
->scache
.sample_spec
, &c
->scache
.memchunk
, &index
);
803 pa_memblock_unref(c
->scache
.memchunk
.memblock
);
804 c
->scache
.memchunk
.memblock
= NULL
;
805 c
->scache
.memchunk
.index
= c
->scache
.memchunk
.length
= 0;
807 pa_xfree(c
->scache
.name
);
808 c
->scache
.name
= NULL
;
810 c
->state
= ESD_NEXT_REQUEST
;
812 ok
= connection_write(c
, sizeof(int));
817 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
818 struct pa_memchunk chunk
;
822 assert(c
->input_memblockq
);
824 /* pa_log("STREAMING_DATA\n"); */
826 if (!(l
= pa_memblockq_missing(c
->input_memblockq
)))
829 if (l
> c
->playback
.fragment_size
)
830 l
= c
->playback
.fragment_size
;
832 if (c
->playback
.current_memblock
)
833 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
834 pa_memblock_unref(c
->playback
.current_memblock
);
835 c
->playback
.current_memblock
= NULL
;
836 c
->playback
.memblock_index
= 0;
839 if (!c
->playback
.current_memblock
) {
840 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
841 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
842 c
->playback
.memblock_index
= 0;
845 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
847 pa_log(__FILE__
": read() failed: %s\n", strerror(errno
));
851 /* pa_log(__FILE__": read %u\n", r); */
853 chunk
.memblock
= c
->playback
.current_memblock
;
854 chunk
.index
= c
->playback
.memblock_index
;
856 assert(chunk
.memblock
);
858 c
->playback
.memblock_index
+= r
;
860 assert(c
->input_memblockq
);
861 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
862 assert(c
->sink_input
);
863 pa_sink_notify(c
->sink_input
->sink
);
869 static int do_write(struct connection
*c
) {
872 /* pa_log("WRITE\n"); */
874 if (c
->write_data_length
) {
877 assert(c
->write_data_index
< c
->write_data_length
);
878 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) {
879 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
883 if ((c
->write_data_index
+=r
) >= c
->write_data_length
)
884 c
->write_data_length
= c
->write_data_index
= 0;
886 } else if (c
->state
== ESD_STREAMING_DATA
&& c
->source_output
) {
887 struct pa_memchunk chunk
;
890 assert(c
->output_memblockq
);
891 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
894 assert(chunk
.memblock
&& chunk
.length
);
896 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
897 pa_memblock_unref(chunk
.memblock
);
898 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
902 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
903 pa_memblock_unref(chunk
.memblock
);
909 static void do_work(struct connection
*c
) {
912 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
913 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
915 /* pa_log("DOWORK %i\n", pa_iochannel_is_hungup(c->io)); */
917 if (!c
->dead
&& pa_iochannel_is_readable(c
->io
))
921 if (!c
->dead
&& pa_iochannel_is_writable(c
->io
))
925 /* In case the line was hungup, make sure to rerun this function
926 as soon as possible, until all data has been read. */
928 if (!c
->dead
&& pa_iochannel_is_hungup(c
->io
))
929 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
935 if (c
->state
== ESD_STREAMING_DATA
&& c
->sink_input
) {
937 pa_memblockq_prebuf_disable(c
->input_memblockq
);
939 pa_iochannel_free(c
->io
);
946 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
947 struct connection
*c
= userdata
;
948 assert(io
&& c
&& c
->io
== io
);
950 /* pa_log("IO\n"); */
955 /*** defer callback ***/
957 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
958 struct connection
*c
= userdata
;
959 assert(a
&& c
&& c
->defer_event
== e
);
961 /* pa_log("DEFER\n"); */
966 /*** sink_input callbacks ***/
968 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
970 assert(i
&& i
->userdata
&& chunk
);
973 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
984 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
985 struct connection
*c
= i
->userdata
;
986 assert(i
&& c
&& length
);
988 /* pa_log("DROP\n"); */
990 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
993 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
996 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
998 /* assert(pa_memblockq_get_length(c->input_memblockq) > 2048); */
1001 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
1002 assert(i
&& i
->userdata
);
1003 connection_free((struct connection
*) i
->userdata
);
1006 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
1007 struct connection
*c
= i
->userdata
;
1009 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
1012 /*** source_output callbacks ***/
1014 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
1015 struct connection
*c
= o
->userdata
;
1016 assert(o
&& c
&& chunk
);
1018 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
1021 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
1024 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
1027 static void source_output_kill_cb(struct pa_source_output
*o
) {
1028 assert(o
&& o
->userdata
);
1029 connection_free((struct connection
*) o
->userdata
);
1032 static pa_usec_t
source_output_get_latency_cb(struct pa_source_output
*o
) {
1033 struct connection
*c
= o
->userdata
;
1035 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
1038 /*** socket server callback ***/
1040 static void auth_timeout(struct pa_mainloop_api
*m
, struct pa_time_event
*e
, const struct timeval
*tv
, void *userdata
) {
1041 struct connection
*c
= userdata
;
1042 assert(m
&& tv
&& c
&& c
->auth_timeout_event
== e
);
1048 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1049 struct connection
*c
;
1050 struct pa_protocol_esound
*p
= userdata
;
1052 assert(s
&& io
&& p
);
1054 if (pa_idxset_ncontents(p
->connections
)+1 > MAX_CONNECTIONS
) {
1055 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
1056 pa_iochannel_free(io
);
1060 c
= pa_xmalloc(sizeof(struct connection
));
1063 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
1065 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
1067 c
->client
= pa_client_new(p
->core
, PA_TYPEID_ESOUND
, cname
);
1069 c
->client
->owner
= p
->module
;
1070 c
->client
->kill
= client_kill_cb
;
1071 c
->client
->userdata
= c
;
1073 c
->authorized
= p
->public;
1074 c
->swap_byte_order
= 0;
1077 c
->read_data_length
= 0;
1078 c
->read_data
= pa_xmalloc(c
->read_data_alloc
= proto_map
[ESD_PROTO_CONNECT
].data_length
);
1080 c
->write_data_length
= c
->write_data_index
= c
->write_data_alloc
= 0;
1081 c
->write_data
= NULL
;
1083 c
->state
= ESD_NEEDS_REQDATA
;
1084 c
->request
= ESD_PROTO_CONNECT
;
1086 c
->sink_input
= NULL
;
1087 c
->input_memblockq
= NULL
;
1089 c
->source_output
= NULL
;
1090 c
->output_memblockq
= NULL
;
1092 c
->playback
.current_memblock
= NULL
;
1093 c
->playback
.memblock_index
= 0;
1094 c
->playback
.fragment_size
= 0;
1096 c
->scache
.memchunk
.length
= c
->scache
.memchunk
.index
= 0;
1097 c
->scache
.memchunk
.memblock
= NULL
;
1098 c
->scache
.name
= NULL
;
1100 if (!c
->authorized
) {
1102 gettimeofday(&tv
, NULL
);
1103 tv
.tv_sec
+= AUTH_TIMEOUT
;
1104 c
->auth_timeout_event
= p
->core
->mainloop
->time_new(p
->core
->mainloop
, &tv
, auth_timeout
, c
);
1106 c
->auth_timeout_event
= NULL
;
1108 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
1109 assert(c
->defer_event
);
1110 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
1112 pa_idxset_put(p
->connections
, c
, &c
->index
);
1115 /*** entry points ***/
1117 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
) {
1118 struct pa_protocol_esound
*p
;
1120 assert(core
&& server
&& ma
);
1122 p
= pa_xmalloc(sizeof(struct pa_protocol_esound
));
1124 if (pa_modargs_get_value_boolean(ma
, "public", &public) < 0) {
1125 pa_log(__FILE__
": public= expects a boolean argument.\n");
1129 if (pa_authkey_load_auto(pa_modargs_get_value(ma
, "cookie", DEFAULT_COOKIE_FILE
), p
->esd_key
, sizeof(p
->esd_key
)) < 0) {
1137 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1139 p
->connections
= pa_idxset_new(NULL
, NULL
);
1140 assert(p
->connections
);
1142 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
1143 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
1149 void pa_protocol_esound_free(struct pa_protocol_esound
*p
) {
1150 struct connection
*c
;
1153 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1156 pa_idxset_free(p
->connections
, NULL
, NULL
);
1157 pa_socket_server_unref(p
->server
);