]> git.sesse.net Git - vlc/blob - src/misc/objects.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: objects.c,v 1.12 2002/07/31 20:56:53 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #ifdef HAVE_STDLIB_H
30 #   include <stdlib.h>                                          /* realloc() */
31 #endif
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36
37 #include "video.h"
38 #include "video_output.h"
39
40 #include "vdec_ext-plugins.h"
41
42 #include "audio_output.h"
43
44 #include "vlc_playlist.h"
45 #include "interface.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static vlc_object_t * vlc_object_find_inner( vlc_object_t *, int, int );
51 static void vlc_object_detach_inner( vlc_object_t *, vlc_object_t * );
52 static void vlc_dumpstructure_inner( vlc_object_t *, int, char * );
53
54 /*****************************************************************************
55  * vlc_object_create: initialize a vlc object
56  *****************************************************************************
57  * This function allocates memory for a vlc object and initializes it. If
58  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
59  * so on, vlc_object_create will use its value for the object size.
60  *****************************************************************************/
61 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
62 {
63     vlc_object_t * p_new;
64     char *         psz_type;
65     size_t         i_size;
66
67     switch( i_type )
68     {
69         case VLC_OBJECT_ROOT:
70             i_size = sizeof(vlc_t);
71             psz_type = "root";
72             break;
73         case VLC_OBJECT_MODULE:
74             i_size = sizeof(module_t);
75             psz_type = "module";
76             break;
77         case VLC_OBJECT_INTF:
78             i_size = sizeof(intf_thread_t);
79             psz_type = "interface";
80             break;
81         case VLC_OBJECT_PLAYLIST:
82             i_size = sizeof(playlist_t);
83             psz_type = "playlist";
84             break;
85         case VLC_OBJECT_INPUT:
86             i_size = sizeof(input_thread_t);
87             psz_type = "input";
88             break;
89         case VLC_OBJECT_DECODER:
90             i_size = sizeof(decoder_fifo_t);
91             psz_type = "decoder";
92             break;
93         case VLC_OBJECT_VOUT:
94             i_size = sizeof(vout_thread_t);
95             psz_type = "video output";
96             break;
97         case VLC_OBJECT_AOUT:
98             i_size = sizeof(aout_thread_t);
99             psz_type = "audio output";
100             break;
101         default:
102             i_size = i_type > sizeof(vlc_object_t)
103                    ? i_type : sizeof(vlc_object_t);
104             i_type = VLC_OBJECT_GENERIC;
105             psz_type = "generic";
106             break;
107     }
108
109     p_new = malloc( i_size );
110
111     if( !p_new )
112     {
113         return NULL;
114     }
115
116     memset( p_new, 0, i_size );
117
118     p_new->i_object_type = i_type;
119     p_new->psz_object_type = psz_type;
120
121     p_new->psz_object_name = NULL;
122
123     p_new->i_refcount = 0;
124     p_new->b_die = 0;
125     p_new->b_error = 0;
126
127     /* If i_type is root, then p_new is our own p_vlc */
128     if( i_type == VLC_OBJECT_ROOT )
129     {
130         p_new->p_vlc = (vlc_t*)p_new;
131         p_new->p_vlc->i_counter = 0;
132         p_new->i_object_id = 0;
133     }
134     else
135     {
136         p_new->p_vlc = p_this->p_vlc;
137
138         vlc_mutex_lock( &p_this->p_vlc->structure_lock );
139         p_new->p_vlc->i_counter++;
140         p_new->i_object_id = p_new->p_vlc->i_counter;
141         vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
142     }
143
144     p_new->pp_parents = NULL;
145     p_new->i_parents = 0;
146     p_new->pp_children = NULL;
147     p_new->i_children = 0;
148
149     p_new->p_private = NULL;
150
151     vlc_mutex_init( p_new, &p_new->object_lock );
152     vlc_cond_init( p_new, &p_new->object_wait );
153
154     //msg_Dbg( p_new, "created object" );
155
156     return p_new;
157 }
158
159 /*****************************************************************************
160  * vlc_object_destroy: destroy a vlc object
161  *****************************************************************************
162  * This function destroys an object that has been previously allocated with
163  * vlc_object_create. The object's refcount must be zero and it must not be
164  * attached to other objects in any way.
165  *****************************************************************************/
166 void __vlc_object_destroy( vlc_object_t *p_this )
167 {
168     int i_delay = 0;
169
170     if( p_this->i_children )
171     {
172         msg_Err( p_this, "cannot delete object with children" );
173         vlc_dumpstructure( p_this );
174         return;
175     }
176
177     if( p_this->i_parents )
178     {
179         msg_Err( p_this, "cannot delete object with parents" );
180         vlc_dumpstructure( p_this );
181         return;
182     }
183
184     while( p_this->i_refcount )
185     {
186         i_delay++;
187
188         /* Don't warn immediately ... 100ms seems OK */
189         if( i_delay == 2 )
190         {
191             msg_Warn( p_this, "refcount is %i, delaying before deletion",
192                               p_this->i_refcount );
193         }
194         else if( i_delay == 12 )
195         {
196             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
197                              p_this->i_refcount );
198         }
199         else if( i_delay == 42 )
200         {
201             msg_Err( p_this, "we waited too long, cancelling destruction" );
202             return;
203         }
204
205         msleep( 100000 );
206     }
207
208     //msg_Dbg( p_this, "destroyed object" );
209
210     vlc_mutex_destroy( &p_this->object_lock );
211     vlc_cond_destroy( &p_this->object_wait );
212
213     free( p_this );
214 }
215
216 /*****************************************************************************
217  * vlc_object_find: find a typed object and increment its refcount
218  *****************************************************************************
219  * This function recursively looks for a given object type. i_mode can be one
220  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
221  *****************************************************************************/
222 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
223 {
224     vlc_object_t *p_found;
225
226     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
227
228     /* If we are of the requested type ourselves, don't look further */
229     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
230     {
231         p_this->i_refcount++;
232         vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
233         return p_this;
234     }
235
236     /* Otherwise, recursively look for the object */
237     if( (i_mode & 0x000f) == FIND_ANYWHERE )
238     {
239         p_found = vlc_object_find_inner( VLC_OBJECT(p_this->p_vlc),
240                                          i_type,
241                                          (i_mode & ~0x000f) | FIND_CHILD );
242     }
243     else
244     {
245         p_found = vlc_object_find_inner( p_this, i_type, i_mode );
246     }
247
248     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
249
250     return p_found;
251 }
252
253 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
254                                              int i_type, int i_mode )
255 {
256     int i;
257     vlc_object_t *p_tmp;
258
259     switch( i_mode & 0x000f )
260     {
261     case FIND_PARENT:
262         for( i = p_this->i_parents; i--; )
263         {
264             p_tmp = p_this->pp_parents[i];
265             if( p_tmp->i_object_type == i_type )
266             {
267                 p_tmp->i_refcount++;
268                 return p_tmp;
269             }
270             else if( p_tmp->i_parents )
271             {
272                 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
273                 if( p_tmp )
274                 {
275                     return p_tmp;
276                 }
277             }
278         }
279         break;
280
281     case FIND_CHILD:
282         for( i = p_this->i_children; i--; )
283         {
284             p_tmp = p_this->pp_children[i];
285             if( p_tmp->i_object_type == i_type )
286             {
287                 p_tmp->i_refcount++;
288                 return p_tmp;
289             }
290             else if( p_tmp->i_children )
291             {
292                 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
293                 if( p_tmp )
294                 {
295                     return p_tmp;
296                 }
297             }
298         }
299         break;
300
301     case FIND_ANYWHERE:
302         /* Handled in vlc_object_find */
303         break;
304     }
305
306     return NULL;
307 }
308
309 /*****************************************************************************
310  * vlc_object_yield: increment an object refcount
311  *****************************************************************************/
312 void __vlc_object_yield( vlc_object_t *p_this )
313 {
314     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
315     p_this->i_refcount++;
316     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
317 }
318
319 /*****************************************************************************
320  * vlc_object_release: decrement an object refcount
321  *****************************************************************************/
322 void __vlc_object_release( vlc_object_t *p_this )
323 {
324     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
325     p_this->i_refcount--;
326     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
327 }
328
329 /*****************************************************************************
330  * vlc_object_attach: attach object to a parent object
331  *****************************************************************************
332  * This function sets p_this as a child of p_parent, and p_parent as a parent
333  * of p_this. This link can be undone using vlc_object_detach.
334  *****************************************************************************/
335 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
336 {
337     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
338
339     p_this->i_parents++;
340     p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
341                             p_this->i_parents * sizeof(vlc_object_t *) );
342     p_this->pp_parents[p_this->i_parents - 1] = p_parent;
343
344     p_parent->i_children++;
345     p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
346                                p_parent->i_children * sizeof(vlc_object_t *) );
347     p_parent->pp_children[p_parent->i_children - 1] = p_this;
348
349     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
350 }
351
352 #if 0 /* UNUSED */
353 /* vlc_object_setchild: attach a child object */
354 void __vlc_object_setchild( vlc_object_t *p_this, vlc_object_t *p_child )
355 {
356     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
357
358     p_this->i_children++;
359     p_this->pp_children = (vlc_object_t **)realloc( p_this->pp_children,
360                              p_this->i_children * sizeof(vlc_object_t *) );
361     p_this->pp_children[p_this->i_children - 1] = p_child;
362
363     p_child->i_parents++;
364     p_child->pp_parents = (vlc_object_t **)realloc( p_child->pp_parents,
365                              p_child->i_parents * sizeof(vlc_object_t *) );
366     p_child->pp_parents[p_child->i_parents - 1] = p_this;
367
368     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
369 }
370 #endif
371
372 /*****************************************************************************
373  * vlc_object_detach_all: detach object from its parents
374  *****************************************************************************
375  * This function unlinks an object from all its parents. It is up to the
376  * object to get rid of its children, so this function doesn't do anything
377  * with them.
378  *****************************************************************************/
379 void __vlc_object_detach_all( vlc_object_t *p_this )
380 {
381     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
382
383     /* FIXME: BORK ! BORK ! BORK !!! THIS STUFF IS BORKED !! FIXME */
384     while( p_this->i_parents )
385     {
386         /* Not very effective because we know the index, but we'd have to
387          * parse p_parent->pp_children anyway. Plus, we remove duplicates
388          * by not using the object's index */
389         vlc_object_detach_inner( p_this, p_this->pp_parents[0] );
390     }
391
392     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
393 }
394
395 /*****************************************************************************
396  * vlc_object_detach: remove a parent/child link
397  *****************************************************************************
398  * This function removes all links between an object and a given parent.
399  *****************************************************************************/
400 void __vlc_object_detach( vlc_object_t *p_this, vlc_object_t *p_parent )
401 {
402     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
403     vlc_object_detach_inner( p_this, p_parent );
404     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
405 }
406
407 static void vlc_object_detach_inner( vlc_object_t *p_this,
408                                      vlc_object_t *p_parent )
409 {
410     int i_index, i;
411
412     /* Remove all of p_this's parents which are p_parent */
413     for( i_index = p_this->i_parents ; i_index-- ; )
414     {
415         if( p_this->pp_parents[i_index] == p_parent )
416         {
417             p_this->i_parents--;
418             for( i = i_index ; i < p_this->i_parents ; i++ )
419             {
420                 p_this->pp_parents[i] = p_this->pp_parents[i+1];
421             }
422         }
423     }
424
425     if( p_this->i_parents )
426     {
427         p_this->pp_parents = (vlc_object_t **)realloc( p_this->pp_parents,
428                                 p_this->i_parents * sizeof(vlc_object_t *) );
429     }
430     else
431     {
432         free( p_this->pp_parents );
433         p_this->pp_parents = NULL;
434     }
435
436     /* Remove all of p_parent's children which are p_this */
437     for( i_index = p_parent->i_children ; i_index-- ; )
438     {
439         if( p_parent->pp_children[i_index] == p_this )
440         {
441             p_parent->i_children--;
442             for( i = i_index ; i < p_parent->i_children ; i++ )
443             {
444                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
445             }
446         }
447     }
448
449     if( p_parent->i_children )
450     {
451         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
452                                p_parent->i_children * sizeof(vlc_object_t *) );
453     }
454     else
455     {
456         free( p_parent->pp_children );
457         p_parent->pp_children = NULL;
458     }
459 }
460
461 /*****************************************************************************
462  * vlc_dumpstructure: print the current vlc structure
463  *****************************************************************************
464  * This function prints an ASCII tree showing the connections between vlc
465  * objects, and additional information such as their refcount, thread ID,
466  * address, etc.
467  *****************************************************************************/
468 void __vlc_dumpstructure( vlc_object_t *p_this )
469 {
470     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
471
472     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
473     psz_foo[0] = '|';
474     vlc_dumpstructure_inner( p_this, 0, psz_foo );
475     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
476 }
477
478 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
479                                      int i_level, char *psz_foo )
480 {
481     int i;
482     char i_back = psz_foo[i_level];
483     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
484
485     psz_name[0] = '\0';
486     if( p_this->psz_object_name )
487     {
488         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
489         psz_name[48] = '\"';
490         psz_name[49] = '\0';
491     }
492
493     psz_children[0] = '\0';
494     switch( p_this->i_children )
495     {
496         case 0:
497             break;
498         case 1:
499             strcpy( psz_children, ", 1 child" );
500             break;
501         default:
502             snprintf( psz_children, 20,
503                       ", %i children", p_this->i_children );
504             psz_children[19] = '\0';
505             break;
506     }
507
508     psz_refcount[0] = '\0';
509     if( p_this->i_refcount )
510     {
511         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
512         psz_refcount[19] = '\0';
513     }
514
515     psz_thread[0] = '\0';
516     if( p_this->b_thread )
517     {
518         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
519         psz_thread[19] = '\0';
520     }
521
522     psz_foo[i_level] = '\0';
523     msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
524               p_this, psz_name, psz_thread, psz_refcount, psz_children );
525     psz_foo[i_level] = i_back;
526
527     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
528     {
529         msg_Warn( p_this, "structure tree is too deep" );
530         return;
531     }
532
533     for( i = 0 ; i < p_this->i_children ; i++ )
534     {
535         if( i_level )
536         {
537             psz_foo[i_level-1] = ' ';
538
539             if( psz_foo[i_level-2] == '`' )
540             {
541                 psz_foo[i_level-2] = ' ';
542             }
543         }
544
545         if( i == p_this->i_children - 1 )
546         {
547             psz_foo[i_level] = '`';
548         }
549         else
550         {
551             psz_foo[i_level] = '|';
552         }
553
554         psz_foo[i_level+1] = '-';
555         psz_foo[i_level+2] = '\0';
556
557         vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );
558     }
559 }
560