]> code.delx.au - pulseaudio/blob - src/modules/module-tunnel.c
bring back module-tunnel, yay!
[pulseaudio] / src / modules / module-tunnel.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38 #include <pulse/version.h>
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/pdispatch.h>
48 #include <pulsecore/pstream.h>
49 #include <pulsecore/pstream-util.h>
50 #include <pulsecore/authkey.h>
51 #include <pulsecore/socket-client.h>
52 #include <pulsecore/socket-util.h>
53 #include <pulsecore/authkey-prop.h>
54 #include <pulsecore/time-smoother.h>
55 #include <pulsecore/thread.h>
56 #include <pulsecore/thread-mq.h>
57 #include <pulsecore/rtclock.h>
58 #include <pulsecore/core-error.h>
59
60 #ifdef TUNNEL_SINK
61 #include "module-tunnel-sink-symdef.h"
62 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
63 PA_MODULE_USAGE(
64 "server=<address> "
65 "sink=<remote sink name> "
66 "cookie=<filename> "
67 "format=<sample format> "
68 "channels=<number of channels> "
69 "rate=<sample rate> "
70 "sink_name=<name for the local sink> "
71 "channel_map=<channel map>")
72 #else
73 #include "module-tunnel-source-symdef.h"
74 PA_MODULE_DESCRIPTION("Tunnel module for sources")
75 PA_MODULE_USAGE(
76 "server=<address> "
77 "source=<remote source name> "
78 "cookie=<filename> "
79 "format=<sample format> "
80 "channels=<number of channels> "
81 "rate=<sample rate> "
82 "source_name=<name for the local source> "
83 "channel_map=<channel map>")
84 #endif
85
86 PA_MODULE_AUTHOR("Lennart Poettering")
87 PA_MODULE_VERSION(PACKAGE_VERSION)
88
89 #define DEFAULT_TLENGTH_MSEC 100
90 #define DEFAULT_MINREQ_MSEC 10
91 #define DEFAULT_MAXLENGTH_MSEC ((DEFAULT_TLENGTH_MSEC*3)/2)
92 #define DEFAULT_FRAGSIZE_MSEC 10
93
94 #define DEFAULT_TIMEOUT 5
95
96 #define LATENCY_INTERVAL 10
97
98 static const char* const valid_modargs[] = {
99 "server",
100 "cookie",
101 "format",
102 "channels",
103 "rate",
104 #ifdef TUNNEL_SINK
105 "sink_name",
106 "sink",
107 #else
108 "source_name",
109 "source",
110 #endif
111 "channel_map",
112 NULL,
113 };
114
115 enum {
116 SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX
117 };
118
119 enum {
120 SINK_MESSAGE_REQUEST = PA_SINK_MESSAGE_MAX,
121 SINK_MESSAGE_POST
122 };
123
124 #ifdef TUNNEL_SINK
125 static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
126 static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
127 #endif
128 static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
129 static void command_overflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
130 static void command_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
131
132 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
133 #ifdef TUNNEL_SINK
134 [PA_COMMAND_REQUEST] = command_request,
135 [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
136 #endif
137 [PA_COMMAND_OVERFLOW] = command_overflow,
138 [PA_COMMAND_UNDERFLOW] = command_underflow,
139 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
140 [PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
141 };
142
143 struct userdata {
144 pa_core *core;
145 pa_module *module;
146
147 pa_thread_mq thread_mq;
148 pa_rtpoll *rtpoll;
149 pa_thread *thread;
150
151 pa_socket_client *client;
152 pa_pstream *pstream;
153 pa_pdispatch *pdispatch;
154
155 char *server_name;
156 #ifdef TUNNEL_SINK
157 char *sink_name;
158 pa_sink *sink;
159 uint32_t requested_bytes;
160 #else
161 char *source_name;
162 pa_source *source;
163 #endif
164
165 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
166
167 uint32_t version;
168 uint32_t ctag;
169 uint32_t device_index;
170 uint32_t channel;
171
172 int64_t counter, counter_delta;
173
174 pa_time_event *time_event;
175
176 pa_bool_t auth_cookie_in_property;
177
178 pa_smoother *smoother;
179
180 uint32_t maxlength;
181 #ifdef TUNNEL_SINK
182 uint32_t tlength;
183 uint32_t minreq;
184 uint32_t prebuf;
185 #else
186 uint32_t fragsize;
187 #endif
188 };
189
190 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) {
191 struct userdata *u = userdata;
192
193 pa_assert(pd);
194 pa_assert(t);
195 pa_assert(u);
196 pa_assert(u->pdispatch == pd);
197
198 pa_log_warn("Stream killed");
199 pa_module_unload_request(u->module);
200 }
201
202 static void command_overflow(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
205 pa_assert(pd);
206 pa_assert(t);
207 pa_assert(u);
208 pa_assert(u->pdispatch == pd);
209
210 pa_log_warn("Server signalled buffer overrun.");
211 }
212
213 static void command_underflow(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
214 struct userdata *u = userdata;
215
216 pa_assert(pd);
217 pa_assert(t);
218 pa_assert(u);
219 pa_assert(u->pdispatch == pd);
220
221 pa_log_warn("Server signalled buffer underrun.");
222 }
223
224 static void stream_cork(struct userdata *u, pa_bool_t cork) {
225 pa_tagstruct *t;
226 pa_assert(u);
227
228 if (cork)
229 pa_smoother_pause(u->smoother, pa_rtclock_usec());
230 else
231 pa_smoother_resume(u->smoother, pa_rtclock_usec());
232
233 t = pa_tagstruct_new(NULL, 0);
234 #ifdef TUNNEL_SINK
235 pa_tagstruct_putu32(t, PA_COMMAND_CORK_PLAYBACK_STREAM);
236 #else
237 pa_tagstruct_putu32(t, PA_COMMAND_CORK_RECORD_STREAM);
238 #endif
239 pa_tagstruct_putu32(t, u->ctag++);
240 pa_tagstruct_putu32(t, u->channel);
241 pa_tagstruct_put_boolean(t, !!cork);
242 pa_pstream_send_tagstruct(u->pstream, t);
243 }
244
245 #ifdef TUNNEL_SINK
246
247 static void send_data(struct userdata *u) {
248 pa_assert(u);
249
250 while (u->requested_bytes > 0) {
251 pa_memchunk memchunk;
252 pa_sink_render(u->sink, u->requested_bytes, &memchunk);
253 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_POST, NULL, 0, &memchunk, NULL);
254 pa_memblock_unref(memchunk.memblock);
255 u->requested_bytes -= memchunk.length;
256 }
257 }
258
259 /* This function is called from IO context -- except when it is not. */
260 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
261 struct userdata *u = PA_SINK(o)->userdata;
262
263 switch (code) {
264
265 case PA_SINK_MESSAGE_SET_STATE: {
266 int r;
267
268 /* First, change the state, because otherwide pa_sink_render() would fail */
269 if ((r = pa_sink_process_msg(o, code, data, offset, chunk)) >= 0)
270 if (PA_SINK_OPENED((pa_sink_state_t) PA_PTR_TO_UINT(data)))
271 send_data(u);
272
273 return r;
274 }
275
276 case SINK_MESSAGE_REQUEST:
277
278 pa_assert(offset > 0);
279 u->requested_bytes += (size_t) offset;
280
281 if (PA_SINK_OPENED(u->sink->thread_info.state))
282 send_data(u);
283
284 return 0;
285
286 case SINK_MESSAGE_POST:
287
288 /* OK, This might be a bit confusing. This message is
289 * delivered to us from the main context -- NOT from the
290 * IO thread context where the rest of the messages are
291 * dispatched. Yeah, ugly, but I am a lazy bastard. */
292
293 pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, chunk);
294 u->counter += chunk->length;
295 u->counter_delta += chunk->length;
296 return 0;
297 }
298
299 return pa_sink_process_msg(o, code, data, offset, chunk);
300 }
301
302 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
303 struct userdata *u;
304 pa_sink_assert_ref(s);
305 u = s->userdata;
306
307 switch ((pa_sink_state_t) state) {
308
309 case PA_SINK_SUSPENDED:
310 pa_assert(PA_SINK_OPENED(s->state));
311 stream_cork(u, TRUE);
312 break;
313
314 case PA_SINK_IDLE:
315 case PA_SINK_RUNNING:
316 if (s->state == PA_SINK_SUSPENDED)
317 stream_cork(u, FALSE);
318 break;
319
320 case PA_SINK_UNLINKED:
321 case PA_SINK_INIT:
322 ;
323 }
324
325 return 0;
326 }
327
328 #else
329
330 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
331 struct userdata *u = PA_SOURCE(o)->userdata;
332
333 switch (code) {
334 case SOURCE_MESSAGE_POST:
335
336 if (PA_SOURCE_OPENED(u->source->thread_info.state))
337 pa_source_post(u->source, chunk);
338 return 0;
339 }
340
341 return pa_source_process_msg(o, code, data, offset, chunk);
342 }
343
344 static int source_set_state(pa_source *s, pa_source_state_t state) {
345 struct userdata *u;
346 pa_source_assert_ref(s);
347 u = s->userdata;
348
349 switch ((pa_source_state_t) state) {
350
351 case PA_SOURCE_SUSPENDED:
352 pa_assert(PA_SOURCE_OPENED(s->state));
353 stream_cork(u, TRUE);
354 break;
355
356 case PA_SOURCE_IDLE:
357 case PA_SOURCE_RUNNING:
358 if (s->state == PA_SOURCE_SUSPENDED)
359 stream_cork(u, FALSE);
360 break;
361
362 case PA_SOURCE_UNLINKED:
363 case PA_SOURCE_INIT:
364 ;
365 }
366
367 return 0;
368 }
369
370 #endif
371
372 static void thread_func(void *userdata) {
373 struct userdata *u = userdata;
374
375 pa_assert(u);
376
377 pa_log_debug("Thread starting up");
378
379 pa_thread_mq_install(&u->thread_mq);
380 pa_rtpoll_install(u->rtpoll);
381
382 for (;;) {
383 int ret;
384
385 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
386 goto fail;
387
388 if (ret == 0)
389 goto finish;
390 }
391
392 fail:
393 /* If this was no regular exit from the loop we have to continue
394 * processing messages until we received PA_MESSAGE_SHUTDOWN */
395 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
396 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
397
398 finish:
399 pa_log_debug("Thread shutting down");
400 }
401
402 #ifdef TUNNEL_SINK
403 static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
404 struct userdata *u = userdata;
405 uint32_t bytes, channel;
406
407 pa_assert(pd);
408 pa_assert(command == PA_COMMAND_REQUEST);
409 pa_assert(t);
410 pa_assert(u);
411 pa_assert(u->pdispatch == pd);
412
413 if (pa_tagstruct_getu32(t, &channel) < 0 ||
414 pa_tagstruct_getu32(t, &bytes) < 0 ||
415 !pa_tagstruct_eof(t)) {
416 pa_log("Invalid protocol reply");
417 goto fail;
418 }
419
420 if (channel != u->channel) {
421 pa_log("Recieved data for invalid channel");
422 goto fail;
423 }
424
425 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL);
426 return;
427
428 fail:
429 pa_module_unload_request(u->module);
430 }
431
432 #endif
433
434 static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
435 struct userdata *u = userdata;
436 pa_usec_t sink_usec, source_usec, transport_usec, host_usec, k;
437 int playing;
438 int64_t write_index, read_index;
439 struct timeval local, remote, now;
440
441 pa_assert(pd);
442 pa_assert(u);
443
444 if (command != PA_COMMAND_REPLY) {
445 if (command == PA_COMMAND_ERROR)
446 pa_log("Failed to get latency.");
447 else
448 pa_log("Protocol error.");
449 goto fail;
450 }
451
452 if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
453 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
454 pa_tagstruct_get_boolean(t, &playing) < 0 ||
455 pa_tagstruct_get_timeval(t, &local) < 0 ||
456 pa_tagstruct_get_timeval(t, &remote) < 0 ||
457 pa_tagstruct_gets64(t, &write_index) < 0 ||
458 pa_tagstruct_gets64(t, &read_index) < 0 ||
459 !pa_tagstruct_eof(t)) {
460 pa_log("Invalid reply. (latency)");
461 goto fail;
462 }
463
464 pa_gettimeofday(&now);
465
466 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
467 /* local and remote seem to have synchronized clocks */
468 #ifdef TUNNEL_SINK
469 transport_usec = pa_timeval_diff(&remote, &local);
470 #else
471 transport_usec = pa_timeval_diff(&now, &remote);
472 #endif
473 } else
474 transport_usec = pa_timeval_diff(&now, &local)/2;
475
476 #ifdef TUNNEL_SINK
477 host_usec = sink_usec + transport_usec;
478 #else
479 host_usec = source_usec + transport_usec;
480 if (host_usec > sink_usec)
481 host_usec -= sink_usec;
482 else
483 host_usec = 0;
484 #endif
485
486 #ifdef TUNNEL_SINK
487 k = pa_bytes_to_usec(u->counter - u->counter_delta, &u->sink->sample_spec);
488
489 if (k > host_usec)
490 k -= host_usec;
491 else
492 k = 0;
493 #else
494 k = pa_bytes_to_usec(u->counter - u->counter_delta, &u->source->sample_spec);
495 k += host_usec;
496 #endif
497
498 pa_smoother_put(u->smoother, pa_rtclock_usec(), k);
499
500 return;
501
502 fail:
503 pa_module_unload_request(u->module);
504 }
505
506 static void request_latency(struct userdata *u) {
507 pa_tagstruct *t;
508 struct timeval now;
509 uint32_t tag;
510 pa_assert(u);
511
512 t = pa_tagstruct_new(NULL, 0);
513 #ifdef TUNNEL_SINK
514 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
515 #else
516 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
517 #endif
518 pa_tagstruct_putu32(t, tag = u->ctag++);
519 pa_tagstruct_putu32(t, u->channel);
520
521 pa_gettimeofday(&now);
522 pa_tagstruct_put_timeval(t, &now);
523
524 pa_pstream_send_tagstruct(u->pstream, t);
525 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
526
527 u->counter_delta = 0;
528 }
529
530 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
531 struct userdata *u = userdata;
532 struct timeval ntv;
533
534 pa_assert(m);
535 pa_assert(e);
536 pa_assert(u);
537
538 request_latency(u);
539
540 pa_gettimeofday(&ntv);
541 ntv.tv_sec += LATENCY_INTERVAL;
542 m->time_restart(e, &ntv);
543 }
544
545 #ifdef TUNNEL_SINK
546 static pa_usec_t sink_get_latency(pa_sink *s) {
547 pa_usec_t t, c;
548 struct userdata *u = s->userdata;
549
550 pa_sink_assert_ref(s);
551
552 c = pa_bytes_to_usec(u->counter, &s->sample_spec);
553 t = pa_smoother_get(u->smoother, pa_rtclock_usec());
554
555 return c > t ? c - t : 0;
556 }
557 #else
558 static pa_usec_t source_get_latency(pa_source *s) {
559 pa_usec_t t, c;
560 struct userdata *u = s->userdata;
561
562 pa_source_assert_ref(s);
563
564 c = pa_bytes_to_usec(u->counter, &s->sample_spec);
565 t = pa_smoother_get(u->smoother, pa_rtclock_usec());
566
567 return t > c ? t - c : 0;
568 }
569 #endif
570
571 #ifdef TUNNEL_SINK
572
573 static void sink_input_info_cb(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
574 struct userdata *u = userdata;
575 uint32_t idx, owner_module, client, sink;
576 pa_usec_t buffer_usec, sink_usec;
577 const char *name, *driver, *resample_method;
578 int mute;
579 pa_sample_spec sample_spec;
580 pa_channel_map channel_map;
581 pa_cvolume volume;
582
583 pa_assert(pd);
584 pa_assert(u);
585
586 if (command != PA_COMMAND_REPLY) {
587 if (command == PA_COMMAND_ERROR)
588 pa_log("Failed to get info.");
589 else
590 pa_log("Protocol error.");
591 goto fail;
592 }
593
594 if (pa_tagstruct_getu32(t, &idx) < 0 ||
595 pa_tagstruct_gets(t, &name) < 0 ||
596 pa_tagstruct_getu32(t, &owner_module) < 0 ||
597 pa_tagstruct_getu32(t, &client) < 0 ||
598 pa_tagstruct_getu32(t, &sink) < 0 ||
599 pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
600 pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
601 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
602 pa_tagstruct_get_usec(t, &buffer_usec) < 0 ||
603 pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
604 pa_tagstruct_gets(t, &resample_method) < 0 ||
605 pa_tagstruct_gets(t, &driver) < 0 ||
606 (u->version >= 11 && pa_tagstruct_get_boolean(t, &mute) < 0) ||
607 !pa_tagstruct_eof(t)) {
608 pa_log("Invalid reply. (get_info)");
609 goto fail;
610 }
611
612 pa_assert(u->sink);
613
614 if ((u->version < 11 || !!mute == !!u->sink->muted) &&
615 pa_cvolume_equal(&volume, &u->sink->volume))
616 return;
617
618 memcpy(&u->sink->volume, &volume, sizeof(pa_cvolume));
619
620 if (u->version >= 11)
621 u->sink->muted = !!mute;
622
623 pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index);
624 return;
625
626 fail:
627 pa_module_unload_request(u->module);
628 }
629
630 static void request_info(struct userdata *u) {
631 pa_tagstruct *t;
632 uint32_t tag;
633 pa_assert(u);
634
635 t = pa_tagstruct_new(NULL, 0);
636 pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INPUT_INFO);
637 pa_tagstruct_putu32(t, tag = u->ctag++);
638 pa_tagstruct_putu32(t, u->device_index);
639 pa_pstream_send_tagstruct(u->pstream, t);
640 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, sink_input_info_cb, u, NULL);
641 }
642
643 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) {
644 struct userdata *u = userdata;
645 pa_subscription_event_type_t e;
646 uint32_t idx;
647
648 pa_assert(pd);
649 pa_assert(t);
650 pa_assert(u);
651 pa_assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
652
653 if (pa_tagstruct_getu32(t, &e) < 0 ||
654 pa_tagstruct_getu32(t, &idx) < 0 ||
655 !pa_tagstruct_eof(t)) {
656 pa_log("Invalid protocol reply");
657 pa_module_unload_request(u->module);
658 return;
659 }
660
661 if (e != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
662 return;
663
664 request_info(u);
665 }
666
667 static void start_subscribe(struct userdata *u) {
668 pa_tagstruct *t;
669 uint32_t tag;
670 pa_assert(u);
671
672 t = pa_tagstruct_new(NULL, 0);
673 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
674 pa_tagstruct_putu32(t, tag = u->ctag++);
675 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK_INPUT);
676 pa_pstream_send_tagstruct(u->pstream, t);
677 }
678 #endif
679
680 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
681 struct userdata *u = userdata;
682 struct timeval ntv;
683 #ifdef TUNNEL_SINK
684 uint32_t bytes;
685 #endif
686
687 pa_assert(pd);
688 pa_assert(u);
689 pa_assert(u->pdispatch == pd);
690
691 if (command != PA_COMMAND_REPLY) {
692 if (command == PA_COMMAND_ERROR)
693 pa_log("Failed to create stream.");
694 else
695 pa_log("Protocol error.");
696 goto fail;
697 }
698
699 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
700 pa_tagstruct_getu32(t, &u->device_index) < 0
701 #ifdef TUNNEL_SINK
702 || pa_tagstruct_getu32(t, &bytes) < 0
703 #endif
704 )
705 goto parse_error;
706
707 if (u->version >= 9) {
708 #ifdef TUNNEL_SINK
709 uint32_t maxlength, tlength, prebuf, minreq;
710
711 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
712 pa_tagstruct_getu32(t, &tlength) < 0 ||
713 pa_tagstruct_getu32(t, &prebuf) < 0 ||
714 pa_tagstruct_getu32(t, &minreq) < 0)
715 goto parse_error;
716 #else
717 uint32_t maxlength, fragsize;
718
719 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
720 pa_tagstruct_getu32(t, &fragsize) < 0)
721 goto parse_error;
722 #endif
723 }
724
725 if (!pa_tagstruct_eof(t))
726 goto parse_error;
727
728 #ifdef TUNNEL_SINK
729 start_subscribe(u);
730 request_info(u);
731 #endif
732
733 pa_assert(!u->time_event);
734 pa_gettimeofday(&ntv);
735 ntv.tv_sec += LATENCY_INTERVAL;
736 u->time_event = u->core->mainloop->time_new(u->core->mainloop, &ntv, timeout_callback, u);
737
738 request_latency(u);
739
740 pa_log_debug("Stream created.");
741
742 #ifdef TUNNEL_SINK
743 pa_asyncmsgq_post(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL, NULL);
744 #endif
745
746 return;
747
748 parse_error:
749 pa_log("Invalid reply. (Create stream)");
750
751 fail:
752 pa_module_unload_request(u->module);
753 }
754
755 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
756 struct userdata *u = userdata;
757 pa_tagstruct *reply;
758 char name[256], un[128], hn[128];
759 #ifdef TUNNEL_SINK
760 pa_cvolume volume;
761 #endif
762
763 pa_assert(pd);
764 pa_assert(u);
765 pa_assert(u->pdispatch == pd);
766
767 if (command != PA_COMMAND_REPLY ||
768 pa_tagstruct_getu32(t, &u->version) < 0 ||
769 !pa_tagstruct_eof(t)) {
770 if (command == PA_COMMAND_ERROR)
771 pa_log("Failed to authenticate");
772 else
773 pa_log("Protocol error.");
774
775 goto fail;
776 }
777
778 /* Minimum supported protocol version */
779 if (u->version < 8) {
780 pa_log("Incompatible protocol version");
781 goto fail;
782 }
783
784 #ifdef TUNNEL_SINK
785 pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, sink %s",
786 pa_get_host_name(hn, sizeof(hn)),
787 pa_get_user_name(un, sizeof(un)),
788 u->sink->name);
789 #else
790 pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, source %s",
791 pa_get_host_name(hn, sizeof(hn)),
792 pa_get_user_name(un, sizeof(un)),
793 u->source->name);
794 #endif
795
796 reply = pa_tagstruct_new(NULL, 0);
797 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
798 pa_tagstruct_putu32(reply, tag = u->ctag++);
799 pa_tagstruct_puts(reply, name);
800 pa_pstream_send_tagstruct(u->pstream, reply);
801 /* We ignore the server's reply here */
802
803 reply = pa_tagstruct_new(NULL, 0);
804
805 #ifdef TUNNEL_SINK
806 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
807 pa_tagstruct_putu32(reply, tag = u->ctag++);
808 pa_tagstruct_puts(reply, name);
809 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
810 pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
811 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
812 pa_tagstruct_puts(reply, u->sink_name);
813 pa_tagstruct_putu32(reply, u->maxlength);
814 pa_tagstruct_put_boolean(reply, FALSE);
815 pa_tagstruct_putu32(reply, u->tlength);
816 pa_tagstruct_putu32(reply, u->prebuf);
817 pa_tagstruct_putu32(reply, u->minreq);
818 pa_tagstruct_putu32(reply, 0);
819 pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
820 pa_tagstruct_put_cvolume(reply, &volume);
821 #else
822 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
823 pa_tagstruct_putu32(reply, tag = u->ctag++);
824 pa_tagstruct_puts(reply, name);
825 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
826 pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
827 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
828 pa_tagstruct_puts(reply, u->source_name);
829 pa_tagstruct_putu32(reply, u->maxlength);
830 pa_tagstruct_put_boolean(reply, 0);
831 pa_tagstruct_putu32(reply, u->fragsize);
832 #endif
833
834 pa_pstream_send_tagstruct(u->pstream, reply);
835 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
836
837 pa_log_debug("Connection authenticated, creating stream ...");
838
839 return;
840
841 fail:
842 pa_module_unload_request(u->module);
843 }
844
845 static void pstream_die_callback(pa_pstream *p, void *userdata) {
846 struct userdata *u = userdata;
847
848 pa_assert(p);
849 pa_assert(u);
850
851 pa_log_warn("Stream died.");
852 pa_module_unload_request(u->module);
853 }
854
855 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
856 struct userdata *u = userdata;
857
858 pa_assert(p);
859 pa_assert(packet);
860 pa_assert(u);
861
862 if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
863 pa_log("Invalid packet");
864 pa_module_unload_request(u->module);
865 return;
866 }
867 }
868
869 #ifndef TUNNEL_SINK
870 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t offset, pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
871 struct userdata *u = userdata;
872
873 pa_assert(p);
874 pa_assert(chunk);
875 pa_assert(u);
876
877 if (channel != u->channel) {
878 pa_log("Recieved memory block on bad channel.");
879 pa_module_unload_request(u->module);
880 return;
881 }
882
883 pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, PA_UINT_TO_PTR(seek), offset, chunk);
884
885 u->counter += chunk->length;
886 u->counter_delta += chunk->length;
887 }
888
889 #endif
890
891 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
892 struct userdata *u = userdata;
893 pa_tagstruct *t;
894 uint32_t tag;
895
896 pa_assert(sc);
897 pa_assert(u);
898 pa_assert(u->client == sc);
899
900 pa_socket_client_unref(u->client);
901 u->client = NULL;
902
903 if (!io) {
904 pa_log("Connection failed: %s", pa_cstrerror(errno));
905 pa_module_unload_request(u->module);
906 return;
907 }
908
909 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
910 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
911
912 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
913 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
914 #ifndef TUNNEL_SINK
915 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
916 #endif
917
918 t = pa_tagstruct_new(NULL, 0);
919 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
920 pa_tagstruct_putu32(t, tag = u->ctag++);
921 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
922 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
923
924 #ifdef HAVE_CREDS
925 {
926 pa_creds ucred;
927
928 if (pa_iochannel_creds_supported(io))
929 pa_iochannel_creds_enable(io);
930
931 ucred.uid = getuid();
932 ucred.gid = getgid();
933
934 pa_pstream_send_tagstruct_with_creds(u->pstream, t, &ucred);
935 }
936 #else
937 pa_pstream_send_tagstruct(u->pstream, t);
938 #endif
939
940 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
941
942 pa_log_debug("Connection established, authenticating ...");
943 }
944
945 #ifdef TUNNEL_SINK
946
947 static int sink_get_volume(pa_sink *sink) {
948 return 0;
949 }
950
951 static int sink_set_volume(pa_sink *sink) {
952 struct userdata *u;
953 pa_tagstruct *t;
954 uint32_t tag;
955
956 pa_assert(sink);
957 u = sink->userdata;
958 pa_assert(u);
959
960 t = pa_tagstruct_new(NULL, 0);
961 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_VOLUME);
962 pa_tagstruct_putu32(t, tag = u->ctag++);
963 pa_tagstruct_putu32(t, u->device_index);
964 pa_tagstruct_put_cvolume(t, &sink->volume);
965 pa_pstream_send_tagstruct(u->pstream, t);
966
967 return 0;
968 }
969
970 static int sink_get_mute(pa_sink *sink) {
971 return 0;
972 }
973
974 static int sink_set_mute(pa_sink *sink) {
975 struct userdata *u;
976 pa_tagstruct *t;
977 uint32_t tag;
978
979 pa_assert(sink);
980 u = sink->userdata;
981 pa_assert(u);
982
983 if (u->version < 11)
984 return -1;
985
986 t = pa_tagstruct_new(NULL, 0);
987 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_MUTE);
988 pa_tagstruct_putu32(t, tag = u->ctag++);
989 pa_tagstruct_putu32(t, u->device_index);
990 pa_tagstruct_put_boolean(t, !!sink->muted);
991 pa_pstream_send_tagstruct(u->pstream, t);
992
993 return 0;
994 }
995
996 #endif
997
998 static int load_key(struct userdata *u, const char*fn) {
999 pa_assert(u);
1000
1001 u->auth_cookie_in_property = FALSE;
1002
1003 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
1004 pa_log_debug("using already loaded auth cookie.");
1005 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
1006 u->auth_cookie_in_property = 1;
1007 return 0;
1008 }
1009
1010 if (!fn)
1011 fn = PA_NATIVE_COOKIE_FILE;
1012
1013 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
1014 return -1;
1015
1016 pa_log_debug("loading cookie from disk.");
1017
1018 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
1019 u->auth_cookie_in_property = TRUE;
1020
1021 return 0;
1022 }
1023
1024 int pa__init(pa_module*m) {
1025 pa_modargs *ma = NULL;
1026 struct userdata *u = NULL;
1027 pa_sample_spec ss;
1028 pa_channel_map map;
1029 char *t, *dn = NULL;
1030
1031 pa_assert(m);
1032
1033 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1034 pa_log("failed to parse module arguments");
1035 goto fail;
1036 }
1037
1038 u = pa_xnew(struct userdata, 1);
1039 m->userdata = u;
1040 u->module = m;
1041 u->core = m->core;
1042 u->client = NULL;
1043 u->pdispatch = NULL;
1044 u->pstream = NULL;
1045 u->server_name = NULL;
1046 #ifdef TUNNEL_SINK
1047 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
1048 u->sink = NULL;
1049 u->requested_bytes = 0;
1050 #else
1051 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
1052 u->source = NULL;
1053 #endif
1054 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE);
1055 u->ctag = 1;
1056 u->device_index = u->channel = PA_INVALID_INDEX;
1057 u->auth_cookie_in_property = FALSE;
1058 u->time_event = NULL;
1059
1060 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1061 u->rtpoll = pa_rtpoll_new();
1062 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
1063
1064 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
1065 goto fail;
1066
1067 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
1068 pa_log("no server specified.");
1069 goto fail;
1070 }
1071
1072 ss = m->core->default_sample_spec;
1073 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
1074 pa_log("invalid sample format specification");
1075 goto fail;
1076 }
1077
1078 if (!(u->client = pa_socket_client_new_string(m->core->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
1079 pa_log("failed to connect to server '%s'", u->server_name);
1080 goto fail;
1081 }
1082
1083 pa_socket_client_set_callback(u->client, on_connection, u);
1084
1085 #ifdef TUNNEL_SINK
1086
1087 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
1088 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
1089
1090 if (!(u->sink = pa_sink_new(m->core, __FILE__, dn, 0, &ss, &map))) {
1091 pa_log("Failed to create sink.");
1092 goto fail;
1093 }
1094
1095 u->sink->parent.process_msg = sink_process_msg;
1096 u->sink->userdata = u;
1097 u->sink->set_state = sink_set_state;
1098 u->sink->get_latency = sink_get_latency;
1099 u->sink->get_volume = sink_get_volume;
1100 u->sink->get_mute = sink_get_mute;
1101 u->sink->set_volume = sink_set_volume;
1102 u->sink->set_mute = sink_set_mute;
1103
1104 pa_sink_set_module(u->sink, m);
1105 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1106 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1107 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));
1108 pa_xfree(t);
1109
1110 #else
1111
1112 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
1113 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
1114
1115 if (!(u->source = pa_source_new(m->core, __FILE__, dn, 0, &ss, &map))) {
1116 pa_log("Failed to create source.");
1117 goto fail;
1118 }
1119
1120 u->source->parent.process_msg = source_process_msg;
1121 u->source->userdata = u;
1122 u->source->set_state = source_set_state;
1123 u->source->get_latency = source_get_latency;
1124
1125 pa_source_set_module(u->source, m);
1126 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1127 pa_source_set_rtpoll(u->source, u->rtpoll);
1128 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));
1129 pa_xfree(t);
1130 #endif
1131
1132 pa_xfree(dn);
1133
1134 u->time_event = NULL;
1135
1136 u->maxlength = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_MAXLENGTH_MSEC, &ss);
1137 #ifdef TUNNEL_SINK
1138 u->tlength = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_TLENGTH_MSEC, &ss);
1139 u->minreq = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_MINREQ_MSEC, &ss);
1140 u->prebuf = u->tlength;
1141 #else
1142 u->fragsize = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_FRAGSIZE_MSEC, &ss);
1143 #endif
1144
1145 u->counter = u->counter_delta = 0;
1146 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
1147
1148 if (!(u->thread = pa_thread_new(thread_func, u))) {
1149 pa_log("Failed to create thread.");
1150 goto fail;
1151 }
1152
1153 #ifdef TUNNEL_SINK
1154 pa_sink_put(u->sink);
1155 #else
1156 pa_source_put(u->source);
1157 #endif
1158
1159 pa_modargs_free(ma);
1160
1161 return 0;
1162
1163 fail:
1164 pa__done(m);
1165
1166 if (ma)
1167 pa_modargs_free(ma);
1168
1169 pa_xfree(dn);
1170
1171 return -1;
1172 }
1173
1174 void pa__done(pa_module*m) {
1175 struct userdata* u;
1176
1177 pa_assert(m);
1178
1179 if (!(u = m->userdata))
1180 return;
1181
1182 #ifdef TUNNEL_SINK
1183 if (u->sink)
1184 pa_sink_unlink(u->sink);
1185 #else
1186 if (u->source)
1187 pa_source_unlink(u->source);
1188 #endif
1189
1190 if (u->thread) {
1191 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1192 pa_thread_free(u->thread);
1193 }
1194
1195 pa_thread_mq_done(&u->thread_mq);
1196
1197 #ifdef TUNNEL_SINK
1198 if (u->sink)
1199 pa_sink_unref(u->sink);
1200 #else
1201 if (u->source)
1202 pa_source_unref(u->source);
1203 #endif
1204
1205 if (u->rtpoll)
1206 pa_rtpoll_free(u->rtpoll);
1207
1208 if (u->pstream) {
1209 pa_pstream_unlink(u->pstream);
1210 pa_pstream_unref(u->pstream);
1211 }
1212
1213 if (u->pdispatch)
1214 pa_pdispatch_unref(u->pdispatch);
1215
1216 if (u->client)
1217 pa_socket_client_unref(u->client);
1218
1219 if (u->auth_cookie_in_property)
1220 pa_authkey_prop_unref(m->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
1221
1222 if (u->smoother)
1223 pa_smoother_free(u->smoother);
1224
1225 if (u->time_event)
1226 u->core->mainloop->time_free(u->time_event);
1227
1228 #ifdef TUNNEL_SINK
1229 pa_xfree(u->sink_name);
1230 #else
1231 pa_xfree(u->source_name);
1232 #endif
1233 pa_xfree(u->server_name);
1234
1235 pa_xfree(u);
1236 }