]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-send.c
really create glitch-free branch
[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_LOAD_ONCE(FALSE);
64 PA_MODULE_USAGE(
65 "source=<name of the source> "
66 "format=<sample format> "
67 "channels=<number of channels> "
68 "rate=<sample rate> "
69 "destination=<destination IP address> "
70 "port=<port number> "
71 "mtu=<maximum transfer unit> "
72 "loop=<loopback to local host?>"
73 );
74
75 #define DEFAULT_PORT 46000
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 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);
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 struct timeval next;
154
155 pa_assert(m);
156 pa_assert(t);
157 pa_assert(tv);
158 pa_assert(u);
159
160 pa_sap_send(&u->sap_context, 0);
161
162 pa_gettimeofday(&next);
163 pa_timeval_add(&next, SAP_INTERVAL * PA_USEC_PER_SEC);
164 m->time_restart(t, &next);
165 }
166
167 int pa__init(pa_module*m) {
168 struct userdata *u;
169 pa_modargs *ma = NULL;
170 const char *dest;
171 uint32_t port = DEFAULT_PORT, mtu;
172 int af, 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 struct sockaddr_in6 sa6, sap_sa6;
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 struct timeval tv;
185 char hn[128], *n;
186 pa_bool_t loop = FALSE;
187 pa_source_output_new_data data;
188
189 pa_assert(m);
190
191 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
192 pa_log("Failed to parse module arguments");
193 goto fail;
194 }
195
196 if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE, 1))) {
197 pa_log("Source does not exist.");
198 goto fail;
199 }
200
201 if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
202 pa_log("Failed to parse \"loop\" parameter.");
203 goto fail;
204 }
205
206 ss = s->sample_spec;
207 pa_rtp_sample_spec_fixup(&ss);
208 cm = s->channel_map;
209 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
210 pa_log("Failed to parse sample specification");
211 goto fail;
212 }
213
214 if (!pa_rtp_sample_spec_valid(&ss)) {
215 pa_log("Specified sample type not compatible with RTP");
216 goto fail;
217 }
218
219 if (ss.channels != cm.channels)
220 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF);
221
222 payload = pa_rtp_payload_from_sample_spec(&ss);
223
224 mtu = pa_frame_align(DEFAULT_MTU, &ss);
225
226 if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
227 pa_log("Invalid MTU.");
228 goto fail;
229 }
230
231 port = DEFAULT_PORT + ((rand() % 512) << 1);
232 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
233 pa_log("port= expects a numerical argument between 1 and 65535.");
234 goto fail;
235 }
236
237 if (port & 1)
238 pa_log_warn("Port number not even as suggested in RFC3550!");
239
240 dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
241
242 if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
243 sa6.sin6_family = af = AF_INET6;
244 sa6.sin6_port = htons(port);
245 sap_sa6 = sa6;
246 sap_sa6.sin6_port = htons(SAP_PORT);
247 } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
248 sa4.sin_family = af = AF_INET;
249 sa4.sin_port = htons(port);
250 sap_sa4 = sa4;
251 sap_sa4.sin_port = htons(SAP_PORT);
252 } else {
253 pa_log("Invalid destination '%s'", dest);
254 goto fail;
255 }
256
257 if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
258 pa_log("socket() failed: %s", pa_cstrerror(errno));
259 goto fail;
260 }
261
262 if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
263 pa_log("connect() failed: %s", pa_cstrerror(errno));
264 goto fail;
265 }
266
267 if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
268 pa_log("socket() failed: %s", pa_cstrerror(errno));
269 goto fail;
270 }
271
272 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) {
273 pa_log("connect() failed: %s", pa_cstrerror(errno));
274 goto fail;
275 }
276
277 j = !!loop;
278 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 ||
279 setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) {
280 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
281 goto fail;
282 }
283
284 /* If the socket queue is full, let's drop packets */
285 pa_make_fd_nonblock(fd);
286 pa_make_udp_socket_low_delay(fd);
287 pa_make_fd_cloexec(fd);
288 pa_make_fd_cloexec(sap_fd);
289
290 pa_source_output_new_data_init(&data);
291 data.name = "RTP Monitor Stream";
292 data.driver = __FILE__;
293 data.module = m;
294 data.source = s;
295 pa_source_output_new_data_set_sample_spec(&data, &ss);
296 pa_source_output_new_data_set_channel_map(&data, &cm);
297
298 if (!(o = pa_source_output_new(m->core, &data, 0))) {
299 pa_log("failed to create source output.");
300 goto fail;
301 }
302
303 o->parent.process_msg = source_output_process_msg;
304 o->push = source_output_push;
305 o->kill = source_output_kill;
306
307 u = pa_xnew(struct userdata, 1);
308 m->userdata = u;
309 o->userdata = u;
310
311 u->module = m;
312 u->source_output = o;
313
314 u->memblockq = pa_memblockq_new(
315 0,
316 MEMBLOCKQ_MAXLENGTH,
317 MEMBLOCKQ_MAXLENGTH,
318 pa_frame_size(&ss),
319 1,
320 0,
321 NULL);
322
323 u->mtu = mtu;
324
325 k = sizeof(sa_dst);
326 pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
327
328 n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
329
330 p = pa_sdp_build(af,
331 af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
332 af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
333 n, port, payload, &ss);
334
335 pa_xfree(n);
336
337 pa_rtp_context_init_send(&u->rtp_context, fd, m->core->cookie, payload, pa_frame_size(&ss));
338 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
339
340 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);
341 pa_log_info("SDP-Data:\n%s\nEOF", p);
342
343 pa_sap_send(&u->sap_context, 0);
344
345 pa_gettimeofday(&tv);
346 pa_timeval_add(&tv, SAP_INTERVAL * PA_USEC_PER_SEC);
347 u->sap_event = m->core->mainloop->time_new(m->core->mainloop, &tv, sap_event_cb, u);
348
349 pa_source_output_put(u->source_output);
350
351 pa_modargs_free(ma);
352
353 return 0;
354
355 fail:
356 if (ma)
357 pa_modargs_free(ma);
358
359 if (fd >= 0)
360 pa_close(fd);
361
362 if (sap_fd >= 0)
363 pa_close(sap_fd);
364
365 if (o) {
366 pa_source_output_unlink(o);
367 pa_source_output_unref(o);
368 }
369
370 return -1;
371 }
372
373 void pa__done(pa_module*m) {
374 struct userdata *u;
375 pa_assert(m);
376
377 if (!(u = m->userdata))
378 return;
379
380 if (u->sap_event)
381 m->core->mainloop->time_free(u->sap_event);
382
383 if (u->source_output) {
384 pa_source_output_unlink(u->source_output);
385 pa_source_output_unref(u->source_output);
386 }
387
388 pa_rtp_context_destroy(&u->rtp_context);
389
390 pa_sap_send(&u->sap_context, 1);
391 pa_sap_context_destroy(&u->sap_context);
392
393 if (u->memblockq)
394 pa_memblockq_free(u->memblockq);
395
396 pa_xfree(u);
397 }