]> git.sesse.net Git - vlc/blob - modules/access/mms/mmstu.c
* ALL: WinCE build fixes.
[vlc] / modules / access / mms / mmstu.c
1 /*****************************************************************************
2  * mms.c: MMS access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
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
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <vlc/vlc.h>
30 #include <string.h>
31 #include <vlc/input.h>
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 #   include <unistd.h>
36 #endif
37 #ifdef HAVE_FCNTL_H
38 #   include <fcntl.h>
39 #endif
40 #ifdef HAVE_SYS_TIME_H
41 #   include <sys/time.h>
42 #endif
43 #ifdef HAVE_SYS_TYPES_H 
44 #   include <sys/types.h>
45 #endif
46 #ifdef HAVE_SYS_STAT_H
47 #   include <sys/stat.h>
48 #endif
49
50 #if defined( UNDER_CE )
51 #   include <winsock.h>
52 #elif WIN32
53 #   include <winsock2.h>
54 #   include <ws2tcpip.h>
55 #   ifndef IN_MULTICAST
56 #       define IN_MULTICAST(a) IN_CLASSD(a)
57 #   endif
58 #else
59 #   include <sys/socket.h>
60 #   include <netinet/in.h>
61 #   if HAVE_ARPA_INET_H
62 #      include <arpa/inet.h>
63 #   elif defined( SYS_BEOS )
64 #      include <net/netdb.h>
65 #   endif
66 #endif
67
68 #include "network.h"
69 #include "asf.h"
70 #include "buffer.h"
71
72 #include "mms.h"
73 #include "mmstu.h"
74
75 #undef MMS_DEBUG
76
77 /****************************************************************************
78  * NOTES:
79  *  MMSProtocole documentation found at http://get.to/sdp
80  ****************************************************************************/
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 int  E_( MMSTUOpen )  ( access_t * );
86 void E_( MMSTUClose ) ( access_t * );
87
88
89 static int Read( access_t *, uint8_t *, int );
90 static int Seek( access_t *, int64_t );
91 static int Control( access_t *, int, va_list );
92
93 static int  MMSOpen ( access_t *, vlc_url_t *, int );
94 static int  MMSStart( access_t *, uint32_t );
95 static int  MMSStop ( access_t * );
96 static void MMSClose( access_t * );
97
98
99 static int  mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
100 static int  mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
101
102 static int  mms_HeaderMediaRead( access_t *, int );
103
104 static int  mms_ReceivePacket( access_t * );
105
106
107 /*
108  * XXX DON'T FREE MY MEMORY !!! XXX
109  * non mais :P
110  */
111 /*
112  * Ok, ok, j'le ferai plus...
113  */
114 /*
115  * Merci :))
116  */
117 /*
118  * Vous pourriez signer vos commentaires (même si on voit bien qui peut
119  * écrire ce genre de trucs :p), et écrire en anglais, bordel de
120  * merde :p.
121  */
122 /*
123  * Alors la ouai ç'est fou les gens qui écrivent des commentaires sans les
124  * signer. Ca mériterait un coup de pied dans le cul ça :)
125  */
126
127 int  E_(MMSTUOpen)( access_t *p_access )
128 {
129     access_sys_t   *p_sys;
130     int             i_proto;
131     int             i_status;
132
133     /* Set up p_access */
134     p_access->pf_read = Read;
135     p_access->pf_block = NULL;
136     p_access->pf_control = Control;
137     p_access->pf_seek = Seek;
138     p_access->info.i_update = 0;
139     p_access->info.i_size = 0;
140     p_access->info.i_pos = 0;
141     p_access->info.b_eof = VLC_FALSE;
142     p_access->info.i_title = 0;
143     p_access->info.i_seekpoint = 0;
144     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
145     memset( p_sys, 0, sizeof( access_sys_t ) );
146
147     /* *** Parse URL and get server addr/port and path *** */
148     vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
149     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
150     {
151         msg_Err( p_access, "invalid server name" );
152         vlc_UrlClean( &p_sys->url );
153         return VLC_EGENERIC;
154     }
155     if( p_sys->url.i_port <= 0 )
156     {
157         p_sys->url.i_port = 1755;
158     }
159
160     /* *** connect to this server *** */
161     /* look at  requested protocol (udp/tcp) */
162     i_proto = MMS_PROTO_AUTO;
163     if( *p_access->psz_access )
164     {
165         if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
166         {
167             i_proto = MMS_PROTO_UDP;
168         }
169         else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
170         {
171             i_proto = MMS_PROTO_TCP;
172         }
173     }
174
175     /* connect */
176     if( i_proto == MMS_PROTO_AUTO )
177     {   /* first try with TCP and then UDP*/
178         if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
179         {
180             i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
181         }
182     }
183     else
184     {
185         i_status = MMSOpen( p_access, &p_sys->url, i_proto );
186     }
187
188     if( i_status )
189     {
190         msg_Err( p_access, "cannot connect to server" );
191         vlc_UrlClean( &p_sys->url );
192         return VLC_EGENERIC;
193     }
194
195     msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
196     /*
197      * i_flags_broadcast
198      *  yy xx ?? ??
199      *  broadcast    yy=0x02, xx= 0x00
200      *  pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
201      */
202     if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
203     {
204         p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
205     }
206     if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
207     {
208         p_sys->b_seekable = VLC_FALSE;
209     }
210     else
211     {
212         p_sys->b_seekable = VLC_TRUE;
213         p_access->info.i_size =
214             (uint64_t)p_sys->i_header +
215             (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
216     }
217
218     /* *** Start stream *** */
219     if( MMSStart( p_access, 0xffffffff ) < 0 )
220     {
221         msg_Err( p_access, "cannot start stream" );
222         MMSClose( p_access );
223         vlc_UrlClean( &p_sys->url );
224         return VLC_EGENERIC;
225     }
226     return VLC_SUCCESS;
227 }
228
229 /*****************************************************************************
230  * Close: free unused data structures
231  *****************************************************************************/
232 void E_(MMSTUClose)( access_t *p_access )
233 {
234     access_sys_t *p_sys = p_access->p_sys;
235
236     /* close connection with server */
237     MMSClose( p_access );
238
239     /* free memory */
240     vlc_UrlClean( &p_sys->url );
241
242     free( p_sys );
243 }
244
245 /*****************************************************************************
246  * Control:
247  *****************************************************************************/
248 static int Control( access_t *p_access, int i_query, va_list args )
249 {
250     access_sys_t *p_sys = p_access->p_sys;
251     vlc_bool_t   *pb_bool;
252     int          *pi_int;
253     int64_t      *pi_64;
254     int           i_int;
255     vlc_value_t  val;
256
257     switch( i_query )
258     {
259         /* */
260         case ACCESS_CAN_SEEK:
261             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
262             *pb_bool = p_sys->b_seekable;
263             break;
264
265         case ACCESS_CAN_FASTSEEK:
266         case ACCESS_CAN_PAUSE:
267             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
268             *pb_bool = VLC_FALSE;
269             break;
270
271         case ACCESS_CAN_CONTROL_PACE:
272             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
273
274 #if 0       /* Disable for now until we have a clock synchro algo
275              * which works with something else than MPEG over UDP */
276             *pb_bool = VLC_FALSE;
277 #endif
278             *pb_bool = VLC_TRUE;
279             break;
280
281         /* */
282         case ACCESS_GET_MTU:
283             pi_int = (int*)va_arg( args, int * );
284             *pi_int = 3 * p_sys->i_packet_length;
285             break;
286
287         case ACCESS_GET_PTS_DELAY:
288             pi_64 = (int64_t*)va_arg( args, int64_t * );
289             var_Get( p_access, "mms-caching", &val );
290             *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
291             break;
292
293         case ACCESS_GET_PRIVATE_ID_STATE:
294             i_int = (int)va_arg( args, int );
295             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t );
296
297             if( i_int < 0 || i_int > 127 )
298                 return VLC_EGENERIC;
299             *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
300             break;
301
302         /* */
303         case ACCESS_SET_PAUSE_STATE:
304         case ACCESS_GET_TITLE_INFO:
305         case ACCESS_SET_TITLE:
306         case ACCESS_SET_SEEKPOINT:
307         case ACCESS_SET_PRIVATE_ID_STATE:
308             return VLC_EGENERIC;
309
310
311         default:
312             msg_Warn( p_access, "unimplemented query in control" );
313             return VLC_EGENERIC;
314
315     }
316     return VLC_SUCCESS;
317 }
318
319 /*****************************************************************************
320  * Seek: try to go at the right place
321  *****************************************************************************/
322 static int Seek( access_t * p_access, int64_t i_pos )
323 {
324     access_sys_t *p_sys = p_access->p_sys;
325     uint32_t    i_packet;
326     uint32_t    i_offset;
327     var_buffer_t buffer;
328
329     if( i_pos < 0 )
330         return VLC_EGENERIC;
331
332     if( i_pos < p_sys->i_header)
333     {
334
335         if( p_access->info.i_pos < p_sys->i_header )
336         {
337             /* no need to restart stream, it was already one
338              * or no stream was yet read */
339             p_access->info.i_pos = i_pos;
340             return VLC_SUCCESS;
341         }
342         else
343         {
344             i_packet = 0xffffffff;
345             i_offset = 0;
346         }
347     }
348     else
349     {
350         i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
351         i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
352     }
353     msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
354
355     MMSStop( p_access );
356     msg_Dbg( p_access, "stream stopped (seek)" );
357
358     /* *** restart stream *** */
359     var_buffer_initwrite( &buffer, 0 );
360     var_buffer_add64( &buffer, 0 ); /* seek point in second */
361     var_buffer_add32( &buffer, 0xffffffff );
362     var_buffer_add32( &buffer, i_packet ); // begin from start
363     var_buffer_add8( &buffer, 0xff ); // stream time limit
364     var_buffer_add8( &buffer, 0xff ); //  on 3bytes ...
365     var_buffer_add8( &buffer, 0xff ); //
366     var_buffer_add8( &buffer, 0x00 ); // don't use limit
367     var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
368
369     mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
370                      buffer.p_data, buffer.i_data );
371
372     var_buffer_free( &buffer );
373
374
375     while( !p_access->b_die )
376     {
377         mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
378         if( p_sys->i_command == 0x1e )
379         {
380             msg_Dbg( p_access, "received 0x1e (seek)" );
381             break;
382         }
383     }
384
385     while( !p_access->b_die )
386     {
387         mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
388         if( p_sys->i_command == 0x05 )
389         {
390             msg_Dbg( p_access, "received 0x05 (seek)" );
391             break;
392         }
393     }
394
395     /* get a packet */
396     mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
397     msg_Dbg( p_access, "Streaming restarted" );
398
399     p_sys->i_media_used += i_offset;
400     p_access->info.i_pos = i_pos;
401     p_access->info.b_eof = VLC_FALSE;
402
403     return VLC_SUCCESS;
404 }
405
406 /*****************************************************************************
407  * Read:
408  *****************************************************************************/
409 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
410 {
411     access_sys_t *p_sys = p_access->p_sys;
412     size_t      i_data;
413     size_t      i_copy;
414
415     i_data = 0;
416
417     /* *** now send data if needed *** */
418     while( i_data < i_len )
419     {
420         if( p_access->info.i_pos < p_sys->i_header )
421         {
422             i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
423             memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
424             i_data += i_copy;
425             p_access->info.i_pos += i_copy;
426         }
427         else if( p_sys->i_media_used < p_sys->i_media )
428         {
429             i_copy = __MIN( i_len - i_data ,
430                             p_sys->i_media - p_sys->i_media_used );
431             memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
432             i_data += i_copy;
433             p_sys->i_media_used += i_copy;
434             p_access->info.i_pos += i_copy;
435         }
436         else if( p_sys->p_media != NULL &&
437                  p_sys->i_media_used < p_sys->i_packet_length )
438         {
439             i_copy = __MIN( i_len - i_data,
440                             p_sys->i_packet_length - p_sys->i_media_used);
441             memset( &p_buffer[i_data], 0, i_copy );
442
443             i_data += i_copy;
444             p_sys->i_media_used += i_copy;
445             p_access->info.i_pos += i_copy;
446         }
447         else if( p_access->info.b_eof ||
448                  mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
449         {
450             break;
451         }
452     }
453
454     return i_data;
455 }
456
457 /****************************************************************************
458  * MMSOpen : Open a connection with the server over mmst or mmsu
459  ****************************************************************************/
460 static int MMSOpen( access_t  *p_access, vlc_url_t *p_url, int  i_proto )
461 {
462     access_sys_t *p_sys = p_access->p_sys;
463     int           b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
464
465     var_buffer_t buffer;
466     char         tmp[4096];
467     uint16_t     *p;
468     int          i_server_version;
469     int          i_tool_version;
470     int          i_update_player_url;
471     int          i_encryption_type;
472     int          i;
473     int          i_streams;
474     int          i_first;
475
476
477     /* *** Open a TCP connection with server *** */
478     msg_Dbg( p_access, "waiting for connection..." );
479     p_sys->i_handle_tcp = net_OpenTCP( p_access, p_url->psz_host, p_url->i_port );
480     if( p_sys->i_handle_tcp < 0 )
481     {
482         msg_Err( p_access, "failed to open a connection (tcp)" );
483         return VLC_EGENERIC;
484     }
485     msg_Dbg( p_access,
486              "connection(tcp) with \"%s:%d\" successful",
487              p_url->psz_host,
488              p_url->i_port );
489
490     /* *** Bind port if UDP protocol is selected *** */
491     if( b_udp )
492     {
493         struct sockaddr_storage name;
494         socklen_t i_namelen = sizeof( name );
495
496         if( getsockname( p_sys->i_handle_tcp,
497                          (struct sockaddr*)&name, &i_namelen ) < 0 )
498         {
499             net_Close( p_sys->i_handle_tcp );
500             return VLC_EGENERIC;
501         }
502
503         /* FIXME: not thread-safe for IPv4 */
504         /* FIXME: not sure if it works fine for IPv6 */
505         if( name.ss_family == AF_INET )
506             p_sys->psz_bind_addr = inet_ntoa( ((struct sockaddr_in *)&name)->sin_addr );
507         else
508             p_sys->psz_bind_addr = p_url->psz_host;
509
510         p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->psz_bind_addr, 7000, "", 0 );
511         if( p_sys->i_handle_udp < 0 )
512         {
513             msg_Err( p_access, "failed to open a connection (udp)" );
514             net_Close( p_sys->i_handle_tcp );
515             return VLC_EGENERIC;
516         }
517         msg_Dbg( p_access,
518                  "connection(udp) at \"%s:%d\" successful",
519                  p_sys->psz_bind_addr, 7000 );
520     }
521     else
522     {
523         p_sys->psz_bind_addr = NULL;
524     }
525
526     /* *** Init context for mms prototcol *** */
527     E_( GenerateGuid )( &p_sys->guid );    /* used to identify client by server */
528     msg_Dbg( p_access,
529              "generated guid: "GUID_FMT,
530              GUID_PRINT( p_sys->guid ) );
531     p_sys->i_command_level = 1;          /* updated after 0x1A command */
532     p_sys->i_seq_num = 0;
533     p_sys->i_media_packet_id_type  = 0x04;
534     p_sys->i_header_packet_id_type = 0x02;
535     p_sys->i_proto = i_proto;
536     p_sys->i_packet_seq_num = 0;
537     p_sys->p_header = NULL;
538     p_sys->i_header = 0;
539     p_sys->p_media = NULL;
540     p_sys->i_media = 0;
541     p_sys->i_media_used = 0;
542
543     p_access->info.i_pos = 0;
544     p_sys->i_buffer_tcp = 0;
545     p_sys->i_buffer_udp = 0;
546     p_sys->p_cmd = NULL;
547     p_sys->i_cmd = 0;
548     p_access->info.b_eof = 0;
549
550     /* *** send command 1 : connection request *** */
551     var_buffer_initwrite( &buffer, 0 );
552     var_buffer_add16( &buffer, 0x001c );
553     var_buffer_add16( &buffer, 0x0003 );
554     sprintf( tmp,
555              "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
556              GUID_PRINT( p_sys->guid ),
557              p_url->psz_host );
558     var_buffer_addUTF16( &buffer, tmp );
559
560     mms_CommandSend( p_access,
561                      0x01,          /* connexion request */
562                      0x00000000,    /* flags, FIXME */
563                      0x0004000b,    /* ???? */
564                      buffer.p_data,
565                      buffer.i_data );
566
567     if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
568     {
569         var_buffer_free( &buffer );
570         MMSClose( p_access );
571         return VLC_EGENERIC;
572     }
573
574     i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
575     i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
576     i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
577     i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
578     p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
579 #define GETUTF16( psz, size ) \
580     { \
581         int i; \
582         psz = malloc( size + 1); \
583         for( i = 0; i < size; i++ ) \
584         { \
585             psz[i] = p[i]; \
586         } \
587         psz[size] = '\0'; \
588         p += 2 * ( size ); \
589     }
590     GETUTF16( p_sys->psz_server_version, i_server_version );
591     GETUTF16( p_sys->psz_tool_version, i_tool_version );
592     GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
593     GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
594 #undef GETUTF16
595     msg_Dbg( p_access,
596              "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
597              p_sys->psz_server_version,
598              p_sys->psz_tool_version,
599              p_sys->psz_update_player_url,
600              p_sys->psz_encryption_type );
601
602     /* *** should make an 18 command to make data timing *** */
603
604     /* *** send command 2 : transport protocol selection *** */
605     var_buffer_reinitwrite( &buffer, 0 );
606     var_buffer_add32( &buffer, 0x00000000 );
607     var_buffer_add32( &buffer, 0x000a0000 );
608     var_buffer_add32( &buffer, 0x00000002 );
609     if( b_udp )
610     {
611         sprintf( tmp,
612                  "\\\\%s\\UDP\\%d",
613                  p_sys->psz_bind_addr,
614                  7000 ); // FIXME
615     }
616     else
617     {
618         sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242"  );
619     }
620     var_buffer_addUTF16( &buffer, tmp );
621     var_buffer_add16( &buffer, '0' );
622
623     mms_CommandSend( p_access,
624                      0x02,          /* connexion request */
625                      0x00000000,    /* flags, FIXME */
626                      0xffffffff,    /* ???? */
627                      buffer.p_data,
628                      buffer.i_data );
629
630     /* *** response from server, should be 0x02 or 0x03 *** */
631     mms_CommandRead( p_access, 0x02, 0x03 );
632     if( p_sys->i_command == 0x03 )
633     {
634         msg_Err( p_access,
635                  "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
636         var_buffer_free( &buffer );
637         MMSClose( p_access );
638         return VLC_EGENERIC;
639     }
640     else if( p_sys->i_command != 0x02 )
641     {
642         msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
643     }
644
645     /* *** send command 5 : media file name/path requested *** */
646     var_buffer_reinitwrite( &buffer, 0 );
647     var_buffer_add64( &buffer, 0 );
648     var_buffer_addUTF16( &buffer, p_url->psz_path );
649
650     mms_CommandSend( p_access,
651                      0x05,
652                      p_sys->i_command_level,
653                      0xffffffff,
654                      buffer.p_data,
655                      buffer.i_data );
656
657     /* *** wait for reponse *** */
658     mms_CommandRead( p_access, 0x1a, 0x06 );
659
660     /* test if server send 0x1A answer */
661     if( p_sys->i_command == 0x1A )
662     {
663         msg_Err( p_access, "id/password requested (not yet supported)" );
664         /*  FIXME */
665         var_buffer_free( &buffer );
666         MMSClose( p_access );
667         return VLC_EGENERIC;
668     }
669     if( p_sys->i_command != 0x06 )
670     {
671         msg_Err( p_access,
672                  "unknown answer (0x%x instead of 0x06)",
673                  p_sys->i_command );
674         var_buffer_free( &buffer );
675         MMSClose( p_access );
676         return( -1 );
677     }
678
679     /*  1 for file ok, 2 for authen ok */
680     switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
681     {
682         case 0x0001:
683             msg_Dbg( p_access, "Media file name/path accepted" );
684             break;
685         case 0x0002:
686             msg_Dbg( p_access, "Authentication accepted" );
687             break;
688         case -1:
689         default:
690         msg_Err( p_access, "error while asking for file %d",
691                  GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
692         var_buffer_free( &buffer );
693         MMSClose( p_access );
694         return VLC_EGENERIC;
695     }
696
697     p_sys->i_flags_broadcast =
698         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
699     p_sys->i_media_length =
700         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
701     p_sys->i_packet_length =
702         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
703     p_sys->i_packet_count =
704         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
705     p_sys->i_max_bit_rate =
706         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
707     p_sys->i_header_size =
708         GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
709
710     msg_Dbg( p_access,
711              "answer 0x06 flags:0x%8.8x media_length:%ds packet_length:%d packet_count:%d max_bit_rate:%d header_size:%d",
712              p_sys->i_flags_broadcast,
713              p_sys->i_media_length,
714              p_sys->i_packet_length,
715              p_sys->i_packet_count,
716              p_sys->i_max_bit_rate,
717              p_sys->i_header_size );
718
719     /* *** send command 15 *** */
720
721     var_buffer_reinitwrite( &buffer, 0 );
722     var_buffer_add32( &buffer, 0 );
723     var_buffer_add32( &buffer, 0x8000 );
724     var_buffer_add32( &buffer, 0xffffffff );
725     var_buffer_add32( &buffer, 0x00 );
726     var_buffer_add32( &buffer, 0x00 );
727     var_buffer_add32( &buffer, 0x00 );
728     var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
729     var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
730     var_buffer_add32( &buffer, 0x00 );
731     mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
732                      buffer.p_data, buffer.i_data );
733
734     /* *** wait for reponse *** */
735     /* Commented out because it fails on some stream (no 0x11 answer) */
736 #if 0
737     mms_CommandRead( p_access, 0x11, 0 );
738
739     if( p_sys->i_command != 0x11 )
740     {
741         msg_Err( p_access,
742                  "unknown answer (0x%x instead of 0x11)",
743                  p_sys->i_command );
744         var_buffer_free( &buffer );
745         MMSClose( p_access );
746         return( -1 );
747     }
748 #endif
749
750     /* *** now read header packet *** */
751     /* XXX could be split over multiples packets */
752     msg_Dbg( p_access, "reading header" );
753     for( ;; )
754     {
755         if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
756         {
757             msg_Err( p_access, "cannot receive header" );
758             var_buffer_free( &buffer );
759             MMSClose( p_access );
760             return VLC_EGENERIC;
761         }
762         if( p_sys->i_header >= p_sys->i_header_size )
763         {
764             msg_Dbg( p_access,
765                      "header complete(%d)",
766                      p_sys->i_header );
767             break;
768         }
769         msg_Dbg( p_access,
770                  "header incomplete (%d/%d), reading more",
771                  p_sys->i_header,
772                  p_sys->i_header_size );
773     }
774
775     /* *** parse header and get stream and their id *** */
776     /* get all streams properties,
777      *
778      * TODO : stream bitrates properties(optional)
779      *        and bitrate mutual exclusion(optional) */
780     E_( asf_HeaderParse )( &p_sys->asfh,
781                            p_sys->p_header, p_sys->i_header );
782     E_( asf_StreamSelect)( &p_sys->asfh,
783                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
784                            var_CreateGetInteger( p_access, "mms-all" ),
785                            var_CreateGetInteger( p_access, "audio" ),
786                            var_CreateGetInteger( p_access, "video" ) );
787
788     /* *** now select stream we want to receive *** */
789     /* TODO take care of stream bitrate TODO */
790     i_streams = 0;
791     i_first = -1;
792     var_buffer_reinitwrite( &buffer, 0 );
793     /* for now, select first audio and video stream */
794     for( i = 1; i < 128; i++ )
795     {
796
797         if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
798         {
799             i_streams++;
800             if( i_first != -1 )
801             {
802                 var_buffer_add16( &buffer, 0xffff );
803                 var_buffer_add16( &buffer, i );
804             }
805             else
806             {
807                 i_first = i;
808             }
809             if( p_sys->asfh.stream[i].i_selected )
810             {
811                 var_buffer_add16( &buffer, 0x0000 );
812                 msg_Info( p_access,
813                           "selecting stream[0x%x] %s (%d kb/s)",
814                           i,
815                           ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO  ) ?
816                                                   "audio" : "video" ,
817                           p_sys->asfh.stream[i].i_bitrate / 1024);
818             }
819             else
820             {
821                 var_buffer_add16( &buffer, 0x0002 );
822                 msg_Info( p_access,
823                           "ignoring stream[0x%x] %s (%d kb/s)",
824                           i,
825                           ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO  ) ?
826                                     "audio" : "video" ,
827                           p_sys->asfh.stream[i].i_bitrate / 1024);
828
829             }
830         }
831     }
832
833     if( i_streams == 0 )
834     {
835         msg_Err( p_access, "cannot find any stream" );
836         var_buffer_free( &buffer );
837         MMSClose( p_access );
838         return VLC_EGENERIC;
839     }
840     mms_CommandSend( p_access, 0x33,
841                      i_streams,
842                      0xffff | ( i_first << 16 ),
843                      buffer.p_data, buffer.i_data );
844
845     mms_CommandRead( p_access, 0x21, 0 );
846     if( p_sys->i_command != 0x21 )
847     {
848         msg_Err( p_access,
849                  "unknown answer (0x%x instead of 0x21)",
850                  p_sys->i_command );
851         var_buffer_free( &buffer );
852         MMSClose( p_access );
853         return VLC_EGENERIC;
854     }
855
856
857     var_buffer_free( &buffer );
858
859     msg_Info( p_access, "connection sucessful" );
860
861     return VLC_SUCCESS;
862 }
863
864 /****************************************************************************
865  * MMSStart : Start streaming
866  ****************************************************************************/
867 static int MMSStart( access_t  *p_access, uint32_t i_packet )
868 {
869     access_sys_t        *p_sys = p_access->p_sys;
870     var_buffer_t    buffer;
871
872     /* *** start stream from packet 0 *** */
873     var_buffer_initwrite( &buffer, 0 );
874     var_buffer_add64( &buffer, 0 ); /* seek point in second */
875     var_buffer_add32( &buffer, 0xffffffff );
876     var_buffer_add32( &buffer, i_packet ); // begin from start
877     var_buffer_add8( &buffer, 0xff ); // stream time limit
878     var_buffer_add8( &buffer, 0xff ); //  on 3bytes ...
879     var_buffer_add8( &buffer, 0xff ); //
880     var_buffer_add8( &buffer, 0x00 ); // don't use limit
881     var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
882
883     mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
884                      buffer.p_data, buffer.i_data );
885
886     var_buffer_free( &buffer );
887
888     mms_CommandRead( p_access, 0x05, 0 );
889
890     if( p_sys->i_command != 0x05 )
891     {
892         msg_Err( p_access,
893                  "unknown answer (0x%x instead of 0x05)",
894                  p_sys->i_command );
895         return( -1 );
896     }
897     else
898     {
899         /* get a packet */
900         mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
901         msg_Dbg( p_access, "Streaming started" );
902         return( 0 );
903     }
904 }
905
906 /****************************************************************************
907  * MMSStop : Stop streaming
908  ****************************************************************************/
909 static int MMSStop( access_t  *p_access )
910 {
911     access_sys_t *p_sys = p_access->p_sys;
912
913     /* *** stop stream but keep connection alive *** */
914     mms_CommandSend( p_access,
915                      0x09,
916                      p_sys->i_command_level,
917                      0x001fffff,
918                      NULL, 0 );
919     return( 0 );
920 }
921
922 /****************************************************************************
923  * MMSClose : Close streaming and connection
924  ****************************************************************************/
925 static void MMSClose( access_t  *p_access )
926 {
927     access_sys_t        *p_sys = p_access->p_sys;
928
929     msg_Dbg( p_access, "Connection closed" );
930
931     /* *** tell server that we will disconnect *** */
932     mms_CommandSend( p_access,
933                      0x0d,
934                      p_sys->i_command_level,
935                      0x00000001,
936                      NULL, 0 );
937
938     /* *** close sockets *** */
939     net_Close( p_sys->i_handle_tcp );
940     if( p_sys->i_proto == MMS_PROTO_UDP )
941     {
942         net_Close( p_sys->i_handle_udp );
943     }
944
945     FREE( p_sys->p_cmd );
946     FREE( p_sys->p_media );
947     FREE( p_sys->p_header );
948
949     FREE( p_sys->psz_server_version );
950     FREE( p_sys->psz_tool_version );
951     FREE( p_sys->psz_update_player_url );
952     FREE( p_sys->psz_encryption_type );
953 }
954
955 /****************************************************************************
956  *
957  * MMS specific functions
958  *
959  ****************************************************************************/
960 static int mms_CommandSend( access_t *p_access,
961                              int i_command,
962                              uint32_t i_prefix1, uint32_t i_prefix2,
963                              uint8_t *p_data, int i_data )
964 {
965     var_buffer_t buffer;
966
967     access_sys_t        *p_sys = p_access->p_sys;
968     int i_data_by8;
969
970     i_data_by8 = ( i_data + 7 ) / 8;
971
972     /* first init uffer */
973     var_buffer_initwrite( &buffer, 0 );
974
975     var_buffer_add32( &buffer, 0x00000001 );    /* start sequence */
976     var_buffer_add32( &buffer, 0xB00BFACE );
977     /* size after protocol type */
978     var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
979     var_buffer_add32( &buffer, 0x20534d4d );    /* protocol "MMS " */
980     var_buffer_add32( &buffer, i_data_by8 + 4 );
981     var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
982     var_buffer_add64( &buffer, 0 );
983     var_buffer_add32( &buffer, i_data_by8 + 2 );
984     var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
985     var_buffer_add32( &buffer, i_prefix1 );    /* command specific */
986     var_buffer_add32( &buffer, i_prefix2 );    /* command specific */
987
988     /* specific command data */
989     if( p_data && i_data > 0 )
990     {
991         var_buffer_addmemory( &buffer, p_data, i_data );
992     }
993
994     /* send it */
995     if( send( p_sys->i_handle_tcp,
996               buffer.p_data,
997               buffer.i_data,
998               0 ) == -1 )
999     {
1000         msg_Err( p_access, "failed to send command" );
1001         return VLC_EGENERIC;
1002     }
1003
1004     var_buffer_free( &buffer );
1005     return VLC_SUCCESS;
1006 }
1007
1008 static int NetFillBuffer( access_t *p_access )
1009 {
1010 #ifdef UNDER_CE
1011     return -1;
1012
1013 #else
1014     access_sys_t    *p_sys = p_access->p_sys;
1015     struct timeval  timeout;
1016     fd_set          fds_r, fds_e;
1017     int             i_ret;
1018
1019     /* FIXME when using udp */
1020     ssize_t i_tcp, i_udp;
1021     ssize_t i_tcp_read, i_udp_read;
1022     int i_handle_max;
1023     int i_try = 0;
1024
1025     i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1026
1027     if( p_sys->i_proto == MMS_PROTO_UDP )
1028     {
1029         i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1030     }
1031     else
1032     {
1033         i_udp = 0;  /* there isn't udp socket */
1034     }
1035
1036     i_handle_max = 0;
1037
1038     if( i_tcp > 0 )
1039         i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1040     if( i_udp > 0 )
1041         i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1042
1043     if( i_handle_max == 0 )
1044     {
1045         msg_Warn( p_access, "nothing to read %d:%d", i_tcp, i_udp );
1046         return 0;
1047     }
1048     else
1049     {
1050         /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1051     }
1052
1053     /* Find if some data is available */
1054     do
1055     {
1056         i_try++;
1057
1058         /* Initialize file descriptor set */
1059         FD_ZERO( &fds_r );
1060         FD_ZERO( &fds_e );
1061
1062         if( i_tcp > 0 )
1063         {
1064             FD_SET( p_sys->i_handle_tcp, &fds_r );
1065             FD_SET( p_sys->i_handle_tcp, &fds_e );
1066         }
1067         if( i_udp > 0 )
1068         {
1069             FD_SET( p_sys->i_handle_udp, &fds_r );
1070             FD_SET( p_sys->i_handle_udp, &fds_e );
1071         }
1072
1073         /* We'll wait 0.5 second if nothing happens */
1074         timeout.tv_sec = 0;
1075         timeout.tv_usec = 500000;
1076
1077         if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1078         {
1079             return -1;
1080         }
1081
1082         if( p_access->b_die || p_access->b_error ) return -1;
1083
1084         //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1085
1086     } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1087              (i_ret < 0 && errno == EINTR) );
1088
1089     if( i_ret < 0 )
1090     {
1091         msg_Err( p_access, "network select error (%s)", strerror(errno) );
1092         return -1;
1093     }
1094
1095     i_tcp_read = i_udp_read = 0;
1096
1097     if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1098     {
1099         i_tcp_read =
1100             recv( p_sys->i_handle_tcp,
1101                   p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1102                   i_tcp + MMS_BUFFER_SIZE/2, 0 );
1103     }
1104
1105     if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1106     {
1107         i_udp_read = recv( p_sys->i_handle_udp,
1108                            p_sys->buffer_udp + p_sys->i_buffer_udp,
1109                            i_udp + MMS_BUFFER_SIZE/2, 0 );
1110     }
1111
1112 #if MMS_DEBUG
1113     if( p_sys->i_proto == MMS_PROTO_UDP )
1114     {
1115         msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1116                  p_sys->i_buffer_tcp, i_tcp_read,
1117                  p_sys->i_buffer_udp, i_udp_read );
1118     }
1119     else
1120     {
1121         msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1122                  p_sys->i_buffer_tcp, i_tcp_read );
1123     }
1124 #endif
1125
1126     if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1127     if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1128
1129     return i_tcp_read + i_udp_read;
1130 #endif
1131 }
1132
1133 static int  mms_ParseCommand( access_t *p_access,
1134                               uint8_t *p_data,
1135                               int i_data,
1136                               int *pi_used )
1137 {
1138  #define GET32( i_pos ) \
1139     ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1140       ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1141       ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1142
1143     access_sys_t        *p_sys = p_access->p_sys;
1144     int         i_length;
1145     uint32_t    i_id;
1146
1147     if( p_sys->p_cmd )
1148     {
1149         free( p_sys->p_cmd );
1150     }
1151     p_sys->i_cmd = i_data;
1152     p_sys->p_cmd = malloc( i_data );
1153     memcpy( p_sys->p_cmd, p_data, i_data );
1154
1155     *pi_used = i_data; /* by default */
1156
1157     if( i_data < MMS_CMD_HEADERSIZE )
1158     {
1159         msg_Warn( p_access, "truncated command (header incomplete)" );
1160         p_sys->i_command = 0;
1161         return -1;
1162     }
1163     i_id =  GetDWLE( p_data + 4 );
1164     i_length = GetDWLE( p_data + 8 ) + 16;
1165
1166     if( i_id != 0xb00bface )
1167     {
1168         msg_Err( p_access,
1169                  "incorrect command header (0x%x)", i_id );
1170         p_sys->i_command = 0;
1171         return -1;
1172     }
1173
1174     if( i_length > p_sys->i_cmd )
1175     {
1176         msg_Warn( p_access,
1177                   "truncated command (missing %d bytes)",
1178                    i_length - i_data  );
1179         p_sys->i_command = 0;
1180         return -1;
1181     }
1182     else if( i_length < p_sys->i_cmd )
1183     {
1184         p_sys->i_cmd = i_length;
1185         *pi_used = i_length;
1186     }
1187
1188     msg_Dbg( p_access,
1189              "recv command start_sequence:0x%8.8x command_id:0x%8.8x length:%d len8:%d sequence 0x%8.8x len8_II:%d dir_comm:0x%8.8x",
1190              GET32( 0 ),
1191              GET32( 4 ),
1192              GET32( 8 ),
1193              /* 12: protocol type "MMS " */
1194              GET32( 16 ),
1195              GET32( 20 ),
1196              /* 24: unknown (0) */
1197              /* 28: unknown (0) */
1198              GET32( 32 ),
1199              GET32( 36 )
1200              /* 40: switches */
1201              /* 44: extra */ );
1202
1203     p_sys->i_command = GET32( 36 ) & 0xffff;
1204 #undef GET32
1205
1206     return MMS_PACKET_CMD;
1207 }
1208
1209 static int  mms_ParsePacket( access_t *p_access,
1210                              uint8_t *p_data, size_t i_data,
1211                              int *pi_used )
1212 {
1213     access_sys_t        *p_sys = p_access->p_sys;
1214     int i_packet_seq_num;
1215     size_t i_packet_length;
1216     uint32_t i_packet_id;
1217
1218     uint8_t  *p_packet;
1219
1220
1221     *pi_used = i_data; /* default */
1222     if( i_data <= 8 )
1223     {
1224         msg_Warn( p_access, "truncated packet (header incomplete)" );
1225         return -1;
1226     }
1227
1228     i_packet_id = p_data[4];
1229     i_packet_seq_num = GetDWLE( p_data );
1230     i_packet_length = GetWLE( p_data + 6 );
1231
1232     //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1233
1234     if( i_packet_length > i_data || i_packet_length <= 8)
1235     {
1236      /*   msg_Dbg( p_access,
1237                  "truncated packet (Declared %d bytes, Actual %d bytes)",
1238                  i_packet_length, i_data  ); */
1239         *pi_used = 0;
1240         return -1;
1241     }
1242     else if( i_packet_length < i_data )
1243     {
1244         *pi_used = i_packet_length;
1245     }
1246
1247     if( i_packet_id == 0xff )
1248     {
1249         msg_Warn( p_access,
1250                   "receive MMS UDP pair timing" );
1251         return( MMS_PACKET_UDP_TIMING );
1252     }
1253
1254     if( i_packet_id != p_sys->i_header_packet_id_type &&
1255         i_packet_id != p_sys->i_media_packet_id_type )
1256     {
1257         msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1258         return -1;
1259     }
1260
1261     /* we now have a media or a header packet */
1262     p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1263     memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1264
1265     if( i_packet_seq_num != p_sys->i_packet_seq_num )
1266     {
1267         /* FIXME for udp could be just wrong order ? */
1268         msg_Warn( p_access,
1269                   "detected packet lost (%d != %d)",
1270                   i_packet_seq_num,
1271                   p_sys->i_packet_seq_num );
1272         p_sys->i_packet_seq_num = i_packet_seq_num;
1273     }
1274     p_sys->i_packet_seq_num++;
1275
1276     if( i_packet_id == p_sys->i_header_packet_id_type )
1277     {
1278         if( p_sys->p_header )
1279         {
1280             p_sys->p_header = realloc( p_sys->p_header,
1281                                           p_sys->i_header + i_packet_length - 8 );
1282             memcpy( &p_sys->p_header[p_sys->i_header],
1283                     p_packet,
1284                     i_packet_length - 8 );
1285             p_sys->i_header += i_packet_length - 8;
1286
1287             free( p_packet );
1288         }
1289         else
1290         {
1291             p_sys->p_header = p_packet;
1292             p_sys->i_header = i_packet_length - 8;
1293         }
1294 /*        msg_Dbg( p_access,
1295                  "receive header packet (%d bytes)",
1296                  i_packet_length - 8 ); */
1297
1298         return MMS_PACKET_HEADER;
1299     }
1300     else
1301     {
1302         FREE( p_sys->p_media );
1303         p_sys->p_media = p_packet;
1304         p_sys->i_media = i_packet_length - 8;
1305         p_sys->i_media_used = 0;
1306 /*        msg_Dbg( p_access,
1307                  "receive media packet (%d bytes)",
1308                  i_packet_length - 8 ); */
1309
1310         return MMS_PACKET_MEDIA;
1311     }
1312 }
1313
1314 static int mms_ReceivePacket( access_t *p_access )
1315 {
1316     access_sys_t *p_sys = p_access->p_sys;
1317     int i_packet_tcp_type;
1318     int i_packet_udp_type;
1319
1320     for( ;; )
1321     {
1322         vlc_bool_t b_refill = VLC_TRUE;
1323
1324         /* first if we need to refill buffer */
1325         if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1326         {
1327             if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface  )
1328             {
1329                 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1330                     p_sys->i_buffer_tcp )
1331                 {
1332                     b_refill = VLC_FALSE;
1333                 }
1334             }
1335             else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1336             {
1337                 b_refill = VLC_FALSE;
1338             }
1339         }
1340         if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1341             GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1342         {
1343             b_refill = VLC_FALSE;
1344         }
1345
1346         if( b_refill && NetFillBuffer( p_access ) < 0 )
1347         {
1348             msg_Warn( p_access, "cannot fill buffer" );
1349             return -1;
1350         }
1351
1352         i_packet_tcp_type = -1;
1353         i_packet_udp_type = -1;
1354
1355         if( p_sys->i_buffer_tcp > 0 )
1356         {
1357             int i_used;
1358
1359             if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1360             {
1361                 i_packet_tcp_type =
1362                     mms_ParseCommand( p_access, p_sys->buffer_tcp,
1363                                       p_sys->i_buffer_tcp, &i_used );
1364
1365             }
1366             else
1367             {
1368                 i_packet_tcp_type =
1369                     mms_ParsePacket( p_access, p_sys->buffer_tcp,
1370                                      p_sys->i_buffer_tcp, &i_used );
1371             }
1372             if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1373             {
1374                 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1375                          MMS_BUFFER_SIZE - i_used );
1376             }
1377             p_sys->i_buffer_tcp -= i_used;
1378         }
1379         else if( p_sys->i_buffer_udp > 0 )
1380         {
1381             int i_used;
1382
1383             i_packet_udp_type =
1384                 mms_ParsePacket( p_access, p_sys->buffer_udp,
1385                                  p_sys->i_buffer_udp, &i_used );
1386
1387             if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1388             {
1389                 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1390                          MMS_BUFFER_SIZE - i_used );
1391             }
1392             p_sys->i_buffer_udp -= i_used;
1393         }
1394
1395         if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1396         {
1397             mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1398             i_packet_tcp_type = -1;
1399         }
1400
1401         if( i_packet_tcp_type != -1 )
1402         {
1403             return i_packet_tcp_type;
1404         }
1405         else if( i_packet_udp_type != -1 )
1406         {
1407             return i_packet_udp_type;
1408         }
1409     }
1410 }
1411
1412 static int mms_ReceiveCommand( access_t *p_access )
1413 {
1414     access_sys_t *p_sys = p_access->p_sys;
1415
1416     for( ;; )
1417     {
1418         int i_used;
1419         int i_status;
1420
1421         if( NetFillBuffer( p_access ) < 0 )
1422         {
1423             msg_Warn( p_access, "cannot fill buffer" );
1424             return VLC_EGENERIC;
1425         }
1426         if( p_sys->i_buffer_tcp > 0 )
1427         {
1428             i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1429                                          p_sys->i_buffer_tcp, &i_used );
1430             if( i_used < MMS_BUFFER_SIZE )
1431             {
1432                 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1433                          MMS_BUFFER_SIZE - i_used );
1434             }
1435             p_sys->i_buffer_tcp -= i_used;
1436
1437             if( i_status < 0 )
1438             {
1439                 return VLC_EGENERIC;
1440             }
1441
1442             if( p_sys->i_command == 0x1b )
1443             {
1444                 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1445             }
1446             else
1447             {
1448                 break;
1449             }
1450         }
1451         else
1452         {
1453             return VLC_EGENERIC;
1454         }
1455     }
1456
1457     return VLC_SUCCESS;
1458 }
1459
1460 #define MMS_RETRY_MAX       10
1461 #define MMS_RETRY_SLEEP     50000
1462
1463 static int mms_CommandRead( access_t *p_access, int i_command1,
1464                             int i_command2 )
1465 {
1466     access_sys_t *p_sys = p_access->p_sys;
1467     int i_count;
1468     int i_status;
1469
1470     for( i_count = 0; i_count < MMS_RETRY_MAX; )
1471     {
1472         i_status = mms_ReceiveCommand( p_access );
1473         if( i_status < 0 || p_sys->i_command == 0 )
1474         {
1475             i_count++;
1476             msleep( MMS_RETRY_SLEEP );
1477         }
1478         else if( i_command1 == 0 && i_command2 == 0)
1479         {
1480             return VLC_SUCCESS;
1481         }
1482         else if( p_sys->i_command == i_command1 ||
1483                  p_sys->i_command == i_command2 )
1484         {
1485             return VLC_SUCCESS;
1486         }
1487         else
1488         {
1489             switch( p_sys->i_command )
1490             {
1491                 case 0x03:
1492                     msg_Warn( p_access, "socket closed by server" );
1493                     p_access->info.b_eof = 1;
1494                     return VLC_EGENERIC;
1495                 case 0x1e:
1496                     msg_Warn( p_access, "end of media stream" );
1497                     p_access->info.b_eof = 1;
1498                     return VLC_EGENERIC;
1499                 default:
1500                     break;
1501             }
1502         }
1503     }
1504     msg_Warn( p_access, "failed to receive command (aborting)" );
1505
1506     return VLC_EGENERIC;
1507 }
1508
1509
1510 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1511 {
1512     access_sys_t *p_sys = p_access->p_sys;
1513     int          i_count;
1514
1515     for( i_count = 0; i_count < MMS_RETRY_MAX; )
1516     {
1517         int i_status;
1518
1519         if( p_access->b_die )
1520             return -1;
1521
1522         i_status = mms_ReceivePacket( p_access );
1523         if( i_status < 0 )
1524         {
1525             i_count++;
1526             msg_Warn( p_access, "cannot receive header (%d/%d)",
1527                       i_count, MMS_RETRY_MAX );
1528             msleep( MMS_RETRY_SLEEP );
1529         }
1530         else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1531         {
1532             return i_type;
1533         }
1534         else if( i_status == MMS_PACKET_CMD )
1535         {
1536             switch( p_sys->i_command )
1537             {
1538                 case 0x03:
1539                     msg_Warn( p_access, "socket closed by server" );
1540                     p_access->info.b_eof = 1;
1541                     return -1;
1542                 case 0x1e:
1543                     msg_Warn( p_access, "end of media stream" );
1544                     p_access->info.b_eof = 1;
1545                     return -1;
1546                 case 0x20:
1547                     /* XXX not too dificult to be done EXCEPT that we
1548                      * need to restart demuxer... and I don't see how we
1549                      * could do that :p */
1550                     msg_Err( p_access,
1551                              "reinitialization needed --> unsupported" );
1552                     p_access->info.b_eof = VLC_TRUE;
1553                     return -1;
1554                 default:
1555                     break;
1556             }
1557         }
1558     }
1559
1560     msg_Err( p_access, "cannot receive %s (aborting)",
1561              ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1562     return -1;
1563 }
1564