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