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.97 2001/04/12 02:40: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>
39 /* Network functions */
41 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
42 #include <netdb.h> /* hostent ... */
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
51 # include <sys/times.h>
62 #include "intf_playlist.h"
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
66 #include "input_ext-dec.h"
69 #include "interface.h"
73 /* #include <netutils.h> */
76 /*****************************************************************************
78 *****************************************************************************/
79 static void RunThread ( input_thread_t *p_input );
80 static int InitThread ( input_thread_t *p_input );
81 static void ErrorThread ( input_thread_t *p_input );
82 static void DestroyThread ( input_thread_t *p_input );
83 static void EndThread ( input_thread_t *p_input );
85 /*****************************************************************************
86 * input_CreateThread: creates a new input thread
87 *****************************************************************************
88 * This function creates a new input, and returns a pointer
89 * to its description. On error, it returns NULL.
90 * If pi_status is NULL, then the function will block until the thread is ready.
91 * If not, it will be updated using one of the THREAD_* constants.
92 *****************************************************************************/
93 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
95 input_thread_t * p_input; /* thread descriptor */
96 int i_status; /* thread status */
98 /* Allocate descriptor */
99 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
100 if( p_input == NULL )
102 intf_ErrMsg( "input error: can't allocate input thread (%s)",
107 /* Packets read once */
108 p_input->i_read_once = INPUT_READ_ONCE;
110 /* Initialize thread properties */
112 p_input->b_error = 0;
116 p_input->p_source = p_item->psz_name;
118 /* I have never understood that stuff --Meuuh */
119 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
120 *p_input->pi_status = THREAD_CREATE;
122 /* Initialize stream description */
123 p_input->stream.i_es_number = 0;
124 p_input->stream.i_selected_es_number = 0;
125 p_input->stream.i_pgrm_number = 0;
126 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
127 p_input->stream.i_mux_rate = 0;
129 /* no stream, no area */
130 p_input->stream.i_area_nb = 0;
131 p_input->stream.pp_areas = NULL;
132 p_input->stream.p_selected_area = NULL;
133 /* By default there is one areas in a stream */
134 input_AddArea( p_input );
135 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
137 /* Initialize stream control properties. */
138 p_input->stream.control.i_status = PLAYING_S;
139 p_input->stream.control.i_rate = DEFAULT_RATE;
140 p_input->stream.control.b_mute = 0;
141 p_input->stream.control.b_bw = 0;
143 /* Initialize default settings for spawned decoders */
144 p_input->p_default_aout = p_main->p_aout;
145 p_input->p_default_vout = p_main->p_vout;
147 /* Create thread and set locks. */
148 vlc_mutex_init( &p_input->stream.stream_lock );
149 vlc_cond_init( &p_input->stream.stream_wait );
150 vlc_mutex_init( &p_input->stream.control.control_lock );
151 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
154 intf_ErrMsg( "input error: can't create input thread (%s)",
160 /* If status is NULL, wait until the thread is created */
161 if( pi_status == NULL )
165 msleep( THREAD_SLEEP );
166 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
167 && (i_status != THREAD_FATAL) );
168 if( i_status != THREAD_READY )
176 /*****************************************************************************
177 * input_DestroyThread: mark an input thread as zombie
178 *****************************************************************************
179 * This function should not return until the thread is effectively cancelled.
180 *****************************************************************************/
181 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
183 int i_status; /* thread status */
186 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
187 *p_input->pi_status = THREAD_DESTROY;
189 /* Request thread destruction */
192 /* Make the thread exit of an eventual vlc_cond_wait() */
193 vlc_mutex_lock( &p_input->stream.stream_lock );
194 vlc_cond_signal( &p_input->stream.stream_wait );
195 vlc_mutex_unlock( &p_input->stream.stream_lock );
197 /* If status is NULL, wait until thread has been destroyed */
198 if( pi_status == NULL )
202 msleep( THREAD_SLEEP );
203 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
204 && (i_status != THREAD_FATAL) );
208 /*****************************************************************************
209 * RunThread: main thread loop
210 *****************************************************************************
211 * Thread in charge of processing the network packets and demultiplexing.
212 *****************************************************************************/
213 static void RunThread( input_thread_t *p_input )
217 if( InitThread( p_input ) )
220 /* If we failed, wait before we are killed, and exit */
221 *p_input->pi_status = THREAD_ERROR;
222 p_input->b_error = 1;
223 ErrorThread( p_input );
224 DestroyThread( p_input );
228 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
230 data_packet_t * pp_packets[p_input->i_read_once];
236 vlc_mutex_lock( &p_input->stream.stream_lock );
238 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
240 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
242 p_input->pf_seek( p_input,
243 p_input->stream.p_selected_area->i_seek );
245 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
247 pgrm_descriptor_t * p_pgrm
248 = p_input->stream.pp_programs[i];
249 /* Escape all decoders for the stream discontinuity they
251 input_EscapeDiscontinuity( p_input, p_pgrm );
253 /* Reinitialize synchro. */
254 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
257 p_input->stream.p_selected_area->i_seek = NO_SEEK;
260 vlc_mutex_unlock( &p_input->stream.stream_lock );
262 i_error = p_input->pf_read( p_input, pp_packets );
264 /* Demultiplex read packets. */
265 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
267 p_input->pf_demux( p_input, pp_packets[i] );
274 /* End of file - we do not set b_die because only the
275 * interface is allowed to do so. */
276 intf_WarnMsg( 1, "End of file reached" );
281 p_input->b_error = 1;
286 if( p_input->b_error || p_input->b_eof )
288 ErrorThread( p_input );
291 EndThread( p_input );
293 DestroyThread( p_input );
295 intf_DbgMsg("Thread end");
298 /*****************************************************************************
299 * InitThread: init the input Thread
300 *****************************************************************************/
301 static int InitThread( input_thread_t * p_input )
305 /* Initialize statistics */
306 p_input->c_loops = 0;
307 p_input->c_bytes = 0;
308 p_input->c_payload_bytes = 0;
309 p_input->c_packets_read = 0;
310 p_input->c_packets_trashed = 0;
313 p_input->p_input_module = module_Need( p_main->p_bank,
314 MODULE_CAPABILITY_INPUT,
315 (probedata_t *)p_input );
317 if( p_input->p_input_module == NULL )
319 intf_ErrMsg( "input error: no suitable input module for `%s'",
324 #define f p_input->p_input_module->p_functions->input.functions.input
325 p_input->pf_init = f.pf_init;
326 p_input->pf_open = f.pf_open;
327 p_input->pf_close = f.pf_close;
328 p_input->pf_end = f.pf_end;
329 p_input->pf_read = f.pf_read;
330 p_input->pf_set_area = f.pf_set_area;
331 p_input->pf_demux = f.pf_demux;
332 p_input->pf_new_packet = f.pf_new_packet;
333 p_input->pf_new_pes = f.pf_new_pes;
334 p_input->pf_delete_packet = f.pf_delete_packet;
335 p_input->pf_delete_pes = f.pf_delete_pes;
336 p_input->pf_rewind = f.pf_rewind;
337 p_input->pf_seek = f.pf_seek;
339 p_input->pf_open( p_input );
341 if( p_input->b_error )
343 /* We barfed -- exit nicely */
344 p_input->pf_close( p_input );
345 module_Unneed( p_main->p_bank, p_input->p_input_module );
349 p_input->pf_init( p_input );
351 if( p_input->b_error )
353 /* We barfed -- exit nicely */
354 p_input->pf_close( p_input );
355 module_Unneed( p_main->p_bank, p_input->p_input_module );
359 *p_input->pi_status = THREAD_READY;
364 /*****************************************************************************
365 * ErrorThread: RunThread() error loop
366 *****************************************************************************
367 * This function is called when an error occured during thread main's loop.
368 *****************************************************************************/
369 static void ErrorThread( input_thread_t *p_input )
371 while( !p_input->b_die )
374 msleep( INPUT_IDLE_SLEEP );
378 /*****************************************************************************
379 * EndThread: end the input thread
380 *****************************************************************************/
381 static void EndThread( input_thread_t * p_input )
383 int * pi_status; /* thread status */
386 pi_status = p_input->pi_status;
387 *pi_status = THREAD_END;
391 struct tms cpu_usage;
394 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
395 cpu_usage.tms_utime, cpu_usage.tms_stime);
399 /* Free all ES and destroy all decoder threads */
400 input_EndStream( p_input );
402 /* Free demultiplexer's data */
403 p_input->pf_end( p_input );
406 p_input->pf_close( p_input );
408 /* Release modules */
409 module_Unneed( p_main->p_bank, p_input->p_input_module );
413 /*****************************************************************************
414 * DestroyThread: destroy the input thread
415 *****************************************************************************/
416 static void DestroyThread( input_thread_t * p_input )
418 int * pi_status; /* thread status */
421 pi_status = p_input->pi_status;
423 /* Destroy Mutex locks */
424 vlc_mutex_destroy( &p_input->stream.control.control_lock );
425 vlc_mutex_destroy( &p_input->stream.stream_lock );
427 /* Free input structure */
431 *pi_status = THREAD_OVER;
434 /*****************************************************************************
435 * input_FileOpen : open a file descriptor
436 *****************************************************************************/
437 void input_FileOpen( input_thread_t * p_input )
439 struct stat stat_info;
442 char *psz_name = p_input->p_source;
444 /* FIXME: this code ought to be in the plugin so that code can
445 * be shared with the *_Probe function */
446 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
448 int i_size = strlen( psz_name );
451 && !strncasecmp( psz_name, "dvd:", 4 ) )
453 /* get rid of the 'dvd:' stuff and try again */
455 i_stat = stat( psz_name, &stat_info );
457 else if( ( i_size > 5 )
458 && !strncasecmp( psz_name, "file:", 5 ) )
460 /* get rid of the 'file:' stuff and try again */
462 i_stat = stat( psz_name, &stat_info );
467 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
468 psz_name, strerror(errno));
469 p_input->b_error = 1;
474 vlc_mutex_lock( &p_input->stream.stream_lock );
476 /* If we are here we can control the pace... */
477 p_input->stream.b_pace_control = 1;
479 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
480 || S_ISBLK(stat_info.st_mode) )
482 p_input->stream.b_seekable = 1;
483 p_input->stream.p_selected_area->i_size = stat_info.st_size;
485 else if( S_ISFIFO(stat_info.st_mode)
487 || S_ISSOCK(stat_info.st_mode)
491 p_input->stream.b_seekable = 0;
492 p_input->stream.p_selected_area->i_size = 0;
496 vlc_mutex_unlock( &p_input->stream.stream_lock );
497 intf_ErrMsg( "input error: unknown file type for `%s'",
499 p_input->b_error = 1;
503 p_input->stream.p_selected_area->i_tell = 0;
504 vlc_mutex_unlock( &p_input->stream.stream_lock );
506 intf_Msg( "input: opening %s", p_input->p_source );
507 if( (p_input->i_handle = open( psz_name,
508 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
510 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
511 p_input->b_error = 1;
517 /*****************************************************************************
518 * input_FileClose : close a file descriptor
519 *****************************************************************************/
520 void input_FileClose( input_thread_t * p_input )
522 close( p_input->i_handle );
528 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
529 /*****************************************************************************
530 * input_NetworkOpen : open a network socket
531 *****************************************************************************/
532 void input_NetworkOpen( input_thread_t * p_input )
535 int i_option_value, i_port;
536 struct sockaddr_in s_socket;
537 boolean_t b_broadcast;
539 /* FIXME : we don't handle channels for the moment */
541 /* Get the remote server */
542 if( p_input->p_source == NULL )
544 p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR,
545 INPUT_SERVER_DEFAULT );
548 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
550 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
551 if( p_input->i_handle == -1 )
553 intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
554 p_input->b_error = 1;
558 /* We may want to reuse an already used socket */
560 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
561 &i_option_value,sizeof( i_option_value ) ) == -1 )
563 intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
565 close( p_input->i_handle );
566 p_input->b_error = 1;
570 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
571 * packet loss caused by scheduling problems */
572 i_option_value = 524288;
573 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
574 sizeof( i_option_value ) ) == -1 )
576 intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)",
578 close( p_input->i_handle );
579 p_input->b_error = 1;
583 /* Get details about what we are supposed to do */
584 b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
585 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
587 /* TODO : here deal with channel stufs */
589 /* Build the local socket */
590 if ( network_BuildLocalAddr( &s_socket, i_port, b_broadcast )
593 close( p_input->i_handle );
594 p_input->b_error = 1;
599 if( bind( p_input->i_handle, (struct sockaddr *)&s_socket,
600 sizeof( s_socket ) ) < 0 )
602 intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
603 close( p_input->i_handle );
604 p_input->b_error = 1;
608 /* Build socket for remote connection */
609 if ( network_BuildRemoteAddr( &s_socket, p_input->p_source )
612 close( p_input->i_handle );
613 p_input->b_error = 1;
617 /* And connect it ... should we really connect ? */
618 if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
619 sizeof( s_socket ) ) == (-1) )
621 intf_ErrMsg("NetworkOpen: can't connect socket" );
622 close( p_input->i_handle );
623 p_input->b_error = 1;
627 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
628 * with the server. */
629 p_input->stream.b_pace_control = 1;
634 /*****************************************************************************
635 * input_NetworkClose : close a network socket
636 *****************************************************************************/
637 void input_NetworkClose( input_thread_t * p_input )
639 close( p_input->i_handle );
640 /* FIXME: deal with channels */