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