Statistics
| Branch: | Revision:

ffmpeg / libavformat / mmsh.c @ f87b1b37

History | View | Annotate | Download (12 KB)

1
/*
2
 * MMS protocol over HTTP
3
 * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/*
23
 * Reference
24
 * Windows Media HTTP Streaming Protocol.
25
 * http://msdn.microsoft.com/en-us/library/cc251059(PROT.10).aspx
26
 */
27

    
28
#include <string.h>
29
#include "libavutil/intreadwrite.h"
30
#include "libavutil/avstring.h"
31
#include "libavformat/internal.h"
32
#include "mms.h"
33
#include "asf.h"
34
#include "http.h"
35
#include "url.h"
36

    
37
#define CHUNK_HEADER_LENGTH 4   // 2bytes chunk type and 2bytes chunk length.
38
#define EXT_HEADER_LENGTH   8   // 4bytes sequence, 2bytes useless and 2bytes chunk length.
39

    
40
// see Ref 2.2.1.8
41
#define USERAGENT  "User-Agent: NSPlayer/4.1.0.3856\r\n"
42
// see Ref 2.2.1.4.33
43
// the guid value can be changed to any valid value.
44
#define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
45

    
46
// see Ref 2.2.3 for packet type define:
47
// chunk type contains 2 fields: Frame and PacketID.
48
// Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
49
typedef enum {
50
    CHUNK_TYPE_DATA          = 0x4424,
51
    CHUNK_TYPE_ASF_HEADER    = 0x4824,
52
    CHUNK_TYPE_END           = 0x4524,
53
    CHUNK_TYPE_STREAM_CHANGE = 0x4324,
54
} ChunkType;
55

    
56
typedef struct {
57
    MMSContext mms;
58
    int request_seq;  ///< request packet sequence
59
    int chunk_seq;    ///< data packet sequence
60
} MMSHContext;
61

    
62
static int mmsh_close(URLContext *h)
63
{
64
    MMSHContext *mmsh = (MMSHContext *)h->priv_data;
65
    MMSContext *mms   = &mmsh->mms;
66
    if (mms->mms_hd)
67
        ffurl_close(mms->mms_hd);
68
    av_free(mms->streams);
69
    av_free(mms->asf_header);
70
    av_freep(&h->priv_data);
71
    return 0;
72
}
73

    
74
static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
75
{
76
    MMSContext *mms = &mmsh->mms;
77
    uint8_t chunk_header[CHUNK_HEADER_LENGTH];
78
    uint8_t ext_header[EXT_HEADER_LENGTH];
79
    ChunkType chunk_type;
80
    int chunk_len, res, ext_header_len;
81

    
82
    res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
83
    if (res != CHUNK_HEADER_LENGTH) {
84
        av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
85
        return AVERROR(EIO);
86
    }
87
    chunk_type = AV_RL16(chunk_header);
88
    chunk_len  = AV_RL16(chunk_header + 2);
89

    
90
    switch (chunk_type) {
91
    case CHUNK_TYPE_END:
92
    case CHUNK_TYPE_STREAM_CHANGE:
93
        ext_header_len = 4;
94
        break;
95
    case CHUNK_TYPE_ASF_HEADER:
96
    case CHUNK_TYPE_DATA:
97
        ext_header_len = 8;
98
        break;
99
    default:
100
        av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
101
        return AVERROR_INVALIDDATA;
102
    }
103

    
104
    res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
105
    if (res != ext_header_len) {
106
        av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
107
        return AVERROR(EIO);
108
    }
109
    *len = chunk_len - ext_header_len;
110
    if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
111
        mmsh->chunk_seq = AV_RL32(ext_header);
112
    return chunk_type;
113
}
114

    
115
static int read_data_packet(MMSHContext *mmsh, const int len)
116
{
117
    MMSContext *mms   = &mmsh->mms;
118
    int res;
119
    if (len > sizeof(mms->in_buffer)) {
120
        av_log(NULL, AV_LOG_ERROR,
121
               "Data packet length %d exceeds the in_buffer size %zu\n",
122
               len, sizeof(mms->in_buffer));
123
        return AVERROR(EIO);
124
    }
125
    res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
126
    av_dlog(NULL, "Data packet len = %d\n", len);
127
    if (res != len) {
128
        av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
129
        return AVERROR(EIO);
130
    }
131
    if (len > mms->asf_packet_len) {
132
        av_log(NULL, AV_LOG_ERROR,
133
               "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
134
        return AVERROR_INVALIDDATA;
135
    } else {
136
        memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
137
    }
138
    mms->read_in_ptr      = mms->in_buffer;
139
    mms->remaining_in_len = mms->asf_packet_len;
140
    return 0;
141
}
142

    
143
static int get_http_header_data(MMSHContext *mmsh)
144
{
145
    MMSContext *mms = &mmsh->mms;
146
    int res, len;
147
    ChunkType chunk_type;
148

    
149
    for (;;) {
150
        len = 0;
151
        res = chunk_type = get_chunk_header(mmsh, &len);
152
        if (res < 0) {
153
            return res;
154
        } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
155
            // get asf header and stored it
156
            if (!mms->header_parsed) {
157
                if (mms->asf_header) {
158
                    if (len != mms->asf_header_size) {
159
                        mms->asf_header_size = len;
160
                        av_dlog(NULL, "Header len changed from %d to %d\n",
161
                                mms->asf_header_size, len);
162
                        av_freep(&mms->asf_header);
163
                    }
164
                }
165
                mms->asf_header = av_mallocz(len);
166
                if (!mms->asf_header) {
167
                    return AVERROR(ENOMEM);
168
                }
169
                mms->asf_header_size = len;
170
            }
171
            if (len > mms->asf_header_size) {
172
                av_log(NULL, AV_LOG_ERROR,
173
                       "Asf header packet len = %d exceed the asf header buf size %d\n",
174
                       len, mms->asf_header_size);
175
                return AVERROR(EIO);
176
            }
177
            res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
178
            if (res != len) {
179
                av_log(NULL, AV_LOG_ERROR,
180
                       "Recv asf header data len %d != expected len %d\n", res, len);
181
                return AVERROR(EIO);
182
            }
183
            mms->asf_header_size = len;
184
            if (!mms->header_parsed) {
185
                res = ff_mms_asf_header_parser(mms);
186
                mms->header_parsed = 1;
187
                return res;
188
            }
189
        } else if (chunk_type == CHUNK_TYPE_DATA) {
190
            // read data packet and do padding
191
            return read_data_packet(mmsh, len);
192
        } else {
193
            if (len) {
194
                if (len > sizeof(mms->in_buffer)) {
195
                    av_log(NULL, AV_LOG_ERROR,
196
                           "Other packet len = %d exceed the in_buffer size %zu\n",
197
                           len, sizeof(mms->in_buffer));
198
                    return AVERROR(EIO);
199
                }
200
                res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
201
                if (res != len) {
202
                    av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
203
                    return AVERROR(EIO);
204
                } else {
205
                    av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
206
                    continue;
207
                }
208
            }
209
        }
210
    }
211
    return 0;
212
}
213

    
214
static int mmsh_open(URLContext *h, const char *uri, int flags)
215
{
216
    int i, port, err;
217
    char httpname[256], path[256], host[128], location[1024];
218
    char *stream_selection = NULL;
219
    char headers[1024];
220
    MMSHContext *mmsh;
221
    MMSContext *mms;
222

    
223
    mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
224
    if (!h->priv_data)
225
        return AVERROR(ENOMEM);
226
    mmsh->request_seq = h->is_streamed = 1;
227
    mms = &mmsh->mms;
228
    av_strlcpy(location, uri, sizeof(location));
229

    
230
    av_url_split(NULL, 0, NULL, 0,
231
        host, sizeof(host), &port, path, sizeof(path), location);
232
    if (port<0)
233
        port = 80; // default mmsh protocol port
234
    ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, path);
235

    
236
    if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_RDONLY) < 0) {
237
        return AVERROR(EIO);
238
    }
239

    
240
    snprintf(headers, sizeof(headers),
241
             "Accept: */*\r\n"
242
             USERAGENT
243
             "Host: %s:%d\r\n"
244
             "Pragma: no-cache,rate=1.000000,stream-time=0,"
245
             "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
246
             CLIENTGUID
247
             "Connection: Close\r\n\r\n",
248
             host, port, mmsh->request_seq++);
249
    ff_http_set_headers(mms->mms_hd, headers);
250

    
251
    err = ffurl_connect(mms->mms_hd);
252
    if (err) {
253
        goto fail;
254
    }
255
    err = get_http_header_data(mmsh);
256
    if (err) {
257
        av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
258
        goto fail;
259
    }
260

    
261
    // close the socket and then reopen it for sending the second play request.
262
    ffurl_close(mms->mms_hd);
263
    memset(headers, 0, sizeof(headers));
264
    if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_RDONLY) < 0) {
265
        return AVERROR(EIO);
266
    }
267
    stream_selection = av_mallocz(mms->stream_num * 19 + 1);
268
    if (!stream_selection)
269
        return AVERROR(ENOMEM);
270
    for (i = 0; i < mms->stream_num; i++) {
271
        char tmp[20];
272
        err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
273
        if (err < 0)
274
            goto fail;
275
        av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
276
    }
277
    // send play request
278
    err = snprintf(headers, sizeof(headers),
279
                   "Accept: */*\r\n"
280
                   USERAGENT
281
                   "Host: %s:%d\r\n"
282
                   "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
283
                   "Pragma: xPlayStrm=1\r\n"
284
                   CLIENTGUID
285
                   "Pragma: stream-switch-count=%d\r\n"
286
                   "Pragma: stream-switch-entry=%s\r\n"
287
                   "Connection: Close\r\n\r\n",
288
                   host, port, mmsh->request_seq++, mms->stream_num, stream_selection);
289
    av_freep(&stream_selection);
290
    if (err < 0) {
291
        av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
292
        goto fail;
293
    }
294
    av_dlog(NULL, "out_buffer is %s", headers);
295
    ff_http_set_headers(mms->mms_hd, headers);
296

    
297
    err = ffurl_connect(mms->mms_hd);
298
    if (err) {
299
          goto fail;
300
    }
301

    
302
    err = get_http_header_data(mmsh);
303
    if (err) {
304
        av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
305
        goto fail;
306
    }
307

    
308
    av_dlog(NULL, "Connection successfully open\n");
309
    return 0;
310
fail:
311
    av_freep(&stream_selection);
312
    mmsh_close(h);
313
    av_dlog(NULL, "Connection failed with error %d\n", err);
314
    return err;
315
}
316

    
317
static int handle_chunk_type(MMSHContext *mmsh)
318
{
319
    MMSContext *mms = &mmsh->mms;
320
    int res, len = 0;
321
    ChunkType chunk_type;
322
    chunk_type = get_chunk_header(mmsh, &len);
323

    
324
    switch (chunk_type) {
325
    case CHUNK_TYPE_END:
326
        mmsh->chunk_seq = 0;
327
        av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
328
        return AVERROR(EIO);
329
    case CHUNK_TYPE_STREAM_CHANGE:
330
        mms->header_parsed = 0;
331
        if (res = get_http_header_data(mmsh)) {
332
            av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
333
            return res;
334
        }
335
        break;
336
    case CHUNK_TYPE_DATA:
337
        return read_data_packet(mmsh, len);
338
    default:
339
        av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
340
        return AVERROR_INVALIDDATA;
341
    }
342
    return 0;
343
}
344

    
345
static int mmsh_read(URLContext *h, uint8_t *buf, int size)
346
{
347
    int res = 0;
348
    MMSHContext *mmsh = h->priv_data;
349
    MMSContext *mms   = &mmsh->mms;
350
    do {
351
        if (mms->asf_header_read_size < mms->asf_header_size) {
352
            // copy asf header into buffer
353
            res = ff_mms_read_header(mms, buf, size);
354
        } else {
355
            if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
356
                return res;
357
            res = ff_mms_read_data(mms, buf, size);
358
        }
359
    } while (!res);
360
    return res;
361
}
362

    
363
URLProtocol ff_mmsh_protocol = {
364
    .name      = "mmsh",
365
    .url_open  = mmsh_open,
366
    .url_read  = mmsh_read,
367
    .url_write = NULL,
368
    .url_seek  = NULL,
369
    .url_close = mmsh_close,
370
};