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