]> git.sesse.net Git - ffmpeg/blob - libavformat/mmsh.c
rename idroq.c to idroqdec.c
[ffmpeg] / libavformat / mmsh.c
1 /*
2  * MMS protocol over HTTP
3  * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
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 /*
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
36 #define CHUNK_HEADER_LENGTH 4   // 2bytes chunk type and 2bytes chunk length.
37 #define EXT_HEADER_LENGTH   8   // 4bytes sequence, 2bytes useless and 2bytes chunk length.
38
39 // see Ref 2.2.1.8
40 #define USERAGENT  "User-Agent: NSPlayer/4.1.0.3856\r\n"
41 // see Ref 2.2.1.4.33
42 // the guid value can be changed to any valid value.
43 #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
44
45 // see Ref 2.2.3 for packet type define:
46 // chunk type contains 2 fields: Frame and PacketID.
47 // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
48 typedef enum {
49     CHUNK_TYPE_DATA          = 0x4424,
50     CHUNK_TYPE_ASF_HEADER    = 0x4824,
51     CHUNK_TYPE_END           = 0x4524,
52     CHUNK_TYPE_STREAM_CHANGE = 0x4324,
53 } ChunkType;
54
55 typedef struct {
56     MMSContext mms;
57     int request_seq;  ///< request packet sequence
58     int chunk_seq;    ///< data packet sequence
59 } MMSHContext;
60
61 static int mmsh_close(URLContext *h)
62 {
63     MMSHContext *mmsh = (MMSHContext *)h->priv_data;
64     MMSContext *mms   = &mmsh->mms;
65     if (mms->mms_hd)
66         url_close(mms->mms_hd);
67     av_free(mms->streams);
68     av_free(mms->asf_header);
69     av_freep(&h->priv_data);
70     return 0;
71 }
72
73 static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
74 {
75     MMSContext *mms = &mmsh->mms;
76     uint8_t chunk_header[CHUNK_HEADER_LENGTH];
77     uint8_t ext_header[EXT_HEADER_LENGTH];
78     ChunkType chunk_type;
79     int chunk_len, res, ext_header_len;
80
81     res = url_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
82     if (res != CHUNK_HEADER_LENGTH) {
83         av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
84         return AVERROR(EIO);
85     }
86     chunk_type = AV_RL16(chunk_header);
87     chunk_len  = AV_RL16(chunk_header + 2);
88
89     switch (chunk_type) {
90     case CHUNK_TYPE_END:
91     case CHUNK_TYPE_STREAM_CHANGE:
92         ext_header_len = 4;
93         break;
94     case CHUNK_TYPE_ASF_HEADER:
95     case CHUNK_TYPE_DATA:
96         ext_header_len = 8;
97         break;
98     default:
99         av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
100         return AVERROR_INVALIDDATA;
101     }
102
103     res = url_read_complete(mms->mms_hd, ext_header, ext_header_len);
104     if (res != ext_header_len) {
105         av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
106         return AVERROR(EIO);
107     }
108     *len = chunk_len - ext_header_len;
109     if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
110         mmsh->chunk_seq = AV_RL32(ext_header);
111     return chunk_type;
112 }
113
114 static int read_data_packet(MMSHContext *mmsh, const int len)
115 {
116     MMSContext *mms   = &mmsh->mms;
117     int res;
118     if (len > sizeof(mms->in_buffer)) {
119         av_log(NULL, AV_LOG_ERROR,
120                "Data packet length %d exceeds the in_buffer size %zu\n",
121                len, sizeof(mms->in_buffer));
122         return AVERROR(EIO);
123     }
124     res = url_read_complete(mms->mms_hd, mms->in_buffer, len);
125     dprintf(NULL, "Data packet len = %d\n", len);
126     if (res != len) {
127         av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
128         return AVERROR(EIO);
129     }
130     if (len > mms->asf_packet_len) {
131         av_log(NULL, AV_LOG_ERROR,
132                "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
133         return AVERROR_INVALIDDATA;
134     } else {
135         memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
136     }
137     mms->read_in_ptr      = mms->in_buffer;
138     mms->remaining_in_len = mms->asf_packet_len;
139     return 0;
140 }
141
142 static int get_http_header_data(MMSHContext *mmsh)
143 {
144     MMSContext *mms = &mmsh->mms;
145     int res, len;
146     ChunkType chunk_type;
147
148     for (;;) {
149         len = 0;
150         chunk_type = get_chunk_header(mmsh, &len);
151         if (chunk_type < 0) {
152             return chunk_type;
153         } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
154             // get asf header and stored it
155             if (!mms->header_parsed) {
156                 if (mms->asf_header) {
157                     if (len != mms->asf_header_size) {
158                         mms->asf_header_size = len;
159                         dprintf(NULL, "Header len changed from %d to %d\n",
160                                 mms->asf_header_size, len);
161                         av_freep(&mms->asf_header);
162                     }
163                 }
164                 mms->asf_header = av_mallocz(len);
165                 if (!mms->asf_header) {
166                     return AVERROR(ENOMEM);
167                 }
168                 mms->asf_header_size = len;
169             }
170             if (len > mms->asf_header_size) {
171                 av_log(NULL, AV_LOG_ERROR,
172                        "Asf header packet len = %d exceed the asf header buf size %d\n",
173                        len, mms->asf_header_size);
174                 return AVERROR(EIO);
175             }
176             res = url_read_complete(mms->mms_hd, mms->asf_header, len);
177             if (res != len) {
178                 av_log(NULL, AV_LOG_ERROR,
179                        "Recv asf header data len %d != expected len %d\n", res, len);
180                 return AVERROR(EIO);
181             }
182             mms->asf_header_size = len;
183             if (!mms->header_parsed) {
184                 res = ff_mms_asf_header_parser(mms);
185                 mms->header_parsed = 1;
186                 return res;
187             }
188         } else if (chunk_type == CHUNK_TYPE_DATA) {
189             // read data packet and do padding
190             return read_data_packet(mmsh, len);
191         } else {
192             if (len) {
193                 if (len > sizeof(mms->in_buffer)) {
194                     av_log(NULL, AV_LOG_ERROR,
195                            "Other packet len = %d exceed the in_buffer size %zu\n",
196                            len, sizeof(mms->in_buffer));
197                     return AVERROR(EIO);
198                 }
199                 res = url_read_complete(mms->mms_hd, mms->in_buffer, len);
200                 if (res != len) {
201                     av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
202                     return AVERROR(EIO);
203                 } else {
204                     dprintf(NULL, "Skip chunk type %d \n", chunk_type);
205                     continue;
206                 }
207             }
208         }
209     }
210     return 0;
211 }
212
213 static int mmsh_open(URLContext *h, const char *uri, int flags)
214 {
215     int i, port, err;
216     char httpname[256], path[256], host[128], location[1024];
217     char *stream_selection = NULL;
218     char headers[1024];
219     MMSHContext *mmsh;
220     MMSContext *mms;
221
222     mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
223     if (!h->priv_data)
224         return AVERROR(ENOMEM);
225     mmsh->request_seq = h->is_streamed = 1;
226     mms = &mmsh->mms;
227     av_strlcpy(location, uri, sizeof(location));
228
229     av_url_split(NULL, 0, NULL, 0,
230         host, sizeof(host), &port, path, sizeof(path), location);
231     if (port<0)
232         port = 80; // default mmsh protocol port
233     ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, path);
234
235     if (url_alloc(&mms->mms_hd, httpname, URL_RDONLY) < 0) {
236         return AVERROR(EIO);
237     }
238
239     snprintf(headers, sizeof(headers),
240              "Accept: */*\r\n"
241              USERAGENT
242              "Host: %s:%d\r\n"
243              "Pragma: no-cache,rate=1.000000,stream-time=0,"
244              "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
245              CLIENTGUID
246              "Connection: Close\r\n\r\n",
247              host, port, mmsh->request_seq++);
248     ff_http_set_headers(mms->mms_hd, headers);
249
250     err = url_connect(mms->mms_hd);
251     if (err) {
252         goto fail;
253     }
254     err = get_http_header_data(mmsh);
255     if (err) {
256         av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
257         goto fail;
258     }
259
260     // close the socket and then reopen it for sending the second play request.
261     url_close(mms->mms_hd);
262     memset(headers, 0, sizeof(headers));
263     if (url_alloc(&mms->mms_hd, httpname, URL_RDONLY) < 0) {
264         return AVERROR(EIO);
265     }
266     stream_selection = av_mallocz(mms->stream_num * 19 + 1);
267     if (!stream_selection)
268         return AVERROR(ENOMEM);
269     for (i = 0; i < mms->stream_num; i++) {
270         char tmp[20];
271         err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
272         if (err < 0)
273             goto fail;
274         av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
275     }
276     // send play request
277     err = snprintf(headers, sizeof(headers),
278                    "Accept: */*\r\n"
279                    USERAGENT
280                    "Host: %s:%d\r\n"
281                    "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
282                    "Pragma: xPlayStrm=1\r\n"
283                    CLIENTGUID
284                    "Pragma: stream-switch-count=%d\r\n"
285                    "Pragma: stream-switch-entry=%s\r\n"
286                    "Connection: Close\r\n\r\n",
287                    host, port, mmsh->request_seq++, mms->stream_num, stream_selection);
288     av_freep(&stream_selection);
289     if (err < 0) {
290         av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
291         goto fail;
292     }
293     dprintf(NULL, "out_buffer is %s", headers);
294     ff_http_set_headers(mms->mms_hd, headers);
295
296     err = url_connect(mms->mms_hd);
297     if (err) {
298           goto fail;
299     }
300
301     err = get_http_header_data(mmsh);
302     if (err) {
303         av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
304         goto fail;
305     }
306
307     dprintf(NULL, "Connection successfully open\n");
308     return 0;
309 fail:
310     av_freep(&stream_selection);
311     mmsh_close(h);
312     dprintf(NULL, "Connection failed with error %d\n", err);
313     return err;
314 }
315
316 static int handle_chunk_type(MMSHContext *mmsh)
317 {
318     MMSContext *mms = &mmsh->mms;
319     int res, len = 0;
320     ChunkType chunk_type;
321     chunk_type = get_chunk_header(mmsh, &len);
322
323     switch (chunk_type) {
324     case CHUNK_TYPE_END:
325         mmsh->chunk_seq = 0;
326         av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
327         return AVERROR(EIO);
328     case CHUNK_TYPE_STREAM_CHANGE:
329         mms->header_parsed = 0;
330         if (res = get_http_header_data(mmsh)) {
331             av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
332             return res;
333         }
334         break;
335     case CHUNK_TYPE_DATA:
336         return read_data_packet(mmsh, len);
337     default:
338         av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
339         return AVERROR_INVALIDDATA;
340     }
341     return 0;
342 }
343
344 static int mmsh_read(URLContext *h, uint8_t *buf, int size)
345 {
346     int res = 0;
347     MMSHContext *mmsh = h->priv_data;
348     MMSContext *mms   = &mmsh->mms;
349     do {
350         if (mms->asf_header_read_size < mms->asf_header_size) {
351             // copy asf header into buffer
352             res = ff_mms_read_header(mms, buf, size);
353         } else {
354             if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
355                 return res;
356             res = ff_mms_read_data(mms, buf, size);
357         }
358     } while (!res);
359     return res;
360 }
361
362 URLProtocol mmsh_protocol = {
363     .name      = "mmsh",
364     .url_open  = mmsh_open,
365     .url_read  = mmsh_read,
366     .url_write = NULL,
367     .url_seek  = NULL,
368     .url_close = mmsh_close,
369 };