]> code.delx.au - pulseaudio/blobdiff - src/modules/rtp/module-rtp-send.c
rtp-send: Add "inhibit_auto_suspend" module argument
[pulseaudio] / src / modules / rtp / module-rtp-send.c
index 00cc6c919ad8b1f8e3df1bf223164a9f154769c1..0cc595b287028c774c4d768a9325659f9533153e 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_ip=<destination IP address> "
+        "source_ip=<source IP address> "
         "port=<port number> "
         "mtu=<maximum transfer unit> "
         "loop=<loopback to local host?> "
-        "ttl=<ttl value>"
+        "ttl=<ttl value> "
+        "inhibit_auto_suspend=<always|never|only_with_non_monitor_sources>"
 );
 
 #define DEFAULT_PORT 46000
 #define DEFAULT_TTL 1
 #define SAP_PORT 9875
+#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
@@ -85,13 +88,21 @@ static const char* const valid_modargs[] = {
     "rate",
     "destination", /* Compatbility */
     "destination_ip",
+    "source_ip",
     "port",
     "mtu" ,
     "loop",
     "ttl",
+    "inhibit_auto_suspend",
     NULL
 };
 
+enum inhibit_auto_suspend {
+    INHIBIT_AUTO_SUSPEND_ALWAYS,
+    INHIBIT_AUTO_SUSPEND_NEVER,
+    INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES
+};
+
 struct userdata {
     pa_module *module;
 
@@ -103,6 +114,8 @@ struct userdata {
     size_t mtu;
 
     pa_time_event *sap_event;
+
+    enum inhibit_auto_suspend inhibit_auto_suspend;
 };
 
 /* Called from I/O thread context */
@@ -123,7 +136,7 @@ static int source_output_process_msg(pa_msgobject *o, int code, void *data, int6
 }
 
 /* Called from I/O thread context */
-static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
+static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
     struct userdata *u;
     pa_source_output_assert_ref(o);
     pa_assert_se(u = o->userdata);
@@ -136,13 +149,46 @@ static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
     pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
 }
 
+static pa_source_output_flags_t get_dont_inhibit_auto_suspend_flag(pa_source *source,
+                                                                   enum inhibit_auto_suspend inhibit_auto_suspend) {
+    pa_assert(source);
+
+    switch (inhibit_auto_suspend) {
+        case INHIBIT_AUTO_SUSPEND_ALWAYS:
+            return 0;
+
+        case INHIBIT_AUTO_SUSPEND_NEVER:
+            return PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
+
+        case INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES:
+            return source->monitor_of ? 0 : PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
+    }
+
+    pa_assert_not_reached();
+}
+
+/* Called from the main thread. */
+static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
+    struct userdata *u;
+
+    pa_assert(o);
+
+    u = o->userdata;
+
+    if (!dest)
+        return;
+
+    o->flags &= ~PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
+    o->flags |= get_dont_inhibit_auto_suspend_flag(dest, u->inhibit_auto_suspend);
+}
+
 /* Called from main context */
-static void source_output_kill(pa_source_output* o) {
+static void source_output_kill_cb(pa_source_output* o) {
     struct userdata *u;
     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);
@@ -165,6 +211,7 @@ int pa__init(pa_module*m) {
     struct userdata *u;
     pa_modargs *ma = NULL;
     const char *dst_addr;
+    const char *src_addr;
     uint32_t port = DEFAULT_PORT, mtu;
     uint32_t ttl = DEFAULT_TTL;
     sa_family_t af;
@@ -172,9 +219,9 @@ int pa__init(pa_module*m) {
     pa_source *s;
     pa_sample_spec ss;
     pa_channel_map cm;
-    struct sockaddr_in dst_sa4, dst_sap_sa4;
+    struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4;
 #ifdef HAVE_IPV6
-    struct sockaddr_in6 dst_sa6, dst_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;
@@ -183,7 +230,9 @@ int pa__init(pa_module*m) {
     int r, j;
     socklen_t k;
     char hn[128], *n;
-    pa_bool_t loop = FALSE;
+    bool loop = false;
+    enum inhibit_auto_suspend inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
+    const char *inhibit_auto_suspend_str;
     pa_source_output_new_data data;
 
     pa_assert(m);
@@ -203,6 +252,19 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    if ((inhibit_auto_suspend_str = pa_modargs_get_value(ma, "inhibit_auto_suspend", NULL))) {
+        if (pa_streq(inhibit_auto_suspend_str, "always"))
+            inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ALWAYS;
+        else if (pa_streq(inhibit_auto_suspend_str, "never"))
+            inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_NEVER;
+        else if (pa_streq(inhibit_auto_suspend_str, "only_with_non_monitor_sources"))
+            inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
+        else {
+            pa_log("Failed to parse the \"inhibit_auto_suspend\" parameter.");
+            goto fail;
+        }
+    }
+
     ss = s->sample_spec;
     pa_rtp_sample_spec_fixup(&ss);
     cm = s->channel_map;
@@ -242,6 +304,26 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP);
+
+    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, 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 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);
@@ -249,12 +331,15 @@ int pa__init(pa_module*m) {
     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
@@ -268,6 +353,16 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    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;
@@ -283,6 +378,16 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    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;
@@ -320,16 +425,17 @@ 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.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;
+    data.flags |= get_dont_inhibit_auto_suspend_flag(s, inhibit_auto_suspend);
 
     pa_source_output_new(&o, m->core, &data);
     pa_source_output_new_data_done(&data);
@@ -340,8 +446,9 @@ int pa__init(pa_module*m) {
     }
 
     o->parent.process_msg = source_output_process_msg;
-    o->push = source_output_push;
-    o->kill = source_output_kill;
+    o->push = source_output_push_cb;
+    o->moving = source_output_moving_cb;
+    o->kill = source_output_kill_cb;
 
     pa_log_info("Configured source latency of %llu ms.",
                 (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC);
@@ -387,12 +494,13 @@ 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, dst_addr, 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);
 
     u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u);
+    u->inhibit_auto_suspend = inhibit_auto_suspend;
 
     pa_source_output_put(u->source_output);