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