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 *****************************************************************************/
31 #include <vlc/input.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
60 /****************************************************************************
62 * MMSProtocole documentation found at http://get.to/sdp
63 ****************************************************************************/
65 /*****************************************************************************
67 *****************************************************************************/
68 int E_( MMSTUOpen ) ( access_t * );
69 void E_( MMSTUClose ) ( access_t * );
72 static int Read( access_t *, uint8_t *, int );
73 static int Seek( access_t *, int64_t );
74 static int Control( access_t *, int, va_list );
76 static int MMSOpen ( access_t *, vlc_url_t *, int );
77 static int MMSStart( access_t *, uint32_t );
78 static int MMSStop ( access_t * );
79 static void MMSClose( access_t * );
82 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
83 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
85 static int mms_HeaderMediaRead( access_t *, int );
87 static int mms_ReceivePacket( access_t * );
91 * XXX DON'T FREE MY MEMORY !!! XXX
95 * Ok, ok, j'le ferai plus...
101 * Vous pourriez signer vos commentaires (m�e si on voit bien qui peut
102 * �rire ce genre de trucs :p), et �rire en anglais, bordel de
106 * Alors la ouai �est fou les gens qui �rivent des commentaires sans les
107 * signer. Ca m�iterait un coup de pied dans le cul � :)
110 int E_(MMSTUOpen)( access_t *p_access )
116 /* Set up p_access */
117 p_access->pf_read = Read;
118 p_access->pf_block = NULL;
119 p_access->pf_control = Control;
120 p_access->pf_seek = Seek;
121 p_access->info.i_update = 0;
122 p_access->info.i_size = 0;
123 p_access->info.i_pos = 0;
124 p_access->info.b_eof = VLC_FALSE;
125 p_access->info.i_title = 0;
126 p_access->info.i_seekpoint = 0;
127 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
128 memset( p_sys, 0, sizeof( access_sys_t ) );
130 /* *** Parse URL and get server addr/port and path *** */
131 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
132 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
134 msg_Err( p_access, "invalid server name" );
135 vlc_UrlClean( &p_sys->url );
138 if( p_sys->url.i_port <= 0 )
140 p_sys->url.i_port = 1755;
143 /* *** connect to this server *** */
144 /* look at requested protocol (udp/tcp) */
145 i_proto = MMS_PROTO_AUTO;
146 if( *p_access->psz_access )
148 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
150 i_proto = MMS_PROTO_UDP;
152 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
154 i_proto = MMS_PROTO_TCP;
159 if( i_proto == MMS_PROTO_AUTO )
160 { /* first try with TCP and then UDP*/
161 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
163 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
168 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
173 msg_Err( p_access, "cannot connect to server" );
174 vlc_UrlClean( &p_sys->url );
178 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
182 * broadcast yy=0x02, xx= 0x00
183 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
185 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
187 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
189 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
191 p_sys->b_seekable = VLC_FALSE;
195 p_sys->b_seekable = VLC_TRUE;
196 p_access->info.i_size =
197 (uint64_t)p_sys->i_header +
198 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
201 /* *** Start stream *** */
202 if( MMSStart( p_access, 0xffffffff ) < 0 )
204 msg_Err( p_access, "cannot start stream" );
205 MMSClose( p_access );
206 vlc_UrlClean( &p_sys->url );
212 /*****************************************************************************
213 * Close: free unused data structures
214 *****************************************************************************/
215 void E_(MMSTUClose)( access_t *p_access )
217 access_sys_t *p_sys = p_access->p_sys;
219 /* close connection with server */
220 MMSClose( p_access );
223 vlc_UrlClean( &p_sys->url );
228 /*****************************************************************************
230 *****************************************************************************/
231 static int Control( access_t *p_access, int i_query, va_list args )
233 access_sys_t *p_sys = p_access->p_sys;
243 case ACCESS_CAN_SEEK:
244 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
245 *pb_bool = p_sys->b_seekable;
248 case ACCESS_CAN_FASTSEEK:
249 case ACCESS_CAN_PAUSE:
250 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
251 *pb_bool = VLC_FALSE;
254 case ACCESS_CAN_CONTROL_PACE:
255 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
257 #if 0 /* Disable for now until we have a clock synchro algo
258 * which works with something else than MPEG over UDP */
259 *pb_bool = VLC_FALSE;
266 pi_int = (int*)va_arg( args, int * );
267 *pi_int = 3 * p_sys->i_packet_length;
270 case ACCESS_GET_PTS_DELAY:
271 pi_64 = (int64_t*)va_arg( args, int64_t * );
272 var_Get( p_access, "mms-caching", &val );
273 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
276 case ACCESS_GET_PRIVATE_ID_STATE:
277 i_int = (int)va_arg( args, int );
278 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
280 if( i_int < 0 || i_int > 127 )
282 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
286 case ACCESS_SET_PAUSE_STATE:
287 case ACCESS_GET_TITLE_INFO:
288 case ACCESS_SET_TITLE:
289 case ACCESS_SET_SEEKPOINT:
290 case ACCESS_SET_PRIVATE_ID_STATE:
295 msg_Warn( p_access, "unimplemented query in control" );
302 /*****************************************************************************
303 * Seek: try to go at the right place
304 *****************************************************************************/
305 static int Seek( access_t * p_access, int64_t i_pos )
307 access_sys_t *p_sys = p_access->p_sys;
315 if( i_pos < p_sys->i_header)
318 if( p_access->info.i_pos < p_sys->i_header )
320 /* no need to restart stream, it was already one
321 * or no stream was yet read */
322 p_access->info.i_pos = i_pos;
327 i_packet = 0xffffffff;
333 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
334 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
336 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
339 msg_Dbg( p_access, "stream stopped (seek)" );
341 /* *** restart stream *** */
342 var_buffer_initwrite( &buffer, 0 );
343 var_buffer_add64( &buffer, 0 ); /* seek point in second */
344 var_buffer_add32( &buffer, 0xffffffff );
345 var_buffer_add32( &buffer, i_packet ); // begin from start
346 var_buffer_add8( &buffer, 0xff ); // stream time limit
347 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
348 var_buffer_add8( &buffer, 0xff ); //
349 var_buffer_add8( &buffer, 0x00 ); // don't use limit
350 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
352 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
353 buffer.p_data, buffer.i_data );
355 var_buffer_free( &buffer );
358 while( !p_access->b_die )
360 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
361 if( p_sys->i_command == 0x1e )
363 msg_Dbg( p_access, "received 0x1e (seek)" );
368 while( !p_access->b_die )
370 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
371 if( p_sys->i_command == 0x05 )
373 msg_Dbg( p_access, "received 0x05 (seek)" );
379 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
380 msg_Dbg( p_access, "Streaming restarted" );
382 p_sys->i_media_used += i_offset;
383 p_access->info.i_pos = i_pos;
384 p_access->info.b_eof = VLC_FALSE;
389 /*****************************************************************************
391 *****************************************************************************/
392 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
394 access_sys_t *p_sys = p_access->p_sys;
400 /* *** now send data if needed *** */
401 while( i_data < (size_t)i_len )
403 if( p_access->info.i_pos < p_sys->i_header )
405 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
406 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
408 p_access->info.i_pos += i_copy;
410 else if( p_sys->i_media_used < p_sys->i_media )
412 i_copy = __MIN( i_len - i_data ,
413 p_sys->i_media - p_sys->i_media_used );
414 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
416 p_sys->i_media_used += i_copy;
417 p_access->info.i_pos += i_copy;
419 else if( p_sys->p_media != NULL &&
420 p_sys->i_media_used < p_sys->i_packet_length )
422 i_copy = __MIN( i_len - i_data,
423 p_sys->i_packet_length - p_sys->i_media_used);
424 memset( &p_buffer[i_data], 0, i_copy );
427 p_sys->i_media_used += i_copy;
428 p_access->info.i_pos += i_copy;
430 else if( p_access->info.b_eof ||
431 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
440 /****************************************************************************
441 * MMSOpen : Open a connection with the server over mmst or mmsu
442 ****************************************************************************/
443 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
445 access_sys_t *p_sys = p_access->p_sys;
446 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
451 int i_server_version;
453 int i_update_player_url;
454 int i_encryption_type;
460 /* *** Open a TCP connection with server *** */
461 msg_Dbg( p_access, "waiting for connection..." );
462 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
463 if( p_sys->i_handle_tcp < 0 )
465 msg_Err( p_access, "failed to open a connection (tcp)" );
469 "connection(tcp) with \"%s:%d\" successful",
473 /* *** Bind port if UDP protocol is selected *** */
476 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
479 net_Close( p_sys->i_handle_tcp );
483 p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->sz_bind_addr,
485 if( p_sys->i_handle_udp < 0 )
487 msg_Err( p_access, "failed to open a connection (udp)" );
488 net_Close( p_sys->i_handle_tcp );
492 "connection(udp) at \"%s:%d\" successful",
493 p_sys->sz_bind_addr, 7000 );
496 /* *** Init context for mms prototcol *** */
497 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
499 "generated guid: "GUID_FMT,
500 GUID_PRINT( p_sys->guid ) );
501 p_sys->i_command_level = 1; /* updated after 0x1A command */
502 p_sys->i_seq_num = 0;
503 p_sys->i_media_packet_id_type = 0x04;
504 p_sys->i_header_packet_id_type = 0x02;
505 p_sys->i_proto = i_proto;
506 p_sys->i_packet_seq_num = 0;
507 p_sys->p_header = NULL;
509 p_sys->p_media = NULL;
511 p_sys->i_media_used = 0;
513 p_access->info.i_pos = 0;
514 p_sys->i_buffer_tcp = 0;
515 p_sys->i_buffer_udp = 0;
518 p_access->info.b_eof = 0;
520 /* *** send command 1 : connection request *** */
521 var_buffer_initwrite( &buffer, 0 );
522 var_buffer_add16( &buffer, 0x001c );
523 var_buffer_add16( &buffer, 0x0003 );
525 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
526 GUID_PRINT( p_sys->guid ),
528 var_buffer_addUTF16( &buffer, tmp );
530 mms_CommandSend( p_access,
531 0x01, /* connexion request */
532 0x00000000, /* flags, FIXME */
533 0x0004000b, /* ???? */
537 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
539 var_buffer_free( &buffer );
540 MMSClose( p_access );
544 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
545 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
546 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
547 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
548 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
549 #define GETUTF16( psz, size ) \
552 psz = malloc( size + 1); \
553 for( i = 0; i < size; i++ ) \
560 GETUTF16( p_sys->psz_server_version, i_server_version );
561 GETUTF16( p_sys->psz_tool_version, i_tool_version );
562 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
563 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
566 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
567 p_sys->psz_server_version,
568 p_sys->psz_tool_version,
569 p_sys->psz_update_player_url,
570 p_sys->psz_encryption_type );
572 /* *** should make an 18 command to make data timing *** */
574 /* *** send command 2 : transport protocol selection *** */
575 var_buffer_reinitwrite( &buffer, 0 );
576 var_buffer_add32( &buffer, 0x00000000 );
577 var_buffer_add32( &buffer, 0x000a0000 );
578 var_buffer_add32( &buffer, 0x00000002 );
588 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
590 var_buffer_addUTF16( &buffer, tmp );
591 var_buffer_add16( &buffer, '0' );
593 mms_CommandSend( p_access,
594 0x02, /* connexion request */
595 0x00000000, /* flags, FIXME */
596 0xffffffff, /* ???? */
600 /* *** response from server, should be 0x02 or 0x03 *** */
601 mms_CommandRead( p_access, 0x02, 0x03 );
602 if( p_sys->i_command == 0x03 )
605 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
606 var_buffer_free( &buffer );
607 MMSClose( p_access );
610 else if( p_sys->i_command != 0x02 )
612 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
615 /* *** send command 5 : media file name/path requested *** */
616 var_buffer_reinitwrite( &buffer, 0 );
617 var_buffer_add64( &buffer, 0 );
618 var_buffer_addUTF16( &buffer, p_url->psz_path );
620 mms_CommandSend( p_access,
622 p_sys->i_command_level,
627 /* *** wait for reponse *** */
628 mms_CommandRead( p_access, 0x1a, 0x06 );
630 /* test if server send 0x1A answer */
631 if( p_sys->i_command == 0x1A )
633 msg_Err( p_access, "id/password requested (not yet supported)" );
635 var_buffer_free( &buffer );
636 MMSClose( p_access );
639 if( p_sys->i_command != 0x06 )
642 "unknown answer (0x%x instead of 0x06)",
644 var_buffer_free( &buffer );
645 MMSClose( p_access );
649 /* 1 for file ok, 2 for authen ok */
650 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
653 msg_Dbg( p_access, "media file name/path accepted" );
656 msg_Dbg( p_access, "authentication accepted" );
660 msg_Err( p_access, "error while asking for file %d",
661 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
662 var_buffer_free( &buffer );
663 MMSClose( p_access );
667 p_sys->i_flags_broadcast =
668 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
669 p_sys->i_media_length =
670 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
671 p_sys->i_packet_length =
672 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
673 p_sys->i_packet_count =
674 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
675 p_sys->i_max_bit_rate =
676 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
677 p_sys->i_header_size =
678 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
681 "answer 0x06 flags:0x%8.8x media_length:%us packet_length:%lu packet_count:%u max_bit_rate:%d header_size:%d",
682 p_sys->i_flags_broadcast,
683 p_sys->i_media_length,
684 p_sys->i_packet_length,
685 p_sys->i_packet_count,
686 p_sys->i_max_bit_rate,
687 p_sys->i_header_size );
689 /* *** send command 15 *** */
691 var_buffer_reinitwrite( &buffer, 0 );
692 var_buffer_add32( &buffer, 0 );
693 var_buffer_add32( &buffer, 0x8000 );
694 var_buffer_add32( &buffer, 0xffffffff );
695 var_buffer_add32( &buffer, 0x00 );
696 var_buffer_add32( &buffer, 0x00 );
697 var_buffer_add32( &buffer, 0x00 );
698 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
699 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
700 var_buffer_add32( &buffer, 0x00 );
701 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
702 buffer.p_data, buffer.i_data );
704 /* *** wait for reponse *** */
705 /* Commented out because it fails on some stream (no 0x11 answer) */
707 mms_CommandRead( p_access, 0x11, 0 );
709 if( p_sys->i_command != 0x11 )
712 "unknown answer (0x%x instead of 0x11)",
714 var_buffer_free( &buffer );
715 MMSClose( p_access );
720 /* *** now read header packet *** */
721 /* XXX could be split over multiples packets */
722 msg_Dbg( p_access, "reading header" );
725 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
727 msg_Err( p_access, "cannot receive header" );
728 var_buffer_free( &buffer );
729 MMSClose( p_access );
732 if( p_sys->i_header >= p_sys->i_header_size )
735 "header complete(%d)",
740 "header incomplete (%d/%d), reading more",
742 p_sys->i_header_size );
745 /* *** parse header and get stream and their id *** */
746 /* get all streams properties,
748 * TODO : stream bitrates properties(optional)
749 * and bitrate mutual exclusion(optional) */
750 E_( asf_HeaderParse )( &p_sys->asfh,
751 p_sys->p_header, p_sys->i_header );
752 E_( asf_StreamSelect)( &p_sys->asfh,
753 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
754 var_CreateGetInteger( p_access, "mms-all" ),
755 var_CreateGetInteger( p_access, "audio" ),
756 var_CreateGetInteger( p_access, "video" ) );
758 /* *** now select stream we want to receive *** */
759 /* TODO take care of stream bitrate TODO */
762 var_buffer_reinitwrite( &buffer, 0 );
763 /* for now, select first audio and video stream */
764 for( i = 1; i < 128; i++ )
767 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
772 var_buffer_add16( &buffer, 0xffff );
773 var_buffer_add16( &buffer, i );
779 if( p_sys->asfh.stream[i].i_selected )
781 var_buffer_add16( &buffer, 0x0000 );
783 "selecting stream[0x%x] %s (%d kb/s)",
785 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
787 p_sys->asfh.stream[i].i_bitrate / 1024);
791 var_buffer_add16( &buffer, 0x0002 );
793 "ignoring stream[0x%x] %s (%d kb/s)",
795 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
797 p_sys->asfh.stream[i].i_bitrate / 1024);
805 msg_Err( p_access, "cannot find any stream" );
806 var_buffer_free( &buffer );
807 MMSClose( p_access );
810 mms_CommandSend( p_access, 0x33,
812 0xffff | ( i_first << 16 ),
813 buffer.p_data, buffer.i_data );
815 mms_CommandRead( p_access, 0x21, 0 );
816 if( p_sys->i_command != 0x21 )
819 "unknown answer (0x%x instead of 0x21)",
821 var_buffer_free( &buffer );
822 MMSClose( p_access );
827 var_buffer_free( &buffer );
829 msg_Info( p_access, "connection successful" );
834 /****************************************************************************
835 * MMSStart : Start streaming
836 ****************************************************************************/
837 static int MMSStart( access_t *p_access, uint32_t i_packet )
839 access_sys_t *p_sys = p_access->p_sys;
842 /* *** start stream from packet 0 *** */
843 var_buffer_initwrite( &buffer, 0 );
844 var_buffer_add64( &buffer, 0 ); /* seek point in second */
845 var_buffer_add32( &buffer, 0xffffffff );
846 var_buffer_add32( &buffer, i_packet ); // begin from start
847 var_buffer_add8( &buffer, 0xff ); // stream time limit
848 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
849 var_buffer_add8( &buffer, 0xff ); //
850 var_buffer_add8( &buffer, 0x00 ); // don't use limit
851 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
853 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
854 buffer.p_data, buffer.i_data );
856 var_buffer_free( &buffer );
858 mms_CommandRead( p_access, 0x05, 0 );
860 if( p_sys->i_command != 0x05 )
863 "unknown answer (0x%x instead of 0x05)",
870 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
871 msg_Dbg( p_access, "streaming started" );
876 /****************************************************************************
877 * MMSStop : Stop streaming
878 ****************************************************************************/
879 static int MMSStop( access_t *p_access )
881 access_sys_t *p_sys = p_access->p_sys;
883 /* *** stop stream but keep connection alive *** */
884 mms_CommandSend( p_access,
886 p_sys->i_command_level,
892 /****************************************************************************
893 * MMSClose : Close streaming and connection
894 ****************************************************************************/
895 static void MMSClose( access_t *p_access )
897 access_sys_t *p_sys = p_access->p_sys;
899 msg_Dbg( p_access, "Connection closed" );
901 /* *** tell server that we will disconnect *** */
902 mms_CommandSend( p_access,
904 p_sys->i_command_level,
908 /* *** close sockets *** */
909 net_Close( p_sys->i_handle_tcp );
910 if( p_sys->i_proto == MMS_PROTO_UDP )
912 net_Close( p_sys->i_handle_udp );
915 FREE( p_sys->p_cmd );
916 FREE( p_sys->p_media );
917 FREE( p_sys->p_header );
919 FREE( p_sys->psz_server_version );
920 FREE( p_sys->psz_tool_version );
921 FREE( p_sys->psz_update_player_url );
922 FREE( p_sys->psz_encryption_type );
925 /****************************************************************************
927 * MMS specific functions
929 ****************************************************************************/
930 static int mms_CommandSend( access_t *p_access, int i_command,
931 uint32_t i_prefix1, uint32_t i_prefix2,
932 uint8_t *p_data, int i_data_old )
935 access_sys_t *p_sys = p_access->p_sys;
936 int i_data_by8, i_ret;
937 int i_data = i_data_old;
939 while( i_data & 0x7 ) i_data++;
940 i_data_by8 = i_data >> 3;
942 /* first init buffer */
943 var_buffer_initwrite( &buffer, 0 );
945 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
946 var_buffer_add32( &buffer, 0xB00BFACE );
947 /* size after protocol type */
948 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
949 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
950 var_buffer_add32( &buffer, i_data_by8 + 4 );
951 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
952 var_buffer_add64( &buffer, 0 );
953 var_buffer_add32( &buffer, i_data_by8 + 2 );
954 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
955 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
956 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
958 /* specific command data */
959 if( p_data && i_data > 0 )
961 var_buffer_addmemory( &buffer, p_data, i_data_old );
964 /* Append padding to the command data */
965 var_buffer_add64( &buffer, 0 );
968 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
969 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
970 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
972 msg_Err( p_access, "failed to send command" );
976 var_buffer_free( &buffer );
980 static int NetFillBuffer( access_t *p_access )
986 access_sys_t *p_sys = p_access->p_sys;
987 struct timeval timeout;
991 /* FIXME when using udp */
992 ssize_t i_tcp, i_udp;
993 ssize_t i_tcp_read, i_udp_read;
997 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
999 if( p_sys->i_proto == MMS_PROTO_UDP )
1001 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1005 i_udp = 0; /* there isn't udp socket */
1011 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1013 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1015 if( i_handle_max == 0 )
1017 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1022 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1025 /* Find if some data is available */
1030 /* Initialize file descriptor set */
1036 FD_SET( p_sys->i_handle_tcp, &fds_r );
1037 FD_SET( p_sys->i_handle_tcp, &fds_e );
1041 FD_SET( p_sys->i_handle_udp, &fds_r );
1042 FD_SET( p_sys->i_handle_udp, &fds_e );
1045 /* We'll wait 0.5 second if nothing happens */
1047 timeout.tv_usec = 500000;
1049 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1054 if( p_access->b_die || p_access->b_error ) return -1;
1056 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1058 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1059 (i_ret < 0 && errno == EINTR) );
1063 msg_Err( p_access, "network select error (%s)", strerror(errno) );
1067 i_tcp_read = i_udp_read = 0;
1069 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1072 recv( p_sys->i_handle_tcp,
1073 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1074 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1077 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1079 i_udp_read = recv( p_sys->i_handle_udp,
1080 p_sys->buffer_udp + p_sys->i_buffer_udp,
1081 i_udp + MMS_BUFFER_SIZE/2, 0 );
1085 if( p_sys->i_proto == MMS_PROTO_UDP )
1087 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1088 p_sys->i_buffer_tcp, i_tcp_read,
1089 p_sys->i_buffer_udp, i_udp_read );
1093 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1094 p_sys->i_buffer_tcp, i_tcp_read );
1098 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1099 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1101 return i_tcp_read + i_udp_read;
1105 static int mms_ParseCommand( access_t *p_access,
1110 #define GET32( i_pos ) \
1111 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1112 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1113 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1115 access_sys_t *p_sys = p_access->p_sys;
1121 free( p_sys->p_cmd );
1123 p_sys->i_cmd = i_data;
1124 p_sys->p_cmd = malloc( i_data );
1125 memcpy( p_sys->p_cmd, p_data, i_data );
1127 *pi_used = i_data; /* by default */
1129 if( i_data < MMS_CMD_HEADERSIZE )
1131 msg_Warn( p_access, "truncated command (header incomplete)" );
1132 p_sys->i_command = 0;
1135 i_id = GetDWLE( p_data + 4 );
1136 i_length = GetDWLE( p_data + 8 ) + 16;
1138 if( i_id != 0xb00bface )
1141 "incorrect command header (0x%x)", i_id );
1142 p_sys->i_command = 0;
1146 if( i_length > p_sys->i_cmd )
1149 "truncated command (missing %d bytes)",
1150 i_length - i_data );
1151 p_sys->i_command = 0;
1154 else if( i_length < p_sys->i_cmd )
1156 p_sys->i_cmd = i_length;
1157 *pi_used = i_length;
1161 "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",
1165 /* 12: protocol type "MMS " */
1168 /* 24: unknown (0) */
1169 /* 28: unknown (0) */
1175 p_sys->i_command = GET32( 36 ) & 0xffff;
1178 return MMS_PACKET_CMD;
1181 static int mms_ParsePacket( access_t *p_access,
1182 uint8_t *p_data, size_t i_data,
1185 access_sys_t *p_sys = p_access->p_sys;
1186 int i_packet_seq_num;
1187 size_t i_packet_length;
1188 uint32_t i_packet_id;
1193 *pi_used = i_data; /* default */
1196 msg_Warn( p_access, "truncated packet (header incomplete)" );
1200 i_packet_id = p_data[4];
1201 i_packet_seq_num = GetDWLE( p_data );
1202 i_packet_length = GetWLE( p_data + 6 );
1204 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1206 if( i_packet_length > i_data || i_packet_length <= 8)
1208 /* msg_Dbg( p_access,
1209 "truncated packet (Declared %d bytes, Actual %d bytes)",
1210 i_packet_length, i_data ); */
1214 else if( i_packet_length < i_data )
1216 *pi_used = i_packet_length;
1219 if( i_packet_id == 0xff )
1222 "receive MMS UDP pair timing" );
1223 return( MMS_PACKET_UDP_TIMING );
1226 if( i_packet_id != p_sys->i_header_packet_id_type &&
1227 i_packet_id != p_sys->i_media_packet_id_type )
1229 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1233 /* we now have a media or a header packet */
1234 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1235 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1237 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1239 /* FIXME for udp could be just wrong order ? */
1241 "detected packet lost (%d != %d)",
1243 p_sys->i_packet_seq_num );
1244 p_sys->i_packet_seq_num = i_packet_seq_num;
1246 p_sys->i_packet_seq_num++;
1248 if( i_packet_id == p_sys->i_header_packet_id_type )
1250 if( p_sys->p_header )
1252 p_sys->p_header = realloc( p_sys->p_header,
1253 p_sys->i_header + i_packet_length - 8 );
1254 memcpy( &p_sys->p_header[p_sys->i_header],
1256 i_packet_length - 8 );
1257 p_sys->i_header += i_packet_length - 8;
1263 p_sys->p_header = p_packet;
1264 p_sys->i_header = i_packet_length - 8;
1266 /* msg_Dbg( p_access,
1267 "receive header packet (%d bytes)",
1268 i_packet_length - 8 ); */
1270 return MMS_PACKET_HEADER;
1274 FREE( p_sys->p_media );
1275 p_sys->p_media = p_packet;
1276 p_sys->i_media = i_packet_length - 8;
1277 p_sys->i_media_used = 0;
1278 /* msg_Dbg( p_access,
1279 "receive media packet (%d bytes)",
1280 i_packet_length - 8 ); */
1282 return MMS_PACKET_MEDIA;
1286 static int mms_ReceivePacket( access_t *p_access )
1288 access_sys_t *p_sys = p_access->p_sys;
1289 int i_packet_tcp_type;
1290 int i_packet_udp_type;
1294 vlc_bool_t b_refill = VLC_TRUE;
1296 /* first if we need to refill buffer */
1297 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1299 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1301 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1302 (uint32_t)p_sys->i_buffer_tcp )
1304 b_refill = VLC_FALSE;
1307 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1309 b_refill = VLC_FALSE;
1312 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1313 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1315 b_refill = VLC_FALSE;
1318 if( b_refill && NetFillBuffer( p_access ) < 0 )
1320 msg_Warn( p_access, "cannot fill buffer" );
1324 i_packet_tcp_type = -1;
1325 i_packet_udp_type = -1;
1327 if( p_sys->i_buffer_tcp > 0 )
1331 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1334 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1335 p_sys->i_buffer_tcp, &i_used );
1341 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1342 p_sys->i_buffer_tcp, &i_used );
1344 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1346 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1347 MMS_BUFFER_SIZE - i_used );
1349 p_sys->i_buffer_tcp -= i_used;
1351 else if( p_sys->i_buffer_udp > 0 )
1356 mms_ParsePacket( p_access, p_sys->buffer_udp,
1357 p_sys->i_buffer_udp, &i_used );
1359 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1361 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1362 MMS_BUFFER_SIZE - i_used );
1364 p_sys->i_buffer_udp -= i_used;
1367 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1369 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1370 i_packet_tcp_type = -1;
1373 if( i_packet_tcp_type != -1 )
1375 return i_packet_tcp_type;
1377 else if( i_packet_udp_type != -1 )
1379 return i_packet_udp_type;
1384 static int mms_ReceiveCommand( access_t *p_access )
1386 access_sys_t *p_sys = p_access->p_sys;
1393 if( NetFillBuffer( p_access ) < 0 )
1395 msg_Warn( p_access, "cannot fill buffer" );
1396 return VLC_EGENERIC;
1398 if( p_sys->i_buffer_tcp > 0 )
1400 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1401 p_sys->i_buffer_tcp, &i_used );
1402 if( i_used < MMS_BUFFER_SIZE )
1404 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1405 MMS_BUFFER_SIZE - i_used );
1407 p_sys->i_buffer_tcp -= i_used;
1411 return VLC_EGENERIC;
1414 if( p_sys->i_command == 0x1b )
1416 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1425 return VLC_EGENERIC;
1432 #define MMS_RETRY_MAX 10
1433 #define MMS_RETRY_SLEEP 50000
1435 static int mms_CommandRead( access_t *p_access, int i_command1,
1438 access_sys_t *p_sys = p_access->p_sys;
1442 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1444 i_status = mms_ReceiveCommand( p_access );
1445 if( i_status < 0 || p_sys->i_command == 0 )
1448 msleep( MMS_RETRY_SLEEP );
1450 else if( i_command1 == 0 && i_command2 == 0)
1454 else if( p_sys->i_command == i_command1 ||
1455 p_sys->i_command == i_command2 )
1461 switch( p_sys->i_command )
1464 msg_Warn( p_access, "socket closed by server" );
1465 p_access->info.b_eof = 1;
1466 return VLC_EGENERIC;
1468 msg_Warn( p_access, "end of media stream" );
1469 p_access->info.b_eof = 1;
1470 return VLC_EGENERIC;
1476 msg_Warn( p_access, "failed to receive command (aborting)" );
1478 return VLC_EGENERIC;
1482 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1484 access_sys_t *p_sys = p_access->p_sys;
1487 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1491 if( p_access->b_die )
1494 i_status = mms_ReceivePacket( p_access );
1498 msg_Warn( p_access, "cannot receive header (%d/%d)",
1499 i_count, MMS_RETRY_MAX );
1500 msleep( MMS_RETRY_SLEEP );
1502 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1506 else if( i_status == MMS_PACKET_CMD )
1508 switch( p_sys->i_command )
1511 msg_Warn( p_access, "socket closed by server" );
1512 p_access->info.b_eof = 1;
1515 msg_Warn( p_access, "end of media stream" );
1516 p_access->info.b_eof = 1;
1519 /* XXX not too dificult to be done EXCEPT that we
1520 * need to restart demuxer... and I don't see how we
1521 * could do that :p */
1523 "reinitialization needed --> unsupported" );
1524 p_access->info.b_eof = VLC_TRUE;
1532 msg_Err( p_access, "cannot receive %s (aborting)",
1533 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );