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.

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