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