mmsh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * MMS protocol over HTTP
  3. * Copyright (c) 2010 Zhentan Feng <spyfeng at gmail dot com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. 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. return 0;
  65. }
  66. static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
  67. {
  68. MMSContext *mms = &mmsh->mms;
  69. uint8_t chunk_header[CHUNK_HEADER_LENGTH];
  70. uint8_t ext_header[EXT_HEADER_LENGTH];
  71. ChunkType chunk_type;
  72. int chunk_len, res, ext_header_len;
  73. res = ffurl_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
  74. if (res != CHUNK_HEADER_LENGTH) {
  75. av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
  76. return AVERROR(EIO);
  77. }
  78. chunk_type = AV_RL16(chunk_header);
  79. chunk_len = AV_RL16(chunk_header + 2);
  80. switch (chunk_type) {
  81. case CHUNK_TYPE_END:
  82. case CHUNK_TYPE_STREAM_CHANGE:
  83. ext_header_len = 4;
  84. break;
  85. case CHUNK_TYPE_ASF_HEADER:
  86. case CHUNK_TYPE_DATA:
  87. ext_header_len = 8;
  88. break;
  89. default:
  90. av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
  91. return AVERROR_INVALIDDATA;
  92. }
  93. res = ffurl_read_complete(mms->mms_hd, ext_header, ext_header_len);
  94. if (res != ext_header_len) {
  95. av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
  96. return AVERROR(EIO);
  97. }
  98. *len = chunk_len - ext_header_len;
  99. if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
  100. mmsh->chunk_seq = AV_RL32(ext_header);
  101. return chunk_type;
  102. }
  103. static int read_data_packet(MMSHContext *mmsh, const int len)
  104. {
  105. MMSContext *mms = &mmsh->mms;
  106. int res;
  107. if (len > sizeof(mms->in_buffer)) {
  108. av_log(NULL, AV_LOG_ERROR,
  109. "Data packet length %d exceeds the in_buffer size %zu\n",
  110. len, sizeof(mms->in_buffer));
  111. return AVERROR(EIO);
  112. }
  113. res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
  114. av_dlog(NULL, "Data packet len = %d\n", len);
  115. if (res != len) {
  116. av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
  117. return AVERROR(EIO);
  118. }
  119. if (len > mms->asf_packet_len) {
  120. av_log(NULL, AV_LOG_ERROR,
  121. "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
  122. return AVERROR_INVALIDDATA;
  123. } else {
  124. memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
  125. }
  126. mms->read_in_ptr = mms->in_buffer;
  127. mms->remaining_in_len = mms->asf_packet_len;
  128. return 0;
  129. }
  130. static int get_http_header_data(MMSHContext *mmsh)
  131. {
  132. MMSContext *mms = &mmsh->mms;
  133. int res, len;
  134. ChunkType chunk_type;
  135. for (;;) {
  136. len = 0;
  137. res = chunk_type = get_chunk_header(mmsh, &len);
  138. if (res < 0) {
  139. return res;
  140. } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
  141. // get asf header and stored it
  142. if (!mms->header_parsed) {
  143. if (mms->asf_header) {
  144. if (len != mms->asf_header_size) {
  145. mms->asf_header_size = len;
  146. av_dlog(NULL, "Header len changed from %d to %d\n",
  147. mms->asf_header_size, len);
  148. av_freep(&mms->asf_header);
  149. }
  150. }
  151. mms->asf_header = av_mallocz(len);
  152. if (!mms->asf_header) {
  153. return AVERROR(ENOMEM);
  154. }
  155. mms->asf_header_size = len;
  156. }
  157. if (len > mms->asf_header_size) {
  158. av_log(NULL, AV_LOG_ERROR,
  159. "Asf header packet len = %d exceed the asf header buf size %d\n",
  160. len, mms->asf_header_size);
  161. return AVERROR(EIO);
  162. }
  163. res = ffurl_read_complete(mms->mms_hd, mms->asf_header, len);
  164. if (res != len) {
  165. av_log(NULL, AV_LOG_ERROR,
  166. "Recv asf header data len %d != expected len %d\n", res, len);
  167. return AVERROR(EIO);
  168. }
  169. mms->asf_header_size = len;
  170. if (!mms->header_parsed) {
  171. res = ff_mms_asf_header_parser(mms);
  172. mms->header_parsed = 1;
  173. return res;
  174. }
  175. } else if (chunk_type == CHUNK_TYPE_DATA) {
  176. // read data packet and do padding
  177. return read_data_packet(mmsh, len);
  178. } else {
  179. if (len) {
  180. if (len > sizeof(mms->in_buffer)) {
  181. av_log(NULL, AV_LOG_ERROR,
  182. "Other packet len = %d exceed the in_buffer size %zu\n",
  183. len, sizeof(mms->in_buffer));
  184. return AVERROR(EIO);
  185. }
  186. res = ffurl_read_complete(mms->mms_hd, mms->in_buffer, len);
  187. if (res != len) {
  188. av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
  189. return AVERROR(EIO);
  190. } else {
  191. av_dlog(NULL, "Skip chunk type %d \n", chunk_type);
  192. continue;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. static int mmsh_open(URLContext *h, const char *uri, int flags)
  199. {
  200. int i, port, err;
  201. char httpname[256], path[256], host[128], location[1024];
  202. char *stream_selection = NULL;
  203. char headers[1024];
  204. MMSHContext *mmsh = h->priv_data;
  205. MMSContext *mms;
  206. mmsh->request_seq = h->is_streamed = 1;
  207. mms = &mmsh->mms;
  208. av_strlcpy(location, uri, sizeof(location));
  209. av_url_split(NULL, 0, NULL, 0,
  210. host, sizeof(host), &port, path, sizeof(path), location);
  211. if (port<0)
  212. port = 80; // default mmsh protocol port
  213. ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
  214. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
  215. &h->interrupt_callback) < 0) {
  216. return AVERROR(EIO);
  217. }
  218. snprintf(headers, sizeof(headers),
  219. "Accept: */*\r\n"
  220. USERAGENT
  221. "Host: %s:%d\r\n"
  222. "Pragma: no-cache,rate=1.000000,stream-time=0,"
  223. "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
  224. CLIENTGUID
  225. "Connection: Close\r\n\r\n",
  226. host, port, mmsh->request_seq++);
  227. av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
  228. err = ffurl_connect(mms->mms_hd, NULL);
  229. if (err) {
  230. goto fail;
  231. }
  232. err = get_http_header_data(mmsh);
  233. if (err) {
  234. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  235. goto fail;
  236. }
  237. // close the socket and then reopen it for sending the second play request.
  238. ffurl_close(mms->mms_hd);
  239. memset(headers, 0, sizeof(headers));
  240. if ((err = ffurl_alloc(&mms->mms_hd, httpname, AVIO_FLAG_READ,
  241. &h->interrupt_callback)) < 0) {
  242. goto fail;
  243. }
  244. stream_selection = av_mallocz(mms->stream_num * 19 + 1);
  245. if (!stream_selection)
  246. return AVERROR(ENOMEM);
  247. for (i = 0; i < mms->stream_num; i++) {
  248. char tmp[20];
  249. err = snprintf(tmp, sizeof(tmp), "ffff:%d:0 ", mms->streams[i].id);
  250. if (err < 0)
  251. goto fail;
  252. av_strlcat(stream_selection, tmp, mms->stream_num * 19 + 1);
  253. }
  254. // send play request
  255. err = snprintf(headers, sizeof(headers),
  256. "Accept: */*\r\n"
  257. USERAGENT
  258. "Host: %s:%d\r\n"
  259. "Pragma: no-cache,rate=1.000000,request-context=%u\r\n"
  260. "Pragma: xPlayStrm=1\r\n"
  261. CLIENTGUID
  262. "Pragma: stream-switch-count=%d\r\n"
  263. "Pragma: stream-switch-entry=%s\r\n"
  264. "Connection: Close\r\n\r\n",
  265. host, port, mmsh->request_seq++, mms->stream_num, stream_selection);
  266. av_freep(&stream_selection);
  267. if (err < 0) {
  268. av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
  269. goto fail;
  270. }
  271. av_dlog(NULL, "out_buffer is %s", headers);
  272. av_opt_set(mms->mms_hd->priv_data, "headers", headers, 0);
  273. err = ffurl_connect(mms->mms_hd, NULL);
  274. if (err) {
  275. goto fail;
  276. }
  277. err = get_http_header_data(mmsh);
  278. if (err) {
  279. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  280. goto fail;
  281. }
  282. av_dlog(NULL, "Connection successfully open\n");
  283. return 0;
  284. fail:
  285. av_freep(&stream_selection);
  286. mmsh_close(h);
  287. av_dlog(NULL, "Connection failed with error %d\n", err);
  288. return err;
  289. }
  290. static int handle_chunk_type(MMSHContext *mmsh)
  291. {
  292. MMSContext *mms = &mmsh->mms;
  293. int res, len = 0;
  294. ChunkType chunk_type;
  295. chunk_type = get_chunk_header(mmsh, &len);
  296. switch (chunk_type) {
  297. case CHUNK_TYPE_END:
  298. mmsh->chunk_seq = 0;
  299. av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
  300. return AVERROR(EIO);
  301. case CHUNK_TYPE_STREAM_CHANGE:
  302. mms->header_parsed = 0;
  303. if (res = get_http_header_data(mmsh)) {
  304. av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
  305. return res;
  306. }
  307. break;
  308. case CHUNK_TYPE_DATA:
  309. return read_data_packet(mmsh, len);
  310. default:
  311. av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
  312. return AVERROR_INVALIDDATA;
  313. }
  314. return 0;
  315. }
  316. static int mmsh_read(URLContext *h, uint8_t *buf, int size)
  317. {
  318. int res = 0;
  319. MMSHContext *mmsh = h->priv_data;
  320. MMSContext *mms = &mmsh->mms;
  321. do {
  322. if (mms->asf_header_read_size < mms->asf_header_size) {
  323. // copy asf header into buffer
  324. res = ff_mms_read_header(mms, buf, size);
  325. } else {
  326. if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
  327. return res;
  328. res = ff_mms_read_data(mms, buf, size);
  329. }
  330. } while (!res);
  331. return res;
  332. }
  333. URLProtocol ff_mmsh_protocol = {
  334. .name = "mmsh",
  335. .url_open = mmsh_open,
  336. .url_read = mmsh_read,
  337. .url_close = mmsh_close,
  338. .priv_data_size = sizeof(MMSHContext),
  339. .flags = URL_PROTOCOL_FLAG_NETWORK,
  340. };