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