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.213 2002/10/08 16:02:05 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 *****************************************************************************/
30 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
44 #include "vlc_playlist.h"
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48 #include "input_ext-dec.h"
49 #include "input_ext-plugins.h"
51 #include "stream_output.h"
53 #include "interface.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static int RunThread ( input_thread_t *p_input );
59 static int InitThread ( input_thread_t *p_input );
60 static void ErrorThread ( input_thread_t *p_input );
61 static void EndThread ( input_thread_t *p_input );
63 /*****************************************************************************
64 * input_CreateThread: creates a new input thread
65 *****************************************************************************
66 * This function creates a new input, and returns a pointer
67 * to its description. On error, it returns NULL.
68 * If pi_status is NULL, then the function will block until the thread is ready.
69 * If not, it will be updated using one of the THREAD_* constants.
70 *****************************************************************************/
71 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
72 playlist_item_t *p_item, int *pi_status )
74 input_thread_t * p_input; /* thread descriptor */
75 input_info_category_t * p_info;
77 /* Allocate descriptor */
78 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
81 msg_Err( p_parent, "out of memory" );
85 /* Initialize thread properties */
89 p_input->psz_source = strdup( p_item->psz_name );
92 p_input->p_demux = NULL;
95 p_input->p_access = NULL;
97 p_input->i_bufsize = 0;
100 /* Initialize statistics */
101 p_input->c_loops = 0;
102 p_input->stream.c_packets_read = 0;
103 p_input->stream.c_packets_trashed = 0;
106 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
107 vlc_cond_init( p_input, &p_input->stream.stream_wait );
108 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
110 /* Initialize stream description */
111 p_input->stream.b_changed = 0;
112 p_input->stream.i_es_number = 0;
113 p_input->stream.i_selected_es_number = 0;
114 p_input->stream.i_pgrm_number = 0;
115 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
116 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
117 p_input->stream.i_mux_rate = 0;
118 p_input->stream.b_seekable = 0;
119 p_input->stream.p_sout = NULL;
121 /* no stream, no program, no area, no es */
122 p_input->stream.p_new_program = NULL;
124 p_input->stream.i_area_nb = 0;
125 p_input->stream.pp_areas = NULL;
126 p_input->stream.p_selected_area = NULL;
127 p_input->stream.p_new_area = NULL;
129 p_input->stream.pp_selected_es = NULL;
130 p_input->stream.p_removed_es = NULL;
131 p_input->stream.p_newly_selected_es = NULL;
133 /* By default there is one area 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_grayscale = config_GetInt( p_input, "grayscale" );
143 /* Initialize input info */
144 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
145 if( !p_input->stream.p_info )
147 msg_Err( p_input, "No memory!" );
150 p_input->stream.p_info->psz_name = strdup("General") ;
151 p_input->stream.p_info->p_info = NULL;
152 p_input->stream.p_info->p_next = NULL;
154 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
156 p_info = input_InfoCategory( p_input, "General" );
157 input_AddInfo( p_info, "playlist item", p_input->psz_source );
158 vlc_object_attach( p_input, p_parent );
160 /* Create thread and wait for its readiness. */
161 if( vlc_thread_create( p_input, "input", RunThread,
162 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
164 msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
172 /*****************************************************************************
173 * input_StopThread: mark an input thread as zombie
174 *****************************************************************************
175 * This function should not return until the thread is effectively cancelled.
176 *****************************************************************************/
177 void input_StopThread( input_thread_t *p_input )
179 /* Make the thread exit from a possible vlc_cond_wait() */
180 vlc_mutex_lock( &p_input->stream.stream_lock );
181 /* Request thread destruction */
184 vlc_cond_signal( &p_input->stream.stream_wait );
185 vlc_mutex_unlock( &p_input->stream.stream_lock );
188 /*****************************************************************************
189 * input_DestroyThread: mark an input thread as zombie
190 *****************************************************************************
191 * This function should not return until the thread is effectively cancelled.
192 *****************************************************************************/
193 void input_DestroyThread( input_thread_t *p_input )
195 /* Join the thread */
196 vlc_thread_join( p_input );
198 /* Destroy Mutex locks */
199 vlc_mutex_destroy( &p_input->stream.control.control_lock );
200 vlc_cond_destroy( &p_input->stream.stream_wait );
201 vlc_mutex_destroy( &p_input->stream.stream_lock );
204 /*****************************************************************************
205 * RunThread: main thread loop
206 *****************************************************************************
207 * Thread in charge of processing the network packets and demultiplexing.
208 *****************************************************************************/
209 static int RunThread( input_thread_t *p_input )
211 /* Signal right now, otherwise we'll get stuck in a peek */
212 vlc_thread_ready( p_input );
214 if( InitThread( p_input ) )
216 /* If we failed, wait before we are killed, and exit */
217 p_input->b_error = 1;
218 ErrorThread( p_input );
223 /* initialization is complete */
224 vlc_mutex_lock( &p_input->stream.stream_lock );
225 p_input->stream.b_changed = 1;
226 vlc_mutex_unlock( &p_input->stream.stream_lock );
228 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
234 vlc_mutex_lock( &p_input->stream.stream_lock );
236 if( p_input->stream.p_new_program )
238 if( p_input->pf_set_program != NULL )
241 /* Reinitialize buffer manager. */
242 input_AccessReinit( p_input );
244 p_input->pf_set_program( p_input,
245 p_input->stream.p_new_program );
247 /* Escape all decoders for the stream discontinuity they
249 input_EscapeDiscontinuity( p_input );
251 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
253 pgrm_descriptor_t * p_pgrm
254 = p_input->stream.pp_programs[i];
256 /* Reinitialize synchro. */
257 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
260 p_input->stream.p_new_program = NULL;
263 if( p_input->stream.p_new_area )
265 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
267 input_AccessReinit( p_input );
269 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
271 /* Escape all decoders for the stream discontinuity they
273 input_EscapeDiscontinuity( p_input );
275 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
277 pgrm_descriptor_t * p_pgrm
278 = p_input->stream.pp_programs[i];
280 /* Reinitialize synchro. */
281 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
284 p_input->stream.p_new_area = NULL;
287 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
289 if( p_input->stream.b_seekable
290 && p_input->pf_seek != NULL )
294 /* Reinitialize buffer manager. */
295 input_AccessReinit( p_input );
297 i_new_pos = p_input->stream.p_selected_area->i_seek;
298 vlc_mutex_unlock( &p_input->stream.stream_lock );
299 p_input->pf_seek( p_input, i_new_pos );
300 vlc_mutex_lock( &p_input->stream.stream_lock );
302 /* Escape all decoders for the stream discontinuity they
304 input_EscapeDiscontinuity( p_input );
306 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
308 pgrm_descriptor_t * p_pgrm
309 = p_input->stream.pp_programs[i];
311 /* Reinitialize synchro. */
312 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
315 p_input->stream.p_selected_area->i_seek = NO_SEEK;
318 if( p_input->stream.p_removed_es )
320 input_UnselectES( p_input, p_input->stream.p_removed_es );
321 p_input->stream.p_removed_es = NULL;
324 if( p_input->stream.p_newly_selected_es )
326 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
327 p_input->stream.p_newly_selected_es = NULL;
330 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
332 if( p_input->stream.b_new_mute )
334 input_EscapeAudioDiscontinuity( p_input );
337 vlc_mutex_lock( &p_input->stream.control.control_lock );
338 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
339 vlc_mutex_unlock( &p_input->stream.control.control_lock );
341 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
344 vlc_mutex_unlock( &p_input->stream.stream_lock );
346 /* Read and demultiplex some data. */
347 i_count = p_input->pf_demux( p_input );
349 if( i_count == 0 && p_input->stream.b_seekable )
351 /* End of file - we do not set b_die because only the
352 * playlist is allowed to do so. */
353 msg_Info( p_input, "EOF reached" );
356 else if( i_count < 0 )
358 p_input->b_error = 1;
362 if( p_input->b_error || p_input->b_eof )
364 ErrorThread( p_input );
367 EndThread( p_input );
372 /*****************************************************************************
373 * InitThread: init the input Thread
374 *****************************************************************************/
375 static int InitThread( input_thread_t * p_input )
377 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
378 char * psz_parser = p_input->psz_source;
380 /* Skip the plug-in names */
381 while( *psz_parser && *psz_parser != ':' )
386 if( psz_parser - p_input->psz_source == 1 )
388 msg_Warn( p_input, "drive letter %c: found in source string",
389 p_input->psz_source ) ;
396 p_input->psz_access = p_input->psz_demux = "";
397 p_input->psz_name = p_input->psz_source;
401 *psz_parser++ = '\0';
403 /* let's skip '//' */
404 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
409 p_input->psz_name = psz_parser ;
411 /* Come back to parse the access and demux plug-ins */
412 psz_parser = p_input->psz_source;
417 p_input->psz_access = "";
419 else if( *psz_parser == '/' )
422 p_input->psz_access = "";
427 p_input->psz_access = psz_parser;
429 while( *psz_parser && *psz_parser != '/' )
434 if( *psz_parser == '/' )
436 *psz_parser++ = '\0';
443 p_input->psz_demux = "";
447 p_input->psz_demux = psz_parser;
451 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
452 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
454 if( input_AccessInit( p_input ) == -1 )
459 /* Find and open appropriate access module */
460 p_input->p_access = module_Need( p_input, "access",
461 p_input->psz_access );
463 if( p_input->p_access == NULL )
465 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
466 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
470 /* Waiting for stream. */
473 p_input->i_bufsize = p_input->i_mtu;
477 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
480 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
482 while( !input_FillBuffer( p_input ) )
484 if( p_input->b_die || p_input->b_error || p_input->b_eof )
486 module_Unneed( p_input, p_input->p_access );
492 /* Find and open appropriate demux module */
493 p_input->p_demux = module_Need( p_input, "demux",
494 p_input->psz_demux );
496 if( p_input->p_demux== NULL )
498 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
499 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
500 module_Unneed( p_input, p_input->p_access );
504 /* Initialize optional stream output. */
505 psz_parser = config_GetPsz( p_input, "sout" );
506 if ( psz_parser != NULL )
509 (p_input->stream.p_sout = sout_NewInstance( p_input, psz_parser ))
512 msg_Err( p_input, "cannot start stream output instance, aborting" );
514 module_Unneed( p_input, p_input->p_access );
515 module_Unneed( p_input, p_input->p_demux );
525 /*****************************************************************************
526 * ErrorThread: RunThread() error loop
527 *****************************************************************************
528 * This function is called when an error occured during thread main's loop.
529 *****************************************************************************/
530 static void ErrorThread( input_thread_t *p_input )
532 while( !p_input->b_die )
535 msleep( INPUT_IDLE_SLEEP );
539 /*****************************************************************************
540 * EndThread: end the input thread
541 *****************************************************************************/
542 static void EndThread( input_thread_t * p_input )
544 #ifdef HAVE_SYS_TIMES_H
545 /* Display statistics */
546 struct tms cpu_usage;
549 msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
550 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
552 msg_Dbg( p_input, "%d loops", p_input->c_loops );
555 /* Free info structures */
556 msg_Dbg( p_input, "freeing info structures...");
557 input_DelInfo( p_input );
559 input_DumpStream( p_input );
561 /* Free all ES and destroy all decoder threads */
562 input_EndStream( p_input );
564 /* Close optional stream output instance */
565 if ( p_input->stream.p_sout != NULL )
567 sout_DeleteInstance( p_input->stream.p_sout );
570 /* Free demultiplexer's data */
571 module_Unneed( p_input, p_input->p_demux );
573 /* Close the access plug-in */
574 module_Unneed( p_input, p_input->p_access );
576 input_AccessEnd( p_input );
578 free( p_input->psz_source );
580 /* Tell we're dead */