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