]> code.delx.au - pulseaudio/blobdiff - src/modules/module-tunnel.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-tunnel.c
index b68bc485f99f740a411902a4eb081a520436037f..b96d46b31302997c17ed68ff8b343ed67289a0d5 100644 (file)
@@ -1,20 +1,23 @@
 /* $Id$ */
 
 /***
-  This file is part of polypaudio.
-  polypaudio is free software; you can redistribute it and/or modify
+  This file is part of PulseAudio.
+
+  Copyright 2004-2006 Lennart Poettering
+  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
+
+  PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
-  polypaudio is distributed in the hope that it will be useful, but
+
+  PulseAudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public License
-  along with polypaudio; if not, write to the Free Software
+  along with PulseAudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
 ***/
 #include <stdio.h>
 #include <stdlib.h>
 
-#include <polyp/version.h>
-
-#include <polypcore/module.h>
-#include <polypcore/util.h>
-#include <polypcore/modargs.h>
-#include <polypcore/log.h>
-#include <polypcore/core-subscribe.h>
-#include <polypcore/xmalloc.h>
-#include <polypcore/sink-input.h>
-#include <polypcore/pdispatch.h>
-#include <polypcore/pstream.h>
-#include <polypcore/pstream-util.h>
-#include <polypcore/authkey.h>
-#include <polypcore/socket-client.h>
-#include <polypcore/socket-util.h>
-#include <polypcore/authkey-prop.h>
+#include <pulse/timeval.h>
+#include <pulse/util.h>
+#include <pulse/version.h>
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-subscribe.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/pdispatch.h>
+#include <pulsecore/pstream.h>
+#include <pulsecore/pstream-util.h>
+#include <pulsecore/authkey.h>
+#include <pulsecore/socket-client.h>
+#include <pulsecore/socket-util.h>
+#include <pulsecore/authkey-prop.h>
 
 #ifdef TUNNEL_SINK
 #include "module-tunnel-sink-symdef.h"
 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
-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>")
+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> "
+        "channel_map=<channel map>")
 #else
 #include "module-tunnel-source-symdef.h"
 PA_MODULE_DESCRIPTION("Tunnel module for sources")
-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>")
+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> "
+        "channel_map=<channel map>")
 #endif
 
 PA_MODULE_AUTHOR("Lennart Poettering")
 PA_MODULE_VERSION(PACKAGE_VERSION)
 
-#define DEFAULT_SINK_NAME "tunnel"
-#define DEFAULT_SOURCE_NAME "tunnel"
-
 #define DEFAULT_TLENGTH (44100*2*2/10)  //(10240*8)
 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
 #define DEFAULT_MINREQ 512
@@ -87,6 +105,7 @@ static const char* const valid_modargs[] = {
     "source_name",
     "source",
 #endif
+    "channel_map",
     NULL,
 };
 
@@ -100,10 +119,10 @@ static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa
 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
 #ifdef TUNNEL_SINK
     [PA_COMMAND_REQUEST] = command_request,
-#endif    
+#endif
     [PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
     [PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
-    [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event, 
+    [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
 };
 
 struct userdata {
@@ -120,7 +139,7 @@ struct userdata {
     char *source_name;
     pa_source *source;
 #endif
-    
+
     pa_module *module;
     pa_core *core;
 
@@ -130,7 +149,7 @@ struct userdata {
     uint32_t ctag;
     uint32_t device_index;
     uint32_t channel;
-    
+
     pa_usec_t host_latency;
 
     pa_time_event *time_event;
@@ -140,7 +159,7 @@ struct userdata {
 
 static void close_stuff(struct userdata *u) {
     assert(u);
-    
+
     if (u->pstream) {
         pa_pstream_close(u->pstream);
         pa_pstream_unref(u->pstream);
@@ -187,7 +206,7 @@ static void command_stream_killed(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t comma
     struct userdata *u = userdata;
     assert(pd && t && u && u->pdispatch == pd);
 
-    pa_log(__FILE__": stream killed");
+    pa_log("stream killed");
     die(u);
 }
 
@@ -204,7 +223,7 @@ static void command_subscribe_event(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t com
     if (pa_tagstruct_getu32(t, &e) < 0 ||
         pa_tagstruct_getu32(t, &idx) < 0 ||
         !pa_tagstruct_eof(t)) {
-        pa_log(__FILE__": invalid protocol reply");
+        pa_log("invalid protocol reply");
         die(u);
         return;
     }
@@ -240,16 +259,18 @@ static void send_bytes(struct userdata *u) {
     while (u->requested_bytes > 0) {
         pa_memchunk chunk;
         if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
-            
-            if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF) 
+
+            if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
                 send_prebuf_request(u);
-            
+
             return;
         }
 
         pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, &chunk);
         pa_memblock_unref(chunk.memblock);
 
+/*         pa_log("sent %lu", (unsigned long) chunk.length); */
+
         if (chunk.length > u->requested_bytes)
             u->requested_bytes = 0;
         else
@@ -265,17 +286,17 @@ static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED ui
     if (pa_tagstruct_getu32(t, &channel) < 0 ||
         pa_tagstruct_getu32(t, &bytes) < 0 ||
         !pa_tagstruct_eof(t)) {
-        pa_log(__FILE__": invalid protocol reply");
+        pa_log("invalid protocol reply");
         die(u);
         return;
     }
 
     if (channel != u->channel) {
-        pa_log(__FILE__": recieved data for invalid channel");
+        pa_log("recieved data for invalid channel");
         die(u);
         return;
     }
-    
+
     u->requested_bytes += bytes;
     send_bytes(u);
 }
@@ -292,13 +313,13 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_G
 
     if (command != PA_COMMAND_REPLY) {
         if (command == PA_COMMAND_ERROR)
-            pa_log(__FILE__": failed to get latency.");
+            pa_log("failed to get latency.");
         else
-            pa_log(__FILE__": protocol error.");
+            pa_log("protocol error.");
         die(u);
         return;
     }
-    
+
     if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
         pa_tagstruct_get_usec(t, &source_usec) < 0 ||
         pa_tagstruct_get_boolean(t, &playing) < 0 ||
@@ -307,20 +328,22 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_G
         pa_tagstruct_gets64(t, &write_index) < 0 ||
         pa_tagstruct_gets64(t, &read_index) < 0 ||
         !pa_tagstruct_eof(t)) {
-        pa_log(__FILE__": invalid reply. (latency)");
+        pa_log("invalid reply. (latency)");
         die(u);
         return;
     }
 
     pa_gettimeofday(&now);
 
+    /* FIXME! This could use some serious love. */
+
     if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
         /* local and remote seem to have synchronized clocks */
 #ifdef TUNNEL_SINK
         transport_usec = pa_timeval_diff(&remote, &local);
 #else
         transport_usec = pa_timeval_diff(&now, &remote);
-#endif    
+#endif
     } else
         transport_usec = pa_timeval_diff(&now, &local)/2;
 
@@ -334,7 +357,7 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_G
         u->host_latency = 0;
 #endif
 
-/*     pa_log(__FILE__": estimated host latency: %0.0f usec", (double) u->host_latency); */
+/*     pa_log("estimated host latency: %0.0f usec", (double) u->host_latency);  */
 }
 
 static void request_latency(struct userdata *u) {
@@ -344,7 +367,7 @@ static void request_latency(struct userdata *u) {
     assert(u);
 
     t = pa_tagstruct_new(NULL, 0);
-#ifdef TUNNEL_SINK    
+#ifdef TUNNEL_SINK
     pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
 #else
     pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
@@ -354,9 +377,9 @@ static void request_latency(struct userdata *u) {
 
     pa_gettimeofday(&now);
     pa_tagstruct_put_timeval(t, &now);
-    
+
     pa_pstream_send_tagstruct(u->pstream, t);
-    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u);
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
 }
 
 static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
@@ -373,9 +396,9 @@ static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_
 
     if (command != PA_COMMAND_REPLY) {
         if (command == PA_COMMAND_ERROR)
-            pa_log(__FILE__": failed to get info.");
+            pa_log("failed to get info.");
         else
-            pa_log(__FILE__": protocol error.");
+            pa_log("protocol error.");
         die(u);
         return;
     }
@@ -394,7 +417,7 @@ static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_
         pa_tagstruct_gets(t, &driver) < 0 ||
         pa_tagstruct_getu32(t, &flags) < 0 ||
         !pa_tagstruct_eof(t)) {
-        pa_log(__FILE__": invalid reply. (get_info)");
+        pa_log("invalid reply. (get_info)");
         die(u);
         return;
     }
@@ -449,7 +472,7 @@ static void request_info(struct userdata *u) {
 #endif
 
     pa_pstream_send_tagstruct(u->pstream, t);
-    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u);
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u, NULL);
 }
 
 static void start_subscribe(struct userdata *u) {
@@ -470,37 +493,79 @@ static void start_subscribe(struct userdata *u) {
     pa_pstream_send_tagstruct(u->pstream, t);
 }
 
+static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
+    struct userdata *u = userdata;
+    struct timeval ntv;
+    assert(m && e && u);
+
+    request_latency(u);
+
+    pa_gettimeofday(&ntv);
+    ntv.tv_sec += LATENCY_INTERVAL;
+    m->time_restart(e, &ntv);
+}
+
 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
     struct userdata *u = userdata;
+    struct timeval ntv;
     assert(pd && u && u->pdispatch == pd);
 
     if (command != PA_COMMAND_REPLY) {
         if (command == PA_COMMAND_ERROR)
-            pa_log(__FILE__": failed to create stream.");
+            pa_log("failed to create stream.");
         else
-            pa_log(__FILE__": protocol error.");
+            pa_log("protocol error.");
         die(u);
         return;
     }
 
     if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
-        pa_tagstruct_getu32(t, &u->device_index) < 0 ||
-#ifdef TUNNEL_SINK        
-        pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
-#endif        
-        !pa_tagstruct_eof(t)) {
-        pa_log(__FILE__": invalid reply. (create stream)");
-        die(u);
-        return;
+        pa_tagstruct_getu32(t, &u->device_index) < 0
+#ifdef TUNNEL_SINK
+        || pa_tagstruct_getu32(t, &u->requested_bytes) < 0
+#endif
+        )
+        goto parse_error;
+
+    if (u->version >= 9) {
+#ifdef TUNNEL_SINK
+        uint32_t maxlength, tlength, prebuf, minreq;
+
+        if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
+            pa_tagstruct_getu32(t, &tlength) < 0 ||
+            pa_tagstruct_getu32(t, &prebuf) < 0 ||
+            pa_tagstruct_getu32(t, &minreq) < 0)
+            goto parse_error;
+#else
+        uint32_t maxlength, fragsize;
+
+        if (pa_tagstruct_getu32(t, &maxlength) < 0 ||
+            pa_tagstruct_getu32(t, &fragsize) < 0)
+            goto parse_error;
+#endif
     }
 
+    if (!pa_tagstruct_eof(t))
+        goto parse_error;
+
     start_subscribe(u);
     request_info(u);
 
+    assert(!u->time_event);
+    pa_gettimeofday(&ntv);
+    ntv.tv_sec += LATENCY_INTERVAL;
+    u->time_event = u->core->mainloop->time_new(u->core->mainloop, &ntv, timeout_callback, u);
+
     request_latency(u);
 #ifdef TUNNEL_SINK
     send_bytes(u);
 #endif
+
+    return;
+
+parse_error:
+    pa_log("invalid reply. (create stream)");
+    die(u);
 }
 
 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
@@ -516,32 +581,32 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
         pa_tagstruct_getu32(t, &u->version) < 0 ||
         !pa_tagstruct_eof(t)) {
         if (command == PA_COMMAND_ERROR)
-            pa_log(__FILE__": failed to authenticate");
+            pa_log("failed to authenticate");
         else
-            pa_log(__FILE__": protocol error.");
+            pa_log("protocol error.");
         die(u);
         return;
     }
 
     /* Minimum supported protocol version */
     if (u->version < 8) {
-        pa_log(__FILE__": incompatible protocol version");
+        pa_log("incompatible protocol version");
         die(u);
         return;
     }
 
 #ifdef TUNNEL_SINK
-    snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
+    pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, sink %s",
              pa_get_host_name(hn, sizeof(hn)),
              pa_get_user_name(un, sizeof(un)),
              u->sink->name);
 #else
-    snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', source '%s'",
+    pa_snprintf(name, sizeof(name), "Tunnel from host %s, user %s, source %s",
              pa_get_host_name(hn, sizeof(hn)),
              pa_get_user_name(un, sizeof(un)),
              u->source->name);
 #endif
-    
+
     reply = pa_tagstruct_new(NULL, 0);
     pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
     pa_tagstruct_putu32(reply, tag = u->ctag++);
@@ -550,7 +615,7 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
     /* We ignore the server's reply here */
 
     reply = pa_tagstruct_new(NULL, 0);
-#ifdef TUNNEL_SINK    
+#ifdef TUNNEL_SINK
     pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
     pa_tagstruct_putu32(reply, tag = u->ctag++);
     pa_tagstruct_puts(reply, name);
@@ -578,26 +643,25 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
     pa_tagstruct_put_boolean(reply, 0);
     pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
 #endif
-    
+
     pa_pstream_send_tagstruct(u->pstream, reply);
-    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u);
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
 }
 
 static void pstream_die_callback(pa_pstream *p, void *userdata) {
     struct userdata *u = userdata;
     assert(p && u);
 
-    pa_log(__FILE__": stream died.");
+    pa_log("stream died.");
     die(u);
 }
 
-
-static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const void*creds, void *userdata) {
+static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
     struct userdata *u = userdata;
     assert(p && packet && u);
 
     if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
-        pa_log(__FILE__": invalid packet");
+        pa_log("invalid packet");
         die(u);
     }
 }
@@ -608,11 +672,11 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, PA_GCC_UN
     assert(p && chunk && u);
 
     if (channel != u->channel) {
-        pa_log(__FILE__": recieved memory block on bad channel.");
+        pa_log("recieved memory block on bad channel.");
         die(u);
         return;
     }
-    
+
     pa_source_post(u->source, chunk);
 }
 #endif
@@ -625,14 +689,14 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata
 
     pa_socket_client_unref(u->client);
     u->client = NULL;
-    
+
     if (!io) {
-        pa_log(__FILE__": connection failed.");
+        pa_log("connection failed.");
         pa_module_unload_request(u->module);
         return;
     }
 
-    u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
+    u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
     u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
 
     pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
@@ -640,15 +704,15 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata
 #ifndef TUNNEL_SINK
     pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
 #endif
-    
+
     t = pa_tagstruct_new(NULL, 0);
     pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
     pa_tagstruct_putu32(t, tag = u->ctag++);
     pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
     pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
     pa_pstream_send_tagstruct(u->pstream, t);
-    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u);
-    
+    pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
+
 }
 
 #ifdef TUNNEL_SINK
@@ -732,6 +796,7 @@ static int sink_set_hw_mute(pa_sink *sink) {
 
     return 0;
 }
+
 #else
 static pa_usec_t source_get_latency(pa_source *source) {
     struct userdata *u;
@@ -796,38 +861,26 @@ static int source_set_hw_mute(pa_source *source) {
 }
 #endif
 
-static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
-    struct userdata *u = userdata;
-    struct timeval ntv;
-    assert(m && e && u);
-
-    request_latency(u);
-    
-    pa_gettimeofday(&ntv);
-    ntv.tv_sec += LATENCY_INTERVAL;
-    m->time_restart(e, &ntv);
-}
-
 static int load_key(struct userdata *u, const char*fn) {
     assert(u);
 
     u->auth_cookie_in_property = 0;
-    
+
     if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
-        pa_log_debug(__FILE__": using already loaded auth cookie.");
+        pa_log_debug("using already loaded auth cookie.");
         pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
         u->auth_cookie_in_property = 1;
         return 0;
     }
-    
+
     if (!fn)
         fn = PA_NATIVE_COOKIE_FILE;
 
     if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
         return -1;
 
-    pa_log_debug(__FILE__": loading cookie from disk.");
-    
+    pa_log_debug("loading cookie from disk.");
+
     if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
         u->auth_cookie_in_property = 1;
 
@@ -838,11 +891,13 @@ int pa__init(pa_core *c, pa_module*m) {
     pa_modargs *ma = NULL;
     struct userdata *u = NULL;
     pa_sample_spec ss;
-    struct timeval ntv;
+    pa_channel_map map;
+    char *t, *dn = NULL;
+
     assert(c && m);
 
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
-        pa_log(__FILE__": failed to parse module arguments");
+        pa_log("failed to parse module arguments");
         goto fail;
     }
 
@@ -867,50 +922,59 @@ int pa__init(pa_core *c, pa_module*m) {
     u->host_latency = 0;
     u->auth_cookie_in_property = 0;
     u->time_event = NULL;
-    
+
     if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
         goto fail;
-    
+
     if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
-        pa_log(__FILE__": no server specified.");
+        pa_log("no server specified.");
         goto fail;
     }
 
     ss = c->default_sample_spec;
-    if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
-        pa_log(__FILE__": invalid sample format specification");
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+        pa_log("invalid sample format specification");
         goto fail;
     }
 
     if (!(u->client = pa_socket_client_new_string(c->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
-        pa_log(__FILE__": failed to connect to server '%s'", u->server_name);
+        pa_log("failed to connect to server '%s'", u->server_name);
         goto fail;
     }
-    
+
     if (!u->client)
         goto fail;
 
     pa_socket_client_set_callback(u->client, on_connection, u);
 
 #ifdef TUNNEL_SINK
-    if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
-        pa_log(__FILE__": failed to create sink.");
+
+    if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+        dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
+
+    if (!(u->sink = pa_sink_new(c, __FILE__, dn, 0, &ss, &map))) {
+        pa_log("failed to create sink.");
         goto fail;
     }
 
-    u->sink->notify = sink_notify;
     u->sink->get_latency = sink_get_latency;
     u->sink->get_hw_volume = sink_get_hw_volume;
     u->sink->set_hw_volume = sink_set_hw_volume;
     u->sink->get_hw_mute = sink_get_hw_mute;
     u->sink->set_hw_mute = sink_set_hw_mute;
+    u->sink->notify = sink_notify;
     u->sink->userdata = u;
-    u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
+    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));
+    pa_xfree(t);
 
     pa_sink_set_owner(u->sink, m);
 #else
-    if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL))) {
-        pa_log(__FILE__": failed to create source.");
+
+    if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
+        dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
+
+    if (!(u->source = pa_source_new(c, __FILE__, dn, 0, &ss, &map))) {
+        pa_log("failed to create source.");
         goto fail;
     }
 
@@ -920,24 +984,29 @@ int pa__init(pa_core *c, pa_module*m) {
     u->source->get_hw_mute = source_get_hw_mute;
     u->source->set_hw_mute = source_set_hw_mute;
     u->source->userdata = u;
-    u->source->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->source_name ? u->source_name : "", u->source_name ? "@" : "", u->server_name);
+
+    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));
+    pa_xfree(t);
 
     pa_source_set_owner(u->source, m);
 #endif
-    
-    pa_gettimeofday(&ntv);
-    ntv.tv_sec += LATENCY_INTERVAL;
-    u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
+
+    pa_xfree(dn);
+
+    u->time_event = NULL;
 
     pa_modargs_free(ma);
 
     return 0;
-    
+
 fail:
     pa__done(c, m);
 
     if (ma)
         pa_modargs_free(ma);
+
+    pa_xfree(dn);
+
     return  -1;
 }
 
@@ -952,7 +1021,7 @@ void pa__done(pa_core *c, pa_module*m) {
 
     if (u->auth_cookie_in_property)
         pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
-    
+
 #ifdef TUNNEL_SINK
     pa_xfree(u->sink_name);
 #else