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