[FFmpeg-devel] [PATCH] Add a SAP demuxer

Martin Storsjo martin
Sat Oct 9 16:44:14 CEST 2010


---
Updated version of what was earlier posted in the muxer+demuxer thread.

This version now includes documentation, too. The demuxer is updated to
handle mpegts in RTP properly too, where AVFMTCTX_NOHEADER is set.

The documentation part is written on top of the suggested SAP muxer docs.

 configure                |    1 +
 doc/general.texi         |    2 +-
 doc/protocols.texi       |   34 +++++++-
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    2 +-
 libavformat/sapdec.c     |  236 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 273 insertions(+), 3 deletions(-)
 create mode 100644 libavformat/sapdec.c

diff --git a/configure b/configure
index 400990a..2e1ad21 100755
--- a/configure
+++ b/configure
@@ -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 aa4e480..4f61b2b 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
- at item SAP                       @tab X @tab
+ at 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 05a7feb..64ea593 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. This is not technically a protocol handler
-in libavformat, it is a muxer.
+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.
 
+ at 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:
@@ -335,6 +339,34 @@ 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:
+
+ at example
+ffmpeg -re -i @var{input} -f sap sap://224.0.0.255
+ at end example
+
+ at subsection Demuxer
+
+The syntax for a SAP url given to the demuxer is:
+ at example
+sap://[@var{address}][:@var{port}]
+ at end example
+
+ at 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:
+
+To play back the first stream announced on the normal SAP multicast address:
+
+ at example
+ffplay sap://
+ at end example
+
 @section tcp
 
 Trasmission Control Protocol.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 61855ad..00bd41f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -229,6 +229,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 66de933..9769de9 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/sapdec.c b/libavformat/sapdec.c
new file mode 100644
index 0000000..4dd7ba3
--- /dev/null
+++ b/libavformat/sapdec.c
@@ -0,0 +1,236 @@
+/*
+ * 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 (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,
+};
+
-- 
1.7.3.1




More information about the ffmpeg-devel mailing list