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