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