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.

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