Statistics
| Branch: | Revision:

ffmpeg / libavformat / rdt.c @ accc248f

History | View | Annotate | Download (9.9 KB)

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 rdt.c
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 "rtp_internal.h"
31
#include "rdt.h"
32
#include "libavutil/base64.h"
33
#include "libavutil/md5.h"
34
#include "rm.h"
35
#include "internal.h"
36

    
37
struct RDTDemuxContext {
38
    AVFormatContext *ic;
39
    AVStream *st;
40
    void *dynamic_protocol_context;
41
    DynamicPayloadPacketHandlerProc parse_packet;
42
    uint32_t prev_sn, prev_ts;
43
};
44

    
45
RDTDemuxContext *
46
ff_rdt_parse_open(AVFormatContext *ic, AVStream *st,
47
                  void *priv_data, RTPDynamicProtocolHandler *handler)
48
{
49
    RDTDemuxContext *s = av_mallocz(sizeof(RDTDemuxContext));
50
    if (!s)
51
        return NULL;
52

    
53
    s->ic = ic;
54
    s->st = st;
55
    s->prev_sn = -1;
56
    s->prev_ts = -1;
57
    s->parse_packet = handler->parse_packet;
58
    s->dynamic_protocol_context = priv_data;
59

    
60
    return s;
61
}
62

    
63
void
64
ff_rdt_parse_close(RDTDemuxContext *s)
65
{
66
    av_free(s);
67
}
68

    
69
struct PayloadContext {
70
    AVFormatContext *rmctx;
71
    uint8_t *mlti_data;
72
    unsigned int mlti_data_size;
73
    char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
74
};
75

    
76
void
77
ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
78
                                  const char *challenge)
79
{
80
    int ch_len = strlen (challenge), i;
81
    unsigned char zres[16],
82
        buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
83
#define XOR_TABLE_SIZE 37
84
    const unsigned char xor_table[XOR_TABLE_SIZE] = {
85
        0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
86
        0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
87
        0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
88
        0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
89
        0x10, 0x57, 0x05, 0x18, 0x54 };
90

    
91
    /* some (length) checks */
92
    if (ch_len == 40) /* what a hack... */
93
        ch_len = 32;
94
    else if (ch_len > 56)
95
        ch_len = 56;
96
    memcpy(buf + 8, challenge, ch_len);
97

    
98
    /* xor challenge bytewise with xor_table */
99
    for (i = 0; i < XOR_TABLE_SIZE; i++)
100
        buf[8 + i] ^= xor_table[i];
101

    
102
    av_md5_sum(zres, buf, 64);
103
    ff_data_to_hex(response, zres, 16);
104
    for (i=0;i<32;i++) response[i] = tolower(response[i]);
105

    
106
    /* add tail */
107
    strcpy (response + 32, "01d0a8e3");
108

    
109
    /* calculate checksum */
110
    for (i = 0; i < 8; i++)
111
        chksum[i] = response[i * 4];
112
    chksum[8] = 0;
113
}
114

    
115
static int
116
rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
117
{
118
    ByteIOContext *pb;
119
    int size;
120
    uint32_t tag;
121

    
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
    url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
139
    tag = get_le32(pb);
140
    if (tag == MKTAG('M', 'L', 'T', 'I')) {
141
        int num, chunk_nr;
142

    
143
        /* read index of MDPR chunk numbers */
144
        num = get_be16(pb);
145
        if (rule_nr < 0 || rule_nr >= num)
146
            return -1;
147
        url_fskip(pb, rule_nr * 2);
148
        chunk_nr = get_be16(pb);
149
        url_fskip(pb, (num - 1 - rule_nr) * 2);
150

    
151
        /* read MDPR chunks */
152
        num = get_be16(pb);
153
        if (chunk_nr >= num)
154
            return -1;
155
        while (chunk_nr--)
156
            url_fskip(pb, get_be32(pb));
157
        size = get_be32(pb);
158
    } else {
159
        size = rdt->mlti_data_size;
160
        url_fseek(pb, 0, SEEK_SET);
161
    }
162
    rdt->rmctx->pb = pb;
163
    if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
164
        return -1;
165

    
166
    url_close_buf(pb);
167
    return 0;
168
}
169

    
170
/**
171
 * Actual data handling.
172
 */
173

    
174
int
175
ff_rdt_parse_header(const uint8_t *buf, int len,
176
                    int *sn, int *seq, int *rn, uint32_t *ts)
177
{
178
    int consumed = 10;
179

    
180
    if (len > 0 && (buf[0] < 0x40 || buf[0] > 0x42)) {
181
        buf += 9;
182
        len -= 9;
183
        consumed += 9;
184
    }
185
    if (len < 10)
186
        return -1;
187
    if (sn)  *sn  = (buf[0]>>1) & 0x1f;
188
    if (seq) *seq = AV_RB16(buf+1);
189
    if (ts)  *ts  = AV_RB32(buf+4);
190
    if (rn)  *rn  = buf[3] & 0x3f;
191

    
192
    return consumed;
193
}
194

    
195
/**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
196
static int
197
rdt_parse_packet (PayloadContext *rdt, AVStream *st,
198
                  AVPacket *pkt, uint32_t *timestamp,
199
                  const uint8_t *buf, int len, int flags)
200
{
201
    int seq = 1, res;
202
    ByteIOContext *pb = rdt->rmctx->pb;
203
    RMContext *rm = rdt->rmctx->priv_data;
204

    
205
    if (rm->audio_pkt_cnt == 0) {
206
        int pos;
207

    
208
        url_open_buf (&pb, buf, len, URL_RDONLY);
209
        flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
210
        rdt->rmctx->pb = pb;
211
        res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
212
                                  &seq, &flags, timestamp);
213
        pos = url_ftell(pb);
214
        url_close_buf (pb);
215
        if (res < 0)
216
            return res;
217
        if (rm->audio_pkt_cnt > 0 &&
218
            st->codec->codec_id == CODEC_ID_AAC) {
219
            memcpy (rdt->buffer, buf + pos, len - pos);
220
            url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
221
            rdt->rmctx->pb = pb;
222
        }
223
    } else {
224
        ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
225
        if (rm->audio_pkt_cnt == 0 &&
226
            st->codec->codec_id == CODEC_ID_AAC)
227
            url_close_buf (pb);
228
    }
229
    pkt->stream_index = st->index;
230
    pkt->pts = *timestamp;
231

    
232
    return rm->audio_pkt_cnt > 0;
233
}
234

    
235
int
236
ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
237
                    const uint8_t *buf, int len)
238
{
239
    int seq, flags = 0, rule, sn;
240
    uint32_t timestamp;
241
    int rv= 0;
242

    
243
    if (!s->parse_packet)
244
        return -1;
245

    
246
    if (!buf) {
247
        /* return the next packets, if any */
248
        timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
249
        rv= s->parse_packet(s->dynamic_protocol_context,
250
                            s->st, pkt, &timestamp, NULL, 0, flags);
251
        return rv;
252
    }
253

    
254
    if (len < 12)
255
        return -1;
256
    rv = ff_rdt_parse_header(buf, len, &sn, &seq, &rule, &timestamp);
257
    if (rv < 0)
258
        return rv;
259
    if (!(rule & 1) && (sn != s->prev_sn || timestamp != s->prev_ts)) {
260
        flags |= PKT_FLAG_KEY;
261
        s->prev_sn = sn;
262
        s->prev_ts = timestamp;
263
    }
264
    buf += rv;
265
    len -= rv;
266

    
267
    rv = s->parse_packet(s->dynamic_protocol_context,
268
                         s->st, pkt, &timestamp, buf, len, flags);
269

    
270
    return rv;
271
}
272

    
273
void
274
ff_rdt_subscribe_rule (char *cmd, int size,
275
                       int stream_nr, int rule_nr)
276
{
277
    av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
278
                stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
279
}
280

    
281
void
282
ff_rdt_subscribe_rule2 (RDTDemuxContext *s, char *cmd, int size,
283
                        int stream_nr, int rule_nr)
284
{
285
    PayloadContext *rdt = s->dynamic_protocol_context;
286

    
287
    rdt_load_mdpr(rdt, s->st, rule_nr * 2);
288
}
289

    
290
static unsigned char *
291
rdt_parse_b64buf (unsigned int *target_len, const char *p)
292
{
293
    unsigned char *target;
294
    int len = strlen(p);
295
    if (*p == '\"') {
296
        p++;
297
        len -= 2; /* skip embracing " at start/end */
298
    }
299
    *target_len = len * 3 / 4;
300
    target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
301
    av_base64_decode(target, p, *target_len);
302
    return target;
303
}
304

    
305
static int
306
rdt_parse_sdp_line (AVStream *stream, PayloadContext *rdt, const char *line)
307
{
308
    const char *p = line;
309

    
310
    if (av_strstart(p, "OpaqueData:buffer;", &p)) {
311
        rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
312
    } else if (av_strstart(p, "StartTime:integer;", &p))
313
        stream->first_dts = atoi(p);
314

    
315
    return 0;
316
}
317

    
318
static PayloadContext *
319
rdt_new_extradata (void)
320
{
321
    PayloadContext *rdt = av_mallocz(sizeof(PayloadContext));
322

    
323
    av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
324

    
325
    return rdt;
326
}
327

    
328
static void
329
rdt_free_extradata (PayloadContext *rdt)
330
{
331
    if (rdt->rmctx)
332
        av_close_input_stream(rdt->rmctx);
333
    av_freep(&rdt->mlti_data);
334
    av_free(rdt);
335
}
336

    
337
#define RDT_HANDLER(n, s, t) \
338
static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
339
    s, \
340
    t, \
341
    CODEC_ID_NONE, \
342
    rdt_parse_sdp_line, \
343
    rdt_new_extradata, \
344
    rdt_free_extradata, \
345
    rdt_parse_packet \
346
};
347

    
348
RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
349
RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
350
RDT_HANDLER(video,      "x-pn-realvideo",                CODEC_TYPE_VIDEO);
351
RDT_HANDLER(audio,      "x-pn-realaudio",                CODEC_TYPE_AUDIO);
352

    
353
void av_register_rdt_dynamic_payload_handlers(void)
354
{
355
    ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
356
    ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
357
    ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
358
    ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
359
}