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