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.

221 lines
6.2KB

  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. } rdt_data;
  39. void
  40. ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
  41. const char *challenge)
  42. {
  43. int ch_len = strlen (challenge), i;
  44. unsigned char zres[16],
  45. buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
  46. #define XOR_TABLE_SIZE 37
  47. const unsigned char xor_table[XOR_TABLE_SIZE] = {
  48. 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
  49. 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
  50. 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
  51. 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
  52. 0x10, 0x57, 0x05, 0x18, 0x54 };
  53. /* some (length) checks */
  54. if (ch_len == 40) /* what a hack... */
  55. ch_len = 32;
  56. else if (ch_len > 56)
  57. ch_len = 56;
  58. memcpy(buf + 8, challenge, ch_len);
  59. /* xor challenge bytewise with xor_table */
  60. for (i = 0; i < XOR_TABLE_SIZE; i++)
  61. buf[8 + i] ^= xor_table[i];
  62. av_md5_sum(zres, buf, 64);
  63. ff_data_to_hex(response, zres, 16);
  64. for (i=0;i<32;i++) response[i] = tolower(response[i]);
  65. /* add tail */
  66. strcpy (response + 32, "01d0a8e3");
  67. /* calculate checksum */
  68. for (i = 0; i < 8; i++)
  69. chksum[i] = response[i * 4];
  70. chksum[8] = 0;
  71. }
  72. static int
  73. rdt_load_mdpr (rdt_data *rdt, AVStream *st, int rule_nr)
  74. {
  75. ByteIOContext *pb;
  76. int size;
  77. uint32_t tag;
  78. /**
  79. * Layout of the MLTI chunk:
  80. * 4:MLTI
  81. * 2:<number of streams>
  82. * Then for each stream ([number_of_streams] times):
  83. * 2:<mdpr index>
  84. * 2:<number of mdpr chunks>
  85. * Then for each mdpr chunk ([number_of_mdpr_chunks] times):
  86. * 4:<size>
  87. * [size]:<data>
  88. * we skip MDPR chunks until we reach the one of the stream
  89. * we're interested in, and forward that ([size]+[data]) to
  90. * the RM demuxer to parse the stream-specific header data.
  91. */
  92. if (!rdt->mlti_data)
  93. return -1;
  94. url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
  95. tag = get_le32(pb);
  96. if (tag == MKTAG('M', 'L', 'T', 'I')) {
  97. int num, chunk_nr;
  98. /* read index of MDPR chunk numbers */
  99. num = get_be16(pb);
  100. if (rule_nr < 0 || rule_nr >= num)
  101. return -1;
  102. url_fskip(pb, rule_nr * 2);
  103. chunk_nr = get_be16(pb);
  104. url_fskip(pb, (num - 1 - rule_nr) * 2);
  105. /* read MDPR chunks */
  106. num = get_be16(pb);
  107. if (chunk_nr >= num)
  108. return -1;
  109. while (chunk_nr--)
  110. url_fskip(pb, get_be32(pb));
  111. size = get_be32(pb);
  112. } else {
  113. size = rdt->mlti_data_size;
  114. url_fseek(pb, 0, SEEK_SET);
  115. }
  116. rdt->rmctx->pb = pb;
  117. if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
  118. return -1;
  119. url_close_buf(pb);
  120. return 0;
  121. }
  122. void
  123. ff_rdt_subscribe_rule (RTPDemuxContext *s, char *cmd, int size,
  124. int stream_nr, int rule_nr)
  125. {
  126. rdt_data *rdt = s->dynamic_protocol_context;
  127. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  128. stream_nr, rule_nr, stream_nr, rule_nr + 1);
  129. rdt_load_mdpr(rdt, s->st, 0);
  130. }
  131. static unsigned char *
  132. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  133. {
  134. unsigned char *target;
  135. int len = strlen(p);
  136. if (*p == '\"') {
  137. p++;
  138. len -= 2; /* skip embracing " at start/end */
  139. }
  140. *target_len = len * 3 / 4;
  141. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  142. av_base64_decode(target, p, *target_len);
  143. return target;
  144. }
  145. static int
  146. rdt_parse_sdp_line (AVStream *stream, void *d, const char *line)
  147. {
  148. rdt_data *rdt = d;
  149. const char *p = line;
  150. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  151. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  152. } else if (av_strstart(p, "StartTime:integer;", &p))
  153. stream->first_dts = atoi(p);
  154. return 0;
  155. }
  156. static void *
  157. rdt_new_extradata (void)
  158. {
  159. rdt_data *rdt = av_mallocz(sizeof(rdt_data));
  160. av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
  161. return rdt;
  162. }
  163. static void
  164. rdt_free_extradata (void *d)
  165. {
  166. rdt_data *rdt = d;
  167. if (rdt->rmctx)
  168. av_close_input_stream(rdt->rmctx);
  169. av_freep(&rdt->mlti_data);
  170. av_free(rdt);
  171. }
  172. #define RDT_HANDLER(n, s, t) \
  173. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  174. s, \
  175. t, \
  176. CODEC_ID_NONE, \
  177. rdt_parse_sdp_line, \
  178. rdt_new_extradata, \
  179. rdt_free_extradata \
  180. };
  181. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
  182. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
  183. RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
  184. RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
  185. void av_register_rdt_dynamic_payload_handlers(void)
  186. {
  187. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  188. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  189. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  190. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  191. }