]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-recv.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / src / modules / rtp / module-rtp-recv.c
1
2 /***
3 This file is part of polypaudio.
4
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.
9
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.
14
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
18 USA.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <polyp/timeval.h>
35 #include <polyp/xmalloc.h>
36
37 #include <polypcore/core-error.h>
38 #include <polypcore/module.h>
39 #include <polypcore/llist.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/sink-input.h>
42 #include <polypcore/memblockq.h>
43 #include <polypcore/log.h>
44 #include <polypcore/core-util.h>
45 #include <polypcore/modargs.h>
46 #include <polypcore/namereg.h>
47 #include <polypcore/sample-util.h>
48
49 #include "module-rtp-recv-symdef.h"
50
51 #include "rtp.h"
52 #include "sdp.h"
53 #include "sap.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("Recieve data from a network via RTP/SAP/SDP")
57 PA_MODULE_VERSION(PACKAGE_VERSION)
58 PA_MODULE_USAGE(
59 "sink=<name of the sink> "
60 "sap_address=<multicast address to listen on> "
61 )
62
63 #define SAP_PORT 9875
64 #define DEFAULT_SAP_ADDRESS "224.0.0.56"
65 #define MEMBLOCKQ_MAXLENGTH (1024*170)
66 #define MAX_SESSIONS 16
67 #define DEATH_TIMEOUT 20000000
68
69 static const char* const valid_modargs[] = {
70 "sink",
71 "sap_address",
72 NULL
73 };
74
75 struct session {
76 struct userdata *userdata;
77
78 pa_sink_input *sink_input;
79 pa_memblockq *memblockq;
80
81 pa_time_event *death_event;
82
83 int first_packet;
84 uint32_t ssrc;
85 uint32_t offset;
86
87 struct pa_sdp_info sdp_info;
88
89 pa_rtp_context rtp_context;
90 pa_io_event* rtp_event;
91 };
92
93 struct userdata {
94 pa_module *module;
95 pa_core *core;
96
97 pa_sap_context sap_context;
98 pa_io_event* sap_event;
99
100 pa_hashmap *by_origin;
101
102 char *sink_name;
103
104 int n_sessions;
105 };
106
107 static void session_free(struct session *s, int from_hash);
108
109 static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
110 struct session *s;
111 assert(i);
112 s = i->userdata;
113
114 return pa_memblockq_peek(s->memblockq, chunk);
115 }
116
117 static void sink_input_drop(pa_sink_input *i, const pa_memchunk *chunk, size_t length) {
118 struct session *s;
119 assert(i);
120 s = i->userdata;
121
122 pa_memblockq_drop(s->memblockq, chunk, length);
123 }
124
125 static void sink_input_kill(pa_sink_input* i) {
126 struct session *s;
127 assert(i);
128 s = i->userdata;
129
130 session_free(s, 1);
131 }
132
133 static pa_usec_t sink_input_get_latency(pa_sink_input *i) {
134 struct session *s;
135 assert(i);
136 s = i->userdata;
137
138 return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &i->sample_spec);
139 }
140
141 static void rtp_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
142 struct session *s = userdata;
143 pa_memchunk chunk;
144 int64_t k, j, delta;
145 struct timeval tv;
146
147 assert(m);
148 assert(e);
149 assert(s);
150 assert(fd == s->rtp_context.fd);
151 assert(flags == PA_IO_EVENT_INPUT);
152
153 if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->core->memblock_stat) < 0)
154 return;
155
156 if (s->sdp_info.payload != s->rtp_context.payload) {
157 pa_memblock_unref(chunk.memblock);
158 return;
159 }
160
161 if (!s->first_packet) {
162 s->first_packet = 1;
163
164 s->ssrc = s->rtp_context.ssrc;
165 s->offset = s->rtp_context.timestamp;
166
167 if (s->ssrc == s->userdata->core->cookie)
168 pa_log_warn(__FILE__": WARNING! Detected RTP packet loop!");
169 } else {
170 if (s->ssrc != s->rtp_context.ssrc) {
171 pa_memblock_unref(chunk.memblock);
172 return;
173 }
174 }
175
176 /* Check wheter there was a timestamp overflow */
177 k = (int64_t) s->rtp_context.timestamp - (int64_t) s->offset;
178 j = (int64_t) 0x100000000LL - (int64_t) s->offset + (int64_t) s->rtp_context.timestamp;
179
180 if ((k < 0 ? -k : k) < (j < 0 ? -j : j))
181 delta = k;
182 else
183 delta = j;
184
185 pa_memblockq_seek(s->memblockq, delta * s->rtp_context.frame_size, PA_SEEK_RELATIVE);
186
187 if (pa_memblockq_push(s->memblockq, &chunk) < 0) {
188 /* queue overflow, let's flush it and try again */
189 pa_memblockq_flush(s->memblockq);
190 pa_memblockq_push(s->memblockq, &chunk);
191 }
192
193 /* The next timestamp we expect */
194 s->offset = s->rtp_context.timestamp + (chunk.length / s->rtp_context.frame_size);
195
196 pa_memblock_unref(chunk.memblock);
197
198 /* Reset death timer */
199 pa_gettimeofday(&tv);
200 pa_timeval_add(&tv, DEATH_TIMEOUT);
201 m->time_restart(s->death_event, &tv);
202 }
203
204 static void death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
205 struct session *s = userdata;
206
207 assert(m);
208 assert(t);
209 assert(tv);
210 assert(s);
211
212 session_free(s, 1);
213 }
214
215 static int mcast_socket(const struct sockaddr* sa, socklen_t salen) {
216 int af, fd = -1, r, one;
217
218 af = sa->sa_family;
219 if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
220 pa_log(__FILE__": Failed to create socket: %s", pa_cstrerror(errno));
221 goto fail;
222 }
223
224 one = 1;
225 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
226 pa_log(__FILE__": SO_REUSEADDR failed: %s", pa_cstrerror(errno));
227 goto fail;
228 }
229
230 if (af == AF_INET) {
231 struct ip_mreq mr4;
232 memset(&mr4, 0, sizeof(mr4));
233 mr4.imr_multiaddr = ((const struct sockaddr_in*) sa)->sin_addr;
234 r = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr4, sizeof(mr4));
235 } else {
236 struct ipv6_mreq mr6;
237 memset(&mr6, 0, sizeof(mr6));
238 mr6.ipv6mr_multiaddr = ((const struct sockaddr_in6*) sa)->sin6_addr;
239 r = setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr6, sizeof(mr6));
240 }
241
242 if (r < 0) {
243 pa_log_info(__FILE__": Joining mcast group failed: %s", pa_cstrerror(errno));
244 goto fail;
245 }
246
247 if (bind(fd, sa, salen) < 0) {
248 pa_log(__FILE__": bind() failed: %s", pa_cstrerror(errno));
249 goto fail;
250 }
251
252 return fd;
253
254 fail:
255 if (fd >= 0)
256 close(fd);
257
258 return -1;
259 }
260
261 static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_info) {
262 struct session *s = NULL;
263 struct timeval tv;
264 char *c;
265 pa_sink *sink;
266 int fd = -1;
267 pa_memblock *silence;
268
269 if (u->n_sessions >= MAX_SESSIONS) {
270 pa_log(__FILE__": session limit reached.");
271 goto fail;
272 }
273
274 if (!(sink = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
275 pa_log(__FILE__": sink does not exist.");
276 goto fail;
277 }
278
279 s = pa_xnew0(struct session, 1);
280 s->userdata = u;
281 s->first_packet = 0;
282 s->sdp_info = *sdp_info;
283
284 if ((fd = mcast_socket((const struct sockaddr*) &sdp_info->sa, sdp_info->salen)) < 0)
285 goto fail;
286
287 c = pa_sprintf_malloc("RTP Stream%s%s%s",
288 sdp_info->session_name ? " (" : "",
289 sdp_info->session_name ? sdp_info->session_name : "",
290 sdp_info->session_name ? ")" : "");
291
292 s->sink_input = pa_sink_input_new(sink, __FILE__, c, &sdp_info->sample_spec, NULL, NULL, 0, PA_RESAMPLER_INVALID);
293 pa_xfree(c);
294
295 if (!s->sink_input) {
296 pa_log(__FILE__": failed to create sink input.");
297 goto fail;
298 }
299
300 s->sink_input->userdata = s;
301 s->sink_input->owner = u->module;
302
303 s->sink_input->peek = sink_input_peek;
304 s->sink_input->drop = sink_input_drop;
305 s->sink_input->kill = sink_input_kill;
306 s->sink_input->get_latency = sink_input_get_latency;
307
308 silence = pa_silence_memblock_new(&s->sink_input->sample_spec,
309 (pa_bytes_per_second(&s->sink_input->sample_spec)/128/pa_frame_size(&s->sink_input->sample_spec))*
310 pa_frame_size(&s->sink_input->sample_spec),
311 s->userdata->core->memblock_stat);
312
313 s->memblockq = pa_memblockq_new(
314 0,
315 MEMBLOCKQ_MAXLENGTH,
316 MEMBLOCKQ_MAXLENGTH,
317 pa_frame_size(&s->sink_input->sample_spec),
318 pa_bytes_per_second(&s->sink_input->sample_spec)/10+1,
319 0,
320 silence,
321 u->core->memblock_stat);
322
323 pa_memblock_unref(silence);
324
325 s->rtp_event = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, rtp_event_cb, s);
326
327 pa_gettimeofday(&tv);
328 pa_timeval_add(&tv, DEATH_TIMEOUT);
329 s->death_event = u->core->mainloop->time_new(u->core->mainloop, &tv, death_event_cb, s);
330
331 pa_hashmap_put(s->userdata->by_origin, s->sdp_info.origin, s);
332
333 pa_rtp_context_init_recv(&s->rtp_context, fd, pa_frame_size(&s->sdp_info.sample_spec));
334
335 pa_log_info(__FILE__": Found new session '%s'", s->sdp_info.session_name);
336
337 u->n_sessions++;
338
339 return s;
340
341 fail:
342 if (s) {
343 if (fd >= 0)
344 close(fd);
345
346 pa_xfree(s);
347 }
348
349 return NULL;
350 }
351
352 static void session_free(struct session *s, int from_hash) {
353 assert(s);
354
355 pa_log_info(__FILE__": Freeing session '%s'", s->sdp_info.session_name);
356
357 s->userdata->core->mainloop->time_free(s->death_event);
358 s->userdata->core->mainloop->io_free(s->rtp_event);
359
360 if (from_hash)
361 pa_hashmap_remove(s->userdata->by_origin, s->sdp_info.origin);
362
363 pa_sink_input_disconnect(s->sink_input);
364 pa_sink_input_unref(s->sink_input);
365
366 pa_memblockq_free(s->memblockq);
367 pa_sdp_info_destroy(&s->sdp_info);
368 pa_rtp_context_destroy(&s->rtp_context);
369
370 assert(s->userdata->n_sessions >= 1);
371 s->userdata->n_sessions--;
372
373 pa_xfree(s);
374 }
375
376 static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
377 struct userdata *u = userdata;
378 int goodbye;
379 pa_sdp_info info;
380 struct session *s;
381
382 assert(m);
383 assert(e);
384 assert(u);
385 assert(fd == u->sap_context.fd);
386 assert(flags == PA_IO_EVENT_INPUT);
387
388 if (pa_sap_recv(&u->sap_context, &goodbye) < 0)
389 return;
390
391 if (!pa_sdp_parse(u->sap_context.sdp_data, &info, goodbye))
392 return;
393
394 if (goodbye) {
395
396 if ((s = pa_hashmap_get(u->by_origin, info.origin)))
397 session_free(s, 1);
398
399 pa_sdp_info_destroy(&info);
400 } else {
401
402 if (!(s = pa_hashmap_get(u->by_origin, info.origin))) {
403 if (!(s = session_new(u, &info)))
404 pa_sdp_info_destroy(&info);
405
406 } else {
407 struct timeval tv;
408
409 pa_gettimeofday(&tv);
410 pa_timeval_add(&tv, DEATH_TIMEOUT);
411 m->time_restart(s->death_event, &tv);
412
413 pa_sdp_info_destroy(&info);
414 }
415 }
416 }
417
418 int pa__init(pa_core *c, pa_module*m) {
419 struct userdata *u;
420 pa_modargs *ma = NULL;
421 struct sockaddr_in sa4;
422 struct sockaddr_in6 sa6;
423 struct sockaddr *sa;
424 socklen_t salen;
425 const char *sap_address;
426 int fd = -1;
427
428 assert(c);
429 assert(m);
430
431 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
432 pa_log(__FILE__": failed to parse module arguments");
433 goto fail;
434 }
435
436 sap_address = pa_modargs_get_value(ma, "sap_address", DEFAULT_SAP_ADDRESS);
437
438 if (inet_pton(AF_INET6, sap_address, &sa6.sin6_addr) > 0) {
439 sa6.sin6_family = AF_INET6;
440 sa6.sin6_port = htons(SAP_PORT);
441 sa = (struct sockaddr*) &sa6;
442 salen = sizeof(sa6);
443 } else if (inet_pton(AF_INET, sap_address, &sa4.sin_addr) > 0) {
444 sa4.sin_family = AF_INET;
445 sa4.sin_port = htons(SAP_PORT);
446 sa = (struct sockaddr*) &sa4;
447 salen = sizeof(sa4);
448 } else {
449 pa_log(__FILE__": invalid SAP address '%s'", sap_address);
450 goto fail;
451 }
452
453 if ((fd = mcast_socket(sa, salen)) < 0)
454 goto fail;
455
456 u = pa_xnew(struct userdata, 1);
457 m->userdata = u;
458 u->module = m;
459 u->core = c;
460 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
461 u->n_sessions = 0;
462
463 u->sap_event = c->mainloop->io_new(c->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u);
464
465 u->by_origin = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
466
467 pa_sap_context_init_recv(&u->sap_context, fd);
468
469 pa_modargs_free(ma);
470
471 return 0;
472
473 fail:
474 if (ma)
475 pa_modargs_free(ma);
476
477 if (fd >= 0)
478 close(fd);
479
480 return -1;
481 }
482
483 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
484 session_free(p, 0);
485 }
486
487 void pa__done(pa_core *c, pa_module*m) {
488 struct userdata *u;
489 assert(c);
490 assert(m);
491
492 if (!(u = m->userdata))
493 return;
494
495 c->mainloop->io_free(u->sap_event);
496 pa_sap_context_destroy(&u->sap_context);
497
498 pa_hashmap_free(u->by_origin, free_func, NULL);
499
500 pa_xfree(u->sink_name);
501 pa_xfree(u);
502 }