add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access / mms / mmstu.c
blobe58612f8dde1d1c9c38c388b4875c0e25292ce45
1 /*****************************************************************************
2 * mms.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_access.h>
35 #include <errno.h>
36 #include <assert.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <sys/types.h>
42 #ifdef HAVE_POLL
43 # include <poll.h>
44 #endif
46 #include <vlc_network.h>
47 #include <vlc_url.h>
48 #include "asf.h"
49 #include "buffer.h"
51 #include "mms.h"
52 #include "mmstu.h"
54 #undef MMS_DEBUG
56 /****************************************************************************
57 * NOTES:
58 * MMSProtocole documentation found at http://get.to/sdp
59 ****************************************************************************/
61 /*****************************************************************************
62 * Local prototypes
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 )
90 access_sys_t *p_sys;
91 int i_proto;
92 int i_status;
94 /* Set up 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 );
115 free( p_sys );
116 return VLC_EGENERIC;
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;
138 /* connect */
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 );
147 else
149 i_status = MMSOpen( p_access, &p_sys->url, i_proto );
152 if( i_status )
154 msg_Err( p_access, "cannot connect to server" );
155 vlc_UrlClean( &p_sys->url );
156 vlc_mutex_destroy( &p_sys->lock_netwrite );
157 free( p_sys );
158 return VLC_EGENERIC;
161 msg_Dbg( p_access, "connected to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
163 * i_flags_broadcast
164 * yy xx ?? ??
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;
176 else
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 );
190 return VLC_EGENERIC;
193 return VLC_SUCCESS;
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 );
208 /* free memory */
209 vlc_UrlClean( &p_sys->url );
211 free( p_sys );
214 /*****************************************************************************
215 * Control:
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;
220 bool *pb_bool;
221 bool b_bool;
222 int64_t *pi_64;
223 int i_int;
225 switch( i_query )
227 /* */
228 case ACCESS_CAN_SEEK:
229 pb_bool = (bool*)va_arg( args, bool* );
230 *pb_bool = p_sys->b_seekable;
231 break;
233 case ACCESS_CAN_FASTSEEK:
234 pb_bool = (bool*)va_arg( args, bool* );
235 *pb_bool = false;
236 break;
238 case ACCESS_CAN_PAUSE:
239 pb_bool = (bool*)va_arg( args, bool* );
240 *pb_bool = true;
241 break;
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 */
248 *pb_bool = false;
249 #endif
250 *pb_bool = true;
251 break;
253 /* */
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);
257 break;
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 )
264 return VLC_EGENERIC;
265 *pb_bool = p_sys->asfh.stream[i_int].i_selected ? true : false;
266 break;
268 /* */
269 case ACCESS_SET_PAUSE_STATE:
270 b_bool = (bool)va_arg( args, int );
271 if( b_bool )
273 MMSStop( p_access );
274 KeepAliveStart( p_access );
276 else
278 KeepAliveStop( p_access );
279 Seek( p_access, p_access->info.i_pos );
281 break;
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:
288 return VLC_EGENERIC;
291 default:
292 msg_Warn( p_access, "unimplemented query in control" );
293 return VLC_EGENERIC;
296 return VLC_SUCCESS;
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;
305 uint32_t i_packet;
306 uint32_t i_offset;
307 var_buffer_t buffer;
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;
316 return VLC_SUCCESS;
318 else
320 i_packet = 0xffffffff;
321 i_offset = 0;
324 else
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 )
330 return VLC_EGENERIC;
332 msg_Dbg( p_access, "seeking to %"PRIu64 " (packet:%u)", i_pos, i_packet );
334 MMSStop( p_access );
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;
359 return VLC_EGENERIC;
362 if( p_sys->i_command == 0x1e )
364 msg_Dbg( p_access, "received 0x1e (seek)" );
365 break;
369 while( vlc_object_alive (p_access) )
371 if( mms_HeaderMediaRead( p_access, MMS_PACKET_CMD ) < 0 )
373 p_access->info.b_eof = true;
374 return VLC_EGENERIC;
376 if( p_sys->i_command == 0x05 )
378 msg_Dbg( p_access, "received 0x05 (seek)" );
379 break;
383 /* get a packet */
384 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
386 p_access->info.b_eof = true;
387 return VLC_EGENERIC;
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;
396 return VLC_SUCCESS;
399 /*****************************************************************************
400 * Block:
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 )
407 return NULL;
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 );
414 if( !p_block )
415 return NULL;
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;
419 return p_block;
421 else if( p_sys->p_media && p_sys->i_media_used < __MAX( p_sys->i_media, p_sys->i_packet_length ) )
423 size_t i_copy = 0;
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 );
432 if( !p_block )
433 return NULL;
435 if( i_copy > 0 )
436 memcpy( &p_block->p_buffer[0], &p_sys->p_media[p_sys->i_media_used], i_copy );
437 if( i_padding > 0 )
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;
442 return p_block;
445 mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA );
446 return NULL;
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;
457 var_buffer_t buffer;
458 char tmp[4096];
459 uint16_t *p;
460 int i_server_version;
461 int i_tool_version;
462 int i_update_player_url;
463 int i_encryption_type;
464 int i;
465 int i_streams;
466 int i_first;
467 char *mediapath;
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)" );
476 return VLC_EGENERIC;
478 msg_Dbg( p_access,
479 "connection(tcp) with \"%s:%d\" successful",
480 p_url->psz_host,
481 p_url->i_port );
483 /* *** Bind port if UDP protocol is selected *** */
484 if( b_udp )
486 if( net_GetSockAddress( p_sys->i_handle_tcp, p_sys->sz_bind_addr,
487 NULL ) )
489 net_Close( p_sys->i_handle_tcp );
490 return VLC_EGENERIC;
493 p_sys->i_handle_udp = net_ListenUDP1( (vlc_object_t *)p_access, p_sys->sz_bind_addr,
494 7000 );
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 );
499 return VLC_EGENERIC;
501 msg_Dbg( p_access,
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 */
508 msg_Dbg( p_access,
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;
518 p_sys->i_header = 0;
519 p_sys->p_media = NULL;
520 p_sys->i_media = 0;
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;
526 p_sys->p_cmd = NULL;
527 p_sys->i_cmd = 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 );
534 sprintf( tmp,
535 "NSPlayer/7.0.0.1956; {"GUID_FMT"}; Host: %s",
536 GUID_PRINT( p_sys->guid ),
537 p_url->psz_host );
538 var_buffer_addUTF16( &buffer, tmp );
540 mms_CommandSend( p_access,
541 0x01, /* connexion request */
542 0x00000000, /* flags, FIXME */
543 0x0004000b, /* ???? */
544 buffer.p_data,
545 buffer.i_data );
547 if( mms_CommandRead( p_access, 0x01, 0 ) < 0 )
549 var_buffer_free( &buffer );
550 MMSClose( p_access );
551 return VLC_EGENERIC;
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 ) \
561 int i; \
562 psz = xmalloc( size + 1); \
563 for( i = 0; i < size; i++ ) \
565 psz[i] = p[i]; \
567 psz[size] = '\0'; \
568 p += ( size ); \
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 );
574 #undef GETUTF16
575 msg_Dbg( p_access,
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 );
589 if( b_udp )
591 sprintf( tmp,
592 "\\\\%s\\UDP\\%d",
593 p_sys->sz_bind_addr,
594 7000 ); // FIXME
596 else
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, /* ???? */
607 buffer.p_data,
608 buffer.i_data );
610 /* *** response from server, should be 0x02 or 0x03 *** */
611 mms_CommandRead( p_access, 0x02, 0x03 );
612 if( p_sys->i_command == 0x03 )
614 msg_Err( p_access,
615 "%s protocol selection failed", b_udp ? "UDP" : "TCP" );
616 var_buffer_free( &buffer );
617 MMSClose( p_access );
618 return VLC_EGENERIC;
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 == '/' )
633 mediapath++;
635 var_buffer_addUTF16( &buffer, mediapath );
637 mms_CommandSend( p_access,
638 0x05,
639 p_sys->i_command_level,
640 0xffffffff,
641 buffer.p_data,
642 buffer.i_data );
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)" );
651 /* FIXME */
652 var_buffer_free( &buffer );
653 MMSClose( p_access );
654 return VLC_EGENERIC;
656 if( p_sys->i_command != 0x06 )
658 msg_Err( p_access,
659 "unknown answer (0x%x instead of 0x06)",
660 p_sys->i_command );
661 var_buffer_free( &buffer );
662 MMSClose( p_access );
663 return( -1 );
666 /* 1 for file ok, 2 for authen ok */
667 switch( GetDWLE( p_sys->p_cmd + MMS_CMD_HEADERSIZE ) )
669 case 0x0001:
670 msg_Dbg( p_access, "media file name/path accepted" );
671 break;
672 case 0x0002:
673 msg_Dbg( p_access, "authentication accepted" );
674 break;
675 case -1:
676 default:
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 );
681 return VLC_EGENERIC;
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 );
697 msg_Dbg( p_access,
698 "answer 0x06 flags:0x%8.8"PRIx32" media_length:%"PRIu32"s "
699 "packet_length:%zu packet_count:%"PRIu32" max_bit_rate:%d "
700 "header_size:%zu",
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) */
725 #if 0
726 mms_CommandRead( p_access, 0x11, 0 );
728 if( p_sys->i_command != 0x11 )
730 msg_Err( p_access,
731 "unknown answer (0x%x instead of 0x11)",
732 p_sys->i_command );
733 var_buffer_free( &buffer );
734 MMSClose( p_access );
735 return( -1 );
737 #endif
739 /* *** now read header packet *** */
740 /* XXX could be split over multiples packets */
741 msg_Dbg( p_access, "reading header" );
742 for( ;; )
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 );
749 return VLC_EGENERIC;
751 if( p_sys->i_header >= p_sys->i_header_size )
753 msg_Dbg( p_access,
754 "header complete(%zu)",
755 p_sys->i_header );
756 break;
758 msg_Dbg( p_access,
759 "header incomplete (%zu/%zu), reading more",
760 p_sys->i_header,
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 */
779 i_streams = 0;
780 i_first = -1;
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 )
788 i_streams++;
789 if( i_first != -1 )
791 var_buffer_add16( &buffer, 0xffff );
792 var_buffer_add16( &buffer, i );
794 else
796 i_first = i;
798 if( p_sys->asfh.stream[i].i_selected )
800 var_buffer_add16( &buffer, 0x0000 );
801 msg_Info( p_access,
802 "selecting stream[0x%x] %s (%d Kib/s)",
804 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
805 "audio" : "video" ,
806 p_sys->asfh.stream[i].i_bitrate / 1024);
808 else
810 var_buffer_add16( &buffer, 0x0002 );
811 msg_Info( p_access,
812 "ignoring stream[0x%x] %s (%d Kib/s)",
814 ( p_sys->asfh.stream[i].i_cat == ASF_STREAM_AUDIO ) ?
815 "audio" : "video" ,
816 p_sys->asfh.stream[i].i_bitrate / 1024);
822 if( i_streams == 0 )
824 msg_Err( p_access, "cannot find any stream" );
825 var_buffer_free( &buffer );
826 MMSClose( p_access );
827 return VLC_EGENERIC;
829 mms_CommandSend( p_access, 0x33,
830 i_streams,
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 )
837 msg_Err( p_access,
838 "unknown answer (0x%x instead of 0x21)",
839 p_sys->i_command );
840 var_buffer_free( &buffer );
841 MMSClose( p_access );
842 return VLC_EGENERIC;
846 var_buffer_free( &buffer );
848 msg_Info( p_access, "connection successful" );
850 return VLC_SUCCESS;
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;
859 var_buffer_t buffer;
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 )
881 msg_Err( p_access,
882 "unknown answer (0x%x instead of 0x05)",
883 p_sys->i_command );
884 return -1;
886 else
888 /* get a packet */
889 if( mms_HeaderMediaRead( p_access, MMS_PACKET_MEDIA ) < 0 )
890 return -1;
891 msg_Dbg( p_access, "streaming started" );
892 return 0;
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,
905 0x09,
906 p_sys->i_command_level,
907 0x001fffff,
908 NULL, 0 );
909 return( 0 );
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,
923 0x0d,
924 p_sys->i_command_level,
925 0x00000001,
926 NULL, 0 );
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 )
954 var_buffer_t buffer;
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 );
987 /* send it */
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" );
997 return VLC_EGENERIC;
1000 var_buffer_free( &buffer );
1001 return VLC_SUCCESS;
1004 static int NetFillBuffer( access_t *p_access )
1006 #ifdef UNDER_CE
1007 return -1;
1009 #else
1010 access_sys_t *p_sys = p_access->p_sys;
1011 int i_ret;
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;
1018 int i_try = 0;
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;
1026 else
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 );
1034 return 0;
1036 else
1038 /* msg_Warn( p_access, "ask for tcp:%d udp:%d", i_tcp, i_udp ); */
1041 /* Find if some data is available */
1044 i_try++;
1046 /* Initialize file descriptor set */
1047 memset (ufd, 0, sizeof (ufd));
1048 nfd = 0;
1050 if( i_tcp > 0 )
1052 ufd[nfd].fd = p_sys->i_handle_tcp;
1053 ufd[nfd].events = POLLIN;
1054 nfd++;
1056 if( i_udp > 0 )
1058 ufd[nfd].fd = p_sys->i_handle_udp;
1059 ufd[nfd].events = POLLIN;
1060 nfd++;
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");
1069 return -1;
1072 if( i_try > 3 && (p_sys->i_buffer_tcp > 0 || p_sys->i_buffer_udp > 0) )
1074 return -1;
1077 if( !vlc_object_alive (p_access) )
1078 return -1;
1080 //msg_Dbg( p_access, "NetFillBuffer: trying again (select)" );
1082 } while( !(i_ret = poll( ufd, nfd, timeout)) ||
1083 (i_ret < 0 && errno == EINTR) );
1085 if( i_ret < 0 )
1087 msg_Err( p_access, "network poll error (%m)" );
1088 return -1;
1091 i_tcp_read = i_udp_read = 0;
1093 if( ( i_tcp > 0 ) && ufd[0].revents )
1095 i_tcp_read =
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 );
1108 #ifdef MMS_DEBUG
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 );
1115 else
1117 msg_Dbg( p_access, "filling buffer TCP:%d+%d",
1118 p_sys->i_buffer_tcp, i_tcp_read );
1120 #endif
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;
1126 #endif
1129 static int mms_ParseCommand( access_t *p_access,
1130 uint8_t *p_data,
1131 size_t i_data,
1132 int *pi_used )
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;
1140 uint32_t i_length;
1141 uint32_t i_id;
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;
1154 return -1;
1156 i_id = GetDWLE( p_data + 4 );
1157 i_length = GetDWLE( p_data + 8 ) + 16;
1159 if( i_id != 0xb00bface || i_length < 16 )
1161 msg_Err( p_access,
1162 "incorrect command header (0x%"PRIx32")", i_id );
1163 p_sys->i_command = 0;
1164 return -1;
1167 if( i_length > p_sys->i_cmd )
1169 msg_Warn( p_access,
1170 "truncated command (missing %zu bytes)",
1171 (size_t)i_length - i_data );
1172 p_sys->i_command = 0;
1173 return -1;
1175 else if( i_length < p_sys->i_cmd )
1177 p_sys->i_cmd = i_length;
1178 *pi_used = i_length;
1181 msg_Dbg( p_access,
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",
1183 GET32( 0 ),
1184 GET32( 4 ),
1185 GET32( 8 ),
1186 /* 12: protocol type "MMS " */
1187 GET32( 16 ),
1188 GET32( 20 ),
1189 /* 24: unknown (0) */
1190 /* 28: unknown (0) */
1191 GET32( 32 ),
1192 GET32( 36 )
1193 /* 40: switches */
1194 /* 44: extra */ );
1196 p_sys->i_command = GET32( 36 ) & 0xffff;
1197 #undef GET32
1199 return MMS_PACKET_CMD;
1202 static int mms_ParsePacket( access_t *p_access,
1203 uint8_t *p_data, size_t i_data,
1204 int *pi_used )
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 */
1212 if( i_data <= 8 )
1214 msg_Warn( p_access, "truncated packet (header incomplete)" );
1215 return -1;
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 ); */
1229 *pi_used = 0;
1230 return -1;
1232 else if( i_packet_length < i_data )
1234 *pi_used = i_packet_length;
1237 if( i_packet_id == 0xff )
1239 msg_Warn( p_access,
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 );
1248 return -1;
1251 /* we now have a media or a header packet */
1252 if( i_packet_seq_num != p_sys->i_packet_seq_num )
1254 #if 0
1255 /* FIXME for udp could be just wrong order ? */
1256 msg_Warn( p_access,
1257 "detected packet lost (%d != %d)",
1258 i_packet_seq_num,
1259 p_sys->i_packet_seq_num );
1260 #endif
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;
1275 else
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;
1288 else
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;
1310 for( ;; )
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 )
1322 b_refill = false;
1325 else if( GetWLE( p_sys->buffer_tcp + 6 ) <= p_sys->i_buffer_tcp )
1327 b_refill = false;
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 )
1333 b_refill = false;
1336 if( b_refill && NetFillBuffer( p_access ) < 0 )
1338 msg_Warn( p_access, "cannot fill buffer" );
1339 return -1;
1342 i_packet_tcp_type = -1;
1343 i_packet_udp_type = -1;
1345 if( p_sys->i_buffer_tcp > 0 )
1347 int i_used;
1349 if( GetDWLE( p_sys->buffer_tcp + 4 ) == 0xb00bface )
1351 i_packet_tcp_type =
1352 mms_ParseCommand( p_access, p_sys->buffer_tcp,
1353 p_sys->i_buffer_tcp, &i_used );
1356 else
1358 i_packet_tcp_type =
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 )
1371 int i_used;
1373 i_packet_udp_type =
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;
1406 for( ;; )
1408 int i_used;
1409 int i_status;
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;
1427 if( i_status < 0 )
1429 return VLC_EGENERIC;
1432 if( p_sys->i_command == 0x1b )
1434 mms_CommandSend( p_access, 0x1b, 0, 0, NULL, 0 );
1436 else
1438 break;
1441 else
1443 return VLC_EGENERIC;
1447 return VLC_SUCCESS;
1450 #define MMS_RETRY_MAX 10
1451 #define MMS_RETRY_SLEEP 50000
1453 static int mms_CommandRead( access_t *p_access, int i_command1,
1454 int i_command2 )
1456 access_sys_t *p_sys = p_access->p_sys;
1457 int i_count;
1458 int i_status;
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 )
1465 i_count++;
1466 msleep( MMS_RETRY_SLEEP );
1468 else if( i_command1 == 0 && i_command2 == 0)
1470 return VLC_SUCCESS;
1472 else if( p_sys->i_command == i_command1 ||
1473 p_sys->i_command == i_command2 )
1475 return VLC_SUCCESS;
1477 else
1479 switch( p_sys->i_command )
1481 case 0x03:
1482 msg_Warn( p_access, "socket closed by server" );
1483 p_access->info.b_eof = true;
1484 return VLC_EGENERIC;
1485 case 0x1e:
1486 msg_Warn( p_access, "end of media stream" );
1487 p_access->info.b_eof = true;
1488 return VLC_EGENERIC;
1489 default:
1490 break;
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;
1504 int i_count;
1506 for( i_count = 0; i_count < MMS_RETRY_MAX; )
1508 int i_status;
1510 if( !vlc_object_alive (p_access) )
1511 return -1;
1513 i_status = mms_ReceivePacket( p_access );
1514 if( i_status < 0 )
1516 i_count++;
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 )
1523 return i_type;
1525 else if( i_status == MMS_PACKET_CMD )
1527 switch( p_sys->i_command )
1529 case 0x03:
1530 msg_Warn( p_access, "socket closed by server" );
1531 p_access->info.b_eof = true;
1532 return -1;
1533 case 0x1e:
1534 msg_Warn( p_access, "end of media stream" );
1535 p_access->info.b_eof = true;
1536 return -1;
1537 case 0x20:
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 */
1541 msg_Err( p_access,
1542 "reinitialization needed --> unsupported" );
1543 p_access->info.b_eof = true;
1544 return -1;
1545 default:
1546 break;
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;
1554 return -1;
1557 static void *KeepAliveThread( void *p_data )
1559 access_t *p_access = p_data;
1561 for( ;; )
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 );
1572 assert(0);
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 )
1579 return;
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 )
1590 return;
1592 vlc_cancel( p_sys->keep_alive );
1593 vlc_join( p_sys->keep_alive, NULL );
1594 p_sys->b_keep_alive = false;