input.c 38 KB
Newer Older
1
2
/*****************************************************************************
 * input.c: input thread
3
 * Read a stream, demultiplex and parse it before sending it to
Michel Kaempf's avatar
Michel Kaempf committed
4
 * decoders.
5
 *****************************************************************************
zorglub's avatar
zorglub committed
6
7
 * Copyright (C) 1998-2004 VideoLAN
 * $Id: input.c,v 1.273 2004/01/06 12:02:06 zorglub 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>
32
#include <vlc/input.h>
33
#include <vlc/decoder.h>
34
#include <vlc/vout.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
35

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

40
#include "vlc_playlist.h"
41

42
43
#include "stream_output.h"

44
#include "vlc_interface.h"
45
#include "codecs.h"
46
#include "../../modules/demux/util/sub.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
47

48
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
49
 * Local prototypes
50
 *****************************************************************************/
51
52
53
54
55
56
57
struct input_thread_sys_t
{
    /* subtitles */
    int              i_sub;
    subtitle_demux_t **sub;
};

Sam Hocevar's avatar
   
Sam Hocevar committed
58
static  int RunThread       ( input_thread_t *p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
59
60
61
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
62

gbazin's avatar
   
gbazin committed
63
64
static void ParseOption     ( input_thread_t *p_input,
                              const char *psz_option );
65

66
67
68
69
70
71
72
73
74
75
76
/*****************************************************************************
 * 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
77

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

93
    /* Allocate descriptor */
94
    p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
95
    if( p_input == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
96
    {
97
        msg_Err( p_parent, "out of memory" );
98
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
99
    }
100

gbazin's avatar
   
gbazin committed
101
    /* Parse input options */
102
    for( i = 0 ; i < p_item->i_categories ; i++ )
gbazin's avatar
   
gbazin committed
103
    {
104
105
106
107
108
109
110
111
112
113
114
115
116
        if( !strncmp( p_item->pp_categories[i]->psz_name, "Options", 7 ) )
        {
            msg_Dbg(p_input,"Parsing %i options for item",
                             p_item->pp_categories[i]->i_infos );
            for( j = 0; j< p_item->pp_categories[i]->i_infos ; j++ )
            {
                msg_Dbg(p_input,"Option : %s",
                         p_item->pp_categories[i]->pp_infos[j]->psz_name);
                ParseOption( p_input,
                             p_item->pp_categories[i]->pp_infos[j]->psz_value);
            }
            break;
        }
gbazin's avatar
   
gbazin committed
117
118
    }

gbazin's avatar
   
gbazin committed
119
120
121
122
123
    /* 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 );
124
125
    var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
126
    var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
127
128

    var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
129
    var_Create( p_input, "sout-all",   VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
130
131
    var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
132
133
    var_Create( p_input, "sout-keep",  VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

134
135
136
    /* decoders */
    var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

137
138
139
    /* play status */

    /* position variable */
140
141
    var_Create( p_input, "position",  VLC_VAR_FLOAT );  /* position 0.0->1.0 */
    var_Create( p_input, "position-offset",  VLC_VAR_FLOAT );  /* relative */
142
143
144
    val.f_float = 0.0;
    var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "position", PositionCallback, NULL );
145
    var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
146
147
148

    /* time variable */
    var_Create( p_input, "time",  VLC_VAR_TIME );
149
    var_Create( p_input, "time-offset",  VLC_VAR_TIME );    /* relative */
150
151
152
    val.i_time = 0;
    var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
    var_AddCallback( p_input, "time", TimeCallback, NULL );
153
    var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

    /* 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 );

176
177
178
179
    /* state variable */
    var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
    val.b_bool = VLC_FALSE;
    var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
gbazin's avatar
   
gbazin committed
180

181
    /* Initialize thread properties */
Sam Hocevar's avatar
   
Sam Hocevar committed
182
    p_input->b_eof      = 0;
183
    p_input->p_sys      = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
184
185

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

188
189
190
    /* Stream */
    p_input->s = NULL;

191
192
193
    /* es out */
    p_input->p_es_out = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
194
    /* Demux */
195
196
197
198
    p_input->p_demux   = NULL;
    p_input->pf_demux  = NULL;
    p_input->pf_rewind = NULL;
    p_input->pf_demux_control = NULL;
199
    p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
Stéphane Borel's avatar
   
Stéphane Borel committed
200
201

    /* Access */
202
    p_input->p_access = NULL;
203

Sam Hocevar's avatar
   
Sam Hocevar committed
204
205
    p_input->i_bufsize = 0;
    p_input->i_mtu = 0;
gbazin's avatar
   
gbazin committed
206
    p_input->i_pts_delay = DEFAULT_PTS_DELAY;
Sam Hocevar's avatar
   
Sam Hocevar committed
207

208
209
210
211
212
213
    /* Initialize statistics */
    p_input->c_loops                    = 0;
    p_input->stream.c_packets_read      = 0;
    p_input->stream.c_packets_trashed   = 0;

    /* Set locks. */
214
    vlc_mutex_init( p_input, &p_input->stream.stream_lock );
215
    vlc_cond_init( p_input, &p_input->stream.stream_wait );
216
    vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
Michel Kaempf's avatar
Michel Kaempf committed
217

218
    /* Initialize stream description */
Stéphane Borel's avatar
   
Stéphane Borel committed
219
    p_input->stream.b_changed = 0;
220
221
    p_input->stream.i_es_number = 0;
    p_input->stream.i_selected_es_number = 0;
222
    p_input->stream.i_pgrm_number = 0;
223
    p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
224
    p_input->stream.b_new_mute = MUTE_NO_CHANGE;
Christophe Massiot's avatar
Christophe Massiot committed
225
    p_input->stream.i_mux_rate = 0;
226
    p_input->stream.b_seekable = 0;
227
    p_input->stream.p_sout = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
228

Sam Hocevar's avatar
   
Sam Hocevar committed
229
230
231
    /* no stream, no program, no area, no es */
    p_input->stream.p_new_program = NULL;

Stéphane Borel's avatar
   
Stéphane Borel committed
232
233
    p_input->stream.i_area_nb = 0;
    p_input->stream.pp_areas = NULL;
234
    p_input->stream.p_selected_area = NULL;
235
    p_input->stream.p_new_area = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
236

Sam Hocevar's avatar
   
Sam Hocevar committed
237
238
239
240
    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
241
    /* By default there is one area in a stream */
gbazin's avatar
   
gbazin committed
242
    input_AddArea( p_input, 0, 1 );
243
    p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
Stéphane Borel's avatar
   
Stéphane Borel committed
244

245
    /* Initialize stream control properties. */
246
    p_input->stream.control.i_status = INIT_S;
247
248
    p_input->stream.control.i_rate = DEFAULT_RATE;
    p_input->stream.control.b_mute = 0;
249
250
    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );

251
252
253
254
255
256
257
258
259
260
261
    /* 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;

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

264
265
    p_info = input_InfoCategory( p_input, _("General") );
    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
266
    vlc_object_attach( p_input, p_parent );
Sam Hocevar's avatar
   
Sam Hocevar committed
267

Sam Hocevar's avatar
Sam Hocevar committed
268
    /* Create thread and wait for its readiness. */
269
270
    if( vlc_thread_create( p_input, "input", RunThread,
                           VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
Michel Kaempf's avatar
Michel Kaempf committed
271
    {
272
        msg_Err( p_input, "cannot create input thread" );
Michel Kaempf's avatar
Michel Kaempf committed
273
        free( p_input );
274
        return NULL;
Michel Kaempf's avatar
Michel Kaempf committed
275
    }
276

277
    return p_input;
Michel Kaempf's avatar
Michel Kaempf committed
278
279
}

280
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
281
 * input_StopThread: mark an input thread as zombie
282
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
283
 * This function should not return until the thread is effectively cancelled.
284
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
285
void input_StopThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
286
{
Sam Hocevar's avatar
   
Sam Hocevar committed
287
288
    /* Make the thread exit from a possible vlc_cond_wait() */
    vlc_mutex_lock( &p_input->stream.stream_lock );
289
290
    /* Request thread destruction */
    p_input->b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
291

292
293
    vlc_cond_signal( &p_input->stream.stream_wait );
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
294
295
296
297
298
299
300
301
302
303
}

/*****************************************************************************
 * 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 */
304
    vlc_thread_join( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
305
306
307

    /* Destroy Mutex locks */
    vlc_mutex_destroy( &p_input->stream.control.control_lock );
Sam Hocevar's avatar
   
Sam Hocevar committed
308
    vlc_cond_destroy( &p_input->stream.stream_wait );
Sam Hocevar's avatar
   
Sam Hocevar committed
309
    vlc_mutex_destroy( &p_input->stream.stream_lock );
Michel Kaempf's avatar
Michel Kaempf committed
310
311
}

312
/*****************************************************************************
313
 * RunThread: main thread loop
314
 *****************************************************************************
315
 * Thread in charge of processing the network packets and demultiplexing.
316
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
317
static int RunThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
318
{
319
320
321
    vlc_value_t  val;
    mtime_t      i_update_next = -1;

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

Sam Hocevar's avatar
   
Sam Hocevar committed
325
326
327
328
    if( InitThread( p_input ) )
    {
        /* If we failed, wait before we are killed, and exit */
        p_input->b_error = 1;
329

Sam Hocevar's avatar
   
Sam Hocevar committed
330
        ErrorThread( p_input );
331
332
333
334

        /* Tell we're dead */
        p_input->b_dead = 1;

Sam Hocevar's avatar
   
Sam Hocevar committed
335
        return 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
336
    }
Michel Kaempf's avatar
Michel Kaempf committed
337

338
    /* initialization is complete */
339
    vlc_mutex_lock( &p_input->stream.stream_lock );
340
341
    p_input->stream.b_changed        = 1;
    p_input->stream.control.i_status = PLAYING_S;
342
343
    vlc_mutex_unlock( &p_input->stream.stream_lock );

344
345
346
    val.i_int = PLAYING_S;
    var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );

Sam Hocevar's avatar
   
Sam Hocevar committed
347
348
    while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
    {
349
        unsigned int i, i_count;
350

Sam Hocevar's avatar
   
Sam Hocevar committed
351
        p_input->c_loops++;
Sam Hocevar's avatar
   
Sam Hocevar committed
352

353
        vlc_mutex_lock( &p_input->stream.stream_lock );
354

355
356
357
358
359
        if( p_input->stream.p_new_program )
        {
            if( p_input->pf_set_program != NULL )
            {

Johan Bilien's avatar
   
Johan Bilien committed
360
361
                /* Reinitialize buffer manager. */
                input_AccessReinit( p_input );
362
363

                p_input->pf_set_program( p_input,
364
                                         p_input->stream.p_new_program );
365

366
367
368
369
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

370
371
372
373
374
375
376
377
378
379
380
                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;
        }
381

382
383
        if( p_input->stream.p_new_area )
        {
384
385
            if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
            {
386
                input_AccessReinit( p_input );
387
388
389

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

390
391
392
393
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

394
395
396
397
398
399
400
401
402
                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;
                }
            }
403
404
405
            p_input->stream.p_new_area = NULL;
        }

406
        if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
Sam Hocevar's avatar
   
Sam Hocevar committed
407
        {
408
            if( p_input->stream.p_selected_area->i_size > 0 )
409
            {
410
                unsigned int i;
411
                mtime_t      i_time;
412
413
                double f = (double)p_input->stream.p_selected_area->i_seek /
                           (double)p_input->stream.p_selected_area->i_size;
414

Christophe Massiot's avatar
Christophe Massiot committed
415
                vlc_mutex_unlock( &p_input->stream.stream_lock );
416
                demux_Control( p_input, DEMUX_SET_POSITION, f );
Christophe Massiot's avatar
Christophe Massiot committed
417
                vlc_mutex_lock( &p_input->stream.stream_lock );
418

419
420
421
422
                /* Escape all decoders for the stream discontinuity they
                 * will encounter. */
                input_EscapeDiscontinuity( p_input );

423
424
                for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
                {
425
                    pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
426
427
428
429

                    /* Reinitialize synchro. */
                    p_pgrm->i_synchro_state = SYNCHRO_REINIT;
                }
430

Laurent Aimar's avatar
Laurent Aimar committed
431
                vlc_mutex_unlock( &p_input->stream.stream_lock );
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
                {
                    int i;
                    vlc_value_t val;

                    /* Help in bar display */
                    val.i_time = i_time;
                    var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );

                    /* Seek subs */
                    for( i = 0; i < p_input->p_sys->i_sub; i++ )
                    {
                        subtitle_Seek( p_input->p_sys->sub[i], i_time );
                    }
                }
Laurent Aimar's avatar
Laurent Aimar committed
447
                vlc_mutex_lock( &p_input->stream.stream_lock );
448
            }
449
            p_input->stream.p_selected_area->i_seek = NO_SEEK;
Sam Hocevar's avatar
   
Sam Hocevar committed
450
        }
451

452
453
454
455
456
457
458
459
460
461
462
463
        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;
        }

464
465
466
467
468
469
470
471
472
473
474
475
476
477
        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;
        }

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

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

gbazin's avatar
   
gbazin committed
483
        if( i_count == 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
484
        {
485
            /* End of file - we do not set b_die because only the
486
             * playlist is allowed to do so. */
487
            msg_Info( p_input, "EOF reached" );
488
489
490
491
492
            p_input->b_eof = 1;
        }
        else if( i_count < 0 )
        {
            p_input->b_error = 1;
493
        }
494
495
496

        if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
        {
497
498
499
500
            int i;
            mtime_t i_time;
            mtime_t i_length;
            double  d_pos;
501
502

            /* update input status variables */
503
            if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
504
            {
505
                val.f_float = (float)d_pos;
506
507
                var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
            }
508
            if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
509
            {
510
                val.i_time = i_time;
511
512
                var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
            }
513
            if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
514
            {
515
                val.i_time = i_length;
516
517
518
                var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
            }

519
520
521
522
523
524
            /* update subs */
            for( i = 0; i < p_input->p_sys->i_sub; i++ )
            {
                subtitle_Demux( p_input->p_sys->sub[i], i_time );
            }

525
            i_update_next = mdate() + I64C(150000);
526
        }
527
528
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
529
    if( p_input->b_error || p_input->b_eof )
530
531
532
    {
        ErrorThread( p_input );
    }
533

534
    EndThread( p_input );
Sam Hocevar's avatar
   
Sam Hocevar committed
535

Sam Hocevar's avatar
   
Sam Hocevar committed
536
    return 0;
537
538
}

539
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
540
 * InitThread: init the input Thread
541
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
542
static int InitThread( input_thread_t * p_input )
Michel Kaempf's avatar
Michel Kaempf committed
543
{
544
    float f_fps;
sigmunau's avatar
sigmunau committed
545
546
547
    playlist_t *p_playlist;
    mtime_t i_length;
    
Christophe Massiot's avatar
Christophe Massiot committed
548
    /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
549
    char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
gbazin's avatar
   
gbazin committed
550
    vlc_value_t val;
551
552
    subtitle_demux_t *p_sub;
    int64_t i_microsecondperframe;
gbazin's avatar
   
gbazin committed
553

Christophe Massiot's avatar
Christophe Massiot committed
554
555
556
557
558
    /* Skip the plug-in names */
    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }
559
#if defined( WIN32 ) || defined( UNDER_CE )
Christophe Massiot's avatar
Christophe Massiot committed
560
    if( psz_parser - p_input->psz_dupsource == 1 )
gbazin's avatar
   
gbazin committed
561
    {
562
        msg_Warn( p_input, "drive letter %c: found in source string",
Christophe Massiot's avatar
Christophe Massiot committed
563
                           p_input->psz_dupsource[0] ) ;
Stéphane Borel's avatar
   
Stéphane Borel committed
564
        psz_parser = "";
gbazin's avatar
   
gbazin committed
565
566
    }
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
567

Christophe Massiot's avatar
Christophe Massiot committed
568
    if( !*psz_parser )
569
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
570
        p_input->psz_access = p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
571
        p_input->psz_name = p_input->psz_source;
Christophe Massiot's avatar
Christophe Massiot committed
572
        free( p_input->psz_dupsource );
573
        p_input->psz_dupsource = NULL;
Michel Kaempf's avatar
Michel Kaempf committed
574
    }
Christophe Massiot's avatar
Christophe Massiot committed
575
576
577
    else
    {
        *psz_parser++ = '\0';
578

Xavier Marchesini's avatar
   
Xavier Marchesini committed
579
580
581
582
        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
583
        }
Xavier Marchesini's avatar
   
Xavier Marchesini committed
584
585

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

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

Stéphane Borel's avatar
   
Stéphane Borel committed
590
        if( !*psz_parser )
Christophe Massiot's avatar
Christophe Massiot committed
591
592
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
593
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
594
595
596
597
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
Sam Hocevar's avatar
   
Sam Hocevar committed
598
            p_input->psz_access = "";
Christophe Massiot's avatar
Christophe Massiot committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
            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
615

Christophe Massiot's avatar
Christophe Massiot committed
616
617
618
        if( !*psz_parser )
        {
            /* No demux */
Sam Hocevar's avatar
   
Sam Hocevar committed
619
            p_input->psz_demux = "";
Christophe Massiot's avatar
Christophe Massiot committed
620
621
622
623
624
        }
        else
        {
            p_input->psz_demux = psz_parser;
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
625
    }
Christophe Massiot's avatar
Christophe Massiot committed
626

627
628
    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
629
630

    if( input_AccessInit( p_input ) == -1 )
631
    {
632
633
634
635
636
637
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }

638
639
640
641
642
643
644
645
646
647
648
649
        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 );
650
651
652
653
654
655
656

            input_AccessEnd( p_input );
            free( p_input->psz_source );
            if( p_input->psz_dupsource != NULL )
            {
                free( p_input->psz_dupsource );
            }
657
658
659
            return VLC_EGENERIC;
        }
        free( val.psz_string );
660
    }
Christophe Massiot's avatar
Christophe Massiot committed
661

662
    p_input->p_es_out = input_EsOutNew( p_input );
663
664
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
665

666
    /* Find and open appropriate access module */
667
668
    p_input->p_access = module_Need( p_input, "access",
                                     p_input->psz_access );
Christophe Massiot's avatar
Christophe Massiot committed
669

gbazin's avatar
   
gbazin committed
670
671
672
673
#ifndef WIN32      /* Remove this gross hack from the win32 build as colons
                    * are forbidden in filenames on Win32. */

    /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
674
675
676
677
678
679
680
681
682
683
684
    if ( p_input->p_access == NULL
          && (*p_input->psz_demux || *p_input->psz_access) )
    {
        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 );
    }
gbazin's avatar
   
gbazin committed
685
#endif
686

687
    if( p_input->p_access == NULL )
688
    {
689
        msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
690
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
691
692
693
694
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
695
696
697
698
699
700
701

        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
702
        return VLC_EGENERIC;
703
    }
Christophe Massiot's avatar
Christophe Massiot committed
704
705
706

    /* Waiting for stream. */
    if( p_input->i_mtu )
Christophe Massiot's avatar
Christophe Massiot committed
707
    {
Christophe Massiot's avatar
Christophe Massiot committed
708
        p_input->i_bufsize = p_input->i_mtu;
Christophe Massiot's avatar
Christophe Massiot committed
709
    }
710
711
    else
    {
Christophe Massiot's avatar
Christophe Massiot committed
712
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
713
    }
714

gbazin's avatar
   
gbazin committed
715
716
    /* If the desynchronisation requested by the user is < 0, we need to
     * cache more data. */
gbazin's avatar
   
gbazin committed
717
718
719
720
    var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_input, "audio-desync", &val );
    if( val.i_int < 0 )
        p_input->i_pts_delay -= (val.i_int * 1000);
gbazin's avatar
   
gbazin committed
721

Sam Hocevar's avatar
   
Sam Hocevar committed
722
    if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
723
    {
Christophe Massiot's avatar
Christophe Massiot committed
724
725
        while( !input_FillBuffer( p_input ) )
        {
726
            if( p_input->b_die || p_input->b_error || p_input->b_eof )
Christophe Massiot's avatar
Christophe Massiot committed
727
            {
728
                module_Unneed( p_input, p_input->p_access );
729
730
731
732
                if ( p_input->stream.p_sout != NULL )
                {
                    sout_DeleteInstance( p_input->stream.p_sout );
                }
733
734
735
736
737
738
                input_AccessEnd( p_input );
                free( p_input->psz_source );
                if( p_input->psz_dupsource != NULL )
                {
                    free( p_input->psz_dupsource );
                }
739
                return VLC_EGENERIC;
Christophe Massiot's avatar
Christophe Massiot committed
740
741
            }
        }
742
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
743

744
745
746
747
748
749
750
    /* Create the stream_t facilities */
    p_input->s = stream_OpenInput( p_input );
    if( p_input->s == NULL )
    {
        /* should nver occur yet */

        msg_Err( p_input, "cannot create stream_t !" );
751

752
753
754
755
756
        module_Unneed( p_input, p_input->p_access );
        if ( p_input->stream.p_sout != NULL )
        {
            sout_DeleteInstance( p_input->stream.p_sout );
        }
757
758
759
760
761
762
        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
763
764
765
        return VLC_EGENERIC;
    }

766
    /* Find and open appropriate demux module */
gbazin's avatar
   
gbazin committed
767
768
769
770
    p_input->p_demux =
        module_Need( p_input, "demux",
                     (p_input->psz_demux && *p_input->psz_demux) ?
                     p_input->psz_demux : "$demux" );
Sam Hocevar's avatar
   
Sam Hocevar committed
771

772
    if( p_input->p_demux == NULL )
773
    {
774
        msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
775
                 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
776

777
        stream_Release( p_input->s );
778
        module_Unneed( p_input, p_input->p_access );
779
        if ( p_input->stream.p_sout != NULL )
780
        {
781
            sout_DeleteInstance( p_input->stream.p_sout );
782
        }
783
784
785
786
787
788
        input_AccessEnd( p_input );
        free( p_input->psz_source );
        if( p_input->psz_dupsource != NULL )
        {
            free( p_input->psz_dupsource );
        }
789
        return VLC_EGENERIC;
790
791
    }

792
793
794
795
796
    /* Init input_thread_sys_t */
    p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
    p_input->p_sys->i_sub = 0;
    p_input->p_sys->sub   = NULL;

sigmunau's avatar
sigmunau committed
797
798
799
800
801
802
803
804
    /* get length */
    if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
    {
        p_playlist = (playlist_t*)vlc_object_find( p_input,
                                                   VLC_OBJECT_PLAYLIST,
                                                   FIND_PARENT );
        if( p_playlist )
        {
805
            playlist_SetDuration( p_playlist, -1 , i_length );
sigmunau's avatar
sigmunau committed
806
            val.b_bool = VLC_TRUE;
807
            var_Set( p_playlist, "item-change", val );
sigmunau's avatar
sigmunau committed
808
809
            vlc_object_release( p_playlist );
        }
810
811
    }

sigmunau's avatar
sigmunau committed
812

813
814
815
816
817
818
819
820
821
822
    /* get fps */
    if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
    {
        i_microsecondperframe = 0;
    }
    else
    {
        i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
    }

823
    /* Look for and add subtitle files */
824
825
826
    var_Get( p_input, "sub-file", &val );
    if( val.psz_string && *val.psz_string )
    {
827
        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe, 0 ) ) )
828
        {
829
            /* Select this ES by default */
830
            es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_sub->p_es, VLC_TRUE );
831

832
833
834
835
            TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
        }
    }
    if( val.psz_string ) free( val.psz_string );
836

837
838
    var_Get( p_input, "sub-autodetect-file", &val );
    if( val.b_bool )
839
    {
840
        int i;
841
842
        char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
        char **tmp2 = tmp;
843
        for( i = 0; *tmp2 != NULL; i++ )
844
        {
845
            if( ( p_sub = subtitle_New( p_input, strdup(*tmp2++), i_microsecondperframe, i ) ) )
846
847
848
            {
                TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
            }
849
        }
850
        free(tmp);
851
852
    }

853
    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
854
855
856
857
858
859
860
    val.b_bool =  VLC_FALSE;
    if( p_input->stream.p_sout )
    {
        var_Get( p_input, "sout-all", &val );
    }
    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
                    val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
861

862
    return VLC_SUCCESS;
Michel Kaempf's avatar
Michel Kaempf committed
863
864
}

865
/*****************************************************************************
866
 * ErrorThread: RunThread() error loop
867
 *****************************************************************************
868
 * This function is called when an error occured during thread main's loop.
869
 *****************************************************************************/
870
static void ErrorThread( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
871
{
872
    while( !p_input->b_die )
Michel Kaempf's avatar
Michel Kaempf committed
873
    {
874
875
        /* Sleep a while */
        msleep( INPUT_IDLE_SLEEP );
Michel Kaempf's avatar
Michel Kaempf committed
876
877
878
    }
}

879
/*****************************************************************************
880
 * EndThread: end the input thread
881
 *****************************************************************************/
882
static void EndThread( input_thread_t * p_input )
883
{
884
    int i;
885
#ifdef HAVE_SYS_TIMES_H
886
887
888
    /* Display statistics */
    struct tms  cpu_usage;
    times( &cpu_usage );
Sam Hocevar's avatar
   
Sam Hocevar committed
889

890
    msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
891
             p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
892
#else
893
    msg_Dbg( p_input, "%ld loops", p_input->c_loops );
894
#endif
895

896
897
    input_DumpStream( p_input );

gbazin's avatar
   
gbazin committed
898
    /* Free demultiplexer's data */
gbazin's avatar
   
gbazin committed
899
    if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
gbazin's avatar