]> git.sesse.net Git - ffmpeg/blob - libavformat/rdt.c
1fa36df04dcd888c4cf8a4e969fc250f98c86081
[ffmpeg] / libavformat / rdt.c
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 PayloadContext {
38     AVFormatContext *rmctx;
39     uint8_t *mlti_data;
40     unsigned int mlti_data_size;
41     uint32_t prev_sn, prev_ts;
42     char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
43 };
44
45 void
46 ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
47                                   const char *challenge)
48 {
49     int ch_len = strlen (challenge), i;
50     unsigned char zres[16],
51         buf[64] = { 0xa1, 0xe9, 0x14, 0x9d, 0x0e, 0x6b, 0x3b, 0x59 };
52 #define XOR_TABLE_SIZE 37
53     const unsigned char xor_table[XOR_TABLE_SIZE] = {
54         0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
55         0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
56         0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
57         0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
58         0x10, 0x57, 0x05, 0x18, 0x54 };
59
60     /* some (length) checks */
61     if (ch_len == 40) /* what a hack... */
62         ch_len = 32;
63     else if (ch_len > 56)
64         ch_len = 56;
65     memcpy(buf + 8, challenge, ch_len);
66
67     /* xor challenge bytewise with xor_table */
68     for (i = 0; i < XOR_TABLE_SIZE; i++)
69         buf[8 + i] ^= xor_table[i];
70
71     av_md5_sum(zres, buf, 64);
72     ff_data_to_hex(response, zres, 16);
73     for (i=0;i<32;i++) response[i] = tolower(response[i]);
74
75     /* add tail */
76     strcpy (response + 32, "01d0a8e3");
77
78     /* calculate checksum */
79     for (i = 0; i < 8; i++)
80         chksum[i] = response[i * 4];
81     chksum[8] = 0;
82 }
83
84 static int
85 rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
86 {
87     ByteIOContext *pb;
88     int size;
89     uint32_t tag;
90
91     /**
92      * Layout of the MLTI chunk:
93      * 4:MLTI
94      * 2:<number of streams>
95      * Then for each stream ([number_of_streams] times):
96      *     2:<mdpr index>
97      * 2:<number of mdpr chunks>
98      * Then for each mdpr chunk ([number_of_mdpr_chunks] times):
99      *     4:<size>
100      *     [size]:<data>
101      * we skip MDPR chunks until we reach the one of the stream
102      * we're interested in, and forward that ([size]+[data]) to
103      * the RM demuxer to parse the stream-specific header data.
104      */
105     if (!rdt->mlti_data)
106         return -1;
107     url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
108     tag = get_le32(pb);
109     if (tag == MKTAG('M', 'L', 'T', 'I')) {
110         int num, chunk_nr;
111
112         /* read index of MDPR chunk numbers */
113         num = get_be16(pb);
114         if (rule_nr < 0 || rule_nr >= num)
115             return -1;
116         url_fskip(pb, rule_nr * 2);
117         chunk_nr = get_be16(pb);
118         url_fskip(pb, (num - 1 - rule_nr) * 2);
119
120         /* read MDPR chunks */
121         num = get_be16(pb);
122         if (chunk_nr >= num)
123             return -1;
124         while (chunk_nr--)
125             url_fskip(pb, get_be32(pb));
126         size = get_be32(pb);
127     } else {
128         size = rdt->mlti_data_size;
129         url_fseek(pb, 0, SEEK_SET);
130     }
131     rdt->rmctx->pb = pb;
132     if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
133         return -1;
134
135     url_close_buf(pb);
136     return 0;
137 }
138
139 /**
140  * Actual data handling.
141  */
142
143 int
144 ff_rdt_parse_header(const uint8_t *buf, int len,
145                     int *sn, int *seq, int *rn, uint32_t *ts)
146 {
147     int consumed = 10;
148
149     if (len > 0 && (buf[0] < 0x40 || buf[0] > 0x42)) {
150         buf += 9;
151         len -= 9;
152         consumed += 9;
153     }
154     if (len < 10)
155         return -1;
156     if (sn)  *sn  = (buf[0]>>1) & 0x1f;
157     if (seq) *seq = AV_RB16(buf+1);
158     if (ts)  *ts  = AV_RB32(buf+4);
159     if (rn)  *rn  = buf[3] & 0x3f;
160
161     return consumed;
162 }
163
164 /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
165 static int
166 rdt_parse_packet (RTPDemuxContext *s, AVPacket *pkt, uint32_t *timestamp,
167                   const uint8_t *buf, int len, int flags)
168 {
169     PayloadContext *rdt = s->dynamic_protocol_context;
170     int seq = 1, res;
171     ByteIOContext *pb = rdt->rmctx->pb;
172     RMContext *rm = rdt->rmctx->priv_data;
173     AVStream *st = s->st;
174
175     if (rm->audio_pkt_cnt == 0) {
176         int pos;
177
178         url_open_buf (&pb, buf, len, URL_RDONLY);
179         flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
180         rdt->rmctx->pb = pb;
181         res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
182                                   &seq, &flags, timestamp);
183         pos = url_ftell(pb);
184         url_close_buf (pb);
185         if (res < 0)
186             return res;
187         if (rm->audio_pkt_cnt > 0 &&
188             st->codec->codec_id == CODEC_ID_AAC) {
189             memcpy (rdt->buffer, buf + pos, len - pos);
190             url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
191             rdt->rmctx->pb = pb;
192         }
193     } else {
194         ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
195         if (rm->audio_pkt_cnt == 0 &&
196             st->codec->codec_id == CODEC_ID_AAC)
197             url_close_buf (pb);
198     }
199     pkt->stream_index = st->index;
200     pkt->pts = *timestamp;
201
202     return rm->audio_pkt_cnt > 0;
203 }
204
205 int
206 ff_rdt_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
207                     const uint8_t *buf, int len)
208 {
209     PayloadContext *rdt = s->dynamic_protocol_context;
210     int seq, flags = 0, rule, sn;
211     uint32_t timestamp;
212     int rv= 0;
213
214     if (!s->parse_packet)
215         return -1;
216
217     if (!buf) {
218         /* return the next packets, if any */
219         timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
220         rv= s->parse_packet(s, pkt, &timestamp, NULL, 0, flags);
221         return rv;
222     }
223
224     if (len < 12)
225         return -1;
226     rv = ff_rdt_parse_header(buf, len, &sn, &seq, &rule, &timestamp);
227     if (rv < 0)
228         return rv;
229     if (!(rule & 1) && (sn != rdt->prev_sn || timestamp != rdt->prev_ts)) {
230         flags |= PKT_FLAG_KEY;
231         rdt->prev_sn = sn;
232         rdt->prev_ts = timestamp;
233     }
234     buf += rv;
235     len -= rv;
236     s->seq = seq;
237
238     rv = s->parse_packet(s, pkt, &timestamp, buf, len, flags);
239
240     return rv;
241 }
242
243 void
244 ff_rdt_subscribe_rule (char *cmd, int size,
245                        int stream_nr, int rule_nr)
246 {
247     av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
248                 stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
249 }
250
251 void
252 ff_rdt_subscribe_rule2 (RTPDemuxContext *s, char *cmd, int size,
253                         int stream_nr, int rule_nr)
254 {
255     PayloadContext *rdt = s->dynamic_protocol_context;
256
257     rdt_load_mdpr(rdt, s->st, rule_nr * 2);
258 }
259
260 static unsigned char *
261 rdt_parse_b64buf (unsigned int *target_len, const char *p)
262 {
263     unsigned char *target;
264     int len = strlen(p);
265     if (*p == '\"') {
266         p++;
267         len -= 2; /* skip embracing " at start/end */
268     }
269     *target_len = len * 3 / 4;
270     target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
271     av_base64_decode(target, p, *target_len);
272     return target;
273 }
274
275 static int
276 rdt_parse_sdp_line (AVStream *stream, PayloadContext *rdt, const char *line)
277 {
278     const char *p = line;
279
280     if (av_strstart(p, "OpaqueData:buffer;", &p)) {
281         rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
282     } else if (av_strstart(p, "StartTime:integer;", &p))
283         stream->first_dts = atoi(p);
284
285     return 0;
286 }
287
288 static PayloadContext *
289 rdt_new_extradata (void)
290 {
291     PayloadContext *rdt = av_mallocz(sizeof(PayloadContext));
292
293     av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
294     rdt->prev_ts = -1;
295     rdt->prev_sn = -1;
296
297     return rdt;
298 }
299
300 static void
301 rdt_free_extradata (PayloadContext *rdt)
302 {
303     if (rdt->rmctx)
304         av_close_input_stream(rdt->rmctx);
305     av_freep(&rdt->mlti_data);
306     av_free(rdt);
307 }
308
309 #define RDT_HANDLER(n, s, t) \
310 static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
311     s, \
312     t, \
313     CODEC_ID_NONE, \
314     rdt_parse_sdp_line, \
315     rdt_new_extradata, \
316     rdt_free_extradata, \
317     rdt_parse_packet \
318 };
319
320 RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
321 RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
322 RDT_HANDLER(video,      "x-pn-realvideo",                CODEC_TYPE_VIDEO);
323 RDT_HANDLER(audio,      "x-pn-realaudio",                CODEC_TYPE_AUDIO);
324
325 void av_register_rdt_dynamic_payload_handlers(void)
326 {
327     ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
328     ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
329     ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
330     ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);
331 }