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