4 This file is part of polypaudio.
6 polypaudio 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.
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.
16 You should have received a copy of the GNU Lesser 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
30 #include <sys/types.h>
34 #include <polyp/version.h>
35 #include <polyp/xmalloc.h>
37 #include <polypcore/module.h>
38 #include <polypcore/util.h>
39 #include <polypcore/modargs.h>
40 #include <polypcore/log.h>
41 #include <polypcore/core-subscribe.h>
42 #include <polypcore/sink-input.h>
43 #include <polypcore/pdispatch.h>
44 #include <polypcore/pstream.h>
45 #include <polypcore/pstream-util.h>
46 #include <polypcore/authkey.h>
47 #include <polypcore/socket-client.h>
48 #include <polypcore/socket-util.h>
49 #include <polypcore/authkey-prop.h>
52 #include "module-tunnel-sink-symdef.h"
53 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
56 "sink=<remote sink name> "
58 "format=<sample format> "
59 "channels=<number of channels> "
61 "sink_name=<name for the local sink> "
62 "channel_map=<channel map>")
64 #include "module-tunnel-source-symdef.h"
65 PA_MODULE_DESCRIPTION("Tunnel module for sources")
68 "source=<remote source name> "
70 "format=<sample format> "
71 "channels=<number of channels> "
73 "source_name=<name for the local source> "
74 "channel_map=<channel map>")
77 PA_MODULE_AUTHOR("Lennart Poettering")
78 PA_MODULE_VERSION(PACKAGE_VERSION)
80 #define DEFAULT_SINK_NAME "tunnel"
81 #define DEFAULT_SOURCE_NAME "tunnel"
83 #define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
84 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
85 #define DEFAULT_MINREQ 512
86 #define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
87 #define DEFAULT_FRAGSIZE 1024
89 #define DEFAULT_TIMEOUT 5
91 #define LATENCY_INTERVAL 10
93 static const char* const valid_modargs[] = {
110 static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
111 static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
114 static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
117 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
119 [PA_COMMAND_REQUEST] = command_request,
121 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
122 [PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
123 [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
127 pa_socket_client *client;
129 pa_pdispatch *pdispatch;
135 uint32_t requested_bytes;
144 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
148 uint32_t device_index;
151 pa_usec_t host_latency;
153 pa_time_event *time_event;
155 int auth_cookie_in_property;
158 static void close_stuff(struct userdata *u) {
162 pa_pstream_close(u->pstream);
163 pa_pstream_unref(u->pstream);
168 pa_pdispatch_unref(u->pdispatch);
173 pa_socket_client_unref(u->client);
179 pa_sink_disconnect(u->sink);
180 pa_sink_unref(u->sink);
185 pa_source_disconnect(u->source);
186 pa_source_unref(u->source);
192 u->core->mainloop->time_free(u->time_event);
193 u->time_event = NULL;
197 static void die(struct userdata *u) {
200 pa_module_unload_request(u->module);
203 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) {
204 struct userdata *u = userdata;
205 assert(pd && t && u && u->pdispatch == pd);
207 pa_log(__FILE__": stream killed");
211 static void request_info(struct userdata *u);
213 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) {
214 struct userdata *u = userdata;
215 pa_subscription_event_type_t e;
218 assert(pd && t && u);
219 assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
221 if (pa_tagstruct_getu32(t, &e) < 0 ||
222 pa_tagstruct_getu32(t, &idx) < 0 ||
223 !pa_tagstruct_eof(t)) {
224 pa_log(__FILE__": invalid protocol reply");
230 if (e != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
233 if (e != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
241 static void send_prebuf_request(struct userdata *u) {
244 t = pa_tagstruct_new(NULL, 0);
245 pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
246 pa_tagstruct_putu32(t, u->ctag++);
247 pa_tagstruct_putu32(t, u->channel);
248 pa_pstream_send_tagstruct(u->pstream, t);
251 static void send_bytes(struct userdata *u) {
257 while (u->requested_bytes > 0) {
259 if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
261 if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
262 send_prebuf_request(u);
267 pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, &chunk);
268 pa_memblock_unref(chunk.memblock);
270 if (chunk.length > u->requested_bytes)
271 u->requested_bytes = 0;
273 u->requested_bytes -= chunk.length;
277 static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
278 struct userdata *u = userdata;
279 uint32_t bytes, channel;
280 assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
282 if (pa_tagstruct_getu32(t, &channel) < 0 ||
283 pa_tagstruct_getu32(t, &bytes) < 0 ||
284 !pa_tagstruct_eof(t)) {
285 pa_log(__FILE__": invalid protocol reply");
290 if (channel != u->channel) {
291 pa_log(__FILE__": recieved data for invalid channel");
296 u->requested_bytes += bytes;
302 static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
303 struct userdata *u = userdata;
304 pa_usec_t sink_usec, source_usec, transport_usec;
306 int64_t write_index, read_index;
307 struct timeval local, remote, now;
310 if (command != PA_COMMAND_REPLY) {
311 if (command == PA_COMMAND_ERROR)
312 pa_log(__FILE__": failed to get latency.");
314 pa_log(__FILE__": protocol error.");
319 if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
320 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
321 pa_tagstruct_get_boolean(t, &playing) < 0 ||
322 pa_tagstruct_get_timeval(t, &local) < 0 ||
323 pa_tagstruct_get_timeval(t, &remote) < 0 ||
324 pa_tagstruct_gets64(t, &write_index) < 0 ||
325 pa_tagstruct_gets64(t, &read_index) < 0 ||
326 !pa_tagstruct_eof(t)) {
327 pa_log(__FILE__": invalid reply. (latency)");
332 pa_gettimeofday(&now);
334 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
335 /* local and remote seem to have synchronized clocks */
337 transport_usec = pa_timeval_diff(&remote, &local);
339 transport_usec = pa_timeval_diff(&now, &remote);
342 transport_usec = pa_timeval_diff(&now, &local)/2;
345 u->host_latency = sink_usec + transport_usec;
347 u->host_latency = source_usec + transport_usec;
348 if (u->host_latency > sink_usec)
349 u->host_latency -= sink_usec;
354 /* pa_log(__FILE__": estimated host latency: %0.0f usec", (double) u->host_latency); */
357 static void request_latency(struct userdata *u) {
363 t = pa_tagstruct_new(NULL, 0);
365 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
367 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
369 pa_tagstruct_putu32(t, tag = u->ctag++);
370 pa_tagstruct_putu32(t, u->channel);
372 pa_gettimeofday(&now);
373 pa_tagstruct_put_timeval(t, &now);
375 pa_pstream_send_tagstruct(u->pstream, t);
376 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
379 static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
380 struct userdata *u = userdata;
381 uint32_t idx, owner_module, monitor_source;
383 const char *name, *description, *monitor_source_name, *driver;
386 pa_sample_spec sample_spec;
387 pa_channel_map channel_map;
391 if (command != PA_COMMAND_REPLY) {
392 if (command == PA_COMMAND_ERROR)
393 pa_log(__FILE__": failed to get info.");
395 pa_log(__FILE__": protocol error.");
400 if (pa_tagstruct_getu32(t, &idx) < 0 ||
401 pa_tagstruct_gets(t, &name) < 0 ||
402 pa_tagstruct_gets(t, &description) < 0 ||
403 pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
404 pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
405 pa_tagstruct_getu32(t, &owner_module) < 0 ||
406 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
407 pa_tagstruct_get_boolean(t, &mute) < 0 ||
408 pa_tagstruct_getu32(t, &monitor_source) < 0 ||
409 pa_tagstruct_gets(t, &monitor_source_name) < 0 ||
410 pa_tagstruct_get_usec(t, &latency) < 0 ||
411 pa_tagstruct_gets(t, &driver) < 0 ||
412 pa_tagstruct_getu32(t, &flags) < 0 ||
413 !pa_tagstruct_eof(t)) {
414 pa_log(__FILE__": invalid reply. (get_info)");
421 if ((!!mute == !!u->sink->hw_muted) &&
422 pa_cvolume_equal(&volume, &u->sink->hw_volume))
426 if ((!!mute == !!u->source->hw_muted) &&
427 pa_cvolume_equal(&volume, &u->source->hw_volume))
432 memcpy(&u->sink->hw_volume, &volume, sizeof(pa_cvolume));
433 u->sink->hw_muted = !!mute;
435 pa_subscription_post(u->sink->core,
436 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
439 memcpy(&u->source->hw_volume, &volume, sizeof(pa_cvolume));
440 u->source->hw_muted = !!mute;
442 pa_subscription_post(u->source->core,
443 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
448 static void request_info(struct userdata *u) {
453 t = pa_tagstruct_new(NULL, 0);
455 pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INFO);
457 pa_tagstruct_putu32(t, PA_COMMAND_GET_SOURCE_INFO);
459 pa_tagstruct_putu32(t, tag = u->ctag++);
461 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
463 pa_tagstruct_puts(t, u->sink_name);
465 pa_tagstruct_puts(t, u->source_name);
468 pa_pstream_send_tagstruct(u->pstream, t);
469 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u, NULL);
472 static void start_subscribe(struct userdata *u) {
477 t = pa_tagstruct_new(NULL, 0);
478 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
479 pa_tagstruct_putu32(t, tag = u->ctag++);
482 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK);
484 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SOURCE);
487 pa_pstream_send_tagstruct(u->pstream, t);
490 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
491 struct userdata *u = userdata;
492 assert(pd && u && u->pdispatch == pd);
494 if (command != PA_COMMAND_REPLY) {
495 if (command == PA_COMMAND_ERROR)
496 pa_log(__FILE__": failed to create stream.");
498 pa_log(__FILE__": protocol error.");
503 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
504 pa_tagstruct_getu32(t, &u->device_index) < 0 ||
506 pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
508 !pa_tagstruct_eof(t)) {
509 pa_log(__FILE__": invalid reply. (create stream)");
523 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
524 struct userdata *u = userdata;
526 char name[256], un[128], hn[128];
530 assert(pd && u && u->pdispatch == pd);
532 if (command != PA_COMMAND_REPLY ||
533 pa_tagstruct_getu32(t, &u->version) < 0 ||
534 !pa_tagstruct_eof(t)) {
535 if (command == PA_COMMAND_ERROR)
536 pa_log(__FILE__": failed to authenticate");
538 pa_log(__FILE__": protocol error.");
543 /* Minimum supported protocol version */
544 if (u->version < 8) {
545 pa_log(__FILE__": incompatible protocol version");
551 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
552 pa_get_host_name(hn, sizeof(hn)),
553 pa_get_user_name(un, sizeof(un)),
556 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', source '%s'",
557 pa_get_host_name(hn, sizeof(hn)),
558 pa_get_user_name(un, sizeof(un)),
562 reply = pa_tagstruct_new(NULL, 0);
563 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
564 pa_tagstruct_putu32(reply, tag = u->ctag++);
565 pa_tagstruct_puts(reply, name);
566 pa_pstream_send_tagstruct(u->pstream, reply);
567 /* We ignore the server's reply here */
569 reply = pa_tagstruct_new(NULL, 0);
571 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
572 pa_tagstruct_putu32(reply, tag = u->ctag++);
573 pa_tagstruct_puts(reply, name);
574 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
575 pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
576 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
577 pa_tagstruct_puts(reply, u->sink_name);
578 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
579 pa_tagstruct_put_boolean(reply, 0);
580 pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
581 pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
582 pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
583 pa_tagstruct_putu32(reply, 0);
584 pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
585 pa_tagstruct_put_cvolume(reply, &volume);
587 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
588 pa_tagstruct_putu32(reply, tag = u->ctag++);
589 pa_tagstruct_puts(reply, name);
590 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
591 pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
592 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
593 pa_tagstruct_puts(reply, u->source_name);
594 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
595 pa_tagstruct_put_boolean(reply, 0);
596 pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
599 pa_pstream_send_tagstruct(u->pstream, reply);
600 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
603 static void pstream_die_callback(pa_pstream *p, void *userdata) {
604 struct userdata *u = userdata;
607 pa_log(__FILE__": stream died.");
612 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const void*creds, void *userdata) {
613 struct userdata *u = userdata;
614 assert(p && packet && u);
616 if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
617 pa_log(__FILE__": invalid packet");
623 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) {
624 struct userdata *u = userdata;
625 assert(p && chunk && u);
627 if (channel != u->channel) {
628 pa_log(__FILE__": recieved memory block on bad channel.");
633 pa_source_post(u->source, chunk);
637 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
638 struct userdata *u = userdata;
641 assert(sc && u && u->client == sc);
643 pa_socket_client_unref(u->client);
647 pa_log(__FILE__": connection failed.");
648 pa_module_unload_request(u->module);
652 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
653 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
655 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
656 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
658 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
661 t = pa_tagstruct_new(NULL, 0);
662 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
663 pa_tagstruct_putu32(t, tag = u->ctag++);
664 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
665 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
666 pa_pstream_send_tagstruct(u->pstream, t);
667 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
672 static void sink_notify(pa_sink*sink) {
674 assert(sink && sink->userdata);
680 static pa_usec_t sink_get_latency(pa_sink *sink) {
684 assert(sink && sink->userdata);
689 if (l > u->requested_bytes) {
690 l -= u->requested_bytes;
691 usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
694 usec += u->host_latency;
699 static int sink_get_hw_volume(pa_sink *sink) {
701 assert(sink && sink->userdata);
707 static int sink_set_hw_volume(pa_sink *sink) {
711 assert(sink && sink->userdata);
714 t = pa_tagstruct_new(NULL, 0);
715 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_VOLUME);
716 pa_tagstruct_putu32(t, tag = u->ctag++);
718 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
719 pa_tagstruct_puts(t, u->sink_name);
720 pa_tagstruct_put_cvolume(t, &sink->hw_volume);
721 pa_pstream_send_tagstruct(u->pstream, t);
726 static int sink_get_hw_mute(pa_sink *sink) {
728 assert(sink && sink->userdata);
734 static int sink_set_hw_mute(pa_sink *sink) {
738 assert(sink && sink->userdata);
741 t = pa_tagstruct_new(NULL, 0);
742 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_MUTE);
743 pa_tagstruct_putu32(t, tag = u->ctag++);
745 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
746 pa_tagstruct_puts(t, u->sink_name);
747 pa_tagstruct_put_boolean(t, !!sink->hw_muted);
748 pa_pstream_send_tagstruct(u->pstream, t);
753 static pa_usec_t source_get_latency(pa_source *source) {
755 assert(source && source->userdata);
756 u = source->userdata;
758 return u->host_latency;
761 static int source_get_hw_volume(pa_source *source) {
763 assert(source && source->userdata);
764 u = source->userdata;
769 static int source_set_hw_volume(pa_source *source) {
773 assert(source && source->userdata);
774 u = source->userdata;
776 t = pa_tagstruct_new(NULL, 0);
777 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_VOLUME);
778 pa_tagstruct_putu32(t, tag = u->ctag++);
780 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
781 pa_tagstruct_puts(t, u->source_name);
782 pa_tagstruct_put_cvolume(t, &source->hw_volume);
783 pa_pstream_send_tagstruct(u->pstream, t);
788 static int source_get_hw_mute(pa_source *source) {
790 assert(source && source->userdata);
791 u = source->userdata;
796 static int source_set_hw_mute(pa_source *source) {
800 assert(source && source->userdata);
801 u = source->userdata;
803 t = pa_tagstruct_new(NULL, 0);
804 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_MUTE);
805 pa_tagstruct_putu32(t, tag = u->ctag++);
807 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
808 pa_tagstruct_puts(t, u->source_name);
809 pa_tagstruct_put_boolean(t, !!source->hw_muted);
810 pa_pstream_send_tagstruct(u->pstream, t);
816 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
817 struct userdata *u = userdata;
823 pa_gettimeofday(&ntv);
824 ntv.tv_sec += LATENCY_INTERVAL;
825 m->time_restart(e, &ntv);
828 static int load_key(struct userdata *u, const char*fn) {
831 u->auth_cookie_in_property = 0;
833 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
834 pa_log_debug(__FILE__": using already loaded auth cookie.");
835 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
836 u->auth_cookie_in_property = 1;
841 fn = PA_NATIVE_COOKIE_FILE;
843 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
846 pa_log_debug(__FILE__": loading cookie from disk.");
848 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
849 u->auth_cookie_in_property = 1;
854 int pa__init(pa_core *c, pa_module*m) {
855 pa_modargs *ma = NULL;
856 struct userdata *u = NULL;
862 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
863 pa_log(__FILE__": failed to parse module arguments");
867 u = pa_xmalloc(sizeof(struct userdata));
874 u->server_name = NULL;
876 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
878 u->requested_bytes = 0;
880 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
884 u->device_index = u->channel = PA_INVALID_INDEX;
886 u->auth_cookie_in_property = 0;
887 u->time_event = NULL;
889 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
892 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
893 pa_log(__FILE__": no server specified.");
897 ss = c->default_sample_spec;
898 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
899 pa_log(__FILE__": invalid sample format specification");
903 if (!(u->client = pa_socket_client_new_string(c->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
904 pa_log(__FILE__": failed to connect to server '%s'", u->server_name);
911 pa_socket_client_set_callback(u->client, on_connection, u);
914 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
915 pa_log(__FILE__": failed to create sink.");
919 u->sink->notify = sink_notify;
920 u->sink->get_latency = sink_get_latency;
921 u->sink->get_hw_volume = sink_get_hw_volume;
922 u->sink->set_hw_volume = sink_set_hw_volume;
923 u->sink->get_hw_mute = sink_get_hw_mute;
924 u->sink->set_hw_mute = sink_set_hw_mute;
925 u->sink->userdata = u;
926 u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
928 pa_sink_set_owner(u->sink, m);
930 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
931 pa_log(__FILE__": failed to create source.");
935 u->source->get_latency = source_get_latency;
936 u->source->get_hw_volume = source_get_hw_volume;
937 u->source->set_hw_volume = source_set_hw_volume;
938 u->source->get_hw_mute = source_get_hw_mute;
939 u->source->set_hw_mute = source_set_hw_mute;
940 u->source->userdata = u;
941 u->source->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->source_name ? u->source_name : "", u->source_name ? "@" : "", u->server_name);
943 pa_source_set_owner(u->source, m);
946 pa_gettimeofday(&ntv);
947 ntv.tv_sec += LATENCY_INTERVAL;
948 u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
962 void pa__done(pa_core *c, pa_module*m) {
966 if (!(u = m->userdata))
971 if (u->auth_cookie_in_property)
972 pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
975 pa_xfree(u->sink_name);
977 pa_xfree(u->source_name);
979 pa_xfree(u->server_name);