]> git.sesse.net Git - vlc/blob - modules/access/mms/mmsh.c
c2a2c24342711b950384b0778757a015fa226841
[vlc] / modules / access / mms / mmsh.c
1 /*****************************************************************************
2  * mmsh.c:
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  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_access.h>
34 #include "vlc_playlist.h"
35 #include "vlc_strings.h"
36
37 #include <vlc_network.h>
38 #include "vlc_url.h"
39 #include "asf.h"
40 #include "buffer.h"
41
42 #include "mms.h"
43 #include "mmsh.h"
44
45 /* TODO:
46  *  - authentication
47  */
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 int  E_(MMSHOpen)  ( access_t * );
53 void E_(MMSHClose) ( access_t * );
54
55 static ssize_t Read( access_t *, uint8_t *, size_t );
56 static ssize_t ReadRedirect( access_t *, uint8_t *, size_t );
57 static int  Seek( access_t *, int64_t );
58 static int  Control( access_t *, int, va_list );
59
60 static int  Describe( access_t  *, char **ppsz_location );
61 static int  Start( access_t *, int64_t );
62 static void Stop( access_t * );
63
64 static int  GetPacket( access_t *, chunk_t * );
65 static void GetHeader( access_t *p_access );
66
67 static int Restart( access_t * );
68 static int Reset( access_t * );
69
70 //#define MMSH_USER_AGENT "NSPlayer/4.1.0.3856"
71 #define MMSH_USER_AGENT "NSPlayer/7.10.0.3059"
72
73 /****************************************************************************
74  * Open: connect to ftp server and ask for file
75  ****************************************************************************/
76 int E_(MMSHOpen)( access_t *p_access )
77 {
78     access_sys_t    *p_sys;
79     char            *psz_location = NULL;
80     char            *psz_proxy;
81
82     /* init p_sys */
83
84     /* Set up p_access */
85     p_access->pf_read = Read;
86     p_access->pf_block = NULL;
87     p_access->pf_control = Control;
88     p_access->pf_seek = Seek;
89     p_access->info.i_update = 0;
90     p_access->info.i_size = 0;
91     p_access->info.i_pos = 0;
92     p_access->info.b_eof = VLC_FALSE;
93     p_access->info.i_title = 0;
94     p_access->info.i_seekpoint = 0;
95
96     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
97     if( !p_sys )
98         return VLC_ENOMEM;
99
100     memset( p_sys, 0, sizeof( access_sys_t ) );
101     p_sys->i_proto= MMS_PROTO_HTTP;
102     p_sys->fd     = -1;
103     p_sys->i_start= 0;
104
105     /* Handle proxy */
106     p_sys->b_proxy = VLC_FALSE;
107     memset( &p_sys->proxy, 0, sizeof(p_sys->proxy) );
108
109     /* Check proxy */
110     /* TODO reuse instead http-proxy from http access ? */
111     psz_proxy = var_CreateGetString( p_access, "mmsh-proxy" );
112     if( !*psz_proxy )
113     {
114         char *psz_http_proxy = config_GetPsz( p_access, "http-proxy" );
115         if( psz_http_proxy && *psz_http_proxy )
116         {
117             free( psz_proxy );
118             psz_proxy = psz_http_proxy;
119             var_SetString( p_access, "mmsh-proxy", psz_proxy );
120         }
121         else
122         {
123             free( psz_http_proxy );
124         }
125     }
126
127     if( *psz_proxy )
128     {
129         p_sys->b_proxy = VLC_TRUE;
130         vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
131     }
132 #ifdef HAVE_GETENV
133     else
134     {
135         char *psz_proxy = getenv( "http_proxy" );
136         if( psz_proxy && *psz_proxy )
137         {
138             p_sys->b_proxy = VLC_TRUE;
139             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
140         }
141     }
142 #endif
143     free( psz_proxy );
144
145     if( p_sys->b_proxy )
146     {
147         if( ( p_sys->proxy.psz_host == NULL ) ||
148             ( *p_sys->proxy.psz_host == '\0' ) )
149         {
150             msg_Warn( p_access, "invalid proxy host" );
151             vlc_UrlClean( &p_sys->proxy );
152             free( p_sys );
153             return VLC_EGENERIC;
154         }
155
156         if( p_sys->proxy.i_port <= 0 )
157             p_sys->proxy.i_port = 80;
158         msg_Dbg( p_access, "Using http proxy %s:%d",
159                  p_sys->proxy.psz_host, p_sys->proxy.i_port );
160     }
161
162     /* open a tcp connection */
163     vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
164     if( ( p_sys->url.psz_host == NULL ) ||
165         ( *p_sys->url.psz_host == '\0' ) )
166     {
167         msg_Err( p_access, "invalid host" );
168         vlc_UrlClean( &p_sys->proxy );
169         vlc_UrlClean( &p_sys->url );
170         free( p_sys );
171         return VLC_EGENERIC;
172     }
173     if( p_sys->url.i_port <= 0 )
174         p_sys->url.i_port = 80;
175
176     if( Describe( p_access, &psz_location ) )
177     {
178         vlc_UrlClean( &p_sys->proxy );
179         vlc_UrlClean( &p_sys->url );
180         free( p_sys );
181         return VLC_EGENERIC;
182     }
183     /* Handle redirection */
184     if( psz_location && *psz_location )
185     {
186         playlist_t * p_playlist = pl_Yield( p_access );
187         msg_Dbg( p_access, "redirection to %s", psz_location );
188
189         /** \bug we do not autodelete here */
190         playlist_Add( p_playlist, psz_location, psz_location,
191                       PLAYLIST_INSERT | PLAYLIST_GO, PLAYLIST_END, VLC_TRUE,
192                       VLC_FALSE );
193         vlc_object_release( p_playlist );
194
195         free( psz_location );
196
197         p_access->pf_read = ReadRedirect;
198         return VLC_SUCCESS;
199     }
200
201     /* Start playing */
202     if( Start( p_access, 0 ) )
203     {
204         msg_Err( p_access, "cannot start stream" );
205         free( p_sys->p_header );
206         vlc_UrlClean( &p_sys->proxy );
207         vlc_UrlClean( &p_sys->url );
208         free( p_sys );
209         return VLC_EGENERIC;
210     }
211
212     if( !p_sys->b_broadcast )
213     {
214         p_access->info.i_size = p_sys->asfh.i_file_size;
215     }
216
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * Close: free unused data structures
222  *****************************************************************************/
223 void E_( MMSHClose )( access_t *p_access )
224 {
225     access_sys_t *p_sys = p_access->p_sys;
226
227     Stop( p_access );
228
229     if( p_sys->p_header )
230         free( p_sys->p_header  );
231
232     vlc_UrlClean( &p_sys->proxy );
233     vlc_UrlClean( &p_sys->url );
234     free( p_sys );
235 }
236
237 /*****************************************************************************
238  * Control:
239  *****************************************************************************/
240 static int Control( access_t *p_access, int i_query, va_list args )
241 {
242     access_sys_t *p_sys = p_access->p_sys;
243     vlc_bool_t   *pb_bool;
244     int          *pi_int;
245     int64_t      *pi_64;
246     int          i_int;
247
248     switch( i_query )
249     {
250         /* */
251         case ACCESS_CAN_SEEK:
252             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
253             *pb_bool = !p_sys->b_broadcast;
254             break;
255
256         case ACCESS_CAN_FASTSEEK:
257         case ACCESS_CAN_PAUSE:
258             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
259             *pb_bool = VLC_FALSE;
260             break;
261
262         case ACCESS_CAN_CONTROL_PACE:
263             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
264             *pb_bool = VLC_TRUE;
265             break;
266
267         /* */
268         case ACCESS_GET_MTU:
269             pi_int = (int*)va_arg( args, int * );
270             *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
271             break;
272
273         case ACCESS_GET_PTS_DELAY:
274             pi_64 = (int64_t*)va_arg( args, int64_t * );
275             *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
276             break;
277
278         case ACCESS_GET_PRIVATE_ID_STATE:
279             i_int = (int)va_arg( args, int );
280             pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
281
282             if( (i_int < 0) || (i_int > 127) )
283                 return VLC_EGENERIC;
284             *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
285             break;
286
287         /* */
288         case ACCESS_SET_PAUSE_STATE:
289         case ACCESS_GET_TITLE_INFO:
290         case ACCESS_SET_TITLE:
291         case ACCESS_SET_SEEKPOINT:
292         case ACCESS_SET_PRIVATE_ID_STATE:
293         case ACCESS_GET_CONTENT_TYPE:
294             return VLC_EGENERIC;
295
296         default:
297             msg_Warn( p_access, "unimplemented query in control" );
298             return VLC_EGENERIC;
299
300     }
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * Seek: try to go at the right place
306  *****************************************************************************/
307 static int Seek( access_t *p_access, int64_t i_pos )
308 {
309     access_sys_t *p_sys = p_access->p_sys;
310     chunk_t      ck;
311     off_t        i_offset;
312     off_t        i_packet;
313
314     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
315
316     i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
317     i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
318
319     Stop( p_access );
320     Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
321
322     while( !p_access->b_die )
323     {
324         msg_Warn( p_access, "GetPacket 1" );
325         if( GetPacket( p_access, &ck ) )
326             break;
327
328         /* skip headers */
329         if( ck.i_type != 0x4824 )
330             break;
331
332         msg_Warn( p_access, "skipping header" );
333     }
334
335     p_access->info.i_pos = i_pos;
336     p_access->info.b_eof = VLC_FALSE;
337     p_sys->i_packet_used += i_offset;
338
339     return VLC_SUCCESS;
340 }
341
342 /*****************************************************************************
343  * Read:
344  *****************************************************************************/
345 static ssize_t ReadRedirect( access_t *p_access, uint8_t *p, size_t i_len )
346 {
347     return 0;
348 }
349
350 /*****************************************************************************
351  * Read:
352  *****************************************************************************/
353 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
354 {
355     access_sys_t *p_sys = p_access->p_sys;
356     size_t       i_copy;
357     size_t       i_data = 0;
358
359     if( p_access->info.b_eof )
360         return 0;
361
362     while( i_data < (size_t) i_len )
363     {
364         if( p_access->info.i_pos < (p_sys->i_start + p_sys->i_header) )
365         {
366             int i_offset = p_access->info.i_pos - p_sys->i_start;
367             i_copy = __MIN( p_sys->i_header - i_offset,
368                             (int)((size_t)i_len - i_data) );
369             memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
370
371             i_data += i_copy;
372             p_access->info.i_pos += i_copy;
373         }
374         else if( p_sys->i_packet_used < p_sys->i_packet_length )
375         {
376             i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
377                             i_len - i_data );
378             memcpy( &p_buffer[i_data],
379                     &p_sys->p_packet[p_sys->i_packet_used],
380                     i_copy );
381
382             i_data += i_copy;
383             p_sys->i_packet_used += i_copy;
384             p_access->info.i_pos += i_copy;
385         }
386         else if( (p_sys->i_packet_length > 0) &&
387                  ((int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size) )
388         {
389             i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
390                             i_len - i_data );
391             memset( &p_buffer[i_data], 0, i_copy );
392
393             i_data += i_copy;
394             p_sys->i_packet_used += i_copy;
395             p_access->info.i_pos += i_copy;
396         }
397         else
398         {
399             chunk_t ck;
400         msg_Warn( p_access, "GetPacket 2" );
401             if( GetPacket( p_access, &ck ) )
402             {
403                 int i_ret = -1;
404                 if( p_sys->b_broadcast )
405                 {
406                     if( (ck.i_type == 0x4524) && (ck.i_sequence != 0) )
407                         i_ret = Restart( p_access );
408                     else if( ck.i_type == 0x4324 )
409                         i_ret = Reset( p_access );
410                 }
411                 if( i_ret )
412                 {
413                     p_access->info.b_eof = VLC_TRUE;
414                     return 0;
415                 }
416             }
417             if( ck.i_type != 0x4424 )
418             {
419                 p_sys->i_packet_used = 0;
420                 p_sys->i_packet_length = 0;
421             }
422         }
423     }
424
425     return( i_data );
426 }
427
428 /* */
429 static int Restart( access_t *p_access )
430 {
431     access_sys_t *p_sys = p_access->p_sys;
432     char *psz_location = NULL;
433
434     msg_Dbg( p_access, "Restart the stream" );
435     p_sys->i_start = p_access->info.i_pos;
436
437     /* */
438     msg_Dbg( p_access, "stoping the stream" );
439     Stop( p_access );
440
441     /* */
442     msg_Dbg( p_access, "describe the stream" );
443     if( Describe( p_access, &psz_location ) )
444     {
445         msg_Err( p_access, "describe failed" );
446         return VLC_EGENERIC;
447     }
448     /* */
449     if( Start( p_access, 0 ) )
450     {
451         msg_Err( p_access, "Start failed" );
452         return VLC_EGENERIC;
453     }
454     return VLC_SUCCESS;
455 }
456 static int Reset( access_t *p_access )
457 {
458     access_sys_t *p_sys = p_access->p_sys;
459     asf_header_t old_asfh = p_sys->asfh;
460     int i;
461
462     msg_Dbg( p_access, "Reset the stream" );
463     p_sys->i_start = p_access->info.i_pos;
464
465     /* */
466     p_sys->i_packet_sequence = 0;
467     p_sys->i_packet_used = 0;
468     p_sys->i_packet_length = 0;
469     p_sys->p_packet = NULL;
470
471     /* Get the next header FIXME memory loss ? */
472     GetHeader( p_access );
473     if( p_sys->i_header <= 0 )
474         return VLC_EGENERIC;
475
476     E_( asf_HeaderParse )( &p_sys->asfh,
477                            p_sys->p_header, p_sys->i_header );
478     msg_Dbg( p_access, "packet count="I64Fd" packet size=%d",
479              p_sys->asfh.i_data_packets_count,
480              p_sys->asfh.i_min_data_packet_size );
481
482     E_( asf_StreamSelect)( &p_sys->asfh,
483                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
484                            var_CreateGetInteger( p_access, "mms-all" ),
485                            var_CreateGetInteger( p_access, "audio" ),
486                            var_CreateGetInteger( p_access, "video" ) );
487
488     /* Check we have comptible asfh */
489     for( i = 1; i < 128; i++ )
490     {
491         asf_stream_t *p_old = &old_asfh.stream[i];
492         asf_stream_t *p_new = &p_sys->asfh.stream[i];
493
494         if( p_old->i_cat != p_new->i_cat || p_old->i_selected != p_new->i_selected )
495             break;
496     }
497     if( i < 128 )
498     {
499         msg_Warn( p_access, "incompatible asf header, restart" );
500         return Restart( p_access );
501     }
502
503     /* */
504     p_sys->i_packet_used = 0;
505     p_sys->i_packet_length = 0;
506     return VLC_SUCCESS;
507 }
508
509 static int OpenConnection( access_t *p_access )
510 {
511     access_sys_t *p_sys = p_access->p_sys;
512     vlc_url_t    srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
513
514     if( ( p_sys->fd = net_ConnectTCP( p_access,
515                                       srv.psz_host, srv.i_port ) ) < 0 )
516     {
517         msg_Err( p_access, "cannot connect to %s:%d",
518                  srv.psz_host, srv.i_port );
519         return VLC_EGENERIC;
520     }
521
522     if( p_sys->b_proxy )
523     {
524         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
525                     "GET http://%s:%d%s HTTP/1.0\r\n",
526                     p_sys->url.psz_host, p_sys->url.i_port,
527                     ( (p_sys->url.psz_path == NULL) ||
528                       (*p_sys->url.psz_path == '\0') ) ?
529                          "/" : p_sys->url.psz_path );
530
531         /* Proxy Authentication */
532         if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
533         {
534             char *buf;
535             char *b64;
536
537             asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
538                        p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
539
540             b64 = vlc_b64_encode( buf );
541             free( buf );
542
543             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
544                         "Proxy-Authorization: Basic %s\r\n", b64 );
545             free( b64 );
546         }
547     }
548     else
549     {
550         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
551                     "GET %s HTTP/1.0\r\n"
552                     "Host: %s:%d\r\n",
553                     ( (p_sys->url.psz_path == NULL) ||
554                       (*p_sys->url.psz_path == '\0') ) ?
555                             "/" : p_sys->url.psz_path,
556                     p_sys->url.psz_host, p_sys->url.i_port );
557     }
558     return VLC_SUCCESS;
559 }
560
561 /*****************************************************************************
562  * Describe:
563  *****************************************************************************/
564 static int Describe( access_t  *p_access, char **ppsz_location )
565 {
566     access_sys_t *p_sys = p_access->p_sys;
567     char         *psz_location = NULL;
568     char         *psz;
569     int          i_code;
570
571     /* Reinit context */
572     p_sys->b_broadcast = VLC_TRUE;
573     p_sys->i_request_context = 1;
574     p_sys->i_packet_sequence = 0;
575     p_sys->i_packet_used = 0;
576     p_sys->i_packet_length = 0;
577     p_sys->p_packet = NULL;
578     E_( GenerateGuid )( &p_sys->guid );
579
580     if( OpenConnection( p_access ) )
581         return VLC_EGENERIC;
582
583     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
584                 "Accept: */*\r\n"
585                 "User-Agent: "MMSH_USER_AGENT"\r\n"
586                 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
587                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
588                 "Connection: Close\r\n",
589                 p_sys->i_request_context++,
590                 GUID_PRINT( p_sys->guid ) );
591
592     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
593     {
594         msg_Err( p_access, "failed to send request" );
595         goto error;
596     }
597
598     /* Receive the http header */
599     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
600     {
601         msg_Err( p_access, "failed to read answer" );
602         goto error;
603     }
604
605     if( strncmp( psz, "HTTP/1.", 7 ) )
606     {
607         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
608         free( psz );
609         goto error;
610     }
611     i_code = atoi( &psz[9] );
612     if( i_code >= 400 )
613     {
614         msg_Err( p_access, "error: %s", psz );
615         free( psz );
616         goto error;
617     }
618
619     msg_Dbg( p_access, "HTTP reply '%s'", psz );
620     free( psz );
621     for( ;; )
622     {
623         char *psz = net_Gets( p_access, p_sys->fd, NULL );
624         char *p;
625
626         if( psz == NULL )
627         {
628             msg_Err( p_access, "failed to read answer" );
629             goto error;
630         }
631
632         if( *psz == '\0' )
633         {
634             free( psz );
635             break;
636         }
637
638         if( ( p = strchr( psz, ':' ) ) == NULL )
639         {
640             msg_Err( p_access, "malformed header line: %s", psz );
641             free( psz );
642             goto error;
643         }
644         *p++ = '\0';
645         while( *p == ' ' ) p++;
646
647         /* FIXME FIXME test Content-Type to see if it's a plain stream or an
648          * asx FIXME */
649         if( !strcasecmp( psz, "Pragma" ) )
650         {
651             if( strstr( p, "features" ) )
652             {
653                 /* FIXME, it is a bit badly done here ..... */
654                 if( strstr( p, "broadcast" ) )
655                 {
656                     msg_Dbg( p_access, "stream type = broadcast" );
657                     p_sys->b_broadcast = VLC_TRUE;
658                 }
659                 else if( strstr( p, "seekable" ) )
660                 {
661                     msg_Dbg( p_access, "stream type = seekable" );
662                     p_sys->b_broadcast = VLC_FALSE;
663                 }
664                 else
665                 {
666                     msg_Warn( p_access, "unknow stream types (%s)", p );
667                     p_sys->b_broadcast = VLC_FALSE;
668                 }
669             }
670         }
671         else if( !strcasecmp( psz, "Location" ) )
672         {
673             psz_location = strdup( p );
674         }
675
676         free( psz );
677     }
678
679     /* Handle the redirection */
680     if( ( (i_code == 301) || (i_code == 302) ||
681           (i_code == 303) || (i_code == 307) ) &&
682         psz_location && *psz_location )
683     {
684         msg_Dbg( p_access, "redirection to %s", psz_location );
685         net_Close( p_sys->fd ); p_sys->fd = -1;
686
687         *ppsz_location = psz_location;
688         return VLC_SUCCESS;
689     }
690
691     /* Read the asf header */
692     GetHeader( p_access );
693     if( p_sys->i_header <= 0 )
694     {
695         msg_Err( p_access, "header size == 0" );
696         goto error;
697     }
698     /* close this connection */
699     net_Close( p_sys->fd );
700     p_sys->fd = -1;
701
702     /* *** parse header and get stream and their id *** */
703     /* get all streams properties,
704      *
705      * TODO : stream bitrates properties(optional)
706      *        and bitrate mutual exclusion(optional) */
707     E_( asf_HeaderParse )( &p_sys->asfh,
708                            p_sys->p_header, p_sys->i_header );
709     msg_Dbg( p_access, "packet count="I64Fd" packet size=%d",
710              p_sys->asfh.i_data_packets_count,
711              p_sys->asfh.i_min_data_packet_size );
712
713     E_( asf_StreamSelect)( &p_sys->asfh,
714                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
715                            var_CreateGetInteger( p_access, "mms-all" ),
716                            var_CreateGetInteger( p_access, "audio" ),
717                            var_CreateGetInteger( p_access, "video" ) );
718     return VLC_SUCCESS;
719
720 error:
721     if( p_sys->fd > 0 )
722     {
723         net_Close( p_sys->fd  );
724         p_sys->fd = -1;
725     }
726     return VLC_EGENERIC;
727 }
728
729 static void GetHeader( access_t *p_access )
730 {
731     access_sys_t *p_sys = p_access->p_sys;
732
733     /* Read the asf header */
734     p_sys->i_header = 0;
735     if( p_sys->p_header )
736         free( p_sys->p_header  );
737     p_sys->p_header = NULL;
738     for( ;; )
739     {
740         chunk_t ck;
741         if( GetPacket( p_access, &ck ) || ck.i_type != 0x4824 )
742             break;
743
744         if( ck.i_data > 0 )
745         {
746             p_sys->i_header += ck.i_data;
747             p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
748             memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
749                     ck.p_data, ck.i_data );
750         }
751     }
752     msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
753 }
754
755
756 /*****************************************************************************
757  * Start stream
758  ****************************************************************************/
759 static int Start( access_t *p_access, int64_t i_pos )
760 {
761     access_sys_t *p_sys = p_access->p_sys;
762     int  i_streams = 0;
763     int  i_streams_selected = 0;
764     int  i;
765     char *psz = NULL;
766
767     msg_Dbg( p_access, "starting stream" );
768
769     for( i = 1; i < 128; i++ )
770     {
771         if( p_sys->asfh.stream[i].i_cat == ASF_STREAM_UNKNOWN )
772             continue;
773         i_streams++;
774         if( p_sys->asfh.stream[i].i_selected )
775             i_streams_selected++;
776     }
777     if( i_streams_selected <= 0 )
778     {
779         msg_Err( p_access, "no stream selected" );
780         return VLC_EGENERIC;
781     }
782
783     if( OpenConnection( p_access ) )
784         return VLC_EGENERIC;
785
786     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
787                 "Accept: */*\r\n"
788                 "User-Agent: "MMSH_USER_AGENT"\r\n" );
789     if( p_sys->b_broadcast )
790     {
791         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
792                     "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
793                     p_sys->i_request_context++ );
794     }
795     else
796     {
797         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
798                     "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
799                     (uint32_t)((i_pos >> 32)&0xffffffff),
800                     (uint32_t)(i_pos&0xffffffff),
801                     p_sys->i_request_context++ );
802     }
803     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
804                 "Pragma: xPlayStrm=1\r\n"
805                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
806                 "Pragma: stream-switch-count=%d\r\n"
807                 "Pragma: stream-switch-entry=",
808                 GUID_PRINT( p_sys->guid ),
809                 i_streams);
810
811     for( i = 1; i < 128; i++ )
812     {
813         if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
814         {
815             int i_select = 2;
816             if( p_sys->asfh.stream[i].i_selected )
817             {
818                 i_select = 0;
819             }
820             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
821                         "ffff:%d:%d ", i, i_select );
822         }
823     }
824     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
825     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
826                 "Connection: Close\r\n" );
827
828     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
829     {
830         msg_Err( p_access, "failed to send request" );
831         return VLC_EGENERIC;
832     }
833
834     psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
835     if( psz == NULL )
836     {
837         msg_Err( p_access, "cannot read data 0" );
838         return VLC_EGENERIC;
839     }
840
841     if( atoi( &psz[9] ) >= 400 )
842     {
843         msg_Err( p_access, "error: %s", psz );
844         free( psz );
845         return VLC_EGENERIC;
846     }
847     msg_Dbg( p_access, "HTTP reply '%s'", psz );
848     free( psz );
849
850     /* FIXME check HTTP code */
851     for( ;; )
852     {
853         char *psz = net_Gets( p_access, p_sys->fd, NULL );
854         if( psz == NULL )
855         {
856             msg_Err( p_access, "cannot read data 1" );
857             return VLC_EGENERIC;
858         }
859         if( *psz == '\0' )
860         {
861             free( psz );
862             break;
863         }
864         msg_Dbg( p_access, "%s", psz );
865         free( psz );
866     }
867
868     p_sys->i_packet_used   = 0;
869     p_sys->i_packet_length = 0;
870
871     return VLC_SUCCESS;
872 }
873
874 /*****************************************************************************
875  * closing stream
876  *****************************************************************************/
877 static void Stop( access_t *p_access )
878 {
879     access_sys_t *p_sys = p_access->p_sys;
880
881     msg_Dbg( p_access, "closing stream" );
882     if( p_sys->fd > 0 )
883     {
884         net_Close( p_sys->fd );
885         p_sys->fd = -1;
886     }
887 }
888
889 /*****************************************************************************
890  * get packet
891  *****************************************************************************/
892 static int GetPacket( access_t * p_access, chunk_t *p_ck )
893 {
894     access_sys_t *p_sys = p_access->p_sys;
895     int restsize;
896
897     /* chunk_t */
898     memset( p_ck, 0, sizeof( chunk_t ) );
899
900     /* Read the chunk header */
901     /* Some headers are short, like 0x4324. Reading 12 bytes will cause us
902      * to lose synchronization with the stream. Just read to the length
903      * (4 bytes), decode and then read up to 8 additional bytes to get the
904      * entire header.
905      */
906     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 4, VLC_TRUE ) < 4 )
907     {
908        msg_Err( p_access, "cannot read data 2" );
909        return VLC_EGENERIC;
910     }
911
912     p_ck->i_type = GetWLE( p_sys->buffer);
913     p_ck->i_size = GetWLE( p_sys->buffer + 2);
914
915     restsize = p_ck->i_size;
916     if( restsize > 8 )
917         restsize = 8;
918
919     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer + 4, restsize, VLC_TRUE ) < restsize )
920     {
921         msg_Err( p_access, "cannot read data 3" );
922         return VLC_EGENERIC;
923     }
924     p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
925     p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
926
927     /* Set i_size2 to 8 if this header was short, since a real value won't be
928      * present in the buffer. Using 8 avoid reading additional data for the
929      * packet.
930      */
931     if( restsize < 8 )
932         p_ck->i_size2 = 8;
933     else
934         p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
935
936     p_ck->p_data      = p_sys->buffer + 12;
937     p_ck->i_data      = p_ck->i_size2 - 8;
938
939     if( p_ck->i_type == 0x4524 )   // Transfer complete
940     {
941         if( p_ck->i_sequence == 0 )
942         {
943             msg_Warn( p_access, "EOF" );
944             return VLC_EGENERIC;
945         }
946         else
947         {
948             msg_Warn( p_access, "next stream following" );
949             return VLC_EGENERIC;
950         }
951     }
952     else if( p_ck->i_type == 0x4324 )
953     {
954         /* 0x4324 is CHUNK_TYPE_RESET: a new stream will follow with a sequence of 0 */
955         msg_Warn( p_access, "next stream following (reset) seq=%d", p_ck->i_sequence  );
956         return VLC_EGENERIC;
957     }
958     else if( (p_ck->i_type != 0x4824) && (p_ck->i_type != 0x4424) )
959     {
960         msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
961         return VLC_EGENERIC;
962     }
963
964     if( (p_ck->i_data > 0) &&
965         (net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12],
966                    p_ck->i_data, VLC_TRUE ) < p_ck->i_data) )
967     {
968         msg_Err( p_access, "cannot read data 4" );
969         return VLC_EGENERIC;
970     }
971
972 #if 0
973     if( (p_sys->i_packet_sequence != 0) &&
974         (p_ck->i_sequence != p_sys->i_packet_sequence) )
975     {
976         msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
977     }
978 #endif
979
980     p_sys->i_packet_sequence = p_ck->i_sequence + 1;
981     p_sys->i_packet_used   = 0;
982     p_sys->i_packet_length = p_ck->i_data;
983     p_sys->p_packet        = p_ck->p_data;
984
985     return VLC_SUCCESS;
986 }