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