2 * Session Announcement Protocol (RFC 2974) muxer
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/parseutils.h"
24 #include "libavutil/random_seed.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/time.h"
31 #include "os_support.h"
32 #include "rtpenc_chain.h"
41 const URLProtocol **protocols;
44 static int sap_write_close(AVFormatContext *s)
46 struct SAPState *sap = s->priv_data;
49 for (i = 0; i < s->nb_streams; i++) {
50 AVFormatContext *rtpctx = s->streams[i]->priv_data;
53 av_write_trailer(rtpctx);
54 avio_close(rtpctx->pb);
55 avformat_free_context(rtpctx);
56 s->streams[i]->priv_data = NULL;
59 if (sap->last_time && sap->ann && sap->ann_fd) {
60 sap->ann[0] |= 4; /* Session deletion*/
61 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
64 av_freep(&sap->protocols);
68 ffurl_close(sap->ann_fd);
73 static int sap_write_header(AVFormatContext *s)
75 struct SAPState *sap = s->priv_data;
76 char host[1024], path[1024], url[1024], announce_addr[50] = "";
78 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
79 AVFormatContext **contexts = NULL;
81 struct sockaddr_storage localaddr;
82 socklen_t addrlen = sizeof(localaddr);
84 AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
86 if (!ff_network_init())
89 /* extract hostname and port */
90 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
91 path, sizeof(path), s->filename);
95 /* search for options */
96 option_list = strrchr(path, '?');
99 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
100 port = strtol(buf, NULL, 10);
102 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
103 same_port = strtol(buf, NULL, 10);
105 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
106 ttl = strtol(buf, NULL, 10);
108 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
109 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
113 if (!announce_addr[0]) {
114 struct addrinfo hints = { 0 }, *ai = NULL;
115 hints.ai_family = AF_UNSPEC;
116 if (getaddrinfo(host, NULL, &hints, &ai)) {
117 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
121 if (ai->ai_family == AF_INET) {
122 /* Also known as sap.mcast.net */
123 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
124 #if HAVE_STRUCT_SOCKADDR_IN6
125 } else if (ai->ai_family == AF_INET6) {
126 /* With IPv6, you can use the same destination in many different
127 * multicast subnets, to choose how far you want it routed.
128 * This one is intended to be routed globally. */
129 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
133 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
134 "address family\n", host);
141 sap->protocols = ffurl_get_protocols(s->protocol_whitelist,
142 s->protocol_blacklist);
143 if (!sap->protocols) {
144 ret = AVERROR(ENOMEM);
148 contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
150 ret = AVERROR(ENOMEM);
154 s->start_time_realtime = av_gettime();
155 for (i = 0; i < s->nb_streams; i++) {
158 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
162 ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL,
163 sap->protocols, NULL);
168 ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
171 s->streams[i]->priv_data = contexts[i];
172 s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
173 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
176 if (s->nb_streams > 0 && title)
177 av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
179 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
180 "?ttl=%d&connect=1", ttl);
181 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
182 &s->interrupt_callback, NULL, sap->protocols, NULL);
188 udp_fd = ffurl_get_file_handle(sap->ann_fd);
189 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
193 if (localaddr.ss_family != AF_INET
194 #if HAVE_STRUCT_SOCKADDR_IN6
195 && localaddr.ss_family != AF_INET6
198 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
202 sap->ann_size = 8192;
203 sap->ann = av_mallocz(sap->ann_size);
208 sap->ann[pos] = (1 << 5);
209 #if HAVE_STRUCT_SOCKADDR_IN6
210 if (localaddr.ss_family == AF_INET6)
211 sap->ann[pos] |= 0x10;
214 sap->ann[pos++] = 0; /* Authentication length */
215 AV_WB16(&sap->ann[pos], av_get_random_seed());
217 if (localaddr.ss_family == AF_INET) {
218 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
219 sizeof(struct in_addr));
220 pos += sizeof(struct in_addr);
221 #if HAVE_STRUCT_SOCKADDR_IN6
223 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
224 sizeof(struct in6_addr));
225 pos += sizeof(struct in6_addr);
229 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
230 pos += strlen(&sap->ann[pos]) + 1;
232 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
233 sap->ann_size - pos)) {
234 ret = AVERROR_INVALIDDATA;
238 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
239 pos += strlen(&sap->ann[pos]);
242 if (sap->ann_size > sap->ann_fd->max_packet_size) {
243 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
256 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
258 AVFormatContext *rtpctx;
259 struct SAPState *sap = s->priv_data;
260 int64_t now = av_gettime_relative();
262 if (!sap->last_time || now - sap->last_time > 5000000) {
263 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
264 /* Don't abort even if we get "Destination unreachable" */
265 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
267 sap->last_time = now;
269 rtpctx = s->streams[pkt->stream_index]->priv_data;
270 return ff_write_chained(rtpctx, 0, pkt, s);
273 AVOutputFormat ff_sap_muxer = {
275 .long_name = NULL_IF_CONFIG_SMALL("SAP output"),
276 .priv_data_size = sizeof(struct SAPState),
277 .audio_codec = AV_CODEC_ID_AAC,
278 .video_codec = AV_CODEC_ID_MPEG4,
279 .write_header = sap_write_header,
280 .write_packet = sap_write_packet,
281 .write_trailer = sap_write_close,
282 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,