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