2 * MMS protocol over HTTP
3 * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Windows Media HTTP Streaming Protocol.
25 * http://msdn.microsoft.com/en-us/library/cc251059(PROT.10).aspx
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/avstring.h"
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.
41 #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
43 // the guid value can be changed to any valid value.
44 #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
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.
50 CHUNK_TYPE_DATA = 0x4424,
51 CHUNK_TYPE_ASF_HEADER = 0x4824,
52 CHUNK_TYPE_END = 0x4524,
53 CHUNK_TYPE_STREAM_CHANGE = 0x4324,
58 uint8_t location[1024];
59 int request_seq; ///< request packet sequence
60 int chunk_seq; ///< data packet sequence
63 static int mmsh_close(URLContext *h)
65 MMSHContext *mmsh = (MMSHContext *)h->priv_data;
66 MMSContext *mms = &mmsh->mms;
68 ffurl_close(mms->mms_hd);
69 av_free(mms->streams);
70 av_free(mms->asf_header);
71 av_freep(&h->priv_data);
75 static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
77 MMSContext *mms = &mmsh->mms;
78 uint8_t chunk_header[CHUNK_HEADER_LENGTH];
79 uint8_t ext_header[EXT_HEADER_LENGTH];
81 int chunk_len, res, ext_header_len;
83 res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
84 if (res != CHUNK_HEADER_LENGTH) {
85 av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
88 chunk_type = AV_RL16(chunk_header);
89 chunk_len = AV_RL16(chunk_header + 2);
93 case CHUNK_TYPE_STREAM_CHANGE:
96 case CHUNK_TYPE_ASF_HEADER:
101 av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
102 return AVERROR_INVALIDDATA;
105 res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
106 if (res != ext_header_len) {
107 av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
110 *len = chunk_len - ext_header_len;
111 if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
112 mmsh->chunk_seq = AV_RL32(ext_header);
116 static int read_data_packet(MMSHContext *mmsh, const int len)
118 MMSContext *mms = &mmsh->mms;
120 if (len > sizeof(mms->in_buffer)) {
121 av_log(NULL, AV_LOG_ERROR,
122 "Data packet length %d exceeds the in_buffer size %zu\n",
123 len, sizeof(mms->in_buffer));
126 res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
127 av_dlog(NULL, "Data packet len = %d\n", len);
129 av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
132 if (len > mms->asf_packet_len) {
133 av_log(NULL, AV_LOG_ERROR,
134 "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
135 return AVERROR_INVALIDDATA;
137 memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
139 mms->read_in_ptr = mms->in_buffer;
140 mms->remaining_in_len = mms->asf_packet_len;
144 static int get_http_header_data(MMSHContext *mmsh)
146 MMSContext *mms = &mmsh->mms;
148 ChunkType chunk_type;
152 res = chunk_type = get_chunk_header(mmsh, &len);
155 } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
156 // get asf header and stored it
157 if (!mms->header_parsed) {
158 if (mms->asf_header) {
159 if (len != mms->asf_header_size) {
160 mms->asf_header_size = len;
161 av_dlog(NULL, "Header len changed from %d to %d\n",
162 mms->asf_header_size, len);
163 av_freep(&mms->asf_header);
166 mms->asf_header = av_mallocz(len);
167 if (!mms->asf_header) {
168 return AVERROR(ENOMEM);
170 mms->asf_header_size = len;
172 if (len > mms->asf_header_size) {
173 av_log(NULL, AV_LOG_ERROR,
174 "Asf header packet len = %d exceed the asf header buf size %d\n",
175 len, mms->asf_header_size);
178 res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
180 av_log(NULL, AV_LOG_ERROR,
181 "Recv asf header data len %d != expected len %d\n", res, len);
184 mms->asf_header_size = len;
185 if (!mms->header_parsed) {
186 res = ff_mms_asf_header_parser(mms);
187 mms->header_parsed = 1;
190 } else if (chunk_type == CHUNK_TYPE_DATA) {
191 // read data packet and do padding
192 return read_data_packet(mmsh, len);
195 if (len > sizeof(mms->in_buffer)) {
196 av_log(NULL, AV_LOG_ERROR,
197 "Other packet len = %d exceed the in_buffer size %zu\n",
198 len, sizeof(mms->in_buffer));
201 res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
203 av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
206 av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
214 static int mmsh_open_internal(URLContext *h, const char *uri, int flags, int timestamp, int64_t pos)
217 char httpname[256], path[256], host[128];
218 char *stream_selection = NULL;
223 mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
225 return AVERROR(ENOMEM);
226 mmsh->request_seq = h->is_streamed = 1;
228 av_strlcpy(mmsh->location, uri, sizeof(mmsh->location));
230 av_url_split(NULL, 0, NULL, 0,
231 host, sizeof(host), &port, path, sizeof(path), mmsh->location);
233 port = 80; // default mmsh protocol port
234 ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
236 if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ) < 0) {
240 snprintf(headers, sizeof(headers),
244 "Pragma: no-cache,rate=1.000000,stream-time=0,"
245 "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
247 "Connection: Close\r\n",
248 host, port, mmsh->request_seq++);
249 ff_http_set_headers(mms->mms_hd, headers);
251 err = ffurl_connect(mms->mms_hd);
255 err = get_http_header_data(mmsh);
257 av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
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_FLAG_READ) < 0) {
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++) {
272 err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
275 av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
278 err = snprintf(headers, sizeof(headers),
282 "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
283 "Pragma: xPlayStrm=1\r\n"
285 "Pragma: stream-switch-count=%d\r\n"
286 "Pragma: stream-switch-entry=%s\r\n"
287 "Pragma: no-cache,rate=1.000000,stream-time=%u"
288 "Connection: Close\r\n",
289 host, port, mmsh->request_seq++, mms->stream_num, stream_selection, timestamp);
290 av_freep(&stream_selection);
292 av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
295 av_dlog(NULL, "out_buffer is %s", headers);
296 ff_http_set_headers(mms->mms_hd, headers);
298 err = ffurl_connect(mms->mms_hd);
303 err = get_http_header_data(mmsh);
305 av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
309 av_dlog(NULL, "Connection successfully open\n");
312 av_freep(&stream_selection);
314 av_dlog(NULL, "Connection failed with error %d\n", err);
318 static int mmsh_open(URLContext *h, const char *uri, int flags)
320 return mmsh_open_internal(h, uri, flags, 0, 0);
323 static int handle_chunk_type(MMSHContext *mmsh)
325 MMSContext *mms = &mmsh->mms;
327 ChunkType chunk_type;
328 chunk_type = get_chunk_header(mmsh, &len);
330 switch (chunk_type) {
333 av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
335 case CHUNK_TYPE_STREAM_CHANGE:
336 mms->header_parsed = 0;
337 if (res = get_http_header_data(mmsh)) {
338 av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
342 case CHUNK_TYPE_DATA:
343 return read_data_packet(mmsh, len);
345 av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
346 return AVERROR_INVALIDDATA;
351 static int mmsh_read(URLContext *h, uint8_t *buf, int size)
354 MMSHContext *mmsh = h->priv_data;
355 MMSContext *mms = &mmsh->mms;
357 if (mms->asf_header_read_size < mms->asf_header_size) {
358 // copy asf header into buffer
359 res = ff_mms_read_header(mms, buf, size);
361 if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
363 res = ff_mms_read_data(mms, buf, size);
369 static int64_t mmsh_read_seek(URLContext *h, int stream_index,
370 int64_t timestamp, int flags)
372 MMSHContext *mmsh = h->priv_data;
373 MMSContext *mms = &mmsh->mms;
376 ret= mmsh_open_internal(h, mmsh->location, 0, FFMAX(timestamp, 0), 0);
380 ffurl_close(mms->mms_hd);
381 av_freep(&mms->streams);
382 av_freep(&mms->asf_header);
386 mms->asf_header_read_size= mms->asf_header_size;
392 static int64_t mmsh_seek(URLContext *h, int64_t pos, int whence)
394 MMSHContext *mmsh = h->priv_data;
395 MMSContext *mms = &mmsh->mms;
397 if(pos == 0 && whence == SEEK_CUR)
398 return mms->asf_header_read_size + mms->remaining_in_len + mmsh->chunk_seq * mms->asf_packet_len;
399 return AVERROR(ENOSYS);
402 URLProtocol ff_mmsh_protocol = {
404 .url_open = mmsh_open,
405 .url_read = mmsh_read,
407 .url_seek = mmsh_seek,
408 .url_close = mmsh_close,
409 .url_read_seek = mmsh_read_seek,