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;
74 p_access->pf_read = Read;
75 p_access->pf_block = NULL;
76 p_access->pf_control = Control;
77 p_access->pf_seek = Seek;
78 p_access->info.i_update = 0;
79 p_access->info.i_size = 0;
80 p_access->info.i_pos = 0;
81 p_access->info.b_eof = VLC_FALSE;
82 p_access->info.i_title = 0;
83 p_access->info.i_seekpoint = 0;
84 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
85 memset( p_sys, 0, sizeof( access_sys_t ) );
86 p_sys->i_proto= MMS_PROTO_HTTP;
90 /* open a tcp connection */
91 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
92 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
94 msg_Err( p_access, "invalid host" );
95 vlc_UrlClean( &p_sys->url );
99 if( p_sys->url.i_port <= 0 )
100 p_sys->url.i_port = 80;
102 if( Describe( p_access, &psz_location ) )
104 vlc_UrlClean( &p_sys->url );
108 /* Handle redirection */
109 if( psz_location && *psz_location )
111 playlist_t * p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_PARENT );
113 msg_Dbg( p_access, "redirection to %s", psz_location );
117 msg_Err( p_access, "redirection failed: can't find playlist" );
118 free( psz_location );
121 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
122 playlist_Add( p_playlist, psz_location, psz_location,
123 PLAYLIST_INSERT | PLAYLIST_GO,
124 p_playlist->i_index + 1 );
125 vlc_object_release( p_playlist );
127 free( psz_location );
129 p_access->pf_read = ReadRedirect;
134 if( Start( p_access, 0 ) )
136 msg_Err( p_access, "cannot start stream" );
137 free( p_sys->p_header );
138 vlc_UrlClean( &p_sys->url );
143 if( !p_sys->b_broadcast )
145 p_access->info.i_size = p_sys->asfh.i_file_size;
148 /* Update default_pts to a suitable value for mms access */
149 var_Get( p_access, "mms-caching", &val );
154 /*****************************************************************************
155 * Close: free unused data structures
156 *****************************************************************************/
157 void E_( MMSHClose )( access_t *p_access )
159 access_sys_t *p_sys = p_access->p_sys;
165 /*****************************************************************************
167 *****************************************************************************/
168 static int Control( access_t *p_access, int i_query, va_list args )
170 access_sys_t *p_sys = p_access->p_sys;
179 case ACCESS_CAN_SEEK:
180 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
181 *pb_bool = !p_sys->b_broadcast;
183 case ACCESS_CAN_FASTSEEK:
184 case ACCESS_CAN_PAUSE:
185 case ACCESS_CAN_CONTROL_PACE:
186 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
187 *pb_bool = VLC_FALSE;
192 pi_int = (int*)va_arg( args, int * );
193 *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
196 case ACCESS_GET_PTS_DELAY:
197 pi_64 = (int64_t*)va_arg( args, int64_t * );
198 var_Get( p_access, "mms-caching", &val );
199 *pi_64 = val.i_int * 1000;
203 case ACCESS_SET_PAUSE_STATE:
204 case ACCESS_GET_TITLE_INFO:
205 case ACCESS_SET_TITLE:
206 case ACCESS_SET_SEEKPOINT:
210 msg_Err( p_access, "unimplemented query in control" );
217 /*****************************************************************************
218 * Seek: try to go at the right place
219 *****************************************************************************/
220 static int Seek( access_t *p_access, int64_t i_pos )
222 access_sys_t *p_sys = p_access->p_sys;
227 msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
229 i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
230 i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
233 Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
235 while( !p_access->b_die )
237 if( GetPacket( p_access, &ck ) )
241 if( ck.i_type != 0x4824 )
244 msg_Warn( p_access, "skipping header" );
247 p_access->info.i_pos = i_pos;
248 p_access->info.b_eof = VLC_FALSE;
249 p_sys->i_packet_used += i_offset;
254 /*****************************************************************************
256 *****************************************************************************/
257 static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
262 /*****************************************************************************
264 *****************************************************************************/
265 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
267 access_sys_t *p_sys = p_access->p_sys;
271 if( p_access->info.b_eof )
274 while( i_data < i_len )
276 if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
278 int i_offset = p_access->info.i_pos - p_sys->i_start;
279 i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
280 memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
283 p_access->info.i_pos += i_copy;
285 else if( p_sys->i_packet_used < p_sys->i_packet_length )
287 i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
290 memcpy( &p_buffer[i_data],
291 &p_sys->p_packet[p_sys->i_packet_used],
295 p_sys->i_packet_used += i_copy;
296 p_access->info.i_pos += i_copy;
298 else if( p_sys->i_packet_length > 0 &&
299 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
301 i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
304 memset( &p_buffer[i_data], 0, i_copy );
307 p_sys->i_packet_used += i_copy;
308 p_access->info.i_pos += i_copy;
313 if( GetPacket( p_access, &ck ) )
315 if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
317 char *psz_location = NULL;
319 p_sys->i_start = p_access->info.i_pos;
321 msg_Dbg( p_access, "stoping the stream" );
324 msg_Dbg( p_access, "describe the stream" );
325 if( Describe( p_access, &psz_location ) )
327 msg_Err( p_access, "describe failed" );
328 p_access->info.b_eof = VLC_TRUE;
331 if( Start( p_access, 0 ) )
333 msg_Err( p_access, "Start failed" );
334 p_access->info.b_eof = VLC_TRUE;
340 p_access->info.b_eof = VLC_TRUE;
344 if( ck.i_type != 0x4424 )
346 p_sys->i_packet_used = 0;
347 p_sys->i_packet_length = 0;
355 /*****************************************************************************
357 *****************************************************************************/
358 static int Describe( access_t *p_access, char **ppsz_location )
360 access_sys_t *p_sys = p_access->p_sys;
361 char *psz_location = NULL;
366 p_sys->b_broadcast = VLC_TRUE;
367 p_sys->i_request_context = 1;
368 p_sys->i_packet_sequence = 0;
369 p_sys->i_packet_used = 0;
370 p_sys->i_packet_length = 0;
371 p_sys->p_packet = NULL;
372 E_( GenerateGuid )( &p_sys->guid );
374 if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
375 p_sys->url.i_port ) ) < 0 )
377 msg_Err( p_access, "cannot connect to%s:%d", p_sys->url.psz_host, p_sys->url.i_port );
381 /* send first request */
382 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
383 "GET %s HTTP/1.0\r\n"
385 "User-Agent: NSPlayer/4.1.0.3856\r\n"
387 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
388 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
389 "Connection: Close\r\n",
390 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
391 p_sys->url.psz_host, p_sys->url.i_port,
392 p_sys->i_request_context++,
393 GUID_PRINT( p_sys->guid ) );
395 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
397 msg_Err( p_access, "failed to send request" );
401 /* Receive the http header */
402 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
404 msg_Err( p_access, "failed to read answer" );
407 if( strncmp( psz, "HTTP/1.", 7 ) )
409 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
413 i_code = atoi( &psz[9] );
416 msg_Err( p_access, "error: %s", psz );
421 msg_Dbg( p_access, "HTTP reply '%s'", psz );
425 char *psz = net_Gets( p_access, p_sys->fd );
430 msg_Err( p_access, "failed to read answer" );
440 if( ( p = strchr( psz, ':' ) ) == NULL )
442 msg_Err( p_access, "malformed header line: %s", psz );
447 while( *p == ' ' ) p++;
449 /* FIXME FIXME test Content-Type to see if it's a plain stream or an
451 if( !strcasecmp( psz, "Pragma" ) )
453 if( strstr( p, "features" ) )
455 /* FIXME, it is a bit badly done here ..... */
456 if( strstr( p, "broadcast" ) )
458 msg_Dbg( p_access, "stream type = broadcast" );
459 p_sys->b_broadcast = VLC_TRUE;
461 else if( strstr( p, "seekable" ) )
463 msg_Dbg( p_access, "stream type = seekable" );
464 p_sys->b_broadcast = VLC_FALSE;
468 msg_Warn( p_access, "unknow stream types (%s)", p );
469 p_sys->b_broadcast = VLC_FALSE;
473 else if( !strcasecmp( psz, "Location" ) )
475 psz_location = strdup( p );
481 /* Handle the redirection */
482 if( ( i_code == 301 || i_code == 302 ||
483 i_code == 303 || i_code == 307 ) &&
484 psz_location && *psz_location )
486 msg_Dbg( p_access, "redirection to %s", psz_location );
487 net_Close( p_sys->fd ); p_sys->fd = -1;
489 *ppsz_location = psz_location;
493 /* Read the asf header */
495 p_sys->p_header = NULL;
499 if( GetPacket( p_access, &ck ) ||
500 ck.i_type != 0x4824 )
507 p_sys->i_header += ck.i_data;
508 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
509 memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
510 ck.p_data, ck.i_data );
513 msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
514 if( p_sys->i_header <= 0 )
516 msg_Err( p_access, "header size == 0" );
519 /* close this connection */
520 net_Close( p_sys->fd ); p_sys->fd = -1;
522 /* *** parse header and get stream and their id *** */
523 /* get all streams properties,
525 * TODO : stream bitrates properties(optional)
526 * and bitrate mutual exclusion(optional) */
527 E_( asf_HeaderParse )( &p_sys->asfh,
528 p_sys->p_header, p_sys->i_header );
529 msg_Dbg( p_access, "packet count=%lld packet size=%d",
530 p_sys->asfh.i_data_packets_count,
531 p_sys->asfh.i_min_data_packet_size );
533 E_( asf_StreamSelect)( &p_sys->asfh,
534 config_GetInt( p_access, "mms-maxbitrate" ),
535 config_GetInt( p_access, "mms-all" ),
536 config_GetInt( p_access, "audio" ),
537 config_GetInt( p_access, "video" ) );
544 net_Close( p_sys->fd );
550 /*****************************************************************************
552 *****************************************************************************/
553 static int Start( access_t *p_access, off_t i_pos )
555 access_sys_t *p_sys = p_access->p_sys;
560 msg_Dbg( p_access, "starting stream" );
562 if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
563 p_sys->url.i_port ) ) < 0 )
565 /* should not occur */
566 msg_Err( p_access, "cannot connect to the server" );
570 for( i = 1; i < 128; i++ )
572 if( p_sys->asfh.stream[i].i_selected )
580 msg_Err( p_access, "no stream selected" );
583 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
584 "GET %s HTTP/1.0\r\n"
586 "User-Agent: NSPlayer/4.1.0.3856\r\n"
588 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
589 p_sys->url.psz_host, p_sys->url.i_port );
590 if( p_sys->b_broadcast )
592 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
593 "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
594 p_sys->i_request_context++ );
598 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
599 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
600 (uint32_t)((i_pos >> 32)&0xffffffff),
601 (uint32_t)(i_pos&0xffffffff),
602 p_sys->i_request_context++ );
604 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
605 "Pragma: xPlayStrm=1\r\n"
606 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
607 "Pragma: stream-switch-count=%d\r\n"
608 "Pragma: stream-switch-entry=",
609 GUID_PRINT( p_sys->guid ),
612 for( i = 1; i < 128; i++ )
614 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
617 if( p_sys->asfh.stream[i].i_selected )
622 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
623 "ffff:%d:%d ", i, i_select );
626 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" );
627 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
629 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
631 msg_Err( p_access, "failed to send request" );
635 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
637 msg_Err( p_access, "cannot read data" );
640 if( atoi( &psz[9] ) >= 400 )
642 msg_Err( p_access, "error: %s", psz );
646 msg_Dbg( p_access, "HTTP reply '%s'", psz );
649 /* FIXME check HTTP code */
652 char *psz = net_Gets( p_access, p_sys->fd );
655 msg_Err( p_access, "cannot read data" );
663 msg_Dbg( p_access, "%s", psz );
667 p_sys->i_packet_used = 0;
668 p_sys->i_packet_length = 0;
673 /*****************************************************************************
675 *****************************************************************************/
676 static void Stop( access_t *p_access )
678 access_sys_t *p_sys = p_access->p_sys;
680 msg_Dbg( p_access, "closing stream" );
683 net_Close( p_sys->fd );
688 /*****************************************************************************
690 *****************************************************************************/
691 static int GetPacket( access_t * p_access, chunk_t *p_ck )
693 access_sys_t *p_sys = p_access->p_sys;
696 memset( p_ck, 0, sizeof( chunk_t ) );
698 /* Read the chunk header */
699 if( net_Read( p_access, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
701 /* msg_Err( p_access, "cannot read data" ); */
705 p_ck->i_type = GetWLE( p_sys->buffer);
706 p_ck->i_size = GetWLE( p_sys->buffer + 2);
707 p_ck->i_sequence = GetDWLE( p_sys->buffer + 4);
708 p_ck->i_unknown = GetWLE( p_sys->buffer + 8);
709 p_ck->i_size2 = GetWLE( p_sys->buffer + 10);
710 p_ck->p_data = p_sys->buffer + 12;
711 p_ck->i_data = p_ck->i_size2 - 8;
713 if( p_ck->i_type == 0x4524 ) // Transfer complete
715 if( p_ck->i_sequence == 0 )
717 msg_Warn( p_access, "EOF" );
722 msg_Warn( p_access, "Next stream follow but not supported" );
726 else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
728 msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
732 if( p_ck->i_data > 0 &&
733 net_Read( p_access, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
735 msg_Err( p_access, "cannot read data" );
739 if( p_sys->i_packet_sequence != 0 &&
740 p_ck->i_sequence != p_sys->i_packet_sequence )
742 msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
745 p_sys->i_packet_sequence = p_ck->i_sequence + 1;
746 p_sys->i_packet_used = 0;
747 p_sys->i_packet_length = p_ck->i_data;
748 p_sys->p_packet = p_ck->p_data;