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 *****************************************************************************/
29 #include <vlc_access.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
49 #include <vlc_network.h>
59 /****************************************************************************
61 * MMSProtocole documentation found at http://get.to/sdp
62 ****************************************************************************/
64 /*****************************************************************************
66 *****************************************************************************/
67 int E_( MMSTUOpen ) ( access_t * );
68 void E_( MMSTUClose ) ( access_t * );
71 static int Read( access_t *, uint8_t *, int );
72 static int Seek( access_t *, int64_t );
73 static int Control( access_t *, int, va_list );
75 static int MMSOpen ( access_t *, vlc_url_t *, int );
76 static int MMSStart( access_t *, uint32_t );
77 static int MMSStop ( access_t * );
78 static void MMSClose( access_t * );
81 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
82 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
84 static int mms_HeaderMediaRead( access_t *, int );
86 static int mms_ReceivePacket( access_t * );
89 int E_(MMSTUOpen)( access_t *p_access )
96 p_access->pf_read = Read;
97 p_access->pf_block = NULL;
98 p_access->pf_control = Control;
99 p_access->pf_seek = Seek;
100 p_access->info.i_update = 0;
101 p_access->info.i_size = 0;
102 p_access->info.i_pos = 0;
103 p_access->info.b_eof = VLC_FALSE;
104 p_access->info.i_title = 0;
105 p_access->info.i_seekpoint = 0;
106 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
107 memset( p_sys, 0, sizeof( access_sys_t ) );
109 /* *** Parse URL and get server addr/port and path *** */
110 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
111 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
113 msg_Err( p_access, "invalid server name" );
114 vlc_UrlClean( &p_sys->url );
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 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
148 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
153 msg_Err( p_access, "cannot connect to server" );
154 vlc_UrlClean( &p_sys->url );
159 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
163 * broadcast yy=0x02, xx= 0x00
164 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
166 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
168 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
170 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
172 p_sys->b_seekable = VLC_FALSE;
176 p_sys->b_seekable = VLC_TRUE;
177 p_access->info.i_size =
178 (uint64_t)p_sys->i_header +
179 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
182 /* *** Start stream *** */
183 if( MMSStart( p_access, 0xffffffff ) < 0 )
185 msg_Err( p_access, "cannot start stream" );
186 E_(MMSTUClose) ( p_access );
192 /*****************************************************************************
193 * Close: free unused data structures
194 *****************************************************************************/
195 void E_(MMSTUClose)( access_t *p_access )
197 access_sys_t *p_sys = p_access->p_sys;
199 /* close connection with server */
200 MMSClose( p_access );
203 vlc_UrlClean( &p_sys->url );
208 /*****************************************************************************
210 *****************************************************************************/
211 static int Control( access_t *p_access, int i_query, va_list args )
213 access_sys_t *p_sys = p_access->p_sys;
223 case ACCESS_CAN_SEEK:
224 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
225 *pb_bool = p_sys->b_seekable;
228 case ACCESS_CAN_FASTSEEK:
229 case ACCESS_CAN_PAUSE:
230 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
231 *pb_bool = VLC_FALSE;
234 case ACCESS_CAN_CONTROL_PACE:
235 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
237 #if 0 /* Disable for now until we have a clock synchro algo
238 * which works with something else than MPEG over UDP */
239 *pb_bool = VLC_FALSE;
246 pi_int = (int*)va_arg( args, int * );
247 *pi_int = 3 * p_sys->i_packet_length;
250 case ACCESS_GET_PTS_DELAY:
251 pi_64 = (int64_t*)va_arg( args, int64_t * );
252 var_Get( p_access, "mms-caching", &val );
253 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
256 case ACCESS_GET_PRIVATE_ID_STATE:
257 i_int = (int)va_arg( args, int );
258 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
260 if( i_int < 0 || i_int > 127 )
262 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
266 case ACCESS_SET_PAUSE_STATE:
267 case ACCESS_GET_TITLE_INFO:
268 case ACCESS_SET_TITLE:
269 case ACCESS_SET_SEEKPOINT:
270 case ACCESS_SET_PRIVATE_ID_STATE:
271 case ACCESS_GET_CONTENT_TYPE:
276 msg_Warn( p_access, "unimplemented query in control" );
283 /*****************************************************************************
284 * Seek: try to go at the right place
285 *****************************************************************************/
286 static int Seek( access_t * p_access, int64_t i_pos )
288 access_sys_t *p_sys = p_access->p_sys;
296 if( i_pos < p_sys->i_header)
299 if( p_access->info.i_pos < p_sys->i_header )
301 /* no need to restart stream, it was already one
302 * or no stream was yet read */
303 p_access->info.i_pos = i_pos;
308 i_packet = 0xffffffff;
314 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
315 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
317 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
320 msg_Dbg( p_access, "stream stopped (seek)" );
322 /* *** restart stream *** */
323 var_buffer_initwrite( &buffer, 0 );
324 var_buffer_add64( &buffer, 0 ); /* seek point in second */
325 var_buffer_add32( &buffer, 0xffffffff );
326 var_buffer_add32( &buffer, i_packet ); // begin from start
327 var_buffer_add8( &buffer, 0xff ); // stream time limit
328 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
329 var_buffer_add8( &buffer, 0xff ); //
330 var_buffer_add8( &buffer, 0x00 ); // don't use limit
331 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
333 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
334 buffer.p_data, buffer.i_data );
336 var_buffer_free( &buffer );
339 while( !p_access->b_die )
341 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
342 if( p_sys->i_command == 0x1e )
344 msg_Dbg( p_access, "received 0x1e (seek)" );
349 while( !p_access->b_die )
351 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
352 if( p_sys->i_command == 0x05 )
354 msg_Dbg( p_access, "received 0x05 (seek)" );
360 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
361 msg_Dbg( p_access, "Streaming restarted" );
363 p_sys->i_media_used += i_offset;
364 p_access->info.i_pos = i_pos;
365 p_access->info.b_eof = VLC_FALSE;
370 /*****************************************************************************
372 *****************************************************************************/
373 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
375 access_sys_t *p_sys = p_access->p_sys;
381 /* *** now send data if needed *** */
382 while( i_data < (size_t)i_len )
384 if( p_access->info.i_pos < p_sys->i_header )
386 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
387 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
389 p_access->info.i_pos += i_copy;
391 else if( p_sys->i_media_used < p_sys->i_media )
393 i_copy = __MIN( i_len - i_data ,
394 p_sys->i_media - p_sys->i_media_used );
395 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
397 p_sys->i_media_used += i_copy;
398 p_access->info.i_pos += i_copy;
400 else if( p_sys->p_media != NULL &&
401 p_sys->i_media_used < p_sys->i_packet_length )
403 i_copy = __MIN( i_len - i_data,
404 p_sys->i_packet_length - p_sys->i_media_used);
405 memset( &p_buffer[i_data], 0, i_copy );
408 p_sys->i_media_used += i_copy;
409 p_access->info.i_pos += i_copy;
411 else if( p_access->info.b_eof ||
412 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
421 /****************************************************************************
422 * MMSOpen : Open a connection with the server over mmst or mmsu
423 ****************************************************************************/
424 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
426 access_sys_t *p_sys = p_access->p_sys;
427 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
432 int i_server_version;
434 int i_update_player_url;
435 int i_encryption_type;
441 /* *** Open a TCP connection with server *** */
442 msg_Dbg( p_access, "waiting for connection..." );
443 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
444 if( p_sys->i_handle_tcp < 0 )
446 msg_Err( p_access, "failed to open a connection (tcp)" );
450 "connection(tcp) with \"%s:%d\" successful",
454 /* *** Bind port if UDP protocol is selected *** */
457 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
460 net_Close( p_sys->i_handle_tcp );
464 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
466 if( p_sys->i_handle_udp < 0 )
468 msg_Err( p_access, "failed to open a connection (udp)" );
469 net_Close( p_sys->i_handle_tcp );
473 "connection(udp) at \"%s:%d\" successful",
474 p_sys->sz_bind_addr, 7000 );
477 /* *** Init context for mms prototcol *** */
478 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
480 "generated guid: "GUID_FMT,
481 GUID_PRINT( p_sys->guid ) );
482 p_sys->i_command_level = 1; /* updated after 0x1A command */
483 p_sys->i_seq_num = 0;
484 p_sys->i_media_packet_id_type = 0x04;
485 p_sys->i_header_packet_id_type = 0x02;
486 p_sys->i_proto = i_proto;
487 p_sys->i_packet_seq_num = 0;
488 p_sys->p_header = NULL;
490 p_sys->p_media = NULL;
492 p_sys->i_media_used = 0;
494 p_access->info.i_pos = 0;
495 p_sys->i_buffer_tcp = 0;
496 p_sys->i_buffer_udp = 0;
499 p_access->info.b_eof = 0;
501 /* *** send command 1 : connection request *** */
502 var_buffer_initwrite( &buffer, 0 );
503 var_buffer_add16( &buffer, 0x001c );
504 var_buffer_add16( &buffer, 0x0003 );
506 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
507 GUID_PRINT( p_sys->guid ),
509 var_buffer_addUTF16( &buffer, tmp );
511 mms_CommandSend( p_access,
512 0x01, /* connexion request */
513 0x00000000, /* flags, FIXME */
514 0x0004000b, /* ???? */
518 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
520 var_buffer_free( &buffer );
521 MMSClose( p_access );
525 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
526 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
527 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
528 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
529 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
530 #define GETUTF16( psz, size ) \
533 psz = malloc( size + 1); \
534 for( i = 0; i < size; i++ ) \
541 GETUTF16( p_sys->psz_server_version, i_server_version );
542 GETUTF16( p_sys->psz_tool_version, i_tool_version );
543 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
544 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
547 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
548 p_sys->psz_server_version,
549 p_sys->psz_tool_version,
550 p_sys->psz_update_player_url,
551 p_sys->psz_encryption_type );
553 /* *** should make an 18 command to make data timing *** */
555 /* *** send command 2 : transport protocol selection *** */
556 var_buffer_reinitwrite( &buffer, 0 );
557 var_buffer_add32( &buffer, 0x00000000 );
558 var_buffer_add32( &buffer, 0x000a0000 );
559 var_buffer_add32( &buffer, 0x00000002 );
569 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
571 var_buffer_addUTF16( &buffer, tmp );
572 var_buffer_add16( &buffer, '0' );
574 mms_CommandSend( p_access,
575 0x02, /* connexion request */
576 0x00000000, /* flags, FIXME */
577 0xffffffff, /* ???? */
581 /* *** response from server, should be 0x02 or 0x03 *** */
582 mms_CommandRead( p_access, 0x02, 0x03 );
583 if( p_sys->i_command == 0x03 )
586 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
587 var_buffer_free( &buffer );
588 MMSClose( p_access );
591 else if( p_sys->i_command != 0x02 )
593 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
596 /* *** send command 5 : media file name/path requested *** */
597 var_buffer_reinitwrite( &buffer, 0 );
598 var_buffer_add64( &buffer, 0 );
599 var_buffer_addUTF16( &buffer, p_url->psz_path );
601 mms_CommandSend( p_access,
603 p_sys->i_command_level,
608 /* *** wait for reponse *** */
609 mms_CommandRead( p_access, 0x1a, 0x06 );
611 /* test if server send 0x1A answer */
612 if( p_sys->i_command == 0x1A )
614 msg_Err( p_access, "id/password requested (not yet supported)" );
616 var_buffer_free( &buffer );
617 MMSClose( p_access );
620 if( p_sys->i_command != 0x06 )
623 "unknown answer (0x%x instead of 0x06)",
625 var_buffer_free( &buffer );
626 MMSClose( p_access );
630 /* 1 for file ok, 2 for authen ok */
631 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
634 msg_Dbg( p_access, "media file name/path accepted" );
637 msg_Dbg( p_access, "authentication accepted" );
641 msg_Err( p_access, "error while asking for file %d",
642 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
643 var_buffer_free( &buffer );
644 MMSClose( p_access );
648 p_sys->i_flags_broadcast =
649 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
650 p_sys->i_media_length =
651 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
652 p_sys->i_packet_length =
653 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
654 p_sys->i_packet_count =
655 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
656 p_sys->i_max_bit_rate =
657 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
658 p_sys->i_header_size =
659 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
662 "answer 0x06 flags:0x%8.8x media_length:%us "
663 "packet_length:%ul packet_count:%d max_bit_rate:%d "
665 p_sys->i_flags_broadcast,
666 p_sys->i_media_length,
667 (unsigned)p_sys->i_packet_length,
668 p_sys->i_packet_count,
669 p_sys->i_max_bit_rate,
670 p_sys->i_header_size );
672 /* *** send command 15 *** */
674 var_buffer_reinitwrite( &buffer, 0 );
675 var_buffer_add32( &buffer, 0 );
676 var_buffer_add32( &buffer, 0x8000 );
677 var_buffer_add32( &buffer, 0xffffffff );
678 var_buffer_add32( &buffer, 0x00 );
679 var_buffer_add32( &buffer, 0x00 );
680 var_buffer_add32( &buffer, 0x00 );
681 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
682 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
683 var_buffer_add32( &buffer, 0x00 );
684 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
685 buffer.p_data, buffer.i_data );
687 /* *** wait for reponse *** */
688 /* Commented out because it fails on some stream (no 0x11 answer) */
690 mms_CommandRead( p_access, 0x11, 0 );
692 if( p_sys->i_command != 0x11 )
695 "unknown answer (0x%x instead of 0x11)",
697 var_buffer_free( &buffer );
698 MMSClose( p_access );
703 /* *** now read header packet *** */
704 /* XXX could be split over multiples packets */
705 msg_Dbg( p_access, "reading header" );
708 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
710 msg_Err( p_access, "cannot receive header" );
711 var_buffer_free( &buffer );
712 MMSClose( p_access );
715 if( p_sys->i_header >= p_sys->i_header_size )
718 "header complete(%d)",
723 "header incomplete (%d/%d), reading more",
725 p_sys->i_header_size );
728 /* *** parse header and get stream and their id *** */
729 /* get all streams properties,
731 * TODO : stream bitrates properties(optional)
732 * and bitrate mutual exclusion(optional) */
733 E_( asf_HeaderParse )( &p_sys->asfh,
734 p_sys->p_header, p_sys->i_header );
735 E_( asf_StreamSelect)( &p_sys->asfh,
736 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
737 var_CreateGetInteger( p_access, "mms-all" ),
738 var_CreateGetInteger( p_access, "audio" ),
739 var_CreateGetInteger( p_access, "video" ) );
741 /* *** now select stream we want to receive *** */
742 /* TODO take care of stream bitrate TODO */
745 var_buffer_reinitwrite( &buffer, 0 );
746 /* for now, select first audio and video stream */
747 for( i = 1; i < 128; i++ )
750 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
755 var_buffer_add16( &buffer, 0xffff );
756 var_buffer_add16( &buffer, i );
762 if( p_sys->asfh.stream[i].i_selected )
764 var_buffer_add16( &buffer, 0x0000 );
766 "selecting stream[0x%x] %s (%d kb/s)",
768 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
770 p_sys->asfh.stream[i].i_bitrate / 1024);
774 var_buffer_add16( &buffer, 0x0002 );
776 "ignoring stream[0x%x] %s (%d kb/s)",
778 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
780 p_sys->asfh.stream[i].i_bitrate / 1024);
788 msg_Err( p_access, "cannot find any stream" );
789 var_buffer_free( &buffer );
790 MMSClose( p_access );
793 mms_CommandSend( p_access, 0x33,
795 0xffff | ( i_first << 16 ),
796 buffer.p_data, buffer.i_data );
798 mms_CommandRead( p_access, 0x21, 0 );
799 if( p_sys->i_command != 0x21 )
802 "unknown answer (0x%x instead of 0x21)",
804 var_buffer_free( &buffer );
805 MMSClose( p_access );
810 var_buffer_free( &buffer );
812 msg_Info( p_access, "connection successful" );
817 /****************************************************************************
818 * MMSStart : Start streaming
819 ****************************************************************************/
820 static int MMSStart( access_t *p_access, uint32_t i_packet )
822 access_sys_t *p_sys = p_access->p_sys;
825 /* *** start stream from packet 0 *** */
826 var_buffer_initwrite( &buffer, 0 );
827 var_buffer_add64( &buffer, 0 ); /* seek point in second */
828 var_buffer_add32( &buffer, 0xffffffff );
829 var_buffer_add32( &buffer, i_packet ); // begin from start
830 var_buffer_add8( &buffer, 0xff ); // stream time limit
831 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
832 var_buffer_add8( &buffer, 0xff ); //
833 var_buffer_add8( &buffer, 0x00 ); // don't use limit
834 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
836 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
837 buffer.p_data, buffer.i_data );
839 var_buffer_free( &buffer );
841 mms_CommandRead( p_access, 0x05, 0 );
843 if( p_sys->i_command != 0x05 )
846 "unknown answer (0x%x instead of 0x05)",
853 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
854 msg_Dbg( p_access, "streaming started" );
859 /****************************************************************************
860 * MMSStop : Stop streaming
861 ****************************************************************************/
862 static int MMSStop( access_t *p_access )
864 access_sys_t *p_sys = p_access->p_sys;
866 /* *** stop stream but keep connection alive *** */
867 mms_CommandSend( p_access,
869 p_sys->i_command_level,
875 /****************************************************************************
876 * MMSClose : Close streaming and connection
877 ****************************************************************************/
878 static void MMSClose( access_t *p_access )
880 access_sys_t *p_sys = p_access->p_sys;
882 msg_Dbg( p_access, "Connection closed" );
884 /* *** tell server that we will disconnect *** */
885 mms_CommandSend( p_access,
887 p_sys->i_command_level,
891 /* *** close sockets *** */
892 net_Close( p_sys->i_handle_tcp );
893 if( p_sys->i_proto == MMS_PROTO_UDP )
895 net_Close( p_sys->i_handle_udp );
898 FREENULL( p_sys->p_cmd );
899 FREENULL( p_sys->p_media );
900 FREENULL( p_sys->p_header );
902 FREENULL( p_sys->psz_server_version );
903 FREENULL( p_sys->psz_tool_version );
904 FREENULL( p_sys->psz_update_player_url );
905 FREENULL( p_sys->psz_encryption_type );
908 /****************************************************************************
910 * MMS specific functions
912 ****************************************************************************/
913 static int mms_CommandSend( access_t *p_access, int i_command,
914 uint32_t i_prefix1, uint32_t i_prefix2,
915 uint8_t *p_data, int i_data_old )
918 access_sys_t *p_sys = p_access->p_sys;
919 int i_data_by8, i_ret;
920 int i_data = i_data_old;
922 while( i_data & 0x7 ) i_data++;
923 i_data_by8 = i_data >> 3;
925 /* first init buffer */
926 var_buffer_initwrite( &buffer, 0 );
928 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
929 var_buffer_add32( &buffer, 0xB00BFACE );
930 /* size after protocol type */
931 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
932 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
933 var_buffer_add32( &buffer, i_data_by8 + 4 );
934 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
935 var_buffer_add64( &buffer, 0 );
936 var_buffer_add32( &buffer, i_data_by8 + 2 );
937 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
938 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
939 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
941 /* specific command data */
942 if( p_data && i_data > 0 )
944 var_buffer_addmemory( &buffer, p_data, i_data_old );
947 /* Append padding to the command data */
948 var_buffer_add64( &buffer, 0 );
951 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
952 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
953 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
955 msg_Err( p_access, "failed to send command" );
959 var_buffer_free( &buffer );
963 static int NetFillBuffer( access_t *p_access )
969 access_sys_t *p_sys = p_access->p_sys;
970 struct timeval timeout;
974 /* FIXME when using udp */
975 ssize_t i_tcp, i_udp;
976 ssize_t i_tcp_read, i_udp_read;
980 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
982 if( p_sys->i_proto == MMS_PROTO_UDP )
984 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
988 i_udp = 0; /* there isn't udp socket */
994 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
996 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
998 if( i_handle_max == 0 )
1000 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1005 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1008 /* Find if some data is available */
1013 /* Initialize file descriptor set */
1019 FD_SET( p_sys->i_handle_tcp, &fds_r );
1020 FD_SET( p_sys->i_handle_tcp, &fds_e );
1024 FD_SET( p_sys->i_handle_udp, &fds_r );
1025 FD_SET( p_sys->i_handle_udp, &fds_e );
1028 /* We'll wait 0.5 second if nothing happens */
1030 timeout.tv_usec = 500000;
1032 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1037 if( p_access->b_die || p_access->b_error ) return -1;
1039 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1041 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1042 (i_ret < 0 && errno == EINTR) );
1046 msg_Err( p_access, "network select error (%m)" );
1050 i_tcp_read = i_udp_read = 0;
1052 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1055 recv( p_sys->i_handle_tcp,
1056 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1057 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1060 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1062 i_udp_read = recv( p_sys->i_handle_udp,
1063 p_sys->buffer_udp + p_sys->i_buffer_udp,
1064 i_udp + MMS_BUFFER_SIZE/2, 0 );
1068 if( p_sys->i_proto == MMS_PROTO_UDP )
1070 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1071 p_sys->i_buffer_tcp, i_tcp_read,
1072 p_sys->i_buffer_udp, i_udp_read );
1076 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1077 p_sys->i_buffer_tcp, i_tcp_read );
1081 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1082 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1084 return i_tcp_read + i_udp_read;
1088 static int mms_ParseCommand( access_t *p_access,
1093 #define GET32( i_pos ) \
1094 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1095 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1096 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1098 access_sys_t *p_sys = p_access->p_sys;
1104 free( p_sys->p_cmd );
1106 p_sys->i_cmd = i_data;
1107 p_sys->p_cmd = malloc( i_data );
1108 memcpy( p_sys->p_cmd, p_data, i_data );
1110 *pi_used = i_data; /* by default */
1112 if( i_data < MMS_CMD_HEADERSIZE )
1114 msg_Warn( p_access, "truncated command (header incomplete)" );
1115 p_sys->i_command = 0;
1118 i_id = GetDWLE( p_data + 4 );
1119 i_length = GetDWLE( p_data + 8 ) + 16;
1121 if( i_id != 0xb00bface )
1124 "incorrect command header (0x%x)", i_id );
1125 p_sys->i_command = 0;
1129 if( i_length > p_sys->i_cmd )
1132 "truncated command (missing %d bytes)",
1133 i_length - i_data );
1134 p_sys->i_command = 0;
1137 else if( i_length < p_sys->i_cmd )
1139 p_sys->i_cmd = i_length;
1140 *pi_used = i_length;
1144 "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",
1148 /* 12: protocol type "MMS " */
1151 /* 24: unknown (0) */
1152 /* 28: unknown (0) */
1158 p_sys->i_command = GET32( 36 ) & 0xffff;
1161 return MMS_PACKET_CMD;
1164 static int mms_ParsePacket( access_t *p_access,
1165 uint8_t *p_data, size_t i_data,
1168 access_sys_t *p_sys = p_access->p_sys;
1169 int i_packet_seq_num;
1170 size_t i_packet_length;
1171 uint32_t i_packet_id;
1176 *pi_used = i_data; /* default */
1179 msg_Warn( p_access, "truncated packet (header incomplete)" );
1183 i_packet_id = p_data[4];
1184 i_packet_seq_num = GetDWLE( p_data );
1185 i_packet_length = GetWLE( p_data + 6 );
1187 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1189 if( i_packet_length > i_data || i_packet_length <= 8)
1191 /* msg_Dbg( p_access,
1192 "truncated packet (Declared %d bytes, Actual %d bytes)",
1193 i_packet_length, i_data ); */
1197 else if( i_packet_length < i_data )
1199 *pi_used = i_packet_length;
1202 if( i_packet_id == 0xff )
1205 "receive MMS UDP pair timing" );
1206 return( MMS_PACKET_UDP_TIMING );
1209 if( i_packet_id != p_sys->i_header_packet_id_type &&
1210 i_packet_id != p_sys->i_media_packet_id_type )
1212 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1216 /* we now have a media or a header packet */
1217 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1218 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1220 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1223 /* FIXME for udp could be just wrong order ? */
1225 "detected packet lost (%d != %d)",
1227 p_sys->i_packet_seq_num );
1230 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1232 if( i_packet_id == p_sys->i_header_packet_id_type )
1234 if( p_sys->p_header )
1236 p_sys->p_header = realloc( p_sys->p_header,
1237 p_sys->i_header + i_packet_length - 8 );
1238 memcpy( &p_sys->p_header[p_sys->i_header],
1240 i_packet_length - 8 );
1241 p_sys->i_header += i_packet_length - 8;
1247 p_sys->p_header = p_packet;
1248 p_sys->i_header = i_packet_length - 8;
1250 /* msg_Dbg( p_access,
1251 "receive header packet (%d bytes)",
1252 i_packet_length - 8 ); */
1254 return MMS_PACKET_HEADER;
1258 FREENULL( p_sys->p_media );
1259 p_sys->p_media = p_packet;
1260 p_sys->i_media = i_packet_length - 8;
1261 p_sys->i_media_used = 0;
1262 /* msg_Dbg( p_access,
1263 "receive media packet (%d bytes)",
1264 i_packet_length - 8 ); */
1266 return MMS_PACKET_MEDIA;
1270 static int mms_ReceivePacket( access_t *p_access )
1272 access_sys_t *p_sys = p_access->p_sys;
1273 int i_packet_tcp_type;
1274 int i_packet_udp_type;
1278 vlc_bool_t b_refill = VLC_TRUE;
1280 /* first if we need to refill buffer */
1281 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1283 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1285 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1286 (uint32_t)p_sys->i_buffer_tcp )
1288 b_refill = VLC_FALSE;
1291 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1293 b_refill = VLC_FALSE;
1296 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1297 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1299 b_refill = VLC_FALSE;
1302 if( b_refill && NetFillBuffer( p_access ) < 0 )
1304 msg_Warn( p_access, "cannot fill buffer" );
1308 i_packet_tcp_type = -1;
1309 i_packet_udp_type = -1;
1311 if( p_sys->i_buffer_tcp > 0 )
1315 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1318 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1319 p_sys->i_buffer_tcp, &i_used );
1325 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1326 p_sys->i_buffer_tcp, &i_used );
1328 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1330 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1331 MMS_BUFFER_SIZE - i_used );
1333 p_sys->i_buffer_tcp -= i_used;
1335 else if( p_sys->i_buffer_udp > 0 )
1340 mms_ParsePacket( p_access, p_sys->buffer_udp,
1341 p_sys->i_buffer_udp, &i_used );
1343 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1345 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1346 MMS_BUFFER_SIZE - i_used );
1348 p_sys->i_buffer_udp -= i_used;
1351 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1353 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1354 i_packet_tcp_type = -1;
1357 if( i_packet_tcp_type != -1 )
1359 return i_packet_tcp_type;
1361 else if( i_packet_udp_type != -1 )
1363 return i_packet_udp_type;
1368 static int mms_ReceiveCommand( access_t *p_access )
1370 access_sys_t *p_sys = p_access->p_sys;
1377 if( NetFillBuffer( p_access ) < 0 )
1379 msg_Warn( p_access, "cannot fill buffer" );
1380 return VLC_EGENERIC;
1382 if( p_sys->i_buffer_tcp > 0 )
1384 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1385 p_sys->i_buffer_tcp, &i_used );
1386 if( i_used < MMS_BUFFER_SIZE )
1388 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1389 MMS_BUFFER_SIZE - i_used );
1391 p_sys->i_buffer_tcp -= i_used;
1395 return VLC_EGENERIC;
1398 if( p_sys->i_command == 0x1b )
1400 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1409 return VLC_EGENERIC;
1416 #define MMS_RETRY_MAX 10
1417 #define MMS_RETRY_SLEEP 50000
1419 static int mms_CommandRead( access_t *p_access, int i_command1,
1422 access_sys_t *p_sys = p_access->p_sys;
1426 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1428 i_status = mms_ReceiveCommand( p_access );
1429 if( i_status < 0 || p_sys->i_command == 0 )
1432 msleep( MMS_RETRY_SLEEP );
1434 else if( i_command1 == 0 && i_command2 == 0)
1438 else if( p_sys->i_command == i_command1 ||
1439 p_sys->i_command == i_command2 )
1445 switch( p_sys->i_command )
1448 msg_Warn( p_access, "socket closed by server" );
1449 p_access->info.b_eof = 1;
1450 return VLC_EGENERIC;
1452 msg_Warn( p_access, "end of media stream" );
1453 p_access->info.b_eof = 1;
1454 return VLC_EGENERIC;
1460 msg_Warn( p_access, "failed to receive command (aborting)" );
1462 return VLC_EGENERIC;
1466 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1468 access_sys_t *p_sys = p_access->p_sys;
1471 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1475 if( p_access->b_die )
1478 i_status = mms_ReceivePacket( p_access );
1482 msg_Warn( p_access, "cannot receive header (%d/%d)",
1483 i_count, MMS_RETRY_MAX );
1484 msleep( MMS_RETRY_SLEEP );
1486 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1490 else if( i_status == MMS_PACKET_CMD )
1492 switch( p_sys->i_command )
1495 msg_Warn( p_access, "socket closed by server" );
1496 p_access->info.b_eof = 1;
1499 msg_Warn( p_access, "end of media stream" );
1500 p_access->info.b_eof = 1;
1503 /* XXX not too dificult to be done EXCEPT that we
1504 * need to restart demuxer... and I don't see how we
1505 * could do that :p */
1507 "reinitialization needed --> unsupported" );
1508 p_access->info.b_eof = VLC_TRUE;
1516 msg_Err( p_access, "cannot receive %s (aborting)",
1517 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );