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.107 2001/05/07 03:14:09 stef 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"
76 /* #include <netutils.h> */
79 /*****************************************************************************
81 *****************************************************************************/
82 static void RunThread ( input_thread_t *p_input );
83 static int InitThread ( input_thread_t *p_input );
84 static void ErrorThread ( input_thread_t *p_input );
85 static void DestroyThread ( input_thread_t *p_input );
86 static void EndThread ( input_thread_t *p_input );
88 /*****************************************************************************
89 * input_CreateThread: creates a new input thread
90 *****************************************************************************
91 * This function creates a new input, and returns a pointer
92 * to its description. On error, it returns NULL.
93 * If pi_status is NULL, then the function will block until the thread is ready.
94 * If not, it will be updated using one of the THREAD_* constants.
95 *****************************************************************************/
96 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
98 input_thread_t * p_input; /* thread descriptor */
99 int i_status; /* thread status */
101 /* Allocate descriptor */
102 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
103 if( p_input == NULL )
105 intf_ErrMsg( "input error: can't allocate input thread (%s)",
110 /* Packets read once */
111 p_input->i_read_once = INPUT_READ_ONCE;
113 /* Initialize thread properties */
115 p_input->b_error = 0;
119 p_input->p_source = p_item->psz_name;
121 /* I have never understood that stuff --Meuuh */
122 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
123 *p_input->pi_status = THREAD_CREATE;
125 /* Initialize stream description */
126 p_input->stream.i_es_number = 0;
127 p_input->stream.i_selected_es_number = 0;
128 p_input->stream.i_pgrm_number = 0;
129 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
130 p_input->stream.i_mux_rate = 0;
132 /* no stream, no area */
133 p_input->stream.i_area_nb = 0;
134 p_input->stream.pp_areas = NULL;
135 p_input->stream.p_selected_area = NULL;
136 /* By default there is one areas in a stream */
137 input_AddArea( p_input );
138 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
140 /* Initialize stream control properties. */
141 p_input->stream.control.i_status = PLAYING_S;
142 p_input->stream.control.i_rate = DEFAULT_RATE;
143 p_input->stream.control.b_mute = 0;
144 p_input->stream.control.b_bw = 0;
146 /* Create thread and set locks. */
147 vlc_mutex_init( &p_input->stream.stream_lock );
148 vlc_cond_init( &p_input->stream.stream_wait );
149 vlc_mutex_init( &p_input->stream.control.control_lock );
150 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
153 intf_ErrMsg( "input error: can't create input thread (%s)",
159 /* If status is NULL, wait until the thread is created */
160 if( pi_status == NULL )
164 msleep( THREAD_SLEEP );
165 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
166 && (i_status != THREAD_FATAL) );
167 if( i_status != THREAD_READY )
175 /*****************************************************************************
176 * input_DestroyThread: mark an input thread as zombie
177 *****************************************************************************
178 * This function should not return until the thread is effectively cancelled.
179 *****************************************************************************/
180 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
182 int i_status; /* thread status */
185 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
186 *p_input->pi_status = THREAD_DESTROY;
188 /* Request thread destruction */
191 /* Make the thread exit of an eventual vlc_cond_wait() */
192 vlc_mutex_lock( &p_input->stream.stream_lock );
193 vlc_cond_signal( &p_input->stream.stream_wait );
194 vlc_mutex_unlock( &p_input->stream.stream_lock );
196 /* If status is NULL, wait until thread has been destroyed */
197 if( pi_status == NULL )
201 msleep( THREAD_SLEEP );
202 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
203 && (i_status != THREAD_FATAL) );
207 /*****************************************************************************
208 * RunThread: main thread loop
209 *****************************************************************************
210 * Thread in charge of processing the network packets and demultiplexing.
211 *****************************************************************************/
212 static void RunThread( input_thread_t *p_input )
216 if( InitThread( p_input ) )
219 /* If we failed, wait before we are killed, and exit */
220 *p_input->pi_status = THREAD_ERROR;
221 p_input->b_error = 1;
222 ErrorThread( p_input );
223 DestroyThread( p_input );
227 /* initialization is completed */
228 vlc_mutex_lock( &p_input->stream.stream_lock );
229 p_input->stream.b_changed = 1;
230 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
234 data_packet_t * pp_packets[p_input->i_read_once];
240 vlc_mutex_lock( &p_input->stream.stream_lock );
242 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
244 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
246 p_input->pf_seek( p_input,
247 p_input->stream.p_selected_area->i_seek );
249 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
251 pgrm_descriptor_t * p_pgrm
252 = p_input->stream.pp_programs[i];
253 /* Escape all decoders for the stream discontinuity they
255 input_EscapeDiscontinuity( p_input, p_pgrm );
257 /* Reinitialize synchro. */
258 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
261 p_input->stream.p_selected_area->i_seek = NO_SEEK;
264 vlc_mutex_unlock( &p_input->stream.stream_lock );
266 i_error = p_input->pf_read( p_input, pp_packets );
268 /* Demultiplex read packets. */
269 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
271 p_input->pf_demux( p_input, pp_packets[i] );
278 /* End of file - we do not set b_die because only the
279 * interface is allowed to do so. */
280 intf_WarnMsg( 3, "input: EOF reached" );
285 p_input->b_error = 1;
290 if( p_input->b_error || p_input->b_eof )
292 ErrorThread( p_input );
295 EndThread( p_input );
297 DestroyThread( p_input );
299 intf_DbgMsg("input: Thread end");
302 /*****************************************************************************
303 * InitThread: init the input Thread
304 *****************************************************************************/
305 static int InitThread( input_thread_t * p_input )
309 /* Initialize statistics */
310 p_input->c_loops = 0;
311 p_input->c_bytes = 0;
312 p_input->c_payload_bytes = 0;
313 p_input->c_packets_read = 0;
314 p_input->c_packets_trashed = 0;
317 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
318 (probedata_t *)p_input );
320 if( p_input->p_input_module == NULL )
322 intf_ErrMsg( "input error: no suitable input module for `%s'",
327 #define f p_input->p_input_module->p_functions->input.functions.input
328 p_input->pf_init = f.pf_init;
329 p_input->pf_open = f.pf_open;
330 p_input->pf_close = f.pf_close;
331 p_input->pf_end = f.pf_end;
332 p_input->pf_read = f.pf_read;
333 p_input->pf_set_area = f.pf_set_area;
334 p_input->pf_demux = f.pf_demux;
335 p_input->pf_new_packet = f.pf_new_packet;
336 p_input->pf_new_pes = f.pf_new_pes;
337 p_input->pf_delete_packet = f.pf_delete_packet;
338 p_input->pf_delete_pes = f.pf_delete_pes;
339 p_input->pf_rewind = f.pf_rewind;
340 p_input->pf_seek = f.pf_seek;
342 p_input->pf_open( p_input );
344 if( p_input->b_error )
346 /* We barfed -- exit nicely */
347 p_input->pf_close( p_input );
348 module_Unneed( p_input->p_input_module );
352 p_input->pf_init( p_input );
354 if( p_input->b_error )
356 /* We barfed -- exit nicely */
357 p_input->pf_close( p_input );
358 module_Unneed( p_input->p_input_module );
362 *p_input->pi_status = THREAD_READY;
367 /*****************************************************************************
368 * ErrorThread: RunThread() error loop
369 *****************************************************************************
370 * This function is called when an error occured during thread main's loop.
371 *****************************************************************************/
372 static void ErrorThread( input_thread_t *p_input )
374 while( !p_input->b_die )
377 msleep( INPUT_IDLE_SLEEP );
381 /*****************************************************************************
382 * EndThread: end the input thread
383 *****************************************************************************/
384 static void EndThread( input_thread_t * p_input )
386 int * pi_status; /* thread status */
389 pi_status = p_input->pi_status;
390 *pi_status = THREAD_END;
394 struct tms cpu_usage;
397 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
398 cpu_usage.tms_utime, cpu_usage.tms_stime);
402 /* Free all ES and destroy all decoder threads */
403 input_EndStream( p_input );
405 /* Free demultiplexer's data */
406 p_input->pf_end( p_input );
409 p_input->pf_close( p_input );
411 /* Release modules */
412 module_Unneed( p_input->p_input_module );
416 /*****************************************************************************
417 * DestroyThread: destroy the input thread
418 *****************************************************************************/
419 static void DestroyThread( input_thread_t * p_input )
421 int * pi_status; /* thread status */
424 pi_status = p_input->pi_status;
426 /* Destroy Mutex locks */
427 vlc_mutex_destroy( &p_input->stream.control.control_lock );
428 vlc_mutex_destroy( &p_input->stream.stream_lock );
430 /* Free input structure */
434 *pi_status = THREAD_OVER;
437 /*****************************************************************************
438 * input_FileOpen : open a file descriptor
439 *****************************************************************************/
440 void input_FileOpen( input_thread_t * p_input )
442 struct stat stat_info;
445 char *psz_name = p_input->p_source;
447 /* FIXME: this code ought to be in the plugin so that code can
448 * be shared with the *_Probe function */
449 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
451 int i_size = strlen( psz_name );
454 && !strncasecmp( psz_name, "dvd:", 4 ) )
456 /* get rid of the 'dvd:' stuff and try again */
458 i_stat = stat( psz_name, &stat_info );
460 else if( ( i_size > 5 )
461 && !strncasecmp( psz_name, "file:", 5 ) )
463 /* get rid of the 'file:' stuff and try again */
465 i_stat = stat( psz_name, &stat_info );
470 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
471 psz_name, strerror(errno));
472 p_input->b_error = 1;
477 vlc_mutex_lock( &p_input->stream.stream_lock );
479 /* If we are here we can control the pace... */
480 p_input->stream.b_pace_control = 1;
482 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
483 || S_ISBLK(stat_info.st_mode) )
485 p_input->stream.b_seekable = 1;
486 p_input->stream.p_selected_area->i_size = stat_info.st_size;
488 else if( S_ISFIFO(stat_info.st_mode)
489 #if !defined( SYS_BEOS ) && !defined( WIN32 )
490 || S_ISSOCK(stat_info.st_mode)
494 p_input->stream.b_seekable = 0;
495 p_input->stream.p_selected_area->i_size = 0;
499 vlc_mutex_unlock( &p_input->stream.stream_lock );
500 intf_ErrMsg( "input error: unknown file type for `%s'",
502 p_input->b_error = 1;
506 p_input->stream.p_selected_area->i_tell = 0;
507 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
511 if( (p_input->i_handle = open( psz_name,
512 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
514 if( (p_input->i_handle = open( psz_name, O_BINARY
515 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
518 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
519 p_input->b_error = 1;
525 /*****************************************************************************
526 * input_FileClose : close a file descriptor
527 *****************************************************************************/
528 void input_FileClose( input_thread_t * p_input )
530 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
531 close( p_input->i_handle );
537 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
538 /*****************************************************************************
539 * input_NetworkOpen : open a network socket
540 *****************************************************************************/
541 void input_NetworkOpen( input_thread_t * p_input )
543 char *psz_server = NULL;
544 char *psz_broadcast = NULL;
547 struct sockaddr_in sock;
549 /* Get the remote server */
550 if( p_input->p_source != NULL )
552 psz_server = p_input->p_source;
554 /* Skip the protocol name */
555 while( *psz_server && *psz_server != ':' )
560 /* Skip the "://" part */
561 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
566 /* Found a server name */
569 char *psz_port = psz_server;
571 /* Skip the hostname part */
572 while( *psz_port && *psz_port != ':' )
577 /* Found a port name */
580 /* Replace ':' with '\0' */
584 psz_broadcast = psz_port;
585 while( *psz_broadcast && *psz_broadcast != ':' )
592 *psz_broadcast = '\0';
594 while( *psz_broadcast && *psz_broadcast == ':' )
601 psz_broadcast = NULL;
604 /* port before broadcast address */
605 if( *psz_port != ':' )
607 i_port = atoi( psz_port );
617 /* Check that we got a valid server */
618 if( psz_server == NULL )
620 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
621 INPUT_SERVER_DEFAULT );
624 /* Check that we got a valid port */
627 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
630 if( psz_broadcast == NULL )
632 /* Are we broadcasting ? */
633 psz_broadcast = main_GetPszVariable( INPUT_BROADCAST_VAR, NULL );
636 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
637 psz_server, i_port, psz_broadcast );
639 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
641 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
642 if( p_input->i_handle == -1 )
644 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
645 p_input->b_error = 1;
649 /* We may want to reuse an already used socket */
651 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
652 &i_opt, sizeof( i_opt ) ) == -1 )
654 intf_ErrMsg("input error: can't configure socket (SO_REUSEADDR: %s)",
656 close( p_input->i_handle );
657 p_input->b_error = 1;
661 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
662 * packet loss caused by scheduling problems */
664 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
665 &i_opt, sizeof( i_opt ) ) == -1 )
667 intf_ErrMsg("input error: can't configure socket (SO_RCVBUF: %s)",
669 close( p_input->i_handle );
670 p_input->b_error = 1;
674 /* Build the local socket */
675 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast )
678 close( p_input->i_handle );
679 p_input->b_error = 1;
684 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
685 sizeof( sock ) ) < 0 )
687 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
688 close( p_input->i_handle );
689 p_input->b_error = 1;
693 /* Build socket for remote connection */
694 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
696 close( p_input->i_handle );
697 p_input->b_error = 1;
701 /* And connect it ... should we really connect ? */
702 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
703 sizeof( sock ) ) == (-1) )
705 intf_ErrMsg( "NetworkOpen: can't connect socket : %s",
707 close( p_input->i_handle );
708 p_input->b_error = 1;
712 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
713 * with the server. */
714 p_input->stream.b_pace_control = 0;
715 p_input->stream.b_seekable = 0;
720 /*****************************************************************************
721 * input_NetworkClose : close a network socket
722 *****************************************************************************/
723 void input_NetworkClose( input_thread_t * p_input )
725 close( p_input->i_handle );