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