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.

322 lines
9.1KB

  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. static int rdt_parse_header(struct RTPDemuxContext *s, const uint8_t *buf,
  128. int len, int *seq, uint32_t *timestamp, int *flags)
  129. {
  130. rdt_data *rdt = s->dynamic_protocol_context;
  131. int consumed = 0, sn;
  132. if (buf[0] < 0x40 || buf[0] > 0x42) {
  133. buf += 9;
  134. len -= 9;
  135. consumed += 9;
  136. }
  137. sn = (buf[0]>>1) & 0x1f;
  138. *seq = AV_RB16(buf+1);
  139. *timestamp = AV_RB32(buf+4);
  140. if (!(buf[3] & 1) && (sn != rdt->prev_sn || *timestamp != rdt->prev_ts)) {
  141. *flags |= PKT_FLAG_KEY;
  142. rdt->prev_sn = sn;
  143. rdt->prev_ts = *timestamp;
  144. }
  145. return consumed + 10;
  146. }
  147. /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
  148. static int
  149. rdt_parse_packet (RTPDemuxContext *s, AVPacket *pkt, uint32_t *timestamp,
  150. const uint8_t *buf, int len, int flags)
  151. {
  152. rdt_data *rdt = s->dynamic_protocol_context;
  153. int seq = 1, res;
  154. ByteIOContext *pb = rdt->rmctx->pb;
  155. RMContext *rm = rdt->rmctx->priv_data;
  156. AVStream *st = s->st;
  157. if (rm->audio_pkt_cnt == 0) {
  158. int pos;
  159. url_open_buf (&pb, buf, len, URL_RDONLY);
  160. flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
  161. rdt->rmctx->pb = pb;
  162. res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
  163. &seq, &flags, timestamp);
  164. pos = url_ftell(pb);
  165. url_close_buf (pb);
  166. if (res < 0)
  167. return res;
  168. if (rm->audio_pkt_cnt > 0 &&
  169. st->codec->codec_id == CODEC_ID_AAC) {
  170. memcpy (rdt->buffer, buf + pos, len - pos);
  171. url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
  172. rdt->rmctx->pb = pb;
  173. }
  174. } else {
  175. ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
  176. if (rm->audio_pkt_cnt == 0 &&
  177. st->codec->codec_id == CODEC_ID_AAC)
  178. url_close_buf (pb);
  179. }
  180. pkt->stream_index = st->index;
  181. pkt->pts = *timestamp;
  182. return rm->audio_pkt_cnt > 0;
  183. }
  184. int
  185. ff_rdt_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  186. const uint8_t *buf, int len)
  187. {
  188. int seq, flags = 0;
  189. uint32_t timestamp;
  190. int rv= 0;
  191. if (!buf) {
  192. /* return the next packets, if any */
  193. timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
  194. rv= rdt_parse_packet(s, pkt, &timestamp, NULL, 0, flags);
  195. return rv;
  196. }
  197. if (len < 12)
  198. return -1;
  199. rv = rdt_parse_header(s, buf, len, &seq, &timestamp, &flags);
  200. if (rv < 0)
  201. return rv;
  202. buf += rv;
  203. len -= rv;
  204. s->seq = seq;
  205. rv = rdt_parse_packet(s, pkt, &timestamp, buf, len, flags);
  206. return rv;
  207. }
  208. void
  209. ff_rdt_subscribe_rule (RTPDemuxContext *s, char *cmd, int size,
  210. int stream_nr, int rule_nr)
  211. {
  212. rdt_data *rdt = s->dynamic_protocol_context;
  213. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  214. stream_nr, rule_nr, stream_nr, rule_nr + 1);
  215. rdt_load_mdpr(rdt, s->st, 0);
  216. }
  217. static unsigned char *
  218. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  219. {
  220. unsigned char *target;
  221. int len = strlen(p);
  222. if (*p == '\"') {
  223. p++;
  224. len -= 2; /* skip embracing " at start/end */
  225. }
  226. *target_len = len * 3 / 4;
  227. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  228. av_base64_decode(target, p, *target_len);
  229. return target;
  230. }
  231. static int
  232. rdt_parse_sdp_line (AVStream *stream, void *d, const char *line)
  233. {
  234. rdt_data *rdt = d;
  235. const char *p = line;
  236. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  237. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  238. } else if (av_strstart(p, "StartTime:integer;", &p))
  239. stream->first_dts = atoi(p);
  240. return 0;
  241. }
  242. static void *
  243. rdt_new_extradata (void)
  244. {
  245. rdt_data *rdt = av_mallocz(sizeof(rdt_data));
  246. av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
  247. rdt->prev_ts = -1;
  248. rdt->prev_sn = -1;
  249. return rdt;
  250. }
  251. static void
  252. rdt_free_extradata (void *d)
  253. {
  254. rdt_data *rdt = d;
  255. if (rdt->rmctx)
  256. av_close_input_stream(rdt->rmctx);
  257. av_freep(&rdt->mlti_data);
  258. av_free(rdt);
  259. }
  260. #define RDT_HANDLER(n, s, t) \
  261. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  262. s, \
  263. t, \
  264. CODEC_ID_NONE, \
  265. rdt_parse_sdp_line, \
  266. rdt_new_extradata, \
  267. rdt_free_extradata \
  268. };
  269. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
  270. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
  271. RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
  272. RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
  273. void av_register_rdt_dynamic_payload_handlers(void)
  274. {
  275. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  276. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  277. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  278. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  279. }