1 /*****************************************************************************
2 * mms.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_access.h>
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
47 #ifdef HAVE_SYS_TYPES_H
48 # include <sys/types.h>
50 #ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h>
57 #include <vlc_network.h>
67 /****************************************************************************
69 * MMSProtocole documentation found at http://get.to/sdp
70 ****************************************************************************/
72 /*****************************************************************************
74 *****************************************************************************/
75 int MMSTUOpen ( access_t * );
76 void MMSTUClose ( access_t * );
79 static block_t *Block( access_t * );
80 static int Seek( access_t *, int64_t );
81 static int Control( access_t *, int, va_list );
83 static int MMSOpen ( access_t *, vlc_url_t *, int );
84 static int MMSStart( access_t *, uint32_t );
85 static int MMSStop ( access_t * );
86 static void MMSClose( access_t * );
89 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
90 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
92 static int mms_HeaderMediaRead( access_t *, int );
94 static int mms_ReceivePacket( access_t * );
96 static void* KeepAliveThread( void * );
98 int MMSTUOpen( access_t *p_access )
104 /* Set up p_access */
105 access_InitFields( p_access );
106 p_access->pf_read = NULL;
107 p_access->pf_block = Block;
108 p_access->pf_control = Control;
109 p_access->pf_seek = Seek;
111 p_access->p_sys = p_sys = calloc( 1, sizeof( access_sys_t ) );
112 if( !p_sys ) return VLC_ENOMEM;
114 p_sys->i_timeout = var_CreateGetInteger( p_access, "mms-timeout" );
116 vlc_mutex_init( &p_sys->lock_netwrite );
118 /* *** Parse URL and get server addr/port and path *** */
119 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
120 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
122 msg_Err( p_access, "invalid server name" );
123 vlc_UrlClean( &p_sys->url );
124 vlc_mutex_destroy( &p_sys->lock_netwrite );
128 if( p_sys->url.i_port <= 0 )
130 p_sys->url.i_port = 1755;
133 /* *** connect to this server *** */
134 /* look at requested protocol (udp/tcp) */
135 i_proto = MMS_PROTO_AUTO;
136 if( *p_access->psz_access )
138 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
140 i_proto = MMS_PROTO_UDP;
142 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
144 i_proto = MMS_PROTO_TCP;
149 if( i_proto == MMS_PROTO_AUTO )
150 { /* first try with TCP and then UDP*/
151 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
153 if( !p_access->b_die )
154 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
159 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
164 msg_Err( p_access, "cannot connect to server" );
165 vlc_UrlClean( &p_sys->url );
166 vlc_mutex_destroy( &p_sys->lock_netwrite );
171 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
175 * broadcast yy=0x02, xx= 0x00
176 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
178 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
180 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
182 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
184 p_sys->b_seekable = false;
188 p_sys->b_seekable = true;
189 p_access->info.i_size =
190 (uint64_t)p_sys->i_header +
191 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
194 /* *** Start stream *** */
195 if( MMSStart( p_access, 0xffffffff ) < 0 )
197 msg_Err( p_access, "cannot start stream" );
198 MMSTUClose ( p_access );
202 /* Keep the connection alive when paused */
203 p_sys->p_keepalive = malloc( sizeof( mmstu_keepalive_t ) );
204 if( !p_sys->p_keepalive )
206 MMSTUClose ( p_access );
209 p_sys->p_keepalive->p_access = p_access;
210 vlc_mutex_init( &p_sys->p_keepalive->lock );
211 vlc_cond_init( &p_sys->p_keepalive->wait );
212 p_sys->p_keepalive->b_paused = false;
213 if( vlc_clone( &p_sys->p_keepalive->handle, KeepAliveThread,
214 p_sys->p_keepalive, VLC_THREAD_PRIORITY_LOW ) )
216 vlc_cond_destroy( &p_sys->p_keepalive->wait );
217 vlc_mutex_destroy( &p_sys->p_keepalive->lock );
218 free( p_sys->p_keepalive );
219 p_sys->p_keepalive = NULL;
225 /*****************************************************************************
226 * Close: free unused data structures
227 *****************************************************************************/
228 void MMSTUClose( access_t *p_access )
230 access_sys_t *p_sys = p_access->p_sys;
232 if( p_sys->p_keepalive )
234 vlc_cancel( p_sys->p_keepalive->handle );
235 vlc_join( p_sys->p_keepalive->handle, NULL );
236 vlc_cond_destroy( &p_sys->p_keepalive->wait );
237 vlc_mutex_destroy( &p_sys->p_keepalive->lock );
238 free( p_sys->p_keepalive );
241 /* close connection with server */
242 MMSClose( p_access );
245 vlc_UrlClean( &p_sys->url );
246 vlc_mutex_destroy( &p_sys->lock_netwrite );
251 /*****************************************************************************
253 *****************************************************************************/
254 static int Control( access_t *p_access, int i_query, va_list args )
256 access_sys_t *p_sys = p_access->p_sys;
265 case ACCESS_CAN_SEEK:
266 pb_bool = (bool*)va_arg( args, bool* );
267 *pb_bool = p_sys->b_seekable;
270 case ACCESS_CAN_FASTSEEK:
271 pb_bool = (bool*)va_arg( args, bool* );
275 case ACCESS_CAN_PAUSE:
276 pb_bool = (bool*)va_arg( args, bool* );
280 case ACCESS_CAN_CONTROL_PACE:
281 pb_bool = (bool*)va_arg( args, bool* );
283 #if 0 /* Disable for now until we have a clock synchro algo
284 * which works with something else than MPEG over UDP */
291 case ACCESS_GET_PTS_DELAY:
292 pi_64 = (int64_t*)va_arg( args, int64_t * );
293 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * INT64_C(1000);
296 case ACCESS_GET_PRIVATE_ID_STATE:
297 i_int = (int)va_arg( args, int );
298 pb_bool = (bool *)va_arg( args, bool * );
300 if( i_int < 0 || i_int > 127 )
302 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? true : false;
306 case ACCESS_SET_PAUSE_STATE:
307 b_bool = (bool)va_arg( args, int );
311 Seek( p_access, p_access->info.i_pos );
313 if( p_sys->p_keepalive )
315 vlc_mutex_lock( &p_sys->p_keepalive->lock );
316 p_sys->p_keepalive->b_paused = b_bool;
318 vlc_cond_signal( &p_sys->p_keepalive->wait );
319 vlc_mutex_unlock( &p_sys->p_keepalive->lock );
323 case ACCESS_GET_TITLE_INFO:
324 case ACCESS_SET_TITLE:
325 case ACCESS_SET_SEEKPOINT:
326 case ACCESS_SET_PRIVATE_ID_STATE:
327 case ACCESS_GET_CONTENT_TYPE:
332 msg_Warn( p_access, "unimplemented query in control" );
339 /*****************************************************************************
340 * Seek: try to go at the right place
341 *****************************************************************************/
342 static int Seek( access_t * p_access, int64_t i_pos )
344 access_sys_t *p_sys = p_access->p_sys;
352 if( i_pos < p_sys->i_header)
355 if( p_access->info.i_pos < p_sys->i_header )
357 /* no need to restart stream, it was already one
358 * or no stream was yet read */
359 p_access->info.i_pos = i_pos;
364 i_packet = 0xffffffff;
370 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
371 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
373 if( p_sys->b_seekable && i_packet >= p_sys->i_packet_count )
376 msg_Dbg( p_access, "seeking to %"PRId64 " (packet:%d)", i_pos, i_packet );
379 msg_Dbg( p_access, "stream stopped (seek)" );
381 /* *** restart stream *** */
382 var_buffer_initwrite( &buffer, 0 );
383 var_buffer_add64( &buffer, 0 ); /* seek point in second */
384 var_buffer_add32( &buffer, 0xffffffff );
385 var_buffer_add32( &buffer, i_packet ); // begin from start
386 var_buffer_add8( &buffer, 0xff ); // stream time limit
387 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
388 var_buffer_add8( &buffer, 0xff ); //
389 var_buffer_add8( &buffer, 0x00 ); // don't use limit
390 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
392 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
393 buffer.p_data, buffer.i_data );
395 var_buffer_free( &buffer );
398 while( vlc_object_alive (p_access) )
400 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
402 p_access->info.b_eof = true;
406 if( p_sys->i_command == 0x1e )
408 msg_Dbg( p_access, "received 0x1e (seek)" );
413 while( vlc_object_alive (p_access) )
415 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
417 p_access->info.b_eof = true;
420 if( p_sys->i_command == 0x05 )
422 msg_Dbg( p_access, "received 0x05 (seek)" );
428 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
430 p_access->info.b_eof = true;
434 msg_Dbg( p_access, "Streaming restarted" );
436 p_sys->i_media_used += i_offset;
437 p_access->info.i_pos = i_pos;
438 p_access->info.b_eof = false;
443 /*****************************************************************************
445 *****************************************************************************/
446 static block_t *Block( access_t *p_access )
448 access_sys_t *p_sys = p_access->p_sys;
450 if( p_access->info.b_eof )
453 if( p_access->info.i_pos < p_sys->i_header )
455 const size_t i_copy = p_sys->i_header - p_access->info.i_pos;
457 block_t *p_block = block_New( p_access, i_copy );
461 memcpy( p_block->p_buffer, &p_sys->p_header[p_access->info.i_pos], i_copy );
462 p_access->info.i_pos += i_copy;
465 else if( p_sys->p_media && p_sys->i_media_used < __MAX( p_sys->i_media, p_sys->i_packet_length ) )
468 size_t i_padding = 0;
470 if( p_sys->i_media_used < p_sys->i_media )
471 i_copy = p_sys->i_media - p_sys->i_media_used;
472 if( __MAX( p_sys->i_media, p_sys->i_media_used ) < p_sys->i_packet_length )
473 i_padding = p_sys->i_packet_length - __MAX( p_sys->i_media, p_sys->i_media_used );
475 block_t *p_block = block_New( p_access, i_copy + i_padding );
480 memcpy( &p_block->p_buffer[0], &p_sys->p_media[p_sys->i_media_used], i_copy );
482 memset( &p_block->p_buffer[i_copy], 0, i_padding );
484 p_sys->i_media_used += i_copy + i_padding;
485 p_access->info.i_pos += i_copy + i_padding;
489 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
493 /****************************************************************************
494 * MMSOpen : Open a connection with the server over mmst or mmsu
495 ****************************************************************************/
496 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
498 access_sys_t *p_sys = p_access->p_sys;
499 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
504 int i_server_version;
506 int i_update_player_url;
507 int i_encryption_type;
514 /* *** Open a TCP connection with server *** */
515 msg_Dbg( p_access, "waiting for connection..." );
516 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
517 if( p_sys->i_handle_tcp < 0 )
519 msg_Err( p_access, "failed to open a connection (tcp)" );
523 "connection(tcp) with \"%s:%d\" successful",
527 /* *** Bind port if UDP protocol is selected *** */
530 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
533 net_Close( p_sys->i_handle_tcp );
537 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
539 if( p_sys->i_handle_udp < 0 )
541 msg_Err( p_access, "failed to open a connection (udp)" );
542 net_Close( p_sys->i_handle_tcp );
546 "connection(udp) at \"%s:%d\" successful",
547 p_sys->sz_bind_addr, 7000 );
550 /* *** Init context for mms prototcol *** */
551 GenerateGuid ( &p_sys->guid ); /* used to identify client by server */
553 "generated guid: "GUID_FMT,
554 GUID_PRINT( p_sys->guid ) );
555 p_sys->i_command_level = 1; /* updated after 0x1A command */
556 p_sys->i_seq_num = 0;
557 p_sys->i_media_packet_id_type = 0x04;
558 p_sys->i_header_packet_id_type = 0x02;
559 p_sys->i_proto = i_proto;
560 p_sys->i_packet_seq_num = 0;
561 p_sys->p_header = NULL;
563 p_sys->p_media = NULL;
565 p_sys->i_media_used = 0;
567 p_access->info.i_pos = 0;
568 p_sys->i_buffer_tcp = 0;
569 p_sys->i_buffer_udp = 0;
572 p_access->info.b_eof = false;
574 /* *** send command 1 : connection request *** */
575 var_buffer_initwrite( &buffer, 0 );
576 var_buffer_add16( &buffer, 0x001c );
577 var_buffer_add16( &buffer, 0x0003 );
579 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
580 GUID_PRINT( p_sys->guid ),
582 var_buffer_addUTF16( &buffer, tmp );
584 mms_CommandSend( p_access,
585 0x01, /* connexion request */
586 0x00000000, /* flags, FIXME */
587 0x0004000b, /* ???? */
591 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
593 var_buffer_free( &buffer );
594 MMSClose( p_access );
598 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
599 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
600 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
601 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
602 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
603 #define GETUTF16( psz, size ) \
606 psz = malloc( size + 1); \
607 for( i = 0; i < size; i++ ) \
614 GETUTF16( p_sys->psz_server_version, i_server_version );
615 GETUTF16( p_sys->psz_tool_version, i_tool_version );
616 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
617 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
620 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
621 p_sys->psz_server_version,
622 p_sys->psz_tool_version,
623 p_sys->psz_update_player_url,
624 p_sys->psz_encryption_type );
626 /* *** should make an 18 command to make data timing *** */
628 /* *** send command 2 : transport protocol selection *** */
629 var_buffer_reinitwrite( &buffer, 0 );
630 var_buffer_add32( &buffer, 0x00000000 );
631 var_buffer_add32( &buffer, 0x000a0000 );
632 var_buffer_add32( &buffer, 0x00000002 );
642 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
644 var_buffer_addUTF16( &buffer, tmp );
645 var_buffer_add16( &buffer, '0' );
647 mms_CommandSend( p_access,
648 0x02, /* connexion request */
649 0x00000000, /* flags, FIXME */
650 0xffffffff, /* ???? */
654 /* *** response from server, should be 0x02 or 0x03 *** */
655 mms_CommandRead( p_access, 0x02, 0x03 );
656 if( p_sys->i_command == 0x03 )
659 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
660 var_buffer_free( &buffer );
661 MMSClose( p_access );
664 else if( p_sys->i_command != 0x02 )
666 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
669 /* *** send command 5 : media file name/path requested *** */
670 var_buffer_reinitwrite( &buffer, 0 );
671 var_buffer_add64( &buffer, 0 );
673 /* media file path shouldn't start with / character */
674 mediapath = p_url->psz_path;
675 if ( *mediapath == '/' )
679 var_buffer_addUTF16( &buffer, mediapath );
681 mms_CommandSend( p_access,
683 p_sys->i_command_level,
688 /* *** wait for reponse *** */
689 mms_CommandRead( p_access, 0x1a, 0x06 );
691 /* test if server send 0x1A answer */
692 if( p_sys->i_command == 0x1A )
694 msg_Err( p_access, "id/password requested (not yet supported)" );
696 var_buffer_free( &buffer );
697 MMSClose( p_access );
700 if( p_sys->i_command != 0x06 )
703 "unknown answer (0x%x instead of 0x06)",
705 var_buffer_free( &buffer );
706 MMSClose( p_access );
710 /* 1 for file ok, 2 for authen ok */
711 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
714 msg_Dbg( p_access, "media file name/path accepted" );
717 msg_Dbg( p_access, "authentication accepted" );
721 msg_Err( p_access, "error while asking for file %d",
722 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
723 var_buffer_free( &buffer );
724 MMSClose( p_access );
728 p_sys->i_flags_broadcast =
729 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
730 p_sys->i_media_length =
731 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
732 p_sys->i_packet_length =
733 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
734 p_sys->i_packet_count =
735 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
736 p_sys->i_max_bit_rate =
737 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
738 p_sys->i_header_size =
739 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
742 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
743 "packet_length:%u packet_count:%"PRId32" max_bit_rate:%d "
745 p_sys->i_flags_broadcast,
746 p_sys->i_media_length,
747 (unsigned)p_sys->i_packet_length,
748 p_sys->i_packet_count,
749 p_sys->i_max_bit_rate,
750 p_sys->i_header_size );
752 /* *** send command 15 *** */
754 var_buffer_reinitwrite( &buffer, 0 );
755 var_buffer_add32( &buffer, 0 );
756 var_buffer_add32( &buffer, 0x8000 );
757 var_buffer_add32( &buffer, 0xffffffff );
758 var_buffer_add32( &buffer, 0x00 );
759 var_buffer_add32( &buffer, 0x00 );
760 var_buffer_add32( &buffer, 0x00 );
761 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
762 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
763 var_buffer_add32( &buffer, 0x00 );
764 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
765 buffer.p_data, buffer.i_data );
767 /* *** wait for reponse *** */
768 /* Commented out because it fails on some stream (no 0x11 answer) */
770 mms_CommandRead( p_access, 0x11, 0 );
772 if( p_sys->i_command != 0x11 )
775 "unknown answer (0x%x instead of 0x11)",
777 var_buffer_free( &buffer );
778 MMSClose( p_access );
783 /* *** now read header packet *** */
784 /* XXX could be split over multiples packets */
785 msg_Dbg( p_access, "reading header" );
788 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
790 msg_Err( p_access, "cannot receive header" );
791 var_buffer_free( &buffer );
792 MMSClose( p_access );
795 if( p_sys->i_header >= p_sys->i_header_size )
798 "header complete(%zu)",
803 "header incomplete (%zu/%zu), reading more",
805 p_sys->i_header_size );
808 /* *** parse header and get stream and their id *** */
809 /* get all streams properties,
811 * TODO : stream bitrates properties(optional)
812 * and bitrate mutual exclusion(optional) */
813 asf_HeaderParse ( &p_sys->asfh,
814 p_sys->p_header, p_sys->i_header );
815 asf_StreamSelect( &p_sys->asfh,
816 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
817 var_CreateGetInteger( p_access, "mms-all" ),
818 var_CreateGetInteger( p_access, "audio" ),
819 var_CreateGetInteger( p_access, "video" ) );
821 /* *** now select stream we want to receive *** */
822 /* TODO take care of stream bitrate TODO */
825 var_buffer_reinitwrite( &buffer, 0 );
826 /* for now, select first audio and video stream */
827 for( i = 1; i < 128; i++ )
830 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
835 var_buffer_add16( &buffer, 0xffff );
836 var_buffer_add16( &buffer, i );
842 if( p_sys->asfh.stream[i].i_selected )
844 var_buffer_add16( &buffer, 0x0000 );
846 "selecting stream[0x%x] %s (%d kb/s)",
848 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
850 p_sys->asfh.stream[i].i_bitrate / 1024);
854 var_buffer_add16( &buffer, 0x0002 );
856 "ignoring stream[0x%x] %s (%d kb/s)",
858 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
860 p_sys->asfh.stream[i].i_bitrate / 1024);
868 msg_Err( p_access, "cannot find any stream" );
869 var_buffer_free( &buffer );
870 MMSClose( p_access );
873 mms_CommandSend( p_access, 0x33,
875 0xffff | ( i_first << 16 ),
876 buffer.p_data, buffer.i_data );
878 mms_CommandRead( p_access, 0x21, 0 );
879 if( p_sys->i_command != 0x21 )
882 "unknown answer (0x%x instead of 0x21)",
884 var_buffer_free( &buffer );
885 MMSClose( p_access );
890 var_buffer_free( &buffer );
892 msg_Info( p_access, "connection successful" );
897 /****************************************************************************
898 * MMSStart : Start streaming
899 ****************************************************************************/
900 static int MMSStart( access_t *p_access, uint32_t i_packet )
902 access_sys_t *p_sys = p_access->p_sys;
905 /* *** start stream from packet 0 *** */
906 var_buffer_initwrite( &buffer, 0 );
907 var_buffer_add64( &buffer, 0 ); /* seek point in second */
908 var_buffer_add32( &buffer, 0xffffffff );
909 var_buffer_add32( &buffer, i_packet ); // begin from start
910 var_buffer_add8( &buffer, 0xff ); // stream time limit
911 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
912 var_buffer_add8( &buffer, 0xff ); //
913 var_buffer_add8( &buffer, 0x00 ); // don't use limit
914 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
916 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
917 buffer.p_data, buffer.i_data );
919 var_buffer_free( &buffer );
921 mms_CommandRead( p_access, 0x05, 0 );
923 if( p_sys->i_command != 0x05 )
926 "unknown answer (0x%x instead of 0x05)",
933 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
935 msg_Dbg( p_access, "streaming started" );
940 /****************************************************************************
941 * MMSStop : Stop streaming
942 ****************************************************************************/
943 static int MMSStop( access_t *p_access )
945 access_sys_t *p_sys = p_access->p_sys;
947 /* *** stop stream but keep connection alive *** */
948 mms_CommandSend( p_access,
950 p_sys->i_command_level,
956 /****************************************************************************
957 * MMSClose : Close streaming and connection
958 ****************************************************************************/
959 static void MMSClose( access_t *p_access )
961 access_sys_t *p_sys = p_access->p_sys;
963 msg_Dbg( p_access, "Connection closed" );
965 /* *** tell server that we will disconnect *** */
966 mms_CommandSend( p_access,
968 p_sys->i_command_level,
972 /* *** close sockets *** */
973 net_Close( p_sys->i_handle_tcp );
974 if( p_sys->i_proto == MMS_PROTO_UDP )
976 net_Close( p_sys->i_handle_udp );
979 FREENULL( p_sys->p_cmd );
980 FREENULL( p_sys->p_media );
981 FREENULL( p_sys->p_header );
983 FREENULL( p_sys->psz_server_version );
984 FREENULL( p_sys->psz_tool_version );
985 FREENULL( p_sys->psz_update_player_url );
986 FREENULL( p_sys->psz_encryption_type );
989 /****************************************************************************
991 * MMS specific functions
993 ****************************************************************************/
994 static int mms_CommandSend( access_t *p_access, int i_command,
995 uint32_t i_prefix1, uint32_t i_prefix2,
996 uint8_t *p_data, int i_data_old )
999 access_sys_t *p_sys = p_access->p_sys;
1000 int i_data_by8, i_ret;
1001 int i_data = i_data_old;
1003 while( i_data & 0x7 ) i_data++;
1004 i_data_by8 = i_data >> 3;
1006 /* first init buffer */
1007 var_buffer_initwrite( &buffer, 0 );
1009 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
1010 var_buffer_add32( &buffer, 0xB00BFACE );
1011 /* size after protocol type */
1012 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
1013 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
1014 var_buffer_add32( &buffer, i_data_by8 + 4 );
1015 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
1016 var_buffer_add64( &buffer, 0 );
1017 var_buffer_add32( &buffer, i_data_by8 + 2 );
1018 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
1019 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
1020 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
1022 /* specific command data */
1023 if( p_data && i_data > 0 )
1025 var_buffer_addmemory( &buffer, p_data, i_data_old );
1028 /* Append padding to the command data */
1029 var_buffer_add64( &buffer, 0 );
1032 vlc_mutex_lock( &p_sys->lock_netwrite );
1033 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
1034 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
1035 vlc_mutex_unlock( &p_sys->lock_netwrite );
1036 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
1038 var_buffer_free( &buffer );
1039 msg_Err( p_access, "failed to send command" );
1040 return VLC_EGENERIC;
1043 var_buffer_free( &buffer );
1047 static int NetFillBuffer( access_t *p_access )
1053 access_sys_t *p_sys = p_access->p_sys;
1055 struct pollfd ufd[2];
1056 unsigned timeout, nfd;
1058 /* FIXME when using udp */
1059 ssize_t i_tcp, i_udp;
1060 ssize_t i_tcp_read, i_udp_read;
1063 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1065 if( p_sys->i_proto == MMS_PROTO_UDP )
1067 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1071 i_udp = 0; /* there isn't udp socket */
1074 if( ( i_udp <= 0 ) && ( i_tcp <= 0 ) )
1076 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1081 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1084 /* Find if some data is available */
1089 /* Initialize file descriptor set */
1090 memset (ufd, 0, sizeof (ufd));
1095 ufd[nfd].fd = p_sys->i_handle_tcp;
1096 ufd[nfd].events = POLLIN;
1101 ufd[nfd].fd = p_sys->i_handle_udp;
1102 ufd[nfd].events = POLLIN;
1106 /* We'll wait 0.5 second if nothing happens */
1107 timeout = __MIN( 500, p_sys->i_timeout );
1109 if( i_try * timeout > p_sys->i_timeout )
1111 msg_Err(p_access, "no data received");
1115 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1120 if( !vlc_object_alive (p_access) || p_access->b_error )
1123 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1125 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1126 (i_ret < 0 && errno == EINTR) );
1130 msg_Err( p_access, "network poll error (%m)" );
1134 i_tcp_read = i_udp_read = 0;
1136 if( ( i_tcp > 0 ) && ufd[0].revents )
1139 recv( p_sys->i_handle_tcp,
1140 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1141 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1144 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1146 i_udp_read = recv( p_sys->i_handle_udp,
1147 p_sys->buffer_udp + p_sys->i_buffer_udp,
1148 i_udp + MMS_BUFFER_SIZE/2, 0 );
1152 if( p_sys->i_proto == MMS_PROTO_UDP )
1154 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1155 p_sys->i_buffer_tcp, i_tcp_read,
1156 p_sys->i_buffer_udp, i_udp_read );
1160 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1161 p_sys->i_buffer_tcp, i_tcp_read );
1165 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1166 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1168 return i_tcp_read + i_udp_read;
1172 static int mms_ParseCommand( access_t *p_access,
1177 #define GET32( i_pos ) \
1178 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1179 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1180 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1182 access_sys_t *p_sys = p_access->p_sys;
1186 free( p_sys->p_cmd );
1187 p_sys->i_cmd = i_data;
1188 p_sys->p_cmd = malloc( i_data );
1189 memcpy( p_sys->p_cmd, p_data, i_data );
1191 *pi_used = i_data; /* by default */
1193 if( i_data < MMS_CMD_HEADERSIZE )
1195 msg_Warn( p_access, "truncated command (header incomplete)" );
1196 p_sys->i_command = 0;
1199 i_id = GetDWLE( p_data + 4 );
1200 i_length = GetDWLE( p_data + 8 ) + 16;
1202 if( i_id != 0xb00bface || i_length < 16 )
1205 "incorrect command header (0x%"PRIx32")", i_id );
1206 p_sys->i_command = 0;
1210 if( i_length > p_sys->i_cmd )
1213 "truncated command (missing %zu bytes)",
1214 (size_t)i_length - i_data );
1215 p_sys->i_command = 0;
1218 else if( i_length < p_sys->i_cmd )
1220 p_sys->i_cmd = i_length;
1221 *pi_used = i_length;
1225 "recv command start_sequence:0x%8.8x command_id:0x%8.8x length:%d len8:%d sequence 0x%8.8x len8_II:%d dir_comm:0x%8.8x",
1229 /* 12: protocol type "MMS " */
1232 /* 24: unknown (0) */
1233 /* 28: unknown (0) */
1239 p_sys->i_command = GET32( 36 ) & 0xffff;
1242 return MMS_PACKET_CMD;
1245 static int mms_ParsePacket( access_t *p_access,
1246 uint8_t *p_data, size_t i_data,
1249 access_sys_t *p_sys = p_access->p_sys;
1250 int i_packet_seq_num;
1251 size_t i_packet_length;
1252 uint32_t i_packet_id;
1254 *pi_used = i_data; /* default */
1257 msg_Warn( p_access, "truncated packet (header incomplete)" );
1261 i_packet_id = p_data[4];
1262 i_packet_seq_num = GetDWLE( p_data );
1263 i_packet_length = GetWLE( p_data + 6 );
1265 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1267 if( i_packet_length > i_data || i_packet_length <= 8)
1269 /* msg_Dbg( p_access,
1270 "truncated packet (Declared %d bytes, Actual %d bytes)",
1271 i_packet_length, i_data ); */
1275 else if( i_packet_length < i_data )
1277 *pi_used = i_packet_length;
1280 if( i_packet_id == 0xff )
1283 "receive MMS UDP pair timing" );
1284 return( MMS_PACKET_UDP_TIMING );
1287 if( i_packet_id != p_sys->i_header_packet_id_type &&
1288 i_packet_id != p_sys->i_media_packet_id_type )
1290 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1294 /* we now have a media or a header packet */
1295 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1298 /* FIXME for udp could be just wrong order ? */
1300 "detected packet lost (%d != %d)",
1302 p_sys->i_packet_seq_num );
1305 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1307 if( i_packet_id == p_sys->i_header_packet_id_type )
1309 if( p_sys->p_header )
1311 p_sys->p_header = realloc( p_sys->p_header,
1312 p_sys->i_header + i_packet_length - 8 );
1313 memcpy( &p_sys->p_header[p_sys->i_header],
1314 p_data + 8, i_packet_length - 8 );
1315 p_sys->i_header += i_packet_length - 8;
1320 uint8_t* p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1321 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1322 p_sys->p_header = p_packet;
1323 p_sys->i_header = i_packet_length - 8;
1325 /* msg_Dbg( p_access,
1326 "receive header packet (%d bytes)",
1327 i_packet_length - 8 ); */
1329 return MMS_PACKET_HEADER;
1333 uint8_t* p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1334 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1335 FREENULL( p_sys->p_media );
1336 p_sys->p_media = p_packet;
1337 p_sys->i_media = i_packet_length - 8;
1338 p_sys->i_media_used = 0;
1339 /* msg_Dbg( p_access,
1340 "receive media packet (%d bytes)",
1341 i_packet_length - 8 ); */
1343 return MMS_PACKET_MEDIA;
1347 static int mms_ReceivePacket( access_t *p_access )
1349 access_sys_t *p_sys = p_access->p_sys;
1350 int i_packet_tcp_type;
1351 int i_packet_udp_type;
1355 bool b_refill = true;
1357 /* first if we need to refill buffer */
1358 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1360 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1362 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1363 (uint32_t)p_sys->i_buffer_tcp )
1368 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1373 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1374 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1379 if( b_refill && NetFillBuffer( p_access ) < 0 )
1381 msg_Warn( p_access, "cannot fill buffer" );
1385 i_packet_tcp_type = -1;
1386 i_packet_udp_type = -1;
1388 if( p_sys->i_buffer_tcp > 0 )
1392 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1395 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1396 p_sys->i_buffer_tcp, &i_used );
1402 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1403 p_sys->i_buffer_tcp, &i_used );
1405 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1407 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1408 MMS_BUFFER_SIZE - i_used );
1410 p_sys->i_buffer_tcp -= i_used;
1412 else if( p_sys->i_buffer_udp > 0 )
1417 mms_ParsePacket( p_access, p_sys->buffer_udp,
1418 p_sys->i_buffer_udp, &i_used );
1420 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1422 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1423 MMS_BUFFER_SIZE - i_used );
1425 p_sys->i_buffer_udp -= i_used;
1428 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1430 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1431 i_packet_tcp_type = -1;
1434 if( i_packet_tcp_type != -1 )
1436 return i_packet_tcp_type;
1438 else if( i_packet_udp_type != -1 )
1440 return i_packet_udp_type;
1445 static int mms_ReceiveCommand( access_t *p_access )
1447 access_sys_t *p_sys = p_access->p_sys;
1454 if( NetFillBuffer( p_access ) < 0 )
1456 msg_Warn( p_access, "cannot fill buffer" );
1457 return VLC_EGENERIC;
1459 if( p_sys->i_buffer_tcp > 0 )
1461 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1462 p_sys->i_buffer_tcp, &i_used );
1463 if( i_used < MMS_BUFFER_SIZE )
1465 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1466 MMS_BUFFER_SIZE - i_used );
1468 p_sys->i_buffer_tcp -= i_used;
1472 return VLC_EGENERIC;
1475 if( p_sys->i_command == 0x1b )
1477 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1486 return VLC_EGENERIC;
1493 #define MMS_RETRY_MAX 10
1494 #define MMS_RETRY_SLEEP 50000
1496 static int mms_CommandRead( access_t *p_access, int i_command1,
1499 access_sys_t *p_sys = p_access->p_sys;
1503 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1505 i_status = mms_ReceiveCommand( p_access );
1506 if( i_status < 0 || p_sys->i_command == 0 )
1509 msleep( MMS_RETRY_SLEEP );
1511 else if( i_command1 == 0 && i_command2 == 0)
1515 else if( p_sys->i_command == i_command1 ||
1516 p_sys->i_command == i_command2 )
1522 switch( p_sys->i_command )
1525 msg_Warn( p_access, "socket closed by server" );
1526 p_access->info.b_eof = true;
1527 return VLC_EGENERIC;
1529 msg_Warn( p_access, "end of media stream" );
1530 p_access->info.b_eof = true;
1531 return VLC_EGENERIC;
1537 p_access->info.b_eof = true;
1538 msg_Warn( p_access, "failed to receive command (aborting)" );
1540 return VLC_EGENERIC;
1544 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1546 access_sys_t *p_sys = p_access->p_sys;
1549 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1553 if( !vlc_object_alive (p_access) )
1556 i_status = mms_ReceivePacket( p_access );
1560 msg_Warn( p_access, "cannot receive header (%d/%d)",
1561 i_count, MMS_RETRY_MAX );
1562 msleep( MMS_RETRY_SLEEP );
1564 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1568 else if( i_status == MMS_PACKET_CMD )
1570 switch( p_sys->i_command )
1573 msg_Warn( p_access, "socket closed by server" );
1574 p_access->info.b_eof = true;
1577 msg_Warn( p_access, "end of media stream" );
1578 p_access->info.b_eof = true;
1581 /* XXX not too dificult to be done EXCEPT that we
1582 * need to restart demuxer... and I don't see how we
1583 * could do that :p */
1585 "reinitialization needed --> unsupported" );
1586 p_access->info.b_eof = true;
1594 msg_Err( p_access, "cannot receive %s (aborting)",
1595 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1596 p_access->info.b_eof = true;
1600 static void* KeepAliveThread( void *p_data )
1602 mmstu_keepalive_t *p_thread = (mmstu_keepalive_t *) p_data;
1603 access_t *p_access = p_thread->p_access;
1605 vlc_mutex_lock( &p_thread->lock );
1606 mutex_cleanup_push( &p_thread->lock );
1610 /* Do nothing until paused (if ever) */
1611 while( !p_thread->b_paused )
1612 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
1618 /* Send keep-alive every ten seconds */
1619 vlc_mutex_unlock( &p_thread->lock );
1620 canc = vlc_savecancel();
1622 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1624 vlc_restorecancel( canc );
1625 vlc_mutex_lock( &p_thread->lock );
1627 msleep( 10 * CLOCK_FREQ );
1629 while( p_thread->b_paused );