1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: input.c,v 1.113 2001/05/30 17:03:12 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 /*****************************************************************************
28 *****************************************************************************/
32 #include <sys/types.h>
37 #ifdef STRNCASECMP_IN_STRINGS_H
42 /* WinSock Includes */
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>
61 # include <sys/times.h>
72 #include "intf_playlist.h"
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
79 #include "interface.h"
84 /*****************************************************************************
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)",
120 /* Packets read once */
121 p_input->i_read_once = INPUT_READ_ONCE;
123 /* Initialize thread properties */
125 p_input->b_error = 0;
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;
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,
172 intf_ErrMsg( "input error: can't create input thread (%s)",
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 )
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 */
204 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
205 *p_input->pi_status = THREAD_DESTROY;
207 /* Request thread destruction */
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 )
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 );
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];
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
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] );
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" );
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 )
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;
354 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
355 (probedata_t *)p_input );
357 if( p_input->p_input_module == NULL )
359 intf_ErrMsg( "input error: no suitable input module for `%s'",
364 #define f p_input->p_input_module->p_functions->input.functions.input
365 p_input->pf_init = f.pf_init;
366 p_input->pf_open = f.pf_open;
367 p_input->pf_close = f.pf_close;
368 p_input->pf_end = f.pf_end;
369 p_input->pf_read = f.pf_read;
370 p_input->pf_set_area = f.pf_set_area;
371 p_input->pf_demux = f.pf_demux;
372 p_input->pf_new_packet = f.pf_new_packet;
373 p_input->pf_new_pes = f.pf_new_pes;
374 p_input->pf_delete_packet = f.pf_delete_packet;
375 p_input->pf_delete_pes = f.pf_delete_pes;
376 p_input->pf_rewind = f.pf_rewind;
377 p_input->pf_seek = f.pf_seek;
379 p_input->pf_open( p_input );
381 if( p_input->b_error )
383 /* We barfed -- exit nicely */
384 p_input->pf_close( p_input );
385 module_Unneed( p_input->p_input_module );
389 p_input->pf_init( 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 );
399 *p_input->pi_status = THREAD_READY;
404 /*****************************************************************************
405 * ErrorThread: RunThread() error loop
406 *****************************************************************************
407 * This function is called when an error occured during thread main's loop.
408 *****************************************************************************/
409 static void ErrorThread( input_thread_t *p_input )
411 while( !p_input->b_die )
414 msleep( INPUT_IDLE_SLEEP );
418 /*****************************************************************************
419 * EndThread: end the input thread
420 *****************************************************************************/
421 static void EndThread( input_thread_t * p_input )
423 int * pi_status; /* thread status */
426 pi_status = p_input->pi_status;
427 *pi_status = THREAD_END;
431 struct tms cpu_usage;
434 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
435 cpu_usage.tms_utime, cpu_usage.tms_stime);
439 /* Free all ES and destroy all decoder threads */
440 input_EndStream( p_input );
442 /* Free demultiplexer's data */
443 p_input->pf_end( p_input );
446 p_input->pf_close( p_input );
448 /* Release modules */
449 module_Unneed( p_input->p_input_module );
453 /*****************************************************************************
454 * DestroyThread: destroy the input thread
455 *****************************************************************************/
456 static void DestroyThread( input_thread_t * p_input )
458 int * pi_status; /* thread status */
461 pi_status = p_input->pi_status;
463 /* Destroy Mutex locks */
464 vlc_mutex_destroy( &p_input->stream.control.control_lock );
465 vlc_mutex_destroy( &p_input->stream.stream_lock );
467 /* Free input structure */
471 *pi_status = THREAD_OVER;
474 /*****************************************************************************
475 * FileOpen : open a file descriptor
476 *****************************************************************************/
477 static void FileOpen( input_thread_t * p_input )
479 struct stat stat_info;
482 char *psz_name = p_input->p_source;
484 /* FIXME: this code ought to be in the plugin so that code can
485 * be shared with the *_Probe function */
486 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
488 int i_size = strlen( psz_name );
491 && !strncasecmp( psz_name, "dvd:", 4 ) )
493 /* get rid of the 'dvd:' stuff and try again */
495 i_stat = stat( psz_name, &stat_info );
497 else if( ( i_size > 5 )
498 && !strncasecmp( psz_name, "file:", 5 ) )
500 /* get rid of the 'file:' stuff and try again */
502 i_stat = stat( psz_name, &stat_info );
507 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
508 psz_name, strerror(errno));
509 p_input->b_error = 1;
514 vlc_mutex_lock( &p_input->stream.stream_lock );
516 /* If we are here we can control the pace... */
517 p_input->stream.b_pace_control = 1;
519 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
520 || S_ISBLK(stat_info.st_mode) )
522 p_input->stream.b_seekable = 1;
523 p_input->stream.p_selected_area->i_size = stat_info.st_size;
525 else if( S_ISFIFO(stat_info.st_mode)
526 #if !defined( SYS_BEOS ) && !defined( WIN32 )
527 || S_ISSOCK(stat_info.st_mode)
531 p_input->stream.b_seekable = 0;
532 p_input->stream.p_selected_area->i_size = 0;
536 vlc_mutex_unlock( &p_input->stream.stream_lock );
537 intf_ErrMsg( "input error: unknown file type for `%s'",
539 p_input->b_error = 1;
543 p_input->stream.p_selected_area->i_tell = 0;
544 vlc_mutex_unlock( &p_input->stream.stream_lock );
546 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
548 if( (p_input->i_handle = open( psz_name,
549 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
551 if( (p_input->i_handle = open( psz_name, O_BINARY
552 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
555 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
556 p_input->b_error = 1;
562 /*****************************************************************************
563 * FileClose : close a file descriptor
564 *****************************************************************************/
565 static void FileClose( input_thread_t * p_input )
567 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
568 close( p_input->i_handle );
574 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
575 /*****************************************************************************
576 * NetworkOpen : open a network socket
577 *****************************************************************************/
578 static void NetworkOpen( input_thread_t * p_input )
580 char *psz_server = NULL;
581 char *psz_broadcast = NULL;
584 struct sockaddr_in sock;
587 /* WinSock Library Init. */
589 int i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
593 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
598 /* Get the remote server */
599 if( p_input->p_source != NULL )
601 psz_server = p_input->p_source;
603 /* Skip the protocol name */
604 while( *psz_server && *psz_server != ':' )
609 /* Skip the "://" part */
610 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
615 /* Found a server name */
618 char *psz_port = psz_server;
620 /* Skip the hostname part */
621 while( *psz_port && *psz_port != ':' )
626 /* Found a port name */
629 /* Replace ':' with '\0' */
633 psz_broadcast = psz_port;
634 while( *psz_broadcast && *psz_broadcast != ':' )
641 *psz_broadcast = '\0';
643 while( *psz_broadcast && *psz_broadcast == ':' )
650 psz_broadcast = NULL;
653 /* port before broadcast address */
654 if( *psz_port != ':' )
656 i_port = atoi( psz_port );
666 /* Check that we got a valid server */
667 if( psz_server == NULL )
669 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
670 INPUT_SERVER_DEFAULT );
673 /* Check that we got a valid port */
676 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
679 if( psz_broadcast == NULL )
681 /* Are we broadcasting ? */
682 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
683 INPUT_BROADCAST_DEFAULT ) )
685 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
686 INPUT_BCAST_ADDR_DEFAULT );
690 psz_broadcast = NULL;
694 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
695 psz_server, i_port, psz_broadcast );
697 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
699 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
700 if( p_input->i_handle == -1 )
702 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
703 p_input->b_error = 1;
707 /* We may want to reuse an already used socket */
709 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
710 &i_opt, sizeof( i_opt ) ) == -1 )
712 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
714 close( p_input->i_handle );
715 p_input->b_error = 1;
719 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
720 * packet loss caused by scheduling problems */
722 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
723 &i_opt, sizeof( i_opt ) ) == -1 )
725 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
727 close( p_input->i_handle );
728 p_input->b_error = 1;
732 /* Build the local socket */
733 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
735 intf_ErrMsg( "input error: can't build local address" );
736 close( p_input->i_handle );
737 p_input->b_error = 1;
742 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
743 sizeof( sock ) ) < 0 )
745 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
746 close( p_input->i_handle );
747 p_input->b_error = 1;
751 /* Build socket for remote connection */
752 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
754 intf_ErrMsg( "input error: can't build remote address" );
755 close( p_input->i_handle );
756 p_input->b_error = 1;
760 /* And connect it ... should we really connect ? */
761 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
762 sizeof( sock ) ) == (-1) )
764 intf_ErrMsg( "input error: can't connect socket, %s",
766 close( p_input->i_handle );
767 p_input->b_error = 1;
771 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
772 * with the server. */
773 p_input->stream.b_pace_control = 0;
774 p_input->stream.b_seekable = 0;
776 intf_WarnMsg( 3, "input: successfully opened network mode" );
781 /*****************************************************************************
782 * NetworkClose : close a network socket
783 *****************************************************************************/
784 static void NetworkClose( input_thread_t * p_input )
786 close( p_input->i_handle );