]> git.sesse.net Git - vlc/blob - src/input/input.c
- Fixed TS input ( a nasty dead lock had appeared 4 days ago )
[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.89 2001/03/07 00:18:46 henri 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 #include <netdb.h>                                             /* hostent ... */
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47
48 #ifdef STATS
49 #   include <sys/times.h>
50 #endif
51
52 #include "config.h"
53 #include "common.h"
54 #include "threads.h"
55 #include "mtime.h"
56 #include "modules.h"
57
58 #include "intf_msg.h"
59 #include "intf_plst.h"
60
61 #include "stream_control.h"
62 #include "input_ext-intf.h"
63 #include "input_ext-dec.h"
64
65 #include "input.h"
66 #include "interface.h"
67
68 #include "main.h"
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static void RunThread       ( input_thread_t *p_input );
74 static  int InitThread      ( input_thread_t *p_input );
75 static void ErrorThread     ( input_thread_t *p_input );
76 static void DestroyThread   ( input_thread_t *p_input );
77 static void EndThread       ( input_thread_t *p_input );
78
79 /*****************************************************************************
80  * input_CreateThread: creates a new input thread
81  *****************************************************************************
82  * This function creates a new input, and returns a pointer
83  * to its description. On error, it returns NULL.
84  * If pi_status is NULL, then the function will block until the thread is ready.
85  * If not, it will be updated using one of the THREAD_* constants.
86  *****************************************************************************/
87 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
88 {
89     input_thread_t *    p_input;                        /* thread descriptor */
90     int                 i_status;                           /* thread status */
91
92     /* Allocate descriptor */
93     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
94     if( p_input == NULL )
95     {
96         intf_ErrMsg( "input error: can't allocate input thread (%s)",
97                      strerror(errno) );
98         return( NULL );
99     }
100
101     /* Packets read once */
102     p_input->i_read_once = INPUT_READ_ONCE;
103
104     /* Initialize thread properties */
105     p_input->b_die              = 0;
106     p_input->b_error            = 0;
107     p_input->b_eof              = 0;
108
109     /* Set target */
110     p_input->p_source           = p_item->psz_name;
111
112     /* I have never understood that stuff --Meuuh */
113     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
114     *p_input->pi_status         = THREAD_CREATE;
115
116     /* Initialize stream description */
117     p_input->stream.i_es_number = 0;
118     p_input->stream.i_selected_es_number = 0;
119     p_input->stream.i_pgrm_number = 0;
120     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
121     p_input->stream.i_mux_rate = 0;
122
123     p_input->stream.i_area_nb = 0;
124     p_input->stream.pp_areas = NULL;
125     /* By default there is one areas in a stream */
126     input_AddArea( p_input );
127     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
128     p_input->stream.p_selected_area->i_seek = NO_SEEK;
129
130     /* Initialize stream control properties. */
131     p_input->stream.control.i_status = PLAYING_S;
132     p_input->stream.control.i_rate = DEFAULT_RATE;
133     p_input->stream.control.b_mute = 0;
134     p_input->stream.control.b_bw = 0;
135
136     /* Initialize default settings for spawned decoders */
137     p_input->p_default_aout = p_main->p_aout;
138     p_input->p_default_vout = p_main->p_vout;
139
140     /* Create thread and set locks. */
141     vlc_mutex_init( &p_input->stream.stream_lock );
142     vlc_cond_init( &p_input->stream.stream_wait );
143     vlc_mutex_init( &p_input->stream.control.control_lock );
144     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
145                            (void *) p_input ) )
146     {
147         intf_ErrMsg( "input error: can't create input thread (%s)",
148                      strerror(errno) );
149         free( p_input );
150         return( NULL );
151     }
152
153     /* If status is NULL, wait until the thread is created */
154     if( pi_status == NULL )
155     {
156         do
157         {
158             msleep( THREAD_SLEEP );
159         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
160                 && (i_status != THREAD_FATAL) );
161         if( i_status != THREAD_READY )
162         {
163             return( NULL );
164         }
165     }
166     return( p_input );
167 }
168
169 /*****************************************************************************
170  * input_DestroyThread: mark an input thread as zombie
171  *****************************************************************************
172  * This function should not return until the thread is effectively cancelled.
173  *****************************************************************************/
174 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
175 {
176     int         i_status;                                   /* thread status */
177
178     /* Set status */
179     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
180     *p_input->pi_status = THREAD_DESTROY;
181
182     /* Request thread destruction */
183     p_input->b_die = 1;
184
185     /* Make the thread exit of an eventual vlc_cond_wait() */
186     vlc_mutex_lock( &p_input->stream.stream_lock );
187     vlc_cond_signal( &p_input->stream.stream_wait );
188     vlc_mutex_unlock( &p_input->stream.stream_lock );
189
190     /* If status is NULL, wait until thread has been destroyed */
191     if( pi_status == NULL )
192     {
193         do
194         {
195             msleep( THREAD_SLEEP );
196         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
197                   && (i_status != THREAD_FATAL) );
198     }
199 }
200
201 /*****************************************************************************
202  * RunThread: main thread loop
203  *****************************************************************************
204  * Thread in charge of processing the network packets and demultiplexing.
205  *****************************************************************************/
206 static void RunThread( input_thread_t *p_input )
207 {
208     int                     i_error, i;
209
210     if( InitThread( p_input ) )
211     {
212
213         /* If we failed, wait before we are killed, and exit */
214         *p_input->pi_status = THREAD_ERROR;
215         p_input->b_error = 1;
216         ErrorThread( p_input );
217         DestroyThread( p_input );
218         return;
219     }
220
221     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
222     {
223         data_packet_t *         pp_packets[p_input->i_read_once];
224
225 #ifdef STATS
226         p_input->c_loops++;
227 #endif
228
229         vlc_mutex_lock( &p_input->stream.stream_lock );
230         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
231         {
232             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
233             {
234                 p_input->pf_seek( p_input,
235                                   p_input->stream.p_selected_area->i_seek );
236
237                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
238                 {
239                     pgrm_descriptor_t * p_pgrm
240                                             = p_input->stream.pp_programs[i];
241                     /* Escape all decoders for the stream discontinuity they
242                      * will encounter. */
243                     input_EscapeDiscontinuity( p_input, p_pgrm );
244
245                     /* Reinitialize synchro. */
246                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
247                 }
248             }
249             p_input->stream.p_selected_area->i_seek = NO_SEEK;
250         }
251         vlc_mutex_unlock( &p_input->stream.stream_lock );
252
253         i_error = p_input->pf_read( p_input, pp_packets );
254
255         /* Demultiplex read packets. */
256         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
257         {
258             p_input->pf_demux( p_input, pp_packets[i] );
259         }
260
261         if( i_error )
262         {
263             if( i_error == 1 )
264             {
265                 /* End of file - we do not set b_die because only the
266                  * interface is allowed to do so. */
267                 intf_WarnMsg( 1, "End of file reached" );
268                 p_input->b_eof = 1;
269             }
270             else
271             {
272                 p_input->b_error = 1;
273             }
274         }
275     }
276
277     if( p_input->b_error || p_input->b_eof )
278     {
279         ErrorThread( p_input );
280     }
281
282     EndThread( p_input );
283
284     DestroyThread( p_input );
285
286     intf_DbgMsg("Thread end");
287 }
288
289 /*****************************************************************************
290  * InitThread: init the input Thread
291  *****************************************************************************/
292 static int InitThread( input_thread_t * p_input )
293 {
294
295 #ifdef STATS
296     /* Initialize statistics */
297     p_input->c_loops                    = 0;
298     p_input->c_bytes                    = 0;
299     p_input->c_payload_bytes            = 0;
300     p_input->c_packets_read             = 0;
301     p_input->c_packets_trashed          = 0;
302 #endif
303
304     p_input->p_input_module = module_Need( p_main->p_bank,
305                                            MODULE_CAPABILITY_INPUT,
306                                            (probedata_t *)p_input );
307
308     if( p_input->p_input_module == NULL )
309     {
310         intf_ErrMsg( "input error: no suitable input module" );
311         return( -1 );
312     }
313
314 #define f p_input->p_input_module->p_functions->input.functions.input
315     p_input->pf_init          = f.pf_init;
316     p_input->pf_open          = f.pf_open;
317     p_input->pf_close         = f.pf_close;
318     p_input->pf_end           = f.pf_end;
319     p_input->pf_read          = f.pf_read;
320     p_input->pf_demux         = f.pf_demux;
321     p_input->pf_new_packet    = f.pf_new_packet;
322     p_input->pf_new_pes       = f.pf_new_pes;
323     p_input->pf_delete_packet = f.pf_delete_packet;
324     p_input->pf_delete_pes    = f.pf_delete_pes;
325     p_input->pf_rewind        = f.pf_rewind;
326     p_input->pf_seek          = f.pf_seek;
327 #undef f
328     p_input->pf_open( p_input );
329
330     if( p_input->b_error )
331     {
332         /* We barfed -- exit nicely */
333         p_input->pf_close( p_input );
334         module_Unneed( p_main->p_bank, p_input->p_input_module );
335         return( -1 );
336     }
337
338     p_input->pf_init( p_input );
339
340     *p_input->pi_status = THREAD_READY;
341
342     return( 0 );
343 }
344
345 /*****************************************************************************
346  * ErrorThread: RunThread() error loop
347  *****************************************************************************
348  * This function is called when an error occured during thread main's loop.
349  *****************************************************************************/
350 static void ErrorThread( input_thread_t *p_input )
351 {
352     while( !p_input->b_die )
353     {
354         /* Sleep a while */
355         msleep( INPUT_IDLE_SLEEP );
356     }
357 }
358
359 /*****************************************************************************
360  * EndThread: end the input thread
361  *****************************************************************************/
362 static void EndThread( input_thread_t * p_input )
363 {
364     int *       pi_status;                                  /* thread status */
365
366     /* Store status */
367     pi_status = p_input->pi_status;
368     *pi_status = THREAD_END;
369
370 #ifdef STATS
371     {
372         struct tms cpu_usage;
373         times( &cpu_usage );
374
375         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
376                  cpu_usage.tms_utime, cpu_usage.tms_stime);
377     }
378 #endif
379
380     /* Free all ES and destroy all decoder threads */
381     input_EndStream( p_input );
382
383     /* Free demultiplexer's data */
384     p_input->pf_end( p_input );
385
386     /* Close stream */
387     p_input->pf_close( p_input );
388
389     /* Release modules */
390     module_Unneed( p_main->p_bank, p_input->p_input_module );
391
392 }
393
394 /*****************************************************************************
395  * DestroyThread: destroy the input thread
396  *****************************************************************************/
397 static void DestroyThread( input_thread_t * p_input )
398 {
399     int *       pi_status;                                  /* thread status */
400
401     /* Store status */
402     pi_status = p_input->pi_status;
403
404     /* Destroy Mutex locks */
405     vlc_mutex_destroy( &p_input->stream.control.control_lock );
406     vlc_mutex_destroy( &p_input->stream.stream_lock );
407     
408     /* Free input structure */
409     free( p_input );
410
411     /* Update status */
412     *pi_status = THREAD_OVER;
413 }
414
415 /*****************************************************************************
416  * input_FileOpen : open a file descriptor
417  *****************************************************************************/
418 void input_FileOpen( input_thread_t * p_input )
419 {
420     struct stat         stat_info;
421     int                 i_stat;
422
423     char *psz_name = p_input->p_source;
424
425     /* FIXME: this code ought to be in the plugin so that code can
426      * be shared with the *_Probe function */
427     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
428     {
429         int i_size = strlen( psz_name );
430
431         if( ( i_size > 4 )
432             && !strncasecmp( psz_name, "dvd:", 4 ) )
433         {
434             /* get rid of the 'dvd:' stuff and try again */
435             psz_name += 4;
436             i_stat = stat( psz_name, &stat_info );
437         }
438         else if( ( i_size > 5 )
439                  && !strncasecmp( psz_name, "file:", 5 ) )
440         {
441             /* get rid of the 'file:' stuff and try again */
442             psz_name += 5;
443             i_stat = stat( psz_name, &stat_info );
444         }
445
446         if( i_stat == (-1) )
447         {
448             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
449                          psz_name, strerror(errno));
450             p_input->b_error = 1;
451             return;
452         }
453     }
454
455     vlc_mutex_lock( &p_input->stream.stream_lock );
456
457     /* If we are here we can control the pace... */
458     p_input->stream.b_pace_control = 1;
459
460     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
461          || S_ISBLK(stat_info.st_mode) )
462     {
463         p_input->stream.b_seekable = 1;
464         p_input->stream.p_selected_area->i_size = stat_info.st_size;
465     }
466     else if( S_ISFIFO(stat_info.st_mode)
467 #ifndef SYS_BEOS
468              || S_ISSOCK(stat_info.st_mode)
469 #endif
470              )
471     {
472         p_input->stream.b_seekable = 0;
473         p_input->stream.p_selected_area->i_size = 0;
474     }
475     else
476     {
477         vlc_mutex_unlock( &p_input->stream.stream_lock );
478         intf_ErrMsg( "input error: unknown file type for `%s'",
479                      psz_name );
480         p_input->b_error = 1;
481         return;
482     }
483
484     p_input->stream.p_selected_area->i_tell = 0;
485     vlc_mutex_unlock( &p_input->stream.stream_lock );
486
487     intf_Msg( "input: opening %s", p_input->p_source );
488     if( (p_input->i_handle = open( psz_name,
489                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
490     {
491         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
492         p_input->b_error = 1;
493         return;
494     }
495
496 }
497
498 /*****************************************************************************
499  * input_FileClose : close a file descriptor
500  *****************************************************************************/
501 void input_FileClose( input_thread_t * p_input )
502 {
503     close( p_input->i_handle );
504
505     return;
506 }
507
508 /*****************************************************************************
509  * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
510  *****************************************************************************/
511 int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port, 
512                       boolean_t b_broadcast )
513 {
514     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
515     struct hostent    * p_hostent;
516     
517     /* Reset struct */
518     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
519     p_socket->sin_family = AF_INET;                                 /* family */
520     p_socket->sin_port = htons( i_port );
521     
522     if( !b_broadcast )
523     {
524         /* Try to get our own IP */
525         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
526         {
527             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
528                          strerror( errno ) );
529             return( -1 );
530         }
531
532     }
533     else
534     {
535         /* Using broadcast address. There are many ways of doing it, one of
536          * the simpliest being a #define ...
537          * FIXME : this is ugly */ 
538         strncpy( psz_hostname, INPUT_BCAST_ADDR,INPUT_MAX_SOURCE_LENGTH );
539     }
540
541     /* Try to convert address directly from in_addr - this will work if
542      * psz_in_addr is dotted decimal. */
543
544 #ifdef HAVE_ARPA_INET_H
545     if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
546 #else
547     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
548 #endif
549     {
550         /* We have a fqdn, try to find its address */
551         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
552         {
553             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
554             return( -1 );
555         }
556         
557         /* Copy the first address of the host in the socket address */
558         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
559                  p_hostent->h_length );
560     }
561
562     return( 0 );
563 }
564
565 /*****************************************************************************
566  * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
567  *****************************************************************************/
568 int input_BuildRemoteAddr( input_thread_t * p_input, 
569                            struct sockaddr_in * p_socket )
570 {
571     struct hostent            * p_hostent;
572
573     /* Reset structure */
574     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
575     p_socket->sin_family = AF_INET;                                 /* family */
576     p_socket->sin_port = htons( 0 );                /* This is for remote end */
577     
578     /* Get the remote server */
579     if( p_input->p_source == NULL )
580     {
581         p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR, 
582                                                  INPUT_SERVER_DEFAULT );
583     }
584
585      /* Try to convert address directly from in_addr - this will work if
586       * psz_in_addr is dotted decimal. */
587 #ifdef HAVE_ARPA_INET_H
588     if( !inet_aton( p_input->p_source, &p_socket->sin_addr) )
589 #else
590     if( (p_socket->sin_addr.s_addr = inet_addr( p_input->p_source )) == -1 )
591 #endif
592     {
593         /* We have a fqdn, try to find its address */
594         if ( (p_hostent = gethostbyname(p_input->p_source)) == NULL )
595         {
596             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
597                          p_input->p_source );
598             return( -1 );
599         }
600         
601         /* Copy the first address of the host in the socket address */
602         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
603                  p_hostent->h_length );
604     }
605
606     return( 0 );
607 }
608
609 /*****************************************************************************
610  * input_NetworkOpen : open a network socket 
611  *****************************************************************************/
612 void input_NetworkOpen( input_thread_t * p_input )
613 {
614  
615     int                 i_option_value, i_port;
616     struct sockaddr_in  s_socket;
617     boolean_t           b_broadcast;
618     
619     /* FIXME : we don't handle channels for the moment */
620
621     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
622      * protocol */
623     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
624     if( p_input->i_handle == -1 )
625     {
626         intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
627         p_input->b_error = 1;
628         return;
629     }
630
631     /* We may want to reuse an already used socket */
632     i_option_value = 1;
633     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
634                     &i_option_value,sizeof( i_option_value ) ) == -1 )
635     {
636         intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
637                     strerror(errno));
638         close( p_input->i_handle );
639         p_input->b_error = 1;
640         return;
641     }
642
643 #ifndef SYS_BEOS
644     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
645      * packet loss caused by scheduling problems */
646     i_option_value = 524288;
647     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
648                     sizeof( i_option_value ) ) == -1 )
649     {
650         intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)", 
651                     strerror(errno));
652         close( p_input->i_handle );
653         p_input->b_error = 1;
654         return;
655     }
656 #endif /* SYS_BEOS */
657
658     /* Get details about what we are supposed to do */
659     b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
660     i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
661
662     /* TODO : here deal with channel stufs */
663
664     /* Build the local socket */
665     if ( input_BuildLocalAddr( &s_socket, i_port, b_broadcast ) 
666          == -1 )
667     {
668         close( p_input->i_handle );
669         p_input->b_error = 1;
670         return;
671     }
672     
673     /* Bind it */
674     if( bind( p_input->i_handle, (struct sockaddr *)&s_socket, 
675               sizeof( s_socket ) ) < 0 )
676     {
677         intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
678         close( p_input->i_handle );
679         p_input->b_error = 1;
680         return;
681     }
682
683     /* Build socket for remote connection */
684     if ( input_BuildRemoteAddr( p_input, &s_socket ) 
685          == -1 )
686     {
687         close( p_input->i_handle );
688         p_input->b_error = 1;
689         return;
690     }
691
692     /* And connect it ... should we really connect ? */
693     if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
694                  sizeof( s_socket ) ) == (-1) )
695     {
696         intf_ErrMsg("NetworkOpen: can't connect socket" );
697         close( p_input->i_handle );
698         p_input->b_error = 1;
699         return;
700     }
701
702     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
703      * with the server. */
704     p_input->stream.b_pace_control = 1;
705     
706     return;
707 }
708
709 /*****************************************************************************
710  * input_NetworkClose : close a network socket
711  *****************************************************************************/
712 void input_NetworkClose( input_thread_t * p_input )
713 {
714     close( p_input->i_handle );
715     /* FIXME: deal with channels */
716 }