]> git.sesse.net Git - vlc/blob - src/input/input.c
* Fixed a warning for *BSD compilation.
[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.109 2001/05/25 13:20:10 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
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static void RunThread       ( input_thread_t *p_input );
81 static  int InitThread      ( input_thread_t *p_input );
82 static void ErrorThread     ( input_thread_t *p_input );
83 static void DestroyThread   ( input_thread_t *p_input );
84 static void EndThread       ( input_thread_t *p_input );
85
86 /*****************************************************************************
87  * input_CreateThread: creates a new input thread
88  *****************************************************************************
89  * This function creates a new input, and returns a pointer
90  * to its description. On error, it returns NULL.
91  * If pi_status is NULL, then the function will block until the thread is ready.
92  * If not, it will be updated using one of the THREAD_* constants.
93  *****************************************************************************/
94 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
95 {
96     input_thread_t *    p_input;                        /* thread descriptor */
97     int                 i_status;                           /* thread status */
98
99     /* Allocate descriptor */
100     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
101     if( p_input == NULL )
102     {
103         intf_ErrMsg( "input error: can't allocate input thread (%s)",
104                      strerror(errno) );
105         return( NULL );
106     }
107
108     /* Packets read once */
109     p_input->i_read_once = INPUT_READ_ONCE;
110
111     /* Initialize thread properties */
112     p_input->b_die              = 0;
113     p_input->b_error            = 0;
114     p_input->b_eof              = 0;
115
116     /* Set target */
117     p_input->p_source           = p_item->psz_name;
118
119     /* I have never understood that stuff --Meuuh */
120     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
121     *p_input->pi_status         = THREAD_CREATE;
122
123     /* Initialize stream description */
124     p_input->stream.i_es_number = 0;
125     p_input->stream.i_selected_es_number = 0;
126     p_input->stream.i_pgrm_number = 0;
127     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
128     p_input->stream.i_mux_rate = 0;
129
130     /* no stream, no area */
131     p_input->stream.i_area_nb = 0;
132     p_input->stream.pp_areas = NULL;
133     p_input->stream.p_selected_area = NULL;
134     p_input->stream.p_new_area = NULL;
135     /* By default there is one areas in a stream */
136     input_AddArea( p_input );
137     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
138
139     /* Initialize stream control properties. */
140     p_input->stream.control.i_status = PLAYING_S;
141     p_input->stream.control.i_rate = DEFAULT_RATE;
142     p_input->stream.control.b_mute = 0;
143     p_input->stream.control.b_bw = 0;
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     /* initialization is completed */
227     vlc_mutex_lock( &p_input->stream.stream_lock );
228     p_input->stream.b_changed = 1;
229     vlc_mutex_unlock( &p_input->stream.stream_lock );
230
231     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
232     {
233         data_packet_t *         pp_packets[p_input->i_read_once];
234
235 #ifdef STATS
236         p_input->c_loops++;
237 #endif
238
239         vlc_mutex_lock( &p_input->stream.stream_lock );
240
241         if( p_input->stream.p_new_area )
242         {
243             p_input->pf_set_area( p_input, p_input->stream.p_new_area );
244             p_input->stream.p_new_area = NULL;
245         }
246
247         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
248         {
249             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
250             {
251                 p_input->pf_seek( p_input,
252                                   p_input->stream.p_selected_area->i_seek );
253
254                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
255                 {
256                     pgrm_descriptor_t * p_pgrm
257                                             = p_input->stream.pp_programs[i];
258                     /* Escape all decoders for the stream discontinuity they
259                      * will encounter. */
260                     input_EscapeDiscontinuity( p_input, p_pgrm );
261
262                     /* Reinitialize synchro. */
263                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
264                 }
265             }
266             p_input->stream.p_selected_area->i_seek = NO_SEEK;
267         }
268
269         if( p_input->stream.p_removed_es )
270         {
271             input_UnselectES( p_input, p_input->stream.p_removed_es );
272             p_input->stream.p_removed_es = NULL;
273         }
274
275         if( p_input->stream.p_newly_selected_es )
276         {
277             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
278             p_input->stream.p_newly_selected_es = NULL;
279         }
280
281         vlc_mutex_unlock( &p_input->stream.stream_lock );
282
283         i_error = p_input->pf_read( p_input, pp_packets );
284
285         /* Demultiplex read packets. */
286         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
287         {
288             p_input->pf_demux( p_input, pp_packets[i] );
289         }
290
291         if( i_error )
292         {
293             if( i_error == 1 )
294             {
295                 /* End of file - we do not set b_die because only the
296                  * interface is allowed to do so. */
297                 intf_WarnMsg( 3, "input: EOF reached" );
298                 p_input->b_eof = 1;
299             }
300             else
301             {
302                 p_input->b_error = 1;
303             }
304         }
305     }
306
307     if( p_input->b_error || p_input->b_eof )
308     {
309         ErrorThread( p_input );
310     }
311
312     EndThread( p_input );
313
314     DestroyThread( p_input );
315
316     intf_DbgMsg("input: Thread end");
317 }
318
319 /*****************************************************************************
320  * InitThread: init the input Thread
321  *****************************************************************************/
322 static int InitThread( input_thread_t * p_input )
323 {
324
325 #ifdef STATS
326     /* Initialize statistics */
327     p_input->c_loops                    = 0;
328     p_input->c_bytes                    = 0;
329     p_input->c_payload_bytes            = 0;
330     p_input->c_packets_read             = 0;
331     p_input->c_packets_trashed          = 0;
332 #endif
333
334     p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
335                                            (probedata_t *)p_input );
336
337     if( p_input->p_input_module == NULL )
338     {
339         intf_ErrMsg( "input error: no suitable input module for `%s'",
340                      p_input->p_source );
341         return( -1 );
342     }
343
344 #define f p_input->p_input_module->p_functions->input.functions.input
345     p_input->pf_init          = f.pf_init;
346     p_input->pf_open          = f.pf_open;
347     p_input->pf_close         = f.pf_close;
348     p_input->pf_end           = f.pf_end;
349     p_input->pf_read          = f.pf_read;
350     p_input->pf_set_area      = f.pf_set_area;
351     p_input->pf_demux         = f.pf_demux;
352     p_input->pf_new_packet    = f.pf_new_packet;
353     p_input->pf_new_pes       = f.pf_new_pes;
354     p_input->pf_delete_packet = f.pf_delete_packet;
355     p_input->pf_delete_pes    = f.pf_delete_pes;
356     p_input->pf_rewind        = f.pf_rewind;
357     p_input->pf_seek          = f.pf_seek;
358 #undef f
359     p_input->pf_open( p_input );
360
361     if( p_input->b_error )
362     {
363         /* We barfed -- exit nicely */
364         p_input->pf_close( p_input );
365         module_Unneed( p_input->p_input_module );
366         return( -1 );
367     }
368
369     p_input->pf_init( p_input );
370
371     if( p_input->b_error )
372     {
373         /* We barfed -- exit nicely */
374         p_input->pf_close( p_input );
375         module_Unneed( p_input->p_input_module );
376         return( -1 );
377     }
378
379     *p_input->pi_status = THREAD_READY;
380
381     return( 0 );
382 }
383
384 /*****************************************************************************
385  * ErrorThread: RunThread() error loop
386  *****************************************************************************
387  * This function is called when an error occured during thread main's loop.
388  *****************************************************************************/
389 static void ErrorThread( input_thread_t *p_input )
390 {
391     while( !p_input->b_die )
392     {
393         /* Sleep a while */
394         msleep( INPUT_IDLE_SLEEP );
395     }
396 }
397
398 /*****************************************************************************
399  * EndThread: end the input thread
400  *****************************************************************************/
401 static void EndThread( input_thread_t * p_input )
402 {
403     int *       pi_status;                                  /* thread status */
404
405     /* Store status */
406     pi_status = p_input->pi_status;
407     *pi_status = THREAD_END;
408
409 #ifdef STATS
410     {
411         struct tms cpu_usage;
412         times( &cpu_usage );
413
414         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
415                  cpu_usage.tms_utime, cpu_usage.tms_stime);
416     }
417 #endif
418
419     /* Free all ES and destroy all decoder threads */
420     input_EndStream( p_input );
421
422     /* Free demultiplexer's data */
423     p_input->pf_end( p_input );
424
425     /* Close stream */
426     p_input->pf_close( p_input );
427
428     /* Release modules */
429     module_Unneed( p_input->p_input_module );
430
431 }
432
433 /*****************************************************************************
434  * DestroyThread: destroy the input thread
435  *****************************************************************************/
436 static void DestroyThread( input_thread_t * p_input )
437 {
438     int *       pi_status;                                  /* thread status */
439
440     /* Store status */
441     pi_status = p_input->pi_status;
442
443     /* Destroy Mutex locks */
444     vlc_mutex_destroy( &p_input->stream.control.control_lock );
445     vlc_mutex_destroy( &p_input->stream.stream_lock );
446     
447     /* Free input structure */
448     free( p_input );
449
450     /* Update status */
451     *pi_status = THREAD_OVER;
452 }
453
454 /*****************************************************************************
455  * input_FileOpen : open a file descriptor
456  *****************************************************************************/
457 void input_FileOpen( input_thread_t * p_input )
458 {
459     struct stat         stat_info;
460     int                 i_stat;
461
462     char *psz_name = p_input->p_source;
463
464     /* FIXME: this code ought to be in the plugin so that code can
465      * be shared with the *_Probe function */
466     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
467     {
468         int i_size = strlen( psz_name );
469
470         if( ( i_size > 4 )
471             && !strncasecmp( psz_name, "dvd:", 4 ) )
472         {
473             /* get rid of the 'dvd:' stuff and try again */
474             psz_name += 4;
475             i_stat = stat( psz_name, &stat_info );
476         }
477         else if( ( i_size > 5 )
478                  && !strncasecmp( psz_name, "file:", 5 ) )
479         {
480             /* get rid of the 'file:' stuff and try again */
481             psz_name += 5;
482             i_stat = stat( psz_name, &stat_info );
483         }
484
485         if( i_stat == (-1) )
486         {
487             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
488                          psz_name, strerror(errno));
489             p_input->b_error = 1;
490             return;
491         }
492     }
493
494     vlc_mutex_lock( &p_input->stream.stream_lock );
495
496     /* If we are here we can control the pace... */
497     p_input->stream.b_pace_control = 1;
498
499     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
500          || S_ISBLK(stat_info.st_mode) )
501     {
502         p_input->stream.b_seekable = 1;
503         p_input->stream.p_selected_area->i_size = stat_info.st_size;
504     }
505     else if( S_ISFIFO(stat_info.st_mode)
506 #if !defined( SYS_BEOS ) && !defined( WIN32 )
507              || S_ISSOCK(stat_info.st_mode)
508 #endif
509              )
510     {
511         p_input->stream.b_seekable = 0;
512         p_input->stream.p_selected_area->i_size = 0;
513     }
514     else
515     {
516         vlc_mutex_unlock( &p_input->stream.stream_lock );
517         intf_ErrMsg( "input error: unknown file type for `%s'",
518                      psz_name );
519         p_input->b_error = 1;
520         return;
521     }
522
523     p_input->stream.p_selected_area->i_tell = 0;
524     vlc_mutex_unlock( &p_input->stream.stream_lock );
525
526     intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
527 #ifndef WIN32
528     if( (p_input->i_handle = open( psz_name,
529                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
530 #else
531     if( (p_input->i_handle = open( psz_name, O_BINARY
532                                    /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
533 #endif
534     {
535         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
536         p_input->b_error = 1;
537         return;
538     }
539
540 }
541
542 /*****************************************************************************
543  * input_FileClose : close a file descriptor
544  *****************************************************************************/
545 void input_FileClose( input_thread_t * p_input )
546 {
547     intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
548     close( p_input->i_handle );
549
550     return;
551 }
552
553
554 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
555 /*****************************************************************************
556  * input_NetworkOpen : open a network socket 
557  *****************************************************************************/
558 void input_NetworkOpen( input_thread_t * p_input )
559 {
560     char                *psz_server = NULL;
561     char                *psz_broadcast = NULL;
562     int                 i_port = 0;
563     int                 i_opt;
564     struct sockaddr_in  sock;
565     
566     /* Get the remote server */
567     if( p_input->p_source != NULL )
568     {
569         psz_server = p_input->p_source;
570
571         /* Skip the protocol name */
572         while( *psz_server && *psz_server != ':' )
573         {
574             psz_server++;
575         }
576
577         /* Skip the "://" part */
578         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
579         {
580             psz_server++;
581         }
582
583         /* Found a server name */
584         if( *psz_server )
585         {
586             char *psz_port = psz_server;
587
588             /* Skip the hostname part */
589             while( *psz_port && *psz_port != ':' )
590             {
591                 psz_port++;
592             }
593
594             /* Found a port name */
595             if( *psz_port )
596             {
597                 /* Replace ':' with '\0' */
598                 *psz_port = '\0';
599                 psz_port++;
600
601                 psz_broadcast = psz_port;
602                 while( *psz_broadcast && *psz_broadcast != ':' )
603                 {
604                     psz_broadcast++;
605                 }
606
607                 if( *psz_broadcast )
608                 {
609                     *psz_broadcast = '\0';
610                     psz_broadcast++;
611                     while( *psz_broadcast && *psz_broadcast == ':' )
612                     {
613                         psz_broadcast++;
614                     }
615                 }
616                 else
617                 {
618                     psz_broadcast = NULL;
619                 }
620
621                 /* port before broadcast address */
622                 if( *psz_port != ':' )
623                 {
624                     i_port = atoi( psz_port );
625                 }
626             }
627         }
628         else
629         {
630             psz_server = NULL;
631         }
632     }
633
634     /* Check that we got a valid server */
635     if( psz_server == NULL )
636     {
637         psz_server = main_GetPszVariable( INPUT_SERVER_VAR, 
638                                           INPUT_SERVER_DEFAULT );
639     }
640
641     /* Check that we got a valid port */
642     if( i_port == 0 )
643     {
644         i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
645     }
646
647     if( psz_broadcast == NULL )
648     {
649         /* Are we broadcasting ? */
650         psz_broadcast = main_GetPszVariable( INPUT_BROADCAST_VAR, NULL );
651     }
652
653     intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
654                      psz_server, i_port, psz_broadcast );
655
656     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
657      * protocol */
658     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
659     if( p_input->i_handle == -1 )
660     {
661         intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
662         p_input->b_error = 1;
663         return;
664     }
665
666     /* We may want to reuse an already used socket */
667     i_opt = 1;
668     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
669                     &i_opt, sizeof( i_opt ) ) == -1 )
670     {
671         intf_ErrMsg("input error: can't configure socket (SO_REUSEADDR: %s)",
672                     strerror(errno));
673         close( p_input->i_handle );
674         p_input->b_error = 1;
675         return;
676     }
677
678     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
679      * packet loss caused by scheduling problems */
680     i_opt = 0x80000;
681     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
682                     &i_opt, sizeof( i_opt ) ) == -1 )
683     {
684         intf_ErrMsg("input error: can't configure socket (SO_RCVBUF: %s)", 
685                     strerror(errno));
686         close( p_input->i_handle );
687         p_input->b_error = 1;
688         return;
689     }
690
691     /* Build the local socket */
692     if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) 
693          == -1 )
694     {
695         close( p_input->i_handle );
696         p_input->b_error = 1;
697         return;
698     }
699     
700     /* Bind it */
701     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
702               sizeof( sock ) ) < 0 )
703     {
704         intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
705         close( p_input->i_handle );
706         p_input->b_error = 1;
707         return;
708     }
709
710     /* Build socket for remote connection */
711     if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
712     {
713         close( p_input->i_handle );
714         p_input->b_error = 1;
715         return;
716     }
717
718     /* And connect it ... should we really connect ? */
719     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
720                  sizeof( sock ) ) == (-1) )
721     {
722         intf_ErrMsg( "NetworkOpen: can't connect socket : %s", 
723                      strerror(errno) );
724         close( p_input->i_handle );
725         p_input->b_error = 1;
726         return;
727     }
728
729     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
730      * with the server. */
731     p_input->stream.b_pace_control = 0;
732     p_input->stream.b_seekable = 0;
733     
734     return;
735 }
736
737 /*****************************************************************************
738  * input_NetworkClose : close a network socket
739  *****************************************************************************/
740 void input_NetworkClose( input_thread_t * p_input )
741 {
742     close( p_input->i_handle );
743 }
744 #endif