* mms: reduce mtu (avoid too much delay with slow connection)
[vlc.git] / modules / access / mms / asf.c
1 /*****************************************************************************
2  * asf.c: MMS access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: asf.c,v 1.1 2003/04/20 19:29:43 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <stdlib.h>
24
25 #include <vlc/vlc.h>
26
27 #include "asf.h"
28 #include "buffer.h"
29
30
31 static int CmpGuid( const guid_t *p_guid1, const guid_t *p_guid2 )
32 {
33     return( ( p_guid1->v1 == p_guid2->v1 &&
34               p_guid1->v2 == p_guid2->v2 &&
35               p_guid1->v3 == p_guid2->v3 &&
36               p_guid1->v4[0] == p_guid2->v4[0] &&
37               p_guid1->v4[1] == p_guid2->v4[1] &&
38               p_guid1->v4[2] == p_guid2->v4[2] &&
39               p_guid1->v4[3] == p_guid2->v4[3] &&
40               p_guid1->v4[4] == p_guid2->v4[4] &&
41               p_guid1->v4[5] == p_guid2->v4[5] &&
42               p_guid1->v4[6] == p_guid2->v4[6] &&
43               p_guid1->v4[7] == p_guid2->v4[7] ) ? 1 : 0 );
44 }
45
46 void E_( GenerateGuid )( guid_t *p_guid )
47 {
48     int i;
49
50     srand( mdate() & 0xffffffff );
51
52     /* FIXME should be generated using random data */
53     p_guid->v1 = 0xbabac001;
54     p_guid->v2 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
55     p_guid->v3 = ( (uint64_t)rand() << 16 ) / RAND_MAX;
56     for( i = 0; i < 8; i++ )
57     {
58         p_guid->v4[i] = ( (uint64_t)rand() * 256 ) / RAND_MAX;
59     }
60 }
61
62 void E_( asf_HeaderParse )  ( asf_header_t *hdr,
63                               uint8_t *p_header, int i_header )
64 {
65     var_buffer_t buffer;
66     guid_t      guid;
67     uint64_t    i_size;
68     int         i;
69
70     for( i = 0; i < 128; i++ )
71     {
72         hdr->stream[i].i_cat = ASF_STREAM_UNKNOWN;
73     }
74
75     //fprintf( stderr, " ---------------------header:%d\n", i_header );
76     var_buffer_initread( &buffer, p_header, i_header );
77
78     var_buffer_getguid( &buffer, &guid );
79
80     if( !CmpGuid( &guid, &asf_object_header_guid ) )
81     {
82 //        XXX Error
83 //        fprintf( stderr, " ---------------------ERROR------\n" );
84     }
85     var_buffer_getmemory( &buffer, NULL, 30 - 16 );
86
87     for( ;; )
88     {
89         //fprintf( stderr, " ---------------------data:%d\n", buffer.i_data );
90
91         var_buffer_getguid( &buffer, &guid );
92         i_size = var_buffer_get64( &buffer );
93
94         //fprintf( stderr, "  guid=0x%8.8x-0x%4.4x-0x%4.4x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x size=%lld\n",
95         //                  guid.v1,guid.v2, guid.v3,
96         //                  guid.v4[0],guid.v4[1],guid.v4[2],guid.v4[3],
97         //                  guid.v4[4],guid.v4[5],guid.v4[6],guid.v4[7],
98         //                  i_size );
99
100         if( CmpGuid( &guid, &asf_object_file_properties_guid ) )
101         {
102             var_buffer_getmemory( &buffer, NULL, 16 );
103             hdr->i_file_size            = var_buffer_get64( &buffer );
104             var_buffer_getmemory( &buffer, NULL, 8 );
105             hdr->i_data_packets_count   = var_buffer_get64( &buffer );
106             var_buffer_getmemory( &buffer, NULL, 8+8+8+4);
107             hdr->i_min_data_packet_size = var_buffer_get32( &buffer );
108
109             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 16 - 8 - 8 - 8 - 8-8-8-4 - 4);
110         }
111         else if( CmpGuid( &guid, &asf_object_stream_properties_guid ) )
112         {
113             int     i_stream_id;
114             guid_t  stream_type;
115             //msg_Dbg( p_input, "found stream_properties" );
116
117             var_buffer_getguid( &buffer, &stream_type );
118             var_buffer_getmemory( &buffer, NULL, 32 );
119             i_stream_id = var_buffer_get8( &buffer ) & 0x7f;
120
121             //fprintf( stderr, " 1---------------------skip:%lld\n", i_size - 24 - 32 - 16 - 1 );
122             var_buffer_getmemory( &buffer, NULL, i_size - 24 - 32 - 16 - 1);
123
124             if( CmpGuid( &stream_type, &asf_object_stream_type_video ) )
125             {
126                 //fprintf( stderr, "\nvideo stream[%d] found\n", i_stream_id );
127                 //msg_Dbg( p_input, "video stream[%d] found", i_stream_id );
128                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_VIDEO;
129             }
130             else if( CmpGuid( &stream_type, &asf_object_stream_type_audio ) )
131             {
132                 //fprintf( stderr, "\naudio stream[%d] found\n", i_stream_id );
133                 //msg_Dbg( p_input, "audio stream[%d] found", i_stream_id );
134                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_AUDIO;
135             }
136             else
137             {
138 //                msg_Dbg( p_input, "unknown stream[%d] found", i_stream_id );
139                 hdr->stream[i_stream_id].i_cat = ASF_STREAM_UNKNOWN;
140             }
141         }
142         else if ( CmpGuid( &guid, &asf_object_bitrate_properties_guid ) )
143         {
144             int     i_count;
145             uint8_t i_stream_id;
146
147             i_count = var_buffer_get16( &buffer );
148             i_size -= 2;
149             while( i_count > 0 )
150             {
151                 i_stream_id = var_buffer_get16( &buffer )&0x7f;
152                 hdr->stream[i_stream_id].i_bitrate =  var_buffer_get32( &buffer );
153                 i_count--;
154                 i_size -= 6;
155             }
156             //fprintf( stderr, " 2---------------------skip:%lld\n", i_size - 24);
157             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
158         }
159         else
160         {
161             //fprintf( stderr, " 3---------------------skip:%lld\n", i_size - 24);
162             // skip unknown guid
163             var_buffer_getmemory( &buffer, NULL, i_size - 24 );
164         }
165
166         if( var_buffer_readempty( &buffer ) )
167         {
168             return;
169         }
170     }
171 }
172
173 void E_( asf_StreamSelect ) ( asf_header_t *hdr,
174                               int i_bitrate_max,
175                               vlc_bool_t b_all, vlc_bool_t b_audio, vlc_bool_t b_video )
176 {
177     /* XXX FIXME use mututal eclusion information */
178     int i;
179     int i_audio, i_video;
180     int i_bitrate_total;
181     char *psz_stream;
182
183     i_audio = 0;
184     i_video = 0;
185     i_bitrate_total = 0;
186     if( b_all )
187     {
188         /* select all valid stream */
189         for( i = 1; i < 128; i++ )
190         {
191             if( hdr->stream[i].i_cat != ASF_STREAM_UNKNOWN )
192             {
193                 hdr->stream[i].i_selected = 1;
194             }
195         }
196         return;
197     }
198     else
199     {
200         for( i = 0; i < 128; i++ )
201         {
202             hdr->stream[i].i_selected = 0; /* by default, not selected */
203         }
204     }
205
206 #if 0
207     psz_stream = config_GetPsz( p_input, "mms-stream" );
208
209     if( psz_stream && *psz_stream )
210     {
211         char *psz_tmp = psz_stream;
212         while( *psz_tmp )
213         {
214             if( *psz_tmp == ',' )
215             {
216                 psz_tmp++;
217             }
218             else
219             {
220                 int i_stream;
221                 i_stream = atoi( psz_tmp );
222                 while( *psz_tmp != '\0' && *psz_tmp != ',' )
223                 {
224                     psz_tmp++;
225                 }
226
227                 if( i_stream > 0 && i_stream < 128 &&
228                     stream[i_stream].i_cat != MMS_STREAM_UNKNOWN )
229                 {
230                     stream[i_stream].i_selected = 1;
231                 }
232             }
233         }
234         FREE( psz_stream );
235         return;
236     }
237     FREE( psz_stream );
238 #endif
239
240     /* big test:
241      * select a stream if
242      *    - no audio nor video stream
243      *    - or:
244      *         - if i_bitrate_max not set keep the highest bitrate
245      *         - if i_bitrate_max is set, keep stream that make we used best
246      *           quality regarding i_bitrate_max
247      *
248      * XXX: little buggy:
249      *        - it doesn't use mutual exclusion info..
250      *        - when selecting a better stream we could select
251      *        something that make i_bitrate_total> i_bitrate_max
252      */
253     for( i = 1; i < 128; i++ )
254     {
255         if( hdr->stream[i].i_cat == ASF_STREAM_UNKNOWN )
256         {
257             continue;
258         }
259         else if( hdr->stream[i].i_cat == ASF_STREAM_AUDIO && b_audio &&
260                  ( i_audio <= 0 ||
261                     ( ( ( hdr->stream[i].i_bitrate > hdr->stream[i_audio].i_bitrate &&
262                           ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_audio].i_bitrate
263                                             < i_bitrate_max || !i_bitrate_max) ) ||
264                         ( hdr->stream[i].i_bitrate < hdr->stream[i_audio].i_bitrate &&
265                               i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
266                       ) )  ) )
267         {
268             /* unselect old stream */
269             if( i_audio > 0 )
270             {
271                 hdr->stream[i_audio].i_selected = 0;
272                 if( hdr->stream[i_audio].i_bitrate> 0 )
273                 {
274                     i_bitrate_total -= hdr->stream[i_audio].i_bitrate;
275                 }
276             }
277
278             hdr->stream[i].i_selected = 1;
279             if( hdr->stream[i].i_bitrate> 0 )
280             {
281                 i_bitrate_total += hdr->stream[i].i_bitrate;
282             }
283             i_audio = i;
284         }
285         else if( hdr->stream[i].i_cat == ASF_STREAM_VIDEO && b_video &&
286                  ( i_video <= 0 ||
287                     (
288                         ( ( hdr->stream[i].i_bitrate > hdr->stream[i_video].i_bitrate &&
289                             ( i_bitrate_total + hdr->stream[i].i_bitrate - hdr->stream[i_video].i_bitrate
290                                             < i_bitrate_max || !i_bitrate_max) ) ||
291                           ( hdr->stream[i].i_bitrate < hdr->stream[i_video].i_bitrate &&
292                             i_bitrate_max != 0 && i_bitrate_total > i_bitrate_max )
293                         ) ) )  )
294         {
295             /* unselect old stream */
296
297             if( i_video > 0 )
298             {
299                 hdr->stream[i_video].i_selected = 0;
300                 if( hdr->stream[i_video].i_bitrate> 0 )
301                 {
302                     i_bitrate_total -= hdr->stream[i_video].i_bitrate;
303                 }
304             }
305
306             hdr->stream[i].i_selected = 1;
307             if( hdr->stream[i].i_bitrate> 0 )
308             {
309                 i_bitrate_total += hdr->stream[i].i_bitrate;
310             }
311             i_video = i;
312         }
313
314     }
315 #if 0
316     if( i_bitrate_max > 0 )
317     {
318         msg_Dbg( p_input,
319                  "requested bitrate:%d real bitrate:%d",
320                  i_bitrate_max, i_bitrate_total );
321     }
322     else
323     {
324         msg_Dbg( p_input,
325                  "total bitrate:%d",
326                  i_bitrate_total );
327     }
328 #endif
329 }
330