]> git.sesse.net Git - vlc/blob - src/input/input.c
* Disabled network input under BeOS so that it compiles again. We'll
[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.90 2001/03/07 01:36:41 sam 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_demux         = f.pf_demux;
323     p_input->pf_new_packet    = f.pf_new_packet;
324     p_input->pf_new_pes       = f.pf_new_pes;
325     p_input->pf_delete_packet = f.pf_delete_packet;
326     p_input->pf_delete_pes    = f.pf_delete_pes;
327     p_input->pf_rewind        = f.pf_rewind;
328     p_input->pf_seek          = f.pf_seek;
329 #undef f
330     p_input->pf_open( p_input );
331
332     if( p_input->b_error )
333     {
334         /* We barfed -- exit nicely */
335         p_input->pf_close( p_input );
336         module_Unneed( p_main->p_bank, p_input->p_input_module );
337         return( -1 );
338     }
339
340     p_input->pf_init( p_input );
341
342     *p_input->pi_status = THREAD_READY;
343
344     return( 0 );
345 }
346
347 /*****************************************************************************
348  * ErrorThread: RunThread() error loop
349  *****************************************************************************
350  * This function is called when an error occured during thread main's loop.
351  *****************************************************************************/
352 static void ErrorThread( input_thread_t *p_input )
353 {
354     while( !p_input->b_die )
355     {
356         /* Sleep a while */
357         msleep( INPUT_IDLE_SLEEP );
358     }
359 }
360
361 /*****************************************************************************
362  * EndThread: end the input thread
363  *****************************************************************************/
364 static void EndThread( input_thread_t * p_input )
365 {
366     int *       pi_status;                                  /* thread status */
367
368     /* Store status */
369     pi_status = p_input->pi_status;
370     *pi_status = THREAD_END;
371
372 #ifdef STATS
373     {
374         struct tms cpu_usage;
375         times( &cpu_usage );
376
377         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
378                  cpu_usage.tms_utime, cpu_usage.tms_stime);
379     }
380 #endif
381
382     /* Free all ES and destroy all decoder threads */
383     input_EndStream( p_input );
384
385     /* Free demultiplexer's data */
386     p_input->pf_end( p_input );
387
388     /* Close stream */
389     p_input->pf_close( p_input );
390
391     /* Release modules */
392     module_Unneed( p_main->p_bank, p_input->p_input_module );
393
394 }
395
396 /*****************************************************************************
397  * DestroyThread: destroy the input thread
398  *****************************************************************************/
399 static void DestroyThread( input_thread_t * p_input )
400 {
401     int *       pi_status;                                  /* thread status */
402
403     /* Store status */
404     pi_status = p_input->pi_status;
405
406     /* Destroy Mutex locks */
407     vlc_mutex_destroy( &p_input->stream.control.control_lock );
408     vlc_mutex_destroy( &p_input->stream.stream_lock );
409     
410     /* Free input structure */
411     free( p_input );
412
413     /* Update status */
414     *pi_status = THREAD_OVER;
415 }
416
417 /*****************************************************************************
418  * input_FileOpen : open a file descriptor
419  *****************************************************************************/
420 void input_FileOpen( input_thread_t * p_input )
421 {
422     struct stat         stat_info;
423     int                 i_stat;
424
425     char *psz_name = p_input->p_source;
426
427     /* FIXME: this code ought to be in the plugin so that code can
428      * be shared with the *_Probe function */
429     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
430     {
431         int i_size = strlen( psz_name );
432
433         if( ( i_size > 4 )
434             && !strncasecmp( psz_name, "dvd:", 4 ) )
435         {
436             /* get rid of the 'dvd:' stuff and try again */
437             psz_name += 4;
438             i_stat = stat( psz_name, &stat_info );
439         }
440         else if( ( i_size > 5 )
441                  && !strncasecmp( psz_name, "file:", 5 ) )
442         {
443             /* get rid of the 'file:' stuff and try again */
444             psz_name += 5;
445             i_stat = stat( psz_name, &stat_info );
446         }
447
448         if( i_stat == (-1) )
449         {
450             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
451                          psz_name, strerror(errno));
452             p_input->b_error = 1;
453             return;
454         }
455     }
456
457     vlc_mutex_lock( &p_input->stream.stream_lock );
458
459     /* If we are here we can control the pace... */
460     p_input->stream.b_pace_control = 1;
461
462     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
463          || S_ISBLK(stat_info.st_mode) )
464     {
465         p_input->stream.b_seekable = 1;
466         p_input->stream.p_selected_area->i_size = stat_info.st_size;
467     }
468     else if( S_ISFIFO(stat_info.st_mode)
469 #ifndef SYS_BEOS
470              || S_ISSOCK(stat_info.st_mode)
471 #endif
472              )
473     {
474         p_input->stream.b_seekable = 0;
475         p_input->stream.p_selected_area->i_size = 0;
476     }
477     else
478     {
479         vlc_mutex_unlock( &p_input->stream.stream_lock );
480         intf_ErrMsg( "input error: unknown file type for `%s'",
481                      psz_name );
482         p_input->b_error = 1;
483         return;
484     }
485
486     p_input->stream.p_selected_area->i_tell = 0;
487     vlc_mutex_unlock( &p_input->stream.stream_lock );
488
489     intf_Msg( "input: opening %s", p_input->p_source );
490     if( (p_input->i_handle = open( psz_name,
491                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
492     {
493         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
494         p_input->b_error = 1;
495         return;
496     }
497
498 }
499
500 /*****************************************************************************
501  * input_FileClose : close a file descriptor
502  *****************************************************************************/
503 void input_FileClose( input_thread_t * p_input )
504 {
505     close( p_input->i_handle );
506
507     return;
508 }
509
510 #ifndef SYS_BEOS
511 /*****************************************************************************
512  * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
513  *****************************************************************************/
514 int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port, 
515                       boolean_t b_broadcast )
516 {
517     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
518     struct hostent    * p_hostent;
519     
520     /* Reset struct */
521     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
522     p_socket->sin_family = AF_INET;                                 /* family */
523     p_socket->sin_port = htons( i_port );
524     
525     if( !b_broadcast )
526     {
527         /* Try to get our own IP */
528         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
529         {
530             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
531                          strerror( errno ) );
532             return( -1 );
533         }
534
535     }
536     else
537     {
538         /* Using broadcast address. There are many ways of doing it, one of
539          * the simpliest being a #define ...
540          * FIXME : this is ugly */ 
541         strncpy( psz_hostname, INPUT_BCAST_ADDR,INPUT_MAX_SOURCE_LENGTH );
542     }
543
544     /* Try to convert address directly from in_addr - this will work if
545      * psz_in_addr is dotted decimal. */
546
547 #ifdef HAVE_ARPA_INET_H
548     if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
549 #else
550     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
551 #endif
552     {
553         /* We have a fqdn, try to find its address */
554         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
555         {
556             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
557             return( -1 );
558         }
559         
560         /* Copy the first address of the host in the socket address */
561         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
562                  p_hostent->h_length );
563     }
564
565     return( 0 );
566 }
567
568 /*****************************************************************************
569  * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
570  *****************************************************************************/
571 int input_BuildRemoteAddr( input_thread_t * p_input, 
572                            struct sockaddr_in * p_socket )
573 {
574     struct hostent            * p_hostent;
575
576     /* Reset structure */
577     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
578     p_socket->sin_family = AF_INET;                                 /* family */
579     p_socket->sin_port = htons( 0 );                /* This is for remote end */
580     
581     /* Get the remote server */
582     if( p_input->p_source == NULL )
583     {
584         p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR, 
585                                                  INPUT_SERVER_DEFAULT );
586     }
587
588      /* Try to convert address directly from in_addr - this will work if
589       * psz_in_addr is dotted decimal. */
590 #ifdef HAVE_ARPA_INET_H
591     if( !inet_aton( p_input->p_source, &p_socket->sin_addr) )
592 #else
593     if( (p_socket->sin_addr.s_addr = inet_addr( p_input->p_source )) == -1 )
594 #endif
595     {
596         /* We have a fqdn, try to find its address */
597         if ( (p_hostent = gethostbyname(p_input->p_source)) == NULL )
598         {
599             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
600                          p_input->p_source );
601             return( -1 );
602         }
603         
604         /* Copy the first address of the host in the socket address */
605         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
606                  p_hostent->h_length );
607     }
608
609     return( 0 );
610 }
611
612 /*****************************************************************************
613  * input_NetworkOpen : open a network socket 
614  *****************************************************************************/
615 void input_NetworkOpen( input_thread_t * p_input )
616 {
617  
618     int                 i_option_value, i_port;
619     struct sockaddr_in  s_socket;
620     boolean_t           b_broadcast;
621     
622     /* FIXME : we don't handle channels for the moment */
623
624     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
625      * protocol */
626     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
627     if( p_input->i_handle == -1 )
628     {
629         intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
630         p_input->b_error = 1;
631         return;
632     }
633
634     /* We may want to reuse an already used socket */
635     i_option_value = 1;
636     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
637                     &i_option_value,sizeof( i_option_value ) ) == -1 )
638     {
639         intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
640                     strerror(errno));
641         close( p_input->i_handle );
642         p_input->b_error = 1;
643         return;
644     }
645
646 #ifndef SYS_BEOS
647     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
648      * packet loss caused by scheduling problems */
649     i_option_value = 524288;
650     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
651                     sizeof( i_option_value ) ) == -1 )
652     {
653         intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)", 
654                     strerror(errno));
655         close( p_input->i_handle );
656         p_input->b_error = 1;
657         return;
658     }
659 #endif /* SYS_BEOS */
660
661     /* Get details about what we are supposed to do */
662     b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
663     i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
664
665     /* TODO : here deal with channel stufs */
666
667     /* Build the local socket */
668     if ( input_BuildLocalAddr( &s_socket, i_port, b_broadcast ) 
669          == -1 )
670     {
671         close( p_input->i_handle );
672         p_input->b_error = 1;
673         return;
674     }
675     
676     /* Bind it */
677     if( bind( p_input->i_handle, (struct sockaddr *)&s_socket, 
678               sizeof( s_socket ) ) < 0 )
679     {
680         intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
681         close( p_input->i_handle );
682         p_input->b_error = 1;
683         return;
684     }
685
686     /* Build socket for remote connection */
687     if ( input_BuildRemoteAddr( p_input, &s_socket ) 
688          == -1 )
689     {
690         close( p_input->i_handle );
691         p_input->b_error = 1;
692         return;
693     }
694
695     /* And connect it ... should we really connect ? */
696     if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
697                  sizeof( s_socket ) ) == (-1) )
698     {
699         intf_ErrMsg("NetworkOpen: can't connect socket" );
700         close( p_input->i_handle );
701         p_input->b_error = 1;
702         return;
703     }
704
705     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
706      * with the server. */
707     p_input->stream.b_pace_control = 1;
708     
709     return;
710 }
711
712 /*****************************************************************************
713  * input_NetworkClose : close a network socket
714  *****************************************************************************/
715 void input_NetworkClose( input_thread_t * p_input )
716 {
717     close( p_input->i_handle );
718     /* FIXME: deal with channels */
719 }
720 #endif