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