]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-send.c
8c9e5f23cf7a47f359f76d380b4f571b3cca25f4
[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 <assert.h>
29 #include <stdio.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/llist.h>
44 #include <pulsecore/source.h>
45 #include <pulsecore/source-output.h>
46 #include <pulsecore/memblockq.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/namereg.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_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 )
71
72 #define DEFAULT_PORT 46000
73 #define SAP_PORT 9875
74 #define DEFAULT_DESTINATION "224.0.0.56"
75 #define MEMBLOCKQ_MAXLENGTH (1024*170)
76 #define DEFAULT_MTU 1280
77 #define SAP_INTERVAL 5000000
78
79 static const char* const valid_modargs[] = {
80 "source",
81 "format",
82 "channels",
83 "rate",
84 "destination",
85 "port",
86 "mtu" ,
87 "loop",
88 NULL
89 };
90
91 struct userdata {
92 pa_module *module;
93 pa_core *core;
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 static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
106 struct userdata *u;
107 assert(o);
108 u = o->userdata;
109
110 if (pa_memblockq_push(u->memblockq, chunk) < 0) {
111 pa_log("Failed to push chunk into memblockq.");
112 return;
113 }
114
115 pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
116 }
117
118 static void source_output_kill(pa_source_output* o) {
119 struct userdata *u;
120 assert(o);
121 u = o->userdata;
122
123 pa_module_unload_request(u->module);
124
125 pa_source_output_disconnect(u->source_output);
126 pa_source_output_unref(u->source_output);
127 u->source_output = NULL;
128 }
129
130 static pa_usec_t source_output_get_latency (pa_source_output *o) {
131 struct userdata *u;
132 assert(o);
133 u = o->userdata;
134
135 return pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &o->sample_spec);
136 }
137
138 static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
139 struct userdata *u = userdata;
140 struct timeval next;
141
142 assert(m);
143 assert(t);
144 assert(tv);
145 assert(u);
146
147 pa_sap_send(&u->sap_context, 0);
148
149 pa_gettimeofday(&next);
150 pa_timeval_add(&next, SAP_INTERVAL);
151 m->time_restart(t, &next);
152 }
153
154 int pa__init(pa_core *c, pa_module*m) {
155 struct userdata *u;
156 pa_modargs *ma = NULL;
157 const char *dest;
158 uint32_t port = DEFAULT_PORT, mtu;
159 int af, fd = -1, sap_fd = -1;
160 pa_source *s;
161 pa_sample_spec ss;
162 pa_channel_map cm;
163 struct sockaddr_in sa4, sap_sa4;
164 struct sockaddr_in6 sa6, sap_sa6;
165 struct sockaddr_storage sa_dst;
166 pa_source_output *o = NULL;
167 uint8_t payload;
168 char *p;
169 int r;
170 socklen_t k;
171 struct timeval tv;
172 char hn[128], *n;
173 int loop = 0;
174 pa_source_output_new_data data;
175
176 assert(c);
177 assert(m);
178
179 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
180 pa_log("failed to parse module arguments");
181 goto fail;
182 }
183
184 if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE, 1))) {
185 pa_log("source does not exist.");
186 goto fail;
187 }
188
189 if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
190 pa_log("failed to parse \"loop\" parameter.");
191 goto fail;
192 }
193
194 ss = s->sample_spec;
195 pa_rtp_sample_spec_fixup(&ss);
196 cm = s->channel_map;
197 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
198 pa_log("failed to parse sample specification");
199 goto fail;
200 }
201
202 if (!pa_rtp_sample_spec_valid(&ss)) {
203 pa_log("specified sample type not compatible with RTP");
204 goto fail;
205 }
206
207 if (ss.channels != cm.channels)
208 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF);
209
210 payload = pa_rtp_payload_from_sample_spec(&ss);
211
212 mtu = (DEFAULT_MTU/pa_frame_size(&ss))*pa_frame_size(&ss);
213
214 if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
215 pa_log("invalid mtu.");
216 goto fail;
217 }
218
219 port = DEFAULT_PORT + ((rand() % 512) << 1);
220 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
221 pa_log("port= expects a numerical argument between 1 and 65535.");
222 goto fail;
223 }
224
225 if (port & 1)
226 pa_log_warn("WARNING: port number not even as suggested in RFC3550!");
227
228 dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION);
229
230 if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
231 sa6.sin6_family = af = AF_INET6;
232 sa6.sin6_port = htons(port);
233 sap_sa6 = sa6;
234 sap_sa6.sin6_port = htons(SAP_PORT);
235 } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
236 sa4.sin_family = af = AF_INET;
237 sa4.sin_port = htons(port);
238 sap_sa4 = sa4;
239 sap_sa4.sin_port = htons(SAP_PORT);
240 } else {
241 pa_log("invalid destination '%s'", dest);
242 goto fail;
243 }
244
245 if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
246 pa_log("socket() failed: %s", pa_cstrerror(errno));
247 goto fail;
248 }
249
250 if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
251 pa_log("connect() failed: %s", pa_cstrerror(errno));
252 goto fail;
253 }
254
255 if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
256 pa_log("socket() failed: %s", pa_cstrerror(errno));
257 goto fail;
258 }
259
260 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) {
261 pa_log("connect() failed: %s", pa_cstrerror(errno));
262 goto fail;
263 }
264
265 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0 ||
266 setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
267 pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
268 goto fail;
269 }
270
271 pa_source_output_new_data_init(&data);
272 data.name = "RTP Monitor Stream";
273 data.driver = __FILE__;
274 data.module = m;
275 data.source = s;
276 pa_source_output_new_data_set_sample_spec(&data, &ss);
277 pa_source_output_new_data_set_channel_map(&data, &cm);
278
279 if (!(o = pa_source_output_new(c, &data, 0))) {
280 pa_log("failed to create source output.");
281 goto fail;
282 }
283
284 o->push = source_output_push;
285 o->kill = source_output_kill;
286 o->get_latency = source_output_get_latency;
287
288 u = pa_xnew(struct userdata, 1);
289 m->userdata = u;
290 o->userdata = u;
291
292 u->module = m;
293 u->core = c;
294 u->source_output = o;
295
296 u->memblockq = pa_memblockq_new(
297 0,
298 MEMBLOCKQ_MAXLENGTH,
299 MEMBLOCKQ_MAXLENGTH,
300 pa_frame_size(&ss),
301 1,
302 0,
303 NULL);
304
305 u->mtu = mtu;
306
307 k = sizeof(sa_dst);
308 r = getsockname(fd, (struct sockaddr*) &sa_dst, &k);
309 assert(r >= 0);
310
311 n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
312
313 p = pa_sdp_build(af,
314 af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
315 af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
316 n, port, payload, &ss);
317
318 pa_xfree(n);
319
320 pa_rtp_context_init_send(&u->rtp_context, fd, c->cookie, payload, pa_frame_size(&ss));
321 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
322
323 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);
324 pa_log_info("SDP-Data:\n%s\n"__FILE__": EOF", p);
325
326 pa_sap_send(&u->sap_context, 0);
327
328 pa_gettimeofday(&tv);
329 pa_timeval_add(&tv, SAP_INTERVAL);
330 u->sap_event = c->mainloop->time_new(c->mainloop, &tv, sap_event_cb, u);
331
332 pa_modargs_free(ma);
333
334 return 0;
335
336 fail:
337 if (ma)
338 pa_modargs_free(ma);
339
340 if (fd >= 0)
341 close(fd);
342
343 if (sap_fd >= 0)
344 close(sap_fd);
345
346 if (o) {
347 pa_source_output_disconnect(o);
348 pa_source_output_unref(o);
349 }
350
351 return -1;
352 }
353
354 void pa__done(pa_core *c, pa_module*m) {
355 struct userdata *u;
356 assert(c);
357 assert(m);
358
359 if (!(u = m->userdata))
360 return;
361
362 c->mainloop->time_free(u->sap_event);
363
364 if (u->source_output) {
365 pa_source_output_disconnect(u->source_output);
366 pa_source_output_unref(u->source_output);
367 }
368
369 pa_rtp_context_destroy(&u->rtp_context);
370
371 pa_sap_send(&u->sap_context, 1);
372 pa_sap_context_destroy(&u->sap_context);
373
374 pa_memblockq_free(u->memblockq);
375
376 pa_xfree(u);
377 }