1 /*****************************************************************************
2 * mms.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc_access.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
49 #include <vlc_network.h>
59 /****************************************************************************
61 * MMSProtocole documentation found at http://get.to/sdp
62 ****************************************************************************/
64 /*****************************************************************************
66 *****************************************************************************/
67 int E_( MMSTUOpen ) ( access_t * );
68 void E_( MMSTUClose ) ( access_t * );
71 static int Read( access_t *, uint8_t *, int );
72 static int Seek( access_t *, int64_t );
73 static int Control( access_t *, int, va_list );
75 static int MMSOpen ( access_t *, vlc_url_t *, int );
76 static int MMSStart( access_t *, uint32_t );
77 static int MMSStop ( access_t * );
78 static void MMSClose( access_t * );
81 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
82 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
84 static int mms_HeaderMediaRead( access_t *, int );
86 static int mms_ReceivePacket( access_t * );
89 int E_(MMSTUOpen)( access_t *p_access )
96 p_access->pf_read = Read;
97 p_access->pf_block = NULL;
98 p_access->pf_control = Control;
99 p_access->pf_seek = Seek;
100 p_access->info.i_update = 0;
101 p_access->info.i_size = 0;
102 p_access->info.i_pos = 0;
103 p_access->info.b_eof = VLC_FALSE;
104 p_access->info.i_title = 0;
105 p_access->info.i_seekpoint = 0;
106 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
107 memset( p_sys, 0, sizeof( access_sys_t ) );
109 /* *** Parse URL and get server addr/port and path *** */
110 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
111 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
113 msg_Err( p_access, "invalid server name" );
114 vlc_UrlClean( &p_sys->url );
118 if( p_sys->url.i_port <= 0 )
120 p_sys->url.i_port = 1755;
123 /* *** connect to this server *** */
124 /* look at requested protocol (udp/tcp) */
125 i_proto = MMS_PROTO_AUTO;
126 if( *p_access->psz_access )
128 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
130 i_proto = MMS_PROTO_UDP;
132 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
134 i_proto = MMS_PROTO_TCP;
139 if( i_proto == MMS_PROTO_AUTO )
140 { /* first try with TCP and then UDP*/
141 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
143 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
148 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
153 msg_Err( p_access, "cannot connect to server" );
154 vlc_UrlClean( &p_sys->url );
159 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
163 * broadcast yy=0x02, xx= 0x00
164 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
166 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
168 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
170 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
172 p_sys->b_seekable = VLC_FALSE;
176 p_sys->b_seekable = VLC_TRUE;
177 p_access->info.i_size =
178 (uint64_t)p_sys->i_header +
179 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
182 /* *** Start stream *** */
183 if( MMSStart( p_access, 0xffffffff ) < 0 )
185 msg_Err( p_access, "cannot start stream" );
186 E_(MMSTUClose) ( p_access );
192 /*****************************************************************************
193 * Close: free unused data structures
194 *****************************************************************************/
195 void E_(MMSTUClose)( access_t *p_access )
197 access_sys_t *p_sys = p_access->p_sys;
199 /* close connection with server */
200 MMSClose( p_access );
203 vlc_UrlClean( &p_sys->url );
208 /*****************************************************************************
210 *****************************************************************************/
211 static int Control( access_t *p_access, int i_query, va_list args )
213 access_sys_t *p_sys = p_access->p_sys;
223 case ACCESS_CAN_SEEK:
224 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
225 *pb_bool = p_sys->b_seekable;
228 case ACCESS_CAN_FASTSEEK:
229 case ACCESS_CAN_PAUSE:
230 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
231 *pb_bool = VLC_FALSE;
234 case ACCESS_CAN_CONTROL_PACE:
235 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
237 #if 0 /* Disable for now until we have a clock synchro algo
238 * which works with something else than MPEG over UDP */
239 *pb_bool = VLC_FALSE;
246 pi_int = (int*)va_arg( args, int * );
247 *pi_int = 3 * p_sys->i_packet_length;
250 case ACCESS_GET_PTS_DELAY:
251 pi_64 = (int64_t*)va_arg( args, int64_t * );
252 var_Get( p_access, "mms-caching", &val );
253 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
256 case ACCESS_GET_PRIVATE_ID_STATE:
257 i_int = (int)va_arg( args, int );
258 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
260 if( i_int < 0 || i_int > 127 )
262 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
266 case ACCESS_SET_PAUSE_STATE:
267 case ACCESS_GET_TITLE_INFO:
268 case ACCESS_SET_TITLE:
269 case ACCESS_SET_SEEKPOINT:
270 case ACCESS_SET_PRIVATE_ID_STATE:
275 msg_Warn( p_access, "unimplemented query in control" );
282 /*****************************************************************************
283 * Seek: try to go at the right place
284 *****************************************************************************/
285 static int Seek( access_t * p_access, int64_t i_pos )
287 access_sys_t *p_sys = p_access->p_sys;
295 if( i_pos < p_sys->i_header)
298 if( p_access->info.i_pos < p_sys->i_header )
300 /* no need to restart stream, it was already one
301 * or no stream was yet read */
302 p_access->info.i_pos = i_pos;
307 i_packet = 0xffffffff;
313 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
314 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
316 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
319 msg_Dbg( p_access, "stream stopped (seek)" );
321 /* *** restart stream *** */
322 var_buffer_initwrite( &buffer, 0 );
323 var_buffer_add64( &buffer, 0 ); /* seek point in second */
324 var_buffer_add32( &buffer, 0xffffffff );
325 var_buffer_add32( &buffer, i_packet ); // begin from start
326 var_buffer_add8( &buffer, 0xff ); // stream time limit
327 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
328 var_buffer_add8( &buffer, 0xff ); //
329 var_buffer_add8( &buffer, 0x00 ); // don't use limit
330 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
332 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
333 buffer.p_data, buffer.i_data );
335 var_buffer_free( &buffer );
338 while( !p_access->b_die )
340 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
341 if( p_sys->i_command == 0x1e )
343 msg_Dbg( p_access, "received 0x1e (seek)" );
348 while( !p_access->b_die )
350 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
351 if( p_sys->i_command == 0x05 )
353 msg_Dbg( p_access, "received 0x05 (seek)" );
359 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
360 msg_Dbg( p_access, "Streaming restarted" );
362 p_sys->i_media_used += i_offset;
363 p_access->info.i_pos = i_pos;
364 p_access->info.b_eof = VLC_FALSE;
369 /*****************************************************************************
371 *****************************************************************************/
372 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
374 access_sys_t *p_sys = p_access->p_sys;
380 /* *** now send data if needed *** */
381 while( i_data < (size_t)i_len )
383 if( p_access->info.i_pos < p_sys->i_header )
385 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
386 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
388 p_access->info.i_pos += i_copy;
390 else if( p_sys->i_media_used < p_sys->i_media )
392 i_copy = __MIN( i_len - i_data ,
393 p_sys->i_media - p_sys->i_media_used );
394 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
396 p_sys->i_media_used += i_copy;
397 p_access->info.i_pos += i_copy;
399 else if( p_sys->p_media != NULL &&
400 p_sys->i_media_used < p_sys->i_packet_length )
402 i_copy = __MIN( i_len - i_data,
403 p_sys->i_packet_length - p_sys->i_media_used);
404 memset( &p_buffer[i_data], 0, i_copy );
407 p_sys->i_media_used += i_copy;
408 p_access->info.i_pos += i_copy;
410 else if( p_access->info.b_eof ||
411 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
420 /****************************************************************************
421 * MMSOpen : Open a connection with the server over mmst or mmsu
422 ****************************************************************************/
423 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
425 access_sys_t *p_sys = p_access->p_sys;
426 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
431 int i_server_version;
433 int i_update_player_url;
434 int i_encryption_type;
440 /* *** Open a TCP connection with server *** */
441 msg_Dbg( p_access, "waiting for connection..." );
442 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
443 if( p_sys->i_handle_tcp < 0 )
445 msg_Err( p_access, "failed to open a connection (tcp)" );
449 "connection(tcp) with \"%s:%d\" successful",
453 /* *** Bind port if UDP protocol is selected *** */
456 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
459 net_Close( p_sys->i_handle_tcp );
463 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
465 if( p_sys->i_handle_udp < 0 )
467 msg_Err( p_access, "failed to open a connection (udp)" );
468 net_Close( p_sys->i_handle_tcp );
472 "connection(udp) at \"%s:%d\" successful",
473 p_sys->sz_bind_addr, 7000 );
476 /* *** Init context for mms prototcol *** */
477 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
479 "generated guid: "GUID_FMT,
480 GUID_PRINT( p_sys->guid ) );
481 p_sys->i_command_level = 1; /* updated after 0x1A command */
482 p_sys->i_seq_num = 0;
483 p_sys->i_media_packet_id_type = 0x04;
484 p_sys->i_header_packet_id_type = 0x02;
485 p_sys->i_proto = i_proto;
486 p_sys->i_packet_seq_num = 0;
487 p_sys->p_header = NULL;
489 p_sys->p_media = NULL;
491 p_sys->i_media_used = 0;
493 p_access->info.i_pos = 0;
494 p_sys->i_buffer_tcp = 0;
495 p_sys->i_buffer_udp = 0;
498 p_access->info.b_eof = 0;
500 /* *** send command 1 : connection request *** */
501 var_buffer_initwrite( &buffer, 0 );
502 var_buffer_add16( &buffer, 0x001c );
503 var_buffer_add16( &buffer, 0x0003 );
505 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
506 GUID_PRINT( p_sys->guid ),
508 var_buffer_addUTF16( &buffer, tmp );
510 mms_CommandSend( p_access,
511 0x01, /* connexion request */
512 0x00000000, /* flags, FIXME */
513 0x0004000b, /* ???? */
517 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
519 var_buffer_free( &buffer );
520 MMSClose( p_access );
524 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
525 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
526 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
527 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
528 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
529 #define GETUTF16( psz, size ) \
532 psz = malloc( size + 1); \
533 for( i = 0; i < size; i++ ) \
540 GETUTF16( p_sys->psz_server_version, i_server_version );
541 GETUTF16( p_sys->psz_tool_version, i_tool_version );
542 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
543 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
546 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
547 p_sys->psz_server_version,
548 p_sys->psz_tool_version,
549 p_sys->psz_update_player_url,
550 p_sys->psz_encryption_type );
552 /* *** should make an 18 command to make data timing *** */
554 /* *** send command 2 : transport protocol selection *** */
555 var_buffer_reinitwrite( &buffer, 0 );
556 var_buffer_add32( &buffer, 0x00000000 );
557 var_buffer_add32( &buffer, 0x000a0000 );
558 var_buffer_add32( &buffer, 0x00000002 );
568 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
570 var_buffer_addUTF16( &buffer, tmp );
571 var_buffer_add16( &buffer, '0' );
573 mms_CommandSend( p_access,
574 0x02, /* connexion request */
575 0x00000000, /* flags, FIXME */
576 0xffffffff, /* ???? */
580 /* *** response from server, should be 0x02 or 0x03 *** */
581 mms_CommandRead( p_access, 0x02, 0x03 );
582 if( p_sys->i_command == 0x03 )
585 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
586 var_buffer_free( &buffer );
587 MMSClose( p_access );
590 else if( p_sys->i_command != 0x02 )
592 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
595 /* *** send command 5 : media file name/path requested *** */
596 var_buffer_reinitwrite( &buffer, 0 );
597 var_buffer_add64( &buffer, 0 );
598 var_buffer_addUTF16( &buffer, p_url->psz_path );
600 mms_CommandSend( p_access,
602 p_sys->i_command_level,
607 /* *** wait for reponse *** */
608 mms_CommandRead( p_access, 0x1a, 0x06 );
610 /* test if server send 0x1A answer */
611 if( p_sys->i_command == 0x1A )
613 msg_Err( p_access, "id/password requested (not yet supported)" );
615 var_buffer_free( &buffer );
616 MMSClose( p_access );
619 if( p_sys->i_command != 0x06 )
622 "unknown answer (0x%x instead of 0x06)",
624 var_buffer_free( &buffer );
625 MMSClose( p_access );
629 /* 1 for file ok, 2 for authen ok */
630 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
633 msg_Dbg( p_access, "media file name/path accepted" );
636 msg_Dbg( p_access, "authentication accepted" );
640 msg_Err( p_access, "error while asking for file %d",
641 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
642 var_buffer_free( &buffer );
643 MMSClose( p_access );
647 p_sys->i_flags_broadcast =
648 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
649 p_sys->i_media_length =
650 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
651 p_sys->i_packet_length =
652 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
653 p_sys->i_packet_count =
654 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
655 p_sys->i_max_bit_rate =
656 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
657 p_sys->i_header_size =
658 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
661 "answer 0x06 flags:0x%8.8x media_length:%us "
662 "packet_length:%ul packet_count:%d max_bit_rate:%d "
664 p_sys->i_flags_broadcast,
665 p_sys->i_media_length,
666 (unsigned)p_sys->i_packet_length,
667 p_sys->i_packet_count,
668 p_sys->i_max_bit_rate,
669 p_sys->i_header_size );
671 /* *** send command 15 *** */
673 var_buffer_reinitwrite( &buffer, 0 );
674 var_buffer_add32( &buffer, 0 );
675 var_buffer_add32( &buffer, 0x8000 );
676 var_buffer_add32( &buffer, 0xffffffff );
677 var_buffer_add32( &buffer, 0x00 );
678 var_buffer_add32( &buffer, 0x00 );
679 var_buffer_add32( &buffer, 0x00 );
680 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
681 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
682 var_buffer_add32( &buffer, 0x00 );
683 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
684 buffer.p_data, buffer.i_data );
686 /* *** wait for reponse *** */
687 /* Commented out because it fails on some stream (no 0x11 answer) */
689 mms_CommandRead( p_access, 0x11, 0 );
691 if( p_sys->i_command != 0x11 )
694 "unknown answer (0x%x instead of 0x11)",
696 var_buffer_free( &buffer );
697 MMSClose( p_access );
702 /* *** now read header packet *** */
703 /* XXX could be split over multiples packets */
704 msg_Dbg( p_access, "reading header" );
707 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
709 msg_Err( p_access, "cannot receive header" );
710 var_buffer_free( &buffer );
711 MMSClose( p_access );
714 if( p_sys->i_header >= p_sys->i_header_size )
717 "header complete(%d)",
722 "header incomplete (%d/%d), reading more",
724 p_sys->i_header_size );
727 /* *** parse header and get stream and their id *** */
728 /* get all streams properties,
730 * TODO : stream bitrates properties(optional)
731 * and bitrate mutual exclusion(optional) */
732 E_( asf_HeaderParse )( &p_sys->asfh,
733 p_sys->p_header, p_sys->i_header );
734 E_( asf_StreamSelect)( &p_sys->asfh,
735 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
736 var_CreateGetInteger( p_access, "mms-all" ),
737 var_CreateGetInteger( p_access, "audio" ),
738 var_CreateGetInteger( p_access, "video" ) );
740 /* *** now select stream we want to receive *** */
741 /* TODO take care of stream bitrate TODO */
744 var_buffer_reinitwrite( &buffer, 0 );
745 /* for now, select first audio and video stream */
746 for( i = 1; i < 128; i++ )
749 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
754 var_buffer_add16( &buffer, 0xffff );
755 var_buffer_add16( &buffer, i );
761 if( p_sys->asfh.stream[i].i_selected )
763 var_buffer_add16( &buffer, 0x0000 );
765 "selecting stream[0x%x] %s (%d kb/s)",
767 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
769 p_sys->asfh.stream[i].i_bitrate / 1024);
773 var_buffer_add16( &buffer, 0x0002 );
775 "ignoring stream[0x%x] %s (%d kb/s)",
777 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
779 p_sys->asfh.stream[i].i_bitrate / 1024);
787 msg_Err( p_access, "cannot find any stream" );
788 var_buffer_free( &buffer );
789 MMSClose( p_access );
792 mms_CommandSend( p_access, 0x33,
794 0xffff | ( i_first << 16 ),
795 buffer.p_data, buffer.i_data );
797 mms_CommandRead( p_access, 0x21, 0 );
798 if( p_sys->i_command != 0x21 )
801 "unknown answer (0x%x instead of 0x21)",
803 var_buffer_free( &buffer );
804 MMSClose( p_access );
809 var_buffer_free( &buffer );
811 msg_Info( p_access, "connection successful" );
816 /****************************************************************************
817 * MMSStart : Start streaming
818 ****************************************************************************/
819 static int MMSStart( access_t *p_access, uint32_t i_packet )
821 access_sys_t *p_sys = p_access->p_sys;
824 /* *** start stream from packet 0 *** */
825 var_buffer_initwrite( &buffer, 0 );
826 var_buffer_add64( &buffer, 0 ); /* seek point in second */
827 var_buffer_add32( &buffer, 0xffffffff );
828 var_buffer_add32( &buffer, i_packet ); // begin from start
829 var_buffer_add8( &buffer, 0xff ); // stream time limit
830 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
831 var_buffer_add8( &buffer, 0xff ); //
832 var_buffer_add8( &buffer, 0x00 ); // don't use limit
833 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
835 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
836 buffer.p_data, buffer.i_data );
838 var_buffer_free( &buffer );
840 mms_CommandRead( p_access, 0x05, 0 );
842 if( p_sys->i_command != 0x05 )
845 "unknown answer (0x%x instead of 0x05)",
852 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
853 msg_Dbg( p_access, "streaming started" );
858 /****************************************************************************
859 * MMSStop : Stop streaming
860 ****************************************************************************/
861 static int MMSStop( access_t *p_access )
863 access_sys_t *p_sys = p_access->p_sys;
865 /* *** stop stream but keep connection alive *** */
866 mms_CommandSend( p_access,
868 p_sys->i_command_level,
874 /****************************************************************************
875 * MMSClose : Close streaming and connection
876 ****************************************************************************/
877 static void MMSClose( access_t *p_access )
879 access_sys_t *p_sys = p_access->p_sys;
881 msg_Dbg( p_access, "Connection closed" );
883 /* *** tell server that we will disconnect *** */
884 mms_CommandSend( p_access,
886 p_sys->i_command_level,
890 /* *** close sockets *** */
891 net_Close( p_sys->i_handle_tcp );
892 if( p_sys->i_proto == MMS_PROTO_UDP )
894 net_Close( p_sys->i_handle_udp );
897 FREENULL( p_sys->p_cmd );
898 FREENULL( p_sys->p_media );
899 FREENULL( p_sys->p_header );
901 FREENULL( p_sys->psz_server_version );
902 FREENULL( p_sys->psz_tool_version );
903 FREENULL( p_sys->psz_update_player_url );
904 FREENULL( p_sys->psz_encryption_type );
907 /****************************************************************************
909 * MMS specific functions
911 ****************************************************************************/
912 static int mms_CommandSend( access_t *p_access, int i_command,
913 uint32_t i_prefix1, uint32_t i_prefix2,
914 uint8_t *p_data, int i_data_old )
917 access_sys_t *p_sys = p_access->p_sys;
918 int i_data_by8, i_ret;
919 int i_data = i_data_old;
921 while( i_data & 0x7 ) i_data++;
922 i_data_by8 = i_data >> 3;
924 /* first init buffer */
925 var_buffer_initwrite( &buffer, 0 );
927 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
928 var_buffer_add32( &buffer, 0xB00BFACE );
929 /* size after protocol type */
930 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
931 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
932 var_buffer_add32( &buffer, i_data_by8 + 4 );
933 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
934 var_buffer_add64( &buffer, 0 );
935 var_buffer_add32( &buffer, i_data_by8 + 2 );
936 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
937 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
938 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
940 /* specific command data */
941 if( p_data && i_data > 0 )
943 var_buffer_addmemory( &buffer, p_data, i_data_old );
946 /* Append padding to the command data */
947 var_buffer_add64( &buffer, 0 );
950 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
951 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
952 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
954 msg_Err( p_access, "failed to send command" );
958 var_buffer_free( &buffer );
962 static int NetFillBuffer( access_t *p_access )
968 access_sys_t *p_sys = p_access->p_sys;
969 struct timeval timeout;
973 /* FIXME when using udp */
974 ssize_t i_tcp, i_udp;
975 ssize_t i_tcp_read, i_udp_read;
979 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
981 if( p_sys->i_proto == MMS_PROTO_UDP )
983 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
987 i_udp = 0; /* there isn't udp socket */
993 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_tcp );
995 i_handle_max = __MAX( i_handle_max, p_sys->i_handle_udp );
997 if( i_handle_max == 0 )
999 msg_Warn( p_access, "nothing to read %d:%d", (int)i_tcp, (int)i_udp );
1004 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1007 /* Find if some data is available */
1012 /* Initialize file descriptor set */
1018 FD_SET( p_sys->i_handle_tcp, &fds_r );
1019 FD_SET( p_sys->i_handle_tcp, &fds_e );
1023 FD_SET( p_sys->i_handle_udp, &fds_r );
1024 FD_SET( p_sys->i_handle_udp, &fds_e );
1027 /* We'll wait 0.5 second if nothing happens */
1029 timeout.tv_usec = 500000;
1031 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1036 if( p_access->b_die || p_access->b_error ) return -1;
1038 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1040 } while( !(i_ret = select(i_handle_max +1, &fds_r, 0, &fds_e, &timeout)) ||
1041 (i_ret < 0 && errno == EINTR) );
1045 msg_Err( p_access, "network select error (%m)" );
1049 i_tcp_read = i_udp_read = 0;
1051 if( i_tcp > 0 && FD_ISSET( p_sys->i_handle_tcp, &fds_r ) )
1054 recv( p_sys->i_handle_tcp,
1055 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1056 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1059 if( i_udp > 0 && FD_ISSET( p_sys->i_handle_udp, &fds_r ) )
1061 i_udp_read = recv( p_sys->i_handle_udp,
1062 p_sys->buffer_udp + p_sys->i_buffer_udp,
1063 i_udp + MMS_BUFFER_SIZE/2, 0 );
1067 if( p_sys->i_proto == MMS_PROTO_UDP )
1069 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1070 p_sys->i_buffer_tcp, i_tcp_read,
1071 p_sys->i_buffer_udp, i_udp_read );
1075 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1076 p_sys->i_buffer_tcp, i_tcp_read );
1080 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1081 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1083 return i_tcp_read + i_udp_read;
1087 static int mms_ParseCommand( access_t *p_access,
1092 #define GET32( i_pos ) \
1093 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1094 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1095 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1097 access_sys_t *p_sys = p_access->p_sys;
1103 free( p_sys->p_cmd );
1105 p_sys->i_cmd = i_data;
1106 p_sys->p_cmd = malloc( i_data );
1107 memcpy( p_sys->p_cmd, p_data, i_data );
1109 *pi_used = i_data; /* by default */
1111 if( i_data < MMS_CMD_HEADERSIZE )
1113 msg_Warn( p_access, "truncated command (header incomplete)" );
1114 p_sys->i_command = 0;
1117 i_id = GetDWLE( p_data + 4 );
1118 i_length = GetDWLE( p_data + 8 ) + 16;
1120 if( i_id != 0xb00bface )
1123 "incorrect command header (0x%x)", i_id );
1124 p_sys->i_command = 0;
1128 if( i_length > p_sys->i_cmd )
1131 "truncated command (missing %d bytes)",
1132 i_length - i_data );
1133 p_sys->i_command = 0;
1136 else if( i_length < p_sys->i_cmd )
1138 p_sys->i_cmd = i_length;
1139 *pi_used = i_length;
1143 "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",
1147 /* 12: protocol type "MMS " */
1150 /* 24: unknown (0) */
1151 /* 28: unknown (0) */
1157 p_sys->i_command = GET32( 36 ) & 0xffff;
1160 return MMS_PACKET_CMD;
1163 static int mms_ParsePacket( access_t *p_access,
1164 uint8_t *p_data, size_t i_data,
1167 access_sys_t *p_sys = p_access->p_sys;
1168 int i_packet_seq_num;
1169 size_t i_packet_length;
1170 uint32_t i_packet_id;
1175 *pi_used = i_data; /* default */
1178 msg_Warn( p_access, "truncated packet (header incomplete)" );
1182 i_packet_id = p_data[4];
1183 i_packet_seq_num = GetDWLE( p_data );
1184 i_packet_length = GetWLE( p_data + 6 );
1186 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1188 if( i_packet_length > i_data || i_packet_length <= 8)
1190 /* msg_Dbg( p_access,
1191 "truncated packet (Declared %d bytes, Actual %d bytes)",
1192 i_packet_length, i_data ); */
1196 else if( i_packet_length < i_data )
1198 *pi_used = i_packet_length;
1201 if( i_packet_id == 0xff )
1204 "receive MMS UDP pair timing" );
1205 return( MMS_PACKET_UDP_TIMING );
1208 if( i_packet_id != p_sys->i_header_packet_id_type &&
1209 i_packet_id != p_sys->i_media_packet_id_type )
1211 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1215 /* we now have a media or a header packet */
1216 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1217 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1219 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1222 /* FIXME for udp could be just wrong order ? */
1224 "detected packet lost (%d != %d)",
1226 p_sys->i_packet_seq_num );
1229 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1231 if( i_packet_id == p_sys->i_header_packet_id_type )
1233 if( p_sys->p_header )
1235 p_sys->p_header = realloc( p_sys->p_header,
1236 p_sys->i_header + i_packet_length - 8 );
1237 memcpy( &p_sys->p_header[p_sys->i_header],
1239 i_packet_length - 8 );
1240 p_sys->i_header += i_packet_length - 8;
1246 p_sys->p_header = p_packet;
1247 p_sys->i_header = i_packet_length - 8;
1249 /* msg_Dbg( p_access,
1250 "receive header packet (%d bytes)",
1251 i_packet_length - 8 ); */
1253 return MMS_PACKET_HEADER;
1257 FREENULL( p_sys->p_media );
1258 p_sys->p_media = p_packet;
1259 p_sys->i_media = i_packet_length - 8;
1260 p_sys->i_media_used = 0;
1261 /* msg_Dbg( p_access,
1262 "receive media packet (%d bytes)",
1263 i_packet_length - 8 ); */
1265 return MMS_PACKET_MEDIA;
1269 static int mms_ReceivePacket( access_t *p_access )
1271 access_sys_t *p_sys = p_access->p_sys;
1272 int i_packet_tcp_type;
1273 int i_packet_udp_type;
1277 vlc_bool_t b_refill = VLC_TRUE;
1279 /* first if we need to refill buffer */
1280 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1282 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1284 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1285 (uint32_t)p_sys->i_buffer_tcp )
1287 b_refill = VLC_FALSE;
1290 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1292 b_refill = VLC_FALSE;
1295 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1296 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1298 b_refill = VLC_FALSE;
1301 if( b_refill && NetFillBuffer( p_access ) < 0 )
1303 msg_Warn( p_access, "cannot fill buffer" );
1307 i_packet_tcp_type = -1;
1308 i_packet_udp_type = -1;
1310 if( p_sys->i_buffer_tcp > 0 )
1314 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1317 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1318 p_sys->i_buffer_tcp, &i_used );
1324 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1325 p_sys->i_buffer_tcp, &i_used );
1327 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1329 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1330 MMS_BUFFER_SIZE - i_used );
1332 p_sys->i_buffer_tcp -= i_used;
1334 else if( p_sys->i_buffer_udp > 0 )
1339 mms_ParsePacket( p_access, p_sys->buffer_udp,
1340 p_sys->i_buffer_udp, &i_used );
1342 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1344 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1345 MMS_BUFFER_SIZE - i_used );
1347 p_sys->i_buffer_udp -= i_used;
1350 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1352 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1353 i_packet_tcp_type = -1;
1356 if( i_packet_tcp_type != -1 )
1358 return i_packet_tcp_type;
1360 else if( i_packet_udp_type != -1 )
1362 return i_packet_udp_type;
1367 static int mms_ReceiveCommand( access_t *p_access )
1369 access_sys_t *p_sys = p_access->p_sys;
1376 if( NetFillBuffer( p_access ) < 0 )
1378 msg_Warn( p_access, "cannot fill buffer" );
1379 return VLC_EGENERIC;
1381 if( p_sys->i_buffer_tcp > 0 )
1383 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1384 p_sys->i_buffer_tcp, &i_used );
1385 if( i_used < MMS_BUFFER_SIZE )
1387 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1388 MMS_BUFFER_SIZE - i_used );
1390 p_sys->i_buffer_tcp -= i_used;
1394 return VLC_EGENERIC;
1397 if( p_sys->i_command == 0x1b )
1399 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1408 return VLC_EGENERIC;
1415 #define MMS_RETRY_MAX 10
1416 #define MMS_RETRY_SLEEP 50000
1418 static int mms_CommandRead( access_t *p_access, int i_command1,
1421 access_sys_t *p_sys = p_access->p_sys;
1425 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1427 i_status = mms_ReceiveCommand( p_access );
1428 if( i_status < 0 || p_sys->i_command == 0 )
1431 msleep( MMS_RETRY_SLEEP );
1433 else if( i_command1 == 0 && i_command2 == 0)
1437 else if( p_sys->i_command == i_command1 ||
1438 p_sys->i_command == i_command2 )
1444 switch( p_sys->i_command )
1447 msg_Warn( p_access, "socket closed by server" );
1448 p_access->info.b_eof = 1;
1449 return VLC_EGENERIC;
1451 msg_Warn( p_access, "end of media stream" );
1452 p_access->info.b_eof = 1;
1453 return VLC_EGENERIC;
1459 msg_Warn( p_access, "failed to receive command (aborting)" );
1461 return VLC_EGENERIC;
1465 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1467 access_sys_t *p_sys = p_access->p_sys;
1470 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1474 if( p_access->b_die )
1477 i_status = mms_ReceivePacket( p_access );
1481 msg_Warn( p_access, "cannot receive header (%d/%d)",
1482 i_count, MMS_RETRY_MAX );
1483 msleep( MMS_RETRY_SLEEP );
1485 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1489 else if( i_status == MMS_PACKET_CMD )
1491 switch( p_sys->i_command )
1494 msg_Warn( p_access, "socket closed by server" );
1495 p_access->info.b_eof = 1;
1498 msg_Warn( p_access, "end of media stream" );
1499 p_access->info.b_eof = 1;
1502 /* XXX not too dificult to be done EXCEPT that we
1503 * need to restart demuxer... and I don't see how we
1504 * could do that :p */
1506 "reinitialization needed --> unsupported" );
1507 p_access->info.b_eof = VLC_TRUE;
1515 msg_Err( p_access, "cannot receive %s (aborting)",
1516 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );