1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 * This file contains the functions to handle the vlc_object_t type
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.
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
41 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
51 #include "stream_output/stream_output.h"
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
57 #include "variables.h"
63 # include <errno.h> /* ENOSYS */
67 /*****************************************************************************
69 *****************************************************************************/
70 static int DumpCommand( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
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 * );
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 );
84 static void vlc_object_destroy( vlc_object_t *p_this );
85 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
86 static void vlc_object_dump( vlc_object_t *p_this );
88 /*****************************************************************************
89 * Local structure lock
90 *****************************************************************************/
91 static vlc_mutex_t structure_lock;
92 static unsigned object_counter = 0;
94 static void close_nocancel (int fd)
96 int canc = vlc_savecancel ();
98 vlc_restorecancel (canc);
101 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102 int i_type, const char *psz_type )
105 vlc_object_internals_t *p_priv;
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).
113 * This function initializes the LibVLC and common data,
114 * and zeroes the rest.
116 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
120 assert (i_size >= sizeof (vlc_object_t));
121 p_new = (vlc_object_t *)(p_priv + 1);
123 p_new->i_object_type = i_type;
124 p_new->psz_object_type = psz_type;
125 p_new->psz_object_name = NULL;
127 p_new->b_die = false;
128 p_new->b_error = false;
129 p_new->b_dead = false;
130 p_new->b_force = false;
132 p_new->psz_header = NULL;
135 p_new->i_flags = p_this->i_flags
136 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
138 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
140 if( !p_priv->p_vars )
148 if( i_type == VLC_OBJECT_LIBVLC )
149 p_new->p_libvlc = (libvlc_int_t*)p_new;
152 p_new->p_libvlc = NULL;
153 vlc_mutex_init( &structure_lock );
156 p_this = p_priv->next = p_priv->prev = p_new;
159 p_new->p_libvlc = p_this->p_libvlc;
161 vlc_spin_init( &p_priv->ref_spin );
162 p_priv->i_refcount = 1;
163 p_priv->pf_destructor = NULL;
164 p_priv->b_thread = false;
165 p_new->p_parent = NULL;
166 p_priv->pp_children = NULL;
167 p_priv->i_children = 0;
169 p_new->p_private = NULL;
171 /* Initialize mutexes and condvars */
172 vlc_mutex_init( &p_priv->lock );
173 vlc_cond_init( p_new, &p_priv->wait );
174 vlc_mutex_init( &p_priv->var_lock );
175 vlc_spin_init( &p_priv->spin );
176 p_priv->pipes[0] = p_priv->pipes[1] = -1;
178 p_priv->next = p_this;
179 vlc_mutex_lock( &structure_lock );
180 p_priv->prev = vlc_internals (p_this)->prev;
181 vlc_internals (p_this)->prev = p_new;
182 vlc_internals (p_priv->prev)->next = p_new;
183 p_new->i_object_id = object_counter++; /* fetch THEN increment */
184 vlc_mutex_unlock( &structure_lock );
186 if( i_type == VLC_OBJECT_LIBVLC )
188 int canc = vlc_savecancel ();
189 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_new, "list", DumpCommand, NULL );
191 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_new, "tree", DumpCommand, NULL );
193 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "vars", DumpCommand, NULL );
195 vlc_restorecancel (canc);
203 * Allocates and initializes a vlc object.
205 * @param i_type known object type (all of them are negative integer values),
206 * or object byte size (always positive).
208 * @return the new object, or NULL on error.
210 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
212 const char * psz_type;
217 case VLC_OBJECT_INTF:
218 i_size = sizeof(intf_thread_t);
219 psz_type = "interface";
221 case VLC_OBJECT_DECODER:
222 i_size = sizeof(decoder_t);
223 psz_type = "decoder";
225 case VLC_OBJECT_PACKETIZER:
226 i_size = sizeof(decoder_t);
227 psz_type = "packetizer";
229 case VLC_OBJECT_ENCODER:
230 i_size = sizeof(encoder_t);
231 psz_type = "encoder";
233 case VLC_OBJECT_AOUT:
234 i_size = sizeof(aout_instance_t);
235 psz_type = "audio output";
237 case VLC_OBJECT_OPENGL:
238 i_size = sizeof( vout_thread_t );
241 case VLC_OBJECT_ANNOUNCE:
242 i_size = sizeof( announce_handler_t );
243 psz_type = "announce";
246 assert( i_type > 0 ); /* unknown type?! */
248 i_type = VLC_OBJECT_GENERIC;
249 psz_type = "generic";
253 return vlc_custom_create( p_this, i_size, i_type, psz_type );
258 ****************************************************************************
259 * Set the destructor of a vlc object
261 * This function sets the destructor of the vlc object. It will be called
262 * when the object is destroyed when the its refcount reaches 0.
263 * (It is called by the internal function vlc_object_destroy())
264 *****************************************************************************/
265 void __vlc_object_set_destructor( vlc_object_t *p_this,
266 vlc_destructor_t pf_destructor )
268 vlc_object_internals_t *p_priv = vlc_internals(p_this );
269 p_priv->pf_destructor = pf_destructor;
273 ****************************************************************************
274 * Destroy a vlc object (Internal)
276 * This function destroys an object that has been previously allocated with
277 * vlc_object_create. The object's refcount must be zero and it must not be
278 * attached to other objects in any way.
280 * This function must be called with cancellation disabled (currently).
281 *****************************************************************************/
282 static void vlc_object_destroy( vlc_object_t *p_this )
284 vlc_object_internals_t *p_priv = vlc_internals( p_this );
286 /* Objects are always detached beforehand */
287 assert( !p_this->p_parent );
289 /* Send a kill to the object's thread if applicable */
290 vlc_object_kill( p_this );
292 /* Call the custom "subclass" destructor */
293 if( p_priv->pf_destructor )
294 p_priv->pf_destructor( p_this );
296 /* Any thread must have been cleaned up at this point. */
297 assert( !p_priv->b_thread );
299 /* Destroy the associated variables, starting from the end so that
300 * no memmove calls have to be done. */
301 while( p_priv->i_vars )
303 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
306 free( p_priv->p_vars );
307 vlc_mutex_destroy( &p_priv->var_lock );
309 free( p_this->psz_header );
311 if( p_this->p_libvlc == NULL )
312 /* We are the global object ... no need to lock. */
313 vlc_mutex_destroy( &structure_lock );
315 FREENULL( p_this->psz_object_name );
317 #if defined(WIN32) || defined(UNDER_CE)
318 /* if object has an associated thread, close it now */
319 if( p_priv->thread_id )
320 CloseHandle(p_priv->thread_id);
323 vlc_spin_destroy( &p_priv->ref_spin );
324 vlc_mutex_destroy( &p_priv->lock );
325 vlc_cond_destroy( &p_priv->wait );
326 vlc_spin_destroy( &p_priv->spin );
327 if( p_priv->pipes[1] != -1 )
328 close( p_priv->pipes[1] );
329 if( p_priv->pipes[0] != -1 )
330 close( p_priv->pipes[0] );
336 /** Inter-object signaling */
338 void __vlc_object_lock( vlc_object_t *obj )
340 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
343 void __vlc_object_unlock( vlc_object_t *obj )
345 vlc_assert_locked( &(vlc_internals(obj)->lock) );
346 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
350 # include <winsock2.h>
351 # include <ws2tcpip.h>
354 * select()-able pipes emulated using Winsock
356 static int pipe (int fd[2])
359 int addrlen = sizeof (addr);
361 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
362 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
363 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
366 memset (&addr, 0, sizeof (addr));
367 addr.sin_family = AF_INET;
368 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
369 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
370 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
372 || connect (c, (PSOCKADDR)&addr, addrlen))
375 a = accept (l, NULL, NULL);
376 if (a == INVALID_SOCKET)
387 if (l != INVALID_SOCKET)
389 if (c != INVALID_SOCKET)
395 #define read( a, b, c ) recv (a, b, c, 0)
397 #define write( a, b, c ) send (a, b, c, 0)
399 #define close( a ) closesocket (a)
403 * Returns the readable end of a pipe that becomes readable once termination
404 * of the object is requested (vlc_object_kill()).
405 * This can be used to wake-up out of a select() or poll() event loop, such
406 * typically when doing network I/O.
408 * Note that the pipe will remain the same for the lifetime of the object.
409 * DO NOT read the pipe nor close it yourself. Ever.
411 * @param obj object that would be "killed"
412 * @return a readable pipe descriptor, or -1 on error.
414 int __vlc_object_waitpipe( vlc_object_t *obj )
416 int pfd[2] = { -1, -1 };
417 vlc_object_internals_t *internals = vlc_internals( obj );
420 vlc_spin_lock (&internals->spin);
421 if (internals->pipes[0] == -1)
423 /* This can only ever happen if someone killed us without locking: */
424 assert (internals->pipes[1] == -1);
425 vlc_spin_unlock (&internals->spin);
430 vlc_spin_lock (&internals->spin);
431 if (internals->pipes[0] == -1)
433 internals->pipes[0] = pfd[0];
434 internals->pipes[1] = pfd[1];
435 pfd[0] = pfd[1] = -1;
439 vlc_spin_unlock (&internals->spin);
443 /* Race condition: vlc_object_kill() already invoked! */
446 vlc_spin_lock (&internals->spin);
447 fd = internals->pipes[1];
448 internals->pipes[1] = -1;
449 vlc_spin_unlock (&internals->spin);
451 msg_Dbg (obj, "waitpipe: object already dying");
456 /* Race condition: two threads call pipe() - unlikely */
462 return internals->pipes[0];
467 * Waits for the object to be signaled (using vlc_object_signal()).
468 * It is assumed that the caller has locked the object. This function will
469 * unlock the object, and lock it again before returning.
470 * If the object was signaled before the caller locked the object, it is
471 * undefined whether the signal will be lost or will wake the process.
473 * This function is a cancellation point. In case of cancellation, the object
474 * will be in locked state.
476 * @return true if the object is dying and should terminate.
478 void __vlc_object_wait( vlc_object_t *obj )
480 vlc_object_internals_t *priv = vlc_internals( obj );
481 vlc_assert_locked( &priv->lock);
482 vlc_cond_wait( &priv->wait, &priv->lock );
487 * Waits for the object to be signaled (using vlc_object_signal()), or for
488 * a timer to expire. It is asserted that the caller holds the object lock.
490 * This function is a cancellation point. In case of cancellation, the object
491 * will be in locked state.
493 * @return 0 if the object was signaled before the timer expiration, or
494 * ETIMEDOUT if the timer expired without any signal.
496 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
498 vlc_object_internals_t *priv = vlc_internals( obj );
499 vlc_assert_locked( &priv->lock);
500 return vlc_cond_timedwait( &priv->wait, &priv->lock, deadline );
505 * Signals an object for which the lock is held.
506 * At least one thread currently sleeping in vlc_object_wait() or
507 * vlc_object_timedwait() will wake up, assuming that there is at least one
508 * such thread in the first place. Otherwise, it is undefined whether the
509 * signal will be lost or will wake up one or more thread later.
511 void __vlc_object_signal_unlocked( vlc_object_t *obj )
513 vlc_assert_locked (&(vlc_internals(obj)->lock));
514 vlc_cond_signal( &(vlc_internals(obj)->wait) );
519 * Requests termination of an object.
520 * If the object is LibVLC, also request to terminate all its children.
522 void __vlc_object_kill( vlc_object_t *p_this )
524 vlc_object_internals_t *priv = vlc_internals( p_this );
527 vlc_thread_cancel( p_this );
528 vlc_object_lock( p_this );
529 p_this->b_die = true;
531 vlc_spin_lock (&priv->spin);
534 vlc_spin_unlock (&priv->spin);
538 msg_Dbg (p_this, "waitpipe: object killed");
542 vlc_object_signal_unlocked( p_this );
543 /* This also serves as a memory barrier toward vlc_object_alive(): */
544 vlc_object_unlock( p_this );
549 * Find an object given its ID.
551 * This function looks for the object whose i_object_id field is i_id.
552 * This function is slow, and often used to hide bugs. Do not use it.
553 * If you need to retain reference to an object, yield the object pointer with
554 * vlc_object_yield(), use the pointer as your reference, and call
555 * vlc_object_release() when you're done.
557 void * vlc_object_get( libvlc_int_t *p_anchor, int i_id )
559 vlc_object_t *obj = NULL;
561 int canc = vlc_savecancel ();
562 fprintf (stderr, "Use of deprecated vlc_object_get(%d) ", i_id);
564 vlc_restorecancel (canc);
566 vlc_mutex_lock( &structure_lock );
568 for( obj = vlc_internals (p_anchor)->next;
569 obj != VLC_OBJECT (p_anchor);
570 obj = vlc_internals (obj)->next )
572 if( obj->i_object_id == i_id )
574 vlc_object_yield( obj );
580 fprintf (stderr, "Object %d does not exist\n", i_id);
583 vlc_mutex_unlock( &structure_lock );
588 ****************************************************************************
589 * find a typed object and increment its refcount
590 *****************************************************************************
591 * This function recursively looks for a given object type. i_mode can be one
592 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
593 *****************************************************************************/
594 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
596 vlc_object_t *p_found;
598 /* If we are of the requested type ourselves, don't look further */
599 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
601 vlc_object_yield( p_this );
605 /* Otherwise, recursively look for the object */
606 if ((i_mode & 0x000f) == FIND_ANYWHERE)
609 if (i_type == VLC_OBJECT_PLAYLIST)
610 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
611 "instead of pl_Yield()");
613 return vlc_object_find (p_this->p_libvlc, i_type,
614 (i_mode & ~0x000f)|FIND_CHILD);
617 vlc_mutex_lock( &structure_lock );
618 p_found = FindObject( p_this, i_type, i_mode );
619 vlc_mutex_unlock( &structure_lock );
624 ****************************************************************************
625 * find a named object and increment its refcount
626 *****************************************************************************
627 * This function recursively looks for a given object name. i_mode can be one
628 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
629 *****************************************************************************/
630 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
633 vlc_object_t *p_found;
635 /* If have the requested name ourselves, don't look further */
636 if( !(i_mode & FIND_STRICT)
637 && p_this->psz_object_name
638 && !strcmp( p_this->psz_object_name, psz_name ) )
640 vlc_object_yield( p_this );
644 vlc_mutex_lock( &structure_lock );
646 /* Otherwise, recursively look for the object */
647 if( (i_mode & 0x000f) == FIND_ANYWHERE )
649 vlc_object_t *p_root = p_this;
652 while( p_root->p_parent != NULL &&
653 p_root != VLC_OBJECT( p_this->p_libvlc ) )
655 p_root = p_root->p_parent;
658 p_found = FindObjectName( p_root, psz_name,
659 (i_mode & ~0x000f)|FIND_CHILD );
660 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
662 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
663 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
668 p_found = FindObjectName( p_this, psz_name, i_mode );
671 vlc_mutex_unlock( &structure_lock );
677 * Increment an object reference counter.
679 void __vlc_object_yield( vlc_object_t *p_this )
681 vlc_object_internals_t *internals = vlc_internals( p_this );
683 vlc_spin_lock( &internals->ref_spin );
684 /* Avoid obvious freed object uses */
685 assert( internals->i_refcount > 0 );
686 /* Increment the counter */
687 internals->i_refcount++;
688 vlc_spin_unlock( &internals->ref_spin );
691 /*****************************************************************************
692 * Decrement an object refcount
693 * And destroy the object if its refcount reach zero.
694 *****************************************************************************/
695 void __vlc_object_release( vlc_object_t *p_this )
697 vlc_object_internals_t *internals = vlc_internals( p_this );
698 bool b_should_destroy;
700 vlc_spin_lock( &internals->ref_spin );
701 assert( internals->i_refcount > 0 );
703 if( internals->i_refcount > 1 )
706 /* There are still other references to the object */
707 internals->i_refcount--;
708 vlc_spin_unlock( &internals->ref_spin );
711 vlc_spin_unlock( &internals->ref_spin );
714 /* Remember that we cannot hold the spin while waiting on the mutex */
715 vlc_mutex_lock( &structure_lock );
716 /* Take the spin again. Note that another thread may have yielded the
717 * object in the (very short) mean time. */
718 vlc_spin_lock( &internals->ref_spin );
719 b_should_destroy = --internals->i_refcount == 0;
720 vlc_spin_unlock( &internals->ref_spin );
722 if( b_should_destroy )
725 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
728 vlc_object_t *leaked = internals->next;
729 while( leaked != p_this )
731 /* We are leaking this object */
733 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
734 leaked->i_object_id, leaked->psz_object_type,
735 leaked->psz_object_name );
736 /* Dump object to ease debugging */
737 vlc_object_dump( leaked );
739 leaked = vlc_internals (leaked)->next;
742 if( internals->next != p_this )
743 /* Dump libvlc object to ease debugging */
744 vlc_object_dump( p_this );
747 /* Remove the object from object list
748 * so that it cannot be encountered by vlc_object_get() */
749 vlc_internals (internals->next)->prev = internals->prev;
750 vlc_internals (internals->prev)->next = internals->next;
752 /* Detach from parent to protect against FIND_CHILDREN */
753 vlc_object_detach_unlocked (p_this);
754 /* Detach from children to protect against FIND_PARENT */
755 for (int i = 0; i < internals->i_children; i++)
756 internals->pp_children[i]->p_parent = NULL;
759 vlc_mutex_unlock( &structure_lock );
761 if( b_should_destroy )
765 free( internals->pp_children );
766 internals->pp_children = NULL;
767 internals->i_children = 0;
768 canc = vlc_savecancel ();
769 vlc_object_destroy( p_this );
770 vlc_restorecancel (canc);
775 ****************************************************************************
776 * attach object to a parent object
777 *****************************************************************************
778 * This function sets p_this as a child of p_parent, and p_parent as a parent
779 * of p_this. This link can be undone using vlc_object_detach.
780 *****************************************************************************/
781 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
783 if( !p_this ) return;
785 vlc_mutex_lock( &structure_lock );
787 /* Attach the parent to its child */
788 assert (!p_this->p_parent);
789 p_this->p_parent = p_parent;
791 /* Attach the child to its parent */
792 vlc_object_internals_t *priv = vlc_internals( p_parent );
793 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
796 vlc_mutex_unlock( &structure_lock );
800 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
802 vlc_assert_locked (&structure_lock);
804 if (p_this->p_parent == NULL)
807 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
811 /* Remove p_this's parent */
812 p_this->p_parent = NULL;
814 /* Remove all of p_parent's children which are p_this */
815 for( i_index = priv->i_children ; i_index-- ; )
817 if( priv->pp_children[i_index] == p_this )
820 for( i = i_index ; i < priv->i_children ; i++ )
821 priv->pp_children[i] = priv->pp_children[i+1];
825 if( priv->i_children )
827 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
828 priv->i_children * sizeof(vlc_object_t *) );
832 /* Special case - don't realloc() to zero to avoid leaking */
833 free( priv->pp_children );
834 priv->pp_children = NULL;
840 ****************************************************************************
841 * detach object from its parent
842 *****************************************************************************
843 * This function removes all links between an object and its parent.
844 *****************************************************************************/
845 void __vlc_object_detach( vlc_object_t *p_this )
847 if( !p_this ) return;
849 vlc_mutex_lock( &structure_lock );
850 if( !p_this->p_parent )
851 msg_Err( p_this, "object is not attached" );
853 vlc_object_detach_unlocked( p_this );
854 vlc_mutex_unlock( &structure_lock );
859 ****************************************************************************
860 * find a list typed objects and increment their refcount
861 *****************************************************************************
862 * This function recursively looks for a given object type. i_mode can be one
863 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
864 *****************************************************************************/
865 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
870 /* Look for the objects */
871 switch( i_mode & 0x000f )
874 /* Modules should probably not be object, and the module should perhaps
875 * not be shared across LibVLC instances. In the mean time, this ugly
876 * hack is brought to you by Courmisch. */
877 if (i_type == VLC_OBJECT_MODULE)
878 return vlc_list_find ((vlc_object_t *)p_module_bank,
880 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
883 vlc_mutex_lock( &structure_lock );
884 i_count = CountChildren( p_this, i_type );
885 p_list = NewList( i_count );
887 /* Check allocation was successful */
888 if( p_list->i_count != i_count )
890 vlc_mutex_unlock( &structure_lock );
891 msg_Err( p_this, "list allocation failed!" );
897 ListChildren( p_list, p_this, i_type );
898 vlc_mutex_unlock( &structure_lock );
902 msg_Err( p_this, "unimplemented!" );
903 p_list = NewList( 0 );
911 * Gets the list of children of an objects, and increment their reference
913 * @return a list (possibly empty) or NULL in case of error.
915 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
918 vlc_object_internals_t *priv = vlc_internals( obj );
920 vlc_mutex_lock( &structure_lock );
921 l = NewList( priv->i_children );
922 for (int i = 0; i < l->i_count; i++)
924 vlc_object_yield( priv->pp_children[i] );
925 l->p_values[i].p_object = priv->pp_children[i];
927 vlc_mutex_unlock( &structure_lock );
931 /*****************************************************************************
932 * DumpCommand: print the current vlc structure
933 *****************************************************************************
934 * This function prints either an ASCII tree showing the connections between
935 * vlc objects, and additional information such as their refcount, thread ID,
936 * etc. (command "tree"), or the same data as a simple list (command "list").
937 *****************************************************************************/
938 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
939 vlc_value_t oldval, vlc_value_t newval, void *p_data )
941 libvlc_int_t *p_libvlc = p_this->p_libvlc;
943 (void)oldval; (void)p_data;
944 if( *psz_cmd == 'l' )
946 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
948 vlc_mutex_lock( &structure_lock );
951 PrintObject (cur, "");
952 cur = vlc_internals (cur)->next;
954 while (cur != VLC_OBJECT(p_libvlc));
955 vlc_mutex_unlock( &structure_lock );
959 vlc_object_t *p_object = NULL;
961 if( *newval.psz_string )
964 int i_id = strtol( newval.psz_string, &end, 0 );
966 p_object = vlc_object_get( p_libvlc, i_id );
968 /* try using the object's name to find it */
969 p_object = vlc_object_find_name( p_this, newval.psz_string,
978 vlc_mutex_lock( &structure_lock );
980 if( *psz_cmd == 't' )
982 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
985 p_object = VLC_OBJECT(p_this->p_libvlc);
988 DumpStructure( p_object, 0, psz_foo );
990 else if( *psz_cmd == 'v' )
995 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
997 PrintObject( p_object, "" );
999 if( !vlc_internals( p_object )->i_vars )
1000 printf( " `-o No variables\n" );
1001 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1003 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1005 const char *psz_type = "unknown";
1006 switch( p_var->i_type & VLC_VAR_TYPE )
1008 #define MYCASE( type, nice ) \
1009 case VLC_VAR_ ## type: \
1012 MYCASE( VOID, "void" );
1013 MYCASE( BOOL, "bool" );
1014 MYCASE( INTEGER, "integer" );
1015 MYCASE( HOTKEY, "hotkey" );
1016 MYCASE( STRING, "string" );
1017 MYCASE( MODULE, "module" );
1018 MYCASE( FILE, "file" );
1019 MYCASE( DIRECTORY, "directory" );
1020 MYCASE( VARIABLE, "variable" );
1021 MYCASE( FLOAT, "float" );
1022 MYCASE( TIME, "time" );
1023 MYCASE( ADDRESS, "address" );
1024 MYCASE( MUTEX, "mutex" );
1025 MYCASE( LIST, "list" );
1028 printf( " %c-o \"%s\" (%s",
1029 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1030 p_var->psz_name, psz_type );
1031 if( p_var->psz_text )
1032 printf( ", %s", p_var->psz_text );
1034 if( p_var->i_type & VLC_VAR_HASCHOICE )
1035 printf( ", has choices" );
1036 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1037 printf( ", command" );
1038 if( p_var->i_entries )
1039 printf( ", %d callbacks", p_var->i_entries );
1040 switch( p_var->i_type & 0x00f0 )
1046 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1048 case VLC_VAR_INTEGER:
1049 printf( ": %d", p_var->val.i_int );
1051 case VLC_VAR_STRING:
1052 printf( ": \"%s\"", p_var->val.psz_string );
1055 printf( ": %f", p_var->val.f_float );
1058 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1060 case VLC_VAR_ADDRESS:
1061 printf( ": %p", p_var->val.p_address );
1071 vlc_mutex_unlock( &structure_lock );
1073 if( *newval.psz_string )
1075 vlc_object_release( p_object );
1082 /*****************************************************************************
1083 * vlc_list_release: free a list previously allocated by vlc_list_find
1084 *****************************************************************************
1085 * This function decreases the refcount of all objects in the list and
1087 *****************************************************************************/
1088 void vlc_list_release( vlc_list_t *p_list )
1092 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1094 vlc_object_release( p_list->p_values[i_index].p_object );
1097 free( p_list->p_values );
1101 /*****************************************************************************
1102 * dump an object. (Debug function)
1103 *****************************************************************************/
1104 static void vlc_object_dump( vlc_object_t *p_this )
1106 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1109 vlc_assert_locked( &structure_lock );
1110 DumpStructure( p_this, 0, psz_foo );
1113 /* Following functions are local */
1115 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1118 vlc_object_t *p_tmp;
1120 switch( i_mode & 0x000f )
1123 p_tmp = p_this->p_parent;
1126 if( p_tmp->i_object_type == i_type )
1128 vlc_object_yield( p_tmp );
1133 return FindObject( p_tmp, i_type, i_mode );
1139 for( i = vlc_internals( p_this )->i_children; i--; )
1141 p_tmp = vlc_internals( p_this )->pp_children[i];
1142 if( p_tmp->i_object_type == i_type )
1144 vlc_object_yield( p_tmp );
1147 else if( vlc_internals( p_tmp )->i_children )
1149 p_tmp = FindObject( p_tmp, i_type, i_mode );
1159 /* Handled in vlc_object_find */
1166 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1167 const char *psz_name,
1171 vlc_object_t *p_tmp;
1173 switch( i_mode & 0x000f )
1176 p_tmp = p_this->p_parent;
1179 if( p_tmp->psz_object_name
1180 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1182 vlc_object_yield( p_tmp );
1187 return FindObjectName( p_tmp, psz_name, i_mode );
1193 for( i = vlc_internals( p_this )->i_children; i--; )
1195 p_tmp = vlc_internals( p_this )->pp_children[i];
1196 if( p_tmp->psz_object_name
1197 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1199 vlc_object_yield( p_tmp );
1202 else if( vlc_internals( p_tmp )->i_children )
1204 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1214 /* Handled in vlc_object_find */
1222 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1224 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1227 int canc = vlc_savecancel ();
1228 memset( &psz_name, 0, sizeof(psz_name) );
1229 if( p_this->psz_object_name )
1231 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1233 psz_name[48] = '\"';
1236 psz_children[0] = '\0';
1237 switch( vlc_internals( p_this )->i_children )
1242 strcpy( psz_children, ", 1 child" );
1245 snprintf( psz_children, 19, ", %i children",
1246 vlc_internals( p_this )->i_children );
1250 psz_refcount[0] = '\0';
1251 if( vlc_internals( p_this )->i_refcount > 0 )
1252 snprintf( psz_refcount, 19, ", refcount %u",
1253 vlc_internals( p_this )->i_refcount );
1255 psz_thread[0] = '\0';
1256 if( vlc_internals( p_this )->b_thread )
1257 snprintf( psz_thread, 29, " (thread %lu)",
1258 (unsigned long)vlc_internals( p_this )->thread_id );
1260 psz_parent[0] = '\0';
1261 if( p_this->p_parent )
1262 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1264 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1265 p_this->i_object_id, p_this->psz_object_type,
1266 psz_name, psz_thread, psz_refcount, psz_children,
1268 vlc_restorecancel (canc);
1271 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1274 char i_back = psz_foo[i_level];
1275 psz_foo[i_level] = '\0';
1277 PrintObject( p_this, psz_foo );
1279 psz_foo[i_level] = i_back;
1281 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1283 msg_Warn( p_this, "structure tree is too deep" );
1287 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1291 psz_foo[i_level-1] = ' ';
1293 if( psz_foo[i_level-2] == '`' )
1295 psz_foo[i_level-2] = ' ';
1299 if( i == vlc_internals( p_this )->i_children - 1 )
1301 psz_foo[i_level] = '`';
1305 psz_foo[i_level] = '|';
1308 psz_foo[i_level+1] = '-';
1309 psz_foo[i_level+2] = '\0';
1311 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1316 static vlc_list_t * NewList( int i_count )
1318 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1319 if( p_list == NULL )
1324 p_list->i_count = i_count;
1328 p_list->p_values = NULL;
1332 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1333 if( p_list->p_values == NULL )
1335 p_list->i_count = 0;
1342 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1345 if( p_list == NULL || i_index >= p_list->i_count )
1350 vlc_object_yield( p_object );
1352 p_list->p_values[i_index].p_object = p_object;
1357 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1359 if( p_list == NULL )
1364 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1365 * sizeof( vlc_value_t ) );
1366 if( p_list->p_values == NULL )
1368 p_list->i_count = 0;
1372 vlc_object_yield( p_object );
1374 p_list->p_values[p_list->i_count].p_object = p_object;
1380 static int CountChildren( vlc_object_t *p_this, int i_type )
1382 vlc_object_t *p_tmp;
1385 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1387 p_tmp = vlc_internals( p_this )->pp_children[i];
1389 if( p_tmp->i_object_type == i_type )
1393 i_count += CountChildren( p_tmp, i_type );
1399 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1401 vlc_object_t *p_tmp;
1404 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1406 p_tmp = vlc_internals( p_this )->pp_children[i];
1408 if( p_tmp->i_object_type == i_type )
1409 ListReplace( p_list, p_tmp, p_list->i_count++ );
1411 ListChildren( p_list, p_tmp, i_type );