9922
|
1 /*
|
|
2 * This file was ported to MPlayer from xine CVS real.c,v 1.8 2003/03/30 17:11:50
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * Copyright (C) 2002 the xine project
|
|
7 *
|
|
8 * This file is part of xine, a free video player.
|
|
9 *
|
|
10 * xine is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * xine is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
23 *
|
|
24 *
|
|
25 * special functions for real streams.
|
|
26 * adopted from joschkas real tools.
|
|
27 *
|
|
28 */
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <string.h>
|
|
32
|
|
33 #include "real.h"
|
|
34 #include "asmrp.h"
|
|
35 #include "sdpplin.h"
|
|
36
|
|
37 /*
|
|
38 #define LOG
|
|
39 */
|
|
40
|
|
41 const unsigned char xor_table[] = {
|
|
42 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
|
|
43 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
|
|
44 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
|
|
45 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
|
|
46 0x10, 0x57, 0x05, 0x18, 0x54, 0x00, 0x00, 0x00 };
|
|
47
|
|
48
|
|
49 #define BE_32C(x,y) x[3]=(char)(y & 0xff);\
|
|
50 x[2]=(char)((y >> 8) & 0xff);\
|
|
51 x[1]=(char)((y >> 16) & 0xff);\
|
|
52 x[0]=(char)((y >> 24) & 0xff);
|
|
53
|
|
54 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
|
|
55
|
|
56 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
|
|
57 (((uint8_t*)(x))[1] << 16) | \
|
|
58 (((uint8_t*)(x))[2] << 8) | \
|
|
59 ((uint8_t*)(x))[3])
|
|
60
|
|
61 #define MAX(x,y) ((x>y) ? x : y)
|
|
62
|
|
63 #ifdef LOG
|
|
64 static void hexdump (const char *buf, int length) {
|
|
65
|
|
66 int i;
|
|
67
|
|
68 printf (" hexdump> ");
|
|
69 for (i = 0; i < length; i++) {
|
|
70 unsigned char c = buf[i];
|
|
71
|
|
72 printf ("%02x", c);
|
|
73
|
|
74 if ((i % 16) == 15)
|
|
75 printf ("\n ");
|
|
76
|
|
77 if ((i % 2) == 1)
|
|
78 printf (" ");
|
|
79
|
|
80 }
|
|
81 printf ("\n");
|
|
82 }
|
|
83 #endif
|
|
84
|
|
85
|
|
86 static void hash(char *field, char *param) {
|
|
87
|
|
88 uint32_t a, b, c, d;
|
|
89
|
|
90
|
|
91 /* fill variables */
|
|
92 memcpy(&a, field, sizeof(uint32_t));
|
|
93 memcpy(&b, &field[4], sizeof(uint32_t));
|
|
94 memcpy(&c, &field[8], sizeof(uint32_t));
|
|
95 memcpy(&d, &field[12], sizeof(uint32_t));
|
|
96
|
|
97 #ifdef LOG
|
|
98 printf("real: hash input: %x %x %x %x\n", a, b, c, d);
|
|
99 printf("real: hash parameter:\n");
|
|
100 hexdump(param, 64);
|
|
101 #endif
|
|
102
|
|
103 a = ((b & c) | (~b & d)) + *((uint32_t*)(param+0x00)) + a - 0x28955B88;
|
|
104 a = ((a << 0x07) | (a >> 0x19)) + b;
|
|
105 d = ((a & b) | (~a & c)) + *((uint32_t*)(param+0x04)) + d - 0x173848AA;
|
|
106 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
|
107 c = ((d & a) | (~d & b)) + *((uint32_t*)(param+0x08)) + c + 0x242070DB;
|
|
108 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
|
109 b = ((c & d) | (~c & a)) + *((uint32_t*)(param+0x0c)) + b - 0x3E423112;
|
|
110 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
|
111 a = ((b & c) | (~b & d)) + *((uint32_t*)(param+0x10)) + a - 0x0A83F051;
|
|
112 a = ((a << 0x07) | (a >> 0x19)) + b;
|
|
113 d = ((a & b) | (~a & c)) + *((uint32_t*)(param+0x14)) + d + 0x4787C62A;
|
|
114 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
|
115 c = ((d & a) | (~d & b)) + *((uint32_t*)(param+0x18)) + c - 0x57CFB9ED;
|
|
116 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
|
117 b = ((c & d) | (~c & a)) + *((uint32_t*)(param+0x1c)) + b - 0x02B96AFF;
|
|
118 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
|
119 a = ((b & c) | (~b & d)) + *((uint32_t*)(param+0x20)) + a + 0x698098D8;
|
|
120 a = ((a << 0x07) | (a >> 0x19)) + b;
|
|
121 d = ((a & b) | (~a & c)) + *((uint32_t*)(param+0x24)) + d - 0x74BB0851;
|
|
122 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
|
123 c = ((d & a) | (~d & b)) + *((uint32_t*)(param+0x28)) + c - 0x0000A44F;
|
|
124 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
|
125 b = ((c & d) | (~c & a)) + *((uint32_t*)(param+0x2C)) + b - 0x76A32842;
|
|
126 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
|
127 a = ((b & c) | (~b & d)) + *((uint32_t*)(param+0x30)) + a + 0x6B901122;
|
|
128 a = ((a << 0x07) | (a >> 0x19)) + b;
|
|
129 d = ((a & b) | (~a & c)) + *((uint32_t*)(param+0x34)) + d - 0x02678E6D;
|
|
130 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
|
131 c = ((d & a) | (~d & b)) + *((uint32_t*)(param+0x38)) + c - 0x5986BC72;
|
|
132 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
|
133 b = ((c & d) | (~c & a)) + *((uint32_t*)(param+0x3c)) + b + 0x49B40821;
|
|
134 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
|
135
|
|
136 a = ((b & d) | (~d & c)) + *((uint32_t*)(param+0x04)) + a - 0x09E1DA9E;
|
|
137 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
|
138 d = ((a & c) | (~c & b)) + *((uint32_t*)(param+0x18)) + d - 0x3FBF4CC0;
|
|
139 d = ((d << 0x09) | (d >> 0x17)) + a;
|
|
140 c = ((d & b) | (~b & a)) + *((uint32_t*)(param+0x2c)) + c + 0x265E5A51;
|
|
141 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
|
142 b = ((c & a) | (~a & d)) + *((uint32_t*)(param+0x00)) + b - 0x16493856;
|
|
143 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
|
144 a = ((b & d) | (~d & c)) + *((uint32_t*)(param+0x14)) + a - 0x29D0EFA3;
|
|
145 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
|
146 d = ((a & c) | (~c & b)) + *((uint32_t*)(param+0x28)) + d + 0x02441453;
|
|
147 d = ((d << 0x09) | (d >> 0x17)) + a;
|
|
148 c = ((d & b) | (~b & a)) + *((uint32_t*)(param+0x3c)) + c - 0x275E197F;
|
|
149 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
|
150 b = ((c & a) | (~a & d)) + *((uint32_t*)(param+0x10)) + b - 0x182C0438;
|
|
151 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
|
152 a = ((b & d) | (~d & c)) + *((uint32_t*)(param+0x24)) + a + 0x21E1CDE6;
|
|
153 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
|
154 d = ((a & c) | (~c & b)) + *((uint32_t*)(param+0x38)) + d - 0x3CC8F82A;
|
|
155 d = ((d << 0x09) | (d >> 0x17)) + a;
|
|
156 c = ((d & b) | (~b & a)) + *((uint32_t*)(param+0x0c)) + c - 0x0B2AF279;
|
|
157 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
|
158 b = ((c & a) | (~a & d)) + *((uint32_t*)(param+0x20)) + b + 0x455A14ED;
|
|
159 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
|
160 a = ((b & d) | (~d & c)) + *((uint32_t*)(param+0x34)) + a - 0x561C16FB;
|
|
161 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
|
162 d = ((a & c) | (~c & b)) + *((uint32_t*)(param+0x08)) + d - 0x03105C08;
|
|
163 d = ((d << 0x09) | (d >> 0x17)) + a;
|
|
164 c = ((d & b) | (~b & a)) + *((uint32_t*)(param+0x1c)) + c + 0x676F02D9;
|
|
165 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
|
166 b = ((c & a) | (~a & d)) + *((uint32_t*)(param+0x30)) + b - 0x72D5B376;
|
|
167 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
|
168
|
|
169 a = (b ^ c ^ d) + *((uint32_t*)(param+0x14)) + a - 0x0005C6BE;
|
|
170 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
|
171 d = (a ^ b ^ c) + *((uint32_t*)(param+0x20)) + d - 0x788E097F;
|
|
172 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
|
173 c = (d ^ a ^ b) + *((uint32_t*)(param+0x2c)) + c + 0x6D9D6122;
|
|
174 c = ((c << 0x10) | (c >> 0x10)) + d;
|
|
175 b = (c ^ d ^ a) + *((uint32_t*)(param+0x38)) + b - 0x021AC7F4;
|
|
176 b = ((b << 0x17) | (b >> 0x09)) + c;
|
|
177 a = (b ^ c ^ d) + *((uint32_t*)(param+0x04)) + a - 0x5B4115BC;
|
|
178 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
|
179 d = (a ^ b ^ c) + *((uint32_t*)(param+0x10)) + d + 0x4BDECFA9;
|
|
180 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
|
181 c = (d ^ a ^ b) + *((uint32_t*)(param+0x1c)) + c - 0x0944B4A0;
|
|
182 c = ((c << 0x10) | (c >> 0x10)) + d;
|
|
183 b = (c ^ d ^ a) + *((uint32_t*)(param+0x28)) + b - 0x41404390;
|
|
184 b = ((b << 0x17) | (b >> 0x09)) + c;
|
|
185 a = (b ^ c ^ d) + *((uint32_t*)(param+0x34)) + a + 0x289B7EC6;
|
|
186 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
|
187 d = (a ^ b ^ c) + *((uint32_t*)(param+0x00)) + d - 0x155ED806;
|
|
188 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
|
189 c = (d ^ a ^ b) + *((uint32_t*)(param+0x0c)) + c - 0x2B10CF7B;
|
|
190 c = ((c << 0x10) | (c >> 0x10)) + d;
|
|
191 b = (c ^ d ^ a) + *((uint32_t*)(param+0x18)) + b + 0x04881D05;
|
|
192 b = ((b << 0x17) | (b >> 0x09)) + c;
|
|
193 a = (b ^ c ^ d) + *((uint32_t*)(param+0x24)) + a - 0x262B2FC7;
|
|
194 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
|
195 d = (a ^ b ^ c) + *((uint32_t*)(param+0x30)) + d - 0x1924661B;
|
|
196 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
|
197 c = (d ^ a ^ b) + *((uint32_t*)(param+0x3c)) + c + 0x1fa27cf8;
|
|
198 c = ((c << 0x10) | (c >> 0x10)) + d;
|
|
199 b = (c ^ d ^ a) + *((uint32_t*)(param+0x08)) + b - 0x3B53A99B;
|
|
200 b = ((b << 0x17) | (b >> 0x09)) + c;
|
|
201
|
|
202 a = ((~d | b) ^ c) + *((uint32_t*)(param+0x00)) + a - 0x0BD6DDBC;
|
|
203 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
|
204 d = ((~c | a) ^ b) + *((uint32_t*)(param+0x1c)) + d + 0x432AFF97;
|
|
205 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
|
206 c = ((~b | d) ^ a) + *((uint32_t*)(param+0x38)) + c - 0x546BDC59;
|
|
207 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
|
208 b = ((~a | c) ^ d) + *((uint32_t*)(param+0x14)) + b - 0x036C5FC7;
|
|
209 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
|
210 a = ((~d | b) ^ c) + *((uint32_t*)(param+0x30)) + a + 0x655B59C3;
|
|
211 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
|
212 d = ((~c | a) ^ b) + *((uint32_t*)(param+0x0C)) + d - 0x70F3336E;
|
|
213 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
|
214 c = ((~b | d) ^ a) + *((uint32_t*)(param+0x28)) + c - 0x00100B83;
|
|
215 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
|
216 b = ((~a | c) ^ d) + *((uint32_t*)(param+0x04)) + b - 0x7A7BA22F;
|
|
217 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
|
218 a = ((~d | b) ^ c) + *((uint32_t*)(param+0x20)) + a + 0x6FA87E4F;
|
|
219 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
|
220 d = ((~c | a) ^ b) + *((uint32_t*)(param+0x3c)) + d - 0x01D31920;
|
|
221 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
|
222 c = ((~b | d) ^ a) + *((uint32_t*)(param+0x18)) + c - 0x5CFEBCEC;
|
|
223 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
|
224 b = ((~a | c) ^ d) + *((uint32_t*)(param+0x34)) + b + 0x4E0811A1;
|
|
225 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
|
226 a = ((~d | b) ^ c) + *((uint32_t*)(param+0x10)) + a - 0x08AC817E;
|
|
227 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
|
228 d = ((~c | a) ^ b) + *((uint32_t*)(param+0x2c)) + d - 0x42C50DCB;
|
|
229 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
|
230 c = ((~b | d) ^ a) + *((uint32_t*)(param+0x08)) + c + 0x2AD7D2BB;
|
|
231 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
|
232 b = ((~a | c) ^ d) + *((uint32_t*)(param+0x24)) + b - 0x14792C6F;
|
|
233 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
|
234
|
|
235 #ifdef LOG
|
|
236 printf("real: hash output: %x %x %x %x\n", a, b, c, d);
|
|
237 #endif
|
|
238
|
|
239 *((uint32_t *)(field+0)) += a;
|
|
240 *((uint32_t *)(field+4)) += b;
|
|
241 *((uint32_t *)(field+8)) += c;
|
|
242 *((uint32_t *)(field+12)) += d;
|
|
243 }
|
|
244
|
|
245 static void call_hash (char *key, char *challenge, int len) {
|
|
246
|
|
247 uint32_t *ptr1, *ptr2;
|
|
248 uint32_t a, b, c, d;
|
|
249
|
|
250 ptr1=(uint32_t*)(key+16);
|
|
251 ptr2=(uint32_t*)(key+20);
|
|
252
|
|
253 a = *ptr1;
|
|
254 b = (a >> 3) & 0x3f;
|
|
255 a += len * 8;
|
|
256 *ptr1 = a;
|
|
257
|
|
258 if (a < (len << 3))
|
|
259 {
|
|
260 #ifdef LOG
|
|
261 printf("not verified: (len << 3) > a true\n");
|
|
262 #endif
|
|
263 ptr2 += 4;
|
|
264 }
|
|
265
|
|
266 *ptr2 += (len >> 0x1d);
|
|
267 a = 64 - b;
|
|
268 c = 0;
|
|
269 if (a <= len)
|
|
270 {
|
|
271
|
|
272 memcpy(key+b+24, challenge, a);
|
|
273 hash(key, key+24);
|
|
274 c = a;
|
|
275 d = c + 0x3f;
|
|
276
|
|
277 while ( d < len ) {
|
|
278
|
|
279 #ifdef LOG
|
|
280 printf("not verified: while ( d < len )\n");
|
|
281 #endif
|
|
282 hash(key, challenge+d-0x3f);
|
|
283 d += 64;
|
|
284 c += 64;
|
|
285 }
|
|
286 b = 0;
|
|
287 }
|
|
288
|
|
289 memcpy(key+b+24, challenge+c, len-c);
|
|
290 }
|
|
291
|
|
292 static void calc_response (char *result, char *field) {
|
|
293
|
|
294 char buf1[128];
|
|
295 char buf2[128];
|
|
296 int i;
|
|
297
|
|
298 memset (buf1, 0, 64);
|
|
299 *buf1 = 128;
|
|
300
|
|
301 memcpy (buf2, field+16, 8);
|
|
302
|
|
303 i = ( *((uint32_t*)(buf2)) >> 3 ) & 0x3f;
|
|
304
|
|
305 if (i < 56) {
|
|
306 i = 56 - i;
|
|
307 } else {
|
|
308 #ifdef LOG
|
|
309 printf("not verified: ! (i < 56)\n");
|
|
310 #endif
|
|
311 i = 120 - i;
|
|
312 }
|
|
313
|
|
314 call_hash (field, buf1, i);
|
|
315 call_hash (field, buf2, 8);
|
|
316
|
|
317 memcpy (result, field, 16);
|
|
318
|
|
319 }
|
|
320
|
|
321
|
|
322 static void calc_response_string (char *result, char *challenge) {
|
|
323
|
|
324 char field[128];
|
|
325 char zres[20];
|
|
326 int i;
|
|
327
|
|
328 /* initialize our field */
|
|
329 BE_32C (field, 0x01234567);
|
|
330 BE_32C ((field+4), 0x89ABCDEF);
|
|
331 BE_32C ((field+8), 0xFEDCBA98);
|
|
332 BE_32C ((field+12), 0x76543210);
|
|
333 BE_32C ((field+16), 0x00000000);
|
|
334 BE_32C ((field+20), 0x00000000);
|
|
335
|
|
336 /* calculate response */
|
|
337 call_hash(field, challenge, 64);
|
|
338 calc_response(zres,field);
|
|
339
|
|
340 /* convert zres to ascii string */
|
|
341 for (i=0; i<16; i++ ) {
|
|
342 char a, b;
|
|
343
|
|
344 a = (zres[i] >> 4) & 15;
|
|
345 b = zres[i] & 15;
|
|
346
|
|
347 result[i*2] = ((a<10) ? (a+48) : (a+87)) & 255;
|
|
348 result[i*2+1] = ((b<10) ? (b+48) : (b+87)) & 255;
|
|
349 }
|
|
350 }
|
|
351
|
|
352 void real_calc_response_and_checksum (char *response, char *chksum, char *challenge) {
|
|
353
|
|
354 int ch_len, table_len, resp_len;
|
|
355 int i;
|
|
356 char *ptr;
|
|
357 char buf[128];
|
|
358
|
|
359 /* initialize return values */
|
|
360 memset(response, 0, 64);
|
|
361 memset(chksum, 0, 34);
|
|
362
|
|
363 /* initialize buffer */
|
|
364 memset(buf, 0, 128);
|
|
365 ptr=buf;
|
|
366 BE_32C(ptr, 0xa1e9149d);
|
|
367 ptr+=4;
|
|
368 BE_32C(ptr, 0x0e6b3b59);
|
|
369 ptr+=4;
|
|
370
|
|
371 /* some (length) checks */
|
|
372 if (challenge != NULL)
|
|
373 {
|
|
374 ch_len = strlen (challenge);
|
|
375
|
|
376 if (ch_len == 40) /* what a hack... */
|
|
377 {
|
|
378 challenge[32]=0;
|
|
379 ch_len=32;
|
|
380 }
|
|
381 if ( ch_len > 56 ) ch_len=56;
|
|
382
|
|
383 /* copy challenge to buf */
|
|
384 memcpy(ptr, challenge, ch_len);
|
|
385 }
|
|
386
|
|
387 if (xor_table != NULL)
|
|
388 {
|
|
389 table_len = strlen(xor_table);
|
|
390
|
|
391 if (table_len > 56) table_len=56;
|
|
392
|
|
393 /* xor challenge bytewise with xor_table */
|
|
394 for (i=0; i<table_len; i++)
|
|
395 ptr[i] = ptr[i] ^ xor_table[i];
|
|
396 }
|
|
397
|
|
398 calc_response_string (response, buf);
|
|
399
|
|
400 /* add tail */
|
|
401 resp_len = strlen (response);
|
|
402 strcpy (&response[resp_len], "01d0a8e3");
|
|
403
|
|
404 /* calculate checksum */
|
|
405 for (i=0; i<resp_len/4; i++)
|
|
406 chksum[i] = response[i*4];
|
|
407 }
|
|
408
|
|
409
|
|
410 /*
|
|
411 * takes a MLTI-Chunk and a rule number got from match_asm_rule,
|
|
412 * returns a pointer to selected data and number of bytes in that.
|
|
413 */
|
|
414
|
|
415 static int select_mlti_data(const char *mlti_chunk, int mlti_size, int selection, char *out) {
|
|
416
|
|
417 int numrules, codec, size;
|
|
418 int i;
|
|
419
|
|
420 /* MLTI chunk should begin with MLTI */
|
|
421
|
|
422 if ((mlti_chunk[0] != 'M')
|
|
423 ||(mlti_chunk[1] != 'L')
|
|
424 ||(mlti_chunk[2] != 'T')
|
|
425 ||(mlti_chunk[3] != 'I'))
|
|
426 {
|
|
427 #ifdef LOG
|
|
428 printf("libreal: MLTI tag not detected, copying data\n");
|
|
429 #endif
|
|
430 memcpy(out, mlti_chunk, mlti_size);
|
|
431 return mlti_size;
|
|
432 }
|
|
433
|
|
434 mlti_chunk+=4;
|
|
435
|
|
436 /* next 16 bits are the number of rules */
|
|
437 numrules=BE_16(mlti_chunk);
|
|
438 if (selection >= numrules) return 0;
|
|
439
|
|
440 /* now <numrules> indices of codecs follows */
|
|
441 /* we skip to selection */
|
|
442 mlti_chunk+=(selection+1)*2;
|
|
443
|
|
444 /* get our index */
|
|
445 codec=BE_16(mlti_chunk);
|
|
446
|
|
447 /* skip to number of codecs */
|
|
448 mlti_chunk+=(numrules-selection)*2;
|
|
449
|
|
450 /* get number of codecs */
|
|
451 numrules=BE_16(mlti_chunk);
|
|
452
|
|
453 if (codec >= numrules) {
|
|
454 printf("codec index >= number of codecs. %i %i\n", codec, numrules);
|
|
455 return 0;
|
|
456 }
|
|
457
|
|
458 mlti_chunk+=2;
|
|
459
|
|
460 /* now seek to selected codec */
|
|
461 for (i=0; i<codec; i++) {
|
|
462 size=BE_32(mlti_chunk);
|
|
463 mlti_chunk+=size+4;
|
|
464 }
|
|
465
|
|
466 size=BE_32(mlti_chunk);
|
|
467
|
|
468 #ifdef LOG
|
|
469 hexdump(mlti_chunk+4, size);
|
|
470 #endif
|
|
471 memcpy(out,mlti_chunk+4, size);
|
|
472 return size;
|
|
473 }
|
|
474
|
|
475 /*
|
|
476 * looking at stream description.
|
|
477 */
|
|
478
|
|
479 rmff_header_t *real_parse_sdp(char *data, char *stream_rules, uint32_t bandwidth) {
|
|
480
|
|
481 sdpplin_t *desc;
|
|
482 rmff_header_t *header;
|
|
483 char buf[2048];
|
|
484 int len, i;
|
|
485 int max_bit_rate=0;
|
|
486 int avg_bit_rate=0;
|
|
487 int max_packet_size=0;
|
|
488 int avg_packet_size=0;
|
|
489 int duration=0;
|
|
490
|
|
491
|
|
492 if (!data) return NULL;
|
|
493
|
|
494 desc=sdpplin_parse(data);
|
|
495
|
|
496 if (!desc) return NULL;
|
|
497
|
|
498 header=calloc(1,sizeof(rmff_header_t));
|
|
499
|
|
500 header->fileheader=rmff_new_fileheader(4+desc->stream_count);
|
|
501 header->cont=rmff_new_cont(
|
|
502 desc->title,
|
|
503 desc->author,
|
|
504 desc->copyright,
|
|
505 desc->abstract);
|
|
506 header->data=rmff_new_dataheader(0,0);
|
|
507 header->streams=calloc(1,sizeof(rmff_mdpr_t*)*(desc->stream_count+1));
|
|
508 #ifdef LOG
|
|
509 printf("number of streams: %u\n", desc->stream_count);
|
|
510 #endif
|
|
511
|
|
512 for (i=0; i<desc->stream_count; i++) {
|
|
513
|
|
514 int j=0;
|
|
515 int n;
|
|
516 char b[64];
|
|
517 int rulematches[16];
|
|
518
|
|
519 #ifdef LOG
|
|
520 printf("calling asmrp_match with:\n%s\n%u\n", desc->stream[i]->asm_rule_book, bandwidth);
|
|
521 #endif
|
|
522 n=asmrp_match(desc->stream[i]->asm_rule_book, bandwidth, rulematches);
|
|
523 for (j=0; j<n; j++) {
|
|
524 #ifdef LOG
|
|
525 printf("asmrp rule match: %u for stream %u\n", rulematches[j], desc->stream[i]->stream_id);
|
|
526 #endif
|
|
527 sprintf(b,"stream=%u;rule=%u,", desc->stream[i]->stream_id, rulematches[j]);
|
|
528 strcat(stream_rules, b);
|
|
529 }
|
|
530
|
|
531 if (!desc->stream[i]->mlti_data) return NULL;
|
|
532
|
|
533 len=select_mlti_data(desc->stream[i]->mlti_data, desc->stream[i]->mlti_data_size, rulematches[0], buf);
|
|
534
|
|
535 header->streams[i]=rmff_new_mdpr(
|
|
536 desc->stream[i]->stream_id,
|
|
537 desc->stream[i]->max_bit_rate,
|
|
538 desc->stream[i]->avg_bit_rate,
|
|
539 desc->stream[i]->max_packet_size,
|
|
540 desc->stream[i]->avg_packet_size,
|
|
541 desc->stream[i]->start_time,
|
|
542 desc->stream[i]->preroll,
|
|
543 desc->stream[i]->duration,
|
|
544 desc->stream[i]->stream_name,
|
|
545 desc->stream[i]->mime_type,
|
|
546 len,
|
|
547 buf);
|
|
548
|
|
549 duration=MAX(duration,desc->stream[i]->duration);
|
|
550 max_bit_rate+=desc->stream[i]->max_bit_rate;
|
|
551 avg_bit_rate+=desc->stream[i]->avg_bit_rate;
|
|
552 max_packet_size=MAX(max_packet_size, desc->stream[i]->max_packet_size);
|
|
553 if (avg_packet_size)
|
|
554 avg_packet_size=(avg_packet_size + desc->stream[i]->avg_packet_size) / 2;
|
|
555 else
|
|
556 avg_packet_size=desc->stream[i]->avg_packet_size;
|
|
557 }
|
|
558
|
|
559 if (stream_rules)
|
|
560 stream_rules[strlen(stream_rules)-1]=0; /* delete last ',' in stream_rules */
|
|
561
|
|
562 header->prop=rmff_new_prop(
|
|
563 max_bit_rate,
|
|
564 avg_bit_rate,
|
|
565 max_packet_size,
|
|
566 avg_packet_size,
|
|
567 0,
|
|
568 duration,
|
|
569 0,
|
|
570 0,
|
|
571 0,
|
|
572 desc->stream_count,
|
|
573 desc->flags);
|
|
574
|
|
575 rmff_fix_header(header);
|
|
576
|
|
577 return header;
|
|
578 }
|
|
579
|
|
580 int real_get_rdt_chunk(rtsp_t *rtsp_session, char *buffer) {
|
|
581
|
|
582 int n=1;
|
|
583 uint8_t header[8];
|
|
584 rmff_pheader_t ph;
|
|
585 int size;
|
|
586 int flags1;
|
|
587 int unknown1;
|
|
588 uint32_t ts;
|
|
589
|
|
590 n=rtsp_read_data(rtsp_session, header, 8);
|
|
591 if (n<8) return 0;
|
|
592 if (header[0] != 0x24)
|
|
593 {
|
|
594 printf("rdt chunk not recognized: got 0x%02x\n", header[0]);
|
|
595 return 0;
|
|
596 }
|
|
597 size=(header[1]<<12)+(header[2]<<8)+(header[3]);
|
|
598 flags1=header[4];
|
|
599 if ((flags1!=0x40)&&(flags1!=0x42))
|
|
600 {
|
|
601 #ifdef LOG
|
|
602 printf("got flags1: 0x%02x\n",flags1);
|
|
603 #endif
|
|
604 header[0]=header[5];
|
|
605 header[1]=header[6];
|
|
606 header[2]=header[7];
|
|
607 n=rtsp_read_data(rtsp_session, header+3, 5);
|
|
608 if (n<5) return 0;
|
|
609 #ifdef LOG
|
|
610 printf("ignoring bytes:\n");
|
|
611 hexdump(header, 8);
|
|
612 #endif
|
|
613 n=rtsp_read_data(rtsp_session, header+4, 4);
|
|
614 if (n<4) return 0;
|
|
615 flags1=header[4];
|
|
616 size-=9;
|
|
617 }
|
|
618 unknown1=(header[5]<<12)+(header[6]<<8)+(header[7]);
|
|
619 n=rtsp_read_data(rtsp_session, header, 6);
|
|
620 if (n<6) return 0;
|
|
621 ts=BE_32(header);
|
|
622
|
|
623 #ifdef LOG
|
|
624 printf("ts: %u size: %u, flags: 0x%02x, unknown values: %u 0x%02x 0x%02x\n",
|
|
625 ts, size, flags1, unknown1, header[4], header[5]);
|
|
626 #endif
|
|
627 size+=2;
|
|
628
|
|
629 ph.object_version=0;
|
|
630 ph.length=size;
|
|
631 ph.stream_number=(flags1>>1)&1;
|
|
632 ph.timestamp=ts;
|
|
633 ph.reserved=0;
|
|
634 ph.flags=0; /* TODO: determine keyframe flag and insert here? */
|
|
635 rmff_dump_pheader(&ph, buffer);
|
|
636 size-=12;
|
|
637 n=rtsp_read_data(rtsp_session, buffer+12, size);
|
|
638
|
|
639 return n+12;
|
|
640 }
|
|
641
|
|
642 rmff_header_t *real_setup_and_get_header(rtsp_t *rtsp_session, uint32_t bandwidth) {
|
|
643
|
|
644 char *description=NULL;
|
|
645 char *session_id=NULL;
|
|
646 rmff_header_t *h;
|
|
647 char *challenge1;
|
|
648 char challenge2[64];
|
|
649 char checksum[34];
|
|
650 char subscribe[256];
|
|
651 char buf[256];
|
|
652 char *mrl=rtsp_get_mrl(rtsp_session);
|
|
653 unsigned int size;
|
|
654 int status;
|
|
655
|
|
656 /* get challenge */
|
|
657 challenge1=strdup(rtsp_search_answers(rtsp_session,"RealChallenge1"));
|
|
658 #ifdef LOG
|
|
659 printf("real: Challenge1: %s\n", challenge1);
|
|
660 #endif
|
|
661
|
|
662 /* request stream description */
|
|
663 rtsp_schedule_field(rtsp_session, "Accept: application/sdp");
|
|
664 sprintf(buf, "Bandwidth: %u", bandwidth);
|
|
665 rtsp_schedule_field(rtsp_session, buf);
|
|
666 rtsp_schedule_field(rtsp_session, "GUID: 00000000-0000-0000-0000-000000000000");
|
|
667 rtsp_schedule_field(rtsp_session, "RegionData: 0");
|
|
668 rtsp_schedule_field(rtsp_session, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
|
|
669 rtsp_schedule_field(rtsp_session, "SupportsMaximumASMBandwidth: 1");
|
|
670 rtsp_schedule_field(rtsp_session, "Language: en-US");
|
|
671 rtsp_schedule_field(rtsp_session, "Require: com.real.retain-entity-for-setup");
|
|
672 status=rtsp_request_describe(rtsp_session,NULL);
|
|
673
|
|
674 if ( status<200 || status>299 )
|
|
675 {
|
|
676 char *alert=rtsp_search_answers(rtsp_session,"Alert");
|
|
677 if (alert) {
|
|
678 printf("real: got message from server:\n%s\n", alert);
|
|
679 }
|
|
680 rtsp_send_ok(rtsp_session);
|
|
681 return NULL;
|
|
682 }
|
|
683
|
|
684 /* receive description */
|
|
685 size=0;
|
|
686 if (!rtsp_search_answers(rtsp_session,"Content-length"))
|
|
687 printf("real: got no Content-length!\n");
|
|
688 else
|
|
689 size=atoi(rtsp_search_answers(rtsp_session,"Content-length"));
|
|
690
|
|
691 if (!rtsp_search_answers(rtsp_session,"ETag"))
|
|
692 printf("real: got no ETag!\n");
|
|
693 else
|
|
694 session_id=strdup(rtsp_search_answers(rtsp_session,"ETag"));
|
|
695
|
|
696 #ifdef LOG
|
|
697 printf("real: Stream description size: %i\n", size);
|
|
698 #endif
|
|
699
|
|
700 description=malloc(sizeof(char)*(size+1));
|
|
701
|
|
702 rtsp_read_data(rtsp_session, description, size);
|
|
703 description[size]=0;
|
|
704
|
|
705 /* parse sdp (sdpplin) and create a header and a subscribe string */
|
|
706 strcpy(subscribe, "Subscribe: ");
|
|
707 h=real_parse_sdp(description, subscribe+11, bandwidth);
|
|
708 if (!h) return NULL;
|
|
709 rmff_fix_header(h);
|
|
710
|
|
711 #ifdef LOG
|
|
712 printf("Title: %s\nCopyright: %s\nAuthor: %s\nStreams: %i\n",
|
|
713 h->cont->title, h->cont->copyright, h->cont->author, h->prop->num_streams);
|
|
714 #endif
|
|
715
|
|
716 /* setup our streams */
|
|
717 real_calc_response_and_checksum (challenge2, checksum, challenge1);
|
|
718 sprintf(buf, "RealChallenge2: %s, sd=%s", challenge2, checksum);
|
|
719 rtsp_schedule_field(rtsp_session, buf);
|
|
720 sprintf(buf, "If-Match: %s", session_id);
|
|
721 rtsp_schedule_field(rtsp_session, buf);
|
|
722 rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
|
|
723 sprintf(buf, "%s/streamid=0", mrl);
|
|
724 rtsp_request_setup(rtsp_session,buf);
|
|
725
|
|
726 if (h->prop->num_streams > 1) {
|
|
727 rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
|
|
728 sprintf(buf, "If-Match: %s", session_id);
|
|
729 rtsp_schedule_field(rtsp_session, buf);
|
|
730
|
|
731 sprintf(buf, "%s/streamid=1", mrl);
|
|
732 rtsp_request_setup(rtsp_session,buf);
|
|
733 }
|
|
734 /* set stream parameter (bandwidth) with our subscribe string */
|
|
735 rtsp_schedule_field(rtsp_session, subscribe);
|
|
736 rtsp_request_setparameter(rtsp_session,NULL);
|
|
737
|
|
738 /* and finally send a play request */
|
|
739 rtsp_schedule_field(rtsp_session, "Range: npt=0-");
|
|
740 rtsp_request_play(rtsp_session,NULL);
|
|
741
|
|
742 return h;
|
|
743 }
|
|
744
|