Statistics
| Branch: | Revision:

ffmpeg / libavformat / sapdec.c @ e731b8d8

History | View | Annotate | Download (6.66 KB)

1
/*
2
 * Session Announcement Protocol (RFC 2974) demuxer
3
 * Copyright (c) 2010 Martin Storsjo
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "avformat.h"
23
#include "libavutil/avstring.h"
24
#include "libavutil/intreadwrite.h"
25
#include "network.h"
26
#include "os_support.h"
27
#include "internal.h"
28
#include "avio_internal.h"
29
#if HAVE_POLL_H
30
#include <poll.h>
31
#endif
32
#include <sys/time.h>
33

    
34
struct SAPState {
35
    URLContext *ann_fd;
36
    AVFormatContext *sdp_ctx;
37
    AVIOContext sdp_pb;
38
    uint16_t hash;
39
    char *sdp;
40
    int eof;
41
};
42

    
43
static int sap_probe(AVProbeData *p)
44
{
45
    if (av_strstart(p->filename, "sap:", NULL))
46
        return AVPROBE_SCORE_MAX;
47
    return 0;
48
}
49

    
50
static int sap_read_close(AVFormatContext *s)
51
{
52
    struct SAPState *sap = s->priv_data;
53
    if (sap->sdp_ctx)
54
        av_close_input_stream(sap->sdp_ctx);
55
    if (sap->ann_fd)
56
        url_close(sap->ann_fd);
57
    av_freep(&sap->sdp);
58
    ff_network_close();
59
    return 0;
60
}
61

    
62
static int sap_read_header(AVFormatContext *s,
63
                           AVFormatParameters *ap)
64
{
65
    struct SAPState *sap = s->priv_data;
66
    char host[1024], path[1024], url[1024];
67
    uint8_t recvbuf[1500];
68
    int port;
69
    int ret, i;
70
    AVInputFormat* infmt;
71

    
72
    if (!ff_network_init())
73
        return AVERROR(EIO);
74

    
75
    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
76
                 path, sizeof(path), s->filename);
77
    if (port < 0)
78
        port = 9875;
79

    
80
    if (!host[0]) {
81
        /* Listen for announcements on sap.mcast.net if no host was specified */
82
        av_strlcpy(host, "224.2.127.254", sizeof(host));
83
    }
84

    
85
    ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
86
                port);
87
    ret = url_open(&sap->ann_fd, url, URL_RDONLY);
88
    if (ret)
89
        goto fail;
90

    
91
    while (1) {
92
        int addr_type, auth_len;
93
        int pos;
94

    
95
        ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
96
        if (ret == AVERROR(EAGAIN))
97
            continue;
98
        if (ret < 0)
99
            goto fail;
100
        recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
101
        if (ret < 8) {
102
            av_log(s, AV_LOG_WARNING, "Received too short packet\n");
103
            continue;
104
        }
105

    
106
        if ((recvbuf[0] & 0xe0) != 0x20) {
107
            av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
108
                                      "received\n");
109
            continue;
110
        }
111

    
112
        if (recvbuf[0] & 0x04) {
113
            av_log(s, AV_LOG_WARNING, "Received stream deletion "
114
                                      "announcement\n");
115
            continue;
116
        }
117
        addr_type = recvbuf[0] & 0x10;
118
        auth_len  = recvbuf[1];
119
        sap->hash = AV_RB16(&recvbuf[2]);
120
        pos = 4;
121
        if (addr_type)
122
            pos += 16; /* IPv6 */
123
        else
124
            pos += 4; /* IPv4 */
125
        pos += auth_len * 4;
126
        if (pos + 4 >= ret) {
127
            av_log(s, AV_LOG_WARNING, "Received too short packet\n");
128
            continue;
129
        }
130
#define MIME "application/sdp"
131
        if (strcmp(&recvbuf[pos], MIME) == 0) {
132
            pos += strlen(MIME) + 1;
133
        } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
134
            // Direct SDP without a mime type
135
        } else {
136
            av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
137
                                      &recvbuf[pos]);
138
            continue;
139
        }
140

    
141
        sap->sdp = av_strdup(&recvbuf[pos]);
142
        break;
143
    }
144

    
145
    av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
146
    ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
147
                  NULL, NULL);
148

    
149
    infmt = av_find_input_format("sdp");
150
    if (!infmt)
151
        goto fail;
152
    sap->sdp_ctx = avformat_alloc_context();
153
    if (!sap->sdp_ctx) {
154
        ret = AVERROR(ENOMEM);
155
        goto fail;
156
    }
157
    sap->sdp_ctx->max_delay = s->max_delay;
158
    ap->prealloced_context = 1;
159
    ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
160
                               infmt, ap);
161
    if (ret < 0)
162
        goto fail;
163
    if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
164
        s->ctx_flags |= AVFMTCTX_NOHEADER;
165
    for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
166
        AVStream *st = av_new_stream(s, i);
167
        if (!st) {
168
            ret = AVERROR(ENOMEM);
169
            goto fail;
170
        }
171
        avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
172
        st->time_base = sap->sdp_ctx->streams[i]->time_base;
173
    }
174

    
175
    return 0;
176

    
177
fail:
178
    sap_read_close(s);
179
    return ret;
180
}
181

    
182
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
183
{
184
    struct SAPState *sap = s->priv_data;
185
    int fd = url_get_file_handle(sap->ann_fd);
186
    int n, ret;
187
    struct pollfd p = {fd, POLLIN, 0};
188
    uint8_t recvbuf[1500];
189

    
190
    if (sap->eof)
191
        return AVERROR_EOF;
192

    
193
    while (1) {
194
        n = poll(&p, 1, 0);
195
        if (n <= 0 || !(p.revents & POLLIN))
196
            break;
197
        ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
198
        if (ret >= 8) {
199
            uint16_t hash = AV_RB16(&recvbuf[2]);
200
            /* Should ideally check the source IP address, too */
201
            if (recvbuf[0] & 0x04 && hash == sap->hash) {
202
                /* Stream deletion */
203
                sap->eof = 1;
204
                return AVERROR_EOF;
205
            }
206
        }
207
    }
208
    ret = av_read_frame(sap->sdp_ctx, pkt);
209
    if (ret < 0)
210
        return ret;
211
    if (s->ctx_flags & AVFMTCTX_NOHEADER) {
212
        while (sap->sdp_ctx->nb_streams > s->nb_streams) {
213
            int i = s->nb_streams;
214
            AVStream *st = av_new_stream(s, i);
215
            if (!st) {
216
                av_free_packet(pkt);
217
                return AVERROR(ENOMEM);
218
            }
219
            avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
220
            st->time_base = sap->sdp_ctx->streams[i]->time_base;
221
        }
222
    }
223
    return ret;
224
}
225

    
226
AVInputFormat ff_sap_demuxer = {
227
    "sap",
228
    NULL_IF_CONFIG_SMALL("SAP input format"),
229
    sizeof(struct SAPState),
230
    sap_probe,
231
    sap_read_header,
232
    sap_fetch_packet,
233
    sap_read_close,
234
    .flags = AVFMT_NOFILE,
235
};
236