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