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