• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2   This file is part of PulseAudio.
3 
4   Copyright 2006 Lennart Poettering
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.1 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, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <stdio.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <errno.h>
28 #include <unistd.h>
29 
30 #include <pulse/rtclock.h>
31 #include <pulse/timeval.h>
32 #include <pulse/util.h>
33 #include <pulse/xmalloc.h>
34 
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/source-output.h>
39 #include <pulsecore/memblockq.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/sample-util.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/socket-util.h>
47 #include <pulsecore/arpa-inet.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_LOAD_ONCE(false);
57 PA_MODULE_USAGE(
58         "source=<name of the source> "
59         "format=<sample format> "
60         "channels=<number of channels> "
61         "rate=<sample rate> "
62         "destination_ip=<destination IP address> "
63         "source_ip=<source IP address> "
64         "port=<port number> "
65         "mtu=<maximum transfer unit> "
66         "loop=<loopback to local host?> "
67         "ttl=<ttl value> "
68         "inhibit_auto_suspend=<always|never|only_with_non_monitor_sources>"
69         "stream_name=<name of the stream>"
70 );
71 
72 #define DEFAULT_PORT 46000
73 #define DEFAULT_TTL 1
74 #define SAP_PORT 9875
75 #define DEFAULT_SOURCE_IP "0.0.0.0"
76 #define DEFAULT_DESTINATION_IP "224.0.0.56"
77 #define MEMBLOCKQ_MAXLENGTH (1024*170)
78 #define DEFAULT_MTU 1280
79 #define SAP_INTERVAL (5*PA_USEC_PER_SEC)
80 
81 static const char* const valid_modargs[] = {
82     "source",
83     "format",
84     "channels",
85     "rate",
86     "destination", /* Compatbility */
87     "destination_ip",
88     "source_ip",
89     "port",
90     "mtu" ,
91     "loop",
92     "ttl",
93     "inhibit_auto_suspend",
94     "stream_name",
95     NULL
96 };
97 
98 enum inhibit_auto_suspend {
99     INHIBIT_AUTO_SUSPEND_ALWAYS,
100     INHIBIT_AUTO_SUSPEND_NEVER,
101     INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES
102 };
103 
104 struct userdata {
105     pa_module *module;
106 
107     pa_source_output *source_output;
108     pa_memblockq *memblockq;
109 
110     pa_rtp_context *rtp_context;
111     pa_sap_context sap_context;
112 
113     pa_time_event *sap_event;
114 
115     enum inhibit_auto_suspend inhibit_auto_suspend;
116 };
117 
118 /* Called from I/O thread context */
source_output_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)119 static int source_output_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
120     struct userdata *u;
121     pa_assert_se(u = PA_SOURCE_OUTPUT(o)->userdata);
122 
123     switch (code) {
124         case PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY:
125             *((pa_usec_t*) data) = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->source_output->sample_spec);
126 
127             /* Fall through, the default handler will add in the extra
128              * latency added by the resampler */
129             break;
130     }
131 
132     return pa_source_output_process_msg(o, code, data, offset, chunk);
133 }
134 
135 /* Called from I/O thread context */
source_output_push_cb(pa_source_output * o,const pa_memchunk * chunk)136 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
137     struct userdata *u;
138     pa_source_output_assert_ref(o);
139     pa_assert_se(u = o->userdata);
140 
141     if (pa_memblockq_push(u->memblockq, chunk) < 0) {
142         pa_log_warn("Failed to push chunk into memblockq.");
143         return;
144     }
145 
146     pa_rtp_send(u->rtp_context, u->memblockq);
147 }
148 
get_dont_inhibit_auto_suspend_flag(pa_source * source,enum inhibit_auto_suspend inhibit_auto_suspend)149 static pa_source_output_flags_t get_dont_inhibit_auto_suspend_flag(pa_source *source,
150                                                                    enum inhibit_auto_suspend inhibit_auto_suspend) {
151     pa_assert(source);
152 
153     switch (inhibit_auto_suspend) {
154         case INHIBIT_AUTO_SUSPEND_ALWAYS:
155             return 0;
156 
157         case INHIBIT_AUTO_SUSPEND_NEVER:
158             return PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
159 
160         case INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES:
161             return source->monitor_of ? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND : 0;
162     }
163 
164     pa_assert_not_reached();
165 }
166 
167 /* Called from the main thread. */
source_output_moving_cb(pa_source_output * o,pa_source * dest)168 static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
169     struct userdata *u;
170 
171     pa_assert(o);
172 
173     u = o->userdata;
174 
175     if (!dest)
176         return;
177 
178     o->flags &= ~PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND;
179     o->flags |= get_dont_inhibit_auto_suspend_flag(dest, u->inhibit_auto_suspend);
180 }
181 
182 /* Called from main context */
source_output_kill_cb(pa_source_output * o)183 static void source_output_kill_cb(pa_source_output* o) {
184     struct userdata *u;
185     pa_source_output_assert_ref(o);
186     pa_assert_se(u = o->userdata);
187 
188     pa_module_unload_request(u->module, true);
189 
190     pa_source_output_unlink(u->source_output);
191     pa_source_output_unref(u->source_output);
192     u->source_output = NULL;
193 }
194 
sap_event_cb(pa_mainloop_api * m,pa_time_event * t,const struct timeval * tv,void * userdata)195 static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) {
196     struct userdata *u = userdata;
197 
198     pa_assert(m);
199     pa_assert(t);
200     pa_assert(u);
201 
202     pa_sap_send(&u->sap_context, 0);
203 
204     pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + SAP_INTERVAL);
205 }
206 
pa__init(pa_module * m)207 int pa__init(pa_module*m) {
208     struct userdata *u;
209     pa_modargs *ma = NULL;
210     const char *dst_addr;
211     const char *src_addr;
212     uint32_t port = DEFAULT_PORT, mtu;
213     uint32_t ttl = DEFAULT_TTL;
214     sa_family_t af;
215     int fd = -1, sap_fd = -1;
216     pa_source *s;
217     pa_sample_spec ss;
218     pa_channel_map cm;
219     struct sockaddr_in dst_sa4, dst_sap_sa4, src_sa4, src_sap_sa4;
220 #ifdef HAVE_IPV6
221     struct sockaddr_in6 dst_sa6, dst_sap_sa6, src_sa6, src_sap_sa6;
222 #endif
223     struct sockaddr_storage sa_dst;
224     pa_source_output *o = NULL;
225     uint8_t payload;
226     char *p;
227     int r, j;
228     socklen_t k;
229     char hn[128], *n;
230     bool loop = false;
231     enum inhibit_auto_suspend inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
232     const char *inhibit_auto_suspend_str;
233     pa_source_output_new_data data;
234 
235     pa_assert(m);
236 
237     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
238         pa_log("Failed to parse module arguments");
239         goto fail;
240     }
241 
242     if (!(s = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) {
243         pa_log("Source does not exist.");
244         goto fail;
245     }
246 
247     if (pa_modargs_get_value_boolean(ma, "loop", &loop) < 0) {
248         pa_log("Failed to parse \"loop\" parameter.");
249         goto fail;
250     }
251 
252     if ((inhibit_auto_suspend_str = pa_modargs_get_value(ma, "inhibit_auto_suspend", NULL))) {
253         if (pa_streq(inhibit_auto_suspend_str, "always"))
254             inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ALWAYS;
255         else if (pa_streq(inhibit_auto_suspend_str, "never"))
256             inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_NEVER;
257         else if (pa_streq(inhibit_auto_suspend_str, "only_with_non_monitor_sources"))
258             inhibit_auto_suspend = INHIBIT_AUTO_SUSPEND_ONLY_WITH_NON_MONITOR_SOURCES;
259         else {
260             pa_log("Failed to parse the \"inhibit_auto_suspend\" parameter.");
261             goto fail;
262         }
263     }
264 
265     ss = s->sample_spec;
266     pa_rtp_sample_spec_fixup(&ss);
267     cm = s->channel_map;
268     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
269         pa_log("Failed to parse sample specification");
270         goto fail;
271     }
272 
273     if (!pa_rtp_sample_spec_valid(&ss)) {
274         pa_log("Specified sample type not compatible with RTP");
275         goto fail;
276     }
277 
278     if (ss.channels != cm.channels)
279         pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_AIFF);
280 
281     payload = pa_rtp_payload_from_sample_spec(&ss);
282 
283     mtu = (uint32_t) pa_frame_align(DEFAULT_MTU, &ss);
284 
285     if (pa_modargs_get_value_u32(ma, "mtu", &mtu) < 0 || mtu < 1 || mtu % pa_frame_size(&ss) != 0) {
286         pa_log("Invalid MTU.");
287         goto fail;
288     }
289 
290     port = DEFAULT_PORT + ((uint32_t) (rand() % 512) << 1);
291     if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
292         pa_log("port= expects a numerical argument between 1 and 65535.");
293         goto fail;
294     }
295 
296     if (port & 1)
297         pa_log_warn("Port number not even as suggested in RFC3550!");
298 
299     if (pa_modargs_get_value_u32(ma, "ttl", &ttl) < 0 || ttl < 1 || ttl > 0xFF) {
300         pa_log("ttl= expects a numerical argument between 1 and 255.");
301         goto fail;
302     }
303 
304     src_addr = pa_modargs_get_value(ma, "source_ip", DEFAULT_SOURCE_IP);
305 
306     if (inet_pton(AF_INET, src_addr, &src_sa4.sin_addr) > 0) {
307         src_sa4.sin_family = af = AF_INET;
308         src_sa4.sin_port = htons(0);
309         memset(&src_sa4.sin_zero, 0, sizeof(src_sa4.sin_zero));
310         src_sap_sa4 = src_sa4;
311 #ifdef HAVE_IPV6
312     } else if (inet_pton(AF_INET6, src_addr, &src_sa6.sin6_addr) > 0) {
313         src_sa6.sin6_family = af = AF_INET6;
314         src_sa6.sin6_port = htons(0);
315         src_sa6.sin6_flowinfo = 0;
316         src_sa6.sin6_scope_id = 0;
317         src_sap_sa6 = src_sa6;
318 #endif
319     } else {
320         pa_log("Invalid source address '%s'", src_addr);
321         goto fail;
322     }
323 
324     dst_addr = pa_modargs_get_value(ma, "destination", NULL);
325     if (dst_addr == NULL)
326         dst_addr = pa_modargs_get_value(ma, "destination_ip", DEFAULT_DESTINATION_IP);
327 
328     if (inet_pton(AF_INET, dst_addr, &dst_sa4.sin_addr) > 0) {
329         dst_sa4.sin_family = af = AF_INET;
330         dst_sa4.sin_port = htons((uint16_t) port);
331         memset(&dst_sa4.sin_zero, 0, sizeof(dst_sa4.sin_zero));
332         dst_sap_sa4 = dst_sa4;
333         dst_sap_sa4.sin_port = htons(SAP_PORT);
334 #ifdef HAVE_IPV6
335     } else if (inet_pton(AF_INET6, dst_addr, &dst_sa6.sin6_addr) > 0) {
336         dst_sa6.sin6_family = af = AF_INET6;
337         dst_sa6.sin6_port = htons((uint16_t) port);
338         dst_sa6.sin6_flowinfo = 0;
339         dst_sa6.sin6_scope_id = 0;
340         dst_sap_sa6 = dst_sa6;
341         dst_sap_sa6.sin6_port = htons(SAP_PORT);
342 #endif
343     } else {
344         pa_log("Invalid destination '%s'", dst_addr);
345         goto fail;
346     }
347 
348     if ((fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
349         pa_log("socket() failed: %s", pa_cstrerror(errno));
350         goto fail;
351     }
352 
353     if (af == AF_INET && bind(fd, (struct sockaddr*) &src_sa4, sizeof(src_sa4)) < 0) {
354         pa_log("bind() failed: %s", pa_cstrerror(errno));
355         goto fail;
356 #ifdef HAVE_IPV6
357     } else if (af == AF_INET6 && bind(fd, (struct sockaddr*) &src_sa6, sizeof(src_sa6)) < 0) {
358         pa_log("bind() failed: %s", pa_cstrerror(errno));
359         goto fail;
360 #endif
361     }
362 
363     if (af == AF_INET && connect(fd, (struct sockaddr*) &dst_sa4, sizeof(dst_sa4)) < 0) {
364         pa_log("connect() failed: %s", pa_cstrerror(errno));
365         goto fail;
366 #ifdef HAVE_IPV6
367     } else if (af == AF_INET6 && connect(fd, (struct sockaddr*) &dst_sa6, sizeof(dst_sa6)) < 0) {
368         pa_log("connect() failed: %s", pa_cstrerror(errno));
369         goto fail;
370 #endif
371     }
372 
373     if ((sap_fd = pa_socket_cloexec(af, SOCK_DGRAM, 0)) < 0) {
374         pa_log("socket() failed: %s", pa_cstrerror(errno));
375         goto fail;
376     }
377 
378     if (af == AF_INET && bind(sap_fd, (struct sockaddr*) &src_sap_sa4, sizeof(src_sap_sa4)) < 0) {
379         pa_log("bind() failed: %s", pa_cstrerror(errno));
380         goto fail;
381 #ifdef HAVE_IPV6
382     } else if (af == AF_INET6 && bind(sap_fd, (struct sockaddr*) &src_sap_sa6, sizeof(src_sap_sa6)) < 0) {
383         pa_log("bind() failed: %s", pa_cstrerror(errno));
384         goto fail;
385 #endif
386     }
387 
388     if (af == AF_INET && connect(sap_fd, (struct sockaddr*) &dst_sap_sa4, sizeof(dst_sap_sa4)) < 0) {
389         pa_log("connect() failed: %s", pa_cstrerror(errno));
390         goto fail;
391 #ifdef HAVE_IPV6
392     } else if (af == AF_INET6 && connect(sap_fd, (struct sockaddr*) &dst_sap_sa6, sizeof(dst_sap_sa6)) < 0) {
393         pa_log("connect() failed: %s", pa_cstrerror(errno));
394         goto fail;
395 #endif
396     }
397 
398     j = loop;
399     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0 ||
400         setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &j, sizeof(j)) < 0) {
401         pa_log("IP_MULTICAST_LOOP failed: %s", pa_cstrerror(errno));
402         goto fail;
403     }
404 
405     if (ttl != DEFAULT_TTL) {
406         int _ttl = (int) ttl;
407 
408         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
409             pa_log("IP_MULTICAST_TTL failed: %s", pa_cstrerror(errno));
410             goto fail;
411         }
412 
413         if (setsockopt(sap_fd, IPPROTO_IP, IP_MULTICAST_TTL, &_ttl, sizeof(_ttl)) < 0) {
414             pa_log("IP_MULTICAST_TTL (sap) failed: %s", pa_cstrerror(errno));
415             goto fail;
416         }
417     }
418 
419     /* If the socket queue is full, let's drop packets */
420     pa_make_fd_nonblock(fd);
421     pa_make_udp_socket_low_delay(fd);
422 
423     pa_source_output_new_data_init(&data);
424     pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, "RTP Monitor Stream");
425     pa_proplist_sets(data.proplist, "rtp.source", src_addr);
426     pa_proplist_sets(data.proplist, "rtp.destination", dst_addr);
427     pa_proplist_setf(data.proplist, "rtp.mtu", "%lu", (unsigned long) mtu);
428     pa_proplist_setf(data.proplist, "rtp.port", "%lu", (unsigned long) port);
429     pa_proplist_setf(data.proplist, "rtp.ttl", "%lu", (unsigned long) ttl);
430     data.driver = __FILE__;
431     data.module = m;
432     pa_source_output_new_data_set_source(&data, s, false, true);
433     pa_source_output_new_data_set_sample_spec(&data, &ss);
434     pa_source_output_new_data_set_channel_map(&data, &cm);
435     data.flags |= get_dont_inhibit_auto_suspend_flag(s, inhibit_auto_suspend);
436 
437     pa_source_output_new(&o, m->core, &data);
438     pa_source_output_new_data_done(&data);
439 
440     if (!o) {
441         pa_log("failed to create source output.");
442         goto fail;
443     }
444 
445     o->parent.process_msg = source_output_process_msg;
446     o->push = source_output_push_cb;
447     o->moving = source_output_moving_cb;
448     o->kill = source_output_kill_cb;
449 
450     pa_log_info("Configured source latency of %llu ms.",
451                 (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC);
452 
453     m->userdata = o->userdata = u = pa_xnew(struct userdata, 1);
454     u->module = m;
455     u->source_output = o;
456 
457     u->memblockq = pa_memblockq_new(
458             "module-rtp-send memblockq",
459             0,
460             MEMBLOCKQ_MAXLENGTH,
461             MEMBLOCKQ_MAXLENGTH,
462             &ss,
463             1,
464             0,
465             0,
466             NULL);
467 
468     k = sizeof(sa_dst);
469     pa_assert_se((r = getsockname(fd, (struct sockaddr*) &sa_dst, &k)) >= 0);
470 
471     n = pa_xstrdup(pa_modargs_get_value(ma, "stream_name", NULL));
472     if (n == NULL)
473         n = pa_sprintf_malloc("PulseAudio RTP Stream on %s", pa_get_fqdn(hn, sizeof(hn)));
474 
475     if (af == AF_INET) {
476         p = pa_sdp_build(af,
477                      (void*) &((struct sockaddr_in*) &sa_dst)->sin_addr,
478                      (void*) &dst_sa4.sin_addr,
479                      n, (uint16_t) port, payload, &ss);
480 #ifdef HAVE_IPV6
481     } else {
482         p = pa_sdp_build(af,
483                      (void*) &((struct sockaddr_in6*) &sa_dst)->sin6_addr,
484                      (void*) &dst_sa6.sin6_addr,
485                      n, (uint16_t) port, payload, &ss);
486 #endif
487     }
488 
489     pa_xfree(n);
490 
491     if (!(u->rtp_context = pa_rtp_context_new_send(fd, payload, mtu, &ss)))
492         goto fail;
493     pa_sap_context_init_send(&u->sap_context, sap_fd, p);
494 
495     pa_log_info("RTP stream initialized with mtu %u on %s:%u from %s ttl=%u, payload=%u",
496             mtu, dst_addr, port, src_addr, ttl, payload);
497     pa_log_info("SDP-Data:\n%s\nEOF", p);
498 
499     pa_sap_send(&u->sap_context, 0);
500 
501     u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u);
502     u->inhibit_auto_suspend = inhibit_auto_suspend;
503 
504     pa_source_output_put(u->source_output);
505 
506     pa_modargs_free(ma);
507 
508     return 0;
509 
510 fail:
511     if (ma)
512         pa_modargs_free(ma);
513 
514     if (fd >= 0)
515         pa_close(fd);
516 
517     if (sap_fd >= 0)
518         pa_close(sap_fd);
519 
520     return -1;
521 }
522 
pa__done(pa_module * m)523 void pa__done(pa_module*m) {
524     struct userdata *u;
525     pa_assert(m);
526 
527     if (!(u = m->userdata))
528         return;
529 
530     if (u->sap_event)
531         m->core->mainloop->time_free(u->sap_event);
532 
533     if (u->source_output) {
534         pa_source_output_unlink(u->source_output);
535         pa_source_output_unref(u->source_output);
536     }
537 
538     pa_rtp_context_free(u->rtp_context);
539 
540     pa_sap_send(&u->sap_context, 1);
541     pa_sap_context_destroy(&u->sap_context);
542 
543     if (u->memblockq)
544         pa_memblockq_free(u->memblockq);
545 
546     pa_xfree(u);
547 }
548