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