]> git.sesse.net Git - vlc/blob - src/input/input.c
* added functions in inputs to change the current program (most of
[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-2001 VideoLAN
7  * $Id: input.c,v 1.162 2001/12/07 16:47:47 jobi 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
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #   include <io.h>
40 #endif
41
42 #include <string.h>
43 #include <errno.h>
44
45 #ifdef STRNCASECMP_IN_STRINGS_H
46 #   include <strings.h>
47 #endif
48
49 #ifdef WIN32
50 #   include <winsock2.h>
51 #   include <ws2tcpip.h>
52 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
53 #   include <netdb.h>                                         /* hostent ... */
54 #   include <sys/socket.h>
55 #   include <netinet/in.h>
56 #   ifdef HAVE_ARPA_INET_H
57 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
58 #   endif
59 #endif
60
61 #ifdef HAVE_SYS_TIMES_H
62 #   include <sys/times.h>
63 #endif
64
65 #include "config.h"
66 #include "common.h"
67 #include "intf_msg.h"
68 #include "threads.h"
69 #include "mtime.h"
70 #include "tests.h"
71 #include "netutils.h"
72 #include "modules.h"
73
74 #include "intf_playlist.h"
75
76 #include "stream_control.h"
77 #include "input_ext-intf.h"
78 #include "input_ext-dec.h"
79 #include "input_ext-plugins.h"
80
81 #include "interface.h"
82
83 #include "main.h"
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 static void RunThread       ( input_thread_t *p_input );
89 static  int InitThread      ( input_thread_t *p_input );
90 static void ErrorThread     ( input_thread_t *p_input );
91 static void CloseThread     ( input_thread_t *p_input );
92 static void DestroyThread   ( input_thread_t *p_input );
93 static void EndThread       ( input_thread_t *p_input );
94
95 static void FileOpen        ( input_thread_t *p_input );
96 static void StdOpen         ( input_thread_t *p_input );
97 static void FileClose       ( input_thread_t *p_input );
98 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
99 static void NetworkOpen     ( input_thread_t *p_input );
100 static void HTTPOpen        ( input_thread_t *p_input );
101 static void NetworkClose    ( input_thread_t *p_input );
102 #endif
103
104 /*****************************************************************************
105  * input_CreateThread: creates a new input thread
106  *****************************************************************************
107  * This function creates a new input, and returns a pointer
108  * to its description. On error, it returns NULL.
109  * If pi_status is NULL, then the function will block until the thread is ready.
110  * If not, it will be updated using one of the THREAD_* constants.
111  *****************************************************************************/
112 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
113 {
114     input_thread_t *    p_input;                        /* thread descriptor */
115     int                 i_status;                           /* thread status */
116
117     /* Allocate descriptor */
118     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
119     if( p_input == NULL )
120     {
121         intf_ErrMsg( "input error: can't allocate input thread (%s)",
122                      strerror(errno) );
123         return( NULL );
124     }
125
126     /* Packets read once */
127     p_input->i_read_once = INPUT_READ_ONCE;
128
129     /* Initialize thread properties */
130     p_input->b_die              = 0;
131     p_input->b_error            = 0;
132     p_input->b_eof              = 0;
133
134     /* Set target */
135     p_input->p_source           = p_item->psz_name;
136
137     /* I have never understood that stuff --Meuuh */
138     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
139     *p_input->pi_status         = THREAD_CREATE;
140
141     /* Initialize stream description */
142     p_input->stream.i_es_number = 0;
143     p_input->stream.i_selected_es_number = 0;
144     p_input->stream.i_pgrm_number = 0;
145     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
146     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
147     p_input->stream.i_mux_rate = 0;
148
149     /* no stream, no area */
150     p_input->stream.i_area_nb = 0;
151     p_input->stream.pp_areas = NULL;
152     p_input->stream.p_selected_area = NULL;
153     p_input->stream.p_new_area = NULL;
154
155     /* By default there is one area in a stream */
156     input_AddArea( p_input );
157     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
158
159     /* Initialize stream control properties. */
160     p_input->stream.control.i_status = PLAYING_S;
161     p_input->stream.control.i_rate = DEFAULT_RATE;
162     p_input->stream.control.b_mute = 0;
163     p_input->stream.control.b_grayscale = main_GetIntVariable(
164                             VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
165     p_input->stream.control.i_smp = main_GetIntVariable(
166                             VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
167
168     intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
169
170     /* Create thread. */
171     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
172                            (void *) p_input ) )
173     {
174         intf_ErrMsg( "input error: can't create input thread (%s)",
175                      strerror(errno) );
176         free( p_input );
177         return( NULL );
178     }
179
180     /* If status is NULL, wait until the thread is created */
181     if( pi_status == NULL )
182     {
183         do
184         {
185             msleep( THREAD_SLEEP );
186         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
187                 && (i_status != THREAD_FATAL) );
188         if( i_status != THREAD_READY )
189         {
190             return( NULL );
191         }
192     }
193     return( p_input );
194 }
195
196 /*****************************************************************************
197  * input_DestroyThread: mark an input thread as zombie
198  *****************************************************************************
199  * This function should not return until the thread is effectively cancelled.
200  *****************************************************************************/
201 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
202 {
203     int         i_status;                                   /* thread status */
204
205     /* Set status */
206     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
207     *p_input->pi_status = THREAD_DESTROY;
208
209     /* Request thread destruction */
210     p_input->b_die = 1;
211
212     /* Make the thread exit of an eventual vlc_cond_wait() */
213     vlc_mutex_lock( &p_input->stream.stream_lock );
214     vlc_cond_signal( &p_input->stream.stream_wait );
215     vlc_mutex_unlock( &p_input->stream.stream_lock );
216
217     /* If status is NULL, wait until thread has been destroyed */
218     if( pi_status == NULL )
219     {
220         do
221         {
222             msleep( THREAD_SLEEP );
223         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
224                   && (i_status != THREAD_FATAL) );
225     }
226 }
227
228 /*****************************************************************************
229  * RunThread: main thread loop
230  *****************************************************************************
231  * Thread in charge of processing the network packets and demultiplexing.
232  *****************************************************************************/
233 static void RunThread( input_thread_t *p_input )
234 {
235     int                     i_error, i;
236     data_packet_t **        pp_packets;
237
238     if( InitThread( p_input ) )
239     {
240         /* If we failed, wait before we are killed, and exit */
241         *p_input->pi_status = THREAD_ERROR;
242         p_input->b_error = 1;
243         ErrorThread( p_input );
244         DestroyThread( p_input );
245         return;
246     }
247
248     /* initialization is completed */
249     vlc_mutex_lock( &p_input->stream.stream_lock );
250     p_input->stream.b_changed = 1;
251     vlc_mutex_unlock( &p_input->stream.stream_lock );
252
253     pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
254                                         sizeof( data_packet_t * ) );
255     if( pp_packets == NULL )
256     {
257         intf_ErrMsg( "input error: out of memory" );
258         free( pp_packets );
259         p_input->b_error = 1;
260     }
261
262     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
263     {
264         p_input->c_loops++;
265
266         vlc_mutex_lock( &p_input->stream.stream_lock );
267
268         if( p_input->stream.p_new_program )
269         {
270             if( p_input->pf_set_program != NULL )
271             {
272
273                 p_input->pf_set_program( p_input, 
274                         p_input->stream.p_new_program );
275
276                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
277                 {
278                     pgrm_descriptor_t * p_pgrm
279                                             = p_input->stream.pp_programs[i];
280                     /* Escape all decoders for the stream discontinuity they
281                      * will encounter. */
282                     input_EscapeDiscontinuity( p_input, p_pgrm );
283
284                     /* Reinitialize synchro. */
285                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
286                 }
287             }
288             p_input->stream.p_new_program = NULL;
289         }
290         
291         if( p_input->stream.p_new_area )
292         {
293             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
294             {
295
296                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
297
298                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
299                 {
300                     pgrm_descriptor_t * p_pgrm
301                                             = p_input->stream.pp_programs[i];
302                     /* Escape all decoders for the stream discontinuity they
303                      * will encounter. */
304                     input_EscapeDiscontinuity( p_input, p_pgrm );
305
306                     /* Reinitialize synchro. */
307                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
308                 }
309             }
310             p_input->stream.p_new_area = NULL;
311         }
312
313         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
314         {
315             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
316             {
317                 p_input->pf_seek( p_input,
318                                   p_input->stream.p_selected_area->i_seek );
319
320                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
321                 {
322                     pgrm_descriptor_t * p_pgrm
323                                             = p_input->stream.pp_programs[i];
324                     /* Escape all decoders for the stream discontinuity they
325                      * will encounter. */
326                     input_EscapeDiscontinuity( p_input, p_pgrm );
327
328                     /* Reinitialize synchro. */
329                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
330                 }
331             }
332             p_input->stream.p_selected_area->i_seek = NO_SEEK;
333         }
334
335         if( p_input->stream.p_removed_es )
336         {
337             input_UnselectES( p_input, p_input->stream.p_removed_es );
338             p_input->stream.p_removed_es = NULL;
339         }
340
341         if( p_input->stream.p_newly_selected_es )
342         {
343             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
344             p_input->stream.p_newly_selected_es = NULL;
345         }
346
347         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
348         {
349             if( p_input->stream.b_new_mute )
350             {
351                 input_EscapeAudioDiscontinuity( p_input );
352             }
353
354             vlc_mutex_lock( &p_input->stream.control.control_lock );
355             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
356             vlc_mutex_unlock( &p_input->stream.control.control_lock );
357
358             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
359         }
360
361         vlc_mutex_unlock( &p_input->stream.stream_lock );
362
363         i_error = p_input->pf_read( p_input, pp_packets );
364
365         /* Demultiplex read packets. */
366         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
367         {
368             p_input->stream.c_packets_read++;
369             p_input->pf_demux( p_input, pp_packets[i] );
370         }
371
372         if( i_error )
373         {
374             if( i_error == 1 )
375             {
376                 /* End of file - we do not set b_die because only the
377                  * interface is allowed to do so. */
378                 intf_WarnMsg( 3, "input: EOF reached" );
379                 p_input->b_eof = 1;
380             }
381             else
382             {
383                 p_input->b_error = 1;
384             }
385         }
386     }
387
388     free( pp_packets );
389
390     if( p_input->b_error || p_input->b_eof )
391     {
392         ErrorThread( p_input );
393     }
394
395     EndThread( p_input );
396
397     DestroyThread( p_input );
398
399     intf_DbgMsg("input: Thread end");
400 }
401
402 /*****************************************************************************
403  * InitThread: init the input Thread
404  *****************************************************************************/
405 static int InitThread( input_thread_t * p_input )
406 {
407
408     /* Initialize statistics */
409     p_input->c_loops                    = 0;
410     p_input->stream.c_packets_read      = 0;
411     p_input->stream.c_packets_trashed   = 0;
412     p_input->p_stream                   = NULL;
413
414     /* Set locks. */
415     vlc_mutex_init( &p_input->stream.stream_lock );
416     vlc_cond_init( &p_input->stream.stream_wait );
417     vlc_mutex_init( &p_input->stream.control.control_lock );
418
419     /* Find appropriate module. */
420     p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
421                                            (probedata_t *)p_input );
422
423     if( p_input->p_input_module == NULL )
424     {
425         intf_ErrMsg( "input error: no suitable input module for `%s'",
426                      p_input->p_source );
427         return( -1 );
428     }
429
430 #define f p_input->p_input_module->p_functions->input.functions.input
431     p_input->pf_init          = f.pf_init;
432     p_input->pf_end           = f.pf_end;
433     p_input->pf_init_bit_stream= f.pf_init_bit_stream;
434     p_input->pf_read          = f.pf_read;
435     p_input->pf_set_area      = f.pf_set_area;
436     p_input->pf_set_program   = f.pf_set_program;
437     p_input->pf_demux         = f.pf_demux;
438     p_input->pf_new_packet    = f.pf_new_packet;
439     p_input->pf_new_pes       = f.pf_new_pes;
440     p_input->pf_delete_packet = f.pf_delete_packet;
441     p_input->pf_delete_pes    = f.pf_delete_pes;
442     p_input->pf_rewind        = f.pf_rewind;
443     p_input->pf_seek          = f.pf_seek;
444
445 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
446     /* FIXME : this is waaaay too kludgy */
447     if( ( strlen( p_input->p_source ) >= 10
448           && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
449           || ( strlen( p_input->p_source ) >= 4
450                 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
451     {
452         /* Network stream */
453         NetworkOpen( p_input );
454         p_input->stream.i_method = INPUT_METHOD_NETWORK;
455     }
456     else if( ( strlen( p_input->p_source ) > 5 )
457                && !strncasecmp( p_input->p_source, "http:", 5 ) )
458     {
459         /* HTTP stream */
460         HTTPOpen( p_input );
461         p_input->stream.i_method = INPUT_METHOD_NETWORK;
462     }
463     else 
464 #endif
465         if( ( ( strlen( p_input->p_source ) > 4 )
466                 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
467             || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
468     {
469         /* DVD - this is THE kludge */
470         f.pf_open( p_input );
471         p_input->stream.i_method = INPUT_METHOD_DVD;
472     }
473     else if( ( ( strlen( p_input->p_source ) > 8 )
474                  && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
475               || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
476     {
477         /* DVDRead - this is THE kludge */
478         f.pf_open( p_input );
479         p_input->stream.i_method = INPUT_METHOD_DVD;
480     }
481     else if( ( strlen( p_input->p_source ) > 4 )
482                && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
483     {
484         /* Dummy input - very kludgy */
485         f.pf_open( p_input );
486     }
487     else if( ( strlen( p_input->p_source ) == 1 )
488                && *p_input->p_source == '-' )
489     {
490         /* Stdin */
491         StdOpen( p_input );
492     }
493     else
494     {
495         /* File input */
496         FileOpen( p_input );
497         p_input->stream.i_method = INPUT_METHOD_FILE;
498     }
499 #undef f
500
501     if( p_input->b_error )
502     {
503         /* We barfed -- exit nicely */
504         module_Unneed( p_input->p_input_module );
505         return( -1 );
506     }
507
508     p_input->pf_init( p_input );
509
510     if( p_input->b_error )
511     {
512         /* We barfed -- exit nicely */
513         CloseThread( p_input );
514         module_Unneed( p_input->p_input_module );
515         return( -1 );
516     }
517
518     *p_input->pi_status = THREAD_READY;
519
520     return( 0 );
521 }
522
523 /*****************************************************************************
524  * ErrorThread: RunThread() error loop
525  *****************************************************************************
526  * This function is called when an error occured during thread main's loop.
527  *****************************************************************************/
528 static void ErrorThread( input_thread_t *p_input )
529 {
530     while( !p_input->b_die )
531     {
532         /* Sleep a while */
533         msleep( INPUT_IDLE_SLEEP );
534     }
535 }
536
537 /*****************************************************************************
538  * EndThread: end the input thread
539  *****************************************************************************/
540 static void EndThread( input_thread_t * p_input )
541 {
542     int *       pi_status;                                  /* thread status */
543
544     /* Store status */
545     pi_status = p_input->pi_status;
546     *pi_status = THREAD_END;
547
548     if( p_main->b_stats )
549     {
550 #ifdef HAVE_SYS_TIMES_H
551         /* Display statistics */
552         struct tms  cpu_usage;
553         times( &cpu_usage );
554
555         intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
556                       p_input->c_loops,
557                       cpu_usage.tms_utime, cpu_usage.tms_stime );
558 #else
559         intf_StatMsg( "input stats: %d loops", p_input->c_loops );
560 #endif
561
562         input_DumpStream( p_input );
563     }
564
565     /* Free all ES and destroy all decoder threads */
566     input_EndStream( p_input );
567
568     /* Free demultiplexer's data */
569     p_input->pf_end( p_input );
570
571     /* Close the input method */
572     CloseThread( p_input );
573
574     /* Release modules */
575     module_Unneed( p_input->p_input_module );
576 }
577
578 /*****************************************************************************
579  * CloseThread: close the target
580  *****************************************************************************/
581 static void CloseThread( input_thread_t * p_input )
582 {
583 #define f p_input->p_input_module->p_functions->input.functions.input
584
585 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
586     /* Close stream */
587     if( ( strlen( p_input->p_source ) > 10
588           && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
589           || ( strlen( p_input->p_source ) > 4
590                 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
591     {
592         NetworkClose( p_input );
593     }
594     else if( ( strlen( p_input->p_source ) > 5 )
595                && !strncasecmp( p_input->p_source, "http:", 5 ) )
596     {
597         NetworkClose( p_input );
598     }
599     else 
600 #endif
601     if( ( ( strlen( p_input->p_source ) > 4 )
602             && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
603         || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
604     {
605         f.pf_close( p_input );
606     }
607     else if( ( strlen( p_input->p_source ) > 4 )
608                && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
609     {
610         f.pf_close( p_input );
611     }
612     else
613     {
614         FileClose( p_input );
615     }
616 #undef f
617 }
618
619 /*****************************************************************************
620  * DestroyThread: destroy the input thread
621  *****************************************************************************/
622 static void DestroyThread( input_thread_t * p_input )
623 {
624     int *       pi_status;                                  /* thread status */
625
626     /* Store status */
627     pi_status = p_input->pi_status;
628
629     /* Destroy Mutex locks */
630     vlc_mutex_destroy( &p_input->stream.control.control_lock );
631     vlc_mutex_destroy( &p_input->stream.stream_lock );
632     
633     /* Free input structure */
634     free( p_input );
635
636     /* Update status */
637     *pi_status = THREAD_OVER;
638 }
639
640 /*****************************************************************************
641  * StdOpen : open standard input
642  *****************************************************************************/
643 static void StdOpen( input_thread_t * p_input )
644 {
645     vlc_mutex_lock( &p_input->stream.stream_lock );
646
647     /* Suppose we can control the pace - this won't work in some cases ! */
648     p_input->stream.b_pace_control = 1;
649
650     p_input->stream.b_seekable = 0;
651     p_input->stream.p_selected_area->i_size = 0;
652     p_input->stream.p_selected_area->i_tell = 0;
653     vlc_mutex_unlock( &p_input->stream.stream_lock );
654
655     intf_WarnMsg( 2, "input: opening stdin" );
656     p_input->i_handle = 0;
657 }
658
659 /*****************************************************************************
660  * FileOpen : open a file descriptor
661  *****************************************************************************/
662 static void FileOpen( input_thread_t * p_input )
663 {
664     struct stat         stat_info;
665     int                 i_stat;
666
667     char *psz_name = p_input->p_source;
668
669     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
670     {
671         int i_size = strlen( psz_name );
672
673         if( ( i_size > 8 )
674             && !strncasecmp( psz_name, "dvdread:", 8 ) )
675         {
676             /* get rid of the 'dvdread:' stuff and try again */
677             psz_name += 8;
678             i_stat = stat( psz_name, &stat_info );
679         }
680         else if( ( i_size > 4 )
681             && !strncasecmp( psz_name, "dvd:", 4 ) )
682         {
683             /* get rid of the 'dvd:' stuff and try again */
684             psz_name += 4;
685             i_stat = stat( psz_name, &stat_info );
686         }
687         else if( ( i_size > 4 )
688                  && !strncasecmp( psz_name, "vcd:", 4 ) )
689         {
690             /* get rid of the 'vcd:' stuff and try again */
691             psz_name += 4;
692             i_stat = stat( psz_name, &stat_info );
693         }
694         else if( ( i_size > 5 )
695                  && !strncasecmp( psz_name, "file:", 5 ) )
696         {
697             /* get rid of the 'file:' stuff and try again */
698             psz_name += 5;
699             i_stat = stat( psz_name, &stat_info );
700         }
701
702         if( i_stat == (-1) )
703         {
704             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
705                          psz_name, strerror(errno));
706             p_input->b_error = 1;
707             return;
708         }
709     }
710
711     vlc_mutex_lock( &p_input->stream.stream_lock );
712
713     /* If we are here we can control the pace... */
714     p_input->stream.b_pace_control = 1;
715
716     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
717          || S_ISBLK(stat_info.st_mode) )
718     {
719         p_input->stream.b_seekable = 1;
720         p_input->stream.p_selected_area->i_size = stat_info.st_size;
721     }
722     else if( S_ISFIFO(stat_info.st_mode)
723 #if !defined( SYS_BEOS ) && !defined( WIN32 )
724              || S_ISSOCK(stat_info.st_mode)
725 #endif
726              )
727     {
728         p_input->stream.b_seekable = 0;
729         p_input->stream.p_selected_area->i_size = 0;
730     }
731     else
732     {
733         vlc_mutex_unlock( &p_input->stream.stream_lock );
734         intf_ErrMsg( "input error: unknown file type for `%s'",
735                      psz_name );
736         p_input->b_error = 1;
737         return;
738     }
739
740     p_input->stream.p_selected_area->i_tell = 0;
741     vlc_mutex_unlock( &p_input->stream.stream_lock );
742
743     intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
744     if( (p_input->i_handle = open( psz_name,
745                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
746     {
747         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
748         p_input->b_error = 1;
749         return;
750     }
751
752 }
753
754 /*****************************************************************************
755  * FileClose : close a file descriptor
756  *****************************************************************************/
757 static void FileClose( input_thread_t * p_input )
758 {
759     intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
760
761     close( p_input->i_handle );
762
763     return;
764 }
765
766 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
767 /*****************************************************************************
768  * NetworkOpen : open a network socket 
769  *****************************************************************************/
770 static void NetworkOpen( input_thread_t * p_input )
771 {
772     char                *psz_server = NULL;
773     char                *psz_bind = NULL;
774     int                 i_server_port = 0;
775     int                 i_bind_port = 0;
776     int                 i_opt;
777     int                 i_opt_size;
778     struct sockaddr_in  sock;
779
780     /* Get the remote server. Syntax is :
781      * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
782     if( p_input->p_source != NULL )
783     {
784         char * psz_parser = p_input->p_source;
785         char * psz_server_port = NULL;
786         char * psz_bind_port = NULL;
787
788         /* Skip the protocol name */
789         while( *psz_parser && *psz_parser != ':' )
790         {
791             psz_parser++;
792         }
793
794         /* Skip the "://" part */
795         while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
796         {
797             psz_parser++;
798         }
799
800         if( *psz_parser && *psz_parser != '@' )
801         {
802             /* Found server */
803             psz_server = psz_parser;
804
805             while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
806             {
807                 psz_parser++;
808             }
809
810             if( *psz_parser == ':' )
811             {
812                 /* Found server port */
813                 *psz_parser = '\0'; /* Terminate server name */
814                 psz_parser++;
815                 psz_server_port = psz_parser;
816
817                 while( *psz_parser && *psz_parser != '@' )
818                 {
819                     psz_parser++;
820                 }
821             }
822         }
823
824         if( *psz_parser == '@' )
825         {
826             /* Found bind address or bind port */
827             *psz_parser = '\0'; /* Terminate server port or name if necessary */
828             psz_parser++;
829
830             if( *psz_parser && *psz_parser != ':' )
831             {
832                 /* Found bind address */
833                 psz_bind = psz_parser;
834
835                 while( *psz_parser && *psz_parser != ':' )
836                 {
837                     psz_parser++;
838                 }
839             }
840
841             if( *psz_parser == ':' )
842             {
843                 /* Found bind port */
844                 *psz_parser = '\0'; /* Terminate bind address if necessary */
845                 psz_parser++;
846
847                 psz_bind_port = psz_parser;
848             }
849         }
850
851         /* Convert ports format */
852         if( psz_server_port != NULL )
853         {
854             i_server_port = strtol( psz_server_port, &psz_parser, 10 );
855             if( *psz_parser )
856             {
857                 intf_ErrMsg( "input error: cannot parse server port near %s",
858                              psz_parser );
859                 p_input->b_error = 1;
860                 return;
861             }
862         }
863
864         if( psz_bind_port != NULL )
865         {
866             i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
867             if( *psz_parser )
868             {
869                 intf_ErrMsg( "input error: cannot parse bind port near %s",
870                              psz_parser );
871                 p_input->b_error = 1;
872                 return;
873             }
874         }
875     }
876     else
877     {
878         /* This is required or NetworkClose will never be called */
879         p_input->p_source = "ts: network input";
880     }
881
882     /* Check that we got a valid port */
883     if( i_bind_port == 0 )
884     {
885         i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
886     }
887
888     intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
889                      psz_server, i_server_port, psz_bind, i_bind_port );
890
891     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
892      * protocol */
893     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
894     if( p_input->i_handle == -1 )
895     {
896         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
897         p_input->b_error = 1;
898         return;
899     }
900
901     /* We may want to reuse an already used socket */
902     i_opt = 1;
903     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
904                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
905     {
906         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
907                      strerror(errno));
908         close( p_input->i_handle );
909         p_input->b_error = 1;
910         return;
911     }
912
913     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
914      * packet loss caused by scheduling problems */
915     i_opt = 0x80000;
916     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
917                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
918     {
919         intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)", 
920                          strerror(errno));
921     }
922
923     /* Check if we really got what we have asked for, because Linux, etc.
924      * will silently limit the max buffer size to net.core.rmem_max which
925      * is typically only 65535 bytes */
926     i_opt = 0;
927     i_opt_size = sizeof( i_opt );
928     if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
929                     (void*) &i_opt, &i_opt_size ) == -1 )
930     {
931         intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)", 
932                          strerror(errno));
933     }
934     else if( i_opt < 0x80000 )
935     {
936         intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
937                          " instead of 0x%x", i_opt, 0x80000 );
938     }
939
940     /* Build the local socket */
941     if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
942     {
943         intf_ErrMsg( "input error: can't build local address" );
944         close( p_input->i_handle );
945         p_input->b_error = 1;
946         return;
947     }
948
949     /* Bind it */
950     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
951               sizeof( sock ) ) < 0 )
952     {
953         intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
954         close( p_input->i_handle );
955         p_input->b_error = 1;
956         return;
957     }
958
959     /* Allow broadcast reception if we bound on INADDR_ANY */
960     if( psz_bind == NULL )
961     {
962         i_opt = 1;
963         if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
964                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
965         {
966             intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)", 
967                              strerror(errno));
968         }
969     }
970
971     /* Join the multicast group if the socket is a multicast address */
972 #ifndef IN_MULTICAST
973 #   define IN_MULTICAST(a)         IN_CLASSD(a)
974 #endif
975
976     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
977     {
978         struct ip_mreq imr;
979
980         imr.imr_interface.s_addr = INADDR_ANY;
981         imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
982         if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
983                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
984         {
985             intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
986                          strerror(errno) );
987             close( p_input->i_handle );
988             p_input->b_error = 1;
989             return;
990         }
991     }
992     
993     if( psz_server != NULL )
994     {
995         /* Build socket for remote connection */
996         if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
997         {
998             intf_ErrMsg( "input error: can't build remote address" );
999             close( p_input->i_handle );
1000             p_input->b_error = 1;
1001             return;
1002         }
1003
1004         /* Connect the socket */
1005         if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1006                      sizeof( sock ) ) == (-1) )
1007         {
1008             intf_ErrMsg( "input error: can't connect socket (%s)", 
1009                          strerror(errno) );
1010             close( p_input->i_handle );
1011             p_input->b_error = 1;
1012             return;
1013         }
1014     }
1015
1016     p_input->stream.b_pace_control = 0;
1017     p_input->stream.b_seekable = 0;
1018
1019     intf_WarnMsg( 3, "input: successfully opened network mode" );
1020     
1021     return;
1022 }
1023
1024 /*****************************************************************************
1025  * NetworkClose : close a network socket
1026  *****************************************************************************/
1027 static void NetworkClose( input_thread_t * p_input )
1028 {
1029     intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1030
1031     close( p_input->i_handle );
1032 }
1033
1034 /*****************************************************************************
1035  * HTTPOpen : make an HTTP request
1036  *****************************************************************************/
1037 static void HTTPOpen( input_thread_t * p_input )
1038 {
1039     char                *psz_server = NULL;
1040     char                *psz_path = NULL;
1041     char                *psz_proxy;
1042     int                 i_port = 0;
1043     int                 i_opt;
1044     struct sockaddr_in  sock;
1045     char                psz_buffer[256];
1046
1047     /* Get the remote server */
1048     if( p_input->p_source != NULL )
1049     {
1050         psz_server = p_input->p_source;
1051
1052         /* Skip the protocol name */
1053         while( *psz_server && *psz_server != ':' )
1054         {
1055             psz_server++;
1056         }
1057
1058         /* Skip the "://" part */
1059         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1060         {
1061             psz_server++;
1062         }
1063
1064         /* Found a server name */
1065         if( *psz_server )
1066         {
1067             char *psz_port = psz_server;
1068
1069             /* Skip the hostname part */
1070             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1071             {
1072                 psz_port++;
1073             }
1074
1075             /* Found a port name */
1076             if( *psz_port )
1077             {
1078                 if( *psz_port == ':' )
1079                 {
1080                     /* Replace ':' with '\0' */
1081                     *psz_port = '\0';
1082                     psz_port++;
1083                 }
1084
1085                 psz_path = psz_port;
1086                 while( *psz_path && *psz_path != '/' )
1087                 {
1088                     psz_path++;
1089                 }
1090
1091                 if( *psz_path )
1092                 {
1093                     *psz_path = '\0';
1094                     psz_path++;
1095                 }
1096                 else
1097                 {
1098                     psz_path = NULL;
1099                 }
1100
1101                 if( *psz_port != '\0' )
1102                 {
1103                     i_port = atoi( psz_port );
1104                 }
1105             }
1106         }
1107         else
1108         {
1109             psz_server = NULL;
1110         }
1111     }
1112
1113     /* Check that we got a valid server */
1114     if( psz_server == NULL )
1115     {
1116         intf_ErrMsg( "input error: No server given" );
1117         p_input->b_error = 1;
1118         return;
1119     }
1120
1121     /* Check that we got a valid port */
1122     if( i_port == 0 )
1123     {
1124         i_port = 80; /* FIXME */
1125     }
1126
1127     intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1128                   i_port, psz_path );
1129
1130     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1131      *      * protocol */
1132     p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1133     if( p_input->i_handle == -1 )
1134     {
1135         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );        p_input->b_error = 1;
1136         return;
1137     }
1138
1139     /* We may want to reuse an already used socket */
1140     i_opt = 1;
1141     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1142                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1143     {
1144         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1145                      strerror(errno));
1146         close( p_input->i_handle );
1147         p_input->b_error = 1;
1148         return;
1149     }
1150
1151     /* Check proxy */
1152     if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1153     {
1154         /* http://myproxy.mydomain:myport/ */
1155         int                 i_proxy_port = 0;
1156
1157         /* Skip the protocol name */
1158         while( *psz_proxy && *psz_proxy != ':' )
1159         {
1160             psz_proxy++;
1161         }
1162
1163         /* Skip the "://" part */
1164         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1165         {
1166             psz_proxy++;
1167         }
1168
1169         /* Found a proxy name */
1170         if( *psz_proxy )
1171         {
1172             char *psz_port = psz_proxy;
1173
1174             /* Skip the hostname part */
1175             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1176             {
1177                 psz_port++;
1178             }
1179
1180             /* Found a port name */
1181             if( *psz_port )
1182             {
1183                 char * psz_junk;
1184
1185                 /* Replace ':' with '\0' */
1186                 *psz_port = '\0';
1187                 psz_port++;
1188
1189                 psz_junk = psz_port;
1190                 while( *psz_junk && *psz_junk != '/' )
1191                 {
1192                     psz_junk++;
1193                 }
1194
1195                 if( *psz_junk )
1196                 {
1197                     *psz_junk = '\0';
1198                 }
1199
1200                 if( *psz_port != '\0' )
1201                 {
1202                     i_proxy_port = atoi( psz_port );
1203                 }
1204             }
1205         }
1206         else
1207         {
1208             intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1209             close( p_input->i_handle );
1210             p_input->b_error = 1;
1211             return;
1212         }
1213
1214         /* Build socket for proxy connection */
1215         if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1216         {
1217             intf_ErrMsg( "input error: can't build remote address" );
1218             close( p_input->i_handle );
1219             p_input->b_error = 1;
1220             return;
1221         }
1222     }
1223     else
1224     {
1225         /* No proxy, direct connection */
1226         if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1227         {
1228             intf_ErrMsg( "input error: can't build remote address" );
1229             close( p_input->i_handle );
1230             p_input->b_error = 1;
1231             return;
1232         }
1233     }
1234
1235     /* Connect the socket */
1236     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1237                  sizeof( sock ) ) == (-1) )
1238     {
1239         intf_ErrMsg( "input error: can't connect socket (%s)",
1240                      strerror(errno) );
1241         close( p_input->i_handle );
1242         p_input->b_error = 1;
1243         return;
1244     }
1245
1246     p_input->stream.b_seekable = 0;
1247     p_input->stream.b_pace_control = 1; /* TCP/IP... */
1248
1249 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1250 #   define HTTP_END       "\r\n"
1251
1252     /* Prepare GET ... */
1253     if( psz_proxy != NULL )
1254     {
1255         snprintf( psz_buffer, sizeof(psz_buffer),
1256                   "GET http://%s:%d/%s HTTP/1.0\r\n"
1257                   HTTP_USERAGENT HTTP_END,
1258                   psz_server, i_port, psz_path );
1259     }
1260     else
1261     {
1262         snprintf( psz_buffer, sizeof(psz_buffer),
1263                   "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1264                   HTTP_USERAGENT HTTP_END,
1265                   psz_path, psz_server );
1266     }
1267     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1268
1269     /* Send GET ... */
1270     if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1271     {
1272         intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1273         close( p_input->i_handle );
1274         p_input->b_error = 1;
1275         return;
1276     }
1277
1278     /* Read HTTP header - this is gonna be fun with plug-ins which do not
1279      * use p_input->p_stream :-( */
1280     if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1281     {
1282         intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1283         close( p_input->i_handle );
1284         p_input->b_error = 1;
1285         return;
1286     }
1287
1288     while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1289     {
1290         if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1291              || *psz_buffer == '\r' || *psz_buffer == '\0' )
1292         {
1293             break;
1294         }
1295         /* FIXME : check Content-Type one day */
1296     }
1297
1298     intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1299 }
1300
1301 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */
1302