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 *****************************************************************************/
30 #include <sys/types.h>
37 #include <vlc/input.h>
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
51 # include <netinet/in.h>
53 # include <arpa/inet.h>
54 # elif defined( SYS_BEOS )
55 # include <net/netdb.h>
68 /****************************************************************************
70 * MMSProtocole documentation found at http://get.to/sdp
71 ****************************************************************************/
73 /*****************************************************************************
75 *****************************************************************************/
76 int E_( MMSTUOpen ) ( access_t * );
77 void E_( MMSTUClose ) ( access_t * );
80 static int Read( access_t *, uint8_t *, int );
81 static int Seek( access_t *, int64_t );
82 static int Control( access_t *, int, va_list );
84 static int MMSOpen ( access_t *, vlc_url_t *, int );
85 static int MMSStart( access_t *, uint32_t );
86 static int MMSStop ( access_t * );
87 static void MMSClose( access_t * );
90 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
91 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
93 static int mms_HeaderMediaRead( access_t *, int );
95 static int mms_ReceivePacket( access_t * );
99 * XXX DON'T FREE MY MEMORY !!! XXX
103 * Ok, ok, j'le ferai plus...
109 * Vous pourriez signer vos commentaires (même si on voit bien qui peut
110 * écrire ce genre de trucs :p), et écrire en anglais, bordel de
114 * Alors la ouai ç'est fou les gens qui écrivent des commentaires sans les
115 * signer. Ca mériterait un coup de pied dans le cul ça :)
118 int E_(MMSTUOpen)( access_t *p_access )
125 /* Set up p_access */
126 p_access->pf_read = Read;
127 p_access->pf_block = NULL;
128 p_access->pf_control = Control;
129 p_access->pf_seek = Seek;
130 p_access->info.i_update = 0;
131 p_access->info.i_size = 0;
132 p_access->info.i_pos = 0;
133 p_access->info.b_eof = VLC_FALSE;
134 p_access->info.i_title = 0;
135 p_access->info.i_seekpoint = 0;
136 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
137 memset( p_sys, 0, sizeof( access_sys_t ) );
139 /* *** Parse URL and get server addr/port and path *** */
140 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
141 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
143 msg_Err( p_access, "invalid server name" );
144 vlc_UrlClean( &p_sys->url );
147 if( p_sys->url.i_port <= 0 )
149 p_sys->url.i_port = 1755;
152 /* *** connect to this server *** */
153 /* look at requested protocol (udp/tcp) */
154 i_proto = MMS_PROTO_AUTO;
155 if( *p_access->psz_access )
157 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
159 i_proto = MMS_PROTO_UDP;
161 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
163 i_proto = MMS_PROTO_TCP;
168 if( i_proto == MMS_PROTO_AUTO )
169 { /* first try with TCP and then UDP*/
170 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
172 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
177 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
182 msg_Err( p_access, "cannot connect to server" );
183 vlc_UrlClean( &p_sys->url );
187 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
191 * broadcast yy=0x02, xx= 0x00
192 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
194 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
196 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
198 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
200 p_sys->b_seekable = VLC_FALSE;
204 p_sys->b_seekable = VLC_TRUE;
205 p_access->info.i_size =
206 (uint64_t)p_sys->i_header +
207 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
210 /* *** Start stream *** */
211 if( MMSStart( p_access, 0xffffffff ) < 0 )
213 msg_Err( p_access, "cannot start stream" );
214 MMSClose( p_access );
215 vlc_UrlClean( &p_sys->url );
219 /* Update default_pts to a suitable value for mms access */
220 var_Get( p_access, "mms-caching", &val );
225 /*****************************************************************************
226 * Close: free unused data structures
227 *****************************************************************************/
228 void E_(MMSTUClose)( access_t *p_access )
230 access_sys_t *p_sys = p_access->p_sys;
232 /* close connection with server */
233 MMSClose( p_access );
236 vlc_UrlClean( &p_sys->url );
241 /*****************************************************************************
243 *****************************************************************************/
244 static int Control( access_t *p_access, int i_query, va_list args )
246 access_sys_t *p_sys = p_access->p_sys;
255 case ACCESS_CAN_SEEK:
256 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
257 *pb_bool = p_sys->b_seekable;
259 case ACCESS_CAN_FASTSEEK:
260 case ACCESS_CAN_PAUSE:
261 case ACCESS_CAN_CONTROL_PACE:
262 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
263 *pb_bool = VLC_FALSE;
268 pi_int = (int*)va_arg( args, int * );
269 *pi_int = 3 * p_sys->i_packet_length;
272 case ACCESS_GET_PTS_DELAY:
273 pi_64 = (int64_t*)va_arg( args, int64_t * );
274 var_Get( p_access, "mms-caching", &val );
275 *pi_64 = val.i_int * 1000;
279 case ACCESS_SET_PAUSE_STATE:
280 case ACCESS_GET_TITLE_INFO:
281 case ACCESS_SET_TITLE:
282 case ACCESS_SET_SEEKPOINT:
286 msg_Err( p_access, "unimplemented query in control" );
293 /*****************************************************************************
294 * Seek: try to go at the right place
295 *****************************************************************************/
296 static int Seek( access_t * p_access, int64_t i_pos )
298 access_sys_t *p_sys = p_access->p_sys;
306 if( i_pos < p_sys->i_header)
309 if( p_access->info.i_pos < p_sys->i_header )
311 /* no need to restart stream, it was already one
312 * or no stream was yet read */
313 p_access->info.i_pos = i_pos;
318 i_packet = 0xffffffff;
324 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
325 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
327 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
330 msg_Dbg( p_access, "stream stopped (seek)" );
332 /* *** restart stream *** */
333 var_buffer_initwrite( &buffer, 0 );
334 var_buffer_add64( &buffer, 0 ); /* seek point in second */
335 var_buffer_add32( &buffer, 0xffffffff );
336 var_buffer_add32( &buffer, i_packet ); // begin from start
337 var_buffer_add8( &buffer, 0xff ); // stream time limit
338 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
339 var_buffer_add8( &buffer, 0xff ); //
340 var_buffer_add8( &buffer, 0x00 ); // don't use limit
341 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
343 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
344 buffer.p_data, buffer.i_data );
346 var_buffer_free( &buffer );
349 while( !p_access->b_die )
351 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
352 if( p_sys->i_command == 0x1e )
354 msg_Dbg( p_access, "received 0x1e (seek)" );
359 while( !p_access->b_die )
361 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
362 if( p_sys->i_command == 0x05 )
364 msg_Dbg( p_access, "received 0x05 (seek)" );
370 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
371 msg_Dbg( p_access, "Streaming restarted" );
373 p_sys->i_media_used += i_offset;
374 p_access->info.i_pos = i_pos;
375 p_access->info.b_eof = VLC_FALSE;
380 /*****************************************************************************
382 *****************************************************************************/
383 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
385 access_sys_t *p_sys = p_access->p_sys;
391 /* *** now send data if needed *** */
392 while( i_data < i_len )
394 if( p_access->info.i_pos < p_sys->i_header )
396 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
397 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
399 p_access->info.i_pos += i_copy;
401 else if( p_sys->i_media_used < p_sys->i_media )
403 i_copy = __MIN( i_len - i_data ,
404 p_sys->i_media - p_sys->i_media_used );
405 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
407 p_sys->i_media_used += i_copy;
408 p_access->info.i_pos += i_copy;
410 else if( p_sys->p_media != NULL &&
411 p_sys->i_media_used < p_sys->i_packet_length )
413 i_copy = __MIN( i_len - i_data,
414 p_sys->i_packet_length - p_sys->i_media_used);
415 memset( &p_buffer[i_data], 0, i_copy );
418 p_sys->i_media_used += i_copy;
419 p_access->info.i_pos += i_copy;
421 else if( p_access->info.b_eof ||
422 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
431 /****************************************************************************
432 * MMSOpen : Open a connection with the server over mmst or mmsu
433 ****************************************************************************/
434 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
436 access_sys_t *p_sys = p_access->p_sys;
437 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
442 int i_server_version;
444 int i_update_player_url;
445 int i_encryption_type;
451 /* *** Open a TCP connection with server *** */
452 msg_Dbg( p_access, "waiting for connection..." );
453 p_sys->i_handle_tcp = net_OpenTCP( p_access, p_url->psz_host, p_url->i_port );
454 if( p_sys->i_handle_tcp < 0 )
456 msg_Err( p_access, "failed to open a connection (tcp)" );
460 "connection(tcp) with \"%s:%d\" successful",
464 /* *** Bind port if UDP protocol is selected *** */
467 struct sockaddr_in name;
468 socklen_t i_namelen = sizeof( struct sockaddr_in );
470 if( getsockname( p_sys->i_handle_tcp,
471 (struct sockaddr*)&name, &i_namelen ) < 0 )
473 net_Close( p_sys->i_handle_tcp );
476 p_sys->psz_bind_addr = inet_ntoa( name.sin_addr );
478 p_sys->i_handle_udp = net_OpenUDP( p_access, p_sys->psz_bind_addr, 7000, "", 0 );
479 if( p_sys->i_handle_udp < 0 )
481 msg_Err( p_access, "failed to open a connection (udp)" );
482 net_Close( p_sys->i_handle_tcp );
486 "connection(udp) at \"%s:%d\" successful",
487 p_sys->psz_bind_addr, 7000 );
491 p_sys->psz_bind_addr = NULL;
494 /* *** Init context for mms prototcol *** */
495 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
497 "generated guid: "GUID_FMT,
498 GUID_PRINT( p_sys->guid ) );
499 p_sys->i_command_level = 1; /* updated after 0x1A command */
500 p_sys->i_seq_num = 0;
501 p_sys->i_media_packet_id_type = 0x04;
502 p_sys->i_header_packet_id_type = 0x02;
503 p_sys->i_proto = i_proto;
504 p_sys->i_packet_seq_num = 0;
505 p_sys->p_header = NULL;
507 p_sys->p_media = NULL;
509 p_sys->i_media_used = 0;
511 p_access->info.i_pos = 0;
512 p_sys->i_buffer_tcp = 0;
513 p_sys->i_buffer_udp = 0;
516 p_access->info.b_eof = 0;
518 /* *** send command 1 : connection request *** */
519 var_buffer_initwrite( &buffer, 0 );
520 var_buffer_add16( &buffer, 0x001c );
521 var_buffer_add16( &buffer, 0x0003 );
523 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
524 GUID_PRINT( p_sys->guid ),
526 var_buffer_addUTF16( &buffer, tmp );
528 mms_CommandSend( p_access,
529 0x01, /* connexion request */
530 0x00000000, /* flags, FIXME */
531 0x0004000b, /* ???? */
535 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
537 var_buffer_free( &buffer );
538 MMSClose( p_access );
542 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
543 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
544 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
545 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
546 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
547 #define GETUTF16( psz, size ) \
550 psz = malloc( size + 1); \
551 for( i = 0; i < size; i++ ) \
558 GETUTF16( p_sys->psz_server_version, i_server_version );
559 GETUTF16( p_sys->psz_tool_version, i_tool_version );
560 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
561 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
564 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
565 p_sys->psz_server_version,
566 p_sys->psz_tool_version,
567 p_sys->psz_update_player_url,
568 p_sys->psz_encryption_type );
570 /* *** should make an 18 command to make data timing *** */
572 /* *** send command 2 : transport protocol selection *** */
573 var_buffer_reinitwrite( &buffer, 0 );
574 var_buffer_add32( &buffer, 0x00000000 );
575 var_buffer_add32( &buffer, 0x000a0000 );
576 var_buffer_add32( &buffer, 0x00000002 );
581 p_sys->psz_bind_addr,
586 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
588 var_buffer_addUTF16( &buffer, tmp );
589 var_buffer_add16( &buffer, '0' );
591 mms_CommandSend( p_access,
592 0x02, /* connexion request */
593 0x00000000, /* flags, FIXME */
594 0xffffffff, /* ???? */
598 /* *** response from server, should be 0x02 or 0x03 *** */
599 mms_CommandRead( p_access, 0x02, 0x03 );
600 if( p_sys->i_command == 0x03 )
603 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
604 var_buffer_free( &buffer );
605 MMSClose( p_access );
608 else if( p_sys->i_command != 0x02 )
610 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
613 /* *** send command 5 : media file name/path requested *** */
614 var_buffer_reinitwrite( &buffer, 0 );
615 var_buffer_add64( &buffer, 0 );
616 var_buffer_addUTF16( &buffer, p_url->psz_path );
618 mms_CommandSend( p_access,
620 p_sys->i_command_level,
625 /* *** wait for reponse *** */
626 mms_CommandRead( p_access, 0x1a, 0x06 );
628 /* test if server send 0x1A answer */
629 if( p_sys->i_command == 0x1A )
631 msg_Err( p_access, "id/password requested (not yet supported)" );
633 var_buffer_free( &buffer );
634 MMSClose( p_access );
637 if( p_sys->i_command != 0x06 )
640 "unknown answer (0x%x instead of 0x06)",
642 var_buffer_free( &buffer );
643 MMSClose( p_access );
647 /* 1 for file ok, 2 for authen ok */
648 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
651 msg_Dbg( p_access, "Media file name/path accepted" );
654 msg_Dbg( p_access, "Authentication accepted" );
658 msg_Err( p_access, "error while asking for file %d",
659 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
660 var_buffer_free( &buffer );
661 MMSClose( p_access );
665 p_sys->i_flags_broadcast =
666 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
667 p_sys->i_media_length =
668 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
669 p_sys->i_packet_length =
670 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
671 p_sys->i_packet_count =
672 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
673 p_sys->i_max_bit_rate =
674 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
675 p_sys->i_header_size =
676 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
679 "answer 0x06 flags:0x%8.8x media_length:%ds packet_length:%d packet_count:%d max_bit_rate:%d header_size:%d",
680 p_sys->i_flags_broadcast,
681 p_sys->i_media_length,
682 p_sys->i_packet_length,
683 p_sys->i_packet_count,
684 p_sys->i_max_bit_rate,
685 p_sys->i_header_size );
687 /* *** send command 15 *** */
689 var_buffer_reinitwrite( &buffer, 0 );
690 var_buffer_add32( &buffer, 0 );
691 var_buffer_add32( &buffer, 0x8000 );
692 var_buffer_add32( &buffer, 0xffffffff );
693 var_buffer_add32( &buffer, 0x00 );
694 var_buffer_add32( &buffer, 0x00 );
695 var_buffer_add32( &buffer, 0x00 );
696 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
697 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
698 var_buffer_add32( &buffer, 0x00 );
699 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
700 buffer.p_data, buffer.i_data );
702 /* *** wait for reponse *** */
703 /* Commented out because it fails on some stream (no 0x11 answer) */
705 mms_CommandRead( p_access, 0x11, 0 );
707 if( p_sys->i_command != 0x11 )
710 "unknown answer (0x%x instead of 0x11)",
712 var_buffer_free( &buffer );
713 MMSClose( p_access );
718 /* *** now read header packet *** */
719 /* XXX could be split over multiples packets */
720 msg_Dbg( p_access, "reading header" );
723 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
725 msg_Err( p_access, "cannot receive header" );
726 var_buffer_free( &buffer );
727 MMSClose( p_access );
730 if( p_sys->i_header >= p_sys->i_header_size )
733 "header complete(%d)",
738 "header incomplete (%d/%d), reading more",
740 p_sys->i_header_size );
743 /* *** parse header and get stream and their id *** */
744 /* get all streams properties,
746 * TODO : stream bitrates properties(optional)
747 * and bitrate mutual exclusion(optional) */
748 E_( asf_HeaderParse )( &p_sys->asfh,
749 p_sys->p_header, p_sys->i_header );
750 E_( asf_StreamSelect)( &p_sys->asfh,
751 config_GetInt( p_access, "mms-maxbitrate" ),
752 config_GetInt( p_access, "mms-all" ),
753 config_GetInt( p_access, "audio" ),
754 config_GetInt( p_access, "video" ) );
756 /* *** now select stream we want to receive *** */
757 /* TODO take care of stream bitrate TODO */
760 var_buffer_reinitwrite( &buffer, 0 );
761 /* for now, select first audio and video stream */
762 for( i = 1; i < 128; i++ )
765 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
770 var_buffer_add16( &buffer, 0xffff );
771 var_buffer_add16( &buffer, i );
777 if( p_sys->asfh.stream[i].i_selected )
779 var_buffer_add16( &buffer, 0x0000 );
781 "selecting stream[0x%x] %s (%d kb/s)",
783 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
785 p_sys->asfh.stream[i].i_bitrate / 1024);
789 var_buffer_add16( &buffer, 0x0002 );
791 "ignoring stream[0x%x] %s (%d kb/s)",
793 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
795 p_sys->asfh.stream[i].i_bitrate / 1024);
803 msg_Err( p_access, "cannot find any stream" );
804 var_buffer_free( &buffer );
805 MMSClose( p_access );
808 mms_CommandSend( p_access, 0x33,
810 0xffff | ( i_first << 16 ),
811 buffer.p_data, buffer.i_data );
813 mms_CommandRead( p_access, 0x21, 0 );
814 if( p_sys->i_command != 0x21 )
817 "unknown answer (0x%x instead of 0x21)",
819 var_buffer_free( &buffer );
820 MMSClose( p_access );
825 var_buffer_free( &buffer );
827 msg_Info( p_access, "connection sucessful" );
832 /****************************************************************************
833 * MMSStart : Start streaming
834 ****************************************************************************/
835 static int MMSStart( access_t *p_access, uint32_t i_packet )
837 access_sys_t *p_sys = p_access->p_sys;
840 /* *** start stream from packet 0 *** */
841 var_buffer_initwrite( &buffer, 0 );
842 var_buffer_add64( &buffer, 0 ); /* seek point in second */
843 var_buffer_add32( &buffer, 0xffffffff );
844 var_buffer_add32( &buffer, i_packet ); // begin from start
845 var_buffer_add8( &buffer, 0xff ); // stream time limit
846 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
847 var_buffer_add8( &buffer, 0xff ); //
848 var_buffer_add8( &buffer, 0x00 ); // don't use limit
849 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
851 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
852 buffer.p_data, buffer.i_data );
854 var_buffer_free( &buffer );
856 mms_CommandRead( p_access, 0x05, 0 );
858 if( p_sys->i_command != 0x05 )
861 "unknown answer (0x%x instead of 0x05)",
868 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
869 msg_Dbg( p_access, "Streaming started" );
874 /****************************************************************************
875 * MMSStop : Stop streaming
876 ****************************************************************************/
877 static int MMSStop( access_t *p_access )
879 access_sys_t *p_sys = p_access->p_sys;
881 /* *** stop stream but keep connection alive *** */
882 mms_CommandSend( p_access,
884 p_sys->i_command_level,
890 /****************************************************************************
891 * MMSClose : Close streaming and connection
892 ****************************************************************************/
893 static void MMSClose( access_t *p_access )
895 access_sys_t *p_sys = p_access->p_sys;
897 msg_Dbg( p_access, "Connection closed" );
899 /* *** tell server that we will disconnect *** */
900 mms_CommandSend( p_access,
902 p_sys->i_command_level,
906 /* *** close sockets *** */
907 net_Close( p_sys->i_handle_tcp );
908 if( p_sys->i_proto == MMS_PROTO_UDP )
910 net_Close( p_sys->i_handle_udp );
913 FREE( p_sys->p_cmd );
914 FREE( p_sys->p_media );
915 FREE( p_sys->p_header );
917 FREE( p_sys->psz_server_version );
918 FREE( p_sys->psz_tool_version );
919 FREE( p_sys->psz_update_player_url );
920 FREE( p_sys->psz_encryption_type );
923 /****************************************************************************
925 * MMS specific functions
927 ****************************************************************************/
928 static int mms_CommandSend( access_t *p_access,
930 uint32_t i_prefix1, uint32_t i_prefix2,
931 uint8_t *p_data, int i_data )
935 access_sys_t *p_sys = p_access->p_sys;
938 i_data_by8 = ( i_data + 7 ) / 8;
940 /* first init uffer */
941 var_buffer_initwrite( &buffer, 0 );
943 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
944 var_buffer_add32( &buffer, 0xB00BFACE );
945 /* size after protocol type */
946 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
947 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
948 var_buffer_add32( &buffer, i_data_by8 + 4 );
949 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
950 var_buffer_add64( &buffer, 0 );
951 var_buffer_add32( &buffer, i_data_by8 + 2 );
952 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
953 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
954 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
956 /* specific command data */
957 if( p_data && i_data > 0 )
959 var_buffer_addmemory( &buffer, p_data, i_data );
963 if( send( p_sys->i_handle_tcp,
968 msg_Err( p_access, "failed to send command" );
972 var_buffer_free( &buffer );
976 static int NetFillBuffer( access_t *p_access )
982 access_sys_t *p_sys = p_access->p_sys;
983 struct timeval timeout;
987 /* FIXME when using udp */
988 ssize_t i_tcp, i_udp;
989 ssize_t i_tcp_read, i_udp_read;
993 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
995 if( p_sys->i_proto == MMS_PROTO_UDP )
997 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
1001 i_udp = 0; /* there isn't udp socket */
1007 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
1009 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
1011 if( i_handle_max == 0 )
1013 msg_Warn( p_access, "nothing to read %d:%d", i_tcp, i_udp );
1018 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1021 /* Find if some data is available */
1026 /* Initialize file descriptor set */
1032 FD_SET( p_sys->i_handle_tcp, &fds_r );
1033 FD_SET( p_sys->i_handle_tcp, &fds_e );
1037 FD_SET( p_sys->i_handle_udp, &fds_r );
1038 FD_SET( p_sys->i_handle_udp, &fds_e );
1041 /* We'll wait 0.5 second if nothing happens */
1043 timeout.tv_usec = 500000;
1045 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1050 if( p_access->b_die || p_access->b_error ) return -1;
1052 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1054 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1055 (i_ret < 0 && errno == EINTR) );
1059 msg_Err( p_access, "network select error (%s)", strerror(errno) );
1063 i_tcp_read = i_udp_read = 0;
1065 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1068 recv( p_sys->i_handle_tcp,
1069 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1070 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1073 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1075 i_udp_read = recv( p_sys->i_handle_udp,
1076 p_sys->buffer_udp + p_sys->i_buffer_udp,
1077 i_udp + MMS_BUFFER_SIZE/2, 0 );
1081 if( p_sys->i_proto == MMS_PROTO_UDP )
1083 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1084 p_sys->i_buffer_tcp, i_tcp_read,
1085 p_sys->i_buffer_udp, i_udp_read );
1089 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1090 p_sys->i_buffer_tcp, i_tcp_read );
1094 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1095 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1097 return i_tcp_read + i_udp_read;
1101 static int mms_ParseCommand( access_t *p_access,
1106 #define GET32( i_pos ) \
1107 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1108 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1109 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1111 access_sys_t *p_sys = p_access->p_sys;
1117 free( p_sys->p_cmd );
1119 p_sys->i_cmd = i_data;
1120 p_sys->p_cmd = malloc( i_data );
1121 memcpy( p_sys->p_cmd, p_data, i_data );
1123 *pi_used = i_data; /* by default */
1125 if( i_data < MMS_CMD_HEADERSIZE )
1127 msg_Warn( p_access, "truncated command (header incomplete)" );
1128 p_sys->i_command = 0;
1131 i_id = GetDWLE( p_data + 4 );
1132 i_length = GetDWLE( p_data + 8 ) + 16;
1134 if( i_id != 0xb00bface )
1137 "incorrect command header (0x%x)", i_id );
1138 p_sys->i_command = 0;
1142 if( i_length > p_sys->i_cmd )
1145 "truncated command (missing %d bytes)",
1146 i_length - i_data );
1147 p_sys->i_command = 0;
1150 else if( i_length < p_sys->i_cmd )
1152 p_sys->i_cmd = i_length;
1153 *pi_used = i_length;
1157 "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",
1161 /* 12: protocol type "MMS " */
1164 /* 24: unknown (0) */
1165 /* 28: unknown (0) */
1171 p_sys->i_command = GET32( 36 ) & 0xffff;
1174 return MMS_PACKET_CMD;
1177 static int mms_ParsePacket( access_t *p_access,
1178 uint8_t *p_data, size_t i_data,
1181 access_sys_t *p_sys = p_access->p_sys;
1182 int i_packet_seq_num;
1183 size_t i_packet_length;
1184 uint32_t i_packet_id;
1189 *pi_used = i_data; /* default */
1192 msg_Warn( p_access, "truncated packet (header incomplete)" );
1196 i_packet_id = p_data[4];
1197 i_packet_seq_num = GetDWLE( p_data );
1198 i_packet_length = GetWLE( p_data + 6 );
1200 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1202 if( i_packet_length > i_data || i_packet_length <= 8)
1205 "truncated packet (missing %d bytes)",
1206 i_packet_length - i_data );
1210 else if( i_packet_length < i_data )
1212 *pi_used = i_packet_length;
1215 if( i_packet_id == 0xff )
1218 "receive MMS UDP pair timing" );
1219 return( MMS_PACKET_UDP_TIMING );
1222 if( i_packet_id != p_sys->i_header_packet_id_type &&
1223 i_packet_id != p_sys->i_media_packet_id_type )
1225 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1229 /* we now have a media or a header packet */
1230 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1231 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1233 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1235 /* FIXME for udp could be just wrong order ? */
1237 "detected packet lost (%d != %d)",
1239 p_sys->i_packet_seq_num );
1240 p_sys->i_packet_seq_num = i_packet_seq_num;
1242 p_sys->i_packet_seq_num++;
1244 if( i_packet_id == p_sys->i_header_packet_id_type )
1246 if( p_sys->p_header )
1248 p_sys->p_header = realloc( p_sys->p_header,
1249 p_sys->i_header + i_packet_length - 8 );
1250 memcpy( &p_sys->p_header[p_sys->i_header],
1252 i_packet_length - 8 );
1253 p_sys->i_header += i_packet_length - 8;
1259 p_sys->p_header = p_packet;
1260 p_sys->i_header = i_packet_length - 8;
1262 /* msg_Dbg( p_access,
1263 "receive header packet (%d bytes)",
1264 i_packet_length - 8 ); */
1266 return MMS_PACKET_HEADER;
1270 FREE( p_sys->p_media );
1271 p_sys->p_media = p_packet;
1272 p_sys->i_media = i_packet_length - 8;
1273 p_sys->i_media_used = 0;
1274 /* msg_Dbg( p_access,
1275 "receive media packet (%d bytes)",
1276 i_packet_length - 8 ); */
1278 return MMS_PACKET_MEDIA;
1282 static int mms_ReceivePacket( access_t *p_access )
1284 access_sys_t *p_sys = p_access->p_sys;
1285 int i_packet_tcp_type;
1286 int i_packet_udp_type;
1290 vlc_bool_t b_refill = VLC_TRUE;
1292 /* first if we need to refill buffer */
1293 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1295 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1297 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1298 p_sys->i_buffer_tcp )
1300 b_refill = VLC_FALSE;
1303 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1305 b_refill = VLC_FALSE;
1308 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1309 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1311 b_refill = VLC_FALSE;
1314 if( b_refill && NetFillBuffer( p_access ) < 0 )
1316 msg_Warn( p_access, "cannot fill buffer" );
1320 i_packet_tcp_type = -1;
1321 i_packet_udp_type = -1;
1323 if( p_sys->i_buffer_tcp > 0 )
1327 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1330 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1331 p_sys->i_buffer_tcp, &i_used );
1337 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1338 p_sys->i_buffer_tcp, &i_used );
1340 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1342 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1343 MMS_BUFFER_SIZE - i_used );
1345 p_sys->i_buffer_tcp -= i_used;
1347 else if( p_sys->i_buffer_udp > 0 )
1352 mms_ParsePacket( p_access, p_sys->buffer_udp,
1353 p_sys->i_buffer_udp, &i_used );
1355 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1357 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1358 MMS_BUFFER_SIZE - i_used );
1360 p_sys->i_buffer_udp -= i_used;
1363 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1365 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1366 i_packet_tcp_type = -1;
1369 if( i_packet_tcp_type != -1 )
1371 return i_packet_tcp_type;
1373 else if( i_packet_udp_type != -1 )
1375 return i_packet_udp_type;
1380 static int mms_ReceiveCommand( access_t *p_access )
1382 access_sys_t *p_sys = p_access->p_sys;
1389 if( NetFillBuffer( p_access ) < 0 )
1391 msg_Warn( p_access, "cannot fill buffer" );
1392 return VLC_EGENERIC;
1394 if( p_sys->i_buffer_tcp > 0 )
1396 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1397 p_sys->i_buffer_tcp, &i_used );
1398 if( i_used < MMS_BUFFER_SIZE )
1400 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1401 MMS_BUFFER_SIZE - i_used );
1403 p_sys->i_buffer_tcp -= i_used;
1407 return VLC_EGENERIC;
1410 if( p_sys->i_command == 0x1b )
1412 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1421 return VLC_EGENERIC;
1428 #define MMS_RETRY_MAX 10
1429 #define MMS_RETRY_SLEEP 50000
1431 static int mms_CommandRead( access_t *p_access, int i_command1,
1434 access_sys_t *p_sys = p_access->p_sys;
1438 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1440 i_status = mms_ReceiveCommand( p_access );
1441 if( i_status < 0 || p_sys->i_command == 0 )
1444 msleep( MMS_RETRY_SLEEP );
1446 else if( i_command1 == 0 && i_command2 == 0)
1450 else if( p_sys->i_command == i_command1 ||
1451 p_sys->i_command == i_command2 )
1457 switch( p_sys->i_command )
1460 msg_Warn( p_access, "socket closed by server" );
1461 p_access->info.b_eof = 1;
1462 return VLC_EGENERIC;
1464 msg_Warn( p_access, "end of media stream" );
1465 p_access->info.b_eof = 1;
1466 return VLC_EGENERIC;
1472 msg_Warn( p_access, "failed to receive command (aborting)" );
1474 return VLC_EGENERIC;
1478 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1480 access_sys_t *p_sys = p_access->p_sys;
1483 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1487 if( p_access->b_die )
1490 i_status = mms_ReceivePacket( p_access );
1494 msg_Warn( p_access, "cannot receive header (%d/%d)",
1495 i_count, MMS_RETRY_MAX );
1496 msleep( MMS_RETRY_SLEEP );
1498 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1502 else if( i_status == MMS_PACKET_CMD )
1504 switch( p_sys->i_command )
1507 msg_Warn( p_access, "socket closed by server" );
1508 p_access->info.b_eof = 1;
1511 msg_Warn( p_access, "end of media stream" );
1512 p_access->info.b_eof = 1;
1515 /* XXX not too dificult to be done EXCEPT that we
1516 * need to restart demuxer... and I don't see how we
1517 * could do that :p */
1519 "reinitialization needed --> unsupported" );
1520 p_access->info.b_eof = VLC_TRUE;
1528 msg_Err( p_access, "cannot receive %s (aborting)",
1529 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );