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