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