]> code.delx.au - pulseaudio/blob - polyp/module-tunnel.c
712ec359e196089c3f046077f04421f95500176d
[pulseaudio] / polyp / module-tunnel.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 <regex.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "module.h"
36 #include "util.h"
37 #include "modargs.h"
38 #include "log.h"
39 #include "subscribe.h"
40 #include "xmalloc.h"
41 #include "sink-input.h"
42 #include "pdispatch.h"
43 #include "pstream.h"
44 #include "pstream-util.h"
45 #include "authkey.h"
46 #include "socket-client.h"
47 #include "socket-util.h"
48 #include "authkey-prop.h"
49
50 #ifdef TUNNEL_SINK
51 #include "module-tunnel-sink-symdef.h"
52 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
53 PA_MODULE_USAGE("server=<address> sink=<remote sink name> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate> sink_name=<name for the local sink>")
54 #else
55 #include "module-tunnel-source-symdef.h"
56 PA_MODULE_DESCRIPTION("Tunnel module for sources")
57 PA_MODULE_USAGE("server=<address> source=<remote source name> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate> source_name=<name for the local source>")
58 #endif
59
60 PA_MODULE_AUTHOR("Lennart Poettering")
61 PA_MODULE_VERSION(PACKAGE_VERSION)
62
63 #define DEFAULT_SINK_NAME "tunnel"
64 #define DEFAULT_SOURCE_NAME "tunnel"
65
66 #define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
67 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
68 #define DEFAULT_MINREQ 512
69 #define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
70 #define DEFAULT_FRAGSIZE 1024
71
72 #define DEFAULT_TIMEOUT 5
73
74 #define LATENCY_INTERVAL 10
75
76 static const char* const valid_modargs[] = {
77 "server",
78 "cookie",
79 "format",
80 "channels",
81 "rate",
82 #ifdef TUNNEL_SINK
83 "sink_name",
84 "sink",
85 #else
86 "source_name",
87 "source",
88 #endif
89 NULL,
90 };
91
92 static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
93
94 #ifdef TUNNEL_SINK
95 static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
96 #endif
97
98 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
99 #ifdef TUNNEL_SINK
100 [PA_COMMAND_REQUEST] = { command_request },
101 #endif
102 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_stream_killed },
103 [PA_COMMAND_RECORD_STREAM_KILLED] = { command_stream_killed },
104 };
105
106 struct userdata {
107 struct pa_socket_client *client;
108 struct pa_pstream *pstream;
109 struct pa_pdispatch *pdispatch;
110
111 char *server_name;
112 #ifdef TUNNEL_SINK
113 char *sink_name;
114 struct pa_sink *sink;
115 uint32_t requested_bytes;
116 #else
117 char *source_name;
118 struct pa_source *source;
119 #endif
120
121 struct pa_module *module;
122 struct pa_core *core;
123
124 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
125
126 uint32_t ctag;
127 uint32_t device_index;
128 uint32_t channel;
129
130 pa_usec_t host_latency;
131
132 struct pa_time_event *time_event;
133
134 int auth_cookie_in_property;
135 };
136
137 static void close_stuff(struct userdata *u) {
138 assert(u);
139
140 if (u->pstream) {
141 pa_pstream_close(u->pstream);
142 pa_pstream_unref(u->pstream);
143 u->pstream = NULL;
144 }
145
146 if (u->pdispatch) {
147 pa_pdispatch_unref(u->pdispatch);
148 u->pdispatch = NULL;
149 }
150
151 if (u->client) {
152 pa_socket_client_unref(u->client);
153 u->client = NULL;
154 }
155
156 #ifdef TUNNEL_SINK
157 if (u->sink) {
158 pa_sink_disconnect(u->sink);
159 pa_sink_unref(u->sink);
160 u->sink = NULL;
161 }
162 #else
163 if (u->source) {
164 pa_source_disconnect(u->source);
165 pa_source_unref(u->source);
166 u->source = NULL;
167 }
168 #endif
169
170 if (u->time_event) {
171 u->core->mainloop->time_free(u->time_event);
172 u->time_event = NULL;
173 }
174 }
175
176 static void die(struct userdata *u) {
177 assert(u);
178 close_stuff(u);
179 pa_module_unload_request(u->module);
180 }
181
182 static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
183 struct userdata *u = userdata;
184 assert(pd && t && u && u->pdispatch == pd);
185
186 pa_log(__FILE__": stream killed\n");
187 die(u);
188 }
189
190 #ifdef TUNNEL_SINK
191 static void send_prebuf_request(struct userdata *u) {
192 struct pa_tagstruct *t;
193
194 t = pa_tagstruct_new(NULL, 0);
195 pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
196 pa_tagstruct_putu32(t, u->ctag++);
197 pa_tagstruct_putu32(t, u->channel);
198 pa_pstream_send_tagstruct(u->pstream, t);
199 }
200
201 static void send_bytes(struct userdata *u) {
202 assert(u);
203
204 if (!u->pstream)
205 return;
206
207 while (u->requested_bytes > 0) {
208 struct pa_memchunk chunk;
209 if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
210
211
212 if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
213 send_prebuf_request(u);
214
215 return;
216 }
217
218 pa_pstream_send_memblock(u->pstream, u->channel, 0, &chunk);
219 pa_memblock_unref(chunk.memblock);
220
221 if (chunk.length > u->requested_bytes)
222 u->requested_bytes = 0;
223 else
224 u->requested_bytes -= chunk.length;
225 }
226 }
227
228 static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
229 struct userdata *u = userdata;
230 uint32_t bytes, channel;
231 assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
232
233 if (pa_tagstruct_getu32(t, &channel) < 0 ||
234 pa_tagstruct_getu32(t, &bytes) < 0 ||
235 !pa_tagstruct_eof(t)) {
236 pa_log(__FILE__": invalid protocol reply\n");
237 die(u);
238 return;
239 }
240
241 if (channel != u->channel) {
242 pa_log(__FILE__": recieved data for invalid channel\n");
243 die(u);
244 return;
245 }
246
247 u->requested_bytes += bytes;
248 send_bytes(u);
249 }
250
251 #endif
252
253 static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
254 struct userdata *u = userdata;
255 pa_usec_t buffer_usec, sink_usec, source_usec, transport_usec;
256 int playing;
257 uint32_t queue_length;
258 struct timeval local, remote, now;
259 assert(pd && u && t);
260
261 if (command != PA_COMMAND_REPLY) {
262 if (command == PA_COMMAND_ERROR)
263 pa_log(__FILE__": failed to get latency.\n");
264 else
265 pa_log(__FILE__": protocol error.\n");
266 die(u);
267 return;
268 }
269
270 if (pa_tagstruct_get_usec(t, &buffer_usec) < 0 ||
271 pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
272 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
273 pa_tagstruct_get_boolean(t, &playing) < 0 ||
274 pa_tagstruct_getu32(t, &queue_length) < 0 ||
275 pa_tagstruct_get_timeval(t, &local) < 0 ||
276 pa_tagstruct_get_timeval(t, &remote) < 0 ||
277 !pa_tagstruct_eof(t)) {
278 pa_log(__FILE__": invalid reply.\n");
279 die(u);
280 return;
281 }
282
283 gettimeofday(&now, NULL);
284
285 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
286 /* local and remote seem to have synchronized clocks */
287 #ifdef TUNNEL_SINK
288 transport_usec = pa_timeval_diff(&remote, &local);
289 #else
290 transport_usec = pa_timeval_diff(&now, &remote);
291 #endif
292 } else
293 transport_usec = pa_timeval_diff(&now, &local)/2;
294
295 #ifdef TUNNEL_SINK
296 u->host_latency = sink_usec + transport_usec;
297 #else
298 u->host_latency = source_usec + transport_usec;
299 if (u->host_latency > sink_usec)
300 u->host_latency -= sink_usec;
301 else
302 u->host_latency = 0;
303 #endif
304
305 /* pa_log(__FILE__": estimated host latency: %0.0f usec\n", (double) u->host_latency); */
306 }
307
308 static void request_latency(struct userdata *u) {
309 struct pa_tagstruct *t;
310 struct timeval now;
311 uint32_t tag;
312 assert(u);
313
314 t = pa_tagstruct_new(NULL, 0);
315 #ifdef TUNNEL_SINK
316 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
317 #else
318 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
319 #endif
320 pa_tagstruct_putu32(t, tag = u->ctag++);
321 pa_tagstruct_putu32(t, u->channel);
322
323 gettimeofday(&now, NULL);
324 pa_tagstruct_put_timeval(t, &now);
325
326 pa_pstream_send_tagstruct(u->pstream, t);
327 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u);
328 }
329
330 static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
331 struct userdata *u = userdata;
332 assert(pd && u && u->pdispatch == pd);
333
334 if (command != PA_COMMAND_REPLY) {
335 if (command == PA_COMMAND_ERROR)
336 pa_log(__FILE__": failed to create stream.\n");
337 else
338 pa_log(__FILE__": protocol error.\n");
339 die(u);
340 return;
341 }
342
343 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
344 pa_tagstruct_getu32(t, &u->device_index) < 0 ||
345 #ifdef TUNNEL_SINK
346 pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
347 #endif
348 !pa_tagstruct_eof(t)) {
349 pa_log(__FILE__": invalid reply.\n");
350 die(u);
351 return;
352 }
353
354 request_latency(u);
355 #ifdef TUNNEL_SINK
356 send_bytes(u);
357 #endif
358 }
359
360 static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
361 struct userdata *u = userdata;
362 struct pa_tagstruct *reply;
363 char name[256], un[128], hn[128];
364 assert(pd && u && u->pdispatch == pd);
365
366 if (command != PA_COMMAND_REPLY || !pa_tagstruct_eof(t)) {
367 if (command == PA_COMMAND_ERROR)
368 pa_log(__FILE__": failed to authenticate\n");
369 else
370 pa_log(__FILE__": protocol error.\n");
371 die(u);
372 return;
373 }
374 #ifdef TUNNEL_SINK
375 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
376 pa_get_host_name(hn, sizeof(hn)),
377 pa_get_user_name(un, sizeof(un)),
378 u->sink->name);
379 #else
380 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', source '%s'",
381 pa_get_host_name(hn, sizeof(hn)),
382 pa_get_user_name(un, sizeof(un)),
383 u->source->name);
384 #endif
385
386 reply = pa_tagstruct_new(NULL, 0);
387 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
388 pa_tagstruct_putu32(reply, tag = u->ctag++);
389 pa_tagstruct_puts(reply, name);
390 pa_pstream_send_tagstruct(u->pstream, reply);
391 /* We ignore the server's reply here */
392
393 reply = pa_tagstruct_new(NULL, 0);
394 #ifdef TUNNEL_SINK
395 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
396 pa_tagstruct_putu32(reply, tag = u->ctag++);
397 pa_tagstruct_puts(reply, name);
398 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
399 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
400 pa_tagstruct_puts(reply, u->sink_name);
401 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
402 pa_tagstruct_put_boolean(reply, 0);
403 pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
404 pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
405 pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
406 pa_tagstruct_putu32(reply, PA_VOLUME_NORM);
407 #else
408 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
409 pa_tagstruct_putu32(reply, tag = u->ctag++);
410 pa_tagstruct_puts(reply, name);
411 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
412 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
413 pa_tagstruct_puts(reply, u->source_name);
414 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
415 pa_tagstruct_put_boolean(reply, 0);
416 pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
417 #endif
418
419 pa_pstream_send_tagstruct(u->pstream, reply);
420 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u);
421 }
422
423 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
424 struct userdata *u = userdata;
425 assert(p && u);
426
427 pa_log(__FILE__": stream died.\n");
428 die(u);
429 }
430
431
432 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
433 struct userdata *u = userdata;
434 assert(p && packet && u);
435
436 if (pa_pdispatch_run(u->pdispatch, packet, u) < 0) {
437 pa_log(__FILE__": invalid packet\n");
438 die(u);
439 }
440 }
441
442 #ifndef TUNNEL_SINK
443 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, uint32_t delta, const struct pa_memchunk *chunk, void *userdata) {
444 struct userdata *u = userdata;
445 assert(p && chunk && u);
446
447 if (channel != u->channel) {
448 pa_log(__FILE__": recieved memory block on bad channel.\n");
449 die(u);
450 return;
451 }
452
453 pa_source_post(u->source, chunk);
454 }
455 #endif
456
457 static void on_connection(struct pa_socket_client *sc, struct pa_iochannel *io, void *userdata) {
458 struct userdata *u = userdata;
459 struct pa_tagstruct *t;
460 uint32_t tag;
461 assert(sc && u && u->client == sc);
462
463 pa_socket_client_unref(u->client);
464 u->client = NULL;
465
466 if (!io) {
467 pa_log(__FILE__": connection failed.\n");
468 pa_module_unload_request(u->module);
469 return;
470 }
471
472 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
473 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
474
475 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
476 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
477 #ifndef TUNNEL_SINK
478 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
479 #endif
480
481 t = pa_tagstruct_new(NULL, 0);
482 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
483 pa_tagstruct_putu32(t, tag = u->ctag++);
484 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
485 pa_pstream_send_tagstruct(u->pstream, t);
486 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u);
487
488 }
489
490 #ifdef TUNNEL_SINK
491 static void sink_notify(struct pa_sink*sink) {
492 struct userdata *u;
493 assert(sink && sink->userdata);
494 u = sink->userdata;
495
496 send_bytes(u);
497 }
498
499 static pa_usec_t sink_get_latency(struct pa_sink *sink) {
500 struct userdata *u;
501 uint32_t l;
502 pa_usec_t usec = 0;
503 assert(sink && sink->userdata);
504 u = sink->userdata;
505
506 l = DEFAULT_TLENGTH;
507
508 if (l > u->requested_bytes) {
509 l -= u->requested_bytes;
510 usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
511 }
512
513 usec += u->host_latency;
514
515 return usec;
516 }
517 #else
518 static pa_usec_t source_get_latency(struct pa_source *source) {
519 struct userdata *u;
520 assert(source && source->userdata);
521 u = source->userdata;
522
523 return u->host_latency;
524 }
525 #endif
526
527 static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
528 struct userdata *u = userdata;
529 struct timeval ntv;
530 assert(m && e && u);
531
532 request_latency(u);
533
534 gettimeofday(&ntv, NULL);
535 ntv.tv_sec += LATENCY_INTERVAL;
536 m->time_restart(e, &ntv);
537 }
538
539 static int load_key(struct userdata *u, const char*fn) {
540 assert(u);
541
542 u->auth_cookie_in_property = 0;
543
544 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
545 pa_log(__FILE__": using already loaded auth cookie.\n");
546 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
547 u->auth_cookie_in_property = 1;
548 return 0;
549 }
550
551 if (!fn)
552 fn = PA_NATIVE_COOKIE_FILE;
553
554 if (pa_authkey_load_from_home(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
555 return -1;
556
557 pa_log(__FILE__": loading cookie from disk.\n");
558
559 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
560 u->auth_cookie_in_property = 1;
561
562 return 0;
563 }
564
565 int pa__init(struct pa_core *c, struct pa_module*m) {
566 struct pa_modargs *ma = NULL;
567 struct userdata *u = NULL;
568 struct pa_sample_spec ss;
569 struct timeval ntv;
570 assert(c && m);
571
572 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
573 pa_log(__FILE__": failed to parse module arguments\n");
574 goto fail;
575 }
576
577 u = pa_xmalloc(sizeof(struct userdata));
578 m->userdata = u;
579 u->module = m;
580 u->core = c;
581 u->client = NULL;
582 u->pdispatch = NULL;
583 u->pstream = NULL;
584 u->server_name = NULL;
585 #ifdef TUNNEL_SINK
586 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
587 u->sink = NULL;
588 u->requested_bytes = 0;
589 #else
590 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
591 u->source = NULL;
592 #endif
593 u->ctag = 1;
594 u->device_index = u->channel = PA_INVALID_INDEX;
595 u->host_latency = 0;
596 u->auth_cookie_in_property = 0;
597 u->time_event = NULL;
598
599 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
600 goto fail;
601
602 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
603 pa_log(__FILE__": no server specified.\n");
604 goto fail;
605 }
606
607 ss = c->default_sample_spec;
608 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
609 pa_log(__FILE__": invalid sample format specification\n");
610 goto fail;
611 }
612
613 if (u->server_name[0] == '/')
614 u->client = pa_socket_client_new_unix(c->mainloop, u->server_name);
615 else {
616 size_t len;
617 struct sockaddr *sa;
618
619 if (!(sa = pa_resolve_server(u->server_name, &len, PA_NATIVE_DEFAULT_PORT))) {
620 pa_log(__FILE__": failed to resolve server '%s'\n", u->server_name);
621 goto fail;
622 }
623
624 u->client = pa_socket_client_new_sockaddr(c->mainloop, sa, len);
625 pa_xfree(sa);
626 }
627
628 if (!u->client)
629 goto fail;
630
631 pa_socket_client_set_callback(u->client, on_connection, u);
632
633 #ifdef TUNNEL_SINK
634 if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss))) {
635 pa_log(__FILE__": failed to create sink.\n");
636 goto fail;
637 }
638
639 u->sink->notify = sink_notify;
640 u->sink->get_latency = sink_get_latency;
641 u->sink->userdata = u;
642 u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
643
644 pa_sink_set_owner(u->sink, m);
645 #else
646 if (!(u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss))) {
647 pa_log(__FILE__": failed to create source.\n");
648 goto fail;
649 }
650
651 u->source->get_latency = source_get_latency;
652 u->source->userdata = u;
653 u->source->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->source_name ? u->source_name : "", u->source_name ? "@" : "", u->server_name);
654
655 pa_source_set_owner(u->source, m);
656 #endif
657
658 gettimeofday(&ntv, NULL);
659 ntv.tv_sec += LATENCY_INTERVAL;
660 u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
661
662 pa_modargs_free(ma);
663
664 return 0;
665
666 fail:
667 pa__done(c, m);
668
669 if (ma)
670 pa_modargs_free(ma);
671 return -1;
672 }
673
674 void pa__done(struct pa_core *c, struct pa_module*m) {
675 struct userdata* u;
676 assert(c && m);
677
678 if (!(u = m->userdata))
679 return;
680
681 close_stuff(u);
682
683 if (u->auth_cookie_in_property)
684 pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
685
686 #ifdef TUNNEL_SINK
687 pa_xfree(u->sink_name);
688 #else
689 pa_xfree(u->source_name);
690 #endif
691 pa_xfree(u->server_name);
692
693 pa_xfree(u);
694 }
695
696