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/sink.h>
37 #include <polypcore/sink-input.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-recv-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Recieve data from a network via RTP/SAP/SDP")
53 PA_MODULE_VERSION(PACKAGE_VERSION
)
55 "sink=<name of the sink> "
56 "sap_address=<multicast address to listen on> "
60 #define DEFAULT_SAP_ADDRESS "224.0.1.2"
61 #define MEMBLOCKQ_MAXLENGTH (1024*170)
62 #define MAX_SESSIONS 16
63 #define DEATH_TIMEOUT 20000000
65 static const char* const valid_modargs
[] = {
72 struct userdata
*userdata
;
74 pa_sink_input
*sink_input
;
75 pa_memblockq
*memblockq
;
77 pa_time_event
*death_event
;
83 struct pa_sdp_info sdp_info
;
85 pa_rtp_context rtp_context
;
86 pa_io_event
* rtp_event
;
93 pa_sap_context sap_context
;
94 pa_io_event
* sap_event
;
96 pa_hashmap
*by_origin
;
101 static void session_free(struct session
*s
, int from_hash
);
103 static int sink_input_peek(pa_sink_input
*i
, pa_memchunk
*chunk
) {
108 return pa_memblockq_peek(s
->memblockq
, chunk
);
111 static void sink_input_drop(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
116 pa_memblockq_drop(s
->memblockq
, chunk
, length
);
119 static void sink_input_kill(pa_sink_input
* i
) {
127 static pa_usec_t
sink_input_get_latency(pa_sink_input
*i
) {
132 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &i
->sample_spec
);
135 static void rtp_event_cb(pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
136 struct session
*s
= userdata
;
144 assert(fd
== s
->rtp_context
.fd
);
145 assert(flags
== PA_IO_EVENT_INPUT
);
147 if (pa_rtp_recv(&s
->rtp_context
, &chunk
, s
->userdata
->core
->memblock_stat
) < 0)
150 if (s
->sdp_info
.payload
!= s
->rtp_context
.payload
) {
151 pa_memblock_unref(chunk
.memblock
);
155 if (!s
->first_packet
) {
158 s
->ssrc
= s
->rtp_context
.ssrc
;
159 s
->offset
= s
->rtp_context
.timestamp
;
161 if (s
->ssrc
!= s
->rtp_context
.ssrc
) {
162 pa_memblock_unref(chunk
.memblock
);
167 /* Check wheter there was a timestamp overflow */
168 k
= (int64_t) s
->rtp_context
.timestamp
- (int64_t) s
->offset
;
169 j
= (int64_t) 0x100000000 - (int64_t) s
->offset
+ (int64_t) s
->rtp_context
.timestamp
;
171 if ((k
< 0 ? -k
: k
) < (j
< 0 ? -j
: j
))
176 pa_memblockq_seek(s
->memblockq
, delta
* s
->rtp_context
.frame_size
, PA_SEEK_RELATIVE
);
177 pa_memblockq_push(s
->memblockq
, &chunk
);
179 /* The next timestamp we expect */
180 s
->offset
= s
->rtp_context
.timestamp
+ (chunk
.length
/ s
->rtp_context
.frame_size
);
182 pa_memblock_unref(chunk
.memblock
);
184 /* Reset death timer */
185 pa_gettimeofday(&tv
);
186 pa_timeval_add(&tv
, DEATH_TIMEOUT
);
187 m
->time_restart(s
->death_event
, &tv
);
190 static void death_event_cb(pa_mainloop_api
*m
, pa_time_event
*t
, const struct timeval
*tv
, void *userdata
) {
191 struct session
*s
= userdata
;
201 static int mcast_socket(const struct sockaddr
* sa
, socklen_t salen
) {
202 int af
, fd
= -1, r
, one
;
205 if ((fd
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
206 pa_log(__FILE__
": Failed to create socket: %s", strerror(errno
));
211 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
)) < 0) {
212 pa_log(__FILE__
": SO_REUSEADDR failed: %s", strerror(errno
));
218 memset(&mr4
, 0, sizeof(mr4
));
219 mr4
.imr_multiaddr
= ((const struct sockaddr_in
*) sa
)->sin_addr
;
220 r
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &mr4
, sizeof(mr4
));
222 struct ipv6_mreq mr6
;
223 memset(&mr6
, 0, sizeof(mr6
));
224 mr6
.ipv6mr_multiaddr
= ((const struct sockaddr_in6
*) sa
)->sin6_addr
;
225 r
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_ADD_MEMBERSHIP
, &mr6
, sizeof(mr6
));
229 pa_log_info(__FILE__
": Joining mcast group failed: %s", strerror(errno
));
233 if (bind(fd
, sa
, salen
) < 0) {
234 pa_log(__FILE__
": bind() failed: %s", strerror(errno
));
247 static struct session
*session_new(struct userdata
*u
, const pa_sdp_info
*sdp_info
) {
248 struct session
*s
= NULL
;
254 if (!(sink
= pa_namereg_get(u
->core
, u
->sink_name
, PA_NAMEREG_SINK
, 1))) {
255 pa_log(__FILE__
": sink does not exist.");
259 s
= pa_xnew0(struct session
, 1);
262 s
->sdp_info
= *sdp_info
;
264 if ((fd
= mcast_socket((const struct sockaddr
*) &sdp_info
->sa
, sdp_info
->salen
)) < 0)
267 c
= pa_sprintf_malloc("RTP Stream%s%s%s",
268 sdp_info
->session_name
? " (" : "",
269 sdp_info
->session_name
? sdp_info
->session_name
: "",
270 sdp_info
->session_name
? ")" : "");
272 s
->sink_input
= pa_sink_input_new(sink
, __FILE__
, c
, &sdp_info
->sample_spec
, NULL
, 0, PA_RESAMPLER_INVALID
);
275 if (!s
->sink_input
) {
276 pa_log(__FILE__
": failed to create sink input.");
280 s
->sink_input
->userdata
= s
;
281 s
->sink_input
->owner
= u
->module
;
283 s
->sink_input
->peek
= sink_input_peek
;
284 s
->sink_input
->drop
= sink_input_drop
;
285 s
->sink_input
->kill
= sink_input_kill
;
286 s
->sink_input
->get_latency
= sink_input_get_latency
;
288 s
->memblockq
= pa_memblockq_new(
292 pa_frame_size(&s
->sink_input
->sample_spec
),
296 u
->core
->memblock_stat
);
298 s
->rtp_event
= u
->core
->mainloop
->io_new(u
->core
->mainloop
, fd
, PA_IO_EVENT_INPUT
, rtp_event_cb
, s
);
300 pa_gettimeofday(&tv
);
301 pa_timeval_add(&tv
, DEATH_TIMEOUT
);
302 s
->death_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, death_event_cb
, s
);
304 pa_hashmap_put(s
->userdata
->by_origin
, s
->sdp_info
.origin
, s
);
306 pa_rtp_context_init_recv(&s
->rtp_context
, fd
, pa_frame_size(&s
->sdp_info
.sample_spec
));
308 pa_log_info(__FILE__
": Found new session '%s'", s
->sdp_info
.session_name
);
323 static void session_free(struct session
*s
, int from_hash
) {
326 pa_log_info(__FILE__
": Freeing session '%s'", s
->sdp_info
.session_name
);
328 s
->userdata
->core
->mainloop
->time_free(s
->death_event
);
329 s
->userdata
->core
->mainloop
->io_free(s
->rtp_event
);
332 pa_hashmap_remove(s
->userdata
->by_origin
, s
->sdp_info
.origin
);
334 pa_sink_input_disconnect(s
->sink_input
);
335 pa_sink_input_unref(s
->sink_input
);
337 pa_memblockq_free(s
->memblockq
);
338 pa_sdp_info_destroy(&s
->sdp_info
);
339 pa_rtp_context_destroy(&s
->rtp_context
);
344 static void sap_event_cb(pa_mainloop_api
*m
, pa_io_event
*e
, int fd
, pa_io_event_flags_t flags
, void *userdata
) {
345 struct userdata
*u
= userdata
;
353 assert(fd
== u
->sap_context
.fd
);
354 assert(flags
== PA_IO_EVENT_INPUT
);
356 if (pa_sap_recv(&u
->sap_context
, &goodbye
) < 0)
359 if (!pa_sdp_parse(u
->sap_context
.sdp_data
, &info
, goodbye
))
364 if ((s
= pa_hashmap_get(u
->by_origin
, info
.origin
)))
367 pa_sdp_info_destroy(&info
);
370 if (!(s
= pa_hashmap_get(u
->by_origin
, info
.origin
))) {
371 if (!(s
= session_new(u
, &info
)))
372 pa_sdp_info_destroy(&info
);
377 pa_gettimeofday(&tv
);
378 pa_timeval_add(&tv
, DEATH_TIMEOUT
);
379 m
->time_restart(s
->death_event
, &tv
);
381 pa_sdp_info_destroy(&info
);
386 int pa__init(pa_core
*c
, pa_module
*m
) {
388 pa_modargs
*ma
= NULL
;
389 struct sockaddr_in sa4
;
390 struct sockaddr_in6 sa6
;
393 const char *sap_address
;
399 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
400 pa_log(__FILE__
": failed to parse module arguments");
404 sap_address
= pa_modargs_get_value(ma
, "sap_address", DEFAULT_SAP_ADDRESS
);
406 if (inet_pton(AF_INET6
, sap_address
, &sa6
.sin6_addr
) > 0) {
407 sa6
.sin6_family
= AF_INET6
;
408 sa6
.sin6_port
= htons(SAP_PORT
);
409 sa
= (struct sockaddr
*) &sa6
;
411 } else if (inet_pton(AF_INET
, sap_address
, &sa4
.sin_addr
) > 0) {
412 sa4
.sin_family
= AF_INET
;
413 sa4
.sin_port
= htons(SAP_PORT
);
414 sa
= (struct sockaddr
*) &sa4
;
417 pa_log(__FILE__
": invalid SAP address '%s'", sap_address
);
421 if ((fd
= mcast_socket(sa
, salen
)) < 0)
424 u
= pa_xnew(struct userdata
, 1);
428 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
430 u
->sap_event
= c
->mainloop
->io_new(c
->mainloop
, fd
, PA_IO_EVENT_INPUT
, sap_event_cb
, u
);
432 u
->by_origin
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
434 pa_sap_context_init_recv(&u
->sap_context
, fd
);
450 static void free_func(void *p
, void *userdata
) {
454 void pa__done(pa_core
*c
, pa_module
*m
) {
459 if (!(u
= m
->userdata
))
462 c
->mainloop
->io_free(u
->sap_event
);
463 pa_sap_context_destroy(&u
->sap_context
);
465 pa_hashmap_free(u
->by_origin
, free_func
, NULL
);
467 pa_xfree(u
->sink_name
);