1 /*****************************************************************************
2 * mmstu.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>
41 #include <sys/types.h>
46 #include <vlc_network.h>
56 /****************************************************************************
58 * MMSProtocole documentation found at http://get.to/sdp
59 ****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
64 int MMSTUOpen ( access_t * );
65 void MMSTUClose ( access_t * );
68 static block_t *Block( access_t * );
69 static int Seek( access_t *, uint64_t );
70 static int Control( access_t *, int, va_list );
72 static int MMSOpen ( access_t *, vlc_url_t *, int );
73 static int MMSStart( access_t *, uint32_t );
74 static int MMSStop ( access_t * );
75 static void MMSClose( access_t * );
78 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
79 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
81 static int mms_HeaderMediaRead( access_t *, int );
83 static int mms_ReceivePacket( access_t * );
85 static void KeepAliveStart( access_t * );
86 static void KeepAliveStop( access_t * );
88 int MMSTUOpen( access_t *p_access )
95 access_InitFields( p_access );
96 p_access->pf_read = NULL;
97 p_access->pf_block = Block;
98 p_access->pf_control = Control;
99 p_access->pf_seek = Seek;
101 p_access->p_sys = p_sys = calloc( 1, sizeof( access_sys_t ) );
102 if( !p_sys ) return VLC_ENOMEM;
104 p_sys->i_timeout = var_CreateGetInteger( p_access, "mms-timeout" );
106 vlc_mutex_init( &p_sys->lock_netwrite );
108 /* *** Parse URL and get server addr/port and path *** */
109 vlc_UrlParse( &p_sys->url, p_access->psz_location, 0 );
110 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
112 msg_Err( p_access, "invalid server name" );
113 vlc_UrlClean( &p_sys->url );
114 vlc_mutex_destroy( &p_sys->lock_netwrite );
118 if( p_sys->url.i_port <= 0 )
120 p_sys->url.i_port = 1755;
123 /* *** connect to this server *** */
124 /* look at requested protocol (udp/tcp) */
125 i_proto = MMS_PROTO_AUTO;
126 if( *p_access->psz_access )
128 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
130 i_proto = MMS_PROTO_UDP;
132 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
134 i_proto = MMS_PROTO_TCP;
139 if( i_proto == MMS_PROTO_AUTO )
140 { /* first try with TCP and then UDP*/
141 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
143 if( vlc_object_alive(p_access) )
144 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
149 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
154 msg_Err( p_access, "cannot connect to server" );
155 vlc_UrlClean( &p_sys->url );
156 vlc_mutex_destroy( &p_sys->lock_netwrite );
161 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
165 * broadcast yy=0x02, xx= 0x00
166 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
168 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
170 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
172 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
174 p_sys->b_seekable = false;
178 p_sys->b_seekable = true;
179 p_access->info.i_size =
180 (uint64_t)p_sys->i_header +
181 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
183 p_sys->b_keep_alive = false;
185 /* *** Start stream *** */
186 if( MMSStart( p_access, 0xffffffff ) < 0 )
188 msg_Err( p_access, "cannot start stream" );
189 MMSTUClose ( p_access );
196 /*****************************************************************************
197 * Close: free unused data structures
198 *****************************************************************************/
199 void MMSTUClose( access_t *p_access )
201 access_sys_t *p_sys = p_access->p_sys;
203 KeepAliveStop( p_access );
205 /* close connection with server */
206 MMSClose( p_access );
209 vlc_UrlClean( &p_sys->url );
214 /*****************************************************************************
216 *****************************************************************************/
217 static int Control( access_t *p_access, int i_query, va_list args )
219 access_sys_t *p_sys = p_access->p_sys;
228 case ACCESS_CAN_SEEK:
229 pb_bool = (bool*)va_arg( args, bool* );
230 *pb_bool = p_sys->b_seekable;
233 case ACCESS_CAN_FASTSEEK:
234 pb_bool = (bool*)va_arg( args, bool* );
238 case ACCESS_CAN_PAUSE:
239 pb_bool = (bool*)va_arg( args, bool* );
243 case ACCESS_CAN_CONTROL_PACE:
244 pb_bool = (bool*)va_arg( args, bool* );
246 #if 0 /* Disable for now until we have a clock synchro algo
247 * which works with something else than MPEG over UDP */
254 case ACCESS_GET_PTS_DELAY:
255 pi_64 = (int64_t*)va_arg( args, int64_t * );
256 *pi_64 = INT64_C(1000)
257 * var_InheritInteger( p_access, "network-caching" );
260 case ACCESS_GET_PRIVATE_ID_STATE:
261 i_int = (int)va_arg( args, int );
262 pb_bool = (bool *)va_arg( args, bool * );
264 if( i_int < 0 || i_int > 127 )
266 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? true : false;
270 case ACCESS_SET_PAUSE_STATE:
271 b_bool = (bool)va_arg( args, int );
275 KeepAliveStart( p_access );
279 KeepAliveStop( p_access );
280 Seek( p_access, p_access->info.i_pos );
284 case ACCESS_GET_TITLE_INFO:
285 case ACCESS_SET_TITLE:
286 case ACCESS_SET_SEEKPOINT:
287 case ACCESS_SET_PRIVATE_ID_STATE:
288 case ACCESS_GET_CONTENT_TYPE:
289 case ACCESS_GET_META:
294 msg_Warn( p_access, "unimplemented query in control" );
301 /*****************************************************************************
302 * Seek: try to go at the right place
303 *****************************************************************************/
304 static int Seek( access_t * p_access, uint64_t i_pos )
306 access_sys_t *p_sys = p_access->p_sys;
311 if( i_pos < p_sys->i_header)
313 if( p_access->info.i_pos < p_sys->i_header )
315 /* no need to restart stream, it was already one
316 * or no stream was yet read */
317 p_access->info.i_pos = i_pos;
322 i_packet = 0xffffffff;
328 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
329 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
331 if( p_sys->b_seekable && i_packet >= p_sys->i_packet_count )
334 msg_Dbg( p_access, "seeking to %"PRIu64 " (packet:%u)", i_pos, i_packet );
337 msg_Dbg( p_access, "stream stopped (seek)" );
339 /* *** restart stream *** */
340 var_buffer_initwrite( &buffer, 0 );
341 var_buffer_add64( &buffer, 0 ); /* seek point in second */
342 var_buffer_add32( &buffer, 0xffffffff );
343 var_buffer_add32( &buffer, i_packet ); // begin from start
344 var_buffer_add8( &buffer, 0xff ); // stream time limit
345 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
346 var_buffer_add8( &buffer, 0xff ); //
347 var_buffer_add8( &buffer, 0x00 ); // don't use limit
348 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
350 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
351 buffer.p_data, buffer.i_data );
353 var_buffer_free( &buffer );
356 while( vlc_object_alive (p_access) )
358 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
360 p_access->info.b_eof = true;
364 if( p_sys->i_command == 0x1e )
366 msg_Dbg( p_access, "received 0x1e (seek)" );
371 while( vlc_object_alive (p_access) )
373 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
375 p_access->info.b_eof = true;
378 if( p_sys->i_command == 0x05 )
380 msg_Dbg( p_access, "received 0x05 (seek)" );
386 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
388 p_access->info.b_eof = true;
392 msg_Dbg( p_access, "Streaming restarted" );
394 p_sys->i_media_used += i_offset;
395 p_access->info.i_pos = i_pos;
396 p_access->info.b_eof = false;
401 /*****************************************************************************
403 *****************************************************************************/
404 static block_t *Block( access_t *p_access )
406 access_sys_t *p_sys = p_access->p_sys;
408 if( p_access->info.b_eof )
411 if( p_access->info.i_pos < p_sys->i_header )
413 const size_t i_copy = p_sys->i_header - p_access->info.i_pos;
415 block_t *p_block = block_New( p_access, i_copy );
419 memcpy( p_block->p_buffer, &p_sys->p_header[p_access->info.i_pos], i_copy );
420 p_access->info.i_pos += i_copy;
423 else if( p_sys->p_media && p_sys->i_media_used < __MAX( p_sys->i_media, p_sys->i_packet_length ) )
426 size_t i_padding = 0;
428 if( p_sys->i_media_used < p_sys->i_media )
429 i_copy = p_sys->i_media - p_sys->i_media_used;
430 if( __MAX( p_sys->i_media, p_sys->i_media_used ) < p_sys->i_packet_length )
431 i_padding = p_sys->i_packet_length - __MAX( p_sys->i_media, p_sys->i_media_used );
433 block_t *p_block = block_New( p_access, i_copy + i_padding );
438 memcpy( &p_block->p_buffer[0], &p_sys->p_media[p_sys->i_media_used], i_copy );
440 memset( &p_block->p_buffer[i_copy], 0, i_padding );
442 p_sys->i_media_used += i_copy + i_padding;
443 p_access->info.i_pos += i_copy + i_padding;
447 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
451 /****************************************************************************
452 * MMSOpen : Open a connection with the server over mmst or mmsu
453 ****************************************************************************/
454 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
456 access_sys_t *p_sys = p_access->p_sys;
457 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
462 int i_server_version;
464 int i_update_player_url;
465 int i_encryption_type;
472 /* *** Open a TCP connection with server *** */
473 msg_Dbg( p_access, "waiting for connection..." );
474 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
475 if( p_sys->i_handle_tcp < 0 )
477 msg_Err( p_access, "failed to open a connection (tcp)" );
481 "connection(tcp) with \"%s:%d\" successful",
485 /* *** Bind port if UDP protocol is selected *** */
488 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
491 net_Close( p_sys->i_handle_tcp );
495 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
497 if( p_sys->i_handle_udp < 0 )
499 msg_Err( p_access, "failed to open a connection (udp)" );
500 net_Close( p_sys->i_handle_tcp );
504 "connection(udp) at \"%s:%d\" successful",
505 p_sys->sz_bind_addr, 7000 );
508 /* *** Init context for mms prototcol *** */
509 GenerateGuid ( &p_sys->guid ); /* used to identify client by server */
511 "generated guid: "GUID_FMT,
512 GUID_PRINT( p_sys->guid ) );
513 p_sys->i_command_level = 1; /* updated after 0x1A command */
514 p_sys->i_seq_num = 0;
515 p_sys->i_media_packet_id_type = 0x04;
516 p_sys->i_header_packet_id_type = 0x02;
517 p_sys->i_proto = i_proto;
518 p_sys->i_packet_seq_num = 0;
519 p_sys->p_header = NULL;
521 p_sys->p_media = NULL;
523 p_sys->i_media_used = 0;
525 p_access->info.i_pos = 0;
526 p_sys->i_buffer_tcp = 0;
527 p_sys->i_buffer_udp = 0;
530 p_access->info.b_eof = false;
532 /* *** send command 1 : connection request *** */
533 var_buffer_initwrite( &buffer, 0 );
534 var_buffer_add16( &buffer, 0x001c );
535 var_buffer_add16( &buffer, 0x0003 );
537 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
538 GUID_PRINT( p_sys->guid ),
539 p_url->psz_host ) < 0 )
541 var_buffer_free( &buffer );
542 net_Close( p_sys->i_handle_tcp );
546 var_buffer_addUTF16( p_access, &buffer, tmp );
549 mms_CommandSend( p_access,
550 0x01, /* connexion request */
551 0x00000000, /* flags, FIXME */
552 0x0004000b, /* ???? */
556 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
558 var_buffer_free( &buffer );
559 MMSClose( p_access );
563 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
564 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
565 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
566 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
567 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
568 #define GETUTF16( psz, size ) \
571 psz = xmalloc( size + 1); \
572 for( i = 0; i < size; i++ ) \
579 GETUTF16( p_sys->psz_server_version, i_server_version );
580 GETUTF16( p_sys->psz_tool_version, i_tool_version );
581 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
582 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
585 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
586 p_sys->psz_server_version,
587 p_sys->psz_tool_version,
588 p_sys->psz_update_player_url,
589 p_sys->psz_encryption_type );
591 /* *** should make an 18 command to make data timing *** */
593 /* *** send command 2 : transport protocol selection *** */
594 var_buffer_reinitwrite( &buffer, 0 );
595 var_buffer_add32( &buffer, 0x00000000 );
596 var_buffer_add32( &buffer, 0x000a0000 );
597 var_buffer_add32( &buffer, 0x00000002 );
605 var_buffer_free( &buffer );
606 MMSClose( p_access );
612 if( asprintf( &tmp, "\\\\192.168.0.1\\TCP\\1242" ) < 0 )
614 var_buffer_free( &buffer );
615 MMSClose( p_access );
619 var_buffer_addUTF16( p_access, &buffer, tmp );
620 var_buffer_add16( &buffer, '0' );
623 mms_CommandSend( p_access,
624 0x02, /* connexion request */
625 0x00000000, /* flags, FIXME */
626 0xffffffff, /* ???? */
630 /* *** response from server, should be 0x02 or 0x03 *** */
631 mms_CommandRead( p_access, 0x02, 0x03 );
632 if( p_sys->i_command == 0x03 )
635 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
636 var_buffer_free( &buffer );
637 MMSClose( p_access );
640 else if( p_sys->i_command != 0x02 )
642 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
645 /* *** send command 5 : media file name/path requested *** */
646 var_buffer_reinitwrite( &buffer, 0 );
647 var_buffer_add64( &buffer, 0 );
649 /* media file path shouldn't start with / character */
650 mediapath = p_url->psz_path;
651 if ( mediapath && *mediapath == '/' )
655 var_buffer_addUTF16( p_access, &buffer, mediapath );
657 mms_CommandSend( p_access,
659 p_sys->i_command_level,
664 /* *** wait for reponse *** */
665 mms_CommandRead( p_access, 0x1a, 0x06 );
667 /* test if server send 0x1A answer */
668 if( p_sys->i_command == 0x1A )
670 msg_Err( p_access, "id/password requested (not yet supported)" );
672 var_buffer_free( &buffer );
673 MMSClose( p_access );
676 if( p_sys->i_command != 0x06 )
679 "unknown answer (0x%x instead of 0x06)",
681 var_buffer_free( &buffer );
682 MMSClose( p_access );
686 /* 1 for file ok, 2 for authen ok */
687 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
690 msg_Dbg( p_access, "media file name/path accepted" );
693 msg_Dbg( p_access, "authentication accepted" );
697 msg_Err( p_access, "error while asking for file %d",
698 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
699 var_buffer_free( &buffer );
700 MMSClose( p_access );
704 p_sys->i_flags_broadcast =
705 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
706 p_sys->i_media_length =
707 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
708 p_sys->i_packet_length =
709 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
710 p_sys->i_packet_count =
711 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
712 p_sys->i_max_bit_rate =
713 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
714 p_sys->i_header_size =
715 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
718 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
719 "packet_length:%zu packet_count:%"PRIu32" max_bit_rate:%d "
721 p_sys->i_flags_broadcast,
722 p_sys->i_media_length,
723 p_sys->i_packet_length,
724 p_sys->i_packet_count,
725 p_sys->i_max_bit_rate,
726 p_sys->i_header_size );
728 /* *** send command 15 *** */
730 var_buffer_reinitwrite( &buffer, 0 );
731 var_buffer_add32( &buffer, 0 );
732 var_buffer_add32( &buffer, 0x8000 );
733 var_buffer_add32( &buffer, 0xffffffff );
734 var_buffer_add32( &buffer, 0x00 );
735 var_buffer_add32( &buffer, 0x00 );
736 var_buffer_add32( &buffer, 0x00 );
737 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
738 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
739 var_buffer_add32( &buffer, 0x00 );
740 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
741 buffer.p_data, buffer.i_data );
743 /* *** wait for reponse *** */
744 /* Commented out because it fails on some stream (no 0x11 answer) */
746 mms_CommandRead( p_access, 0x11, 0 );
748 if( p_sys->i_command != 0x11 )
751 "unknown answer (0x%x instead of 0x11)",
753 var_buffer_free( &buffer );
754 MMSClose( p_access );
759 /* *** now read header packet *** */
760 /* XXX could be split over multiples packets */
761 msg_Dbg( p_access, "reading header" );
764 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
766 msg_Err( p_access, "cannot receive header" );
767 var_buffer_free( &buffer );
768 MMSClose( p_access );
771 if( p_sys->i_header >= p_sys->i_header_size )
774 "header complete(%zu)",
779 "header incomplete (%zu/%zu), reading more",
781 p_sys->i_header_size );
784 /* *** parse header and get stream and their id *** */
785 /* get all streams properties,
787 * TODO : stream bitrates properties(optional)
788 * and bitrate mutual exclusion(optional) */
789 asf_HeaderParse ( &p_sys->asfh,
790 p_sys->p_header, p_sys->i_header );
791 asf_StreamSelect( &p_sys->asfh,
792 var_InheritInteger( p_access, "mms-maxbitrate" ),
793 var_InheritBool( p_access, "mms-all" ),
794 var_InheritBool( p_access, "audio" ),
795 var_InheritBool( p_access, "video" ) );
797 /* *** now select stream we want to receive *** */
798 /* TODO take care of stream bitrate TODO */
801 var_buffer_reinitwrite( &buffer, 0 );
802 /* for now, select first audio and video stream */
803 for( i = 1; i < 128; i++ )
806 if( p_sys->asfh.stream[i].i_cat != ASF_CODEC_TYPE_UNKNOWN )
811 var_buffer_add16( &buffer, 0xffff );
812 var_buffer_add16( &buffer, i );
818 if( p_sys->asfh.stream[i].i_selected )
820 var_buffer_add16( &buffer, 0x0000 );
822 "selecting stream[0x%x] %s (%d Kib/s)",
824 ( p_sys->asfh.stream[i].i_cat == ASF_CODEC_TYPE_AUDIO ) ?
826 p_sys->asfh.stream[i].i_bitrate / 1024);
830 var_buffer_add16( &buffer, 0x0002 );
832 "ignoring stream[0x%x] %s (%d Kib/s)",
834 ( p_sys->asfh.stream[i].i_cat == ASF_CODEC_TYPE_AUDIO ) ?
836 p_sys->asfh.stream[i].i_bitrate / 1024);
844 msg_Err( p_access, "cannot find any stream" );
845 var_buffer_free( &buffer );
846 MMSClose( p_access );
849 mms_CommandSend( p_access, 0x33,
851 0xffff | ( i_first << 16 ),
852 buffer.p_data, buffer.i_data );
854 mms_CommandRead( p_access, 0x21, 0 );
855 if( p_sys->i_command != 0x21 )
858 "unknown answer (0x%x instead of 0x21)",
860 var_buffer_free( &buffer );
861 MMSClose( p_access );
866 var_buffer_free( &buffer );
868 msg_Info( p_access, "connection successful" );
873 /****************************************************************************
874 * MMSStart : Start streaming
875 ****************************************************************************/
876 static int MMSStart( access_t *p_access, uint32_t i_packet )
878 access_sys_t *p_sys = p_access->p_sys;
881 /* *** start stream from packet 0 *** */
882 var_buffer_initwrite( &buffer, 0 );
883 var_buffer_add64( &buffer, 0 ); /* seek point in second */
884 var_buffer_add32( &buffer, 0xffffffff );
885 var_buffer_add32( &buffer, i_packet ); // begin from start
886 var_buffer_add8( &buffer, 0xff ); // stream time limit
887 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
888 var_buffer_add8( &buffer, 0xff ); //
889 var_buffer_add8( &buffer, 0x00 ); // don't use limit
890 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
892 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
893 buffer.p_data, buffer.i_data );
895 var_buffer_free( &buffer );
897 mms_CommandRead( p_access, 0x05, 0 );
899 if( p_sys->i_command != 0x05 )
902 "unknown answer (0x%x instead of 0x05)",
909 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
911 msg_Dbg( p_access, "streaming started" );
916 /****************************************************************************
917 * MMSStop : Stop streaming
918 ****************************************************************************/
919 static int MMSStop( access_t *p_access )
921 access_sys_t *p_sys = p_access->p_sys;
923 /* *** stop stream but keep connection alive *** */
924 mms_CommandSend( p_access,
926 p_sys->i_command_level,
932 /****************************************************************************
933 * MMSClose : Close streaming and connection
934 ****************************************************************************/
935 static void MMSClose( access_t *p_access )
937 access_sys_t *p_sys = p_access->p_sys;
939 msg_Dbg( p_access, "Connection closed" );
941 /* *** tell server that we will disconnect *** */
942 mms_CommandSend( p_access,
944 p_sys->i_command_level,
948 /* *** close sockets *** */
949 net_Close( p_sys->i_handle_tcp );
950 if( p_sys->i_proto == MMS_PROTO_UDP )
952 net_Close( p_sys->i_handle_udp );
955 FREENULL( p_sys->p_cmd );
956 FREENULL( p_sys->p_media );
957 FREENULL( p_sys->p_header );
959 FREENULL( p_sys->psz_server_version );
960 FREENULL( p_sys->psz_tool_version );
961 FREENULL( p_sys->psz_update_player_url );
962 FREENULL( p_sys->psz_encryption_type );
965 /****************************************************************************
967 * MMS specific functions
969 ****************************************************************************/
970 static int mms_CommandSend( access_t *p_access, int i_command,
971 uint32_t i_prefix1, uint32_t i_prefix2,
972 uint8_t *p_data, int i_data_old )
975 access_sys_t *p_sys = p_access->p_sys;
976 int i_data_by8, i_ret;
977 int i_data = i_data_old;
979 while( i_data & 0x7 ) i_data++;
980 i_data_by8 = i_data >> 3;
982 /* first init buffer */
983 var_buffer_initwrite( &buffer, 0 );
985 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
986 var_buffer_add32( &buffer, 0xB00BFACE );
987 /* size after protocol type */
988 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
989 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
990 var_buffer_add32( &buffer, i_data_by8 + 4 );
991 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
992 var_buffer_add64( &buffer, 0 );
993 var_buffer_add32( &buffer, i_data_by8 + 2 );
994 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
995 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
996 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
998 /* specific command data */
999 if( p_data && i_data > 0 )
1001 var_buffer_addmemory( &buffer, p_data, i_data_old );
1004 /* Append padding to the command data */
1005 var_buffer_add64( &buffer, 0 );
1008 vlc_mutex_lock( &p_sys->lock_netwrite );
1009 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
1010 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
1011 vlc_mutex_unlock( &p_sys->lock_netwrite );
1013 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
1015 var_buffer_free( &buffer );
1016 msg_Err( p_access, "failed to send command" );
1017 return VLC_EGENERIC;
1020 var_buffer_free( &buffer );
1024 static int NetFillBuffer( access_t *p_access )
1026 access_sys_t *p_sys = p_access->p_sys;
1028 struct pollfd ufd[2];
1029 unsigned timeout, nfd;
1031 /* FIXME when using udp */
1032 ssize_t i_tcp, i_udp;
1033 ssize_t i_tcp_read, i_udp_read;
1036 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1038 if( p_sys->i_proto == MMS_PROTO_UDP )
1040 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1044 i_udp = 0; /* there isn't udp socket */
1047 if( ( i_udp <= 0 ) && ( i_tcp <= 0 ) )
1049 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1054 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1057 /* Find if some data is available */
1062 /* Initialize file descriptor set */
1063 memset (ufd, 0, sizeof (ufd));
1068 ufd[nfd].fd = p_sys->i_handle_tcp;
1069 ufd[nfd].events = POLLIN;
1074 ufd[nfd].fd = p_sys->i_handle_udp;
1075 ufd[nfd].events = POLLIN;
1079 /* We'll wait 0.5 second if nothing happens */
1080 timeout = __MIN( 500, p_sys->i_timeout );
1082 if( i_try * timeout > p_sys->i_timeout )
1084 msg_Err(p_access, "no data received");
1088 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1093 if( !vlc_object_alive (p_access) )
1096 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1098 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1099 (i_ret < 0 && errno == EINTR) );
1103 msg_Err( p_access, "network poll error (%m)" );
1107 i_tcp_read = i_udp_read = 0;
1109 if( ( i_tcp > 0 ) && ufd[0].revents )
1112 recv( p_sys->i_handle_tcp,
1113 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1114 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1117 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1119 i_udp_read = recv( p_sys->i_handle_udp,
1120 p_sys->buffer_udp + p_sys->i_buffer_udp,
1121 i_udp + MMS_BUFFER_SIZE/2, 0 );
1125 if( p_sys->i_proto == MMS_PROTO_UDP )
1127 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1128 p_sys->i_buffer_tcp, i_tcp_read,
1129 p_sys->i_buffer_udp, i_udp_read );
1133 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1134 p_sys->i_buffer_tcp, i_tcp_read );
1138 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1139 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1141 return i_tcp_read + i_udp_read;
1144 static int mms_ParseCommand( access_t *p_access,
1149 #define GET32( i_pos ) \
1150 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1151 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1152 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1154 access_sys_t *p_sys = p_access->p_sys;
1158 free( p_sys->p_cmd );
1159 p_sys->i_cmd = i_data;
1160 p_sys->p_cmd = xmalloc( i_data );
1161 memcpy( p_sys->p_cmd, p_data, i_data );
1163 *pi_used = i_data; /* by default */
1165 if( i_data < MMS_CMD_HEADERSIZE )
1167 msg_Warn( p_access, "truncated command (header incomplete)" );
1168 p_sys->i_command = 0;
1171 i_id = GetDWLE( p_data + 4 );
1172 i_length = GetDWLE( p_data + 8 ) + 16;
1174 if( i_id != 0xb00bface || i_length < 16 )
1177 "incorrect command header (0x%"PRIx32")", i_id );
1178 p_sys->i_command = 0;
1182 if( i_length > p_sys->i_cmd )
1185 "truncated command (missing %zu bytes)",
1186 (size_t)i_length - i_data );
1187 p_sys->i_command = 0;
1190 else if( i_length < p_sys->i_cmd )
1192 p_sys->i_cmd = i_length;
1193 *pi_used = i_length;
1197 "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",
1201 /* 12: protocol type "MMS " */
1204 /* 24: unknown (0) */
1205 /* 28: unknown (0) */
1211 p_sys->i_command = GET32( 36 ) & 0xffff;
1214 return MMS_PACKET_CMD;
1217 static int mms_ParsePacket( access_t *p_access,
1218 uint8_t *p_data, size_t i_data,
1221 access_sys_t *p_sys = p_access->p_sys;
1222 int i_packet_seq_num;
1223 size_t i_packet_length;
1224 uint32_t i_packet_id;
1226 *pi_used = i_data; /* default */
1229 msg_Warn( p_access, "truncated packet (header incomplete)" );
1233 i_packet_id = p_data[4];
1234 i_packet_seq_num = GetDWLE( p_data );
1235 i_packet_length = GetWLE( p_data + 6 );
1237 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1239 if( i_packet_length > i_data || i_packet_length <= 8)
1241 /* msg_Dbg( p_access,
1242 "truncated packet (Declared %d bytes, Actual %d bytes)",
1243 i_packet_length, i_data ); */
1247 else if( i_packet_length < i_data )
1249 *pi_used = i_packet_length;
1252 if( i_packet_id == 0xff )
1255 "receive MMS UDP pair timing" );
1256 return( MMS_PACKET_UDP_TIMING );
1259 if( i_packet_id != p_sys->i_header_packet_id_type &&
1260 i_packet_id != p_sys->i_media_packet_id_type )
1262 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1266 /* we now have a media or a header packet */
1267 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1270 /* FIXME for udp could be just wrong order ? */
1272 "detected packet lost (%d != %d)",
1274 p_sys->i_packet_seq_num );
1277 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1279 if( i_packet_id == p_sys->i_header_packet_id_type )
1281 if( p_sys->p_header )
1283 p_sys->p_header = xrealloc( p_sys->p_header,
1284 p_sys->i_header + i_packet_length - 8 );
1285 memcpy( &p_sys->p_header[p_sys->i_header],
1286 p_data + 8, i_packet_length - 8 );
1287 p_sys->i_header += i_packet_length - 8;
1292 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1293 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1294 p_sys->p_header = p_packet;
1295 p_sys->i_header = i_packet_length - 8;
1297 /* msg_Dbg( p_access,
1298 "receive header packet (%d bytes)",
1299 i_packet_length - 8 ); */
1301 return MMS_PACKET_HEADER;
1305 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1306 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1307 FREENULL( p_sys->p_media );
1308 p_sys->p_media = p_packet;
1309 p_sys->i_media = i_packet_length - 8;
1310 p_sys->i_media_used = 0;
1311 /* msg_Dbg( p_access,
1312 "receive media packet (%d bytes)",
1313 i_packet_length - 8 ); */
1315 return MMS_PACKET_MEDIA;
1319 static int mms_ReceivePacket( access_t *p_access )
1321 access_sys_t *p_sys = p_access->p_sys;
1322 int i_packet_tcp_type;
1323 int i_packet_udp_type;
1327 bool b_refill = true;
1329 /* first if we need to refill buffer */
1330 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1332 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1334 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1335 (uint32_t)p_sys->i_buffer_tcp )
1340 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1345 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1346 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1351 if( b_refill && NetFillBuffer( p_access ) < 0 )
1353 msg_Warn( p_access, "cannot fill buffer" );
1357 i_packet_tcp_type = -1;
1358 i_packet_udp_type = -1;
1360 if( p_sys->i_buffer_tcp > 0 )
1364 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1367 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1368 p_sys->i_buffer_tcp, &i_used );
1374 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1375 p_sys->i_buffer_tcp, &i_used );
1377 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1379 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1380 MMS_BUFFER_SIZE - i_used );
1382 p_sys->i_buffer_tcp -= i_used;
1384 else if( p_sys->i_buffer_udp > 0 )
1389 mms_ParsePacket( p_access, p_sys->buffer_udp,
1390 p_sys->i_buffer_udp, &i_used );
1392 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1394 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1395 MMS_BUFFER_SIZE - i_used );
1397 p_sys->i_buffer_udp -= i_used;
1400 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1402 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1403 i_packet_tcp_type = -1;
1406 if( i_packet_tcp_type != -1 )
1408 return i_packet_tcp_type;
1410 else if( i_packet_udp_type != -1 )
1412 return i_packet_udp_type;
1417 static int mms_ReceiveCommand( access_t *p_access )
1419 access_sys_t *p_sys = p_access->p_sys;
1426 if( NetFillBuffer( p_access ) < 0 )
1428 msg_Warn( p_access, "cannot fill buffer" );
1429 return VLC_EGENERIC;
1431 if( p_sys->i_buffer_tcp > 0 )
1433 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1434 p_sys->i_buffer_tcp, &i_used );
1435 if( i_used < MMS_BUFFER_SIZE )
1437 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1438 MMS_BUFFER_SIZE - i_used );
1440 p_sys->i_buffer_tcp -= i_used;
1444 return VLC_EGENERIC;
1447 if( p_sys->i_command == 0x1b )
1449 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1458 return VLC_EGENERIC;
1465 #define MMS_RETRY_MAX 10
1466 #define MMS_RETRY_SLEEP 50000
1468 static int mms_CommandRead( access_t *p_access, int i_command1,
1471 access_sys_t *p_sys = p_access->p_sys;
1475 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1477 i_status = mms_ReceiveCommand( p_access );
1478 if( i_status < 0 || p_sys->i_command == 0 )
1481 msleep( MMS_RETRY_SLEEP );
1483 else if( i_command1 == 0 && i_command2 == 0)
1487 else if( p_sys->i_command == i_command1 ||
1488 p_sys->i_command == i_command2 )
1494 switch( p_sys->i_command )
1497 msg_Warn( p_access, "socket closed by server" );
1498 p_access->info.b_eof = true;
1499 return VLC_EGENERIC;
1501 msg_Warn( p_access, "end of media stream" );
1502 p_access->info.b_eof = true;
1503 return VLC_EGENERIC;
1509 p_access->info.b_eof = true;
1510 msg_Warn( p_access, "failed to receive command (aborting)" );
1512 return VLC_EGENERIC;
1516 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1518 access_sys_t *p_sys = p_access->p_sys;
1521 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1525 if( !vlc_object_alive (p_access) )
1528 i_status = mms_ReceivePacket( p_access );
1532 msg_Warn( p_access, "cannot receive header (%d/%d)",
1533 i_count, MMS_RETRY_MAX );
1534 msleep( MMS_RETRY_SLEEP );
1536 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1540 else if( i_status == MMS_PACKET_CMD )
1542 switch( p_sys->i_command )
1545 msg_Warn( p_access, "socket closed by server" );
1546 p_access->info.b_eof = true;
1549 msg_Warn( p_access, "end of media stream" );
1550 p_access->info.b_eof = true;
1553 /* XXX not too dificult to be done EXCEPT that we
1554 * need to restart demuxer... and I don't see how we
1555 * could do that :p */
1557 "reinitialization needed --> unsupported" );
1558 p_access->info.b_eof = true;
1566 msg_Err( p_access, "cannot receive %s (aborting)",
1567 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1568 p_access->info.b_eof = true;
1573 static void *KeepAliveThread( void *p_data )
1575 access_t *p_access = p_data;
1579 /* Send keep-alive every ten seconds */
1580 int canc = vlc_savecancel();
1582 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1584 vlc_restorecancel( canc );
1586 msleep( 10 * CLOCK_FREQ );
1591 static void KeepAliveStart( access_t *p_access )
1593 access_sys_t *p_sys = p_access->p_sys;
1594 if( p_sys->b_keep_alive )
1597 p_sys->b_keep_alive = !vlc_clone( &p_sys->keep_alive,
1598 KeepAliveThread, p_access,
1599 VLC_THREAD_PRIORITY_LOW );
1602 static void KeepAliveStop( access_t *p_access )
1604 access_sys_t *p_sys = p_access->p_sys;
1605 if( !p_sys->b_keep_alive )
1608 vlc_cancel( p_sys->keep_alive );
1609 vlc_join( p_sys->keep_alive, NULL );
1610 p_sys->b_keep_alive = false;