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>
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( !p_access->b_die )
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:
293 msg_Warn( p_access, "unimplemented query in control" );
300 /*****************************************************************************
301 * Seek: try to go at the right place
302 *****************************************************************************/
303 static int Seek( access_t * p_access, uint64_t i_pos )
305 access_sys_t *p_sys = p_access->p_sys;
310 if( i_pos < p_sys->i_header)
312 if( p_access->info.i_pos < p_sys->i_header )
314 /* no need to restart stream, it was already one
315 * or no stream was yet read */
316 p_access->info.i_pos = i_pos;
321 i_packet = 0xffffffff;
327 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
328 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
330 if( p_sys->b_seekable && i_packet >= p_sys->i_packet_count )
333 msg_Dbg( p_access, "seeking to %"PRIu64 " (packet:%u)", i_pos, i_packet );
336 msg_Dbg( p_access, "stream stopped (seek)" );
338 /* *** restart stream *** */
339 var_buffer_initwrite( &buffer, 0 );
340 var_buffer_add64( &buffer, 0 ); /* seek point in second */
341 var_buffer_add32( &buffer, 0xffffffff );
342 var_buffer_add32( &buffer, i_packet ); // begin from start
343 var_buffer_add8( &buffer, 0xff ); // stream time limit
344 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
345 var_buffer_add8( &buffer, 0xff ); //
346 var_buffer_add8( &buffer, 0x00 ); // don't use limit
347 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
349 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
350 buffer.p_data, buffer.i_data );
352 var_buffer_free( &buffer );
355 while( vlc_object_alive (p_access) )
357 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
359 p_access->info.b_eof = true;
363 if( p_sys->i_command == 0x1e )
365 msg_Dbg( p_access, "received 0x1e (seek)" );
370 while( vlc_object_alive (p_access) )
372 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
374 p_access->info.b_eof = true;
377 if( p_sys->i_command == 0x05 )
379 msg_Dbg( p_access, "received 0x05 (seek)" );
385 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
387 p_access->info.b_eof = true;
391 msg_Dbg( p_access, "Streaming restarted" );
393 p_sys->i_media_used += i_offset;
394 p_access->info.i_pos = i_pos;
395 p_access->info.b_eof = false;
400 /*****************************************************************************
402 *****************************************************************************/
403 static block_t *Block( access_t *p_access )
405 access_sys_t *p_sys = p_access->p_sys;
407 if( p_access->info.b_eof )
410 if( p_access->info.i_pos < p_sys->i_header )
412 const size_t i_copy = p_sys->i_header - p_access->info.i_pos;
414 block_t *p_block = block_New( p_access, i_copy );
418 memcpy( p_block->p_buffer, &p_sys->p_header[p_access->info.i_pos], i_copy );
419 p_access->info.i_pos += i_copy;
422 else if( p_sys->p_media && p_sys->i_media_used < __MAX( p_sys->i_media, p_sys->i_packet_length ) )
425 size_t i_padding = 0;
427 if( p_sys->i_media_used < p_sys->i_media )
428 i_copy = p_sys->i_media - p_sys->i_media_used;
429 if( __MAX( p_sys->i_media, p_sys->i_media_used ) < p_sys->i_packet_length )
430 i_padding = p_sys->i_packet_length - __MAX( p_sys->i_media, p_sys->i_media_used );
432 block_t *p_block = block_New( p_access, i_copy + i_padding );
437 memcpy( &p_block->p_buffer[0], &p_sys->p_media[p_sys->i_media_used], i_copy );
439 memset( &p_block->p_buffer[i_copy], 0, i_padding );
441 p_sys->i_media_used += i_copy + i_padding;
442 p_access->info.i_pos += i_copy + i_padding;
446 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
450 /****************************************************************************
451 * MMSOpen : Open a connection with the server over mmst or mmsu
452 ****************************************************************************/
453 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
455 access_sys_t *p_sys = p_access->p_sys;
456 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
461 int i_server_version;
463 int i_update_player_url;
464 int i_encryption_type;
471 /* *** Open a TCP connection with server *** */
472 msg_Dbg( p_access, "waiting for connection..." );
473 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
474 if( p_sys->i_handle_tcp < 0 )
476 msg_Err( p_access, "failed to open a connection (tcp)" );
480 "connection(tcp) with \"%s:%d\" successful",
484 /* *** Bind port if UDP protocol is selected *** */
487 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
490 net_Close( p_sys->i_handle_tcp );
494 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
496 if( p_sys->i_handle_udp < 0 )
498 msg_Err( p_access, "failed to open a connection (udp)" );
499 net_Close( p_sys->i_handle_tcp );
503 "connection(udp) at \"%s:%d\" successful",
504 p_sys->sz_bind_addr, 7000 );
507 /* *** Init context for mms prototcol *** */
508 GenerateGuid ( &p_sys->guid ); /* used to identify client by server */
510 "generated guid: "GUID_FMT,
511 GUID_PRINT( p_sys->guid ) );
512 p_sys->i_command_level = 1; /* updated after 0x1A command */
513 p_sys->i_seq_num = 0;
514 p_sys->i_media_packet_id_type = 0x04;
515 p_sys->i_header_packet_id_type = 0x02;
516 p_sys->i_proto = i_proto;
517 p_sys->i_packet_seq_num = 0;
518 p_sys->p_header = NULL;
520 p_sys->p_media = NULL;
522 p_sys->i_media_used = 0;
524 p_access->info.i_pos = 0;
525 p_sys->i_buffer_tcp = 0;
526 p_sys->i_buffer_udp = 0;
529 p_access->info.b_eof = false;
531 /* *** send command 1 : connection request *** */
532 var_buffer_initwrite( &buffer, 0 );
533 var_buffer_add16( &buffer, 0x001c );
534 var_buffer_add16( &buffer, 0x0003 );
536 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
537 GUID_PRINT( p_sys->guid ),
539 var_buffer_addUTF16( &buffer, tmp );
541 mms_CommandSend( p_access,
542 0x01, /* connexion request */
543 0x00000000, /* flags, FIXME */
544 0x0004000b, /* ???? */
548 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
550 var_buffer_free( &buffer );
551 MMSClose( p_access );
555 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
556 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
557 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
558 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
559 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
560 #define GETUTF16( psz, size ) \
563 psz = xmalloc( size + 1); \
564 for( i = 0; i < size; i++ ) \
571 GETUTF16( p_sys->psz_server_version, i_server_version );
572 GETUTF16( p_sys->psz_tool_version, i_tool_version );
573 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
574 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
577 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
578 p_sys->psz_server_version,
579 p_sys->psz_tool_version,
580 p_sys->psz_update_player_url,
581 p_sys->psz_encryption_type );
583 /* *** should make an 18 command to make data timing *** */
585 /* *** send command 2 : transport protocol selection *** */
586 var_buffer_reinitwrite( &buffer, 0 );
587 var_buffer_add32( &buffer, 0x00000000 );
588 var_buffer_add32( &buffer, 0x000a0000 );
589 var_buffer_add32( &buffer, 0x00000002 );
599 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
601 var_buffer_addUTF16( &buffer, tmp );
602 var_buffer_add16( &buffer, '0' );
604 mms_CommandSend( p_access,
605 0x02, /* connexion request */
606 0x00000000, /* flags, FIXME */
607 0xffffffff, /* ???? */
611 /* *** response from server, should be 0x02 or 0x03 *** */
612 mms_CommandRead( p_access, 0x02, 0x03 );
613 if( p_sys->i_command == 0x03 )
616 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
617 var_buffer_free( &buffer );
618 MMSClose( p_access );
621 else if( p_sys->i_command != 0x02 )
623 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
626 /* *** send command 5 : media file name/path requested *** */
627 var_buffer_reinitwrite( &buffer, 0 );
628 var_buffer_add64( &buffer, 0 );
630 /* media file path shouldn't start with / character */
631 mediapath = p_url->psz_path;
632 if ( mediapath && *mediapath == '/' )
636 var_buffer_addUTF16( &buffer, mediapath );
638 mms_CommandSend( p_access,
640 p_sys->i_command_level,
645 /* *** wait for reponse *** */
646 mms_CommandRead( p_access, 0x1a, 0x06 );
648 /* test if server send 0x1A answer */
649 if( p_sys->i_command == 0x1A )
651 msg_Err( p_access, "id/password requested (not yet supported)" );
653 var_buffer_free( &buffer );
654 MMSClose( p_access );
657 if( p_sys->i_command != 0x06 )
660 "unknown answer (0x%x instead of 0x06)",
662 var_buffer_free( &buffer );
663 MMSClose( p_access );
667 /* 1 for file ok, 2 for authen ok */
668 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
671 msg_Dbg( p_access, "media file name/path accepted" );
674 msg_Dbg( p_access, "authentication accepted" );
678 msg_Err( p_access, "error while asking for file %d",
679 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
680 var_buffer_free( &buffer );
681 MMSClose( p_access );
685 p_sys->i_flags_broadcast =
686 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
687 p_sys->i_media_length =
688 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
689 p_sys->i_packet_length =
690 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
691 p_sys->i_packet_count =
692 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
693 p_sys->i_max_bit_rate =
694 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
695 p_sys->i_header_size =
696 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
699 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
700 "packet_length:%zu packet_count:%"PRIu32" max_bit_rate:%d "
702 p_sys->i_flags_broadcast,
703 p_sys->i_media_length,
704 p_sys->i_packet_length,
705 p_sys->i_packet_count,
706 p_sys->i_max_bit_rate,
707 p_sys->i_header_size );
709 /* *** send command 15 *** */
711 var_buffer_reinitwrite( &buffer, 0 );
712 var_buffer_add32( &buffer, 0 );
713 var_buffer_add32( &buffer, 0x8000 );
714 var_buffer_add32( &buffer, 0xffffffff );
715 var_buffer_add32( &buffer, 0x00 );
716 var_buffer_add32( &buffer, 0x00 );
717 var_buffer_add32( &buffer, 0x00 );
718 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
719 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
720 var_buffer_add32( &buffer, 0x00 );
721 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
722 buffer.p_data, buffer.i_data );
724 /* *** wait for reponse *** */
725 /* Commented out because it fails on some stream (no 0x11 answer) */
727 mms_CommandRead( p_access, 0x11, 0 );
729 if( p_sys->i_command != 0x11 )
732 "unknown answer (0x%x instead of 0x11)",
734 var_buffer_free( &buffer );
735 MMSClose( p_access );
740 /* *** now read header packet *** */
741 /* XXX could be split over multiples packets */
742 msg_Dbg( p_access, "reading header" );
745 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
747 msg_Err( p_access, "cannot receive header" );
748 var_buffer_free( &buffer );
749 MMSClose( p_access );
752 if( p_sys->i_header >= p_sys->i_header_size )
755 "header complete(%zu)",
760 "header incomplete (%zu/%zu), reading more",
762 p_sys->i_header_size );
765 /* *** parse header and get stream and their id *** */
766 /* get all streams properties,
768 * TODO : stream bitrates properties(optional)
769 * and bitrate mutual exclusion(optional) */
770 asf_HeaderParse ( &p_sys->asfh,
771 p_sys->p_header, p_sys->i_header );
772 asf_StreamSelect( &p_sys->asfh,
773 var_InheritInteger( p_access, "mms-maxbitrate" ),
774 var_InheritBool( p_access, "mms-all" ),
775 var_InheritBool( p_access, "audio" ),
776 var_InheritBool( p_access, "video" ) );
778 /* *** now select stream we want to receive *** */
779 /* TODO take care of stream bitrate TODO */
782 var_buffer_reinitwrite( &buffer, 0 );
783 /* for now, select first audio and video stream */
784 for( i = 1; i < 128; i++ )
787 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
792 var_buffer_add16( &buffer, 0xffff );
793 var_buffer_add16( &buffer, i );
799 if( p_sys->asfh.stream[i].i_selected )
801 var_buffer_add16( &buffer, 0x0000 );
803 "selecting stream[0x%x] %s (%d Kib/s)",
805 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
807 p_sys->asfh.stream[i].i_bitrate / 1024);
811 var_buffer_add16( &buffer, 0x0002 );
813 "ignoring stream[0x%x] %s (%d Kib/s)",
815 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
817 p_sys->asfh.stream[i].i_bitrate / 1024);
825 msg_Err( p_access, "cannot find any stream" );
826 var_buffer_free( &buffer );
827 MMSClose( p_access );
830 mms_CommandSend( p_access, 0x33,
832 0xffff | ( i_first << 16 ),
833 buffer.p_data, buffer.i_data );
835 mms_CommandRead( p_access, 0x21, 0 );
836 if( p_sys->i_command != 0x21 )
839 "unknown answer (0x%x instead of 0x21)",
841 var_buffer_free( &buffer );
842 MMSClose( p_access );
847 var_buffer_free( &buffer );
849 msg_Info( p_access, "connection successful" );
854 /****************************************************************************
855 * MMSStart : Start streaming
856 ****************************************************************************/
857 static int MMSStart( access_t *p_access, uint32_t i_packet )
859 access_sys_t *p_sys = p_access->p_sys;
862 /* *** start stream from packet 0 *** */
863 var_buffer_initwrite( &buffer, 0 );
864 var_buffer_add64( &buffer, 0 ); /* seek point in second */
865 var_buffer_add32( &buffer, 0xffffffff );
866 var_buffer_add32( &buffer, i_packet ); // begin from start
867 var_buffer_add8( &buffer, 0xff ); // stream time limit
868 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
869 var_buffer_add8( &buffer, 0xff ); //
870 var_buffer_add8( &buffer, 0x00 ); // don't use limit
871 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
873 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
874 buffer.p_data, buffer.i_data );
876 var_buffer_free( &buffer );
878 mms_CommandRead( p_access, 0x05, 0 );
880 if( p_sys->i_command != 0x05 )
883 "unknown answer (0x%x instead of 0x05)",
890 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
892 msg_Dbg( p_access, "streaming started" );
897 /****************************************************************************
898 * MMSStop : Stop streaming
899 ****************************************************************************/
900 static int MMSStop( access_t *p_access )
902 access_sys_t *p_sys = p_access->p_sys;
904 /* *** stop stream but keep connection alive *** */
905 mms_CommandSend( p_access,
907 p_sys->i_command_level,
913 /****************************************************************************
914 * MMSClose : Close streaming and connection
915 ****************************************************************************/
916 static void MMSClose( access_t *p_access )
918 access_sys_t *p_sys = p_access->p_sys;
920 msg_Dbg( p_access, "Connection closed" );
922 /* *** tell server that we will disconnect *** */
923 mms_CommandSend( p_access,
925 p_sys->i_command_level,
929 /* *** close sockets *** */
930 net_Close( p_sys->i_handle_tcp );
931 if( p_sys->i_proto == MMS_PROTO_UDP )
933 net_Close( p_sys->i_handle_udp );
936 FREENULL( p_sys->p_cmd );
937 FREENULL( p_sys->p_media );
938 FREENULL( p_sys->p_header );
940 FREENULL( p_sys->psz_server_version );
941 FREENULL( p_sys->psz_tool_version );
942 FREENULL( p_sys->psz_update_player_url );
943 FREENULL( p_sys->psz_encryption_type );
946 /****************************************************************************
948 * MMS specific functions
950 ****************************************************************************/
951 static int mms_CommandSend( access_t *p_access, int i_command,
952 uint32_t i_prefix1, uint32_t i_prefix2,
953 uint8_t *p_data, int i_data_old )
956 access_sys_t *p_sys = p_access->p_sys;
957 int i_data_by8, i_ret;
958 int i_data = i_data_old;
960 while( i_data & 0x7 ) i_data++;
961 i_data_by8 = i_data >> 3;
963 /* first init buffer */
964 var_buffer_initwrite( &buffer, 0 );
966 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
967 var_buffer_add32( &buffer, 0xB00BFACE );
968 /* size after protocol type */
969 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
970 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
971 var_buffer_add32( &buffer, i_data_by8 + 4 );
972 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
973 var_buffer_add64( &buffer, 0 );
974 var_buffer_add32( &buffer, i_data_by8 + 2 );
975 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
976 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
977 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
979 /* specific command data */
980 if( p_data && i_data > 0 )
982 var_buffer_addmemory( &buffer, p_data, i_data_old );
985 /* Append padding to the command data */
986 var_buffer_add64( &buffer, 0 );
989 vlc_mutex_lock( &p_sys->lock_netwrite );
990 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
991 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
992 vlc_mutex_unlock( &p_sys->lock_netwrite );
994 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
996 var_buffer_free( &buffer );
997 msg_Err( p_access, "failed to send command" );
1001 var_buffer_free( &buffer );
1005 static int NetFillBuffer( access_t *p_access )
1011 access_sys_t *p_sys = p_access->p_sys;
1013 struct pollfd ufd[2];
1014 unsigned timeout, nfd;
1016 /* FIXME when using udp */
1017 ssize_t i_tcp, i_udp;
1018 ssize_t i_tcp_read, i_udp_read;
1021 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1023 if( p_sys->i_proto == MMS_PROTO_UDP )
1025 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1029 i_udp = 0; /* there isn't udp socket */
1032 if( ( i_udp <= 0 ) && ( i_tcp <= 0 ) )
1034 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1039 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1042 /* Find if some data is available */
1047 /* Initialize file descriptor set */
1048 memset (ufd, 0, sizeof (ufd));
1053 ufd[nfd].fd = p_sys->i_handle_tcp;
1054 ufd[nfd].events = POLLIN;
1059 ufd[nfd].fd = p_sys->i_handle_udp;
1060 ufd[nfd].events = POLLIN;
1064 /* We'll wait 0.5 second if nothing happens */
1065 timeout = __MIN( 500, p_sys->i_timeout );
1067 if( i_try * timeout > p_sys->i_timeout )
1069 msg_Err(p_access, "no data received");
1073 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1078 if( !vlc_object_alive (p_access) )
1081 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1083 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1084 (i_ret < 0 && errno == EINTR) );
1088 msg_Err( p_access, "network poll error (%m)" );
1092 i_tcp_read = i_udp_read = 0;
1094 if( ( i_tcp > 0 ) && ufd[0].revents )
1097 recv( p_sys->i_handle_tcp,
1098 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1099 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1102 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1104 i_udp_read = recv( p_sys->i_handle_udp,
1105 p_sys->buffer_udp + p_sys->i_buffer_udp,
1106 i_udp + MMS_BUFFER_SIZE/2, 0 );
1110 if( p_sys->i_proto == MMS_PROTO_UDP )
1112 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1113 p_sys->i_buffer_tcp, i_tcp_read,
1114 p_sys->i_buffer_udp, i_udp_read );
1118 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1119 p_sys->i_buffer_tcp, i_tcp_read );
1123 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1124 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1126 return i_tcp_read + i_udp_read;
1130 static int mms_ParseCommand( access_t *p_access,
1135 #define GET32( i_pos ) \
1136 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1137 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1138 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1140 access_sys_t *p_sys = p_access->p_sys;
1144 free( p_sys->p_cmd );
1145 p_sys->i_cmd = i_data;
1146 p_sys->p_cmd = xmalloc( i_data );
1147 memcpy( p_sys->p_cmd, p_data, i_data );
1149 *pi_used = i_data; /* by default */
1151 if( i_data < MMS_CMD_HEADERSIZE )
1153 msg_Warn( p_access, "truncated command (header incomplete)" );
1154 p_sys->i_command = 0;
1157 i_id = GetDWLE( p_data + 4 );
1158 i_length = GetDWLE( p_data + 8 ) + 16;
1160 if( i_id != 0xb00bface || i_length < 16 )
1163 "incorrect command header (0x%"PRIx32")", i_id );
1164 p_sys->i_command = 0;
1168 if( i_length > p_sys->i_cmd )
1171 "truncated command (missing %zu bytes)",
1172 (size_t)i_length - i_data );
1173 p_sys->i_command = 0;
1176 else if( i_length < p_sys->i_cmd )
1178 p_sys->i_cmd = i_length;
1179 *pi_used = i_length;
1183 "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",
1187 /* 12: protocol type "MMS " */
1190 /* 24: unknown (0) */
1191 /* 28: unknown (0) */
1197 p_sys->i_command = GET32( 36 ) & 0xffff;
1200 return MMS_PACKET_CMD;
1203 static int mms_ParsePacket( access_t *p_access,
1204 uint8_t *p_data, size_t i_data,
1207 access_sys_t *p_sys = p_access->p_sys;
1208 int i_packet_seq_num;
1209 size_t i_packet_length;
1210 uint32_t i_packet_id;
1212 *pi_used = i_data; /* default */
1215 msg_Warn( p_access, "truncated packet (header incomplete)" );
1219 i_packet_id = p_data[4];
1220 i_packet_seq_num = GetDWLE( p_data );
1221 i_packet_length = GetWLE( p_data + 6 );
1223 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1225 if( i_packet_length > i_data || i_packet_length <= 8)
1227 /* msg_Dbg( p_access,
1228 "truncated packet (Declared %d bytes, Actual %d bytes)",
1229 i_packet_length, i_data ); */
1233 else if( i_packet_length < i_data )
1235 *pi_used = i_packet_length;
1238 if( i_packet_id == 0xff )
1241 "receive MMS UDP pair timing" );
1242 return( MMS_PACKET_UDP_TIMING );
1245 if( i_packet_id != p_sys->i_header_packet_id_type &&
1246 i_packet_id != p_sys->i_media_packet_id_type )
1248 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1252 /* we now have a media or a header packet */
1253 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1256 /* FIXME for udp could be just wrong order ? */
1258 "detected packet lost (%d != %d)",
1260 p_sys->i_packet_seq_num );
1263 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1265 if( i_packet_id == p_sys->i_header_packet_id_type )
1267 if( p_sys->p_header )
1269 p_sys->p_header = xrealloc( p_sys->p_header,
1270 p_sys->i_header + i_packet_length - 8 );
1271 memcpy( &p_sys->p_header[p_sys->i_header],
1272 p_data + 8, i_packet_length - 8 );
1273 p_sys->i_header += i_packet_length - 8;
1278 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1279 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1280 p_sys->p_header = p_packet;
1281 p_sys->i_header = i_packet_length - 8;
1283 /* msg_Dbg( p_access,
1284 "receive header packet (%d bytes)",
1285 i_packet_length - 8 ); */
1287 return MMS_PACKET_HEADER;
1291 uint8_t* p_packet = xmalloc( i_packet_length - 8 ); // don't bother with preheader
1292 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1293 FREENULL( p_sys->p_media );
1294 p_sys->p_media = p_packet;
1295 p_sys->i_media = i_packet_length - 8;
1296 p_sys->i_media_used = 0;
1297 /* msg_Dbg( p_access,
1298 "receive media packet (%d bytes)",
1299 i_packet_length - 8 ); */
1301 return MMS_PACKET_MEDIA;
1305 static int mms_ReceivePacket( access_t *p_access )
1307 access_sys_t *p_sys = p_access->p_sys;
1308 int i_packet_tcp_type;
1309 int i_packet_udp_type;
1313 bool b_refill = true;
1315 /* first if we need to refill buffer */
1316 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1318 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1320 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1321 (uint32_t)p_sys->i_buffer_tcp )
1326 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1331 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1332 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1337 if( b_refill && NetFillBuffer( p_access ) < 0 )
1339 msg_Warn( p_access, "cannot fill buffer" );
1343 i_packet_tcp_type = -1;
1344 i_packet_udp_type = -1;
1346 if( p_sys->i_buffer_tcp > 0 )
1350 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1353 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1354 p_sys->i_buffer_tcp, &i_used );
1360 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1361 p_sys->i_buffer_tcp, &i_used );
1363 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1365 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1366 MMS_BUFFER_SIZE - i_used );
1368 p_sys->i_buffer_tcp -= i_used;
1370 else if( p_sys->i_buffer_udp > 0 )
1375 mms_ParsePacket( p_access, p_sys->buffer_udp,
1376 p_sys->i_buffer_udp, &i_used );
1378 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1380 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1381 MMS_BUFFER_SIZE - i_used );
1383 p_sys->i_buffer_udp -= i_used;
1386 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1388 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1389 i_packet_tcp_type = -1;
1392 if( i_packet_tcp_type != -1 )
1394 return i_packet_tcp_type;
1396 else if( i_packet_udp_type != -1 )
1398 return i_packet_udp_type;
1403 static int mms_ReceiveCommand( access_t *p_access )
1405 access_sys_t *p_sys = p_access->p_sys;
1412 if( NetFillBuffer( p_access ) < 0 )
1414 msg_Warn( p_access, "cannot fill buffer" );
1415 return VLC_EGENERIC;
1417 if( p_sys->i_buffer_tcp > 0 )
1419 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1420 p_sys->i_buffer_tcp, &i_used );
1421 if( i_used < MMS_BUFFER_SIZE )
1423 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1424 MMS_BUFFER_SIZE - i_used );
1426 p_sys->i_buffer_tcp -= i_used;
1430 return VLC_EGENERIC;
1433 if( p_sys->i_command == 0x1b )
1435 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1444 return VLC_EGENERIC;
1451 #define MMS_RETRY_MAX 10
1452 #define MMS_RETRY_SLEEP 50000
1454 static int mms_CommandRead( access_t *p_access, int i_command1,
1457 access_sys_t *p_sys = p_access->p_sys;
1461 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1463 i_status = mms_ReceiveCommand( p_access );
1464 if( i_status < 0 || p_sys->i_command == 0 )
1467 msleep( MMS_RETRY_SLEEP );
1469 else if( i_command1 == 0 && i_command2 == 0)
1473 else if( p_sys->i_command == i_command1 ||
1474 p_sys->i_command == i_command2 )
1480 switch( p_sys->i_command )
1483 msg_Warn( p_access, "socket closed by server" );
1484 p_access->info.b_eof = true;
1485 return VLC_EGENERIC;
1487 msg_Warn( p_access, "end of media stream" );
1488 p_access->info.b_eof = true;
1489 return VLC_EGENERIC;
1495 p_access->info.b_eof = true;
1496 msg_Warn( p_access, "failed to receive command (aborting)" );
1498 return VLC_EGENERIC;
1502 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1504 access_sys_t *p_sys = p_access->p_sys;
1507 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1511 if( !vlc_object_alive (p_access) )
1514 i_status = mms_ReceivePacket( p_access );
1518 msg_Warn( p_access, "cannot receive header (%d/%d)",
1519 i_count, MMS_RETRY_MAX );
1520 msleep( MMS_RETRY_SLEEP );
1522 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1526 else if( i_status == MMS_PACKET_CMD )
1528 switch( p_sys->i_command )
1531 msg_Warn( p_access, "socket closed by server" );
1532 p_access->info.b_eof = true;
1535 msg_Warn( p_access, "end of media stream" );
1536 p_access->info.b_eof = true;
1539 /* XXX not too dificult to be done EXCEPT that we
1540 * need to restart demuxer... and I don't see how we
1541 * could do that :p */
1543 "reinitialization needed --> unsupported" );
1544 p_access->info.b_eof = true;
1552 msg_Err( p_access, "cannot receive %s (aborting)",
1553 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );
1554 p_access->info.b_eof = true;
1559 static void *KeepAliveThread( void *p_data )
1561 access_t *p_access = p_data;
1565 /* Send keep-alive every ten seconds */
1566 int canc = vlc_savecancel();
1568 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1570 vlc_restorecancel( canc );
1572 msleep( 10 * CLOCK_FREQ );
1577 static void KeepAliveStart( access_t *p_access )
1579 access_sys_t *p_sys = p_access->p_sys;
1580 if( p_sys->b_keep_alive )
1583 p_sys->b_keep_alive = !vlc_clone( &p_sys->keep_alive,
1584 KeepAliveThread, p_access,
1585 VLC_THREAD_PRIORITY_LOW );
1588 static void KeepAliveStop( access_t *p_access )
1590 access_sys_t *p_sys = p_access->p_sys;
1591 if( !p_sys->b_keep_alive )
1594 vlc_cancel( p_sys->keep_alive );
1595 vlc_join( p_sys->keep_alive, NULL );
1596 p_sys->b_keep_alive = false;