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