* Fixed a bug introduced in my previous commit which broke DVD input.
[vlc.git] / src / input / input.c
blobaa46afc39814f30dd34747ecbdeea162373c6cd1
1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
4 * decoders.
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: input.c,v 1.114 2001/05/30 22:16:07 sam Exp $
9 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #include "defs.h"
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #ifdef STRNCASECMP_IN_STRINGS_H
38 # include <strings.h>
39 #endif
40 #include <errno.h>
42 /* WinSock Includes */
44 #ifdef WIN32
45 #include <winsock2.h>
46 #endif
49 /* Network functions */
51 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
52 #include <netdb.h> /* hostent ... */
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #endif
60 #ifdef STATS
61 # include <sys/times.h>
62 #endif
64 #include "config.h"
65 #include "common.h"
66 #include "threads.h"
67 #include "mtime.h"
68 #include "netutils.h"
69 #include "modules.h"
71 #include "intf_msg.h"
72 #include "intf_playlist.h"
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
78 #include "input.h"
79 #include "interface.h"
81 #include "main.h"
84 /*****************************************************************************
85 * Local prototypes
86 *****************************************************************************/
87 static void RunThread ( input_thread_t *p_input );
88 static int InitThread ( input_thread_t *p_input );
89 static void ErrorThread ( input_thread_t *p_input );
90 static void DestroyThread ( input_thread_t *p_input );
91 static void EndThread ( input_thread_t *p_input );
93 static void FileOpen ( input_thread_t *p_input );
94 static void FileClose ( input_thread_t *p_input );
95 static void NetworkOpen ( input_thread_t *p_input );
96 static void NetworkClose ( input_thread_t *p_input );
98 /*****************************************************************************
99 * input_CreateThread: creates a new input thread
100 *****************************************************************************
101 * This function creates a new input, and returns a pointer
102 * to its description. On error, it returns NULL.
103 * If pi_status is NULL, then the function will block until the thread is ready.
104 * If not, it will be updated using one of the THREAD_* constants.
105 *****************************************************************************/
106 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
108 input_thread_t * p_input; /* thread descriptor */
109 int i_status; /* thread status */
111 /* Allocate descriptor */
112 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
113 if( p_input == NULL )
115 intf_ErrMsg( "input error: can't allocate input thread (%s)",
116 strerror(errno) );
117 return( NULL );
120 /* Packets read once */
121 p_input->i_read_once = INPUT_READ_ONCE;
123 /* Initialize thread properties */
124 p_input->b_die = 0;
125 p_input->b_error = 0;
126 p_input->b_eof = 0;
128 /* Set target */
129 p_input->p_source = p_item->psz_name;
131 /* I have never understood that stuff --Meuuh */
132 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
133 *p_input->pi_status = THREAD_CREATE;
135 /* Initialize stream description */
136 p_input->stream.i_es_number = 0;
137 p_input->stream.i_selected_es_number = 0;
138 p_input->stream.i_pgrm_number = 0;
139 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
140 p_input->stream.i_mux_rate = 0;
142 /* no stream, no area */
143 p_input->stream.i_area_nb = 0;
144 p_input->stream.pp_areas = NULL;
145 p_input->stream.p_selected_area = NULL;
146 p_input->stream.p_new_area = NULL;
147 /* By default there is one areas in a stream */
148 input_AddArea( p_input );
149 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
151 /* Initialize stream control properties. */
152 p_input->stream.control.i_status = PLAYING_S;
153 p_input->stream.control.i_rate = DEFAULT_RATE;
154 p_input->stream.control.b_mute = 0;
155 p_input->stream.control.b_bw = 0;
157 /* Setup callbacks */
158 p_input->pf_file_open = FileOpen;
159 p_input->pf_file_close = FileClose;
160 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
161 p_input->pf_network_open = NetworkOpen;
162 p_input->pf_network_close = NetworkClose;
163 #endif
165 /* Create thread and set locks. */
166 vlc_mutex_init( &p_input->stream.stream_lock );
167 vlc_cond_init( &p_input->stream.stream_wait );
168 vlc_mutex_init( &p_input->stream.control.control_lock );
169 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
170 (void *) p_input ) )
172 intf_ErrMsg( "input error: can't create input thread (%s)",
173 strerror(errno) );
174 free( p_input );
175 return( NULL );
178 /* If status is NULL, wait until the thread is created */
179 if( pi_status == NULL )
183 msleep( THREAD_SLEEP );
184 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
185 && (i_status != THREAD_FATAL) );
186 if( i_status != THREAD_READY )
188 return( NULL );
191 return( p_input );
194 /*****************************************************************************
195 * input_DestroyThread: mark an input thread as zombie
196 *****************************************************************************
197 * This function should not return until the thread is effectively cancelled.
198 *****************************************************************************/
199 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
201 int i_status; /* thread status */
203 /* Set status */
204 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
205 *p_input->pi_status = THREAD_DESTROY;
207 /* Request thread destruction */
208 p_input->b_die = 1;
210 /* Make the thread exit of an eventual vlc_cond_wait() */
211 vlc_mutex_lock( &p_input->stream.stream_lock );
212 vlc_cond_signal( &p_input->stream.stream_wait );
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
215 /* If status is NULL, wait until thread has been destroyed */
216 if( pi_status == NULL )
220 msleep( THREAD_SLEEP );
221 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
222 && (i_status != THREAD_FATAL) );
226 /*****************************************************************************
227 * RunThread: main thread loop
228 *****************************************************************************
229 * Thread in charge of processing the network packets and demultiplexing.
230 *****************************************************************************/
231 static void RunThread( input_thread_t *p_input )
233 int i_error, i;
235 if( InitThread( p_input ) )
238 /* If we failed, wait before we are killed, and exit */
239 *p_input->pi_status = THREAD_ERROR;
240 p_input->b_error = 1;
241 ErrorThread( p_input );
242 DestroyThread( p_input );
243 return;
246 /* initialization is completed */
247 vlc_mutex_lock( &p_input->stream.stream_lock );
248 p_input->stream.b_changed = 1;
249 vlc_mutex_unlock( &p_input->stream.stream_lock );
251 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
253 data_packet_t * pp_packets[p_input->i_read_once];
255 #ifdef STATS
256 p_input->c_loops++;
257 #endif
259 vlc_mutex_lock( &p_input->stream.stream_lock );
261 if( p_input->stream.p_new_area )
263 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
264 p_input->stream.p_new_area = NULL;
267 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
269 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
271 p_input->pf_seek( p_input,
272 p_input->stream.p_selected_area->i_seek );
274 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
276 pgrm_descriptor_t * p_pgrm
277 = p_input->stream.pp_programs[i];
278 /* Escape all decoders for the stream discontinuity they
279 * will encounter. */
280 input_EscapeDiscontinuity( p_input, p_pgrm );
282 /* Reinitialize synchro. */
283 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
286 p_input->stream.p_selected_area->i_seek = NO_SEEK;
289 if( p_input->stream.p_removed_es )
291 input_UnselectES( p_input, p_input->stream.p_removed_es );
292 p_input->stream.p_removed_es = NULL;
295 if( p_input->stream.p_newly_selected_es )
297 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
298 p_input->stream.p_newly_selected_es = NULL;
301 vlc_mutex_unlock( &p_input->stream.stream_lock );
303 i_error = p_input->pf_read( p_input, pp_packets );
305 /* Demultiplex read packets. */
306 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
308 p_input->pf_demux( p_input, pp_packets[i] );
311 if( i_error )
313 if( i_error == 1 )
315 /* End of file - we do not set b_die because only the
316 * interface is allowed to do so. */
317 intf_WarnMsg( 3, "input: EOF reached" );
318 p_input->b_eof = 1;
320 else
322 p_input->b_error = 1;
327 if( p_input->b_error || p_input->b_eof )
329 ErrorThread( p_input );
332 EndThread( p_input );
334 DestroyThread( p_input );
336 intf_DbgMsg("input: Thread end");
339 /*****************************************************************************
340 * InitThread: init the input Thread
341 *****************************************************************************/
342 static int InitThread( input_thread_t * p_input )
345 #ifdef STATS
346 /* Initialize statistics */
347 p_input->c_loops = 0;
348 p_input->c_bytes = 0;
349 p_input->c_payload_bytes = 0;
350 p_input->c_packets_read = 0;
351 p_input->c_packets_trashed = 0;
352 #endif
354 /* Default, might get overwritten */
355 p_input->pf_open = p_input->pf_file_open;
356 p_input->pf_close = p_input->pf_file_close;
358 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
359 (probedata_t *)p_input );
361 if( p_input->p_input_module == NULL )
363 intf_ErrMsg( "input error: no suitable input module for `%s'",
364 p_input->p_source );
365 return( -1 );
368 #define f p_input->p_input_module->p_functions->input.functions.input
369 p_input->pf_init = f.pf_init;
370 if( f.pf_open != NULL )
372 p_input->pf_open = f.pf_open;
374 if( f.pf_close != NULL )
376 p_input->pf_close = f.pf_close;
378 p_input->pf_end = f.pf_end;
379 p_input->pf_read = f.pf_read;
380 p_input->pf_set_area = f.pf_set_area;
381 p_input->pf_demux = f.pf_demux;
382 p_input->pf_new_packet = f.pf_new_packet;
383 p_input->pf_new_pes = f.pf_new_pes;
384 p_input->pf_delete_packet = f.pf_delete_packet;
385 p_input->pf_delete_pes = f.pf_delete_pes;
386 p_input->pf_rewind = f.pf_rewind;
387 p_input->pf_seek = f.pf_seek;
388 #undef f
389 p_input->pf_open( p_input );
391 if( p_input->b_error )
393 /* We barfed -- exit nicely */
394 p_input->pf_close( p_input );
395 module_Unneed( p_input->p_input_module );
396 return( -1 );
399 p_input->pf_init( p_input );
401 if( p_input->b_error )
403 /* We barfed -- exit nicely */
404 p_input->pf_close( p_input );
405 module_Unneed( p_input->p_input_module );
406 return( -1 );
409 *p_input->pi_status = THREAD_READY;
411 return( 0 );
414 /*****************************************************************************
415 * ErrorThread: RunThread() error loop
416 *****************************************************************************
417 * This function is called when an error occured during thread main's loop.
418 *****************************************************************************/
419 static void ErrorThread( input_thread_t *p_input )
421 while( !p_input->b_die )
423 /* Sleep a while */
424 msleep( INPUT_IDLE_SLEEP );
428 /*****************************************************************************
429 * EndThread: end the input thread
430 *****************************************************************************/
431 static void EndThread( input_thread_t * p_input )
433 int * pi_status; /* thread status */
435 /* Store status */
436 pi_status = p_input->pi_status;
437 *pi_status = THREAD_END;
439 #ifdef STATS
441 struct tms cpu_usage;
442 times( &cpu_usage );
444 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
445 cpu_usage.tms_utime, cpu_usage.tms_stime);
447 #endif
449 /* Free all ES and destroy all decoder threads */
450 input_EndStream( p_input );
452 /* Free demultiplexer's data */
453 p_input->pf_end( p_input );
455 /* Close stream */
456 p_input->pf_close( p_input );
458 /* Release modules */
459 module_Unneed( p_input->p_input_module );
463 /*****************************************************************************
464 * DestroyThread: destroy the input thread
465 *****************************************************************************/
466 static void DestroyThread( input_thread_t * p_input )
468 int * pi_status; /* thread status */
470 /* Store status */
471 pi_status = p_input->pi_status;
473 /* Destroy Mutex locks */
474 vlc_mutex_destroy( &p_input->stream.control.control_lock );
475 vlc_mutex_destroy( &p_input->stream.stream_lock );
477 /* Free input structure */
478 free( p_input );
480 /* Update status */
481 *pi_status = THREAD_OVER;
484 /*****************************************************************************
485 * FileOpen : open a file descriptor
486 *****************************************************************************/
487 static void FileOpen( input_thread_t * p_input )
489 struct stat stat_info;
490 int i_stat;
492 char *psz_name = p_input->p_source;
494 /* FIXME: this code ought to be in the plugin so that code can
495 * be shared with the *_Probe function */
496 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
498 int i_size = strlen( psz_name );
500 if( ( i_size > 4 )
501 && !strncasecmp( psz_name, "dvd:", 4 ) )
503 /* get rid of the 'dvd:' stuff and try again */
504 psz_name += 4;
505 i_stat = stat( psz_name, &stat_info );
507 else if( ( i_size > 5 )
508 && !strncasecmp( psz_name, "file:", 5 ) )
510 /* get rid of the 'file:' stuff and try again */
511 psz_name += 5;
512 i_stat = stat( psz_name, &stat_info );
515 if( i_stat == (-1) )
517 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
518 psz_name, strerror(errno));
519 p_input->b_error = 1;
520 return;
524 vlc_mutex_lock( &p_input->stream.stream_lock );
526 /* If we are here we can control the pace... */
527 p_input->stream.b_pace_control = 1;
529 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
530 || S_ISBLK(stat_info.st_mode) )
532 p_input->stream.b_seekable = 1;
533 p_input->stream.p_selected_area->i_size = stat_info.st_size;
535 else if( S_ISFIFO(stat_info.st_mode)
536 #if !defined( SYS_BEOS ) && !defined( WIN32 )
537 || S_ISSOCK(stat_info.st_mode)
538 #endif
541 p_input->stream.b_seekable = 0;
542 p_input->stream.p_selected_area->i_size = 0;
544 else
546 vlc_mutex_unlock( &p_input->stream.stream_lock );
547 intf_ErrMsg( "input error: unknown file type for `%s'",
548 psz_name );
549 p_input->b_error = 1;
550 return;
553 p_input->stream.p_selected_area->i_tell = 0;
554 vlc_mutex_unlock( &p_input->stream.stream_lock );
556 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
557 #ifndef WIN32
558 if( (p_input->i_handle = open( psz_name,
559 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
560 #else
561 if( (p_input->i_handle = open( psz_name, O_BINARY
562 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
563 #endif
565 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
566 p_input->b_error = 1;
567 return;
572 /*****************************************************************************
573 * FileClose : close a file descriptor
574 *****************************************************************************/
575 static void FileClose( input_thread_t * p_input )
577 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
578 close( p_input->i_handle );
580 return;
584 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
585 /*****************************************************************************
586 * NetworkOpen : open a network socket
587 *****************************************************************************/
588 static void NetworkOpen( input_thread_t * p_input )
590 char *psz_server = NULL;
591 char *psz_broadcast = NULL;
592 int i_port = 0;
593 int i_opt;
594 struct sockaddr_in sock;
596 #ifdef WIN32
597 /* WinSock Library Init. */
598 WSADATA Data;
599 int i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
601 if( i_err )
603 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
604 return ;
606 #endif
608 /* Get the remote server */
609 if( p_input->p_source != NULL )
611 psz_server = p_input->p_source;
613 /* Skip the protocol name */
614 while( *psz_server && *psz_server != ':' )
616 psz_server++;
619 /* Skip the "://" part */
620 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
622 psz_server++;
625 /* Found a server name */
626 if( *psz_server )
628 char *psz_port = psz_server;
630 /* Skip the hostname part */
631 while( *psz_port && *psz_port != ':' )
633 psz_port++;
636 /* Found a port name */
637 if( *psz_port )
639 /* Replace ':' with '\0' */
640 *psz_port = '\0';
641 psz_port++;
643 psz_broadcast = psz_port;
644 while( *psz_broadcast && *psz_broadcast != ':' )
646 psz_broadcast++;
649 if( *psz_broadcast )
651 *psz_broadcast = '\0';
652 psz_broadcast++;
653 while( *psz_broadcast && *psz_broadcast == ':' )
655 psz_broadcast++;
658 else
660 psz_broadcast = NULL;
663 /* port before broadcast address */
664 if( *psz_port != ':' )
666 i_port = atoi( psz_port );
670 else
672 psz_server = NULL;
676 /* Check that we got a valid server */
677 if( psz_server == NULL )
679 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
680 INPUT_SERVER_DEFAULT );
683 /* Check that we got a valid port */
684 if( i_port == 0 )
686 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
689 if( psz_broadcast == NULL )
691 /* Are we broadcasting ? */
692 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
693 INPUT_BROADCAST_DEFAULT ) )
695 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
696 INPUT_BCAST_ADDR_DEFAULT );
698 else
700 psz_broadcast = NULL;
704 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
705 psz_server, i_port, psz_broadcast );
707 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
708 * protocol */
709 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
710 if( p_input->i_handle == -1 )
712 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
713 p_input->b_error = 1;
714 return;
717 /* We may want to reuse an already used socket */
718 i_opt = 1;
719 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
720 &i_opt, sizeof( i_opt ) ) == -1 )
722 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
723 strerror(errno));
724 close( p_input->i_handle );
725 p_input->b_error = 1;
726 return;
729 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
730 * packet loss caused by scheduling problems */
731 i_opt = 0x80000;
732 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
733 &i_opt, sizeof( i_opt ) ) == -1 )
735 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
736 strerror(errno));
737 close( p_input->i_handle );
738 p_input->b_error = 1;
739 return;
742 /* Build the local socket */
743 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
745 intf_ErrMsg( "input error: can't build local address" );
746 close( p_input->i_handle );
747 p_input->b_error = 1;
748 return;
751 /* Bind it */
752 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
753 sizeof( sock ) ) < 0 )
755 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
756 close( p_input->i_handle );
757 p_input->b_error = 1;
758 return;
761 /* Build socket for remote connection */
762 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
764 intf_ErrMsg( "input error: can't build remote address" );
765 close( p_input->i_handle );
766 p_input->b_error = 1;
767 return;
770 /* And connect it ... should we really connect ? */
771 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
772 sizeof( sock ) ) == (-1) )
774 intf_ErrMsg( "input error: can't connect socket, %s",
775 strerror(errno) );
776 close( p_input->i_handle );
777 p_input->b_error = 1;
778 return;
781 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
782 * with the server. */
783 p_input->stream.b_pace_control = 0;
784 p_input->stream.b_seekable = 0;
786 intf_WarnMsg( 3, "input: successfully opened network mode" );
788 return;
791 /*****************************************************************************
792 * NetworkClose : close a network socket
793 *****************************************************************************/
794 static void NetworkClose( input_thread_t * p_input )
796 close( p_input->i_handle );
798 #ifdef WIN32
799 WSACleanup();
800 #endif
803 #endif