3 This file is part of polypaudio.
5 polypaudio is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 2 of the License,
8 or (at your option) any later version.
10 polypaudio is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with polypaudio; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
34 #include <polypcore/module.h>
35 #include <polypcore/llist.h>
36 #include <polypcore/source.h>
37 #include <polypcore/source-output.h>
38 #include <polypcore/memblockq.h>
39 #include <polypcore/log.h>
40 #include <polypcore/util.h>
41 #include <polypcore/xmalloc.h>
42 #include <polypcore/modargs.h>
43 #include <polypcore/namereg.h>
45 #include "module-rtp-monitor-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP")
53 PA_MODULE_VERSION(PACKAGE_VERSION
)
55 "source=<name for the source> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "destinaton=<destination IP address> "
61 "mtu=<maximum transfer unit> "
64 #define DEFAULT_PORT 5004
66 #define DEFAULT_DESTINATION "224.0.1.2"
67 #define MEMBLOCKQ_MAXLENGTH (1024*170)
68 #define DEFAULT_MTU 1024
69 #define SAP_INTERVAL 5000000
71 static const char* const valid_modargs
[] = {
85 pa_source_output
*source_output
;
86 pa_memblockq
*memblockq
;
88 pa_rtp_context rtp_context
;
89 pa_sap_context sap_context
;
92 pa_time_event
*sap_event
;
95 static void source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
100 if (pa_memblockq_push(u
->memblockq
, chunk
) < 0) {
101 pa_log(__FILE__
": Failed to push chunk into memblockq.");
105 pa_rtp_send(&u
->rtp_context
, u
->mtu
, u
->memblockq
);
108 static void source_output_kill(pa_source_output
* o
) {
113 pa_module_unload_request(u
->module
);
115 pa_source_output_disconnect(u
->source_output
);
116 pa_source_output_unref(u
->source_output
);
117 u
->source_output
= NULL
;
120 static pa_usec_t
source_output_get_latency (pa_source_output
*o
) {
125 return pa_bytes_to_usec(pa_memblockq_get_length(u
->memblockq
), &o
->sample_spec
);
128 static void sap_event(pa_mainloop_api
*m
, pa_time_event
*t
, const struct timeval
*tv
, void *userdata
) {
129 struct userdata
*u
= userdata
;
137 pa_sap_send(&u
->sap_context
, 0);
139 pa_gettimeofday(&next
);
140 pa_timeval_add(&next
, SAP_INTERVAL
);
141 m
->time_restart(t
, &next
);
144 int pa__init(pa_core
*c
, pa_module
*m
) {
146 pa_modargs
*ma
= NULL
;
148 uint32_t port
= DEFAULT_PORT
, mtu
;
149 int af
, fd
= -1, sap_fd
= -1;
153 struct sockaddr_in sa4
, sap_sa4
;
154 struct sockaddr_in6 sa6
, sap_sa6
;
155 struct sockaddr_storage sa_dst
;
156 pa_source_output
*o
= NULL
;
166 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
167 pa_log(__FILE__
": failed to parse module arguments");
171 if (!(s
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source", NULL
), PA_NAMEREG_SOURCE
, 1))) {
172 pa_log(__FILE__
": source does not exist.");
177 pa_rtp_sample_spec_fixup(&ss
);
179 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
180 pa_log(__FILE__
": failed to parse sample specification");
184 if (!pa_rtp_sample_spec_valid(&ss
)) {
185 pa_log(__FILE__
": specified sample type not compatible with RTP");
189 if (ss
.channels
!= cm
.channels
)
190 pa_channel_map_init_auto(&cm
, ss
.channels
);
192 payload
= pa_rtp_payload_type(&ss
);
194 mtu
= (DEFAULT_MTU
/pa_frame_size(&ss
))*pa_frame_size(&ss
);
196 if (pa_modargs_get_value_u32(ma
, "mtu", &mtu
) < 0 || mtu
< 1 || mtu
% pa_frame_size(&ss
) != 0) {
197 pa_log(__FILE__
": invalid mtu.");
201 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
< 1 || port
> 0xFFFF) {
202 pa_log(__FILE__
": port= expects a numerical argument between 1 and 65535.");
206 if ((dest
= pa_modargs_get_value(ma
, "destination", DEFAULT_DESTINATION
))) {
207 if (inet_pton(AF_INET6
, dest
, &sa6
.sin6_addr
) > 0) {
208 sa6
.sin6_family
= af
= AF_INET6
;
209 sa6
.sin6_port
= htons(port
);
211 sap_sa6
.sin6_port
= htons(SAP_PORT
);
212 } else if (inet_pton(AF_INET
, dest
, &sa4
.sin_addr
) > 0) {
213 sa4
.sin_family
= af
= AF_INET
;
214 sa4
.sin_port
= htons(port
);
216 sap_sa4
.sin_port
= htons(SAP_PORT
);
218 pa_log(__FILE__
": invalid destination '%s'", dest
);
223 if ((fd
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
224 pa_log(__FILE__
": socket() failed: %s", strerror(errno
));
228 if (connect(fd
, af
== AF_INET
? (struct sockaddr
*) &sa4
: (struct sockaddr
*) &sa6
, af
== AF_INET
? sizeof(sa4
) : sizeof(sa6
)) < 0) {
229 pa_log(__FILE__
": connect() failed: %s", strerror(errno
));
233 if ((sap_fd
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
234 pa_log(__FILE__
": socket() failed: %s", strerror(errno
));
238 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) {
239 pa_log(__FILE__
": connect() failed: %s", strerror(errno
));
243 if (!(o
= pa_source_output_new(s
, __FILE__
, "RTP Monitor Stream", &ss
, &cm
, PA_RESAMPLER_INVALID
))) {
244 pa_log(__FILE__
": failed to create source output.");
248 o
->push
= source_output_push
;
249 o
->kill
= source_output_kill
;
250 o
->get_latency
= source_output_get_latency
;
253 u
= pa_xnew(struct userdata
, 1);
259 u
->source_output
= o
;
261 u
->memblockq
= pa_memblockq_new(
274 r
= getsockname(fd
, (struct sockaddr
*) &sa_dst
, &k
);
278 af
== AF_INET
? (void*) &((struct sockaddr_in
*) &sa_dst
)->sin_addr
: (void*) &((struct sockaddr_in6
*) &sa_dst
)->sin6_addr
,
279 af
== AF_INET
? (void*) &sa4
.sin_addr
: (void*) &sa6
.sin6_addr
,
280 "Polypaudio RTP Stream", port
, payload
, &ss
);
282 pa_rtp_context_init_send(&u
->rtp_context
, fd
, 0, payload
, pa_frame_size(&ss
));
283 pa_sap_context_init_send(&u
->sap_context
, sap_fd
, p
);
285 pa_log_info("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
);
286 pa_log_info("SDP-Data:\n%s\nEOF", p
);
288 pa_sap_send(&u
->sap_context
, 0);
290 pa_gettimeofday(&tv
);
291 pa_timeval_add(&tv
, SAP_INTERVAL
);
292 u
->sap_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, sap_event
, u
);
309 pa_source_output_disconnect(o
);
310 pa_source_output_unref(o
);
316 void pa__done(pa_core
*c
, pa_module
*m
) {
321 if (!(u
= m
->userdata
))
324 c
->mainloop
->time_free(u
->sap_event
);
326 if (u
->source_output
) {
327 pa_source_output_disconnect(u
->source_output
);
328 pa_source_output_unref(u
->source_output
);
331 pa_rtp_context_destroy(&u
->rtp_context
);
333 pa_sap_send(&u
->sap_context
, 1);
334 pa_sap_context_destroy(&u
->sap_context
);
336 pa_memblockq_free(u
->memblockq
);