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) ( input_thread_t * );
50 void E_(MMSHClose) ( input_thread_t * );
52 static ssize_t Read( input_thread_t *, byte_t *, size_t );
53 static ssize_t ReadRedirect( input_thread_t *, byte_t *, size_t );
54 static void Seek( input_thread_t *, off_t );
56 static int Describe( input_thread_t *, char **ppsz_location );
57 static int Start( input_thread_t *, off_t );
58 static void Stop( input_thread_t * );
59 static int GetPacket( input_thread_t *, chunk_t * );
61 /****************************************************************************
62 * Open: connect to ftp server and ask for file
63 ****************************************************************************/
64 int E_( MMSHOpen ) ( input_thread_t *p_input )
68 char *psz_location = NULL;
73 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
74 p_sys->i_proto = MMS_PROTO_HTTP;
80 /* open a tcp connection */
81 vlc_UrlParse( &p_sys->url, p_input->psz_name, 0 );
82 if( p_sys->url.psz_host == NULL && *p_sys->url.psz_host == '\0' )
84 msg_Err( p_input, "invalid host" );
85 vlc_UrlClean( &p_sys->url );
89 if( p_sys->url.i_port <= 0 )
91 p_sys->url.i_port = 80;
94 if( Describe( p_input, &psz_location ) )
96 vlc_UrlClean( &p_sys->url );
100 /* Handle redirection */
101 if( psz_location && *psz_location )
103 playlist_t * p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
105 msg_Dbg( p_input, "redirection to %s", psz_location );
109 msg_Err( p_input, "redirection failed: can't find playlist" );
110 free( psz_location );
113 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
114 playlist_Add( p_playlist, psz_location, psz_location,
115 PLAYLIST_INSERT | PLAYLIST_GO,
116 p_playlist->i_index + 1 );
117 vlc_object_release( p_playlist );
119 free( psz_location );
121 p_input->pf_read = ReadRedirect;
122 p_input->pf_seek = NULL;
123 p_input->pf_set_program = input_SetProgram;
124 p_input->pf_set_area = NULL;
126 /* *** finished to set some variable *** */
127 vlc_mutex_lock( &p_input->stream.stream_lock );
128 p_input->stream.b_pace_control = 0;
129 p_input->stream.p_selected_area->i_size = 0;
130 p_input->stream.b_seekable = 0;
131 p_input->stream.p_selected_area->i_tell = 0;
132 p_input->stream.i_method = INPUT_METHOD_NETWORK;
133 vlc_mutex_unlock( &p_input->stream.stream_lock );
139 if( Start( p_input, 0 ) )
141 msg_Err( p_input, "cannot start stream" );
142 free( p_sys->p_header );
143 vlc_UrlClean( &p_sys->url );
148 /* *** set exported functions *** */
149 p_input->pf_read = Read;
150 p_input->pf_seek = Seek;
151 p_input->pf_set_program = input_SetProgram;
152 p_input->pf_set_area = NULL;
154 p_input->p_private = NULL;
155 p_input->i_mtu = 3 * p_sys->asfh.i_min_data_packet_size;
157 /* *** finished to set some variable *** */
158 vlc_mutex_lock( &p_input->stream.stream_lock );
159 p_input->stream.b_pace_control = 0;
160 if( p_sys->b_broadcast )
162 p_input->stream.p_selected_area->i_size = 0;
163 p_input->stream.b_seekable = 0;
167 p_input->stream.p_selected_area->i_size = p_sys->asfh.i_file_size;
168 p_input->stream.b_seekable = 1;
170 p_input->stream.p_selected_area->i_tell = 0;
171 p_input->stream.i_method = INPUT_METHOD_NETWORK;
172 vlc_mutex_unlock( &p_input->stream.stream_lock );
174 /* Update default_pts to a suitable value for mms access */
175 var_Get( p_input, "mms-caching", &val );
176 p_input->i_pts_delay = val.i_int * 1000;
181 /*****************************************************************************
182 * Close: free unused data structures
183 *****************************************************************************/
184 void E_( MMSHClose )( input_thread_t *p_input )
186 access_sys_t *p_sys = p_input->p_access_data;
188 msg_Dbg( p_input, "stopping stream" );
196 /*****************************************************************************
197 * Seek: try to go at the right place
198 *****************************************************************************/
199 static void Seek( input_thread_t * p_input, off_t i_pos )
201 access_sys_t *p_sys = p_input->p_access_data;
206 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
207 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
209 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
211 vlc_mutex_lock( &p_input->stream.stream_lock );
214 Start( p_input, i_packet * p_sys->asfh.i_min_data_packet_size );
218 if( GetPacket( p_input, &ck ) )
224 if( ck.i_type != 0x4824 )
228 msg_Warn( p_input, "skipping header" );
231 p_sys->i_pos = i_pos;
232 p_sys->i_packet_used += i_offset;
234 p_input->stream.p_selected_area->i_tell = i_pos;
235 vlc_mutex_unlock( &p_input->stream.stream_lock );
238 /*****************************************************************************
240 *****************************************************************************/
241 static ssize_t ReadRedirect( input_thread_t *p_input, byte_t *p, size_t i )
246 /*****************************************************************************
248 *****************************************************************************/
249 static ssize_t Read ( input_thread_t * p_input, byte_t * p_buffer,
252 access_sys_t *p_sys = p_input->p_access_data;
256 while( i_data < i_len )
258 if( p_sys->i_pos < p_sys->i_start + p_sys->i_header )
260 int i_offset = p_sys->i_pos - p_sys->i_start;
261 i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
262 memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
265 p_sys->i_pos += i_copy;
267 else if( p_sys->i_packet_used < p_sys->i_packet_length )
269 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
272 memcpy( &p_buffer[i_data],
273 &p_sys->p_packet[p_sys->i_packet_used],
277 p_sys->i_packet_used += i_copy;
278 p_sys->i_pos += i_copy;
280 else if( p_sys->i_packet_length > 0 &&
281 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
283 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
286 memset( &p_buffer[i_data], 0, i_copy );
289 p_sys->i_packet_used += i_copy;
290 p_sys->i_pos += i_copy;
295 if( GetPacket( p_input, &ck ) )
297 if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
299 char *psz_location = NULL;
301 p_sys->i_start = p_sys->i_pos;
303 msg_Dbg( p_input, "stoping the stream" );
306 msg_Dbg( p_input, "describe the stream" );
307 if( Describe( p_input, &psz_location ) )
309 msg_Err( p_input, "describe failed" );
312 if( Start( p_input, 0 ) )
314 msg_Err( p_input, "Start failed" );
323 if( ck.i_type != 0x4424 )
325 p_sys->i_packet_used = 0;
326 p_sys->i_packet_length = 0;
334 /*****************************************************************************
336 *****************************************************************************/
337 static int Describe( input_thread_t *p_input, char **ppsz_location )
339 access_sys_t *p_sys = p_input->p_access_data;
340 char *psz_location = NULL;
345 p_sys->b_broadcast = VLC_TRUE;
346 p_sys->i_request_context = 1;
347 p_sys->i_packet_sequence = 0;
348 p_sys->i_packet_used = 0;
349 p_sys->i_packet_length = 0;
350 p_sys->p_packet = NULL;
351 E_( GenerateGuid )( &p_sys->guid );
353 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->url.psz_host,
354 p_sys->url.i_port ) ) < 0 )
356 msg_Err( p_input, "cannot connect to%s:%d", p_sys->url.psz_host, p_sys->url.i_port );
360 /* send first request */
361 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
362 "GET %s HTTP/1.0\r\n"
364 "User-Agent: NSPlayer/4.1.0.3856\r\n"
366 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
367 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
368 "Connection: Close\r\n",
369 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
370 p_sys->url.psz_host, p_sys->url.i_port,
371 p_sys->i_request_context++,
372 GUID_PRINT( p_sys->guid ) );
374 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
376 msg_Err( p_input, "failed to send request" );
380 /* Receive the http header */
381 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
383 msg_Err( p_input, "failed to read answer" );
386 if( strncmp( psz, "HTTP/1.", 7 ) )
388 msg_Err( p_input, "invalid HTTP reply '%s'", psz );
392 i_code = atoi( &psz[9] );
395 msg_Err( p_input, "error: %s", psz );
400 msg_Dbg( p_input, "HTTP reply '%s'", psz );
404 char *psz = net_Gets( p_input, p_sys->fd );
409 msg_Err( p_input, "failed to read answer" );
419 if( ( p = strchr( psz, ':' ) ) == NULL )
421 msg_Err( p_input, "malformed header line: %s", psz );
426 while( *p == ' ' ) p++;
428 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
430 if( !strcasecmp( psz, "Pragma" ) )
432 if( strstr( p, "features" ) )
434 /* FIXME, it is a bit badly done here ..... */
435 if( strstr( p, "broadcast" ) )
437 msg_Dbg( p_input, "stream type = broadcast" );
438 p_sys->b_broadcast = VLC_TRUE;
440 else if( strstr( p, "seekable" ) )
442 msg_Dbg( p_input, "stream type = seekable" );
443 p_sys->b_broadcast = VLC_FALSE;
447 msg_Warn( p_input, "unknow stream types (%s)", p );
448 p_sys->b_broadcast = VLC_FALSE;
452 else if( !strcasecmp( psz, "Location" ) )
454 psz_location = strdup( p );
460 /* Handle the redirection */
461 if( ( i_code == 301 || i_code == 302 ||
462 i_code == 303 || i_code == 307 ) &&
463 psz_location && *psz_location )
465 msg_Dbg( p_input, "redirection to %s", psz_location );
466 net_Close( p_sys->fd ); p_sys->fd = -1;
468 *ppsz_location = psz_location;
472 /* Read the asf header */
474 p_sys->p_header = NULL;
478 if( GetPacket( p_input, &ck ) ||
479 ck.i_type != 0x4824 )
486 p_sys->i_header += ck.i_data;
487 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
488 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
489 ck.p_data, ck.i_data );
492 msg_Dbg( p_input, "complete header size=%d", p_sys->i_header );
493 if( p_sys->i_header <= 0 )
495 msg_Err( p_input, "header size == 0" );
498 /* close this connection */
499 net_Close( p_sys->fd ); p_sys->fd = -1;
501 /* *** parse header and get stream and their id *** */
502 /* get all streams properties,
504 * TODO : stream bitrates properties(optional)
505 * and bitrate mutual exclusion(optional) */
506 E_( asf_HeaderParse )( &p_sys->asfh,
507 p_sys->p_header, p_sys->i_header );
508 msg_Dbg( p_input, "packet count=%lld packet size=%d",
509 p_sys->asfh.i_data_packets_count,
510 p_sys->asfh.i_min_data_packet_size );
512 E_( asf_StreamSelect)( &p_sys->asfh,
513 config_GetInt( p_input, "mms-maxbitrate" ),
514 config_GetInt( p_input, "mms-all" ),
515 config_GetInt( p_input, "audio" ),
516 config_GetInt( p_input, "video" ) );
523 net_Close( p_sys->fd );
529 /*****************************************************************************
531 *****************************************************************************/
532 static int Start( input_thread_t *p_input, off_t i_pos )
534 access_sys_t *p_sys = p_input->p_access_data;
539 msg_Dbg( p_input, "starting stream" );
541 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->url.psz_host,
542 p_sys->url.i_port ) ) < 0 )
544 /* should not occur */
545 msg_Err( p_input, "cannot connect to the server" );
549 for( i = 1; i < 128; i++ )
551 if( p_sys->asfh.stream[i].i_selected )
559 msg_Err( p_input, "no stream selected" );
562 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
563 "GET %s HTTP/1.0\r\n"
565 "User-Agent: NSPlayer/4.1.0.3856\r\n"
567 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
568 p_sys->url.psz_host, p_sys->url.i_port );
569 if( p_sys->b_broadcast )
571 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
572 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
573 p_sys->i_request_context++ );
577 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
578 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
579 (uint32_t)((i_pos >> 32)&0xffffffff),
580 (uint32_t)(i_pos&0xffffffff),
581 p_sys->i_request_context++ );
583 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
584 "Pragma: xPlayStrm=1\r\n"
585 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
586 "Pragma: stream-switch-count=%d\r\n"
587 "Pragma: stream-switch-entry=",
588 GUID_PRINT( p_sys->guid ),
591 for( i = 1; i < 128; i++ )
593 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
596 if( p_sys->asfh.stream[i].i_selected )
601 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
602 "ffff:%d:%d ", i, i_select );
605 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" );
606 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
608 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
610 msg_Err( p_input, "failed to send request" );
614 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
616 msg_Err( p_input, "cannot read data" );
619 if( atoi( &psz[9] ) >= 400 )
621 msg_Err( p_input, "error: %s", psz );
625 msg_Dbg( p_input, "HTTP reply '%s'", psz );
628 /* FIXME check HTTP code */
631 char *psz = net_Gets( p_input, p_sys->fd );
634 msg_Err( p_input, "cannot read data" );
642 msg_Dbg( p_input, "%s", psz );
646 p_sys->i_packet_used = 0;
647 p_sys->i_packet_length = 0;
652 /*****************************************************************************
654 *****************************************************************************/
655 static void Stop( input_thread_t *p_input )
657 access_sys_t *p_sys = p_input->p_access_data;
659 msg_Dbg( p_input, "closing stream" );
662 net_Close( p_sys->fd );
667 /*****************************************************************************
669 *****************************************************************************/
670 static int GetPacket( input_thread_t * p_input, chunk_t *p_ck )
672 access_sys_t *p_sys = p_input->p_access_data;
675 memset( p_ck, 0, sizeof( chunk_t ) );
677 /* Read the chunk header */
678 if( net_Read( p_input, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
680 /* msg_Err( p_input, "cannot read data" ); */
684 p_ck->i_type = GetWLE( p_sys->buffer);
685 p_ck->i_size = GetWLE( p_sys->buffer + 2);
686 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
687 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
688 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
689 p_ck->p_data = p_sys->buffer + 12;
690 p_ck->i_data = p_ck->i_size2 - 8;
692 if( p_ck->i_type == 0x4524 ) // Transfer complete
694 if( p_ck->i_sequence == 0 )
696 msg_Warn( p_input, "EOF" );
701 msg_Warn( p_input, "Next stream follow but not supported" );
705 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
707 msg_Err( p_input, "invalid chunk FATAL (0x%x)", p_ck->i_type );
711 if( p_ck->i_data > 0 &&
712 net_Read( p_input, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
714 msg_Err( p_input, "cannot read data" );
718 if( p_sys->i_packet_sequence != 0 &&
719 p_ck->i_sequence != p_sys->i_packet_sequence )
721 msg_Warn( p_input, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
724 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
725 p_sys->i_packet_used = 0;
726 p_sys->i_packet_length = p_ck->i_data;
727 p_sys->p_packet = p_ck->p_data;