You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
13KB

  1. /*
  2. * Realmedia RTSP protocol (RDT) support.
  3. * Copyright (c) 2007 Ronald S. Bultje
  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. * @file rdt.c
  23. * @brief Realmedia RTSP protocol (RDT) support
  24. * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  25. */
  26. #include "avformat.h"
  27. #include "libavutil/avstring.h"
  28. #include "rtp_internal.h"
  29. #include "rdt.h"
  30. #include "libavutil/base64.h"
  31. #include "libavutil/md5.h"
  32. #include "rm.h"
  33. #include "internal.h"
  34. #include <libavcodec/bitstream.h>
  35. struct RDTDemuxContext {
  36. AVFormatContext *ic;
  37. AVStream *st;
  38. void *dynamic_protocol_context;
  39. DynamicPayloadPacketHandlerProc parse_packet;
  40. uint32_t prev_timestamp;
  41. int prev_set_id;
  42. };
  43. RDTDemuxContext *
  44. ff_rdt_parse_open(AVFormatContext *ic, AVStream *st,
  45. void *priv_data, RTPDynamicProtocolHandler *handler)
  46. {
  47. RDTDemuxContext *s = av_mallocz(sizeof(RDTDemuxContext));
  48. if (!s)
  49. return NULL;
  50. s->ic = ic;
  51. s->st = st;
  52. s->prev_set_id = -1;
  53. s->prev_timestamp = -1;
  54. s->parse_packet = handler->parse_packet;
  55. s->dynamic_protocol_context = priv_data;
  56. return s;
  57. }
  58. void
  59. ff_rdt_parse_close(RDTDemuxContext *s)
  60. {
  61. av_free(s);
  62. }
  63. struct PayloadContext {
  64. AVFormatContext *rmctx;
  65. uint8_t *mlti_data;
  66. unsigned int mlti_data_size;
  67. char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
  68. };
  69. void
  70. ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
  71. const char *challenge)
  72. {
  73. int ch_len = strlen (challenge), i;
  74. unsigned char zres[16],
  75. buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
  76. #define XOR_TABLE_SIZE 37
  77. const unsigned char xor_table[XOR_TABLE_SIZE] = {
  78. 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
  79. 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
  80. 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
  81. 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
  82. 0x10, 0x57, 0x05, 0x18, 0x54 };
  83. /* some (length) checks */
  84. if (ch_len == 40) /* what a hack... */
  85. ch_len = 32;
  86. else if (ch_len > 56)
  87. ch_len = 56;
  88. memcpy(buf + 8, challenge, ch_len);
  89. /* xor challenge bytewise with xor_table */
  90. for (i = 0; i < XOR_TABLE_SIZE; i++)
  91. buf[8 + i] ^= xor_table[i];
  92. av_md5_sum(zres, buf, 64);
  93. ff_data_to_hex(response, zres, 16);
  94. for (i=0;i<32;i++) response[i] = tolower(response[i]);
  95. /* add tail */
  96. strcpy (response + 32, "01d0a8e3");
  97. /* calculate checksum */
  98. for (i = 0; i < 8; i++)
  99. chksum[i] = response[i * 4];
  100. chksum[8] = 0;
  101. }
  102. static int
  103. rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
  104. {
  105. ByteIOContext pb;
  106. int size;
  107. uint32_t tag;
  108. /**
  109. * Layout of the MLTI chunk:
  110. * 4:MLTI
  111. * 2:<number of streams>
  112. * Then for each stream ([number_of_streams] times):
  113. * 2:<mdpr index>
  114. * 2:<number of mdpr chunks>
  115. * Then for each mdpr chunk ([number_of_mdpr_chunks] times):
  116. * 4:<size>
  117. * [size]:<data>
  118. * we skip MDPR chunks until we reach the one of the stream
  119. * we're interested in, and forward that ([size]+[data]) to
  120. * the RM demuxer to parse the stream-specific header data.
  121. */
  122. if (!rdt->mlti_data)
  123. return -1;
  124. init_put_byte(&pb, rdt->mlti_data, rdt->mlti_data_size, 0,
  125. NULL, NULL, NULL, NULL);
  126. tag = get_le32(&pb);
  127. if (tag == MKTAG('M', 'L', 'T', 'I')) {
  128. int num, chunk_nr;
  129. /* read index of MDPR chunk numbers */
  130. num = get_be16(&pb);
  131. if (rule_nr < 0 || rule_nr >= num)
  132. return -1;
  133. url_fskip(&pb, rule_nr * 2);
  134. chunk_nr = get_be16(&pb);
  135. url_fskip(&pb, (num - 1 - rule_nr) * 2);
  136. /* read MDPR chunks */
  137. num = get_be16(&pb);
  138. if (chunk_nr >= num)
  139. return -1;
  140. while (chunk_nr--)
  141. url_fskip(&pb, get_be32(&pb));
  142. size = get_be32(&pb);
  143. } else {
  144. size = rdt->mlti_data_size;
  145. url_fseek(&pb, 0, SEEK_SET);
  146. }
  147. if (ff_rm_read_mdpr_codecdata(rdt->rmctx, &pb, st, size) < 0)
  148. return -1;
  149. return 0;
  150. }
  151. /**
  152. * Actual data handling.
  153. */
  154. int
  155. ff_rdt_parse_header(const uint8_t *buf, int len,
  156. int *pset_id, int *pseq_no, int *pstream_id,
  157. int *pis_keyframe, uint32_t *ptimestamp)
  158. {
  159. GetBitContext gb;
  160. int consumed = 0, set_id, seq_no, stream_id, is_keyframe,
  161. len_included, need_reliable;
  162. uint32_t timestamp;
  163. /* skip status packets */
  164. while (len >= 5 && buf[1] == 0xFF /* status packet */) {
  165. int pkt_len;
  166. if (!(buf[0] & 0x80))
  167. return -1; /* not followed by a data packet */
  168. pkt_len = AV_RB16(buf+3);
  169. buf += pkt_len;
  170. len -= pkt_len;
  171. consumed += pkt_len;
  172. }
  173. if (len < 16)
  174. return -1;
  175. /**
  176. * Layout of the header (in bits):
  177. * 1: len_included
  178. * Flag indicating whether this header includes a length field;
  179. * this can be used to concatenate multiple RDT packets in a
  180. * single UDP/TCP data frame and is used to precede RDT data
  181. * by stream status packets
  182. * 1: need_reliable
  183. * Flag indicating whether this header includes a "reliable
  184. * sequence number"; these are apparently sequence numbers of
  185. * data packets alone. For data packets, this flag is always
  186. * set, according to the Real documentation [1]
  187. * 5: set_id
  188. * ID of a set of streams of identical content, possibly with
  189. * different codecs or bitrates
  190. * 1: is_reliable
  191. * Flag set for certain streams deemed less tolerable for packet
  192. * loss
  193. * 16: seq_no
  194. * Packet sequence number; if >=0xFF00, this is a non-data packet
  195. * containing stream status info, the second byte indicates the
  196. * type of status packet (see wireshark docs / source code [2])
  197. * if (len_included) {
  198. * 16: packet_len
  199. * } else {
  200. * packet_len = remainder of UDP/TCP frame
  201. * }
  202. * 1: is_back_to_back
  203. * Back-to-Back flag; used for timing, set for one in every 10
  204. * packets, according to the Real documentation [1]
  205. * 1: is_slow_data
  206. * Slow-data flag; currently unused, according to Real docs [1]
  207. * 5: stream_id
  208. * ID of the stream within this particular set of streams
  209. * 1: is_no_keyframe
  210. * Non-keyframe flag (unset if packet belongs to a keyframe)
  211. * 32: timestamp (PTS)
  212. * if (set_id == 0x1F) {
  213. * 16: set_id (extended set-of-streams ID; see set_id)
  214. * }
  215. * if (need_reliable) {
  216. * 16: reliable_seq_no
  217. * Reliable sequence number (see need_reliable)
  218. * }
  219. * if (stream_id == 0x3F) {
  220. * 16: stream_id (extended stream ID; see stream_id)
  221. * }
  222. * [1] https://protocol.helixcommunity.org/files/2005/devdocs/RDT_Feature_Level_20.txt
  223. * [2] http://www.wireshark.org/docs/dfref/r/rdt.html and
  224. * http://anonsvn.wireshark.org/viewvc/trunk/epan/dissectors/packet-rdt.c
  225. */
  226. init_get_bits(&gb, buf, len << 3);
  227. len_included = get_bits1(&gb);
  228. need_reliable = get_bits1(&gb);
  229. set_id = get_bits(&gb, 5);
  230. skip_bits(&gb, 1);
  231. seq_no = get_bits(&gb, 16);
  232. if (len_included)
  233. skip_bits(&gb, 16);
  234. skip_bits(&gb, 2);
  235. stream_id = get_bits(&gb, 5);
  236. is_keyframe = !get_bits1(&gb);
  237. timestamp = get_bits_long(&gb, 32);
  238. if (set_id == 0x1f)
  239. set_id = get_bits(&gb, 16);
  240. if (need_reliable)
  241. skip_bits(&gb, 16);
  242. if (stream_id == 0x1f)
  243. stream_id = get_bits(&gb, 16);
  244. if (pset_id) *pset_id = set_id;
  245. if (pseq_no) *pseq_no = seq_no;
  246. if (pstream_id) *pstream_id = stream_id;
  247. if (pis_keyframe) *pis_keyframe = is_keyframe;
  248. if (ptimestamp) *ptimestamp = timestamp;
  249. return consumed + (get_bits_count(&gb) >> 3);
  250. }
  251. /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
  252. static int
  253. rdt_parse_packet (PayloadContext *rdt, AVStream *st,
  254. AVPacket *pkt, uint32_t *timestamp,
  255. const uint8_t *buf, int len, int flags)
  256. {
  257. int seq = 1, res;
  258. ByteIOContext pb;
  259. RMContext *rm = rdt->rmctx->priv_data;
  260. if (rm->audio_pkt_cnt == 0) {
  261. int pos;
  262. init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
  263. flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
  264. res = ff_rm_parse_packet (rdt->rmctx, &pb, st, len, pkt,
  265. &seq, &flags, timestamp);
  266. pos = url_ftell(&pb);
  267. if (res < 0)
  268. return res;
  269. if (rm->audio_pkt_cnt > 0 &&
  270. st->codec->codec_id == CODEC_ID_AAC) {
  271. memcpy (rdt->buffer, buf + pos, len - pos);
  272. rdt->rmctx->pb = av_alloc_put_byte (rdt->buffer, len - pos, 0,
  273. NULL, NULL, NULL, NULL);
  274. }
  275. } else {
  276. ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb, st, pkt);
  277. if (rm->audio_pkt_cnt == 0 &&
  278. st->codec->codec_id == CODEC_ID_AAC)
  279. av_freep(&rdt->rmctx->pb);
  280. }
  281. pkt->stream_index = st->index;
  282. pkt->pts = *timestamp;
  283. return rm->audio_pkt_cnt > 0;
  284. }
  285. int
  286. ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
  287. const uint8_t *buf, int len)
  288. {
  289. int seq_no, flags = 0, stream_id, set_id, is_keyframe;
  290. uint32_t timestamp;
  291. int rv= 0;
  292. if (!s->parse_packet)
  293. return -1;
  294. if (!buf) {
  295. /* return the next packets, if any */
  296. timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
  297. rv= s->parse_packet(s->dynamic_protocol_context,
  298. s->st, pkt, &timestamp, NULL, 0, flags);
  299. return rv;
  300. }
  301. if (len < 12)
  302. return -1;
  303. rv = ff_rdt_parse_header(buf, len, &set_id, &seq_no, &stream_id, &is_keyframe, &timestamp);
  304. if (rv < 0)
  305. return rv;
  306. if (is_keyframe && (set_id != s->prev_set_id || timestamp != s->prev_timestamp)) {
  307. flags |= PKT_FLAG_KEY;
  308. s->prev_set_id = set_id;
  309. s->prev_timestamp = timestamp;
  310. }
  311. buf += rv;
  312. len -= rv;
  313. rv = s->parse_packet(s->dynamic_protocol_context,
  314. s->st, pkt, &timestamp, buf, len, flags);
  315. return rv;
  316. }
  317. void
  318. ff_rdt_subscribe_rule (char *cmd, int size,
  319. int stream_nr, int rule_nr)
  320. {
  321. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  322. stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
  323. }
  324. void
  325. ff_rdt_subscribe_rule2 (RDTDemuxContext *s, char *cmd, int size,
  326. int stream_nr, int rule_nr)
  327. {
  328. PayloadContext *rdt = s->dynamic_protocol_context;
  329. rdt_load_mdpr(rdt, s->st, rule_nr * 2);
  330. }
  331. static unsigned char *
  332. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  333. {
  334. unsigned char *target;
  335. int len = strlen(p);
  336. if (*p == '\"') {
  337. p++;
  338. len -= 2; /* skip embracing " at start/end */
  339. }
  340. *target_len = len * 3 / 4;
  341. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  342. av_base64_decode(target, p, *target_len);
  343. return target;
  344. }
  345. static int
  346. rdt_parse_sdp_line (AVStream *stream, PayloadContext *rdt, const char *line)
  347. {
  348. const char *p = line;
  349. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  350. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  351. } else if (av_strstart(p, "StartTime:integer;", &p))
  352. stream->first_dts = atoi(p);
  353. return 0;
  354. }
  355. static PayloadContext *
  356. rdt_new_extradata (void)
  357. {
  358. PayloadContext *rdt = av_mallocz(sizeof(PayloadContext));
  359. av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
  360. return rdt;
  361. }
  362. static void
  363. rdt_free_extradata (PayloadContext *rdt)
  364. {
  365. if (rdt->rmctx)
  366. av_close_input_stream(rdt->rmctx);
  367. av_freep(&rdt->mlti_data);
  368. av_free(rdt);
  369. }
  370. #define RDT_HANDLER(n, s, t) \
  371. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  372. s, \
  373. t, \
  374. CODEC_ID_NONE, \
  375. rdt_parse_sdp_line, \
  376. rdt_new_extradata, \
  377. rdt_free_extradata, \
  378. rdt_parse_packet \
  379. };
  380. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
  381. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
  382. RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
  383. RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
  384. void av_register_rdt_dynamic_payload_handlers(void)
  385. {
  386. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  387. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  388. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  389. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  390. }