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( strstr( p, "features" ) )
181 /* FIXME, it is a bit badly done here ..... */
182 if( strstr( p, "broadcast" ) )
184 msg_Dbg( p_input, "stream type = broadcast" );
185 p_sys->b_broadcast = VLC_TRUE;
187 else if( strstr( p, "seekable" ) )
189 msg_Dbg( p_input, "stream type = seekable" );
190 p_sys->b_broadcast = VLC_FALSE;
194 msg_Warn( p_input, "unknow stream types (%s)", p );
195 p_sys->b_broadcast = VLC_FALSE;
199 else if( !strcasecmp( psz, "Location" ) )
201 psz_location = strdup( p );
207 /* Handle the redirection */
208 if( ( i_code == 301 || i_code == 302 ||
209 i_code == 303 || i_code == 307 ) &&
210 psz_location && *psz_location )
212 playlist_t * p_playlist;
214 msg_Dbg( p_input, "redirection to %s", psz_location );
215 net_Close( p_sys->fd ); p_sys->fd = -1;
217 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
220 msg_Err( p_input, "redirection failed: can't find playlist" );
223 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
224 playlist_Add( p_playlist, psz_location, psz_location,
225 PLAYLIST_INSERT | PLAYLIST_GO,
226 p_playlist->i_index + 1 );
227 vlc_object_release( p_playlist );
229 p_input->pf_read = ReadRedirect;
230 p_input->pf_seek = NULL;
231 p_input->pf_set_program = input_SetProgram;
232 p_input->pf_set_area = NULL;
234 /* *** finished to set some variable *** */
235 vlc_mutex_lock( &p_input->stream.stream_lock );
236 p_input->stream.b_pace_control = 0;
237 p_input->stream.p_selected_area->i_size = 0;
238 p_input->stream.b_seekable = 0;
239 p_input->stream.p_selected_area->i_tell = 0;
240 p_input->stream.i_method = INPUT_METHOD_NETWORK;
241 vlc_mutex_unlock( &p_input->stream.stream_lock );
246 /* Read the asf header */
248 p_sys->p_header = NULL;
252 if( GetPacket( p_input, &ck ) ||
253 ck.i_type != 0x4824 )
260 p_sys->i_header += ck.i_data;
261 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
262 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
263 ck.p_data, ck.i_data );
266 msg_Dbg( p_input, "complete header size=%d", p_sys->i_header );
267 if( p_sys->i_header <= 0 )
269 msg_Err( p_input, "header size == 0" );
272 /* close this connection */
273 net_Close( p_sys->fd ); p_sys->fd = -1;
275 /* *** parse header and get stream and their id *** */
276 /* get all streams properties,
278 * TODO : stream bitrates properties(optional)
279 * and bitrate mutual exclusion(optional) */
280 E_( asf_HeaderParse )( &p_sys->asfh,
281 p_sys->p_header, p_sys->i_header );
282 msg_Dbg( p_input, "packet count=%lld packet size=%d",
283 p_sys->asfh.i_data_packets_count,
284 p_sys->asfh.i_min_data_packet_size );
286 E_( asf_StreamSelect)( &p_sys->asfh,
287 config_GetInt( p_input, "mms-maxbitrate" ),
288 config_GetInt( p_input, "mms-all" ),
289 config_GetInt( p_input, "audio" ),
290 config_GetInt( p_input, "video" ) );
292 if( Start( p_input, 0 ) )
294 msg_Err( p_input, "cannot start stream" );
298 /* *** set exported functions *** */
299 p_input->pf_read = Read;
300 p_input->pf_seek = Seek;
301 p_input->pf_set_program = input_SetProgram;
302 p_input->pf_set_area = NULL;
304 p_input->p_private = NULL;
305 p_input->i_mtu = 3 * p_sys->asfh.i_min_data_packet_size;
307 /* *** finished to set some variable *** */
308 vlc_mutex_lock( &p_input->stream.stream_lock );
309 p_input->stream.b_pace_control = 0;
310 if( p_sys->b_broadcast )
312 p_input->stream.p_selected_area->i_size = 0;
313 p_input->stream.b_seekable = 0;
317 p_input->stream.p_selected_area->i_size = p_sys->asfh.i_file_size;
318 p_input->stream.b_seekable = 1;
320 p_input->stream.p_selected_area->i_tell = 0;
321 p_input->stream.i_method = INPUT_METHOD_NETWORK;
322 vlc_mutex_unlock( &p_input->stream.stream_lock );
324 /* Update default_pts to a suitable value for mms access */
325 var_Get( p_input, "mms-caching", &val );
326 p_input->i_pts_delay = val.i_int * 1000;
331 vlc_UrlClean( &p_sys->url );
334 net_Close( p_sys->fd );
340 /*****************************************************************************
341 * Close: free unused data structures
342 *****************************************************************************/
343 void E_( MMSHClose )( input_thread_t *p_input )
345 access_sys_t *p_sys = p_input->p_access_data;
347 msg_Dbg( p_input, "stopping stream" );
354 /*****************************************************************************
355 * Seek: try to go at the right place
356 *****************************************************************************/
357 static void Seek( input_thread_t * p_input, off_t i_pos )
359 access_sys_t *p_sys = p_input->p_access_data;
364 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
365 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
367 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
369 vlc_mutex_lock( &p_input->stream.stream_lock );
372 Start( p_input, i_packet * p_sys->asfh.i_min_data_packet_size );
376 if( GetPacket( p_input, &ck ) )
382 if( ck.i_type != 0x4824 )
386 msg_Warn( p_input, "skipping header" );
389 p_sys->i_pos = i_pos;
390 p_sys->i_packet_used += i_offset;
392 p_input->stream.p_selected_area->i_tell = i_pos;
393 vlc_mutex_unlock( &p_input->stream.stream_lock );
396 /*****************************************************************************
398 *****************************************************************************/
399 static ssize_t ReadRedirect( input_thread_t *p_input, byte_t *p, size_t i )
404 /*****************************************************************************
406 *****************************************************************************/
407 static ssize_t Read ( input_thread_t * p_input, byte_t * p_buffer,
410 access_sys_t *p_sys = p_input->p_access_data;
414 while( i_data < i_len )
416 if( p_sys->i_pos < p_sys->i_header )
418 i_copy = __MIN( p_sys->i_header - p_sys->i_pos, i_len - i_data );
419 memcpy( &p_buffer[i_data], &p_sys->p_header[p_sys->i_pos], i_copy );
422 p_sys->i_pos += i_copy;
424 else if( p_sys->i_packet_used < p_sys->i_packet_length )
426 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
429 memcpy( &p_buffer[i_data],
430 &p_sys->p_packet[p_sys->i_packet_used],
434 p_sys->i_packet_used += i_copy;
435 p_sys->i_pos += i_copy;
437 else if( p_sys->i_packet_length > 0 &&
438 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
440 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
443 memset( &p_buffer[i_data], 0, i_copy );
446 p_sys->i_packet_used += i_copy;
447 p_sys->i_pos += i_copy;
452 if( GetPacket( p_input, &ck ) )
456 if( ck.i_type == 0x4824 )
458 p_sys->i_packet_used = 0;
459 p_sys->i_packet_length = 0;
467 /*****************************************************************************
469 *****************************************************************************/
470 static int Start( input_thread_t *p_input, off_t i_pos )
472 access_sys_t *p_sys = p_input->p_access_data;
477 msg_Dbg( p_input, "starting stream" );
479 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->url.psz_host,
480 p_sys->url.i_port ) ) < 0 )
482 /* should not occur */
483 msg_Err( p_input, "cannot connect to the server" );
487 for( i = 1; i < 128; i++ )
489 if( p_sys->asfh.stream[i].i_selected )
497 msg_Err( p_input, "no stream selected" );
500 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
501 "GET %s HTTP/1.0\r\n"
503 "User-Agent: NSPlayer/4.1.0.3856\r\n"
505 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
506 p_sys->url.psz_host, p_sys->url.i_port );
507 if( p_sys->b_broadcast )
509 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
510 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
511 p_sys->i_request_context++ );
515 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
516 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
517 (uint32_t)((i_pos >> 32)&0xffffffff),
518 (uint32_t)(i_pos&0xffffffff),
519 p_sys->i_request_context++ );
521 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
522 "Pragma: xPlayStrm=1\r\n"
523 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
524 "Pragma: stream-switch-count=%d\r\n"
525 "Pragma: stream-switch-entry=",
526 GUID_PRINT( p_sys->guid ),
529 for( i = 1; i < 128; i++ )
531 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
534 if( p_sys->asfh.stream[i].i_selected )
539 net_Printf( VLC_OBJECT(p_input), p_sys->fd,
540 "ffff:%d:%d ", i, i_select );
543 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" );
544 net_Printf( VLC_OBJECT(p_input), p_sys->fd, "Connection: Close\r\n" );
546 if( net_Printf( VLC_OBJECT(p_input), p_sys->fd, "\r\n" ) < 0 )
548 msg_Err( p_input, "failed to send request" );
552 if( ( psz = net_Gets( VLC_OBJECT(p_input), p_sys->fd ) ) == NULL )
554 msg_Err( p_input, "cannot read data" );
557 if( atoi( &psz[9] ) >= 400 )
559 msg_Err( p_input, "error: %s", psz );
563 msg_Dbg( p_input, "HTTP reply '%s'", psz );
566 /* FIXME check HTTP code */
569 char *psz = net_Gets( p_input, p_sys->fd );
572 msg_Err( p_input, "cannot read data" );
580 msg_Dbg( p_input, "%s", psz );
584 p_sys->i_packet_used = 0;
585 p_sys->i_packet_length = 0;
590 /*****************************************************************************
592 *****************************************************************************/
593 static void Stop( input_thread_t *p_input )
595 access_sys_t *p_sys = p_input->p_access_data;
597 msg_Dbg( p_input, "closing stream" );
598 net_Close( p_sys->fd ); p_sys->fd = -1;
601 /*****************************************************************************
603 *****************************************************************************/
604 static int GetPacket( input_thread_t * p_input, chunk_t *p_ck )
606 access_sys_t *p_sys = p_input->p_access_data;
608 /* Read the chunk header */
609 if( net_Read( p_input, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
611 /* msg_Err( p_input, "cannot read data" ); */
615 p_ck->i_type = GetWLE( p_sys->buffer);
616 p_ck->i_size = GetWLE( p_sys->buffer + 2);
617 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
618 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
619 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
620 p_ck->p_data = p_sys->buffer + 12;
621 p_ck->i_data = p_ck->i_size2 - 8;
623 if( p_ck->i_type == 0x4524 ) // Transfer complete
625 msg_Warn( p_input, "EOF" );
628 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
630 msg_Err( p_input, "invalid chunk FATAL" );
634 if( net_Read( p_input, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
636 msg_Err( p_input, "cannot read data" );
640 if( p_sys->i_packet_sequence != 0 &&
641 p_ck->i_sequence != p_sys->i_packet_sequence )
643 msg_Warn( p_input, "packet lost ?" );
646 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
647 p_sys->i_packet_used = 0;
648 p_sys->i_packet_length = p_ck->i_data;
649 p_sys->p_packet = p_ck->p_data;