4 This file is part of polypaudio.
6 polypaudio 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 of the License,
9 or (at your option) any later version.
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
35 #include <polyp/timeval.h>
36 #include <polyp/util.h>
37 #include <polyp/xmalloc.h>
39 #include <polypcore/core-error.h>
40 #include <polypcore/module.h>
41 #include <polypcore/llist.h>
42 #include <polypcore/source.h>
43 #include <polypcore/source-output.h>
44 #include <polypcore/memblockq.h>
45 #include <polypcore/log.h>
46 #include <polypcore/core-util.h>
47 #include <polypcore/modargs.h>
48 #include <polypcore/namereg.h>
50 #include "module-rtp-send-symdef.h"
56 PA_MODULE_AUTHOR("Lennart Poettering")
57 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP")
58 PA_MODULE_VERSION(PACKAGE_VERSION
)
60 "source=<name of the source> "
61 "format=<sample format> "
62 "channels=<number of channels> "
64 "destination=<destination IP address> "
66 "mtu=<maximum transfer unit> "
67 "loop=<loopback to local host?>"
70 #define DEFAULT_PORT 46000
72 #define DEFAULT_DESTINATION "224.0.0.56"
73 #define MEMBLOCKQ_MAXLENGTH (1024*170)
74 #define DEFAULT_MTU 1280
75 #define SAP_INTERVAL 5000000
77 static const char* const valid_modargs
[] = {
92 pa_source_output
*source_output
;
93 pa_memblockq
*memblockq
;
95 pa_rtp_context rtp_context
;
96 pa_sap_context sap_context
;
99 pa_time_event
*sap_event
;
102 static void source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
107 if (pa_memblockq_push(u
->memblockq
, chunk
) < 0) {
108 pa_log(__FILE__
": Failed to push chunk into memblockq.");
112 pa_rtp_send(&u
->rtp_context
, u
->mtu
, u
->memblockq
);
115 static void source_output_kill(pa_source_output
* o
) {
120 pa_module_unload_request(u
->module
);
122 pa_source_output_disconnect(u
->source_output
);
123 pa_source_output_unref(u
->source_output
);
124 u
->source_output
= NULL
;
127 static pa_usec_t
source_output_get_latency (pa_source_output
*o
) {
132 return pa_bytes_to_usec(pa_memblockq_get_length(u
->memblockq
), &o
->sample_spec
);
135 static void sap_event_cb(pa_mainloop_api
*m
, pa_time_event
*t
, const struct timeval
*tv
, void *userdata
) {
136 struct userdata
*u
= userdata
;
144 pa_sap_send(&u
->sap_context
, 0);
146 pa_gettimeofday(&next
);
147 pa_timeval_add(&next
, SAP_INTERVAL
);
148 m
->time_restart(t
, &next
);
151 int pa__init(pa_core
*c
, pa_module
*m
) {
153 pa_modargs
*ma
= NULL
;
155 uint32_t port
= DEFAULT_PORT
, mtu
;
156 int af
, fd
= -1, sap_fd
= -1;
160 struct sockaddr_in sa4
, sap_sa4
;
161 struct sockaddr_in6 sa6
, sap_sa6
;
162 struct sockaddr_storage sa_dst
;
163 pa_source_output
*o
= NULL
;
175 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
176 pa_log(__FILE__
": failed to parse module arguments");
180 if (!(s
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source", NULL
), PA_NAMEREG_SOURCE
, 1))) {
181 pa_log(__FILE__
": source does not exist.");
185 if (pa_modargs_get_value_boolean(ma
, "loop", &loop
) < 0) {
186 pa_log(__FILE__
": failed to parse \"loop\" parameter.");
191 pa_rtp_sample_spec_fixup(&ss
);
193 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
194 pa_log(__FILE__
": failed to parse sample specification");
198 if (!pa_rtp_sample_spec_valid(&ss
)) {
199 pa_log(__FILE__
": specified sample type not compatible with RTP");
203 if (ss
.channels
!= cm
.channels
)
204 pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_AIFF
);
206 payload
= pa_rtp_payload_from_sample_spec(&ss
);
208 mtu
= (DEFAULT_MTU
/pa_frame_size(&ss
))*pa_frame_size(&ss
);
210 if (pa_modargs_get_value_u32(ma
, "mtu", &mtu
) < 0 || mtu
< 1 || mtu
% pa_frame_size(&ss
) != 0) {
211 pa_log(__FILE__
": invalid mtu.");
215 port
= DEFAULT_PORT
+ ((rand() % 512) << 1);
216 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
< 1 || port
> 0xFFFF) {
217 pa_log(__FILE__
": port= expects a numerical argument between 1 and 65535.");
222 pa_log_warn(__FILE__
": WARNING: port number not even as suggested in RFC3550!");
224 dest
= pa_modargs_get_value(ma
, "destination", DEFAULT_DESTINATION
);
226 if (inet_pton(AF_INET6
, dest
, &sa6
.sin6_addr
) > 0) {
227 sa6
.sin6_family
= af
= AF_INET6
;
228 sa6
.sin6_port
= htons(port
);
230 sap_sa6
.sin6_port
= htons(SAP_PORT
);
231 } else if (inet_pton(AF_INET
, dest
, &sa4
.sin_addr
) > 0) {
232 sa4
.sin_family
= af
= AF_INET
;
233 sa4
.sin_port
= htons(port
);
235 sap_sa4
.sin_port
= htons(SAP_PORT
);
237 pa_log(__FILE__
": invalid destination '%s'", dest
);
241 if ((fd
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
242 pa_log(__FILE__
": socket() failed: %s", pa_cstrerror(errno
));
246 if (connect(fd
, af
== AF_INET
? (struct sockaddr
*) &sa4
: (struct sockaddr
*) &sa6
, af
== AF_INET
? sizeof(sa4
) : sizeof(sa6
)) < 0) {
247 pa_log(__FILE__
": connect() failed: %s", pa_cstrerror(errno
));
251 if ((sap_fd
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
252 pa_log(__FILE__
": socket() failed: %s", pa_cstrerror(errno
));
256 if (connect(sap_fd
, af
== AF_INET
? (struct sockaddr
*) &sap_sa4
: (struct sockaddr
*) &sap_sa6
, af
== AF_INET
? sizeof(sap_sa4
) : sizeof(sap_sa6
)) < 0) {
257 pa_log(__FILE__
": connect() failed: %s", pa_cstrerror(errno
));
261 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &loop
, sizeof(loop
)) < 0 ||
262 setsockopt(sap_fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &loop
, sizeof(loop
)) < 0) {
263 pa_log(__FILE__
": IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno
));
267 if (!(o
= pa_source_output_new(s
, __FILE__
, "RTP Monitor Stream", &ss
, &cm
, PA_RESAMPLER_INVALID
))) {
268 pa_log(__FILE__
": failed to create source output.");
272 o
->push
= source_output_push
;
273 o
->kill
= source_output_kill
;
274 o
->get_latency
= source_output_get_latency
;
277 u
= pa_xnew(struct userdata
, 1);
283 u
->source_output
= o
;
285 u
->memblockq
= pa_memblockq_new(
298 r
= getsockname(fd
, (struct sockaddr
*) &sa_dst
, &k
);
301 n
= pa_sprintf_malloc("Polypaudio RTP Stream on %s", pa_get_fqdn(hn
, sizeof(hn
)));
304 af
== AF_INET
? (void*) &((struct sockaddr_in
*) &sa_dst
)->sin_addr
: (void*) &((struct sockaddr_in6
*) &sa_dst
)->sin6_addr
,
305 af
== AF_INET
? (void*) &sa4
.sin_addr
: (void*) &sa6
.sin6_addr
,
306 n
, port
, payload
, &ss
);
310 pa_rtp_context_init_send(&u
->rtp_context
, fd
, c
->cookie
, payload
, pa_frame_size(&ss
));
311 pa_sap_context_init_send(&u
->sap_context
, sap_fd
, p
);
313 pa_log_info(__FILE__
": RTP stream initialized with mtu %u on %s:%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu
, dest
, port
, u
->rtp_context
.ssrc
, payload
, u
->rtp_context
.sequence
);
314 pa_log_info(__FILE__
": SDP-Data:\n%s\n"__FILE__
": EOF", p
);
316 pa_sap_send(&u
->sap_context
, 0);
318 pa_gettimeofday(&tv
);
319 pa_timeval_add(&tv
, SAP_INTERVAL
);
320 u
->sap_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, sap_event_cb
, u
);
337 pa_source_output_disconnect(o
);
338 pa_source_output_unref(o
);
344 void pa__done(pa_core
*c
, pa_module
*m
) {
349 if (!(u
= m
->userdata
))
352 c
->mainloop
->time_free(u
->sap_event
);
354 if (u
->source_output
) {
355 pa_source_output_disconnect(u
->source_output
);
356 pa_source_output_unref(u
->source_output
);
359 pa_rtp_context_destroy(&u
->rtp_context
);
361 pa_sap_send(&u
->sap_context
, 1);
362 pa_sap_context_destroy(&u
->sap_context
);
364 pa_memblockq_free(u
->memblockq
);