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 Start( input_thread_t *, off_t );
57 static void Stop( input_thread_t * );
58 static int GetPacket( input_thread_t *, chunk_t * );
60 /****************************************************************************
61 * Open: connect to ftp server and ask for file
62 ****************************************************************************/
63 int E_( MMSHOpen ) ( input_thread_t *p_input )
68 char *psz_location = NULL;
74 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
75 p_sys->i_proto = MMS_PROTO_HTTP;
78 p_sys->i_request_context = 1;
79 p_sys->b_broadcast = VLC_TRUE;
80 p_sys->p_packet = NULL;
81 p_sys->i_packet_sequence = 0;
82 p_sys->i_packet_used = 0;
83 p_sys->i_packet_length = 0;
85 p_sys->i_request_context = 1;
86 E_( GenerateGuid )( &p_sys->guid );
88 /* open a tcp connection */
89 vlc_UrlParse( &p_sys->url, p_input->psz_name, 0 );
90 if( p_sys->url.psz_host == NULL && *p_sys->url.psz_host == '\0' )
92 msg_Err( p_input, "invalid host" );
95 if( p_sys->url.i_port <= 0 )
97 p_sys->url.i_port = 80;
100 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->url.psz_host,
101 p_sys->url.i_port ) ) < 0 )
103 msg_Err( p_input, "cannot connect to%s:%d", p_sys->url.psz_host, p_sys->url.i_port );
107 /* send first request */
108 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
109 "GET %s HTTP/1.0\r\n"
111 "User-Agent: NSPlayer/4.1.0.3856\r\n"
113 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
114 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
115 "Connection: Close\r\n",
116 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
117 p_sys->url.psz_host, p_sys->url.i_port,
118 p_sys->i_request_context++,
119 GUID_PRINT( p_sys->guid ) );
121 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
123 msg_Err( p_input, "failed to send request" );
127 /* Receive the http header */
128 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
130 msg_Err( p_input, "failed to read answer" );
133 if( strncmp( psz, "HTTP/1.", 7 ) )
135 msg_Err( p_input, "invalid HTTP reply '%s'", psz );
139 i_code = atoi( &psz[9] );
142 msg_Err( p_input, "error: %s", psz );
147 msg_Dbg( p_input, "HTTP reply '%s'", psz );
151 char *psz = net_Gets( p_input, p_sys->fd );
156 msg_Err( p_input, "failed to read answer" );
166 if( ( p = strchr( psz, ':' ) ) == NULL )
168 msg_Err( p_input, "malformed header line: %s", psz );
173 while( *p == ' ' ) p++;
175 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
177 if( !strcasecmp( psz, "Pragma" ) )
179 if( !strncasecmp( p, "features", 8 ) )
181 if( strstr( p, "broadcast" ) )
183 msg_Dbg( p_input, "stream type = broadcast" );
184 p_sys->b_broadcast = VLC_TRUE;
186 else if( strstr( p, "seekable" ) )
188 msg_Dbg( p_input, "stream type = seekable" );
189 p_sys->b_broadcast = VLC_FALSE;
193 msg_Warn( p_input, "unknow stream types (%s)", p );
194 p_sys->b_broadcast = VLC_FALSE;
198 else if( !strcasecmp( psz, "Location" ) )
200 psz_location = strdup( p );
206 /* Handle the redirection */
207 if( ( i_code == 301 || i_code == 302 ||
208 i_code == 303 || i_code == 307 ) &&
209 psz_location && *psz_location )
211 playlist_t * p_playlist;
213 msg_Dbg( p_input, "redirection to %s", psz_location );
214 net_Close( p_sys->fd ); p_sys->fd = -1;
216 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
219 msg_Err( p_input, "redirection failed: can't find playlist" );
222 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
223 playlist_Add( p_playlist, psz_location, psz_location,
224 PLAYLIST_INSERT | PLAYLIST_GO,
225 p_playlist->i_index + 1 );
226 vlc_object_release( p_playlist );
228 p_input->pf_read = ReadRedirect;
229 p_input->pf_seek = NULL;
230 p_input->pf_set_program = input_SetProgram;
231 p_input->pf_set_area = NULL;
233 /* *** finished to set some variable *** */
234 vlc_mutex_lock( &p_input->stream.stream_lock );
235 p_input->stream.b_pace_control = 0;
236 p_input->stream.p_selected_area->i_size = 0;
237 p_input->stream.b_seekable = 0;
238 p_input->stream.p_selected_area->i_tell = 0;
239 p_input->stream.i_method = INPUT_METHOD_NETWORK;
240 vlc_mutex_unlock( &p_input->stream.stream_lock );
245 /* Read the asf header */
247 p_sys->p_header = NULL;
251 if( GetPacket( p_input, &ck ) ||
252 ck.i_type != 0x4824 )
259 p_sys->i_header += ck.i_data;
260 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
261 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
262 ck.p_data, ck.i_data );
265 msg_Dbg( p_input, "complete header size=%d", p_sys->i_header );
266 if( p_sys->i_header <= 0 )
268 msg_Err( p_input, "header size == 0" );
271 /* close this connection */
272 net_Close( p_sys->fd ); p_sys->fd = -1;
274 /* *** parse header and get stream and their id *** */
275 /* get all streams properties,
277 * TODO : stream bitrates properties(optional)
278 * and bitrate mutual exclusion(optional) */
279 E_( asf_HeaderParse )( &p_sys->asfh,
280 p_sys->p_header, p_sys->i_header );
281 msg_Dbg( p_input, "packet count=%lld packet size=%d",
282 p_sys->asfh.i_data_packets_count,
283 p_sys->asfh.i_min_data_packet_size );
285 E_( asf_StreamSelect)( &p_sys->asfh,
286 config_GetInt( p_input, "mms-maxbitrate" ),
287 config_GetInt( p_input, "mms-all" ),
288 config_GetInt( p_input, "audio" ),
289 config_GetInt( p_input, "video" ) );
291 if( Start( p_input, 0 ) )
293 msg_Err( p_input, "cannot start stream" );
297 /* *** set exported functions *** */
298 p_input->pf_read = Read;
299 p_input->pf_seek = Seek;
300 p_input->pf_set_program = input_SetProgram;
301 p_input->pf_set_area = NULL;
303 p_input->p_private = NULL;
304 p_input->i_mtu = 3 * p_sys->asfh.i_min_data_packet_size;
306 /* *** finished to set some variable *** */
307 vlc_mutex_lock( &p_input->stream.stream_lock );
308 p_input->stream.b_pace_control = 0;
309 if( p_sys->b_broadcast )
311 p_input->stream.p_selected_area->i_size = 0;
312 p_input->stream.b_seekable = 0;
316 p_input->stream.p_selected_area->i_size = p_sys->asfh.i_file_size;
317 p_input->stream.b_seekable = 1;
319 p_input->stream.p_selected_area->i_tell = 0;
320 p_input->stream.i_method = INPUT_METHOD_NETWORK;
321 vlc_mutex_unlock( &p_input->stream.stream_lock );
323 /* Update default_pts to a suitable value for mms access */
324 var_Get( p_input, "mms-caching", &val );
325 p_input->i_pts_delay = val.i_int * 1000;
330 vlc_UrlClean( &p_sys->url );
333 net_Close( p_sys->fd );
339 /*****************************************************************************
340 * Close: free unused data structures
341 *****************************************************************************/
342 void E_( MMSHClose )( input_thread_t *p_input )
344 access_sys_t *p_sys = p_input->p_access_data;
346 msg_Dbg( p_input, "stopping stream" );
353 /*****************************************************************************
354 * Seek: try to go at the right place
355 *****************************************************************************/
356 static void Seek( input_thread_t * p_input, off_t i_pos )
358 access_sys_t *p_sys = p_input->p_access_data;
363 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
364 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
366 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
368 vlc_mutex_lock( &p_input->stream.stream_lock );
371 Start( p_input, i_packet * p_sys->asfh.i_min_data_packet_size );
375 if( GetPacket( p_input, &ck ) )
381 if( ck.i_type != 0x4824 )
385 msg_Warn( p_input, "skipping header" );
388 p_sys->i_pos = i_pos;
389 p_sys->i_packet_used += i_offset;
391 p_input->stream.p_selected_area->i_tell = i_pos;
392 vlc_mutex_unlock( &p_input->stream.stream_lock );
395 /*****************************************************************************
397 *****************************************************************************/
398 static ssize_t ReadRedirect( input_thread_t *p_input, byte_t *p, size_t i )
403 /*****************************************************************************
405 *****************************************************************************/
406 static ssize_t Read ( input_thread_t * p_input, byte_t * p_buffer,
409 access_sys_t *p_sys = p_input->p_access_data;
413 while( i_data < i_len )
415 if( p_sys->i_pos < p_sys->i_header )
417 i_copy = __MIN( p_sys->i_header - p_sys->i_pos, i_len - i_data );
418 memcpy( &p_buffer[i_data], p_sys->p_header, i_copy );
421 p_sys->i_pos += i_copy;
423 else if( p_sys->i_packet_used < p_sys->i_packet_length )
425 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
428 memcpy( &p_buffer[i_data],
429 &p_sys->p_packet[p_sys->i_packet_used],
433 p_sys->i_packet_used += i_copy;
434 p_sys->i_pos += i_copy;
436 else if( p_sys->i_pos + i_data > p_sys->i_header &&
437 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
439 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
442 memset( &p_buffer[i_data], 0, i_copy );
445 p_sys->i_packet_used += i_copy;
446 p_sys->i_pos += i_copy;
451 if( GetPacket( p_input, &ck ) )
455 if( ck.i_type == 0x4824 )
457 p_sys->i_packet_used = 0;
458 p_sys->i_packet_length = 0;
466 /*****************************************************************************
468 *****************************************************************************/
469 static int Start( input_thread_t *p_input, off_t i_pos )
471 access_sys_t *p_sys = p_input->p_access_data;
476 msg_Dbg( p_input, "starting stream" );
478 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->url.psz_host,
479 p_sys->url.i_port ) ) < 0 )
481 /* should not occur */
482 msg_Err( p_input, "cannot connect to the server" );
486 for( i = 1; i < 128; i++ )
488 if( p_sys->asfh.stream[i].i_selected )
496 msg_Err( p_input, "no stream selected" );
499 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
500 "GET %s HTTP/1.0\r\n"
502 "User-Agent: NSPlayer/4.1.0.3856\r\n"
504 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
505 p_sys->url.psz_host, p_sys->url.i_port );
506 if( p_sys->b_broadcast )
508 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
509 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
510 p_sys->i_request_context++ );
514 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
515 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
516 (uint32_t)((i_pos >> 32)&0xffffffff),
517 (uint32_t)(i_pos&0xffffffff),
518 p_sys->i_request_context++ );
520 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
521 "Pragma: xPlayStrm=1\r\n"
522 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
523 "Pragma: stream-switch-count=%d\r\n"
524 "Pragma: stream-switch-entry=",
525 GUID_PRINT( p_sys->guid ),
528 for( i = 1; i < 128; i++ )
530 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
533 if( p_sys->asfh.stream[i].i_selected )
538 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
539 "ffff:%d:%d ", i, i_select );
542 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" );
543 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
545 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
547 msg_Err( p_input, "failed to send request" );
551 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
553 msg_Err( p_input, "cannot read data" );
556 if( atoi( &psz[9] ) >= 400 )
558 msg_Err( p_input, "error: %s", psz );
562 msg_Dbg( p_input, "HTTP reply '%s'", psz );
565 /* FIXME check HTTP code */
568 char *psz = net_Gets( p_input, p_sys->fd );
571 msg_Err( p_input, "cannot read data" );
579 msg_Dbg( p_input, "%s", psz );
583 p_sys->i_packet_used = 0;
584 p_sys->i_packet_length = 0;
589 /*****************************************************************************
591 *****************************************************************************/
592 static void Stop( input_thread_t *p_input )
594 access_sys_t *p_sys = p_input->p_access_data;
596 msg_Dbg( p_input, "closing stream" );
597 net_Close( p_sys->fd ); p_sys->fd = -1;
600 /*****************************************************************************
602 *****************************************************************************/
603 static int GetPacket( input_thread_t * p_input, chunk_t *p_ck )
605 access_sys_t *p_sys = p_input->p_access_data;
607 /* Read the chunk header */
608 if( net_Read( p_input, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
610 /* msg_Err( p_input, "cannot read data" ); */
614 p_ck->i_type = GetWLE( p_sys->buffer);
615 p_ck->i_size = GetWLE( p_sys->buffer + 2);
616 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
617 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
618 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
619 p_ck->p_data = p_sys->buffer + 12;
620 p_ck->i_data = p_ck->i_size2 - 8;
622 if( p_ck->i_type == 0x4524 ) // Transfer complete
624 msg_Warn( p_input, "EOF" );
627 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
629 msg_Err( p_input, "invalid chunk FATAL" );
633 if( net_Read( p_input, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
635 msg_Err( p_input, "cannot read data" );
639 if( p_sys->i_packet_sequence != 0 &&
640 p_ck->i_sequence != p_sys->i_packet_sequence )
642 msg_Warn( p_input, "packet lost ?" );
645 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
646 p_sys->i_packet_used = 0;
647 p_sys->i_packet_length = p_ck->i_data;
648 p_sys->p_packet = p_ck->p_data;