Implement RDT-specific data parsing routines. After these changes, simple
[ffmpeg-lucabe.git] / libavformat / rdt.c
blob2007566b3a673ae202ef0c80e3b963f8c772d43b
1 /*
2 * Realmedia RTSP protocol (RDT) support.
3 * Copyright (c) 2007 Ronald S. Bultje
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file rdt.c
24 * @brief Realmedia RTSP protocol (RDT) support
25 * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
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"
37 typedef struct rdt_data {
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 } rdt_data;
45 void
46 ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
47 const char *challenge)
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 };
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);
67 /* xor challenge bytewise with xor_table */
68 for (i = 0; i < XOR_TABLE_SIZE; i++)
69 buf[8 + i] ^= xor_table[i];
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]);
75 /* add tail */
76 strcpy (response + 32, "01d0a8e3");
78 /* calculate checksum */
79 for (i = 0; i < 8; i++)
80 chksum[i] = response[i * 4];
81 chksum[8] = 0;
84 static int
85 rdt_load_mdpr (rdt_data *rdt, AVStream *st, int rule_nr)
87 ByteIOContext *pb;
88 int size;
89 uint32_t tag;
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.
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;
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);
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);
131 rdt->rmctx->pb = pb;
132 if (ff_rm_read_mdpr_codecdata(rdt->rmctx, st, size) < 0)
133 return -1;
135 url_close_buf(pb);
136 return 0;
140 * Actual data handling.
143 static int rdt_parse_header(struct RTPDemuxContext *s, const uint8_t *buf,
144 int len, int *seq, uint32_t *timestamp, int *flags)
146 rdt_data *rdt = s->dynamic_protocol_context;
147 int consumed = 0, sn;
149 if (buf[0] < 0x40 || buf[0] > 0x42) {
150 buf += 9;
151 len -= 9;
152 consumed += 9;
154 sn = (buf[0]>>1) & 0x1f;
155 *seq = AV_RB16(buf+1);
156 *timestamp = AV_RB32(buf+4);
157 if (!(buf[3] & 1) && (sn != rdt->prev_sn || *timestamp != rdt->prev_ts)) {
158 *flags |= PKT_FLAG_KEY;
159 rdt->prev_sn = sn;
160 rdt->prev_ts = *timestamp;
163 return consumed + 10;
166 /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
167 static int
168 rdt_parse_packet (RTPDemuxContext *s, AVPacket *pkt, uint32_t *timestamp,
169 const uint8_t *buf, int len, int flags)
171 rdt_data *rdt = s->dynamic_protocol_context;
172 int seq = 1, res;
173 ByteIOContext *pb = rdt->rmctx->pb;
174 RMContext *rm = rdt->rmctx->priv_data;
175 AVStream *st = s->st;
177 if (rm->audio_pkt_cnt == 0) {
178 int pos;
180 url_open_buf (&pb, buf, len, URL_RDONLY);
181 flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
182 rdt->rmctx->pb = pb;
183 res = ff_rm_parse_packet (rdt->rmctx, st, len, pkt,
184 &seq, &flags, timestamp);
185 pos = url_ftell(pb);
186 url_close_buf (pb);
187 if (res < 0)
188 return res;
189 if (rm->audio_pkt_cnt > 0 &&
190 st->codec->codec_id == CODEC_ID_AAC) {
191 memcpy (rdt->buffer, buf + pos, len - pos);
192 url_open_buf (&pb, rdt->buffer, len - pos, URL_RDONLY);
193 rdt->rmctx->pb = pb;
195 } else {
196 ff_rm_retrieve_cache (rdt->rmctx, st, pkt);
197 if (rm->audio_pkt_cnt == 0 &&
198 st->codec->codec_id == CODEC_ID_AAC)
199 url_close_buf (pb);
201 pkt->stream_index = st->index;
202 pkt->pts = *timestamp;
204 return rm->audio_pkt_cnt > 0;
208 ff_rdt_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
209 const uint8_t *buf, int len)
211 int seq, flags = 0;
212 uint32_t timestamp;
213 int rv= 0;
215 if (!buf) {
216 /* return the next packets, if any */
217 timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
218 rv= rdt_parse_packet(s, pkt, &timestamp, NULL, 0, flags);
219 return rv;
222 if (len < 12)
223 return -1;
224 rv = rdt_parse_header(s, buf, len, &seq, &timestamp, &flags);
225 if (rv < 0)
226 return rv;
227 buf += rv;
228 len -= rv;
229 s->seq = seq;
231 rv = rdt_parse_packet(s, pkt, &timestamp, buf, len, flags);
233 return rv;
236 void
237 ff_rdt_subscribe_rule (RTPDemuxContext *s, char *cmd, int size,
238 int stream_nr, int rule_nr)
240 rdt_data *rdt = s->dynamic_protocol_context;
242 av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",
243 stream_nr, rule_nr, stream_nr, rule_nr + 1);
245 rdt_load_mdpr(rdt, s->st, 0);
248 static unsigned char *
249 rdt_parse_b64buf (unsigned int *target_len, const char *p)
251 unsigned char *target;
252 int len = strlen(p);
253 if (*p == '\"') {
254 p++;
255 len -= 2; /* skip embracing " at start/end */
257 *target_len = len * 3 / 4;
258 target = av_mallocz(*target_len + FF_INPUT_BUFFER_PADDING_SIZE);
259 av_base64_decode(target, p, *target_len);
260 return target;
263 static int
264 rdt_parse_sdp_line (AVStream *stream, void *d, const char *line)
266 rdt_data *rdt = d;
267 const char *p = line;
269 if (av_strstart(p, "OpaqueData:buffer;", &p)) {
270 rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
271 } else if (av_strstart(p, "StartTime:integer;", &p))
272 stream->first_dts = atoi(p);
274 return 0;
277 static void *
278 rdt_new_extradata (void)
280 rdt_data *rdt = av_mallocz(sizeof(rdt_data));
282 av_open_input_stream(&rdt->rmctx, NULL, "", &rdt_demuxer, NULL);
283 rdt->prev_ts = -1;
284 rdt->prev_sn = -1;
286 return rdt;
289 static void
290 rdt_free_extradata (void *d)
292 rdt_data *rdt = d;
294 if (rdt->rmctx)
295 av_close_input_stream(rdt->rmctx);
296 av_freep(&rdt->mlti_data);
297 av_free(rdt);
300 #define RDT_HANDLER(n, s, t) \
301 static RTPDynamicProtocolHandler ff_rdt_ ## n ## _handler = { \
302 s, \
303 t, \
304 CODEC_ID_NONE, \
305 rdt_parse_sdp_line, \
306 rdt_new_extradata, \
307 rdt_free_extradata \
310 RDT_HANDLER(live_video, "x-pn-multirate-realvideo-live", CODEC_TYPE_VIDEO);
311 RDT_HANDLER(live_audio, "x-pn-multirate-realaudio-live", CODEC_TYPE_AUDIO);
312 RDT_HANDLER(video, "x-pn-realvideo", CODEC_TYPE_VIDEO);
313 RDT_HANDLER(audio, "x-pn-realaudio", CODEC_TYPE_AUDIO);
315 void av_register_rdt_dynamic_payload_handlers(void)
317 ff_register_dynamic_payload_handler(&ff_rdt_video_handler);
318 ff_register_dynamic_payload_handler(&ff_rdt_audio_handler);
319 ff_register_dynamic_payload_handler(&ff_rdt_live_video_handler);
320 ff_register_dynamic_payload_handler(&ff_rdt_live_audio_handler);