2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/socket.h>
28 #include <netinet/in.h>
32 #include <pulse/rtclock.h>
33 #include <pulse/timeval.h>
34 #include <pulse/util.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/source-output.h>
41 #include <pulsecore/memblockq.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/namereg.h>
46 #include <pulsecore/sample-util.h>
47 #include <pulsecore/macro.h>
48 #include <pulsecore/socket-util.h>
49 #include <pulsecore/arpa-inet.h>
51 #include "module-rtp-send-symdef.h"
57 PA_MODULE_AUTHOR("Lennart Poettering");
58 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP");
59 PA_MODULE_VERSION(PACKAGE_VERSION);
60 PA_MODULE_LOAD_ONCE(false);
62 "source=<name of the source> "
63 "format=<sample format> "
64 "channels=<number of channels> "
66 "destination_ip=<destination IP address> "
67 "source_ip=<source IP address> "
69 "mtu=<maximum transfer unit> "
70 "loop=<loopback to local host?> "
72 "inhibit_auto_suspend=<always|never|only_with_non_monitor_sources>"
75 #define DEFAULT_PORT 46000
78 #define DEFAULT_SOURCE_IP "0.0.0.0"
79 #define DEFAULT_DESTINATION_IP "224.0.0.56"
80 #define MEMBLOCKQ_MAXLENGTH (1024*170)
81 #define DEFAULT_MTU 1280
82 #define SAP_INTERVAL (5*PA_USEC_PER_SEC)
84 static const char* const valid_modargs[] = {
89 "destination", /* Compatbility */
96 "inhibit_auto_suspend",
100 enum inhibit_auto_suspend {
101 INHIBIT_AUTO_SUSPEND_ALWAYS,
102 INHIBIT_AUTO_SUSPEND_NEVER,
103 INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES
109 pa_source_output *source_output;
110 pa_memblockq *memblockq;
112 pa_rtp_context rtp_context;
113 pa_sap_context sap_context;
116 pa_time_event *sap_event;
118 enum inhibit_auto_suspend inhibit_auto_suspend;
121 /* Called from I/O thread context */
122 static int source_output_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
124 pa_assert_se(u = PA_SOURCE_OUTPUT(o)->userdata);
127 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY:
128 *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->source_output->sample_spec);
130 /* Fall through, the default handler will add in the extra
131 * latency added by the resampler */
135 return pa_source_output_process_msg(o, code, data, offset, chunk);
138 /* Called from I/O thread context */
139 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
141 pa_source_output_assert_ref(o);
142 pa_assert_se(u = o->userdata);
144 if (pa_memblockq_push(u->memblockq, chunk) < 0) {
145 pa_log_warn("Failed to push chunk into memblockq.");
149 pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
152 static pa_source_output_flags_t get_dont_inhibit_auto_suspend_flag(pa_source *source,
153 enum inhibit_auto_suspend inhibit_auto_suspend) {
156 switch (inhibit_auto_suspend) {
157 case INHIBIT_AUTO_SUSPEND_ALWAYS:
160 case INHIBIT_AUTO_SUSPEND_NEVER:
161 return PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
163 case INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES:
164 return source->monitor_of ? 0 : PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
167 pa_assert_not_reached();
170 /* Called from the main thread. */
171 static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
181 o->flags &= ~PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
182 o->flags |= get_dont_inhibit_auto_suspend_flag(dest, u->inhibit_auto_suspend);
185 /* Called from main context */
186 static void source_output_kill_cb(pa_source_output* o) {
188 pa_source_output_assert_ref(o);
189 pa_assert_se(u = o->userdata);
191 pa_module_unload_request(u->module, true);
193 pa_source_output_unlink(u->source_output);
194 pa_source_output_unref(u->source_output);
195 u->source_output = NULL;
198 static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
199 struct userdata *u = userdata;
205 pa_sap_send(&u->sap_context, 0);
207 pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + SAP_INTERVAL);
210 int pa__init(pa_module*m) {
212 pa_modargs *ma = NULL;
213 const char *dst_addr;
214 const char *src_addr;
215 uint32_t port = DEFAULT_PORT, mtu;
216 uint32_t ttl = DEFAULT_TTL;
218 int fd = -1, sap_fd = -1;
222 struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4;
224 struct sockaddr_in6 dst_sa6, dst_sap_sa6, src_sa6, src_sap_sa6;
226 struct sockaddr_storage sa_dst;
227 pa_source_output *o = NULL;
234 enum inhibit_auto_suspend inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
235 const char *inhibit_auto_suspend_str;
236 pa_source_output_new_data data;
240 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
241 pa_log("Failed to parse module arguments");
245 if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
246 pa_log("Source does not exist.");
250 if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
251 pa_log("Failed to parse \"loop\" parameter.");
255 if ((inhibit_auto_suspend_str = pa_modargs_get_value(ma, "inhibit_auto_suspend", NULL))) {
256 if (pa_streq(inhibit_auto_suspend_str, "always"))
257 inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ALWAYS;
258 else if (pa_streq(inhibit_auto_suspend_str, "never"))
259 inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_NEVER;
260 else if (pa_streq(inhibit_auto_suspend_str, "only_with_non_monitor_sources"))
261 inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
263 pa_log("Failed to parse the \"inhibit_auto_suspend\" parameter.");
269 pa_rtp_sample_spec_fixup(&ss);
271 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
272 pa_log("Failed to parse sample specification");
276 if (!pa_rtp_sample_spec_valid(&ss)) {
277 pa_log("Specified sample type not compatible with RTP");
281 if (ss.channels != cm.channels)
282 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF);
284 payload = pa_rtp_payload_from_sample_spec(&ss);
286 mtu = (uint32_t) pa_frame_align(DEFAULT_MTU, &ss);
288 if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
289 pa_log("Invalid MTU.");
293 port = DEFAULT_PORT + ((uint32_t) (rand() % 512) << 1);
294 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
295 pa_log("port= expects a numerical argument between 1 and 65535.");
300 pa_log_warn("Port number not even as suggested in RFC3550!");
302 if (pa_modargs_get_value_u32(ma, "ttl", &ttl) < 0 || ttl < 1 || ttl > 0xFF) {
303 pa_log("ttl= expects a numerical argument between 1 and 255.");
307 src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP);
309 if (inet_pton(AF_INET, src_addr, &src_sa4.sin_addr) > 0) {
310 src_sa4.sin_family = af = AF_INET;
311 src_sa4.sin_port = htons(0);
312 memset(&src_sa4.sin_zero, 0, sizeof(src_sa4.sin_zero));
313 src_sap_sa4 = src_sa4;
315 } else if (inet_pton(AF_INET6, src_addr, &src_sa6.sin6_addr) > 0) {
316 src_sa6.sin6_family = af = AF_INET6;
317 src_sa6.sin6_port = htons(0);
318 src_sa6.sin6_flowinfo = 0;
319 src_sa6.sin6_scope_id = 0;
320 src_sap_sa6 = src_sa6;
323 pa_log("Invalid source address '%s'", src_addr);
327 dst_addr = pa_modargs_get_value(ma, "destination", NULL);
328 if (dst_addr == NULL)
329 dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP);
331 if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) {
332 dst_sa4.sin_family = af = AF_INET;
333 dst_sa4.sin_port = htons((uint16_t) port);
334 memset(&dst_sa4.sin_zero, 0, sizeof(dst_sa4.sin_zero));
335 dst_sap_sa4 = dst_sa4;
336 dst_sap_sa4.sin_port = htons(SAP_PORT);
338 } else if (inet_pton(AF_INET6, dst_addr, &dst_sa6.sin6_addr) > 0) {
339 dst_sa6.sin6_family = af = AF_INET6;
340 dst_sa6.sin6_port = htons((uint16_t) port);
341 dst_sa6.sin6_flowinfo = 0;
342 dst_sa6.sin6_scope_id = 0;
343 dst_sap_sa6 = dst_sa6;
344 dst_sap_sa6.sin6_port = htons(SAP_PORT);
347 pa_log("Invalid destination '%s'", dst_addr);
351 if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
352 pa_log("socket() failed: %s", pa_cstrerror(errno));
356 if (af == AF_INET && bind(fd, (struct sockaddr*) &src_sa4, sizeof(src_sa4)) < 0) {
357 pa_log("bind() failed: %s", pa_cstrerror(errno));
360 } else if (af == AF_INET6 && bind(fd, (struct sockaddr*) &src_sa6, sizeof(src_sa6)) < 0) {
361 pa_log("bind() failed: %s", pa_cstrerror(errno));
366 if (af == AF_INET && connect(fd, (struct sockaddr*) &dst_sa4, sizeof(dst_sa4)) < 0) {
367 pa_log("connect() failed: %s", pa_cstrerror(errno));
370 } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &dst_sa6, sizeof(dst_sa6)) < 0) {
371 pa_log("connect() failed: %s", pa_cstrerror(errno));
376 if ((sap_fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
377 pa_log("socket() failed: %s", pa_cstrerror(errno));
381 if (af == AF_INET && bind(sap_fd, (struct sockaddr*) &src_sap_sa4, sizeof(src_sap_sa4)) < 0) {
382 pa_log("bind() failed: %s", pa_cstrerror(errno));
385 } else if (af == AF_INET6 && bind(sap_fd, (struct sockaddr*) &src_sap_sa6, sizeof(src_sap_sa6)) < 0) {
386 pa_log("bind() failed: %s", pa_cstrerror(errno));
391 if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &dst_sap_sa4, sizeof(dst_sap_sa4)) < 0) {
392 pa_log("connect() failed: %s", pa_cstrerror(errno));
395 } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &dst_sap_sa6, sizeof(dst_sap_sa6)) < 0) {
396 pa_log("connect() failed: %s", pa_cstrerror(errno));
402 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 ||
403 setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) {
404 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
408 if (ttl != DEFAULT_TTL) {
409 int _ttl = (int) ttl;
411 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
412 pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno));
416 if (setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
417 pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno));
422 /* If the socket queue is full, let's drop packets */
423 pa_make_fd_nonblock(fd);
424 pa_make_udp_socket_low_delay(fd);
426 pa_source_output_new_data_init(&data);
427 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream");
428 pa_proplist_sets(data.proplist, "rtp.source", src_addr);
429 pa_proplist_sets(data.proplist, "rtp.destination", dst_addr);
430 pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu);
431 pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port);
432 pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl);
433 data.driver = __FILE__;
435 pa_source_output_new_data_set_source(&data, s, false);
436 pa_source_output_new_data_set_sample_spec(&data, &ss);
437 pa_source_output_new_data_set_channel_map(&data, &cm);
438 data.flags |= get_dont_inhibit_auto_suspend_flag(s, inhibit_auto_suspend);
440 pa_source_output_new(&o, m->core, &data);
441 pa_source_output_new_data_done(&data);
444 pa_log("failed to create source output.");
448 o->parent.process_msg = source_output_process_msg;
449 o->push = source_output_push_cb;
450 o->moving = source_output_moving_cb;
451 o->kill = source_output_kill_cb;
453 pa_log_info("Configured source latency of %llu ms.",
454 (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC);
456 m->userdata = o->userdata = u = pa_xnew(struct userdata, 1);
458 u->source_output = o;
460 u->memblockq = pa_memblockq_new(
461 "module-rtp-send memblockq",
474 pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
476 n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
480 (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr,
481 (void*) &dst_sa4.sin_addr,
482 n, (uint16_t) port, payload, &ss);
486 (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
487 (void*) &dst_sa6.sin6_addr,
488 n, (uint16_t) port, payload, &ss);
494 pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
495 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
497 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);
498 pa_log_info("SDP-Data:\n%s\nEOF", p);
500 pa_sap_send(&u->sap_context, 0);
502 u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u);
503 u->inhibit_auto_suspend = inhibit_auto_suspend;
505 pa_source_output_put(u->source_output);
522 pa_source_output_unlink(o);
523 pa_source_output_unref(o);
529 void pa__done(pa_module*m) {
533 if (!(u = m->userdata))
537 m->core->mainloop->time_free(u->sap_event);
539 if (u->source_output) {
540 pa_source_output_unlink(u->source_output);
541 pa_source_output_unref(u->source_output);
544 pa_rtp_context_destroy(&u->rtp_context);
546 pa_sap_send(&u->sap_context, 1);
547 pa_sap_context_destroy(&u->sap_context);
550 pa_memblockq_free(u->memblockq);