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 ssize_t Read( access_t *, uint8_t *, size_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 p_access->pf_read = Read;
106 p_access->pf_block = NULL;
107 p_access->pf_control = Control;
108 p_access->pf_seek = Seek;
109 p_access->info.i_update = 0;
110 p_access->info.i_size = 0;
111 p_access->info.i_pos = 0;
112 p_access->info.b_eof = false;
113 p_access->info.i_title = 0;
114 p_access->info.i_seekpoint = 0;
115 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
116 if( !p_sys ) return VLC_ENOMEM;
117 memset( p_sys, 0, sizeof( access_sys_t ) );
119 p_sys->i_timeout = var_CreateGetInteger( p_access, "mms-timeout" );
121 vlc_mutex_init( &p_sys->lock_netwrite );
123 /* *** Parse URL and get server addr/port and path *** */
124 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
125 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
127 msg_Err( p_access, "invalid server name" );
128 vlc_UrlClean( &p_sys->url );
129 vlc_mutex_destroy( &p_sys->lock_netwrite );
133 if( p_sys->url.i_port <= 0 )
135 p_sys->url.i_port = 1755;
138 /* *** connect to this server *** */
139 /* look at requested protocol (udp/tcp) */
140 i_proto = MMS_PROTO_AUTO;
141 if( *p_access->psz_access )
143 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
145 i_proto = MMS_PROTO_UDP;
147 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
149 i_proto = MMS_PROTO_TCP;
154 if( i_proto == MMS_PROTO_AUTO )
155 { /* first try with TCP and then UDP*/
156 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
158 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
163 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
168 msg_Err( p_access, "cannot connect to server" );
169 vlc_UrlClean( &p_sys->url );
170 vlc_mutex_destroy( &p_sys->lock_netwrite );
175 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
179 * broadcast yy=0x02, xx= 0x00
180 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
182 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
184 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
186 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
188 p_sys->b_seekable = false;
192 p_sys->b_seekable = true;
193 p_access->info.i_size =
194 (uint64_t)p_sys->i_header +
195 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
198 /* *** Start stream *** */
199 if( MMSStart( p_access, 0xffffffff ) < 0 )
201 msg_Err( p_access, "cannot start stream" );
202 MMSTUClose ( p_access );
206 /* Keep the connection alive when paused */
207 p_sys->p_keepalive = malloc( sizeof( mmstu_keepalive_t ) );
208 p_sys->p_keepalive->p_access = p_access;
209 vlc_mutex_init( &p_sys->p_keepalive->lock );
210 p_sys->p_keepalive->b_paused = false;
211 if( vlc_clone( &p_sys->p_keepalive->handle, KeepAliveThread,
212 p_sys->p_keepalive, VLC_THREAD_PRIORITY_LOW ) )
214 vlc_mutex_destroy( &p_sys->p_keepalive->lock );
215 free( p_sys->p_keepalive );
216 p_sys->p_keepalive = NULL;
222 /*****************************************************************************
223 * Close: free unused data structures
224 *****************************************************************************/
225 void MMSTUClose( access_t *p_access )
227 access_sys_t *p_sys = p_access->p_sys;
229 if( p_sys->p_keepalive )
231 vlc_cancel( p_sys->p_keepalive->handle );
232 vlc_join( p_sys->p_keepalive->handle, NULL );
233 vlc_mutex_destroy( &p_sys->p_keepalive->lock );
234 free( p_sys->p_keepalive );
237 /* close connection with server */
238 MMSClose( p_access );
241 vlc_UrlClean( &p_sys->url );
242 vlc_mutex_destroy( &p_sys->lock_netwrite );
247 /*****************************************************************************
249 *****************************************************************************/
250 static int Control( access_t *p_access, int i_query, va_list args )
252 access_sys_t *p_sys = p_access->p_sys;
263 case ACCESS_CAN_SEEK:
264 pb_bool = (bool*)va_arg( args, bool* );
265 *pb_bool = p_sys->b_seekable;
268 case ACCESS_CAN_FASTSEEK:
269 pb_bool = (bool*)va_arg( args, bool* );
273 case ACCESS_CAN_PAUSE:
274 pb_bool = (bool*)va_arg( args, bool* );
278 case ACCESS_CAN_CONTROL_PACE:
279 pb_bool = (bool*)va_arg( args, bool* );
281 #if 0 /* Disable for now until we have a clock synchro algo
282 * which works with something else than MPEG over UDP */
290 pi_int = (int*)va_arg( args, int * );
291 *pi_int = 3 * p_sys->i_packet_length;
294 case ACCESS_GET_PTS_DELAY:
295 pi_64 = (int64_t*)va_arg( args, int64_t * );
296 var_Get( p_access, "mms-caching", &val );
297 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * INT64_C(1000);
300 case ACCESS_GET_PRIVATE_ID_STATE:
301 i_int = (int)va_arg( args, int );
302 pb_bool = (bool *)va_arg( args, bool * );
304 if( i_int < 0 || i_int > 127 )
306 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? true : false;
310 case ACCESS_SET_PAUSE_STATE:
311 b_bool = (bool)va_arg( args, int );
315 Seek( p_access, p_access->info.i_pos );
317 vlc_mutex_lock( &p_sys->p_keepalive->lock );
318 p_sys->p_keepalive->b_paused = b_bool;
320 vlc_cond_signal( &p_sys->p_keepalive->wait );
321 vlc_mutex_unlock( &p_sys->p_keepalive->lock );
324 case ACCESS_GET_TITLE_INFO:
325 case ACCESS_SET_TITLE:
326 case ACCESS_SET_SEEKPOINT:
327 case ACCESS_SET_PRIVATE_ID_STATE:
328 case ACCESS_GET_CONTENT_TYPE:
333 msg_Warn( p_access, "unimplemented query in control" );
340 /*****************************************************************************
341 * Seek: try to go at the right place
342 *****************************************************************************/
343 static int Seek( access_t * p_access, int64_t i_pos )
345 access_sys_t *p_sys = p_access->p_sys;
353 if( i_pos < p_sys->i_header)
356 if( p_access->info.i_pos < p_sys->i_header )
358 /* no need to restart stream, it was already one
359 * or no stream was yet read */
360 p_access->info.i_pos = i_pos;
365 i_packet = 0xffffffff;
371 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
372 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
374 msg_Dbg( p_access, "seeking to %"PRId64 " (packet:%d)", i_pos, i_packet );
377 msg_Dbg( p_access, "stream stopped (seek)" );
379 /* *** restart stream *** */
380 var_buffer_initwrite( &buffer, 0 );
381 var_buffer_add64( &buffer, 0 ); /* seek point in second */
382 var_buffer_add32( &buffer, 0xffffffff );
383 var_buffer_add32( &buffer, i_packet ); // begin from start
384 var_buffer_add8( &buffer, 0xff ); // stream time limit
385 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
386 var_buffer_add8( &buffer, 0xff ); //
387 var_buffer_add8( &buffer, 0x00 ); // don't use limit
388 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
390 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
391 buffer.p_data, buffer.i_data );
393 var_buffer_free( &buffer );
396 while( vlc_object_alive (p_access) )
398 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
400 p_access->info.b_eof = true;
404 if( p_sys->i_command == 0x1e )
406 msg_Dbg( p_access, "received 0x1e (seek)" );
411 while( vlc_object_alive (p_access) )
413 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
415 p_access->info.b_eof = true;
418 if( p_sys->i_command == 0x05 )
420 msg_Dbg( p_access, "received 0x05 (seek)" );
426 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
428 p_access->info.b_eof = true;
432 msg_Dbg( p_access, "Streaming restarted" );
434 p_sys->i_media_used += i_offset;
435 p_access->info.i_pos = i_pos;
436 p_access->info.b_eof = false;
441 /*****************************************************************************
443 *****************************************************************************/
444 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
446 access_sys_t *p_sys = p_access->p_sys;
450 if( p_access->info.b_eof )
457 /* *** now send data if needed *** */
458 while( i_data < i_len )
460 if( p_access->info.i_pos < p_sys->i_header )
462 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
463 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
465 p_access->info.i_pos += i_copy;
467 else if( p_sys->i_media_used < p_sys->i_media )
469 i_copy = __MIN( i_len - i_data ,
470 p_sys->i_media - p_sys->i_media_used );
471 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
473 p_sys->i_media_used += i_copy;
474 p_access->info.i_pos += i_copy;
476 else if( p_sys->p_media != NULL &&
477 p_sys->i_media_used < p_sys->i_packet_length )
479 i_copy = __MIN( i_len - i_data,
480 p_sys->i_packet_length - p_sys->i_media_used);
481 memset( &p_buffer[i_data], 0, i_copy );
484 p_sys->i_media_used += i_copy;
485 p_access->info.i_pos += i_copy;
487 else if( p_access->info.b_eof ||
488 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
497 /****************************************************************************
498 * MMSOpen : Open a connection with the server over mmst or mmsu
499 ****************************************************************************/
500 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
502 access_sys_t *p_sys = p_access->p_sys;
503 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
508 int i_server_version;
510 int i_update_player_url;
511 int i_encryption_type;
518 /* *** Open a TCP connection with server *** */
519 msg_Dbg( p_access, "waiting for connection..." );
520 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
521 if( p_sys->i_handle_tcp < 0 )
523 msg_Err( p_access, "failed to open a connection (tcp)" );
527 "connection(tcp) with \"%s:%d\" successful",
531 /* *** Bind port if UDP protocol is selected *** */
534 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
537 net_Close( p_sys->i_handle_tcp );
541 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
543 if( p_sys->i_handle_udp < 0 )
545 msg_Err( p_access, "failed to open a connection (udp)" );
546 net_Close( p_sys->i_handle_tcp );
550 "connection(udp) at \"%s:%d\" successful",
551 p_sys->sz_bind_addr, 7000 );
554 /* *** Init context for mms prototcol *** */
555 GenerateGuid ( &p_sys->guid ); /* used to identify client by server */
557 "generated guid: "GUID_FMT,
558 GUID_PRINT( p_sys->guid ) );
559 p_sys->i_command_level = 1; /* updated after 0x1A command */
560 p_sys->i_seq_num = 0;
561 p_sys->i_media_packet_id_type = 0x04;
562 p_sys->i_header_packet_id_type = 0x02;
563 p_sys->i_proto = i_proto;
564 p_sys->i_packet_seq_num = 0;
565 p_sys->p_header = NULL;
567 p_sys->p_media = NULL;
569 p_sys->i_media_used = 0;
571 p_access->info.i_pos = 0;
572 p_sys->i_buffer_tcp = 0;
573 p_sys->i_buffer_udp = 0;
576 p_access->info.b_eof = false;
578 /* *** send command 1 : connection request *** */
579 var_buffer_initwrite( &buffer, 0 );
580 var_buffer_add16( &buffer, 0x001c );
581 var_buffer_add16( &buffer, 0x0003 );
583 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
584 GUID_PRINT( p_sys->guid ),
586 var_buffer_addUTF16( &buffer, tmp );
588 mms_CommandSend( p_access,
589 0x01, /* connexion request */
590 0x00000000, /* flags, FIXME */
591 0x0004000b, /* ???? */
595 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
597 var_buffer_free( &buffer );
598 MMSClose( p_access );
602 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
603 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
604 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
605 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
606 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
607 #define GETUTF16( psz, size ) \
610 psz = malloc( size + 1); \
611 for( i = 0; i < size; i++ ) \
618 GETUTF16( p_sys->psz_server_version, i_server_version );
619 GETUTF16( p_sys->psz_tool_version, i_tool_version );
620 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
621 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
624 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
625 p_sys->psz_server_version,
626 p_sys->psz_tool_version,
627 p_sys->psz_update_player_url,
628 p_sys->psz_encryption_type );
630 /* *** should make an 18 command to make data timing *** */
632 /* *** send command 2 : transport protocol selection *** */
633 var_buffer_reinitwrite( &buffer, 0 );
634 var_buffer_add32( &buffer, 0x00000000 );
635 var_buffer_add32( &buffer, 0x000a0000 );
636 var_buffer_add32( &buffer, 0x00000002 );
646 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
648 var_buffer_addUTF16( &buffer, tmp );
649 var_buffer_add16( &buffer, '0' );
651 mms_CommandSend( p_access,
652 0x02, /* connexion request */
653 0x00000000, /* flags, FIXME */
654 0xffffffff, /* ???? */
658 /* *** response from server, should be 0x02 or 0x03 *** */
659 mms_CommandRead( p_access, 0x02, 0x03 );
660 if( p_sys->i_command == 0x03 )
663 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
664 var_buffer_free( &buffer );
665 MMSClose( p_access );
668 else if( p_sys->i_command != 0x02 )
670 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
673 /* *** send command 5 : media file name/path requested *** */
674 var_buffer_reinitwrite( &buffer, 0 );
675 var_buffer_add64( &buffer, 0 );
677 /* media file path shouldn't start with / character */
678 mediapath = p_url->psz_path;
679 if ( *mediapath == '/' )
683 var_buffer_addUTF16( &buffer, mediapath );
685 mms_CommandSend( p_access,
687 p_sys->i_command_level,
692 /* *** wait for reponse *** */
693 mms_CommandRead( p_access, 0x1a, 0x06 );
695 /* test if server send 0x1A answer */
696 if( p_sys->i_command == 0x1A )
698 msg_Err( p_access, "id/password requested (not yet supported)" );
700 var_buffer_free( &buffer );
701 MMSClose( p_access );
704 if( p_sys->i_command != 0x06 )
707 "unknown answer (0x%x instead of 0x06)",
709 var_buffer_free( &buffer );
710 MMSClose( p_access );
714 /* 1 for file ok, 2 for authen ok */
715 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
718 msg_Dbg( p_access, "media file name/path accepted" );
721 msg_Dbg( p_access, "authentication accepted" );
725 msg_Err( p_access, "error while asking for file %d",
726 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
727 var_buffer_free( &buffer );
728 MMSClose( p_access );
732 p_sys->i_flags_broadcast =
733 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
734 p_sys->i_media_length =
735 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
736 p_sys->i_packet_length =
737 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
738 p_sys->i_packet_count =
739 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
740 p_sys->i_max_bit_rate =
741 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
742 p_sys->i_header_size =
743 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
746 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
747 "packet_length:%zul packet_count:%"PRId32" max_bit_rate:%d "
749 p_sys->i_flags_broadcast,
750 p_sys->i_media_length,
751 (unsigned)p_sys->i_packet_length,
752 p_sys->i_packet_count,
753 p_sys->i_max_bit_rate,
754 p_sys->i_header_size );
756 /* *** send command 15 *** */
758 var_buffer_reinitwrite( &buffer, 0 );
759 var_buffer_add32( &buffer, 0 );
760 var_buffer_add32( &buffer, 0x8000 );
761 var_buffer_add32( &buffer, 0xffffffff );
762 var_buffer_add32( &buffer, 0x00 );
763 var_buffer_add32( &buffer, 0x00 );
764 var_buffer_add32( &buffer, 0x00 );
765 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
766 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
767 var_buffer_add32( &buffer, 0x00 );
768 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
769 buffer.p_data, buffer.i_data );
771 /* *** wait for reponse *** */
772 /* Commented out because it fails on some stream (no 0x11 answer) */
774 mms_CommandRead( p_access, 0x11, 0 );
776 if( p_sys->i_command != 0x11 )
779 "unknown answer (0x%x instead of 0x11)",
781 var_buffer_free( &buffer );
782 MMSClose( p_access );
787 /* *** now read header packet *** */
788 /* XXX could be split over multiples packets */
789 msg_Dbg( p_access, "reading header" );
792 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
794 msg_Err( p_access, "cannot receive header" );
795 var_buffer_free( &buffer );
796 MMSClose( p_access );
799 if( p_sys->i_header >= p_sys->i_header_size )
802 "header complete(%zu)",
807 "header incomplete (%zu/%zu), reading more",
809 p_sys->i_header_size );
812 /* *** parse header and get stream and their id *** */
813 /* get all streams properties,
815 * TODO : stream bitrates properties(optional)
816 * and bitrate mutual exclusion(optional) */
817 asf_HeaderParse ( &p_sys->asfh,
818 p_sys->p_header, p_sys->i_header );
819 asf_StreamSelect( &p_sys->asfh,
820 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
821 var_CreateGetInteger( p_access, "mms-all" ),
822 var_CreateGetInteger( p_access, "audio" ),
823 var_CreateGetInteger( p_access, "video" ) );
825 /* *** now select stream we want to receive *** */
826 /* TODO take care of stream bitrate TODO */
829 var_buffer_reinitwrite( &buffer, 0 );
830 /* for now, select first audio and video stream */
831 for( i = 1; i < 128; i++ )
834 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
839 var_buffer_add16( &buffer, 0xffff );
840 var_buffer_add16( &buffer, i );
846 if( p_sys->asfh.stream[i].i_selected )
848 var_buffer_add16( &buffer, 0x0000 );
850 "selecting stream[0x%x] %s (%d kb/s)",
852 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
854 p_sys->asfh.stream[i].i_bitrate / 1024);
858 var_buffer_add16( &buffer, 0x0002 );
860 "ignoring stream[0x%x] %s (%d kb/s)",
862 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
864 p_sys->asfh.stream[i].i_bitrate / 1024);
872 msg_Err( p_access, "cannot find any stream" );
873 var_buffer_free( &buffer );
874 MMSClose( p_access );
877 mms_CommandSend( p_access, 0x33,
879 0xffff | ( i_first << 16 ),
880 buffer.p_data, buffer.i_data );
882 mms_CommandRead( p_access, 0x21, 0 );
883 if( p_sys->i_command != 0x21 )
886 "unknown answer (0x%x instead of 0x21)",
888 var_buffer_free( &buffer );
889 MMSClose( p_access );
894 var_buffer_free( &buffer );
896 msg_Info( p_access, "connection successful" );
901 /****************************************************************************
902 * MMSStart : Start streaming
903 ****************************************************************************/
904 static int MMSStart( access_t *p_access, uint32_t i_packet )
906 access_sys_t *p_sys = p_access->p_sys;
909 /* *** start stream from packet 0 *** */
910 var_buffer_initwrite( &buffer, 0 );
911 var_buffer_add64( &buffer, 0 ); /* seek point in second */
912 var_buffer_add32( &buffer, 0xffffffff );
913 var_buffer_add32( &buffer, i_packet ); // begin from start
914 var_buffer_add8( &buffer, 0xff ); // stream time limit
915 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
916 var_buffer_add8( &buffer, 0xff ); //
917 var_buffer_add8( &buffer, 0x00 ); // don't use limit
918 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
920 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
921 buffer.p_data, buffer.i_data );
923 var_buffer_free( &buffer );
925 mms_CommandRead( p_access, 0x05, 0 );
927 if( p_sys->i_command != 0x05 )
930 "unknown answer (0x%x instead of 0x05)",
937 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
939 msg_Dbg( p_access, "streaming started" );
944 /****************************************************************************
945 * MMSStop : Stop streaming
946 ****************************************************************************/
947 static int MMSStop( access_t *p_access )
949 access_sys_t *p_sys = p_access->p_sys;
951 /* *** stop stream but keep connection alive *** */
952 mms_CommandSend( p_access,
954 p_sys->i_command_level,
960 /****************************************************************************
961 * MMSClose : Close streaming and connection
962 ****************************************************************************/
963 static void MMSClose( access_t *p_access )
965 access_sys_t *p_sys = p_access->p_sys;
967 msg_Dbg( p_access, "Connection closed" );
969 /* *** tell server that we will disconnect *** */
970 mms_CommandSend( p_access,
972 p_sys->i_command_level,
976 /* *** close sockets *** */
977 net_Close( p_sys->i_handle_tcp );
978 if( p_sys->i_proto == MMS_PROTO_UDP )
980 net_Close( p_sys->i_handle_udp );
983 FREENULL( p_sys->p_cmd );
984 FREENULL( p_sys->p_media );
985 FREENULL( p_sys->p_header );
987 FREENULL( p_sys->psz_server_version );
988 FREENULL( p_sys->psz_tool_version );
989 FREENULL( p_sys->psz_update_player_url );
990 FREENULL( p_sys->psz_encryption_type );
993 /****************************************************************************
995 * MMS specific functions
997 ****************************************************************************/
998 static int mms_CommandSend( access_t *p_access, int i_command,
999 uint32_t i_prefix1, uint32_t i_prefix2,
1000 uint8_t *p_data, int i_data_old )
1002 var_buffer_t buffer;
1003 access_sys_t *p_sys = p_access->p_sys;
1004 int i_data_by8, i_ret;
1005 int i_data = i_data_old;
1007 while( i_data & 0x7 ) i_data++;
1008 i_data_by8 = i_data >> 3;
1010 /* first init buffer */
1011 var_buffer_initwrite( &buffer, 0 );
1013 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
1014 var_buffer_add32( &buffer, 0xB00BFACE );
1015 /* size after protocol type */
1016 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
1017 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
1018 var_buffer_add32( &buffer, i_data_by8 + 4 );
1019 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
1020 var_buffer_add64( &buffer, 0 );
1021 var_buffer_add32( &buffer, i_data_by8 + 2 );
1022 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
1023 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
1024 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
1026 /* specific command data */
1027 if( p_data && i_data > 0 )
1029 var_buffer_addmemory( &buffer, p_data, i_data_old );
1032 /* Append padding to the command data */
1033 var_buffer_add64( &buffer, 0 );
1036 vlc_mutex_lock( &p_sys->lock_netwrite );
1037 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
1038 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
1039 vlc_mutex_unlock( &p_sys->lock_netwrite );
1040 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
1042 msg_Err( p_access, "failed to send command" );
1043 return VLC_EGENERIC;
1046 var_buffer_free( &buffer );
1050 static int NetFillBuffer( access_t *p_access )
1056 access_sys_t *p_sys = p_access->p_sys;
1058 struct pollfd ufd[2];
1059 unsigned timeout, nfd;
1061 /* FIXME when using udp */
1062 ssize_t i_tcp, i_udp;
1063 ssize_t i_tcp_read, i_udp_read;
1066 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1068 if( p_sys->i_proto == MMS_PROTO_UDP )
1070 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1074 i_udp = 0; /* there isn't udp socket */
1077 if( ( i_udp <= 0 ) && ( i_tcp <= 0 ) )
1079 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1084 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1087 /* Find if some data is available */
1092 /* Initialize file descriptor set */
1093 memset (ufd, 0, sizeof (ufd));
1098 ufd[nfd].fd = p_sys->i_handle_tcp;
1099 ufd[nfd].events = POLLIN;
1104 ufd[nfd].fd = p_sys->i_handle_tcp;
1105 ufd[nfd].events = POLLIN;
1109 /* We'll wait 0.5 second if nothing happens */
1112 if( i_try * timeout > p_sys->i_timeout )
1114 msg_Err(p_access, "no data received");
1118 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1123 if( !vlc_object_alive (p_access) || p_access->b_error ) return -1;
1125 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1127 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1128 (i_ret < 0 && errno == EINTR) );
1132 msg_Err( p_access, "network poll error (%m)" );
1136 i_tcp_read = i_udp_read = 0;
1138 if( ( i_tcp > 0 ) && ufd[0].revents )
1141 recv( p_sys->i_handle_tcp,
1142 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1143 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1146 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1148 i_udp_read = recv( p_sys->i_handle_udp,
1149 p_sys->buffer_udp + p_sys->i_buffer_udp,
1150 i_udp + MMS_BUFFER_SIZE/2, 0 );
1154 if( p_sys->i_proto == MMS_PROTO_UDP )
1156 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1157 p_sys->i_buffer_tcp, i_tcp_read,
1158 p_sys->i_buffer_udp, i_udp_read );
1162 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1163 p_sys->i_buffer_tcp, i_tcp_read );
1167 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1168 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1170 return i_tcp_read + i_udp_read;
1174 static int mms_ParseCommand( access_t *p_access,
1179 #define GET32( i_pos ) \
1180 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1181 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1182 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1184 access_sys_t *p_sys = p_access->p_sys;
1188 free( p_sys->p_cmd );
1189 p_sys->i_cmd = i_data;
1190 p_sys->p_cmd = malloc( i_data );
1191 memcpy( p_sys->p_cmd, p_data, i_data );
1193 *pi_used = i_data; /* by default */
1195 if( i_data < MMS_CMD_HEADERSIZE )
1197 msg_Warn( p_access, "truncated command (header incomplete)" );
1198 p_sys->i_command = 0;
1201 i_id = GetDWLE( p_data + 4 );
1202 i_length = GetDWLE( p_data + 8 ) + 16;
1204 if( i_id != 0xb00bface || i_length < 16 )
1207 "incorrect command header (0x%"PRIx32")", i_id );
1208 p_sys->i_command = 0;
1212 if( i_length > p_sys->i_cmd )
1215 "truncated command (missing %zu bytes)",
1216 (size_t)i_length - i_data );
1217 p_sys->i_command = 0;
1220 else if( i_length < p_sys->i_cmd )
1222 p_sys->i_cmd = i_length;
1223 *pi_used = i_length;
1227 "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",
1231 /* 12: protocol type "MMS " */
1234 /* 24: unknown (0) */
1235 /* 28: unknown (0) */
1241 p_sys->i_command = GET32( 36 ) & 0xffff;
1244 return MMS_PACKET_CMD;
1247 static int mms_ParsePacket( access_t *p_access,
1248 uint8_t *p_data, size_t i_data,
1251 access_sys_t *p_sys = p_access->p_sys;
1252 int i_packet_seq_num;
1253 size_t i_packet_length;
1254 uint32_t i_packet_id;
1259 *pi_used = i_data; /* default */
1262 msg_Warn( p_access, "truncated packet (header incomplete)" );
1266 i_packet_id = p_data[4];
1267 i_packet_seq_num = GetDWLE( p_data );
1268 i_packet_length = GetWLE( p_data + 6 );
1270 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1272 if( i_packet_length > i_data || i_packet_length <= 8)
1274 /* msg_Dbg( p_access,
1275 "truncated packet (Declared %d bytes, Actual %d bytes)",
1276 i_packet_length, i_data ); */
1280 else if( i_packet_length < i_data )
1282 *pi_used = i_packet_length;
1285 if( i_packet_id == 0xff )
1288 "receive MMS UDP pair timing" );
1289 return( MMS_PACKET_UDP_TIMING );
1292 if( i_packet_id != p_sys->i_header_packet_id_type &&
1293 i_packet_id != p_sys->i_media_packet_id_type )
1295 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1299 /* we now have a media or a header packet */
1300 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1301 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1303 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1306 /* FIXME for udp could be just wrong order ? */
1308 "detected packet lost (%d != %d)",
1310 p_sys->i_packet_seq_num );
1313 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1315 if( i_packet_id == p_sys->i_header_packet_id_type )
1317 if( p_sys->p_header )
1319 p_sys->p_header = realloc( p_sys->p_header,
1320 p_sys->i_header + i_packet_length - 8 );
1321 memcpy( &p_sys->p_header[p_sys->i_header],
1323 i_packet_length - 8 );
1324 p_sys->i_header += i_packet_length - 8;
1330 p_sys->p_header = p_packet;
1331 p_sys->i_header = i_packet_length - 8;
1333 /* msg_Dbg( p_access,
1334 "receive header packet (%d bytes)",
1335 i_packet_length - 8 ); */
1337 return MMS_PACKET_HEADER;
1341 FREENULL( p_sys->p_media );
1342 p_sys->p_media = p_packet;
1343 p_sys->i_media = i_packet_length - 8;
1344 p_sys->i_media_used = 0;
1345 /* msg_Dbg( p_access,
1346 "receive media packet (%d bytes)",
1347 i_packet_length - 8 ); */
1349 return MMS_PACKET_MEDIA;
1353 static int mms_ReceivePacket( access_t *p_access )
1355 access_sys_t *p_sys = p_access->p_sys;
1356 int i_packet_tcp_type;
1357 int i_packet_udp_type;
1361 bool b_refill = true;
1363 /* first if we need to refill buffer */
1364 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1366 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1368 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1369 (uint32_t)p_sys->i_buffer_tcp )
1374 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1379 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1380 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1385 if( b_refill && NetFillBuffer( p_access ) < 0 )
1387 msg_Warn( p_access, "cannot fill buffer" );
1391 i_packet_tcp_type = -1;
1392 i_packet_udp_type = -1;
1394 if( p_sys->i_buffer_tcp > 0 )
1398 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1401 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1402 p_sys->i_buffer_tcp, &i_used );
1408 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1409 p_sys->i_buffer_tcp, &i_used );
1411 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1413 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1414 MMS_BUFFER_SIZE - i_used );
1416 p_sys->i_buffer_tcp -= i_used;
1418 else if( p_sys->i_buffer_udp > 0 )
1423 mms_ParsePacket( p_access, p_sys->buffer_udp,
1424 p_sys->i_buffer_udp, &i_used );
1426 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1428 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1429 MMS_BUFFER_SIZE - i_used );
1431 p_sys->i_buffer_udp -= i_used;
1434 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1436 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1437 i_packet_tcp_type = -1;
1440 if( i_packet_tcp_type != -1 )
1442 return i_packet_tcp_type;
1444 else if( i_packet_udp_type != -1 )
1446 return i_packet_udp_type;
1451 static int mms_ReceiveCommand( access_t *p_access )
1453 access_sys_t *p_sys = p_access->p_sys;
1460 if( NetFillBuffer( p_access ) < 0 )
1462 msg_Warn( p_access, "cannot fill buffer" );
1463 return VLC_EGENERIC;
1465 if( p_sys->i_buffer_tcp > 0 )
1467 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1468 p_sys->i_buffer_tcp, &i_used );
1469 if( i_used < MMS_BUFFER_SIZE )
1471 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1472 MMS_BUFFER_SIZE - i_used );
1474 p_sys->i_buffer_tcp -= i_used;
1478 return VLC_EGENERIC;
1481 if( p_sys->i_command == 0x1b )
1483 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1492 return VLC_EGENERIC;
1499 #define MMS_RETRY_MAX 10
1500 #define MMS_RETRY_SLEEP 50000
1502 static int mms_CommandRead( access_t *p_access, int i_command1,
1505 access_sys_t *p_sys = p_access->p_sys;
1509 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1511 i_status = mms_ReceiveCommand( p_access );
1512 if( i_status < 0 || p_sys->i_command == 0 )
1515 msleep( MMS_RETRY_SLEEP );
1517 else if( i_command1 == 0 && i_command2 == 0)
1521 else if( p_sys->i_command == i_command1 ||
1522 p_sys->i_command == i_command2 )
1528 switch( p_sys->i_command )
1531 msg_Warn( p_access, "socket closed by server" );
1532 p_access->info.b_eof = true;
1533 return VLC_EGENERIC;
1535 msg_Warn( p_access, "end of media stream" );
1536 p_access->info.b_eof = true;
1537 return VLC_EGENERIC;
1543 p_access->info.b_eof = true;
1544 msg_Warn( p_access, "failed to receive command (aborting)" );
1546 return VLC_EGENERIC;
1550 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1552 access_sys_t *p_sys = p_access->p_sys;
1555 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1559 if( !vlc_object_alive (p_access) )
1562 i_status = mms_ReceivePacket( p_access );
1566 msg_Warn( p_access, "cannot receive header (%d/%d)",
1567 i_count, MMS_RETRY_MAX );
1568 msleep( MMS_RETRY_SLEEP );
1570 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1574 else if( i_status == MMS_PACKET_CMD )
1576 switch( p_sys->i_command )
1579 msg_Warn( p_access, "socket closed by server" );
1580 p_access->info.b_eof = true;
1583 msg_Warn( p_access, "end of media stream" );
1584 p_access->info.b_eof = true;
1587 /* XXX not too dificult to be done EXCEPT that we
1588 * need to restart demuxer... and I don't see how we
1589 * could do that :p */
1591 "reinitialization needed --> unsupported" );
1592 p_access->info.b_eof = true;
1600 msg_Err( p_access, "cannot receive %s (aborting)",
1601 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1602 p_access->info.b_eof = true;
1606 static void* KeepAliveThread( void *p_data )
1608 mmstu_keepalive_t *p_thread = (mmstu_keepalive_t *) p_data;
1609 access_t *p_access = p_thread->p_access;
1611 vlc_mutex_lock( &p_thread->lock );
1612 mutex_cleanup_push( &p_thread->lock );
1616 /* Do nothing until paused (if ever) */
1617 while( !p_thread->b_paused )
1618 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
1624 /* Send keep-alive every ten seconds */
1625 vlc_mutex_unlock( &p_thread->lock );
1626 canc = vlc_savecancel();
1628 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1630 vlc_restorecancel( canc );
1631 vlc_mutex_lock( &p_thread->lock );
1633 msleep( 10 * CLOCK_FREQ );
1635 while( p_thread->b_paused );