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/intreadwrite.h"
29 #include "os_support.h"
30 #include "rtpenc_chain.h"
40 static int sap_write_close(AVFormatContext *s)
42 struct SAPState *sap = s->priv_data;
45 for (i = 0; i < s->nb_streams; i++) {
46 AVFormatContext *rtpctx = s->streams[i]->priv_data;
49 av_write_trailer(rtpctx);
50 avio_close(rtpctx->pb);
51 avformat_free_context(rtpctx);
52 s->streams[i]->priv_data = NULL;
55 if (sap->last_time && sap->ann && sap->ann_fd) {
56 sap->ann[0] |= 4; /* Session deletion*/
57 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
62 ffurl_close(sap->ann_fd);
67 static int sap_write_header(AVFormatContext *s)
69 struct SAPState *sap = s->priv_data;
70 char host[1024], path[1024], url[1024], announce_addr[50] = "";
72 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
73 AVFormatContext **contexts = NULL;
75 struct sockaddr_storage localaddr;
76 socklen_t addrlen = sizeof(localaddr);
79 if (!ff_network_init())
82 /* extract hostname and port */
83 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
84 path, sizeof(path), s->filename);
88 /* search for options */
89 option_list = strrchr(path, '?');
92 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
93 port = strtol(buf, NULL, 10);
95 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
96 same_port = strtol(buf, NULL, 10);
98 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
99 ttl = strtol(buf, NULL, 10);
101 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
102 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
106 if (!announce_addr[0]) {
107 struct addrinfo hints, *ai = NULL;
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = AF_UNSPEC;
110 if (getaddrinfo(host, NULL, &hints, &ai)) {
111 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
115 if (ai->ai_family == AF_INET) {
116 /* Also known as sap.mcast.net */
117 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
118 #if HAVE_STRUCT_SOCKADDR_IN6
119 } else if (ai->ai_family == AF_INET6) {
120 /* With IPv6, you can use the same destination in many different
121 * multicast subnets, to choose how far you want it routed.
122 * This one is intended to be routed globally. */
123 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
127 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
128 "address family\n", host);
135 contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
137 ret = AVERROR(ENOMEM);
141 s->start_time_realtime = av_gettime();
142 for (i = 0; i < s->nb_streams; i++) {
145 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
149 ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
154 s->streams[i]->priv_data = contexts[i] =
155 ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
156 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
159 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
160 "?ttl=%d&connect=1", ttl);
161 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
162 &s->interrupt_callback, NULL);
168 udp_fd = ffurl_get_file_handle(sap->ann_fd);
169 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
173 if (localaddr.ss_family != AF_INET
174 #if HAVE_STRUCT_SOCKADDR_IN6
175 && localaddr.ss_family != AF_INET6
178 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
182 sap->ann_size = 8192;
183 sap->ann = av_mallocz(sap->ann_size);
188 sap->ann[pos] = (1 << 5);
189 #if HAVE_STRUCT_SOCKADDR_IN6
190 if (localaddr.ss_family == AF_INET6)
191 sap->ann[pos] |= 0x10;
194 sap->ann[pos++] = 0; /* Authentication length */
195 AV_WB16(&sap->ann[pos], av_get_random_seed());
197 if (localaddr.ss_family == AF_INET) {
198 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
199 sizeof(struct in_addr));
200 pos += sizeof(struct in_addr);
201 #if HAVE_STRUCT_SOCKADDR_IN6
203 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
204 sizeof(struct in6_addr));
205 pos += sizeof(struct in6_addr);
209 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
210 pos += strlen(&sap->ann[pos]) + 1;
212 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
213 sap->ann_size - pos)) {
214 ret = AVERROR_INVALIDDATA;
218 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
219 pos += strlen(&sap->ann[pos]);
222 if (sap->ann_size > sap->ann_fd->max_packet_size) {
223 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
236 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
238 AVFormatContext *rtpctx;
239 struct SAPState *sap = s->priv_data;
240 int64_t now = av_gettime();
242 if (!sap->last_time || now - sap->last_time > 5000000) {
243 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
244 /* Don't abort even if we get "Destination unreachable" */
245 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
247 sap->last_time = now;
249 rtpctx = s->streams[pkt->stream_index]->priv_data;
250 return ff_write_chained(rtpctx, 0, pkt, s);
253 AVOutputFormat ff_sap_muxer = {
255 .long_name = NULL_IF_CONFIG_SMALL("SAP output format"),
256 .priv_data_size = sizeof(struct SAPState),
257 .audio_codec = CODEC_ID_AAC,
258 .video_codec = CODEC_ID_MPEG4,
259 .write_header = sap_write_header,
260 .write_packet = sap_write_packet,
261 .write_trailer = sap_write_close,
262 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,