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>
37
38 #define CHUNK_HEADER_LENGTH 4 // 2bytes chunk type and 2bytes chunk length.
39 #define EXT_HEADER_LENGTH 8 // 4bytes sequence, 2bytes useless and 2bytes chunk length.
40
41 // see Ref 2.2.1.8
42 #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
43 // see Ref 2.2.1.4.33
44 // the guid value can be changed to any valid value.
45 #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
46
47 // see Ref 2.2.3 for packet type define:
48 // chunk type contains 2 fields: Frame and PacketID.
49 // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
56
63
65 {
72 return 0;
73 }
74
76 {
81 int chunk_len,
res, ext_header_len;
82
87 }
88 chunk_type =
AV_RL16(chunk_header);
89 chunk_len =
AV_RL16(chunk_header + 2);
90
91 switch (chunk_type) {
94 ext_header_len = 4;
95 break;
98 ext_header_len = 8;
99 break;
100 default:
103 }
104
106 if (res != ext_header_len) {
109 }
110 *len = chunk_len - ext_header_len;
113 return chunk_type;
114 }
115
117 {
122 "Data packet length %d exceeds the in_buffer size %zu\n",
125 }
127 av_dlog(NULL,
"Data packet len = %d\n", len);
128 if (res != len) {
131 }
134 "Chunk length %d exceed packet length %d\n",len, mms->
asf_packet_len);
136 } else {
138 }
141 return 0;
142 }
143
145 {
149
150 for (;;) {
151 len = 0;
153 if (res < 0) {
156 // get asf header and stored it
161 av_dlog(NULL,
"Header len changed from %d to %d\n",
164 }
165 }
169 }
171 }
174 "Asf header packet len = %d exceed the asf header buf size %d\n",
177 }
179 if (res != len) {
181 "Recv asf header data len %d != expected len %d\n", res, len);
183 }
189 }
191 // read data packet and do padding
193 } else {
194 if (len) {
197 "Other packet len = %d exceed the in_buffer size %zu\n",
200 }
202 if (res != len) {
205 } else {
206 av_dlog(NULL,
"Skip chunk type %d \n", chunk_type);
207 continue;
208 }
209 }
210 }
211 }
212 }
213
215 {
216 int i, port, err;
217 char httpname[256], path[256], host[128];
218 char *stream_selection = NULL;
219 char headers[1024];
222
226
228 host,
sizeof(host), &port, path,
sizeof(path), mmsh->
location);
229 if (port<0)
230 port = 80; // default mmsh protocol port
231 ff_url_join(httpname,
sizeof(httpname),
"http", NULL, host, port,
"%s", path);
232
236 }
237
239 "Accept: */*\r\n"
241 "Host: %s:%d\r\n"
242 "Pragma: no-cache,rate=1.000000,stream-time=0,"
243 "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
245 "Connection: Close\r\n",
248
250 if (err) {
251 goto fail;
252 }
254 if (err) {
256 goto fail;
257 }
258
259 // close the socket and then reopen it for sending the second play request.
261 memset(headers, 0, sizeof(headers));
264 goto fail;
265 }
267 if (!stream_selection)
270 char tmp[20];
272 if (err < 0)
273 goto fail;
275 }
276 // send play request
277 err =
snprintf(headers,
sizeof(headers),
278 "Accept: */*\r\n"
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 "Pragma: no-cache,rate=1.000000,stream-time=%u"
287 "Connection: Close\r\n",
290 if (err < 0) {
292 goto fail;
293 }
294 av_dlog(NULL,
"out_buffer is %s", headers);
296
298 if (err) {
299 goto fail;
300 }
301
303 if (err) {
305 goto fail;
306 }
307
308 av_dlog(NULL,
"Connection successfully open\n");
309 return 0;
310 fail:
313 av_dlog(NULL,
"Connection failed with error %d\n", err);
314 return err;
315 }
316
318 {
320 }
321
323 {
328
329 switch (chunk_type) {
339 }
340 break;
343 default:
346 }
347 return 0;
348 }
349
351 {
355 do {
357 // copy asf header into buffer
359 } else {
363 }
364 } while (!res);
366 }
367
369 int64_t timestamp,
int flags)
370 {
374
375 if (!mmsh)
377
380 if(ret>=0){
386 }else {
389 }
390
392 }
393
395 {
398
399 if(pos == 0 && whence == SEEK_CUR)
402 }
403
413 };