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