]> code.delx.au - pulseaudio/blob - src/modules/module-tunnel.c
don't try to send pause request before our stream is properly set up
[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 if (!u->pstream)
234 return;
235
236 t = pa_tagstruct_new(NULL, 0);
237 #ifdef TUNNEL_SINK
238 pa_tagstruct_putu32(t, PA_COMMAND_CORK_PLAYBACK_STREAM);
239 #else
240 pa_tagstruct_putu32(t, PA_COMMAND_CORK_RECORD_STREAM);
241 #endif
242 pa_tagstruct_putu32(t, u->ctag++);
243 pa_tagstruct_putu32(t, u->channel);
244 pa_tagstruct_put_boolean(t, !!cork);
245 pa_pstream_send_tagstruct(u->pstream, t);
246 }
247
248 #ifdef TUNNEL_SINK
249
250 static void send_data(struct userdata *u) {
251 pa_assert(u);
252
253 while (u->requested_bytes > 0) {
254 pa_memchunk memchunk;
255 pa_sink_render(u->sink, u->requested_bytes, &memchunk);
256 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_POST, NULL, 0, &memchunk, NULL);
257 pa_memblock_unref(memchunk.memblock);
258 u->requested_bytes -= memchunk.length;
259 }
260 }
261
262 /* This function is called from IO context -- except when it is not. */
263 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
264 struct userdata *u = PA_SINK(o)->userdata;
265
266 switch (code) {
267
268 case PA_SINK_MESSAGE_SET_STATE: {
269 int r;
270
271 /* First, change the state, because otherwide pa_sink_render() would fail */
272 if ((r = pa_sink_process_msg(o, code, data, offset, chunk)) >= 0)
273 if (PA_SINK_OPENED((pa_sink_state_t) PA_PTR_TO_UINT(data)))
274 send_data(u);
275
276 return r;
277 }
278
279 case SINK_MESSAGE_REQUEST:
280
281 pa_assert(offset > 0);
282 u->requested_bytes += (size_t) offset;
283
284 if (PA_SINK_OPENED(u->sink->thread_info.state))
285 send_data(u);
286
287 return 0;
288
289 case SINK_MESSAGE_POST:
290
291 /* OK, This might be a bit confusing. This message is
292 * delivered to us from the main context -- NOT from the
293 * IO thread context where the rest of the messages are
294 * dispatched. Yeah, ugly, but I am a lazy bastard. */
295
296 pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, chunk);
297 u->counter += chunk->length;
298 u->counter_delta += chunk->length;
299 return 0;
300 }
301
302 return pa_sink_process_msg(o, code, data, offset, chunk);
303 }
304
305 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
306 struct userdata *u;
307 pa_sink_assert_ref(s);
308 u = s->userdata;
309
310 switch ((pa_sink_state_t) state) {
311
312 case PA_SINK_SUSPENDED:
313 pa_assert(PA_SINK_OPENED(s->state));
314 stream_cork(u, TRUE);
315 break;
316
317 case PA_SINK_IDLE:
318 case PA_SINK_RUNNING:
319 if (s->state == PA_SINK_SUSPENDED)
320 stream_cork(u, FALSE);
321 break;
322
323 case PA_SINK_UNLINKED:
324 case PA_SINK_INIT:
325 ;
326 }
327
328 return 0;
329 }
330
331 #else
332
333 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
334 struct userdata *u = PA_SOURCE(o)->userdata;
335
336 switch (code) {
337 case SOURCE_MESSAGE_POST:
338
339 if (PA_SOURCE_OPENED(u->source->thread_info.state))
340 pa_source_post(u->source, chunk);
341 return 0;
342 }
343
344 return pa_source_process_msg(o, code, data, offset, chunk);
345 }
346
347 static int source_set_state(pa_source *s, pa_source_state_t state) {
348 struct userdata *u;
349 pa_source_assert_ref(s);
350 u = s->userdata;
351
352 switch ((pa_source_state_t) state) {
353
354 case PA_SOURCE_SUSPENDED:
355 pa_assert(PA_SOURCE_OPENED(s->state));
356 stream_cork(u, TRUE);
357 break;
358
359 case PA_SOURCE_IDLE:
360 case PA_SOURCE_RUNNING:
361 if (s->state == PA_SOURCE_SUSPENDED)
362 stream_cork(u, FALSE);
363 break;
364
365 case PA_SOURCE_UNLINKED:
366 case PA_SOURCE_INIT:
367 ;
368 }
369
370 return 0;
371 }
372
373 #endif
374
375 static void thread_func(void *userdata) {
376 struct userdata *u = userdata;
377
378 pa_assert(u);
379
380 pa_log_debug("Thread starting up");
381
382 pa_thread_mq_install(&u->thread_mq);
383 pa_rtpoll_install(u->rtpoll);
384
385 for (;;) {
386 int ret;
387
388 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
389 goto fail;
390
391 if (ret == 0)
392 goto finish;
393 }
394
395 fail:
396 /* If this was no regular exit from the loop we have to continue
397 * processing messages until we received PA_MESSAGE_SHUTDOWN */
398 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
399 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
400
401 finish:
402 pa_log_debug("Thread shutting down");
403 }
404
405 #ifdef TUNNEL_SINK
406 static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
407 struct userdata *u = userdata;
408 uint32_t bytes, channel;
409
410 pa_assert(pd);
411 pa_assert(command == PA_COMMAND_REQUEST);
412 pa_assert(t);
413 pa_assert(u);
414 pa_assert(u->pdispatch == pd);
415
416 if (pa_tagstruct_getu32(t, &channel) < 0 ||
417 pa_tagstruct_getu32(t, &bytes) < 0 ||
418 !pa_tagstruct_eof(t)) {
419 pa_log("Invalid protocol reply");
420 goto fail;
421 }
422
423 if (channel != u->channel) {
424 pa_log("Recieved data for invalid channel");
425 goto fail;
426 }
427
428 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL);
429 return;
430
431 fail:
432 pa_module_unload_request(u->module);
433 }
434
435 #endif
436
437 static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
438 struct userdata *u = userdata;
439 pa_usec_t sink_usec, source_usec, transport_usec, host_usec, k;
440 int playing;
441 int64_t write_index, read_index;
442 struct timeval local, remote, now;
443
444 pa_assert(pd);
445 pa_assert(u);
446
447 if (command != PA_COMMAND_REPLY) {
448 if (command == PA_COMMAND_ERROR)
449 pa_log("Failed to get latency.");
450 else
451 pa_log("Protocol error.");
452 goto fail;
453 }
454
455 if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
456 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
457 pa_tagstruct_get_boolean(t, &playing) < 0 ||
458 pa_tagstruct_get_timeval(t, &local) < 0 ||
459 pa_tagstruct_get_timeval(t, &remote) < 0 ||
460 pa_tagstruct_gets64(t, &write_index) < 0 ||
461 pa_tagstruct_gets64(t, &read_index) < 0 ||
462 !pa_tagstruct_eof(t)) {
463 pa_log("Invalid reply. (latency)");
464 goto fail;
465 }
466
467 pa_gettimeofday(&now);
468
469 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
470 /* local and remote seem to have synchronized clocks */
471 #ifdef TUNNEL_SINK
472 transport_usec = pa_timeval_diff(&remote, &local);
473 #else
474 transport_usec = pa_timeval_diff(&now, &remote);
475 #endif
476 } else
477 transport_usec = pa_timeval_diff(&now, &local)/2;
478
479 #ifdef TUNNEL_SINK
480 host_usec = sink_usec + transport_usec;
481 #else
482 host_usec = source_usec + transport_usec;
483 if (host_usec > sink_usec)
484 host_usec -= sink_usec;
485 else
486 host_usec = 0;
487 #endif
488
489 #ifdef TUNNEL_SINK
490 k = pa_bytes_to_usec(u->counter - u->counter_delta, &u->sink->sample_spec);
491
492 if (k > host_usec)
493 k -= host_usec;
494 else
495 k = 0;
496 #else
497 k = pa_bytes_to_usec(u->counter - u->counter_delta, &u->source->sample_spec);
498 k += host_usec;
499 #endif
500
501 pa_smoother_put(u->smoother, pa_rtclock_usec(), k);
502
503 return;
504
505 fail:
506 pa_module_unload_request(u->module);
507 }
508
509 static void request_latency(struct userdata *u) {
510 pa_tagstruct *t;
511 struct timeval now;
512 uint32_t tag;
513 pa_assert(u);
514
515 t = pa_tagstruct_new(NULL, 0);
516 #ifdef TUNNEL_SINK
517 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
518 #else
519 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
520 #endif
521 pa_tagstruct_putu32(t, tag = u->ctag++);
522 pa_tagstruct_putu32(t, u->channel);
523
524 pa_gettimeofday(&now);
525 pa_tagstruct_put_timeval(t, &now);
526
527 pa_pstream_send_tagstruct(u->pstream, t);
528 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
529
530 u->counter_delta = 0;
531 }
532
533 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
534 struct userdata *u = userdata;
535 struct timeval ntv;
536
537 pa_assert(m);
538 pa_assert(e);
539 pa_assert(u);
540
541 request_latency(u);
542
543 pa_gettimeofday(&ntv);
544 ntv.tv_sec += LATENCY_INTERVAL;
545 m->time_restart(e, &ntv);
546 }
547
548 #ifdef TUNNEL_SINK
549 static pa_usec_t sink_get_latency(pa_sink *s) {
550 pa_usec_t t, c;
551 struct userdata *u = s->userdata;
552
553 pa_sink_assert_ref(s);
554
555 c = pa_bytes_to_usec(u->counter, &s->sample_spec);
556 t = pa_smoother_get(u->smoother, pa_rtclock_usec());
557
558 return c > t ? c - t : 0;
559 }
560 #else
561 static pa_usec_t source_get_latency(pa_source *s) {
562 pa_usec_t t, c;
563 struct userdata *u = s->userdata;
564
565 pa_source_assert_ref(s);
566
567 c = pa_bytes_to_usec(u->counter, &s->sample_spec);
568 t = pa_smoother_get(u->smoother, pa_rtclock_usec());
569
570 return t > c ? t - c : 0;
571 }
572 #endif
573
574 #ifdef TUNNEL_SINK
575
576 static void sink_input_info_cb(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
577 struct userdata *u = userdata;
578 uint32_t idx, owner_module, client, sink;
579 pa_usec_t buffer_usec, sink_usec;
580 const char *name, *driver, *resample_method;
581 int mute;
582 pa_sample_spec sample_spec;
583 pa_channel_map channel_map;
584 pa_cvolume volume;
585
586 pa_assert(pd);
587 pa_assert(u);
588
589 if (command != PA_COMMAND_REPLY) {
590 if (command == PA_COMMAND_ERROR)
591 pa_log("Failed to get info.");
592 else
593 pa_log("Protocol error.");
594 goto fail;
595 }
596
597 if (pa_tagstruct_getu32(t, &idx) < 0 ||
598 pa_tagstruct_gets(t, &name) < 0 ||
599 pa_tagstruct_getu32(t, &owner_module) < 0 ||
600 pa_tagstruct_getu32(t, &client) < 0 ||
601 pa_tagstruct_getu32(t, &sink) < 0 ||
602 pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
603 pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
604 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
605 pa_tagstruct_get_usec(t, &buffer_usec) < 0 ||
606 pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
607 pa_tagstruct_gets(t, &resample_method) < 0 ||
608 pa_tagstruct_gets(t, &driver) < 0 ||
609 (u->version >= 11 && pa_tagstruct_get_boolean(t, &mute) < 0) ||
610 !pa_tagstruct_eof(t)) {
611 pa_log("Invalid reply. (get_info)");
612 goto fail;
613 }
614
615 pa_assert(u->sink);
616
617 if ((u->version < 11 || !!mute == !!u->sink->muted) &&
618 pa_cvolume_equal(&volume, &u->sink->volume))
619 return;
620
621 memcpy(&u->sink->volume, &volume, sizeof(pa_cvolume));
622
623 if (u->version >= 11)
624 u->sink->muted = !!mute;
625
626 pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index);
627 return;
628
629 fail:
630 pa_module_unload_request(u->module);
631 }
632
633 static void request_info(struct userdata *u) {
634 pa_tagstruct *t;
635 uint32_t tag;
636 pa_assert(u);
637
638 t = pa_tagstruct_new(NULL, 0);
639 pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INPUT_INFO);
640 pa_tagstruct_putu32(t, tag = u->ctag++);
641 pa_tagstruct_putu32(t, u->device_index);
642 pa_pstream_send_tagstruct(u->pstream, t);
643 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, sink_input_info_cb, u, NULL);
644 }
645
646 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) {
647 struct userdata *u = userdata;
648 pa_subscription_event_type_t e;
649 uint32_t idx;
650
651 pa_assert(pd);
652 pa_assert(t);
653 pa_assert(u);
654 pa_assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
655
656 if (pa_tagstruct_getu32(t, &e) < 0 ||
657 pa_tagstruct_getu32(t, &idx) < 0 ||
658 !pa_tagstruct_eof(t)) {
659 pa_log("Invalid protocol reply");
660 pa_module_unload_request(u->module);
661 return;
662 }
663
664 if (e != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
665 return;
666
667 request_info(u);
668 }
669
670 static void start_subscribe(struct userdata *u) {
671 pa_tagstruct *t;
672 uint32_t tag;
673 pa_assert(u);
674
675 t = pa_tagstruct_new(NULL, 0);
676 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
677 pa_tagstruct_putu32(t, tag = u->ctag++);
678 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK_INPUT);
679 pa_pstream_send_tagstruct(u->pstream, t);
680 }
681 #endif
682
683 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
684 struct userdata *u = userdata;
685 struct timeval ntv;
686 #ifdef TUNNEL_SINK
687 uint32_t bytes;
688 #endif
689
690 pa_assert(pd);
691 pa_assert(u);
692 pa_assert(u->pdispatch == pd);
693
694 if (command != PA_COMMAND_REPLY) {
695 if (command == PA_COMMAND_ERROR)
696 pa_log("Failed to create stream.");
697 else
698 pa_log("Protocol error.");
699 goto fail;
700 }
701
702 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
703 pa_tagstruct_getu32(t, &u->device_index) < 0
704 #ifdef TUNNEL_SINK
705 || pa_tagstruct_getu32(t, &bytes) < 0
706 #endif
707 )
708 goto parse_error;
709
710 if (u->version >= 9) {
711 #ifdef TUNNEL_SINK
712 uint32_t maxlength, tlength, prebuf, minreq;
713
714 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
715 pa_tagstruct_getu32(t, &tlength) < 0 ||
716 pa_tagstruct_getu32(t, &prebuf) < 0 ||
717 pa_tagstruct_getu32(t, &minreq) < 0)
718 goto parse_error;
719 #else
720 uint32_t maxlength, fragsize;
721
722 if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
723 pa_tagstruct_getu32(t, &fragsize) < 0)
724 goto parse_error;
725 #endif
726 }
727
728 if (!pa_tagstruct_eof(t))
729 goto parse_error;
730
731 #ifdef TUNNEL_SINK
732 start_subscribe(u);
733 request_info(u);
734 #endif
735
736 pa_assert(!u->time_event);
737 pa_gettimeofday(&ntv);
738 ntv.tv_sec += LATENCY_INTERVAL;
739 u->time_event = u->core->mainloop->time_new(u->core->mainloop, &ntv, timeout_callback, u);
740
741 request_latency(u);
742
743 pa_log_debug("Stream created.");
744
745 #ifdef TUNNEL_SINK
746 pa_asyncmsgq_post(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REQUEST, NULL, bytes, NULL, NULL);
747 #endif
748
749 return;
750
751 parse_error:
752 pa_log("Invalid reply. (Create stream)");
753
754 fail:
755 pa_module_unload_request(u->module);
756 }
757
758 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
759 struct userdata *u = userdata;
760 pa_tagstruct *reply;
761 char name[256], un[128], hn[128];
762 #ifdef TUNNEL_SINK
763 pa_cvolume volume;
764 #endif
765
766 pa_assert(pd);
767 pa_assert(u);
768 pa_assert(u->pdispatch == pd);
769
770 if (command != PA_COMMAND_REPLY ||
771 pa_tagstruct_getu32(t, &u->version) < 0 ||
772 !pa_tagstruct_eof(t)) {
773 if (command == PA_COMMAND_ERROR)
774 pa_log("Failed to authenticate");
775 else
776 pa_log("Protocol error.");
777
778 goto fail;
779 }
780
781 /* Minimum supported protocol version */
782 if (u->version < 8) {
783 pa_log("Incompatible protocol version");
784 goto fail;
785 }
786
787 #ifdef TUNNEL_SINK
788 pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, sink %s",
789 pa_get_host_name(hn, sizeof(hn)),
790 pa_get_user_name(un, sizeof(un)),
791 u->sink->name);
792 #else
793 pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, source %s",
794 pa_get_host_name(hn, sizeof(hn)),
795 pa_get_user_name(un, sizeof(un)),
796 u->source->name);
797 #endif
798
799 reply = pa_tagstruct_new(NULL, 0);
800 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
801 pa_tagstruct_putu32(reply, tag = u->ctag++);
802 pa_tagstruct_puts(reply, name);
803 pa_pstream_send_tagstruct(u->pstream, reply);
804 /* We ignore the server's reply here */
805
806 reply = pa_tagstruct_new(NULL, 0);
807
808 #ifdef TUNNEL_SINK
809 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
810 pa_tagstruct_putu32(reply, tag = u->ctag++);
811 pa_tagstruct_puts(reply, name);
812 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
813 pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
814 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
815 pa_tagstruct_puts(reply, u->sink_name);
816 pa_tagstruct_putu32(reply, u->maxlength);
817 pa_tagstruct_put_boolean(reply, !PA_SINK_OPENED(pa_sink_get_state(u->sink)));
818 pa_tagstruct_putu32(reply, u->tlength);
819 pa_tagstruct_putu32(reply, u->prebuf);
820 pa_tagstruct_putu32(reply, u->minreq);
821 pa_tagstruct_putu32(reply, 0);
822 pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
823 pa_tagstruct_put_cvolume(reply, &volume);
824 #else
825 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
826 pa_tagstruct_putu32(reply, tag = u->ctag++);
827 pa_tagstruct_puts(reply, name);
828 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
829 pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
830 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
831 pa_tagstruct_puts(reply, u->source_name);
832 pa_tagstruct_putu32(reply, u->maxlength);
833 pa_tagstruct_put_boolean(reply, !PA_SOURCE_OPENED(pa_source_get_state(u->source)));
834 pa_tagstruct_putu32(reply, u->fragsize);
835 #endif
836
837 pa_pstream_send_tagstruct(u->pstream, reply);
838 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
839
840 pa_log_debug("Connection authenticated, creating stream ...");
841
842 return;
843
844 fail:
845 pa_module_unload_request(u->module);
846 }
847
848 static void pstream_die_callback(pa_pstream *p, void *userdata) {
849 struct userdata *u = userdata;
850
851 pa_assert(p);
852 pa_assert(u);
853
854 pa_log_warn("Stream died.");
855 pa_module_unload_request(u->module);
856 }
857
858 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
859 struct userdata *u = userdata;
860
861 pa_assert(p);
862 pa_assert(packet);
863 pa_assert(u);
864
865 if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
866 pa_log("Invalid packet");
867 pa_module_unload_request(u->module);
868 return;
869 }
870 }
871
872 #ifndef TUNNEL_SINK
873 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) {
874 struct userdata *u = userdata;
875
876 pa_assert(p);
877 pa_assert(chunk);
878 pa_assert(u);
879
880 if (channel != u->channel) {
881 pa_log("Recieved memory block on bad channel.");
882 pa_module_unload_request(u->module);
883 return;
884 }
885
886 pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, PA_UINT_TO_PTR(seek), offset, chunk);
887
888 u->counter += chunk->length;
889 u->counter_delta += chunk->length;
890 }
891
892 #endif
893
894 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
895 struct userdata *u = userdata;
896 pa_tagstruct *t;
897 uint32_t tag;
898
899 pa_assert(sc);
900 pa_assert(u);
901 pa_assert(u->client == sc);
902
903 pa_socket_client_unref(u->client);
904 u->client = NULL;
905
906 if (!io) {
907 pa_log("Connection failed: %s", pa_cstrerror(errno));
908 pa_module_unload_request(u->module);
909 return;
910 }
911
912 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
913 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
914
915 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
916 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
917 #ifndef TUNNEL_SINK
918 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
919 #endif
920
921 t = pa_tagstruct_new(NULL, 0);
922 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
923 pa_tagstruct_putu32(t, tag = u->ctag++);
924 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
925 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
926
927 #ifdef HAVE_CREDS
928 {
929 pa_creds ucred;
930
931 if (pa_iochannel_creds_supported(io))
932 pa_iochannel_creds_enable(io);
933
934 ucred.uid = getuid();
935 ucred.gid = getgid();
936
937 pa_pstream_send_tagstruct_with_creds(u->pstream, t, &ucred);
938 }
939 #else
940 pa_pstream_send_tagstruct(u->pstream, t);
941 #endif
942
943 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
944
945 pa_log_debug("Connection established, authenticating ...");
946 }
947
948 #ifdef TUNNEL_SINK
949
950 static int sink_get_volume(pa_sink *sink) {
951 return 0;
952 }
953
954 static int sink_set_volume(pa_sink *sink) {
955 struct userdata *u;
956 pa_tagstruct *t;
957 uint32_t tag;
958
959 pa_assert(sink);
960 u = sink->userdata;
961 pa_assert(u);
962
963 t = pa_tagstruct_new(NULL, 0);
964 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_VOLUME);
965 pa_tagstruct_putu32(t, tag = u->ctag++);
966 pa_tagstruct_putu32(t, u->device_index);
967 pa_tagstruct_put_cvolume(t, &sink->volume);
968 pa_pstream_send_tagstruct(u->pstream, t);
969
970 return 0;
971 }
972
973 static int sink_get_mute(pa_sink *sink) {
974 return 0;
975 }
976
977 static int sink_set_mute(pa_sink *sink) {
978 struct userdata *u;
979 pa_tagstruct *t;
980 uint32_t tag;
981
982 pa_assert(sink);
983 u = sink->userdata;
984 pa_assert(u);
985
986 if (u->version < 11)
987 return -1;
988
989 t = pa_tagstruct_new(NULL, 0);
990 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_MUTE);
991 pa_tagstruct_putu32(t, tag = u->ctag++);
992 pa_tagstruct_putu32(t, u->device_index);
993 pa_tagstruct_put_boolean(t, !!sink->muted);
994 pa_pstream_send_tagstruct(u->pstream, t);
995
996 return 0;
997 }
998
999 #endif
1000
1001 static int load_key(struct userdata *u, const char*fn) {
1002 pa_assert(u);
1003
1004 u->auth_cookie_in_property = FALSE;
1005
1006 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
1007 pa_log_debug("using already loaded auth cookie.");
1008 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
1009 u->auth_cookie_in_property = 1;
1010 return 0;
1011 }
1012
1013 if (!fn)
1014 fn = PA_NATIVE_COOKIE_FILE;
1015
1016 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
1017 return -1;
1018
1019 pa_log_debug("loading cookie from disk.");
1020
1021 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
1022 u->auth_cookie_in_property = TRUE;
1023
1024 return 0;
1025 }
1026
1027 int pa__init(pa_module*m) {
1028 pa_modargs *ma = NULL;
1029 struct userdata *u = NULL;
1030 pa_sample_spec ss;
1031 pa_channel_map map;
1032 char *t, *dn = NULL;
1033
1034 pa_assert(m);
1035
1036 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1037 pa_log("failed to parse module arguments");
1038 goto fail;
1039 }
1040
1041 u = pa_xnew(struct userdata, 1);
1042 m->userdata = u;
1043 u->module = m;
1044 u->core = m->core;
1045 u->client = NULL;
1046 u->pdispatch = NULL;
1047 u->pstream = NULL;
1048 u->server_name = NULL;
1049 #ifdef TUNNEL_SINK
1050 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
1051 u->sink = NULL;
1052 u->requested_bytes = 0;
1053 #else
1054 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
1055 u->source = NULL;
1056 #endif
1057 u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE);
1058 u->ctag = 1;
1059 u->device_index = u->channel = PA_INVALID_INDEX;
1060 u->auth_cookie_in_property = FALSE;
1061 u->time_event = NULL;
1062
1063 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1064 u->rtpoll = pa_rtpoll_new();
1065 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
1066
1067 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
1068 goto fail;
1069
1070 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
1071 pa_log("no server specified.");
1072 goto fail;
1073 }
1074
1075 ss = m->core->default_sample_spec;
1076 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
1077 pa_log("invalid sample format specification");
1078 goto fail;
1079 }
1080
1081 if (!(u->client = pa_socket_client_new_string(m->core->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
1082 pa_log("failed to connect to server '%s'", u->server_name);
1083 goto fail;
1084 }
1085
1086 pa_socket_client_set_callback(u->client, on_connection, u);
1087
1088 #ifdef TUNNEL_SINK
1089
1090 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
1091 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
1092
1093 if (!(u->sink = pa_sink_new(m->core, __FILE__, dn, 1, &ss, &map))) {
1094 pa_log("Failed to create sink.");
1095 goto fail;
1096 }
1097
1098 u->sink->parent.process_msg = sink_process_msg;
1099 u->sink->userdata = u;
1100 u->sink->set_state = sink_set_state;
1101 u->sink->get_latency = sink_get_latency;
1102 u->sink->get_volume = sink_get_volume;
1103 u->sink->get_mute = sink_get_mute;
1104 u->sink->set_volume = sink_set_volume;
1105 u->sink->set_mute = sink_set_mute;
1106 u->sink->flags = PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL;
1107
1108 pa_sink_set_module(u->sink, m);
1109 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1110 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1111 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));
1112 pa_xfree(t);
1113
1114 #else
1115
1116 if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
1117 dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
1118
1119 if (!(u->source = pa_source_new(m->core, __FILE__, dn, 1, &ss, &map))) {
1120 pa_log("Failed to create source.");
1121 goto fail;
1122 }
1123
1124 u->source->parent.process_msg = source_process_msg;
1125 u->source->userdata = u;
1126 u->source->set_state = source_set_state;
1127 u->source->get_latency = source_get_latency;
1128 u->source->flags = PA_SOURCE_NETWORK|PA_SOURCE_LATENCY;
1129
1130 pa_source_set_module(u->source, m);
1131 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1132 pa_source_set_rtpoll(u->source, u->rtpoll);
1133 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));
1134 pa_xfree(t);
1135 #endif
1136
1137 pa_xfree(dn);
1138
1139 u->time_event = NULL;
1140
1141 u->maxlength = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_MAXLENGTH_MSEC, &ss);
1142 #ifdef TUNNEL_SINK
1143 u->tlength = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_TLENGTH_MSEC, &ss);
1144 u->minreq = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_MINREQ_MSEC, &ss);
1145 u->prebuf = u->tlength;
1146 #else
1147 u->fragsize = pa_usec_to_bytes(PA_USEC_PER_MSEC * DEFAULT_FRAGSIZE_MSEC, &ss);
1148 #endif
1149
1150 u->counter = u->counter_delta = 0;
1151 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
1152
1153 if (!(u->thread = pa_thread_new(thread_func, u))) {
1154 pa_log("Failed to create thread.");
1155 goto fail;
1156 }
1157
1158 #ifdef TUNNEL_SINK
1159 pa_sink_put(u->sink);
1160 #else
1161 pa_source_put(u->source);
1162 #endif
1163
1164 pa_modargs_free(ma);
1165
1166 return 0;
1167
1168 fail:
1169 pa__done(m);
1170
1171 if (ma)
1172 pa_modargs_free(ma);
1173
1174 pa_xfree(dn);
1175
1176 return -1;
1177 }
1178
1179 void pa__done(pa_module*m) {
1180 struct userdata* u;
1181
1182 pa_assert(m);
1183
1184 if (!(u = m->userdata))
1185 return;
1186
1187 #ifdef TUNNEL_SINK
1188 if (u->sink)
1189 pa_sink_unlink(u->sink);
1190 #else
1191 if (u->source)
1192 pa_source_unlink(u->source);
1193 #endif
1194
1195 if (u->thread) {
1196 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1197 pa_thread_free(u->thread);
1198 }
1199
1200 pa_thread_mq_done(&u->thread_mq);
1201
1202 #ifdef TUNNEL_SINK
1203 if (u->sink)
1204 pa_sink_unref(u->sink);
1205 #else
1206 if (u->source)
1207 pa_source_unref(u->source);
1208 #endif
1209
1210 if (u->rtpoll)
1211 pa_rtpoll_free(u->rtpoll);
1212
1213 if (u->pstream) {
1214 pa_pstream_unlink(u->pstream);
1215 pa_pstream_unref(u->pstream);
1216 }
1217
1218 if (u->pdispatch)
1219 pa_pdispatch_unref(u->pdispatch);
1220
1221 if (u->client)
1222 pa_socket_client_unref(u->client);
1223
1224 if (u->auth_cookie_in_property)
1225 pa_authkey_prop_unref(m->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
1226
1227 if (u->smoother)
1228 pa_smoother_free(u->smoother);
1229
1230 if (u->time_event)
1231 u->core->mainloop->time_free(u->time_event);
1232
1233 #ifdef TUNNEL_SINK
1234 pa_xfree(u->sink_name);
1235 #else
1236 pa_xfree(u->source_name);
1237 #endif
1238 pa_xfree(u->server_name);
1239
1240 pa_xfree(u);
1241 }