mmsh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * Reference
  23. * Windows Media HTTP Streaming Protocol.
  24. * http://msdn.microsoft.com/en-us/library/cc251059(PROT.10).aspx
  25. */
  26. #include <string.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/avstring.h"
  29. #include "internal.h"
  30. #include "mms.h"
  31. #include "asf.h"
  32. #include "http.h"
  33. #include "url.h"
  34. #define CHUNK_HEADER_LENGTH 4 // 2bytes chunk type and 2bytes chunk length.
  35. #define EXT_HEADER_LENGTH 8 // 4bytes sequence, 2bytes useless and 2bytes chunk length.
  36. // see Ref 2.2.1.8
  37. #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
  38. // see Ref 2.2.1.4.33
  39. // the guid value can be changed to any valid value.
  40. #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
  41. // see Ref 2.2.3 for packet type define:
  42. // chunk type contains 2 fields: Frame and PacketID.
  43. // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
  44. typedef enum {
  45. CHUNK_TYPE_DATA = 0x4424,
  46. CHUNK_TYPE_ASF_HEADER = 0x4824,
  47. CHUNK_TYPE_END = 0x4524,
  48. CHUNK_TYPE_STREAM_CHANGE = 0x4324,
  49. } ChunkType;
  50. typedef struct {
  51. MMSContext mms;
  52. uint8_t location[1024];
  53. int request_seq; ///< request packet sequence
  54. int chunk_seq; ///< data packet sequence
  55. } MMSHContext;
  56. static int mmsh_close(URLContext *h)
  57. {
  58. MMSHContext *mmsh = (MMSHContext *)h->priv_data;
  59. MMSContext *mms = &mmsh->mms;
  60. if (mms->mms_hd)
  61. ffurl_close(mms->mms_hd);
  62. av_free(mms->streams);
  63. av_free(mms->asf_header);
  64. av_freep(&h->priv_data);
  65. return 0;
  66. }
  67. static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
  68. {
  69. MMSContext *mms = &mmsh->mms;
  70. uint8_t chunk_header[CHUNK_HEADER_LENGTH];
  71. uint8_t ext_header[EXT_HEADER_LENGTH];
  72. ChunkType chunk_type;
  73. int chunk_len, res, ext_header_len;
  74. res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
  75. if (res != CHUNK_HEADER_LENGTH) {
  76. av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
  77. return AVERROR(EIO);
  78. }
  79. chunk_type = AV_RL16(chunk_header);
  80. chunk_len = AV_RL16(chunk_header + 2);
  81. switch (chunk_type) {
  82. case CHUNK_TYPE_END:
  83. case CHUNK_TYPE_STREAM_CHANGE:
  84. ext_header_len = 4;
  85. break;
  86. case CHUNK_TYPE_ASF_HEADER:
  87. case CHUNK_TYPE_DATA:
  88. ext_header_len = 8;
  89. break;
  90. default:
  91. av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
  92. return AVERROR_INVALIDDATA;
  93. }
  94. res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
  95. if (res != ext_header_len) {
  96. av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
  97. return AVERROR(EIO);
  98. }
  99. *len = chunk_len - ext_header_len;
  100. if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
  101. mmsh->chunk_seq = AV_RL32(ext_header);
  102. return chunk_type;
  103. }
  104. static int read_data_packet(MMSHContext *mmsh, const int len)
  105. {
  106. MMSContext *mms = &mmsh->mms;
  107. int res;
  108. if (len > sizeof(mms->in_buffer)) {
  109. av_log(NULL, AV_LOG_ERROR,
  110. "Data packet length %d exceeds the in_buffer size %zu\n",
  111. len, sizeof(mms->in_buffer));
  112. return AVERROR(EIO);
  113. }
  114. res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
  115. av_dlog(NULL, "Data packet len = %d\n", len);
  116. if (res != len) {
  117. av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
  118. return AVERROR(EIO);
  119. }
  120. if (len > mms->asf_packet_len) {
  121. av_log(NULL, AV_LOG_ERROR,
  122. "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
  123. return AVERROR_INVALIDDATA;
  124. } else {
  125. memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
  126. }
  127. mms->read_in_ptr = mms->in_buffer;
  128. mms->remaining_in_len = mms->asf_packet_len;
  129. return 0;
  130. }
  131. static int get_http_header_data(MMSHContext *mmsh)
  132. {
  133. MMSContext *mms = &mmsh->mms;
  134. int res, len;
  135. ChunkType chunk_type;
  136. for (;;) {
  137. len = 0;
  138. res = chunk_type = get_chunk_header(mmsh, &len);
  139. if (res < 0) {
  140. return res;
  141. } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
  142. // get asf header and stored it
  143. if (!mms->header_parsed) {
  144. if (mms->asf_header) {
  145. if (len != mms->asf_header_size) {
  146. mms->asf_header_size = len;
  147. av_dlog(NULL, "Header len changed from %d to %d\n",
  148. mms->asf_header_size, len);
  149. av_freep(&mms->asf_header);
  150. }
  151. }
  152. mms->asf_header = av_mallocz(len);
  153. if (!mms->asf_header) {
  154. return AVERROR(ENOMEM);
  155. }
  156. mms->asf_header_size = len;
  157. }
  158. if (len > mms->asf_header_size) {
  159. av_log(NULL, AV_LOG_ERROR,
  160. "Asf header packet len = %d exceed the asf header buf size %d\n",
  161. len, mms->asf_header_size);
  162. return AVERROR(EIO);
  163. }
  164. res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
  165. if (res != len) {
  166. av_log(NULL, AV_LOG_ERROR,
  167. "Recv asf header data len %d != expected len %d\n", res, len);
  168. return AVERROR(EIO);
  169. }
  170. mms->asf_header_size = len;
  171. if (!mms->header_parsed) {
  172. res = ff_mms_asf_header_parser(mms);
  173. mms->header_parsed = 1;
  174. return res;
  175. }
  176. } else if (chunk_type == CHUNK_TYPE_DATA) {
  177. // read data packet and do padding
  178. return read_data_packet(mmsh, len);
  179. } else {
  180. if (len) {
  181. if (len > sizeof(mms->in_buffer)) {
  182. av_log(NULL, AV_LOG_ERROR,
  183. "Other packet len = %d exceed the in_buffer size %zu\n",
  184. len, sizeof(mms->in_buffer));
  185. return AVERROR(EIO);
  186. }
  187. res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
  188. if (res != len) {
  189. av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
  190. return AVERROR(EIO);
  191. } else {
  192. av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
  193. continue;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. static int mmsh_open_internal(URLContext *h, const char *uri, int flags, int timestamp, int64_t pos)
  200. {
  201. int i, port, err;
  202. char httpname[256], path[256], host[128];
  203. char *stream_selection = NULL;
  204. char headers[1024];
  205. MMSHContext *mmsh;
  206. MMSContext *mms;
  207. mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
  208. if (!h->priv_data)
  209. return AVERROR(ENOMEM);
  210. mmsh->request_seq = h->is_streamed = 1;
  211. mms = &mmsh->mms;
  212. av_strlcpy(mmsh->location, uri, sizeof(mmsh->location));
  213. av_url_split(NULL, 0, NULL, 0,
  214. host, sizeof(host), &port, path, sizeof(path), mmsh->location);
  215. if (port<0)
  216. port = 80; // default mmsh protocol port
  217. ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
  218. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ) < 0) {
  219. return AVERROR(EIO);
  220. }
  221. snprintf(headers, sizeof(headers),
  222. "Accept: */*\r\n"
  223. USERAGENT
  224. "Host: %s:%d\r\n"
  225. "Pragma: no-cache,rate=1.000000,stream-time=0,"
  226. "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
  227. CLIENTGUID
  228. "Connection: Close\r\n",
  229. host, port, mmsh->request_seq++);
  230. ff_http_set_headers(mms->mms_hd, headers);
  231. err = ffurl_connect(mms->mms_hd);
  232. if (err) {
  233. goto fail;
  234. }
  235. err = get_http_header_data(mmsh);
  236. if (err) {
  237. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  238. goto fail;
  239. }
  240. // close the socket and then reopen it for sending the second play request.
  241. ffurl_close(mms->mms_hd);
  242. memset(headers, 0, sizeof(headers));
  243. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ) < 0) {
  244. return AVERROR(EIO);
  245. }
  246. stream_selection = av_mallocz(mms->stream_num * 19 + 1);
  247. if (!stream_selection)
  248. return AVERROR(ENOMEM);
  249. for (i = 0; i < mms->stream_num; i++) {
  250. char tmp[20];
  251. err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
  252. if (err < 0)
  253. goto fail;
  254. av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
  255. }
  256. // send play request
  257. err = snprintf(headers, sizeof(headers),
  258. "Accept: */*\r\n"
  259. USERAGENT
  260. "Host: %s:%d\r\n"
  261. "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
  262. "Pragma: xPlayStrm=1\r\n"
  263. CLIENTGUID
  264. "Pragma: stream-switch-count=%d\r\n"
  265. "Pragma: stream-switch-entry=%s\r\n"
  266. "Pragma: no-cache,rate=1.000000,stream-time=%u"
  267. "Connection: Close\r\n",
  268. host, port, mmsh->request_seq++, mms->stream_num, stream_selection, timestamp);
  269. av_freep(&stream_selection);
  270. if (err < 0) {
  271. av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
  272. goto fail;
  273. }
  274. av_dlog(NULL, "out_buffer is %s", headers);
  275. ff_http_set_headers(mms->mms_hd, headers);
  276. err = ffurl_connect(mms->mms_hd);
  277. if (err) {
  278. goto fail;
  279. }
  280. err = get_http_header_data(mmsh);
  281. if (err) {
  282. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  283. goto fail;
  284. }
  285. av_dlog(NULL, "Connection successfully open\n");
  286. return 0;
  287. fail:
  288. av_freep(&stream_selection);
  289. mmsh_close(h);
  290. av_dlog(NULL, "Connection failed with error %d\n", err);
  291. return err;
  292. }
  293. static int mmsh_open(URLContext *h, const char *uri, int flags)
  294. {
  295. return mmsh_open_internal(h, uri, flags, 0, 0);
  296. }
  297. static int handle_chunk_type(MMSHContext *mmsh)
  298. {
  299. MMSContext *mms = &mmsh->mms;
  300. int res, len = 0;
  301. ChunkType chunk_type;
  302. chunk_type = get_chunk_header(mmsh, &len);
  303. switch (chunk_type) {
  304. case CHUNK_TYPE_END:
  305. mmsh->chunk_seq = 0;
  306. av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
  307. return AVERROR(EIO);
  308. case CHUNK_TYPE_STREAM_CHANGE:
  309. mms->header_parsed = 0;
  310. if (res = get_http_header_data(mmsh)) {
  311. av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
  312. return res;
  313. }
  314. break;
  315. case CHUNK_TYPE_DATA:
  316. return read_data_packet(mmsh, len);
  317. default:
  318. av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
  319. return AVERROR_INVALIDDATA;
  320. }
  321. return 0;
  322. }
  323. static int mmsh_read(URLContext *h, uint8_t *buf, int size)
  324. {
  325. int res = 0;
  326. MMSHContext *mmsh = h->priv_data;
  327. MMSContext *mms = &mmsh->mms;
  328. do {
  329. if (mms->asf_header_read_size < mms->asf_header_size) {
  330. // copy asf header into buffer
  331. res = ff_mms_read_header(mms, buf, size);
  332. } else {
  333. if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
  334. return res;
  335. res = ff_mms_read_data(mms, buf, size);
  336. }
  337. } while (!res);
  338. return res;
  339. }
  340. static int64_t mmsh_read_seek(URLContext *h, int stream_index,
  341. int64_t timestamp, int flags)
  342. {
  343. MMSHContext *mmsh = h->priv_data;
  344. MMSContext *mms = &mmsh->mms;
  345. int ret;
  346. ret= mmsh_open_internal(h, mmsh->location, 0, FFMAX(timestamp, 0), 0);
  347. if(ret>=0){
  348. if (mms->mms_hd)
  349. ffurl_close(mms->mms_hd);
  350. av_freep(&mms->streams);
  351. av_freep(&mms->asf_header);
  352. av_free(mmsh);
  353. mmsh = h->priv_data;
  354. mms = &mmsh->mms;
  355. mms->asf_header_read_size= mms->asf_header_size;
  356. }else
  357. h->priv_data= mmsh;
  358. return ret;
  359. }
  360. static int64_t mmsh_seek(URLContext *h, int64_t pos, int whence)
  361. {
  362. MMSHContext *mmsh = h->priv_data;
  363. MMSContext *mms = &mmsh->mms;
  364. if(pos == 0 && whence == SEEK_CUR)
  365. return mms->asf_header_read_size + mms->remaining_in_len + mmsh->chunk_seq * mms->asf_packet_len;
  366. return AVERROR(ENOSYS);
  367. }
  368. URLProtocol ff_mmsh_protocol = {
  369. .name = "mmsh",
  370. .url_open = mmsh_open,
  371. .url_read = mmsh_read,
  372. .url_write = NULL,
  373. .url_seek = mmsh_seek,
  374. .url_close = mmsh_close,
  375. .url_read_seek = mmsh_read_seek,
  376. };