]> code.delx.au - pulseaudio/blob - src/modules/rtp/module-rtp-monitor.c
* change default multicast address
[pulseaudio] / src / modules / rtp / module-rtp-monitor.c
1
2 /***
3 This file is part of polypaudio.
4
5 polypaudio is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 2 of the License,
8 or (at your option) any later version.
9
10 polypaudio is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with polypaudio; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <assert.h>
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 <polypcore/module.h>
35 #include <polypcore/llist.h>
36 #include <polypcore/source.h>
37 #include <polypcore/source-output.h>
38 #include <polypcore/memblockq.h>
39 #include <polypcore/log.h>
40 #include <polypcore/util.h>
41 #include <polypcore/xmalloc.h>
42 #include <polypcore/modargs.h>
43 #include <polypcore/namereg.h>
44
45 #include "module-rtp-monitor-symdef.h"
46
47 #include "rtp.h"
48 #include "sdp.h"
49 #include "sap.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("Read data from source and send it to the network via RTP")
53 PA_MODULE_VERSION(PACKAGE_VERSION)
54 PA_MODULE_USAGE(
55 "source=<name for the source> "
56 "format=<sample format> "
57 "channels=<number of channels> "
58 "rate=<sample rate> "
59 "destinaton=<destination IP address> "
60 "port=<port number> "
61 "mtu=<maximum transfer unit> "
62 )
63
64 #define DEFAULT_PORT 5004
65 #define SAP_PORT 9875
66 #define DEFAULT_DESTINATION "224.0.1.2"
67 #define MEMBLOCKQ_MAXLENGTH (1024*170)
68 #define DEFAULT_MTU 1024
69 #define SAP_INTERVAL 5000000
70
71 static const char* const valid_modargs[] = {
72 "source",
73 "format",
74 "channels",
75 "rate",
76 "destination",
77 "port",
78 NULL
79 };
80
81 struct userdata {
82 pa_module *module;
83 pa_core *core;
84
85 pa_source_output *source_output;
86 pa_memblockq *memblockq;
87
88 pa_rtp_context rtp_context;
89 pa_sap_context sap_context;
90 size_t mtu;
91
92 pa_time_event *sap_event;
93 };
94
95 static void source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
96 struct userdata *u;
97 assert(o);
98 u = o->userdata;
99
100 if (pa_memblockq_push(u->memblockq, chunk) < 0) {
101 pa_log(__FILE__": Failed to push chunk into memblockq.");
102 return;
103 }
104
105 pa_rtp_send(&u->rtp_context, u->mtu, u->memblockq);
106 }
107
108 static void source_output_kill(pa_source_output* o) {
109 struct userdata *u;
110 assert(o);
111 u = o->userdata;
112
113 pa_module_unload_request(u->module);
114
115 pa_source_output_disconnect(u->source_output);
116 pa_source_output_unref(u->source_output);
117 u->source_output = NULL;
118 }
119
120 static pa_usec_t source_output_get_latency (pa_source_output *o) {
121 struct userdata *u;
122 assert(o);
123 u = o->userdata;
124
125 return pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &o->sample_spec);
126 }
127
128 static void sap_event(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
129 struct userdata *u = userdata;
130 struct timeval next;
131
132 assert(m);
133 assert(t);
134 assert(tv);
135 assert(u);
136
137 pa_sap_send(&u->sap_context, 0);
138
139 pa_gettimeofday(&next);
140 pa_timeval_add(&next, SAP_INTERVAL);
141 m->time_restart(t, &next);
142 }
143
144 int pa__init(pa_core *c, pa_module*m) {
145 struct userdata *u;
146 pa_modargs *ma = NULL;
147 const char *dest;
148 uint32_t port = DEFAULT_PORT, mtu;
149 int af, fd = -1, sap_fd = -1;
150 pa_source *s;
151 pa_sample_spec ss;
152 pa_channel_map cm;
153 struct sockaddr_in sa4, sap_sa4;
154 struct sockaddr_in6 sa6, sap_sa6;
155 struct sockaddr_storage sa_dst;
156 pa_source_output *o = NULL;
157 uint8_t payload;
158 char *p;
159 int r;
160 socklen_t k;
161 struct timeval tv;
162
163 assert(c);
164 assert(m);
165
166 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
167 pa_log(__FILE__": failed to parse module arguments");
168 goto fail;
169 }
170
171 if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE, 1))) {
172 pa_log(__FILE__": source does not exist.");
173 goto fail;
174 }
175
176 ss = s->sample_spec;
177 pa_rtp_sample_spec_fixup(&ss);
178 cm = s->channel_map;
179 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
180 pa_log(__FILE__": failed to parse sample specification");
181 goto fail;
182 }
183
184 if (!pa_rtp_sample_spec_valid(&ss)) {
185 pa_log(__FILE__": specified sample type not compatible with RTP");
186 goto fail;
187 }
188
189 if (ss.channels != cm.channels)
190 pa_channel_map_init_auto(&cm, ss.channels);
191
192 payload = pa_rtp_payload_type(&ss);
193
194 mtu = (DEFAULT_MTU/pa_frame_size(&ss))*pa_frame_size(&ss);
195
196 if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
197 pa_log(__FILE__": invalid mtu.");
198 goto fail;
199 }
200
201 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
202 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
203 goto fail;
204 }
205
206 if ((dest = pa_modargs_get_value(ma, "destination", DEFAULT_DESTINATION))) {
207 if (inet_pton(AF_INET6, dest, &sa6.sin6_addr) > 0) {
208 sa6.sin6_family = af = AF_INET6;
209 sa6.sin6_port = htons(port);
210 sap_sa6 = sa6;
211 sap_sa6.sin6_port = htons(SAP_PORT);
212 } else if (inet_pton(AF_INET, dest, &sa4.sin_addr) > 0) {
213 sa4.sin_family = af = AF_INET;
214 sa4.sin_port = htons(port);
215 sap_sa4 = sa4;
216 sap_sa4.sin_port = htons(SAP_PORT);
217 } else {
218 pa_log(__FILE__": invalid destination '%s'", dest);
219 goto fail;
220 }
221 }
222
223 if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
224 pa_log(__FILE__": socket() failed: %s", strerror(errno));
225 goto fail;
226 }
227
228 if (connect(fd, af == AF_INET ? (struct sockaddr*) &sa4 : (struct sockaddr*) &sa6, af == AF_INET ? sizeof(sa4) : sizeof(sa6)) < 0) {
229 pa_log(__FILE__": connect() failed: %s", strerror(errno));
230 goto fail;
231 }
232
233 if ((sap_fd = socket(af, SOCK_DGRAM, 0)) < 0) {
234 pa_log(__FILE__": socket() failed: %s", strerror(errno));
235 goto fail;
236 }
237
238 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) {
239 pa_log(__FILE__": connect() failed: %s", strerror(errno));
240 goto fail;
241 }
242
243 if (!(o = pa_source_output_new(s, __FILE__, "RTP Monitor Stream", &ss, &cm, PA_RESAMPLER_INVALID))) {
244 pa_log(__FILE__": failed to create source output.");
245 goto fail;
246 }
247
248 o->push = source_output_push;
249 o->kill = source_output_kill;
250 o->get_latency = source_output_get_latency;
251 o->owner = m;
252
253 u = pa_xnew(struct userdata, 1);
254 m->userdata = u;
255 o->userdata = u;
256
257 u->module = m;
258 u->core = c;
259 u->source_output = o;
260
261 u->memblockq = pa_memblockq_new(
262 0,
263 MEMBLOCKQ_MAXLENGTH,
264 MEMBLOCKQ_MAXLENGTH,
265 pa_frame_size(&ss),
266 1,
267 0,
268 NULL,
269 c->memblock_stat);
270
271 u->mtu = mtu;
272
273 k = sizeof(sa_dst);
274 r = getsockname(fd, (struct sockaddr*) &sa_dst, &k);
275 assert(r >= 0);
276
277 p = pa_sdp_build(af,
278 af == AF_INET ? (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr : (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
279 af == AF_INET ? (void*) &sa4.sin_addr : (void*) &sa6.sin6_addr,
280 "Polypaudio RTP Stream", port, payload, &ss);
281
282 pa_rtp_context_init_send(&u->rtp_context, fd, 0, payload, pa_frame_size(&ss));
283 pa_sap_context_init_send(&u->sap_context, sap_fd, p);
284
285 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);
286 pa_log_info("SDP-Data:\n%s\nEOF", p);
287
288 pa_sap_send(&u->sap_context, 0);
289
290 pa_gettimeofday(&tv);
291 pa_timeval_add(&tv, SAP_INTERVAL);
292 u->sap_event = c->mainloop->time_new(c->mainloop, &tv, sap_event, u);
293
294 pa_modargs_free(ma);
295
296 return 0;
297
298 fail:
299 if (ma)
300 pa_modargs_free(ma);
301
302 if (fd >= 0)
303 close(fd);
304
305 if (sap_fd >= 0)
306 close(sap_fd);
307
308 if (o) {
309 pa_source_output_disconnect(o);
310 pa_source_output_unref(o);
311 }
312
313 return -1;
314 }
315
316 void pa__done(pa_core *c, pa_module*m) {
317 struct userdata *u;
318 assert(c);
319 assert(m);
320
321 if (!(u = m->userdata))
322 return;
323
324 c->mainloop->time_free(u->sap_event);
325
326 if (u->source_output) {
327 pa_source_output_disconnect(u->source_output);
328 pa_source_output_unref(u->source_output);
329 }
330
331 pa_rtp_context_destroy(&u->rtp_context);
332
333 pa_sap_send(&u->sap_context, 1);
334 pa_sap_context_destroy(&u->sap_context);
335
336 pa_memblockq_free(u->memblockq);
337
338 pa_xfree(u);
339 }