sapdec.c 7.17 KB
Newer Older
Martin Storsjö's avatar
Martin Storsjö committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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"
23
#include "libavutil/avassert.h"
Martin Storsjö's avatar
Martin Storsjö committed
24 25 26 27 28
#include "libavutil/avstring.h"
#include "libavutil/intreadwrite.h"
#include "network.h"
#include "os_support.h"
#include "internal.h"
29
#include "avio_internal.h"
30
#include "url.h"
31
#include "rtpdec.h"
32 33
#if HAVE_POLL_H
#include <poll.h>
Martin Storsjö's avatar
Martin Storsjö committed
34 35 36 37 38
#endif

struct SAPState {
    URLContext *ann_fd;
    AVFormatContext *sdp_ctx;
39
    AVIOContext sdp_pb;
Martin Storsjö's avatar
Martin Storsjö committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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)
56
        avformat_close_input(&sap->sdp_ctx);
Martin Storsjö's avatar
Martin Storsjö committed
57
    if (sap->ann_fd)
58
        ffurl_close(sap->ann_fd);
Martin Storsjö's avatar
Martin Storsjö committed
59 60 61 62 63
    av_freep(&sap->sdp);
    ff_network_close();
    return 0;
}

64
static int sap_read_header(AVFormatContext *s)
Martin Storsjö's avatar
Martin Storsjö committed
65 66 67
{
    struct SAPState *sap = s->priv_data;
    char host[1024], path[1024], url[1024];
68
    uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
Martin Storsjö's avatar
Martin Storsjö committed
69 70 71 72 73 74 75 76
    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,
77
                 path, sizeof(path), s->url);
Martin Storsjö's avatar
Martin Storsjö committed
78 79 80 81 82 83 84 85 86 87
    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);
88 89
    ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_READ,
                               &s->interrupt_callback, NULL,
90
                               s->protocol_whitelist, s->protocol_blacklist, NULL);
Martin Storsjö's avatar
Martin Storsjö committed
91 92 93 94 95 96 97
    if (ret)
        goto fail;

    while (1) {
        int addr_type, auth_len;
        int pos;

98
        ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
Martin Storsjö's avatar
Martin Storsjö committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        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);
149
    ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
Martin Storsjö's avatar
Martin Storsjö committed
150 151 152 153 154 155 156 157 158 159 160
                  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;
161
    sap->sdp_ctx->pb        = &sap->sdp_pb;
162
    sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
163

164
    if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
165
        goto fail;
166

167
    ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
Martin Storsjö's avatar
Martin Storsjö committed
168 169 170 171 172
    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++) {
173
        AVStream *st = avformat_new_stream(s, NULL);
Martin Storsjö's avatar
Martin Storsjö committed
174 175 176 177
        if (!st) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
178
        st->id = i;
179
        avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
Martin Storsjö's avatar
Martin Storsjö committed
180 181 182 183 184 185 186 187 188 189 190 191 192
        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;
193
    int fd = ffurl_get_file_handle(sap->ann_fd);
Martin Storsjö's avatar
Martin Storsjö committed
194
    int n, ret;
195
    struct pollfd p = {fd, POLLIN, 0};
196
    uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
Martin Storsjö's avatar
Martin Storsjö committed
197 198 199 200 201

    if (sap->eof)
        return AVERROR_EOF;

    while (1) {
202 203
        n = poll(&p, 1, 0);
        if (n <= 0 || !(p.revents & POLLIN))
Martin Storsjö's avatar
Martin Storsjö committed
204
            break;
205
        ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
Martin Storsjö's avatar
Martin Storsjö committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        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;
222
            AVStream *st = avformat_new_stream(s, NULL);
Martin Storsjö's avatar
Martin Storsjö committed
223
            if (!st) {
224
                av_packet_unref(pkt);
Martin Storsjö's avatar
Martin Storsjö committed
225 226
                return AVERROR(ENOMEM);
            }
227
            st->id = i;
228
            avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
Martin Storsjö's avatar
Martin Storsjö committed
229 230 231 232 233 234
            st->time_base = sap->sdp_ctx->streams[i]->time_base;
        }
    }
    return ret;
}

235
AVInputFormat ff_sap_demuxer = {
236
    .name           = "sap",
237
    .long_name      = NULL_IF_CONFIG_SMALL("SAP input"),
238 239 240 241 242
    .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,
243
    .flags          = AVFMT_NOFILE,
Martin Storsjö's avatar
Martin Storsjö committed
244
};