input.c 29.7 KB
Newer Older
1
2
/*****************************************************************************
 * input.c: input thread
Michel Kaempf's avatar
Michel Kaempf committed
3
4
 * Read an MPEG2 stream, demultiplex and parse it before sending it to
 * decoders.
5
 *****************************************************************************
6
 * Copyright (C) 1998-2002 VideoLAN
7
 * $Id: input.c,v 1.237 2003/09/07 22:51:11 fenrir Exp $
8
 *
9
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
10
11
12
13
14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16
17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21
22
23
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

26
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
27
 * Preamble
28
 *****************************************************************************/
29
#include <stdlib.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
30

31
#include <vlc/vlc.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
32

33
#include <string.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
34

35
36
#ifdef HAVE_SYS_TIMES_H
#   include <sys/times.h>
37
#endif
38

39
#include "vlc_playlist.h"
40

41
42
43
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
44
#include "input_ext-plugins.h"
Michel Lespinasse's avatar
Yop,    
Michel Lespinasse committed
45

46
#include "stream_output.h"
47
#include <vlc/vout.h>
48

49
#include "vlc_interface.h"
50
#include "ninput.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
51

52
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
53
 * Local prototypes
54
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
55
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
56
57
58
static  int InitThread      ( input_thread_t *p_input );
static void ErrorThread     ( input_thread_t *p_input );
static void EndThread       ( input_thread_t *p_input );
Michel Kaempf's avatar
Michel Kaempf committed
59

gbazin's avatar
   
gbazin committed
60
61
static void ParseOption     ( input_thread_t *p_input,
                              const char *psz_option );
62
63
64
65
66
67
68
69
70
71
72
/*****************************************************************************
 * Callbacks
 *****************************************************************************/
static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int TimeCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int StateCallback   ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data );
gbazin's avatar
   
gbazin committed
73

74
/*****************************************************************************
75
 * input_CreateThread: creates a new input thread
76
 *****************************************************************************
77
78
 * This function creates a new input, and returns a pointer
 * to its description. On error, it returns NULL.
79
 *****************************************************************************/
80
input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
81
                                      playlist_item_t *p_item )
Michel Kaempf's avatar
Michel Kaempf committed
82
{
83
    input_thread_t *    p_input;                        /* thread descriptor */
84
    input_info_category_t * p_info;
85
    vlc_value_t val;
gbazin's avatar
   
gbazin committed
86
    int i;
87

88
    /* Allocate descriptor */
89
    p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
90
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
91
    {
92
        msg_Err( p_parent, "out of memory" );
93
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
94
    }
95

gbazin's avatar
   
gbazin committed
96
97
98
99
100
101
    /* Parse input options */
    for( i = 0; i < p_item->i_options; i++ )
    {
        ParseOption( p_input, p_item->ppsz_options[i] );
    }

gbazin's avatar
   
gbazin committed
102
103
104
105
106
107
108
109
110
    /* Create a few object variables we'll need later on */
    var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
    var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );

    var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    var_Create( p_input, "sout-keep",  VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

    /* play status */

    /* position variable */
    var_Create( p_input, "position",  VLC_VAR_FLOAT );  /* position 0.0 -> 1.0 */
    val.f_float = 0.0;
    var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "position", PositionCallback, NULL );

    /* time variable */
    var_Create( p_input, "time",  VLC_VAR_TIME );
    val.i_time = 0;
    var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "time", TimeCallback, NULL );

    /* length variable */
    var_Create( p_input, "length",  VLC_VAR_TIME );
    val.i_time = 0;
    var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );

    /* rate variable */
    var_Create( p_input, "rate", VLC_VAR_INTEGER );
    var_Create( p_input, "rate-slower", VLC_VAR_VOID );
    var_Create( p_input, "rate-faster", VLC_VAR_VOID );
    val.i_int = DEFAULT_RATE;
    var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "rate", RateCallback, NULL );
    var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
    var_AddCallback( p_input, "rate-faster", RateCallback, NULL );

    /* state variable */
    var_Create( p_input, "state", VLC_VAR_INTEGER );
    val.i_int = INIT_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "state", StateCallback, NULL );

gbazin's avatar
   
gbazin committed
148

149
    /* Initialize thread properties */
Sam Hocevar's avatar
   
Sam Hocevar committed
150
    p_input->b_eof      = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
151
152

    /* Set target */
153
    p_input->psz_source = strdup( p_item->psz_uri );
Sam Hocevar's avatar
   
Sam Hocevar committed
154

Stéphane Borel's avatar
   
Stéphane Borel committed
155
    /* Demux */
156
157
158
159
    p_input->p_demux   = NULL;
    p_input->pf_demux  = NULL;
    p_input->pf_rewind = NULL;
    p_input->pf_demux_control = NULL;
Stéphane Borel's avatar
   
Stéphane Borel committed
160
161

    /* Access */
162
    p_input->p_access = NULL;
163

Sam Hocevar's avatar
   
Sam Hocevar committed
164
165
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
166
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
167

168
169
170
171
172
173
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
174
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
175
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
176
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
177

178
    /* Initialize stream description */
Stéphane Borel's avatar
   
Stéphane Borel committed
179
    p_input->stream.b_changed = 0;
180
181
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
182
    p_input->stream.i_pgrm_number = 0;
183
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
184
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
185
    p_input->stream.i_mux_rate = 0;
186
    p_input->stream.b_seekable = 0;
187
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
188

Sam Hocevar's avatar
   
Sam Hocevar committed
189
190
191
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
192
193
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
194
    p_input->stream.p_selected_area = NULL;
195
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
196

Sam Hocevar's avatar
   
Sam Hocevar committed
197
198
199
200
    p_input->stream.pp_selected_es = NULL;
    p_input->stream.p_removed_es = NULL;
    p_input->stream.p_newly_selected_es = NULL;

Sam Hocevar's avatar
   
Sam Hocevar committed
201
    /* By default there is one area in a stream */
gbazin's avatar
   
gbazin committed
202
    input_AddArea( p_input, 0, 1 );
203
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
   
Stéphane Borel committed
204

205
    /* Initialize stream control properties. */
206
    p_input->stream.control.i_status = INIT_S;
207
208
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
209
210
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

211
212
213
214
215
216
217
218
219
220
221
    /* Initialize input info */
    p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
    if( !p_input->stream.p_info )
    {
        msg_Err( p_input, "No memory!" );
        return NULL;
    }
    p_input->stream.p_info->psz_name = strdup("General") ;
    p_input->stream.p_info->p_info = NULL;
    p_input->stream.p_info->p_next = NULL;

222
    msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
Michel Kaempf's avatar
Michel Kaempf committed
223

224
225
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
226
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
227

Sam Hocevar's avatar
Sam Hocevar committed
228
    /* Create thread and wait for its readiness. */
229
230
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
231
    {
232
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
233
        free( p_input );
234
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
235
    }
236

237
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
238
239
}

240
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
241
 * input_StopThread: mark an input thread as zombie
242
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
243
 * This function should not return until the thread is effectively cancelled.
244
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
245
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
246
{
Sam Hocevar's avatar
   
Sam Hocevar committed
247
248
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
249
250
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
251

252
253
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
254
255
256
257
258
259
260
261
262
263
}

/*****************************************************************************
 * input_DestroyThread: mark an input thread as zombie
 *****************************************************************************
 * This function should not return until the thread is effectively cancelled.
 *****************************************************************************/
void input_DestroyThread( input_thread_t *p_input )
{
    /* Join the thread */
264
    vlc_thread_join( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
265
266
267

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
268
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
   
Sam Hocevar committed
269
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
270
271
}

272
/*****************************************************************************
273
 * RunThread: main thread loop
274
 *****************************************************************************
275
 * Thread in charge of processing the network packets and demultiplexing.
276
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
277
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
278
{
279
280
281
    vlc_value_t  val;
    mtime_t      i_update_next = -1;

Sam Hocevar's avatar
Sam Hocevar committed
282
283
284
    /* Signal right now, otherwise we'll get stuck in a peek */
    vlc_thread_ready( p_input );

Sam Hocevar's avatar
   
Sam Hocevar committed
285
286
287
288
289
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
        p_input->b_error = 1;
        ErrorThread( p_input );
Sam Hocevar's avatar
Sam Hocevar committed
290
        p_input->b_dead = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
291
        return 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
292
    }
Michel Kaempf's avatar
Michel Kaempf committed
293

294
    /* initialization is complete */
295
    vlc_mutex_lock( &p_input->stream.stream_lock );
296
297
    p_input->stream.b_changed        = 1;
    p_input->stream.control.i_status = PLAYING_S;
298
299
    vlc_mutex_unlock( &p_input->stream.stream_lock );

300
301
302
    val.i_int = PLAYING_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

Sam Hocevar's avatar
   
Sam Hocevar committed
303
304
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
305
        unsigned int i, i_count;
306

Sam Hocevar's avatar
   
Sam Hocevar committed
307
        p_input->c_loops++;
Sam Hocevar's avatar
   
Sam Hocevar committed
308

309
        vlc_mutex_lock( &p_input->stream.stream_lock );
310

311
312
313
314
315
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
   
Johan Bilien committed
316
317
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
318
319

                p_input->pf_set_program( p_input,
320
                                         p_input->stream.p_new_program );
321

322
323
324
325
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

326
327
328
329
330
331
332
333
334
335
336
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
                    pgrm_descriptor_t * p_pgrm
                                            = p_input->stream.pp_programs[i];

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
            }
            p_input->stream.p_new_program = NULL;
        }
337

338
339
        if( p_input->stream.p_new_area )
        {
340
341
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
342
                input_AccessReinit( p_input );
343
344
345

                p_input->pf_set_area( p_input, p_input->stream.p_new_area );

346
347
348
349
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

350
351
352
353
354
355
356
357
358
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
                    pgrm_descriptor_t * p_pgrm
                                            = p_input->stream.pp_programs[i];

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
            }
359
360
361
            p_input->stream.p_new_area = NULL;
        }

362
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
   
Sam Hocevar committed
363
        {
364
            if( p_input->stream.p_selected_area->i_size > 0 )
365
            {
366
367
368
                unsigned int i;
                double f = (double)p_input->stream.p_selected_area->i_seek /
                           (double)p_input->stream.p_selected_area->i_size;
369

Christophe Massiot's avatar
Christophe Massiot committed
370
                vlc_mutex_unlock( &p_input->stream.stream_lock );
371
                demux_Control( p_input, DEMUX_SET_POSITION, f );
Christophe Massiot's avatar
Christophe Massiot committed
372
                vlc_mutex_lock( &p_input->stream.stream_lock );
373

374
375
376
377
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

378
379
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
380
                    pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
381
382
383
384
385

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
            }
386
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
   
Sam Hocevar committed
387
        }
388

389
390
391
392
393
394
395
396
397
398
399
400
        if( p_input->stream.p_removed_es )
        {
            input_UnselectES( p_input, p_input->stream.p_removed_es );
            p_input->stream.p_removed_es = NULL;
        }

        if( p_input->stream.p_newly_selected_es )
        {
            input_SelectES( p_input, p_input->stream.p_newly_selected_es );
            p_input->stream.p_newly_selected_es = NULL;
        }

401
402
403
404
405
406
407
408
409
410
411
412
413
414
        if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
        {
            if( p_input->stream.b_new_mute )
            {
                input_EscapeAudioDiscontinuity( p_input );
            }

            vlc_mutex_lock( &p_input->stream.control.control_lock );
            p_input->stream.control.b_mute = p_input->stream.b_new_mute;
            vlc_mutex_unlock( &p_input->stream.control.control_lock );

            p_input->stream.b_new_mute = MUTE_NO_CHANGE;
        }

415
        vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
416

Christophe Massiot's avatar
Christophe Massiot committed
417
418
        /* Read and demultiplex some data. */
        i_count = p_input->pf_demux( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
419

gbazin's avatar
   
gbazin committed
420
        if( i_count == 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
421
        {
422
            /* End of file - we do not set b_die because only the
423
             * playlist is allowed to do so. */
424
            msg_Info( p_input, "EOF reached" );
425
426
427
428
429
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
430
        }
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

        if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
        {
            double d;

            /* update input status variables */
            if( !demux_Control( p_input, DEMUX_GET_POSITION, &d ) )
            {
                val.f_float = (float)d;
                var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
            }
            if( !demux_Control( p_input, DEMUX_GET_TIME, &val.i_time ) )
            {
                var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
            }
            if( !demux_Control( p_input, DEMUX_GET_LENGTH, &val.i_time ) )
            {
                var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
            }

            i_update_next = mdate() + 200000LL;
        }
453
454
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
455
    if( p_input->b_error || p_input->b_eof )
456
457
458
    {
        ErrorThread( p_input );
    }
459

460
    EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
461

Sam Hocevar's avatar
   
Sam Hocevar committed
462
    return 0;
463
464
}

465
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
466
 * InitThread: init the input Thread
467
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
468
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
469
{
Christophe Massiot's avatar
Christophe Massiot committed
470
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
471
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
gbazin's avatar
   
gbazin committed
472
    vlc_value_t val;
gbazin's avatar
   
gbazin committed
473

Christophe Massiot's avatar
Christophe Massiot committed
474
475
476
477
478
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
479
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
480
    if( psz_parser - p_input->psz_dupsource == 1 )
gbazin's avatar
   
gbazin committed
481
    {
482
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
483
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
   
Stéphane Borel committed
484
        psz_parser = "";
gbazin's avatar
   
gbazin committed
485
486
    }
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
487

Christophe Massiot's avatar
Christophe Massiot committed
488
    if( !*psz_parser )
489
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
490
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
491
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
492
        free( p_input->psz_dupsource );
493
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
494
    }
Christophe Massiot's avatar
Christophe Massiot committed
495
496
497
    else
    {
        *psz_parser++ = '\0';
498

Xavier Marchesini's avatar
   
Xavier Marchesini committed
499
500
501
502
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
503
        }
Xavier Marchesini's avatar
   
Xavier Marchesini committed
504
505

        p_input->psz_name = psz_parser ;
Sam Hocevar's avatar
   
Sam Hocevar committed
506

Christophe Massiot's avatar
Christophe Massiot committed
507
        /* Come back to parse the access and demux plug-ins */
Christophe Massiot's avatar
Christophe Massiot committed
508
        psz_parser = p_input->psz_dupsource;
gbazin's avatar
   
gbazin committed
509

Stéphane Borel's avatar
   
Stéphane Borel committed
510
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
511
512
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
513
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
514
515
516
517
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
518
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
            psz_parser++;
        }
        else
        {
            p_input->psz_access = psz_parser;

            while( *psz_parser && *psz_parser != '/' )
            {
                psz_parser++;
            }

            if( *psz_parser == '/' )
            {
                *psz_parser++ = '\0';
            }
        }
gbazin's avatar
   
gbazin committed
535

Christophe Massiot's avatar
Christophe Massiot committed
536
537
538
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
   
Sam Hocevar committed
539
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
540
541
542
543
544
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
545
    }
Christophe Massiot's avatar
Christophe Massiot committed
546

547
548
    msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
             p_input->psz_access, p_input->psz_demux, p_input->psz_name );
Christophe Massiot's avatar
Christophe Massiot committed
549
550

    if( input_AccessInit( p_input ) == -1 )
551
    {
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
        return VLC_EGENERIC;
    }

    /* Initialize optional stream output. (before demuxer)*/
    var_Get( p_input, "sout", &val );
    if( val.psz_string != NULL )
    {
        if ( *val.psz_string && (p_input->stream.p_sout =
             sout_NewInstance( p_input, val.psz_string )) == NULL )
        {
            msg_Err( p_input, "cannot start stream output instance, aborting" );
            free( val.psz_string );
            return VLC_EGENERIC;
        }
        free( val.psz_string );
567
    }
Christophe Massiot's avatar
Christophe Massiot committed
568

569
    /* Find and open appropriate access module */
570
571
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
572

573
574
575
576
577
578
579
580
581
582
583
584
585
586
    if ( p_input->p_access == NULL
          && (*p_input->psz_demux || *p_input->psz_access) )
    {
        /* Maybe we got something like :
         * /Volumes/toto:titi/gabu.mpg */
        p_input->psz_access = p_input->psz_demux = "";
        p_input->psz_name = p_input->psz_source;
        free( p_input->psz_dupsource);
        p_input->psz_dupsource = NULL;

        p_input->p_access = module_Need( p_input, "access",
                                         p_input->psz_access );
    }

587
    if( p_input->p_access == NULL )
588
    {
589
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
590
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
591
592
593
594
595
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        return VLC_EGENERIC;
596
    }
Christophe Massiot's avatar
Christophe Massiot committed
597
598
599

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
600
    {
Christophe Massiot's avatar
Christophe Massiot committed
601
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
602
    }
603
604
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
605
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
606
    }
607

gbazin's avatar
   
gbazin committed
608
609
610
611
612
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
    if( p_input->p_vlc->i_desync < 0 )
        p_input->i_pts_delay -= p_input->p_vlc->i_desync;

Sam Hocevar's avatar
   
Sam Hocevar committed
613
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
614
    {
Christophe Massiot's avatar
Christophe Massiot committed
615
616
        while( !input_FillBuffer( p_input ) )
        {
617
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
618
            {
619
                module_Unneed( p_input, p_input->p_access );
620
621
622
623
624
                if ( p_input->stream.p_sout != NULL )
                {
                    sout_DeleteInstance( p_input->stream.p_sout );
                }
                return VLC_EGENERIC;
Christophe Massiot's avatar
Christophe Massiot committed
625
626
            }
        }
627
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
628

629
    /* Find and open appropriate demux module */
630
631
    p_input->p_demux = module_Need( p_input, "demux",
                                    p_input->psz_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
632

633
    if( p_input->p_demux == NULL )
634
    {
635
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
636
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
637
        module_Unneed( p_input, p_input->p_access );
638
        if ( p_input->stream.p_sout != NULL )
639
        {
640
            sout_DeleteInstance( p_input->stream.p_sout );
641
        }
642
        return VLC_EGENERIC;
643
644
    }

645
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
646
647
}

648
/*****************************************************************************
649
 * ErrorThread: RunThread() error loop
650
 *****************************************************************************
651
 * This function is called when an error occured during thread main's loop.
652
 *****************************************************************************/
653
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
654
{
655
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
656
    {
657
658
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
659
660
661
    }
}

662
/*****************************************************************************
663
 * EndThread: end the input thread
664
 *****************************************************************************/
665
static void EndThread( input_thread_t * p_input )
666
{
667
#ifdef HAVE_SYS_TIMES_H
668
669
670
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
671

672
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
673
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
674
#else
675
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
676
#endif
677

678
679
    input_DumpStream( p_input );

Sam Hocevar's avatar
   
Sam Hocevar committed
680
681
682
    /* Free all ES and destroy all decoder threads */
    input_EndStream( p_input );

683
684
685
    /* Close optional stream output instance */
    if ( p_input->stream.p_sout != NULL )
    {
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        vlc_object_t *p_pl = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
        vlc_value_t keep;

        if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
        {
            /* attach sout to the playlist */
            msg_Warn( p_input, "keeping sout" );
            vlc_object_detach( p_input->stream.p_sout );
            vlc_object_attach( p_input->stream.p_sout, p_pl );
        }
        else
        {
            msg_Warn( p_input, "destroying sout" );
            sout_DeleteInstance( p_input->stream.p_sout );
        }
        if( p_pl )
        {
            vlc_object_release( p_pl );
        }
705
706
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
707
    /* Free demultiplexer's data */
708
    module_Unneed( p_input, p_input->p_demux );
Sam Hocevar's avatar
   
Sam Hocevar committed
709

710
    /* Close the access plug-in */
711
    module_Unneed( p_input, p_input->p_access );
Sam Hocevar's avatar
   
Sam Hocevar committed
712

Christophe Massiot's avatar
Christophe Massiot committed
713
714
    input_AccessEnd( p_input );

715
716
717
718
    /* Free info structures XXX destroy es before 'cause vorbis */
    msg_Dbg( p_input, "freeing info structures...");
    input_DelInfo( p_input );

Christophe Massiot's avatar
Christophe Massiot committed
719
    free( p_input->psz_source );
720
    if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
721
722
723

    /* Tell we're dead */
    p_input->b_dead = 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
724
725
}

gbazin's avatar
   
gbazin committed
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*****************************************************************************
 * ParseOption: parses the options for the input
 *****************************************************************************
 * This function parses the input (config) options and creates their associated
 * object variables.
 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
 * bar is the value of the option.
 *****************************************************************************/
static void ParseOption( input_thread_t *p_input, const char *psz_option )
{
    char *psz_name = (char *)psz_option;
    char *psz_value = strchr( psz_option, '=' );
    int  i_name_len, i_type;
    vlc_bool_t b_isno = VLC_FALSE;
    vlc_value_t val;

    if( psz_value ) i_name_len = psz_value - psz_option;
    else i_name_len = strlen( psz_option );

    /* It's too much of an hassle to remove the ':' when we parse
     * the cmd line :) */
    if( i_name_len && *psz_name == ':' )
    {
        psz_name++;
        i_name_len--;
    }

    if( i_name_len == 0 ) return;

    psz_name = strndup( psz_name, i_name_len );
    if( psz_value ) psz_value++;

    i_type = config_GetType( p_input, psz_name );

    if( !i_type && !psz_value )
    {
        /* check for "no-foo" or "nofoo" */
        if( !strncmp( psz_name, "no-", 3 ) )
        {
            memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
        }
        else if( !strncmp( psz_name, "no", 2 ) )
        {
            memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
        }
        else goto cleanup;           /* Option doesn't exist */

        b_isno = VLC_TRUE;
        i_type = config_GetType( p_input, psz_name );

        if( !i_type ) goto cleanup;  /* Option doesn't exist */
    }
    else if( !i_type ) goto cleanup; /* Option doesn't exist */

    if( ( i_type != VLC_VAR_BOOL ) &&
        ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */

    /* Create the variable in the input object.
     * Children of the input object will be able to retreive this value
     * thanks to the inheritance property of the object variables. */
    var_Create( p_input, psz_name, i_type );

    switch( i_type )
    {
    case VLC_VAR_BOOL:
        val.b_bool = !b_isno;
        break;

    case VLC_VAR_INTEGER:
        val.i_int = atoi( psz_value );
        break;

    case VLC_VAR_FLOAT:
        val.f_float = atof( psz_value );
        break;

    case VLC_VAR_STRING:
    case VLC_VAR_FILE:
    case VLC_VAR_DIRECTORY:
        val.psz_string = psz_value;
        break;

    default:
        goto cleanup;
        break;
    }

    var_Set( p_input, psz_name, val );

    msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );

  cleanup:
    if( psz_name ) free( psz_name );
    return;
}
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905

/*****************************************************************************
 * Callbacks  (position, time, state, rate )
 *****************************************************************************/
static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    input_thread_t *p_input = (input_thread_t *)p_this;

    msg_Warn( p_input, "cmd=%s old=%f new=%f",
              psz_cmd,
              oldval.f_float, newval.f_float );

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.p_selected_area->i_seek =
        (int64_t)( newval.f_float * (double)p_input->stream.p_selected_area->i_size );
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return VLC_SUCCESS;
}

static int TimeCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    input_thread_t *p_input = (input_thread_t *)p_this;

    /* FIXME TODO FIXME */
    msg_Warn( p_input, "cmd=%s old=%lld new=%lld",
              psz_cmd,
              oldval.i_time, newval.i_time );

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.p_selected_area->i_seek =
        newval.i_time / 1000000 * 50 * p_input->stream.i_mux_rate;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    return VLC_SUCCESS;
}

static int StateCallback   ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
    msg_Warn( p_input, "cmd=%s old=%d new=%d",
              psz_cmd, oldval.i_int, newval.i_int );

    switch( newval.i_int )
    {
        case PLAYING_S:
            input_SetStatus( p_input, INPUT_STATUS_PLAY );
            return VLC_SUCCESS;
        case PAUSE_S:
            input_SetStatus( p_input, INPUT_STATUS_PAUSE );
            return VLC_SUCCESS;
        case END_S:
            input_SetStatus( p_input, INPUT_STATUS_END );
            return VLC_SUCCESS;
        default:
            msg_Err( p_input, "cannot set new state (invalid)" );
            return VLC_EGENERIC;
    }
}

static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    input_thread_t *p_input = (input_thread_t *)p_this;

    if( !strcmp( psz_cmd, "rate-slower" ) )
    {
        input_SetStatus( p_input, INPUT_STATUS_SLOWER );
    }
    else if( !strcmp( psz_cmd, "rate-faster" ) )
    {
        input_SetStatus( p_input, INPUT_STATUS_FASTER );
    }
    else
    {
        msg_Warn( p_input, "cmd=%s old=%d new=%d",
                  psz_cmd, oldval.i_int, newval.i_int );
        input_SetRate( p_input, newval.i_int );
    }
    return VLC_SUCCESS;
}