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?> "
74 #define DEFAULT_PORT 46000
77 #define DEFAULT_SOURCE_IP "0.0.0.0"
78 #define DEFAULT_DESTINATION_IP "224.0.0.56"
79 #define MEMBLOCKQ_MAXLENGTH (1024*170)
80 #define DEFAULT_MTU 1280
81 #define SAP_INTERVAL (5*PA_USEC_PER_SEC)
83 static const char* const valid_modargs
[] = {
88 "destination", /* Compatbility */
101 pa_source_output
*source_output
;
102 pa_memblockq
*memblockq
;
104 pa_rtp_context rtp_context
;
105 pa_sap_context sap_context
;
108 pa_time_event
*sap_event
;
111 /* Called from I/O thread context */
112 static int source_output_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
114 pa_assert_se(u
= PA_SOURCE_OUTPUT(o
)->userdata
);
117 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY
:
118 *((pa_usec_t
*) data
) = pa_bytes_to_usec(pa_memblockq_get_length(u
->memblockq
), &u
->source_output
->sample_spec
);
120 /* Fall through, the default handler will add in the extra
121 * latency added by the resampler */
125 return pa_source_output_process_msg(o
, code
, data
, offset
, chunk
);
128 /* Called from I/O thread context */
129 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
131 pa_source_output_assert_ref(o
);
132 pa_assert_se(u
= o
->userdata
);
134 if (pa_memblockq_push(u
->memblockq
, chunk
) < 0) {
135 pa_log_warn("Failed to push chunk into memblockq.");
139 pa_rtp_send(&u
->rtp_context
, u
->mtu
, u
->memblockq
);
142 /* Called from main context */
143 static void source_output_kill_cb(pa_source_output
* o
) {
145 pa_source_output_assert_ref(o
);
146 pa_assert_se(u
= o
->userdata
);
148 pa_module_unload_request(u
->module
, true);
150 pa_source_output_unlink(u
->source_output
);
151 pa_source_output_unref(u
->source_output
);
152 u
->source_output
= NULL
;
155 static void sap_event_cb(pa_mainloop_api
*m
, pa_time_event
*t
, const struct timeval
*tv
, void *userdata
) {
156 struct userdata
*u
= userdata
;
162 pa_sap_send(&u
->sap_context
, 0);
164 pa_core_rttime_restart(u
->module
->core
, t
, pa_rtclock_now() + SAP_INTERVAL
);
167 int pa__init(pa_module
*m
) {
169 pa_modargs
*ma
= NULL
;
170 const char *dst_addr
;
171 const char *src_addr
;
172 uint32_t port
= DEFAULT_PORT
, mtu
;
173 uint32_t ttl
= DEFAULT_TTL
;
175 int fd
= -1, sap_fd
= -1;
179 struct sockaddr_in dst_sa4
, dst_sap_sa4
, src_sa4
, src_sap_sa4
;
181 struct sockaddr_in6 dst_sa6
, dst_sap_sa6
, src_sa6
, src_sap_sa6
;
183 struct sockaddr_storage sa_dst
;
184 pa_source_output
*o
= NULL
;
191 pa_source_output_new_data data
;
195 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
196 pa_log("Failed to parse module arguments");
200 if (!(s
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source", NULL
), PA_NAMEREG_SOURCE
))) {
201 pa_log("Source does not exist.");
205 if (pa_modargs_get_value_boolean(ma
, "loop", &loop
) < 0) {
206 pa_log("Failed to parse \"loop\" parameter.");
211 pa_rtp_sample_spec_fixup(&ss
);
213 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
214 pa_log("Failed to parse sample specification");
218 if (!pa_rtp_sample_spec_valid(&ss
)) {
219 pa_log("Specified sample type not compatible with RTP");
223 if (ss
.channels
!= cm
.channels
)
224 pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_AIFF
);
226 payload
= pa_rtp_payload_from_sample_spec(&ss
);
228 mtu
= (uint32_t) pa_frame_align(DEFAULT_MTU
, &ss
);
230 if (pa_modargs_get_value_u32(ma
, "mtu", &mtu
) < 0 || mtu
< 1 || mtu
% pa_frame_size(&ss
) != 0) {
231 pa_log("Invalid MTU.");
235 port
= DEFAULT_PORT
+ ((uint32_t) (rand() % 512) << 1);
236 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
< 1 || port
> 0xFFFF) {
237 pa_log("port= expects a numerical argument between 1 and 65535.");
242 pa_log_warn("Port number not even as suggested in RFC3550!");
244 if (pa_modargs_get_value_u32(ma
, "ttl", &ttl
) < 0 || ttl
< 1 || ttl
> 0xFF) {
245 pa_log("ttl= expects a numerical argument between 1 and 255.");
249 src_addr
= pa_modargs_get_value(ma
, "source_ip", DEFAULT_SOURCE_IP
);
251 if (inet_pton(AF_INET
, src_addr
, &src_sa4
.sin_addr
) > 0) {
252 src_sa4
.sin_family
= af
= AF_INET
;
253 src_sa4
.sin_port
= htons(0);
254 memset(&src_sa4
.sin_zero
, 0, sizeof(src_sa4
.sin_zero
));
255 src_sap_sa4
= src_sa4
;
257 } else if (inet_pton(AF_INET6
, src_addr
, &src_sa6
.sin6_addr
) > 0) {
258 src_sa6
.sin6_family
= af
= AF_INET6
;
259 src_sa6
.sin6_port
= htons(0);
260 src_sa6
.sin6_flowinfo
= 0;
261 src_sa6
.sin6_scope_id
= 0;
262 src_sap_sa6
= src_sa6
;
265 pa_log("Invalid source address '%s'", src_addr
);
269 dst_addr
= pa_modargs_get_value(ma
, "destination", NULL
);
270 if (dst_addr
== NULL
)
271 dst_addr
= pa_modargs_get_value(ma
, "destination_ip", DEFAULT_DESTINATION_IP
);
273 if (inet_pton(AF_INET
, dst_addr
, &dst_sa4
.sin_addr
) > 0) {
274 dst_sa4
.sin_family
= af
= AF_INET
;
275 dst_sa4
.sin_port
= htons((uint16_t) port
);
276 memset(&dst_sa4
.sin_zero
, 0, sizeof(dst_sa4
.sin_zero
));
277 dst_sap_sa4
= dst_sa4
;
278 dst_sap_sa4
.sin_port
= htons(SAP_PORT
);
280 } else if (inet_pton(AF_INET6
, dst_addr
, &dst_sa6
.sin6_addr
) > 0) {
281 dst_sa6
.sin6_family
= af
= AF_INET6
;
282 dst_sa6
.sin6_port
= htons((uint16_t) port
);
283 dst_sa6
.sin6_flowinfo
= 0;
284 dst_sa6
.sin6_scope_id
= 0;
285 dst_sap_sa6
= dst_sa6
;
286 dst_sap_sa6
.sin6_port
= htons(SAP_PORT
);
289 pa_log("Invalid destination '%s'", dst_addr
);
293 if ((fd
= pa_socket_cloexec(af
, SOCK_DGRAM
, 0)) < 0) {
294 pa_log("socket() failed: %s", pa_cstrerror(errno
));
298 if (af
== AF_INET
&& bind(fd
, (struct sockaddr
*) &src_sa4
, sizeof(src_sa4
)) < 0) {
299 pa_log("bind() failed: %s", pa_cstrerror(errno
));
302 } else if (af
== AF_INET6
&& bind(fd
, (struct sockaddr
*) &src_sa6
, sizeof(src_sa6
)) < 0) {
303 pa_log("bind() failed: %s", pa_cstrerror(errno
));
308 if (af
== AF_INET
&& connect(fd
, (struct sockaddr
*) &dst_sa4
, sizeof(dst_sa4
)) < 0) {
309 pa_log("connect() failed: %s", pa_cstrerror(errno
));
312 } else if (af
== AF_INET6
&& connect(fd
, (struct sockaddr
*) &dst_sa6
, sizeof(dst_sa6
)) < 0) {
313 pa_log("connect() failed: %s", pa_cstrerror(errno
));
318 if ((sap_fd
= pa_socket_cloexec(af
, SOCK_DGRAM
, 0)) < 0) {
319 pa_log("socket() failed: %s", pa_cstrerror(errno
));
323 if (af
== AF_INET
&& bind(sap_fd
, (struct sockaddr
*) &src_sap_sa4
, sizeof(src_sap_sa4
)) < 0) {
324 pa_log("bind() failed: %s", pa_cstrerror(errno
));
327 } else if (af
== AF_INET6
&& bind(sap_fd
, (struct sockaddr
*) &src_sap_sa6
, sizeof(src_sap_sa6
)) < 0) {
328 pa_log("bind() failed: %s", pa_cstrerror(errno
));
333 if (af
== AF_INET
&& connect(sap_fd
, (struct sockaddr
*) &dst_sap_sa4
, sizeof(dst_sap_sa4
)) < 0) {
334 pa_log("connect() failed: %s", pa_cstrerror(errno
));
337 } else if (af
== AF_INET6
&& connect(sap_fd
, (struct sockaddr
*) &dst_sap_sa6
, sizeof(dst_sap_sa6
)) < 0) {
338 pa_log("connect() failed: %s", pa_cstrerror(errno
));
344 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &j
, sizeof(j
)) < 0 ||
345 setsockopt(sap_fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &j
, sizeof(j
)) < 0) {
346 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno
));
350 if (ttl
!= DEFAULT_TTL
) {
351 int _ttl
= (int) ttl
;
353 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &_ttl
, sizeof(_ttl
)) < 0) {
354 pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno
));
358 if (setsockopt(sap_fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &_ttl
, sizeof(_ttl
)) < 0) {
359 pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno
));
364 /* If the socket queue is full, let's drop packets */
365 pa_make_fd_nonblock(fd
);
366 pa_make_udp_socket_low_delay(fd
);
368 pa_source_output_new_data_init(&data
);
369 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_NAME
, "RTP Monitor Stream");
370 pa_proplist_sets(data
.proplist
, "rtp.source", src_addr
);
371 pa_proplist_sets(data
.proplist
, "rtp.destination", dst_addr
);
372 pa_proplist_setf(data
.proplist
, "rtp.mtu", "%lu", (unsigned long) mtu
);
373 pa_proplist_setf(data
.proplist
, "rtp.port", "%lu", (unsigned long) port
);
374 pa_proplist_setf(data
.proplist
, "rtp.ttl", "%lu", (unsigned long) ttl
);
375 data
.driver
= __FILE__
;
377 pa_source_output_new_data_set_source(&data
, s
, false);
378 pa_source_output_new_data_set_sample_spec(&data
, &ss
);
379 pa_source_output_new_data_set_channel_map(&data
, &cm
);
381 pa_source_output_new(&o
, m
->core
, &data
);
382 pa_source_output_new_data_done(&data
);
385 pa_log("failed to create source output.");
389 o
->parent
.process_msg
= source_output_process_msg
;
390 o
->push
= source_output_push_cb
;
391 o
->kill
= source_output_kill_cb
;
393 pa_log_info("Configured source latency of %llu ms.",
394 (unsigned long long) pa_source_output_set_requested_latency(o
, pa_bytes_to_usec(mtu
, &o
->sample_spec
)) / PA_USEC_PER_MSEC
);
396 m
->userdata
= o
->userdata
= u
= pa_xnew(struct userdata
, 1);
398 u
->source_output
= o
;
400 u
->memblockq
= pa_memblockq_new(
401 "module-rtp-send memblockq",
414 pa_assert_se((r
= getsockname(fd
, (struct sockaddr
*) &sa_dst
, &k
)) >= 0);
416 n
= pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn
, sizeof(hn
)));
420 (void*) &((struct sockaddr_in
*) &sa_dst
)->sin_addr
,
421 (void*) &dst_sa4
.sin_addr
,
422 n
, (uint16_t) port
, payload
, &ss
);
426 (void*) &((struct sockaddr_in6
*) &sa_dst
)->sin6_addr
,
427 (void*) &dst_sa6
.sin6_addr
,
428 n
, (uint16_t) port
, payload
, &ss
);
434 pa_rtp_context_init_send(&u
->rtp_context
, fd
, m
->core
->cookie
, payload
, pa_frame_size(&ss
));
435 pa_sap_context_init_send(&u
->sap_context
, sap_fd
, p
);
437 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
);
438 pa_log_info("SDP-Data:\n%s\nEOF", p
);
440 pa_sap_send(&u
->sap_context
, 0);
442 u
->sap_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + SAP_INTERVAL
, sap_event_cb
, u
);
444 pa_source_output_put(u
->source_output
);
461 pa_source_output_unlink(o
);
462 pa_source_output_unref(o
);
468 void pa__done(pa_module
*m
) {
472 if (!(u
= m
->userdata
))
476 m
->core
->mainloop
->time_free(u
->sap_event
);
478 if (u
->source_output
) {
479 pa_source_output_unlink(u
->source_output
);
480 pa_source_output_unref(u
->source_output
);
483 pa_rtp_context_destroy(&u
->rtp_context
);
485 pa_sap_send(&u
->sap_context
, 1);
486 pa_sap_context_destroy(&u
->sap_context
);
489 pa_memblockq_free(u
->memblockq
);