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