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