]> code.delx.au - pulseaudio/blob - src/modules/module-tunnel.c
fix a race condition with stream connection vs. latency measuremtn (found by theBear)
[pulseaudio] / src / modules / module-tunnel.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/util.h>
36 #include <pulse/version.h>
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-subscribe.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/pdispatch.h>
46 #include <pulsecore/pstream.h>
47 #include <pulsecore/pstream-util.h>
48 #include <pulsecore/authkey.h>
49 #include <pulsecore/socket-client.h>
50 #include <pulsecore/socket-util.h>
51 #include <pulsecore/authkey-prop.h>
52
53 #ifdef TUNNEL_SINK
54 #include "module-tunnel-sink-symdef.h"
55 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
56 PA_MODULE_USAGE(
57 "server=<address> "
58 "sink=<remote sink name> "
59 "cookie=<filename> "
60 "format=<sample format> "
61 "channels=<number of channels> "
62 "rate=<sample rate> "
63 "sink_name=<name for the local sink> "
64 "channel_map=<channel map>")
65 #else
66 #include "module-tunnel-source-symdef.h"
67 PA_MODULE_DESCRIPTION("Tunnel module for sources")
68 PA_MODULE_USAGE(
69 "server=<address> "
70 "source=<remote source name> "
71 "cookie=<filename> "
72 "format=<sample format> "
73 "channels=<number of channels> "
74 "rate=<sample rate> "
75 "source_name=<name for the local source> "
76 "channel_map=<channel map>")
77 #endif
78
79 PA_MODULE_AUTHOR("Lennart Poettering")
80 PA_MODULE_VERSION(PACKAGE_VERSION)
81
82 #define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
83 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
84 #define DEFAULT_MINREQ 512
85 #define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
86 #define DEFAULT_FRAGSIZE 1024
87
88 #define DEFAULT_TIMEOUT 5
89
90 #define LATENCY_INTERVAL 10
91
92 static const char* const valid_modargs[] = {
93 "server",
94 "cookie",
95 "format",
96 "channels",
97 "rate",
98 #ifdef TUNNEL_SINK
99 "sink_name",
100 "sink",
101 #else
102 "source_name",
103 "source",
104 #endif
105 "channel_map",
106 NULL,
107 };
108
109 static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
110 static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
111
112 #ifdef TUNNEL_SINK
113 static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
114 #endif
115
116 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
117 #ifdef TUNNEL_SINK
118 [PA_COMMAND_REQUEST] = command_request,
119 #endif
120 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
121 [PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
122 [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
123 };
124
125 struct userdata {
126 pa_socket_client *client;
127 pa_pstream *pstream;
128 pa_pdispatch *pdispatch;
129
130 char *server_name;
131 #ifdef TUNNEL_SINK
132 char *sink_name;
133 pa_sink *sink;
134 uint32_t requested_bytes;
135 #else
136 char *source_name;
137 pa_source *source;
138 #endif
139
140 pa_module *module;
141 pa_core *core;
142
143 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
144
145 uint32_t version;
146 uint32_t ctag;
147 uint32_t device_index;
148 uint32_t channel;
149
150 pa_usec_t host_latency;
151
152 pa_time_event *time_event;
153
154 int auth_cookie_in_property;
155 };
156
157 static void close_stuff(struct userdata *u) {
158 assert(u);
159
160 if (u->pstream) {
161 pa_pstream_close(u->pstream);
162 pa_pstream_unref(u->pstream);
163 u->pstream = NULL;
164 }
165
166 if (u->pdispatch) {
167 pa_pdispatch_unref(u->pdispatch);
168 u->pdispatch = NULL;
169 }
170
171 if (u->client) {
172 pa_socket_client_unref(u->client);
173 u->client = NULL;
174 }
175
176 #ifdef TUNNEL_SINK
177 if (u->sink) {
178 pa_sink_disconnect(u->sink);
179 pa_sink_unref(u->sink);
180 u->sink = NULL;
181 }
182 #else
183 if (u->source) {
184 pa_source_disconnect(u->source);
185 pa_source_unref(u->source);
186 u->source = NULL;
187 }
188 #endif
189
190 if (u->time_event) {
191 u->core->mainloop->time_free(u->time_event);
192 u->time_event = NULL;
193 }
194 }
195
196 static void die(struct userdata *u) {
197 assert(u);
198 close_stuff(u);
199 pa_module_unload_request(u->module);
200 }
201
202 static void command_stream_killed(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
203 struct userdata *u = userdata;
204 assert(pd && t && u && u->pdispatch == pd);
205
206 pa_log("stream killed");
207 die(u);
208 }
209
210 static void request_info(struct userdata *u);
211
212 static void command_subscribe_event(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
213 struct userdata *u = userdata;
214 pa_subscription_event_type_t e;
215 uint32_t idx;
216
217 assert(pd && t && u);
218 assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
219
220 if (pa_tagstruct_getu32(t, &e) < 0 ||
221 pa_tagstruct_getu32(t, &idx) < 0 ||
222 !pa_tagstruct_eof(t)) {
223 pa_log("invalid protocol reply");
224 die(u);
225 return;
226 }
227
228 #ifdef TUNNEL_SINK
229 if (e != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
230 return;
231 #else
232 if (e != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
233 return;
234 #endif
235
236 request_info(u);
237 }
238
239 #ifdef TUNNEL_SINK
240 static void send_prebuf_request(struct userdata *u) {
241 pa_tagstruct *t;
242
243 t = pa_tagstruct_new(NULL, 0);
244 pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
245 pa_tagstruct_putu32(t, u->ctag++);
246 pa_tagstruct_putu32(t, u->channel);
247 pa_pstream_send_tagstruct(u->pstream, t);
248 }
249
250 static void send_bytes(struct userdata *u) {
251 assert(u);
252
253 if (!u->pstream)
254 return;
255
256 while (u->requested_bytes > 0) {
257 pa_memchunk chunk;
258 if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
259
260 if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
261 send_prebuf_request(u);
262
263 return;
264 }
265
266 pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, &chunk);
267 pa_memblock_unref(chunk.memblock);
268
269 /* pa_log("sent %lu", (unsigned long) chunk.length); */
270
271 if (chunk.length > u->requested_bytes)
272 u->requested_bytes = 0;
273 else
274 u->requested_bytes -= chunk.length;
275 }
276 }
277
278 static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
279 struct userdata *u = userdata;
280 uint32_t bytes, channel;
281 assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
282
283 if (pa_tagstruct_getu32(t, &channel) < 0 ||
284 pa_tagstruct_getu32(t, &bytes) < 0 ||
285 !pa_tagstruct_eof(t)) {
286 pa_log("invalid protocol reply");
287 die(u);
288 return;
289 }
290
291 if (channel != u->channel) {
292 pa_log("recieved data for invalid channel");
293 die(u);
294 return;
295 }
296
297 u->requested_bytes += bytes;
298 send_bytes(u);
299 }
300
301 #endif
302
303 static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
304 struct userdata *u = userdata;
305 pa_usec_t sink_usec, source_usec, transport_usec;
306 int playing;
307 int64_t write_index, read_index;
308 struct timeval local, remote, now;
309 assert(pd && u);
310
311 if (command != PA_COMMAND_REPLY) {
312 if (command == PA_COMMAND_ERROR)
313 pa_log("failed to get latency.");
314 else
315 pa_log("protocol error.");
316 die(u);
317 return;
318 }
319
320 if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
321 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
322 pa_tagstruct_get_boolean(t, &playing) < 0 ||
323 pa_tagstruct_get_timeval(t, &local) < 0 ||
324 pa_tagstruct_get_timeval(t, &remote) < 0 ||
325 pa_tagstruct_gets64(t, &write_index) < 0 ||
326 pa_tagstruct_gets64(t, &read_index) < 0 ||
327 !pa_tagstruct_eof(t)) {
328 pa_log("invalid reply. (latency)");
329 die(u);
330 return;
331 }
332
333 pa_gettimeofday(&now);
334
335 /* FIXME! This could use some serious love. */
336
337 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
338 /* local and remote seem to have synchronized clocks */
339 #ifdef TUNNEL_SINK
340 transport_usec = pa_timeval_diff(&remote, &local);
341 #else
342 transport_usec = pa_timeval_diff(&now, &remote);
343 #endif
344 } else
345 transport_usec = pa_timeval_diff(&now, &local)/2;
346
347 #ifdef TUNNEL_SINK
348 u->host_latency = sink_usec + transport_usec;
349 #else
350 u->host_latency = source_usec + transport_usec;
351 if (u->host_latency > sink_usec)
352 u->host_latency -= sink_usec;
353 else
354 u->host_latency = 0;
355 #endif
356
357 /* pa_log("estimated host latency: %0.0f usec", (double) u->host_latency); */
358 }
359
360 static void request_latency(struct userdata *u) {
361 pa_tagstruct *t;
362 struct timeval now;
363 uint32_t tag;
364 assert(u);
365
366 t = pa_tagstruct_new(NULL, 0);
367 #ifdef TUNNEL_SINK
368 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
369 #else
370 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
371 #endif
372 pa_tagstruct_putu32(t, tag = u->ctag++);
373 pa_tagstruct_putu32(t, u->channel);
374
375 pa_gettimeofday(&now);
376 pa_tagstruct_put_timeval(t, &now);
377
378 pa_pstream_send_tagstruct(u->pstream, t);
379 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
380 }
381
382 static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
383 struct userdata *u = userdata;
384 uint32_t idx, owner_module, monitor_source;
385 pa_usec_t latency;
386 const char *name, *description, *monitor_source_name, *driver;
387 int mute;
388 uint32_t flags;
389 pa_sample_spec sample_spec;
390 pa_channel_map channel_map;
391 pa_cvolume volume;
392 assert(pd && u);
393
394 if (command != PA_COMMAND_REPLY) {
395 if (command == PA_COMMAND_ERROR)
396 pa_log("failed to get info.");
397 else
398 pa_log("protocol error.");
399 die(u);
400 return;
401 }
402
403 if (pa_tagstruct_getu32(t, &idx) < 0 ||
404 pa_tagstruct_gets(t, &name) < 0 ||
405 pa_tagstruct_gets(t, &description) < 0 ||
406 pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
407 pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
408 pa_tagstruct_getu32(t, &owner_module) < 0 ||
409 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
410 pa_tagstruct_get_boolean(t, &mute) < 0 ||
411 pa_tagstruct_getu32(t, &monitor_source) < 0 ||
412 pa_tagstruct_gets(t, &monitor_source_name) < 0 ||
413 pa_tagstruct_get_usec(t, &latency) < 0 ||
414 pa_tagstruct_gets(t, &driver) < 0 ||
415 pa_tagstruct_getu32(t, &flags) < 0 ||
416 !pa_tagstruct_eof(t)) {
417 pa_log("invalid reply. (get_info)");
418 die(u);
419 return;
420 }
421
422 #ifdef TUNNEL_SINK
423 assert(u->sink);
424 if ((!!mute == !!u->sink->hw_muted) &&
425 pa_cvolume_equal(&volume, &u->sink->hw_volume))
426 return;
427 #else
428 assert(u->source);
429 if ((!!mute == !!u->source->hw_muted) &&
430 pa_cvolume_equal(&volume, &u->source->hw_volume))
431 return;
432 #endif
433
434 #ifdef TUNNEL_SINK
435 memcpy(&u->sink->hw_volume, &volume, sizeof(pa_cvolume));
436 u->sink->hw_muted = !!mute;
437
438 pa_subscription_post(u->sink->core,
439 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
440 u->sink->index);
441 #else
442 memcpy(&u->source->hw_volume, &volume, sizeof(pa_cvolume));
443 u->source->hw_muted = !!mute;
444
445 pa_subscription_post(u->source->core,
446 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
447 u->source->index);
448 #endif
449 }
450
451 static void request_info(struct userdata *u) {
452 pa_tagstruct *t;
453 uint32_t tag;
454 assert(u);
455
456 t = pa_tagstruct_new(NULL, 0);
457 #ifdef TUNNEL_SINK
458 pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INFO);
459 #else
460 pa_tagstruct_putu32(t, PA_COMMAND_GET_SOURCE_INFO);
461 #endif
462 pa_tagstruct_putu32(t, tag = u->ctag++);
463
464 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
465 #ifdef TUNNEL_SINK
466 pa_tagstruct_puts(t, u->sink_name);
467 #else
468 pa_tagstruct_puts(t, u->source_name);
469 #endif
470
471 pa_pstream_send_tagstruct(u->pstream, t);
472 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u, NULL);
473 }
474
475 static void start_subscribe(struct userdata *u) {
476 pa_tagstruct *t;
477 uint32_t tag;
478 assert(u);
479
480 t = pa_tagstruct_new(NULL, 0);
481 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
482 pa_tagstruct_putu32(t, tag = u->ctag++);
483
484 #ifdef TUNNEL_SINK
485 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK);
486 #else
487 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SOURCE);
488 #endif
489
490 pa_pstream_send_tagstruct(u->pstream, t);
491 }
492
493 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
494 struct userdata *u = userdata;
495 struct timeval ntv;
496 assert(m && e && u);
497
498 request_latency(u);
499
500 pa_gettimeofday(&ntv);
501 ntv.tv_sec += LATENCY_INTERVAL;
502 m->time_restart(e, &ntv);
503 }
504
505 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
506 struct userdata *u = userdata;
507 struct timeval ntv;
508 assert(pd && u && u->pdispatch == pd);
509
510 if (command != PA_COMMAND_REPLY) {
511 if (command == PA_COMMAND_ERROR)
512 pa_log("failed to create stream.");
513 else
514 pa_log("protocol error.");
515 die(u);
516 return;
517 }
518
519 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
520 pa_tagstruct_getu32(t, &u->device_index) < 0
521 #ifdef TUNNEL_SINK
522 || pa_tagstruct_getu32(t, &u->requested_bytes) < 0
523 #endif
524 )
525 goto parse_error;
526
527 if (u->version >= 9) {
528 #ifdef TUNNEL_SINK
529 uint32_t maxlength, tlength, prebuf, minreq;
530
531 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
532 pa_tagstruct_getu32(t, &tlength) < 0 ||
533 pa_tagstruct_getu32(t, &prebuf) < 0 ||
534 pa_tagstruct_getu32(t, &minreq) < 0)
535 goto parse_error;
536 #else
537 uint32_t maxlength, fragsize;
538
539 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
540 pa_tagstruct_getu32(t, &fragsize) < 0)
541 goto parse_error;
542 #endif
543 }
544
545 if (!pa_tagstruct_eof(t))
546 goto parse_error;
547
548 start_subscribe(u);
549 request_info(u);
550
551 assert(!u->time_event);
552 pa_gettimeofday(&ntv);
553 ntv.tv_sec += LATENCY_INTERVAL;
554 u->time_event = u->core->mainloop->time_new(u->core->mainloop, &ntv, timeout_callback, u);
555
556 request_latency(u);
557 #ifdef TUNNEL_SINK
558 send_bytes(u);
559 #endif
560
561 return;
562
563 parse_error:
564 pa_log("invalid reply. (create stream)");
565 die(u);
566 }
567
568 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
569 struct userdata *u = userdata;
570 pa_tagstruct *reply;
571 char name[256], un[128], hn[128];
572 #ifdef TUNNEL_SINK
573 pa_cvolume volume;
574 #endif
575 assert(pd && u && u->pdispatch == pd);
576
577 if (command != PA_COMMAND_REPLY ||
578 pa_tagstruct_getu32(t, &u->version) < 0 ||
579 !pa_tagstruct_eof(t)) {
580 if (command == PA_COMMAND_ERROR)
581 pa_log("failed to authenticate");
582 else
583 pa_log("protocol error.");
584 die(u);
585 return;
586 }
587
588 /* Minimum supported protocol version */
589 if (u->version < 8) {
590 pa_log("incompatible protocol version");
591 die(u);
592 return;
593 }
594
595 #ifdef TUNNEL_SINK
596 snprintf(name, sizeof(name), "Tunnel from host %s, user %s, sink %s",
597 pa_get_host_name(hn, sizeof(hn)),
598 pa_get_user_name(un, sizeof(un)),
599 u->sink->name);
600 #else
601 snprintf(name, sizeof(name), "Tunnel from host %s, user %s, source %s",
602 pa_get_host_name(hn, sizeof(hn)),
603 pa_get_user_name(un, sizeof(un)),
604 u->source->name);
605 #endif
606
607 reply = pa_tagstruct_new(NULL, 0);
608 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
609 pa_tagstruct_putu32(reply, tag = u->ctag++);
610 pa_tagstruct_puts(reply, name);
611 pa_pstream_send_tagstruct(u->pstream, reply);
612 /* We ignore the server's reply here */
613
614 reply = pa_tagstruct_new(NULL, 0);
615 #ifdef TUNNEL_SINK
616 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
617 pa_tagstruct_putu32(reply, tag = u->ctag++);
618 pa_tagstruct_puts(reply, name);
619 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
620 pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
621 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
622 pa_tagstruct_puts(reply, u->sink_name);
623 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
624 pa_tagstruct_put_boolean(reply, 0);
625 pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
626 pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
627 pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
628 pa_tagstruct_putu32(reply, 0);
629 pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
630 pa_tagstruct_put_cvolume(reply, &volume);
631 #else
632 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
633 pa_tagstruct_putu32(reply, tag = u->ctag++);
634 pa_tagstruct_puts(reply, name);
635 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
636 pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
637 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
638 pa_tagstruct_puts(reply, u->source_name);
639 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
640 pa_tagstruct_put_boolean(reply, 0);
641 pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
642 #endif
643
644 pa_pstream_send_tagstruct(u->pstream, reply);
645 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
646 }
647
648 static void pstream_die_callback(pa_pstream *p, void *userdata) {
649 struct userdata *u = userdata;
650 assert(p && u);
651
652 pa_log("stream died.");
653 die(u);
654 }
655
656 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
657 struct userdata *u = userdata;
658 assert(p && packet && u);
659
660 if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
661 pa_log("invalid packet");
662 die(u);
663 }
664 }
665
666 #ifndef TUNNEL_SINK
667 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, PA_GCC_UNUSED int64_t offset, PA_GCC_UNUSED pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
668 struct userdata *u = userdata;
669 assert(p && chunk && u);
670
671 if (channel != u->channel) {
672 pa_log("recieved memory block on bad channel.");
673 die(u);
674 return;
675 }
676
677 pa_source_post(u->source, chunk);
678 }
679 #endif
680
681 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
682 struct userdata *u = userdata;
683 pa_tagstruct *t;
684 uint32_t tag;
685 assert(sc && u && u->client == sc);
686
687 pa_socket_client_unref(u->client);
688 u->client = NULL;
689
690 if (!io) {
691 pa_log("connection failed.");
692 pa_module_unload_request(u->module);
693 return;
694 }
695
696 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
697 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
698
699 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
700 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
701 #ifndef TUNNEL_SINK
702 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
703 #endif
704
705 t = pa_tagstruct_new(NULL, 0);
706 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
707 pa_tagstruct_putu32(t, tag = u->ctag++);
708 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
709 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
710 pa_pstream_send_tagstruct(u->pstream, t);
711 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
712
713 }
714
715 #ifdef TUNNEL_SINK
716 static void sink_notify(pa_sink*sink) {
717 struct userdata *u;
718 assert(sink && sink->userdata);
719 u = sink->userdata;
720
721 send_bytes(u);
722 }
723
724 static pa_usec_t sink_get_latency(pa_sink *sink) {
725 struct userdata *u;
726 uint32_t l;
727 pa_usec_t usec = 0;
728 assert(sink && sink->userdata);
729 u = sink->userdata;
730
731 l = DEFAULT_TLENGTH;
732
733 if (l > u->requested_bytes) {
734 l -= u->requested_bytes;
735 usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
736 }
737
738 usec += u->host_latency;
739
740 return usec;
741 }
742
743 static int sink_get_hw_volume(pa_sink *sink) {
744 struct userdata *u;
745 assert(sink && sink->userdata);
746 u = sink->userdata;
747
748 return 0;
749 }
750
751 static int sink_set_hw_volume(pa_sink *sink) {
752 struct userdata *u;
753 pa_tagstruct *t;
754 uint32_t tag;
755 assert(sink && sink->userdata);
756 u = sink->userdata;
757
758 t = pa_tagstruct_new(NULL, 0);
759 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_VOLUME);
760 pa_tagstruct_putu32(t, tag = u->ctag++);
761
762 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
763 pa_tagstruct_puts(t, u->sink_name);
764 pa_tagstruct_put_cvolume(t, &sink->hw_volume);
765 pa_pstream_send_tagstruct(u->pstream, t);
766
767 return 0;
768 }
769
770 static int sink_get_hw_mute(pa_sink *sink) {
771 struct userdata *u;
772 assert(sink && sink->userdata);
773 u = sink->userdata;
774
775 return 0;
776 }
777
778 static int sink_set_hw_mute(pa_sink *sink) {
779 struct userdata *u;
780 pa_tagstruct *t;
781 uint32_t tag;
782 assert(sink && sink->userdata);
783 u = sink->userdata;
784
785 t = pa_tagstruct_new(NULL, 0);
786 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_MUTE);
787 pa_tagstruct_putu32(t, tag = u->ctag++);
788
789 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
790 pa_tagstruct_puts(t, u->sink_name);
791 pa_tagstruct_put_boolean(t, !!sink->hw_muted);
792 pa_pstream_send_tagstruct(u->pstream, t);
793
794 return 0;
795 }
796
797 #else
798 static pa_usec_t source_get_latency(pa_source *source) {
799 struct userdata *u;
800 assert(source && source->userdata);
801 u = source->userdata;
802
803 return u->host_latency;
804 }
805
806 static int source_get_hw_volume(pa_source *source) {
807 struct userdata *u;
808 assert(source && source->userdata);
809 u = source->userdata;
810
811 return 0;
812 }
813
814 static int source_set_hw_volume(pa_source *source) {
815 struct userdata *u;
816 pa_tagstruct *t;
817 uint32_t tag;
818 assert(source && source->userdata);
819 u = source->userdata;
820
821 t = pa_tagstruct_new(NULL, 0);
822 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_VOLUME);
823 pa_tagstruct_putu32(t, tag = u->ctag++);
824
825 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
826 pa_tagstruct_puts(t, u->source_name);
827 pa_tagstruct_put_cvolume(t, &source->hw_volume);
828 pa_pstream_send_tagstruct(u->pstream, t);
829
830 return 0;
831 }
832
833 static int source_get_hw_mute(pa_source *source) {
834 struct userdata *u;
835 assert(source && source->userdata);
836 u = source->userdata;
837
838 return 0;
839 }
840
841 static int source_set_hw_mute(pa_source *source) {
842 struct userdata *u;
843 pa_tagstruct *t;
844 uint32_t tag;
845 assert(source && source->userdata);
846 u = source->userdata;
847
848 t = pa_tagstruct_new(NULL, 0);
849 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_MUTE);
850 pa_tagstruct_putu32(t, tag = u->ctag++);
851
852 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
853 pa_tagstruct_puts(t, u->source_name);
854 pa_tagstruct_put_boolean(t, !!source->hw_muted);
855 pa_pstream_send_tagstruct(u->pstream, t);
856
857 return 0;
858 }
859 #endif
860
861 static int load_key(struct userdata *u, const char*fn) {
862 assert(u);
863
864 u->auth_cookie_in_property = 0;
865
866 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
867 pa_log_debug("using already loaded auth cookie.");
868 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
869 u->auth_cookie_in_property = 1;
870 return 0;
871 }
872
873 if (!fn)
874 fn = PA_NATIVE_COOKIE_FILE;
875
876 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
877 return -1;
878
879 pa_log_debug("loading cookie from disk.");
880
881 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
882 u->auth_cookie_in_property = 1;
883
884 return 0;
885 }
886
887 int pa__init(pa_core *c, pa_module*m) {
888 pa_modargs *ma = NULL;
889 struct userdata *u = NULL;
890 pa_sample_spec ss;
891 pa_channel_map map;
892 char *t, *dn = NULL;
893
894 assert(c && m);
895
896 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
897 pa_log("failed to parse module arguments");
898 goto fail;
899 }
900
901 u = pa_xmalloc(sizeof(struct userdata));
902 m->userdata = u;
903 u->module = m;
904 u->core = c;
905 u->client = NULL;
906 u->pdispatch = NULL;
907 u->pstream = NULL;
908 u->server_name = NULL;
909 #ifdef TUNNEL_SINK
910 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
911 u->sink = NULL;
912 u->requested_bytes = 0;
913 #else
914 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
915 u->source = NULL;
916 #endif
917 u->ctag = 1;
918 u->device_index = u->channel = PA_INVALID_INDEX;
919 u->host_latency = 0;
920 u->auth_cookie_in_property = 0;
921 u->time_event = NULL;
922
923 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
924 goto fail;
925
926 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
927 pa_log("no server specified.");
928 goto fail;
929 }
930
931 ss = c->default_sample_spec;
932 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
933 pa_log("invalid sample format specification");
934 goto fail;
935 }
936
937 if (!(u->client = pa_socket_client_new_string(c->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
938 pa_log("failed to connect to server '%s'", u->server_name);
939 goto fail;
940 }
941
942 if (!u->client)
943 goto fail;
944
945 pa_socket_client_set_callback(u->client, on_connection, u);
946
947 #ifdef TUNNEL_SINK
948
949 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
950 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
951
952 if (!(u->sink = pa_sink_new(c, __FILE__, dn, 0, &ss, &map))) {
953 pa_log("failed to create sink.");
954 goto fail;
955 }
956
957 u->sink->get_latency = sink_get_latency;
958 u->sink->get_hw_volume = sink_get_hw_volume;
959 u->sink->set_hw_volume = sink_set_hw_volume;
960 u->sink->get_hw_mute = sink_get_hw_mute;
961 u->sink->set_hw_mute = sink_set_hw_mute;
962 u->sink->notify = sink_notify;
963 u->sink->userdata = u;
964 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Tunnel to %s%s%s", u->sink_name ? u->sink_name : "", u->sink_name ? " on " : "", u->server_name));
965 pa_xfree(t);
966
967 pa_sink_set_owner(u->sink, m);
968 #else
969
970 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
971 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
972
973 if (!(u->source = pa_source_new(c, __FILE__, dn, 0, &ss, &map))) {
974 pa_log("failed to create source.");
975 goto fail;
976 }
977
978 u->source->get_latency = source_get_latency;
979 u->source->get_hw_volume = source_get_hw_volume;
980 u->source->set_hw_volume = source_set_hw_volume;
981 u->source->get_hw_mute = source_get_hw_mute;
982 u->source->set_hw_mute = source_set_hw_mute;
983 u->source->userdata = u;
984
985 pa_source_set_description(u->source, t = pa_sprintf_malloc("Tunnel to %s%s%s", u->source_name ? u->source_name : "", u->source_name ? " on " : "", u->server_name));
986 pa_xfree(t);
987
988 pa_source_set_owner(u->source, m);
989 #endif
990
991 pa_xfree(dn);
992
993 u->time_event = NULL;
994
995 pa_modargs_free(ma);
996
997 return 0;
998
999 fail:
1000 pa__done(c, m);
1001
1002 if (ma)
1003 pa_modargs_free(ma);
1004
1005 pa_xfree(dn);
1006
1007 return -1;
1008 }
1009
1010 void pa__done(pa_core *c, pa_module*m) {
1011 struct userdata* u;
1012 assert(c && m);
1013
1014 if (!(u = m->userdata))
1015 return;
1016
1017 close_stuff(u);
1018
1019 if (u->auth_cookie_in_property)
1020 pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
1021
1022 #ifdef TUNNEL_SINK
1023 pa_xfree(u->sink_name);
1024 #else
1025 pa_xfree(u->source_name);
1026 #endif
1027 pa_xfree(u->server_name);
1028
1029 pa_xfree(u);
1030 }
1031
1032