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