cfc746a3a1efe8da408b0fc0ac26140831e24a55
[vlc.git] / src / input / input_internal.h
1 /*****************************************************************************
2  * input_internal.h: Internal input structures
3  *****************************************************************************
4  * Copyright (C) 1998-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
25 # error This header file can only be included from LibVLC.
26 #endif
27
28 #ifndef _INPUT_INTERNAL_H
29 #define _INPUT_INTERNAL_H 1
30
31 #include <vlc_access.h>
32 #include <vlc_demux.h>
33 #include <vlc_input.h>
34
35 /*****************************************************************************
36  *  Private input fields
37  *****************************************************************************/
38 /* input_source_t: gathers all information per input source */
39 typedef struct
40 {
41     /* Input item description */
42     input_item_t *p_item;
43
44     /* Access/Stream/Demux plugins */
45     access_t *p_access;
46     stream_t *p_stream;
47     demux_t  *p_demux;
48
49     /* Title infos for that input */
50     vlc_bool_t   b_title_demux; /* Titles/Seekpoints provided by demux */
51     int          i_title;
52     input_title_t **title;
53
54     int i_title_offset;
55     int i_seekpoint_offset;
56
57     int i_title_start;
58     int i_title_end;
59     int i_seekpoint_start;
60     int i_seekpoint_end;
61
62     /* Properties */
63     vlc_bool_t b_can_pause;
64     vlc_bool_t b_can_pace_control;
65     vlc_bool_t b_can_rate_control;
66     vlc_bool_t b_rescale_ts;
67
68     vlc_bool_t b_eof;   /* eof of demuxer */
69     double     f_fps;
70
71     /* Clock average variation */
72     int     i_cr_average;
73
74 } input_source_t;
75
76 /** Private input fields */
77 struct input_thread_private_t
78 {
79     /* Global properties */
80     vlc_bool_t  b_can_pause;
81     vlc_bool_t  b_can_rate_control;
82
83     int         i_rate;
84     /* */
85     int64_t     i_start;    /* :start-time,0 by default */
86     int64_t     i_stop;     /* :stop-time, 0 if none */
87     int64_t     i_run;      /* :run-time, 0 if none */
88
89     /* Title infos FIXME multi-input (not easy) ? */
90     int          i_title;
91     input_title_t **title;
92
93     int i_title_offset;
94     int i_seekpoint_offset;
95
96     /* User bookmarks FIXME won't be easy with multiples input */
97     int         i_bookmark;
98     seekpoint_t **bookmark;
99
100     /* Input attachment */
101     int i_attachment;
102     input_attachment_t **attachment;
103
104     /* Output */
105     es_out_t    *p_es_out;
106     sout_instance_t *p_sout;            /* XXX Move it to es_out ? */
107     vlc_bool_t      b_sout_keep;
108     vlc_bool_t      b_out_pace_control; /*     idem ? */
109
110     /* Main input properties */
111     input_source_t input;
112     /* Slave demuxers (subs, and others) */
113     int            i_slave;
114     input_source_t **slave;
115
116     /* Stats counters */
117     struct {
118         counter_t *p_read_packets;
119         counter_t *p_read_bytes;
120         counter_t *p_input_bitrate;
121         counter_t *p_demux_read;
122         counter_t *p_demux_bitrate;
123         counter_t *p_decoded_audio;
124         counter_t *p_decoded_video;
125         counter_t *p_decoded_sub;
126         counter_t *p_sout_sent_packets;
127         counter_t *p_sout_sent_bytes;
128         counter_t *p_sout_send_bitrate;
129         counter_t *p_played_abuffers;
130         counter_t *p_lost_abuffers;
131         counter_t *p_displayed_pictures;
132         counter_t *p_lost_pictures;
133         vlc_mutex_t counters_lock;
134     } counters;
135
136     /* Buffer of pending actions */
137     vlc_mutex_t lock_control;
138     int i_control;
139     struct
140     {
141         /* XXX for string value you have to allocate it before calling
142          * input_ControlPush */
143         int         i_type;
144         vlc_value_t val;
145     } control[INPUT_CONTROL_FIFO_SIZE];
146 };
147
148 /***************************************************************************
149  * Internal control helpers
150  ***************************************************************************/
151 enum input_control_e
152 {
153     INPUT_CONTROL_SET_DIE,
154
155     INPUT_CONTROL_SET_STATE,
156
157     INPUT_CONTROL_SET_RATE,
158     INPUT_CONTROL_SET_RATE_SLOWER,
159     INPUT_CONTROL_SET_RATE_FASTER,
160
161     INPUT_CONTROL_SET_POSITION,
162     INPUT_CONTROL_SET_POSITION_OFFSET,
163
164     INPUT_CONTROL_SET_TIME,
165     INPUT_CONTROL_SET_TIME_OFFSET,
166
167     INPUT_CONTROL_SET_PROGRAM,
168
169     INPUT_CONTROL_SET_TITLE,
170     INPUT_CONTROL_SET_TITLE_NEXT,
171     INPUT_CONTROL_SET_TITLE_PREV,
172
173     INPUT_CONTROL_SET_SEEKPOINT,
174     INPUT_CONTROL_SET_SEEKPOINT_NEXT,
175     INPUT_CONTROL_SET_SEEKPOINT_PREV,
176
177     INPUT_CONTROL_SET_BOOKMARK,
178
179     INPUT_CONTROL_SET_ES,
180
181     INPUT_CONTROL_SET_AUDIO_DELAY,
182     INPUT_CONTROL_SET_SPU_DELAY,
183
184     INPUT_CONTROL_ADD_SLAVE,
185 };
186
187 /* Internal helpers */
188 static inline void input_ControlPush( input_thread_t *p_input,
189                                       int i_type, vlc_value_t *p_val )
190 {
191     vlc_mutex_lock( &p_input->p->lock_control );
192     if( i_type == INPUT_CONTROL_SET_DIE )
193     {
194         /* Special case, empty the control */
195         p_input->p->i_control = 1;
196         p_input->p->control[0].i_type = i_type;
197         memset( &p_input->p->control[0].val, 0, sizeof( vlc_value_t ) );
198     }
199     else
200     {
201         if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
202         {
203             msg_Err( p_input, "input control fifo overflow, trashing type=%d",
204                      i_type );
205             vlc_mutex_unlock( &p_input->p->lock_control );
206             return;
207         }
208         p_input->p->control[p_input->p->i_control].i_type = i_type;
209         if( p_val )
210             p_input->p->control[p_input->p->i_control].val = *p_val;
211         else
212             memset( &p_input->p->control[p_input->p->i_control].val, 0,
213                     sizeof( vlc_value_t ) );
214
215         p_input->p->i_control++;
216     }
217     vlc_mutex_unlock( &p_input->p->lock_control );
218 }
219
220 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
221  * anyway. */
222 static inline void input_ItemInit( vlc_object_t *p_o, input_item_t *p_i )
223 {
224     memset( p_i, 0, sizeof(input_item_t) );
225     p_i->psz_name = NULL;
226     p_i->psz_uri = NULL;
227     TAB_INIT( p_i->i_es, p_i->es );
228     TAB_INIT( p_i->i_options, p_i->ppsz_options );
229     p_i->optflagv = NULL, p_i->optflagc = 0;
230     TAB_INIT( p_i->i_categories, p_i->pp_categories );
231
232     p_i->i_type = ITEM_TYPE_UNKNOWN;
233     p_i->b_fixed_name = VLC_TRUE;
234
235     p_i->p_stats = NULL;
236     p_i->p_meta = NULL;
237
238     vlc_mutex_init( p_o, &p_i->lock );
239     vlc_event_manager_init( &p_i->event_manager, p_i, p_o );
240     vlc_event_manager_register_event_type( &p_i->event_manager,
241         vlc_InputItemMetaChanged );
242     vlc_event_manager_register_event_type( &p_i->event_manager,
243         vlc_InputItemSubItemAdded );
244     vlc_event_manager_register_event_type( &p_i->event_manager,
245         vlc_InputItemDurationChanged );
246     vlc_event_manager_register_event_type( &p_i->event_manager,
247         vlc_InputItemPreparsedChanged );
248 }
249
250 static inline void input_item_SetPreparsed( input_item_t *p_i, vlc_bool_t preparsed )
251 {
252     vlc_bool_t send_event = VLC_FALSE;
253
254     if( !p_i->p_meta )
255         p_i->p_meta = vlc_meta_New();
256
257     vlc_mutex_lock( &p_i->lock );
258     int new_status;
259     if( preparsed )
260         new_status = p_i->p_meta->i_status | ITEM_PREPARSED;
261     else
262         new_status = p_i->p_meta->i_status & ~ITEM_PREPARSED;
263     if ( p_i->p_meta->i_status != new_status )
264     {
265         p_i->p_meta->i_status = new_status;
266         send_event = VLC_TRUE;
267     }
268
269     vlc_mutex_unlock( &p_i->lock );
270
271     if ( send_event == VLC_TRUE )
272     {
273         vlc_event_t event;
274         event.type = vlc_InputItemPreparsedChanged;
275         event.u.input_item_preparsed_changed.new_status = new_status;
276         vlc_event_send( &p_i->event_manager, &event );
277     }
278 }
279
280 static inline void input_item_SetMetaFetched( input_item_t *p_i, vlc_bool_t metafetched )
281 {
282     if( !p_i->p_meta )
283         p_i->p_meta = vlc_meta_New();
284
285     if( metafetched )
286         p_i->p_meta->i_status |= ITEM_META_FETCHED;
287     else
288         p_i->p_meta->i_status &= ~ITEM_META_FETCHED;
289 }
290
291 static inline void input_item_SetArtNotFound( input_item_t *p_i, vlc_bool_t notfound )
292 {
293     if( !p_i->p_meta )
294         p_i->p_meta = vlc_meta_New();
295
296     if( notfound )
297         p_i->p_meta->i_status |= ITEM_ART_NOTFOUND;
298     else
299         p_i->p_meta->i_status &= ~ITEM_ART_NOTFOUND;
300 }
301
302 static inline void input_item_SetArtFetched( input_item_t *p_i, vlc_bool_t artfetched )
303 {
304     if( !p_i->p_meta )
305         p_i->p_meta = vlc_meta_New();
306
307     if( artfetched )
308         p_i->p_meta->i_status |= ITEM_ART_FETCHED;
309     else
310         p_i->p_meta->i_status &= ~ITEM_ART_FETCHED;
311 }
312
313
314 /**********************************************************************
315  * Item metadata
316  **********************************************************************/
317 typedef struct playlist_album_t
318 {
319     char *psz_artist;
320     char *psz_album;
321     char *psz_arturl;
322     vlc_bool_t b_found;
323 } playlist_album_t;
324
325 int         input_MetaFetch     ( playlist_t *, input_item_t * );
326 int         input_ArtFind       ( playlist_t *, input_item_t * );
327 vlc_bool_t  input_MetaSatisfied ( playlist_t*, input_item_t*,
328                                   uint32_t*, uint32_t* );
329 int         input_DownloadAndCacheArt ( playlist_t *, input_item_t * );
330
331 /* Becarefull; p_item lock HAS to be taken */
332 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input );
333
334 /***************************************************************************
335  * Internal prototypes
336  ***************************************************************************/
337
338 /* misc/stats.c */
339 input_stats_t *stats_NewInputStats( input_thread_t *p_input );
340
341 /* input.c */
342 #define input_CreateThreadExtended(a,b,c,d) __input_CreateThreadExtended(VLC_OBJECT(a),b,c,d)
343 input_thread_t *__input_CreateThreadExtended ( vlc_object_t *, input_item_t *, const char *, sout_instance_t * );
344
345 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t ** );
346
347 /* var.c */
348 void input_ControlVarInit ( input_thread_t * );
349 void input_ControlVarClean( input_thread_t * );
350 void input_ControlVarNavigation( input_thread_t * );
351 void input_ControlVarTitle( input_thread_t *, int i_title );
352
353 void input_ConfigVarInit ( input_thread_t * );
354
355 /* stream.c */
356 stream_t *stream_AccessNew( access_t *p_access, vlc_bool_t );
357 void stream_AccessDelete( stream_t *s );
358 void stream_AccessReset( stream_t *s );
359 void stream_AccessUpdate( stream_t *s );
360
361 /* decoder.c */
362 void       input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush );
363 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec );
364 int        input_DecoderSetCcState( decoder_t *, vlc_bool_t b_decode, int i_channel );
365 int        input_DecoderGetCcState( decoder_t *, vlc_bool_t *pb_decode, int i_channel );
366 void       input_DecoderIsCcPresent( decoder_t *, vlc_bool_t pb_present[4] );
367
368 /* es_out.c */
369 es_out_t  *input_EsOutNew( input_thread_t *, int i_rate );
370 void       input_EsOutDelete( es_out_t * );
371 es_out_id_t *input_EsOutGetFromID( es_out_t *, int i_id );
372 void       input_EsOutSetDelay( es_out_t *, int i_cat, int64_t );
373 void       input_EsOutChangeRate( es_out_t *, int );
374 void       input_EsOutChangeState( es_out_t * );
375 void       input_EsOutChangePosition( es_out_t * );
376 vlc_bool_t input_EsOutDecodersEmpty( es_out_t * );
377
378 /* clock.c */
379 enum /* Synchro states */
380 {
381     SYNCHRO_OK     = 0,
382     SYNCHRO_START  = 1,
383     SYNCHRO_REINIT = 2,
384 };
385
386 typedef struct
387 {
388     /* Synchronization information */
389     mtime_t                 delta_cr;
390     mtime_t                 cr_ref, sysdate_ref;
391     mtime_t                 last_sysdate;
392     mtime_t                 last_cr; /* reference to detect unexpected stream
393                                       * discontinuities                      */
394     mtime_t                 last_pts;
395     mtime_t                 last_update;
396     int                     i_synchro_state;
397
398     vlc_bool_t              b_master;
399
400     int                     i_rate;
401
402     /* Config */
403     int                     i_cr_average;
404     int                     i_delta_cr_residue;
405 } input_clock_t;
406
407 void    input_ClockInit( input_clock_t *, vlc_bool_t b_master, int i_cr_average, int i_rate );
408 void    input_ClockSetPCR( input_thread_t *, input_clock_t *, mtime_t );
409 void    input_ClockResetPCR( input_clock_t * );
410 mtime_t input_ClockGetTS( input_thread_t *, input_clock_t *, mtime_t );
411 void    input_ClockSetRate( input_clock_t *cl, int i_rate );
412
413 /* Subtitles */
414 char **subtitles_Detect( input_thread_t *, char* path, const char *fname );
415 int subtitles_Filter( const char *);
416
417 void MRLSplit( char *, const char **, const char **, char ** );
418
419 static inline void input_ChangeState( input_thread_t *p_input, int state )
420 {
421     var_SetInteger( p_input, "state", p_input->i_state = state );
422 }
423
424 /* Access */
425
426 #define access2_New( a, b, c, d ) __access2_New(VLC_OBJECT(a), b, c, d )
427 access_t * __access2_New( vlc_object_t *p_obj, const char *psz_access,
428                           const char *psz_demux, const char *psz_path );
429 access_t * access2_FilterNew( access_t *p_source,
430                               const char *psz_access_filter );
431 void access2_Delete( access_t * );
432
433 /* Demuxer */
434 #include <vlc_demux.h>
435
436 /* stream_t *s could be null and then it mean a access+demux in one */
437 #define demux2_New( a, b, c, d, e, f,g ) __demux2_New(VLC_OBJECT(a),b,c,d,e,f,g)
438 demux_t *__demux2_New(vlc_object_t *p_obj, const char *psz_access, const char *psz_demux, const char *psz_path, stream_t *s, es_out_t *out, vlc_bool_t );
439
440 void demux2_Delete(demux_t *);
441
442 static inline int demux2_Demux( demux_t *p_demux )
443 {
444     return p_demux->pf_demux( p_demux );
445 }
446 static inline int demux2_vaControl( demux_t *p_demux, int i_query, va_list args )
447 {
448     return p_demux->pf_control( p_demux, i_query, args );
449 }
450 static inline int demux2_Control( demux_t *p_demux, int i_query, ... )
451 {
452     va_list args;
453     int     i_result;
454
455     va_start( args, i_query );
456     i_result = demux2_vaControl( p_demux, i_query, args );
457     va_end( args );
458     return i_result;
459 }
460
461 /* Stream */
462 /**
463  * stream_t definition
464  */
465 struct stream_t
466 {
467     VLC_COMMON_MEMBERS
468
469     /*block_t *(*pf_block)  ( stream_t *, int i_size );*/
470     int      (*pf_read)   ( stream_t *, void *p_read, int i_read );
471     int      (*pf_peek)   ( stream_t *, const uint8_t **pp_peek, int i_peek );
472     int      (*pf_control)( stream_t *, int i_query, va_list );
473     void     (*pf_destroy)( stream_t *);
474
475     stream_sys_t *p_sys;
476
477     /* UTF-16 and UTF-32 file reading */
478     vlc_iconv_t     conv;
479     int             i_char_width;
480     vlc_bool_t      b_little_endian;
481 };
482
483 #include <libvlc.h>
484
485 static inline stream_t *vlc_stream_create( vlc_object_t *obj )
486 {
487     return (stream_t *)vlc_custom_create( obj, sizeof(stream_t),
488                                           VLC_OBJECT_STREAM, "stream" );
489 }
490
491 #endif