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.

413 lines
12KB

  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. url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
  123. tag = get_le32(pb);
  124. if (tag == MKTAG('M', 'L', 'T', 'I')) {
  125. int num, chunk_nr;
  126. /* read index of MDPR chunk numbers */
  127. num = get_be16(pb);
  128. if (rule_nr < 0 || rule_nr >= num)
  129. return -1;
  130. url_fskip(pb, rule_nr * 2);
  131. chunk_nr = get_be16(pb);
  132. url_fskip(pb, (num - 1 - rule_nr) * 2);
  133. /* read MDPR chunks */
  134. num = get_be16(pb);
  135. if (chunk_nr >= num)
  136. return -1;
  137. while (chunk_nr--)
  138. url_fskip(pb, get_be32(pb));
  139. size = get_be32(pb);
  140. } else {
  141. size = rdt->mlti_data_size;
  142. url_fseek(pb, 0, SEEK_SET);
  143. }
  144. rdt->rmctx->pb = pb;
  145. if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
  146. return -1;
  147. url_close_buf(pb);
  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 *set_id, int *seq_no, int *stream_id,
  156. int *is_keyframe, uint32_t *timestamp)
  157. {
  158. int consumed = 10;
  159. if (len > 0 && (buf[0] < 0x40 || buf[0] > 0x42)) {
  160. buf += 9;
  161. len -= 9;
  162. consumed += 9;
  163. }
  164. if (len < 10)
  165. return -1;
  166. /**
  167. * Layout of the header (in bits):
  168. * 1: len_included
  169. * Flag indicating whether this header includes a length field;
  170. * this can be used to concatenate multiple RDT packets in a
  171. * single UDP/TCP data frame and is used to precede RDT data
  172. * by stream status packets
  173. * 1: need_reliable
  174. * Flag indicating whether this header includes a "reliable
  175. * sequence number"; these are apparently sequence numbers of
  176. * data packets alone. For data packets, this flag is always
  177. * set, according to the Real documentation [1]
  178. * 5: set_id
  179. * ID of a set of streams of identical content, possibly with
  180. * different codecs or bitrates
  181. * 1: is_reliable
  182. * Flag set for certain streams deemed less tolerable for packet
  183. * loss
  184. * 16: seq_no
  185. * Packet sequence number; if >=0xFF00, this is a non-data packet
  186. * containing stream status info, the second byte indicates the
  187. * type of status packet (see wireshark docs / source code [2])
  188. * if (len_included) {
  189. * 16: packet_len
  190. * } else {
  191. * packet_len = remainder of UDP/TCP frame
  192. * }
  193. * 1: is_back_to_back
  194. * Back-to-Back flag; used for timing, set for one in every 10
  195. * packets, according to the Real documentation [1]
  196. * 1: is_slow_data
  197. * Slow-data flag; currently unused, according to Real docs [1]
  198. * 5: stream_id
  199. * ID of the stream within this particular set of streams
  200. * 1: is_no_keyframe
  201. * Non-keyframe flag (unset if packet belongs to a keyframe)
  202. * 32: timestamp (PTS)
  203. * if (set_id == 0x1F) {
  204. * 16: set_id (extended set-of-streams ID; see set_id)
  205. * }
  206. * if (need_reliable) {
  207. * 16: reliable_seq_no
  208. * Reliable sequence number (see need_reliable)
  209. * }
  210. * if (stream_id == 0x3F) {
  211. * 16: stream_id (extended stream ID; see stream_id)
  212. * }
  213. * [1] https://protocol.helixcommunity.org/files/2005/devdocs/RDT_Feature_Level_20.txt
  214. * [2] http://www.wireshark.org/docs/dfref/r/rdt.html and
  215. * http://anonsvn.wireshark.org/viewvc/trunk/epan/dissectors/packet-rdt.c
  216. */
  217. if (set_id) *set_id = (buf[0]>>1) & 0x1f;
  218. if (seq_no) *seq_no = AV_RB16(buf+1);
  219. if (timestamp) *timestamp = AV_RB32(buf+4);
  220. if (stream_id) *stream_id = (buf[3]>>1) & 0x1f;
  221. if (is_keyframe) *is_keyframe = !(buf[3] & 0x1);
  222. return consumed;
  223. }
  224. /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
  225. static int
  226. rdt_parse_packet (PayloadContext *rdt, AVStream *st,
  227. AVPacket *pkt, uint32_t *timestamp,
  228. const uint8_t *buf, int len, int flags)
  229. {
  230. int seq = 1, res;
  231. ByteIOContext *pb = rdt->rmctx->pb;
  232. RMContext *rm = rdt->rmctx->priv_data;
  233. if (rm->audio_pkt_cnt == 0) {
  234. int pos;
  235. url_open_buf (&pb, buf, len, URL_RDONLY);
  236. flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
  237. rdt->rmctx->pb = pb;
  238. res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
  239. &seq, &flags, timestamp);
  240. pos = url_ftell(pb);
  241. url_close_buf (pb);
  242. if (res < 0)
  243. return res;
  244. if (rm->audio_pkt_cnt > 0 &&
  245. st->codec->codec_id == CODEC_ID_AAC) {
  246. memcpy (rdt->buffer, buf + pos, len - pos);
  247. url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
  248. rdt->rmctx->pb = pb;
  249. }
  250. } else {
  251. ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
  252. if (rm->audio_pkt_cnt == 0 &&
  253. st->codec->codec_id == CODEC_ID_AAC)
  254. url_close_buf (pb);
  255. }
  256. pkt->stream_index = st->index;
  257. pkt->pts = *timestamp;
  258. return rm->audio_pkt_cnt > 0;
  259. }
  260. int
  261. ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
  262. const uint8_t *buf, int len)
  263. {
  264. int seq_no, flags = 0, stream_id, set_id, is_keyframe;
  265. uint32_t timestamp;
  266. int rv= 0;
  267. if (!s->parse_packet)
  268. return -1;
  269. if (!buf) {
  270. /* return the next packets, if any */
  271. timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
  272. rv= s->parse_packet(s->dynamic_protocol_context,
  273. s->st, pkt, &timestamp, NULL, 0, flags);
  274. return rv;
  275. }
  276. if (len < 12)
  277. return -1;
  278. rv = ff_rdt_parse_header(buf, len, &set_id, &seq_no, &stream_id, &is_keyframe, &timestamp);
  279. if (rv < 0)
  280. return rv;
  281. if (is_keyframe && (set_id != s->prev_set_id || timestamp != s->prev_timestamp)) {
  282. flags |= PKT_FLAG_KEY;
  283. s->prev_set_id = set_id;
  284. s->prev_timestamp = timestamp;
  285. }
  286. buf += rv;
  287. len -= rv;
  288. rv = s->parse_packet(s->dynamic_protocol_context,
  289. s->st, pkt, &timestamp, buf, len, flags);
  290. return rv;
  291. }
  292. void
  293. ff_rdt_subscribe_rule (char *cmd, int size,
  294. int stream_nr, int rule_nr)
  295. {
  296. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  297. stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
  298. }
  299. void
  300. ff_rdt_subscribe_rule2 (RDTDemuxContext *s, char *cmd, int size,
  301. int stream_nr, int rule_nr)
  302. {
  303. PayloadContext *rdt = s->dynamic_protocol_context;
  304. rdt_load_mdpr(rdt, s->st, rule_nr * 2);
  305. }
  306. static unsigned char *
  307. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  308. {
  309. unsigned char *target;
  310. int len = strlen(p);
  311. if (*p == '\"') {
  312. p++;
  313. len -= 2; /* skip embracing " at start/end */
  314. }
  315. *target_len = len * 3 / 4;
  316. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  317. av_base64_decode(target, p, *target_len);
  318. return target;
  319. }
  320. static int
  321. rdt_parse_sdp_line (AVStream *stream, PayloadContext *rdt, const char *line)
  322. {
  323. const char *p = line;
  324. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  325. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  326. } else if (av_strstart(p, "StartTime:integer;", &p))
  327. stream->first_dts = atoi(p);
  328. return 0;
  329. }
  330. static PayloadContext *
  331. rdt_new_extradata (void)
  332. {
  333. PayloadContext *rdt = av_mallocz(sizeof(PayloadContext));
  334. av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
  335. return rdt;
  336. }
  337. static void
  338. rdt_free_extradata (PayloadContext *rdt)
  339. {
  340. if (rdt->rmctx)
  341. av_close_input_stream(rdt->rmctx);
  342. av_freep(&rdt->mlti_data);
  343. av_free(rdt);
  344. }
  345. #define RDT_HANDLER(n, s, t) \
  346. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  347. s, \
  348. t, \
  349. CODEC_ID_NONE, \
  350. rdt_parse_sdp_line, \
  351. rdt_new_extradata, \
  352. rdt_free_extradata, \
  353. rdt_parse_packet \
  354. };
  355. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
  356. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
  357. RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
  358. RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
  359. void av_register_rdt_dynamic_payload_handlers(void)
  360. {
  361. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  362. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  363. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  364. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  365. }