]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-recv.c
remove all occurences of
[pulseaudio] / src / modules / rtp / module-rtp-recv.c
1
2 /***
3 This file is part of PulseAudio.
4
5 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/llist.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/sink-input.h>
42 #include <pulsecore/memblockq.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/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->mempool) < 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("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("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("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_JOIN_GROUP, &mr6, sizeof(mr6));
240 }
241
242 if (r < 0) {
243 pa_log_info("Joining mcast group failed: %s", pa_cstrerror(errno));
244 goto fail;
245 }
246
247 if (bind(fd, sa, salen) < 0) {
248 pa_log("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 pa_sink_input_new_data data;
269
270 if (u->n_sessions >= MAX_SESSIONS) {
271 pa_log("session limit reached.");
272 goto fail;
273 }
274
275 if (!(sink = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
276 pa_log("sink does not exist.");
277 goto fail;
278 }
279
280 s = pa_xnew0(struct session, 1);
281 s->userdata = u;
282 s->first_packet = 0;
283 s->sdp_info = *sdp_info;
284
285 if ((fd = mcast_socket((const struct sockaddr*) &sdp_info->sa, sdp_info->salen)) < 0)
286 goto fail;
287
288 c = pa_sprintf_malloc("RTP Stream%s%s%s",
289 sdp_info->session_name ? " (" : "",
290 sdp_info->session_name ? sdp_info->session_name : "",
291 sdp_info->session_name ? ")" : "");
292
293 pa_sink_input_new_data_init(&data);
294 data.sink = sink;
295 data.driver = __FILE__;
296 data.name = c;
297 data.module = u->module;
298 pa_sink_input_new_data_set_sample_spec(&data, &sdp_info->sample_spec);
299
300 s->sink_input = pa_sink_input_new(u->core, &data, 0);
301 pa_xfree(c);
302
303 if (!s->sink_input) {
304 pa_log("failed to create sink input.");
305 goto fail;
306 }
307
308 s->sink_input->userdata = s;
309
310 s->sink_input->peek = sink_input_peek;
311 s->sink_input->drop = sink_input_drop;
312 s->sink_input->kill = sink_input_kill;
313 s->sink_input->get_latency = sink_input_get_latency;
314
315 silence = pa_silence_memblock_new(s->userdata->core->mempool,
316 &s->sink_input->sample_spec,
317 (pa_bytes_per_second(&s->sink_input->sample_spec)/128/pa_frame_size(&s->sink_input->sample_spec))*
318 pa_frame_size(&s->sink_input->sample_spec));
319
320 s->memblockq = pa_memblockq_new(
321 0,
322 MEMBLOCKQ_MAXLENGTH,
323 MEMBLOCKQ_MAXLENGTH,
324 pa_frame_size(&s->sink_input->sample_spec),
325 pa_bytes_per_second(&s->sink_input->sample_spec)/10+1,
326 0,
327 silence);
328
329 pa_memblock_unref(silence);
330
331 s->rtp_event = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, rtp_event_cb, s);
332
333 pa_gettimeofday(&tv);
334 pa_timeval_add(&tv, DEATH_TIMEOUT);
335 s->death_event = u->core->mainloop->time_new(u->core->mainloop, &tv, death_event_cb, s);
336
337 pa_hashmap_put(s->userdata->by_origin, s->sdp_info.origin, s);
338
339 pa_rtp_context_init_recv(&s->rtp_context, fd, pa_frame_size(&s->sdp_info.sample_spec));
340
341 pa_log_info("Found new session '%s'", s->sdp_info.session_name);
342
343 u->n_sessions++;
344
345 return s;
346
347 fail:
348 if (s) {
349 if (fd >= 0)
350 close(fd);
351
352 pa_xfree(s);
353 }
354
355 return NULL;
356 }
357
358 static void session_free(struct session *s, int from_hash) {
359 assert(s);
360
361 pa_log_info("Freeing session '%s'", s->sdp_info.session_name);
362
363 s->userdata->core->mainloop->time_free(s->death_event);
364 s->userdata->core->mainloop->io_free(s->rtp_event);
365
366 if (from_hash)
367 pa_hashmap_remove(s->userdata->by_origin, s->sdp_info.origin);
368
369 pa_sink_input_disconnect(s->sink_input);
370 pa_sink_input_unref(s->sink_input);
371
372 pa_memblockq_free(s->memblockq);
373 pa_sdp_info_destroy(&s->sdp_info);
374 pa_rtp_context_destroy(&s->rtp_context);
375
376 assert(s->userdata->n_sessions >= 1);
377 s->userdata->n_sessions--;
378
379 pa_xfree(s);
380 }
381
382 static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
383 struct userdata *u = userdata;
384 int goodbye;
385 pa_sdp_info info;
386 struct session *s;
387
388 assert(m);
389 assert(e);
390 assert(u);
391 assert(fd == u->sap_context.fd);
392 assert(flags == PA_IO_EVENT_INPUT);
393
394 if (pa_sap_recv(&u->sap_context, &goodbye) < 0)
395 return;
396
397 if (!pa_sdp_parse(u->sap_context.sdp_data, &info, goodbye))
398 return;
399
400 if (goodbye) {
401
402 if ((s = pa_hashmap_get(u->by_origin, info.origin)))
403 session_free(s, 1);
404
405 pa_sdp_info_destroy(&info);
406 } else {
407
408 if (!(s = pa_hashmap_get(u->by_origin, info.origin))) {
409 if (!(s = session_new(u, &info)))
410 pa_sdp_info_destroy(&info);
411
412 } else {
413 struct timeval tv;
414
415 pa_gettimeofday(&tv);
416 pa_timeval_add(&tv, DEATH_TIMEOUT);
417 m->time_restart(s->death_event, &tv);
418
419 pa_sdp_info_destroy(&info);
420 }
421 }
422 }
423
424 int pa__init(pa_core *c, pa_module*m) {
425 struct userdata *u;
426 pa_modargs *ma = NULL;
427 struct sockaddr_in sa4;
428 struct sockaddr_in6 sa6;
429 struct sockaddr *sa;
430 socklen_t salen;
431 const char *sap_address;
432 int fd = -1;
433
434 assert(c);
435 assert(m);
436
437 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
438 pa_log("failed to parse module arguments");
439 goto fail;
440 }
441
442 sap_address = pa_modargs_get_value(ma, "sap_address", DEFAULT_SAP_ADDRESS);
443
444 if (inet_pton(AF_INET6, sap_address, &sa6.sin6_addr) > 0) {
445 sa6.sin6_family = AF_INET6;
446 sa6.sin6_port = htons(SAP_PORT);
447 sa = (struct sockaddr*) &sa6;
448 salen = sizeof(sa6);
449 } else if (inet_pton(AF_INET, sap_address, &sa4.sin_addr) > 0) {
450 sa4.sin_family = AF_INET;
451 sa4.sin_port = htons(SAP_PORT);
452 sa = (struct sockaddr*) &sa4;
453 salen = sizeof(sa4);
454 } else {
455 pa_log("invalid SAP address '%s'", sap_address);
456 goto fail;
457 }
458
459 if ((fd = mcast_socket(sa, salen)) < 0)
460 goto fail;
461
462 u = pa_xnew(struct userdata, 1);
463 m->userdata = u;
464 u->module = m;
465 u->core = c;
466 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
467 u->n_sessions = 0;
468
469 u->sap_event = c->mainloop->io_new(c->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u);
470
471 u->by_origin = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
472
473 pa_sap_context_init_recv(&u->sap_context, fd);
474
475 pa_modargs_free(ma);
476
477 return 0;
478
479 fail:
480 if (ma)
481 pa_modargs_free(ma);
482
483 if (fd >= 0)
484 close(fd);
485
486 return -1;
487 }
488
489 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
490 session_free(p, 0);
491 }
492
493 void pa__done(pa_core *c, pa_module*m) {
494 struct userdata *u;
495 assert(c);
496 assert(m);
497
498 if (!(u = m->userdata))
499 return;
500
501 c->mainloop->io_free(u->sap_event);
502 pa_sap_context_destroy(&u->sap_context);
503
504 pa_hashmap_free(u->by_origin, free_func, NULL);
505
506 pa_xfree(u->sink_name);
507 pa_xfree(u);
508 }