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