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