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