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