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