rdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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
  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 "rtpdec.h"
  29. #include "rdt.h"
  30. #include "libavutil/base64.h"
  31. #include "libavutil/md5.h"
  32. #include "rm.h"
  33. #include "internal.h"
  34. #include "avio_internal.h"
  35. #include "libavcodec/get_bits.h"
  36. struct RDTDemuxContext {
  37. AVFormatContext *ic; /**< the containing (RTSP) demux context */
  38. /** Each RDT stream-set (represented by one RTSPStream) can contain
  39. * multiple streams (of the same content, but with possibly different
  40. * codecs/bitrates). Each such stream is represented by one AVStream
  41. * in the AVFormatContext, and this variable points to the offset in
  42. * that array such that the first is the first stream of this set. */
  43. AVStream **streams;
  44. int n_streams; /**< streams with identifical content in this set */
  45. void *dynamic_protocol_context;
  46. DynamicPayloadPacketHandlerProc parse_packet;
  47. uint32_t prev_timestamp;
  48. int prev_set_id, prev_stream_id;
  49. };
  50. RDTDemuxContext *
  51. ff_rdt_parse_open(AVFormatContext *ic, int first_stream_of_set_idx,
  52. void *priv_data, RTPDynamicProtocolHandler *handler)
  53. {
  54. RDTDemuxContext *s = av_mallocz(sizeof(RDTDemuxContext));
  55. if (!s)
  56. return NULL;
  57. s->ic = ic;
  58. s->streams = &ic->streams[first_stream_of_set_idx];
  59. do {
  60. s->n_streams++;
  61. } while (first_stream_of_set_idx + s->n_streams < ic->nb_streams &&
  62. s->streams[s->n_streams]->id == s->streams[0]->id);
  63. s->prev_set_id = -1;
  64. s->prev_stream_id = -1;
  65. s->prev_timestamp = -1;
  66. s->parse_packet = handler ? handler->parse_packet : NULL;
  67. s->dynamic_protocol_context = priv_data;
  68. return s;
  69. }
  70. void
  71. ff_rdt_parse_close(RDTDemuxContext *s)
  72. {
  73. av_free(s);
  74. }
  75. struct PayloadContext {
  76. AVFormatContext *rmctx;
  77. int nb_rmst;
  78. RMStream **rmst;
  79. uint8_t *mlti_data;
  80. unsigned int mlti_data_size;
  81. char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
  82. int audio_pkt_cnt; /**< remaining audio packets in rmdec */
  83. };
  84. void
  85. ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
  86. const char *challenge)
  87. {
  88. int ch_len = strlen (challenge), i;
  89. unsigned char zres[16],
  90. buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
  91. #define XOR_TABLE_SIZE 37
  92. const unsigned char xor_table[XOR_TABLE_SIZE] = {
  93. 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
  94. 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
  95. 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
  96. 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
  97. 0x10, 0x57, 0x05, 0x18, 0x54 };
  98. /* some (length) checks */
  99. if (ch_len == 40) /* what a hack... */
  100. ch_len = 32;
  101. else if (ch_len > 56)
  102. ch_len = 56;
  103. memcpy(buf + 8, challenge, ch_len);
  104. /* xor challenge bytewise with xor_table */
  105. for (i = 0; i < XOR_TABLE_SIZE; i++)
  106. buf[8 + i] ^= xor_table[i];
  107. av_md5_sum(zres, buf, 64);
  108. ff_data_to_hex(response, zres, 16, 1);
  109. /* add tail */
  110. strcpy (response + 32, "01d0a8e3");
  111. /* calculate checksum */
  112. for (i = 0; i < 8; i++)
  113. chksum[i] = response[i * 4];
  114. chksum[8] = 0;
  115. }
  116. static int
  117. rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
  118. {
  119. AVIOContext pb;
  120. int size;
  121. uint32_t tag;
  122. /**
  123. * Layout of the MLTI chunk:
  124. * 4: MLTI
  125. * 2: number of streams
  126. * Then for each stream ([number_of_streams] times):
  127. * 2: mdpr index
  128. * 2: number of mdpr chunks
  129. * Then for each mdpr chunk ([number_of_mdpr_chunks] times):
  130. * 4: size
  131. * [size]: data
  132. * we skip MDPR chunks until we reach the one of the stream
  133. * we're interested in, and forward that ([size]+[data]) to
  134. * the RM demuxer to parse the stream-specific header data.
  135. */
  136. if (!rdt->mlti_data)
  137. return -1;
  138. ffio_init_context(&pb, rdt->mlti_data, rdt->mlti_data_size, 0,
  139. NULL, NULL, NULL, NULL);
  140. tag = avio_rl32(&pb);
  141. if (tag == MKTAG('M', 'L', 'T', 'I')) {
  142. int num, chunk_nr;
  143. /* read index of MDPR chunk numbers */
  144. num = avio_rb16(&pb);
  145. if (rule_nr < 0 || rule_nr >= num)
  146. return -1;
  147. avio_skip(&pb, rule_nr * 2);
  148. chunk_nr = avio_rb16(&pb);
  149. avio_skip(&pb, (num - 1 - rule_nr) * 2);
  150. /* read MDPR chunks */
  151. num = avio_rb16(&pb);
  152. if (chunk_nr >= num)
  153. return -1;
  154. while (chunk_nr--)
  155. avio_skip(&pb, avio_rb32(&pb));
  156. size = avio_rb32(&pb);
  157. } else {
  158. size = rdt->mlti_data_size;
  159. avio_seek(&pb, 0, SEEK_SET);
  160. }
  161. if (ff_rm_read_mdpr_codecdata(rdt->rmctx, &pb, st, rdt->rmst[st->index], size) < 0)
  162. return -1;
  163. return 0;
  164. }
  165. /**
  166. * Actual data handling.
  167. */
  168. int
  169. ff_rdt_parse_header(const uint8_t *buf, int len,
  170. int *pset_id, int *pseq_no, int *pstream_id,
  171. int *pis_keyframe, uint32_t *ptimestamp)
  172. {
  173. GetBitContext gb;
  174. int consumed = 0, set_id, seq_no, stream_id, is_keyframe,
  175. len_included, need_reliable;
  176. uint32_t timestamp;
  177. /* skip status packets */
  178. while (len >= 5 && buf[1] == 0xFF /* status packet */) {
  179. int pkt_len;
  180. if (!(buf[0] & 0x80))
  181. return -1; /* not followed by a data packet */
  182. pkt_len = AV_RB16(buf+3);
  183. buf += pkt_len;
  184. len -= pkt_len;
  185. consumed += pkt_len;
  186. }
  187. if (len < 16)
  188. return -1;
  189. /**
  190. * Layout of the header (in bits):
  191. * 1: len_included
  192. * Flag indicating whether this header includes a length field;
  193. * this can be used to concatenate multiple RDT packets in a
  194. * single UDP/TCP data frame and is used to precede RDT data
  195. * by stream status packets
  196. * 1: need_reliable
  197. * Flag indicating whether this header includes a "reliable
  198. * sequence number"; these are apparently sequence numbers of
  199. * data packets alone. For data packets, this flag is always
  200. * set, according to the Real documentation [1]
  201. * 5: set_id
  202. * ID of a set of streams of identical content, possibly with
  203. * different codecs or bitrates
  204. * 1: is_reliable
  205. * Flag set for certain streams deemed less tolerable for packet
  206. * loss
  207. * 16: seq_no
  208. * Packet sequence number; if >=0xFF00, this is a non-data packet
  209. * containing stream status info, the second byte indicates the
  210. * type of status packet (see wireshark docs / source code [2])
  211. * if (len_included) {
  212. * 16: packet_len
  213. * } else {
  214. * packet_len = remainder of UDP/TCP frame
  215. * }
  216. * 1: is_back_to_back
  217. * Back-to-Back flag; used for timing, set for one in every 10
  218. * packets, according to the Real documentation [1]
  219. * 1: is_slow_data
  220. * Slow-data flag; currently unused, according to Real docs [1]
  221. * 5: stream_id
  222. * ID of the stream within this particular set of streams
  223. * 1: is_no_keyframe
  224. * Non-keyframe flag (unset if packet belongs to a keyframe)
  225. * 32: timestamp (PTS)
  226. * if (set_id == 0x1F) {
  227. * 16: set_id (extended set-of-streams ID; see set_id)
  228. * }
  229. * if (need_reliable) {
  230. * 16: reliable_seq_no
  231. * Reliable sequence number (see need_reliable)
  232. * }
  233. * if (stream_id == 0x3F) {
  234. * 16: stream_id (extended stream ID; see stream_id)
  235. * }
  236. * [1] https://protocol.helixcommunity.org/files/2005/devdocs/RDT_Feature_Level_20.txt
  237. * [2] http://www.wireshark.org/docs/dfref/r/rdt.html and
  238. * http://anonsvn.wireshark.org/viewvc/trunk/epan/dissectors/packet-rdt.c
  239. */
  240. init_get_bits(&gb, buf, len << 3);
  241. len_included = get_bits1(&gb);
  242. need_reliable = get_bits1(&gb);
  243. set_id = get_bits(&gb, 5);
  244. skip_bits(&gb, 1);
  245. seq_no = get_bits(&gb, 16);
  246. if (len_included)
  247. skip_bits(&gb, 16);
  248. skip_bits(&gb, 2);
  249. stream_id = get_bits(&gb, 5);
  250. is_keyframe = !get_bits1(&gb);
  251. timestamp = get_bits_long(&gb, 32);
  252. if (set_id == 0x1f)
  253. set_id = get_bits(&gb, 16);
  254. if (need_reliable)
  255. skip_bits(&gb, 16);
  256. if (stream_id == 0x1f)
  257. stream_id = get_bits(&gb, 16);
  258. if (pset_id) *pset_id = set_id;
  259. if (pseq_no) *pseq_no = seq_no;
  260. if (pstream_id) *pstream_id = stream_id;
  261. if (pis_keyframe) *pis_keyframe = is_keyframe;
  262. if (ptimestamp) *ptimestamp = timestamp;
  263. return consumed + (get_bits_count(&gb) >> 3);
  264. }
  265. /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
  266. static int
  267. rdt_parse_packet (AVFormatContext *ctx, PayloadContext *rdt, AVStream *st,
  268. AVPacket *pkt, uint32_t *timestamp,
  269. const uint8_t *buf, int len, int flags)
  270. {
  271. int seq = 1, res;
  272. AVIOContext pb;
  273. if (rdt->audio_pkt_cnt == 0) {
  274. int pos;
  275. ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
  276. flags = (flags & RTP_FLAG_KEY) ? 2 : 0;
  277. res = ff_rm_parse_packet (rdt->rmctx, &pb, st, rdt->rmst[st->index], len, pkt,
  278. &seq, flags, *timestamp);
  279. pos = avio_tell(&pb);
  280. if (res < 0)
  281. return res;
  282. if (res > 0) {
  283. if (st->codec->codec_id == CODEC_ID_AAC) {
  284. memcpy (rdt->buffer, buf + pos, len - pos);
  285. rdt->rmctx->pb = avio_alloc_context (rdt->buffer, len - pos, 0,
  286. NULL, NULL, NULL, NULL);
  287. }
  288. goto get_cache;
  289. }
  290. } else {
  291. get_cache:
  292. rdt->audio_pkt_cnt =
  293. ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb,
  294. st, rdt->rmst[st->index], pkt);
  295. if (rdt->audio_pkt_cnt == 0 &&
  296. st->codec->codec_id == CODEC_ID_AAC)
  297. av_freep(&rdt->rmctx->pb);
  298. }
  299. pkt->stream_index = st->index;
  300. pkt->pts = *timestamp;
  301. return rdt->audio_pkt_cnt > 0;
  302. }
  303. int
  304. ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
  305. uint8_t **bufptr, int len)
  306. {
  307. uint8_t *buf = bufptr ? *bufptr : NULL;
  308. int seq_no, flags = 0, stream_id, set_id, is_keyframe;
  309. uint32_t timestamp;
  310. int rv= 0;
  311. if (!s->parse_packet)
  312. return -1;
  313. if (!buf && s->prev_stream_id != -1) {
  314. /* return the next packets, if any */
  315. timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
  316. rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
  317. s->streams[s->prev_stream_id],
  318. pkt, &timestamp, NULL, 0, flags);
  319. return rv;
  320. }
  321. if (len < 12)
  322. return -1;
  323. rv = ff_rdt_parse_header(buf, len, &set_id, &seq_no, &stream_id, &is_keyframe, &timestamp);
  324. if (rv < 0)
  325. return rv;
  326. if (is_keyframe &&
  327. (set_id != s->prev_set_id || timestamp != s->prev_timestamp ||
  328. stream_id != s->prev_stream_id)) {
  329. flags |= RTP_FLAG_KEY;
  330. s->prev_set_id = set_id;
  331. s->prev_timestamp = timestamp;
  332. }
  333. s->prev_stream_id = stream_id;
  334. buf += rv;
  335. len -= rv;
  336. if (s->prev_stream_id >= s->n_streams) {
  337. s->prev_stream_id = -1;
  338. return -1;
  339. }
  340. rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
  341. s->streams[s->prev_stream_id],
  342. pkt, &timestamp, buf, len, flags);
  343. return rv;
  344. }
  345. void
  346. ff_rdt_subscribe_rule (char *cmd, int size,
  347. int stream_nr, int rule_nr)
  348. {
  349. av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
  350. stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
  351. }
  352. static unsigned char *
  353. rdt_parse_b64buf (unsigned int *target_len, const char *p)
  354. {
  355. unsigned char *target;
  356. int len = strlen(p);
  357. if (*p == '\"') {
  358. p++;
  359. len -= 2; /* skip embracing " at start/end */
  360. }
  361. *target_len = len * 3 / 4;
  362. target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
  363. av_base64_decode(target, p, *target_len);
  364. return target;
  365. }
  366. static int
  367. rdt_parse_sdp_line (AVFormatContext *s, int st_index,
  368. PayloadContext *rdt, const char *line)
  369. {
  370. AVStream *stream = s->streams[st_index];
  371. const char *p = line;
  372. if (av_strstart(p, "OpaqueData:buffer;", &p)) {
  373. rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
  374. } else if (av_strstart(p, "StartTime:integer;", &p))
  375. stream->first_dts = atoi(p);
  376. else if (av_strstart(p, "ASMRuleBook:string;", &p)) {
  377. int n, first = -1;
  378. for (n = 0; n < s->nb_streams; n++)
  379. if (s->streams[n]->id == stream->id) {
  380. int count = s->streams[n]->index + 1;
  381. if (first == -1) first = n;
  382. if (rdt->nb_rmst < count) {
  383. RMStream **rmst= av_realloc(rdt->rmst, count*sizeof(*rmst));
  384. if (!rmst)
  385. return AVERROR(ENOMEM);
  386. memset(rmst + rdt->nb_rmst, 0,
  387. (count - rdt->nb_rmst) * sizeof(*rmst));
  388. rdt->rmst = rmst;
  389. rdt->nb_rmst = count;
  390. }
  391. rdt->rmst[s->streams[n]->index] = ff_rm_alloc_rmstream();
  392. rdt_load_mdpr(rdt, s->streams[n], (n - first) * 2);
  393. if (s->streams[n]->codec->codec_id == CODEC_ID_AAC)
  394. s->streams[n]->codec->frame_size = 1; // FIXME
  395. }
  396. }
  397. return 0;
  398. }
  399. static void
  400. real_parse_asm_rule(AVStream *st, const char *p, const char *end)
  401. {
  402. do {
  403. /* can be either averagebandwidth= or AverageBandwidth= */
  404. if (sscanf(p, " %*1[Aa]verage%*1[Bb]andwidth=%d", &st->codec->bit_rate) == 1)
  405. break;
  406. if (!(p = strchr(p, ',')) || p > end)
  407. p = end;
  408. p++;
  409. } while (p < end);
  410. }
  411. static AVStream *
  412. add_dstream(AVFormatContext *s, AVStream *orig_st)
  413. {
  414. AVStream *st;
  415. if (!(st = av_new_stream(s, orig_st->id)))
  416. return NULL;
  417. st->codec->codec_type = orig_st->codec->codec_type;
  418. st->first_dts = orig_st->first_dts;
  419. return st;
  420. }
  421. static void
  422. real_parse_asm_rulebook(AVFormatContext *s, AVStream *orig_st,
  423. const char *p)
  424. {
  425. const char *end;
  426. int n_rules = 0, odd = 0;
  427. AVStream *st;
  428. /**
  429. * The ASMRuleBook contains a list of comma-separated strings per rule,
  430. * and each rule is separated by a ;. The last one also has a ; at the
  431. * end so we can use it as delimiter.
  432. * Every rule occurs twice, once for when the RTSP packet header marker
  433. * is set and once for if it isn't. We only read the first because we
  434. * don't care much (that's what the "odd" variable is for).
  435. * Each rule contains a set of one or more statements, optionally
  436. * preceeded by a single condition. If there's a condition, the rule
  437. * starts with a '#'. Multiple conditions are merged between brackets,
  438. * so there are never multiple conditions spread out over separate
  439. * statements. Generally, these conditions are bitrate limits (min/max)
  440. * for multi-bitrate streams.
  441. */
  442. if (*p == '\"') p++;
  443. while (1) {
  444. if (!(end = strchr(p, ';')))
  445. break;
  446. if (!odd && end != p) {
  447. if (n_rules > 0)
  448. st = add_dstream(s, orig_st);
  449. else
  450. st = orig_st;
  451. if (!st)
  452. break;
  453. real_parse_asm_rule(st, p, end);
  454. n_rules++;
  455. }
  456. p = end + 1;
  457. odd ^= 1;
  458. }
  459. }
  460. void
  461. ff_real_parse_sdp_a_line (AVFormatContext *s, int stream_index,
  462. const char *line)
  463. {
  464. const char *p = line;
  465. if (av_strstart(p, "ASMRuleBook:string;", &p))
  466. real_parse_asm_rulebook(s, s->streams[stream_index], p);
  467. }
  468. static PayloadContext *
  469. rdt_new_context (void)
  470. {
  471. PayloadContext *rdt = av_mallocz(sizeof(PayloadContext));
  472. avformat_open_input(&rdt->rmctx, "", &ff_rdt_demuxer, NULL);
  473. return rdt;
  474. }
  475. static void
  476. rdt_free_context (PayloadContext *rdt)
  477. {
  478. int i;
  479. for (i = 0; i < rdt->nb_rmst; i++)
  480. if (rdt->rmst[i]) {
  481. ff_rm_free_rmstream(rdt->rmst[i]);
  482. av_freep(&rdt->rmst[i]);
  483. }
  484. if (rdt->rmctx)
  485. av_close_input_file(rdt->rmctx);
  486. av_freep(&rdt->mlti_data);
  487. av_freep(&rdt->rmst);
  488. av_free(rdt);
  489. }
  490. #define RDT_HANDLER(n, s, t) \
  491. static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
  492. .enc_name = s, \
  493. .codec_type = t, \
  494. .codec_id = CODEC_ID_NONE, \
  495. .parse_sdp_a_line = rdt_parse_sdp_line, \
  496. .alloc = rdt_new_context, \
  497. .free = rdt_free_context, \
  498. .parse_packet = rdt_parse_packet \
  499. }
  500. RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", AVMEDIA_TYPE_VIDEO);
  501. RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", AVMEDIA_TYPE_AUDIO);
  502. RDT_HANDLER(video, "x-pn-realvideo", AVMEDIA_TYPE_VIDEO);
  503. RDT_HANDLER(audio, "x-pn-realaudio", AVMEDIA_TYPE_AUDIO);
  504. void av_register_rdt_dynamic_payload_handlers(void)
  505. {
  506. ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
  507. ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
  508. ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
  509. ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
  510. }