1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
32 #include "vlc_playlist.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 int E_(MMSHOpen) ( access_t * );
50 void E_(MMSHClose) ( access_t * );
52 static int Read( access_t *, uint8_t *, int );
53 static int ReadRedirect( access_t *, uint8_t *, int );
54 static int Seek( access_t *, int64_t );
55 static int Control( access_t *, int, va_list );
57 static int Describe( access_t *, char **ppsz_location );
58 static int Start( access_t *, int64_t );
59 static void Stop( access_t * );
60 static int GetPacket( access_t *, chunk_t * );
62 /****************************************************************************
63 * Open: connect to ftp server and ask for file
64 ****************************************************************************/
65 int E_(MMSHOpen)( access_t *p_access )
68 char *psz_location = NULL;
73 p_access->pf_read = Read;
74 p_access->pf_block = NULL;
75 p_access->pf_control = Control;
76 p_access->pf_seek = Seek;
77 p_access->info.i_update = 0;
78 p_access->info.i_size = 0;
79 p_access->info.i_pos = 0;
80 p_access->info.b_eof = VLC_FALSE;
81 p_access->info.i_title = 0;
82 p_access->info.i_seekpoint = 0;
83 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
84 memset( p_sys, 0, sizeof( access_sys_t ) );
85 p_sys->i_proto= MMS_PROTO_HTTP;
89 /* open a tcp connection */
90 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
91 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
93 msg_Err( p_access, "invalid host" );
94 vlc_UrlClean( &p_sys->url );
98 if( p_sys->url.i_port <= 0 )
99 p_sys->url.i_port = 80;
101 if( Describe( p_access, &psz_location ) )
103 vlc_UrlClean( &p_sys->url );
107 /* Handle redirection */
108 if( psz_location && *psz_location )
110 playlist_t * p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_PARENT );
112 msg_Dbg( p_access, "redirection to %s", psz_location );
116 msg_Err( p_access, "redirection failed: can't find playlist" );
117 free( psz_location );
120 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
121 playlist_Add( p_playlist, psz_location, psz_location,
122 PLAYLIST_INSERT | PLAYLIST_GO,
123 p_playlist->i_index + 1 );
124 vlc_object_release( p_playlist );
126 free( psz_location );
128 p_access->pf_read = ReadRedirect;
133 if( Start( p_access, 0 ) )
135 msg_Err( p_access, "cannot start stream" );
136 free( p_sys->p_header );
137 vlc_UrlClean( &p_sys->url );
142 if( !p_sys->b_broadcast )
144 p_access->info.i_size = p_sys->asfh.i_file_size;
150 /*****************************************************************************
151 * Close: free unused data structures
152 *****************************************************************************/
153 void E_( MMSHClose )( access_t *p_access )
155 access_sys_t *p_sys = p_access->p_sys;
161 /*****************************************************************************
163 *****************************************************************************/
164 static int Control( access_t *p_access, int i_query, va_list args )
166 access_sys_t *p_sys = p_access->p_sys;
174 case ACCESS_CAN_SEEK:
175 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
176 *pb_bool = !p_sys->b_broadcast;
178 case ACCESS_CAN_FASTSEEK:
179 case ACCESS_CAN_PAUSE:
180 case ACCESS_CAN_CONTROL_PACE:
181 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
182 *pb_bool = VLC_FALSE;
187 pi_int = (int*)va_arg( args, int * );
188 *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
191 case ACCESS_GET_PTS_DELAY:
192 pi_64 = (int64_t*)va_arg( args, int64_t * );
193 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
197 case ACCESS_SET_PAUSE_STATE:
198 case ACCESS_GET_TITLE_INFO:
199 case ACCESS_SET_TITLE:
200 case ACCESS_SET_SEEKPOINT:
201 case ACCESS_SET_PRIVATE_ID_STATE:
205 msg_Warn( p_access, "unimplemented query in control" );
212 /*****************************************************************************
213 * Seek: try to go at the right place
214 *****************************************************************************/
215 static int Seek( access_t *p_access, int64_t i_pos )
217 access_sys_t *p_sys = p_access->p_sys;
222 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
224 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
225 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
228 Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
230 while( !p_access->b_die )
232 if( GetPacket( p_access, &ck ) )
236 if( ck.i_type != 0x4824 )
239 msg_Warn( p_access, "skipping header" );
242 p_access->info.i_pos = i_pos;
243 p_access->info.b_eof = VLC_FALSE;
244 p_sys->i_packet_used += i_offset;
249 /*****************************************************************************
251 *****************************************************************************/
252 static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
257 /*****************************************************************************
259 *****************************************************************************/
260 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
262 access_sys_t *p_sys = p_access->p_sys;
266 if( p_access->info.b_eof )
269 while( i_data < i_len )
271 if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
273 int i_offset = p_access->info.i_pos - p_sys->i_start;
274 i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
275 memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
278 p_access->info.i_pos += i_copy;
280 else if( p_sys->i_packet_used < p_sys->i_packet_length )
282 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
285 memcpy( &p_buffer[i_data],
286 &p_sys->p_packet[p_sys->i_packet_used],
290 p_sys->i_packet_used += i_copy;
291 p_access->info.i_pos += i_copy;
293 else if( p_sys->i_packet_length > 0 &&
294 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
296 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
299 memset( &p_buffer[i_data], 0, i_copy );
302 p_sys->i_packet_used += i_copy;
303 p_access->info.i_pos += i_copy;
308 if( GetPacket( p_access, &ck ) )
310 if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
312 char *psz_location = NULL;
314 p_sys->i_start = p_access->info.i_pos;
316 msg_Dbg( p_access, "stoping the stream" );
319 msg_Dbg( p_access, "describe the stream" );
320 if( Describe( p_access, &psz_location ) )
322 msg_Err( p_access, "describe failed" );
323 p_access->info.b_eof = VLC_TRUE;
326 if( Start( p_access, 0 ) )
328 msg_Err( p_access, "Start failed" );
329 p_access->info.b_eof = VLC_TRUE;
335 p_access->info.b_eof = VLC_TRUE;
339 if( ck.i_type != 0x4424 )
341 p_sys->i_packet_used = 0;
342 p_sys->i_packet_length = 0;
350 /*****************************************************************************
352 *****************************************************************************/
353 static int Describe( access_t *p_access, char **ppsz_location )
355 access_sys_t *p_sys = p_access->p_sys;
356 char *psz_location = NULL;
361 p_sys->b_broadcast = VLC_TRUE;
362 p_sys->i_request_context = 1;
363 p_sys->i_packet_sequence = 0;
364 p_sys->i_packet_used = 0;
365 p_sys->i_packet_length = 0;
366 p_sys->p_packet = NULL;
367 E_( GenerateGuid )( &p_sys->guid );
369 if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
370 p_sys->url.i_port ) ) < 0 )
372 msg_Err( p_access, "cannot connect to%s:%d", p_sys->url.psz_host, p_sys->url.i_port );
376 /* send first request */
377 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
378 "GET %s HTTP/1.0\r\n"
380 "User-Agent: NSPlayer/4.1.0.3856\r\n"
382 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
383 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
384 "Connection: Close\r\n",
385 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
386 p_sys->url.psz_host, p_sys->url.i_port,
387 p_sys->i_request_context++,
388 GUID_PRINT( p_sys->guid ) );
390 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
392 msg_Err( p_access, "failed to send request" );
396 /* Receive the http header */
397 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
399 msg_Err( p_access, "failed to read answer" );
402 if( strncmp( psz, "HTTP/1.", 7 ) )
404 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
408 i_code = atoi( &psz[9] );
411 msg_Err( p_access, "error: %s", psz );
416 msg_Dbg( p_access, "HTTP reply '%s'", psz );
420 char *psz = net_Gets( p_access, p_sys->fd );
425 msg_Err( p_access, "failed to read answer" );
435 if( ( p = strchr( psz, ':' ) ) == NULL )
437 msg_Err( p_access, "malformed header line: %s", psz );
442 while( *p == ' ' ) p++;
444 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
446 if( !strcasecmp( psz, "Pragma" ) )
448 if( strstr( p, "features" ) )
450 /* FIXME, it is a bit badly done here ..... */
451 if( strstr( p, "broadcast" ) )
453 msg_Dbg( p_access, "stream type = broadcast" );
454 p_sys->b_broadcast = VLC_TRUE;
456 else if( strstr( p, "seekable" ) )
458 msg_Dbg( p_access, "stream type = seekable" );
459 p_sys->b_broadcast = VLC_FALSE;
463 msg_Warn( p_access, "unknow stream types (%s)", p );
464 p_sys->b_broadcast = VLC_FALSE;
468 else if( !strcasecmp( psz, "Location" ) )
470 psz_location = strdup( p );
476 /* Handle the redirection */
477 if( ( i_code == 301 || i_code == 302 ||
478 i_code == 303 || i_code == 307 ) &&
479 psz_location && *psz_location )
481 msg_Dbg( p_access, "redirection to %s", psz_location );
482 net_Close( p_sys->fd ); p_sys->fd = -1;
484 *ppsz_location = psz_location;
488 /* Read the asf header */
490 p_sys->p_header = NULL;
494 if( GetPacket( p_access, &ck ) ||
495 ck.i_type != 0x4824 )
502 p_sys->i_header += ck.i_data;
503 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
504 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
505 ck.p_data, ck.i_data );
508 msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
509 if( p_sys->i_header <= 0 )
511 msg_Err( p_access, "header size == 0" );
514 /* close this connection */
515 net_Close( p_sys->fd ); p_sys->fd = -1;
517 /* *** parse header and get stream and their id *** */
518 /* get all streams properties,
520 * TODO : stream bitrates properties(optional)
521 * and bitrate mutual exclusion(optional) */
522 E_( asf_HeaderParse )( &p_sys->asfh,
523 p_sys->p_header, p_sys->i_header );
524 msg_Dbg( p_access, "packet count=%lld packet size=%d",
525 p_sys->asfh.i_data_packets_count,
526 p_sys->asfh.i_min_data_packet_size );
528 E_( asf_StreamSelect)( &p_sys->asfh,
529 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
530 var_CreateGetInteger( p_access, "mms-all" ),
531 var_CreateGetInteger( p_access, "audio" ),
532 var_CreateGetInteger( p_access, "video" ) );
539 net_Close( p_sys->fd );
545 /*****************************************************************************
547 *****************************************************************************/
548 static int Start( access_t *p_access, off_t i_pos )
550 access_sys_t *p_sys = p_access->p_sys;
555 msg_Dbg( p_access, "starting stream" );
557 if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
558 p_sys->url.i_port ) ) < 0 )
560 /* should not occur */
561 msg_Err( p_access, "cannot connect to the server" );
565 for( i = 1; i < 128; i++ )
567 if( p_sys->asfh.stream[i].i_selected )
575 msg_Err( p_access, "no stream selected" );
578 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
579 "GET %s HTTP/1.0\r\n"
581 "User-Agent: NSPlayer/4.1.0.3856\r\n"
583 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
584 p_sys->url.psz_host, p_sys->url.i_port );
585 if( p_sys->b_broadcast )
587 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
588 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
589 p_sys->i_request_context++ );
593 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
594 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
595 (uint32_t)((i_pos >> 32)&0xffffffff),
596 (uint32_t)(i_pos&0xffffffff),
597 p_sys->i_request_context++ );
599 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
600 "Pragma: xPlayStrm=1\r\n"
601 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
602 "Pragma: stream-switch-count=%d\r\n"
603 "Pragma: stream-switch-entry=",
604 GUID_PRINT( p_sys->guid ),
607 for( i = 1; i < 128; i++ )
609 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
612 if( p_sys->asfh.stream[i].i_selected )
617 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
618 "ffff:%d:%d ", i, i_select );
621 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" );
622 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
624 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
626 msg_Err( p_access, "failed to send request" );
630 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
632 msg_Err( p_access, "cannot read data" );
635 if( atoi( &psz[9] ) >= 400 )
637 msg_Err( p_access, "error: %s", psz );
641 msg_Dbg( p_access, "HTTP reply '%s'", psz );
644 /* FIXME check HTTP code */
647 char *psz = net_Gets( p_access, p_sys->fd );
650 msg_Err( p_access, "cannot read data" );
658 msg_Dbg( p_access, "%s", psz );
662 p_sys->i_packet_used = 0;
663 p_sys->i_packet_length = 0;
668 /*****************************************************************************
670 *****************************************************************************/
671 static void Stop( access_t *p_access )
673 access_sys_t *p_sys = p_access->p_sys;
675 msg_Dbg( p_access, "closing stream" );
678 net_Close( p_sys->fd );
683 /*****************************************************************************
685 *****************************************************************************/
686 static int GetPacket( access_t * p_access, chunk_t *p_ck )
688 access_sys_t *p_sys = p_access->p_sys;
691 memset( p_ck, 0, sizeof( chunk_t ) );
693 /* Read the chunk header */
694 if( net_Read( p_access, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
696 /* msg_Err( p_access, "cannot read data" ); */
700 p_ck->i_type = GetWLE( p_sys->buffer);
701 p_ck->i_size = GetWLE( p_sys->buffer + 2);
702 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
703 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
704 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
705 p_ck->p_data = p_sys->buffer + 12;
706 p_ck->i_data = p_ck->i_size2 - 8;
708 if( p_ck->i_type == 0x4524 ) // Transfer complete
710 if( p_ck->i_sequence == 0 )
712 msg_Warn( p_access, "EOF" );
717 msg_Warn( p_access, "Next stream follow but not supported" );
721 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
723 msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
727 if( p_ck->i_data > 0 &&
728 net_Read( p_access, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
730 msg_Err( p_access, "cannot read data" );
734 if( p_sys->i_packet_sequence != 0 &&
735 p_ck->i_sequence != p_sys->i_packet_sequence )
737 msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
740 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
741 p_sys->i_packet_used = 0;
742 p_sys->i_packet_length = p_ck->i_data;
743 p_sys->p_packet = p_ck->p_data;