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.

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