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