1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2002 VideoLAN
7 * $Id: input.c,v 1.214 2002/11/10 18:04:23 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 *****************************************************************************/
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
45 #ifdef HAVE_SYS_TIMES_H
46 # include <sys/times.h>
50 #include "vlc_playlist.h"
52 #include "stream_control.h"
53 #include "input_ext-intf.h"
54 #include "input_ext-dec.h"
55 #include "input_ext-plugins.h"
57 #include "stream_output.h"
59 #include "interface.h"
61 /*****************************************************************************
63 *****************************************************************************/
64 static int RunThread ( input_thread_t *p_input );
65 static int InitThread ( input_thread_t *p_input );
66 static void ErrorThread ( input_thread_t *p_input );
67 static void EndThread ( input_thread_t *p_input );
69 /*****************************************************************************
70 * input_CreateThread: creates a new input thread
71 *****************************************************************************
72 * This function creates a new input, and returns a pointer
73 * to its description. On error, it returns NULL.
74 * If pi_status is NULL, then the function will block until the thread is ready.
75 * If not, it will be updated using one of the THREAD_* constants.
76 *****************************************************************************/
77 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
78 playlist_item_t *p_item, int *pi_status )
80 input_thread_t * p_input; /* thread descriptor */
81 input_info_category_t * p_info;
83 /* Allocate descriptor */
84 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
87 msg_Err( p_parent, "out of memory" );
91 /* Initialize thread properties */
95 p_input->psz_source = strdup( p_item->psz_name );
98 p_input->p_demux = NULL;
101 p_input->p_access = NULL;
103 p_input->i_bufsize = 0;
106 /* Initialize statistics */
107 p_input->c_loops = 0;
108 p_input->stream.c_packets_read = 0;
109 p_input->stream.c_packets_trashed = 0;
112 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
113 vlc_cond_init( p_input, &p_input->stream.stream_wait );
114 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
116 /* Initialize stream description */
117 p_input->stream.b_changed = 0;
118 p_input->stream.i_es_number = 0;
119 p_input->stream.i_selected_es_number = 0;
120 p_input->stream.i_pgrm_number = 0;
121 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
122 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
123 p_input->stream.i_mux_rate = 0;
124 p_input->stream.b_seekable = 0;
125 p_input->stream.p_sout = NULL;
127 /* no stream, no program, no area, no es */
128 p_input->stream.p_new_program = NULL;
130 p_input->stream.i_area_nb = 0;
131 p_input->stream.pp_areas = NULL;
132 p_input->stream.p_selected_area = NULL;
133 p_input->stream.p_new_area = NULL;
135 p_input->stream.pp_selected_es = NULL;
136 p_input->stream.p_removed_es = NULL;
137 p_input->stream.p_newly_selected_es = NULL;
139 /* By default there is one area in a stream */
140 input_AddArea( p_input );
141 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
143 /* Initialize stream control properties. */
144 p_input->stream.control.i_status = PLAYING_S;
145 p_input->stream.control.i_rate = DEFAULT_RATE;
146 p_input->stream.control.b_mute = 0;
147 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
149 /* Initialize input info */
150 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
151 if( !p_input->stream.p_info )
153 msg_Err( p_input, "No memory!" );
156 p_input->stream.p_info->psz_name = strdup("General") ;
157 p_input->stream.p_info->p_info = NULL;
158 p_input->stream.p_info->p_next = NULL;
160 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
162 p_info = input_InfoCategory( p_input, "General" );
163 input_AddInfo( p_info, "playlist item", p_input->psz_source );
164 vlc_object_attach( p_input, p_parent );
166 /* Create thread and wait for its readiness. */
167 if( vlc_thread_create( p_input, "input", RunThread,
168 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
170 msg_Err( p_input, "cannot create input thread" );
178 /*****************************************************************************
179 * input_StopThread: mark an input thread as zombie
180 *****************************************************************************
181 * This function should not return until the thread is effectively cancelled.
182 *****************************************************************************/
183 void input_StopThread( input_thread_t *p_input )
185 /* Make the thread exit from a possible vlc_cond_wait() */
186 vlc_mutex_lock( &p_input->stream.stream_lock );
187 /* Request thread destruction */
190 vlc_cond_signal( &p_input->stream.stream_wait );
191 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 )
201 /* Join the thread */
202 vlc_thread_join( p_input );
204 /* Destroy Mutex locks */
205 vlc_mutex_destroy( &p_input->stream.control.control_lock );
206 vlc_cond_destroy( &p_input->stream.stream_wait );
207 vlc_mutex_destroy( &p_input->stream.stream_lock );
210 /*****************************************************************************
211 * RunThread: main thread loop
212 *****************************************************************************
213 * Thread in charge of processing the network packets and demultiplexing.
214 *****************************************************************************/
215 static int RunThread( input_thread_t *p_input )
217 /* Signal right now, otherwise we'll get stuck in a peek */
218 vlc_thread_ready( p_input );
220 if( InitThread( p_input ) )
222 /* If we failed, wait before we are killed, and exit */
223 p_input->b_error = 1;
224 ErrorThread( p_input );
229 /* initialization is complete */
230 vlc_mutex_lock( &p_input->stream.stream_lock );
231 p_input->stream.b_changed = 1;
232 vlc_mutex_unlock( &p_input->stream.stream_lock );
234 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
240 vlc_mutex_lock( &p_input->stream.stream_lock );
242 if( p_input->stream.p_new_program )
244 if( p_input->pf_set_program != NULL )
247 /* Reinitialize buffer manager. */
248 input_AccessReinit( p_input );
250 p_input->pf_set_program( p_input,
251 p_input->stream.p_new_program );
253 /* Escape all decoders for the stream discontinuity they
255 input_EscapeDiscontinuity( p_input );
257 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
259 pgrm_descriptor_t * p_pgrm
260 = p_input->stream.pp_programs[i];
262 /* Reinitialize synchro. */
263 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
266 p_input->stream.p_new_program = NULL;
269 if( p_input->stream.p_new_area )
271 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
273 input_AccessReinit( p_input );
275 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
277 /* Escape all decoders for the stream discontinuity they
279 input_EscapeDiscontinuity( p_input );
281 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
283 pgrm_descriptor_t * p_pgrm
284 = p_input->stream.pp_programs[i];
286 /* Reinitialize synchro. */
287 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
290 p_input->stream.p_new_area = NULL;
293 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
295 if( p_input->stream.b_seekable
296 && p_input->pf_seek != NULL )
300 /* Reinitialize buffer manager. */
301 input_AccessReinit( p_input );
303 i_new_pos = p_input->stream.p_selected_area->i_seek;
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
305 p_input->pf_seek( p_input, i_new_pos );
306 vlc_mutex_lock( &p_input->stream.stream_lock );
308 /* Escape all decoders for the stream discontinuity they
310 input_EscapeDiscontinuity( p_input );
312 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
314 pgrm_descriptor_t * p_pgrm
315 = p_input->stream.pp_programs[i];
317 /* Reinitialize synchro. */
318 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
321 p_input->stream.p_selected_area->i_seek = NO_SEEK;
324 if( p_input->stream.p_removed_es )
326 input_UnselectES( p_input, p_input->stream.p_removed_es );
327 p_input->stream.p_removed_es = NULL;
330 if( p_input->stream.p_newly_selected_es )
332 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
333 p_input->stream.p_newly_selected_es = NULL;
336 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
338 if( p_input->stream.b_new_mute )
340 input_EscapeAudioDiscontinuity( p_input );
343 vlc_mutex_lock( &p_input->stream.control.control_lock );
344 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
345 vlc_mutex_unlock( &p_input->stream.control.control_lock );
347 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
350 vlc_mutex_unlock( &p_input->stream.stream_lock );
352 /* Read and demultiplex some data. */
353 i_count = p_input->pf_demux( p_input );
355 if( i_count == 0 && p_input->stream.b_seekable )
357 /* End of file - we do not set b_die because only the
358 * playlist is allowed to do so. */
359 msg_Info( p_input, "EOF reached" );
362 else if( i_count < 0 )
364 p_input->b_error = 1;
368 if( p_input->b_error || p_input->b_eof )
370 ErrorThread( p_input );
373 EndThread( p_input );
378 /*****************************************************************************
379 * InitThread: init the input Thread
380 *****************************************************************************/
381 static int InitThread( input_thread_t * p_input )
383 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
384 char * psz_parser = p_input->psz_source;
386 /* Skip the plug-in names */
387 while( *psz_parser && *psz_parser != ':' )
392 if( psz_parser - p_input->psz_source == 1 )
394 msg_Warn( p_input, "drive letter %c: found in source string",
395 p_input->psz_source ) ;
402 p_input->psz_access = p_input->psz_demux = "";
403 p_input->psz_name = p_input->psz_source;
407 *psz_parser++ = '\0';
409 /* let's skip '//' */
410 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
415 p_input->psz_name = psz_parser ;
417 /* Come back to parse the access and demux plug-ins */
418 psz_parser = p_input->psz_source;
423 p_input->psz_access = "";
425 else if( *psz_parser == '/' )
428 p_input->psz_access = "";
433 p_input->psz_access = psz_parser;
435 while( *psz_parser && *psz_parser != '/' )
440 if( *psz_parser == '/' )
442 *psz_parser++ = '\0';
449 p_input->psz_demux = "";
453 p_input->psz_demux = psz_parser;
457 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
458 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
460 if( input_AccessInit( p_input ) == -1 )
465 /* Find and open appropriate access module */
466 p_input->p_access = module_Need( p_input, "access",
467 p_input->psz_access );
469 if( p_input->p_access == NULL )
471 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
472 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
476 /* Waiting for stream. */
479 p_input->i_bufsize = p_input->i_mtu;
483 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
486 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
488 while( !input_FillBuffer( p_input ) )
490 if( p_input->b_die || p_input->b_error || p_input->b_eof )
492 module_Unneed( p_input, p_input->p_access );
498 /* Find and open appropriate demux module */
499 p_input->p_demux = module_Need( p_input, "demux",
500 p_input->psz_demux );
502 if( p_input->p_demux== NULL )
504 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
505 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
506 module_Unneed( p_input, p_input->p_access );
510 /* Initialize optional stream output. */
511 psz_parser = config_GetPsz( p_input, "sout" );
512 if ( psz_parser != NULL )
515 (p_input->stream.p_sout = sout_NewInstance( p_input, psz_parser ))
518 msg_Err( p_input, "cannot start stream output instance, aborting" );
520 module_Unneed( p_input, p_input->p_access );
521 module_Unneed( p_input, p_input->p_demux );
531 /*****************************************************************************
532 * ErrorThread: RunThread() error loop
533 *****************************************************************************
534 * This function is called when an error occured during thread main's loop.
535 *****************************************************************************/
536 static void ErrorThread( input_thread_t *p_input )
538 while( !p_input->b_die )
541 msleep( INPUT_IDLE_SLEEP );
545 /*****************************************************************************
546 * EndThread: end the input thread
547 *****************************************************************************/
548 static void EndThread( input_thread_t * p_input )
550 #ifdef HAVE_SYS_TIMES_H
551 /* Display statistics */
552 struct tms cpu_usage;
555 msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
556 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
558 msg_Dbg( p_input, "%d loops", p_input->c_loops );
561 /* Free info structures */
562 msg_Dbg( p_input, "freeing info structures...");
563 input_DelInfo( p_input );
565 input_DumpStream( p_input );
567 /* Free all ES and destroy all decoder threads */
568 input_EndStream( p_input );
570 /* Close optional stream output instance */
571 if ( p_input->stream.p_sout != NULL )
573 sout_DeleteInstance( p_input->stream.p_sout );
576 /* Free demultiplexer's data */
577 module_Unneed( p_input, p_input->p_demux );
579 /* Close the access plug-in */
580 module_Unneed( p_input, p_input->p_access );
582 input_AccessEnd( p_input );
584 free( p_input->psz_source );
586 /* Tell we're dead */