00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <string.h>
00029 #include "libavutil/intreadwrite.h"
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/opt.h"
00032 #include "internal.h"
00033 #include "mms.h"
00034 #include "asf.h"
00035 #include "http.h"
00036 #include "url.h"
00037
00038 #define CHUNK_HEADER_LENGTH 4 // 2bytes chunk type and 2bytes chunk length.
00039 #define EXT_HEADER_LENGTH 8 // 4bytes sequence, 2bytes useless and 2bytes chunk length.
00040
00041
00042 #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
00043
00044
00045 #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
00046
00047
00048
00049
00050 typedef enum {
00051 CHUNK_TYPE_DATA = 0x4424,
00052 CHUNK_TYPE_ASF_HEADER = 0x4824,
00053 CHUNK_TYPE_END = 0x4524,
00054 CHUNK_TYPE_STREAM_CHANGE = 0x4324,
00055 } ChunkType;
00056
00057 typedef struct {
00058 MMSContext mms;
00059 uint8_t location[1024];
00060 int request_seq;
00061 int chunk_seq;
00062 } MMSHContext;
00063
00064 static int mmsh_close(URLContext *h)
00065 {
00066 MMSHContext *mmsh = (MMSHContext *)h->priv_data;
00067 MMSContext *mms = &mmsh->mms;
00068 if (mms->mms_hd)
00069 ffurl_close(mms->mms_hd);
00070 av_free(mms->streams);
00071 av_free(mms->asf_header);
00072 return 0;
00073 }
00074
00075 static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
00076 {
00077 MMSContext *mms = &mmsh->mms;
00078 uint8_t chunk_header[CHUNK_HEADER_LENGTH];
00079 uint8_t ext_header[EXT_HEADER_LENGTH];
00080 ChunkType chunk_type;
00081 int chunk_len, res, ext_header_len;
00082
00083 res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
00084 if (res != CHUNK_HEADER_LENGTH) {
00085 av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
00086 return AVERROR(EIO);
00087 }
00088 chunk_type = AV_RL16(chunk_header);
00089 chunk_len = AV_RL16(chunk_header + 2);
00090
00091 switch (chunk_type) {
00092 case CHUNK_TYPE_END:
00093 case CHUNK_TYPE_STREAM_CHANGE:
00094 ext_header_len = 4;
00095 break;
00096 case CHUNK_TYPE_ASF_HEADER:
00097 case CHUNK_TYPE_DATA:
00098 ext_header_len = 8;
00099 break;
00100 default:
00101 av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
00102 return AVERROR_INVALIDDATA;
00103 }
00104
00105 res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
00106 if (res != ext_header_len) {
00107 av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
00108 return AVERROR(EIO);
00109 }
00110 *len = chunk_len - ext_header_len;
00111 if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
00112 mmsh->chunk_seq = AV_RL32(ext_header);
00113 return chunk_type;
00114 }
00115
00116 static int read_data_packet(MMSHContext *mmsh, const int len)
00117 {
00118 MMSContext *mms = &mmsh->mms;
00119 int res;
00120 if (len > sizeof(mms->in_buffer)) {
00121 av_log(NULL, AV_LOG_ERROR,
00122 "Data packet length %d exceeds the in_buffer size %zu\n",
00123 len, sizeof(mms->in_buffer));
00124 return AVERROR(EIO);
00125 }
00126 res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
00127 av_dlog(NULL, "Data packet len = %d\n", len);
00128 if (res != len) {
00129 av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
00130 return AVERROR(EIO);
00131 }
00132 if (len > mms->asf_packet_len) {
00133 av_log(NULL, AV_LOG_ERROR,
00134 "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
00135 return AVERROR_INVALIDDATA;
00136 } else {
00137 memset(mms->in_buffer + len, 0, mms->asf_packet_len - len);
00138 }
00139 mms->read_in_ptr = mms->in_buffer;
00140 mms->remaining_in_len = mms->asf_packet_len;
00141 return 0;
00142 }
00143
00144 static int get_http_header_data(MMSHContext *mmsh)
00145 {
00146 MMSContext *mms = &mmsh->mms;
00147 int res, len;
00148 ChunkType chunk_type;
00149
00150 for (;;) {
00151 len = 0;
00152 res = chunk_type = get_chunk_header(mmsh, &len);
00153 if (res < 0) {
00154 return res;
00155 } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
00156
00157 if (!mms->header_parsed) {
00158 if (mms->asf_header) {
00159 if (len != mms->asf_header_size) {
00160 mms->asf_header_size = len;
00161 av_dlog(NULL, "Header len changed from %d to %d\n",
00162 mms->asf_header_size, len);
00163 av_freep(&mms->asf_header);
00164 }
00165 }
00166 mms->asf_header = av_mallocz(len);
00167 if (!mms->asf_header) {
00168 return AVERROR(ENOMEM);
00169 }
00170 mms->asf_header_size = len;
00171 }
00172 if (len > mms->asf_header_size) {
00173 av_log(NULL, AV_LOG_ERROR,
00174 "Asf header packet len = %d exceed the asf header buf size %d\n",
00175 len, mms->asf_header_size);
00176 return AVERROR(EIO);
00177 }
00178 res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
00179 if (res != len) {
00180 av_log(NULL, AV_LOG_ERROR,
00181 "Recv asf header data len %d != expected len %d\n", res, len);
00182 return AVERROR(EIO);
00183 }
00184 mms->asf_header_size = len;
00185 if (!mms->header_parsed) {
00186 res = ff_mms_asf_header_parser(mms);
00187 mms->header_parsed = 1;
00188 return res;
00189 }
00190 } else if (chunk_type == CHUNK_TYPE_DATA) {
00191
00192 return read_data_packet(mmsh, len);
00193 } else {
00194 if (len) {
00195 if (len > sizeof(mms->in_buffer)) {
00196 av_log(NULL, AV_LOG_ERROR,
00197 "Other packet len = %d exceed the in_buffer size %zu\n",
00198 len, sizeof(mms->in_buffer));
00199 return AVERROR(EIO);
00200 }
00201 res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
00202 if (res != len) {
00203 av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
00204 return AVERROR(EIO);
00205 } else {
00206 av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
00207 continue;
00208 }
00209 }
00210 }
00211 }
00212 }
00213
00214 static int mmsh_open_internal(URLContext *h, const char *uri, int flags, int timestamp, int64_t pos)
00215 {
00216 int i, port, err;
00217 char httpname[256], path[256], host[128];
00218 char *stream_selection = NULL;
00219 char headers[1024];
00220 MMSHContext *mmsh = h->priv_data;
00221 MMSContext *mms;
00222
00223 mmsh->request_seq = h->is_streamed = 1;
00224 mms = &mmsh->mms;
00225 av_strlcpy(mmsh->location, uri, sizeof(mmsh->location));
00226
00227 av_url_split(NULL, 0, NULL, 0,
00228 host, sizeof(host), &port, path, sizeof(path), mmsh->location);
00229 if (port<0)
00230 port = 80;
00231 ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
00232
00233 if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
00234 &h->interrupt_callback) < 0) {
00235 return AVERROR(EIO);
00236 }
00237
00238 snprintf(headers, sizeof(headers),
00239 "Accept: */*\r\n"
00240 USERAGENT
00241 "Host: %s:%d\r\n"
00242 "Pragma: no-cache,rate=1.000000,stream-time=0,"
00243 "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
00244 CLIENTGUID
00245 "Connection: Close\r\n",
00246 host, port, mmsh->request_seq++);
00247 av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
00248
00249 err = ffurl_connect(mms->mms_hd, NULL);
00250 if (err) {
00251 goto fail;
00252 }
00253 err = get_http_header_data(mmsh);
00254 if (err) {
00255 av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
00256 goto fail;
00257 }
00258
00259
00260 ffurl_close(mms->mms_hd);
00261 memset(headers, 0, sizeof(headers));
00262 if ((err = ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
00263 &h->interrupt_callback)) < 0) {
00264 goto fail;
00265 }
00266 stream_selection = av_mallocz(mms->stream_num * 19 + 1);
00267 if (!stream_selection)
00268 return AVERROR(ENOMEM);
00269 for (i = 0; i < mms->stream_num; i++) {
00270 char tmp[20];
00271 err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
00272 if (err < 0)
00273 goto fail;
00274 av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
00275 }
00276
00277 err = snprintf(headers, sizeof(headers),
00278 "Accept: */*\r\n"
00279 USERAGENT
00280 "Host: %s:%d\r\n"
00281 "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
00282 "Pragma: xPlayStrm=1\r\n"
00283 CLIENTGUID
00284 "Pragma: stream-switch-count=%d\r\n"
00285 "Pragma: stream-switch-entry=%s\r\n"
00286 "Pragma: no-cache,rate=1.000000,stream-time=%u"
00287 "Connection: Close\r\n",
00288 host, port, mmsh->request_seq++, mms->stream_num, stream_selection, timestamp);
00289 av_freep(&stream_selection);
00290 if (err < 0) {
00291 av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
00292 goto fail;
00293 }
00294 av_dlog(NULL, "out_buffer is %s", headers);
00295 av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
00296
00297 err = ffurl_connect(mms->mms_hd, NULL);
00298 if (err) {
00299 goto fail;
00300 }
00301
00302 err = get_http_header_data(mmsh);
00303 if (err) {
00304 av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
00305 goto fail;
00306 }
00307
00308 av_dlog(NULL, "Connection successfully open\n");
00309 return 0;
00310 fail:
00311 av_freep(&stream_selection);
00312 mmsh_close(h);
00313 av_dlog(NULL, "Connection failed with error %d\n", err);
00314 return err;
00315 }
00316
00317 static int mmsh_open(URLContext *h, const char *uri, int flags)
00318 {
00319 return mmsh_open_internal(h, uri, flags, 0, 0);
00320 }
00321
00322 static int handle_chunk_type(MMSHContext *mmsh)
00323 {
00324 MMSContext *mms = &mmsh->mms;
00325 int res, len = 0;
00326 ChunkType chunk_type;
00327 chunk_type = get_chunk_header(mmsh, &len);
00328
00329 switch (chunk_type) {
00330 case CHUNK_TYPE_END:
00331 mmsh->chunk_seq = 0;
00332 av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
00333 return AVERROR(EIO);
00334 case CHUNK_TYPE_STREAM_CHANGE:
00335 mms->header_parsed = 0;
00336 if (res = get_http_header_data(mmsh)) {
00337 av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
00338 return res;
00339 }
00340 break;
00341 case CHUNK_TYPE_DATA:
00342 return read_data_packet(mmsh, len);
00343 default:
00344 av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
00345 return AVERROR_INVALIDDATA;
00346 }
00347 return 0;
00348 }
00349
00350 static int mmsh_read(URLContext *h, uint8_t *buf, int size)
00351 {
00352 int res = 0;
00353 MMSHContext *mmsh = h->priv_data;
00354 MMSContext *mms = &mmsh->mms;
00355 do {
00356 if (mms->asf_header_read_size < mms->asf_header_size) {
00357
00358 res = ff_mms_read_header(mms, buf, size);
00359 } else {
00360 if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
00361 return res;
00362 res = ff_mms_read_data(mms, buf, size);
00363 }
00364 } while (!res);
00365 return res;
00366 }
00367
00368 static int64_t mmsh_read_seek(URLContext *h, int stream_index,
00369 int64_t timestamp, int flags)
00370 {
00371 MMSHContext *mmsh = h->priv_data;
00372 MMSContext *mms = &mmsh->mms;
00373 int ret;
00374
00375 ret= mmsh_open_internal(h, mmsh->location, 0, FFMAX(timestamp, 0), 0);
00376
00377 if(ret>=0){
00378 if (mms->mms_hd)
00379 ffurl_close(mms->mms_hd);
00380 av_freep(&mms->streams);
00381 av_freep(&mms->asf_header);
00382 av_free(mmsh);
00383 mmsh = h->priv_data;
00384 mms = &mmsh->mms;
00385 mms->asf_header_read_size= mms->asf_header_size;
00386 }else
00387 h->priv_data= mmsh;
00388 return ret;
00389 }
00390
00391 static int64_t mmsh_seek(URLContext *h, int64_t pos, int whence)
00392 {
00393 MMSHContext *mmsh = h->priv_data;
00394 MMSContext *mms = &mmsh->mms;
00395
00396 if(pos == 0 && whence == SEEK_CUR)
00397 return mms->asf_header_read_size + mms->remaining_in_len + mmsh->chunk_seq * mms->asf_packet_len;
00398 return AVERROR(ENOSYS);
00399 }
00400
00401 URLProtocol ff_mmsh_protocol = {
00402 .name = "mmsh",
00403 .url_open = mmsh_open,
00404 .url_read = mmsh_read,
00405 .url_seek = mmsh_seek,
00406 .url_close = mmsh_close,
00407 .url_read_seek = mmsh_read_seek,
00408 .priv_data_size = sizeof(MMSHContext),
00409 };