diff options
author | Martin Storsjö <martin@martin.st> | 2010-10-13 09:06:59 +0000 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2010-10-13 09:06:59 +0000 |
commit | 01c8d258da5db97e67ac89af493ffb9bc2f82d2d (patch) | |
tree | 126ee44281397bf2e4d12488e2d4c1271ad34139 | |
parent | 65cdee9c95d8f97dcb471c346b1afe87a16ee726 (diff) | |
download | ffmpeg-01c8d258da5db97e67ac89af493ffb9bc2f82d2d.tar.gz |
Add a SAP demuxer
Originally committed as revision 25463 to svn://svn.ffmpeg.org/ffmpeg/trunk
-rw-r--r-- | Changelog | 2 | ||||
-rwxr-xr-x | configure | 1 | ||||
-rw-r--r-- | doc/general.texi | 2 | ||||
-rw-r--r-- | doc/protocols.texi | 46 | ||||
-rw-r--r-- | libavformat/Makefile | 1 | ||||
-rw-r--r-- | libavformat/allformats.c | 2 | ||||
-rw-r--r-- | libavformat/avformat.h | 2 | ||||
-rw-r--r-- | libavformat/sapdec.c | 238 |
8 files changed, 289 insertions, 5 deletions
@@ -44,7 +44,7 @@ version <next>: - blackframe filter - Demuxer for Leitch/Harris' VR native stream format (LXF) - RTP depacketization of the X-QT QuickTime format -- SAP (Session Announcement Protocol, RFC 2974) muxer +- SAP (Session Announcement Protocol, RFC 2974) muxer and demuxer - cropdetect filter @@ -1363,6 +1363,7 @@ ogg_demuxer_select="golomb" psp_muxer_select="mov_muxer" rtsp_demuxer_select="http_protocol sdp_demuxer" rtsp_muxer_select="rtp_muxer http_protocol sdp_demuxer" +sap_demuxer_select="sdp_demuxer" sap_muxer_select="rtp_muxer rtp_protocol" sdp_demuxer_select="asf_demuxer rm_demuxer rtp_protocol mpegts_demuxer mov_demuxer" spdif_muxer_select="aac_parser" diff --git a/doc/general.texi b/doc/general.texi index aa4e480c44..4f61b2bac3 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -217,7 +217,7 @@ library: @tab Output is performed by publishing stream to RTMP server @item RTP @tab X @tab @item RTSP @tab X @tab X -@item SAP @tab X @tab +@item SAP @tab X @tab X @item SDP @tab @tab X @item Sega FILM/CPK @tab @tab X @tab Used in many Sega Saturn console games. diff --git a/doc/protocols.texi b/doc/protocols.texi index a9ef517b24..b76e45c3d3 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -290,10 +290,12 @@ ffmpeg -re -i @var{input} -f rtsp -muxdelay 0.1 rtsp://server/live.sdp @section sap Session Announcement Protocol (RFC 2974). This is not technically a -protocol handler in libavformat, it is a muxer. +protocol handler in libavformat, it is a muxer and demuxer. It is used for signalling of RTP streams, by announcing the SDP for the streams regularly on a separate port. +@subsection Muxer + The syntax for a SAP url given to the muxer is: @example sap://@var{destination}[:@var{port}][?@var{options}] @@ -325,6 +327,8 @@ If set to 1, send all RTP streams on the same port pair. If zero (the default), all streams are sent on unique ports, with each stream on a port 2 numbers higher than the previous. VLC/Live555 requires this to be set to 1, to be able to receive the stream. +The RTP stack in libavformat for receiving requires all streams to be sent +on unique ports. @end table Example command lines follow. @@ -335,6 +339,46 @@ To broadcast a stream on the local subnet, for watching in VLC: ffmpeg -re -i @var{input} -f sap sap://224.0.0.255?same_port=1 @end example +Similarly, for watching in ffplay: + +@example +ffmpeg -re -i @var{input} -f sap sap://224.0.0.255 +@end example + +And for watching in ffplay, over IPv6: + +@example +ffmpeg -re -i @var{input} -f sap sap://[ff0e::1:2:3:4] +@end example + +@subsection Demuxer + +The syntax for a SAP url given to the demuxer is: +@example +sap://[@var{address}][:@var{port}] +@end example + +@var{address} is the multicast address to listen for announcements on, +if omitted, the default 224.2.127.254 (sap.mcast.net) is used. @var{port} +is the port that is listened on, 9875 if omitted. + +The demuxers listens for announcements on the given address and port. +Once an announcement is received, it tries to receive that particular stream. + +Example command lines follow. + +To play back the first stream announced on the normal SAP multicast address: + +@example +ffplay sap:// +@end example + +To play back the first stream announced on one the default IPv6 SAP multicast address: + +@example +ffplay sap://[ff0e::2:7ffe] +@end example + @section tcp Trasmission Control Protocol. diff --git a/libavformat/Makefile b/libavformat/Makefile index 4e874d57f5..2ba8b09938 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -231,6 +231,7 @@ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \ avc.o OBJS-$(CONFIG_RTSP_DEMUXER) += rtsp.o httpauth.o OBJS-$(CONFIG_RTSP_MUXER) += rtsp.o rtspenc.o httpauth.o +OBJS-$(CONFIG_SAP_DEMUXER) += sapdec.o OBJS-$(CONFIG_SAP_MUXER) += sapenc.o rtpenc_chain.o OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o \ rdt.o \ diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 66de9332f5..9769de9f0f 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -181,7 +181,7 @@ void av_register_all(void) REGISTER_MUXDEMUX (RSO, rso); REGISTER_MUXER (RTP, rtp); REGISTER_MUXDEMUX (RTSP, rtsp); - REGISTER_MUXER (SAP, sap); + REGISTER_MUXDEMUX (SAP, sap); REGISTER_DEMUXER (SDP, sdp); #if CONFIG_SDP_DEMUXER av_register_rtp_dynamic_payload_handlers(); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 42ba01f897..849db4ca8f 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -22,7 +22,7 @@ #define AVFORMAT_AVFORMAT_H #define LIBAVFORMAT_VERSION_MAJOR 52 -#define LIBAVFORMAT_VERSION_MINOR 81 +#define LIBAVFORMAT_VERSION_MINOR 82 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c new file mode 100644 index 0000000000..19d524314a --- /dev/null +++ b/libavformat/sapdec.c @@ -0,0 +1,238 @@ +/* + * 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" +#include "libavutil/avstring.h" +#include "libavutil/intreadwrite.h" +#include "network.h" +#include "os_support.h" +#include "internal.h" +#if HAVE_SYS_SELECT_H +#include <sys/select.h> +#endif + +struct SAPState { + URLContext *ann_fd; + AVFormatContext *sdp_ctx; + ByteIOContext sdp_pb; + uint16_t hash; + char *sdp; + int eof; +}; + +static int sap_probe(AVProbeData *p) +{ + 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) + av_close_input_stream(sap->sdp_ctx); + if (sap->ann_fd) + url_close(sap->ann_fd); + av_freep(&sap->sdp); + ff_network_close(); + return 0; +} + +static int sap_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + struct SAPState *sap = s->priv_data; + char host[1024], path[1024], url[1024]; + uint8_t recvbuf[1500]; + int port; + int ret, i; + AVInputFormat* infmt; + + if (!ff_network_init()) + return AVERROR(EIO); + + av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, + path, sizeof(path), s->filename); + 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); + ret = url_open(&sap->ann_fd, url, URL_RDONLY); + if (ret) + goto fail; + + while (1) { + int addr_type, auth_len; + int pos; + + ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1); + 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]); + break; + } + + av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp); + init_put_byte(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL, + 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; + ap->prealloced_context = 1; + ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp", + infmt, ap); + 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++) { + AVStream *st = av_new_stream(s, i); + if (!st) { + ret = AVERROR(ENOMEM); + goto fail; + } + avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); + 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; + int fd = url_get_file_handle(sap->ann_fd); + int n, ret; + fd_set rfds; + struct timeval tv; + uint8_t recvbuf[1500]; + + if (sap->eof) + return AVERROR_EOF; + + while (1) { + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + tv.tv_sec = tv.tv_usec = 0; + n = select(fd + 1, &rfds, NULL, NULL, &tv); + if (n <= 0 || !FD_ISSET(fd, &rfds)) + break; + ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf)); + 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; + AVStream *st = av_new_stream(s, i); + if (!st) { + av_free_packet(pkt); + return AVERROR(ENOMEM); + } + avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); + st->time_base = sap->sdp_ctx->streams[i]->time_base; + } + } + return ret; +} + +AVInputFormat sap_demuxer = { + "sap", + NULL_IF_CONFIG_SMALL("SAP input format"), + sizeof(struct SAPState), + sap_probe, + sap_read_header, + sap_fetch_packet, + sap_read_close, + .flags = AVFMT_NOFILE, +}; + |