mmsh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. int request_seq; ///< request packet sequence
  53. int chunk_seq; ///< data packet sequence
  54. } MMSHContext;
  55. static int mmsh_close(URLContext *h)
  56. {
  57. MMSHContext *mmsh = (MMSHContext *)h->priv_data;
  58. MMSContext *mms = &mmsh->mms;
  59. if (mms->mms_hd)
  60. ffurl_close(mms->mms_hd);
  61. av_free(mms->streams);
  62. av_free(mms->asf_header);
  63. av_freep(&h->priv_data);
  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;
  205. MMSContext *mms;
  206. mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
  207. if (!h->priv_data)
  208. return AVERROR(ENOMEM);
  209. mmsh->request_seq = h->is_streamed = 1;
  210. mms = &mmsh->mms;
  211. av_strlcpy(location, uri, sizeof(location));
  212. av_url_split(NULL, 0, NULL, 0,
  213. host, sizeof(host), &port, path, sizeof(path), location);
  214. if (port<0)
  215. port = 80; // default mmsh protocol port
  216. ff_url_join(httpname, sizeof(httpname), "http", NULL, host, port, "%s", path);
  217. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_RDONLY) < 0) {
  218. return AVERROR(EIO);
  219. }
  220. snprintf(headers, sizeof(headers),
  221. "Accept: */*\r\n"
  222. USERAGENT
  223. "Host: %s:%d\r\n"
  224. "Pragma: no-cache,rate=1.000000,stream-time=0,"
  225. "stream-offset=0:0,request-context=%u,max-duration=0\r\n"
  226. CLIENTGUID
  227. "Connection: Close\r\n",
  228. host, port, mmsh->request_seq++);
  229. ff_http_set_headers(mms->mms_hd, headers);
  230. err = ffurl_connect(mms->mms_hd);
  231. if (err) {
  232. goto fail;
  233. }
  234. err = get_http_header_data(mmsh);
  235. if (err) {
  236. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  237. goto fail;
  238. }
  239. // close the socket and then reopen it for sending the second play request.
  240. ffurl_close(mms->mms_hd);
  241. memset(headers, 0, sizeof(headers));
  242. if (ffurl_alloc(&mms->mms_hd, httpname, AVIO_RDONLY) < 0) {
  243. return AVERROR(EIO);
  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. "Connection: Close\r\n",
  266. host, port, mmsh->request_seq++, mms->stream_num, stream_selection);
  267. av_freep(&stream_selection);
  268. if (err < 0) {
  269. av_log(NULL, AV_LOG_ERROR, "Build play request failed!\n");
  270. goto fail;
  271. }
  272. av_dlog(NULL, "out_buffer is %s", headers);
  273. ff_http_set_headers(mms->mms_hd, headers);
  274. err = ffurl_connect(mms->mms_hd);
  275. if (err) {
  276. goto fail;
  277. }
  278. err = get_http_header_data(mmsh);
  279. if (err) {
  280. av_log(NULL, AV_LOG_ERROR, "Get http header data failed!\n");
  281. goto fail;
  282. }
  283. av_dlog(NULL, "Connection successfully open\n");
  284. return 0;
  285. fail:
  286. av_freep(&stream_selection);
  287. mmsh_close(h);
  288. av_dlog(NULL, "Connection failed with error %d\n", err);
  289. return err;
  290. }
  291. static int handle_chunk_type(MMSHContext *mmsh)
  292. {
  293. MMSContext *mms = &mmsh->mms;
  294. int res, len = 0;
  295. ChunkType chunk_type;
  296. chunk_type = get_chunk_header(mmsh, &len);
  297. switch (chunk_type) {
  298. case CHUNK_TYPE_END:
  299. mmsh->chunk_seq = 0;
  300. av_log(NULL, AV_LOG_ERROR, "Stream ended!\n");
  301. return AVERROR(EIO);
  302. case CHUNK_TYPE_STREAM_CHANGE:
  303. mms->header_parsed = 0;
  304. if (res = get_http_header_data(mmsh)) {
  305. av_log(NULL, AV_LOG_ERROR,"Stream changed! Failed to get new header!\n");
  306. return res;
  307. }
  308. break;
  309. case CHUNK_TYPE_DATA:
  310. return read_data_packet(mmsh, len);
  311. default:
  312. av_log(NULL, AV_LOG_ERROR, "Recv other type packet %d\n", chunk_type);
  313. return AVERROR_INVALIDDATA;
  314. }
  315. return 0;
  316. }
  317. static int mmsh_read(URLContext *h, uint8_t *buf, int size)
  318. {
  319. int res = 0;
  320. MMSHContext *mmsh = h->priv_data;
  321. MMSContext *mms = &mmsh->mms;
  322. do {
  323. if (mms->asf_header_read_size < mms->asf_header_size) {
  324. // copy asf header into buffer
  325. res = ff_mms_read_header(mms, buf, size);
  326. } else {
  327. if (!mms->remaining_in_len && (res = handle_chunk_type(mmsh)))
  328. return res;
  329. res = ff_mms_read_data(mms, buf, size);
  330. }
  331. } while (!res);
  332. return res;
  333. }
  334. URLProtocol ff_mmsh_protocol = {
  335. .name = "mmsh",
  336. .url_open = mmsh_open,
  337. .url_read = mmsh_read,
  338. .url_write = NULL,
  339. .url_seek = NULL,
  340. .url_close = mmsh_close,
  341. };