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.109 2001/05/25 13:20:10 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 /* Network functions */
44 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
45 #include <netdb.h> /* hostent ... */
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
54 # include <sys/times.h>
65 #include "intf_playlist.h"
67 #include "stream_control.h"
68 #include "input_ext-intf.h"
69 #include "input_ext-dec.h"
72 #include "interface.h"
77 /*****************************************************************************
79 *****************************************************************************/
80 static void RunThread ( input_thread_t *p_input );
81 static int InitThread ( input_thread_t *p_input );
82 static void ErrorThread ( input_thread_t *p_input );
83 static void DestroyThread ( input_thread_t *p_input );
84 static void EndThread ( input_thread_t *p_input );
86 /*****************************************************************************
87 * input_CreateThread: creates a new input thread
88 *****************************************************************************
89 * This function creates a new input, and returns a pointer
90 * to its description. On error, it returns NULL.
91 * If pi_status is NULL, then the function will block until the thread is ready.
92 * If not, it will be updated using one of the THREAD_* constants.
93 *****************************************************************************/
94 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
96 input_thread_t * p_input; /* thread descriptor */
97 int i_status; /* thread status */
99 /* Allocate descriptor */
100 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
101 if( p_input == NULL )
103 intf_ErrMsg( "input error: can't allocate input thread (%s)",
108 /* Packets read once */
109 p_input->i_read_once = INPUT_READ_ONCE;
111 /* Initialize thread properties */
113 p_input->b_error = 0;
117 p_input->p_source = p_item->psz_name;
119 /* I have never understood that stuff --Meuuh */
120 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
121 *p_input->pi_status = THREAD_CREATE;
123 /* Initialize stream description */
124 p_input->stream.i_es_number = 0;
125 p_input->stream.i_selected_es_number = 0;
126 p_input->stream.i_pgrm_number = 0;
127 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
128 p_input->stream.i_mux_rate = 0;
130 /* no stream, no area */
131 p_input->stream.i_area_nb = 0;
132 p_input->stream.pp_areas = NULL;
133 p_input->stream.p_selected_area = NULL;
134 p_input->stream.p_new_area = NULL;
135 /* By default there is one areas in a stream */
136 input_AddArea( p_input );
137 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
139 /* Initialize stream control properties. */
140 p_input->stream.control.i_status = PLAYING_S;
141 p_input->stream.control.i_rate = DEFAULT_RATE;
142 p_input->stream.control.b_mute = 0;
143 p_input->stream.control.b_bw = 0;
145 /* Create thread and set locks. */
146 vlc_mutex_init( &p_input->stream.stream_lock );
147 vlc_cond_init( &p_input->stream.stream_wait );
148 vlc_mutex_init( &p_input->stream.control.control_lock );
149 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
152 intf_ErrMsg( "input error: can't create input thread (%s)",
158 /* If status is NULL, wait until the thread is created */
159 if( pi_status == NULL )
163 msleep( THREAD_SLEEP );
164 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
165 && (i_status != THREAD_FATAL) );
166 if( i_status != THREAD_READY )
174 /*****************************************************************************
175 * input_DestroyThread: mark an input thread as zombie
176 *****************************************************************************
177 * This function should not return until the thread is effectively cancelled.
178 *****************************************************************************/
179 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
181 int i_status; /* thread status */
184 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
185 *p_input->pi_status = THREAD_DESTROY;
187 /* Request thread destruction */
190 /* Make the thread exit of an eventual vlc_cond_wait() */
191 vlc_mutex_lock( &p_input->stream.stream_lock );
192 vlc_cond_signal( &p_input->stream.stream_wait );
193 vlc_mutex_unlock( &p_input->stream.stream_lock );
195 /* If status is NULL, wait until thread has been destroyed */
196 if( pi_status == NULL )
200 msleep( THREAD_SLEEP );
201 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
202 && (i_status != THREAD_FATAL) );
206 /*****************************************************************************
207 * RunThread: main thread loop
208 *****************************************************************************
209 * Thread in charge of processing the network packets and demultiplexing.
210 *****************************************************************************/
211 static void RunThread( input_thread_t *p_input )
215 if( InitThread( p_input ) )
218 /* If we failed, wait before we are killed, and exit */
219 *p_input->pi_status = THREAD_ERROR;
220 p_input->b_error = 1;
221 ErrorThread( p_input );
222 DestroyThread( p_input );
226 /* initialization is completed */
227 vlc_mutex_lock( &p_input->stream.stream_lock );
228 p_input->stream.b_changed = 1;
229 vlc_mutex_unlock( &p_input->stream.stream_lock );
231 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
233 data_packet_t * pp_packets[p_input->i_read_once];
239 vlc_mutex_lock( &p_input->stream.stream_lock );
241 if( p_input->stream.p_new_area )
243 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
244 p_input->stream.p_new_area = NULL;
247 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
249 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
251 p_input->pf_seek( p_input,
252 p_input->stream.p_selected_area->i_seek );
254 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
256 pgrm_descriptor_t * p_pgrm
257 = p_input->stream.pp_programs[i];
258 /* Escape all decoders for the stream discontinuity they
260 input_EscapeDiscontinuity( p_input, p_pgrm );
262 /* Reinitialize synchro. */
263 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
266 p_input->stream.p_selected_area->i_seek = NO_SEEK;
269 if( p_input->stream.p_removed_es )
271 input_UnselectES( p_input, p_input->stream.p_removed_es );
272 p_input->stream.p_removed_es = NULL;
275 if( p_input->stream.p_newly_selected_es )
277 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
278 p_input->stream.p_newly_selected_es = NULL;
281 vlc_mutex_unlock( &p_input->stream.stream_lock );
283 i_error = p_input->pf_read( p_input, pp_packets );
285 /* Demultiplex read packets. */
286 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
288 p_input->pf_demux( p_input, pp_packets[i] );
295 /* End of file - we do not set b_die because only the
296 * interface is allowed to do so. */
297 intf_WarnMsg( 3, "input: EOF reached" );
302 p_input->b_error = 1;
307 if( p_input->b_error || p_input->b_eof )
309 ErrorThread( p_input );
312 EndThread( p_input );
314 DestroyThread( p_input );
316 intf_DbgMsg("input: Thread end");
319 /*****************************************************************************
320 * InitThread: init the input Thread
321 *****************************************************************************/
322 static int InitThread( input_thread_t * p_input )
326 /* Initialize statistics */
327 p_input->c_loops = 0;
328 p_input->c_bytes = 0;
329 p_input->c_payload_bytes = 0;
330 p_input->c_packets_read = 0;
331 p_input->c_packets_trashed = 0;
334 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
335 (probedata_t *)p_input );
337 if( p_input->p_input_module == NULL )
339 intf_ErrMsg( "input error: no suitable input module for `%s'",
344 #define f p_input->p_input_module->p_functions->input.functions.input
345 p_input->pf_init = f.pf_init;
346 p_input->pf_open = f.pf_open;
347 p_input->pf_close = f.pf_close;
348 p_input->pf_end = f.pf_end;
349 p_input->pf_read = f.pf_read;
350 p_input->pf_set_area = f.pf_set_area;
351 p_input->pf_demux = f.pf_demux;
352 p_input->pf_new_packet = f.pf_new_packet;
353 p_input->pf_new_pes = f.pf_new_pes;
354 p_input->pf_delete_packet = f.pf_delete_packet;
355 p_input->pf_delete_pes = f.pf_delete_pes;
356 p_input->pf_rewind = f.pf_rewind;
357 p_input->pf_seek = f.pf_seek;
359 p_input->pf_open( p_input );
361 if( p_input->b_error )
363 /* We barfed -- exit nicely */
364 p_input->pf_close( p_input );
365 module_Unneed( p_input->p_input_module );
369 p_input->pf_init( p_input );
371 if( p_input->b_error )
373 /* We barfed -- exit nicely */
374 p_input->pf_close( p_input );
375 module_Unneed( p_input->p_input_module );
379 *p_input->pi_status = THREAD_READY;
384 /*****************************************************************************
385 * ErrorThread: RunThread() error loop
386 *****************************************************************************
387 * This function is called when an error occured during thread main's loop.
388 *****************************************************************************/
389 static void ErrorThread( input_thread_t *p_input )
391 while( !p_input->b_die )
394 msleep( INPUT_IDLE_SLEEP );
398 /*****************************************************************************
399 * EndThread: end the input thread
400 *****************************************************************************/
401 static void EndThread( input_thread_t * p_input )
403 int * pi_status; /* thread status */
406 pi_status = p_input->pi_status;
407 *pi_status = THREAD_END;
411 struct tms cpu_usage;
414 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
415 cpu_usage.tms_utime, cpu_usage.tms_stime);
419 /* Free all ES and destroy all decoder threads */
420 input_EndStream( p_input );
422 /* Free demultiplexer's data */
423 p_input->pf_end( p_input );
426 p_input->pf_close( p_input );
428 /* Release modules */
429 module_Unneed( p_input->p_input_module );
433 /*****************************************************************************
434 * DestroyThread: destroy the input thread
435 *****************************************************************************/
436 static void DestroyThread( input_thread_t * p_input )
438 int * pi_status; /* thread status */
441 pi_status = p_input->pi_status;
443 /* Destroy Mutex locks */
444 vlc_mutex_destroy( &p_input->stream.control.control_lock );
445 vlc_mutex_destroy( &p_input->stream.stream_lock );
447 /* Free input structure */
451 *pi_status = THREAD_OVER;
454 /*****************************************************************************
455 * input_FileOpen : open a file descriptor
456 *****************************************************************************/
457 void input_FileOpen( input_thread_t * p_input )
459 struct stat stat_info;
462 char *psz_name = p_input->p_source;
464 /* FIXME: this code ought to be in the plugin so that code can
465 * be shared with the *_Probe function */
466 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
468 int i_size = strlen( psz_name );
471 && !strncasecmp( psz_name, "dvd:", 4 ) )
473 /* get rid of the 'dvd:' stuff and try again */
475 i_stat = stat( psz_name, &stat_info );
477 else if( ( i_size > 5 )
478 && !strncasecmp( psz_name, "file:", 5 ) )
480 /* get rid of the 'file:' stuff and try again */
482 i_stat = stat( psz_name, &stat_info );
487 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
488 psz_name, strerror(errno));
489 p_input->b_error = 1;
494 vlc_mutex_lock( &p_input->stream.stream_lock );
496 /* If we are here we can control the pace... */
497 p_input->stream.b_pace_control = 1;
499 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
500 || S_ISBLK(stat_info.st_mode) )
502 p_input->stream.b_seekable = 1;
503 p_input->stream.p_selected_area->i_size = stat_info.st_size;
505 else if( S_ISFIFO(stat_info.st_mode)
506 #if !defined( SYS_BEOS ) && !defined( WIN32 )
507 || S_ISSOCK(stat_info.st_mode)
511 p_input->stream.b_seekable = 0;
512 p_input->stream.p_selected_area->i_size = 0;
516 vlc_mutex_unlock( &p_input->stream.stream_lock );
517 intf_ErrMsg( "input error: unknown file type for `%s'",
519 p_input->b_error = 1;
523 p_input->stream.p_selected_area->i_tell = 0;
524 vlc_mutex_unlock( &p_input->stream.stream_lock );
526 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
528 if( (p_input->i_handle = open( psz_name,
529 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
531 if( (p_input->i_handle = open( psz_name, O_BINARY
532 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
535 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
536 p_input->b_error = 1;
542 /*****************************************************************************
543 * input_FileClose : close a file descriptor
544 *****************************************************************************/
545 void input_FileClose( input_thread_t * p_input )
547 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
548 close( p_input->i_handle );
554 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
555 /*****************************************************************************
556 * input_NetworkOpen : open a network socket
557 *****************************************************************************/
558 void input_NetworkOpen( input_thread_t * p_input )
560 char *psz_server = NULL;
561 char *psz_broadcast = NULL;
564 struct sockaddr_in sock;
566 /* Get the remote server */
567 if( p_input->p_source != NULL )
569 psz_server = p_input->p_source;
571 /* Skip the protocol name */
572 while( *psz_server && *psz_server != ':' )
577 /* Skip the "://" part */
578 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
583 /* Found a server name */
586 char *psz_port = psz_server;
588 /* Skip the hostname part */
589 while( *psz_port && *psz_port != ':' )
594 /* Found a port name */
597 /* Replace ':' with '\0' */
601 psz_broadcast = psz_port;
602 while( *psz_broadcast && *psz_broadcast != ':' )
609 *psz_broadcast = '\0';
611 while( *psz_broadcast && *psz_broadcast == ':' )
618 psz_broadcast = NULL;
621 /* port before broadcast address */
622 if( *psz_port != ':' )
624 i_port = atoi( psz_port );
634 /* Check that we got a valid server */
635 if( psz_server == NULL )
637 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
638 INPUT_SERVER_DEFAULT );
641 /* Check that we got a valid port */
644 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
647 if( psz_broadcast == NULL )
649 /* Are we broadcasting ? */
650 psz_broadcast = main_GetPszVariable( INPUT_BROADCAST_VAR, NULL );
653 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
654 psz_server, i_port, psz_broadcast );
656 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
658 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
659 if( p_input->i_handle == -1 )
661 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
662 p_input->b_error = 1;
666 /* We may want to reuse an already used socket */
668 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
669 &i_opt, sizeof( i_opt ) ) == -1 )
671 intf_ErrMsg("input error: can't configure socket (SO_REUSEADDR: %s)",
673 close( p_input->i_handle );
674 p_input->b_error = 1;
678 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
679 * packet loss caused by scheduling problems */
681 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
682 &i_opt, sizeof( i_opt ) ) == -1 )
684 intf_ErrMsg("input error: can't configure socket (SO_RCVBUF: %s)",
686 close( p_input->i_handle );
687 p_input->b_error = 1;
691 /* Build the local socket */
692 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast )
695 close( p_input->i_handle );
696 p_input->b_error = 1;
701 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
702 sizeof( sock ) ) < 0 )
704 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
705 close( p_input->i_handle );
706 p_input->b_error = 1;
710 /* Build socket for remote connection */
711 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
713 close( p_input->i_handle );
714 p_input->b_error = 1;
718 /* And connect it ... should we really connect ? */
719 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
720 sizeof( sock ) ) == (-1) )
722 intf_ErrMsg( "NetworkOpen: can't connect socket : %s",
724 close( p_input->i_handle );
725 p_input->b_error = 1;
729 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
730 * with the server. */
731 p_input->stream.b_pace_control = 0;
732 p_input->stream.b_seekable = 0;
737 /*****************************************************************************
738 * input_NetworkClose : close a network socket
739 *****************************************************************************/
740 void input_NetworkClose( input_thread_t * p_input )
742 close( p_input->i_handle );