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