mmsh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "libavutil/opt.h"
  30. #include "internal.h"
  31. #include "mms.h"
  32. #include "asf.h"
  33. #include "http.h"
  34. #include "url.h"
  35. #define CHUNK_HEADER_LENGTH 4 // 2bytes chunk type and 2bytes chunk length.
  36. #define EXT_HEADER_LENGTH 8 // 4bytes sequence, 2bytes useless and 2bytes chunk length.
  37. // see Ref 2.2.1.8
  38. #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
  39. // see Ref 2.2.1.4.33
  40. // the guid value can be changed to any valid value.
  41. #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
  42. // see Ref 2.2.3 for packet type define:
  43. // chunk type contains 2 fields: Frame and PacketID.
  44. // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
  45. typedef enum {
  46. CHUNK_TYPE_DATA = 0x4424,
  47. CHUNK_TYPE_ASF_HEADER = 0x4824,
  48. CHUNK_TYPE_END = 0x4524,
  49. CHUNK_TYPE_STREAM_CHANGE = 0x4324,
  50. } ChunkType;
  51. typedef struct {
  52. MMSContext mms;
  53. uint8_t location[1024];
  54. int request_seq; ///< request packet sequence
  55. int chunk_seq; ///< data packet sequence
  56. } MMSHContext;
  57. static int mmsh_close(URLContext *h)
  58. {
  59. MMSHContext *mmsh = (MMSHContext *)h->priv_data;
  60. MMSContext *mms = &mmsh->mms;
  61. if (mms->mms_hd)
  62. ffurl_close(mms->mms_hd);
  63. av_free(mms->streams);
  64. av_free(mms->asf_header);
  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 = h->priv_data;
  206. MMSContext *mms;
  207. mmsh->request_seq = h->is_streamed = 1;
  208. mms = &mmsh->mms;
  209. av_strlcpy(mmsh->location, uri, sizeof(mmsh->location));
  210. av_url_split(NULL, 0, NULL, 0,
  211. host, sizeof(host), &port, path, sizeof(path), mmsh->location);
  212. if (port<0)
  213. port = 80; // default mmsh protocol port
  214. ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
  215. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
  216. &h->interrupt_callback) < 0) {
  217. return AVERROR(EIO);
  218. }
  219. snprintf(headers, sizeof(headers),
  220. "Accept: */*\r\n"
  221. USERAGENT
  222. "Host: %s:%d\r\n"
  223. "Pragma: no-cache,rate=1.000000,stream-time=0,"
  224. "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
  225. CLIENTGUID
  226. "Connection: Close\r\n",
  227. host, port, mmsh->request_seq++);
  228. av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
  229. err = ffurl_connect(mms->mms_hd, NULL);
  230. if (err) {
  231. goto fail;
  232. }
  233. err = get_http_header_data(mmsh);
  234. if (err) {
  235. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  236. goto fail;
  237. }
  238. // close the socket and then reopen it for sending the second play request.
  239. ffurl_close(mms->mms_hd);
  240. memset(headers, 0, sizeof(headers));
  241. if ((err = ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
  242. &h->interrupt_callback)) < 0) {
  243. goto fail;
  244. }
  245. stream_selection = av_mallocz(mms->stream_num * 19 + 1);
  246. if (!stream_selection)
  247. return AVERROR(ENOMEM);
  248. for (i = 0; i < mms->stream_num; i++) {
  249. char tmp[20];
  250. err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
  251. if (err < 0)
  252. goto fail;
  253. av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
  254. }
  255. // send play request
  256. err = snprintf(headers, sizeof(headers),
  257. "Accept: */*\r\n"
  258. USERAGENT
  259. "Host: %s:%d\r\n"
  260. "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
  261. "Pragma: xPlayStrm=1\r\n"
  262. CLIENTGUID
  263. "Pragma: stream-switch-count=%d\r\n"
  264. "Pragma: stream-switch-entry=%s\r\n"
  265. "Pragma: no-cache,rate=1.000000,stream-time=%u"
  266. "Connection: Close\r\n",
  267. host, port, mmsh->request_seq++, mms->stream_num, stream_selection, timestamp);
  268. av_freep(&stream_selection);
  269. if (err < 0) {
  270. av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
  271. goto fail;
  272. }
  273. av_dlog(NULL, "out_buffer is %s", headers);
  274. av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
  275. err = ffurl_connect(mms->mms_hd, NULL);
  276. if (err) {
  277. goto fail;
  278. }
  279. err = get_http_header_data(mmsh);
  280. if (err) {
  281. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  282. goto fail;
  283. }
  284. av_dlog(NULL, "Connection successfully open\n");
  285. return 0;
  286. fail:
  287. av_freep(&stream_selection);
  288. av_dlog(NULL, "Connection failed with error %d\n", err);
  289. return err;
  290. }
  291. static int mmsh_open(URLContext *h, const char *uri, int flags)
  292. {
  293. int ret = mmsh_open_internal(h, uri, flags, 0, 0);
  294. if (ret < 0)
  295. mmsh_close(h);
  296. return ret;
  297. }
  298. static int handle_chunk_type(MMSHContext *mmsh)
  299. {
  300. MMSContext *mms = &mmsh->mms;
  301. int res, len = 0;
  302. ChunkType chunk_type;
  303. chunk_type = get_chunk_header(mmsh, &len);
  304. switch (chunk_type) {
  305. case CHUNK_TYPE_END:
  306. mmsh->chunk_seq = 0;
  307. av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
  308. return AVERROR(EIO);
  309. case CHUNK_TYPE_STREAM_CHANGE:
  310. mms->header_parsed = 0;
  311. if (res = get_http_header_data(mmsh)) {
  312. av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
  313. return res;
  314. }
  315. break;
  316. case CHUNK_TYPE_DATA:
  317. return read_data_packet(mmsh, len);
  318. default:
  319. av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
  320. return AVERROR_INVALIDDATA;
  321. }
  322. return 0;
  323. }
  324. static int mmsh_read(URLContext *h, uint8_t *buf, int size)
  325. {
  326. int res = 0;
  327. MMSHContext *mmsh = h->priv_data;
  328. MMSContext *mms = &mmsh->mms;
  329. do {
  330. if (mms->asf_header_read_size < mms->asf_header_size) {
  331. // copy asf header into buffer
  332. res = ff_mms_read_header(mms, buf, size);
  333. } else {
  334. if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
  335. return res;
  336. res = ff_mms_read_data(mms, buf, size);
  337. }
  338. } while (!res);
  339. return res;
  340. }
  341. static int64_t mmsh_read_seek(URLContext *h, int stream_index,
  342. int64_t timestamp, int flags)
  343. {
  344. MMSHContext *mmsh = h->priv_data;
  345. MMSContext *mms = &mmsh->mms;
  346. int ret;
  347. ret= mmsh_open_internal(h, mmsh->location, 0, FFMAX(timestamp, 0), 0);
  348. if(ret>=0){
  349. if (mms->mms_hd)
  350. ffurl_close(mms->mms_hd);
  351. av_freep(&mms->streams);
  352. av_freep(&mms->asf_header);
  353. av_free(mmsh);
  354. mmsh = h->priv_data;
  355. mms = &mmsh->mms;
  356. mms->asf_header_read_size= mms->asf_header_size;
  357. }else
  358. h->priv_data= mmsh;
  359. return ret;
  360. }
  361. static int64_t mmsh_seek(URLContext *h, int64_t pos, int whence)
  362. {
  363. MMSHContext *mmsh = h->priv_data;
  364. MMSContext *mms = &mmsh->mms;
  365. if(pos == 0 && whence == SEEK_CUR)
  366. return mms->asf_header_read_size + mms->remaining_in_len + mmsh->chunk_seq * mms->asf_packet_len;
  367. return AVERROR(ENOSYS);
  368. }
  369. URLProtocol ff_mmsh_protocol = {
  370. .name = "mmsh",
  371. .url_open = mmsh_open,
  372. .url_read = mmsh_read,
  373. .url_seek = mmsh_seek,
  374. .url_close = mmsh_close,
  375. .url_read_seek = mmsh_read_seek,
  376. .priv_data_size = sizeof(MMSHContext),
  377. .flags = URL_PROTOCOL_FLAG_NETWORK,
  378. };