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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_access.h>
41 #include <sys/types.h>
46 #include <vlc_network.h>
56 /****************************************************************************
58 * MMSProtocole documentation found at http://get.to/sdp
59 ****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
64 int MMSTUOpen ( access_t
* );
65 void MMSTUClose ( access_t
* );
68 static block_t
*Block( access_t
* );
69 static int Seek( access_t
*, uint64_t );
70 static int Control( access_t
*, int, va_list );
72 static int MMSOpen ( access_t
*, vlc_url_t
*, int );
73 static int MMSStart( access_t
*, uint32_t );
74 static int MMSStop ( access_t
* );
75 static void MMSClose( access_t
* );
78 static int mms_CommandRead( access_t
*p_access
, int i_command1
, int i_command2
);
79 static int mms_CommandSend( access_t
*, int, uint32_t, uint32_t, uint8_t *, int );
81 static int mms_HeaderMediaRead( access_t
*, int );
83 static int mms_ReceivePacket( access_t
* );
85 static void KeepAliveStart( access_t
* );
86 static void KeepAliveStop( access_t
* );
88 int MMSTUOpen( access_t
*p_access
)
95 access_InitFields( p_access
);
96 p_access
->pf_read
= NULL
;
97 p_access
->pf_block
= Block
;
98 p_access
->pf_control
= Control
;
99 p_access
->pf_seek
= Seek
;
101 p_access
->p_sys
= p_sys
= calloc( 1, sizeof( access_sys_t
) );
102 if( !p_sys
) return VLC_ENOMEM
;
104 p_sys
->i_timeout
= var_CreateGetInteger( p_access
, "mms-timeout" );
106 vlc_mutex_init( &p_sys
->lock_netwrite
);
108 /* *** Parse URL and get server addr/port and path *** */
109 vlc_UrlParse( &p_sys
->url
, p_access
->psz_location
, 0 );
110 if( p_sys
->url
.psz_host
== NULL
|| *p_sys
->url
.psz_host
== '\0' )
112 msg_Err( p_access
, "invalid server name" );
113 vlc_UrlClean( &p_sys
->url
);
114 vlc_mutex_destroy( &p_sys
->lock_netwrite
);
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 if( !p_access
->b_die
)
144 i_status
= MMSOpen( p_access
, &p_sys
->url
, MMS_PROTO_UDP
);
149 i_status
= MMSOpen( p_access
, &p_sys
->url
, i_proto
);
154 msg_Err( p_access
, "cannot connect to server" );
155 vlc_UrlClean( &p_sys
->url
);
156 vlc_mutex_destroy( &p_sys
->lock_netwrite
);
161 msg_Dbg( p_access
, "connected to %s:%d", p_sys
->url
.psz_host
, p_sys
->url
.i_port
);
165 * broadcast yy=0x02, xx= 0x00
166 * pre-recorded yy=0x01, xx= 0x80 if video, 0x00 no video
168 if( p_sys
->i_packet_count
<= 0 && p_sys
->asfh
.i_data_packets_count
> 0 )
170 p_sys
->i_packet_count
= p_sys
->asfh
.i_data_packets_count
;
172 if( p_sys
->i_packet_count
<= 0 || ( p_sys
->i_flags_broadcast
>> 24 ) == 0x02 )
174 p_sys
->b_seekable
= false;
178 p_sys
->b_seekable
= true;
179 p_access
->info
.i_size
=
180 (uint64_t)p_sys
->i_header
+
181 (uint64_t)p_sys
->i_packet_count
* (uint64_t)p_sys
->i_packet_length
;
183 p_sys
->b_keep_alive
= false;
185 /* *** Start stream *** */
186 if( MMSStart( p_access
, 0xffffffff ) < 0 )
188 msg_Err( p_access
, "cannot start stream" );
189 MMSTUClose ( p_access
);
196 /*****************************************************************************
197 * Close: free unused data structures
198 *****************************************************************************/
199 void MMSTUClose( access_t
*p_access
)
201 access_sys_t
*p_sys
= p_access
->p_sys
;
203 KeepAliveStop( p_access
);
205 /* close connection with server */
206 MMSClose( p_access
);
209 vlc_UrlClean( &p_sys
->url
);
214 /*****************************************************************************
216 *****************************************************************************/
217 static int Control( access_t
*p_access
, int i_query
, va_list args
)
219 access_sys_t
*p_sys
= p_access
->p_sys
;
228 case ACCESS_CAN_SEEK
:
229 pb_bool
= (bool*)va_arg( args
, bool* );
230 *pb_bool
= p_sys
->b_seekable
;
233 case ACCESS_CAN_FASTSEEK
:
234 pb_bool
= (bool*)va_arg( args
, bool* );
238 case ACCESS_CAN_PAUSE
:
239 pb_bool
= (bool*)va_arg( args
, bool* );
243 case ACCESS_CAN_CONTROL_PACE
:
244 pb_bool
= (bool*)va_arg( args
, bool* );
246 #if 0 /* Disable for now until we have a clock synchro algo
247 * which works with something else than MPEG over UDP */
254 case ACCESS_GET_PTS_DELAY
:
255 pi_64
= (int64_t*)va_arg( args
, int64_t * );
256 *pi_64
= var_GetInteger( p_access
, "mms-caching" ) * INT64_C(1000);
259 case ACCESS_GET_PRIVATE_ID_STATE
:
260 i_int
= (int)va_arg( args
, int );
261 pb_bool
= (bool *)va_arg( args
, bool * );
263 if( i_int
< 0 || i_int
> 127 )
265 *pb_bool
= p_sys
->asfh
.stream
[i_int
].i_selected
? true : false;
269 case ACCESS_SET_PAUSE_STATE
:
270 b_bool
= (bool)va_arg( args
, int );
274 KeepAliveStart( p_access
);
278 KeepAliveStop( p_access
);
279 Seek( p_access
, p_access
->info
.i_pos
);
283 case ACCESS_GET_TITLE_INFO
:
284 case ACCESS_SET_TITLE
:
285 case ACCESS_SET_SEEKPOINT
:
286 case ACCESS_SET_PRIVATE_ID_STATE
:
287 case ACCESS_GET_CONTENT_TYPE
:
292 msg_Warn( p_access
, "unimplemented query in control" );
299 /*****************************************************************************
300 * Seek: try to go at the right place
301 *****************************************************************************/
302 static int Seek( access_t
* p_access
, uint64_t i_pos
)
304 access_sys_t
*p_sys
= p_access
->p_sys
;
309 if( i_pos
< p_sys
->i_header
)
311 if( p_access
->info
.i_pos
< p_sys
->i_header
)
313 /* no need to restart stream, it was already one
314 * or no stream was yet read */
315 p_access
->info
.i_pos
= i_pos
;
320 i_packet
= 0xffffffff;
326 i_packet
= ( i_pos
- p_sys
->i_header
) / p_sys
->i_packet_length
;
327 i_offset
= ( i_pos
- p_sys
->i_header
) % p_sys
->i_packet_length
;
329 if( p_sys
->b_seekable
&& i_packet
>= p_sys
->i_packet_count
)
332 msg_Dbg( p_access
, "seeking to %"PRIu64
" (packet:%u)", i_pos
, i_packet
);
335 msg_Dbg( p_access
, "stream stopped (seek)" );
337 /* *** restart stream *** */
338 var_buffer_initwrite( &buffer
, 0 );
339 var_buffer_add64( &buffer
, 0 ); /* seek point in second */
340 var_buffer_add32( &buffer
, 0xffffffff );
341 var_buffer_add32( &buffer
, i_packet
); // begin from start
342 var_buffer_add8( &buffer
, 0xff ); // stream time limit
343 var_buffer_add8( &buffer
, 0xff ); // on 3bytes ...
344 var_buffer_add8( &buffer
, 0xff ); //
345 var_buffer_add8( &buffer
, 0x00 ); // don't use limit
346 var_buffer_add32( &buffer
, p_sys
->i_media_packet_id_type
);
348 mms_CommandSend( p_access
, 0x07, p_sys
->i_command_level
, 0x0001ffff,
349 buffer
.p_data
, buffer
.i_data
);
351 var_buffer_free( &buffer
);
354 while( vlc_object_alive (p_access
) )
356 if( mms_HeaderMediaRead( p_access
, MMS_PACKET_CMD
) < 0 )
358 p_access
->info
.b_eof
= true;
362 if( p_sys
->i_command
== 0x1e )
364 msg_Dbg( p_access
, "received 0x1e (seek)" );
369 while( vlc_object_alive (p_access
) )
371 if( mms_HeaderMediaRead( p_access
, MMS_PACKET_CMD
) < 0 )
373 p_access
->info
.b_eof
= true;
376 if( p_sys
->i_command
== 0x05 )
378 msg_Dbg( p_access
, "received 0x05 (seek)" );
384 if( mms_HeaderMediaRead( p_access
, MMS_PACKET_MEDIA
) < 0 )
386 p_access
->info
.b_eof
= true;
390 msg_Dbg( p_access
, "Streaming restarted" );
392 p_sys
->i_media_used
+= i_offset
;
393 p_access
->info
.i_pos
= i_pos
;
394 p_access
->info
.b_eof
= false;
399 /*****************************************************************************
401 *****************************************************************************/
402 static block_t
*Block( access_t
*p_access
)
404 access_sys_t
*p_sys
= p_access
->p_sys
;
406 if( p_access
->info
.b_eof
)
409 if( p_access
->info
.i_pos
< p_sys
->i_header
)
411 const size_t i_copy
= p_sys
->i_header
- p_access
->info
.i_pos
;
413 block_t
*p_block
= block_New( p_access
, i_copy
);
417 memcpy( p_block
->p_buffer
, &p_sys
->p_header
[p_access
->info
.i_pos
], i_copy
);
418 p_access
->info
.i_pos
+= i_copy
;
421 else if( p_sys
->p_media
&& p_sys
->i_media_used
< __MAX( p_sys
->i_media
, p_sys
->i_packet_length
) )
424 size_t i_padding
= 0;
426 if( p_sys
->i_media_used
< p_sys
->i_media
)
427 i_copy
= p_sys
->i_media
- p_sys
->i_media_used
;
428 if( __MAX( p_sys
->i_media
, p_sys
->i_media_used
) < p_sys
->i_packet_length
)
429 i_padding
= p_sys
->i_packet_length
- __MAX( p_sys
->i_media
, p_sys
->i_media_used
);
431 block_t
*p_block
= block_New( p_access
, i_copy
+ i_padding
);
436 memcpy( &p_block
->p_buffer
[0], &p_sys
->p_media
[p_sys
->i_media_used
], i_copy
);
438 memset( &p_block
->p_buffer
[i_copy
], 0, i_padding
);
440 p_sys
->i_media_used
+= i_copy
+ i_padding
;
441 p_access
->info
.i_pos
+= i_copy
+ i_padding
;
445 mms_HeaderMediaRead( p_access
, MMS_PACKET_MEDIA
);
449 /****************************************************************************
450 * MMSOpen : Open a connection with the server over mmst or mmsu
451 ****************************************************************************/
452 static int MMSOpen( access_t
*p_access
, vlc_url_t
*p_url
, int i_proto
)
454 access_sys_t
*p_sys
= p_access
->p_sys
;
455 int b_udp
= ( i_proto
== MMS_PROTO_UDP
) ? 1 : 0;
460 int i_server_version
;
462 int i_update_player_url
;
463 int i_encryption_type
;
470 /* *** Open a TCP connection with server *** */
471 msg_Dbg( p_access
, "waiting for connection..." );
472 p_sys
->i_handle_tcp
= net_ConnectTCP( p_access
, p_url
->psz_host
, p_url
->i_port
);
473 if( p_sys
->i_handle_tcp
< 0 )
475 msg_Err( p_access
, "failed to open a connection (tcp)" );
479 "connection(tcp) with \"%s:%d\" successful",
483 /* *** Bind port if UDP protocol is selected *** */
486 if( net_GetSockAddress( p_sys
->i_handle_tcp
, p_sys
->sz_bind_addr
,
489 net_Close( p_sys
->i_handle_tcp
);
493 p_sys
->i_handle_udp
= net_ListenUDP1( (vlc_object_t
*)p_access
, p_sys
->sz_bind_addr
,
495 if( p_sys
->i_handle_udp
< 0 )
497 msg_Err( p_access
, "failed to open a connection (udp)" );
498 net_Close( p_sys
->i_handle_tcp
);
502 "connection(udp) at \"%s:%d\" successful",
503 p_sys
->sz_bind_addr
, 7000 );
506 /* *** Init context for mms prototcol *** */
507 GenerateGuid ( &p_sys
->guid
); /* used to identify client by server */
509 "generated guid: "GUID_FMT
,
510 GUID_PRINT( p_sys
->guid
) );
511 p_sys
->i_command_level
= 1; /* updated after 0x1A command */
512 p_sys
->i_seq_num
= 0;
513 p_sys
->i_media_packet_id_type
= 0x04;
514 p_sys
->i_header_packet_id_type
= 0x02;
515 p_sys
->i_proto
= i_proto
;
516 p_sys
->i_packet_seq_num
= 0;
517 p_sys
->p_header
= NULL
;
519 p_sys
->p_media
= NULL
;
521 p_sys
->i_media_used
= 0;
523 p_access
->info
.i_pos
= 0;
524 p_sys
->i_buffer_tcp
= 0;
525 p_sys
->i_buffer_udp
= 0;
528 p_access
->info
.b_eof
= false;
530 /* *** send command 1 : connection request *** */
531 var_buffer_initwrite( &buffer
, 0 );
532 var_buffer_add16( &buffer
, 0x001c );
533 var_buffer_add16( &buffer
, 0x0003 );
535 "NSPlayer/7.0.0.1956; {"GUID_FMT
"}; Host: %s",
536 GUID_PRINT( p_sys
->guid
),
538 var_buffer_addUTF16( &buffer
, tmp
);
540 mms_CommandSend( p_access
,
541 0x01, /* connexion request */
542 0x00000000, /* flags, FIXME */
543 0x0004000b, /* ???? */
547 if( mms_CommandRead( p_access
, 0x01, 0 ) < 0 )
549 var_buffer_free( &buffer
);
550 MMSClose( p_access
);
554 i_server_version
= GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 32 );
555 i_tool_version
= GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 36 );
556 i_update_player_url
= GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 40 );
557 i_encryption_type
= GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 44 );
558 p
= (uint16_t*)( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 48 );
559 #define GETUTF16( psz, size ) \
562 psz = xmalloc( size + 1); \
563 for( i = 0; i < size; i++ ) \
570 GETUTF16( p_sys
->psz_server_version
, i_server_version
);
571 GETUTF16( p_sys
->psz_tool_version
, i_tool_version
);
572 GETUTF16( p_sys
->psz_update_player_url
, i_update_player_url
);
573 GETUTF16( p_sys
->psz_encryption_type
, i_encryption_type
);
576 "0x01 --> server_version:\"%s\" tool_version:\"%s\" update_player_url:\"%s\" encryption_type:\"%s\"",
577 p_sys
->psz_server_version
,
578 p_sys
->psz_tool_version
,
579 p_sys
->psz_update_player_url
,
580 p_sys
->psz_encryption_type
);
582 /* *** should make an 18 command to make data timing *** */
584 /* *** send command 2 : transport protocol selection *** */
585 var_buffer_reinitwrite( &buffer
, 0 );
586 var_buffer_add32( &buffer
, 0x00000000 );
587 var_buffer_add32( &buffer
, 0x000a0000 );
588 var_buffer_add32( &buffer
, 0x00000002 );
598 sprintf( tmp
, "\\\\192.168.0.1\\TCP\\1242" );
600 var_buffer_addUTF16( &buffer
, tmp
);
601 var_buffer_add16( &buffer
, '0' );
603 mms_CommandSend( p_access
,
604 0x02, /* connexion request */
605 0x00000000, /* flags, FIXME */
606 0xffffffff, /* ???? */
610 /* *** response from server, should be 0x02 or 0x03 *** */
611 mms_CommandRead( p_access
, 0x02, 0x03 );
612 if( p_sys
->i_command
== 0x03 )
615 "%s protocol selection failed", b_udp
? "UDP" : "TCP" );
616 var_buffer_free( &buffer
);
617 MMSClose( p_access
);
620 else if( p_sys
->i_command
!= 0x02 )
622 msg_Warn( p_access
, "received command isn't 0x02 in reponse to 0x02" );
625 /* *** send command 5 : media file name/path requested *** */
626 var_buffer_reinitwrite( &buffer
, 0 );
627 var_buffer_add64( &buffer
, 0 );
629 /* media file path shouldn't start with / character */
630 mediapath
= p_url
->psz_path
;
631 if ( *mediapath
== '/' )
635 var_buffer_addUTF16( &buffer
, mediapath
);
637 mms_CommandSend( p_access
,
639 p_sys
->i_command_level
,
644 /* *** wait for reponse *** */
645 mms_CommandRead( p_access
, 0x1a, 0x06 );
647 /* test if server send 0x1A answer */
648 if( p_sys
->i_command
== 0x1A )
650 msg_Err( p_access
, "id/password requested (not yet supported)" );
652 var_buffer_free( &buffer
);
653 MMSClose( p_access
);
656 if( p_sys
->i_command
!= 0x06 )
659 "unknown answer (0x%x instead of 0x06)",
661 var_buffer_free( &buffer
);
662 MMSClose( p_access
);
666 /* 1 for file ok, 2 for authen ok */
667 switch( GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
) )
670 msg_Dbg( p_access
, "media file name/path accepted" );
673 msg_Dbg( p_access
, "authentication accepted" );
677 msg_Err( p_access
, "error while asking for file %d",
678 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
) );
679 var_buffer_free( &buffer
);
680 MMSClose( p_access
);
684 p_sys
->i_flags_broadcast
=
685 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 12 );
686 p_sys
->i_media_length
=
687 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 24 );
688 p_sys
->i_packet_length
=
689 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 44 );
690 p_sys
->i_packet_count
=
691 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 48 );
692 p_sys
->i_max_bit_rate
=
693 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 56 );
694 p_sys
->i_header_size
=
695 GetDWLE( p_sys
->p_cmd
+ MMS_CMD_HEADERSIZE
+ 60 );
698 "answer 0x06 flags:0x%8.8"PRIx32
" media_length:%"PRIu32
"s "
699 "packet_length:%zu packet_count:%"PRIu32
" max_bit_rate:%d "
701 p_sys
->i_flags_broadcast
,
702 p_sys
->i_media_length
,
703 p_sys
->i_packet_length
,
704 p_sys
->i_packet_count
,
705 p_sys
->i_max_bit_rate
,
706 p_sys
->i_header_size
);
708 /* *** send command 15 *** */
710 var_buffer_reinitwrite( &buffer
, 0 );
711 var_buffer_add32( &buffer
, 0 );
712 var_buffer_add32( &buffer
, 0x8000 );
713 var_buffer_add32( &buffer
, 0xffffffff );
714 var_buffer_add32( &buffer
, 0x00 );
715 var_buffer_add32( &buffer
, 0x00 );
716 var_buffer_add32( &buffer
, 0x00 );
717 var_buffer_add64( &buffer
, (((uint64_t)0x40ac2000)<<32) );
718 var_buffer_add32( &buffer
, p_sys
->i_header_packet_id_type
);
719 var_buffer_add32( &buffer
, 0x00 );
720 mms_CommandSend( p_access
, 0x15, p_sys
->i_command_level
, 0x00,
721 buffer
.p_data
, buffer
.i_data
);
723 /* *** wait for reponse *** */
724 /* Commented out because it fails on some stream (no 0x11 answer) */
726 mms_CommandRead( p_access
, 0x11, 0 );
728 if( p_sys
->i_command
!= 0x11 )
731 "unknown answer (0x%x instead of 0x11)",
733 var_buffer_free( &buffer
);
734 MMSClose( p_access
);
739 /* *** now read header packet *** */
740 /* XXX could be split over multiples packets */
741 msg_Dbg( p_access
, "reading header" );
744 if( mms_HeaderMediaRead( p_access
, MMS_PACKET_HEADER
) < 0 )
746 msg_Err( p_access
, "cannot receive header" );
747 var_buffer_free( &buffer
);
748 MMSClose( p_access
);
751 if( p_sys
->i_header
>= p_sys
->i_header_size
)
754 "header complete(%zu)",
759 "header incomplete (%zu/%zu), reading more",
761 p_sys
->i_header_size
);
764 /* *** parse header and get stream and their id *** */
765 /* get all streams properties,
767 * TODO : stream bitrates properties(optional)
768 * and bitrate mutual exclusion(optional) */
769 asf_HeaderParse ( &p_sys
->asfh
,
770 p_sys
->p_header
, p_sys
->i_header
);
771 asf_StreamSelect( &p_sys
->asfh
,
772 var_InheritInteger( p_access
, "mms-maxbitrate" ),
773 var_InheritBool( p_access
, "mms-all" ),
774 var_InheritBool( p_access
, "audio" ),
775 var_InheritBool( p_access
, "video" ) );
777 /* *** now select stream we want to receive *** */
778 /* TODO take care of stream bitrate TODO */
781 var_buffer_reinitwrite( &buffer
, 0 );
782 /* for now, select first audio and video stream */
783 for( i
= 1; i
< 128; i
++ )
786 if( p_sys
->asfh
.stream
[i
].i_cat
!= ASF_STREAM_UNKNOWN
)
791 var_buffer_add16( &buffer
, 0xffff );
792 var_buffer_add16( &buffer
, i
);
798 if( p_sys
->asfh
.stream
[i
].i_selected
)
800 var_buffer_add16( &buffer
, 0x0000 );
802 "selecting stream[0x%x] %s (%d Kib/s)",
804 ( p_sys
->asfh
.stream
[i
].i_cat
== ASF_STREAM_AUDIO
) ?
806 p_sys
->asfh
.stream
[i
].i_bitrate
/ 1024);
810 var_buffer_add16( &buffer
, 0x0002 );
812 "ignoring stream[0x%x] %s (%d Kib/s)",
814 ( p_sys
->asfh
.stream
[i
].i_cat
== ASF_STREAM_AUDIO
) ?
816 p_sys
->asfh
.stream
[i
].i_bitrate
/ 1024);
824 msg_Err( p_access
, "cannot find any stream" );
825 var_buffer_free( &buffer
);
826 MMSClose( p_access
);
829 mms_CommandSend( p_access
, 0x33,
831 0xffff | ( i_first
<< 16 ),
832 buffer
.p_data
, buffer
.i_data
);
834 mms_CommandRead( p_access
, 0x21, 0 );
835 if( p_sys
->i_command
!= 0x21 )
838 "unknown answer (0x%x instead of 0x21)",
840 var_buffer_free( &buffer
);
841 MMSClose( p_access
);
846 var_buffer_free( &buffer
);
848 msg_Info( p_access
, "connection successful" );
853 /****************************************************************************
854 * MMSStart : Start streaming
855 ****************************************************************************/
856 static int MMSStart( access_t
*p_access
, uint32_t i_packet
)
858 access_sys_t
*p_sys
= p_access
->p_sys
;
861 /* *** start stream from packet 0 *** */
862 var_buffer_initwrite( &buffer
, 0 );
863 var_buffer_add64( &buffer
, 0 ); /* seek point in second */
864 var_buffer_add32( &buffer
, 0xffffffff );
865 var_buffer_add32( &buffer
, i_packet
); // begin from start
866 var_buffer_add8( &buffer
, 0xff ); // stream time limit
867 var_buffer_add8( &buffer
, 0xff ); // on 3bytes ...
868 var_buffer_add8( &buffer
, 0xff ); //
869 var_buffer_add8( &buffer
, 0x00 ); // don't use limit
870 var_buffer_add32( &buffer
, p_sys
->i_media_packet_id_type
);
872 mms_CommandSend( p_access
, 0x07, p_sys
->i_command_level
, 0x0001ffff,
873 buffer
.p_data
, buffer
.i_data
);
875 var_buffer_free( &buffer
);
877 mms_CommandRead( p_access
, 0x05, 0 );
879 if( p_sys
->i_command
!= 0x05 )
882 "unknown answer (0x%x instead of 0x05)",
889 if( mms_HeaderMediaRead( p_access
, MMS_PACKET_MEDIA
) < 0 )
891 msg_Dbg( p_access
, "streaming started" );
896 /****************************************************************************
897 * MMSStop : Stop streaming
898 ****************************************************************************/
899 static int MMSStop( access_t
*p_access
)
901 access_sys_t
*p_sys
= p_access
->p_sys
;
903 /* *** stop stream but keep connection alive *** */
904 mms_CommandSend( p_access
,
906 p_sys
->i_command_level
,
912 /****************************************************************************
913 * MMSClose : Close streaming and connection
914 ****************************************************************************/
915 static void MMSClose( access_t
*p_access
)
917 access_sys_t
*p_sys
= p_access
->p_sys
;
919 msg_Dbg( p_access
, "Connection closed" );
921 /* *** tell server that we will disconnect *** */
922 mms_CommandSend( p_access
,
924 p_sys
->i_command_level
,
928 /* *** close sockets *** */
929 net_Close( p_sys
->i_handle_tcp
);
930 if( p_sys
->i_proto
== MMS_PROTO_UDP
)
932 net_Close( p_sys
->i_handle_udp
);
935 FREENULL( p_sys
->p_cmd
);
936 FREENULL( p_sys
->p_media
);
937 FREENULL( p_sys
->p_header
);
939 FREENULL( p_sys
->psz_server_version
);
940 FREENULL( p_sys
->psz_tool_version
);
941 FREENULL( p_sys
->psz_update_player_url
);
942 FREENULL( p_sys
->psz_encryption_type
);
945 /****************************************************************************
947 * MMS specific functions
949 ****************************************************************************/
950 static int mms_CommandSend( access_t
*p_access
, int i_command
,
951 uint32_t i_prefix1
, uint32_t i_prefix2
,
952 uint8_t *p_data
, int i_data_old
)
955 access_sys_t
*p_sys
= p_access
->p_sys
;
956 int i_data_by8
, i_ret
;
957 int i_data
= i_data_old
;
959 while( i_data
& 0x7 ) i_data
++;
960 i_data_by8
= i_data
>> 3;
962 /* first init buffer */
963 var_buffer_initwrite( &buffer
, 0 );
965 var_buffer_add32( &buffer
, 0x00000001 ); /* start sequence */
966 var_buffer_add32( &buffer
, 0xB00BFACE );
967 /* size after protocol type */
968 var_buffer_add32( &buffer
, i_data
+ MMS_CMD_HEADERSIZE
- 16 );
969 var_buffer_add32( &buffer
, 0x20534d4d ); /* protocol "MMS " */
970 var_buffer_add32( &buffer
, i_data_by8
+ 4 );
971 var_buffer_add32( &buffer
, p_sys
->i_seq_num
); p_sys
->i_seq_num
++;
972 var_buffer_add64( &buffer
, 0 );
973 var_buffer_add32( &buffer
, i_data_by8
+ 2 );
974 var_buffer_add32( &buffer
, 0x00030000 | i_command
); /* dir | command */
975 var_buffer_add32( &buffer
, i_prefix1
); /* command specific */
976 var_buffer_add32( &buffer
, i_prefix2
); /* command specific */
978 /* specific command data */
979 if( p_data
&& i_data
> 0 )
981 var_buffer_addmemory( &buffer
, p_data
, i_data_old
);
984 /* Append padding to the command data */
985 var_buffer_add64( &buffer
, 0 );
988 vlc_mutex_lock( &p_sys
->lock_netwrite
);
989 i_ret
= net_Write( p_access
, p_sys
->i_handle_tcp
, NULL
, buffer
.p_data
,
990 buffer
.i_data
- ( 8 - ( i_data
- i_data_old
) ) );
991 vlc_mutex_unlock( &p_sys
->lock_netwrite
);
993 if( i_ret
!= buffer
.i_data
- ( 8 - ( i_data
- i_data_old
) ) )
995 var_buffer_free( &buffer
);
996 msg_Err( p_access
, "failed to send command" );
1000 var_buffer_free( &buffer
);
1004 static int NetFillBuffer( access_t
*p_access
)
1010 access_sys_t
*p_sys
= p_access
->p_sys
;
1012 struct pollfd ufd
[2];
1013 unsigned timeout
, nfd
;
1015 /* FIXME when using udp */
1016 ssize_t i_tcp
, i_udp
;
1017 ssize_t i_tcp_read
, i_udp_read
;
1020 i_tcp
= MMS_BUFFER_SIZE
/2 - p_sys
->i_buffer_tcp
;
1022 if( p_sys
->i_proto
== MMS_PROTO_UDP
)
1024 i_udp
= MMS_BUFFER_SIZE
/2 - p_sys
->i_buffer_udp
;
1028 i_udp
= 0; /* there isn't udp socket */
1031 if( ( i_udp
<= 0 ) && ( i_tcp
<= 0 ) )
1033 msg_Warn( p_access
, "nothing to read %d:%d", (int)i_tcp
, (int)i_udp
);
1038 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1041 /* Find if some data is available */
1046 /* Initialize file descriptor set */
1047 memset (ufd
, 0, sizeof (ufd
));
1052 ufd
[nfd
].fd
= p_sys
->i_handle_tcp
;
1053 ufd
[nfd
].events
= POLLIN
;
1058 ufd
[nfd
].fd
= p_sys
->i_handle_udp
;
1059 ufd
[nfd
].events
= POLLIN
;
1063 /* We'll wait 0.5 second if nothing happens */
1064 timeout
= __MIN( 500, p_sys
->i_timeout
);
1066 if( i_try
* timeout
> p_sys
->i_timeout
)
1068 msg_Err(p_access
, "no data received");
1072 if( i_try
> 3 && (p_sys
->i_buffer_tcp
> 0 || p_sys
->i_buffer_udp
> 0) )
1077 if( !vlc_object_alive (p_access
) )
1080 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1082 } while( !(i_ret
= poll( ufd
, nfd
, timeout
)) ||
1083 (i_ret
< 0 && errno
== EINTR
) );
1087 msg_Err( p_access
, "network poll error (%m)" );
1091 i_tcp_read
= i_udp_read
= 0;
1093 if( ( i_tcp
> 0 ) && ufd
[0].revents
)
1096 recv( p_sys
->i_handle_tcp
,
1097 p_sys
->buffer_tcp
+ p_sys
->i_buffer_tcp
,
1098 i_tcp
+ MMS_BUFFER_SIZE
/2, 0 );
1101 if( i_udp
> 0 && ufd
[i_tcp
> 0].revents
)
1103 i_udp_read
= recv( p_sys
->i_handle_udp
,
1104 p_sys
->buffer_udp
+ p_sys
->i_buffer_udp
,
1105 i_udp
+ MMS_BUFFER_SIZE
/2, 0 );
1109 if( p_sys
->i_proto
== MMS_PROTO_UDP
)
1111 msg_Dbg( p_access
, "filling buffer TCP:%d+%d UDP:%d+%d",
1112 p_sys
->i_buffer_tcp
, i_tcp_read
,
1113 p_sys
->i_buffer_udp
, i_udp_read
);
1117 msg_Dbg( p_access
, "filling buffer TCP:%d+%d",
1118 p_sys
->i_buffer_tcp
, i_tcp_read
);
1122 if( i_tcp_read
> 0 ) p_sys
->i_buffer_tcp
+= i_tcp_read
;
1123 if( i_udp_read
> 0 ) p_sys
->i_buffer_udp
+= i_udp_read
;
1125 return i_tcp_read
+ i_udp_read
;
1129 static int mms_ParseCommand( access_t
*p_access
,
1134 #define GET32( i_pos ) \
1135 ( p_sys->p_cmd[i_pos] + ( p_sys->p_cmd[i_pos +1] << 8 ) + \
1136 ( p_sys->p_cmd[i_pos + 2] << 16 ) + \
1137 ( p_sys->p_cmd[i_pos + 3] << 24 ) )
1139 access_sys_t
*p_sys
= p_access
->p_sys
;
1143 free( p_sys
->p_cmd
);
1144 p_sys
->i_cmd
= i_data
;
1145 p_sys
->p_cmd
= xmalloc( i_data
);
1146 memcpy( p_sys
->p_cmd
, p_data
, i_data
);
1148 *pi_used
= i_data
; /* by default */
1150 if( i_data
< MMS_CMD_HEADERSIZE
)
1152 msg_Warn( p_access
, "truncated command (header incomplete)" );
1153 p_sys
->i_command
= 0;
1156 i_id
= GetDWLE( p_data
+ 4 );
1157 i_length
= GetDWLE( p_data
+ 8 ) + 16;
1159 if( i_id
!= 0xb00bface || i_length
< 16 )
1162 "incorrect command header (0x%"PRIx32
")", i_id
);
1163 p_sys
->i_command
= 0;
1167 if( i_length
> p_sys
->i_cmd
)
1170 "truncated command (missing %zu bytes)",
1171 (size_t)i_length
- i_data
);
1172 p_sys
->i_command
= 0;
1175 else if( i_length
< p_sys
->i_cmd
)
1177 p_sys
->i_cmd
= i_length
;
1178 *pi_used
= i_length
;
1182 "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",
1186 /* 12: protocol type "MMS " */
1189 /* 24: unknown (0) */
1190 /* 28: unknown (0) */
1196 p_sys
->i_command
= GET32( 36 ) & 0xffff;
1199 return MMS_PACKET_CMD
;
1202 static int mms_ParsePacket( access_t
*p_access
,
1203 uint8_t *p_data
, size_t i_data
,
1206 access_sys_t
*p_sys
= p_access
->p_sys
;
1207 int i_packet_seq_num
;
1208 size_t i_packet_length
;
1209 uint32_t i_packet_id
;
1211 *pi_used
= i_data
; /* default */
1214 msg_Warn( p_access
, "truncated packet (header incomplete)" );
1218 i_packet_id
= p_data
[4];
1219 i_packet_seq_num
= GetDWLE( p_data
);
1220 i_packet_length
= GetWLE( p_data
+ 6 );
1222 //msg_Warn( p_access, "------->i_packet_length=%d, i_data=%d", i_packet_length, i_data );
1224 if( i_packet_length
> i_data
|| i_packet_length
<= 8)
1226 /* msg_Dbg( p_access,
1227 "truncated packet (Declared %d bytes, Actual %d bytes)",
1228 i_packet_length, i_data ); */
1232 else if( i_packet_length
< i_data
)
1234 *pi_used
= i_packet_length
;
1237 if( i_packet_id
== 0xff )
1240 "receive MMS UDP pair timing" );
1241 return( MMS_PACKET_UDP_TIMING
);
1244 if( i_packet_id
!= p_sys
->i_header_packet_id_type
&&
1245 i_packet_id
!= p_sys
->i_media_packet_id_type
)
1247 msg_Warn( p_access
, "incorrect Packet Id Type (0x%x)", i_packet_id
);
1251 /* we now have a media or a header packet */
1252 if( i_packet_seq_num
!= p_sys
->i_packet_seq_num
)
1255 /* FIXME for udp could be just wrong order ? */
1257 "detected packet lost (%d != %d)",
1259 p_sys
->i_packet_seq_num
);
1262 p_sys
->i_packet_seq_num
= i_packet_seq_num
+ 1;
1264 if( i_packet_id
== p_sys
->i_header_packet_id_type
)
1266 if( p_sys
->p_header
)
1268 p_sys
->p_header
= xrealloc( p_sys
->p_header
,
1269 p_sys
->i_header
+ i_packet_length
- 8 );
1270 memcpy( &p_sys
->p_header
[p_sys
->i_header
],
1271 p_data
+ 8, i_packet_length
- 8 );
1272 p_sys
->i_header
+= i_packet_length
- 8;
1277 uint8_t* p_packet
= xmalloc( i_packet_length
- 8 ); // don't bother with preheader
1278 memcpy( p_packet
, p_data
+ 8, i_packet_length
- 8 );
1279 p_sys
->p_header
= p_packet
;
1280 p_sys
->i_header
= i_packet_length
- 8;
1282 /* msg_Dbg( p_access,
1283 "receive header packet (%d bytes)",
1284 i_packet_length - 8 ); */
1286 return MMS_PACKET_HEADER
;
1290 uint8_t* p_packet
= xmalloc( i_packet_length
- 8 ); // don't bother with preheader
1291 memcpy( p_packet
, p_data
+ 8, i_packet_length
- 8 );
1292 FREENULL( p_sys
->p_media
);
1293 p_sys
->p_media
= p_packet
;
1294 p_sys
->i_media
= i_packet_length
- 8;
1295 p_sys
->i_media_used
= 0;
1296 /* msg_Dbg( p_access,
1297 "receive media packet (%d bytes)",
1298 i_packet_length - 8 ); */
1300 return MMS_PACKET_MEDIA
;
1304 static int mms_ReceivePacket( access_t
*p_access
)
1306 access_sys_t
*p_sys
= p_access
->p_sys
;
1307 int i_packet_tcp_type
;
1308 int i_packet_udp_type
;
1312 bool b_refill
= true;
1314 /* first if we need to refill buffer */
1315 if( p_sys
->i_buffer_tcp
>= MMS_CMD_HEADERSIZE
)
1317 if( GetDWLE( p_sys
->buffer_tcp
+ 4 ) == 0xb00bface )
1319 if( GetDWLE( p_sys
->buffer_tcp
+ 8 ) + 16 <=
1320 (uint32_t)p_sys
->i_buffer_tcp
)
1325 else if( GetWLE( p_sys
->buffer_tcp
+ 6 ) <= p_sys
->i_buffer_tcp
)
1330 if( p_sys
->i_proto
== MMS_PROTO_UDP
&& p_sys
->i_buffer_udp
>= 8 &&
1331 GetWLE( p_sys
->buffer_udp
+ 6 ) <= p_sys
->i_buffer_udp
)
1336 if( b_refill
&& NetFillBuffer( p_access
) < 0 )
1338 msg_Warn( p_access
, "cannot fill buffer" );
1342 i_packet_tcp_type
= -1;
1343 i_packet_udp_type
= -1;
1345 if( p_sys
->i_buffer_tcp
> 0 )
1349 if( GetDWLE( p_sys
->buffer_tcp
+ 4 ) == 0xb00bface )
1352 mms_ParseCommand( p_access
, p_sys
->buffer_tcp
,
1353 p_sys
->i_buffer_tcp
, &i_used
);
1359 mms_ParsePacket( p_access
, p_sys
->buffer_tcp
,
1360 p_sys
->i_buffer_tcp
, &i_used
);
1362 if( i_used
> 0 && i_used
< MMS_BUFFER_SIZE
)
1364 memmove( p_sys
->buffer_tcp
, p_sys
->buffer_tcp
+ i_used
,
1365 MMS_BUFFER_SIZE
- i_used
);
1367 p_sys
->i_buffer_tcp
-= i_used
;
1369 else if( p_sys
->i_buffer_udp
> 0 )
1374 mms_ParsePacket( p_access
, p_sys
->buffer_udp
,
1375 p_sys
->i_buffer_udp
, &i_used
);
1377 if( i_used
> 0 && i_used
< MMS_BUFFER_SIZE
)
1379 memmove( p_sys
->buffer_udp
, p_sys
->buffer_udp
+ i_used
,
1380 MMS_BUFFER_SIZE
- i_used
);
1382 p_sys
->i_buffer_udp
-= i_used
;
1385 if( i_packet_tcp_type
== MMS_PACKET_CMD
&& p_sys
->i_command
== 0x1b )
1387 mms_CommandSend( p_access
, 0x1b, 0, 0, NULL
, 0 );
1388 i_packet_tcp_type
= -1;
1391 if( i_packet_tcp_type
!= -1 )
1393 return i_packet_tcp_type
;
1395 else if( i_packet_udp_type
!= -1 )
1397 return i_packet_udp_type
;
1402 static int mms_ReceiveCommand( access_t
*p_access
)
1404 access_sys_t
*p_sys
= p_access
->p_sys
;
1411 if( NetFillBuffer( p_access
) < 0 )
1413 msg_Warn( p_access
, "cannot fill buffer" );
1414 return VLC_EGENERIC
;
1416 if( p_sys
->i_buffer_tcp
> 0 )
1418 i_status
= mms_ParseCommand( p_access
, p_sys
->buffer_tcp
,
1419 p_sys
->i_buffer_tcp
, &i_used
);
1420 if( i_used
< MMS_BUFFER_SIZE
)
1422 memmove( p_sys
->buffer_tcp
, p_sys
->buffer_tcp
+ i_used
,
1423 MMS_BUFFER_SIZE
- i_used
);
1425 p_sys
->i_buffer_tcp
-= i_used
;
1429 return VLC_EGENERIC
;
1432 if( p_sys
->i_command
== 0x1b )
1434 mms_CommandSend( p_access
, 0x1b, 0, 0, NULL
, 0 );
1443 return VLC_EGENERIC
;
1450 #define MMS_RETRY_MAX 10
1451 #define MMS_RETRY_SLEEP 50000
1453 static int mms_CommandRead( access_t
*p_access
, int i_command1
,
1456 access_sys_t
*p_sys
= p_access
->p_sys
;
1460 for( i_count
= 0; i_count
< MMS_RETRY_MAX
; )
1462 i_status
= mms_ReceiveCommand( p_access
);
1463 if( i_status
< 0 || p_sys
->i_command
== 0 )
1466 msleep( MMS_RETRY_SLEEP
);
1468 else if( i_command1
== 0 && i_command2
== 0)
1472 else if( p_sys
->i_command
== i_command1
||
1473 p_sys
->i_command
== i_command2
)
1479 switch( p_sys
->i_command
)
1482 msg_Warn( p_access
, "socket closed by server" );
1483 p_access
->info
.b_eof
= true;
1484 return VLC_EGENERIC
;
1486 msg_Warn( p_access
, "end of media stream" );
1487 p_access
->info
.b_eof
= true;
1488 return VLC_EGENERIC
;
1494 p_access
->info
.b_eof
= true;
1495 msg_Warn( p_access
, "failed to receive command (aborting)" );
1497 return VLC_EGENERIC
;
1501 static int mms_HeaderMediaRead( access_t
*p_access
, int i_type
)
1503 access_sys_t
*p_sys
= p_access
->p_sys
;
1506 for( i_count
= 0; i_count
< MMS_RETRY_MAX
; )
1510 if( !vlc_object_alive (p_access
) )
1513 i_status
= mms_ReceivePacket( p_access
);
1517 msg_Warn( p_access
, "cannot receive header (%d/%d)",
1518 i_count
, MMS_RETRY_MAX
);
1519 msleep( MMS_RETRY_SLEEP
);
1521 else if( i_status
== i_type
|| i_type
== MMS_PACKET_ANY
)
1525 else if( i_status
== MMS_PACKET_CMD
)
1527 switch( p_sys
->i_command
)
1530 msg_Warn( p_access
, "socket closed by server" );
1531 p_access
->info
.b_eof
= true;
1534 msg_Warn( p_access
, "end of media stream" );
1535 p_access
->info
.b_eof
= true;
1538 /* XXX not too dificult to be done EXCEPT that we
1539 * need to restart demuxer... and I don't see how we
1540 * could do that :p */
1542 "reinitialization needed --> unsupported" );
1543 p_access
->info
.b_eof
= true;
1551 msg_Err( p_access
, "cannot receive %s (aborting)",
1552 ( i_type
== MMS_PACKET_HEADER
) ? "header" : "media data" );
1553 p_access
->info
.b_eof
= true;
1557 static void *KeepAliveThread( void *p_data
)
1559 access_t
*p_access
= p_data
;
1563 /* Send keep-alive every ten seconds */
1564 int canc
= vlc_savecancel();
1566 mms_CommandSend( p_access
, 0x1b, 0, 0, NULL
, 0 );
1568 vlc_restorecancel( canc
);
1570 msleep( 10 * CLOCK_FREQ
);
1575 static void KeepAliveStart( access_t
*p_access
)
1577 access_sys_t
*p_sys
= p_access
->p_sys
;
1578 if( p_sys
->b_keep_alive
)
1581 p_sys
->b_keep_alive
= !vlc_clone( &p_sys
->keep_alive
,
1582 KeepAliveThread
, p_access
,
1583 VLC_THREAD_PRIORITY_LOW
);
1586 static void KeepAliveStop( access_t
*p_access
)
1588 access_sys_t
*p_sys
= p_access
->p_sys
;
1589 if( !p_sys
->b_keep_alive
)
1592 vlc_cancel( p_sys
->keep_alive
);
1593 vlc_join( p_sys
->keep_alive
, NULL
);
1594 p_sys
->b_keep_alive
= false;