1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
32 #include "vlc_playlist.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 int E_(MMSHOpen) ( access_t * );
51 void E_(MMSHClose) ( access_t * );
53 static int Read( access_t *, uint8_t *, int );
54 static int ReadRedirect( access_t *, uint8_t *, int );
55 static int Seek( access_t *, int64_t );
56 static int Control( access_t *, int, va_list );
58 static int Describe( access_t *, char **ppsz_location );
59 static int Start( access_t *, int64_t );
60 static void Stop( access_t * );
61 static int GetPacket( access_t *, chunk_t * );
63 /****************************************************************************
64 * Open: connect to ftp server and ask for file
65 ****************************************************************************/
66 int E_(MMSHOpen)( access_t *p_access )
69 char *psz_location = NULL;
74 p_access->pf_read = Read;
75 p_access->pf_block = NULL;
76 p_access->pf_control = Control;
77 p_access->pf_seek = Seek;
78 p_access->info.i_update = 0;
79 p_access->info.i_size = 0;
80 p_access->info.i_pos = 0;
81 p_access->info.b_eof = VLC_FALSE;
82 p_access->info.i_title = 0;
83 p_access->info.i_seekpoint = 0;
84 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
85 memset( p_sys, 0, sizeof( access_sys_t ) );
86 p_sys->i_proto= MMS_PROTO_HTTP;
90 /* open a tcp connection */
91 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
92 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
94 msg_Err( p_access, "invalid host" );
95 vlc_UrlClean( &p_sys->url );
99 if( p_sys->url.i_port <= 0 )
100 p_sys->url.i_port = 80;
102 if( Describe( p_access, &psz_location ) )
104 vlc_UrlClean( &p_sys->url );
108 /* Handle redirection */
109 if( psz_location && *psz_location )
111 playlist_t * p_playlist = pl_Yield( p_access );
112 msg_Dbg( p_access, "redirection to %s", psz_location );
114 /** \bug we do not autodelete here */
115 playlist_PlaylistAdd( p_playlist, psz_location, psz_location,
116 PLAYLIST_INSERT | PLAYLIST_GO, PLAYLIST_END );
117 vlc_object_release( p_playlist );
119 free( psz_location );
121 p_access->pf_read = ReadRedirect;
126 if( Start( p_access, 0 ) )
128 msg_Err( p_access, "cannot start stream" );
129 free( p_sys->p_header );
130 vlc_UrlClean( &p_sys->url );
135 if( !p_sys->b_broadcast )
137 p_access->info.i_size = p_sys->asfh.i_file_size;
143 /*****************************************************************************
144 * Close: free unused data structures
145 *****************************************************************************/
146 void E_( MMSHClose )( access_t *p_access )
148 access_sys_t *p_sys = p_access->p_sys;
154 /*****************************************************************************
156 *****************************************************************************/
157 static int Control( access_t *p_access, int i_query, va_list args )
159 access_sys_t *p_sys = p_access->p_sys;
168 case ACCESS_CAN_SEEK:
169 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
170 *pb_bool = !p_sys->b_broadcast;
173 case ACCESS_CAN_FASTSEEK:
174 case ACCESS_CAN_PAUSE:
175 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
176 *pb_bool = VLC_FALSE;
179 case ACCESS_CAN_CONTROL_PACE:
180 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
182 #if 0 /* Disable for now until we have a clock synchro algo
183 * which works with something else than MPEG over UDP */
184 *pb_bool = VLC_FALSE;
191 pi_int = (int*)va_arg( args, int * );
192 *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
195 case ACCESS_GET_PTS_DELAY:
196 pi_64 = (int64_t*)va_arg( args, int64_t * );
197 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
200 case ACCESS_GET_PRIVATE_ID_STATE:
201 i_int = (int)va_arg( args, int );
202 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
204 if( i_int < 0 || i_int > 127 )
206 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
210 case ACCESS_SET_PAUSE_STATE:
211 case ACCESS_GET_TITLE_INFO:
212 case ACCESS_SET_TITLE:
213 case ACCESS_SET_SEEKPOINT:
214 case ACCESS_SET_PRIVATE_ID_STATE:
218 msg_Warn( p_access, "unimplemented query in control" );
225 /*****************************************************************************
226 * Seek: try to go at the right place
227 *****************************************************************************/
228 static int Seek( access_t *p_access, int64_t i_pos )
230 access_sys_t *p_sys = p_access->p_sys;
235 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
237 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
238 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
241 Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
243 while( !p_access->b_die )
245 if( GetPacket( p_access, &ck ) )
249 if( ck.i_type != 0x4824 )
252 msg_Warn( p_access, "skipping header" );
255 p_access->info.i_pos = i_pos;
256 p_access->info.b_eof = VLC_FALSE;
257 p_sys->i_packet_used += i_offset;
262 /*****************************************************************************
264 *****************************************************************************/
265 static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
270 /*****************************************************************************
272 *****************************************************************************/
273 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
275 access_sys_t *p_sys = p_access->p_sys;
279 if( p_access->info.b_eof )
282 while( i_data < (size_t) i_len )
284 if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
286 int i_offset = p_access->info.i_pos - p_sys->i_start;
287 i_copy = __MIN( p_sys->i_header - i_offset, (int)((size_t)i_len - i_data) );
288 memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
291 p_access->info.i_pos += i_copy;
293 else if( p_sys->i_packet_used < p_sys->i_packet_length )
295 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
298 memcpy( &p_buffer[i_data],
299 &p_sys->p_packet[p_sys->i_packet_used],
303 p_sys->i_packet_used += i_copy;
304 p_access->info.i_pos += i_copy;
306 else if( (p_sys->i_packet_length > 0) &&
307 ((int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size) )
309 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
312 memset( &p_buffer[i_data], 0, i_copy );
315 p_sys->i_packet_used += i_copy;
316 p_access->info.i_pos += i_copy;
321 if( GetPacket( p_access, &ck ) )
323 if( (ck.i_type == 0x4524) && (ck.i_sequence != 0) &&
324 (p_sys->b_broadcast) )
326 char *psz_location = NULL;
328 p_sys->i_start = p_access->info.i_pos;
330 msg_Dbg( p_access, "stoping the stream" );
333 msg_Dbg( p_access, "describe the stream" );
334 if( Describe( p_access, &psz_location ) )
336 msg_Err( p_access, "describe failed" );
337 p_access->info.b_eof = VLC_TRUE;
340 if( Start( p_access, 0 ) )
342 msg_Err( p_access, "Start failed" );
343 p_access->info.b_eof = VLC_TRUE;
349 p_access->info.b_eof = VLC_TRUE;
353 if( ck.i_type != 0x4424 )
355 p_sys->i_packet_used = 0;
356 p_sys->i_packet_length = 0;
364 /*****************************************************************************
366 *****************************************************************************/
367 static int Describe( access_t *p_access, char **ppsz_location )
369 access_sys_t *p_sys = p_access->p_sys;
370 char *psz_location = NULL;
375 p_sys->b_broadcast = VLC_TRUE;
376 p_sys->i_request_context = 1;
377 p_sys->i_packet_sequence = 0;
378 p_sys->i_packet_used = 0;
379 p_sys->i_packet_length = 0;
380 p_sys->p_packet = NULL;
381 E_( GenerateGuid )( &p_sys->guid );
383 if( ( p_sys->fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
384 p_sys->url.i_port ) ) < 0 )
386 msg_Err( p_access, "cannot connect to %s:%d", p_sys->url.psz_host,
391 /* send first request */
392 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
393 "GET %s HTTP/1.0\r\n"
395 "User-Agent: NSPlayer/4.1.0.3856\r\n"
397 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
398 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
399 "Connection: Close\r\n",
400 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
401 p_sys->url.psz_host, p_sys->url.i_port,
402 p_sys->i_request_context++,
403 GUID_PRINT( p_sys->guid ) );
405 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
407 msg_Err( p_access, "failed to send request" );
411 /* Receive the http header */
412 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
414 msg_Err( p_access, "failed to read answer" );
417 if( strncmp( psz, "HTTP/1.", 7 ) )
419 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
423 i_code = atoi( &psz[9] );
426 msg_Err( p_access, "error: %s", psz );
431 msg_Dbg( p_access, "HTTP reply '%s'", psz );
435 char *psz = net_Gets( p_access, p_sys->fd, NULL );
440 msg_Err( p_access, "failed to read answer" );
450 if( ( p = strchr( psz, ':' ) ) == NULL )
452 msg_Err( p_access, "malformed header line: %s", psz );
457 while( *p == ' ' ) p++;
459 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
461 if( !strcasecmp( psz, "Pragma" ) )
463 if( strstr( p, "features" ) )
465 /* FIXME, it is a bit badly done here ..... */
466 if( strstr( p, "broadcast" ) )
468 msg_Dbg( p_access, "stream type = broadcast" );
469 p_sys->b_broadcast = VLC_TRUE;
471 else if( strstr( p, "seekable" ) )
473 msg_Dbg( p_access, "stream type = seekable" );
474 p_sys->b_broadcast = VLC_FALSE;
478 msg_Warn( p_access, "unknow stream types (%s)", p );
479 p_sys->b_broadcast = VLC_FALSE;
483 else if( !strcasecmp( psz, "Location" ) )
485 psz_location = strdup( p );
491 /* Handle the redirection */
492 if( ( i_code == 301 || i_code == 302 ||
493 i_code == 303 || i_code == 307 ) &&
494 psz_location && *psz_location )
496 msg_Dbg( p_access, "redirection to %s", psz_location );
497 net_Close( p_sys->fd ); p_sys->fd = -1;
499 *ppsz_location = psz_location;
503 /* Read the asf header */
505 p_sys->p_header = NULL;
509 if( GetPacket( p_access, &ck ) ||
510 (ck.i_type != 0x4824) )
517 p_sys->i_header += ck.i_data;
518 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
519 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
520 ck.p_data, ck.i_data );
523 msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
524 if( p_sys->i_header <= 0 )
526 msg_Err( p_access, "header size == 0" );
529 /* close this connection */
530 net_Close( p_sys->fd ); p_sys->fd = -1;
532 /* *** parse header and get stream and their id *** */
533 /* get all streams properties,
535 * TODO : stream bitrates properties(optional)
536 * and bitrate mutual exclusion(optional) */
537 E_( asf_HeaderParse )( &p_sys->asfh,
538 p_sys->p_header, p_sys->i_header );
539 msg_Dbg( p_access, "packet count="I64Fd" packet size=%d",
540 p_sys->asfh.i_data_packets_count,
541 p_sys->asfh.i_min_data_packet_size );
543 E_( asf_StreamSelect)( &p_sys->asfh,
544 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
545 var_CreateGetInteger( p_access, "mms-all" ),
546 var_CreateGetInteger( p_access, "audio" ),
547 var_CreateGetInteger( p_access, "video" ) );
553 net_Close( p_sys->fd );
559 /*****************************************************************************
561 *****************************************************************************/
562 static int Start( access_t *p_access, off_t i_pos )
564 access_sys_t *p_sys = p_access->p_sys;
569 msg_Dbg( p_access, "starting stream" );
571 if( ( p_sys->fd = net_ConnectTCP( p_access, p_sys->url.psz_host,
572 p_sys->url.i_port ) ) < 0 )
574 /* should not occur */
575 msg_Err( p_access, "cannot connect to the server" );
579 for( i = 1; i < 128; i++ )
581 if( p_sys->asfh.stream[i].i_selected )
589 msg_Err( p_access, "no stream selected" );
592 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
593 "GET %s HTTP/1.0\r\n"
595 "User-Agent: NSPlayer/4.1.0.3856\r\n"
597 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
598 p_sys->url.psz_host, p_sys->url.i_port );
599 if( p_sys->b_broadcast )
601 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
602 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
603 p_sys->i_request_context++ );
607 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
608 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
609 (uint32_t)((i_pos >> 32)&0xffffffff),
610 (uint32_t)(i_pos&0xffffffff),
611 p_sys->i_request_context++ );
613 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
614 "Pragma: xPlayStrm=1\r\n"
615 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
616 "Pragma: stream-switch-count=%d\r\n"
617 "Pragma: stream-switch-entry=",
618 GUID_PRINT( p_sys->guid ),
621 for( i = 1; i < 128; i++ )
623 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
626 if( p_sys->asfh.stream[i].i_selected )
631 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
632 "ffff:%d:%d ", i, i_select );
635 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
636 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
637 "Connection: Close\r\n" );
639 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
641 msg_Err( p_access, "failed to send request" );
645 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
647 msg_Err( p_access, "cannot read data" );
650 if( atoi( &psz[9] ) >= 400 )
652 msg_Err( p_access, "error: %s", psz );
656 msg_Dbg( p_access, "HTTP reply '%s'", psz );
659 /* FIXME check HTTP code */
662 char *psz = net_Gets( p_access, p_sys->fd, NULL );
665 msg_Err( p_access, "cannot read data" );
673 msg_Dbg( p_access, "%s", psz );
677 p_sys->i_packet_used = 0;
678 p_sys->i_packet_length = 0;
683 /*****************************************************************************
685 *****************************************************************************/
686 static void Stop( access_t *p_access )
688 access_sys_t *p_sys = p_access->p_sys;
690 msg_Dbg( p_access, "closing stream" );
693 net_Close( p_sys->fd );
698 /*****************************************************************************
700 *****************************************************************************/
701 static int GetPacket( access_t * p_access, chunk_t *p_ck )
703 access_sys_t *p_sys = p_access->p_sys;
707 memset( p_ck, 0, sizeof( chunk_t ) );
709 /* Read the chunk header */
710 /* Some headers are short, like 0x4324. Reading 12 bytes will cause us
711 * to lose synchronization with the stream. Just read to the length
712 * (4 bytes), decode and then read up to 8 additional bytes to get the
715 if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 4, VLC_TRUE ) < 4 )
717 msg_Err( p_access, "cannot read data" );
720 p_ck->i_type = GetWLE( p_sys->buffer);
721 p_ck->i_size = GetWLE( p_sys->buffer + 2);
723 restsize = p_ck->i_size;
727 if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer + 4, restsize, VLC_TRUE ) < restsize )
729 msg_Err( p_access, "cannot read data" );
732 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
733 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
735 /* Set i_size2 to 8 if this header was short, since a real value won't be
736 * present in the buffer. Using 8 avoid reading additional data for the
742 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
744 p_ck->p_data = p_sys->buffer + 12;
745 p_ck->i_data = p_ck->i_size2 - 8;
747 if( p_ck->i_type == 0x4524 ) // Transfer complete
749 if( p_ck->i_sequence == 0 )
751 msg_Warn( p_access, "EOF" );
756 msg_Warn( p_access, "next stream follow but not supported" );
760 /* 0x4324 is CHUNK_TYPE_RESET. We can safely ignore it: a new stream will
761 * follow with a sequence of 0 */
762 else if( (p_ck->i_type != 0x4824) && (p_ck->i_type != 0x4424) &&
763 (p_ck->i_type != 0x4324) )
765 msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
769 if( (p_ck->i_data > 0) &&
770 (net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12],
771 p_ck->i_data, VLC_TRUE ) < p_ck->i_data) )
773 msg_Err( p_access, "cannot read data" );
777 if( (p_sys->i_packet_sequence != 0) &&
778 (p_ck->i_sequence != p_sys->i_packet_sequence) )
780 msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
783 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
784 p_sys->i_packet_used = 0;
785 p_sys->i_packet_length = p_ck->i_data;
786 p_sys->p_packet = p_ck->p_data;