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>
51 # include <winsock2.h>
52 # include <ws2tcpip.h>
54 # define IN_MULTICAST(a) IN_CLASSD(a)
57 # include <sys/socket.h>
58 # include <netinet/in.h>
60 # include <arpa/inet.h>
61 # elif defined( SYS_BEOS )
62 # include <net/netdb.h>
75 /****************************************************************************
77 * MMSProtocole documentation found at http://get.to/sdp
78 ****************************************************************************/
80 /*****************************************************************************
82 *****************************************************************************/
83 int E_( MMSTUOpen ) ( access_t * );
84 void E_( MMSTUClose ) ( access_t * );
87 static int Read( access_t *, uint8_t *, int );
88 static int Seek( access_t *, int64_t );
89 static int Control( access_t *, int, va_list );
91 static int MMSOpen ( access_t *, vlc_url_t *, int );
92 static int MMSStart( access_t *, uint32_t );
93 static int MMSStop ( access_t * );
94 static void MMSClose( access_t * );
97 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
98 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
100 static int mms_HeaderMediaRead( access_t *, int );
102 static int mms_ReceivePacket( access_t * );
106 * XXX DON'T FREE MY MEMORY !!! XXX
110 * Ok, ok, j'le ferai plus...
116 * Vous pourriez signer vos commentaires (même si on voit bien qui peut
117 * écrire ce genre de trucs :p), et écrire en anglais, bordel de
121 * Alors la ouai ç'est fou les gens qui écrivent des commentaires sans les
122 * signer. Ca mériterait un coup de pied dans le cul ça :)
125 int E_(MMSTUOpen)( access_t *p_access )
131 /* Set up p_access */
132 p_access->pf_read = Read;
133 p_access->pf_block = NULL;
134 p_access->pf_control = Control;
135 p_access->pf_seek = Seek;
136 p_access->info.i_update = 0;
137 p_access->info.i_size = 0;
138 p_access->info.i_pos = 0;
139 p_access->info.b_eof = VLC_FALSE;
140 p_access->info.i_title = 0;
141 p_access->info.i_seekpoint = 0;
142 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
143 memset( p_sys, 0, sizeof( access_sys_t ) );
145 /* *** Parse URL and get server addr/port and path *** */
146 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
147 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
149 msg_Err( p_access, "invalid server name" );
150 vlc_UrlClean( &p_sys->url );
153 if( p_sys->url.i_port <= 0 )
155 p_sys->url.i_port = 1755;
158 /* *** connect to this server *** */
159 /* look at requested protocol (udp/tcp) */
160 i_proto = MMS_PROTO_AUTO;
161 if( *p_access->psz_access )
163 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
165 i_proto = MMS_PROTO_UDP;
167 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
169 i_proto = MMS_PROTO_TCP;
174 if( i_proto == MMS_PROTO_AUTO )
175 { /* first try with TCP and then UDP*/
176 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
178 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
183 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
188 msg_Err( p_access, "cannot connect to server" );
189 vlc_UrlClean( &p_sys->url );
193 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
197 * broadcast yy=0x02, xx= 0x00
198 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
200 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
202 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
204 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
206 p_sys->b_seekable = VLC_FALSE;
210 p_sys->b_seekable = VLC_TRUE;
211 p_access->info.i_size =
212 (uint64_t)p_sys->i_header +
213 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
216 /* *** Start stream *** */
217 if( MMSStart( p_access, 0xffffffff ) < 0 )
219 msg_Err( p_access, "cannot start stream" );
220 MMSClose( p_access );
221 vlc_UrlClean( &p_sys->url );
227 /*****************************************************************************
228 * Close: free unused data structures
229 *****************************************************************************/
230 void E_(MMSTUClose)( access_t *p_access )
232 access_sys_t *p_sys = p_access->p_sys;
234 /* close connection with server */
235 MMSClose( p_access );
238 vlc_UrlClean( &p_sys->url );
243 /*****************************************************************************
245 *****************************************************************************/
246 static int Control( access_t *p_access, int i_query, va_list args )
248 access_sys_t *p_sys = p_access->p_sys;
258 case ACCESS_CAN_SEEK:
259 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
260 *pb_bool = p_sys->b_seekable;
263 case ACCESS_CAN_FASTSEEK:
264 case ACCESS_CAN_PAUSE:
265 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
266 *pb_bool = VLC_FALSE;
269 case ACCESS_CAN_CONTROL_PACE:
270 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
272 #if 0 /* Disable for now until we have a clock synchro algo
273 * which works with something else than MPEG over UDP */
274 *pb_bool = VLC_FALSE;
281 pi_int = (int*)va_arg( args, int * );
282 *pi_int = 3 * p_sys->i_packet_length;
285 case ACCESS_GET_PTS_DELAY:
286 pi_64 = (int64_t*)va_arg( args, int64_t * );
287 var_Get( p_access, "mms-caching", &val );
288 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
291 case ACCESS_GET_PRIVATE_ID_STATE:
292 i_int = (int)va_arg( args, int );
293 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t );
295 if( i_int < 0 || i_int > 127 )
297 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
301 case ACCESS_SET_PAUSE_STATE:
302 case ACCESS_GET_TITLE_INFO:
303 case ACCESS_SET_TITLE:
304 case ACCESS_SET_SEEKPOINT:
305 case ACCESS_SET_PRIVATE_ID_STATE:
310 msg_Warn( p_access, "unimplemented query in control" );
317 /*****************************************************************************
318 * Seek: try to go at the right place
319 *****************************************************************************/
320 static int Seek( access_t * p_access, int64_t i_pos )
322 access_sys_t *p_sys = p_access->p_sys;
330 if( i_pos < p_sys->i_header)
333 if( p_access->info.i_pos < p_sys->i_header )
335 /* no need to restart stream, it was already one
336 * or no stream was yet read */
337 p_access->info.i_pos = i_pos;
342 i_packet = 0xffffffff;
348 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
349 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
351 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
354 msg_Dbg( p_access, "stream stopped (seek)" );
356 /* *** restart stream *** */
357 var_buffer_initwrite( &buffer, 0 );
358 var_buffer_add64( &buffer, 0 ); /* seek point in second */
359 var_buffer_add32( &buffer, 0xffffffff );
360 var_buffer_add32( &buffer, i_packet ); // begin from start
361 var_buffer_add8( &buffer, 0xff ); // stream time limit
362 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
363 var_buffer_add8( &buffer, 0xff ); //
364 var_buffer_add8( &buffer, 0x00 ); // don't use limit
365 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
367 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
368 buffer.p_data, buffer.i_data );
370 var_buffer_free( &buffer );
373 while( !p_access->b_die )
375 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
376 if( p_sys->i_command == 0x1e )
378 msg_Dbg( p_access, "received 0x1e (seek)" );
383 while( !p_access->b_die )
385 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
386 if( p_sys->i_command == 0x05 )
388 msg_Dbg( p_access, "received 0x05 (seek)" );
394 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
395 msg_Dbg( p_access, "Streaming restarted" );
397 p_sys->i_media_used += i_offset;
398 p_access->info.i_pos = i_pos;
399 p_access->info.b_eof = VLC_FALSE;
404 /*****************************************************************************
406 *****************************************************************************/
407 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
409 access_sys_t *p_sys = p_access->p_sys;
415 /* *** now send data if needed *** */
416 while( i_data < i_len )
418 if( p_access->info.i_pos < p_sys->i_header )
420 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
421 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
423 p_access->info.i_pos += i_copy;
425 else if( p_sys->i_media_used < p_sys->i_media )
427 i_copy = __MIN( i_len - i_data ,
428 p_sys->i_media - p_sys->i_media_used );
429 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
431 p_sys->i_media_used += i_copy;
432 p_access->info.i_pos += i_copy;
434 else if( p_sys->p_media != NULL &&
435 p_sys->i_media_used < p_sys->i_packet_length )
437 i_copy = __MIN( i_len - i_data,
438 p_sys->i_packet_length - p_sys->i_media_used);
439 memset( &p_buffer[i_data], 0, i_copy );
442 p_sys->i_media_used += i_copy;
443 p_access->info.i_pos += i_copy;
445 else if( p_access->info.b_eof ||
446 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
455 /****************************************************************************
456 * MMSOpen : Open a connection with the server over mmst or mmsu
457 ****************************************************************************/
458 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
460 access_sys_t *p_sys = p_access->p_sys;
461 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
466 int i_server_version;
468 int i_update_player_url;
469 int i_encryption_type;
475 /* *** Open a TCP connection with server *** */
476 msg_Dbg( p_access, "waiting for connection..." );
477 p_sys->i_handle_tcp = net_OpenTCP( p_access, p_url->psz_host, p_url->i_port );
478 if( p_sys->i_handle_tcp < 0 )
480 msg_Err( p_access, "failed to open a connection (tcp)" );
484 "connection(tcp) with \"%s:%d\" successful",
488 /* *** Bind port if UDP protocol is selected *** */
491 struct sockaddr_storage name;
492 socklen_t i_namelen = sizeof( name );
494 if( getsockname( p_sys->i_handle_tcp,
495 (struct sockaddr*)&name, &i_namelen ) < 0 )
497 net_Close( p_sys->i_handle_tcp );
501 /* FIXME: not thread-safe for IPv4 */
502 /* FIXME: not sure if it works fine for IPv6 */
503 if( name.ss_family == AF_INET )
504 p_sys->psz_bind_addr = inet_ntoa( ((struct sockaddr_in *)&name)->sin_addr );
506 p_sys->psz_bind_addr = p_url->psz_host;
508 p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->psz_bind_addr, 7000, "", 0 );
509 if( p_sys->i_handle_udp < 0 )
511 msg_Err( p_access, "failed to open a connection (udp)" );
512 net_Close( p_sys->i_handle_tcp );
516 "connection(udp) at \"%s:%d\" successful",
517 p_sys->psz_bind_addr, 7000 );
521 p_sys->psz_bind_addr = NULL;
524 /* *** Init context for mms prototcol *** */
525 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
527 "generated guid: "GUID_FMT,
528 GUID_PRINT( p_sys->guid ) );
529 p_sys->i_command_level = 1; /* updated after 0x1A command */
530 p_sys->i_seq_num = 0;
531 p_sys->i_media_packet_id_type = 0x04;
532 p_sys->i_header_packet_id_type = 0x02;
533 p_sys->i_proto = i_proto;
534 p_sys->i_packet_seq_num = 0;
535 p_sys->p_header = NULL;
537 p_sys->p_media = NULL;
539 p_sys->i_media_used = 0;
541 p_access->info.i_pos = 0;
542 p_sys->i_buffer_tcp = 0;
543 p_sys->i_buffer_udp = 0;
546 p_access->info.b_eof = 0;
548 /* *** send command 1 : connection request *** */
549 var_buffer_initwrite( &buffer, 0 );
550 var_buffer_add16( &buffer, 0x001c );
551 var_buffer_add16( &buffer, 0x0003 );
553 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
554 GUID_PRINT( p_sys->guid ),
556 var_buffer_addUTF16( &buffer, tmp );
558 mms_CommandSend( p_access,
559 0x01, /* connexion request */
560 0x00000000, /* flags, FIXME */
561 0x0004000b, /* ???? */
565 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
567 var_buffer_free( &buffer );
568 MMSClose( p_access );
572 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
573 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
574 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
575 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
576 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
577 #define GETUTF16( psz, size ) \
580 psz = malloc( size + 1); \
581 for( i = 0; i < size; i++ ) \
588 GETUTF16( p_sys->psz_server_version, i_server_version );
589 GETUTF16( p_sys->psz_tool_version, i_tool_version );
590 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
591 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
594 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
595 p_sys->psz_server_version,
596 p_sys->psz_tool_version,
597 p_sys->psz_update_player_url,
598 p_sys->psz_encryption_type );
600 /* *** should make an 18 command to make data timing *** */
602 /* *** send command 2 : transport protocol selection *** */
603 var_buffer_reinitwrite( &buffer, 0 );
604 var_buffer_add32( &buffer, 0x00000000 );
605 var_buffer_add32( &buffer, 0x000a0000 );
606 var_buffer_add32( &buffer, 0x00000002 );
611 p_sys->psz_bind_addr,
616 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
618 var_buffer_addUTF16( &buffer, tmp );
619 var_buffer_add16( &buffer, '0' );
621 mms_CommandSend( p_access,
622 0x02, /* connexion request */
623 0x00000000, /* flags, FIXME */
624 0xffffffff, /* ???? */
628 /* *** response from server, should be 0x02 or 0x03 *** */
629 mms_CommandRead( p_access, 0x02, 0x03 );
630 if( p_sys->i_command == 0x03 )
633 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
634 var_buffer_free( &buffer );
635 MMSClose( p_access );
638 else if( p_sys->i_command != 0x02 )
640 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
643 /* *** send command 5 : media file name/path requested *** */
644 var_buffer_reinitwrite( &buffer, 0 );
645 var_buffer_add64( &buffer, 0 );
646 var_buffer_addUTF16( &buffer, p_url->psz_path );
648 mms_CommandSend( p_access,
650 p_sys->i_command_level,
655 /* *** wait for reponse *** */
656 mms_CommandRead( p_access, 0x1a, 0x06 );
658 /* test if server send 0x1A answer */
659 if( p_sys->i_command == 0x1A )
661 msg_Err( p_access, "id/password requested (not yet supported)" );
663 var_buffer_free( &buffer );
664 MMSClose( p_access );
667 if( p_sys->i_command != 0x06 )
670 "unknown answer (0x%x instead of 0x06)",
672 var_buffer_free( &buffer );
673 MMSClose( p_access );
677 /* 1 for file ok, 2 for authen ok */
678 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
681 msg_Dbg( p_access, "Media file name/path accepted" );
684 msg_Dbg( p_access, "Authentication accepted" );
688 msg_Err( p_access, "error while asking for file %d",
689 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
690 var_buffer_free( &buffer );
691 MMSClose( p_access );
695 p_sys->i_flags_broadcast =
696 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
697 p_sys->i_media_length =
698 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
699 p_sys->i_packet_length =
700 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
701 p_sys->i_packet_count =
702 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
703 p_sys->i_max_bit_rate =
704 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
705 p_sys->i_header_size =
706 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
709 "answer 0x06 flags:0x%8.8x media_length:%ds packet_length:%d packet_count:%d max_bit_rate:%d header_size:%d",
710 p_sys->i_flags_broadcast,
711 p_sys->i_media_length,
712 p_sys->i_packet_length,
713 p_sys->i_packet_count,
714 p_sys->i_max_bit_rate,
715 p_sys->i_header_size );
717 /* *** send command 15 *** */
719 var_buffer_reinitwrite( &buffer, 0 );
720 var_buffer_add32( &buffer, 0 );
721 var_buffer_add32( &buffer, 0x8000 );
722 var_buffer_add32( &buffer, 0xffffffff );
723 var_buffer_add32( &buffer, 0x00 );
724 var_buffer_add32( &buffer, 0x00 );
725 var_buffer_add32( &buffer, 0x00 );
726 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
727 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
728 var_buffer_add32( &buffer, 0x00 );
729 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
730 buffer.p_data, buffer.i_data );
732 /* *** wait for reponse *** */
733 /* Commented out because it fails on some stream (no 0x11 answer) */
735 mms_CommandRead( p_access, 0x11, 0 );
737 if( p_sys->i_command != 0x11 )
740 "unknown answer (0x%x instead of 0x11)",
742 var_buffer_free( &buffer );
743 MMSClose( p_access );
748 /* *** now read header packet *** */
749 /* XXX could be split over multiples packets */
750 msg_Dbg( p_access, "reading header" );
753 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
755 msg_Err( p_access, "cannot receive header" );
756 var_buffer_free( &buffer );
757 MMSClose( p_access );
760 if( p_sys->i_header >= p_sys->i_header_size )
763 "header complete(%d)",
768 "header incomplete (%d/%d), reading more",
770 p_sys->i_header_size );
773 /* *** parse header and get stream and their id *** */
774 /* get all streams properties,
776 * TODO : stream bitrates properties(optional)
777 * and bitrate mutual exclusion(optional) */
778 E_( asf_HeaderParse )( &p_sys->asfh,
779 p_sys->p_header, p_sys->i_header );
780 E_( asf_StreamSelect)( &p_sys->asfh,
781 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
782 var_CreateGetInteger( p_access, "mms-all" ),
783 var_CreateGetInteger( p_access, "audio" ),
784 var_CreateGetInteger( p_access, "video" ) );
786 /* *** now select stream we want to receive *** */
787 /* TODO take care of stream bitrate TODO */
790 var_buffer_reinitwrite( &buffer, 0 );
791 /* for now, select first audio and video stream */
792 for( i = 1; i < 128; i++ )
795 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
800 var_buffer_add16( &buffer, 0xffff );
801 var_buffer_add16( &buffer, i );
807 if( p_sys->asfh.stream[i].i_selected )
809 var_buffer_add16( &buffer, 0x0000 );
811 "selecting stream[0x%x] %s (%d kb/s)",
813 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
815 p_sys->asfh.stream[i].i_bitrate / 1024);
819 var_buffer_add16( &buffer, 0x0002 );
821 "ignoring stream[0x%x] %s (%d kb/s)",
823 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
825 p_sys->asfh.stream[i].i_bitrate / 1024);
833 msg_Err( p_access, "cannot find any stream" );
834 var_buffer_free( &buffer );
835 MMSClose( p_access );
838 mms_CommandSend( p_access, 0x33,
840 0xffff | ( i_first << 16 ),
841 buffer.p_data, buffer.i_data );
843 mms_CommandRead( p_access, 0x21, 0 );
844 if( p_sys->i_command != 0x21 )
847 "unknown answer (0x%x instead of 0x21)",
849 var_buffer_free( &buffer );
850 MMSClose( p_access );
855 var_buffer_free( &buffer );
857 msg_Info( p_access, "connection sucessful" );
862 /****************************************************************************
863 * MMSStart : Start streaming
864 ****************************************************************************/
865 static int MMSStart( access_t *p_access, uint32_t i_packet )
867 access_sys_t *p_sys = p_access->p_sys;
870 /* *** start stream from packet 0 *** */
871 var_buffer_initwrite( &buffer, 0 );
872 var_buffer_add64( &buffer, 0 ); /* seek point in second */
873 var_buffer_add32( &buffer, 0xffffffff );
874 var_buffer_add32( &buffer, i_packet ); // begin from start
875 var_buffer_add8( &buffer, 0xff ); // stream time limit
876 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
877 var_buffer_add8( &buffer, 0xff ); //
878 var_buffer_add8( &buffer, 0x00 ); // don't use limit
879 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
881 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
882 buffer.p_data, buffer.i_data );
884 var_buffer_free( &buffer );
886 mms_CommandRead( p_access, 0x05, 0 );
888 if( p_sys->i_command != 0x05 )
891 "unknown answer (0x%x instead of 0x05)",
898 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
899 msg_Dbg( p_access, "Streaming started" );
904 /****************************************************************************
905 * MMSStop : Stop streaming
906 ****************************************************************************/
907 static int MMSStop( access_t *p_access )
909 access_sys_t *p_sys = p_access->p_sys;
911 /* *** stop stream but keep connection alive *** */
912 mms_CommandSend( p_access,
914 p_sys->i_command_level,
920 /****************************************************************************
921 * MMSClose : Close streaming and connection
922 ****************************************************************************/
923 static void MMSClose( access_t *p_access )
925 access_sys_t *p_sys = p_access->p_sys;
927 msg_Dbg( p_access, "Connection closed" );
929 /* *** tell server that we will disconnect *** */
930 mms_CommandSend( p_access,
932 p_sys->i_command_level,
936 /* *** close sockets *** */
937 net_Close( p_sys->i_handle_tcp );
938 if( p_sys->i_proto == MMS_PROTO_UDP )
940 net_Close( p_sys->i_handle_udp );
943 FREE( p_sys->p_cmd );
944 FREE( p_sys->p_media );
945 FREE( p_sys->p_header );
947 FREE( p_sys->psz_server_version );
948 FREE( p_sys->psz_tool_version );
949 FREE( p_sys->psz_update_player_url );
950 FREE( p_sys->psz_encryption_type );
953 /****************************************************************************
955 * MMS specific functions
957 ****************************************************************************/
958 static int mms_CommandSend( access_t *p_access,
960 uint32_t i_prefix1, uint32_t i_prefix2,
961 uint8_t *p_data, int i_data )
965 access_sys_t *p_sys = p_access->p_sys;
968 i_data_by8 = ( i_data + 7 ) / 8;
970 /* first init uffer */
971 var_buffer_initwrite( &buffer, 0 );
973 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
974 var_buffer_add32( &buffer, 0xB00BFACE );
975 /* size after protocol type */
976 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
977 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
978 var_buffer_add32( &buffer, i_data_by8 + 4 );
979 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
980 var_buffer_add64( &buffer, 0 );
981 var_buffer_add32( &buffer, i_data_by8 + 2 );
982 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
983 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
984 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
986 /* specific command data */
987 if( p_data && i_data > 0 )
989 var_buffer_addmemory( &buffer, p_data, i_data );
993 if( send( p_sys->i_handle_tcp,
998 msg_Err( p_access, "failed to send command" );
1002 var_buffer_free( &buffer );
1006 static int NetFillBuffer( access_t *p_access )
1012 access_sys_t *p_sys = p_access->p_sys;
1013 struct timeval timeout;
1014 fd_set fds_r, fds_e;
1017 /* FIXME when using udp */
1018 ssize_t i_tcp, i_udp;
1019 ssize_t i_tcp_read, i_udp_read;
1023 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
1025 if( p_sys->i_proto == MMS_PROTO_UDP )
1027 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1031 i_udp = 0; /* there isn't udp socket */
1037 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1039 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1041 if( i_handle_max == 0 )
1043 msg_Warn( p_access, "nothing to read %d:%d", i_tcp, i_udp );
1048 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1051 /* Find if some data is available */
1056 /* Initialize file descriptor set */
1062 FD_SET( p_sys->i_handle_tcp, &fds_r );
1063 FD_SET( p_sys->i_handle_tcp, &fds_e );
1067 FD_SET( p_sys->i_handle_udp, &fds_r );
1068 FD_SET( p_sys->i_handle_udp, &fds_e );
1071 /* We'll wait 0.5 second if nothing happens */
1073 timeout.tv_usec = 500000;
1075 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1080 if( p_access->b_die || p_access->b_error ) return -1;
1082 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1084 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1085 (i_ret < 0 && errno == EINTR) );
1089 msg_Err( p_access, "network select error (%s)", strerror(errno) );
1093 i_tcp_read = i_udp_read = 0;
1095 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1098 recv( p_sys->i_handle_tcp,
1099 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1100 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1103 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1105 i_udp_read = recv( p_sys->i_handle_udp,
1106 p_sys->buffer_udp + p_sys->i_buffer_udp,
1107 i_udp + MMS_BUFFER_SIZE/2, 0 );
1111 if( p_sys->i_proto == MMS_PROTO_UDP )
1113 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1114 p_sys->i_buffer_tcp, i_tcp_read,
1115 p_sys->i_buffer_udp, i_udp_read );
1119 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1120 p_sys->i_buffer_tcp, i_tcp_read );
1124 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1125 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1127 return i_tcp_read + i_udp_read;
1131 static int mms_ParseCommand( access_t *p_access,
1136 #define GET32( i_pos ) \
1137 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1138 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1139 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1141 access_sys_t *p_sys = p_access->p_sys;
1147 free( p_sys->p_cmd );
1149 p_sys->i_cmd = i_data;
1150 p_sys->p_cmd = malloc( i_data );
1151 memcpy( p_sys->p_cmd, p_data, i_data );
1153 *pi_used = i_data; /* by default */
1155 if( i_data < MMS_CMD_HEADERSIZE )
1157 msg_Warn( p_access, "truncated command (header incomplete)" );
1158 p_sys->i_command = 0;
1161 i_id = GetDWLE( p_data + 4 );
1162 i_length = GetDWLE( p_data + 8 ) + 16;
1164 if( i_id != 0xb00bface )
1167 "incorrect command header (0x%x)", i_id );
1168 p_sys->i_command = 0;
1172 if( i_length > p_sys->i_cmd )
1175 "truncated command (missing %d bytes)",
1176 i_length - i_data );
1177 p_sys->i_command = 0;
1180 else if( i_length < p_sys->i_cmd )
1182 p_sys->i_cmd = i_length;
1183 *pi_used = i_length;
1187 "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",
1191 /* 12: protocol type "MMS " */
1194 /* 24: unknown (0) */
1195 /* 28: unknown (0) */
1201 p_sys->i_command = GET32( 36 ) & 0xffff;
1204 return MMS_PACKET_CMD;
1207 static int mms_ParsePacket( access_t *p_access,
1208 uint8_t *p_data, size_t i_data,
1211 access_sys_t *p_sys = p_access->p_sys;
1212 int i_packet_seq_num;
1213 size_t i_packet_length;
1214 uint32_t i_packet_id;
1219 *pi_used = i_data; /* default */
1222 msg_Warn( p_access, "truncated packet (header incomplete)" );
1226 i_packet_id = p_data[4];
1227 i_packet_seq_num = GetDWLE( p_data );
1228 i_packet_length = GetWLE( p_data + 6 );
1230 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1232 if( i_packet_length > i_data || i_packet_length <= 8)
1234 /* msg_Dbg( p_access,
1235 "truncated packet (Declared %d bytes, Actual %d bytes)",
1236 i_packet_length, i_data ); */
1240 else if( i_packet_length < i_data )
1242 *pi_used = i_packet_length;
1245 if( i_packet_id == 0xff )
1248 "receive MMS UDP pair timing" );
1249 return( MMS_PACKET_UDP_TIMING );
1252 if( i_packet_id != p_sys->i_header_packet_id_type &&
1253 i_packet_id != p_sys->i_media_packet_id_type )
1255 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1259 /* we now have a media or a header packet */
1260 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1261 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1263 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1265 /* FIXME for udp could be just wrong order ? */
1267 "detected packet lost (%d != %d)",
1269 p_sys->i_packet_seq_num );
1270 p_sys->i_packet_seq_num = i_packet_seq_num;
1272 p_sys->i_packet_seq_num++;
1274 if( i_packet_id == p_sys->i_header_packet_id_type )
1276 if( p_sys->p_header )
1278 p_sys->p_header = realloc( p_sys->p_header,
1279 p_sys->i_header + i_packet_length - 8 );
1280 memcpy( &p_sys->p_header[p_sys->i_header],
1282 i_packet_length - 8 );
1283 p_sys->i_header += i_packet_length - 8;
1289 p_sys->p_header = p_packet;
1290 p_sys->i_header = i_packet_length - 8;
1292 /* msg_Dbg( p_access,
1293 "receive header packet (%d bytes)",
1294 i_packet_length - 8 ); */
1296 return MMS_PACKET_HEADER;
1300 FREE( p_sys->p_media );
1301 p_sys->p_media = p_packet;
1302 p_sys->i_media = i_packet_length - 8;
1303 p_sys->i_media_used = 0;
1304 /* msg_Dbg( p_access,
1305 "receive media packet (%d bytes)",
1306 i_packet_length - 8 ); */
1308 return MMS_PACKET_MEDIA;
1312 static int mms_ReceivePacket( access_t *p_access )
1314 access_sys_t *p_sys = p_access->p_sys;
1315 int i_packet_tcp_type;
1316 int i_packet_udp_type;
1320 vlc_bool_t b_refill = VLC_TRUE;
1322 /* first if we need to refill buffer */
1323 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1325 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1327 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1328 p_sys->i_buffer_tcp )
1330 b_refill = VLC_FALSE;
1333 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1335 b_refill = VLC_FALSE;
1338 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1339 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1341 b_refill = VLC_FALSE;
1344 if( b_refill && NetFillBuffer( p_access ) < 0 )
1346 msg_Warn( p_access, "cannot fill buffer" );
1350 i_packet_tcp_type = -1;
1351 i_packet_udp_type = -1;
1353 if( p_sys->i_buffer_tcp > 0 )
1357 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1360 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1361 p_sys->i_buffer_tcp, &i_used );
1367 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1368 p_sys->i_buffer_tcp, &i_used );
1370 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1372 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1373 MMS_BUFFER_SIZE - i_used );
1375 p_sys->i_buffer_tcp -= i_used;
1377 else if( p_sys->i_buffer_udp > 0 )
1382 mms_ParsePacket( p_access, p_sys->buffer_udp,
1383 p_sys->i_buffer_udp, &i_used );
1385 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1387 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1388 MMS_BUFFER_SIZE - i_used );
1390 p_sys->i_buffer_udp -= i_used;
1393 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1395 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1396 i_packet_tcp_type = -1;
1399 if( i_packet_tcp_type != -1 )
1401 return i_packet_tcp_type;
1403 else if( i_packet_udp_type != -1 )
1405 return i_packet_udp_type;
1410 static int mms_ReceiveCommand( access_t *p_access )
1412 access_sys_t *p_sys = p_access->p_sys;
1419 if( NetFillBuffer( p_access ) < 0 )
1421 msg_Warn( p_access, "cannot fill buffer" );
1422 return VLC_EGENERIC;
1424 if( p_sys->i_buffer_tcp > 0 )
1426 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1427 p_sys->i_buffer_tcp, &i_used );
1428 if( i_used < MMS_BUFFER_SIZE )
1430 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1431 MMS_BUFFER_SIZE - i_used );
1433 p_sys->i_buffer_tcp -= i_used;
1437 return VLC_EGENERIC;
1440 if( p_sys->i_command == 0x1b )
1442 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1451 return VLC_EGENERIC;
1458 #define MMS_RETRY_MAX 10
1459 #define MMS_RETRY_SLEEP 50000
1461 static int mms_CommandRead( access_t *p_access, int i_command1,
1464 access_sys_t *p_sys = p_access->p_sys;
1468 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1470 i_status = mms_ReceiveCommand( p_access );
1471 if( i_status < 0 || p_sys->i_command == 0 )
1474 msleep( MMS_RETRY_SLEEP );
1476 else if( i_command1 == 0 && i_command2 == 0)
1480 else if( p_sys->i_command == i_command1 ||
1481 p_sys->i_command == i_command2 )
1487 switch( p_sys->i_command )
1490 msg_Warn( p_access, "socket closed by server" );
1491 p_access->info.b_eof = 1;
1492 return VLC_EGENERIC;
1494 msg_Warn( p_access, "end of media stream" );
1495 p_access->info.b_eof = 1;
1496 return VLC_EGENERIC;
1502 msg_Warn( p_access, "failed to receive command (aborting)" );
1504 return VLC_EGENERIC;
1508 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1510 access_sys_t *p_sys = p_access->p_sys;
1513 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1517 if( p_access->b_die )
1520 i_status = mms_ReceivePacket( p_access );
1524 msg_Warn( p_access, "cannot receive header (%d/%d)",
1525 i_count, MMS_RETRY_MAX );
1526 msleep( MMS_RETRY_SLEEP );
1528 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1532 else if( i_status == MMS_PACKET_CMD )
1534 switch( p_sys->i_command )
1537 msg_Warn( p_access, "socket closed by server" );
1538 p_access->info.b_eof = 1;
1541 msg_Warn( p_access, "end of media stream" );
1542 p_access->info.b_eof = 1;
1545 /* XXX not too dificult to be done EXCEPT that we
1546 * need to restart demuxer... and I don't see how we
1547 * could do that :p */
1549 "reinitialization needed --> unsupported" );
1550 p_access->info.b_eof = VLC_TRUE;
1558 msg_Err( p_access, "cannot receive %s (aborting)",
1559 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );