]> code.delx.au - pulseaudio/blobdiff - src/modules/rtp/module-rtp-send.c
modules: Fix uninitialized variable in rtp-send
[pulseaudio] / src / modules / rtp / module-rtp-send.c
index 7131629cd8e7915c5bb833cfd650ce29c326bef7..14f00b39dd3fbbbe154fb538a9cedc93477965cc 100644 (file)
 PA_MODULE_AUTHOR("Lennart Poettering");
 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP");
 PA_MODULE_VERSION(PACKAGE_VERSION);
-PA_MODULE_LOAD_ONCE(FALSE);
+PA_MODULE_LOAD_ONCE(false);
 PA_MODULE_USAGE(
         "source=<name of the source> "
         "format=<sample format> "
         "channels=<number of channels> "
         "rate=<sample rate> "
-        "destination=<destination IP address> "
+        "destination_ip=<destination IP address> "
+        "source_ip=<source IP address> "
         "port=<port number> "
         "mtu=<maximum transfer unit> "
         "loop=<loopback to local host?> "
@@ -73,7 +74,8 @@ PA_MODULE_USAGE(
 #define DEFAULT_PORT 46000
 #define DEFAULT_TTL 1
 #define SAP_PORT 9875
-#define DEFAULT_DESTINATION "224.0.0.56"
+#define DEFAULT_SOURCE_IP "0.0.0.0"
+#define DEFAULT_DESTINATION_IP "224.0.0.56"
 #define MEMBLOCKQ_MAXLENGTH (1024*170)
 #define DEFAULT_MTU 1280
 #define SAP_INTERVAL (5*PA_USEC_PER_SEC)
@@ -83,7 +85,9 @@ static const char* const valid_modargs[] = {
     "format",
     "channels",
     "rate",
-    "destination",
+    "destination", /* Compatbility */
+    "destination_ip",
+    "source_ip",
     "port",
     "mtu" ,
     "loop",
@@ -141,7 +145,7 @@ static void source_output_kill(pa_source_output* o) {
     pa_source_output_assert_ref(o);
     pa_assert_se(u = o->userdata);
 
-    pa_module_unload_request(u->module, TRUE);
+    pa_module_unload_request(u->module, true);
 
     pa_source_output_unlink(u->source_output);
     pa_source_output_unref(u->source_output);
@@ -163,7 +167,8 @@ static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct time
 int pa__init(pa_module*m) {
     struct userdata *u;
     pa_modargs *ma = NULL;
-    const char *dest;
+    const char *dst_addr;
+    const char *src_addr;
     uint32_t port = DEFAULT_PORT, mtu;
     uint32_t ttl = DEFAULT_TTL;
     sa_family_t af;
@@ -171,9 +176,9 @@ int pa__init(pa_module*m) {
     pa_source *s;
     pa_sample_spec ss;
     pa_channel_map cm;
-    struct sockaddr_in sa4, sap_sa4;
+    struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4;
 #ifdef HAVE_IPV6
-    struct sockaddr_in6 sa6, sap_sa6;
+    struct sockaddr_in6 dst_sa6, dst_sap_sa6, src_sa6, src_sap_sa6;
 #endif
     struct sockaddr_storage sa_dst;
     pa_source_output *o = NULL;
@@ -182,7 +187,7 @@ int pa__init(pa_module*m) {
     int r, j;
     socklen_t k;
     char hn[128], *n;
-    pa_bool_t loop = FALSE;
+    bool loop = false;
     pa_source_output_new_data data;
 
     pa_assert(m);
@@ -241,22 +246,47 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
+    src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP);
 
-    if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
-        sa4.sin_family = af = AF_INET;
-        sa4.sin_port = htons((uint16_t) port);
-        sap_sa4 = sa4;
-        sap_sa4.sin_port = htons(SAP_PORT);
+    if (inet_pton(AF_INET, src_addr, &src_sa4.sin_addr) > 0) {
+        src_sa4.sin_family = af = AF_INET;
+        src_sa4.sin_port = htons(0);
+        memset(&src_sa4.sin_zero, 0, sizeof(src_sa4.sin_zero));
+        src_sap_sa4 = src_sa4;
 #ifdef HAVE_IPV6
-    } else if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
-        sa6.sin6_family = af = AF_INET6;
-        sa6.sin6_port = htons((uint16_t) port);
-        sap_sa6 = sa6;
-        sap_sa6.sin6_port = htons(SAP_PORT);
+    } else if (inet_pton(AF_INET6, src_addr, &src_sa6.sin6_addr) > 0) {
+        src_sa6.sin6_family = af = AF_INET6;
+        src_sa6.sin6_port = htons(0);
+        src_sa6.sin6_flowinfo = 0;
+        src_sa6.sin6_scope_id = 0;
+        src_sap_sa6 = src_sa6;
 #endif
     } else {
-        pa_log("Invalid destination '%s'", dest);
+        pa_log("Invalid source address '%s'", src_addr);
+        goto fail;
+    }
+
+    dst_addr = pa_modargs_get_value(ma, "destination", NULL);
+    if (dst_addr == NULL)
+        dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP);
+
+    if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) {
+        dst_sa4.sin_family = af = AF_INET;
+        dst_sa4.sin_port = htons((uint16_t) port);
+        memset(&dst_sa4.sin_zero, 0, sizeof(dst_sa4.sin_zero));
+        dst_sap_sa4 = dst_sa4;
+        dst_sap_sa4.sin_port = htons(SAP_PORT);
+#ifdef HAVE_IPV6
+    } else if (inet_pton(AF_INET6, dst_addr, &dst_sa6.sin6_addr) > 0) {
+        dst_sa6.sin6_family = af = AF_INET6;
+        dst_sa6.sin6_port = htons((uint16_t) port);
+        dst_sa6.sin6_flowinfo = 0;
+        dst_sa6.sin6_scope_id = 0;
+        dst_sap_sa6 = dst_sa6;
+        dst_sap_sa6.sin6_port = htons(SAP_PORT);
+#endif
+    } else {
+        pa_log("Invalid destination '%s'", dst_addr);
         goto fail;
     }
 
@@ -265,11 +295,21 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    if (af == AF_INET && connect(fd, (struct sockaddr*) &sa4, sizeof(sa4)) < 0) {
+    if (af == AF_INET && bind(fd, (struct sockaddr*) &src_sa4, sizeof(src_sa4)) < 0) {
+        pa_log("bind() failed: %s", pa_cstrerror(errno));
+        goto fail;
+#ifdef HAVE_IPV6
+    } else if (af == AF_INET6 && bind(fd, (struct sockaddr*) &src_sa6, sizeof(src_sa6)) < 0) {
+        pa_log("bind() failed: %s", pa_cstrerror(errno));
+        goto fail;
+#endif
+    }
+
+    if (af == AF_INET && connect(fd, (struct sockaddr*) &dst_sa4, sizeof(dst_sa4)) < 0) {
         pa_log("connect() failed: %s", pa_cstrerror(errno));
         goto fail;
 #ifdef HAVE_IPV6
-    } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &sa6, sizeof(sa6)) < 0) {
+    } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &dst_sa6, sizeof(dst_sa6)) < 0) {
         pa_log("connect() failed: %s", pa_cstrerror(errno));
         goto fail;
 #endif
@@ -280,11 +320,21 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &sap_sa4, sizeof(sap_sa4)) < 0) {
+    if (af == AF_INET && bind(sap_fd, (struct sockaddr*) &src_sap_sa4, sizeof(src_sap_sa4)) < 0) {
+        pa_log("bind() failed: %s", pa_cstrerror(errno));
+        goto fail;
+#ifdef HAVE_IPV6
+    } else if (af == AF_INET6 && bind(sap_fd, (struct sockaddr*) &src_sap_sa6, sizeof(src_sap_sa6)) < 0) {
+        pa_log("bind() failed: %s", pa_cstrerror(errno));
+        goto fail;
+#endif
+    }
+
+    if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &dst_sap_sa4, sizeof(dst_sap_sa4)) < 0) {
         pa_log("connect() failed: %s", pa_cstrerror(errno));
         goto fail;
 #ifdef HAVE_IPV6
-    } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &sap_sa6, sizeof(sap_sa6)) < 0) {
+    } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &dst_sap_sa6, sizeof(dst_sap_sa6)) < 0) {
         pa_log("connect() failed: %s", pa_cstrerror(errno));
         goto fail;
 #endif
@@ -317,16 +367,16 @@ int pa__init(pa_module*m) {
 
     pa_source_output_new_data_init(&data);
     pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream");
-    pa_proplist_sets(data.proplist, "rtp.destination", dest);
+    pa_proplist_sets(data.proplist, "rtp.source", src_addr);
+    pa_proplist_sets(data.proplist, "rtp.destination", dst_addr);
     pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu);
     pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port);
     pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl);
     data.driver = __FILE__;
     data.module = m;
-    pa_source_output_new_data_set_source(&data, s, FALSE);
+    pa_source_output_new_data_set_source(&data, s, false);
     pa_source_output_new_data_set_sample_spec(&data, &ss);
     pa_source_output_new_data_set_channel_map(&data, &cm);
-    data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
 
     pa_source_output_new(&o, m->core, &data);
     pa_source_output_new_data_done(&data);
@@ -348,10 +398,11 @@ int pa__init(pa_module*m) {
     u->source_output = o;
 
     u->memblockq = pa_memblockq_new(
+            "module-rtp-send memblockq",
             0,
             MEMBLOCKQ_MAXLENGTH,
             MEMBLOCKQ_MAXLENGTH,
-            pa_frame_size(&ss),
+            &ss,
             1,
             0,
             0,
@@ -367,13 +418,13 @@ int pa__init(pa_module*m) {
     if (af == AF_INET) {
         p = pa_sdp_build(af,
                      (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr,
-                     (void*) &sa4.sin_addr,
+                     (void*) &dst_sa4.sin_addr,
                      n, (uint16_t) port, payload, &ss);
 #ifdef HAVE_IPV6
     } else {
         p = pa_sdp_build(af,
                      (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
-                     (void*) &sa6.sin6_addr,
+                     (void*) &dst_sa6.sin6_addr,
                      n, (uint16_t) port, payload, &ss);
 #endif
     }
@@ -383,7 +434,7 @@ int pa__init(pa_module*m) {
     pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
     pa_sap_context_init_send(&u->sap_context, sap_fd, p);
 
-    pa_log_info("RTP stream initialized with mtu %u on %s:%u ttl=%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, ttl, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
+    pa_log_info("RTP stream initialized with mtu %u on %s:%u from %s ttl=%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dst_addr, port, src_addr, ttl, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
     pa_log_info("SDP-Data:\n%s\nEOF", p);
 
     pa_sap_send(&u->sap_context, 0);