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