2 * Session Announcement Protocol (RFC 2974) muxer
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
39 static int sap_write_close(AVFormatContext *s)
41 struct SAPState *sap = s->priv_data;
44 for (i = 0; i < s->nb_streams; i++) {
45 AVFormatContext *rtpctx = s->streams[i]->priv_data;
48 av_write_trailer(rtpctx);
49 avio_close(rtpctx->pb);
50 avformat_free_context(rtpctx);
51 s->streams[i]->priv_data = NULL;
54 if (sap->last_time && sap->ann && sap->ann_fd) {
55 sap->ann[0] |= 4; /* Session deletion*/
56 url_write(sap->ann_fd, sap->ann, sap->ann_size);
61 url_close(sap->ann_fd);
66 static int sap_write_header(AVFormatContext *s)
68 struct SAPState *sap = s->priv_data;
69 char host[1024], path[1024], url[1024], announce_addr[50] = "";
71 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
72 AVFormatContext **contexts = NULL;
74 struct sockaddr_storage localaddr;
75 socklen_t addrlen = sizeof(localaddr);
78 if (!ff_network_init())
81 /* extract hostname and port */
82 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
83 path, sizeof(path), s->filename);
87 /* search for options */
88 option_list = strrchr(path, '?');
91 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
92 port = strtol(buf, NULL, 10);
94 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
95 same_port = strtol(buf, NULL, 10);
97 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
98 ttl = strtol(buf, NULL, 10);
100 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
101 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
105 if (!announce_addr[0]) {
106 struct addrinfo hints, *ai = NULL;
107 memset(&hints, 0, sizeof(hints));
108 hints.ai_family = AF_UNSPEC;
109 if (getaddrinfo(host, NULL, &hints, &ai)) {
110 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
114 if (ai->ai_family == AF_INET) {
115 /* Also known as sap.mcast.net */
116 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
117 #if HAVE_STRUCT_SOCKADDR_IN6
118 } else if (ai->ai_family == AF_INET6) {
119 /* With IPv6, you can use the same destination in many different
120 * multicast subnets, to choose how far you want it routed.
121 * This one is intended to be routed globally. */
122 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
126 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
127 "address family\n", host);
134 contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
136 ret = AVERROR(ENOMEM);
140 s->start_time_realtime = av_gettime();
141 for (i = 0; i < s->nb_streams; i++) {
144 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
148 ret = url_open(&fd, url, URL_WRONLY);
153 s->streams[i]->priv_data = contexts[i] =
154 ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
155 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
158 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
159 "?ttl=%d&connect=1", ttl);
160 ret = url_open(&sap->ann_fd, url, URL_WRONLY);
166 udp_fd = url_get_file_handle(sap->ann_fd);
167 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
171 if (localaddr.ss_family != AF_INET
172 #if HAVE_STRUCT_SOCKADDR_IN6
173 && localaddr.ss_family != AF_INET6
176 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
180 sap->ann_size = 8192;
181 sap->ann = av_mallocz(sap->ann_size);
186 sap->ann[pos] = (1 << 5);
187 #if HAVE_STRUCT_SOCKADDR_IN6
188 if (localaddr.ss_family == AF_INET6)
189 sap->ann[pos] |= 0x10;
192 sap->ann[pos++] = 0; /* Authentication length */
193 AV_WB16(&sap->ann[pos], av_get_random_seed());
195 if (localaddr.ss_family == AF_INET) {
196 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
197 sizeof(struct in_addr));
198 pos += sizeof(struct in_addr);
199 #if HAVE_STRUCT_SOCKADDR_IN6
201 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
202 sizeof(struct in6_addr));
203 pos += sizeof(struct in6_addr);
207 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
208 pos += strlen(&sap->ann[pos]) + 1;
210 if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
211 sap->ann_size - pos)) {
212 ret = AVERROR_INVALIDDATA;
216 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
217 pos += strlen(&sap->ann[pos]);
220 if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
221 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
234 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
236 AVFormatContext *rtpctx;
237 struct SAPState *sap = s->priv_data;
238 int64_t now = av_gettime();
240 if (!sap->last_time || now - sap->last_time > 5000000) {
241 int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
242 /* Don't abort even if we get "Destination unreachable" */
243 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
245 sap->last_time = now;
247 rtpctx = s->streams[pkt->stream_index]->priv_data;
248 return ff_write_chained(rtpctx, 0, pkt, s);
251 AVOutputFormat ff_sap_muxer = {
253 NULL_IF_CONFIG_SMALL("SAP output format"),
256 sizeof(struct SAPState),
262 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,