]> git.sesse.net Git - vlc/blob - src/input/input.c
-Fixed the lock segfault bug with some compilers
[vlc] / src / input / input.c
1 /*****************************************************************************
2  * input.c: input thread
3  * Read an MPEG2 stream, demultiplex and parse it before sending it to
4  * decoders.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  * $Id: input.c,v 1.96 2001/04/10 17:47:05 stef Exp $
8  *
9  * Authors: Christophe Massiot <massiot@via.ecp.fr>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38
39 /* Network functions */
40
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>
48 #endif
49
50 #ifdef STATS
51 #   include <sys/times.h>
52 #endif
53
54 #include "config.h"
55 #include "common.h"
56 #include "threads.h"
57 #include "mtime.h"
58 #include "netutils.h"
59 #include "modules.h"
60
61 #include "intf_msg.h"
62 #include "intf_playlist.h"
63
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
66 #include "input_ext-dec.h"
67
68 #include "input.h"
69 #include "interface.h"
70
71 #include "main.h"
72
73  /* #include <netutils.h> */
74
75
76 /*****************************************************************************
77  * Local prototypes
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 );
84
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 )
94 {
95     input_thread_t *    p_input;                        /* thread descriptor */
96     int                 i_status;                           /* thread status */
97
98     /* Allocate descriptor */
99     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
100     if( p_input == NULL )
101     {
102         intf_ErrMsg( "input error: can't allocate input thread (%s)",
103                      strerror(errno) );
104         return( NULL );
105     }
106
107     /* Packets read once */
108     p_input->i_read_once = INPUT_READ_ONCE;
109
110     /* Initialize thread properties */
111     p_input->b_die              = 0;
112     p_input->b_error            = 0;
113     p_input->b_eof              = 0;
114
115     /* Set target */
116     p_input->p_source           = p_item->psz_name;
117
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;
121
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;
128
129     p_input->stream.i_area_nb = 0;
130     p_input->stream.pp_areas = NULL;
131     /* By default there is one areas in a stream */
132     input_AddArea( p_input );
133     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
134     p_input->stream.p_selected_area->i_seek = NO_SEEK;
135
136     /* Initialize stream control properties. */
137     p_input->stream.control.i_status = PLAYING_S;
138     p_input->stream.control.i_rate = DEFAULT_RATE;
139     p_input->stream.control.b_mute = 0;
140     p_input->stream.control.b_bw = 0;
141
142     /* Initialize default settings for spawned decoders */
143     p_input->p_default_aout = p_main->p_aout;
144     p_input->p_default_vout = p_main->p_vout;
145
146     /* Create thread and set locks. */
147     vlc_mutex_init( &p_input->stream.stream_lock );
148     vlc_cond_init( &p_input->stream.stream_wait );
149     vlc_mutex_init( &p_input->stream.control.control_lock );
150     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
151                            (void *) p_input ) )
152     {
153         intf_ErrMsg( "input error: can't create input thread (%s)",
154                      strerror(errno) );
155         free( p_input );
156         return( NULL );
157     }
158
159     /* If status is NULL, wait until the thread is created */
160     if( pi_status == NULL )
161     {
162         do
163         {
164             msleep( THREAD_SLEEP );
165         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
166                 && (i_status != THREAD_FATAL) );
167         if( i_status != THREAD_READY )
168         {
169             return( NULL );
170         }
171     }
172     return( p_input );
173 }
174
175 /*****************************************************************************
176  * input_DestroyThread: mark an input thread as zombie
177  *****************************************************************************
178  * This function should not return until the thread is effectively cancelled.
179  *****************************************************************************/
180 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
181 {
182     int         i_status;                                   /* thread status */
183
184     /* Set status */
185     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
186     *p_input->pi_status = THREAD_DESTROY;
187
188     /* Request thread destruction */
189     p_input->b_die = 1;
190
191     /* Make the thread exit of an eventual vlc_cond_wait() */
192     vlc_mutex_lock( &p_input->stream.stream_lock );
193     vlc_cond_signal( &p_input->stream.stream_wait );
194     vlc_mutex_unlock( &p_input->stream.stream_lock );
195
196     /* If status is NULL, wait until thread has been destroyed */
197     if( pi_status == NULL )
198     {
199         do
200         {
201             msleep( THREAD_SLEEP );
202         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
203                   && (i_status != THREAD_FATAL) );
204     }
205 }
206
207 /*****************************************************************************
208  * RunThread: main thread loop
209  *****************************************************************************
210  * Thread in charge of processing the network packets and demultiplexing.
211  *****************************************************************************/
212 static void RunThread( input_thread_t *p_input )
213 {
214     int                     i_error, i;
215
216     if( InitThread( p_input ) )
217     {
218
219         /* If we failed, wait before we are killed, and exit */
220         *p_input->pi_status = THREAD_ERROR;
221         p_input->b_error = 1;
222         ErrorThread( p_input );
223         DestroyThread( p_input );
224         return;
225     }
226
227     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
228     {
229         data_packet_t *         pp_packets[p_input->i_read_once];
230
231 #ifdef STATS
232         p_input->c_loops++;
233 #endif
234
235         vlc_mutex_lock( &p_input->stream.stream_lock );
236
237         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
238         {
239             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
240             {
241                 p_input->pf_seek( p_input,
242                                   p_input->stream.p_selected_area->i_seek );
243
244                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
245                 {
246                     pgrm_descriptor_t * p_pgrm
247                                             = p_input->stream.pp_programs[i];
248                     /* Escape all decoders for the stream discontinuity they
249                      * will encounter. */
250                     input_EscapeDiscontinuity( p_input, p_pgrm );
251
252                     /* Reinitialize synchro. */
253                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
254                 }
255             }
256             p_input->stream.p_selected_area->i_seek = NO_SEEK;
257         }
258
259         vlc_mutex_unlock( &p_input->stream.stream_lock );
260
261         i_error = p_input->pf_read( p_input, pp_packets );
262
263         /* Demultiplex read packets. */
264         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
265         {
266             p_input->pf_demux( p_input, pp_packets[i] );
267         }
268
269         if( i_error )
270         {
271             if( i_error == 1 )
272             {
273                 /* End of file - we do not set b_die because only the
274                  * interface is allowed to do so. */
275                 intf_WarnMsg( 1, "End of file reached" );
276                 p_input->b_eof = 1;
277             }
278             else
279             {
280                 p_input->b_error = 1;
281             }
282         }
283     }
284
285     if( p_input->b_error || p_input->b_eof )
286     {
287         ErrorThread( p_input );
288     }
289
290     EndThread( p_input );
291
292     DestroyThread( p_input );
293
294     intf_DbgMsg("Thread end");
295 }
296
297 /*****************************************************************************
298  * InitThread: init the input Thread
299  *****************************************************************************/
300 static int InitThread( input_thread_t * p_input )
301 {
302
303 #ifdef STATS
304     /* Initialize statistics */
305     p_input->c_loops                    = 0;
306     p_input->c_bytes                    = 0;
307     p_input->c_payload_bytes            = 0;
308     p_input->c_packets_read             = 0;
309     p_input->c_packets_trashed          = 0;
310 #endif
311
312     p_input->p_input_module = module_Need( p_main->p_bank,
313                                            MODULE_CAPABILITY_INPUT,
314                                            (probedata_t *)p_input );
315
316     if( p_input->p_input_module == NULL )
317     {
318         intf_ErrMsg( "input error: no suitable input module for `%s'",
319                      p_input->p_source );
320         return( -1 );
321     }
322
323 #define f p_input->p_input_module->p_functions->input.functions.input
324     p_input->pf_init          = f.pf_init;
325     p_input->pf_open          = f.pf_open;
326     p_input->pf_close         = f.pf_close;
327     p_input->pf_end           = f.pf_end;
328     p_input->pf_read          = f.pf_read;
329     p_input->pf_set_area      = f.pf_set_area;
330     p_input->pf_demux         = f.pf_demux;
331     p_input->pf_new_packet    = f.pf_new_packet;
332     p_input->pf_new_pes       = f.pf_new_pes;
333     p_input->pf_delete_packet = f.pf_delete_packet;
334     p_input->pf_delete_pes    = f.pf_delete_pes;
335     p_input->pf_rewind        = f.pf_rewind;
336     p_input->pf_seek          = f.pf_seek;
337 #undef f
338     p_input->pf_open( p_input );
339
340     if( p_input->b_error )
341     {
342         /* We barfed -- exit nicely */
343         p_input->pf_close( p_input );
344         module_Unneed( p_main->p_bank, p_input->p_input_module );
345         return( -1 );
346     }
347
348     p_input->pf_init( p_input );
349
350     if( p_input->b_error )
351     {
352         /* We barfed -- exit nicely */
353         p_input->pf_close( p_input );
354         module_Unneed( p_main->p_bank, p_input->p_input_module );
355         return( -1 );
356     }
357
358     *p_input->pi_status = THREAD_READY;
359
360     return( 0 );
361 }
362
363 /*****************************************************************************
364  * ErrorThread: RunThread() error loop
365  *****************************************************************************
366  * This function is called when an error occured during thread main's loop.
367  *****************************************************************************/
368 static void ErrorThread( input_thread_t *p_input )
369 {
370     while( !p_input->b_die )
371     {
372         /* Sleep a while */
373         msleep( INPUT_IDLE_SLEEP );
374     }
375 }
376
377 /*****************************************************************************
378  * EndThread: end the input thread
379  *****************************************************************************/
380 static void EndThread( input_thread_t * p_input )
381 {
382     int *       pi_status;                                  /* thread status */
383
384     /* Store status */
385     pi_status = p_input->pi_status;
386     *pi_status = THREAD_END;
387
388 #ifdef STATS
389     {
390         struct tms cpu_usage;
391         times( &cpu_usage );
392
393         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
394                  cpu_usage.tms_utime, cpu_usage.tms_stime);
395     }
396 #endif
397
398     /* Free all ES and destroy all decoder threads */
399     input_EndStream( p_input );
400
401     /* Free demultiplexer's data */
402     p_input->pf_end( p_input );
403
404     /* Close stream */
405     p_input->pf_close( p_input );
406
407     /* Release modules */
408     module_Unneed( p_main->p_bank, p_input->p_input_module );
409
410 }
411
412 /*****************************************************************************
413  * DestroyThread: destroy the input thread
414  *****************************************************************************/
415 static void DestroyThread( input_thread_t * p_input )
416 {
417     int *       pi_status;                                  /* thread status */
418
419     /* Store status */
420     pi_status = p_input->pi_status;
421
422     /* Destroy Mutex locks */
423     vlc_mutex_destroy( &p_input->stream.control.control_lock );
424     vlc_mutex_destroy( &p_input->stream.stream_lock );
425     
426     /* Free input structure */
427     free( p_input );
428
429     /* Update status */
430     *pi_status = THREAD_OVER;
431 }
432
433 /*****************************************************************************
434  * input_FileOpen : open a file descriptor
435  *****************************************************************************/
436 void input_FileOpen( input_thread_t * p_input )
437 {
438     struct stat         stat_info;
439     int                 i_stat;
440
441     char *psz_name = p_input->p_source;
442
443     /* FIXME: this code ought to be in the plugin so that code can
444      * be shared with the *_Probe function */
445     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
446     {
447         int i_size = strlen( psz_name );
448
449         if( ( i_size > 4 )
450             && !strncasecmp( psz_name, "dvd:", 4 ) )
451         {
452             /* get rid of the 'dvd:' stuff and try again */
453             psz_name += 4;
454             i_stat = stat( psz_name, &stat_info );
455         }
456         else if( ( i_size > 5 )
457                  && !strncasecmp( psz_name, "file:", 5 ) )
458         {
459             /* get rid of the 'file:' stuff and try again */
460             psz_name += 5;
461             i_stat = stat( psz_name, &stat_info );
462         }
463
464         if( i_stat == (-1) )
465         {
466             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
467                          psz_name, strerror(errno));
468             p_input->b_error = 1;
469             return;
470         }
471     }
472
473     vlc_mutex_lock( &p_input->stream.stream_lock );
474
475     /* If we are here we can control the pace... */
476     p_input->stream.b_pace_control = 1;
477
478     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
479          || S_ISBLK(stat_info.st_mode) )
480     {
481         p_input->stream.b_seekable = 1;
482         p_input->stream.p_selected_area->i_size = stat_info.st_size;
483     }
484     else if( S_ISFIFO(stat_info.st_mode)
485 #ifndef SYS_BEOS
486              || S_ISSOCK(stat_info.st_mode)
487 #endif
488              )
489     {
490         p_input->stream.b_seekable = 0;
491         p_input->stream.p_selected_area->i_size = 0;
492     }
493     else
494     {
495         vlc_mutex_unlock( &p_input->stream.stream_lock );
496         intf_ErrMsg( "input error: unknown file type for `%s'",
497                      psz_name );
498         p_input->b_error = 1;
499         return;
500     }
501
502     p_input->stream.p_selected_area->i_tell = 0;
503     vlc_mutex_unlock( &p_input->stream.stream_lock );
504
505     intf_Msg( "input: opening %s", p_input->p_source );
506     if( (p_input->i_handle = open( psz_name,
507                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
508     {
509         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
510         p_input->b_error = 1;
511         return;
512     }
513
514 }
515
516 /*****************************************************************************
517  * input_FileClose : close a file descriptor
518  *****************************************************************************/
519 void input_FileClose( input_thread_t * p_input )
520 {
521     close( p_input->i_handle );
522
523     return;
524 }
525
526
527 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
528 /*****************************************************************************
529  * input_NetworkOpen : open a network socket 
530  *****************************************************************************/
531 void input_NetworkOpen( input_thread_t * p_input )
532 {
533  
534     int                 i_option_value, i_port;
535     struct sockaddr_in  s_socket;
536     boolean_t           b_broadcast;
537     
538     /* FIXME : we don't handle channels for the moment */
539     
540     /* Get the remote server */
541     if( p_input->p_source == NULL )
542     {
543         p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR, 
544                                                  INPUT_SERVER_DEFAULT );
545     }
546     
547     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
548      * protocol */
549     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
550     if( p_input->i_handle == -1 )
551     {
552         intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
553         p_input->b_error = 1;
554         return;
555     }
556
557     /* We may want to reuse an already used socket */
558     i_option_value = 1;
559     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
560                     &i_option_value,sizeof( i_option_value ) ) == -1 )
561     {
562         intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
563                     strerror(errno));
564         close( p_input->i_handle );
565         p_input->b_error = 1;
566         return;
567     }
568
569     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
570      * packet loss caused by scheduling problems */
571     i_option_value = 524288;
572     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
573                     sizeof( i_option_value ) ) == -1 )
574     {
575         intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)", 
576                     strerror(errno));
577         close( p_input->i_handle );
578         p_input->b_error = 1;
579         return;
580     }
581
582     /* Get details about what we are supposed to do */
583     b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
584     i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
585
586     /* TODO : here deal with channel stufs */
587     
588     /* Build the local socket */
589     if ( network_BuildLocalAddr( &s_socket, i_port, b_broadcast ) 
590          == -1 )
591     {
592         close( p_input->i_handle );
593         p_input->b_error = 1;
594         return;
595     }
596     
597     /* Bind it */
598     if( bind( p_input->i_handle, (struct sockaddr *)&s_socket, 
599               sizeof( s_socket ) ) < 0 )
600     {
601         intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
602         close( p_input->i_handle );
603         p_input->b_error = 1;
604         return;
605     }
606
607     /* Build socket for remote connection */
608     if ( network_BuildRemoteAddr( &s_socket, p_input->p_source ) 
609          == -1 )
610     {
611         close( p_input->i_handle );
612         p_input->b_error = 1;
613         return;
614     }
615
616     /* And connect it ... should we really connect ? */
617     if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
618                  sizeof( s_socket ) ) == (-1) )
619     {
620         intf_ErrMsg("NetworkOpen: can't connect socket" );
621         close( p_input->i_handle );
622         p_input->b_error = 1;
623         return;
624     }
625
626     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
627      * with the server. */
628     p_input->stream.b_pace_control = 1;
629     
630     return;
631 }
632
633 /*****************************************************************************
634  * input_NetworkClose : close a network socket
635  *****************************************************************************/
636 void input_NetworkClose( input_thread_t * p_input )
637 {
638     close( p_input->i_handle );
639     /* FIXME: deal with channels */
640 }
641 #endif