]> git.sesse.net Git - vlc/blob - src/misc/objects.c
98a4062018c9fbb7015bcc58feff05addae9420a
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  */
28
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <vlc/vlc.h>
34
35 #include "../libvlc.h"
36 #include <vlc_vout.h>
37 #include <vlc_aout.h>
38 #include "audio_output/aout_internal.h"
39
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
42 #include <vlc_stream.h>
43
44 #include <vlc_sout.h>
45 #include "stream_output/stream_output.h"
46
47 #include "vlc_playlist.h"
48 #include "vlc_interface.h"
49 #include "vlc_codec.h"
50 #include "vlc_filter.h"
51
52 #include "vlc_httpd.h"
53 #include "vlc_vlm.h"
54 #include "input/vlm_internal.h"
55 #include "vlc_vod.h"
56 #include "vlc_tls.h"
57 #include "vlc_xml.h"
58 #include "vlc_osd.h"
59 #include "vlc_meta.h"
60
61 #include "variables.h"
62 #ifndef WIN32
63 # include <unistd.h>
64 #else
65 # include <io.h>
66 # include <fcntl.h>
67 #endif
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int  DumpCommand( vlc_object_t *, char const *,
73                          vlc_value_t, vlc_value_t, void * );
74
75 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
76 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
77 static void           DetachObject  ( vlc_object_t * );
78 static void           PrintObject   ( vlc_object_t *, const char * );
79 static void           DumpStructure ( vlc_object_t *, int, char * );
80 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
81 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
82
83 static vlc_list_t   * NewList       ( int );
84 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
85 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
86 static int            CountChildren ( vlc_object_t *, int );
87 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
88
89 /*****************************************************************************
90  * Local structure lock
91  *****************************************************************************/
92 static vlc_mutex_t    structure_lock;
93 static vlc_object_internals_t global_internals;
94
95 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
96                                  int i_type, const char *psz_type )
97 {
98     vlc_object_t *p_new;
99     vlc_object_internals_t *p_priv;
100
101     if( i_type == VLC_OBJECT_GLOBAL )
102     {
103         p_new = p_this;
104         p_priv = &global_internals;
105         memset( p_priv, 0, sizeof( *p_priv ) );
106     }
107     else
108     {
109         p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
110         if( p_priv == NULL )
111             return NULL;
112
113         p_new = (vlc_object_t *)(p_priv + 1);
114     }
115
116     p_new->p_internals = p_priv;
117     p_new->i_object_type = i_type;
118     p_new->psz_object_type = psz_type;
119
120     p_new->psz_object_name = NULL;
121
122     p_new->b_die = VLC_FALSE;
123     p_new->b_error = VLC_FALSE;
124     p_new->b_dead = VLC_FALSE;
125     p_priv->b_attached = VLC_FALSE;
126     p_new->b_force = VLC_FALSE;
127
128     p_new->psz_header = NULL;
129
130     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
131         p_new->i_flags |= OBJECT_FLAGS_NODBG;
132     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
133         p_new->i_flags |= OBJECT_FLAGS_QUIET;
134     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
135         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
136
137     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
138
139     if( !p_priv->p_vars )
140     {
141         if( i_type != VLC_OBJECT_GLOBAL )
142             free( p_priv );
143         return NULL;
144     }
145
146     if( i_type == VLC_OBJECT_GLOBAL )
147     {
148         /* If i_type is global, then p_new is actually p_libvlc_global */
149         libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
150         p_new->p_libvlc = NULL;
151
152         p_libvlc_global->i_counter = 0;
153         p_new->i_object_id = 0;
154
155         p_libvlc_global->i_objects = 1;
156         p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
157         p_libvlc_global->pp_objects[0] = p_new;
158         p_priv->b_attached = VLC_TRUE;
159     }
160     else
161     {
162         libvlc_global_data_t *p_libvlc_global = vlc_global();
163         if( i_type == VLC_OBJECT_LIBVLC )
164         {
165             p_new->p_libvlc = (libvlc_int_t*)p_new;
166             p_priv->b_attached = VLC_TRUE;
167         }
168         else
169         {
170             p_new->p_libvlc = p_this->p_libvlc;
171         }
172
173         vlc_mutex_lock( &structure_lock );
174
175         p_libvlc_global->i_counter++;
176         p_new->i_object_id = p_libvlc_global->i_counter;
177
178         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
179          * useless to try and recover anything if pp_objects gets smashed. */
180         TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
181                     p_new );
182
183         vlc_mutex_unlock( &structure_lock );
184     }
185
186     p_priv->i_refcount = 0;
187     p_new->p_parent = NULL;
188     p_new->pp_children = NULL;
189     p_new->i_children = 0;
190
191     p_new->p_private = NULL;
192
193     /* Initialize mutexes and condvars */
194     vlc_mutex_init( p_new, &p_new->object_lock );
195     vlc_cond_init( p_new, &p_new->object_wait );
196     vlc_mutex_init( p_new, &p_priv->var_lock );
197     p_priv->pipes[0] = p_priv->pipes[1] = -1;
198
199     if( i_type == VLC_OBJECT_GLOBAL )
200     {
201         vlc_mutex_init( p_new, &structure_lock );
202     }
203
204     if( i_type == VLC_OBJECT_LIBVLC )
205     {
206         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
207         var_AddCallback( p_new, "list", DumpCommand, NULL );
208         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
209         var_AddCallback( p_new, "tree", DumpCommand, NULL );
210         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
211         var_AddCallback( p_new, "vars", DumpCommand, NULL );
212     }
213
214     return p_new;
215 }
216
217
218 /**
219  * Allocates and initializes a vlc object.
220  *
221  * @param i_type known object type (all of them are negative integer values),
222  *               or object byte size (always positive).
223  *
224  * @return the new object, or NULL on error.
225  */
226 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
227 {
228     const char   * psz_type;
229     size_t         i_size;
230
231     switch( i_type )
232     {
233         case VLC_OBJECT_GLOBAL:
234             i_size = sizeof(libvlc_global_data_t);
235             psz_type = "global";
236             break;
237         case VLC_OBJECT_LIBVLC:
238             i_size = sizeof(libvlc_int_t);
239             psz_type = "libvlc";
240             break;
241         case VLC_OBJECT_INTF:
242             i_size = sizeof(intf_thread_t);
243             psz_type = "interface";
244             break;
245         case VLC_OBJECT_DIALOGS:
246             i_size = sizeof(intf_thread_t);
247             psz_type = "dialogs";
248             break;
249         case VLC_OBJECT_PLAYLIST:
250             i_size = sizeof(playlist_t);
251             psz_type = "playlist";
252             break;
253         case VLC_OBJECT_SD:
254             i_size = sizeof(services_discovery_t);
255             psz_type = "services discovery";
256             break;
257         case VLC_OBJECT_INPUT:
258             i_size = sizeof(input_thread_t);
259             psz_type = "input";
260             break;
261         case VLC_OBJECT_DEMUX:
262             i_size = sizeof(demux_t);
263             psz_type = "demux";
264             break;
265         case VLC_OBJECT_ACCESS:
266             i_size = sizeof(access_t);
267             psz_type = "access";
268             break;
269         case VLC_OBJECT_DECODER:
270             i_size = sizeof(decoder_t);
271             psz_type = "decoder";
272             break;
273         case VLC_OBJECT_PACKETIZER:
274             i_size = sizeof(decoder_t);
275             psz_type = "packetizer";
276             break;
277         case VLC_OBJECT_ENCODER:
278             i_size = sizeof(encoder_t);
279             psz_type = "encoder";
280             break;
281         case VLC_OBJECT_FILTER:
282             i_size = sizeof(filter_t);
283             psz_type = "filter";
284             break;
285         case VLC_OBJECT_VOUT:
286             i_size = sizeof(vout_thread_t);
287             psz_type = "video output";
288             break;
289         case VLC_OBJECT_SPU:
290             i_size = sizeof(spu_t);
291             psz_type = "subpicture";
292             break;
293         case VLC_OBJECT_AOUT:
294             i_size = sizeof(aout_instance_t);
295             psz_type = "audio output";
296             break;
297         case VLC_OBJECT_SOUT:
298             i_size = sizeof(sout_instance_t);
299             psz_type = "stream output";
300             break;
301         case VLC_OBJECT_VLM:
302             i_size = sizeof( vlm_t );
303             psz_type = "vlm dameon";
304             break;
305         case VLC_OBJECT_VOD:
306             i_size = sizeof( vod_t );
307             psz_type = "vod server";
308             break;
309         case VLC_OBJECT_XML:
310             i_size = sizeof( xml_t );
311             psz_type = "xml";
312             break;
313         case VLC_OBJECT_OPENGL:
314             i_size = sizeof( vout_thread_t );
315             psz_type = "opengl";
316             break;
317         case VLC_OBJECT_ANNOUNCE:
318             i_size = sizeof( announce_handler_t );
319             psz_type = "announce";
320             break;
321         case VLC_OBJECT_META_ENGINE:
322             i_size = sizeof( meta_engine_t );
323             psz_type = "meta engine";
324             break;
325         case VLC_OBJECT_OSDMENU:
326             i_size = sizeof( osd_menu_t );
327             psz_type = "osd menu";
328             break;
329         default:
330             i_size = i_type > (int)sizeof(vlc_object_t)
331                          ? i_type : (int)sizeof(vlc_object_t);
332             i_type = VLC_OBJECT_GENERIC;
333             psz_type = "generic";
334             break;
335     }
336
337     return vlc_custom_create( p_this, i_size, i_type, psz_type );
338 }
339
340
341 /**
342  ****************************************************************************
343  * Destroy a vlc object
344  *
345  * This function destroys an object that has been previously allocated with
346  * vlc_object_create. The object's refcount must be zero and it must not be
347  * attached to other objects in any way.
348  *****************************************************************************/
349 void __vlc_object_destroy( vlc_object_t *p_this )
350 {
351     vlc_object_internals_t *p_priv = vlc_internals( p_this );
352     int i_delay = 0;
353
354     if( p_this->i_children )
355     {
356         msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
357                  p_this->i_object_id, p_this->psz_object_name );
358         return;
359     }
360
361     if( p_this->p_parent )
362     {
363         msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
364                  p_this->i_object_id, p_this->psz_object_name );
365         return;
366     }
367
368     while( p_priv->i_refcount > 0 )
369     {
370         i_delay++;
371
372         /* Don't warn immediately ... 100ms seems OK */
373         if( i_delay == 2 )
374         {
375             msg_Warn( p_this,
376                   "refcount is %u, delaying before deletion (id=%d,type=%d)",
377                   p_priv->i_refcount, p_this->i_object_id,
378                   p_this->i_object_type );
379         }
380         else if( i_delay == 10 )
381         {
382             msg_Err( p_this,
383                   "refcount is %u, delaying again (id=%d,type=%d)",
384                   p_priv->i_refcount, p_this->i_object_id,
385                   p_this->i_object_type );
386         }
387         else if( i_delay == 20 )
388         {
389             msg_Err( p_this,
390                   "waited too long, cancelling destruction (id=%d,type=%d)",
391                   p_this->i_object_id, p_this->i_object_type );
392             return;
393         }
394
395         msleep( 100000 );
396     }
397
398     /* Destroy the associated variables, starting from the end so that
399      * no memmove calls have to be done. */
400     while( p_priv->i_vars )
401     {
402         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
403     }
404
405     free( p_priv->p_vars );
406     vlc_mutex_destroy( &p_priv->var_lock );
407
408     if( p_this->psz_header ) free( p_this->psz_header );
409
410     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
411     {
412         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
413         /* We are the global object ... no need to lock. */
414         free( p_global->pp_objects );
415         p_global->pp_objects = NULL;
416         p_global->i_objects--;
417
418         vlc_mutex_destroy( &structure_lock );
419     }
420     else
421     {
422         libvlc_global_data_t *p_libvlc_global = vlc_global();
423         int i_index;
424
425         vlc_mutex_lock( &structure_lock );
426
427         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
428          * useless to try and recover anything if pp_objects gets smashed. */
429         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
430                              p_libvlc_global->i_objects );
431         REMOVE_ELEM( p_libvlc_global->pp_objects,
432                      p_libvlc_global->i_objects, i_index );
433
434         vlc_mutex_unlock( &structure_lock );
435     }
436
437 #if defined(WIN32) || defined(UNDER_CE)
438     /* if object has an associated thread, close it now */
439     if( p_priv->thread_id.hThread )
440        CloseHandle(p_priv->thread_id.hThread);
441 #endif
442
443     vlc_mutex_destroy( &p_this->object_lock );
444     vlc_cond_destroy( &p_this->object_wait );
445     if( p_priv->pipes[0] != -1 )
446         close( p_priv->pipes[0] );
447     if( p_priv->pipes[1] != -1 )
448         close( p_priv->pipes[1] );
449
450     /* global is not dynamically allocated by vlc_object_create */
451     if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
452         free( p_priv );
453 }
454
455
456 /** Inter-object signaling */
457
458 void __vlc_object_lock( vlc_object_t *obj )
459 {
460     vlc_mutex_lock( &obj->object_lock );
461 }
462
463 void __vlc_object_unlock( vlc_object_t *obj )
464 {
465     vlc_assert_locked( &obj->object_lock );
466     vlc_mutex_unlock( &obj->object_lock );
467 }
468
469 /**
470  * Returns the readable end of a pipe that becomes readable whenever
471  * an object is signaled. This can be used to wait for VLC object events
472  * inside select(), poll() loops or frameworks providing an event loop.
473  *
474  * Note that the pipe will remain the same for the lifetime of the object.
475  * DO NOT close it yourself. Ever.
476  *
477  * DO NOT try to read from the pipe either: call vlc_object_wait() instead.
478  * Assuming the pipe is readable, vlc_object_wait() will not block.
479  * Also note that, as with vlc_object_wait(), there may be spurious wakeups.
480  *
481  * @param obj object that would be signaled (object lock MUST hold)
482  * @return a readable pipe descriptor, or -1 on error.
483  */
484 int vlc_object_waitpipe( vlc_object_t *obj )
485 {
486     int *pipes = obj->p_internals->pipes;
487     vlc_assert_locked( &obj->object_lock );
488
489     if( pipes[1] == -1 )
490     {
491         /* This can only ever happen if someone killed us without locking */
492         assert( pipes[0] == -1 );
493
494 #ifndef WIN32
495         if( pipe( pipes ) )
496 #endif
497             return -1;
498     }
499
500     return pipes[0];
501 }
502
503
504 /**
505  * Waits for the object to be signaled (using vlc_object_signal()).
506  * If the object already has a signal pending, this function will return
507  * immediately. It is asserted that the caller holds the object lock.
508  *
509  * @return true if the object is dying and should terminate.
510  */
511 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
512 {
513     vlc_assert_locked( &obj->object_lock );
514
515     int fd = obj->p_internals->pipes[0];
516     if( fd != -1 )
517     {
518         if( read( fd, &(char){ 0 }, 1 ) == 0 )
519         {
520             close( fd );
521             obj->p_internals->pipes[1] = -1;
522         }
523         return obj->b_die;
524     }
525
526     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
527     return obj->b_die;
528 }
529
530
531 /**
532  * Waits for the object to be signaled (using vlc_object_signal()), or for
533  * a timer to expire.
534  * If the object already has a signal pending, this function will return
535  * immediately. It is asserted that the caller holds the object lock.
536  *
537  * @return negative if the object is dying and should terminate,
538  * positive if the the object has been signaled but is not dying,
539  * 0 if timeout has been reached.
540  */
541 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
542 {
543     int v;
544
545     vlc_assert_locked( &obj->object_lock );
546     v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
547     if( v == 0 ) /* signaled */
548         return obj->b_die ? -1 : 1;
549     return 0;
550 }
551
552
553 /**
554  * Checks whether an object has been "killed".
555  * The object lock must be held.
556  *
557  * Typical code for an object thread could be:
558  *
559    vlc_object_lock (self);
560    ...initialization...
561    while (vlc_object_alive (self))
562    {
563        ...preprocessing...
564
565        if (vlc_object_wait (self))
566            continue;
567
568        ...postprocessing...
569    }
570    ...deinitialization...
571    vlc_object_unlock (self);
572  *
573  *
574  * @return true iff the object has not been killed yet
575  */
576 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
577 {
578     vlc_assert_locked( &obj->object_lock );
579     return !obj->b_die;
580 }
581
582
583 /**
584  * Signals an object for which the lock is held.
585  */
586 void __vlc_object_signal_unlocked( vlc_object_t *obj )
587 {
588     vlc_assert_locked( &obj->object_lock );
589
590     int fd = obj->p_internals->pipes[1];
591     if( fd != -1 )
592         while( write( fd, &(char){ 0 }, 1 ) < 0 );
593
594     vlc_cond_signal( &obj->object_wait );
595 }
596
597
598 /**
599  * Requests termination of an object.
600  * If the object is LibVLC, also request to terminate all its children.
601  */
602 void __vlc_object_kill( vlc_object_t *p_this )
603 {
604     vlc_mutex_lock( &p_this->object_lock );
605     p_this->b_die = VLC_TRUE;
606
607     if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
608         for( int i = 0; i < p_this->i_children ; i++ )
609             vlc_object_kill( p_this->pp_children[i] );
610
611     int fd = p_this->p_internals->pipes[1];
612     if( fd != -1 )
613     {
614         close( fd ); /* closing a pipe makes it readable too */
615         p_this->p_internals->pipes[1] = -1;
616     }
617
618     vlc_object_signal_unlocked( p_this );
619     vlc_mutex_unlock( &p_this->object_lock );
620 }
621
622
623 /**
624  * find an object given its ID
625  *
626  * This function looks for the object whose i_object_id field is i_id. We
627  * use a dichotomy so that lookups are in log2(n).
628  *****************************************************************************/
629 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
630 {
631     int i_max, i_middle;
632     vlc_object_t **pp_objects;
633     libvlc_global_data_t *p_libvlc_global = vlc_global();
634
635     vlc_mutex_lock( &structure_lock );
636
637     pp_objects = p_libvlc_global->pp_objects;
638
639     /* Perform our dichotomy */
640     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
641     {
642         i_middle = i_max / 2;
643
644         if( pp_objects[i_middle]->i_object_id > i_id )
645         {
646             i_max = i_middle;
647         }
648         else if( pp_objects[i_middle]->i_object_id < i_id )
649         {
650             if( i_middle )
651             {
652                 pp_objects += i_middle;
653                 i_max -= i_middle;
654             }
655             else
656             {
657                 /* This happens when there are only two remaining objects */
658                 if( pp_objects[i_middle+1]->i_object_id == i_id )
659                 {
660                     vlc_mutex_unlock( &structure_lock );
661                     pp_objects[i_middle+1]->p_internals->i_refcount++;
662                     return pp_objects[i_middle+1];
663                 }
664                 break;
665             }
666         }
667         else
668         {
669             vlc_mutex_unlock( &structure_lock );
670             pp_objects[i_middle]->p_internals->i_refcount++;
671             return pp_objects[i_middle];
672         }
673
674         if( i_max == 0 )
675         {
676             /* this means that i_max == i_middle, and since we have already
677              * tested pp_objects[i_middle]), p_found is properly set. */
678             break;
679         }
680     }
681
682     vlc_mutex_unlock( &structure_lock );
683     return NULL;
684 }
685
686 /**
687  ****************************************************************************
688  * find a typed object and increment its refcount
689  *****************************************************************************
690  * This function recursively looks for a given object type. i_mode can be one
691  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
692  *****************************************************************************/
693 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
694 {
695     vlc_object_t *p_found;
696
697     vlc_mutex_lock( &structure_lock );
698
699     /* If we are of the requested type ourselves, don't look further */
700     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
701     {
702         p_this->p_internals->i_refcount++;
703         vlc_mutex_unlock( &structure_lock );
704         return p_this;
705     }
706
707     /* Otherwise, recursively look for the object */
708     if( (i_mode & 0x000f) == FIND_ANYWHERE )
709     {
710         vlc_object_t *p_root = p_this;
711
712         /* Find the root */
713         while( p_root->p_parent != NULL &&
714                p_root != VLC_OBJECT( p_this->p_libvlc ) )
715         {
716             p_root = p_root->p_parent;
717         }
718
719         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
720         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
721         {
722             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
723                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
724         }
725     }
726     else
727     {
728         p_found = FindObject( p_this, i_type, i_mode );
729     }
730
731     vlc_mutex_unlock( &structure_lock );
732
733     return p_found;
734 }
735
736 /**
737  ****************************************************************************
738  * find a named object and increment its refcount
739  *****************************************************************************
740  * This function recursively looks for a given object name. i_mode can be one
741  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
742  *****************************************************************************/
743 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
744                                int i_mode )
745 {
746     vlc_object_t *p_found;
747
748     vlc_mutex_lock( &structure_lock );
749
750     /* If have the requested name ourselves, don't look further */
751     if( !(i_mode & FIND_STRICT)
752         && p_this->psz_object_name
753         && !strcmp( p_this->psz_object_name, psz_name ) )
754     {
755         p_this->p_internals->i_refcount++;
756         vlc_mutex_unlock( &structure_lock );
757         return p_this;
758     }
759
760     /* Otherwise, recursively look for the object */
761     if( (i_mode & 0x000f) == FIND_ANYWHERE )
762     {
763         vlc_object_t *p_root = p_this;
764
765         /* Find the root */
766         while( p_root->p_parent != NULL &&
767                p_root != VLC_OBJECT( p_this->p_libvlc ) )
768         {
769             p_root = p_root->p_parent;
770         }
771
772         p_found = FindObjectName( p_root, psz_name,
773                                  (i_mode & ~0x000f)|FIND_CHILD );
774         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
775         {
776             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
777                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
778         }
779     }
780     else
781     {
782         p_found = FindObjectName( p_this, psz_name, i_mode );
783     }
784
785     vlc_mutex_unlock( &structure_lock );
786
787     return p_found;
788 }
789
790 /**
791  ****************************************************************************
792  * increment an object refcount
793  *****************************************************************************/
794 void __vlc_object_yield( vlc_object_t *p_this )
795 {
796     vlc_mutex_lock( &structure_lock );
797     p_this->p_internals->i_refcount++;
798     vlc_mutex_unlock( &structure_lock );
799 }
800
801 static inline void Release( vlc_object_t *obj )
802 {
803     assert( obj->p_internals->i_refcount > 0 );
804     obj->p_internals->i_refcount--;
805 }
806
807 /*****************************************************************************
808  * decrement an object refcount
809  *****************************************************************************/
810 void __vlc_object_release( vlc_object_t *p_this )
811 {
812     vlc_mutex_lock( &structure_lock );
813     Release( p_this );
814     vlc_mutex_unlock( &structure_lock );
815 }
816
817 /**
818  ****************************************************************************
819  * attach object to a parent object
820  *****************************************************************************
821  * This function sets p_this as a child of p_parent, and p_parent as a parent
822  * of p_this. This link can be undone using vlc_object_detach.
823  *****************************************************************************/
824 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
825 {
826     if( !p_this ) return;
827
828     vlc_mutex_lock( &structure_lock );
829
830     /* Attach the parent to its child */
831     p_this->p_parent = p_parent;
832
833     /* Attach the child to its parent */
834     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
835                  p_parent->i_children, p_this );
836
837     /* Climb up the tree to see whether we are connected with the root */
838     if( p_parent->p_internals->b_attached )
839     {
840         SetAttachment( p_this, VLC_TRUE );
841     }
842
843     vlc_mutex_unlock( &structure_lock );
844 }
845
846 /**
847  ****************************************************************************
848  * detach object from its parent
849  *****************************************************************************
850  * This function removes all links between an object and its parent.
851  *****************************************************************************/
852 void __vlc_object_detach( vlc_object_t *p_this )
853 {
854     if( !p_this ) return;
855
856     vlc_mutex_lock( &structure_lock );
857     if( !p_this->p_parent )
858     {
859         msg_Err( p_this, "object is not attached" );
860         vlc_mutex_unlock( &structure_lock );
861         return;
862     }
863
864     /* Climb up the tree to see whether we are connected with the root */
865     if( p_this->p_parent->p_internals->b_attached )
866     {
867         SetAttachment( p_this, VLC_FALSE );
868     }
869
870     DetachObject( p_this );
871     vlc_mutex_unlock( &structure_lock );
872     p_this = NULL;
873 }
874
875 /**
876  ****************************************************************************
877  * find a list typed objects and increment their refcount
878  *****************************************************************************
879  * This function recursively looks for a given object type. i_mode can be one
880  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
881  *****************************************************************************/
882 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
883 {
884     vlc_list_t *p_list;
885     vlc_object_t **pp_current, **pp_end;
886     int i_count = 0, i_index = 0;
887     libvlc_global_data_t *p_libvlc_global = vlc_global();
888
889     vlc_mutex_lock( &structure_lock );
890
891     /* Look for the objects */
892     switch( i_mode & 0x000f )
893     {
894     case FIND_ANYWHERE:
895         pp_current = p_libvlc_global->pp_objects;
896         pp_end = pp_current + p_libvlc_global->i_objects;
897
898         for( ; pp_current < pp_end ; pp_current++ )
899         {
900             if( (*pp_current)->p_internals->b_attached
901                  && (*pp_current)->i_object_type == i_type )
902             {
903                 i_count++;
904             }
905         }
906
907         p_list = NewList( i_count );
908         pp_current = p_libvlc_global->pp_objects;
909
910         for( ; pp_current < pp_end ; pp_current++ )
911         {
912             if( (*pp_current)->p_internals->b_attached
913                  && (*pp_current)->i_object_type == i_type )
914             {
915                 ListReplace( p_list, *pp_current, i_index );
916                 if( i_index < i_count ) i_index++;
917             }
918         }
919     break;
920
921     case FIND_CHILD:
922         i_count = CountChildren( p_this, i_type );
923         p_list = NewList( i_count );
924
925         /* Check allocation was successful */
926         if( p_list->i_count != i_count )
927         {
928             msg_Err( p_this, "list allocation failed!" );
929             p_list->i_count = 0;
930             break;
931         }
932
933         p_list->i_count = 0;
934         ListChildren( p_list, p_this, i_type );
935         break;
936
937     default:
938         msg_Err( p_this, "unimplemented!" );
939         p_list = NewList( 0 );
940         break;
941     }
942
943     vlc_mutex_unlock( &structure_lock );
944
945     return p_list;
946 }
947
948 /*****************************************************************************
949  * DumpCommand: print the current vlc structure
950  *****************************************************************************
951  * This function prints either an ASCII tree showing the connections between
952  * vlc objects, and additional information such as their refcount, thread ID,
953  * etc. (command "tree"), or the same data as a simple list (command "list").
954  *****************************************************************************/
955 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
956                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
957 {
958     libvlc_global_data_t *p_libvlc_global = vlc_global();
959
960     (void)oldval; (void)p_data;
961     if( *psz_cmd == 'l' )
962     {
963         vlc_mutex_lock( &structure_lock );
964
965         vlc_object_t **pp_current, **pp_end;
966
967         pp_current = p_libvlc_global->pp_objects;
968         pp_end = pp_current + p_libvlc_global->i_objects;
969
970         for( ; pp_current < pp_end ; pp_current++ )
971         {
972             if( (*pp_current)->p_internals->b_attached )
973             {
974                 PrintObject( *pp_current, "" );
975             }
976             else
977             {
978                 printf( " o %.8i %s (not attached)\n",
979                         (*pp_current)->i_object_id,
980                         (*pp_current)->psz_object_type );
981             }
982         }
983
984         vlc_mutex_unlock( &structure_lock );
985     }
986     else
987     {
988         vlc_object_t *p_object = NULL;
989
990         if( *newval.psz_string )
991         {
992             char *end;
993             int i_id = strtol( newval.psz_string, &end, 0 );
994             if( end != newval.psz_string )
995                 p_object = vlc_object_get( p_this, i_id );
996             else
997             {
998                 /* try using the object's name to find it */
999                 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1000                 if( p_libvlc )
1001                 {
1002                     /* Look in p_libvlc's children tree */
1003                     p_object = vlc_object_find_name( p_libvlc,
1004                                                      newval.psz_string,
1005                                                      FIND_CHILD );
1006                     vlc_object_release( p_libvlc );
1007                 }
1008                 if( !p_object )
1009                 {
1010                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1011                     p_object = vlc_object_find_name( p_this,
1012                                                      newval.psz_string,
1013                                                      FIND_CHILD );
1014                 }
1015             }
1016
1017             if( !p_object )
1018             {
1019                 return VLC_ENOOBJ;
1020             }
1021         }
1022
1023         vlc_mutex_lock( &structure_lock );
1024
1025         if( *psz_cmd == 't' )
1026         {
1027             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1028
1029             if( !p_object )
1030                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1031
1032             psz_foo[0] = '|';
1033             DumpStructure( p_object, 0, psz_foo );
1034         }
1035         else if( *psz_cmd == 'v' )
1036         {
1037             int i;
1038
1039             if( !p_object )
1040                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1041
1042             PrintObject( p_object, "" );
1043
1044             if( !p_object->p_internals->i_vars )
1045                 printf( " `-o No variables\n" );
1046             for( i = 0; i < p_object->p_internals->i_vars; i++ )
1047             {
1048                 variable_t *p_var = p_object->p_internals->p_vars + i;
1049
1050                 const char *psz_type = "unknown";
1051                 switch( p_var->i_type & VLC_VAR_TYPE )
1052                 {
1053 #define MYCASE( type, nice )                \
1054                     case VLC_VAR_ ## type:  \
1055                         psz_type = nice;    \
1056                         break;
1057                     MYCASE( VOID, "void" );
1058                     MYCASE( BOOL, "bool" );
1059                     MYCASE( INTEGER, "integer" );
1060                     MYCASE( HOTKEY, "hotkey" );
1061                     MYCASE( STRING, "string" );
1062                     MYCASE( MODULE, "module" );
1063                     MYCASE( FILE, "file" );
1064                     MYCASE( DIRECTORY, "directory" );
1065                     MYCASE( VARIABLE, "variable" );
1066                     MYCASE( FLOAT, "float" );
1067                     MYCASE( TIME, "time" );
1068                     MYCASE( ADDRESS, "address" );
1069                     MYCASE( MUTEX, "mutex" );
1070                     MYCASE( LIST, "list" );
1071 #undef MYCASE
1072                 }
1073                 printf( " %c-o \"%s\" (%s",
1074                         i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1075                         p_var->psz_name, psz_type );
1076                 if( p_var->psz_text )
1077                     printf( ", %s", p_var->psz_text );
1078                 printf( ")" );
1079                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1080                     printf( ", command" );
1081                 if( p_var->i_entries )
1082                     printf( ", %d callbacks", p_var->i_entries );
1083                 switch( p_var->i_type & 0x00f0 )
1084                 {
1085                     case VLC_VAR_VOID:
1086                     case VLC_VAR_MUTEX:
1087                         break;
1088                     case VLC_VAR_BOOL:
1089                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1090                         break;
1091                     case VLC_VAR_INTEGER:
1092                         printf( ": %d", p_var->val.i_int );
1093                         break;
1094                     case VLC_VAR_STRING:
1095                         printf( ": \"%s\"", p_var->val.psz_string );
1096                         break;
1097                     case VLC_VAR_FLOAT:
1098                         printf( ": %f", p_var->val.f_float );
1099                         break;
1100                     case VLC_VAR_TIME:
1101                         printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1102                         break;
1103                     case VLC_VAR_ADDRESS:
1104                         printf( ": %p", p_var->val.p_address );
1105                         break;
1106                     case VLC_VAR_LIST:
1107                         printf( ": TODO" );
1108                         break;
1109                 }
1110                 printf( "\n" );
1111             }
1112         }
1113
1114         vlc_mutex_unlock( &structure_lock );
1115
1116         if( *newval.psz_string )
1117         {
1118             vlc_object_release( p_object );
1119         }
1120     }
1121
1122     return VLC_SUCCESS;
1123 }
1124
1125 /*****************************************************************************
1126  * vlc_list_release: free a list previously allocated by vlc_list_find
1127  *****************************************************************************
1128  * This function decreases the refcount of all objects in the list and
1129  * frees the list.
1130  *****************************************************************************/
1131 void vlc_list_release( vlc_list_t *p_list )
1132 {
1133     int i_index;
1134
1135     vlc_mutex_lock( &structure_lock );
1136     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1137     {
1138         Release( p_list->p_values[i_index].p_object );
1139     }
1140     vlc_mutex_unlock( &structure_lock );
1141
1142     free( p_list->p_values );
1143     free( p_list );
1144 }
1145
1146 /* Following functions are local */
1147
1148 /*****************************************************************************
1149  * FindIndex: find the index of an object in an array of objects
1150  *****************************************************************************
1151  * This function assumes that p_this can be found in pp_objects. It will not
1152  * crash if p_this cannot be found, but will return a wrong value. It is your
1153  * duty to check the return value if you are not certain that the object could
1154  * be found for sure.
1155  *****************************************************************************/
1156 static int FindIndex( vlc_object_t *p_this,
1157                       vlc_object_t **pp_objects, int i_count )
1158 {
1159     int i_middle = i_count / 2;
1160
1161     if( i_count == 0 )
1162     {
1163         return 0;
1164     }
1165
1166     if( pp_objects[i_middle] == p_this )
1167     {
1168         return i_middle;
1169     }
1170
1171     if( i_count == 1 )
1172     {
1173         return 0;
1174     }
1175
1176     /* We take advantage of the sorted array */
1177     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1178     {
1179         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1180                                              i_count - i_middle );
1181     }
1182     else
1183     {
1184         return FindIndex( p_this, pp_objects, i_middle );
1185     }
1186 }
1187
1188 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1189 {
1190     int i;
1191     vlc_object_t *p_tmp;
1192
1193     switch( i_mode & 0x000f )
1194     {
1195     case FIND_PARENT:
1196         p_tmp = p_this->p_parent;
1197         if( p_tmp )
1198         {
1199             if( p_tmp->i_object_type == i_type )
1200             {
1201                 p_tmp->p_internals->i_refcount++;
1202                 return p_tmp;
1203             }
1204             else
1205             {
1206                 return FindObject( p_tmp, i_type, i_mode );
1207             }
1208         }
1209         break;
1210
1211     case FIND_CHILD:
1212         for( i = p_this->i_children; i--; )
1213         {
1214             p_tmp = p_this->pp_children[i];
1215             if( p_tmp->i_object_type == i_type )
1216             {
1217                 p_tmp->p_internals->i_refcount++;
1218                 return p_tmp;
1219             }
1220             else if( p_tmp->i_children )
1221             {
1222                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1223                 if( p_tmp )
1224                 {
1225                     return p_tmp;
1226                 }
1227             }
1228         }
1229         break;
1230
1231     case FIND_ANYWHERE:
1232         /* Handled in vlc_object_find */
1233         break;
1234     }
1235
1236     return NULL;
1237 }
1238
1239 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1240                                       const char *psz_name,
1241                                       int i_mode )
1242 {
1243     int i;
1244     vlc_object_t *p_tmp;
1245
1246     switch( i_mode & 0x000f )
1247     {
1248     case FIND_PARENT:
1249         p_tmp = p_this->p_parent;
1250         if( p_tmp )
1251         {
1252             if( p_tmp->psz_object_name
1253                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1254             {
1255                 p_tmp->p_internals->i_refcount++;
1256                 return p_tmp;
1257             }
1258             else
1259             {
1260                 return FindObjectName( p_tmp, psz_name, i_mode );
1261             }
1262         }
1263         break;
1264
1265     case FIND_CHILD:
1266         for( i = p_this->i_children; i--; )
1267         {
1268             p_tmp = p_this->pp_children[i];
1269             if( p_tmp->psz_object_name
1270                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1271             {
1272                 p_tmp->p_internals->i_refcount++;
1273                 return p_tmp;
1274             }
1275             else if( p_tmp->i_children )
1276             {
1277                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1278                 if( p_tmp )
1279                 {
1280                     return p_tmp;
1281                 }
1282             }
1283         }
1284         break;
1285
1286     case FIND_ANYWHERE:
1287         /* Handled in vlc_object_find */
1288         break;
1289     }
1290
1291     return NULL;
1292 }
1293
1294 static void DetachObject( vlc_object_t *p_this )
1295 {
1296     vlc_object_t *p_parent = p_this->p_parent;
1297     int i_index, i;
1298
1299     /* Remove p_this's parent */
1300     p_this->p_parent = NULL;
1301
1302     /* Remove all of p_parent's children which are p_this */
1303     for( i_index = p_parent->i_children ; i_index-- ; )
1304     {
1305         if( p_parent->pp_children[i_index] == p_this )
1306         {
1307             p_parent->i_children--;
1308             for( i = i_index ; i < p_parent->i_children ; i++ )
1309             {
1310                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1311             }
1312         }
1313     }
1314
1315     if( p_parent->i_children )
1316     {
1317         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1318                                p_parent->i_children * sizeof(vlc_object_t *) );
1319     }
1320     else
1321     {
1322         free( p_parent->pp_children );
1323         p_parent->pp_children = NULL;
1324     }
1325 }
1326
1327 /*****************************************************************************
1328  * SetAttachment: recursively set the b_attached flag of a subtree.
1329  *****************************************************************************
1330  * This function is used by the attach and detach functions to propagate
1331  * the b_attached flag in a subtree.
1332  *****************************************************************************/
1333 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1334 {
1335     int i_index;
1336
1337     for( i_index = p_this->i_children ; i_index-- ; )
1338     {
1339         SetAttachment( p_this->pp_children[i_index], b_attached );
1340     }
1341
1342     p_this->p_internals->b_attached = b_attached;
1343 }
1344
1345 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1346 {
1347     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1348          psz_parent[20];
1349
1350     psz_name[0] = '\0';
1351     if( p_this->psz_object_name )
1352     {
1353         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1354         if( psz_name[48] )
1355             psz_name[48] = '\"';
1356     }
1357
1358     psz_children[0] = '\0';
1359     switch( p_this->i_children )
1360     {
1361         case 0:
1362             break;
1363         case 1:
1364             strcpy( psz_children, ", 1 child" );
1365             break;
1366         default:
1367             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1368             break;
1369     }
1370
1371     psz_refcount[0] = '\0';
1372     if( p_this->p_internals->i_refcount > 0 )
1373         snprintf( psz_refcount, 19, ", refcount %u",
1374                   p_this->p_internals->i_refcount );
1375
1376     psz_thread[0] = '\0';
1377     if( p_this->p_internals->b_thread )
1378         snprintf( psz_thread, 29, " (thread %u)",
1379 #if defined(WIN32) || defined(UNDER_CE)
1380                   (unsigned)p_this->p_internals->thread_id.id );
1381 #else
1382                   (unsigned)p_this->p_internals->thread_id );
1383 #endif
1384
1385     psz_parent[0] = '\0';
1386     if( p_this->p_parent )
1387         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1388
1389     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1390             p_this->i_object_id, p_this->psz_object_type,
1391             psz_name, psz_thread, psz_refcount, psz_children,
1392             psz_parent );
1393 }
1394
1395 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1396 {
1397     int i;
1398     char i_back = psz_foo[i_level];
1399     psz_foo[i_level] = '\0';
1400
1401     PrintObject( p_this, psz_foo );
1402
1403     psz_foo[i_level] = i_back;
1404
1405     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1406     {
1407         msg_Warn( p_this, "structure tree is too deep" );
1408         return;
1409     }
1410
1411     for( i = 0 ; i < p_this->i_children ; i++ )
1412     {
1413         if( i_level )
1414         {
1415             psz_foo[i_level-1] = ' ';
1416
1417             if( psz_foo[i_level-2] == '`' )
1418             {
1419                 psz_foo[i_level-2] = ' ';
1420             }
1421         }
1422
1423         if( i == p_this->i_children - 1 )
1424         {
1425             psz_foo[i_level] = '`';
1426         }
1427         else
1428         {
1429             psz_foo[i_level] = '|';
1430         }
1431
1432         psz_foo[i_level+1] = '-';
1433         psz_foo[i_level+2] = '\0';
1434
1435         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1436     }
1437 }
1438
1439 static vlc_list_t * NewList( int i_count )
1440 {
1441     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1442     if( p_list == NULL )
1443     {
1444         return NULL;
1445     }
1446
1447     p_list->i_count = i_count;
1448
1449     if( i_count == 0 )
1450     {
1451         p_list->p_values = NULL;
1452         return p_list;
1453     }
1454
1455     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1456     if( p_list->p_values == NULL )
1457     {
1458         p_list->i_count = 0;
1459         return p_list;
1460     }
1461
1462     return p_list;
1463 }
1464
1465 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1466                          int i_index )
1467 {
1468     if( p_list == NULL || i_index >= p_list->i_count )
1469     {
1470         return;
1471     }
1472
1473     p_object->p_internals->i_refcount++;
1474
1475     p_list->p_values[i_index].p_object = p_object;
1476
1477     return;
1478 }
1479
1480 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1481 {
1482     if( p_list == NULL )
1483     {
1484         return;
1485     }
1486
1487     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1488                                 * sizeof( vlc_value_t ) );
1489     if( p_list->p_values == NULL )
1490     {
1491         p_list->i_count = 0;
1492         return;
1493     }
1494
1495     p_object->p_internals->i_refcount++;
1496
1497     p_list->p_values[p_list->i_count].p_object = p_object;
1498     p_list->i_count++;
1499
1500     return;
1501 }*/
1502
1503 static int CountChildren( vlc_object_t *p_this, int i_type )
1504 {
1505     vlc_object_t *p_tmp;
1506     int i, i_count = 0;
1507
1508     for( i = 0; i < p_this->i_children; i++ )
1509     {
1510         p_tmp = p_this->pp_children[i];
1511
1512         if( p_tmp->i_object_type == i_type )
1513         {
1514             i_count++;
1515         }
1516
1517         if( p_tmp->i_children )
1518         {
1519             i_count += CountChildren( p_tmp, i_type );
1520         }
1521     }
1522
1523     return i_count;
1524 }
1525
1526 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1527 {
1528     vlc_object_t *p_tmp;
1529     int i;
1530
1531     for( i = 0; i < p_this->i_children; i++ )
1532     {
1533         p_tmp = p_this->pp_children[i];
1534
1535         if( p_tmp->i_object_type == i_type )
1536         {
1537             ListReplace( p_list, p_tmp, p_list->i_count++ );
1538         }
1539
1540         if( p_tmp->i_children )
1541         {
1542             ListChildren( p_list, p_tmp, i_type );
1543         }
1544     }
1545 }