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