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.

335 lines
9.3KB

  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. typedef struct rdt_data {
  35. AVFormatContext *rmctx;
  36. uint8_t *mlti_data;
  37. unsigned int mlti_data_size;
  38. uint32_t prev_sn, prev_ts;
  39. char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
  40. } rdt_data;
  41. void
  42. ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
  43. const char *challenge)
  44. {
  45. int ch_len = strlen (challenge), i;
  46. unsigned char zres[16],
  47. buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
  48. #define XOR_TABLE_SIZE 37
  49. const unsigned char xor_table[XOR_TABLE_SIZE] = {
  50. 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
  51. 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
  52. 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
  53. 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
  54. 0x10, 0x57, 0x05, 0x18, 0x54 };
  55. /* some (length) checks */
  56. if (ch_len == 40) /* what a hack... */
  57. ch_len = 32;
  58. else if (ch_len > 56)
  59. ch_len = 56;
  60. memcpy(buf + 8, challenge, ch_len);
  61. /* xor challenge bytewise with xor_table */
  62. for (i = 0; i < XOR_TABLE_SIZE; i++)
  63. buf[8 + i] ^= xor_table[i];
  64. av_md5_sum(zres, buf, 64);
  65. ff_data_to_hex(response, zres, 16);
  66. for (i=0;i<32;i++) response[i] = tolower(response[i]);
  67. /* add tail */
  68. strcpy (response + 32, "01d0a8e3");
  69. /* calculate checksum */
  70. for (i = 0; i < 8; i++)
  71. chksum[i] = response[i * 4];
  72. chksum[8] = 0;
  73. }
  74. static int
  75. rdt_load_mdpr (rdt_data *rdt, AVStream *st, int rule_nr)
  76. {
  77. ByteIOContext *pb;
  78. int size;
  79. uint32_t tag;
  80. /**
  81. * Layout of the MLTI chunk:
  82. * 4:MLTI
  83. * 2:<number of streams>
  84. * Then for each stream ([number_of_streams] times):
  85. * 2:<mdpr index>
  86. * 2:<number of mdpr chunks>
  87. * Then for each mdpr chunk ([number_of_mdpr_chunks] times):
  88. * 4:<size>
  89. * [size]:<data>
  90. * we skip MDPR chunks until we reach the one of the stream
  91. * we're interested in, and forward that ([size]+[data]) to
  92. * the RM demuxer to parse the stream-specific header data.
  93. */
  94. if (!rdt->mlti_data)
  95. return -1;
  96. url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
  97. tag = get_le32(pb);
  98. if (tag == MKTAG('M', 'L', 'T', 'I')) {
  99. int num, chunk_nr;
  100. /* read index of MDPR chunk numbers */
  101. num = get_be16(pb);
  102. if (rule_nr < 0 || rule_nr >= num)
  103. return -1;
  104. url_fskip(pb, rule_nr * 2);
  105. chunk_nr = get_be16(pb);
  106. url_fskip(pb, (num - 1 - rule_nr) * 2);
  107. /* read MDPR chunks */
  108. num = get_be16(pb);
  109. if (chunk_nr >= num)
  110. return -1;
  111. while (chunk_nr--)
  112. url_fskip(pb, get_be32(pb));
  113. size = get_be32(pb);
  114. } else {
  115. size = rdt->mlti_data_size;
  116. url_fseek(pb, 0, SEEK_SET);
  117. }
  118. rdt->rmctx->pb = pb;
  119. if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
  120. return -1;
  121. url_close_buf(pb);
  122. return 0;
  123. }
  124. /**
  125. * Actual data handling.
  126. */
  127. int
  128. ff_rdt_parse_header(const uint8_t *buf, int len,
  129. int *sn, int *seq, int *rn, uint32_t *ts)
  130. {
  131. int consumed = 10;
  132. if (len > 0 && (buf[0] < 0x40 || buf[0] > 0x42)) {
  133. buf += 9;
  134. len -= 9;
  135. consumed += 9;
  136. }
  137. if (len < 10)
  138. return -1;
  139. if (sn) *sn = (buf[0]>>1) & 0x1f;
  140. if (seq) *seq = AV_RB16(buf+1);
  141. if (ts) *ts = AV_RB32(buf+4);
  142. if (rn) *rn = buf[3] & 0x3f;
  143. return consumed;
  144. }
  145. /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
  146. static int
  147. rdt_parse_packet (RTPDemuxContext *s, AVPacket *pkt, uint32_t *timestamp,
  148. const uint8_t *buf, int len, int flags)
  149. {
  150. rdt_data *rdt = s->dynamic_protocol_context;
  151. int seq = 1, res;
  152. ByteIOContext *pb = rdt->rmctx->pb;
  153. RMContext *rm = rdt->rmctx->priv_data;
  154. AVStream *st = s->st;
  155. if (rm->audio_pkt_cnt == 0) {
  156. int pos;
  157. url_open_buf (&pb, buf, len, URL_RDONLY);
  158. flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
  159. rdt->rmctx->pb = pb;
  160. res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
  161. &seq, &flags, timestamp);
  162. pos = url_ftell(pb);
  163. url_close_buf (pb);
  164. if (res < 0)
  165. return res;
  166. if (rm->audio_pkt_cnt > 0 &&
  167. st->codec->codec_id == CODEC_ID_AAC) {
  168. memcpy (rdt->buffer, buf + pos, len - pos);
  169. url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
  170. rdt->rmctx->pb = pb;
  171. }
  172. } else {
  173. ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
  174. if (rm->audio_pkt_cnt == 0 &&
  175. st->codec->codec_id == CODEC_ID_AAC)
  176. url_close_buf (pb);
  177. }
  178. pkt->stream_index = st->index;
  179. pkt->pts = *timestamp;
  180. return rm->audio_pkt_cnt > 0;
  181. }
  182. int
  183. ff_rdt_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  184. const uint8_t *buf, int len)
  185. {
  186. rdt_data *rdt = s->dynamic_protocol_context;
  187. int seq, flags = 0, rule, sn;
  188. uint32_t timestamp;
  189. int rv= 0;
  190. if (!s->parse_packet)
  191. return -1;
  192. if (!buf) {
  193. /* return the next packets, if any */
  194. timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
  195. rv= s->parse_packet(s, pkt, &timestamp, NULL, 0, flags);
  196. return rv;
  197. }
  198. if (len < 12)
  199. return -1;
  200. rv = ff_rdt_parse_header(buf, len, &sn, &seq, &rule, &timestamp);
  201. if (rv < 0)
  202. return rv;
  203. if (!(rule & 1) && (sn != rdt->prev_sn || timestamp != rdt->prev_ts)) {
  204. flags |= PKT_FLAG_KEY;
  205. rdt->prev_sn = sn;
  206. rdt->prev_ts = timestamp;
  207. }
  208. buf += rv;
  209. len -= rv;
  210. s->seq = seq;
  211. rv = s->parse_packet(s, pkt, &timestamp, buf, len, flags);
  212. return rv;
  213. }
  214. void
  215. ff_rdt_subscribe_rule (char *cmd, int size,
  216. int stream_nr, int rule_nr)
  217. {
  218. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  219. stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
  220. }
  221. void
  222. ff_rdt_subscribe_rule2 (RTPDemuxContext *s, char *cmd, int size,
  223. int stream_nr, int rule_nr)
  224. {
  225. rdt_data *rdt = s->dynamic_protocol_context;
  226. rdt_load_mdpr(rdt, s->st, rule_nr * 2);
  227. }
  228. static unsigned char *
  229. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  230. {
  231. unsigned char *target;
  232. int len = strlen(p);
  233. if (*p == '\"') {
  234. p++;
  235. len -= 2; /* skip embracing " at start/end */
  236. }
  237. *target_len = len * 3 / 4;
  238. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  239. av_base64_decode(target, p, *target_len);
  240. return target;
  241. }
  242. static int
  243. rdt_parse_sdp_line (AVStream *stream, void *d, const char *line)
  244. {
  245. rdt_data *rdt = d;
  246. const char *p = line;
  247. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  248. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  249. } else if (av_strstart(p, "StartTime:integer;", &p))
  250. stream->first_dts = atoi(p);
  251. return 0;
  252. }
  253. static void *
  254. rdt_new_extradata (void)
  255. {
  256. rdt_data *rdt = av_mallocz(sizeof(rdt_data));
  257. av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
  258. rdt->prev_ts = -1;
  259. rdt->prev_sn = -1;
  260. return rdt;
  261. }
  262. static void
  263. rdt_free_extradata (void *d)
  264. {
  265. rdt_data *rdt = d;
  266. if (rdt->rmctx)
  267. av_close_input_stream(rdt->rmctx);
  268. av_freep(&rdt->mlti_data);
  269. av_free(rdt);
  270. }
  271. #define RDT_HANDLER(n, s, t) \
  272. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  273. s, \
  274. t, \
  275. CODEC_ID_NONE, \
  276. rdt_parse_sdp_line, \
  277. rdt_new_extradata, \
  278. rdt_free_extradata, \
  279. rdt_parse_packet \
  280. };
  281. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
  282. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
  283. RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
  284. RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
  285. void av_register_rdt_dynamic_payload_handlers(void)
  286. {
  287. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  288. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  289. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  290. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  291. }