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