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