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