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.

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