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