2010-10-08 15:05:04 +03:00
|
|
|
/*
|
|
|
|
* Session Announcement Protocol (RFC 2974) muxer
|
|
|
|
* Copyright (c) 2010 Martin Storsjo
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2011-02-16 10:52:38 +02:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-10-08 15:05:04 +03:00
|
|
|
#include "libavutil/random_seed.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2012-12-17 12:04:15 +03:00
|
|
|
#include "libavutil/dict.h"
|
2010-10-08 15:05:04 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-07-27 17:28:36 +03:00
|
|
|
#include "libavutil/time.h"
|
2010-10-08 15:05:04 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "network.h"
|
2010-10-09 12:16:40 +03:00
|
|
|
#include "os_support.h"
|
2010-10-08 15:05:04 +03:00
|
|
|
#include "rtpenc_chain.h"
|
2011-03-31 17:25:10 +03:00
|
|
|
#include "url.h"
|
2010-10-08 15:05:04 +03:00
|
|
|
|
|
|
|
struct SAPState {
|
|
|
|
uint8_t *ann;
|
|
|
|
int ann_size;
|
|
|
|
URLContext *ann_fd;
|
|
|
|
int64_t last_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sap_write_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
struct SAPState *sap = s->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVFormatContext *rtpctx = s->streams[i]->priv_data;
|
|
|
|
if (!rtpctx)
|
|
|
|
continue;
|
|
|
|
av_write_trailer(rtpctx);
|
2015-01-09 14:39:48 +02:00
|
|
|
avio_closep(&rtpctx->pb);
|
2011-02-04 12:04:18 +02:00
|
|
|
avformat_free_context(rtpctx);
|
2010-10-08 15:05:04 +03:00
|
|
|
s->streams[i]->priv_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sap->last_time && sap->ann && sap->ann_fd) {
|
|
|
|
sap->ann[0] |= 4; /* Session deletion*/
|
2011-03-31 17:48:01 +03:00
|
|
|
ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
|
2010-10-08 15:05:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&sap->ann);
|
2020-04-03 17:03:38 +02:00
|
|
|
ffurl_closep(&sap->ann_fd);
|
2010-10-08 15:05:04 +03:00
|
|
|
ff_network_close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sap_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
struct SAPState *sap = s->priv_data;
|
|
|
|
char host[1024], path[1024], url[1024], announce_addr[50] = "";
|
|
|
|
char *option_list;
|
2010-10-09 17:39:31 +03:00
|
|
|
int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
|
2010-10-08 15:05:04 +03:00
|
|
|
AVFormatContext **contexts = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
struct sockaddr_storage localaddr;
|
|
|
|
socklen_t addrlen = sizeof(localaddr);
|
|
|
|
int udp_fd;
|
2012-12-17 12:04:15 +03:00
|
|
|
AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
|
2010-10-08 15:05:04 +03:00
|
|
|
|
|
|
|
if (!ff_network_init())
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
/* extract hostname and port */
|
2010-10-09 17:39:31 +03:00
|
|
|
av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
|
2017-12-30 00:30:14 +02:00
|
|
|
path, sizeof(path), s->url);
|
2010-10-09 17:39:31 +03:00
|
|
|
if (base_port < 0)
|
|
|
|
base_port = 5004;
|
2010-10-08 15:05:04 +03:00
|
|
|
|
|
|
|
/* search for options */
|
|
|
|
option_list = strrchr(path, '?');
|
|
|
|
if (option_list) {
|
|
|
|
char buf[50];
|
2011-02-16 10:52:38 +02:00
|
|
|
if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
|
2010-10-09 17:39:31 +03:00
|
|
|
port = strtol(buf, NULL, 10);
|
2010-10-08 15:05:04 +03:00
|
|
|
}
|
2011-02-16 10:52:38 +02:00
|
|
|
if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
|
2010-10-08 15:05:04 +03:00
|
|
|
same_port = strtol(buf, NULL, 10);
|
|
|
|
}
|
2011-02-16 10:52:38 +02:00
|
|
|
if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
|
2010-10-08 15:05:04 +03:00
|
|
|
ttl = strtol(buf, NULL, 10);
|
|
|
|
}
|
2011-02-16 10:52:38 +02:00
|
|
|
if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
|
2010-10-08 15:05:04 +03:00
|
|
|
av_strlcpy(announce_addr, buf, sizeof(announce_addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!announce_addr[0]) {
|
2011-06-06 15:13:02 +03:00
|
|
|
struct addrinfo hints = { 0 }, *ai = NULL;
|
2010-10-08 21:07:40 +03:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2010-10-08 15:05:04 +03:00
|
|
|
if (getaddrinfo(host, NULL, &hints, &ai)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (ai->ai_family == AF_INET) {
|
|
|
|
/* Also known as sap.mcast.net */
|
|
|
|
av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
|
2010-10-08 20:48:13 +03:00
|
|
|
#if HAVE_STRUCT_SOCKADDR_IN6
|
2010-10-08 15:05:04 +03:00
|
|
|
} else if (ai->ai_family == AF_INET6) {
|
|
|
|
/* With IPv6, you can use the same destination in many different
|
|
|
|
* multicast subnets, to choose how far you want it routed.
|
|
|
|
* This one is intended to be routed globally. */
|
|
|
|
av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
|
2010-10-08 20:48:13 +03:00
|
|
|
#endif
|
2010-10-08 15:05:04 +03:00
|
|
|
} else {
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
|
|
|
|
"address family\n", host);
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
|
|
|
|
2014-07-21 04:19:12 +03:00
|
|
|
contexts = av_mallocz_array(s->nb_streams, sizeof(AVFormatContext*));
|
2010-10-08 15:05:04 +03:00
|
|
|
if (!contexts) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-12 02:58:34 +03:00
|
|
|
if (s->start_time_realtime == 0 || s->start_time_realtime == AV_NOPTS_VALUE)
|
|
|
|
s->start_time_realtime = av_gettime();
|
2010-10-08 15:05:04 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
URLContext *fd;
|
2017-12-30 00:30:14 +02:00
|
|
|
char *new_url;
|
2010-10-08 15:05:04 +03:00
|
|
|
|
|
|
|
ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
|
|
|
|
"?ttl=%d", ttl);
|
|
|
|
if (!same_port)
|
|
|
|
base_port += 2;
|
2016-01-30 03:17:51 +02:00
|
|
|
ret = ffurl_open_whitelist(&fd, url, AVIO_FLAG_WRITE,
|
|
|
|
&s->interrupt_callback, NULL,
|
2016-04-21 16:55:09 +02:00
|
|
|
s->protocol_whitelist, s->protocol_blacklist, NULL);
|
2010-10-08 15:05:04 +03:00
|
|
|
if (ret) {
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-11-11 22:44:28 +03:00
|
|
|
ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
|
2012-05-25 22:26:00 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
s->streams[i]->priv_data = contexts[i];
|
2014-05-31 01:09:58 +03:00
|
|
|
s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
|
2017-12-30 00:30:14 +02:00
|
|
|
new_url = av_strdup(url);
|
|
|
|
if (!new_url) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ff_format_set_url(contexts[i], new_url);
|
2010-10-08 15:05:04 +03:00
|
|
|
}
|
|
|
|
|
2012-12-17 12:04:15 +03:00
|
|
|
if (s->nb_streams > 0 && title)
|
2012-12-17 12:04:15 +03:00
|
|
|
av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
|
|
|
|
|
2010-10-08 15:05:04 +03:00
|
|
|
ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
|
|
|
|
"?ttl=%d&connect=1", ttl);
|
2016-01-30 03:17:51 +02:00
|
|
|
ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_WRITE,
|
|
|
|
&s->interrupt_callback, NULL,
|
2016-04-21 16:55:09 +02:00
|
|
|
s->protocol_whitelist, s->protocol_blacklist, NULL);
|
2010-10-08 15:05:04 +03:00
|
|
|
if (ret) {
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-03-31 18:51:24 +03:00
|
|
|
udp_fd = ffurl_get_file_handle(sap->ann_fd);
|
2010-10-08 15:05:04 +03:00
|
|
|
if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-10-08 20:48:13 +03:00
|
|
|
if (localaddr.ss_family != AF_INET
|
|
|
|
#if HAVE_STRUCT_SOCKADDR_IN6
|
|
|
|
&& localaddr.ss_family != AF_INET6
|
|
|
|
#endif
|
|
|
|
) {
|
2010-10-08 15:05:04 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
sap->ann_size = 8192;
|
|
|
|
sap->ann = av_mallocz(sap->ann_size);
|
|
|
|
if (!sap->ann) {
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-10-08 20:48:13 +03:00
|
|
|
sap->ann[pos] = (1 << 5);
|
|
|
|
#if HAVE_STRUCT_SOCKADDR_IN6
|
|
|
|
if (localaddr.ss_family == AF_INET6)
|
|
|
|
sap->ann[pos] |= 0x10;
|
|
|
|
#endif
|
|
|
|
pos++;
|
2010-10-08 15:05:04 +03:00
|
|
|
sap->ann[pos++] = 0; /* Authentication length */
|
|
|
|
AV_WB16(&sap->ann[pos], av_get_random_seed());
|
|
|
|
pos += 2;
|
|
|
|
if (localaddr.ss_family == AF_INET) {
|
|
|
|
memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
|
|
|
|
sizeof(struct in_addr));
|
|
|
|
pos += sizeof(struct in_addr);
|
2010-10-08 20:48:13 +03:00
|
|
|
#if HAVE_STRUCT_SOCKADDR_IN6
|
2010-10-08 15:05:04 +03:00
|
|
|
} else {
|
|
|
|
memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
pos += sizeof(struct in6_addr);
|
2010-10-08 20:48:13 +03:00
|
|
|
#endif
|
2010-10-08 15:05:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
|
|
|
|
pos += strlen(&sap->ann[pos]) + 1;
|
|
|
|
|
2011-04-08 12:36:12 +03:00
|
|
|
if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
|
2012-05-25 22:16:06 +03:00
|
|
|
sap->ann_size - pos)) {
|
2010-10-08 15:05:04 +03:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
av_freep(&contexts);
|
|
|
|
av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
|
|
|
|
pos += strlen(&sap->ann[pos]);
|
|
|
|
sap->ann_size = pos;
|
|
|
|
|
2011-03-31 18:58:04 +03:00
|
|
|
if (sap->ann_size > sap->ann_fd->max_packet_size) {
|
2010-10-08 15:05:04 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
|
|
|
|
"packet\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_free(contexts);
|
|
|
|
sap_write_close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AVFormatContext *rtpctx;
|
|
|
|
struct SAPState *sap = s->priv_data;
|
2014-05-07 00:16:51 +03:00
|
|
|
int64_t now = av_gettime_relative();
|
2010-10-08 15:05:04 +03:00
|
|
|
|
|
|
|
if (!sap->last_time || now - sap->last_time > 5000000) {
|
2011-03-31 17:48:01 +03:00
|
|
|
int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
|
2010-10-08 15:05:04 +03:00
|
|
|
/* Don't abort even if we get "Destination unreachable" */
|
2011-02-19 20:14:11 +02:00
|
|
|
if (ret < 0 && ret != AVERROR(ECONNREFUSED))
|
2010-10-08 15:05:04 +03:00
|
|
|
return ret;
|
|
|
|
sap->last_time = now;
|
|
|
|
}
|
|
|
|
rtpctx = s->streams[pkt->stream_index]->priv_data;
|
2014-07-24 23:39:22 +03:00
|
|
|
return ff_write_chained(rtpctx, 0, pkt, s, 0);
|
2010-10-08 15:05:04 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVOutputFormat ff_sap_muxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "sap",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("SAP output"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(struct SAPState),
|
2012-08-05 12:11:04 +03:00
|
|
|
.audio_codec = AV_CODEC_ID_AAC,
|
|
|
|
.video_codec = AV_CODEC_ID_MPEG4,
|
2011-07-16 23:18:12 +03:00
|
|
|
.write_header = sap_write_header,
|
|
|
|
.write_packet = sap_write_packet,
|
|
|
|
.write_trailer = sap_write_close,
|
2012-04-06 17:50:48 +03:00
|
|
|
.flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
|
2010-10-08 15:05:04 +03:00
|
|
|
};
|