1 /*****************************************************************************
2 * mms.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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>
50 #if defined( UNDER_CE )
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
56 # define IN_MULTICAST(a) IN_CLASSD(a)
59 # include <sys/socket.h>
60 # include <netinet/in.h>
62 # include <arpa/inet.h>
63 # elif defined( SYS_BEOS )
64 # include <net/netdb.h>
77 /****************************************************************************
79 * MMSProtocole documentation found at http://get.to/sdp
80 ****************************************************************************/
82 /*****************************************************************************
84 *****************************************************************************/
85 int E_( MMSTUOpen ) ( access_t * );
86 void E_( MMSTUClose ) ( access_t * );
89 static int Read( access_t *, uint8_t *, int );
90 static int Seek( access_t *, int64_t );
91 static int Control( access_t *, int, va_list );
93 static int MMSOpen ( access_t *, vlc_url_t *, int );
94 static int MMSStart( access_t *, uint32_t );
95 static int MMSStop ( access_t * );
96 static void MMSClose( access_t * );
99 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
100 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
102 static int mms_HeaderMediaRead( access_t *, int );
104 static int mms_ReceivePacket( access_t * );
108 * XXX DON'T FREE MY MEMORY !!! XXX
112 * Ok, ok, j'le ferai plus...
118 * Vous pourriez signer vos commentaires (même si on voit bien qui peut
119 * écrire ce genre de trucs :p), et écrire en anglais, bordel de
123 * Alors la ouai ç'est fou les gens qui écrivent des commentaires sans les
124 * signer. Ca mériterait un coup de pied dans le cul ça :)
127 int E_(MMSTUOpen)( access_t *p_access )
133 /* Set up p_access */
134 p_access->pf_read = Read;
135 p_access->pf_block = NULL;
136 p_access->pf_control = Control;
137 p_access->pf_seek = Seek;
138 p_access->info.i_update = 0;
139 p_access->info.i_size = 0;
140 p_access->info.i_pos = 0;
141 p_access->info.b_eof = VLC_FALSE;
142 p_access->info.i_title = 0;
143 p_access->info.i_seekpoint = 0;
144 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
145 memset( p_sys, 0, sizeof( access_sys_t ) );
147 /* *** Parse URL and get server addr/port and path *** */
148 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
149 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
151 msg_Err( p_access, "invalid server name" );
152 vlc_UrlClean( &p_sys->url );
155 if( p_sys->url.i_port <= 0 )
157 p_sys->url.i_port = 1755;
160 /* *** connect to this server *** */
161 /* look at requested protocol (udp/tcp) */
162 i_proto = MMS_PROTO_AUTO;
163 if( *p_access->psz_access )
165 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
167 i_proto = MMS_PROTO_UDP;
169 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
171 i_proto = MMS_PROTO_TCP;
176 if( i_proto == MMS_PROTO_AUTO )
177 { /* first try with TCP and then UDP*/
178 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
180 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
185 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
190 msg_Err( p_access, "cannot connect to server" );
191 vlc_UrlClean( &p_sys->url );
195 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
199 * broadcast yy=0x02, xx= 0x00
200 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
202 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
204 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
206 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
208 p_sys->b_seekable = VLC_FALSE;
212 p_sys->b_seekable = VLC_TRUE;
213 p_access->info.i_size =
214 (uint64_t)p_sys->i_header +
215 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
218 /* *** Start stream *** */
219 if( MMSStart( p_access, 0xffffffff ) < 0 )
221 msg_Err( p_access, "cannot start stream" );
222 MMSClose( p_access );
223 vlc_UrlClean( &p_sys->url );
229 /*****************************************************************************
230 * Close: free unused data structures
231 *****************************************************************************/
232 void E_(MMSTUClose)( access_t *p_access )
234 access_sys_t *p_sys = p_access->p_sys;
236 /* close connection with server */
237 MMSClose( p_access );
240 vlc_UrlClean( &p_sys->url );
245 /*****************************************************************************
247 *****************************************************************************/
248 static int Control( access_t *p_access, int i_query, va_list args )
250 access_sys_t *p_sys = p_access->p_sys;
260 case ACCESS_CAN_SEEK:
261 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
262 *pb_bool = p_sys->b_seekable;
265 case ACCESS_CAN_FASTSEEK:
266 case ACCESS_CAN_PAUSE:
267 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
268 *pb_bool = VLC_FALSE;
271 case ACCESS_CAN_CONTROL_PACE:
272 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
274 #if 0 /* Disable for now until we have a clock synchro algo
275 * which works with something else than MPEG over UDP */
276 *pb_bool = VLC_FALSE;
283 pi_int = (int*)va_arg( args, int * );
284 *pi_int = 3 * p_sys->i_packet_length;
287 case ACCESS_GET_PTS_DELAY:
288 pi_64 = (int64_t*)va_arg( args, int64_t * );
289 var_Get( p_access, "mms-caching", &val );
290 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
293 case ACCESS_GET_PRIVATE_ID_STATE:
294 i_int = (int)va_arg( args, int );
295 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t );
297 if( i_int < 0 || i_int > 127 )
299 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
303 case ACCESS_SET_PAUSE_STATE:
304 case ACCESS_GET_TITLE_INFO:
305 case ACCESS_SET_TITLE:
306 case ACCESS_SET_SEEKPOINT:
307 case ACCESS_SET_PRIVATE_ID_STATE:
312 msg_Warn( p_access, "unimplemented query in control" );
319 /*****************************************************************************
320 * Seek: try to go at the right place
321 *****************************************************************************/
322 static int Seek( access_t * p_access, int64_t i_pos )
324 access_sys_t *p_sys = p_access->p_sys;
332 if( i_pos < p_sys->i_header)
335 if( p_access->info.i_pos < p_sys->i_header )
337 /* no need to restart stream, it was already one
338 * or no stream was yet read */
339 p_access->info.i_pos = i_pos;
344 i_packet = 0xffffffff;
350 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
351 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
353 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
356 msg_Dbg( p_access, "stream stopped (seek)" );
358 /* *** restart stream *** */
359 var_buffer_initwrite( &buffer, 0 );
360 var_buffer_add64( &buffer, 0 ); /* seek point in second */
361 var_buffer_add32( &buffer, 0xffffffff );
362 var_buffer_add32( &buffer, i_packet ); // begin from start
363 var_buffer_add8( &buffer, 0xff ); // stream time limit
364 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
365 var_buffer_add8( &buffer, 0xff ); //
366 var_buffer_add8( &buffer, 0x00 ); // don't use limit
367 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
369 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
370 buffer.p_data, buffer.i_data );
372 var_buffer_free( &buffer );
375 while( !p_access->b_die )
377 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
378 if( p_sys->i_command == 0x1e )
380 msg_Dbg( p_access, "received 0x1e (seek)" );
385 while( !p_access->b_die )
387 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
388 if( p_sys->i_command == 0x05 )
390 msg_Dbg( p_access, "received 0x05 (seek)" );
396 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
397 msg_Dbg( p_access, "Streaming restarted" );
399 p_sys->i_media_used += i_offset;
400 p_access->info.i_pos = i_pos;
401 p_access->info.b_eof = VLC_FALSE;
406 /*****************************************************************************
408 *****************************************************************************/
409 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
411 access_sys_t *p_sys = p_access->p_sys;
417 /* *** now send data if needed *** */
418 while( i_data < i_len )
420 if( p_access->info.i_pos < p_sys->i_header )
422 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
423 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
425 p_access->info.i_pos += i_copy;
427 else if( p_sys->i_media_used < p_sys->i_media )
429 i_copy = __MIN( i_len - i_data ,
430 p_sys->i_media - p_sys->i_media_used );
431 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
433 p_sys->i_media_used += i_copy;
434 p_access->info.i_pos += i_copy;
436 else if( p_sys->p_media != NULL &&
437 p_sys->i_media_used < p_sys->i_packet_length )
439 i_copy = __MIN( i_len - i_data,
440 p_sys->i_packet_length - p_sys->i_media_used);
441 memset( &p_buffer[i_data], 0, i_copy );
444 p_sys->i_media_used += i_copy;
445 p_access->info.i_pos += i_copy;
447 else if( p_access->info.b_eof ||
448 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
457 /****************************************************************************
458 * MMSOpen : Open a connection with the server over mmst or mmsu
459 ****************************************************************************/
460 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
462 access_sys_t *p_sys = p_access->p_sys;
463 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
468 int i_server_version;
470 int i_update_player_url;
471 int i_encryption_type;
477 /* *** Open a TCP connection with server *** */
478 msg_Dbg( p_access, "waiting for connection..." );
479 p_sys->i_handle_tcp = net_OpenTCP( p_access, p_url->psz_host, p_url->i_port );
480 if( p_sys->i_handle_tcp < 0 )
482 msg_Err( p_access, "failed to open a connection (tcp)" );
486 "connection(tcp) with \"%s:%d\" successful",
490 /* *** Bind port if UDP protocol is selected *** */
493 struct sockaddr_storage name;
494 socklen_t i_namelen = sizeof( name );
496 if( getsockname( p_sys->i_handle_tcp,
497 (struct sockaddr*)&name, &i_namelen ) < 0 )
499 net_Close( p_sys->i_handle_tcp );
503 /* FIXME: not thread-safe for IPv4 */
504 /* FIXME: not sure if it works fine for IPv6 */
505 if( name.ss_family == AF_INET )
506 p_sys->psz_bind_addr = inet_ntoa( ((struct sockaddr_in *)&name)->sin_addr );
508 p_sys->psz_bind_addr = p_url->psz_host;
510 p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->psz_bind_addr, 7000, "", 0 );
511 if( p_sys->i_handle_udp < 0 )
513 msg_Err( p_access, "failed to open a connection (udp)" );
514 net_Close( p_sys->i_handle_tcp );
518 "connection(udp) at \"%s:%d\" successful",
519 p_sys->psz_bind_addr, 7000 );
523 p_sys->psz_bind_addr = NULL;
526 /* *** Init context for mms prototcol *** */
527 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
529 "generated guid: "GUID_FMT,
530 GUID_PRINT( p_sys->guid ) );
531 p_sys->i_command_level = 1; /* updated after 0x1A command */
532 p_sys->i_seq_num = 0;
533 p_sys->i_media_packet_id_type = 0x04;
534 p_sys->i_header_packet_id_type = 0x02;
535 p_sys->i_proto = i_proto;
536 p_sys->i_packet_seq_num = 0;
537 p_sys->p_header = NULL;
539 p_sys->p_media = NULL;
541 p_sys->i_media_used = 0;
543 p_access->info.i_pos = 0;
544 p_sys->i_buffer_tcp = 0;
545 p_sys->i_buffer_udp = 0;
548 p_access->info.b_eof = 0;
550 /* *** send command 1 : connection request *** */
551 var_buffer_initwrite( &buffer, 0 );
552 var_buffer_add16( &buffer, 0x001c );
553 var_buffer_add16( &buffer, 0x0003 );
555 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
556 GUID_PRINT( p_sys->guid ),
558 var_buffer_addUTF16( &buffer, tmp );
560 mms_CommandSend( p_access,
561 0x01, /* connexion request */
562 0x00000000, /* flags, FIXME */
563 0x0004000b, /* ???? */
567 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
569 var_buffer_free( &buffer );
570 MMSClose( p_access );
574 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
575 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
576 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
577 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
578 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
579 #define GETUTF16( psz, size ) \
582 psz = malloc( size + 1); \
583 for( i = 0; i < size; i++ ) \
590 GETUTF16( p_sys->psz_server_version, i_server_version );
591 GETUTF16( p_sys->psz_tool_version, i_tool_version );
592 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
593 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
596 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
597 p_sys->psz_server_version,
598 p_sys->psz_tool_version,
599 p_sys->psz_update_player_url,
600 p_sys->psz_encryption_type );
602 /* *** should make an 18 command to make data timing *** */
604 /* *** send command 2 : transport protocol selection *** */
605 var_buffer_reinitwrite( &buffer, 0 );
606 var_buffer_add32( &buffer, 0x00000000 );
607 var_buffer_add32( &buffer, 0x000a0000 );
608 var_buffer_add32( &buffer, 0x00000002 );
613 p_sys->psz_bind_addr,
618 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
620 var_buffer_addUTF16( &buffer, tmp );
621 var_buffer_add16( &buffer, '0' );
623 mms_CommandSend( p_access,
624 0x02, /* connexion request */
625 0x00000000, /* flags, FIXME */
626 0xffffffff, /* ???? */
630 /* *** response from server, should be 0x02 or 0x03 *** */
631 mms_CommandRead( p_access, 0x02, 0x03 );
632 if( p_sys->i_command == 0x03 )
635 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
636 var_buffer_free( &buffer );
637 MMSClose( p_access );
640 else if( p_sys->i_command != 0x02 )
642 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
645 /* *** send command 5 : media file name/path requested *** */
646 var_buffer_reinitwrite( &buffer, 0 );
647 var_buffer_add64( &buffer, 0 );
648 var_buffer_addUTF16( &buffer, p_url->psz_path );
650 mms_CommandSend( p_access,
652 p_sys->i_command_level,
657 /* *** wait for reponse *** */
658 mms_CommandRead( p_access, 0x1a, 0x06 );
660 /* test if server send 0x1A answer */
661 if( p_sys->i_command == 0x1A )
663 msg_Err( p_access, "id/password requested (not yet supported)" );
665 var_buffer_free( &buffer );
666 MMSClose( p_access );
669 if( p_sys->i_command != 0x06 )
672 "unknown answer (0x%x instead of 0x06)",
674 var_buffer_free( &buffer );
675 MMSClose( p_access );
679 /* 1 for file ok, 2 for authen ok */
680 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
683 msg_Dbg( p_access, "Media file name/path accepted" );
686 msg_Dbg( p_access, "Authentication accepted" );
690 msg_Err( p_access, "error while asking for file %d",
691 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
692 var_buffer_free( &buffer );
693 MMSClose( p_access );
697 p_sys->i_flags_broadcast =
698 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
699 p_sys->i_media_length =
700 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
701 p_sys->i_packet_length =
702 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
703 p_sys->i_packet_count =
704 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
705 p_sys->i_max_bit_rate =
706 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
707 p_sys->i_header_size =
708 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
711 "answer 0x06 flags:0x%8.8x media_length:%ds packet_length:%d packet_count:%d max_bit_rate:%d header_size:%d",
712 p_sys->i_flags_broadcast,
713 p_sys->i_media_length,
714 p_sys->i_packet_length,
715 p_sys->i_packet_count,
716 p_sys->i_max_bit_rate,
717 p_sys->i_header_size );
719 /* *** send command 15 *** */
721 var_buffer_reinitwrite( &buffer, 0 );
722 var_buffer_add32( &buffer, 0 );
723 var_buffer_add32( &buffer, 0x8000 );
724 var_buffer_add32( &buffer, 0xffffffff );
725 var_buffer_add32( &buffer, 0x00 );
726 var_buffer_add32( &buffer, 0x00 );
727 var_buffer_add32( &buffer, 0x00 );
728 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
729 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
730 var_buffer_add32( &buffer, 0x00 );
731 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
732 buffer.p_data, buffer.i_data );
734 /* *** wait for reponse *** */
735 /* Commented out because it fails on some stream (no 0x11 answer) */
737 mms_CommandRead( p_access, 0x11, 0 );
739 if( p_sys->i_command != 0x11 )
742 "unknown answer (0x%x instead of 0x11)",
744 var_buffer_free( &buffer );
745 MMSClose( p_access );
750 /* *** now read header packet *** */
751 /* XXX could be split over multiples packets */
752 msg_Dbg( p_access, "reading header" );
755 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
757 msg_Err( p_access, "cannot receive header" );
758 var_buffer_free( &buffer );
759 MMSClose( p_access );
762 if( p_sys->i_header >= p_sys->i_header_size )
765 "header complete(%d)",
770 "header incomplete (%d/%d), reading more",
772 p_sys->i_header_size );
775 /* *** parse header and get stream and their id *** */
776 /* get all streams properties,
778 * TODO : stream bitrates properties(optional)
779 * and bitrate mutual exclusion(optional) */
780 E_( asf_HeaderParse )( &p_sys->asfh,
781 p_sys->p_header, p_sys->i_header );
782 E_( asf_StreamSelect)( &p_sys->asfh,
783 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
784 var_CreateGetInteger( p_access, "mms-all" ),
785 var_CreateGetInteger( p_access, "audio" ),
786 var_CreateGetInteger( p_access, "video" ) );
788 /* *** now select stream we want to receive *** */
789 /* TODO take care of stream bitrate TODO */
792 var_buffer_reinitwrite( &buffer, 0 );
793 /* for now, select first audio and video stream */
794 for( i = 1; i < 128; i++ )
797 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
802 var_buffer_add16( &buffer, 0xffff );
803 var_buffer_add16( &buffer, i );
809 if( p_sys->asfh.stream[i].i_selected )
811 var_buffer_add16( &buffer, 0x0000 );
813 "selecting stream[0x%x] %s (%d kb/s)",
815 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
817 p_sys->asfh.stream[i].i_bitrate / 1024);
821 var_buffer_add16( &buffer, 0x0002 );
823 "ignoring stream[0x%x] %s (%d kb/s)",
825 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
827 p_sys->asfh.stream[i].i_bitrate / 1024);
835 msg_Err( p_access, "cannot find any stream" );
836 var_buffer_free( &buffer );
837 MMSClose( p_access );
840 mms_CommandSend( p_access, 0x33,
842 0xffff | ( i_first << 16 ),
843 buffer.p_data, buffer.i_data );
845 mms_CommandRead( p_access, 0x21, 0 );
846 if( p_sys->i_command != 0x21 )
849 "unknown answer (0x%x instead of 0x21)",
851 var_buffer_free( &buffer );
852 MMSClose( p_access );
857 var_buffer_free( &buffer );
859 msg_Info( p_access, "connection sucessful" );
864 /****************************************************************************
865 * MMSStart : Start streaming
866 ****************************************************************************/
867 static int MMSStart( access_t *p_access, uint32_t i_packet )
869 access_sys_t *p_sys = p_access->p_sys;
872 /* *** start stream from packet 0 *** */
873 var_buffer_initwrite( &buffer, 0 );
874 var_buffer_add64( &buffer, 0 ); /* seek point in second */
875 var_buffer_add32( &buffer, 0xffffffff );
876 var_buffer_add32( &buffer, i_packet ); // begin from start
877 var_buffer_add8( &buffer, 0xff ); // stream time limit
878 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
879 var_buffer_add8( &buffer, 0xff ); //
880 var_buffer_add8( &buffer, 0x00 ); // don't use limit
881 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
883 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
884 buffer.p_data, buffer.i_data );
886 var_buffer_free( &buffer );
888 mms_CommandRead( p_access, 0x05, 0 );
890 if( p_sys->i_command != 0x05 )
893 "unknown answer (0x%x instead of 0x05)",
900 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
901 msg_Dbg( p_access, "Streaming started" );
906 /****************************************************************************
907 * MMSStop : Stop streaming
908 ****************************************************************************/
909 static int MMSStop( access_t *p_access )
911 access_sys_t *p_sys = p_access->p_sys;
913 /* *** stop stream but keep connection alive *** */
914 mms_CommandSend( p_access,
916 p_sys->i_command_level,
922 /****************************************************************************
923 * MMSClose : Close streaming and connection
924 ****************************************************************************/
925 static void MMSClose( access_t *p_access )
927 access_sys_t *p_sys = p_access->p_sys;
929 msg_Dbg( p_access, "Connection closed" );
931 /* *** tell server that we will disconnect *** */
932 mms_CommandSend( p_access,
934 p_sys->i_command_level,
938 /* *** close sockets *** */
939 net_Close( p_sys->i_handle_tcp );
940 if( p_sys->i_proto == MMS_PROTO_UDP )
942 net_Close( p_sys->i_handle_udp );
945 FREE( p_sys->p_cmd );
946 FREE( p_sys->p_media );
947 FREE( p_sys->p_header );
949 FREE( p_sys->psz_server_version );
950 FREE( p_sys->psz_tool_version );
951 FREE( p_sys->psz_update_player_url );
952 FREE( p_sys->psz_encryption_type );
955 /****************************************************************************
957 * MMS specific functions
959 ****************************************************************************/
960 static int mms_CommandSend( access_t *p_access,
962 uint32_t i_prefix1, uint32_t i_prefix2,
963 uint8_t *p_data, int i_data )
967 access_sys_t *p_sys = p_access->p_sys;
970 i_data_by8 = ( i_data + 7 ) / 8;
972 /* first init uffer */
973 var_buffer_initwrite( &buffer, 0 );
975 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
976 var_buffer_add32( &buffer, 0xB00BFACE );
977 /* size after protocol type */
978 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
979 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
980 var_buffer_add32( &buffer, i_data_by8 + 4 );
981 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
982 var_buffer_add64( &buffer, 0 );
983 var_buffer_add32( &buffer, i_data_by8 + 2 );
984 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
985 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
986 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
988 /* specific command data */
989 if( p_data && i_data > 0 )
991 var_buffer_addmemory( &buffer, p_data, i_data );
995 if( send( p_sys->i_handle_tcp,
1000 msg_Err( p_access, "failed to send command" );
1001 return VLC_EGENERIC;
1004 var_buffer_free( &buffer );
1008 static int NetFillBuffer( access_t *p_access )
1014 access_sys_t *p_sys = p_access->p_sys;
1015 struct timeval timeout;
1016 fd_set fds_r, fds_e;
1019 /* FIXME when using udp */
1020 ssize_t i_tcp, i_udp;
1021 ssize_t i_tcp_read, i_udp_read;
1025 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1027 if( p_sys->i_proto == MMS_PROTO_UDP )
1029 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1033 i_udp = 0; /* there isn't udp socket */
1039 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1041 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1043 if( i_handle_max == 0 )
1045 msg_Warn( p_access, "nothing to read %d:%d", i_tcp, i_udp );
1050 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1053 /* Find if some data is available */
1058 /* Initialize file descriptor set */
1064 FD_SET( p_sys->i_handle_tcp, &fds_r );
1065 FD_SET( p_sys->i_handle_tcp, &fds_e );
1069 FD_SET( p_sys->i_handle_udp, &fds_r );
1070 FD_SET( p_sys->i_handle_udp, &fds_e );
1073 /* We'll wait 0.5 second if nothing happens */
1075 timeout.tv_usec = 500000;
1077 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1082 if( p_access->b_die || p_access->b_error ) return -1;
1084 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1086 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1087 (i_ret < 0 && errno == EINTR) );
1091 msg_Err( p_access, "network select error (%s)", strerror(errno) );
1095 i_tcp_read = i_udp_read = 0;
1097 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1100 recv( p_sys->i_handle_tcp,
1101 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1102 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1105 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1107 i_udp_read = recv( p_sys->i_handle_udp,
1108 p_sys->buffer_udp + p_sys->i_buffer_udp,
1109 i_udp + MMS_BUFFER_SIZE/2, 0 );
1113 if( p_sys->i_proto == MMS_PROTO_UDP )
1115 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1116 p_sys->i_buffer_tcp, i_tcp_read,
1117 p_sys->i_buffer_udp, i_udp_read );
1121 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1122 p_sys->i_buffer_tcp, i_tcp_read );
1126 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1127 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1129 return i_tcp_read + i_udp_read;
1133 static int mms_ParseCommand( access_t *p_access,
1138 #define GET32( i_pos ) \
1139 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1140 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1141 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1143 access_sys_t *p_sys = p_access->p_sys;
1149 free( p_sys->p_cmd );
1151 p_sys->i_cmd = i_data;
1152 p_sys->p_cmd = malloc( i_data );
1153 memcpy( p_sys->p_cmd, p_data, i_data );
1155 *pi_used = i_data; /* by default */
1157 if( i_data < MMS_CMD_HEADERSIZE )
1159 msg_Warn( p_access, "truncated command (header incomplete)" );
1160 p_sys->i_command = 0;
1163 i_id = GetDWLE( p_data + 4 );
1164 i_length = GetDWLE( p_data + 8 ) + 16;
1166 if( i_id != 0xb00bface )
1169 "incorrect command header (0x%x)", i_id );
1170 p_sys->i_command = 0;
1174 if( i_length > p_sys->i_cmd )
1177 "truncated command (missing %d bytes)",
1178 i_length - i_data );
1179 p_sys->i_command = 0;
1182 else if( i_length < p_sys->i_cmd )
1184 p_sys->i_cmd = i_length;
1185 *pi_used = i_length;
1189 "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",
1193 /* 12: protocol type "MMS " */
1196 /* 24: unknown (0) */
1197 /* 28: unknown (0) */
1203 p_sys->i_command = GET32( 36 ) & 0xffff;
1206 return MMS_PACKET_CMD;
1209 static int mms_ParsePacket( access_t *p_access,
1210 uint8_t *p_data, size_t i_data,
1213 access_sys_t *p_sys = p_access->p_sys;
1214 int i_packet_seq_num;
1215 size_t i_packet_length;
1216 uint32_t i_packet_id;
1221 *pi_used = i_data; /* default */
1224 msg_Warn( p_access, "truncated packet (header incomplete)" );
1228 i_packet_id = p_data[4];
1229 i_packet_seq_num = GetDWLE( p_data );
1230 i_packet_length = GetWLE( p_data + 6 );
1232 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1234 if( i_packet_length > i_data || i_packet_length <= 8)
1236 /* msg_Dbg( p_access,
1237 "truncated packet (Declared %d bytes, Actual %d bytes)",
1238 i_packet_length, i_data ); */
1242 else if( i_packet_length < i_data )
1244 *pi_used = i_packet_length;
1247 if( i_packet_id == 0xff )
1250 "receive MMS UDP pair timing" );
1251 return( MMS_PACKET_UDP_TIMING );
1254 if( i_packet_id != p_sys->i_header_packet_id_type &&
1255 i_packet_id != p_sys->i_media_packet_id_type )
1257 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1261 /* we now have a media or a header packet */
1262 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1263 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1265 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1267 /* FIXME for udp could be just wrong order ? */
1269 "detected packet lost (%d != %d)",
1271 p_sys->i_packet_seq_num );
1272 p_sys->i_packet_seq_num = i_packet_seq_num;
1274 p_sys->i_packet_seq_num++;
1276 if( i_packet_id == p_sys->i_header_packet_id_type )
1278 if( p_sys->p_header )
1280 p_sys->p_header = realloc( p_sys->p_header,
1281 p_sys->i_header + i_packet_length - 8 );
1282 memcpy( &p_sys->p_header[p_sys->i_header],
1284 i_packet_length - 8 );
1285 p_sys->i_header += i_packet_length - 8;
1291 p_sys->p_header = p_packet;
1292 p_sys->i_header = i_packet_length - 8;
1294 /* msg_Dbg( p_access,
1295 "receive header packet (%d bytes)",
1296 i_packet_length - 8 ); */
1298 return MMS_PACKET_HEADER;
1302 FREE( p_sys->p_media );
1303 p_sys->p_media = p_packet;
1304 p_sys->i_media = i_packet_length - 8;
1305 p_sys->i_media_used = 0;
1306 /* msg_Dbg( p_access,
1307 "receive media packet (%d bytes)",
1308 i_packet_length - 8 ); */
1310 return MMS_PACKET_MEDIA;
1314 static int mms_ReceivePacket( access_t *p_access )
1316 access_sys_t *p_sys = p_access->p_sys;
1317 int i_packet_tcp_type;
1318 int i_packet_udp_type;
1322 vlc_bool_t b_refill = VLC_TRUE;
1324 /* first if we need to refill buffer */
1325 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1327 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1329 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1330 p_sys->i_buffer_tcp )
1332 b_refill = VLC_FALSE;
1335 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1337 b_refill = VLC_FALSE;
1340 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1341 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1343 b_refill = VLC_FALSE;
1346 if( b_refill && NetFillBuffer( p_access ) < 0 )
1348 msg_Warn( p_access, "cannot fill buffer" );
1352 i_packet_tcp_type = -1;
1353 i_packet_udp_type = -1;
1355 if( p_sys->i_buffer_tcp > 0 )
1359 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1362 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1363 p_sys->i_buffer_tcp, &i_used );
1369 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1370 p_sys->i_buffer_tcp, &i_used );
1372 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1374 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1375 MMS_BUFFER_SIZE - i_used );
1377 p_sys->i_buffer_tcp -= i_used;
1379 else if( p_sys->i_buffer_udp > 0 )
1384 mms_ParsePacket( p_access, p_sys->buffer_udp,
1385 p_sys->i_buffer_udp, &i_used );
1387 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1389 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1390 MMS_BUFFER_SIZE - i_used );
1392 p_sys->i_buffer_udp -= i_used;
1395 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1397 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1398 i_packet_tcp_type = -1;
1401 if( i_packet_tcp_type != -1 )
1403 return i_packet_tcp_type;
1405 else if( i_packet_udp_type != -1 )
1407 return i_packet_udp_type;
1412 static int mms_ReceiveCommand( access_t *p_access )
1414 access_sys_t *p_sys = p_access->p_sys;
1421 if( NetFillBuffer( p_access ) < 0 )
1423 msg_Warn( p_access, "cannot fill buffer" );
1424 return VLC_EGENERIC;
1426 if( p_sys->i_buffer_tcp > 0 )
1428 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1429 p_sys->i_buffer_tcp, &i_used );
1430 if( i_used < MMS_BUFFER_SIZE )
1432 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1433 MMS_BUFFER_SIZE - i_used );
1435 p_sys->i_buffer_tcp -= i_used;
1439 return VLC_EGENERIC;
1442 if( p_sys->i_command == 0x1b )
1444 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1453 return VLC_EGENERIC;
1460 #define MMS_RETRY_MAX 10
1461 #define MMS_RETRY_SLEEP 50000
1463 static int mms_CommandRead( access_t *p_access, int i_command1,
1466 access_sys_t *p_sys = p_access->p_sys;
1470 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1472 i_status = mms_ReceiveCommand( p_access );
1473 if( i_status < 0 || p_sys->i_command == 0 )
1476 msleep( MMS_RETRY_SLEEP );
1478 else if( i_command1 == 0 && i_command2 == 0)
1482 else if( p_sys->i_command == i_command1 ||
1483 p_sys->i_command == i_command2 )
1489 switch( p_sys->i_command )
1492 msg_Warn( p_access, "socket closed by server" );
1493 p_access->info.b_eof = 1;
1494 return VLC_EGENERIC;
1496 msg_Warn( p_access, "end of media stream" );
1497 p_access->info.b_eof = 1;
1498 return VLC_EGENERIC;
1504 msg_Warn( p_access, "failed to receive command (aborting)" );
1506 return VLC_EGENERIC;
1510 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1512 access_sys_t *p_sys = p_access->p_sys;
1515 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1519 if( p_access->b_die )
1522 i_status = mms_ReceivePacket( p_access );
1526 msg_Warn( p_access, "cannot receive header (%d/%d)",
1527 i_count, MMS_RETRY_MAX );
1528 msleep( MMS_RETRY_SLEEP );
1530 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1534 else if( i_status == MMS_PACKET_CMD )
1536 switch( p_sys->i_command )
1539 msg_Warn( p_access, "socket closed by server" );
1540 p_access->info.b_eof = 1;
1543 msg_Warn( p_access, "end of media stream" );
1544 p_access->info.b_eof = 1;
1547 /* XXX not too dificult to be done EXCEPT that we
1548 * need to restart demuxer... and I don't see how we
1549 * could do that :p */
1551 "reinitialization needed --> unsupported" );
1552 p_access->info.b_eof = VLC_TRUE;
1560 msg_Err( p_access, "cannot receive %s (aborting)",
1561 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );