]> git.sesse.net Git - vlc/blob - include/vlc_input.h
Partial rewrite of stats to avoid lookups (Closes:#693)
[vlc] / include / vlc_input.h
1 /*****************************************************************************
2  * vlc_input.h: Core input structures
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /* __ is need because conflict with <vlc/input.h> */
26 #ifndef _VLC__INPUT_H
27 #define _VLC__INPUT_H 1
28
29 #include <vlc_playlist.h>
30 #include <vlc_meta.h>
31
32 struct vlc_meta_t;
33
34 /*****************************************************************************
35  * input_item_t: Describes an input and is used to spawn input_thread_t objects
36  *****************************************************************************/
37 struct info_t
38 {
39     char *psz_name;            /**< Name of this info */
40     char *psz_value;           /**< Value of the info */
41 };
42
43 struct info_category_t
44 {
45     char   *psz_name;      /**< Name of this category */
46     int    i_infos;        /**< Number of infos in the category */
47     struct info_t **pp_infos;     /**< Pointer to an array of infos */
48 };
49
50 struct input_item_t
51 {
52     VLC_GC_MEMBERS
53     int        i_id;                 /**< Identifier of the item */
54
55     char       *psz_name;            /**< text describing this item */
56     char       *psz_uri;             /**< mrl of this item */
57     vlc_bool_t  b_fixed_name;        /**< Can the interface change the name ?*/
58
59     int        i_options;            /**< Number of input options */
60     char       **ppsz_options;       /**< Array of input options */
61
62     mtime_t    i_duration;           /**< Duration in milliseconds*/
63
64     uint8_t    i_type;               /**< Type (file, disc, ...) */
65
66     int        i_categories;         /**< Number of info categories */
67     info_category_t **pp_categories; /**< Pointer to the first info category */
68
69     int         i_es;                /**< Number of es format descriptions */
70     es_format_t **es;                /**< Es formats */
71
72     input_stats_t *p_stats;          /**< Statistics */
73     int           i_nb_played;       /**< Number of times played */
74
75     vlc_meta_t *p_meta;
76
77     vlc_mutex_t lock;                /**< Lock for the item */
78 };
79
80 #define ITEM_TYPE_UNKNOWN       0
81 #define ITEM_TYPE_AFILE         1
82 #define ITEM_TYPE_VFILE         2
83 #define ITEM_TYPE_DIRECTORY     3
84 #define ITEM_TYPE_DISC          4
85 #define ITEM_TYPE_CDDA          5
86 #define ITEM_TYPE_CARD          6
87 #define ITEM_TYPE_NET           7
88 #define ITEM_TYPE_PLAYLIST      8
89 #define ITEM_TYPE_NODE          9
90
91 static inline void vlc_input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
92 {
93     memset( p_i, 0, sizeof(input_item_t) );
94     p_i->psz_name = 0;
95     p_i->psz_uri = 0;
96     p_i->i_es = 0;
97     p_i->es = 0;
98     p_i->i_options  = 0;
99     p_i->ppsz_options = 0;
100     p_i->i_categories = 0 ;
101     p_i->pp_categories = 0;
102     p_i->i_type = ITEM_TYPE_UNKNOWN;
103     p_i->b_fixed_name = VLC_TRUE;
104
105     p_i->p_stats = (input_stats_t*) malloc( sizeof( input_stats_t ) );
106     p_i->p_meta = NULL;
107     vlc_mutex_init( p_o, &p_i->p_stats->lock );
108
109     vlc_mutex_init( p_o, &p_i->lock );
110 }
111
112 static inline void vlc_input_item_CopyOptions( input_item_t *p_parent,
113                                                input_item_t *p_child )
114 {
115     int i;
116     for( i = 0 ; i< p_parent->i_options; i++ )
117     {
118         char *psz_option= strdup( p_parent->ppsz_options[i] );
119         p_child->i_options++;
120         p_child->ppsz_options = (char **)realloc( p_child->ppsz_options,
121                                                   p_child->i_options *
122                                                   sizeof( char * ) );
123         p_child->ppsz_options[p_child->i_options-1] = psz_option;
124     }
125 }
126
127 VLC_EXPORT( void, vlc_input_item_AddOption, ( input_item_t *p_input, const char *psz_option ) );
128
129 static inline void vlc_input_item_Clean( input_item_t *p_i )
130 {
131     if( p_i->psz_name ) free( p_i->psz_name );
132     if( p_i->psz_uri ) free( p_i->psz_uri );
133     if( p_i->p_stats ) free( p_i->p_stats );
134     p_i->psz_name = 0;
135     p_i->psz_uri = 0;
136
137     if( p_i->p_meta ) vlc_meta_Delete( p_i->p_meta );
138
139     while( p_i->i_options )
140     {
141         p_i->i_options--;
142         if( p_i->ppsz_options[p_i->i_options] )
143             free( p_i->ppsz_options[p_i->i_options] );
144         if( !p_i->i_options ) free( p_i->ppsz_options );
145     }
146
147     while( p_i->i_es )
148     {
149         p_i->i_es--;
150         es_format_Clean( p_i->es[p_i->i_es] );
151         if( !p_i->i_es ) free( p_i->es );
152     }
153
154     while( p_i->i_categories )
155     {
156         info_category_t *p_category =
157             p_i->pp_categories[--(p_i->i_categories)];
158
159         while( p_category->i_infos )
160         {
161             p_category->i_infos--;
162
163             if( p_category->pp_infos[p_category->i_infos]->psz_name )
164                 free( p_category->pp_infos[p_category->i_infos]->psz_name);
165             if( p_category->pp_infos[p_category->i_infos]->psz_value )
166                 free( p_category->pp_infos[p_category->i_infos]->psz_value );
167             free( p_category->pp_infos[p_category->i_infos] );
168
169             if( !p_category->i_infos ) free( p_category->pp_infos );
170         }
171
172         if( p_category->psz_name ) free( p_category->psz_name );
173         free( p_category );
174
175         if( !p_i->i_categories ) free( p_i->pp_categories );
176     }
177
178     vlc_mutex_destroy( &p_i->lock );
179 }
180
181 VLC_EXPORT( char *, vlc_input_item_GetInfo, ( input_item_t *p_i, const char *psz_cat,const char *psz_name ) );
182 VLC_EXPORT(int, vlc_input_item_AddInfo, ( input_item_t *p_i, const char *psz_cat, const char *psz_name, const char *psz_format, ... ) );
183
184 #define input_ItemNew( a,b,c ) input_ItemNewExt( a, b, c, 0, NULL, -1 )
185 #define input_ItemNewExt(a,b,c,d,e,f) __input_ItemNewExt( VLC_OBJECT(a),b,c,d,e,f)
186 VLC_EXPORT( input_item_t *, __input_ItemNewExt, (vlc_object_t *, const char *, const char*, int, const char **, int)  );
187 VLC_EXPORT( input_item_t *, input_ItemNewWithType, ( vlc_object_t *, const char *, const char *e, int, const char **, int, int ) );
188
189 VLC_EXPORT( input_item_t *, input_ItemGetById, (playlist_t *, int ) );
190
191
192 /*****************************************************************************
193  * Seek point: (generalisation of chapters)
194  *****************************************************************************/
195 struct seekpoint_t
196 {
197     int64_t i_byte_offset;
198     int64_t i_time_offset;
199     char    *psz_name;
200     int     i_level;
201 };
202
203 static inline seekpoint_t *vlc_seekpoint_New( void )
204 {
205     seekpoint_t *point = (seekpoint_t*)malloc( sizeof( seekpoint_t ) );
206     point->i_byte_offset =
207     point->i_time_offset = -1;
208     point->i_level = 0;
209     point->psz_name = NULL;
210     return point;
211 }
212
213 static inline void vlc_seekpoint_Delete( seekpoint_t *point )
214 {
215     if( !point ) return;
216     if( point->psz_name ) free( point->psz_name );
217     free( point );
218 }
219
220 static inline seekpoint_t *vlc_seekpoint_Duplicate( seekpoint_t *src )
221 {
222     seekpoint_t *point = vlc_seekpoint_New();
223     if( src->psz_name ) point->psz_name = strdup( src->psz_name );
224     point->i_time_offset = src->i_time_offset;
225     point->i_byte_offset = src->i_byte_offset;
226     return point;
227 }
228
229 /*****************************************************************************
230  * Title:
231  *****************************************************************************/
232 typedef struct
233 {
234     char        *psz_name;
235
236     vlc_bool_t  b_menu;      /* Is it a menu or a normal entry */
237
238     int64_t     i_length;   /* Length(microsecond) if known, else 0 */
239     int64_t     i_size;     /* Size (bytes) if known, else 0 */
240
241     /* Title seekpoint */
242     int         i_seekpoint;
243     seekpoint_t **seekpoint;
244
245 } input_title_t;
246
247 static inline input_title_t *vlc_input_title_New( )
248 {
249     input_title_t *t = (input_title_t*)malloc( sizeof( input_title_t ) );
250
251     t->psz_name = NULL;
252     t->b_menu = VLC_FALSE;
253     t->i_length = 0;
254     t->i_size   = 0;
255     t->i_seekpoint = 0;
256     t->seekpoint = NULL;
257
258     return t;
259 }
260
261 static inline void vlc_input_title_Delete( input_title_t *t )
262 {
263     int i;
264     if( t == NULL )
265         return;
266
267     if( t->psz_name ) free( t->psz_name );
268     for( i = 0; i < t->i_seekpoint; i++ )
269     {
270         if( t->seekpoint[i]->psz_name ) free( t->seekpoint[i]->psz_name );
271         free( t->seekpoint[i] );
272     }
273     if( t->seekpoint ) free( t->seekpoint );
274     free( t );
275 }
276
277 static inline input_title_t *vlc_input_title_Duplicate( input_title_t *t )
278 {
279     input_title_t *dup = vlc_input_title_New( );
280     int i;
281
282     if( t->psz_name ) dup->psz_name = strdup( t->psz_name );
283     dup->b_menu      = t->b_menu;
284     dup->i_length    = t->i_length;
285     dup->i_size      = t->i_size;
286     dup->i_seekpoint = t->i_seekpoint;
287     if( t->i_seekpoint > 0 )
288     {
289         dup->seekpoint = (seekpoint_t**)calloc( t->i_seekpoint,
290                                                 sizeof(seekpoint_t*) );
291
292         for( i = 0; i < t->i_seekpoint; i++ )
293         {
294             dup->seekpoint[i] = vlc_seekpoint_Duplicate( t->seekpoint[i] );
295         }
296     }
297
298     return dup;
299 }
300
301 /*****************************************************************************
302  * input defines/constants.
303  *****************************************************************************/
304
305 /* "state" value */
306 enum input_state_e
307 {
308     INIT_S,
309     PLAYING_S,
310     PAUSE_S,
311     END_S
312 };
313
314 /* "rate" default, min/max
315  * A rate below 1000 plays the movie faster,
316  * A rate above 1000 plays the movie slower.
317  */
318 #define INPUT_RATE_DEFAULT  1000
319 #define INPUT_RATE_MIN       125            /* Up to 8/1 */
320 #define INPUT_RATE_MAX      8000            /* Up to 1/8 */
321
322 /* input_source_t: gathers all information per input source */
323 typedef struct
324 {
325     /* Input item description */
326     input_item_t *p_item;
327
328     /* Access/Stream/Demux plugins */
329     access_t *p_access;
330     stream_t *p_stream;
331     demux_t  *p_demux;
332
333     /* Title infos for that input */
334     vlc_bool_t   b_title_demux; /* Titles/Seekpoints provided by demux */
335     int          i_title;
336     input_title_t **title;
337
338     int i_title_offset;
339     int i_seekpoint_offset;
340
341     int i_title_start;
342     int i_title_end;
343     int i_seekpoint_start;
344     int i_seekpoint_end;
345
346     /* Properties */
347     vlc_bool_t b_can_pace_control;
348     vlc_bool_t b_can_pause;
349     vlc_bool_t b_eof;   /* eof of demuxer */
350
351     /* Clock average variation */
352     int     i_cr_average;
353
354 } input_source_t;
355
356 /* i_update field of access_t/demux_t */
357 #define INPUT_UPDATE_NONE       0x0000
358 #define INPUT_UPDATE_SIZE       0x0001
359 #define INPUT_UPDATE_TITLE      0x0010
360 #define INPUT_UPDATE_SEEKPOINT  0x0020
361 #define INPUT_UPDATE_META       0x0040
362
363 /* Input control XXX: internal */
364 #define INPUT_CONTROL_FIFO_SIZE    100
365
366 /*****************************************************************************
367  * input_thread_t
368  *****************************************************************************
369  * XXX: this strucrures is *PRIVATE* so nobody can touch it out of src/input.
370  * I plan to move it to src/input/input_internal.h anyway
371  *
372  * XXX: look at src/input/input.c:input_CreateThread for accessible variables
373  *      YOU CANNOT HAVE ACCESS TO THE CONTENT OF input_thread_t except
374  *      p_input->input.p_item (and it's only temporary).
375  * XXX: move the docs somewhere (better than src/input )
376  *****************************************************************************/
377 struct input_thread_t
378 {
379     VLC_COMMON_MEMBERS
380
381      /* Global properties */
382     vlc_bool_t  b_eof;
383     vlc_bool_t  b_can_pace_control;
384     vlc_bool_t  b_can_pause;
385
386     /* Global state */
387     int         i_state;
388     int         i_rate;
389
390     /* */
391     int64_t     i_start;    /* :start-time,0 by default */
392     int64_t     i_time;     /* Current time */
393     int64_t     i_stop;     /* :stop-time, 0 if none */
394
395     /* Title infos FIXME multi-input (not easy) ? */
396     int          i_title;
397     input_title_t **title;
398
399     int i_title_offset;
400     int i_seekpoint_offset;
401
402     /* User bookmarks FIXME won't be easy with multiples input */
403     int         i_bookmark;
404     seekpoint_t **bookmark;
405
406     /* Global meta datas FIXME move to input_item_t ? */
407     vlc_meta_t  *p_meta;
408
409     /* Output */
410     es_out_t    *p_es_out;
411     sout_instance_t *p_sout;            /* XXX Move it to es_out ? */
412     vlc_bool_t      b_out_pace_control; /*     idem ? */
413
414     /* Internal caching common for all inputs */
415     int64_t i_pts_delay;
416
417     /* Main input properties */
418     input_source_t input;
419
420     /* Slave demuxers (subs, and others) */
421     int            i_slave;
422     input_source_t **slave;
423
424     /* Stats counters */
425     struct {
426         counter_t *p_read_packets;
427         counter_t *p_read_bytes;
428         counter_t *p_input_bitrate;
429         counter_t *p_demux_read;
430         counter_t *p_demux_bitrate;
431         counter_t *p_decoded_audio;
432         counter_t *p_decoded_video;
433         counter_t *p_decoded_sub;
434         counter_t *p_sout_sent_packets;
435         counter_t *p_sout_sent_bytes;
436         counter_t *p_sout_send_bitrate;
437         counter_t *p_played_abuffers;
438         counter_t *p_lost_abuffers;
439         counter_t *p_displayed_pictures;
440         counter_t *p_lost_pictures;
441         vlc_mutex_t counters_lock;
442     } counters;
443
444     /* Buffer of pending actions */
445     vlc_mutex_t lock_control;
446     int i_control;
447     struct
448     {
449         /* XXX: val isn't duplicated so it won't works with string */
450         int         i_type;
451         vlc_value_t val;
452     } control[INPUT_CONTROL_FIFO_SIZE];
453 };
454
455 /*****************************************************************************
456  * Prototypes
457  *****************************************************************************/
458 #define input_CreateThread(a,b) __input_CreateThread(VLC_OBJECT(a),b)
459 VLC_EXPORT( input_thread_t *, __input_CreateThread, ( vlc_object_t *, input_item_t * ) );
460 #define input_CreateThread2(a,b,c) __input_CreateThread2(VLC_OBJECT(a),b,c)
461 VLC_EXPORT( input_thread_t *, __input_CreateThread2, ( vlc_object_t *, input_item_t *, char * ) );
462 #define input_Preparse(a,b) __input_Preparse(VLC_OBJECT(a),b)
463 VLC_EXPORT( int, __input_Preparse, ( vlc_object_t *, input_item_t * ) );
464
465 #define input_Read(a,b,c) __input_Read(VLC_OBJECT(a),b, c)
466 VLC_EXPORT( int, __input_Read, ( vlc_object_t *, input_item_t *, vlc_bool_t ) );
467 VLC_EXPORT( void,             input_StopThread,     ( input_thread_t * ) );
468 VLC_EXPORT( void,             input_DestroyThread,  ( input_thread_t * ) );
469
470 enum input_query_e
471 {
472     /* input variable "position" */
473     INPUT_GET_POSITION,         /* arg1= double *       res=    */
474     INPUT_SET_POSITION,         /* arg1= double         res=can fail    */
475
476     /* input variable "length" */
477     INPUT_GET_LENGTH,           /* arg1= int64_t *      res=can fail    */
478
479     /* input variable "time" */
480     INPUT_GET_TIME,             /* arg1= int64_t *      res=    */
481     INPUT_SET_TIME,             /* arg1= int64_t        res=can fail    */
482
483     /* input variable "rate" (1 is DEFAULT_RATE) */
484     INPUT_GET_RATE,             /* arg1= int *          res=    */
485     INPUT_SET_RATE,             /* arg1= int            res=can fail    */
486
487     /* input variable "state" */
488     INPUT_GET_STATE,            /* arg1= int *          res=    */
489     INPUT_SET_STATE,            /* arg1= int            res=can fail    */
490
491     /* input variable "audio-delay" and "sub-delay" */
492     INPUT_GET_AUDIO_DELAY,      /* arg1 = int* res=can fail */
493     INPUT_SET_AUDIO_DELAY,      /* arg1 = int  res=can fail */
494     INPUT_GET_SPU_DELAY,        /* arg1 = int* res=can fail */
495     INPUT_SET_SPU_DELAY,        /* arg1 = int  res=can fail */
496
497     /* Meta datas */
498     INPUT_ADD_INFO,   /* arg1= char* arg2= char* arg3=...     res=can fail */
499     INPUT_GET_INFO,   /* arg1= char* arg2= char* arg3= char** res=can fail */
500     INPUT_DEL_INFO,   /* arg1= char* arg2= char*              res=can fail */
501     INPUT_SET_NAME,   /* arg1= char* res=can fail    */
502
503     /* Input config options */
504     INPUT_ADD_OPTION,      /* arg1= char * arg2= char *  res=can fail*/
505
506     /* Input properties */
507     INPUT_GET_BYTE_POSITION,     /* arg1= int64_t *       res=    */
508     INPUT_SET_BYTE_SIZE,         /* arg1= int64_t *       res=    */
509
510     /* bookmarks */
511     INPUT_GET_BOOKMARKS,   /* arg1= seekpoint_t *** arg2= int * res=can fail */
512     INPUT_CLEAR_BOOKMARKS, /* res=can fail */
513     INPUT_ADD_BOOKMARK,    /* arg1= seekpoint_t *  res=can fail   */
514     INPUT_CHANGE_BOOKMARK, /* arg1= seekpoint_t * arg2= int * res=can fail   */
515     INPUT_DEL_BOOKMARK,    /* arg1= seekpoint_t *  res=can fail   */
516     INPUT_SET_BOOKMARK,    /* arg1= int  res=can fail    */
517
518     /* On the fly input slave */
519     INPUT_ADD_SLAVE        /* arg1= char * */
520 };
521
522 VLC_EXPORT( int, input_vaControl,( input_thread_t *, int i_query, va_list  ) );
523 VLC_EXPORT( int, input_Control,  ( input_thread_t *, int i_query, ...  ) );
524
525 VLC_EXPORT( decoder_t *, input_DecoderNew, ( input_thread_t *, es_format_t *, vlc_bool_t b_force_decoder ) );
526 VLC_EXPORT( void, input_DecoderDelete, ( decoder_t * ) );
527 VLC_EXPORT( void, input_DecoderDecode,( decoder_t *, block_t * ) );
528
529 VLC_EXPORT( vlc_bool_t, input_AddSubtitles, ( input_thread_t *, char *, vlc_bool_t ) );
530
531 #endif