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., 59 Temple Place - Suite 330, Boston, MA 02111, 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>
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 * );
90 * XXX DON'T FREE MY MEMORY !!! XXX
94 * Ok, ok, j'le ferai plus...
100 * Vous pourriez signer vos commentaires (même si on voit bien qui peut
101 * écrire ce genre de trucs :p), et écrire en anglais, bordel de
105 * Alors la ouai ç'est fou les gens qui écrivent des commentaires sans les
106 * signer. Ca mériterait un coup de pied dans le cul ça :)
109 int E_(MMSTUOpen)( access_t *p_access )
115 /* Set up p_access */
116 p_access->pf_read = Read;
117 p_access->pf_block = NULL;
118 p_access->pf_control = Control;
119 p_access->pf_seek = Seek;
120 p_access->info.i_update = 0;
121 p_access->info.i_size = 0;
122 p_access->info.i_pos = 0;
123 p_access->info.b_eof = VLC_FALSE;
124 p_access->info.i_title = 0;
125 p_access->info.i_seekpoint = 0;
126 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
127 memset( p_sys, 0, sizeof( access_sys_t ) );
129 /* *** Parse URL and get server addr/port and path *** */
130 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
131 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
133 msg_Err( p_access, "invalid server name" );
134 vlc_UrlClean( &p_sys->url );
137 if( p_sys->url.i_port <= 0 )
139 p_sys->url.i_port = 1755;
142 /* *** connect to this server *** */
143 /* look at requested protocol (udp/tcp) */
144 i_proto = MMS_PROTO_AUTO;
145 if( *p_access->psz_access )
147 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
149 i_proto = MMS_PROTO_UDP;
151 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
153 i_proto = MMS_PROTO_TCP;
158 if( i_proto == MMS_PROTO_AUTO )
159 { /* first try with TCP and then UDP*/
160 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
162 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
167 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
172 msg_Err( p_access, "cannot connect to server" );
173 vlc_UrlClean( &p_sys->url );
177 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
181 * broadcast yy=0x02, xx= 0x00
182 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
184 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
186 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
188 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
190 p_sys->b_seekable = VLC_FALSE;
194 p_sys->b_seekable = VLC_TRUE;
195 p_access->info.i_size =
196 (uint64_t)p_sys->i_header +
197 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
200 /* *** Start stream *** */
201 if( MMSStart( p_access, 0xffffffff ) < 0 )
203 msg_Err( p_access, "cannot start stream" );
204 MMSClose( p_access );
205 vlc_UrlClean( &p_sys->url );
211 /*****************************************************************************
212 * Close: free unused data structures
213 *****************************************************************************/
214 void E_(MMSTUClose)( access_t *p_access )
216 access_sys_t *p_sys = p_access->p_sys;
218 /* close connection with server */
219 MMSClose( p_access );
222 vlc_UrlClean( &p_sys->url );
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Control( access_t *p_access, int i_query, va_list args )
232 access_sys_t *p_sys = p_access->p_sys;
242 case ACCESS_CAN_SEEK:
243 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
244 *pb_bool = p_sys->b_seekable;
247 case ACCESS_CAN_FASTSEEK:
248 case ACCESS_CAN_PAUSE:
249 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
250 *pb_bool = VLC_FALSE;
253 case ACCESS_CAN_CONTROL_PACE:
254 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
256 #if 0 /* Disable for now until we have a clock synchro algo
257 * which works with something else than MPEG over UDP */
258 *pb_bool = VLC_FALSE;
265 pi_int = (int*)va_arg( args, int * );
266 *pi_int = 3 * p_sys->i_packet_length;
269 case ACCESS_GET_PTS_DELAY:
270 pi_64 = (int64_t*)va_arg( args, int64_t * );
271 var_Get( p_access, "mms-caching", &val );
272 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
275 case ACCESS_GET_PRIVATE_ID_STATE:
276 i_int = (int)va_arg( args, int );
277 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
279 if( i_int < 0 || i_int > 127 )
281 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
285 case ACCESS_SET_PAUSE_STATE:
286 case ACCESS_GET_TITLE_INFO:
287 case ACCESS_SET_TITLE:
288 case ACCESS_SET_SEEKPOINT:
289 case ACCESS_SET_PRIVATE_ID_STATE:
294 msg_Warn( p_access, "unimplemented query in control" );
301 /*****************************************************************************
302 * Seek: try to go at the right place
303 *****************************************************************************/
304 static int Seek( access_t * p_access, int64_t i_pos )
306 access_sys_t *p_sys = p_access->p_sys;
314 if( i_pos < p_sys->i_header)
317 if( p_access->info.i_pos < p_sys->i_header )
319 /* no need to restart stream, it was already one
320 * or no stream was yet read */
321 p_access->info.i_pos = i_pos;
326 i_packet = 0xffffffff;
332 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
333 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
335 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
338 msg_Dbg( p_access, "stream stopped (seek)" );
340 /* *** restart stream *** */
341 var_buffer_initwrite( &buffer, 0 );
342 var_buffer_add64( &buffer, 0 ); /* seek point in second */
343 var_buffer_add32( &buffer, 0xffffffff );
344 var_buffer_add32( &buffer, i_packet ); // begin from start
345 var_buffer_add8( &buffer, 0xff ); // stream time limit
346 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
347 var_buffer_add8( &buffer, 0xff ); //
348 var_buffer_add8( &buffer, 0x00 ); // don't use limit
349 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
351 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
352 buffer.p_data, buffer.i_data );
354 var_buffer_free( &buffer );
357 while( !p_access->b_die )
359 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
360 if( p_sys->i_command == 0x1e )
362 msg_Dbg( p_access, "received 0x1e (seek)" );
367 while( !p_access->b_die )
369 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
370 if( p_sys->i_command == 0x05 )
372 msg_Dbg( p_access, "received 0x05 (seek)" );
378 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
379 msg_Dbg( p_access, "Streaming restarted" );
381 p_sys->i_media_used += i_offset;
382 p_access->info.i_pos = i_pos;
383 p_access->info.b_eof = VLC_FALSE;
388 /*****************************************************************************
390 *****************************************************************************/
391 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
393 access_sys_t *p_sys = p_access->p_sys;
399 /* *** now send data if needed *** */
400 while( i_data < (size_t)i_len )
402 if( p_access->info.i_pos < p_sys->i_header )
404 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
405 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
407 p_access->info.i_pos += i_copy;
409 else if( p_sys->i_media_used < p_sys->i_media )
411 i_copy = __MIN( i_len - i_data ,
412 p_sys->i_media - p_sys->i_media_used );
413 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
415 p_sys->i_media_used += i_copy;
416 p_access->info.i_pos += i_copy;
418 else if( p_sys->p_media != NULL &&
419 p_sys->i_media_used < p_sys->i_packet_length )
421 i_copy = __MIN( i_len - i_data,
422 p_sys->i_packet_length - p_sys->i_media_used);
423 memset( &p_buffer[i_data], 0, i_copy );
426 p_sys->i_media_used += i_copy;
427 p_access->info.i_pos += i_copy;
429 else if( p_access->info.b_eof ||
430 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
439 /****************************************************************************
440 * MMSOpen : Open a connection with the server over mmst or mmsu
441 ****************************************************************************/
442 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
444 access_sys_t *p_sys = p_access->p_sys;
445 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
450 int i_server_version;
452 int i_update_player_url;
453 int i_encryption_type;
459 /* *** Open a TCP connection with server *** */
460 msg_Dbg( p_access, "waiting for connection..." );
461 p_sys->i_handle_tcp = net_OpenTCP( p_access, p_url->psz_host, p_url->i_port );
462 if( p_sys->i_handle_tcp < 0 )
464 msg_Err( p_access, "failed to open a connection (tcp)" );
468 "connection(tcp) with \"%s:%d\" successful",
472 /* *** Bind port if UDP protocol is selected *** */
475 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
478 net_Close( p_sys->i_handle_tcp );
482 p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->sz_bind_addr,
484 if( p_sys->i_handle_udp < 0 )
486 msg_Err( p_access, "failed to open a connection (udp)" );
487 net_Close( p_sys->i_handle_tcp );
491 "connection(udp) at \"%s:%d\" successful",
492 p_sys->sz_bind_addr, 7000 );
495 /* *** Init context for mms prototcol *** */
496 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
498 "generated guid: "GUID_FMT,
499 GUID_PRINT( p_sys->guid ) );
500 p_sys->i_command_level = 1; /* updated after 0x1A command */
501 p_sys->i_seq_num = 0;
502 p_sys->i_media_packet_id_type = 0x04;
503 p_sys->i_header_packet_id_type = 0x02;
504 p_sys->i_proto = i_proto;
505 p_sys->i_packet_seq_num = 0;
506 p_sys->p_header = NULL;
508 p_sys->p_media = NULL;
510 p_sys->i_media_used = 0;
512 p_access->info.i_pos = 0;
513 p_sys->i_buffer_tcp = 0;
514 p_sys->i_buffer_udp = 0;
517 p_access->info.b_eof = 0;
519 /* *** send command 1 : connection request *** */
520 var_buffer_initwrite( &buffer, 0 );
521 var_buffer_add16( &buffer, 0x001c );
522 var_buffer_add16( &buffer, 0x0003 );
524 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
525 GUID_PRINT( p_sys->guid ),
527 var_buffer_addUTF16( &buffer, tmp );
529 mms_CommandSend( p_access,
530 0x01, /* connexion request */
531 0x00000000, /* flags, FIXME */
532 0x0004000b, /* ???? */
536 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
538 var_buffer_free( &buffer );
539 MMSClose( p_access );
543 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
544 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
545 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
546 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
547 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
548 #define GETUTF16( psz, size ) \
551 psz = malloc( size + 1); \
552 for( i = 0; i < size; i++ ) \
559 GETUTF16( p_sys->psz_server_version, i_server_version );
560 GETUTF16( p_sys->psz_tool_version, i_tool_version );
561 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
562 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
565 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
566 p_sys->psz_server_version,
567 p_sys->psz_tool_version,
568 p_sys->psz_update_player_url,
569 p_sys->psz_encryption_type );
571 /* *** should make an 18 command to make data timing *** */
573 /* *** send command 2 : transport protocol selection *** */
574 var_buffer_reinitwrite( &buffer, 0 );
575 var_buffer_add32( &buffer, 0x00000000 );
576 var_buffer_add32( &buffer, 0x000a0000 );
577 var_buffer_add32( &buffer, 0x00000002 );
587 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
589 var_buffer_addUTF16( &buffer, tmp );
590 var_buffer_add16( &buffer, '0' );
592 mms_CommandSend( p_access,
593 0x02, /* connexion request */
594 0x00000000, /* flags, FIXME */
595 0xffffffff, /* ???? */
599 /* *** response from server, should be 0x02 or 0x03 *** */
600 mms_CommandRead( p_access, 0x02, 0x03 );
601 if( p_sys->i_command == 0x03 )
604 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
605 var_buffer_free( &buffer );
606 MMSClose( p_access );
609 else if( p_sys->i_command != 0x02 )
611 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
614 /* *** send command 5 : media file name/path requested *** */
615 var_buffer_reinitwrite( &buffer, 0 );
616 var_buffer_add64( &buffer, 0 );
617 var_buffer_addUTF16( &buffer, p_url->psz_path );
619 mms_CommandSend( p_access,
621 p_sys->i_command_level,
626 /* *** wait for reponse *** */
627 mms_CommandRead( p_access, 0x1a, 0x06 );
629 /* test if server send 0x1A answer */
630 if( p_sys->i_command == 0x1A )
632 msg_Err( p_access, "id/password requested (not yet supported)" );
634 var_buffer_free( &buffer );
635 MMSClose( p_access );
638 if( p_sys->i_command != 0x06 )
641 "unknown answer (0x%x instead of 0x06)",
643 var_buffer_free( &buffer );
644 MMSClose( p_access );
648 /* 1 for file ok, 2 for authen ok */
649 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
652 msg_Dbg( p_access, "Media file name/path accepted" );
655 msg_Dbg( p_access, "Authentication accepted" );
659 msg_Err( p_access, "error while asking for file %d",
660 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
661 var_buffer_free( &buffer );
662 MMSClose( p_access );
666 p_sys->i_flags_broadcast =
667 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
668 p_sys->i_media_length =
669 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
670 p_sys->i_packet_length =
671 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
672 p_sys->i_packet_count =
673 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
674 p_sys->i_max_bit_rate =
675 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
676 p_sys->i_header_size =
677 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
680 "answer 0x06 flags:0x%8.8x media_length:%ds packet_length:%d packet_count:%d max_bit_rate:%d header_size:%d",
681 p_sys->i_flags_broadcast,
682 p_sys->i_media_length,
683 p_sys->i_packet_length,
684 p_sys->i_packet_count,
685 p_sys->i_max_bit_rate,
686 p_sys->i_header_size );
688 /* *** send command 15 *** */
690 var_buffer_reinitwrite( &buffer, 0 );
691 var_buffer_add32( &buffer, 0 );
692 var_buffer_add32( &buffer, 0x8000 );
693 var_buffer_add32( &buffer, 0xffffffff );
694 var_buffer_add32( &buffer, 0x00 );
695 var_buffer_add32( &buffer, 0x00 );
696 var_buffer_add32( &buffer, 0x00 );
697 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
698 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
699 var_buffer_add32( &buffer, 0x00 );
700 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
701 buffer.p_data, buffer.i_data );
703 /* *** wait for reponse *** */
704 /* Commented out because it fails on some stream (no 0x11 answer) */
706 mms_CommandRead( p_access, 0x11, 0 );
708 if( p_sys->i_command != 0x11 )
711 "unknown answer (0x%x instead of 0x11)",
713 var_buffer_free( &buffer );
714 MMSClose( p_access );
719 /* *** now read header packet *** */
720 /* XXX could be split over multiples packets */
721 msg_Dbg( p_access, "reading header" );
724 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
726 msg_Err( p_access, "cannot receive header" );
727 var_buffer_free( &buffer );
728 MMSClose( p_access );
731 if( p_sys->i_header >= p_sys->i_header_size )
734 "header complete(%d)",
739 "header incomplete (%d/%d), reading more",
741 p_sys->i_header_size );
744 /* *** parse header and get stream and their id *** */
745 /* get all streams properties,
747 * TODO : stream bitrates properties(optional)
748 * and bitrate mutual exclusion(optional) */
749 E_( asf_HeaderParse )( &p_sys->asfh,
750 p_sys->p_header, p_sys->i_header );
751 E_( asf_StreamSelect)( &p_sys->asfh,
752 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
753 var_CreateGetInteger( p_access, "mms-all" ),
754 var_CreateGetInteger( p_access, "audio" ),
755 var_CreateGetInteger( p_access, "video" ) );
757 /* *** now select stream we want to receive *** */
758 /* TODO take care of stream bitrate TODO */
761 var_buffer_reinitwrite( &buffer, 0 );
762 /* for now, select first audio and video stream */
763 for( i = 1; i < 128; i++ )
766 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
771 var_buffer_add16( &buffer, 0xffff );
772 var_buffer_add16( &buffer, i );
778 if( p_sys->asfh.stream[i].i_selected )
780 var_buffer_add16( &buffer, 0x0000 );
782 "selecting stream[0x%x] %s (%d kb/s)",
784 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
786 p_sys->asfh.stream[i].i_bitrate / 1024);
790 var_buffer_add16( &buffer, 0x0002 );
792 "ignoring stream[0x%x] %s (%d kb/s)",
794 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
796 p_sys->asfh.stream[i].i_bitrate / 1024);
804 msg_Err( p_access, "cannot find any stream" );
805 var_buffer_free( &buffer );
806 MMSClose( p_access );
809 mms_CommandSend( p_access, 0x33,
811 0xffff | ( i_first << 16 ),
812 buffer.p_data, buffer.i_data );
814 mms_CommandRead( p_access, 0x21, 0 );
815 if( p_sys->i_command != 0x21 )
818 "unknown answer (0x%x instead of 0x21)",
820 var_buffer_free( &buffer );
821 MMSClose( p_access );
826 var_buffer_free( &buffer );
828 msg_Info( p_access, "connection successful" );
833 /****************************************************************************
834 * MMSStart : Start streaming
835 ****************************************************************************/
836 static int MMSStart( access_t *p_access, uint32_t i_packet )
838 access_sys_t *p_sys = p_access->p_sys;
841 /* *** start stream from packet 0 *** */
842 var_buffer_initwrite( &buffer, 0 );
843 var_buffer_add64( &buffer, 0 ); /* seek point in second */
844 var_buffer_add32( &buffer, 0xffffffff );
845 var_buffer_add32( &buffer, i_packet ); // begin from start
846 var_buffer_add8( &buffer, 0xff ); // stream time limit
847 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
848 var_buffer_add8( &buffer, 0xff ); //
849 var_buffer_add8( &buffer, 0x00 ); // don't use limit
850 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
852 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
853 buffer.p_data, buffer.i_data );
855 var_buffer_free( &buffer );
857 mms_CommandRead( p_access, 0x05, 0 );
859 if( p_sys->i_command != 0x05 )
862 "unknown answer (0x%x instead of 0x05)",
869 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
870 msg_Dbg( p_access, "Streaming started" );
875 /****************************************************************************
876 * MMSStop : Stop streaming
877 ****************************************************************************/
878 static int MMSStop( access_t *p_access )
880 access_sys_t *p_sys = p_access->p_sys;
882 /* *** stop stream but keep connection alive *** */
883 mms_CommandSend( p_access,
885 p_sys->i_command_level,
891 /****************************************************************************
892 * MMSClose : Close streaming and connection
893 ****************************************************************************/
894 static void MMSClose( access_t *p_access )
896 access_sys_t *p_sys = p_access->p_sys;
898 msg_Dbg( p_access, "Connection closed" );
900 /* *** tell server that we will disconnect *** */
901 mms_CommandSend( p_access,
903 p_sys->i_command_level,
907 /* *** close sockets *** */
908 net_Close( p_sys->i_handle_tcp );
909 if( p_sys->i_proto == MMS_PROTO_UDP )
911 net_Close( p_sys->i_handle_udp );
914 FREE( p_sys->p_cmd );
915 FREE( p_sys->p_media );
916 FREE( p_sys->p_header );
918 FREE( p_sys->psz_server_version );
919 FREE( p_sys->psz_tool_version );
920 FREE( p_sys->psz_update_player_url );
921 FREE( p_sys->psz_encryption_type );
924 /****************************************************************************
926 * MMS specific functions
928 ****************************************************************************/
929 static int mms_CommandSend( access_t *p_access,
931 uint32_t i_prefix1, uint32_t i_prefix2,
932 uint8_t *p_data, int i_data_old )
936 access_sys_t *p_sys = p_access->p_sys;
938 int i_data = i_data_old;
940 while( i_data & 0x7 ) i_data++;
941 i_data_by8 = i_data >> 3;
943 /* first init buffer */
944 var_buffer_initwrite( &buffer, 0 );
946 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
947 var_buffer_add32( &buffer, 0xB00BFACE );
948 /* size after protocol type */
949 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
950 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
951 var_buffer_add32( &buffer, i_data_by8 + 4 );
952 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
953 var_buffer_add64( &buffer, 0 );
954 var_buffer_add32( &buffer, i_data_by8 + 2 );
955 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
956 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
957 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
959 /* specific command data */
960 if( p_data && i_data > 0 )
962 var_buffer_addmemory( &buffer, p_data, i_data_old );
965 /* Append padding to the command data */
966 var_buffer_add64( &buffer, 0 );
969 if( send( p_sys->i_handle_tcp,
971 buffer.i_data - ( 8 - ( i_data - i_data_old ) ),
974 msg_Err( p_access, "failed to send command" );
978 var_buffer_free( &buffer );
982 static int NetFillBuffer( access_t *p_access )
988 access_sys_t *p_sys = p_access->p_sys;
989 struct timeval timeout;
993 /* FIXME when using udp */
994 ssize_t i_tcp, i_udp;
995 ssize_t i_tcp_read, i_udp_read;
999 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1001 if( p_sys->i_proto == MMS_PROTO_UDP )
1003 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1007 i_udp = 0; /* there isn't udp socket */
1013 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1015 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1017 if( i_handle_max == 0 )
1019 msg_Warn( p_access, "nothing to read %d:%d", i_tcp, i_udp );
1024 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1027 /* Find if some data is available */
1032 /* Initialize file descriptor set */
1038 FD_SET( p_sys->i_handle_tcp, &fds_r );
1039 FD_SET( p_sys->i_handle_tcp, &fds_e );
1043 FD_SET( p_sys->i_handle_udp, &fds_r );
1044 FD_SET( p_sys->i_handle_udp, &fds_e );
1047 /* We'll wait 0.5 second if nothing happens */
1049 timeout.tv_usec = 500000;
1051 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1056 if( p_access->b_die || p_access->b_error ) return -1;
1058 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1060 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1061 (i_ret < 0 && errno == EINTR) );
1065 msg_Err( p_access, "network select error (%s)", strerror(errno) );
1069 i_tcp_read = i_udp_read = 0;
1071 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1074 recv( p_sys->i_handle_tcp,
1075 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1076 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1079 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1081 i_udp_read = recv( p_sys->i_handle_udp,
1082 p_sys->buffer_udp + p_sys->i_buffer_udp,
1083 i_udp + MMS_BUFFER_SIZE/2, 0 );
1087 if( p_sys->i_proto == MMS_PROTO_UDP )
1089 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1090 p_sys->i_buffer_tcp, i_tcp_read,
1091 p_sys->i_buffer_udp, i_udp_read );
1095 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1096 p_sys->i_buffer_tcp, i_tcp_read );
1100 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1101 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1103 return i_tcp_read + i_udp_read;
1107 static int mms_ParseCommand( access_t *p_access,
1112 #define GET32( i_pos ) \
1113 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1114 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1115 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1117 access_sys_t *p_sys = p_access->p_sys;
1123 free( p_sys->p_cmd );
1125 p_sys->i_cmd = i_data;
1126 p_sys->p_cmd = malloc( i_data );
1127 memcpy( p_sys->p_cmd, p_data, i_data );
1129 *pi_used = i_data; /* by default */
1131 if( i_data < MMS_CMD_HEADERSIZE )
1133 msg_Warn( p_access, "truncated command (header incomplete)" );
1134 p_sys->i_command = 0;
1137 i_id = GetDWLE( p_data + 4 );
1138 i_length = GetDWLE( p_data + 8 ) + 16;
1140 if( i_id != 0xb00bface )
1143 "incorrect command header (0x%x)", i_id );
1144 p_sys->i_command = 0;
1148 if( i_length > p_sys->i_cmd )
1151 "truncated command (missing %d bytes)",
1152 i_length - i_data );
1153 p_sys->i_command = 0;
1156 else if( i_length < p_sys->i_cmd )
1158 p_sys->i_cmd = i_length;
1159 *pi_used = i_length;
1163 "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",
1167 /* 12: protocol type "MMS " */
1170 /* 24: unknown (0) */
1171 /* 28: unknown (0) */
1177 p_sys->i_command = GET32( 36 ) & 0xffff;
1180 return MMS_PACKET_CMD;
1183 static int mms_ParsePacket( access_t *p_access,
1184 uint8_t *p_data, size_t i_data,
1187 access_sys_t *p_sys = p_access->p_sys;
1188 int i_packet_seq_num;
1189 size_t i_packet_length;
1190 uint32_t i_packet_id;
1195 *pi_used = i_data; /* default */
1198 msg_Warn( p_access, "truncated packet (header incomplete)" );
1202 i_packet_id = p_data[4];
1203 i_packet_seq_num = GetDWLE( p_data );
1204 i_packet_length = GetWLE( p_data + 6 );
1206 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1208 if( i_packet_length > i_data || i_packet_length <= 8)
1210 /* msg_Dbg( p_access,
1211 "truncated packet (Declared %d bytes, Actual %d bytes)",
1212 i_packet_length, i_data ); */
1216 else if( i_packet_length < i_data )
1218 *pi_used = i_packet_length;
1221 if( i_packet_id == 0xff )
1224 "receive MMS UDP pair timing" );
1225 return( MMS_PACKET_UDP_TIMING );
1228 if( i_packet_id != p_sys->i_header_packet_id_type &&
1229 i_packet_id != p_sys->i_media_packet_id_type )
1231 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1235 /* we now have a media or a header packet */
1236 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1237 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1239 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1241 /* FIXME for udp could be just wrong order ? */
1243 "detected packet lost (%d != %d)",
1245 p_sys->i_packet_seq_num );
1246 p_sys->i_packet_seq_num = i_packet_seq_num;
1248 p_sys->i_packet_seq_num++;
1250 if( i_packet_id == p_sys->i_header_packet_id_type )
1252 if( p_sys->p_header )
1254 p_sys->p_header = realloc( p_sys->p_header,
1255 p_sys->i_header + i_packet_length - 8 );
1256 memcpy( &p_sys->p_header[p_sys->i_header],
1258 i_packet_length - 8 );
1259 p_sys->i_header += i_packet_length - 8;
1265 p_sys->p_header = p_packet;
1266 p_sys->i_header = i_packet_length - 8;
1268 /* msg_Dbg( p_access,
1269 "receive header packet (%d bytes)",
1270 i_packet_length - 8 ); */
1272 return MMS_PACKET_HEADER;
1276 FREE( p_sys->p_media );
1277 p_sys->p_media = p_packet;
1278 p_sys->i_media = i_packet_length - 8;
1279 p_sys->i_media_used = 0;
1280 /* msg_Dbg( p_access,
1281 "receive media packet (%d bytes)",
1282 i_packet_length - 8 ); */
1284 return MMS_PACKET_MEDIA;
1288 static int mms_ReceivePacket( access_t *p_access )
1290 access_sys_t *p_sys = p_access->p_sys;
1291 int i_packet_tcp_type;
1292 int i_packet_udp_type;
1296 vlc_bool_t b_refill = VLC_TRUE;
1298 /* first if we need to refill buffer */
1299 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1301 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1303 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1304 p_sys->i_buffer_tcp )
1306 b_refill = VLC_FALSE;
1309 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1311 b_refill = VLC_FALSE;
1314 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1315 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1317 b_refill = VLC_FALSE;
1320 if( b_refill && NetFillBuffer( p_access ) < 0 )
1322 msg_Warn( p_access, "cannot fill buffer" );
1326 i_packet_tcp_type = -1;
1327 i_packet_udp_type = -1;
1329 if( p_sys->i_buffer_tcp > 0 )
1333 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1336 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1337 p_sys->i_buffer_tcp, &i_used );
1343 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1344 p_sys->i_buffer_tcp, &i_used );
1346 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1348 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1349 MMS_BUFFER_SIZE - i_used );
1351 p_sys->i_buffer_tcp -= i_used;
1353 else if( p_sys->i_buffer_udp > 0 )
1358 mms_ParsePacket( p_access, p_sys->buffer_udp,
1359 p_sys->i_buffer_udp, &i_used );
1361 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1363 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1364 MMS_BUFFER_SIZE - i_used );
1366 p_sys->i_buffer_udp -= i_used;
1369 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1371 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1372 i_packet_tcp_type = -1;
1375 if( i_packet_tcp_type != -1 )
1377 return i_packet_tcp_type;
1379 else if( i_packet_udp_type != -1 )
1381 return i_packet_udp_type;
1386 static int mms_ReceiveCommand( access_t *p_access )
1388 access_sys_t *p_sys = p_access->p_sys;
1395 if( NetFillBuffer( p_access ) < 0 )
1397 msg_Warn( p_access, "cannot fill buffer" );
1398 return VLC_EGENERIC;
1400 if( p_sys->i_buffer_tcp > 0 )
1402 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1403 p_sys->i_buffer_tcp, &i_used );
1404 if( i_used < MMS_BUFFER_SIZE )
1406 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1407 MMS_BUFFER_SIZE - i_used );
1409 p_sys->i_buffer_tcp -= i_used;
1413 return VLC_EGENERIC;
1416 if( p_sys->i_command == 0x1b )
1418 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1427 return VLC_EGENERIC;
1434 #define MMS_RETRY_MAX 10
1435 #define MMS_RETRY_SLEEP 50000
1437 static int mms_CommandRead( access_t *p_access, int i_command1,
1440 access_sys_t *p_sys = p_access->p_sys;
1444 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1446 i_status = mms_ReceiveCommand( p_access );
1447 if( i_status < 0 || p_sys->i_command == 0 )
1450 msleep( MMS_RETRY_SLEEP );
1452 else if( i_command1 == 0 && i_command2 == 0)
1456 else if( p_sys->i_command == i_command1 ||
1457 p_sys->i_command == i_command2 )
1463 switch( p_sys->i_command )
1466 msg_Warn( p_access, "socket closed by server" );
1467 p_access->info.b_eof = 1;
1468 return VLC_EGENERIC;
1470 msg_Warn( p_access, "end of media stream" );
1471 p_access->info.b_eof = 1;
1472 return VLC_EGENERIC;
1478 msg_Warn( p_access, "failed to receive command (aborting)" );
1480 return VLC_EGENERIC;
1484 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1486 access_sys_t *p_sys = p_access->p_sys;
1489 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1493 if( p_access->b_die )
1496 i_status = mms_ReceivePacket( p_access );
1500 msg_Warn( p_access, "cannot receive header (%d/%d)",
1501 i_count, MMS_RETRY_MAX );
1502 msleep( MMS_RETRY_SLEEP );
1504 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1508 else if( i_status == MMS_PACKET_CMD )
1510 switch( p_sys->i_command )
1513 msg_Warn( p_access, "socket closed by server" );
1514 p_access->info.b_eof = 1;
1517 msg_Warn( p_access, "end of media stream" );
1518 p_access->info.b_eof = 1;
1521 /* XXX not too dificult to be done EXCEPT that we
1522 * need to restart demuxer... and I don't see how we
1523 * could do that :p */
1525 "reinitialization needed --> unsupported" );
1526 p_access->info.b_eof = VLC_TRUE;
1534 msg_Err( p_access, "cannot receive %s (aborting)",
1535 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );