1 /*****************************************************************************
2 * mmstu.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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;
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;
227 case ACCESS_CAN_SEEK:
228 pb_bool = (bool*)va_arg( args, bool* );
229 *pb_bool = p_sys->b_seekable;
232 case ACCESS_CAN_FASTSEEK:
233 pb_bool = (bool*)va_arg( args, bool* );
237 case ACCESS_CAN_PAUSE:
238 pb_bool = (bool*)va_arg( args, bool* );
242 case ACCESS_CAN_CONTROL_PACE:
243 pb_bool = (bool*)va_arg( args, bool* );
245 #if 0 /* Disable for now until we have a clock synchro algo
246 * which works with something else than MPEG over UDP */
252 case ACCESS_GET_SIZE:
253 *va_arg( args, uint64_t * ) = p_sys->i_size;
256 case ACCESS_GET_PTS_DELAY:
257 pi_64 = (int64_t*)va_arg( args, int64_t * );
258 *pi_64 = INT64_C(1000)
259 * var_InheritInteger( p_access, "network-caching" );
262 case ACCESS_GET_PRIVATE_ID_STATE:
263 i_int = (int)va_arg( args, int );
264 pb_bool = (bool *)va_arg( args, bool * );
266 if( i_int < 0 || i_int > 127 )
268 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? true : false;
271 case ACCESS_SET_PAUSE_STATE:
272 b_bool = (bool)va_arg( args, int );
276 KeepAliveStart( p_access );
280 KeepAliveStop( p_access );
281 Seek( p_access, p_access->info.i_pos );
291 /*****************************************************************************
292 * Seek: try to go at the right place
293 *****************************************************************************/
294 static int Seek( access_t * p_access, uint64_t i_pos )
296 access_sys_t *p_sys = p_access->p_sys;
301 if( i_pos < p_sys->i_header)
303 if( p_access->info.i_pos < p_sys->i_header )
305 /* no need to restart stream, it was already one
306 * or no stream was yet read */
307 p_access->info.i_pos = i_pos;
312 i_packet = 0xffffffff;
318 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
319 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
321 if( p_sys->b_seekable && i_packet >= p_sys->i_packet_count )
324 msg_Dbg( p_access, "seeking to %"PRIu64 " (packet:%u)", i_pos, i_packet );
327 msg_Dbg( p_access, "stream stopped (seek)" );
329 /* *** restart stream *** */
330 var_buffer_initwrite( &buffer, 0 );
331 var_buffer_add64( &buffer, 0 ); /* seek point in second */
332 var_buffer_add32( &buffer, 0xffffffff );
333 var_buffer_add32( &buffer, i_packet ); // begin from start
334 var_buffer_add8( &buffer, 0xff ); // stream time limit
335 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
336 var_buffer_add8( &buffer, 0xff ); //
337 var_buffer_add8( &buffer, 0x00 ); // don't use limit
338 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
340 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
341 buffer.p_data, buffer.i_data );
343 var_buffer_free( &buffer );
346 while( vlc_object_alive (p_access) )
348 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
350 p_access->info.b_eof = true;
354 if( p_sys->i_command == 0x1e )
356 msg_Dbg( p_access, "received 0x1e (seek)" );
361 while( vlc_object_alive (p_access) )
363 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
365 p_access->info.b_eof = true;
368 if( p_sys->i_command == 0x05 )
370 msg_Dbg( p_access, "received 0x05 (seek)" );
376 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
378 p_access->info.b_eof = true;
382 msg_Dbg( p_access, "Streaming restarted" );
384 p_sys->i_media_used += i_offset;
385 p_access->info.i_pos = i_pos;
386 p_access->info.b_eof = false;
391 /*****************************************************************************
393 *****************************************************************************/
394 static block_t *Block( access_t *p_access )
396 access_sys_t *p_sys = p_access->p_sys;
398 if( p_access->info.b_eof )
401 if( p_access->info.i_pos < p_sys->i_header )
403 const size_t i_copy = p_sys->i_header - p_access->info.i_pos;
405 block_t *p_block = block_Alloc( i_copy );
409 memcpy( p_block->p_buffer, &p_sys->p_header[p_access->info.i_pos], i_copy );
410 p_access->info.i_pos += i_copy;
413 else if( p_sys->p_media && p_sys->i_media_used < __MAX( p_sys->i_media, p_sys->i_packet_length ) )
416 size_t i_padding = 0;
418 if( p_sys->i_media_used < p_sys->i_media )
419 i_copy = p_sys->i_media - p_sys->i_media_used;
420 if( __MAX( p_sys->i_media, p_sys->i_media_used ) < p_sys->i_packet_length )
421 i_padding = p_sys->i_packet_length - __MAX( p_sys->i_media, p_sys->i_media_used );
423 block_t *p_block = block_Alloc( i_copy + i_padding );
428 memcpy( &p_block->p_buffer[0], &p_sys->p_media[p_sys->i_media_used], i_copy );
430 memset( &p_block->p_buffer[i_copy], 0, i_padding );
432 p_sys->i_media_used += i_copy + i_padding;
433 p_access->info.i_pos += i_copy + i_padding;
437 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
441 /****************************************************************************
442 * MMSOpen : Open a connection with the server over mmst or mmsu
443 ****************************************************************************/
444 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
446 access_sys_t *p_sys = p_access->p_sys;
447 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
452 int i_server_version;
454 int i_update_player_url;
455 int i_encryption_type;
462 /* *** Open a TCP connection with server *** */
463 msg_Dbg( p_access, "waiting for connection..." );
464 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
465 if( p_sys->i_handle_tcp < 0 )
467 msg_Err( p_access, "failed to open a connection (tcp)" );
471 "connection(tcp) with \"%s:%d\" successful",
475 /* *** Bind port if UDP protocol is selected *** */
478 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
481 net_Close( p_sys->i_handle_tcp );
485 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
487 if( p_sys->i_handle_udp < 0 )
489 msg_Err( p_access, "failed to open a connection (udp)" );
490 net_Close( p_sys->i_handle_tcp );
494 "connection(udp) at \"%s:%d\" successful",
495 p_sys->sz_bind_addr, 7000 );
498 /* *** Init context for mms prototcol *** */
499 GenerateGuid ( &p_sys->guid ); /* used to identify client by server */
501 "generated guid: "GUID_FMT,
502 GUID_PRINT( p_sys->guid ) );
503 p_sys->i_command_level = 1; /* updated after 0x1A command */
504 p_sys->i_seq_num = 0;
505 p_sys->i_media_packet_id_type = 0x04;
506 p_sys->i_header_packet_id_type = 0x02;
507 p_sys->i_proto = i_proto;
508 p_sys->i_packet_seq_num = 0;
509 p_sys->p_header = NULL;
511 p_sys->p_media = NULL;
513 p_sys->i_media_used = 0;
515 p_access->info.i_pos = 0;
516 p_sys->i_buffer_tcp = 0;
517 p_sys->i_buffer_udp = 0;
520 p_access->info.b_eof = false;
522 /* *** send command 1 : connection request *** */
523 var_buffer_initwrite( &buffer, 0 );
524 var_buffer_add16( &buffer, 0x001c );
525 var_buffer_add16( &buffer, 0x0003 );
527 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
528 GUID_PRINT( p_sys->guid ),
529 p_url->psz_host ) < 0 )
531 var_buffer_free( &buffer );
532 net_Close( p_sys->i_handle_tcp );
536 var_buffer_addUTF16( p_access, &buffer, tmp );
539 mms_CommandSend( p_access,
540 0x01, /* connexion request */
541 0x00000000, /* flags, FIXME */
542 0x0004000b, /* ???? */
546 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
548 var_buffer_free( &buffer );
549 MMSClose( p_access );
553 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
554 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
555 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
556 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
557 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
558 #define GETUTF16( psz, size ) \
561 psz = xmalloc( size + 1); \
562 for( i = 0; i < size; i++ ) \
569 GETUTF16( p_sys->psz_server_version, i_server_version );
570 GETUTF16( p_sys->psz_tool_version, i_tool_version );
571 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
572 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
575 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
576 p_sys->psz_server_version,
577 p_sys->psz_tool_version,
578 p_sys->psz_update_player_url,
579 p_sys->psz_encryption_type );
581 /* *** should make an 18 command to make data timing *** */
583 /* *** send command 2 : transport protocol selection *** */
584 var_buffer_reinitwrite( &buffer, 0 );
585 var_buffer_add32( &buffer, 0x00000000 );
586 var_buffer_add32( &buffer, 0x000a0000 );
587 var_buffer_add32( &buffer, 0x00000002 );
595 var_buffer_free( &buffer );
596 MMSClose( p_access );
602 if( asprintf( &tmp, "\\\\192.168.0.1\\TCP\\1242" ) < 0 )
604 var_buffer_free( &buffer );
605 MMSClose( p_access );
609 var_buffer_addUTF16( p_access, &buffer, tmp );
610 var_buffer_add16( &buffer, '0' );
613 mms_CommandSend( p_access,
614 0x02, /* connexion request */
615 0x00000000, /* flags, FIXME */
616 0xffffffff, /* ???? */
620 /* *** response from server, should be 0x02 or 0x03 *** */
621 mms_CommandRead( p_access, 0x02, 0x03 );
622 if( p_sys->i_command == 0x03 )
625 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
626 var_buffer_free( &buffer );
627 MMSClose( p_access );
630 else if( p_sys->i_command != 0x02 )
632 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
635 /* *** send command 5 : media file name/path requested *** */
636 var_buffer_reinitwrite( &buffer, 0 );
637 var_buffer_add64( &buffer, 0 );
639 /* media file path shouldn't start with / character */
640 mediapath = p_url->psz_path;
641 if ( mediapath && *mediapath == '/' )
645 var_buffer_addUTF16( p_access, &buffer, mediapath );
647 mms_CommandSend( p_access,
649 p_sys->i_command_level,
654 /* *** wait for reponse *** */
655 mms_CommandRead( p_access, 0x1a, 0x06 );
657 /* test if server send 0x1A answer */
658 if( p_sys->i_command == 0x1A )
660 msg_Err( p_access, "id/password requested (not yet supported)" );
662 var_buffer_free( &buffer );
663 MMSClose( p_access );
666 if( p_sys->i_command != 0x06 )
669 "unknown answer (0x%x instead of 0x06)",
671 var_buffer_free( &buffer );
672 MMSClose( p_access );
676 /* 1 for file ok, 2 for authen ok */
677 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
680 msg_Dbg( p_access, "media file name/path accepted" );
683 msg_Dbg( p_access, "authentication accepted" );
687 msg_Err( p_access, "error while asking for file %d",
688 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
689 var_buffer_free( &buffer );
690 MMSClose( p_access );
694 p_sys->i_flags_broadcast =
695 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
696 p_sys->i_media_length =
697 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
698 p_sys->i_packet_length =
699 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
700 p_sys->i_packet_count =
701 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
702 p_sys->i_max_bit_rate =
703 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
704 p_sys->i_header_size =
705 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
708 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
709 "packet_length:%zu packet_count:%"PRIu32" max_bit_rate:%d "
711 p_sys->i_flags_broadcast,
712 p_sys->i_media_length,
713 p_sys->i_packet_length,
714 p_sys->i_packet_count,
715 p_sys->i_max_bit_rate,
716 p_sys->i_header_size );
718 /* *** send command 15 *** */
720 var_buffer_reinitwrite( &buffer, 0 );
721 var_buffer_add32( &buffer, 0 );
722 var_buffer_add32( &buffer, 0x8000 );
723 var_buffer_add32( &buffer, 0xffffffff );
724 var_buffer_add32( &buffer, 0x00 );
725 var_buffer_add32( &buffer, 0x00 );
726 var_buffer_add32( &buffer, 0x00 );
727 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
728 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
729 var_buffer_add32( &buffer, 0x00 );
730 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
731 buffer.p_data, buffer.i_data );
733 /* *** wait for reponse *** */
734 /* Commented out because it fails on some stream (no 0x11 answer) */
736 mms_CommandRead( p_access, 0x11, 0 );
738 if( p_sys->i_command != 0x11 )
741 "unknown answer (0x%x instead of 0x11)",
743 var_buffer_free( &buffer );
744 MMSClose( p_access );
749 /* *** now read header packet *** */
750 /* XXX could be split over multiples packets */
751 msg_Dbg( p_access, "reading header" );
754 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
756 msg_Err( p_access, "cannot receive header" );
757 var_buffer_free( &buffer );
758 MMSClose( p_access );
761 if( p_sys->i_header >= p_sys->i_header_size )
764 "header complete(%zu)",
769 "header incomplete (%zu/%zu), reading more",
771 p_sys->i_header_size );
774 /* *** parse header and get stream and their id *** */
775 /* get all streams properties,
777 * TODO : stream bitrates properties(optional)
778 * and bitrate mutual exclusion(optional) */
779 asf_HeaderParse ( &p_sys->asfh,
780 p_sys->p_header, p_sys->i_header );
781 asf_StreamSelect( &p_sys->asfh,
782 var_InheritInteger( p_access, "mms-maxbitrate" ),
783 var_InheritBool( p_access, "mms-all" ),
784 var_InheritBool( p_access, "audio" ),
785 var_InheritBool( p_access, "video" ) );
787 /* *** now select stream we want to receive *** */
788 /* TODO take care of stream bitrate TODO */
791 var_buffer_reinitwrite( &buffer, 0 );
792 /* for now, select first audio and video stream */
793 for( i = 1; i < 128; i++ )
796 if( p_sys->asfh.stream[i].i_cat != ASF_CODEC_TYPE_UNKNOWN )
801 var_buffer_add16( &buffer, 0xffff );
802 var_buffer_add16( &buffer, i );
808 if( p_sys->asfh.stream[i].i_selected )
810 var_buffer_add16( &buffer, 0x0000 );
812 "selecting stream[0x%x] %s (%d Kib/s)",
814 ( p_sys->asfh.stream[i].i_cat == ASF_CODEC_TYPE_AUDIO ) ?
816 p_sys->asfh.stream[i].i_bitrate / 1024);
820 var_buffer_add16( &buffer, 0x0002 );
822 "ignoring 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);
834 msg_Err( p_access, "cannot find any stream" );
835 var_buffer_free( &buffer );
836 MMSClose( p_access );
839 mms_CommandSend( p_access, 0x33,
841 0xffff | ( i_first << 16 ),
842 buffer.p_data, buffer.i_data );
844 mms_CommandRead( p_access, 0x21, 0 );
845 if( p_sys->i_command != 0x21 )
848 "unknown answer (0x%x instead of 0x21)",
850 var_buffer_free( &buffer );
851 MMSClose( p_access );
856 var_buffer_free( &buffer );
858 msg_Info( p_access, "connection successful" );
863 /****************************************************************************
864 * MMSStart : Start streaming
865 ****************************************************************************/
866 static int MMSStart( access_t *p_access, uint32_t i_packet )
868 access_sys_t *p_sys = p_access->p_sys;
871 /* *** start stream from packet 0 *** */
872 var_buffer_initwrite( &buffer, 0 );
873 var_buffer_add64( &buffer, 0 ); /* seek point in second */
874 var_buffer_add32( &buffer, 0xffffffff );
875 var_buffer_add32( &buffer, i_packet ); // begin from start
876 var_buffer_add8( &buffer, 0xff ); // stream time limit
877 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
878 var_buffer_add8( &buffer, 0xff ); //
879 var_buffer_add8( &buffer, 0x00 ); // don't use limit
880 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
882 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
883 buffer.p_data, buffer.i_data );
885 var_buffer_free( &buffer );
887 mms_CommandRead( p_access, 0x05, 0 );
889 if( p_sys->i_command != 0x05 )
892 "unknown answer (0x%x instead of 0x05)",
899 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
901 msg_Dbg( p_access, "streaming started" );
906 /****************************************************************************
907 * MMSStop : Stop streaming
908 ****************************************************************************/
909 static int MMSStop( access_t *p_access )
911 access_sys_t *p_sys = p_access->p_sys;
913 /* *** stop stream but keep connection alive *** */
914 mms_CommandSend( p_access,
916 p_sys->i_command_level,
922 /****************************************************************************
923 * MMSClose : Close streaming and connection
924 ****************************************************************************/
925 static void MMSClose( access_t *p_access )
927 access_sys_t *p_sys = p_access->p_sys;
929 msg_Dbg( p_access, "Connection closed" );
931 /* *** tell server that we will disconnect *** */
932 mms_CommandSend( p_access,
934 p_sys->i_command_level,
938 /* *** close sockets *** */
939 net_Close( p_sys->i_handle_tcp );
940 if( p_sys->i_proto == MMS_PROTO_UDP )
942 net_Close( p_sys->i_handle_udp );
945 FREENULL( p_sys->p_cmd );
946 FREENULL( p_sys->p_media );
947 FREENULL( p_sys->p_header );
949 FREENULL( p_sys->psz_server_version );
950 FREENULL( p_sys->psz_tool_version );
951 FREENULL( p_sys->psz_update_player_url );
952 FREENULL( p_sys->psz_encryption_type );
955 /****************************************************************************
957 * MMS specific functions
959 ****************************************************************************/
960 static int mms_CommandSend( access_t *p_access, int i_command,
961 uint32_t i_prefix1, uint32_t i_prefix2,
962 uint8_t *p_data, int i_data_old )
965 access_sys_t *p_sys = p_access->p_sys;
966 int i_data_by8, i_ret;
967 int i_data = i_data_old;
969 while( i_data & 0x7 ) i_data++;
970 i_data_by8 = i_data >> 3;
972 /* first init buffer */
973 var_buffer_initwrite( &buffer, 0 );
975 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
976 var_buffer_add32( &buffer, 0xB00BFACE );
977 /* size after protocol type */
978 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
979 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
980 var_buffer_add32( &buffer, i_data_by8 + 4 );
981 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
982 var_buffer_add64( &buffer, 0 );
983 var_buffer_add32( &buffer, i_data_by8 + 2 );
984 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
985 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
986 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
988 /* specific command data */
989 if( p_data && i_data > 0 )
991 var_buffer_addmemory( &buffer, p_data, i_data_old );
994 /* Append padding to the command data */
995 var_buffer_add64( &buffer, 0 );
998 vlc_mutex_lock( &p_sys->lock_netwrite );
999 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
1000 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
1001 vlc_mutex_unlock( &p_sys->lock_netwrite );
1003 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
1005 var_buffer_free( &buffer );
1006 msg_Err( p_access, "failed to send command" );
1007 return VLC_EGENERIC;
1010 var_buffer_free( &buffer );
1014 static int NetFillBuffer( access_t *p_access )
1016 access_sys_t *p_sys = p_access->p_sys;
1018 struct pollfd ufd[2];
1019 unsigned timeout, nfd;
1021 /* FIXME when using udp */
1022 ssize_t i_tcp, i_udp;
1023 ssize_t i_tcp_read, i_udp_read;
1026 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1028 if( p_sys->i_proto == MMS_PROTO_UDP )
1030 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1034 i_udp = 0; /* there isn't udp socket */
1037 if( ( i_udp <= 0 ) && ( i_tcp <= 0 ) )
1039 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1044 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1047 /* Find if some data is available */
1052 /* Initialize file descriptor set */
1053 memset (ufd, 0, sizeof (ufd));
1058 ufd[nfd].fd = p_sys->i_handle_tcp;
1059 ufd[nfd].events = POLLIN;
1064 ufd[nfd].fd = p_sys->i_handle_udp;
1065 ufd[nfd].events = POLLIN;
1069 /* We'll wait 0.5 second if nothing happens */
1070 timeout = __MIN( 500, p_sys->i_timeout );
1072 if( i_try * timeout > p_sys->i_timeout )
1074 msg_Err(p_access, "no data received");
1078 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1083 if( !vlc_object_alive (p_access) )
1086 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1088 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1089 (i_ret < 0 && errno == EINTR) );
1093 msg_Err( p_access, "network poll error: %s", vlc_strerror_c(errno) );
1097 i_tcp_read = i_udp_read = 0;
1099 if( ( i_tcp > 0 ) && ufd[0].revents )
1102 recv( p_sys->i_handle_tcp,
1103 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1104 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1107 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1109 i_udp_read = recv( p_sys->i_handle_udp,
1110 p_sys->buffer_udp + p_sys->i_buffer_udp,
1111 i_udp + MMS_BUFFER_SIZE/2, 0 );
1115 if( p_sys->i_proto == MMS_PROTO_UDP )
1117 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1118 p_sys->i_buffer_tcp, i_tcp_read,
1119 p_sys->i_buffer_udp, i_udp_read );
1123 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1124 p_sys->i_buffer_tcp, i_tcp_read );
1128 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1129 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1131 return i_tcp_read + i_udp_read;
1134 static int mms_ParseCommand( access_t *p_access,
1139 #define GET32( i_pos ) \
1140 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1141 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1142 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1144 access_sys_t *p_sys = p_access->p_sys;
1148 free( p_sys->p_cmd );
1149 p_sys->i_cmd = i_data;
1150 p_sys->p_cmd = xmalloc( i_data );
1151 memcpy( p_sys->p_cmd, p_data, i_data );
1153 *pi_used = i_data; /* by default */
1155 if( i_data < MMS_CMD_HEADERSIZE )
1157 msg_Warn( p_access, "truncated command (header incomplete)" );
1158 p_sys->i_command = 0;
1161 i_id = GetDWLE( p_data + 4 );
1162 i_length = GetDWLE( p_data + 8 ) + 16;
1164 if( i_id != 0xb00bface || i_length < 16 )
1167 "incorrect command header (0x%"PRIx32")", i_id );
1168 p_sys->i_command = 0;
1172 if( i_length > p_sys->i_cmd )
1175 "truncated command (missing %zu bytes)",
1176 (size_t)i_length - i_data );
1177 p_sys->i_command = 0;
1180 else if( i_length < p_sys->i_cmd )
1182 p_sys->i_cmd = i_length;
1183 *pi_used = i_length;
1187 "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",
1191 /* 12: protocol type "MMS " */
1194 /* 24: unknown (0) */
1195 /* 28: unknown (0) */
1201 p_sys->i_command = GET32( 36 ) & 0xffff;
1204 return MMS_PACKET_CMD;
1207 static int mms_ParsePacket( access_t *p_access,
1208 uint8_t *p_data, size_t i_data,
1211 access_sys_t *p_sys = p_access->p_sys;
1212 int i_packet_seq_num;
1213 size_t i_packet_length;
1214 uint32_t i_packet_id;
1216 *pi_used = i_data; /* default */
1219 msg_Warn( p_access, "truncated packet (header incomplete)" );
1223 i_packet_id = p_data[4];
1224 i_packet_seq_num = GetDWLE( p_data );
1225 i_packet_length = GetWLE( p_data + 6 );
1227 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1229 if( i_packet_length > i_data || i_packet_length <= 8)
1231 /* msg_Dbg( p_access,
1232 "truncated packet (Declared %d bytes, Actual %d bytes)",
1233 i_packet_length, i_data ); */
1237 else if( i_packet_length < i_data )
1239 *pi_used = i_packet_length;
1242 if( i_packet_id == 0xff )
1245 "receive MMS UDP pair timing" );
1246 return( MMS_PACKET_UDP_TIMING );
1249 if( i_packet_id != p_sys->i_header_packet_id_type &&
1250 i_packet_id != p_sys->i_media_packet_id_type )
1252 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1256 /* we now have a media or a header packet */
1257 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1260 /* FIXME for udp could be just wrong order ? */
1262 "detected packet lost (%d != %d)",
1264 p_sys->i_packet_seq_num );
1267 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1269 if( i_packet_id == p_sys->i_header_packet_id_type )
1271 if( p_sys->p_header )
1273 p_sys->p_header = xrealloc( p_sys->p_header,
1274 p_sys->i_header + i_packet_length - 8 );
1275 memcpy( &p_sys->p_header[p_sys->i_header],
1276 p_data + 8, i_packet_length - 8 );
1277 p_sys->i_header += i_packet_length - 8;
1282 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1283 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1284 p_sys->p_header = p_packet;
1285 p_sys->i_header = i_packet_length - 8;
1287 /* msg_Dbg( p_access,
1288 "receive header packet (%d bytes)",
1289 i_packet_length - 8 ); */
1291 return MMS_PACKET_HEADER;
1295 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1296 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1297 FREENULL( p_sys->p_media );
1298 p_sys->p_media = p_packet;
1299 p_sys->i_media = i_packet_length - 8;
1300 p_sys->i_media_used = 0;
1301 /* msg_Dbg( p_access,
1302 "receive media packet (%d bytes)",
1303 i_packet_length - 8 ); */
1305 return MMS_PACKET_MEDIA;
1309 static int mms_ReceivePacket( access_t *p_access )
1311 access_sys_t *p_sys = p_access->p_sys;
1312 int i_packet_tcp_type;
1313 int i_packet_udp_type;
1317 bool b_refill = true;
1319 /* first if we need to refill buffer */
1320 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1322 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1324 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1325 (uint32_t)p_sys->i_buffer_tcp )
1330 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1335 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1336 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1341 if( b_refill && NetFillBuffer( p_access ) < 0 )
1343 msg_Warn( p_access, "cannot fill buffer" );
1347 i_packet_tcp_type = -1;
1348 i_packet_udp_type = -1;
1350 if( p_sys->i_buffer_tcp > 0 )
1354 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1357 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1358 p_sys->i_buffer_tcp, &i_used );
1364 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1365 p_sys->i_buffer_tcp, &i_used );
1367 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1369 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1370 MMS_BUFFER_SIZE - i_used );
1372 p_sys->i_buffer_tcp -= i_used;
1374 else if( p_sys->i_buffer_udp > 0 )
1379 mms_ParsePacket( p_access, p_sys->buffer_udp,
1380 p_sys->i_buffer_udp, &i_used );
1382 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1384 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1385 MMS_BUFFER_SIZE - i_used );
1387 p_sys->i_buffer_udp -= i_used;
1390 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1392 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1393 i_packet_tcp_type = -1;
1396 if( i_packet_tcp_type != -1 )
1398 return i_packet_tcp_type;
1400 else if( i_packet_udp_type != -1 )
1402 return i_packet_udp_type;
1407 static int mms_ReceiveCommand( access_t *p_access )
1409 access_sys_t *p_sys = p_access->p_sys;
1416 if( NetFillBuffer( p_access ) < 0 )
1418 msg_Warn( p_access, "cannot fill buffer" );
1419 return VLC_EGENERIC;
1421 if( p_sys->i_buffer_tcp > 0 )
1423 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1424 p_sys->i_buffer_tcp, &i_used );
1425 if( i_used < MMS_BUFFER_SIZE )
1427 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1428 MMS_BUFFER_SIZE - i_used );
1430 p_sys->i_buffer_tcp -= i_used;
1434 return VLC_EGENERIC;
1437 if( p_sys->i_command == 0x1b )
1439 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1448 return VLC_EGENERIC;
1455 #define MMS_RETRY_MAX 10
1456 #define MMS_RETRY_SLEEP 50000
1458 static int mms_CommandRead( access_t *p_access, int i_command1,
1461 access_sys_t *p_sys = p_access->p_sys;
1465 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1467 i_status = mms_ReceiveCommand( p_access );
1468 if( i_status < 0 || p_sys->i_command == 0 )
1471 msleep( MMS_RETRY_SLEEP );
1473 else if( i_command1 == 0 && i_command2 == 0)
1477 else if( p_sys->i_command == i_command1 ||
1478 p_sys->i_command == i_command2 )
1484 switch( p_sys->i_command )
1487 msg_Warn( p_access, "socket closed by server" );
1488 p_access->info.b_eof = true;
1489 return VLC_EGENERIC;
1491 msg_Warn( p_access, "end of media stream" );
1492 p_access->info.b_eof = true;
1493 return VLC_EGENERIC;
1499 p_access->info.b_eof = true;
1500 msg_Warn( p_access, "failed to receive command (aborting)" );
1502 return VLC_EGENERIC;
1506 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1508 access_sys_t *p_sys = p_access->p_sys;
1511 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1515 if( !vlc_object_alive (p_access) )
1518 i_status = mms_ReceivePacket( p_access );
1522 msg_Warn( p_access, "cannot receive header (%d/%d)",
1523 i_count, MMS_RETRY_MAX );
1524 msleep( MMS_RETRY_SLEEP );
1526 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1530 else if( i_status == MMS_PACKET_CMD )
1532 switch( p_sys->i_command )
1535 msg_Warn( p_access, "socket closed by server" );
1536 p_access->info.b_eof = true;
1539 msg_Warn( p_access, "end of media stream" );
1540 p_access->info.b_eof = true;
1543 /* XXX not too dificult to be done EXCEPT that we
1544 * need to restart demuxer... and I don't see how we
1545 * could do that :p */
1547 "reinitialization needed --> unsupported" );
1548 p_access->info.b_eof = true;
1556 msg_Err( p_access, "cannot receive %s (aborting)",
1557 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1558 p_access->info.b_eof = true;
1563 static void *KeepAliveThread( void *p_data )
1565 access_t *p_access = p_data;
1569 /* Send keep-alive every ten seconds */
1570 int canc = vlc_savecancel();
1572 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1574 vlc_restorecancel( canc );
1576 msleep( 10 * CLOCK_FREQ );
1581 static void KeepAliveStart( access_t *p_access )
1583 access_sys_t *p_sys = p_access->p_sys;
1584 if( p_sys->b_keep_alive )
1587 p_sys->b_keep_alive = !vlc_clone( &p_sys->keep_alive,
1588 KeepAliveThread, p_access,
1589 VLC_THREAD_PRIORITY_LOW );
1592 static void KeepAliveStop( access_t *p_access )
1594 access_sys_t *p_sys = p_access->p_sys;
1595 if( !p_sys->b_keep_alive )
1598 vlc_cancel( p_sys->keep_alive );
1599 vlc_join( p_sys->keep_alive, NULL );
1600 p_sys->b_keep_alive = false;