]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Merge VLC_OBJECT_PACKETIZER with VLC_OBJECT_DECODER
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /**
24  * \file
25  * This file contains the functions to handle the vlc_object_t type
26  *
27  * Unless otherwise stated, functions in this file are not cancellation point.
28  * All functions in this file are safe w.r.t. deferred cancellation.
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40
41 #include "../libvlc.h"
42 #include <vlc_aout.h>
43 #include "audio_output/aout_internal.h"
44
45 #include "vlc_interface.h"
46 #include "vlc_codec.h"
47
48 #include "variables.h"
49 #ifndef WIN32
50 # include <unistd.h>
51 #else
52 # include <io.h>
53 # include <fcntl.h>
54 # include <errno.h> /* ENOSYS */
55 #endif
56
57 #include <assert.h>
58
59 #if defined (HAVE_SYS_EVENTFD_H)
60 # include <sys/eventfd.h>
61 # define HAVE_EVENTFD 1
62 #endif
63
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int  DumpCommand( vlc_object_t *, char const *,
69                          vlc_value_t, vlc_value_t, void * );
70
71 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void           PrintObject   ( vlc_object_t *, const char * );
74 static void           DumpStructure ( vlc_object_t *, int, char * );
75
76 static vlc_list_t   * NewList       ( int );
77 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
78 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
79 static int            CountChildren ( vlc_object_t *, int );
80 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
81
82 static void vlc_object_destroy( vlc_object_t *p_this );
83 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
84 #ifndef NDEBUG
85 static void vlc_object_dump( vlc_object_t *p_this );
86 #endif
87
88 /*****************************************************************************
89  * Local structure lock
90  *****************************************************************************/
91 static void libvlc_lock (libvlc_int_t *p_libvlc)
92 {
93     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
94 }
95
96 static void libvlc_unlock (libvlc_int_t *p_libvlc)
97 {
98     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
99 }
100
101 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102                            int i_type, const char *psz_type )
103 {
104     vlc_object_t *p_new;
105     vlc_object_internals_t *p_priv;
106
107     /* NOTE:
108      * VLC objects are laid out as follow:
109      * - first the LibVLC-private per-object data,
110      * - then VLC_COMMON members from vlc_object_t,
111      * - finally, the type-specific data (if any).
112      *
113      * This function initializes the LibVLC and common data,
114      * and zeroes the rest.
115      */
116     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
117     if( p_priv == NULL )
118         return NULL;
119
120     assert (i_size >= sizeof (vlc_object_t));
121     p_new = (vlc_object_t *)(p_priv + 1);
122
123     p_priv->i_object_type = i_type;
124     p_new->psz_object_type = psz_type;
125     p_new->psz_object_name = NULL;
126
127     p_new->b_die = false;
128     p_new->b_error = false;
129     p_new->b_force = false;
130
131     p_new->psz_header = NULL;
132
133     if (p_this)
134         p_new->i_flags = p_this->i_flags
135             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
136
137     p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
138
139     if( !p_priv->p_vars )
140     {
141         free( p_priv );
142         return NULL;
143     }
144
145     if( p_this == NULL )
146     {
147         libvlc_int_t *self = (libvlc_int_t*)p_new;
148         p_new->p_libvlc = self;
149         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150         p_this = p_priv->next = p_priv->prev = p_new;
151     }
152     else
153         p_new->p_libvlc = p_this->p_libvlc;
154
155     vlc_spin_init( &p_priv->ref_spin );
156     p_priv->i_refcount = 1;
157     p_priv->pf_destructor = NULL;
158     p_priv->b_thread = false;
159     p_new->p_parent = NULL;
160     p_priv->pp_children = NULL;
161     p_priv->i_children = 0;
162
163     p_new->p_private = NULL;
164
165     /* Initialize mutexes and condvars */
166     vlc_mutex_init( &p_priv->var_lock );
167     vlc_cond_init( &p_priv->var_wait );
168     p_priv->pipes[0] = p_priv->pipes[1] = -1;
169
170     p_priv->next = p_this;
171     libvlc_lock (p_new->p_libvlc);
172     p_priv->prev = vlc_internals (p_this)->prev;
173     vlc_internals (p_this)->prev = p_new;
174     vlc_internals (p_priv->prev)->next = p_new;
175     libvlc_unlock (p_new->p_libvlc);
176
177     if (p_new == VLC_OBJECT(p_new->p_libvlc))
178     {   /* TODO: should be in src/libvlc.c */
179         int canc = vlc_savecancel ();
180         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181         var_AddCallback( p_new, "list", DumpCommand, NULL );
182         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183         var_AddCallback( p_new, "tree", DumpCommand, NULL );
184         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185         var_AddCallback( p_new, "vars", DumpCommand, NULL );
186         vlc_restorecancel (canc);
187     }
188
189     return p_new;
190 }
191
192
193 /**
194  * Allocates and initializes a vlc object.
195  *
196  * @param i_type known object type (all of them are negative integer values),
197  *               or object byte size (always positive).
198  *
199  * @return the new object, or NULL on error.
200  */
201 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
202 {
203     const char   * psz_type;
204     size_t         i_size;
205
206     switch( i_type )
207     {
208         case VLC_OBJECT_INTF:
209             i_size = sizeof(intf_thread_t);
210             psz_type = "interface";
211             break;
212         case VLC_OBJECT_DECODER:
213             i_size = sizeof(decoder_t);
214             psz_type = "decoder";
215             break;
216         case VLC_OBJECT_AOUT:
217             i_size = sizeof(aout_instance_t);
218             psz_type = "audio output";
219             break;
220         default:
221             assert( i_type > 0 ); /* unknown type?! */
222             i_size = i_type;
223             i_type = VLC_OBJECT_GENERIC;
224             psz_type = "generic";
225             break;
226     }
227
228     return vlc_custom_create( p_this, i_size, i_type, psz_type );
229 }
230
231
232 /**
233  ****************************************************************************
234  * Set the destructor of a vlc object
235  *
236  * This function sets the destructor of the vlc object. It will be called
237  * when the object is destroyed when the its refcount reaches 0.
238  * (It is called by the internal function vlc_object_destroy())
239  *****************************************************************************/
240 void __vlc_object_set_destructor( vlc_object_t *p_this,
241                                   vlc_destructor_t pf_destructor )
242 {
243     vlc_object_internals_t *p_priv = vlc_internals(p_this );
244
245     vlc_spin_lock( &p_priv->ref_spin );
246     p_priv->pf_destructor = pf_destructor;
247     vlc_spin_unlock( &p_priv->ref_spin );
248 }
249
250 /**
251  ****************************************************************************
252  * Destroy a vlc object (Internal)
253  *
254  * This function destroys an object that has been previously allocated with
255  * vlc_object_create. The object's refcount must be zero and it must not be
256  * attached to other objects in any way.
257  *
258  * This function must be called with cancellation disabled (currently).
259  *****************************************************************************/
260 static void vlc_object_destroy( vlc_object_t *p_this )
261 {
262     vlc_object_internals_t *p_priv = vlc_internals( p_this );
263
264     /* Objects are always detached beforehand */
265     assert( !p_this->p_parent );
266
267     /* Send a kill to the object's thread if applicable */
268     vlc_object_kill( p_this );
269
270     /* Call the custom "subclass" destructor */
271     if( p_priv->pf_destructor )
272         p_priv->pf_destructor( p_this );
273
274     /* Any thread must have been cleaned up at this point. */
275     assert( !p_priv->b_thread );
276
277     /* Destroy the associated variables, starting from the end so that
278      * no memmove calls have to be done. */
279     while( p_priv->i_vars )
280     {
281         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
282     }
283
284     free( p_priv->p_vars );
285     vlc_cond_destroy( &p_priv->var_wait );
286     vlc_mutex_destroy( &p_priv->var_lock );
287
288     free( p_this->psz_header );
289
290     FREENULL( p_this->psz_object_name );
291
292     vlc_spin_destroy( &p_priv->ref_spin );
293     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
294         close( p_priv->pipes[1] );
295     if( p_priv->pipes[0] != -1 )
296         close( p_priv->pipes[0] );
297     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
298         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
299
300     free( p_priv );
301 }
302
303
304 #ifdef WIN32
305 # include <winsock2.h>
306 # include <ws2tcpip.h>
307
308 /**
309  * select()-able pipes emulated using Winsock
310  */
311 static int pipe (int fd[2])
312 {
313     SOCKADDR_IN addr;
314     int addrlen = sizeof (addr);
315
316     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
317            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
318     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
319         goto error;
320
321     memset (&addr, 0, sizeof (addr));
322     addr.sin_family = AF_INET;
323     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
324     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
325      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
326      || listen (l, 1)
327      || connect (c, (PSOCKADDR)&addr, addrlen))
328         goto error;
329
330     a = accept (l, NULL, NULL);
331     if (a == INVALID_SOCKET)
332         goto error;
333
334     closesocket (l);
335     //shutdown (a, 0);
336     //shutdown (c, 1);
337     fd[0] = c;
338     fd[1] = a;
339     return 0;
340
341 error:
342     if (l != INVALID_SOCKET)
343         closesocket (l);
344     if (c != INVALID_SOCKET)
345         closesocket (c);
346     return -1;
347 }
348
349 #undef  read
350 #define read( a, b, c )  recv (a, b, c, 0)
351 #undef  write
352 #define write( a, b, c ) send (a, b, c, 0)
353 #undef  close
354 #define close( a )       closesocket (a)
355 #endif /* WIN32 */
356
357 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
358
359 /**
360  * Returns the readable end of a pipe that becomes readable once termination
361  * of the object is requested (vlc_object_kill()).
362  * This can be used to wake-up out of a select() or poll() event loop, such
363  * typically when doing network I/O.
364  *
365  * Note that the pipe will remain the same for the lifetime of the object.
366  * DO NOT read the pipe nor close it yourself. Ever.
367  *
368  * @param obj object that would be "killed"
369  * @return a readable pipe descriptor, or -1 on error.
370  */
371 int vlc_object_waitpipe( vlc_object_t *obj )
372 {
373     vlc_object_internals_t *internals = vlc_internals( obj );
374
375     vlc_mutex_lock (&pipe_lock);
376     if (internals->pipes[0] == -1)
377     {
378         /* This can only ever happen if someone killed us without locking: */
379         assert (internals->pipes[1] == -1);
380
381 #ifdef HAVE_EVENTFD
382         internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
383         if (internals->pipes[0] == -1)
384 #endif
385         {
386             if (pipe (internals->pipes))
387                 internals->pipes[0] = internals->pipes[1] = -1;
388         }
389
390         if (internals->pipes[0] != -1 && obj->b_die)
391         {   /* Race condition: vlc_object_kill() already invoked! */
392             msg_Dbg (obj, "waitpipe: object already dying");
393             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
394         }
395     }
396     vlc_mutex_unlock (&pipe_lock);
397     return internals->pipes[0];
398 }
399
400
401 /**
402  * Requests termination of an object, cancels the object thread, and make the
403  * object wait pipe (if it exists) readable. Not a cancellation point.
404  */
405 void __vlc_object_kill( vlc_object_t *p_this )
406 {
407     vlc_object_internals_t *priv = vlc_internals( p_this );
408     int fd = -1;
409
410     vlc_thread_cancel( p_this );
411     vlc_mutex_lock( &pipe_lock );
412     if( !p_this->b_die )
413     {
414         fd = priv->pipes[1];
415         p_this->b_die = true;
416     }
417
418     /* This also serves as a memory barrier toward vlc_object_alive(): */
419     vlc_mutex_unlock( &pipe_lock );
420
421     if (fd != -1)
422     {
423         int canc = vlc_savecancel ();
424
425         /* write _after_ setting b_die, so vlc_object_alive() returns false */
426         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
427         msg_Dbg (p_this, "waitpipe: object killed");
428         vlc_restorecancel (canc);
429     }
430 }
431
432
433 /*****************************************************************************
434  * find a typed object and increment its refcount
435  *****************************************************************************
436  * This function recursively looks for a given object type. i_mode can be one
437  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
438  *****************************************************************************/
439 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
440 {
441     vlc_object_t *p_found;
442
443     /* If we are of the requested type ourselves, don't look further */
444     if( !(i_mode & FIND_STRICT)
445      && vlc_internals (p_this)->i_object_type == i_type )
446     {
447         vlc_object_hold( p_this );
448         return p_this;
449     }
450
451     /* Otherwise, recursively look for the object */
452     if ((i_mode & 0x000f) == FIND_ANYWHERE)
453         return vlc_object_find (p_this->p_libvlc, i_type,
454                                 (i_mode & ~0x000f)|FIND_CHILD);
455
456     libvlc_lock (p_this->p_libvlc);
457     p_found = FindObject( p_this, i_type, i_mode );
458     libvlc_unlock (p_this->p_libvlc);
459     return p_found;
460 }
461
462 #undef vlc_object_find_name
463 /**
464  * Finds a named object and increment its reference count.
465  * Beware that objects found in this manner can be "owned" by another thread,
466  * be of _any_ type, and be attached to any module (if any). With such an
467  * object reference, you can set or get object variables, emit log messages,
468  * and read write-once object parameters (psz_object_type, etc).
469  * You CANNOT cast the object to a more specific object type, and you
470  * definitely cannot invoke object type-specific callbacks with this.
471  *
472  * @param p_this object to search from
473  * @param psz_name name of the object to search for
474  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
475  *
476  * @return a matching object (must be released by the caller),
477  * or NULL on error.
478  */
479 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
480                                     const char *psz_name, int i_mode )
481 {
482     vlc_object_t *p_found;
483
484     /* If have the requested name ourselves, don't look further */
485     if( !(i_mode & FIND_STRICT)
486         && p_this->psz_object_name
487         && !strcmp( p_this->psz_object_name, psz_name ) )
488     {
489         vlc_object_hold( p_this );
490         return p_this;
491     }
492
493     libvlc_lock (p_this->p_libvlc);
494
495     /* Otherwise, recursively look for the object */
496     if( (i_mode & 0x000f) == FIND_ANYWHERE )
497     {
498         vlc_object_t *p_root = p_this;
499
500         /* Find the root */
501         while( p_root->p_parent != NULL &&
502                p_root != VLC_OBJECT( p_this->p_libvlc ) )
503         {
504             p_root = p_root->p_parent;
505         }
506
507         p_found = FindObjectName( p_root, psz_name,
508                                  (i_mode & ~0x000f)|FIND_CHILD );
509         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
510         {
511             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
512                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
513         }
514     }
515     else
516     {
517         p_found = FindObjectName( p_this, psz_name, i_mode );
518     }
519
520     libvlc_unlock (p_this->p_libvlc);
521     return p_found;
522 }
523
524 /**
525  * Increment an object reference counter.
526  */
527 void * __vlc_object_hold( vlc_object_t *p_this )
528 {
529     vlc_object_internals_t *internals = vlc_internals( p_this );
530
531     vlc_spin_lock( &internals->ref_spin );
532     /* Avoid obvious freed object uses */
533     assert( internals->i_refcount > 0 );
534     /* Increment the counter */
535     internals->i_refcount++;
536     vlc_spin_unlock( &internals->ref_spin );
537     return p_this;
538 }
539
540 /*****************************************************************************
541  * Decrement an object refcount
542  * And destroy the object if its refcount reach zero.
543  *****************************************************************************/
544 void __vlc_object_release( vlc_object_t *p_this )
545 {
546     vlc_object_internals_t *internals = vlc_internals( p_this );
547     vlc_object_t *parent = NULL;
548     bool b_should_destroy;
549
550     vlc_spin_lock( &internals->ref_spin );
551     assert( internals->i_refcount > 0 );
552
553     if( internals->i_refcount > 1 )
554     {
555         /* Fast path */
556         /* There are still other references to the object */
557         internals->i_refcount--;
558         vlc_spin_unlock( &internals->ref_spin );
559         return;
560     }
561     vlc_spin_unlock( &internals->ref_spin );
562
563     /* Slow path */
564     /* Remember that we cannot hold the spin while waiting on the mutex */
565     libvlc_lock (p_this->p_libvlc);
566     /* Take the spin again. Note that another thread may have held the
567      * object in the (very short) mean time. */
568     vlc_spin_lock( &internals->ref_spin );
569     b_should_destroy = --internals->i_refcount == 0;
570     vlc_spin_unlock( &internals->ref_spin );
571
572     if( b_should_destroy )
573     {
574         parent = p_this->p_parent;
575
576 #ifndef NDEBUG
577         if( VLC_OBJECT(p_this->p_libvlc) == p_this )
578         {
579             /* Test for leaks */
580             vlc_object_t *leaked = internals->next;
581             while( leaked != p_this )
582             {
583                 /* We are leaking this object */
584                 fprintf( stderr,
585                          "ERROR: leaking object (%p, type:%s, name:%s)\n",
586                          leaked, leaked->psz_object_type,
587                          leaked->psz_object_name );
588                 /* Dump object to ease debugging */
589                 vlc_object_dump( leaked );
590                 fflush(stderr);
591                 leaked = vlc_internals (leaked)->next;
592             }
593
594             if( internals->next != p_this )
595                 /* Dump libvlc object to ease debugging */
596                 vlc_object_dump( p_this );
597         }
598 #endif
599         /* Remove the object from object list
600          * so that it cannot be encountered by vlc_object_get() */
601         vlc_internals (internals->next)->prev = internals->prev;
602         vlc_internals (internals->prev)->next = internals->next;
603
604         if (parent)
605             /* Detach from parent to protect against FIND_CHILDREN */
606             vlc_object_detach_unlocked (p_this);
607
608         /* We have no children */
609         assert (internals->i_children == 0);
610     }
611     libvlc_unlock (p_this->p_libvlc);
612
613     if( b_should_destroy )
614     {
615         int canc;
616
617         canc = vlc_savecancel ();
618         vlc_object_destroy( p_this );
619         vlc_restorecancel (canc);
620         if (parent)
621             vlc_object_release (parent);
622     }
623 }
624
625 /**
626  ****************************************************************************
627  * attach object to a parent object
628  *****************************************************************************
629  * This function sets p_this as a child of p_parent, and p_parent as a parent
630  * of p_this. This link can be undone using vlc_object_detach.
631  *****************************************************************************/
632 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
633 {
634     if( !p_this ) return;
635
636     vlc_object_hold (p_parent);
637     libvlc_lock (p_this->p_libvlc);
638
639     /* Attach the parent to its child */
640     assert (!p_this->p_parent);
641     p_this->p_parent = p_parent;
642
643     /* Attach the child to its parent */
644     vlc_object_internals_t *priv = vlc_internals( p_parent );
645     INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
646                  p_this );
647     libvlc_unlock (p_this->p_libvlc);
648 }
649
650
651 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
652 {
653     if (p_this->p_parent == NULL)
654         return;
655
656     vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
657
658     int i_index, i;
659
660     /* Remove p_this's parent */
661     p_this->p_parent = NULL;
662
663     /* Remove all of p_parent's children which are p_this */
664     for( i_index = priv->i_children ; i_index-- ; )
665     {
666         if( priv->pp_children[i_index] == p_this )
667         {
668             priv->i_children--;
669             for( i = i_index ; i < priv->i_children ; i++ )
670                 priv->pp_children[i] = priv->pp_children[i+1];
671         }
672     }
673
674     if( priv->i_children )
675     {
676         vlc_object_t **pp_children = (vlc_object_t **)
677             realloc( priv->pp_children,
678                      priv->i_children * sizeof(vlc_object_t *) );
679         if( pp_children )
680             priv->pp_children = pp_children;
681     }
682     else
683     {
684         /* Special case - don't realloc() to zero to avoid leaking */
685         free( priv->pp_children );
686         priv->pp_children = NULL;
687     }
688 }
689
690
691 /**
692  ****************************************************************************
693  * detach object from its parent
694  *****************************************************************************
695  * This function removes all links between an object and its parent.
696  *****************************************************************************/
697 void __vlc_object_detach( vlc_object_t *p_this )
698 {
699     vlc_object_t *p_parent;
700     if( !p_this ) return;
701
702     libvlc_lock (p_this->p_libvlc);
703     p_parent = p_this->p_parent;
704     if (p_parent)
705         vlc_object_detach_unlocked( p_this );
706     libvlc_unlock (p_this->p_libvlc);
707
708     if (p_parent)
709         vlc_object_release (p_parent);
710 }
711
712
713 /**
714  ****************************************************************************
715  * find a list typed objects and increment their refcount
716  *****************************************************************************
717  * This function recursively looks for a given object type. i_mode can be one
718  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
719  *****************************************************************************/
720 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
721 {
722     vlc_list_t *p_list;
723     int i_count = 0;
724
725     /* Look for the objects */
726     switch( i_mode & 0x000f )
727     {
728     case FIND_ANYWHERE:
729         return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
730
731     case FIND_CHILD:
732         libvlc_lock (p_this->p_libvlc);
733         i_count = CountChildren( p_this, i_type );
734         p_list = NewList( i_count );
735
736         /* Check allocation was successful */
737         if( p_list->i_count != i_count )
738         {
739             libvlc_unlock (p_this->p_libvlc);
740             p_list->i_count = 0;
741             break;
742         }
743
744         p_list->i_count = 0;
745         ListChildren( p_list, p_this, i_type );
746         libvlc_unlock (p_this->p_libvlc);
747         break;
748
749     default:
750         msg_Err( p_this, "unimplemented!" );
751         p_list = NewList( 0 );
752         break;
753     }
754
755     return p_list;
756 }
757
758 /**
759  * Gets the list of children of an objects, and increment their reference
760  * count.
761  * @return a list (possibly empty) or NULL in case of error.
762  */
763 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
764 {
765     vlc_list_t *l;
766     vlc_object_internals_t *priv = vlc_internals( obj );
767
768     libvlc_lock (obj->p_libvlc);
769     l = NewList( priv->i_children );
770     for (int i = 0; i < l->i_count; i++)
771     {
772         vlc_object_hold( priv->pp_children[i] );
773         l->p_values[i].p_object = priv->pp_children[i];
774     }
775     libvlc_unlock (obj->p_libvlc);
776     return l;
777 }
778
779 /*****************************************************************************
780  * DumpCommand: print the current vlc structure
781  *****************************************************************************
782  * This function prints either an ASCII tree showing the connections between
783  * vlc objects, and additional information such as their refcount, thread ID,
784  * etc. (command "tree"), or the same data as a simple list (command "list").
785  *****************************************************************************/
786 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
787                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
788 {
789     libvlc_int_t *p_libvlc = p_this->p_libvlc;
790
791     (void)oldval; (void)p_data;
792     if( *psz_cmd == 'l' )
793     {
794         vlc_object_t *cur = VLC_OBJECT (p_libvlc);
795
796         libvlc_lock (p_this->p_libvlc);
797         do
798         {
799             PrintObject (cur, "");
800             cur = vlc_internals (cur)->next;
801         }
802         while (cur != VLC_OBJECT(p_libvlc));
803         libvlc_unlock (p_this->p_libvlc);
804     }
805     else
806     {
807         vlc_object_t *p_object = NULL;
808
809         if( *newval.psz_string )
810         {
811             /* try using the object's name to find it */
812             p_object = vlc_object_find_name( p_this, newval.psz_string,
813                                              FIND_ANYWHERE );
814             if( !p_object )
815             {
816                 return VLC_ENOOBJ;
817             }
818         }
819
820         libvlc_lock (p_this->p_libvlc);
821         if( *psz_cmd == 't' )
822         {
823             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
824
825             if( !p_object )
826                 p_object = VLC_OBJECT(p_this->p_libvlc);
827
828             psz_foo[0] = '|';
829             DumpStructure( p_object, 0, psz_foo );
830         }
831         else if( *psz_cmd == 'v' )
832         {
833             int i;
834
835             if( !p_object )
836                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
837
838             PrintObject( p_object, "" );
839
840             if( !vlc_internals( p_object )->i_vars )
841                 printf( " `-o No variables\n" );
842             for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
843             {
844                 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
845
846                 const char *psz_type = "unknown";
847                 switch( p_var->i_type & VLC_VAR_TYPE )
848                 {
849 #define MYCASE( type, nice )                \
850                     case VLC_VAR_ ## type:  \
851                         psz_type = nice;    \
852                         break;
853                     MYCASE( VOID, "void" );
854                     MYCASE( BOOL, "bool" );
855                     MYCASE( INTEGER, "integer" );
856                     MYCASE( HOTKEY, "hotkey" );
857                     MYCASE( STRING, "string" );
858                     MYCASE( MODULE, "module" );
859                     MYCASE( FILE, "file" );
860                     MYCASE( DIRECTORY, "directory" );
861                     MYCASE( VARIABLE, "variable" );
862                     MYCASE( FLOAT, "float" );
863                     MYCASE( TIME, "time" );
864                     MYCASE( ADDRESS, "address" );
865                     MYCASE( MUTEX, "mutex" );
866                     MYCASE( LIST, "list" );
867 #undef MYCASE
868                 }
869                 printf( " %c-o \"%s\" (%s",
870                         i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
871                         p_var->psz_name, psz_type );
872                 if( p_var->psz_text )
873                     printf( ", %s", p_var->psz_text );
874                 printf( ")" );
875                 if( p_var->i_type & VLC_VAR_HASCHOICE )
876                     printf( ", has choices" );
877                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
878                     printf( ", command" );
879                 if( p_var->i_entries )
880                     printf( ", %d callbacks", p_var->i_entries );
881                 switch( p_var->i_type & VLC_VAR_CLASS )
882                 {
883                     case VLC_VAR_VOID:
884                     case VLC_VAR_MUTEX:
885                         break;
886                     case VLC_VAR_BOOL:
887                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
888                         break;
889                     case VLC_VAR_INTEGER:
890                         printf( ": %d", p_var->val.i_int );
891                         break;
892                     case VLC_VAR_STRING:
893                         printf( ": \"%s\"", p_var->val.psz_string );
894                         break;
895                     case VLC_VAR_FLOAT:
896                         printf( ": %f", p_var->val.f_float );
897                         break;
898                     case VLC_VAR_TIME:
899                         printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
900                         break;
901                     case VLC_VAR_ADDRESS:
902                         printf( ": %p", p_var->val.p_address );
903                         break;
904                     case VLC_VAR_LIST:
905                         printf( ": TODO" );
906                         break;
907                 }
908                 printf( "\n" );
909             }
910         }
911         libvlc_unlock (p_this->p_libvlc);
912
913         if( *newval.psz_string )
914         {
915             vlc_object_release( p_object );
916         }
917     }
918
919     return VLC_SUCCESS;
920 }
921
922 /*****************************************************************************
923  * vlc_list_release: free a list previously allocated by vlc_list_find
924  *****************************************************************************
925  * This function decreases the refcount of all objects in the list and
926  * frees the list.
927  *****************************************************************************/
928 void vlc_list_release( vlc_list_t *p_list )
929 {
930     int i_index;
931
932     for( i_index = 0; i_index < p_list->i_count; i_index++ )
933     {
934         vlc_object_release( p_list->p_values[i_index].p_object );
935     }
936
937     free( p_list->p_values );
938     free( p_list );
939 }
940
941 /*****************************************************************************
942  * dump an object. (Debug function)
943  *****************************************************************************/
944 #ifndef NDEBUG
945 static void vlc_object_dump( vlc_object_t *p_this )
946 {
947     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
948     psz_foo[0] = '|';
949
950     DumpStructure( p_this, 0, psz_foo );
951 }
952 #endif
953
954 /* Following functions are local */
955
956 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
957 {
958     int i;
959     vlc_object_t *p_tmp;
960
961     switch( i_mode & 0x000f )
962     {
963     case FIND_PARENT:
964         p_tmp = p_this->p_parent;
965         if( p_tmp )
966         {
967             if( vlc_internals( p_tmp )->i_object_type == i_type )
968             {
969                 vlc_object_hold( p_tmp );
970                 return p_tmp;
971             }
972             else
973             {
974                 return FindObject( p_tmp, i_type, i_mode );
975             }
976         }
977         break;
978
979     case FIND_CHILD:
980         for( i = vlc_internals( p_this )->i_children; i--; )
981         {
982             p_tmp = vlc_internals( p_this )->pp_children[i];
983             if( vlc_internals( p_tmp )->i_object_type == i_type )
984             {
985                 vlc_object_hold( p_tmp );
986                 return p_tmp;
987             }
988             else if( vlc_internals( p_tmp )->i_children )
989             {
990                 p_tmp = FindObject( p_tmp, i_type, i_mode );
991                 if( p_tmp )
992                 {
993                     return p_tmp;
994                 }
995             }
996         }
997         break;
998
999     case FIND_ANYWHERE:
1000         /* Handled in vlc_object_find */
1001         break;
1002     }
1003
1004     return NULL;
1005 }
1006
1007 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1008                                       const char *psz_name,
1009                                       int i_mode )
1010 {
1011     int i;
1012     vlc_object_t *p_tmp;
1013
1014     switch( i_mode & 0x000f )
1015     {
1016     case FIND_PARENT:
1017         p_tmp = p_this->p_parent;
1018         if( p_tmp )
1019         {
1020             if( p_tmp->psz_object_name
1021                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1022             {
1023                 vlc_object_hold( p_tmp );
1024                 return p_tmp;
1025             }
1026             else
1027             {
1028                 return FindObjectName( p_tmp, psz_name, i_mode );
1029             }
1030         }
1031         break;
1032
1033     case FIND_CHILD:
1034         for( i = vlc_internals( p_this )->i_children; i--; )
1035         {
1036             p_tmp = vlc_internals( p_this )->pp_children[i];
1037             if( p_tmp->psz_object_name
1038                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1039             {
1040                 vlc_object_hold( p_tmp );
1041                 return p_tmp;
1042             }
1043             else if( vlc_internals( p_tmp )->i_children )
1044             {
1045                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1046                 if( p_tmp )
1047                 {
1048                     return p_tmp;
1049                 }
1050             }
1051         }
1052         break;
1053
1054     case FIND_ANYWHERE:
1055         /* Handled in vlc_object_find */
1056         break;
1057     }
1058
1059     return NULL;
1060 }
1061
1062
1063 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1064 {
1065     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1066          psz_parent[20];
1067
1068     int canc = vlc_savecancel ();
1069     memset( &psz_name, 0, sizeof(psz_name) );
1070     if( p_this->psz_object_name )
1071     {
1072         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1073         if( psz_name[48] )
1074             psz_name[48] = '\"';
1075     }
1076
1077     psz_children[0] = '\0';
1078     switch( vlc_internals( p_this )->i_children )
1079     {
1080         case 0:
1081             break;
1082         case 1:
1083             strcpy( psz_children, ", 1 child" );
1084             break;
1085         default:
1086             snprintf( psz_children, 19, ", %i children",
1087                       vlc_internals( p_this )->i_children );
1088             break;
1089     }
1090
1091     psz_refcount[0] = '\0';
1092     if( vlc_internals( p_this )->i_refcount > 0 )
1093         snprintf( psz_refcount, 19, ", refcount %u",
1094                   vlc_internals( p_this )->i_refcount );
1095
1096     psz_thread[0] = '\0';
1097     if( vlc_internals( p_this )->b_thread )
1098         snprintf( psz_thread, 29, " (thread %lu)",
1099                   (unsigned long)vlc_internals( p_this )->thread_id );
1100
1101     psz_parent[0] = '\0';
1102     if( p_this->p_parent )
1103         snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1104
1105     printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1106             p_this, p_this->psz_object_type,
1107             psz_name, psz_thread, psz_refcount, psz_children,
1108             psz_parent );
1109     vlc_restorecancel (canc);
1110 }
1111
1112 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1113 {
1114     int i;
1115     char i_back = psz_foo[i_level];
1116     psz_foo[i_level] = '\0';
1117
1118     PrintObject( p_this, psz_foo );
1119
1120     psz_foo[i_level] = i_back;
1121
1122     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1123     {
1124         msg_Warn( p_this, "structure tree is too deep" );
1125         return;
1126     }
1127
1128     for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1129     {
1130         if( i_level )
1131         {
1132             psz_foo[i_level-1] = ' ';
1133
1134             if( psz_foo[i_level-2] == '`' )
1135             {
1136                 psz_foo[i_level-2] = ' ';
1137             }
1138         }
1139
1140         if( i == vlc_internals( p_this )->i_children - 1 )
1141         {
1142             psz_foo[i_level] = '`';
1143         }
1144         else
1145         {
1146             psz_foo[i_level] = '|';
1147         }
1148
1149         psz_foo[i_level+1] = '-';
1150         psz_foo[i_level+2] = '\0';
1151
1152         DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1153                        psz_foo );
1154     }
1155 }
1156
1157 static vlc_list_t * NewList( int i_count )
1158 {
1159     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1160     if( p_list == NULL )
1161     {
1162         return NULL;
1163     }
1164
1165     p_list->i_count = i_count;
1166
1167     if( i_count == 0 )
1168     {
1169         p_list->p_values = NULL;
1170         return p_list;
1171     }
1172
1173     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1174     if( p_list->p_values == NULL )
1175     {
1176         p_list->i_count = 0;
1177         return p_list;
1178     }
1179
1180     return p_list;
1181 }
1182
1183 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1184                          int i_index )
1185 {
1186     if( p_list == NULL || i_index >= p_list->i_count )
1187     {
1188         return;
1189     }
1190
1191     vlc_object_hold( p_object );
1192
1193     p_list->p_values[i_index].p_object = p_object;
1194
1195     return;
1196 }
1197
1198 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1199 {
1200     if( p_list == NULL )
1201     {
1202         return;
1203     }
1204
1205     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1206                                 * sizeof( vlc_value_t ) );
1207     if( p_list->p_values == NULL )
1208     {
1209         p_list->i_count = 0;
1210         return;
1211     }
1212
1213     vlc_object_hold( p_object );
1214
1215     p_list->p_values[p_list->i_count].p_object = p_object;
1216     p_list->i_count++;
1217
1218     return;
1219 }*/
1220
1221 static int CountChildren( vlc_object_t *p_this, int i_type )
1222 {
1223     vlc_object_t *p_tmp;
1224     int i, i_count = 0;
1225
1226     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1227     {
1228         p_tmp = vlc_internals( p_this )->pp_children[i];
1229
1230         if( vlc_internals( p_tmp )->i_object_type == i_type )
1231         {
1232             i_count++;
1233         }
1234         i_count += CountChildren( p_tmp, i_type );
1235     }
1236
1237     return i_count;
1238 }
1239
1240 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1241 {
1242     vlc_object_t *p_tmp;
1243     int i;
1244
1245     for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1246     {
1247         p_tmp = vlc_internals( p_this )->pp_children[i];
1248
1249         if( vlc_internals( p_tmp )->i_object_type == i_type )
1250             ListReplace( p_list, p_tmp, p_list->i_count++ );
1251
1252         ListChildren( p_list, p_tmp, i_type );
1253     }
1254 }