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 *****************************************************************************/
33 #include <vlc_access.h>
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
46 #ifdef HAVE_SYS_TYPES_H
47 # include <sys/types.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
56 #include <vlc_network.h>
66 /****************************************************************************
68 * MMSProtocole documentation found at http://get.to/sdp
69 ****************************************************************************/
71 /*****************************************************************************
73 *****************************************************************************/
74 int E_( MMSTUOpen ) ( access_t * );
75 void E_( MMSTUClose ) ( access_t * );
78 static ssize_t Read( access_t *, uint8_t *, size_t );
79 static int Seek( access_t *, int64_t );
80 static int Control( access_t *, int, va_list );
82 static int MMSOpen ( access_t *, vlc_url_t *, int );
83 static int MMSStart( access_t *, uint32_t );
84 static int MMSStop ( access_t * );
85 static void MMSClose( access_t * );
88 static int mms_CommandRead( access_t *p_access, int i_command1, int i_command2 );
89 static int mms_CommandSend( access_t *, int, uint32_t, uint32_t, uint8_t *, int );
91 static int mms_HeaderMediaRead( access_t *, int );
93 static int mms_ReceivePacket( access_t * );
96 int E_(MMSTUOpen)( access_t *p_access )
102 /* Set up p_access */
103 p_access->pf_read = Read;
104 p_access->pf_block = NULL;
105 p_access->pf_control = Control;
106 p_access->pf_seek = Seek;
107 p_access->info.i_update = 0;
108 p_access->info.i_size = 0;
109 p_access->info.i_pos = 0;
110 p_access->info.b_eof = VLC_FALSE;
111 p_access->info.i_title = 0;
112 p_access->info.i_seekpoint = 0;
113 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
114 memset( p_sys, 0, sizeof( access_sys_t ) );
116 /* *** Parse URL and get server addr/port and path *** */
117 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
118 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
120 msg_Err( p_access, "invalid server name" );
121 vlc_UrlClean( &p_sys->url );
125 if( p_sys->url.i_port <= 0 )
127 p_sys->url.i_port = 1755;
130 /* *** connect to this server *** */
131 /* look at requested protocol (udp/tcp) */
132 i_proto = MMS_PROTO_AUTO;
133 if( *p_access->psz_access )
135 if( !strncmp( p_access->psz_access, "mmsu", 4 ) )
137 i_proto = MMS_PROTO_UDP;
139 else if( !strncmp( p_access->psz_access, "mmst", 4 ) )
141 i_proto = MMS_PROTO_TCP;
146 if( i_proto == MMS_PROTO_AUTO )
147 { /* first try with TCP and then UDP*/
148 if( ( i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_TCP ) ) )
150 i_status = MMSOpen( p_access, &p_sys->url, MMS_PROTO_UDP );
155 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
160 msg_Err( p_access, "cannot connect to server" );
161 vlc_UrlClean( &p_sys->url );
166 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
170 * broadcast yy=0x02, xx= 0x00
171 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
173 if( p_sys->i_packet_count <= 0 && p_sys->asfh.i_data_packets_count > 0 )
175 p_sys->i_packet_count = p_sys->asfh.i_data_packets_count;
177 if( p_sys->i_packet_count <= 0 || ( p_sys->i_flags_broadcast >> 24 ) == 0x02 )
179 p_sys->b_seekable = VLC_FALSE;
183 p_sys->b_seekable = VLC_TRUE;
184 p_access->info.i_size =
185 (uint64_t)p_sys->i_header +
186 (uint64_t)p_sys->i_packet_count * (uint64_t)p_sys->i_packet_length;
189 /* *** Start stream *** */
190 if( MMSStart( p_access, 0xffffffff ) < 0 )
192 msg_Err( p_access, "cannot start stream" );
193 E_(MMSTUClose) ( p_access );
199 /*****************************************************************************
200 * Close: free unused data structures
201 *****************************************************************************/
202 void E_(MMSTUClose)( access_t *p_access )
204 access_sys_t *p_sys = p_access->p_sys;
206 /* close connection with server */
207 MMSClose( p_access );
210 vlc_UrlClean( &p_sys->url );
215 /*****************************************************************************
217 *****************************************************************************/
218 static int Control( access_t *p_access, int i_query, va_list args )
220 access_sys_t *p_sys = p_access->p_sys;
230 case ACCESS_CAN_SEEK:
231 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
232 *pb_bool = p_sys->b_seekable;
235 case ACCESS_CAN_FASTSEEK:
236 case ACCESS_CAN_PAUSE:
237 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
238 *pb_bool = VLC_FALSE;
241 case ACCESS_CAN_CONTROL_PACE:
242 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
244 #if 0 /* Disable for now until we have a clock synchro algo
245 * which works with something else than MPEG over UDP */
246 *pb_bool = VLC_FALSE;
253 pi_int = (int*)va_arg( args, int * );
254 *pi_int = 3 * p_sys->i_packet_length;
257 case ACCESS_GET_PTS_DELAY:
258 pi_64 = (int64_t*)va_arg( args, int64_t * );
259 var_Get( p_access, "mms-caching", &val );
260 *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
263 case ACCESS_GET_PRIVATE_ID_STATE:
264 i_int = (int)va_arg( args, int );
265 pb_bool = (vlc_bool_t *)va_arg( args, vlc_bool_t * );
267 if( i_int < 0 || i_int > 127 )
269 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
273 case ACCESS_SET_PAUSE_STATE:
274 case ACCESS_GET_TITLE_INFO:
275 case ACCESS_SET_TITLE:
276 case ACCESS_SET_SEEKPOINT:
277 case ACCESS_SET_PRIVATE_ID_STATE:
278 case ACCESS_GET_CONTENT_TYPE:
283 msg_Warn( p_access, "unimplemented query in control" );
290 /*****************************************************************************
291 * Seek: try to go at the right place
292 *****************************************************************************/
293 static int Seek( access_t * p_access, int64_t i_pos )
295 access_sys_t *p_sys = p_access->p_sys;
303 if( i_pos < p_sys->i_header)
306 if( p_access->info.i_pos < p_sys->i_header )
308 /* no need to restart stream, it was already one
309 * or no stream was yet read */
310 p_access->info.i_pos = i_pos;
315 i_packet = 0xffffffff;
321 i_packet = ( i_pos - p_sys->i_header ) / p_sys->i_packet_length;
322 i_offset = ( i_pos - p_sys->i_header ) % p_sys->i_packet_length;
324 msg_Dbg( p_access, "seeking to "I64Fd " (packet:%d)", i_pos, i_packet );
327 msg_Dbg( p_access, "stream stopped (seek)" );
329 /* *** restart stream *** */
330 var_buffer_initwrite( &buffer, 0 );
331 var_buffer_add64( &buffer, 0 ); /* seek point in second */
332 var_buffer_add32( &buffer, 0xffffffff );
333 var_buffer_add32( &buffer, i_packet ); // begin from start
334 var_buffer_add8( &buffer, 0xff ); // stream time limit
335 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
336 var_buffer_add8( &buffer, 0xff ); //
337 var_buffer_add8( &buffer, 0x00 ); // don't use limit
338 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
340 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
341 buffer.p_data, buffer.i_data );
343 var_buffer_free( &buffer );
346 while( !p_access->b_die )
348 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
349 if( p_sys->i_command == 0x1e )
351 msg_Dbg( p_access, "received 0x1e (seek)" );
356 while( !p_access->b_die )
358 mms_HeaderMediaRead( p_access, MMS_PACKET_CMD );
359 if( p_sys->i_command == 0x05 )
361 msg_Dbg( p_access, "received 0x05 (seek)" );
367 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
368 msg_Dbg( p_access, "Streaming restarted" );
370 p_sys->i_media_used += i_offset;
371 p_access->info.i_pos = i_pos;
372 p_access->info.b_eof = VLC_FALSE;
377 /*****************************************************************************
379 *****************************************************************************/
380 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
382 access_sys_t *p_sys = p_access->p_sys;
388 /* *** now send data if needed *** */
389 while( i_data < (size_t)i_len )
391 if( p_access->info.i_pos < p_sys->i_header )
393 i_copy = __MIN( i_len, p_sys->i_header - p_access->info.i_pos );
394 memcpy( &p_buffer[i_data], &p_sys->p_header[p_access->info.i_pos], i_copy );
396 p_access->info.i_pos += i_copy;
398 else if( p_sys->i_media_used < p_sys->i_media )
400 i_copy = __MIN( i_len - i_data ,
401 p_sys->i_media - p_sys->i_media_used );
402 memcpy( &p_buffer[i_data], &p_sys->p_media[p_sys->i_media_used], i_copy );
404 p_sys->i_media_used += i_copy;
405 p_access->info.i_pos += i_copy;
407 else if( p_sys->p_media != NULL &&
408 p_sys->i_media_used < p_sys->i_packet_length )
410 i_copy = __MIN( i_len - i_data,
411 p_sys->i_packet_length - p_sys->i_media_used);
412 memset( &p_buffer[i_data], 0, i_copy );
415 p_sys->i_media_used += i_copy;
416 p_access->info.i_pos += i_copy;
418 else if( p_access->info.b_eof ||
419 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
428 /****************************************************************************
429 * MMSOpen : Open a connection with the server over mmst or mmsu
430 ****************************************************************************/
431 static int MMSOpen( access_t *p_access, vlc_url_t *p_url, int i_proto )
433 access_sys_t *p_sys = p_access->p_sys;
434 int b_udp = ( i_proto == MMS_PROTO_UDP ) ? 1 : 0;
439 int i_server_version;
441 int i_update_player_url;
442 int i_encryption_type;
448 /* *** Open a TCP connection with server *** */
449 msg_Dbg( p_access, "waiting for connection..." );
450 p_sys->i_handle_tcp = net_ConnectTCP( p_access, p_url->psz_host, p_url->i_port );
451 if( p_sys->i_handle_tcp < 0 )
453 msg_Err( p_access, "failed to open a connection (tcp)" );
457 "connection(tcp) with \"%s:%d\" successful",
461 /* *** Bind port if UDP protocol is selected *** */
464 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
467 net_Close( p_sys->i_handle_tcp );
471 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
473 if( p_sys->i_handle_udp < 0 )
475 msg_Err( p_access, "failed to open a connection (udp)" );
476 net_Close( p_sys->i_handle_tcp );
480 "connection(udp) at \"%s:%d\" successful",
481 p_sys->sz_bind_addr, 7000 );
484 /* *** Init context for mms prototcol *** */
485 E_( GenerateGuid )( &p_sys->guid ); /* used to identify client by server */
487 "generated guid: "GUID_FMT,
488 GUID_PRINT( p_sys->guid ) );
489 p_sys->i_command_level = 1; /* updated after 0x1A command */
490 p_sys->i_seq_num = 0;
491 p_sys->i_media_packet_id_type = 0x04;
492 p_sys->i_header_packet_id_type = 0x02;
493 p_sys->i_proto = i_proto;
494 p_sys->i_packet_seq_num = 0;
495 p_sys->p_header = NULL;
497 p_sys->p_media = NULL;
499 p_sys->i_media_used = 0;
501 p_access->info.i_pos = 0;
502 p_sys->i_buffer_tcp = 0;
503 p_sys->i_buffer_udp = 0;
506 p_access->info.b_eof = 0;
508 /* *** send command 1 : connection request *** */
509 var_buffer_initwrite( &buffer, 0 );
510 var_buffer_add16( &buffer, 0x001c );
511 var_buffer_add16( &buffer, 0x0003 );
513 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
514 GUID_PRINT( p_sys->guid ),
516 var_buffer_addUTF16( &buffer, tmp );
518 mms_CommandSend( p_access,
519 0x01, /* connexion request */
520 0x00000000, /* flags, FIXME */
521 0x0004000b, /* ???? */
525 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
527 var_buffer_free( &buffer );
528 MMSClose( p_access );
532 i_server_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 32 );
533 i_tool_version = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 36 );
534 i_update_player_url = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 40 );
535 i_encryption_type = GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
536 p = (uint16_t*)( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
537 #define GETUTF16( psz, size ) \
540 psz = malloc( size + 1); \
541 for( i = 0; i < size; i++ ) \
548 GETUTF16( p_sys->psz_server_version, i_server_version );
549 GETUTF16( p_sys->psz_tool_version, i_tool_version );
550 GETUTF16( p_sys->psz_update_player_url, i_update_player_url );
551 GETUTF16( p_sys->psz_encryption_type, i_encryption_type );
554 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
555 p_sys->psz_server_version,
556 p_sys->psz_tool_version,
557 p_sys->psz_update_player_url,
558 p_sys->psz_encryption_type );
560 /* *** should make an 18 command to make data timing *** */
562 /* *** send command 2 : transport protocol selection *** */
563 var_buffer_reinitwrite( &buffer, 0 );
564 var_buffer_add32( &buffer, 0x00000000 );
565 var_buffer_add32( &buffer, 0x000a0000 );
566 var_buffer_add32( &buffer, 0x00000002 );
576 sprintf( tmp, "\\\\192.168.0.1\\TCP\\1242" );
578 var_buffer_addUTF16( &buffer, tmp );
579 var_buffer_add16( &buffer, '0' );
581 mms_CommandSend( p_access,
582 0x02, /* connexion request */
583 0x00000000, /* flags, FIXME */
584 0xffffffff, /* ???? */
588 /* *** response from server, should be 0x02 or 0x03 *** */
589 mms_CommandRead( p_access, 0x02, 0x03 );
590 if( p_sys->i_command == 0x03 )
593 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
594 var_buffer_free( &buffer );
595 MMSClose( p_access );
598 else if( p_sys->i_command != 0x02 )
600 msg_Warn( p_access, "received command isn't 0x02 in reponse to 0x02" );
603 /* *** send command 5 : media file name/path requested *** */
604 var_buffer_reinitwrite( &buffer, 0 );
605 var_buffer_add64( &buffer, 0 );
606 var_buffer_addUTF16( &buffer, p_url->psz_path );
608 mms_CommandSend( p_access,
610 p_sys->i_command_level,
615 /* *** wait for reponse *** */
616 mms_CommandRead( p_access, 0x1a, 0x06 );
618 /* test if server send 0x1A answer */
619 if( p_sys->i_command == 0x1A )
621 msg_Err( p_access, "id/password requested (not yet supported)" );
623 var_buffer_free( &buffer );
624 MMSClose( p_access );
627 if( p_sys->i_command != 0x06 )
630 "unknown answer (0x%x instead of 0x06)",
632 var_buffer_free( &buffer );
633 MMSClose( p_access );
637 /* 1 for file ok, 2 for authen ok */
638 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
641 msg_Dbg( p_access, "media file name/path accepted" );
644 msg_Dbg( p_access, "authentication accepted" );
648 msg_Err( p_access, "error while asking for file %d",
649 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) );
650 var_buffer_free( &buffer );
651 MMSClose( p_access );
655 p_sys->i_flags_broadcast =
656 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 12 );
657 p_sys->i_media_length =
658 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 24 );
659 p_sys->i_packet_length =
660 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 44 );
661 p_sys->i_packet_count =
662 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 48 );
663 p_sys->i_max_bit_rate =
664 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 56 );
665 p_sys->i_header_size =
666 GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE + 60 );
669 "answer 0x06 flags:0x%8.8x media_length:%us "
670 "packet_length:%ul packet_count:%d max_bit_rate:%d "
672 p_sys->i_flags_broadcast,
673 p_sys->i_media_length,
674 (unsigned)p_sys->i_packet_length,
675 p_sys->i_packet_count,
676 p_sys->i_max_bit_rate,
677 p_sys->i_header_size );
679 /* *** send command 15 *** */
681 var_buffer_reinitwrite( &buffer, 0 );
682 var_buffer_add32( &buffer, 0 );
683 var_buffer_add32( &buffer, 0x8000 );
684 var_buffer_add32( &buffer, 0xffffffff );
685 var_buffer_add32( &buffer, 0x00 );
686 var_buffer_add32( &buffer, 0x00 );
687 var_buffer_add32( &buffer, 0x00 );
688 var_buffer_add64( &buffer, (((uint64_t)0x40ac2000)<<32) );
689 var_buffer_add32( &buffer, p_sys->i_header_packet_id_type );
690 var_buffer_add32( &buffer, 0x00 );
691 mms_CommandSend( p_access, 0x15, p_sys->i_command_level, 0x00,
692 buffer.p_data, buffer.i_data );
694 /* *** wait for reponse *** */
695 /* Commented out because it fails on some stream (no 0x11 answer) */
697 mms_CommandRead( p_access, 0x11, 0 );
699 if( p_sys->i_command != 0x11 )
702 "unknown answer (0x%x instead of 0x11)",
704 var_buffer_free( &buffer );
705 MMSClose( p_access );
710 /* *** now read header packet *** */
711 /* XXX could be split over multiples packets */
712 msg_Dbg( p_access, "reading header" );
715 if( mms_HeaderMediaRead( p_access, MMS_PACKET_HEADER ) < 0 )
717 msg_Err( p_access, "cannot receive header" );
718 var_buffer_free( &buffer );
719 MMSClose( p_access );
722 if( p_sys->i_header >= p_sys->i_header_size )
725 "header complete(%d)",
730 "header incomplete (%d/%d), reading more",
732 p_sys->i_header_size );
735 /* *** parse header and get stream and their id *** */
736 /* get all streams properties,
738 * TODO : stream bitrates properties(optional)
739 * and bitrate mutual exclusion(optional) */
740 E_( asf_HeaderParse )( &p_sys->asfh,
741 p_sys->p_header, p_sys->i_header );
742 E_( asf_StreamSelect)( &p_sys->asfh,
743 var_CreateGetInteger( p_access, "mms-maxbitrate" ),
744 var_CreateGetInteger( p_access, "mms-all" ),
745 var_CreateGetInteger( p_access, "audio" ),
746 var_CreateGetInteger( p_access, "video" ) );
748 /* *** now select stream we want to receive *** */
749 /* TODO take care of stream bitrate TODO */
752 var_buffer_reinitwrite( &buffer, 0 );
753 /* for now, select first audio and video stream */
754 for( i = 1; i < 128; i++ )
757 if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
762 var_buffer_add16( &buffer, 0xffff );
763 var_buffer_add16( &buffer, i );
769 if( p_sys->asfh.stream[i].i_selected )
771 var_buffer_add16( &buffer, 0x0000 );
773 "selecting stream[0x%x] %s (%d kb/s)",
775 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
777 p_sys->asfh.stream[i].i_bitrate / 1024);
781 var_buffer_add16( &buffer, 0x0002 );
783 "ignoring stream[0x%x] %s (%d kb/s)",
785 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
787 p_sys->asfh.stream[i].i_bitrate / 1024);
795 msg_Err( p_access, "cannot find any stream" );
796 var_buffer_free( &buffer );
797 MMSClose( p_access );
800 mms_CommandSend( p_access, 0x33,
802 0xffff | ( i_first << 16 ),
803 buffer.p_data, buffer.i_data );
805 mms_CommandRead( p_access, 0x21, 0 );
806 if( p_sys->i_command != 0x21 )
809 "unknown answer (0x%x instead of 0x21)",
811 var_buffer_free( &buffer );
812 MMSClose( p_access );
817 var_buffer_free( &buffer );
819 msg_Info( p_access, "connection successful" );
824 /****************************************************************************
825 * MMSStart : Start streaming
826 ****************************************************************************/
827 static int MMSStart( access_t *p_access, uint32_t i_packet )
829 access_sys_t *p_sys = p_access->p_sys;
832 /* *** start stream from packet 0 *** */
833 var_buffer_initwrite( &buffer, 0 );
834 var_buffer_add64( &buffer, 0 ); /* seek point in second */
835 var_buffer_add32( &buffer, 0xffffffff );
836 var_buffer_add32( &buffer, i_packet ); // begin from start
837 var_buffer_add8( &buffer, 0xff ); // stream time limit
838 var_buffer_add8( &buffer, 0xff ); // on 3bytes ...
839 var_buffer_add8( &buffer, 0xff ); //
840 var_buffer_add8( &buffer, 0x00 ); // don't use limit
841 var_buffer_add32( &buffer, p_sys->i_media_packet_id_type );
843 mms_CommandSend( p_access, 0x07, p_sys->i_command_level, 0x0001ffff,
844 buffer.p_data, buffer.i_data );
846 var_buffer_free( &buffer );
848 mms_CommandRead( p_access, 0x05, 0 );
850 if( p_sys->i_command != 0x05 )
853 "unknown answer (0x%x instead of 0x05)",
860 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
861 msg_Dbg( p_access, "streaming started" );
866 /****************************************************************************
867 * MMSStop : Stop streaming
868 ****************************************************************************/
869 static int MMSStop( access_t *p_access )
871 access_sys_t *p_sys = p_access->p_sys;
873 /* *** stop stream but keep connection alive *** */
874 mms_CommandSend( p_access,
876 p_sys->i_command_level,
882 /****************************************************************************
883 * MMSClose : Close streaming and connection
884 ****************************************************************************/
885 static void MMSClose( access_t *p_access )
887 access_sys_t *p_sys = p_access->p_sys;
889 msg_Dbg( p_access, "Connection closed" );
891 /* *** tell server that we will disconnect *** */
892 mms_CommandSend( p_access,
894 p_sys->i_command_level,
898 /* *** close sockets *** */
899 net_Close( p_sys->i_handle_tcp );
900 if( p_sys->i_proto == MMS_PROTO_UDP )
902 net_Close( p_sys->i_handle_udp );
905 FREENULL( p_sys->p_cmd );
906 FREENULL( p_sys->p_media );
907 FREENULL( p_sys->p_header );
909 FREENULL( p_sys->psz_server_version );
910 FREENULL( p_sys->psz_tool_version );
911 FREENULL( p_sys->psz_update_player_url );
912 FREENULL( p_sys->psz_encryption_type );
915 /****************************************************************************
917 * MMS specific functions
919 ****************************************************************************/
920 static int mms_CommandSend( access_t *p_access, int i_command,
921 uint32_t i_prefix1, uint32_t i_prefix2,
922 uint8_t *p_data, int i_data_old )
925 access_sys_t *p_sys = p_access->p_sys;
926 int i_data_by8, i_ret;
927 int i_data = i_data_old;
929 while( i_data & 0x7 ) i_data++;
930 i_data_by8 = i_data >> 3;
932 /* first init buffer */
933 var_buffer_initwrite( &buffer, 0 );
935 var_buffer_add32( &buffer, 0x00000001 ); /* start sequence */
936 var_buffer_add32( &buffer, 0xB00BFACE );
937 /* size after protocol type */
938 var_buffer_add32( &buffer, i_data + MMS_CMD_HEADERSIZE - 16 );
939 var_buffer_add32( &buffer, 0x20534d4d ); /* protocol "MMS " */
940 var_buffer_add32( &buffer, i_data_by8 + 4 );
941 var_buffer_add32( &buffer, p_sys->i_seq_num ); p_sys->i_seq_num++;
942 var_buffer_add64( &buffer, 0 );
943 var_buffer_add32( &buffer, i_data_by8 + 2 );
944 var_buffer_add32( &buffer, 0x00030000 | i_command ); /* dir | command */
945 var_buffer_add32( &buffer, i_prefix1 ); /* command specific */
946 var_buffer_add32( &buffer, i_prefix2 ); /* command specific */
948 /* specific command data */
949 if( p_data && i_data > 0 )
951 var_buffer_addmemory( &buffer, p_data, i_data_old );
954 /* Append padding to the command data */
955 var_buffer_add64( &buffer, 0 );
958 i_ret = net_Write( p_access, p_sys->i_handle_tcp, NULL, buffer.p_data,
959 buffer.i_data - ( 8 - ( i_data - i_data_old ) ) );
960 if( i_ret != buffer.i_data - ( 8 - ( i_data - i_data_old ) ) )
962 msg_Err( p_access, "failed to send command" );
966 var_buffer_free( &buffer );
970 static int NetFillBuffer( access_t *p_access )
976 access_sys_t *p_sys = p_access->p_sys;
978 struct pollfd ufd[2];
979 unsigned timeout, nfd;
981 /* FIXME when using udp */
982 ssize_t i_tcp, i_udp;
983 ssize_t i_tcp_read, i_udp_read;
986 i_tcp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_tcp;
988 if( p_sys->i_proto == MMS_PROTO_UDP )
990 i_udp = MMS_BUFFER_SIZE/2 - p_sys->i_buffer_udp;
994 i_udp = 0; /* there isn't udp socket */
997 if( ( i_udp <= 0 ) && ( i_tcp <= 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 */
1013 memset (ufd, 0, sizeof (ufd));
1018 ufd[nfd].fd = p_sys->i_handle_tcp;
1019 ufd[nfd].events = POLLIN;
1024 ufd[nfd].fd = p_sys->i_handle_tcp;
1025 ufd[nfd].events = POLLIN;
1029 /* We'll wait 0.5 second if nothing happens */
1032 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1037 if( p_access->b_die || p_access->b_error ) return -1;
1039 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1041 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1042 (i_ret < 0 && errno == EINTR) );
1046 msg_Err( p_access, "network poll error (%m)" );
1050 i_tcp_read = i_udp_read = 0;
1052 if( ( i_tcp > 0 ) && ufd[0].revents )
1055 recv( p_sys->i_handle_tcp,
1056 p_sys->buffer_tcp + p_sys->i_buffer_tcp,
1057 i_tcp + MMS_BUFFER_SIZE/2, 0 );
1060 if( i_udp > 0 && ufd[i_tcp > 0].revents )
1062 i_udp_read = recv( p_sys->i_handle_udp,
1063 p_sys->buffer_udp + p_sys->i_buffer_udp,
1064 i_udp + MMS_BUFFER_SIZE/2, 0 );
1068 if( p_sys->i_proto == MMS_PROTO_UDP )
1070 msg_Dbg( p_access, "filling buffer TCP:%d+%d UDP:%d+%d",
1071 p_sys->i_buffer_tcp, i_tcp_read,
1072 p_sys->i_buffer_udp, i_udp_read );
1076 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1077 p_sys->i_buffer_tcp, i_tcp_read );
1081 if( i_tcp_read > 0 ) p_sys->i_buffer_tcp += i_tcp_read;
1082 if( i_udp_read > 0 ) p_sys->i_buffer_udp += i_udp_read;
1084 return i_tcp_read + i_udp_read;
1088 static int mms_ParseCommand( access_t *p_access,
1093 #define GET32( i_pos ) \
1094 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1095 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1096 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1098 access_sys_t *p_sys = p_access->p_sys;
1102 free( p_sys->p_cmd );
1103 p_sys->i_cmd = i_data;
1104 p_sys->p_cmd = malloc( i_data );
1105 memcpy( p_sys->p_cmd, p_data, i_data );
1107 *pi_used = i_data; /* by default */
1109 if( i_data < MMS_CMD_HEADERSIZE )
1111 msg_Warn( p_access, "truncated command (header incomplete)" );
1112 p_sys->i_command = 0;
1115 i_id = GetDWLE( p_data + 4 );
1116 i_length = GetDWLE( p_data + 8 ) + 16;
1118 if( i_id != 0xb00bface )
1121 "incorrect command header (0x%x)", i_id );
1122 p_sys->i_command = 0;
1126 if( i_length > p_sys->i_cmd )
1129 "truncated command (missing %d bytes)",
1130 i_length - i_data );
1131 p_sys->i_command = 0;
1134 else if( i_length < p_sys->i_cmd )
1136 p_sys->i_cmd = i_length;
1137 *pi_used = i_length;
1141 "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",
1145 /* 12: protocol type "MMS " */
1148 /* 24: unknown (0) */
1149 /* 28: unknown (0) */
1155 p_sys->i_command = GET32( 36 ) & 0xffff;
1158 return MMS_PACKET_CMD;
1161 static int mms_ParsePacket( access_t *p_access,
1162 uint8_t *p_data, size_t i_data,
1165 access_sys_t *p_sys = p_access->p_sys;
1166 int i_packet_seq_num;
1167 size_t i_packet_length;
1168 uint32_t i_packet_id;
1173 *pi_used = i_data; /* default */
1176 msg_Warn( p_access, "truncated packet (header incomplete)" );
1180 i_packet_id = p_data[4];
1181 i_packet_seq_num = GetDWLE( p_data );
1182 i_packet_length = GetWLE( p_data + 6 );
1184 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1186 if( i_packet_length > i_data || i_packet_length <= 8)
1188 /* msg_Dbg( p_access,
1189 "truncated packet (Declared %d bytes, Actual %d bytes)",
1190 i_packet_length, i_data ); */
1194 else if( i_packet_length < i_data )
1196 *pi_used = i_packet_length;
1199 if( i_packet_id == 0xff )
1202 "receive MMS UDP pair timing" );
1203 return( MMS_PACKET_UDP_TIMING );
1206 if( i_packet_id != p_sys->i_header_packet_id_type &&
1207 i_packet_id != p_sys->i_media_packet_id_type )
1209 msg_Warn( p_access, "incorrect Packet Id Type (0x%x)", i_packet_id );
1213 /* we now have a media or a header packet */
1214 p_packet = malloc( i_packet_length - 8 ); // don't bother with preheader
1215 memcpy( p_packet, p_data + 8, i_packet_length - 8 );
1217 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1220 /* FIXME for udp could be just wrong order ? */
1222 "detected packet lost (%d != %d)",
1224 p_sys->i_packet_seq_num );
1227 p_sys->i_packet_seq_num = i_packet_seq_num + 1;
1229 if( i_packet_id == p_sys->i_header_packet_id_type )
1231 if( p_sys->p_header )
1233 p_sys->p_header = realloc( p_sys->p_header,
1234 p_sys->i_header + i_packet_length - 8 );
1235 memcpy( &p_sys->p_header[p_sys->i_header],
1237 i_packet_length - 8 );
1238 p_sys->i_header += i_packet_length - 8;
1244 p_sys->p_header = p_packet;
1245 p_sys->i_header = i_packet_length - 8;
1247 /* msg_Dbg( p_access,
1248 "receive header packet (%d bytes)",
1249 i_packet_length - 8 ); */
1251 return MMS_PACKET_HEADER;
1255 FREENULL( p_sys->p_media );
1256 p_sys->p_media = p_packet;
1257 p_sys->i_media = i_packet_length - 8;
1258 p_sys->i_media_used = 0;
1259 /* msg_Dbg( p_access,
1260 "receive media packet (%d bytes)",
1261 i_packet_length - 8 ); */
1263 return MMS_PACKET_MEDIA;
1267 static int mms_ReceivePacket( access_t *p_access )
1269 access_sys_t *p_sys = p_access->p_sys;
1270 int i_packet_tcp_type;
1271 int i_packet_udp_type;
1275 vlc_bool_t b_refill = VLC_TRUE;
1277 /* first if we need to refill buffer */
1278 if( p_sys->i_buffer_tcp >= MMS_CMD_HEADERSIZE )
1280 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1282 if( GetDWLE( p_sys->buffer_tcp + 8 ) + 16 <=
1283 (uint32_t)p_sys->i_buffer_tcp )
1285 b_refill = VLC_FALSE;
1288 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1290 b_refill = VLC_FALSE;
1293 if( p_sys->i_proto == MMS_PROTO_UDP && p_sys->i_buffer_udp >= 8 &&
1294 GetWLE( p_sys->buffer_udp + 6 ) <= p_sys->i_buffer_udp )
1296 b_refill = VLC_FALSE;
1299 if( b_refill && NetFillBuffer( p_access ) < 0 )
1301 msg_Warn( p_access, "cannot fill buffer" );
1305 i_packet_tcp_type = -1;
1306 i_packet_udp_type = -1;
1308 if( p_sys->i_buffer_tcp > 0 )
1312 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1315 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1316 p_sys->i_buffer_tcp, &i_used );
1322 mms_ParsePacket( p_access, p_sys->buffer_tcp,
1323 p_sys->i_buffer_tcp, &i_used );
1325 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1327 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1328 MMS_BUFFER_SIZE - i_used );
1330 p_sys->i_buffer_tcp -= i_used;
1332 else if( p_sys->i_buffer_udp > 0 )
1337 mms_ParsePacket( p_access, p_sys->buffer_udp,
1338 p_sys->i_buffer_udp, &i_used );
1340 if( i_used > 0 && i_used < MMS_BUFFER_SIZE )
1342 memmove( p_sys->buffer_udp, p_sys->buffer_udp + i_used,
1343 MMS_BUFFER_SIZE - i_used );
1345 p_sys->i_buffer_udp -= i_used;
1348 if( i_packet_tcp_type == MMS_PACKET_CMD && p_sys->i_command == 0x1b )
1350 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1351 i_packet_tcp_type = -1;
1354 if( i_packet_tcp_type != -1 )
1356 return i_packet_tcp_type;
1358 else if( i_packet_udp_type != -1 )
1360 return i_packet_udp_type;
1365 static int mms_ReceiveCommand( access_t *p_access )
1367 access_sys_t *p_sys = p_access->p_sys;
1374 if( NetFillBuffer( p_access ) < 0 )
1376 msg_Warn( p_access, "cannot fill buffer" );
1377 return VLC_EGENERIC;
1379 if( p_sys->i_buffer_tcp > 0 )
1381 i_status = mms_ParseCommand( p_access, p_sys->buffer_tcp,
1382 p_sys->i_buffer_tcp, &i_used );
1383 if( i_used < MMS_BUFFER_SIZE )
1385 memmove( p_sys->buffer_tcp, p_sys->buffer_tcp + i_used,
1386 MMS_BUFFER_SIZE - i_used );
1388 p_sys->i_buffer_tcp -= i_used;
1392 return VLC_EGENERIC;
1395 if( p_sys->i_command == 0x1b )
1397 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1406 return VLC_EGENERIC;
1413 #define MMS_RETRY_MAX 10
1414 #define MMS_RETRY_SLEEP 50000
1416 static int mms_CommandRead( access_t *p_access, int i_command1,
1419 access_sys_t *p_sys = p_access->p_sys;
1423 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1425 i_status = mms_ReceiveCommand( p_access );
1426 if( i_status < 0 || p_sys->i_command == 0 )
1429 msleep( MMS_RETRY_SLEEP );
1431 else if( i_command1 == 0 && i_command2 == 0)
1435 else if( p_sys->i_command == i_command1 ||
1436 p_sys->i_command == i_command2 )
1442 switch( p_sys->i_command )
1445 msg_Warn( p_access, "socket closed by server" );
1446 p_access->info.b_eof = 1;
1447 return VLC_EGENERIC;
1449 msg_Warn( p_access, "end of media stream" );
1450 p_access->info.b_eof = 1;
1451 return VLC_EGENERIC;
1457 msg_Warn( p_access, "failed to receive command (aborting)" );
1459 return VLC_EGENERIC;
1463 static int mms_HeaderMediaRead( access_t *p_access, int i_type )
1465 access_sys_t *p_sys = p_access->p_sys;
1468 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1472 if( p_access->b_die )
1475 i_status = mms_ReceivePacket( p_access );
1479 msg_Warn( p_access, "cannot receive header (%d/%d)",
1480 i_count, MMS_RETRY_MAX );
1481 msleep( MMS_RETRY_SLEEP );
1483 else if( i_status == i_type || i_type == MMS_PACKET_ANY )
1487 else if( i_status == MMS_PACKET_CMD )
1489 switch( p_sys->i_command )
1492 msg_Warn( p_access, "socket closed by server" );
1493 p_access->info.b_eof = 1;
1496 msg_Warn( p_access, "end of media stream" );
1497 p_access->info.b_eof = 1;
1500 /* XXX not too dificult to be done EXCEPT that we
1501 * need to restart demuxer... and I don't see how we
1502 * could do that :p */
1504 "reinitialization needed --> unsupported" );
1505 p_access->info.b_eof = VLC_TRUE;
1513 msg_Err( p_access, "cannot receive %s (aborting)",
1514 ( i_type == MMS_PACKET_HEADER ) ? "header" : "media data" );