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