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/dict.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/time.h"
31 #include "os_support.h"
32 #include "rtpenc_chain.h"
42 static int sap_write_close(AVFormatContext *s)
44 struct SAPState *sap = s->priv_data;
47 for (i = 0; i < s->nb_streams; i++) {
48 AVFormatContext *rtpctx = s->streams[i]->priv_data;
51 av_write_trailer(rtpctx);
52 avio_closep(&rtpctx->pb);
53 avformat_free_context(rtpctx);
54 s->streams[i]->priv_data = NULL;
57 if (sap->last_time && sap->ann && sap->ann_fd) {
58 sap->ann[0] |= 4; /* Session deletion*/
59 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
63 ffurl_closep(&sap->ann_fd);
68 static int sap_write_header(AVFormatContext *s)
70 struct SAPState *sap = s->priv_data;
71 char host[1024], path[1024], url[1024], announce_addr[50] = "";
73 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
74 AVFormatContext **contexts = NULL;
76 struct sockaddr_storage localaddr;
77 socklen_t addrlen = sizeof(localaddr);
79 AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
81 if (!ff_network_init())
84 /* extract hostname and port */
85 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
86 path, sizeof(path), s->url);
90 /* search for options */
91 option_list = strrchr(path, '?');
94 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
95 port = strtol(buf, NULL, 10);
97 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
98 same_port = strtol(buf, NULL, 10);
100 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
101 ttl = strtol(buf, NULL, 10);
103 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
104 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
108 if (!announce_addr[0]) {
109 struct addrinfo hints = { 0 }, *ai = NULL;
110 hints.ai_family = AF_UNSPEC;
111 if (getaddrinfo(host, NULL, &hints, &ai)) {
112 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
116 if (ai->ai_family == AF_INET) {
117 /* Also known as sap.mcast.net */
118 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
119 #if HAVE_STRUCT_SOCKADDR_IN6
120 } else if (ai->ai_family == AF_INET6) {
121 /* With IPv6, you can use the same destination in many different
122 * multicast subnets, to choose how far you want it routed.
123 * This one is intended to be routed globally. */
124 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
128 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
129 "address family\n", host);
136 contexts = av_mallocz_array(s->nb_streams, sizeof(AVFormatContext*));
138 ret = AVERROR(ENOMEM);
142 if (s->start_time_realtime == 0 || s->start_time_realtime == AV_NOPTS_VALUE)
143 s->start_time_realtime = av_gettime();
144 for (i = 0; i < s->nb_streams; i++) {
148 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
152 ret = ffurl_open_whitelist(&fd, url, AVIO_FLAG_WRITE,
153 &s->interrupt_callback, NULL,
154 s->protocol_whitelist, s->protocol_blacklist, NULL);
159 ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
162 s->streams[i]->priv_data = contexts[i];
163 s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
164 new_url = av_strdup(url);
166 ret = AVERROR(ENOMEM);
169 ff_format_set_url(contexts[i], new_url);
172 if (s->nb_streams > 0 && title)
173 av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
175 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
176 "?ttl=%d&connect=1", ttl);
177 ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_WRITE,
178 &s->interrupt_callback, NULL,
179 s->protocol_whitelist, s->protocol_blacklist, NULL);
185 udp_fd = ffurl_get_file_handle(sap->ann_fd);
186 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
190 if (localaddr.ss_family != AF_INET
191 #if HAVE_STRUCT_SOCKADDR_IN6
192 && localaddr.ss_family != AF_INET6
195 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
199 sap->ann_size = 8192;
200 sap->ann = av_mallocz(sap->ann_size);
205 sap->ann[pos] = (1 << 5);
206 #if HAVE_STRUCT_SOCKADDR_IN6
207 if (localaddr.ss_family == AF_INET6)
208 sap->ann[pos] |= 0x10;
211 sap->ann[pos++] = 0; /* Authentication length */
212 AV_WB16(&sap->ann[pos], av_get_random_seed());
214 if (localaddr.ss_family == AF_INET) {
215 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
216 sizeof(struct in_addr));
217 pos += sizeof(struct in_addr);
218 #if HAVE_STRUCT_SOCKADDR_IN6
220 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
221 sizeof(struct in6_addr));
222 pos += sizeof(struct in6_addr);
226 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
227 pos += strlen(&sap->ann[pos]) + 1;
229 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
230 sap->ann_size - pos)) {
231 ret = AVERROR_INVALIDDATA;
235 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
236 pos += strlen(&sap->ann[pos]);
239 if (sap->ann_size > sap->ann_fd->max_packet_size) {
240 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
253 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
255 AVFormatContext *rtpctx;
256 struct SAPState *sap = s->priv_data;
257 int64_t now = av_gettime_relative();
259 if (!sap->last_time || now - sap->last_time > 5000000) {
260 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
261 /* Don't abort even if we get "Destination unreachable" */
262 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
264 sap->last_time = now;
266 rtpctx = s->streams[pkt->stream_index]->priv_data;
267 return ff_write_chained(rtpctx, 0, pkt, s, 0);
270 AVOutputFormat ff_sap_muxer = {
272 .long_name = NULL_IF_CONFIG_SMALL("SAP output"),
273 .priv_data_size = sizeof(struct SAPState),
274 .audio_codec = AV_CODEC_ID_AAC,
275 .video_codec = AV_CODEC_ID_MPEG4,
276 .write_header = sap_write_header,
277 .write_packet = sap_write_packet,
278 .write_trailer = sap_write_close,
279 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,