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