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