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.207 2002/07/24 23:11:55 massiot 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_module = NULL;
91 p_input->pf_init = NULL;
92 p_input->pf_end = NULL;
93 p_input->pf_demux = NULL;
94 p_input->pf_rewind = NULL;
97 p_input->p_access_module = NULL;
98 p_input->pf_open = NULL;
99 p_input->pf_close = NULL;
100 p_input->pf_read = NULL;
101 p_input->pf_seek = NULL;
102 p_input->pf_set_area = NULL;
103 p_input->pf_set_program = NULL;
105 p_input->i_bufsize = 0;
108 /* Initialize statistics */
109 p_input->c_loops = 0;
110 p_input->stream.c_packets_read = 0;
111 p_input->stream.c_packets_trashed = 0;
114 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
115 vlc_cond_init( p_input, &p_input->stream.stream_wait );
116 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
118 /* Initialize stream description */
119 p_input->stream.b_changed = 0;
120 p_input->stream.i_es_number = 0;
121 p_input->stream.i_selected_es_number = 0;
122 p_input->stream.i_pgrm_number = 0;
123 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
124 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
125 p_input->stream.i_mux_rate = 0;
126 p_input->stream.b_seekable = 0;
128 /* no stream, no program, no area, no es */
129 p_input->stream.p_new_program = NULL;
131 p_input->stream.i_area_nb = 0;
132 p_input->stream.pp_areas = NULL;
133 p_input->stream.p_selected_area = NULL;
134 p_input->stream.p_new_area = NULL;
136 p_input->stream.pp_selected_es = NULL;
137 p_input->stream.p_removed_es = NULL;
138 p_input->stream.p_newly_selected_es = NULL;
140 /* By default there is one area in a stream */
141 input_AddArea( p_input );
142 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
144 /* Initialize stream control properties. */
145 p_input->stream.control.i_status = PLAYING_S;
146 p_input->stream.control.i_rate = DEFAULT_RATE;
147 p_input->stream.control.b_mute = 0;
148 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
149 p_input->stream.control.i_smp = config_GetInt( p_input, "vdec-smp" );
151 /* Initialize input info */
152 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
153 if( !p_input->stream.p_info )
155 msg_Err( p_input, "No memory!" );
158 p_input->stream.p_info->psz_name = strdup("General") ;
159 p_input->stream.p_info->p_info = NULL;
160 p_input->stream.p_info->p_next = NULL;
163 msg_Dbg( p_input, "finding category \"hepp\"");
164 p_info = input_InfoCategory( p_input, "hepp" );
165 msg_Dbg( p_input, "adding testkey/testval");
166 input_AddInfo( p_info, "testkey", "testval");
169 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
171 p_info = input_InfoCategory( p_input, "General");
172 input_AddInfo( p_info, "Playlist item", p_input->psz_source );
173 vlc_object_attach( p_input, p_parent );
175 /* Create thread and wait for its readiness. */
176 if( vlc_thread_create( p_input, "input", RunThread, VLC_TRUE ) )
178 msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
186 /*****************************************************************************
187 * input_StopThread: mark an input thread as zombie
188 *****************************************************************************
189 * This function should not return until the thread is effectively cancelled.
190 *****************************************************************************/
191 void input_StopThread( input_thread_t *p_input )
193 /* Make the thread exit from a possible vlc_cond_wait() */
194 vlc_mutex_lock( &p_input->stream.stream_lock );
195 /* Request thread destruction */
198 vlc_cond_signal( &p_input->stream.stream_wait );
199 vlc_mutex_unlock( &p_input->stream.stream_lock );
202 /*****************************************************************************
203 * input_DestroyThread: mark an input thread as zombie
204 *****************************************************************************
205 * This function should not return until the thread is effectively cancelled.
206 *****************************************************************************/
207 void input_DestroyThread( input_thread_t *p_input )
209 /* Join the thread */
210 vlc_thread_join( p_input );
212 /* Destroy Mutex locks */
213 vlc_mutex_destroy( &p_input->stream.control.control_lock );
214 vlc_cond_destroy( &p_input->stream.stream_wait );
215 vlc_mutex_destroy( &p_input->stream.stream_lock );
218 /*****************************************************************************
219 * RunThread: main thread loop
220 *****************************************************************************
221 * Thread in charge of processing the network packets and demultiplexing.
222 *****************************************************************************/
223 static int RunThread( input_thread_t *p_input )
225 /* Signal right now, otherwise we'll get stuck in a peek */
226 vlc_thread_ready( p_input );
228 if( InitThread( p_input ) )
230 /* If we failed, wait before we are killed, and exit */
231 p_input->b_error = 1;
232 ErrorThread( p_input );
237 /* initialization is complete */
238 vlc_mutex_lock( &p_input->stream.stream_lock );
239 p_input->stream.b_changed = 1;
240 vlc_mutex_unlock( &p_input->stream.stream_lock );
242 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
248 vlc_mutex_lock( &p_input->stream.stream_lock );
250 if( p_input->stream.p_new_program )
252 if( p_input->pf_set_program != NULL )
255 /* Reinitialize buffer manager. */
256 input_AccessReinit( p_input );
258 p_input->pf_set_program( p_input,
259 p_input->stream.p_new_program );
261 /* Escape all decoders for the stream discontinuity they
263 input_EscapeDiscontinuity( p_input );
265 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
267 pgrm_descriptor_t * p_pgrm
268 = p_input->stream.pp_programs[i];
270 /* Reinitialize synchro. */
271 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
274 p_input->stream.p_new_program = NULL;
277 if( p_input->stream.p_new_area )
279 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
281 input_AccessReinit( p_input );
283 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
285 /* Escape all decoders for the stream discontinuity they
287 input_EscapeDiscontinuity( p_input );
289 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
291 pgrm_descriptor_t * p_pgrm
292 = p_input->stream.pp_programs[i];
294 /* Reinitialize synchro. */
295 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
298 p_input->stream.p_new_area = NULL;
301 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
303 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
307 /* Reinitialize buffer manager. */
308 input_AccessReinit( p_input );
310 i_new_pos = p_input->stream.p_selected_area->i_seek;
311 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 p_input->pf_seek( p_input, i_new_pos );
313 vlc_mutex_lock( &p_input->stream.stream_lock );
315 /* Escape all decoders for the stream discontinuity they
317 input_EscapeDiscontinuity( p_input );
319 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
321 pgrm_descriptor_t * p_pgrm
322 = p_input->stream.pp_programs[i];
324 /* Reinitialize synchro. */
325 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
328 p_input->stream.p_selected_area->i_seek = NO_SEEK;
331 if( p_input->stream.p_removed_es )
333 input_UnselectES( p_input, p_input->stream.p_removed_es );
334 p_input->stream.p_removed_es = NULL;
337 if( p_input->stream.p_newly_selected_es )
339 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
340 p_input->stream.p_newly_selected_es = NULL;
343 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
345 if( p_input->stream.b_new_mute )
347 input_EscapeAudioDiscontinuity( p_input );
350 vlc_mutex_lock( &p_input->stream.control.control_lock );
351 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
352 vlc_mutex_unlock( &p_input->stream.control.control_lock );
354 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
357 vlc_mutex_unlock( &p_input->stream.stream_lock );
359 /* Read and demultiplex some data. */
360 i_count = p_input->pf_demux( p_input );
362 if( i_count == 0 && p_input->stream.b_seekable )
364 /* End of file - we do not set b_die because only the
365 * interface is allowed to do so. */
366 msg_Info( p_input, "EOF reached" );
369 else if( i_count < 0 )
371 p_input->b_error = 1;
375 if( p_input->b_error || p_input->b_eof )
377 ErrorThread( p_input );
380 EndThread( p_input );
385 /*****************************************************************************
386 * InitThread: init the input Thread
387 *****************************************************************************/
388 static int InitThread( input_thread_t * p_input )
390 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
391 char * psz_parser = p_input->psz_source;
393 /* Skip the plug-in names */
394 while( *psz_parser && *psz_parser != ':' )
399 if( psz_parser - p_input->psz_source == 1 )
401 msg_Warn( p_input, "drive letter %c: found in source string",
402 p_input->psz_source ) ;
409 p_input->psz_access = p_input->psz_demux = "";
410 p_input->psz_name = p_input->psz_source;
414 *psz_parser++ = '\0';
416 /* let's skip '//' */
417 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
422 p_input->psz_name = psz_parser ;
424 /* Come back to parse the access and demux plug-ins */
425 psz_parser = p_input->psz_source;
430 p_input->psz_access = "";
432 else if( *psz_parser == '/' )
435 p_input->psz_access = "";
440 p_input->psz_access = psz_parser;
442 while( *psz_parser && *psz_parser != '/' )
447 if( *psz_parser == '/' )
449 *psz_parser++ = '\0';
456 p_input->psz_demux = "";
460 p_input->psz_demux = psz_parser;
464 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
465 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
467 if( input_AccessInit( p_input ) == -1 )
472 /* Find and open appropriate access module */
473 p_input->p_access_module =
474 module_Need( p_input, MODULE_CAPABILITY_ACCESS,
475 p_input->psz_access, (void *)p_input );
477 if( p_input->p_access_module == NULL )
479 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
480 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
484 #define f p_input->p_access_module->p_functions->access.functions.access
485 p_input->pf_open = f.pf_open;
486 p_input->pf_close = f.pf_close;
487 p_input->pf_read = f.pf_read;
488 p_input->pf_set_area = f.pf_set_area;
489 p_input->pf_set_program = f.pf_set_program;
490 p_input->pf_seek = f.pf_seek;
493 /* Waiting for stream. */
496 p_input->i_bufsize = p_input->i_mtu;
500 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
503 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
505 while( !input_FillBuffer( p_input ) )
507 if( p_input->b_die || p_input->b_error || p_input->b_eof )
509 module_Unneed( p_input->p_access_module );
515 /* Find and open appropriate demux module */
516 p_input->p_demux_module =
517 module_Need( p_input, MODULE_CAPABILITY_DEMUX,
518 p_input->psz_demux, (void *)p_input );
520 if( p_input->p_demux_module == NULL )
522 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
523 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
524 module_Unneed( p_input->p_access_module );
528 #define f p_input->p_demux_module->p_functions->demux.functions.demux
529 p_input->pf_init = f.pf_init;
530 p_input->pf_end = f.pf_end;
531 p_input->pf_demux = f.pf_demux;
532 p_input->pf_rewind = f.pf_rewind;
538 /*****************************************************************************
539 * ErrorThread: RunThread() error loop
540 *****************************************************************************
541 * This function is called when an error occured during thread main's loop.
542 *****************************************************************************/
543 static void ErrorThread( input_thread_t *p_input )
545 while( !p_input->b_die )
548 msleep( INPUT_IDLE_SLEEP );
552 /*****************************************************************************
553 * EndThread: end the input thread
554 *****************************************************************************/
555 static void EndThread( input_thread_t * p_input )
557 #ifdef HAVE_SYS_TIMES_H
558 /* Display statistics */
559 struct tms cpu_usage;
562 msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
563 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
565 msg_Dbg( p_input, "%d loops", p_input->c_loops );
568 /* Free info structures */
569 msg_Dbg( p_input, "freeing info structures...");
570 input_DelInfo( p_input );
572 input_DumpStream( p_input );
574 /* Free all ES and destroy all decoder threads */
575 input_EndStream( p_input );
577 /* Free demultiplexer's data */
578 p_input->pf_end( p_input );
579 module_Unneed( p_input->p_demux_module );
581 /* Close the access plug-in */
582 p_input->pf_close( p_input );
583 module_Unneed( p_input->p_access_module );
585 input_AccessEnd( p_input );
587 free( p_input->psz_source );
589 /* Tell we're dead */