]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-send.c
581e6be31a1fc27abffcd9a9f6f7b24087e5fc0b
[pulseaudio] / src / modules / rtp / module-rtp-send.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <pulse/rtclock.h>
33 #include <pulse/timeval.h>
34 #include <pulse/util.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/source-output.h>
41 #include <pulsecore/memblockq.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/namereg.h>
46 #include <pulsecore/sample-util.h>
47 #include <pulsecore/macro.h>
48 #include <pulsecore/socket-util.h>
49 #include <pulsecore/arpa-inet.h>
50
51 #include "module-rtp-send-symdef.h"
52
53 #include "rtp.h"
54 #include "sdp.h"
55 #include "sap.h"
56
57 PA_MODULE_AUTHOR("Lennart Poettering");
58 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP");
59 PA_MODULE_VERSION(PACKAGE_VERSION);
60 PA_MODULE_LOAD_ONCE(false);
61 PA_MODULE_USAGE(
62 "source=<name of the source> "
63 "format=<sample format> "
64 "channels=<number of channels> "
65 "rate=<sample rate> "
66 "destination_ip=<destination IP address> "
67 "source_ip=<source IP address> "
68 "port=<port number> "
69 "mtu=<maximum transfer unit> "
70 "loop=<loopback to local host?> "
71 "ttl=<ttl value>"
72 );
73
74 #define DEFAULT_PORT 46000
75 #define DEFAULT_TTL 1
76 #define SAP_PORT 9875
77 #define DEFAULT_SOURCE_IP "0.0.0.0"
78 #define DEFAULT_DESTINATION_IP "224.0.0.56"
79 #define MEMBLOCKQ_MAXLENGTH (1024*170)
80 #define DEFAULT_MTU 1280
81 #define SAP_INTERVAL (5*PA_USEC_PER_SEC)
82
83 static const char* const valid_modargs[] = {
84 "source",
85 "format",
86 "channels",
87 "rate",
88 "destination", /* Compatbility */
89 "destination_ip",
90 "source_ip",
91 "port",
92 "mtu" ,
93 "loop",
94 "ttl",
95 NULL
96 };
97
98 struct userdata {
99 pa_module *module;
100
101 pa_source_output *source_output;
102 pa_memblockq *memblockq;
103
104 pa_rtp_context rtp_context;
105 pa_sap_context sap_context;
106 size_t mtu;
107
108 pa_time_event *sap_event;
109 };
110
111 /* Called from I/O thread context */
112 static int source_output_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
113 struct userdata *u;
114 pa_assert_se(u = PA_SOURCE_OUTPUT(o)->userdata);
115
116 switch (code) {
117 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY:
118 *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->source_output->sample_spec);
119
120 /* Fall through, the default handler will add in the extra
121 * latency added by the resampler */
122 break;
123 }
124
125 return pa_source_output_process_msg(o, code, data, offset, chunk);
126 }
127
128 /* Called from I/O thread context */
129 static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
130 struct userdata *u;
131 pa_source_output_assert_ref(o);
132 pa_assert_se(u = o->userdata);
133
134 if (pa_memblockq_push(u->memblockq, chunk) < 0) {
135 pa_log_warn("Failed to push chunk into memblockq.");
136 return;
137 }
138
139 pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
140 }
141
142 /* Called from main context */
143 static void source_output_kill(pa_source_output* o) {
144 struct userdata *u;
145 pa_source_output_assert_ref(o);
146 pa_assert_se(u = o->userdata);
147
148 pa_module_unload_request(u->module, true);
149
150 pa_source_output_unlink(u->source_output);
151 pa_source_output_unref(u->source_output);
152 u->source_output = NULL;
153 }
154
155 static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
156 struct userdata *u = userdata;
157
158 pa_assert(m);
159 pa_assert(t);
160 pa_assert(u);
161
162 pa_sap_send(&u->sap_context, 0);
163
164 pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + SAP_INTERVAL);
165 }
166
167 int pa__init(pa_module*m) {
168 struct userdata *u;
169 pa_modargs *ma = NULL;
170 const char *dst_addr;
171 const char *src_addr;
172 uint32_t port = DEFAULT_PORT, mtu;
173 uint32_t ttl = DEFAULT_TTL;
174 sa_family_t af;
175 int fd = -1, sap_fd = -1;
176 pa_source *s;
177 pa_sample_spec ss;
178 pa_channel_map cm;
179 struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4;
180 #ifdef HAVE_IPV6
181 struct sockaddr_in6 dst_sa6, dst_sap_sa6, src_sa6, src_sap_sa6;
182 #endif
183 struct sockaddr_storage sa_dst;
184 pa_source_output *o = NULL;
185 uint8_t payload;
186 char *p;
187 int r, j;
188 socklen_t k;
189 char hn[128], *n;
190 bool loop = false;
191 pa_source_output_new_data data;
192
193 pa_assert(m);
194
195 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
196 pa_log("Failed to parse module arguments");
197 goto fail;
198 }
199
200 if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
201 pa_log("Source does not exist.");
202 goto fail;
203 }
204
205 if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
206 pa_log("Failed to parse \"loop\" parameter.");
207 goto fail;
208 }
209
210 ss = s->sample_spec;
211 pa_rtp_sample_spec_fixup(&ss);
212 cm = s->channel_map;
213 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
214 pa_log("Failed to parse sample specification");
215 goto fail;
216 }
217
218 if (!pa_rtp_sample_spec_valid(&ss)) {
219 pa_log("Specified sample type not compatible with RTP");
220 goto fail;
221 }
222
223 if (ss.channels != cm.channels)
224 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF);
225
226 payload = pa_rtp_payload_from_sample_spec(&ss);
227
228 mtu = (uint32_t) pa_frame_align(DEFAULT_MTU, &ss);
229
230 if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
231 pa_log("Invalid MTU.");
232 goto fail;
233 }
234
235 port = DEFAULT_PORT + ((uint32_t) (rand() % 512) << 1);
236 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
237 pa_log("port= expects a numerical argument between 1 and 65535.");
238 goto fail;
239 }
240
241 if (port & 1)
242 pa_log_warn("Port number not even as suggested in RFC3550!");
243
244 if (pa_modargs_get_value_u32(ma, "ttl", &ttl) < 0 || ttl < 1 || ttl > 0xFF) {
245 pa_log("ttl= expects a numerical argument between 1 and 255.");
246 goto fail;
247 }
248
249 src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP);
250
251 if (inet_pton(AF_INET, src_addr, &src_sa4.sin_addr) > 0) {
252 src_sa4.sin_family = af = AF_INET;
253 src_sa4.sin_port = htons(0);
254 memset(&src_sa4.sin_zero, 0, sizeof(src_sa4.sin_zero));
255 src_sap_sa4 = src_sa4;
256 #ifdef HAVE_IPV6
257 } else if (inet_pton(AF_INET6, src_addr, &src_sa6.sin6_addr) > 0) {
258 src_sa6.sin6_family = af = AF_INET6;
259 src_sa6.sin6_port = htons(0);
260 src_sa6.sin6_flowinfo = 0;
261 src_sa6.sin6_scope_id = 0;
262 src_sap_sa6 = src_sa6;
263 #endif
264 } else {
265 pa_log("Invalid source address '%s'", src_addr);
266 goto fail;
267 }
268
269 dst_addr = pa_modargs_get_value(ma, "destination", NULL);
270 if (dst_addr == NULL)
271 dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP);
272
273 if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) {
274 dst_sa4.sin_family = af = AF_INET;
275 dst_sa4.sin_port = htons((uint16_t) port);
276 memset(&dst_sa4.sin_zero, 0, sizeof(dst_sa4.sin_zero));
277 dst_sap_sa4 = dst_sa4;
278 dst_sap_sa4.sin_port = htons(SAP_PORT);
279 #ifdef HAVE_IPV6
280 } else if (inet_pton(AF_INET6, dst_addr, &dst_sa6.sin6_addr) > 0) {
281 dst_sa6.sin6_family = af = AF_INET6;
282 dst_sa6.sin6_port = htons((uint16_t) port);
283 dst_sa6.sin6_flowinfo = 0;
284 src_sa6.sin6_scope_id = 0;
285 dst_sap_sa6 = dst_sa6;
286 dst_sap_sa6.sin6_port = htons(SAP_PORT);
287 #endif
288 } else {
289 pa_log("Invalid destination '%s'", dst_addr);
290 goto fail;
291 }
292
293 if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
294 pa_log("socket() failed: %s", pa_cstrerror(errno));
295 goto fail;
296 }
297
298 if (af == AF_INET && bind(fd, (struct sockaddr*) &src_sa4, sizeof(src_sa4)) < 0) {
299 pa_log("bind() failed: %s", pa_cstrerror(errno));
300 goto fail;
301 #ifdef HAVE_IPV6
302 } else if (af == AF_INET6 && bind(fd, (struct sockaddr*) &src_sa6, sizeof(src_sa6)) < 0) {
303 pa_log("bind() failed: %s", pa_cstrerror(errno));
304 goto fail;
305 #endif
306 }
307
308 if (af == AF_INET && connect(fd, (struct sockaddr*) &dst_sa4, sizeof(dst_sa4)) < 0) {
309 pa_log("connect() failed: %s", pa_cstrerror(errno));
310 goto fail;
311 #ifdef HAVE_IPV6
312 } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &dst_sa6, sizeof(dst_sa6)) < 0) {
313 pa_log("connect() failed: %s", pa_cstrerror(errno));
314 goto fail;
315 #endif
316 }
317
318 if ((sap_fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
319 pa_log("socket() failed: %s", pa_cstrerror(errno));
320 goto fail;
321 }
322
323 if (af == AF_INET && bind(sap_fd, (struct sockaddr*) &src_sap_sa4, sizeof(src_sap_sa4)) < 0) {
324 pa_log("bind() failed: %s", pa_cstrerror(errno));
325 goto fail;
326 #ifdef HAVE_IPV6
327 } else if (af == AF_INET6 && bind(sap_fd, (struct sockaddr*) &src_sap_sa6, sizeof(src_sap_sa6)) < 0) {
328 pa_log("bind() failed: %s", pa_cstrerror(errno));
329 goto fail;
330 #endif
331 }
332
333 if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &dst_sap_sa4, sizeof(dst_sap_sa4)) < 0) {
334 pa_log("connect() failed: %s", pa_cstrerror(errno));
335 goto fail;
336 #ifdef HAVE_IPV6
337 } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &dst_sap_sa6, sizeof(dst_sap_sa6)) < 0) {
338 pa_log("connect() failed: %s", pa_cstrerror(errno));
339 goto fail;
340 #endif
341 }
342
343 j = !!loop;
344 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 ||
345 setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) {
346 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
347 goto fail;
348 }
349
350 if (ttl != DEFAULT_TTL) {
351 int _ttl = (int) ttl;
352
353 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
354 pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno));
355 goto fail;
356 }
357
358 if (setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
359 pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno));
360 goto fail;
361 }
362 }
363
364 /* If the socket queue is full, let's drop packets */
365 pa_make_fd_nonblock(fd);
366 pa_make_udp_socket_low_delay(fd);
367
368 pa_source_output_new_data_init(&data);
369 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream");
370 pa_proplist_sets(data.proplist, "rtp.source", src_addr);
371 pa_proplist_sets(data.proplist, "rtp.destination", dst_addr);
372 pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu);
373 pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port);
374 pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl);
375 data.driver = __FILE__;
376 data.module = m;
377 pa_source_output_new_data_set_source(&data, s, false);
378 pa_source_output_new_data_set_sample_spec(&data, &ss);
379 pa_source_output_new_data_set_channel_map(&data, &cm);
380
381 pa_source_output_new(&o, m->core, &data);
382 pa_source_output_new_data_done(&data);
383
384 if (!o) {
385 pa_log("failed to create source output.");
386 goto fail;
387 }
388
389 o->parent.process_msg = source_output_process_msg;
390 o->push = source_output_push;
391 o->kill = source_output_kill;
392
393 pa_log_info("Configured source latency of %llu ms.",
394 (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC);
395
396 m->userdata = o->userdata = u = pa_xnew(struct userdata, 1);
397 u->module = m;
398 u->source_output = o;
399
400 u->memblockq = pa_memblockq_new(
401 "module-rtp-send memblockq",
402 0,
403 MEMBLOCKQ_MAXLENGTH,
404 MEMBLOCKQ_MAXLENGTH,
405 &ss,
406 1,
407 0,
408 0,
409 NULL);
410
411 u->mtu = mtu;
412
413 k = sizeof(sa_dst);
414 pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
415
416 n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
417
418 if (af == AF_INET) {
419 p = pa_sdp_build(af,
420 (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr,
421 (void*) &dst_sa4.sin_addr,
422 n, (uint16_t) port, payload, &ss);
423 #ifdef HAVE_IPV6
424 } else {
425 p = pa_sdp_build(af,
426 (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
427 (void*) &dst_sa6.sin6_addr,
428 n, (uint16_t) port, payload, &ss);
429 #endif
430 }
431
432 pa_xfree(n);
433
434 pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
435 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
436
437 pa_log_info("RTP stream initialized with mtu %u on %s:%u from %s ttl=%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dst_addr, port, src_addr, ttl, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
438 pa_log_info("SDP-Data:\n%s\nEOF", p);
439
440 pa_sap_send(&u->sap_context, 0);
441
442 u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u);
443
444 pa_source_output_put(u->source_output);
445
446 pa_modargs_free(ma);
447
448 return 0;
449
450 fail:
451 if (ma)
452 pa_modargs_free(ma);
453
454 if (fd >= 0)
455 pa_close(fd);
456
457 if (sap_fd >= 0)
458 pa_close(sap_fd);
459
460 if (o) {
461 pa_source_output_unlink(o);
462 pa_source_output_unref(o);
463 }
464
465 return -1;
466 }
467
468 void pa__done(pa_module*m) {
469 struct userdata *u;
470 pa_assert(m);
471
472 if (!(u = m->userdata))
473 return;
474
475 if (u->sap_event)
476 m->core->mainloop->time_free(u->sap_event);
477
478 if (u->source_output) {
479 pa_source_output_unlink(u->source_output);
480 pa_source_output_unref(u->source_output);
481 }
482
483 pa_rtp_context_destroy(&u->rtp_context);
484
485 pa_sap_send(&u->sap_context, 1);
486 pa_sap_context_destroy(&u->sap_context);
487
488 if (u->memblockq)
489 pa_memblockq_free(u->memblockq);
490
491 pa_xfree(u);
492 }