1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mmsh.c,v 1.8 2004/01/26 16:30:34 fenrir Exp $
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 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
36 #include <vlc/input.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 int E_(MMSHOpen) ( input_thread_t * );
49 void E_(MMSHClose) ( input_thread_t * );
50 static ssize_t Read( input_thread_t *, byte_t *, size_t );
51 static void Seek( input_thread_t *, off_t );
53 static ssize_t NetFill( input_thread_t *, access_sys_t *, int );
55 static int mmsh_start ( input_thread_t *, off_t );
56 static void mmsh_stop ( input_thread_t * );
57 static int mmsh_get_packet( input_thread_t *, chunk_t * );
59 static http_answer_t *http_answer_parse( uint8_t *, int );
60 static void http_answer_free ( http_answer_t * );
61 static http_field_t *http_field_find ( http_field_t *, char * );
63 static int chunk_parse( chunk_t *, uint8_t *, int );
65 /****************************************************************************
66 * Open: connect to ftp server and ask for file
67 ****************************************************************************/
68 int E_( MMSHOpen ) ( input_thread_t *p_input )
74 http_field_t *p_field;
80 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
81 p_sys->i_proto = MMS_PROTO_HTTP;
84 p_sys->i_request_context = 1;
86 p_sys->i_buffer_pos = 0;
87 p_sys->b_broadcast = VLC_TRUE;
88 p_sys->p_packet = NULL;
89 p_sys->i_packet_sequence = 0;
90 p_sys->i_packet_used = 0;
91 p_sys->i_packet_length = 0;
93 p_sys->i_request_context = 1;
94 E_( GenerateGuid )( &p_sys->guid );
96 /* open a tcp connection */
97 p_sys->p_url = E_( url_new )( p_input->psz_name );
99 if( *p_sys->p_url->psz_host == '\0' )
101 msg_Err( p_input, "invalid server addresse" );
104 if( p_sys->p_url->i_port <= 0 )
106 p_sys->p_url->i_port = 80;
109 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->p_url->psz_host,
110 p_sys->p_url->i_port ) ) < 0 )
112 msg_Err( p_input, "cannot connect" );
116 /* *** send first request *** */
117 p = &p_sys->buffer[0];
118 p += sprintf( p, "GET %s HTTP/1.0\r\n", p_sys->p_url->psz_path );
119 p += sprintf( p,"Accept: */*\r\n" );
120 p += sprintf( p, "User-Agent: NSPlayer/4.1.0.3856\r\n" );
121 p += sprintf( p, "Host: %s:%d\r\n",
122 p_sys->p_url->psz_host, p_sys->p_url->i_port );
123 p += sprintf( p, "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n",
124 p_sys->i_request_context++ );
125 p += sprintf( p, "Pragma: xClientGUID={"GUID_FMT"}\r\n",
126 GUID_PRINT( p_sys->guid ) );
127 p += sprintf( p, "Connection: Close\r\n\r\n" );
129 net_Write( p_input, p_sys->fd, p_sys->buffer, p - p_sys->buffer );
131 if( NetFill ( p_input, p_sys, BUFFER_SIZE ) <= 0 )
133 msg_Err( p_input, "cannot read answer" );
136 net_Close( p_sys->fd ); p_sys->fd = -1;
138 p_ans = http_answer_parse( p_sys->buffer, p_sys->i_buffer );
141 msg_Err( p_input, "cannot parse answer" );
145 if( p_ans->i_error >= 400 )
147 msg_Err( p_input, "error %d (server return=`%s')",
148 p_ans->i_error, p_ans->psz_answer );
149 http_answer_free( p_ans );
152 else if( p_ans->i_error >= 300 )
154 msg_Err( p_input, "FIXME redirect unsuported %d (server return=`%s')",
155 p_ans->i_error, p_ans->psz_answer );
156 http_answer_free( p_ans );
159 else if( p_ans->i_body <= 0 )
161 msg_Err( p_input, "empty answer" );
162 http_answer_free( p_ans );
166 /* now get features */
167 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
169 for( p_field = p_ans->p_fields;
171 p_field = http_field_find( p_field->p_next, "Pragma" ) )
173 if( !strncasecmp( p_field->psz_value, "features", 8 ) )
175 if( strstr( p_field->psz_value, "broadcast" ) )
177 msg_Dbg( p_input, "stream type = broadcast" );
178 p_sys->b_broadcast = VLC_TRUE;
180 else if( strstr( p_field->psz_value, "seekable" ) )
182 msg_Dbg( p_input, "stream type = seekable" );
183 p_sys->b_broadcast = VLC_FALSE;
187 msg_Warn( p_input, "unknow stream types (%s)",
188 p_field->psz_value );
189 p_sys->b_broadcast = VLC_FALSE;
196 p_sys->p_header = malloc( p_ans->i_body );
199 if( chunk_parse( &ck, p_ans->p_body, p_ans->i_body ) )
201 msg_Err( p_input, "invalid chunk answer" );
204 if( ck.i_type != 0x4824 )
206 msg_Err( p_input, "invalid chunk (0x%x)", ck.i_type );
211 memcpy( &p_sys->p_header[p_sys->i_header],
215 p_sys->i_header += ck.i_data;
219 p_ans->p_body += 12 + ck.i_data;
220 p_ans->i_body -= 12 + ck.i_data;
222 } while( p_ans->i_body > 12 );
224 http_answer_free( p_ans );
226 msg_Dbg( p_input, "complete header size=%d", p_sys->i_header );
227 if( p_sys->i_header <= 0 )
229 msg_Err( p_input, "header size == 0" );
232 /* *** parse header and get stream and their id *** */
233 /* get all streams properties,
235 * TODO : stream bitrates properties(optional)
236 * and bitrate mutual exclusion(optional) */
237 E_( asf_HeaderParse )( &p_sys->asfh,
238 p_sys->p_header, p_sys->i_header );
239 msg_Dbg( p_input, "packet count=%lld packet size=%d",
240 p_sys->asfh.i_data_packets_count,
241 p_sys->asfh.i_min_data_packet_size );
243 E_( asf_StreamSelect)( &p_sys->asfh,
244 config_GetInt( p_input, "mms-maxbitrate" ),
245 config_GetInt( p_input, "mms-all" ),
246 config_GetInt( p_input, "audio" ),
247 config_GetInt( p_input, "video" ) );
249 if( mmsh_start( p_input, 0 ) )
251 msg_Err( p_input, "cannot start stream" );
255 /* *** set exported functions *** */
256 p_input->pf_read = Read;
257 p_input->pf_seek = Seek;
258 p_input->pf_set_program = input_SetProgram;
259 p_input->pf_set_area = NULL;
261 p_input->p_private = NULL;
262 p_input->i_mtu = 3 * p_sys->asfh.i_min_data_packet_size;
264 /* *** finished to set some variable *** */
265 vlc_mutex_lock( &p_input->stream.stream_lock );
266 p_input->stream.b_pace_control = 0;
267 if( p_sys->b_broadcast )
269 p_input->stream.p_selected_area->i_size = 0;
270 p_input->stream.b_seekable = 0;
274 p_input->stream.p_selected_area->i_size = p_sys->asfh.i_file_size;
275 p_input->stream.b_seekable = 1;
277 p_input->stream.p_selected_area->i_tell = 0;
278 p_input->stream.i_method = INPUT_METHOD_NETWORK;
279 vlc_mutex_unlock( &p_input->stream.stream_lock );
281 /* Update default_pts to a suitable value for mms access */
282 var_Get( p_input, "mms-caching", &val );
283 p_input->i_pts_delay = val.i_int * 1000;
288 E_( url_free )( p_sys->p_url );
292 net_Close( p_sys->fd );
298 /*****************************************************************************
299 * Close: free unused data structures
300 *****************************************************************************/
301 void E_( MMSHClose ) ( input_thread_t *p_input )
303 access_sys_t *p_sys = p_input->p_access_data;
305 msg_Dbg( p_input, "stopping stream" );
307 mmsh_stop( p_input );
312 /*****************************************************************************
313 * Seek: try to go at the right place
314 *****************************************************************************/
315 static void Seek( input_thread_t * p_input, off_t i_pos )
317 access_sys_t *p_sys = p_input->p_access_data;
322 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
323 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
325 msg_Dbg( p_input, "seeking to "I64Fd, i_pos );
327 vlc_mutex_lock( &p_input->stream.stream_lock );
329 mmsh_stop( p_input );
330 mmsh_start( p_input, i_packet * p_sys->asfh.i_min_data_packet_size );
334 if( mmsh_get_packet( p_input, &ck ) )
340 if( ck.i_type != 0x4824 )
344 msg_Warn( p_input, "skipping header" );
347 p_sys->i_pos = i_pos;
348 p_sys->i_packet_used += i_offset;
350 p_input->stream.p_selected_area->i_tell = i_pos;
351 vlc_mutex_unlock( &p_input->stream.stream_lock );
354 /*****************************************************************************
356 *****************************************************************************/
357 static ssize_t Read ( input_thread_t * p_input, byte_t * p_buffer,
360 access_sys_t *p_sys = p_input->p_access_data;
364 while( i_data < i_len )
366 if( p_sys->i_packet_used < p_sys->i_packet_length )
368 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
371 memcpy( &p_buffer[i_data],
372 &p_sys->p_packet[p_sys->i_packet_used],
376 p_sys->i_packet_used += i_copy;
378 else if( p_sys->i_pos + i_data > p_sys->i_header &&
379 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
381 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
384 memset( &p_buffer[i_data], 0, i_copy );
387 p_sys->i_packet_used += i_copy;
392 /* get a new packet */
393 /* fill enought data (>12) */
394 msg_Dbg( p_input, "waiting data (buffer = %d bytes)",
397 if( mmsh_get_packet( p_input, &ck ) )
404 p_sys->i_pos += i_data;
409 /*****************************************************************************
411 *****************************************************************************/
412 static ssize_t NetFill( input_thread_t *p_input, access_sys_t *p_sys, int i_size )
417 i_size = __MIN( i_size, BUFFER_SIZE - p_sys->i_buffer );
427 i_read = net_Read( p_input, p_sys->fd,
428 &p_sys->buffer[p_sys->i_buffer], i_size, VLC_FALSE );
436 msg_Dbg( p_input, "another try %d/2", i_try );
440 if( i_read < 0 || p_input->b_die || p_input->b_error )
446 p_sys->i_buffer += i_read;
447 if( i_total >= i_size )
453 p_sys->buffer[p_sys->i_buffer] = '\0';
459 /*****************************************************************************
461 *****************************************************************************/
462 static int mmsh_start( input_thread_t *p_input, off_t i_pos )
464 access_sys_t *p_sys = p_input->p_access_data;
468 http_answer_t *p_ans;
470 msg_Dbg( p_input, "starting stream" );
472 if( ( p_sys->fd = net_OpenTCP( p_input, p_sys->p_url->psz_host,
473 p_sys->p_url->i_port ) ) < 0 )
475 /* should not occur */
476 msg_Err( p_input, "cannot connect to the server" );
480 for( i = 1; i < 128; i++ )
482 if( p_sys->asfh.stream[i].i_selected )
490 msg_Err( p_input, "no stream selected" );
494 p = &p_sys->buffer[0];
495 p += sprintf( p, "GET %s HTTP/1.0\r\n", p_sys->p_url->psz_path );
496 p += sprintf( p,"Accept: */*\r\n" );
497 p += sprintf( p, "User-Agent: NSPlayer/4.1.0.3856\r\n" );
498 p += sprintf( p, "Host: %s:%d\r\n",
499 p_sys->p_url->psz_host, p_sys->p_url->i_port );
500 if( p_sys->b_broadcast )
502 p += sprintf( p,"Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
503 p_sys->i_request_context++ );
507 p += sprintf( p, "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
508 (uint32_t)((i_pos >> 32)&0xffffffff),
509 (uint32_t)(i_pos&0xffffffff),
510 p_sys->i_request_context++ );
512 p += sprintf( p, "Pragma: xPlayStrm=1\r\n" );
513 p += sprintf( p, "Pragma: xClientGUID={"GUID_FMT"}\r\n",
514 GUID_PRINT( p_sys->guid ) );
515 p += sprintf( p, "Pragma: stream-switch-count=%d\r\n", i_streams );
516 p += sprintf( p, "Pragma: stream-switch-entry=" );
517 for( i = 0; i < i_streams; i++ )
519 if( p_sys->asfh.stream[i].i_selected )
521 p += sprintf( p, "ffff:%d:0 ", p_sys->asfh.stream[i].i_id );
525 p += sprintf( p, "ffff:%d:2 ", p_sys->asfh.stream[i].i_id );
528 p += sprintf( p, "\r\n" );
529 p += sprintf( p, "Connection: Close\r\n\r\n" );
531 net_Write( p_input, p_sys->fd, p_sys->buffer, p - p_sys->buffer );
533 msg_Dbg( p_input, "filling buffer" );
534 /* we read until we found a \r\n\r\n or \n\n */
536 p_sys->i_buffer_pos = 0;
543 p = &p_sys->buffer[p_sys->i_buffer];
544 i_read = net_Read( p_input, p_sys->fd, &p_sys->buffer[p_sys->i_buffer], 1024, VLC_FALSE );
552 msg_Dbg( p_input, "another try (%d/12)", i_try );
556 if( i_read <= 0 || p_input->b_die || p_input->b_error )
560 p_sys->i_buffer += i_read;
561 p_sys->buffer[p_sys->i_buffer] = '\0';
563 if( strstr( p, "\r\n\r\n" ) || strstr( p, "\n\n" ) )
565 msg_Dbg( p_input, "body found" );
568 if( p_sys->i_buffer >= BUFFER_SIZE - 1024 )
570 msg_Dbg( p_input, "buffer size exeded" );
575 p_ans = http_answer_parse( p_sys->buffer, p_sys->i_buffer );
578 msg_Err( p_input, "cannot parse answer" );
582 if( p_ans->i_error < 200 || p_ans->i_error >= 300 )
584 msg_Err( p_input, "error %d (server return=`%s')",
585 p_ans->i_error, p_ans->psz_answer );
586 http_answer_free( p_ans );
592 p_sys->i_buffer_pos = 0;
597 p_sys->i_buffer_pos = p_ans->p_body - p_sys->buffer;
599 http_answer_free( p_ans );
604 /*****************************************************************************
606 *****************************************************************************/
607 static void mmsh_stop( input_thread_t *p_input )
609 access_sys_t *p_sys = p_input->p_access_data;
611 msg_Dbg( p_input, "closing stream" );
612 net_Close( p_sys->fd ); p_sys->fd = -1;
615 /*****************************************************************************
617 *****************************************************************************/
618 static int mmsh_get_packet( input_thread_t * p_input, chunk_t *p_ck )
620 access_sys_t *p_sys = p_input->p_access_data;
622 int i_mov = p_sys->i_buffer - p_sys->i_buffer_pos;
624 if( p_sys->i_buffer_pos > BUFFER_SIZE / 2 )
628 memmove( &p_sys->buffer[0],
629 &p_sys->buffer[p_sys->i_buffer_pos],
633 p_sys->i_buffer = i_mov;
634 p_sys->i_buffer_pos = 0;
637 if( NetFill( p_input, p_sys, 12 ) < 12 )
639 msg_Warn( p_input, "cannot fill buffer" );
643 chunk_parse( p_ck, &p_sys->buffer[p_sys->i_buffer_pos],
644 p_sys->i_buffer - p_sys->i_buffer_pos );
646 if( p_ck->i_type == 0x4524 ) // Transfer complete
648 msg_Warn( p_input, "EOF" );
651 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
653 msg_Err( p_input, "invalid chunk FATAL" );
657 if( p_ck->i_data < p_ck->i_size2 - 8 )
659 if( NetFill( p_input, p_sys, p_ck->i_size2 - 8 - p_ck->i_data ) <= 0 )
661 msg_Warn( p_input, "cannot fill buffer" );
664 chunk_parse( p_ck, &p_sys->buffer[p_sys->i_buffer_pos],
665 p_sys->i_buffer - p_sys->i_buffer_pos );
668 if( p_sys->i_packet_sequence != 0 &&
669 p_ck->i_sequence != p_sys->i_packet_sequence )
671 msg_Warn( p_input, "packet lost ?" );
674 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
675 p_sys->i_packet_used = 0;
676 p_sys->i_packet_length = p_ck->i_data;
677 p_sys->p_packet = p_ck->p_data;
679 p_sys->i_buffer_pos += 12 + p_ck->i_data;
684 /*****************************************************************************
686 *****************************************************************************/
687 static int http_next_line( uint8_t **pp_data, int *pi_data )
689 char *p, *p_end = *pp_data + *pi_data;
691 for( p = *pp_data; p < p_end; p++ )
693 if( p + 1 < p_end && *p == '\n' )
695 *pi_data = p_end - p - 1;
699 if( p + 2 < p_end && p[0] == '\r' && p[1] == '\n' )
701 *pi_data = p_end - p - 2;
711 /*****************************************************************************
713 *****************************************************************************/
714 static http_answer_t *http_answer_parse( uint8_t *p_data, int i_data )
716 http_answer_t *ans = malloc( sizeof( http_answer_t ) );
717 http_field_t **pp_last;
720 if( strncmp( p_data, "HTTP/1.", 7 ) )
725 ans->i_version = atoi( &p_data[7] );
726 ans->i_error = strtol( p_data + 8, &p, 0 );
731 if( ( ( end = strchr( p, '\r' ) ) == NULL )&&
732 ( ( end = strchr( p, '\n' ) ) == NULL ) )
734 end = &p_data[i_data];
737 ans->psz_answer = strndup( p, end - p );
739 fprintf( stderr, "version=%d error=%d answer=%s\n",
740 ans->i_version, ans->i_error, ans->psz_answer );
741 ans->p_fields = NULL;
745 pp_last = &ans->p_fields;
749 http_field_t *p_field;
752 if( http_next_line( &p_data, &i_data ) )
756 if( !strncmp( p_data, "\r\n", 2 ) || !strncmp( p_data, "\n", 1 ) )
761 colon = strstr( p_data, ": " );
766 end = strstr( colon, "\n" ) - 1;
772 p_field = malloc( sizeof( http_field_t ) );
773 p_field->psz_name = strndup( p_data, colon - p_data );
774 p_field->psz_value = strndup( colon + 2, end - colon - 2 );
775 p_field->p_next = NULL;
778 pp_last = &p_field->p_next;
780 fprintf( stderr, "field name=`%s' value=`%s'\n",
781 p_field->psz_name, p_field->psz_value );
785 if( http_next_line( &p_data, &i_data ) )
790 ans->p_body = p_data;
791 ans->i_body = i_data;
792 fprintf( stderr, "body size=%d\n", i_data );
797 /*****************************************************************************
799 *****************************************************************************/
800 static void http_answer_free( http_answer_t *ans )
802 http_field_t *p_field = ans->p_fields;
806 http_field_t *p_next;
808 p_next = p_field->p_next;
809 free( p_field->psz_name );
810 free( p_field->psz_value );
816 free( ans->psz_answer );
820 /*****************************************************************************
822 *****************************************************************************/
823 static http_field_t *http_field_find( http_field_t *p_field, char *psz_name )
828 if( !strcasecmp( p_field->psz_name, psz_name ) )
833 p_field = p_field->p_next;
839 /*****************************************************************************
841 *****************************************************************************/
842 static int chunk_parse( chunk_t *ck, uint8_t *p_data, int i_data )
849 ck->i_type = GetWLE( p_data );
850 ck->i_size = GetWLE( p_data + 2);
851 ck->i_sequence = GetDWLE( p_data + 4);
852 ck->i_unknown = GetWLE( p_data + 8);
853 ck->i_size2 = GetWLE( p_data + 10);
855 ck->p_data = p_data + 12;
856 ck->i_data = __MIN( i_data - 12, ck->i_size2 - 8 );