]> git.sesse.net Git - vlc/blob - modules/access/mms/mmsh.c
access_mms: fix variable type.
[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 *, int64_t );
58 static int  Control( access_t *, int, va_list );
59
60 static int  Describe( access_t  *, char **ppsz_location );
61 static int  Start( access_t *, int64_t );
62 static void Stop( access_t * );
63
64 static int  GetPacket( access_t *, chunk_t * );
65 static void GetHeader( access_t *p_access );
66
67 static int Restart( access_t * );
68 static int Reset( access_t * );
69
70 //#define MMSH_USER_AGENT "NSPlayer/4.1.0.3856"
71 #define MMSH_USER_AGENT "NSPlayer/7.10.0.3059"
72
73 /****************************************************************************
74  * Open: connect to ftp server and ask for file
75  ****************************************************************************/
76 int 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_path, 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_AddSubItem( input_GetItem( p_input ), p_new_loc );
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_t)var_GetInteger( p_access, "mms-caching" ) * INT64_C(1000);
252             break;
253
254         case ACCESS_GET_PRIVATE_ID_STATE:
255             i_int = (int)va_arg( args, int );
256             pb_bool = (bool *)va_arg( args, bool * );
257
258             if( (i_int < 0) || (i_int > 127) )
259                 return VLC_EGENERIC;
260             *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? true : false;
261             break;
262
263         /* */
264         case ACCESS_SET_PAUSE_STATE:
265             b_bool = (bool)va_arg( args, int );
266             if( b_bool )
267                 Stop( p_access );
268             else
269                 Seek( p_access, p_access->info.i_pos );
270             break;
271
272         case ACCESS_GET_TITLE_INFO:
273         case ACCESS_SET_TITLE:
274         case ACCESS_SET_SEEKPOINT:
275         case ACCESS_SET_PRIVATE_ID_STATE:
276         case ACCESS_GET_CONTENT_TYPE:
277             return VLC_EGENERIC;
278
279         default:
280             msg_Warn( p_access, "unimplemented query in control" );
281             return VLC_EGENERIC;
282
283     }
284     return VLC_SUCCESS;
285 }
286
287 /*****************************************************************************
288  * Seek: try to go at the right place
289  *****************************************************************************/
290 static int Seek( access_t *p_access, int64_t i_pos )
291 {
292     access_sys_t *p_sys = p_access->p_sys;
293     chunk_t      ck;
294     off_t        i_offset;
295     off_t        i_packet;
296
297     msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
298
299     i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
300     i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
301
302     Stop( p_access );
303     Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
304
305     while( vlc_object_alive (p_access) )
306     {
307         if( GetPacket( p_access, &ck ) )
308             break;
309
310         /* skip headers */
311         if( ck.i_type != 0x4824 )
312             break;
313
314         msg_Warn( p_access, "skipping header" );
315     }
316
317     p_access->info.i_pos = i_pos;
318     p_access->info.b_eof = false;
319     p_sys->i_packet_used += i_offset;
320
321     return VLC_SUCCESS;
322 }
323
324 /*****************************************************************************
325  * ReadRedirect:
326  *****************************************************************************/
327 static ssize_t ReadRedirect( access_t *p_access, uint8_t *p, size_t i_len )
328 {
329     VLC_UNUSED(p_access); VLC_UNUSED(p); VLC_UNUSED(i_len);
330     return 0;
331 }
332
333 /*****************************************************************************
334  * Block:
335  *****************************************************************************/
336 static block_t *Block( access_t *p_access )
337 {
338     access_sys_t *p_sys = p_access->p_sys;
339     const unsigned i_packet_min = p_sys->asfh.i_min_data_packet_size;
340
341     if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
342     {
343         const size_t i_offset = p_access->info.i_pos - p_sys->i_start;
344         const size_t i_copy = p_sys->i_header - i_offset;
345
346         block_t *p_block = block_New( p_access, i_copy );
347         if( !p_block )
348             return NULL;
349
350         memcpy( p_block->p_buffer, &p_sys->p_header[i_offset], i_copy );
351         p_access->info.i_pos += i_copy;
352         return p_block;
353     }
354     else if( p_sys->i_packet_length > 0 &&
355              p_sys->i_packet_used < __MAX( p_sys->i_packet_length, i_packet_min ) )
356     {
357         size_t i_copy = 0;
358         size_t i_padding = 0;
359
360         if( p_sys->i_packet_used < p_sys->i_packet_length )
361             i_copy = p_sys->i_packet_length - p_sys->i_packet_used;
362         if( __MAX( p_sys->i_packet_used, p_sys->i_packet_length ) < i_packet_min )
363             i_padding = i_packet_min - __MAX( p_sys->i_packet_used, p_sys->i_packet_length );
364
365         block_t *p_block = block_New( p_access, i_copy + i_padding );
366         if( !p_block )
367             return NULL;
368
369         if( i_copy > 0 )
370             memcpy( &p_block->p_buffer[0], &p_sys->p_packet[p_sys->i_packet_used], i_copy );
371         if( i_padding > 0 )
372             memset( &p_block->p_buffer[i_copy], 0, i_padding );
373
374         p_sys->i_packet_used += i_copy + i_padding;
375         p_access->info.i_pos += i_copy + i_padding;
376         return p_block;
377
378     }
379
380     chunk_t ck;
381     if( GetPacket( p_access, &ck ) )
382     {
383         int i_ret = -1;
384         if( p_sys->b_broadcast )
385         {
386             if( (ck.i_type == 0x4524) && (ck.i_sequence != 0) )
387                 i_ret = Restart( p_access );
388             else if( ck.i_type == 0x4324 )
389                 i_ret = Reset( p_access );
390         }
391         if( i_ret )
392         {
393             p_access->info.b_eof = true;
394             return 0;
395         }
396     }
397     if( ck.i_type != 0x4424 )
398     {
399         p_sys->i_packet_used = 0;
400         p_sys->i_packet_length = 0;
401     }
402
403     return NULL;
404 }
405
406 /* */
407 static int Restart( access_t *p_access )
408 {
409     access_sys_t *p_sys = p_access->p_sys;
410     char *psz_location = NULL;
411
412     msg_Dbg( p_access, "Restart the stream" );
413     p_sys->i_start = p_access->info.i_pos;
414
415     /* */
416     msg_Dbg( p_access, "stoping the stream" );
417     Stop( p_access );
418
419     /* */
420     msg_Dbg( p_access, "describe the stream" );
421     if( Describe( p_access, &psz_location ) )
422     {
423         msg_Err( p_access, "describe failed" );
424         return VLC_EGENERIC;
425     }
426     free( psz_location );
427
428     /* */
429     if( Start( p_access, 0 ) )
430     {
431         msg_Err( p_access, "Start failed" );
432         return VLC_EGENERIC;
433     }
434     return VLC_SUCCESS;
435 }
436 static int Reset( access_t *p_access )
437 {
438     access_sys_t *p_sys = p_access->p_sys;
439     asf_header_t old_asfh = p_sys->asfh;
440     int i;
441
442     msg_Dbg( p_access, "Reset the stream" );
443     p_sys->i_start = p_access->info.i_pos;
444
445     /* */
446     p_sys->i_packet_sequence = 0;
447     p_sys->i_packet_used = 0;
448     p_sys->i_packet_length = 0;
449     p_sys->p_packet = NULL;
450
451     /* Get the next header FIXME memory loss ? */
452     GetHeader( p_access );
453     if( p_sys->i_header <= 0 )
454         return VLC_EGENERIC;
455
456     asf_HeaderParse ( &p_sys->asfh,
457                        p_sys->p_header, p_sys->i_header );
458     msg_Dbg( p_access, "packet count=%"PRId64" packet size=%d",
459              p_sys->asfh.i_data_packets_count,
460              p_sys->asfh.i_min_data_packet_size );
461
462     asf_StreamSelect( &p_sys->asfh,
463                        var_CreateGetInteger( p_access, "mms-maxbitrate" ),
464                        var_CreateGetBool( p_access, "mms-all" ),
465                        var_CreateGetInteger( p_access, "audio" ),
466                        var_CreateGetInteger( p_access, "video" ) );
467
468     /* Check we have comptible asfh */
469     for( i = 1; i < 128; i++ )
470     {
471         asf_stream_t *p_old = &old_asfh.stream[i];
472         asf_stream_t *p_new = &p_sys->asfh.stream[i];
473
474         if( p_old->i_cat != p_new->i_cat || p_old->i_selected != p_new->i_selected )
475             break;
476     }
477     if( i < 128 )
478     {
479         msg_Warn( p_access, "incompatible asf header, restart" );
480         return Restart( p_access );
481     }
482
483     /* */
484     p_sys->i_packet_used = 0;
485     p_sys->i_packet_length = 0;
486     return VLC_SUCCESS;
487 }
488
489 static int OpenConnection( access_t *p_access )
490 {
491     access_sys_t *p_sys = p_access->p_sys;
492     vlc_url_t    srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
493
494     if( ( p_sys->fd = net_ConnectTCP( p_access,
495                                       srv.psz_host, srv.i_port ) ) < 0 )
496     {
497         msg_Err( p_access, "cannot connect to %s:%d",
498                  srv.psz_host, srv.i_port );
499         return VLC_EGENERIC;
500     }
501
502     if( p_sys->b_proxy )
503     {
504         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
505                     "GET http://%s:%d%s HTTP/1.0\r\n",
506                     p_sys->url.psz_host, p_sys->url.i_port,
507                     ( (p_sys->url.psz_path == NULL) ||
508                       (*p_sys->url.psz_path == '\0') ) ?
509                          "/" : p_sys->url.psz_path );
510
511         /* Proxy Authentication */
512         if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
513         {
514             char *buf;
515             char *b64;
516
517             if( asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
518                        p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" ) == -1 )
519                 return VLC_ENOMEM;
520
521             b64 = vlc_b64_encode( buf );
522             free( buf );
523
524             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
525                         "Proxy-Authorization: Basic %s\r\n", b64 );
526             free( b64 );
527         }
528     }
529     else
530     {
531         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
532                     "GET %s HTTP/1.0\r\n"
533                     "Host: %s:%d\r\n",
534                     ( (p_sys->url.psz_path == NULL) ||
535                       (*p_sys->url.psz_path == '\0') ) ?
536                             "/" : p_sys->url.psz_path,
537                     p_sys->url.psz_host, p_sys->url.i_port );
538     }
539     return VLC_SUCCESS;
540 }
541
542 /*****************************************************************************
543  * Describe:
544  *****************************************************************************/
545 static int Describe( access_t  *p_access, char **ppsz_location )
546 {
547     access_sys_t *p_sys = p_access->p_sys;
548     char         *psz_location = NULL;
549     char         *psz;
550     int          i_code;
551
552     /* Reinit context */
553     p_sys->b_broadcast = true;
554     p_sys->i_request_context = 1;
555     p_sys->i_packet_sequence = 0;
556     p_sys->i_packet_used = 0;
557     p_sys->i_packet_length = 0;
558     p_sys->p_packet = NULL;
559
560     GenerateGuid ( &p_sys->guid );
561
562     if( OpenConnection( p_access ) )
563         return VLC_EGENERIC;
564
565     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
566                 "Accept: */*\r\n"
567                 "User-Agent: "MMSH_USER_AGENT"\r\n"
568                 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
569                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
570                 "Connection: Close\r\n",
571                 p_sys->i_request_context++,
572                 GUID_PRINT( p_sys->guid ) );
573
574     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
575     {
576         msg_Err( p_access, "failed to send request" );
577         goto error;
578     }
579
580     /* Receive the http header */
581     if( ( psz = net_Gets( p_access, p_sys->fd, NULL ) ) == NULL )
582     {
583         msg_Err( p_access, "failed to read answer" );
584         goto error;
585     }
586
587     if( strncmp( psz, "HTTP/1.", 7 ) )
588     {
589         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
590         free( psz );
591         goto error;
592     }
593     i_code = atoi( &psz[9] );
594     if( i_code >= 400 )
595     {
596         msg_Err( p_access, "error: %s", psz );
597         free( psz );
598         goto error;
599     }
600
601     msg_Dbg( p_access, "HTTP reply '%s'", psz );
602     free( psz );
603     for( ;; )
604     {
605         char *psz = net_Gets( p_access, p_sys->fd, NULL );
606         char *p;
607
608         if( psz == NULL )
609         {
610             msg_Err( p_access, "failed to read answer" );
611             goto error;
612         }
613
614         if( *psz == '\0' )
615         {
616             free( psz );
617             break;
618         }
619
620         if( ( p = strchr( psz, ':' ) ) == NULL )
621         {
622             msg_Err( p_access, "malformed header line: %s", psz );
623             free( psz );
624             goto error;
625         }
626         *p++ = '\0';
627         while( *p == ' ' ) p++;
628
629         /* FIXME FIXME test Content-Type to see if it's a plain stream or an
630          * asx FIXME */
631         if( !strcasecmp( psz, "Pragma" ) )
632         {
633             if( strstr( p, "features" ) )
634             {
635                 /* FIXME, it is a bit badly done here ..... */
636                 if( strstr( p, "broadcast" ) )
637                 {
638                     msg_Dbg( p_access, "stream type = broadcast" );
639                     p_sys->b_broadcast = true;
640                 }
641                 else if( strstr( p, "seekable" ) )
642                 {
643                     msg_Dbg( p_access, "stream type = seekable" );
644                     p_sys->b_broadcast = false;
645                 }
646                 else
647                 {
648                     msg_Warn( p_access, "unknow stream types (%s)", p );
649                     p_sys->b_broadcast = false;
650                 }
651             }
652         }
653         else if( !strcasecmp( psz, "Location" ) )
654         {
655             psz_location = strdup( p );
656         }
657
658         free( psz );
659     }
660
661     /* Handle the redirection */
662     if( ( (i_code == 301) || (i_code == 302) ||
663           (i_code == 303) || (i_code == 307) ) &&
664         psz_location && *psz_location )
665     {
666         msg_Dbg( p_access, "redirection to %s", psz_location );
667         net_Close( p_sys->fd ); p_sys->fd = -1;
668
669         *ppsz_location = psz_location;
670         return VLC_SUCCESS;
671     }
672     free( psz_location );
673
674     /* Read the asf header */
675     GetHeader( p_access );
676     if( p_sys->i_header <= 0 )
677     {
678         msg_Err( p_access, "header size == 0" );
679         goto error;
680     }
681     /* close this connection */
682     net_Close( p_sys->fd );
683     p_sys->fd = -1;
684
685     /* *** parse header and get stream and their id *** */
686     /* get all streams properties,
687      *
688      * TODO : stream bitrates properties(optional)
689      *        and bitrate mutual exclusion(optional) */
690     asf_HeaderParse ( &p_sys->asfh,
691                        p_sys->p_header, p_sys->i_header );
692     msg_Dbg( p_access, "packet count=%"PRId64" packet size=%d",
693              p_sys->asfh.i_data_packets_count,
694              p_sys->asfh.i_min_data_packet_size );
695
696     if( p_sys->asfh.i_min_data_packet_size <= 0 )
697         goto error;
698
699     asf_StreamSelect( &p_sys->asfh,
700                        var_CreateGetInteger( p_access, "mms-maxbitrate" ),
701                        var_CreateGetBool( p_access, "mms-all" ),
702                        var_CreateGetInteger( p_access, "audio" ),
703                        var_CreateGetInteger( p_access, "video" ) );
704     return VLC_SUCCESS;
705
706 error:
707     if( p_sys->fd > 0 )
708     {
709         net_Close( p_sys->fd  );
710         p_sys->fd = -1;
711     }
712     return VLC_EGENERIC;
713 }
714
715 static void GetHeader( access_t *p_access )
716 {
717     access_sys_t *p_sys = p_access->p_sys;
718
719     /* Read the asf header */
720     p_sys->i_header = 0;
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 = xrealloc( 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  * Start stream
743  ****************************************************************************/
744 static int Start( access_t *p_access, int64_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 = NULL;
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             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
806                         "ffff:%d:%d ", i, i_select );
807         }
808     }
809     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
810     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
811                 "Connection: Close\r\n" );
812
813     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
814     {
815         msg_Err( p_access, "failed to send request" );
816         return VLC_EGENERIC;
817     }
818
819     psz = net_Gets( p_access, p_sys->fd, NULL );
820     if( psz == NULL )
821     {
822         msg_Err( p_access, "cannot read data 0" );
823         return VLC_EGENERIC;
824     }
825
826     if( atoi( &psz[9] ) >= 400 )
827     {
828         msg_Err( p_access, "error: %s", psz );
829         free( psz );
830         return VLC_EGENERIC;
831     }
832     msg_Dbg( p_access, "HTTP reply '%s'", psz );
833     free( psz );
834
835     /* FIXME check HTTP code */
836     for( ;; )
837     {
838         char *psz = net_Gets( p_access, p_sys->fd, NULL );
839         if( psz == NULL )
840         {
841             msg_Err( p_access, "cannot read data 1" );
842             return VLC_EGENERIC;
843         }
844         if( *psz == '\0' )
845         {
846             free( psz );
847             break;
848         }
849         msg_Dbg( p_access, "%s", psz );
850         free( psz );
851     }
852
853     p_sys->i_packet_used   = 0;
854     p_sys->i_packet_length = 0;
855
856     return VLC_SUCCESS;
857 }
858
859 /*****************************************************************************
860  * closing stream
861  *****************************************************************************/
862 static void Stop( access_t *p_access )
863 {
864     access_sys_t *p_sys = p_access->p_sys;
865
866     msg_Dbg( p_access, "closing stream" );
867     if( p_sys->fd > 0 )
868     {
869         net_Close( p_sys->fd );
870         p_sys->fd = -1;
871     }
872 }
873
874 /*****************************************************************************
875  * get packet
876  *****************************************************************************/
877 static int GetPacket( access_t * p_access, chunk_t *p_ck )
878 {
879     access_sys_t *p_sys = p_access->p_sys;
880     int restsize;
881
882     /* chunk_t */
883     memset( p_ck, 0, sizeof( chunk_t ) );
884
885     /* Read the chunk header */
886     /* Some headers are short, like 0x4324. Reading 12 bytes will cause us
887      * to lose synchronization with the stream. Just read to the length
888      * (4 bytes), decode and then read up to 8 additional bytes to get the
889      * entire header.
890      */
891     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 4, true ) < 4 )
892     {
893        msg_Err( p_access, "cannot read data 2" );
894        return VLC_EGENERIC;
895     }
896
897     p_ck->i_type = GetWLE( p_sys->buffer);
898     p_ck->i_size = GetWLE( p_sys->buffer + 2);
899
900     restsize = p_ck->i_size;
901     if( restsize > 8 )
902         restsize = 8;
903
904     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer + 4, restsize, true ) < restsize )
905     {
906         msg_Err( p_access, "cannot read data 3" );
907         return VLC_EGENERIC;
908     }
909     p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
910     p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
911
912     /* Set i_size2 to 8 if this header was short, since a real value won't be
913      * present in the buffer. Using 8 avoid reading additional data for the
914      * packet.
915      */
916     if( restsize < 8 )
917         p_ck->i_size2 = 8;
918     else
919         p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
920
921     p_ck->p_data      = p_sys->buffer + 12;
922     p_ck->i_data      = p_ck->i_size2 - 8;
923
924     if( p_ck->i_type == 0x4524 )   // Transfer complete
925     {
926         if( p_ck->i_sequence == 0 )
927         {
928             msg_Warn( p_access, "EOF" );
929             return VLC_EGENERIC;
930         }
931         else
932         {
933             msg_Warn( p_access, "next stream following" );
934             return VLC_EGENERIC;
935         }
936     }
937     else if( p_ck->i_type == 0x4324 )
938     {
939         /* 0x4324 is CHUNK_TYPE_RESET: a new stream will follow with a sequence of 0 */
940         msg_Warn( p_access, "next stream following (reset) seq=%d", p_ck->i_sequence  );
941         return VLC_EGENERIC;
942     }
943     else if( (p_ck->i_type != 0x4824) && (p_ck->i_type != 0x4424) )
944     {
945         msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
946         return VLC_EGENERIC;
947     }
948
949     if( (p_ck->i_data > 0) &&
950         (net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12],
951                    p_ck->i_data, true ) < p_ck->i_data) )
952     {
953         msg_Err( p_access, "cannot read data 4" );
954         return VLC_EGENERIC;
955     }
956
957 #if 0
958     if( (p_sys->i_packet_sequence != 0) &&
959         (p_ck->i_sequence != p_sys->i_packet_sequence) )
960     {
961         msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
962     }
963 #endif
964
965     p_sys->i_packet_sequence = p_ck->i_sequence + 1;
966     p_sys->i_packet_used   = 0;
967     p_sys->i_packet_length = p_ck->i_data;
968     p_sys->p_packet        = p_ck->p_data;
969
970     return VLC_SUCCESS;
971 }