2010-10-13 12:06:59 +03:00
|
|
|
/*
|
|
|
|
* Session Announcement Protocol (RFC 2974) demuxer
|
|
|
|
* 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"
|
2014-10-23 16:27:21 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2010-10-13 12:06:59 +03:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "os_support.h"
|
|
|
|
#include "internal.h"
|
2011-02-20 12:04:13 +02:00
|
|
|
#include "avio_internal.h"
|
2011-03-31 17:25:10 +03:00
|
|
|
#include "url.h"
|
2013-04-05 02:45:52 +03:00
|
|
|
#include "rtpdec.h"
|
2011-01-28 04:12:21 +02:00
|
|
|
#if HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
2010-10-13 12:06:59 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct SAPState {
|
|
|
|
URLContext *ann_fd;
|
|
|
|
AVFormatContext *sdp_ctx;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext sdp_pb;
|
2010-10-13 12:06:59 +03:00
|
|
|
uint16_t hash;
|
|
|
|
char *sdp;
|
|
|
|
int eof;
|
|
|
|
};
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int sap_probe(const AVProbeData *p)
|
2010-10-13 12:06:59 +03:00
|
|
|
{
|
|
|
|
if (av_strstart(p->filename, "sap:", NULL))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sap_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
struct SAPState *sap = s->priv_data;
|
|
|
|
if (sap->sdp_ctx)
|
2011-12-11 12:38:28 +03:00
|
|
|
avformat_close_input(&sap->sdp_ctx);
|
2020-04-03 17:03:38 +02:00
|
|
|
ffurl_closep(&sap->ann_fd);
|
2010-10-13 12:06:59 +03:00
|
|
|
av_freep(&sap->sdp);
|
|
|
|
ff_network_close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int sap_read_header(AVFormatContext *s)
|
2010-10-13 12:06:59 +03:00
|
|
|
{
|
|
|
|
struct SAPState *sap = s->priv_data;
|
|
|
|
char host[1024], path[1024], url[1024];
|
2013-04-05 02:45:52 +03:00
|
|
|
uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
|
2021-02-25 04:11:32 +02:00
|
|
|
const AVInputFormat *infmt;
|
2010-10-13 12:06:59 +03:00
|
|
|
int port;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
if (!ff_network_init())
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
|
2017-12-30 00:30:14 +02:00
|
|
|
path, sizeof(path), s->url);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (port < 0)
|
|
|
|
port = 9875;
|
|
|
|
|
|
|
|
if (!host[0]) {
|
|
|
|
/* Listen for announcements on sap.mcast.net if no host was specified */
|
|
|
|
av_strlcpy(host, "224.2.127.254", sizeof(host));
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
|
|
|
|
port);
|
2016-01-30 03:17:51 +02:00
|
|
|
ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_READ,
|
|
|
|
&s->interrupt_callback, NULL,
|
2016-04-21 16:55:09 +02:00
|
|
|
s->protocol_whitelist, s->protocol_blacklist, NULL);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int addr_type, auth_len;
|
|
|
|
int pos;
|
|
|
|
|
2011-03-31 17:31:43 +03:00
|
|
|
ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (ret == AVERROR(EAGAIN))
|
|
|
|
continue;
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
|
|
|
|
if (ret < 8) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Received too short packet\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((recvbuf[0] & 0xe0) != 0x20) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
|
|
|
|
"received\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recvbuf[0] & 0x04) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Received stream deletion "
|
|
|
|
"announcement\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
addr_type = recvbuf[0] & 0x10;
|
|
|
|
auth_len = recvbuf[1];
|
|
|
|
sap->hash = AV_RB16(&recvbuf[2]);
|
|
|
|
pos = 4;
|
|
|
|
if (addr_type)
|
|
|
|
pos += 16; /* IPv6 */
|
|
|
|
else
|
|
|
|
pos += 4; /* IPv4 */
|
|
|
|
pos += auth_len * 4;
|
|
|
|
if (pos + 4 >= ret) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Received too short packet\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#define MIME "application/sdp"
|
|
|
|
if (strcmp(&recvbuf[pos], MIME) == 0) {
|
|
|
|
pos += strlen(MIME) + 1;
|
|
|
|
} else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
|
|
|
|
// Direct SDP without a mime type
|
|
|
|
} else {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
|
|
|
|
&recvbuf[pos]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sap->sdp = av_strdup(&recvbuf[pos]);
|
2019-10-10 04:55:29 +02:00
|
|
|
if (!sap->sdp) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-10-13 12:06:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
|
2011-02-20 12:04:13 +02:00
|
|
|
ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
|
2010-10-13 12:06:59 +03:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
infmt = av_find_input_format("sdp");
|
|
|
|
if (!infmt)
|
|
|
|
goto fail;
|
|
|
|
sap->sdp_ctx = avformat_alloc_context();
|
|
|
|
if (!sap->sdp_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
sap->sdp_ctx->max_delay = s->max_delay;
|
2011-06-04 18:36:30 +03:00
|
|
|
sap->sdp_ctx->pb = &sap->sdp_pb;
|
2011-11-06 23:55:40 +03:00
|
|
|
sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
|
2014-10-23 16:27:21 +03:00
|
|
|
|
2016-03-03 19:14:26 +02:00
|
|
|
if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
|
2014-10-24 20:23:23 +03:00
|
|
|
goto fail;
|
2014-10-23 16:27:21 +03:00
|
|
|
|
2011-06-04 18:36:30 +03:00
|
|
|
ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
|
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (!st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-06-18 12:43:24 +03:00
|
|
|
st->id = i;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
|
2010-10-13 12:06:59 +03:00
|
|
|
st->time_base = sap->sdp_ctx->streams[i]->time_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sap_read_close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
struct SAPState *sap = s->priv_data;
|
2011-03-31 18:51:24 +03:00
|
|
|
int fd = ffurl_get_file_handle(sap->ann_fd);
|
2010-10-13 12:06:59 +03:00
|
|
|
int n, ret;
|
2011-01-28 04:12:21 +02:00
|
|
|
struct pollfd p = {fd, POLLIN, 0};
|
2013-04-05 02:45:52 +03:00
|
|
|
uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
|
2010-10-13 12:06:59 +03:00
|
|
|
|
|
|
|
if (sap->eof)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
while (1) {
|
2011-01-28 04:12:21 +02:00
|
|
|
n = poll(&p, 1, 0);
|
|
|
|
if (n <= 0 || !(p.revents & POLLIN))
|
2010-10-13 12:06:59 +03:00
|
|
|
break;
|
2011-03-31 17:31:43 +03:00
|
|
|
ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
|
2010-10-13 12:06:59 +03:00
|
|
|
if (ret >= 8) {
|
|
|
|
uint16_t hash = AV_RB16(&recvbuf[2]);
|
|
|
|
/* Should ideally check the source IP address, too */
|
|
|
|
if (recvbuf[0] & 0x04 && hash == sap->hash) {
|
|
|
|
/* Stream deletion */
|
|
|
|
sap->eof = 1;
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = av_read_frame(sap->sdp_ctx, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (s->ctx_flags & AVFMTCTX_NOHEADER) {
|
|
|
|
while (sap->sdp_ctx->nb_streams > s->nb_streams) {
|
|
|
|
int i = s->nb_streams;
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2010-10-13 12:06:59 +03:00
|
|
|
if (!st) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2011-06-18 12:43:24 +03:00
|
|
|
st->id = i;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
|
2010-10-13 12:06:59 +03:00
|
|
|
st->time_base = sap->sdp_ctx->streams[i]->time_base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_sap_demuxer = {
|
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 input"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(struct SAPState),
|
|
|
|
.read_probe = sap_probe,
|
|
|
|
.read_header = sap_read_header,
|
|
|
|
.read_packet = sap_fetch_packet,
|
|
|
|
.read_close = sap_read_close,
|
2012-04-06 17:50:48 +03:00
|
|
|
.flags = AVFMT_NOFILE,
|
2010-10-13 12:06:59 +03:00
|
|
|
};
|