]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-send.c
make O_CLOEXEC, O_NONBLOCK and socket low latency fd ops more uniform: always return...
[pulseaudio] / src / modules / rtp / module-rtp-send.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/llist.h>
43 #include <pulsecore/source.h>
44 #include <pulsecore/source-output.h>
45 #include <pulsecore/memblockq.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/namereg.h>
50 #include <pulsecore/sample-util.h>
51 #include <pulsecore/macro.h>
52 #include <pulsecore/socket-util.h>
53
54 #include "module-rtp-send-symdef.h"
55
56 #include "rtp.h"
57 #include "sdp.h"
58 #include "sap.h"
59
60 PA_MODULE_AUTHOR("Lennart Poettering")
61 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP/SAP/SDP")
62 PA_MODULE_VERSION(PACKAGE_VERSION)
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 )
73
74 #define DEFAULT_PORT 46000
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
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 NULL
91 };
92
93 struct userdata {
94 pa_module *module;
95
96 pa_source_output *source_output;
97 pa_memblockq *memblockq;
98
99 pa_rtp_context rtp_context;
100 pa_sap_context sap_context;
101 size_t mtu;
102
103 pa_time_event *sap_event;
104 };
105
106 /* Called from I/O thread context */
107 static int source_output_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
108 struct userdata *u;
109 pa_assert_se(u = PA_SOURCE_OUTPUT(o)->userdata);
110
111 switch (code) {
112 case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY:
113 *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->source_output->sample_spec);
114
115 /* Fall through, the default handler will add in the extra
116 * latency added by the resampler */
117 break;
118 }
119
120 return pa_source_output_process_msg(o, code, data, offset, chunk);
121 }
122
123 /* Called from I/O thread context */
124 static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
125 struct userdata *u;
126 pa_source_output_assert_ref(o);
127 pa_assert_se(u = o->userdata);
128
129 if (pa_memblockq_push(u->memblockq, chunk) < 0) {
130 pa_log_warn("Failed to push chunk into memblockq.");
131 return;
132 }
133
134 pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
135 }
136
137 /* Called from main context */
138 static void source_output_kill(pa_source_output* o) {
139 struct userdata *u;
140 pa_source_output_assert_ref(o);
141 pa_assert_se(u = o->userdata);
142
143 pa_module_unload_request(u->module);
144
145 pa_source_output_unlink(u->source_output);
146 pa_source_output_unref(u->source_output);
147 u->source_output = NULL;
148 }
149
150 static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
151 struct userdata *u = userdata;
152 struct timeval next;
153
154 pa_assert(m);
155 pa_assert(t);
156 pa_assert(tv);
157 pa_assert(u);
158
159 pa_sap_send(&u->sap_context, 0);
160
161 pa_gettimeofday(&next);
162 pa_timeval_add(&next, SAP_INTERVAL * PA_USEC_PER_SEC);
163 m->time_restart(t, &next);
164 }
165
166 int pa__init(pa_module*m) {
167 struct userdata *u;
168 pa_modargs *ma = NULL;
169 const char *dest;
170 uint32_t port = DEFAULT_PORT, mtu;
171 int af, fd = -1, sap_fd = -1;
172 pa_source *s;
173 pa_sample_spec ss;
174 pa_channel_map cm;
175 struct sockaddr_in sa4, sap_sa4;
176 struct sockaddr_in6 sa6, sap_sa6;
177 struct sockaddr_storage sa_dst;
178 pa_source_output *o = NULL;
179 uint8_t payload;
180 char *p;
181 int r;
182 socklen_t k;
183 struct timeval tv;
184 char hn[128], *n;
185 int loop = 0;
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, 1))) {
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 = 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 + ((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 dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
240
241 if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
242 sa6.sin6_family = af = AF_INET6;
243 sa6.sin6_port = htons(port);
244 sap_sa6 = sa6;
245 sap_sa6.sin6_port = htons(SAP_PORT);
246 } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
247 sa4.sin_family = af = AF_INET;
248 sa4.sin_port = htons(port);
249 sap_sa4 = sa4;
250 sap_sa4.sin_port = htons(SAP_PORT);
251 } else {
252 pa_log("Invalid destination '%s'", dest);
253 goto fail;
254 }
255
256 if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
257 pa_log("socket() failed: %s", pa_cstrerror(errno));
258 goto fail;
259 }
260
261 if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
262 pa_log("connect() failed: %s", pa_cstrerror(errno));
263 goto fail;
264 }
265
266 if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
267 pa_log("socket() failed: %s", pa_cstrerror(errno));
268 goto fail;
269 }
270
271 if (connect(sap_fd, af == AF_INET ? (struct sockaddr*) &sap_sa4 : (struct sockaddr*) &sap_sa6, af == AF_INET ? sizeof(sap_sa4) : sizeof(sap_sa6)) < 0) {
272 pa_log("connect() failed: %s", pa_cstrerror(errno));
273 goto fail;
274 }
275
276 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0 ||
277 setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
278 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
279 goto fail;
280 }
281
282 /* If the socket queue is full, let's drop packets */
283 pa_make_fd_nonblock(fd);
284 pa_make_udp_socket_low_delay(fd);
285 pa_make_fd_cloexec(fd);
286 pa_make_fd_cloexec(sap_fd);
287
288 pa_source_output_new_data_init(&data);
289 data.name = "RTP Monitor Stream";
290 data.driver = __FILE__;
291 data.module = m;
292 data.source = s;
293 pa_source_output_new_data_set_sample_spec(&data, &ss);
294 pa_source_output_new_data_set_channel_map(&data, &cm);
295
296 if (!(o = pa_source_output_new(m->core, &data, 0))) {
297 pa_log("failed to create source output.");
298 goto fail;
299 }
300
301 o->parent.process_msg = source_output_process_msg;
302 o->push = source_output_push;
303 o->kill = source_output_kill;
304
305 u = pa_xnew(struct userdata, 1);
306 m->userdata = u;
307 o->userdata = u;
308
309 u->module = m;
310 u->source_output = o;
311
312 u->memblockq = pa_memblockq_new(
313 0,
314 MEMBLOCKQ_MAXLENGTH,
315 MEMBLOCKQ_MAXLENGTH,
316 pa_frame_size(&ss),
317 1,
318 0,
319 NULL);
320
321 u->mtu = mtu;
322
323 k = sizeof(sa_dst);
324 pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
325
326 n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
327
328 p = pa_sdp_build(af,
329 af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
330 af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
331 n, port, payload, &ss);
332
333 pa_xfree(n);
334
335 pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
336 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
337
338 pa_log_info("RTP stream initialized with mtu %u on %s:%u, SSRC=0x%08x, payload=%u, initial sequence #%u", mtu, dest, port, u->rtp_context.ssrc, payload, u->rtp_context.sequence);
339 pa_log_info("SDP-Data:\n%s\nEOF", p);
340
341 pa_sap_send(&u->sap_context, 0);
342
343 pa_gettimeofday(&tv);
344 pa_timeval_add(&tv, SAP_INTERVAL * PA_USEC_PER_SEC);
345 u->sap_event = m->core->mainloop->time_new(m->core->mainloop, &tv, sap_event_cb, u);
346
347 pa_source_output_put(u->source_output);
348
349 pa_modargs_free(ma);
350
351 return 0;
352
353 fail:
354 if (ma)
355 pa_modargs_free(ma);
356
357 if (fd >= 0)
358 pa_close(fd);
359
360 if (sap_fd >= 0)
361 pa_close(sap_fd);
362
363 if (o) {
364 pa_source_output_unlink(o);
365 pa_source_output_unref(o);
366 }
367
368 return -1;
369 }
370
371 void pa__done(pa_module*m) {
372 struct userdata *u;
373 pa_assert(m);
374
375 if (!(u = m->userdata))
376 return;
377
378 if (u->sap_event)
379 m->core->mainloop->time_free(u->sap_event);
380
381 if (u->source_output) {
382 pa_source_output_unlink(u->source_output);
383 pa_source_output_unref(u->source_output);
384 }
385
386 pa_rtp_context_destroy(&u->rtp_context);
387
388 pa_sap_send(&u->sap_context, 1);
389 pa_sap_context_destroy(&u->sap_context);
390
391 if (u->memblockq)
392 pa_memblockq_free(u->memblockq);
393
394 pa_xfree(u);
395 }