1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: input.c,v 1.209 2002/07/31 20:56:52 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 "interface.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int RunThread ( input_thread_t *p_input );
57 static int InitThread ( input_thread_t *p_input );
58 static void ErrorThread ( input_thread_t *p_input );
59 static void EndThread ( input_thread_t *p_input );
61 /*****************************************************************************
62 * input_CreateThread: creates a new input thread
63 *****************************************************************************
64 * This function creates a new input, and returns a pointer
65 * to its description. On error, it returns NULL.
66 * If pi_status is NULL, then the function will block until the thread is ready.
67 * If not, it will be updated using one of the THREAD_* constants.
68 *****************************************************************************/
69 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
70 playlist_item_t *p_item, int *pi_status )
72 input_thread_t * p_input; /* thread descriptor */
73 input_info_category_t * p_info;
75 /* Allocate descriptor */
76 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
79 msg_Err( p_parent, "out of memory" );
83 /* Initialize thread properties */
87 p_input->psz_source = strdup( p_item->psz_name );
90 p_input->p_demux = NULL;
93 p_input->p_access = NULL;
95 p_input->i_bufsize = 0;
98 /* Initialize statistics */
100 p_input->stream.c_packets_read = 0;
101 p_input->stream.c_packets_trashed = 0;
104 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
105 vlc_cond_init( p_input, &p_input->stream.stream_wait );
106 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
108 /* Initialize stream description */
109 p_input->stream.b_changed = 0;
110 p_input->stream.i_es_number = 0;
111 p_input->stream.i_selected_es_number = 0;
112 p_input->stream.i_pgrm_number = 0;
113 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
114 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
115 p_input->stream.i_mux_rate = 0;
116 p_input->stream.b_seekable = 0;
118 /* no stream, no program, no area, no es */
119 p_input->stream.p_new_program = NULL;
121 p_input->stream.i_area_nb = 0;
122 p_input->stream.pp_areas = NULL;
123 p_input->stream.p_selected_area = NULL;
124 p_input->stream.p_new_area = NULL;
126 p_input->stream.pp_selected_es = NULL;
127 p_input->stream.p_removed_es = NULL;
128 p_input->stream.p_newly_selected_es = NULL;
130 /* By default there is one area in a stream */
131 input_AddArea( p_input );
132 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
134 /* Initialize stream control properties. */
135 p_input->stream.control.i_status = PLAYING_S;
136 p_input->stream.control.i_rate = DEFAULT_RATE;
137 p_input->stream.control.b_mute = 0;
138 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
140 /* Initialize input info */
141 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
142 if( !p_input->stream.p_info )
144 msg_Err( p_input, "No memory!" );
147 p_input->stream.p_info->psz_name = strdup("General") ;
148 p_input->stream.p_info->p_info = NULL;
149 p_input->stream.p_info->p_next = NULL;
151 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
153 p_info = input_InfoCategory( p_input, "General");
154 input_AddInfo( p_info, "Playlist item", p_input->psz_source );
155 vlc_object_attach( p_input, p_parent );
157 /* Create thread and wait for its readiness. */
158 if( vlc_thread_create( p_input, "input", RunThread, VLC_TRUE ) )
160 msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
168 /*****************************************************************************
169 * input_StopThread: mark an input thread as zombie
170 *****************************************************************************
171 * This function should not return until the thread is effectively cancelled.
172 *****************************************************************************/
173 void input_StopThread( input_thread_t *p_input )
175 /* Make the thread exit from a possible vlc_cond_wait() */
176 vlc_mutex_lock( &p_input->stream.stream_lock );
177 /* Request thread destruction */
180 vlc_cond_signal( &p_input->stream.stream_wait );
181 vlc_mutex_unlock( &p_input->stream.stream_lock );
184 /*****************************************************************************
185 * input_DestroyThread: mark an input thread as zombie
186 *****************************************************************************
187 * This function should not return until the thread is effectively cancelled.
188 *****************************************************************************/
189 void input_DestroyThread( input_thread_t *p_input )
191 /* Join the thread */
192 vlc_thread_join( p_input );
194 /* Destroy Mutex locks */
195 vlc_mutex_destroy( &p_input->stream.control.control_lock );
196 vlc_cond_destroy( &p_input->stream.stream_wait );
197 vlc_mutex_destroy( &p_input->stream.stream_lock );
200 /*****************************************************************************
201 * RunThread: main thread loop
202 *****************************************************************************
203 * Thread in charge of processing the network packets and demultiplexing.
204 *****************************************************************************/
205 static int RunThread( input_thread_t *p_input )
207 /* Signal right now, otherwise we'll get stuck in a peek */
208 vlc_thread_ready( p_input );
210 if( InitThread( p_input ) )
212 /* If we failed, wait before we are killed, and exit */
213 p_input->b_error = 1;
214 ErrorThread( p_input );
219 /* initialization is complete */
220 vlc_mutex_lock( &p_input->stream.stream_lock );
221 p_input->stream.b_changed = 1;
222 vlc_mutex_unlock( &p_input->stream.stream_lock );
224 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
230 vlc_mutex_lock( &p_input->stream.stream_lock );
232 if( p_input->stream.p_new_program )
234 if( p_input->pf_set_program != NULL )
237 /* Reinitialize buffer manager. */
238 input_AccessReinit( p_input );
240 p_input->pf_set_program( p_input,
241 p_input->stream.p_new_program );
243 /* Escape all decoders for the stream discontinuity they
245 input_EscapeDiscontinuity( p_input );
247 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
249 pgrm_descriptor_t * p_pgrm
250 = p_input->stream.pp_programs[i];
252 /* Reinitialize synchro. */
253 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
256 p_input->stream.p_new_program = NULL;
259 if( p_input->stream.p_new_area )
261 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
263 input_AccessReinit( p_input );
265 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
267 /* Escape all decoders for the stream discontinuity they
269 input_EscapeDiscontinuity( p_input );
271 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
273 pgrm_descriptor_t * p_pgrm
274 = p_input->stream.pp_programs[i];
276 /* Reinitialize synchro. */
277 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
280 p_input->stream.p_new_area = NULL;
283 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
285 if( p_input->stream.b_seekable
286 && p_input->pf_seek != NULL )
290 /* Reinitialize buffer manager. */
291 input_AccessReinit( p_input );
293 i_new_pos = p_input->stream.p_selected_area->i_seek;
294 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 p_input->pf_seek( p_input, i_new_pos );
296 vlc_mutex_lock( &p_input->stream.stream_lock );
298 /* Escape all decoders for the stream discontinuity they
300 input_EscapeDiscontinuity( p_input );
302 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
304 pgrm_descriptor_t * p_pgrm
305 = p_input->stream.pp_programs[i];
307 /* Reinitialize synchro. */
308 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
311 p_input->stream.p_selected_area->i_seek = NO_SEEK;
314 if( p_input->stream.p_removed_es )
316 input_UnselectES( p_input, p_input->stream.p_removed_es );
317 p_input->stream.p_removed_es = NULL;
320 if( p_input->stream.p_newly_selected_es )
322 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
323 p_input->stream.p_newly_selected_es = NULL;
326 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
328 if( p_input->stream.b_new_mute )
330 input_EscapeAudioDiscontinuity( p_input );
333 vlc_mutex_lock( &p_input->stream.control.control_lock );
334 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
335 vlc_mutex_unlock( &p_input->stream.control.control_lock );
337 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
340 vlc_mutex_unlock( &p_input->stream.stream_lock );
342 /* Read and demultiplex some data. */
343 i_count = p_input->pf_demux( p_input );
345 if( i_count == 0 && p_input->stream.b_seekable )
347 /* End of file - we do not set b_die because only the
348 * playlist is allowed to do so. */
349 msg_Info( p_input, "EOF reached" );
352 else if( i_count < 0 )
354 p_input->b_error = 1;
358 if( p_input->b_error || p_input->b_eof )
360 ErrorThread( p_input );
363 EndThread( p_input );
368 /*****************************************************************************
369 * InitThread: init the input Thread
370 *****************************************************************************/
371 static int InitThread( input_thread_t * p_input )
373 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
374 char * psz_parser = p_input->psz_source;
376 /* Skip the plug-in names */
377 while( *psz_parser && *psz_parser != ':' )
382 if( psz_parser - p_input->psz_source == 1 )
384 msg_Warn( p_input, "drive letter %c: found in source string",
385 p_input->psz_source ) ;
392 p_input->psz_access = p_input->psz_demux = "";
393 p_input->psz_name = p_input->psz_source;
397 *psz_parser++ = '\0';
399 /* let's skip '//' */
400 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
405 p_input->psz_name = psz_parser ;
407 /* Come back to parse the access and demux plug-ins */
408 psz_parser = p_input->psz_source;
413 p_input->psz_access = "";
415 else if( *psz_parser == '/' )
418 p_input->psz_access = "";
423 p_input->psz_access = psz_parser;
425 while( *psz_parser && *psz_parser != '/' )
430 if( *psz_parser == '/' )
432 *psz_parser++ = '\0';
439 p_input->psz_demux = "";
443 p_input->psz_demux = psz_parser;
447 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
448 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
450 if( input_AccessInit( p_input ) == -1 )
455 /* Find and open appropriate access module */
456 p_input->p_access = module_Need( p_input, "access",
457 p_input->psz_access );
459 if( p_input->p_access == NULL )
461 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
462 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
466 /* Waiting for stream. */
469 p_input->i_bufsize = p_input->i_mtu;
473 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
476 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
478 while( !input_FillBuffer( p_input ) )
480 if( p_input->b_die || p_input->b_error || p_input->b_eof )
482 module_Unneed( p_input, p_input->p_access );
488 /* Find and open appropriate demux module */
489 p_input->p_demux = module_Need( p_input, "demux",
490 p_input->psz_demux );
492 if( p_input->p_demux== NULL )
494 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
495 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
496 module_Unneed( p_input, p_input->p_access );
503 /*****************************************************************************
504 * ErrorThread: RunThread() error loop
505 *****************************************************************************
506 * This function is called when an error occured during thread main's loop.
507 *****************************************************************************/
508 static void ErrorThread( input_thread_t *p_input )
510 while( !p_input->b_die )
513 msleep( INPUT_IDLE_SLEEP );
517 /*****************************************************************************
518 * EndThread: end the input thread
519 *****************************************************************************/
520 static void EndThread( input_thread_t * p_input )
522 #ifdef HAVE_SYS_TIMES_H
523 /* Display statistics */
524 struct tms cpu_usage;
527 msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
528 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
530 msg_Dbg( p_input, "%d loops", p_input->c_loops );
533 /* Free info structures */
534 msg_Dbg( p_input, "freeing info structures...");
535 input_DelInfo( p_input );
537 input_DumpStream( p_input );
539 /* Free all ES and destroy all decoder threads */
540 input_EndStream( p_input );
542 /* Free demultiplexer's data */
543 module_Unneed( p_input, p_input->p_demux );
545 /* Close the access plug-in */
546 module_Unneed( p_input, p_input->p_access );
548 input_AccessEnd( p_input );
550 free( p_input->psz_source );
552 /* Tell we're dead */