• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/mmsh.c

Go to the documentation of this file.
00001 /*
00002  * MMS protocol over HTTP
00003  * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /*
00023  * Reference
00024  * Windows Media HTTP Streaming Protocol.
00025  * http://msdn.microsoft.com/en-us/library/cc251059(PROT.10).aspx
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 // see Ref 2.2.1.8
00042 #define USERAGENT  "User-Agent: NSPlayer/4.1.0.3856\r\n"
00043 // see Ref 2.2.1.4.33
00044 // the guid value can be changed to any valid value.
00045 #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
00046 
00047 // see Ref 2.2.3 for packet type define:
00048 // chunk type contains 2 fields: Frame and PacketID.
00049 // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
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     int request_seq;  
00060     int chunk_seq;    
00061 } MMSHContext;
00062 
00063 static int mmsh_close(URLContext *h)
00064 {
00065     MMSHContext *mmsh = (MMSHContext *)h->priv_data;
00066     MMSContext *mms   = &mmsh->mms;
00067     if (mms->mms_hd)
00068         ffurl_close(mms->mms_hd);
00069     av_free(mms->streams);
00070     av_free(mms->asf_header);
00071     return 0;
00072 }
00073 
00074 static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
00075 {
00076     MMSContext *mms = &mmsh->mms;
00077     uint8_t chunk_header[CHUNK_HEADER_LENGTH];
00078     uint8_t ext_header[EXT_HEADER_LENGTH];
00079     ChunkType chunk_type;
00080     int chunk_len, res, ext_header_len;
00081 
00082     res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
00083     if (res != CHUNK_HEADER_LENGTH) {
00084         av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
00085         return AVERROR(EIO);
00086     }
00087     chunk_type = AV_RL16(chunk_header);
00088     chunk_len  = AV_RL16(chunk_header + 2);
00089 
00090     switch (chunk_type) {
00091     case CHUNK_TYPE_END:
00092     case CHUNK_TYPE_STREAM_CHANGE:
00093         ext_header_len = 4;
00094         break;
00095     case CHUNK_TYPE_ASF_HEADER:
00096     case CHUNK_TYPE_DATA:
00097         ext_header_len = 8;
00098         break;
00099     default:
00100         av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
00101         return AVERROR_INVALIDDATA;
00102     }
00103 
00104     res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
00105     if (res != ext_header_len) {
00106         av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
00107         return AVERROR(EIO);
00108     }
00109     *len = chunk_len - ext_header_len;
00110     if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
00111         mmsh->chunk_seq = AV_RL32(ext_header);
00112     return chunk_type;
00113 }
00114 
00115 static int read_data_packet(MMSHContext *mmsh, const int len)
00116 {
00117     MMSContext *mms   = &mmsh->mms;
00118     int res;
00119     if (len > sizeof(mms->in_buffer)) {
00120         av_log(NULL, AV_LOG_ERROR,
00121                "Data packet length %d exceeds the in_buffer size %zu\n",
00122                len, sizeof(mms->in_buffer));
00123         return AVERROR(EIO);
00124     }
00125     res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
00126     av_dlog(NULL, "Data packet len = %d\n", len);
00127     if (res != len) {
00128         av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
00129         return AVERROR(EIO);
00130     }
00131     if (len > mms->asf_packet_len) {
00132         av_log(NULL, AV_LOG_ERROR,
00133                "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
00134         return AVERROR_INVALIDDATA;
00135     } else {
00136         memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
00137     }
00138     mms->read_in_ptr      = mms->in_buffer;
00139     mms->remaining_in_len = mms->asf_packet_len;
00140     return 0;
00141 }
00142 
00143 static int get_http_header_data(MMSHContext *mmsh)
00144 {
00145     MMSContext *mms = &mmsh->mms;
00146     int res, len;
00147     ChunkType chunk_type;
00148 
00149     for (;;) {
00150         len = 0;
00151         res = chunk_type = get_chunk_header(mmsh, &len);
00152         if (res < 0) {
00153             return res;
00154         } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
00155             // get asf header and stored it
00156             if (!mms->header_parsed) {
00157                 if (mms->asf_header) {
00158                     if (len != mms->asf_header_size) {
00159                         mms->asf_header_size = len;
00160                         av_dlog(NULL, "Header len changed from %d to %d\n",
00161                                 mms->asf_header_size, len);
00162                         av_freep(&mms->asf_header);
00163                     }
00164                 }
00165                 mms->asf_header = av_mallocz(len);
00166                 if (!mms->asf_header) {
00167                     return AVERROR(ENOMEM);
00168                 }
00169                 mms->asf_header_size = len;
00170             }
00171             if (len > mms->asf_header_size) {
00172                 av_log(NULL, AV_LOG_ERROR,
00173                        "Asf header packet len = %d exceed the asf header buf size %d\n",
00174                        len, mms->asf_header_size);
00175                 return AVERROR(EIO);
00176             }
00177             res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
00178             if (res != len) {
00179                 av_log(NULL, AV_LOG_ERROR,
00180                        "Recv asf header data len %d != expected len %d\n", res, len);
00181                 return AVERROR(EIO);
00182             }
00183             mms->asf_header_size = len;
00184             if (!mms->header_parsed) {
00185                 res = ff_mms_asf_header_parser(mms);
00186                 mms->header_parsed = 1;
00187                 return res;
00188             }
00189         } else if (chunk_type == CHUNK_TYPE_DATA) {
00190             // read data packet and do padding
00191             return read_data_packet(mmsh, len);
00192         } else {
00193             if (len) {
00194                 if (len > sizeof(mms->in_buffer)) {
00195                     av_log(NULL, AV_LOG_ERROR,
00196                            "Other packet len = %d exceed the in_buffer size %zu\n",
00197                            len, sizeof(mms->in_buffer));
00198                     return AVERROR(EIO);
00199                 }
00200                 res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
00201                 if (res != len) {
00202                     av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
00203                     return AVERROR(EIO);
00204                 } else {
00205                     av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
00206                     continue;
00207                 }
00208             }
00209         }
00210     }
00211 }
00212 
00213 static int mmsh_open(URLContext *h, const char *uri, int flags)
00214 {
00215     int i, port, err;
00216     char httpname[256], path[256], host[128], location[1024];
00217     char *stream_selection = NULL;
00218     char headers[1024];
00219     MMSHContext *mmsh = h->priv_data;
00220     MMSContext *mms;
00221 
00222     mmsh->request_seq = h->is_streamed = 1;
00223     mms = &mmsh->mms;
00224     av_strlcpy(location, uri, sizeof(location));
00225 
00226     av_url_split(NULL, 0, NULL, 0,
00227         host, sizeof(host), &port, path, sizeof(path), location);
00228     if (port<0)
00229         port = 80; // default mmsh protocol port
00230     ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
00231 
00232     if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
00233                     &h->interrupt_callback) < 0) {
00234         return AVERROR(EIO);
00235     }
00236 
00237     snprintf(headers, sizeof(headers),
00238              "Accept: */*\r\n"
00239              USERAGENT
00240              "Host: %s:%d\r\n"
00241              "Pragma: no-cache,rate=1.000000,stream-time=0,"
00242              "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
00243              CLIENTGUID
00244              "Connection: Close\r\n\r\n",
00245              host, port, mmsh->request_seq++);
00246     av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
00247 
00248     err = ffurl_connect(mms->mms_hd, NULL);
00249     if (err) {
00250         goto fail;
00251     }
00252     err = get_http_header_data(mmsh);
00253     if (err) {
00254         av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
00255         goto fail;
00256     }
00257 
00258     // close the socket and then reopen it for sending the second play request.
00259     ffurl_close(mms->mms_hd);
00260     memset(headers, 0, sizeof(headers));
00261     if ((err = ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
00262                            &h->interrupt_callback)) < 0) {
00263         goto fail;
00264     }
00265     stream_selection = av_mallocz(mms->stream_num * 19 + 1);
00266     if (!stream_selection)
00267         return AVERROR(ENOMEM);
00268     for (i = 0; i < mms->stream_num; i++) {
00269         char tmp[20];
00270         err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
00271         if (err < 0)
00272             goto fail;
00273         av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
00274     }
00275     // send play request
00276     err = snprintf(headers, sizeof(headers),
00277                    "Accept: */*\r\n"
00278                    USERAGENT
00279                    "Host: %s:%d\r\n"
00280                    "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
00281                    "Pragma: xPlayStrm=1\r\n"
00282                    CLIENTGUID
00283                    "Pragma: stream-switch-count=%d\r\n"
00284                    "Pragma: stream-switch-entry=%s\r\n"
00285                    "Connection: Close\r\n\r\n",
00286                    host, port, mmsh->request_seq++, mms->stream_num, stream_selection);
00287     av_freep(&stream_selection);
00288     if (err < 0) {
00289         av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
00290         goto fail;
00291     }
00292     av_dlog(NULL, "out_buffer is %s", headers);
00293     av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
00294 
00295     err = ffurl_connect(mms->mms_hd, NULL);
00296     if (err) {
00297           goto fail;
00298     }
00299 
00300     err = get_http_header_data(mmsh);
00301     if (err) {
00302         av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
00303         goto fail;
00304     }
00305 
00306     av_dlog(NULL, "Connection successfully open\n");
00307     return 0;
00308 fail:
00309     av_freep(&stream_selection);
00310     mmsh_close(h);
00311     av_dlog(NULL, "Connection failed with error %d\n", err);
00312     return err;
00313 }
00314 
00315 static int handle_chunk_type(MMSHContext *mmsh)
00316 {
00317     MMSContext *mms = &mmsh->mms;
00318     int res, len = 0;
00319     ChunkType chunk_type;
00320     chunk_type = get_chunk_header(mmsh, &len);
00321 
00322     switch (chunk_type) {
00323     case CHUNK_TYPE_END:
00324         mmsh->chunk_seq = 0;
00325         av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
00326         return AVERROR(EIO);
00327     case CHUNK_TYPE_STREAM_CHANGE:
00328         mms->header_parsed = 0;
00329         if (res = get_http_header_data(mmsh)) {
00330             av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
00331             return res;
00332         }
00333         break;
00334     case CHUNK_TYPE_DATA:
00335         return read_data_packet(mmsh, len);
00336     default:
00337         av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
00338         return AVERROR_INVALIDDATA;
00339     }
00340     return 0;
00341 }
00342 
00343 static int mmsh_read(URLContext *h, uint8_t *buf, int size)
00344 {
00345     int res = 0;
00346     MMSHContext *mmsh = h->priv_data;
00347     MMSContext *mms   = &mmsh->mms;
00348     do {
00349         if (mms->asf_header_read_size < mms->asf_header_size) {
00350             // copy asf header into buffer
00351             res = ff_mms_read_header(mms, buf, size);
00352         } else {
00353             if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
00354                 return res;
00355             res = ff_mms_read_data(mms, buf, size);
00356         }
00357     } while (!res);
00358     return res;
00359 }
00360 
00361 URLProtocol ff_mmsh_protocol = {
00362     .name           = "mmsh",
00363     .url_open       = mmsh_open,
00364     .url_read       = mmsh_read,
00365     .url_close      = mmsh_close,
00366     .priv_data_size = sizeof(MMSHContext),
00367     .flags          = URL_PROTOCOL_FLAG_NETWORK,
00368 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1