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