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