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 "libavformat/internal.h"
  30. #include "mms.h"
  31. #include "asf.h"
  32. #include "http.h"
  33. #define CHUNK_HEADER_LENGTH 4 // 2bytes chunk type and 2bytes chunk length.
  34. #define EXT_HEADER_LENGTH 8 // 4bytes sequence, 2bytes useless and 2bytes chunk length.
  35. // see Ref 2.2.1.8
  36. #define USERAGENT "User-Agent: NSPlayer/4.1.0.3856\r\n"
  37. // see Ref 2.2.1.4.33
  38. // the guid value can be changed to any valid value.
  39. #define CLIENTGUID "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}\r\n"
  40. // see Ref 2.2.3 for packet type define:
  41. // chunk type contains 2 fields: Frame and PacketID.
  42. // Frame is 0x24 or 0xA4(rarely), different PacketID indicates different packet type.
  43. typedef enum {
  44. CHUNK_TYPE_DATA = 0x4424,
  45. CHUNK_TYPE_ASF_HEADER = 0x4824,
  46. CHUNK_TYPE_END = 0x4524,
  47. CHUNK_TYPE_STREAM_CHANGE = 0x4324,
  48. } ChunkType;
  49. typedef struct {
  50. MMSContext mms;
  51. int request_seq; ///< request packet sequence
  52. int chunk_seq; ///< data packet sequence
  53. } MMSHContext;
  54. static int mmsh_close(URLContext *h)
  55. {
  56. MMSHContext *mmsh = (MMSHContext *)h->priv_data;
  57. MMSContext *mms = &mmsh->mms;
  58. if (mms->mms_hd)
  59. url_close(mms->mms_hd);
  60. av_free(mms->streams);
  61. av_free(mms->asf_header);
  62. av_freep(&h->priv_data);
  63. return 0;
  64. }
  65. static ChunkType get_chunk_header(MMSHContext *mmsh, int *len)
  66. {
  67. MMSContext *mms = &mmsh->mms;
  68. uint8_t chunk_header[CHUNK_HEADER_LENGTH];
  69. uint8_t ext_header[EXT_HEADER_LENGTH];
  70. ChunkType chunk_type;
  71. int chunk_len, res, ext_header_len;
  72. res = url_read_complete(mms->mms_hd, chunk_header, CHUNK_HEADER_LENGTH);
  73. if (res != CHUNK_HEADER_LENGTH) {
  74. av_log(NULL, AV_LOG_ERROR, "Read data packet header failed!\n");
  75. return AVERROR(EIO);
  76. }
  77. chunk_type = AV_RL16(chunk_header);
  78. chunk_len = AV_RL16(chunk_header + 2);
  79. switch (chunk_type) {
  80. case CHUNK_TYPE_END:
  81. case CHUNK_TYPE_STREAM_CHANGE:
  82. ext_header_len = 4;
  83. break;
  84. case CHUNK_TYPE_ASF_HEADER:
  85. case CHUNK_TYPE_DATA:
  86. ext_header_len = 8;
  87. break;
  88. default:
  89. av_log(NULL, AV_LOG_ERROR, "Strange chunk type %d\n", chunk_type);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. res = url_read_complete(mms->mms_hd, ext_header, ext_header_len);
  93. if (res != ext_header_len) {
  94. av_log(NULL, AV_LOG_ERROR, "Read ext header failed!\n");
  95. return AVERROR(EIO);
  96. }
  97. *len = chunk_len - ext_header_len;
  98. if (chunk_type == CHUNK_TYPE_END || chunk_type == CHUNK_TYPE_DATA)
  99. mmsh->chunk_seq = AV_RL32(ext_header);
  100. return chunk_type;
  101. }
  102. static int read_data_packet(MMSHContext *mmsh, const int len)
  103. {
  104. MMSContext *mms = &mmsh->mms;
  105. int res;
  106. if (len > sizeof(mms->in_buffer)) {
  107. av_log(NULL, AV_LOG_ERROR,
  108. "Data packet length %d exceeds the in_buffer size %zu\n",
  109. len, sizeof(mms->in_buffer));
  110. return AVERROR(EIO);
  111. }
  112. res = url_read_complete(mms->mms_hd, mms->in_buffer, len);
  113. dprintf(NULL, "Data packet len = %d\n", len);
  114. if (res != len) {
  115. av_log(NULL, AV_LOG_ERROR, "Read data packet failed!\n");
  116. return AVERROR(EIO);
  117. }
  118. if (len > mms->asf_packet_len) {
  119. av_log(NULL, AV_LOG_ERROR,
  120. "Chunk length %d exceed packet length %d\n",len, mms->asf_packet_len);
  121. return AVERROR_INVALIDDATA;
  122. } else {
  123. memset(mms->in_buffer + len, 0, mms->asf_packet_len - len); // padding
  124. }
  125. mms->read_in_ptr = mms->in_buffer;
  126. mms->remaining_in_len = mms->asf_packet_len;
  127. return 0;
  128. }
  129. static int get_http_header_data(MMSHContext *mmsh)
  130. {
  131. MMSContext *mms = &mmsh->mms;
  132. int res, len;
  133. ChunkType chunk_type;
  134. for (;;) {
  135. len = 0;
  136. res = chunk_type = get_chunk_header(mmsh, &len);
  137. if (res < 0) {
  138. return res;
  139. } else if (chunk_type == CHUNK_TYPE_ASF_HEADER){
  140. // get asf header and stored it
  141. if (!mms->header_parsed) {
  142. if (mms->asf_header) {
  143. if (len != mms->asf_header_size) {
  144. mms->asf_header_size = len;
  145. dprintf(NULL, "Header len changed from %d to %d\n",
  146. mms->asf_header_size, len);
  147. av_freep(&mms->asf_header);
  148. }
  149. }
  150. mms->asf_header = av_mallocz(len);
  151. if (!mms->asf_header) {
  152. return AVERROR(ENOMEM);
  153. }
  154. mms->asf_header_size = len;
  155. }
  156. if (len > mms->asf_header_size) {
  157. av_log(NULL, AV_LOG_ERROR,
  158. "Asf header packet len = %d exceed the asf header buf size %d\n",
  159. len, mms->asf_header_size);
  160. return AVERROR(EIO);
  161. }
  162. res = url_read_complete(mms->mms_hd, mms->asf_header, len);
  163. if (res != len) {
  164. av_log(NULL, AV_LOG_ERROR,
  165. "Recv asf header data len %d != expected len %d\n", res, len);
  166. return AVERROR(EIO);
  167. }
  168. mms->asf_header_size = len;
  169. if (!mms->header_parsed) {
  170. res = ff_mms_asf_header_parser(mms);
  171. mms->header_parsed = 1;
  172. return res;
  173. }
  174. } else if (chunk_type == CHUNK_TYPE_DATA) {
  175. // read data packet and do padding
  176. return read_data_packet(mmsh, len);
  177. } else {
  178. if (len) {
  179. if (len > sizeof(mms->in_buffer)) {
  180. av_log(NULL, AV_LOG_ERROR,
  181. "Other packet len = %d exceed the in_buffer size %zu\n",
  182. len, sizeof(mms->in_buffer));
  183. return AVERROR(EIO);
  184. }
  185. res = url_read_complete(mms->mms_hd, mms->in_buffer, len);
  186. if (res != len) {
  187. av_log(NULL, AV_LOG_ERROR, "Read other chunk type data failed!\n");
  188. return AVERROR(EIO);
  189. } else {
  190. dprintf(NULL, "Skip chunk type %d \n", chunk_type);
  191. continue;
  192. }
  193. }
  194. }
  195. }
  196. return 0;
  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, path);
  217. if (url_alloc(&mms->mms_hd, httpname, URL_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\r\n",
  228. host, port, mmsh->request_seq++);
  229. ff_http_set_headers(mms->mms_hd, headers);
  230. err = url_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. url_close(mms->mms_hd);
  241. memset(headers, 0, sizeof(headers));
  242. if (url_alloc(&mms->mms_hd, httpname, URL_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\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. dprintf(NULL, "out_buffer is %s", headers);
  273. ff_http_set_headers(mms->mms_hd, headers);
  274. err = url_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. dprintf(NULL, "Connection successfully open\n");
  284. return 0;
  285. fail:
  286. av_freep(&stream_selection);
  287. mmsh_close(h);
  288. dprintf(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. };