]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-esound.c
set esound.byte_order property
[pulseaudio] / src / pulsecore / protocol-esound.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits.h>
32
33 #include <pulse/sample.h>
34 #include <pulse/timeval.h>
35 #include <pulse/utf8.h>
36 #include <pulse/xmalloc.h>
37 #include <pulse/proplist.h>
38
39 #include <pulsecore/esound.h>
40 #include <pulsecore/memblock.h>
41 #include <pulsecore/client.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/source-output.h>
45 #include <pulsecore/source.h>
46 #include <pulsecore/core-scache.h>
47 #include <pulsecore/sample-util.h>
48 #include <pulsecore/authkey.h>
49 #include <pulsecore/namereg.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/core-error.h>
53 #include <pulsecore/ipacl.h>
54 #include <pulsecore/macro.h>
55 #include <pulsecore/thread-mq.h>
56 #include <pulsecore/shared.h>
57
58 #include "endianmacros.h"
59
60 #include "protocol-esound.h"
61
62 /* Don't accept more connection than this */
63 #define MAX_CONNECTIONS 64
64
65 /* Kick a client if it doesn't authenticate within this time */
66 #define AUTH_TIMEOUT 5
67
68 #define DEFAULT_COOKIE_FILE ".esd_auth"
69
70 #define PLAYBACK_BUFFER_SECONDS (.25)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
73
74 #define MAX_CACHE_SAMPLE_SIZE (2048000)
75
76 #define DEFAULT_SINK_LATENCY (150*PA_USEC_PER_MSEC)
77 #define DEFAULT_SOURCE_LATENCY (150*PA_USEC_PER_MSEC)
78
79 #define SCACHE_PREFIX "esound."
80
81 /* This is heavily based on esound's code */
82
83 typedef struct connection {
84 pa_msgobject parent;
85
86 uint32_t index;
87 pa_bool_t dead;
88 pa_esound_protocol *protocol;
89 pa_esound_options *options;
90 pa_iochannel *io;
91 pa_client *client;
92 pa_bool_t authorized, swap_byte_order;
93 void *write_data;
94 size_t write_data_alloc, write_data_index, write_data_length;
95 void *read_data;
96 size_t read_data_alloc, read_data_length;
97 esd_proto_t request;
98 esd_client_state_t state;
99 pa_sink_input *sink_input;
100 pa_source_output *source_output;
101 pa_memblockq *input_memblockq, *output_memblockq;
102 pa_defer_event *defer_event;
103
104 char *original_name;
105
106 struct {
107 pa_memblock *current_memblock;
108 size_t memblock_index;
109 pa_atomic_t missing;
110 pa_bool_t underrun;
111 } playback;
112
113 struct {
114 pa_memchunk memchunk;
115 char *name;
116 pa_sample_spec sample_spec;
117 } scache;
118
119 pa_time_event *auth_timeout_event;
120 } connection;
121
122 PA_DECLARE_CLASS(connection);
123 #define CONNECTION(o) (connection_cast(o))
124 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
125
126 struct pa_esound_protocol {
127 PA_REFCNT_DECLARE;
128
129 pa_core *core;
130 pa_idxset *connections;
131 unsigned n_player;
132 };
133
134 enum {
135 SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
136 SINK_INPUT_MESSAGE_DISABLE_PREBUF
137 };
138
139 enum {
140 CONNECTION_MESSAGE_REQUEST_DATA,
141 CONNECTION_MESSAGE_POST_DATA,
142 CONNECTION_MESSAGE_UNLINK_CONNECTION
143 };
144
145 typedef struct proto_handler {
146 size_t data_length;
147 int (*proc)(connection *c, esd_proto_t request, const void *data, size_t length);
148 const char *description;
149 } esd_proto_handler_info_t;
150
151 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk);
152 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes);
153 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes);
154 static void sink_input_kill_cb(pa_sink_input *i);
155 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk);
156 static pa_usec_t source_output_get_latency_cb(pa_source_output *o);
157
158 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk);
159 static void source_output_kill_cb(pa_source_output *o);
160
161 static int esd_proto_connect(connection *c, esd_proto_t request, const void *data, size_t length);
162 static int esd_proto_stream_play(connection *c, esd_proto_t request, const void *data, size_t length);
163 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length);
164 static int esd_proto_get_latency(connection *c, esd_proto_t request, const void *data, size_t length);
165 static int esd_proto_server_info(connection *c, esd_proto_t request, const void *data, size_t length);
166 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length);
167 static int esd_proto_stream_pan(connection *c, esd_proto_t request, const void *data, size_t length);
168 static int esd_proto_sample_cache(connection *c, esd_proto_t request, const void *data, size_t length);
169 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length);
170 static int esd_proto_sample_get_id(connection *c, esd_proto_t request, const void *data, size_t length);
171 static int esd_proto_standby_or_resume(connection *c, esd_proto_t request, const void *data, size_t length);
172
173 /* the big map of protocol handler info */
174 static struct proto_handler proto_map[ESD_PROTO_MAX] = {
175 { ESD_KEY_LEN + sizeof(int), esd_proto_connect, "connect" },
176 { ESD_KEY_LEN + sizeof(int), NULL, "lock" },
177 { ESD_KEY_LEN + sizeof(int), NULL, "unlock" },
178
179 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_play, "stream play" },
180 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream rec" },
181 { ESD_NAME_MAX + 2 * sizeof(int), esd_proto_stream_record, "stream mon" },
182
183 { ESD_NAME_MAX + 3 * sizeof(int), esd_proto_sample_cache, "sample cache" }, /* 6 */
184 { sizeof(int), esd_proto_sample_free_or_play, "sample free" },
185 { sizeof(int), esd_proto_sample_free_or_play, "sample play" }, /* 8 */
186 { sizeof(int), NULL, "sample loop" },
187 { sizeof(int), NULL, "sample stop" },
188 { (size_t) -1, NULL, "TODO: sample kill" },
189
190 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "standby" }, /* NOOP! */
191 { ESD_KEY_LEN + sizeof(int), esd_proto_standby_or_resume, "resume" }, /* NOOP! */ /* 13 */
192
193 { ESD_NAME_MAX, esd_proto_sample_get_id, "sample getid" }, /* 14 */
194 { ESD_NAME_MAX + 2 * sizeof(int), NULL, "stream filter" },
195
196 { sizeof(int), esd_proto_server_info, "server info" },
197 { sizeof(int), esd_proto_all_info, "all info" },
198 { (size_t) -1, NULL, "TODO: subscribe" },
199 { (size_t) -1, NULL, "TODO: unsubscribe" },
200
201 { 3 * sizeof(int), esd_proto_stream_pan, "stream pan"},
202 { 3 * sizeof(int), NULL, "sample pan" },
203
204 { sizeof(int), NULL, "standby mode" },
205 { 0, esd_proto_get_latency, "get latency" }
206 };
207
208 static void connection_unlink(connection *c) {
209 pa_assert(c);
210
211 if (!c->protocol)
212 return;
213
214 if (c->options) {
215 pa_esound_options_unref(c->options);
216 c->options = NULL;
217 }
218
219 if (c->sink_input) {
220 pa_sink_input_unlink(c->sink_input);
221 pa_sink_input_unref(c->sink_input);
222 c->sink_input = NULL;
223 }
224
225 if (c->source_output) {
226 pa_source_output_unlink(c->source_output);
227 pa_source_output_unref(c->source_output);
228 c->source_output = NULL;
229 }
230
231 if (c->client) {
232 pa_client_free(c->client);
233 c->client = NULL;
234 }
235
236 if (c->state == ESD_STREAMING_DATA)
237 c->protocol->n_player--;
238
239 if (c->io) {
240 pa_iochannel_free(c->io);
241 c->io = NULL;
242 }
243
244 if (c->defer_event) {
245 c->protocol->core->mainloop->defer_free(c->defer_event);
246 c->defer_event = NULL;
247 }
248
249 if (c->auth_timeout_event) {
250 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
251 c->auth_timeout_event = NULL;
252 }
253
254 pa_assert_se(pa_idxset_remove_by_data(c->protocol->connections, c, NULL) == c);
255 c->protocol = NULL;
256 connection_unref(c);
257 }
258
259 static void connection_free(pa_object *obj) {
260 connection *c = CONNECTION(obj);
261 pa_assert(c);
262
263 if (c->input_memblockq)
264 pa_memblockq_free(c->input_memblockq);
265 if (c->output_memblockq)
266 pa_memblockq_free(c->output_memblockq);
267
268 if (c->playback.current_memblock)
269 pa_memblock_unref(c->playback.current_memblock);
270
271 pa_xfree(c->read_data);
272 pa_xfree(c->write_data);
273
274 if (c->scache.memchunk.memblock)
275 pa_memblock_unref(c->scache.memchunk.memblock);
276 pa_xfree(c->scache.name);
277
278 pa_xfree(c->original_name);
279 pa_xfree(c);
280 }
281
282 static void connection_write_prepare(connection *c, size_t length) {
283 size_t t;
284 pa_assert(c);
285
286 t = c->write_data_length+length;
287
288 if (c->write_data_alloc < t)
289 c->write_data = pa_xrealloc(c->write_data, c->write_data_alloc = t);
290
291 pa_assert(c->write_data);
292 }
293
294 static void connection_write(connection *c, const void *data, size_t length) {
295 size_t i;
296 pa_assert(c);
297
298 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
299
300 connection_write_prepare(c, length);
301
302 pa_assert(c->write_data);
303
304 i = c->write_data_length;
305 c->write_data_length += length;
306
307 memcpy((uint8_t*) c->write_data + i, data, length);
308 }
309
310 static void format_esd2native(int format, pa_bool_t swap_bytes, pa_sample_spec *ss) {
311 pa_assert(ss);
312
313 ss->channels = (uint8_t) (((format & ESD_MASK_CHAN) == ESD_STEREO) ? 2 : 1);
314 if ((format & ESD_MASK_BITS) == ESD_BITS16)
315 ss->format = swap_bytes ? PA_SAMPLE_S16RE : PA_SAMPLE_S16NE;
316 else
317 ss->format = PA_SAMPLE_U8;
318 }
319
320 static int format_native2esd(pa_sample_spec *ss) {
321 int format = 0;
322
323 format = (ss->format == PA_SAMPLE_U8) ? ESD_BITS8 : ESD_BITS16;
324 format |= (ss->channels >= 2) ? ESD_STEREO : ESD_MONO;
325
326 return format;
327 }
328
329 #define CHECK_VALIDITY(expression, ...) do { \
330 if (!(expression)) { \
331 pa_log_warn(__FILE__ ": " __VA_ARGS__); \
332 return -1; \
333 } \
334 } while(0);
335
336 /*** esound commands ***/
337
338 static int esd_proto_connect(connection *c, esd_proto_t request, const void *data, size_t length) {
339 uint32_t ekey;
340 int ok;
341
342 connection_assert_ref(c);
343 pa_assert(data);
344 pa_assert(length == (ESD_KEY_LEN + sizeof(uint32_t)));
345
346 if (!c->authorized && c->options->auth_cookie) {
347 const uint8_t*key;
348
349 if ((key = pa_auth_cookie_read(c->options->auth_cookie, ESD_KEY_LEN)))
350 if (memcmp(data, key, ESD_KEY_LEN) == 0)
351 c->authorized = TRUE;
352 }
353
354 if (!c->authorized) {
355 pa_log("Kicked client with invalid authorization key.");
356 return -1;
357 }
358
359 if (c->auth_timeout_event) {
360 c->protocol->core->mainloop->time_free(c->auth_timeout_event);
361 c->auth_timeout_event = NULL;
362 }
363
364 data = (const char*)data + ESD_KEY_LEN;
365
366 memcpy(&ekey, data, sizeof(uint32_t));
367 if (ekey == ESD_ENDIAN_KEY)
368 c->swap_byte_order = FALSE;
369 else if (ekey == ESD_SWAP_ENDIAN_KEY)
370 c->swap_byte_order = TRUE;
371 else {
372 pa_log_warn("Client sent invalid endian key");
373 return -1;
374 }
375
376 pa_proplist_sets(c->client->proplist, "esound.byte_order", c->swap_byte_order ? "reverse" : "native");
377
378 ok = 1;
379 connection_write(c, &ok, sizeof(int));
380 return 0;
381 }
382
383 static int esd_proto_stream_play(connection *c, esd_proto_t request, const void *data, size_t length) {
384 char name[ESD_NAME_MAX], *utf8_name;
385 int32_t format, rate;
386 pa_sample_spec ss;
387 size_t l;
388 pa_sink *sink = NULL;
389 pa_sink_input_new_data sdata;
390
391 connection_assert_ref(c);
392 pa_assert(data);
393 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
394
395 memcpy(&format, data, sizeof(int32_t));
396 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
397 data = (const char*) data + sizeof(int32_t);
398
399 memcpy(&rate, data, sizeof(int32_t));
400 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
401 data = (const char*) data + sizeof(int32_t);
402
403 ss.rate = (uint32_t) rate;
404 format_esd2native(format, c->swap_byte_order, &ss);
405
406 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification");
407
408 if (c->options->default_sink) {
409 sink = pa_namereg_get(c->protocol->core, c->options->default_sink, PA_NAMEREG_SINK);
410 CHECK_VALIDITY(sink, "No such sink: %s", pa_strnull(c->options->default_sink));
411 }
412
413 pa_strlcpy(name, data, sizeof(name));
414
415 utf8_name = pa_utf8_filter(name);
416 pa_client_set_name(c->client, utf8_name);
417 pa_xfree(utf8_name);
418
419 c->original_name = pa_xstrdup(name);
420
421 pa_assert(!c->sink_input && !c->input_memblockq);
422
423 pa_sink_input_new_data_init(&sdata);
424 sdata.driver = __FILE__;
425 sdata.module = c->options->module;
426 sdata.client = c->client;
427 sdata.sink = sink;
428 pa_sink_input_new_data_set_sample_spec(&sdata, &ss);
429
430 pa_sink_input_new(&c->sink_input, c->protocol->core, &sdata, 0);
431 pa_sink_input_new_data_done(&sdata);
432
433 CHECK_VALIDITY(c->sink_input, "Failed to create sink input.");
434
435 l = (size_t) ((double) pa_bytes_per_second(&ss)*PLAYBACK_BUFFER_SECONDS);
436 c->input_memblockq = pa_memblockq_new(
437 0,
438 l,
439 l,
440 pa_frame_size(&ss),
441 (size_t) -1,
442 l/PLAYBACK_BUFFER_FRAGMENTS,
443 0,
444 NULL);
445 pa_iochannel_socket_set_rcvbuf(c->io, l);
446
447 c->sink_input->parent.process_msg = sink_input_process_msg;
448 c->sink_input->pop = sink_input_pop_cb;
449 c->sink_input->process_rewind = sink_input_process_rewind_cb;
450 c->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
451 c->sink_input->kill = sink_input_kill_cb;
452 c->sink_input->userdata = c;
453
454 pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY);
455
456 c->state = ESD_STREAMING_DATA;
457
458 c->protocol->n_player++;
459
460 pa_atomic_store(&c->playback.missing, (int) pa_memblockq_missing(c->input_memblockq));
461
462 pa_sink_input_put(c->sink_input);
463
464 return 0;
465 }
466
467 static int esd_proto_stream_record(connection *c, esd_proto_t request, const void *data, size_t length) {
468 char name[ESD_NAME_MAX], *utf8_name;
469 int32_t format, rate;
470 pa_source *source = NULL;
471 pa_sample_spec ss;
472 size_t l;
473 pa_source_output_new_data sdata;
474
475 connection_assert_ref(c);
476 pa_assert(data);
477 pa_assert(length == (sizeof(int32_t)*2+ESD_NAME_MAX));
478
479 memcpy(&format, data, sizeof(int32_t));
480 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
481 data = (const char*) data + sizeof(int32_t);
482
483 memcpy(&rate, data, sizeof(int32_t));
484 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
485 data = (const char*) data + sizeof(int32_t);
486
487 ss.rate = (uint32_t) rate;
488 format_esd2native(format, c->swap_byte_order, &ss);
489
490 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
491
492 if (request == ESD_PROTO_STREAM_MON) {
493 pa_sink* sink;
494
495 sink = pa_namereg_get(c->protocol->core, c->options->default_sink, PA_NAMEREG_SINK);
496 CHECK_VALIDITY(sink, "No such sink: %s", pa_strnull(c->options->default_sink));
497
498 source = sink->monitor_source;
499 CHECK_VALIDITY(source, "No such source.");
500 } else {
501 pa_assert(request == ESD_PROTO_STREAM_REC);
502
503 if (c->options->default_source) {
504 source = pa_namereg_get(c->protocol->core, c->options->default_source, PA_NAMEREG_SOURCE);
505 CHECK_VALIDITY(source, "No such source: %s", pa_strnull(c->options->default_source));
506 }
507 }
508
509 pa_strlcpy(name, data, sizeof(name));
510
511 utf8_name = pa_utf8_filter(name);
512 pa_client_set_name(c->client, utf8_name);
513 pa_xfree(utf8_name);
514
515 c->original_name = pa_xstrdup(name);
516
517 pa_assert(!c->output_memblockq && !c->source_output);
518
519 pa_source_output_new_data_init(&sdata);
520 sdata.driver = __FILE__;
521 sdata.module = c->options->module;
522 sdata.client = c->client;
523 sdata.source = source;
524 pa_source_output_new_data_set_sample_spec(&sdata, &ss);
525
526 pa_source_output_new(&c->source_output, c->protocol->core, &sdata, 0);
527 pa_source_output_new_data_done(&sdata);
528
529 CHECK_VALIDITY(c->source_output, "Failed to create source output.");
530
531 l = (size_t) (pa_bytes_per_second(&ss)*RECORD_BUFFER_SECONDS);
532 c->output_memblockq = pa_memblockq_new(
533 0,
534 l,
535 l,
536 pa_frame_size(&ss),
537 1,
538 0,
539 0,
540 NULL);
541 pa_iochannel_socket_set_sndbuf(c->io, l);
542
543 c->source_output->push = source_output_push_cb;
544 c->source_output->kill = source_output_kill_cb;
545 c->source_output->get_latency = source_output_get_latency_cb;
546 c->source_output->userdata = c;
547
548 pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY);
549
550 c->state = ESD_STREAMING_DATA;
551
552 c->protocol->n_player++;
553
554 pa_source_output_put(c->source_output);
555
556 return 0;
557 }
558
559 static int esd_proto_get_latency(connection *c, esd_proto_t request, const void *data, size_t length) {
560 pa_sink *sink;
561 int32_t latency;
562
563 connection_ref(c);
564 pa_assert(!data);
565 pa_assert(length == 0);
566
567 if (!(sink = pa_namereg_get(c->protocol->core, c->options->default_sink, PA_NAMEREG_SINK)))
568 latency = 0;
569 else {
570 double usec = (double) pa_sink_get_latency(sink);
571 latency = (int) ((usec*44100)/1000000);
572 }
573
574 latency = PA_MAYBE_INT32_SWAP(c->swap_byte_order, latency);
575 connection_write(c, &latency, sizeof(int32_t));
576 return 0;
577 }
578
579 static int esd_proto_server_info(connection *c, esd_proto_t request, const void *data, size_t length) {
580 int32_t rate = 44100, format = ESD_STEREO|ESD_BITS16;
581 int32_t response;
582 pa_sink *sink;
583
584 connection_ref(c);
585 pa_assert(data);
586 pa_assert(length == sizeof(int32_t));
587
588 if ((sink = pa_namereg_get(c->protocol->core, c->options->default_sink, PA_NAMEREG_SINK))) {
589 rate = (int32_t) sink->sample_spec.rate;
590 format = format_native2esd(&sink->sample_spec);
591 }
592
593 connection_write_prepare(c, sizeof(int32_t) * 3);
594
595 response = 0;
596 connection_write(c, &response, sizeof(int32_t));
597 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
598 connection_write(c, &rate, sizeof(int32_t));
599 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
600 connection_write(c, &format, sizeof(int32_t));
601
602 return 0;
603 }
604
605 static int esd_proto_all_info(connection *c, esd_proto_t request, const void *data, size_t length) {
606 size_t t, k, s;
607 connection *conn;
608 uint32_t idx = PA_IDXSET_INVALID;
609 unsigned nsamples;
610 char terminator[sizeof(int32_t)*6+ESD_NAME_MAX];
611
612 connection_ref(c);
613 pa_assert(data);
614 pa_assert(length == sizeof(int32_t));
615
616 if (esd_proto_server_info(c, request, data, length) < 0)
617 return -1;
618
619 k = sizeof(int32_t)*5+ESD_NAME_MAX;
620 s = sizeof(int32_t)*6+ESD_NAME_MAX;
621 nsamples = pa_idxset_size(c->protocol->core->scache);
622 t = s*(nsamples+1) + k*(c->protocol->n_player+1);
623
624 connection_write_prepare(c, t);
625
626 memset(terminator, 0, sizeof(terminator));
627
628 for (conn = pa_idxset_first(c->protocol->connections, &idx); conn; conn = pa_idxset_next(c->protocol->connections, &idx)) {
629 int32_t id, format = ESD_BITS16 | ESD_STEREO, rate = 44100, lvolume = ESD_VOLUME_BASE, rvolume = ESD_VOLUME_BASE;
630 char name[ESD_NAME_MAX];
631
632 if (conn->state != ESD_STREAMING_DATA)
633 continue;
634
635 pa_assert(t >= k*2+s);
636
637 if (conn->sink_input) {
638 pa_cvolume volume = *pa_sink_input_get_volume(conn->sink_input);
639 rate = (int32_t) conn->sink_input->sample_spec.rate;
640 lvolume = (int32_t) ((volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
641 rvolume = (int32_t) ((volume.values[1]*ESD_VOLUME_BASE)/PA_VOLUME_NORM);
642 format = format_native2esd(&conn->sink_input->sample_spec);
643 }
644
645 /* id */
646 id = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) (conn->index+1));
647 connection_write(c, &id, sizeof(int32_t));
648
649 /* name */
650 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
651 if (conn->original_name)
652 strncpy(name, conn->original_name, ESD_NAME_MAX);
653 else if (conn->client && pa_proplist_gets(conn->client->proplist, PA_PROP_APPLICATION_NAME))
654 strncpy(name, pa_proplist_gets(conn->client->proplist, PA_PROP_APPLICATION_NAME), ESD_NAME_MAX);
655 connection_write(c, name, ESD_NAME_MAX);
656
657 /* rate */
658 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
659 connection_write(c, &rate, sizeof(int32_t));
660
661 /* left */
662 lvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, lvolume);
663 connection_write(c, &lvolume, sizeof(int32_t));
664
665 /*right*/
666 rvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rvolume);
667 connection_write(c, &rvolume, sizeof(int32_t));
668
669 /*format*/
670 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
671 connection_write(c, &format, sizeof(int32_t));
672
673 t -= k;
674 }
675
676 pa_assert(t == s*(nsamples+1)+k);
677 t -= k;
678
679 connection_write(c, terminator, k);
680
681 if (nsamples) {
682 pa_scache_entry *ce;
683
684 idx = PA_IDXSET_INVALID;
685 for (ce = pa_idxset_first(c->protocol->core->scache, &idx); ce; ce = pa_idxset_next(c->protocol->core->scache, &idx)) {
686 int32_t id, rate, lvolume, rvolume, format, len;
687 char name[ESD_NAME_MAX];
688
689 pa_assert(t >= s*2);
690
691 /* id */
692 id = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int) (ce->index+1));
693 connection_write(c, &id, sizeof(int32_t));
694
695 /* name */
696 memset(name, 0, ESD_NAME_MAX); /* don't leak old data */
697 if (strncmp(ce->name, SCACHE_PREFIX, sizeof(SCACHE_PREFIX)-1) == 0)
698 strncpy(name, ce->name+sizeof(SCACHE_PREFIX)-1, ESD_NAME_MAX);
699 else
700 pa_snprintf(name, ESD_NAME_MAX, "native.%s", ce->name);
701 connection_write(c, name, ESD_NAME_MAX);
702
703 /* rate */
704 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) ce->sample_spec.rate);
705 connection_write(c, &rate, sizeof(int32_t));
706
707 /* left */
708 lvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) ((ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM));
709 connection_write(c, &lvolume, sizeof(int32_t));
710
711 /*right*/
712 rvolume = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int32_t) ((ce->volume.values[0]*ESD_VOLUME_BASE)/PA_VOLUME_NORM));
713 connection_write(c, &rvolume, sizeof(int32_t));
714
715 /*format*/
716 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format_native2esd(&ce->sample_spec));
717 connection_write(c, &format, sizeof(int32_t));
718
719 /*length*/
720 len = PA_MAYBE_INT32_SWAP(c->swap_byte_order, (int) ce->memchunk.length);
721 connection_write(c, &len, sizeof(int32_t));
722
723 t -= s;
724 }
725 }
726
727 pa_assert(t == s);
728
729 connection_write(c, terminator, s);
730
731 return 0;
732 }
733
734 static int esd_proto_stream_pan(connection *c, esd_proto_t request, const void *data, size_t length) {
735 int32_t ok;
736 uint32_t idx, lvolume, rvolume;
737 connection *conn;
738
739 connection_assert_ref(c);
740 pa_assert(data);
741 pa_assert(length == sizeof(int32_t)*3);
742
743 memcpy(&idx, data, sizeof(uint32_t));
744 idx = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
745 data = (const char*)data + sizeof(uint32_t);
746
747 memcpy(&lvolume, data, sizeof(uint32_t));
748 lvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, lvolume);
749 data = (const char*)data + sizeof(uint32_t);
750
751 memcpy(&rvolume, data, sizeof(uint32_t));
752 rvolume = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, rvolume);
753 data = (const char*)data + sizeof(uint32_t);
754
755 if ((conn = pa_idxset_get_by_index(c->protocol->connections, idx)) && conn->sink_input) {
756 pa_cvolume volume;
757 volume.values[0] = (lvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
758 volume.values[1] = (rvolume*PA_VOLUME_NORM)/ESD_VOLUME_BASE;
759 volume.channels = 2;
760 pa_sink_input_set_volume(conn->sink_input, &volume, TRUE);
761 ok = 1;
762 } else
763 ok = 0;
764
765 connection_write(c, &ok, sizeof(int32_t));
766
767 return 0;
768 }
769
770 static int esd_proto_sample_cache(connection *c, esd_proto_t request, const void *data, size_t length) {
771 pa_sample_spec ss;
772 int32_t format, rate, sc_length;
773 uint32_t idx;
774 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
775
776 connection_assert_ref(c);
777 pa_assert(data);
778 pa_assert(length == (ESD_NAME_MAX+3*sizeof(int32_t)));
779
780 memcpy(&format, data, sizeof(int32_t));
781 format = PA_MAYBE_INT32_SWAP(c->swap_byte_order, format);
782 data = (const char*)data + sizeof(int32_t);
783
784 memcpy(&rate, data, sizeof(int32_t));
785 rate = PA_MAYBE_INT32_SWAP(c->swap_byte_order, rate);
786 data = (const char*)data + sizeof(int32_t);
787
788 ss.rate = (uint32_t) rate;
789 format_esd2native(format, c->swap_byte_order, &ss);
790
791 CHECK_VALIDITY(pa_sample_spec_valid(&ss), "Invalid sample specification.");
792
793 memcpy(&sc_length, data, sizeof(int32_t));
794 sc_length = PA_MAYBE_INT32_SWAP(c->swap_byte_order, sc_length);
795 data = (const char*)data + sizeof(int32_t);
796
797 CHECK_VALIDITY(sc_length <= MAX_CACHE_SAMPLE_SIZE, "Sample too large (%d bytes).", (int)sc_length);
798
799 strcpy(name, SCACHE_PREFIX);
800 pa_strlcpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
801
802 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
803
804 pa_assert(!c->scache.memchunk.memblock);
805 c->scache.memchunk.memblock = pa_memblock_new(c->protocol->core->mempool, (size_t) sc_length);
806 c->scache.memchunk.index = 0;
807 c->scache.memchunk.length = (size_t) sc_length;
808 c->scache.sample_spec = ss;
809 pa_assert(!c->scache.name);
810 c->scache.name = pa_xstrdup(name);
811
812 c->state = ESD_CACHING_SAMPLE;
813
814 pa_scache_add_item(c->protocol->core, c->scache.name, NULL, NULL, NULL, c->client->proplist, &idx);
815
816 idx += 1;
817 connection_write(c, &idx, sizeof(uint32_t));
818
819 return 0;
820 }
821
822 static int esd_proto_sample_get_id(connection *c, esd_proto_t request, const void *data, size_t length) {
823 int32_t ok;
824 uint32_t idx;
825 char name[ESD_NAME_MAX+sizeof(SCACHE_PREFIX)-1];
826
827 connection_assert_ref(c);
828 pa_assert(data);
829 pa_assert(length == ESD_NAME_MAX);
830
831 strcpy(name, SCACHE_PREFIX);
832 pa_strlcpy(name+sizeof(SCACHE_PREFIX)-1, data, ESD_NAME_MAX);
833
834 CHECK_VALIDITY(pa_utf8_valid(name), "Invalid UTF8 in sample name.");
835
836 ok = -1;
837 if ((idx = pa_scache_get_id_by_name(c->protocol->core, name)) != PA_IDXSET_INVALID)
838 ok = (int32_t) idx + 1;
839
840 connection_write(c, &ok, sizeof(int32_t));
841
842 return 0;
843 }
844
845 static int esd_proto_sample_free_or_play(connection *c, esd_proto_t request, const void *data, size_t length) {
846 int32_t ok;
847 const char *name;
848 uint32_t idx;
849
850 connection_assert_ref(c);
851 pa_assert(data);
852 pa_assert(length == sizeof(int32_t));
853
854 memcpy(&idx, data, sizeof(uint32_t));
855 idx = PA_MAYBE_UINT32_SWAP(c->swap_byte_order, idx) - 1;
856
857 ok = 0;
858
859 if ((name = pa_scache_get_name_by_id(c->protocol->core, idx))) {
860 if (request == ESD_PROTO_SAMPLE_PLAY) {
861 pa_sink *sink;
862
863 if ((sink = pa_namereg_get(c->protocol->core, c->options->default_sink, PA_NAMEREG_SINK)))
864 if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM, c->client->proplist, NULL) >= 0)
865 ok = (int32_t) idx + 1;
866 } else {
867 pa_assert(request == ESD_PROTO_SAMPLE_FREE);
868
869 if (pa_scache_remove_item(c->protocol->core, name) >= 0)
870 ok = (int32_t) idx + 1;
871 }
872 }
873
874 connection_write(c, &ok, sizeof(int32_t));
875
876 return 0;
877 }
878
879 static int esd_proto_standby_or_resume(connection *c, esd_proto_t request, const void *data, size_t length) {
880 int32_t ok;
881
882 connection_assert_ref(c);
883
884 connection_write_prepare(c, sizeof(int32_t) * 2);
885
886 ok = 1;
887 connection_write(c, &ok, sizeof(int32_t));
888 connection_write(c, &ok, sizeof(int32_t));
889
890 return 0;
891 }
892
893 /*** client callbacks ***/
894
895 static void client_kill_cb(pa_client *c) {
896 pa_assert(c);
897
898 connection_unlink(CONNECTION(c->userdata));
899 }
900
901 /*** pa_iochannel callbacks ***/
902
903 static int do_read(connection *c) {
904 connection_assert_ref(c);
905
906 /* pa_log("READ"); */
907
908 if (c->state == ESD_NEXT_REQUEST) {
909 ssize_t r;
910 pa_assert(c->read_data_length < sizeof(c->request));
911
912 if ((r = pa_iochannel_read(c->io,
913 ((uint8_t*) &c->request) + c->read_data_length,
914 sizeof(c->request) - c->read_data_length)) <= 0) {
915
916 if (r < 0 && (errno == EINTR || errno == EAGAIN))
917 return 0;
918
919 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
920 return -1;
921 }
922
923 c->read_data_length += (size_t) r;
924
925 if (c->read_data_length >= sizeof(c->request)) {
926 struct proto_handler *handler;
927
928 c->request = PA_MAYBE_INT32_SWAP(c->swap_byte_order, c->request);
929
930 if (c->request < ESD_PROTO_CONNECT || c->request >= ESD_PROTO_MAX) {
931 pa_log("recieved invalid request.");
932 return -1;
933 }
934
935 handler = proto_map+c->request;
936
937 /* pa_log("executing request #%u", c->request); */
938
939 if (!handler->proc) {
940 pa_log("recieved unimplemented request #%u.", c->request);
941 return -1;
942 }
943
944 if (handler->data_length == 0) {
945 c->read_data_length = 0;
946
947 if (handler->proc(c, c->request, NULL, 0) < 0)
948 return -1;
949
950 } else {
951 if (c->read_data_alloc < handler->data_length)
952 c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length);
953 pa_assert(c->read_data);
954
955 c->state = ESD_NEEDS_REQDATA;
956 c->read_data_length = 0;
957 }
958 }
959
960 } else if (c->state == ESD_NEEDS_REQDATA) {
961 ssize_t r;
962 struct proto_handler *handler = proto_map+c->request;
963
964 pa_assert(handler->proc);
965
966 pa_assert(c->read_data && c->read_data_length < handler->data_length);
967
968 if ((r = pa_iochannel_read(c->io,
969 (uint8_t*) c->read_data + c->read_data_length,
970 handler->data_length - c->read_data_length)) <= 0) {
971
972 if (r < 0 && (errno == EINTR || errno == EAGAIN))
973 return 0;
974
975 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
976 return -1;
977 }
978
979 c->read_data_length += (size_t) r;
980 if (c->read_data_length >= handler->data_length) {
981 size_t l = c->read_data_length;
982 pa_assert(handler->proc);
983
984 c->state = ESD_NEXT_REQUEST;
985 c->read_data_length = 0;
986
987 if (handler->proc(c, c->request, c->read_data, l) < 0)
988 return -1;
989 }
990 } else if (c->state == ESD_CACHING_SAMPLE) {
991 ssize_t r;
992 void *p;
993
994 pa_assert(c->scache.memchunk.memblock);
995 pa_assert(c->scache.name);
996 pa_assert(c->scache.memchunk.index < c->scache.memchunk.length);
997
998 p = pa_memblock_acquire(c->scache.memchunk.memblock);
999 r = pa_iochannel_read(c->io, (uint8_t*) p+c->scache.memchunk.index, c->scache.memchunk.length-c->scache.memchunk.index);
1000 pa_memblock_release(c->scache.memchunk.memblock);
1001
1002 if (r <= 0) {
1003 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1004 return 0;
1005
1006 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
1007 return -1;
1008 }
1009
1010 c->scache.memchunk.index += (size_t) r;
1011 pa_assert(c->scache.memchunk.index <= c->scache.memchunk.length);
1012
1013 if (c->scache.memchunk.index == c->scache.memchunk.length) {
1014 uint32_t idx;
1015
1016 c->scache.memchunk.index = 0;
1017 pa_scache_add_item(c->protocol->core, c->scache.name, &c->scache.sample_spec, NULL, &c->scache.memchunk, c->client->proplist, &idx);
1018
1019 pa_memblock_unref(c->scache.memchunk.memblock);
1020 c->scache.memchunk.memblock = NULL;
1021 c->scache.memchunk.index = c->scache.memchunk.length = 0;
1022
1023 pa_xfree(c->scache.name);
1024 c->scache.name = NULL;
1025
1026 c->state = ESD_NEXT_REQUEST;
1027
1028 idx += 1;
1029 connection_write(c, &idx, sizeof(uint32_t));
1030 }
1031
1032 } else if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1033 pa_memchunk chunk;
1034 ssize_t r;
1035 size_t l;
1036 void *p;
1037 size_t space;
1038
1039 pa_assert(c->input_memblockq);
1040
1041 /* pa_log("STREAMING_DATA"); */
1042
1043 if (!(l = (size_t) pa_atomic_load(&c->playback.missing)))
1044 return 0;
1045
1046 if (c->playback.current_memblock) {
1047
1048 space = pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index;
1049
1050 if (space <= 0) {
1051 pa_memblock_unref(c->playback.current_memblock);
1052 c->playback.current_memblock = NULL;
1053 }
1054 }
1055
1056 if (!c->playback.current_memblock) {
1057 pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, (size_t) -1));
1058 c->playback.memblock_index = 0;
1059
1060 space = pa_memblock_get_length(c->playback.current_memblock);
1061 }
1062
1063 if (l > space)
1064 l = space;
1065
1066 p = pa_memblock_acquire(c->playback.current_memblock);
1067 r = pa_iochannel_read(c->io, (uint8_t*) p+c->playback.memblock_index, l);
1068 pa_memblock_release(c->playback.current_memblock);
1069
1070 if (r <= 0) {
1071
1072 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1073 return 0;
1074
1075 pa_log_debug("read(): %s", r < 0 ? pa_cstrerror(errno) : "EOF");
1076 return -1;
1077 }
1078
1079 chunk.memblock = c->playback.current_memblock;
1080 chunk.index = c->playback.memblock_index;
1081 chunk.length = (size_t) r;
1082
1083 c->playback.memblock_index += (size_t) r;
1084
1085 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
1086 pa_atomic_sub(&c->playback.missing, (int) r);
1087 }
1088
1089 return 0;
1090 }
1091
1092 static int do_write(connection *c) {
1093 connection_assert_ref(c);
1094
1095 /* pa_log("WRITE"); */
1096
1097 if (c->write_data_length) {
1098 ssize_t r;
1099
1100 pa_assert(c->write_data_index < c->write_data_length);
1101 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) {
1102
1103 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1104 return 0;
1105
1106 pa_log("write(): %s", pa_cstrerror(errno));
1107 return -1;
1108 }
1109
1110 c->write_data_index += (size_t) r;
1111 if (c->write_data_index >= c->write_data_length)
1112 c->write_data_length = c->write_data_index = 0;
1113
1114 } else if (c->state == ESD_STREAMING_DATA && c->source_output) {
1115 pa_memchunk chunk;
1116 ssize_t r;
1117 void *p;
1118
1119 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
1120 return 0;
1121
1122 pa_assert(chunk.memblock);
1123 pa_assert(chunk.length);
1124
1125 p = pa_memblock_acquire(chunk.memblock);
1126 r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
1127 pa_memblock_release(chunk.memblock);
1128
1129 pa_memblock_unref(chunk.memblock);
1130
1131 if (r < 0) {
1132
1133 if (r < 0 && (errno == EINTR || errno == EAGAIN))
1134 return 0;
1135
1136 pa_log("write(): %s", pa_cstrerror(errno));
1137 return -1;
1138 }
1139
1140 pa_memblockq_drop(c->output_memblockq, (size_t) r);
1141 }
1142
1143 return 0;
1144 }
1145
1146 static void do_work(connection *c) {
1147 connection_assert_ref(c);
1148
1149 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
1150
1151 if (c->dead)
1152 return;
1153
1154 if (pa_iochannel_is_readable(c->io))
1155 if (do_read(c) < 0)
1156 goto fail;
1157
1158 if (c->state == ESD_STREAMING_DATA && !c->sink_input && pa_iochannel_is_hungup(c->io))
1159 /* In case we are in capture mode we will never call read()
1160 * on the socket, hence we need to detect the hangup manually
1161 * here, instead of simply waiting for read() to return 0. */
1162 goto fail;
1163
1164 if (pa_iochannel_is_writable(c->io))
1165 if (do_write(c) < 0)
1166 goto fail;
1167
1168 return;
1169
1170 fail:
1171
1172 if (c->state == ESD_STREAMING_DATA && c->sink_input) {
1173 c->dead = TRUE;
1174
1175 pa_iochannel_free(c->io);
1176 c->io = NULL;
1177
1178 pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
1179 } else
1180 connection_unlink(c);
1181 }
1182
1183 static void io_callback(pa_iochannel*io, void *userdata) {
1184 connection *c = CONNECTION(userdata);
1185
1186 connection_assert_ref(c);
1187 pa_assert(io);
1188
1189 do_work(c);
1190 }
1191
1192 static void defer_callback(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
1193 connection *c = CONNECTION(userdata);
1194
1195 connection_assert_ref(c);
1196 pa_assert(e);
1197
1198 do_work(c);
1199 }
1200
1201 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
1202 connection *c = CONNECTION(o);
1203 connection_assert_ref(c);
1204
1205 switch (code) {
1206 case CONNECTION_MESSAGE_REQUEST_DATA:
1207 do_work(c);
1208 break;
1209
1210 case CONNECTION_MESSAGE_POST_DATA:
1211 /* pa_log("got data %u", chunk->length); */
1212 pa_memblockq_push_align(c->output_memblockq, chunk);
1213 do_work(c);
1214 break;
1215
1216 case CONNECTION_MESSAGE_UNLINK_CONNECTION:
1217 connection_unlink(c);
1218 break;
1219 }
1220
1221 return 0;
1222 }
1223
1224 /*** sink_input callbacks ***/
1225
1226 /* Called from thread context */
1227 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
1228 pa_sink_input *i = PA_SINK_INPUT(o);
1229 connection*c;
1230
1231 pa_sink_input_assert_ref(i);
1232 c = CONNECTION(i->userdata);
1233 connection_assert_ref(c);
1234
1235 switch (code) {
1236
1237 case SINK_INPUT_MESSAGE_POST_DATA: {
1238 pa_assert(chunk);
1239
1240 /* New data from the main loop */
1241 pa_memblockq_push_align(c->input_memblockq, chunk);
1242
1243 if (pa_memblockq_is_readable(c->input_memblockq) && c->playback.underrun) {
1244 pa_log_debug("Requesting rewind due to end of underrun.");
1245 pa_sink_input_request_rewind(c->sink_input, 0, FALSE, TRUE, FALSE);
1246 }
1247
1248 /* pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
1249
1250 return 0;
1251 }
1252
1253 case SINK_INPUT_MESSAGE_DISABLE_PREBUF:
1254 pa_memblockq_prebuf_disable(c->input_memblockq);
1255 return 0;
1256
1257 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
1258 pa_usec_t *r = userdata;
1259
1260 *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
1261
1262 /* Fall through, the default handler will add in the extra
1263 * latency added by the resampler */
1264 }
1265
1266 default:
1267 return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
1268 }
1269 }
1270
1271 /* Called from thread context */
1272 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
1273 connection*c;
1274
1275 pa_sink_input_assert_ref(i);
1276 c = CONNECTION(i->userdata);
1277 connection_assert_ref(c);
1278 pa_assert(chunk);
1279
1280 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
1281
1282 c->playback.underrun = TRUE;
1283
1284 if (c->dead && pa_sink_input_safe_to_remove(i))
1285 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
1286
1287 return -1;
1288 } else {
1289 size_t m;
1290
1291 chunk->length = PA_MIN(length, chunk->length);
1292
1293 c->playback.underrun = FALSE;
1294
1295 pa_memblockq_drop(c->input_memblockq, chunk->length);
1296 m = pa_memblockq_pop_missing(c->input_memblockq);
1297
1298 if (m > 0)
1299 if (pa_atomic_add(&c->playback.missing, (int) m) <= 0)
1300 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
1301
1302 return 0;
1303 }
1304 }
1305
1306 /* Called from thread context */
1307 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
1308 connection *c;
1309
1310 pa_sink_input_assert_ref(i);
1311 c = CONNECTION(i->userdata);
1312 connection_assert_ref(c);
1313
1314 /* If we are in an underrun, then we don't rewind */
1315 if (i->thread_info.underrun_for > 0)
1316 return;
1317
1318 pa_memblockq_rewind(c->input_memblockq, nbytes);
1319 }
1320
1321 /* Called from thread context */
1322 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
1323 connection *c;
1324
1325 pa_sink_input_assert_ref(i);
1326 c = CONNECTION(i->userdata);
1327 connection_assert_ref(c);
1328
1329 pa_memblockq_set_maxrewind(c->input_memblockq, nbytes);
1330 }
1331
1332 static void sink_input_kill_cb(pa_sink_input *i) {
1333 pa_sink_input_assert_ref(i);
1334
1335 connection_unlink(CONNECTION(i->userdata));
1336 }
1337
1338 /*** source_output callbacks ***/
1339
1340 /* Called from thread context */
1341 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
1342 connection *c;
1343
1344 pa_source_output_assert_ref(o);
1345 c = CONNECTION(o->userdata);
1346 pa_assert(c);
1347 pa_assert(chunk);
1348
1349 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
1350 }
1351
1352 static void source_output_kill_cb(pa_source_output *o) {
1353 pa_source_output_assert_ref(o);
1354
1355 connection_unlink(CONNECTION(o->userdata));
1356 }
1357
1358 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
1359 connection*c;
1360
1361 pa_source_output_assert_ref(o);
1362 c = CONNECTION(o->userdata);
1363 pa_assert(c);
1364
1365 return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
1366 }
1367
1368 /*** entry points ***/
1369
1370 static void auth_timeout(pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata) {
1371 connection *c = CONNECTION(userdata);
1372
1373 pa_assert(m);
1374 pa_assert(tv);
1375 connection_assert_ref(c);
1376 pa_assert(c->auth_timeout_event == e);
1377
1378 if (!c->authorized)
1379 connection_unlink(c);
1380 }
1381
1382 void pa_esound_protocol_connect(pa_esound_protocol *p, pa_iochannel *io, pa_esound_options *o) {
1383 connection *c;
1384 char pname[128];
1385 pa_client_new_data data;
1386 pa_client *client;
1387
1388 pa_assert(p);
1389 pa_assert(io);
1390 pa_assert(o);
1391
1392 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
1393 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
1394 pa_iochannel_free(io);
1395 return;
1396 }
1397
1398 pa_client_new_data_init(&data);
1399 data.module = o->module;
1400 data.driver = __FILE__;
1401 pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
1402 pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "EsounD client (%s)", pname);
1403 pa_proplist_sets(data.proplist, "esound-protocol.peer", pname);
1404 client = pa_client_new(p->core, &data);
1405 pa_client_new_data_done(&data);
1406
1407 if (!client)
1408 return;
1409
1410 c = pa_msgobject_new(connection);
1411 c->parent.parent.free = connection_free;
1412 c->parent.process_msg = connection_process_msg;
1413 c->protocol = p;
1414 c->io = io;
1415 pa_iochannel_set_callback(c->io, io_callback, c);
1416
1417 c->client = client;
1418 c->client->kill = client_kill_cb;
1419 c->client->userdata = c;
1420
1421 c->options = pa_esound_options_ref(o);
1422 c->authorized = FALSE;
1423 c->swap_byte_order = FALSE;
1424 c->dead = FALSE;
1425
1426 c->read_data_length = 0;
1427 c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length);
1428
1429 c->write_data_length = c->write_data_index = c->write_data_alloc = 0;
1430 c->write_data = NULL;
1431
1432 c->state = ESD_NEEDS_REQDATA;
1433 c->request = ESD_PROTO_CONNECT;
1434
1435 c->sink_input = NULL;
1436 c->input_memblockq = NULL;
1437
1438 c->source_output = NULL;
1439 c->output_memblockq = NULL;
1440
1441 c->playback.current_memblock = NULL;
1442 c->playback.memblock_index = 0;
1443 c->playback.underrun = TRUE;
1444 pa_atomic_store(&c->playback.missing, 0);
1445
1446 pa_memchunk_reset(&c->scache.memchunk);
1447 c->scache.name = NULL;
1448
1449 c->original_name = NULL;
1450
1451 if (o->auth_anonymous) {
1452 pa_log_info("Client authenticated anonymously.");
1453 c->authorized = TRUE;
1454 }
1455
1456 if (!c->authorized &&
1457 o->auth_ip_acl &&
1458 pa_ip_acl_check(o->auth_ip_acl, pa_iochannel_get_recv_fd(io)) > 0) {
1459
1460 pa_log_info("Client authenticated by IP ACL.");
1461 c->authorized = TRUE;
1462 }
1463
1464 if (!c->authorized) {
1465 struct timeval tv;
1466 pa_gettimeofday(&tv);
1467 tv.tv_sec += AUTH_TIMEOUT;
1468 c->auth_timeout_event = p->core->mainloop->time_new(p->core->mainloop, &tv, auth_timeout, c);
1469 } else
1470 c->auth_timeout_event = NULL;
1471
1472 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
1473 p->core->mainloop->defer_enable(c->defer_event, 0);
1474
1475 pa_idxset_put(p->connections, c, &c->index);
1476 }
1477
1478 void pa_esound_protocol_disconnect(pa_esound_protocol *p, pa_module *m) {
1479 connection *c;
1480 void *state = NULL;
1481
1482 pa_assert(p);
1483 pa_assert(m);
1484
1485 while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
1486 if (c->options->module == m)
1487 connection_unlink(c);
1488 }
1489
1490 static pa_esound_protocol* esound_protocol_new(pa_core *c) {
1491 pa_esound_protocol *p;
1492
1493 pa_assert(c);
1494
1495 p = pa_xnew(pa_esound_protocol, 1);
1496 PA_REFCNT_INIT(p);
1497 p->core = c;
1498 p->connections = pa_idxset_new(NULL, NULL);
1499 p->n_player = 0;
1500
1501 pa_assert_se(pa_shared_set(c, "esound-protocol", p) >= 0);
1502
1503 return p;
1504 }
1505
1506 pa_esound_protocol* pa_esound_protocol_get(pa_core *c) {
1507 pa_esound_protocol *p;
1508
1509 if ((p = pa_shared_get(c, "esound-protocol")))
1510 return pa_esound_protocol_ref(p);
1511
1512 return esound_protocol_new(c);
1513 }
1514
1515 pa_esound_protocol* pa_esound_protocol_ref(pa_esound_protocol *p) {
1516 pa_assert(p);
1517 pa_assert(PA_REFCNT_VALUE(p) >= 1);
1518
1519 PA_REFCNT_INC(p);
1520
1521 return p;
1522 }
1523
1524 void pa_esound_protocol_unref(pa_esound_protocol *p) {
1525 connection *c;
1526 pa_assert(p);
1527 pa_assert(PA_REFCNT_VALUE(p) >= 1);
1528
1529 if (PA_REFCNT_DEC(p) > 0)
1530 return;
1531
1532 while ((c = pa_idxset_first(p->connections, NULL)))
1533 connection_unlink(c);
1534
1535 pa_idxset_free(p->connections, NULL, NULL);
1536
1537 pa_assert_se(pa_shared_remove(p->core, "esound-protocol") >= 0);
1538
1539 pa_xfree(p);
1540 }
1541
1542 pa_esound_options* pa_esound_options_new(void) {
1543 pa_esound_options *o;
1544
1545 o = pa_xnew0(pa_esound_options, 1);
1546 PA_REFCNT_INIT(o);
1547
1548 return o;
1549 }
1550
1551 pa_esound_options* pa_esound_options_ref(pa_esound_options *o) {
1552 pa_assert(o);
1553 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1554
1555 PA_REFCNT_INC(o);
1556
1557 return o;
1558 }
1559
1560 void pa_esound_options_unref(pa_esound_options *o) {
1561 pa_assert(o);
1562 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1563
1564 if (PA_REFCNT_DEC(o) > 0)
1565 return;
1566
1567 if (o->auth_ip_acl)
1568 pa_ip_acl_free(o->auth_ip_acl);
1569
1570 if (o->auth_cookie)
1571 pa_auth_cookie_unref(o->auth_cookie);
1572
1573 pa_xfree(o->default_sink);
1574 pa_xfree(o->default_source);
1575
1576 pa_xfree(o);
1577 }
1578
1579 int pa_esound_options_parse(pa_esound_options *o, pa_core *c, pa_modargs *ma) {
1580 pa_bool_t enabled;
1581 const char *acl;
1582
1583 pa_assert(o);
1584 pa_assert(PA_REFCNT_VALUE(o) >= 1);
1585 pa_assert(ma);
1586
1587 if (pa_modargs_get_value_boolean(ma, "auth-anonymous", &o->auth_anonymous) < 0) {
1588 pa_log("auth-anonymous= expects a boolean argument.");
1589 return -1;
1590 }
1591
1592 if ((acl = pa_modargs_get_value(ma, "auth-ip-acl", NULL))) {
1593 pa_ip_acl *ipa;
1594
1595 if (!(ipa = pa_ip_acl_new(acl))) {
1596 pa_log("Failed to parse IP ACL '%s'", acl);
1597 return -1;
1598 }
1599
1600 if (o->auth_ip_acl)
1601 pa_ip_acl_free(o->auth_ip_acl);
1602
1603 o->auth_ip_acl = ipa;
1604 }
1605
1606 enabled = TRUE;
1607 if (pa_modargs_get_value_boolean(ma, "auth-cookie-enabled", &enabled) < 0) {
1608 pa_log("auth-cookie-enabled= expects a boolean argument.");
1609 return -1;
1610 }
1611
1612 if (o->auth_cookie)
1613 pa_auth_cookie_unref(o->auth_cookie);
1614
1615 if (enabled) {
1616 const char *cn;
1617
1618 /* The new name for this is 'auth-cookie', for compat reasons
1619 * we check the old name too */
1620 if (!(cn = pa_modargs_get_value(ma, "auth-cookie", NULL)))
1621 if (!(cn = pa_modargs_get_value(ma, "cookie", NULL)))
1622 cn = DEFAULT_COOKIE_FILE;
1623
1624 if (!(o->auth_cookie = pa_auth_cookie_get(c, cn, ESD_KEY_LEN)))
1625 return -1;
1626
1627 } else
1628 o->auth_cookie = NULL;
1629
1630 pa_xfree(o->default_sink);
1631 o->default_sink = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
1632
1633 pa_xfree(o->default_source);
1634 o->default_source = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
1635
1636 return 0;
1637 }