]> git.sesse.net Git - vlc/blob - modules/access/mms/mmsh.c
Removed unwanted debug messages.
[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_ItemNew( p_access, psz_location, psz_location );
192         input_ItemAddSubItem( 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             asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
543                        p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
544
545             b64 = vlc_b64_encode( buf );
546             free( buf );
547
548             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
549                         "Proxy-Authorization: Basic %s\r\n", b64 );
550             free( b64 );
551         }
552     }
553     else
554     {
555         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
556                     "GET %s HTTP/1.0\r\n"
557                     "Host: %s:%d\r\n",
558                     ( (p_sys->url.psz_path == NULL) ||
559                       (*p_sys->url.psz_path == '\0') ) ?
560                             "/" : p_sys->url.psz_path,
561                     p_sys->url.psz_host, p_sys->url.i_port );
562     }
563     return VLC_SUCCESS;
564 }
565
566 /*****************************************************************************
567  * Describe:
568  *****************************************************************************/
569 static int Describe( access_t  *p_access, char **ppsz_location )
570 {
571     access_sys_t *p_sys = p_access->p_sys;
572     char         *psz_location = NULL;
573     char         *psz;
574     int          i_code;
575
576     /* Reinit context */
577     p_sys->b_broadcast = true;
578     p_sys->i_request_context = 1;
579     p_sys->i_packet_sequence = 0;
580     p_sys->i_packet_used = 0;
581     p_sys->i_packet_length = 0;
582     p_sys->p_packet = NULL;
583      GenerateGuid ( &p_sys->guid );
584
585     if( OpenConnection( p_access ) )
586         return VLC_EGENERIC;
587
588     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
589                 "Accept: */*\r\n"
590                 "User-Agent: "MMSH_USER_AGENT"\r\n"
591                 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
592                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
593                 "Connection: Close\r\n",
594                 p_sys->i_request_context++,
595                 GUID_PRINT( p_sys->guid ) );
596
597     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
598     {
599         msg_Err( p_access, "failed to send request" );
600         goto error;
601     }
602
603     /* Receive the http header */
604     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
605     {
606         msg_Err( p_access, "failed to read answer" );
607         goto error;
608     }
609
610     if( strncmp( psz, "HTTP/1.", 7 ) )
611     {
612         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
613         free( psz );
614         goto error;
615     }
616     i_code = atoi( &psz[9] );
617     if( i_code >= 400 )
618     {
619         msg_Err( p_access, "error: %s", psz );
620         free( psz );
621         goto error;
622     }
623
624     msg_Dbg( p_access, "HTTP reply '%s'", psz );
625     free( psz );
626     for( ;; )
627     {
628         char *psz = net_Gets( p_access, p_sys->fd, NULL );
629         char *p;
630
631         if( psz == NULL )
632         {
633             msg_Err( p_access, "failed to read answer" );
634             goto error;
635         }
636
637         if( *psz == '\0' )
638         {
639             free( psz );
640             break;
641         }
642
643         if( ( p = strchr( psz, ':' ) ) == NULL )
644         {
645             msg_Err( p_access, "malformed header line: %s", psz );
646             free( psz );
647             goto error;
648         }
649         *p++ = '\0';
650         while( *p == ' ' ) p++;
651
652         /* FIXME FIXME test Content-Type to see if it's a plain stream or an
653          * asx FIXME */
654         if( !strcasecmp( psz, "Pragma" ) )
655         {
656             if( strstr( p, "features" ) )
657             {
658                 /* FIXME, it is a bit badly done here ..... */
659                 if( strstr( p, "broadcast" ) )
660                 {
661                     msg_Dbg( p_access, "stream type = broadcast" );
662                     p_sys->b_broadcast = true;
663                 }
664                 else if( strstr( p, "seekable" ) )
665                 {
666                     msg_Dbg( p_access, "stream type = seekable" );
667                     p_sys->b_broadcast = false;
668                 }
669                 else
670                 {
671                     msg_Warn( p_access, "unknow stream types (%s)", p );
672                     p_sys->b_broadcast = false;
673                 }
674             }
675         }
676         else if( !strcasecmp( psz, "Location" ) )
677         {
678             psz_location = strdup( p );
679         }
680
681         free( psz );
682     }
683
684     /* Handle the redirection */
685     if( ( (i_code == 301) || (i_code == 302) ||
686           (i_code == 303) || (i_code == 307) ) &&
687         psz_location && *psz_location )
688     {
689         msg_Dbg( p_access, "redirection to %s", psz_location );
690         net_Close( p_sys->fd ); p_sys->fd = -1;
691
692         *ppsz_location = psz_location;
693         return VLC_SUCCESS;
694     }
695
696     /* Read the asf header */
697     GetHeader( p_access );
698     if( p_sys->i_header <= 0 )
699     {
700         msg_Err( p_access, "header size == 0" );
701         goto error;
702     }
703     /* close this connection */
704     net_Close( p_sys->fd );
705     p_sys->fd = -1;
706
707     /* *** parse header and get stream and their id *** */
708     /* get all streams properties,
709      *
710      * TODO : stream bitrates properties(optional)
711      *        and bitrate mutual exclusion(optional) */
712      asf_HeaderParse ( &p_sys->asfh,
713                            p_sys->p_header, p_sys->i_header );
714     msg_Dbg( p_access, "packet count=%"PRId64" packet size=%d",
715              p_sys->asfh.i_data_packets_count,
716              p_sys->asfh.i_min_data_packet_size );
717
718      asf_StreamSelect( &p_sys->asfh,
719                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
720                            var_CreateGetInteger( p_access, "mms-all" ),
721                            var_CreateGetInteger( p_access, "audio" ),
722                            var_CreateGetInteger( p_access, "video" ) );
723     return VLC_SUCCESS;
724
725 error:
726     if( p_sys->fd > 0 )
727     {
728         net_Close( p_sys->fd  );
729         p_sys->fd = -1;
730     }
731     return VLC_EGENERIC;
732 }
733
734 static void GetHeader( access_t *p_access )
735 {
736     access_sys_t *p_sys = p_access->p_sys;
737
738     /* Read the asf header */
739     p_sys->i_header = 0;
740     free( p_sys->p_header  );
741     p_sys->p_header = NULL;
742     for( ;; )
743     {
744         chunk_t ck;
745         if( GetPacket( p_access, &ck ) || ck.i_type != 0x4824 )
746             break;
747
748         if( ck.i_data > 0 )
749         {
750             p_sys->i_header += ck.i_data;
751             p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
752             memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
753                     ck.p_data, ck.i_data );
754         }
755     }
756     msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
757 }
758
759
760 /*****************************************************************************
761  * Start stream
762  ****************************************************************************/
763 static int Start( access_t *p_access, int64_t i_pos )
764 {
765     access_sys_t *p_sys = p_access->p_sys;
766     int  i_streams = 0;
767     int  i_streams_selected = 0;
768     int  i;
769     char *psz = NULL;
770
771     msg_Dbg( p_access, "starting stream" );
772
773     for( i = 1; i < 128; i++ )
774     {
775         if( p_sys->asfh.stream[i].i_cat == ASF_STREAM_UNKNOWN )
776             continue;
777         i_streams++;
778         if( p_sys->asfh.stream[i].i_selected )
779             i_streams_selected++;
780     }
781     if( i_streams_selected <= 0 )
782     {
783         msg_Err( p_access, "no stream selected" );
784         return VLC_EGENERIC;
785     }
786
787     if( OpenConnection( p_access ) )
788         return VLC_EGENERIC;
789
790     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
791                 "Accept: */*\r\n"
792                 "User-Agent: "MMSH_USER_AGENT"\r\n" );
793     if( p_sys->b_broadcast )
794     {
795         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
796                     "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
797                     p_sys->i_request_context++ );
798     }
799     else
800     {
801         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
802                     "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
803                     (uint32_t)((i_pos >> 32)&0xffffffff),
804                     (uint32_t)(i_pos&0xffffffff),
805                     p_sys->i_request_context++ );
806     }
807     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
808                 "Pragma: xPlayStrm=1\r\n"
809                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
810                 "Pragma: stream-switch-count=%d\r\n"
811                 "Pragma: stream-switch-entry=",
812                 GUID_PRINT( p_sys->guid ),
813                 i_streams);
814
815     for( i = 1; i < 128; i++ )
816     {
817         if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
818         {
819             int i_select = 2;
820             if( p_sys->asfh.stream[i].i_selected )
821             {
822                 i_select = 0;
823             }
824             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
825                         "ffff:%d:%d ", i, i_select );
826         }
827     }
828     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
829     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
830                 "Connection: Close\r\n" );
831
832     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
833     {
834         msg_Err( p_access, "failed to send request" );
835         return VLC_EGENERIC;
836     }
837
838     psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
839     if( psz == NULL )
840     {
841         msg_Err( p_access, "cannot read data 0" );
842         return VLC_EGENERIC;
843     }
844
845     if( atoi( &psz[9] ) >= 400 )
846     {
847         msg_Err( p_access, "error: %s", psz );
848         free( psz );
849         return VLC_EGENERIC;
850     }
851     msg_Dbg( p_access, "HTTP reply '%s'", psz );
852     free( psz );
853
854     /* FIXME check HTTP code */
855     for( ;; )
856     {
857         char *psz = net_Gets( p_access, p_sys->fd, NULL );
858         if( psz == NULL )
859         {
860             msg_Err( p_access, "cannot read data 1" );
861             return VLC_EGENERIC;
862         }
863         if( *psz == '\0' )
864         {
865             free( psz );
866             break;
867         }
868         msg_Dbg( p_access, "%s", psz );
869         free( psz );
870     }
871
872     p_sys->i_packet_used   = 0;
873     p_sys->i_packet_length = 0;
874
875     return VLC_SUCCESS;
876 }
877
878 /*****************************************************************************
879  * closing stream
880  *****************************************************************************/
881 static void Stop( access_t *p_access )
882 {
883     access_sys_t *p_sys = p_access->p_sys;
884
885     msg_Dbg( p_access, "closing stream" );
886     if( p_sys->fd > 0 )
887     {
888         net_Close( p_sys->fd );
889         p_sys->fd = -1;
890     }
891 }
892
893 /*****************************************************************************
894  * get packet
895  *****************************************************************************/
896 static int GetPacket( access_t * p_access, chunk_t *p_ck )
897 {
898     access_sys_t *p_sys = p_access->p_sys;
899     int restsize;
900
901     /* chunk_t */
902     memset( p_ck, 0, sizeof( chunk_t ) );
903
904     /* Read the chunk header */
905     /* Some headers are short, like 0x4324. Reading 12 bytes will cause us
906      * to lose synchronization with the stream. Just read to the length
907      * (4 bytes), decode and then read up to 8 additional bytes to get the
908      * entire header.
909      */
910     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 4, true ) < 4 )
911     {
912        msg_Err( p_access, "cannot read data 2" );
913        return VLC_EGENERIC;
914     }
915
916     p_ck->i_type = GetWLE( p_sys->buffer);
917     p_ck->i_size = GetWLE( p_sys->buffer + 2);
918
919     restsize = p_ck->i_size;
920     if( restsize > 8 )
921         restsize = 8;
922
923     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer + 4, restsize, true ) < restsize )
924     {
925         msg_Err( p_access, "cannot read data 3" );
926         return VLC_EGENERIC;
927     }
928     p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
929     p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
930
931     /* Set i_size2 to 8 if this header was short, since a real value won't be
932      * present in the buffer. Using 8 avoid reading additional data for the
933      * packet.
934      */
935     if( restsize < 8 )
936         p_ck->i_size2 = 8;
937     else
938         p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
939
940     p_ck->p_data      = p_sys->buffer + 12;
941     p_ck->i_data      = p_ck->i_size2 - 8;
942
943     if( p_ck->i_type == 0x4524 )   // Transfer complete
944     {
945         if( p_ck->i_sequence == 0 )
946         {
947             msg_Warn( p_access, "EOF" );
948             return VLC_EGENERIC;
949         }
950         else
951         {
952             msg_Warn( p_access, "next stream following" );
953             return VLC_EGENERIC;
954         }
955     }
956     else if( p_ck->i_type == 0x4324 )
957     {
958         /* 0x4324 is CHUNK_TYPE_RESET: a new stream will follow with a sequence of 0 */
959         msg_Warn( p_access, "next stream following (reset) seq=%d", p_ck->i_sequence  );
960         return VLC_EGENERIC;
961     }
962     else if( (p_ck->i_type != 0x4824) && (p_ck->i_type != 0x4424) )
963     {
964         msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
965         return VLC_EGENERIC;
966     }
967
968     if( (p_ck->i_data > 0) &&
969         (net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12],
970                    p_ck->i_data, true ) < p_ck->i_data) )
971     {
972         msg_Err( p_access, "cannot read data 4" );
973         return VLC_EGENERIC;
974     }
975
976 #if 0
977     if( (p_sys->i_packet_sequence != 0) &&
978         (p_ck->i_sequence != p_sys->i_packet_sequence) )
979     {
980         msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
981     }
982 #endif
983
984     p_sys->i_packet_sequence = p_ck->i_sequence + 1;
985     p_sys->i_packet_used   = 0;
986     p_sys->i_packet_length = p_ck->i_data;
987     p_sys->p_packet        = p_ck->p_data;
988
989     return VLC_SUCCESS;
990 }