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 {
119 int res;
122 "Data packet length %d exceeds the in_buffer size %"SIZE_SPECIFIER "\n",
125 }
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) {
154 return res;
156 // get asf header and stored it
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 }
188 return res;
189 }
191 // read data packet and do padding
193 } else {
194 if (len) {
197 "Other packet len = %d exceed the in_buffer size %"SIZE_SPECIFIER "\n",
200 }
202 if (res != len) {
205 } else {
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
254 }
255 }
256
258 if (err) {
260 }
262 if (err) {
265 }
266
267 // close the socket and then reopen it for sending the second play request.
269 memset(headers, 0, sizeof(headers));
273 }
275 if (!stream_selection)
280 if (err < 0)
283 }
284 // send play request
285 err =
snprintf(headers,
sizeof(headers),
286 "Accept: */*\r\n"
288 "Host: %s:%d\r\n"
289 "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
290 "Pragma: xPlayStrm=1\r\n"
291 CLIENTGUID
292 "Pragma: stream-switch-count=%d\r\n"
293 "Pragma: stream-switch-entry=%s\r\n"
294 "Pragma: no-cache,rate=1.000000,stream-time=%u"
295 "Connection: Close\r\n",
298 if (err < 0) {
301 }
304
306 if (err) {
308 }
309
311 if (err) {
314 }
315
317 return 0;
322 return err;
323 }
324
326 {
328 }
329
331 {
336
337 switch (chunk_type) {
346 return res;
347 }
348 break;
351 default:
354 }
355 return 0;
356 }
357
359 {
360 int res = 0;
363 do {
365 // copy asf header into buffer
367 } else {
369 return res;
371 }
372 } while (!res);
373 return res;
374 }
375
377 int64_t timestamp,
int flags)
378 {
381 int ret;
382
383 if (!mmsh)
385
388 if(ret>=0){
394 }else {
397 }
398
399 return ret;
400 }
401
403 {
406
407 if(pos == 0 && whence == SEEK_CUR)
410 }
411
421 .default_whitelist = "http,tcp",
422 };
static int mmsh_open(URLContext *h, const char *uri, int flags)
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
int request_seq
request packet sequence
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
URLContext * mms_hd
TCP connection handle.
#define URL_PROTOCOL_FLAG_NETWORK
uint8_t * read_in_ptr
Pointer for reading from incoming buffer.
static int get_http_header_data(MMSHContext *mmsh)
int is_streamed
true if streamed (no seek possible), default = false
int ffurl_connect(URLContext *uc, AVDictionary **options)
Connect an URLContext that has been allocated by ffurl_alloc.
AVIOInterruptCB interrupt_callback
static int read_data_packet(MMSHContext *mmsh, const int len)
#define EXT_HEADER_LENGTH
#define AVIO_FLAG_READ
read-only
int ff_mms_read_header(MMSContext *mms, uint8_t *buf, const int size)
int stream_num
stream numbers.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
int header_parsed
The header has been received and parsed.
static int handle_chunk_type(MMSHContext *mmsh)
uint8_t * asf_header
Internal handling of the ASF header.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
static int64_t mmsh_seek(URLContext *h, int64_t pos, int whence)
int ff_mms_read_data(MMSContext *mms, uint8_t *buf, const int size)
#define CHUNK_HEADER_LENGTH
uint8_t in_buffer[65536]
Buffer for incoming packets.
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * protocol_whitelist
static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
char * av_strdup(const char *s)
Duplicate a string.
int remaining_in_len
Reading length from incoming buffer.
static int64_t mmsh_read_seek(URLContext *h, int stream_index, int64_t timestamp, int flags)
static int mmsh_open_internal(URLContext *h, const char *uri, int flags, int timestamp, int64_t pos)
int chunk_seq
data packet sequence
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
int asf_header_size
Size of stored ASF header.
int ffurl_close(URLContext *h)
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary...
const URLProtocol ff_mmsh_protocol
static int mmsh_close(URLContext *h)
static int mmsh_read(URLContext *h, uint8_t *buf, int size)
unbuffered private I/O API
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int ff_mms_asf_header_parser(MMSContext *mms)