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