1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_interface.h"
52 #include "vlc_codec.h"
53 #include "vlc_filter.h"
55 #include "variables.h"
61 # include <errno.h> /* ENOSYS */
65 /*****************************************************************************
67 *****************************************************************************/
68 static int DumpCommand( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void PrintObject ( vlc_object_t *, const char * );
74 static void DumpStructure ( vlc_object_t *, int, char * );
76 static vlc_list_t * NewList ( int );
77 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
78 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
79 static int CountChildren ( vlc_object_t *, int );
80 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
82 static void vlc_object_destroy( vlc_object_t *p_this );
83 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
85 /*****************************************************************************
86 * Local structure lock
87 *****************************************************************************/
88 static vlc_mutex_t structure_lock;
90 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
91 int i_type, const char *psz_type )
94 vlc_object_internals_t *p_priv;
97 * VLC objects are laid out as follow:
98 * - first the LibVLC-private per-object data,
99 * - then VLC_COMMON members from vlc_object_t,
100 * - finally, the type-specific data (if any).
102 * This function initializes the LibVLC and common data,
103 * and zeroes the rest.
105 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
109 assert (i_size >= sizeof (vlc_object_t));
110 p_new = (vlc_object_t *)(p_priv + 1);
112 p_new->i_object_type = i_type;
113 p_new->psz_object_type = psz_type;
114 p_new->psz_object_name = NULL;
116 p_new->b_die = false;
117 p_new->b_error = false;
118 p_new->b_dead = false;
119 p_new->b_force = false;
121 p_new->psz_header = NULL;
124 p_new->i_flags = p_this->i_flags
125 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
127 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
129 if( !p_priv->p_vars )
135 libvlc_global_data_t *p_libvlc_global;
138 /* Only the global root object is created out of the blue */
139 p_libvlc_global = (libvlc_global_data_t *)p_new;
140 p_new->p_libvlc = NULL;
142 p_libvlc_global->i_counter = 0;
143 p_priv->next = p_priv->prev = p_new;
144 vlc_mutex_init( &structure_lock );
148 p_libvlc_global = vlc_global();
149 if( i_type == VLC_OBJECT_LIBVLC )
150 p_new->p_libvlc = (libvlc_int_t*)p_new;
152 p_new->p_libvlc = p_this->p_libvlc;
155 vlc_spin_init( &p_priv->ref_spin );
156 p_priv->i_refcount = 1;
157 p_priv->pf_destructor = NULL;
158 p_priv->b_thread = false;
159 p_new->p_parent = NULL;
160 p_priv->pp_children = NULL;
161 p_priv->i_children = 0;
163 p_new->p_private = NULL;
165 /* Initialize mutexes and condvars */
166 vlc_mutex_init( &p_new->object_lock );
167 vlc_cond_init( p_new, &p_new->object_wait );
168 vlc_mutex_init( &p_priv->var_lock );
169 vlc_spin_init( &p_priv->spin );
170 p_priv->pipes[0] = p_priv->pipes[1] = -1;
172 p_priv->next = VLC_OBJECT (p_libvlc_global);
173 vlc_mutex_lock( &structure_lock );
174 p_priv->prev = vlc_internals (p_libvlc_global)->prev;
175 vlc_internals (p_libvlc_global)->prev = p_new;
176 vlc_internals (p_priv->prev)->next = p_new;
177 p_new->i_object_id = p_libvlc_global->i_counter++;
178 vlc_mutex_unlock( &structure_lock );
180 if( i_type == VLC_OBJECT_LIBVLC )
182 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_new, "list", DumpCommand, NULL );
184 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185 var_AddCallback( p_new, "tree", DumpCommand, NULL );
186 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_new, "vars", DumpCommand, NULL );
195 * Allocates and initializes a vlc object.
197 * @param i_type known object type (all of them are negative integer values),
198 * or object byte size (always positive).
200 * @return the new object, or NULL on error.
202 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
204 const char * psz_type;
209 case VLC_OBJECT_LIBVLC:
210 i_size = sizeof(libvlc_int_t);
213 case VLC_OBJECT_INTF:
214 i_size = sizeof(intf_thread_t);
215 psz_type = "interface";
217 case VLC_OBJECT_DIALOGS:
218 i_size = sizeof(intf_thread_t);
219 psz_type = "dialogs";
221 case VLC_OBJECT_DEMUX:
222 i_size = sizeof(demux_t);
225 case VLC_OBJECT_ACCESS:
226 i_size = sizeof(access_t);
229 case VLC_OBJECT_DECODER:
230 i_size = sizeof(decoder_t);
231 psz_type = "decoder";
233 case VLC_OBJECT_PACKETIZER:
234 i_size = sizeof(decoder_t);
235 psz_type = "packetizer";
237 case VLC_OBJECT_ENCODER:
238 i_size = sizeof(encoder_t);
239 psz_type = "encoder";
241 case VLC_OBJECT_FILTER:
242 i_size = sizeof(filter_t);
245 case VLC_OBJECT_VOUT:
246 i_size = sizeof(vout_thread_t);
247 psz_type = "video output";
249 case VLC_OBJECT_AOUT:
250 i_size = sizeof(aout_instance_t);
251 psz_type = "audio output";
253 case VLC_OBJECT_SOUT:
254 i_size = sizeof(sout_instance_t);
255 psz_type = "stream output";
257 case VLC_OBJECT_OPENGL:
258 i_size = sizeof( vout_thread_t );
261 case VLC_OBJECT_ANNOUNCE:
262 i_size = sizeof( announce_handler_t );
263 psz_type = "announce";
265 case VLC_OBJECT_INTERACTION:
266 i_size = sizeof( interaction_t );
267 psz_type = "interaction";
270 i_size = i_type > (int)sizeof(vlc_object_t)
271 ? i_type : (int)sizeof(vlc_object_t);
272 i_type = VLC_OBJECT_GENERIC;
273 psz_type = "generic";
277 return vlc_custom_create( p_this, i_size, i_type, psz_type );
282 ****************************************************************************
283 * Set the destructor of a vlc object
285 * This function sets the destructor of the vlc object. It will be called
286 * when the object is destroyed when the its refcount reaches 0.
287 * (It is called by the internal function vlc_object_destroy())
288 *****************************************************************************/
289 void __vlc_object_set_destructor( vlc_object_t *p_this,
290 vlc_destructor_t pf_destructor )
292 vlc_object_internals_t *p_priv = vlc_internals(p_this );
293 p_priv->pf_destructor = pf_destructor;
297 ****************************************************************************
298 * Destroy a vlc object (Internal)
300 * This function destroys an object that has been previously allocated with
301 * vlc_object_create. The object's refcount must be zero and it must not be
302 * attached to other objects in any way.
303 *****************************************************************************/
304 static void vlc_object_destroy( vlc_object_t *p_this )
306 vlc_object_internals_t *p_priv = vlc_internals( p_this );
308 /* Objects are always detached beforehand */
309 assert( !p_this->p_parent );
311 /* Send a kill to the object's thread if applicable */
312 vlc_object_kill( p_this );
314 /* If we are running on a thread, wait until it ends */
315 if( p_priv->b_thread )
316 vlc_thread_join( p_this );
318 /* Call the custom "subclass" destructor */
319 if( p_priv->pf_destructor )
320 p_priv->pf_destructor( p_this );
322 /* Destroy the associated variables, starting from the end so that
323 * no memmove calls have to be done. */
324 while( p_priv->i_vars )
326 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
329 free( p_priv->p_vars );
330 vlc_mutex_destroy( &p_priv->var_lock );
332 free( p_this->psz_header );
334 if( p_this->p_libvlc == NULL )
336 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
339 assert( p_global == vlc_global() );
341 for( vlc_object_t *leaked = p_priv->next;
343 leaked = vlc_internals (leaked)->next )
345 /* We are leaking this object */
347 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
348 leaked->i_object_id, leaked->psz_object_type,
349 leaked->psz_object_name );
350 /* Dump libvlc object to ease debugging */
351 vlc_object_dump( leaked );
355 if( p_priv->next != p_this )
357 /* Dump libvlc object to ease debugging */
358 vlc_object_dump( p_this );
359 /* Strongly abort, cause we want these to be fixed */
364 /* We are the global object ... no need to lock. */
365 vlc_mutex_destroy( &structure_lock );
368 FREENULL( p_this->psz_object_name );
370 #if defined(WIN32) || defined(UNDER_CE)
371 /* if object has an associated thread, close it now */
372 if( p_priv->thread_id )
373 CloseHandle(p_priv->thread_id);
376 vlc_spin_destroy( &p_priv->ref_spin );
377 vlc_mutex_destroy( &p_this->object_lock );
378 vlc_cond_destroy( &p_this->object_wait );
379 vlc_spin_destroy( &p_priv->spin );
380 if( p_priv->pipes[1] != -1 )
381 close( p_priv->pipes[1] );
382 if( p_priv->pipes[0] != -1 )
383 close( p_priv->pipes[0] );
389 /** Inter-object signaling */
391 void __vlc_object_lock( vlc_object_t *obj )
393 vlc_mutex_lock( &obj->object_lock );
396 void __vlc_object_unlock( vlc_object_t *obj )
398 vlc_assert_locked( &obj->object_lock );
399 vlc_mutex_unlock( &obj->object_lock );
403 # include <winsock2.h>
404 # include <ws2tcpip.h>
407 * select()-able pipes emulated using Winsock
409 static int pipe (int fd[2])
412 int addrlen = sizeof (addr);
414 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
415 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
416 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
419 memset (&addr, 0, sizeof (addr));
420 addr.sin_family = AF_INET;
421 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
422 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
423 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
425 || connect (c, (PSOCKADDR)&addr, addrlen))
428 a = accept (l, NULL, NULL);
429 if (a == INVALID_SOCKET)
440 if (l != INVALID_SOCKET)
442 if (c != INVALID_SOCKET)
448 #define read( a, b, c ) recv (a, b, c, 0)
450 #define write( a, b, c ) send (a, b, c, 0)
452 #define close( a ) closesocket (a)
456 * Returns the readable end of a pipe that becomes readable once termination
457 * of the object is requested (vlc_object_kill()).
458 * This can be used to wake-up out of a select() or poll() event loop, such
459 * typically when doing network I/O.
461 * Note that the pipe will remain the same for the lifetime of the object.
462 * DO NOT read the pipe nor close it yourself. Ever.
464 * @param obj object that would be "killed"
465 * @return a readable pipe descriptor, or -1 on error.
467 int __vlc_object_waitpipe( vlc_object_t *obj )
469 int pfd[2] = { -1, -1 };
470 vlc_object_internals_t *internals = vlc_internals( obj );
473 vlc_spin_lock (&internals->spin);
474 if (internals->pipes[0] == -1)
476 /* This can only ever happen if someone killed us without locking: */
477 assert (internals->pipes[1] == -1);
478 vlc_spin_unlock (&internals->spin);
483 vlc_spin_lock (&internals->spin);
484 if (internals->pipes[0] == -1)
486 internals->pipes[0] = pfd[0];
487 internals->pipes[1] = pfd[1];
488 pfd[0] = pfd[1] = -1;
492 vlc_spin_unlock (&internals->spin);
496 /* Race condition: vlc_object_kill() already invoked! */
499 vlc_spin_lock (&internals->spin);
500 fd = internals->pipes[1];
501 internals->pipes[1] = -1;
502 vlc_spin_unlock (&internals->spin);
504 msg_Dbg (obj, "waitpipe: object already dying");
509 /* Race condition: two threads call pipe() - unlikely */
515 return internals->pipes[0];
520 * Waits for the object to be signaled (using vlc_object_signal()).
521 * If the object already has a signal pending, this function will return
522 * immediately. It is asserted that the caller holds the object lock.
524 * @return true if the object is dying and should terminate.
526 bool __vlc_object_wait( vlc_object_t *obj )
528 vlc_assert_locked( &obj->object_lock );
529 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
535 * Waits for the object to be signaled (using vlc_object_signal()), or for
537 * If the object already has a signal pending, this function will return
538 * immediately. It is asserted that the caller holds the object lock.
540 * @return negative if the object is dying and should terminate,
541 * positive if the the object has been signaled but is not dying,
542 * 0 if timeout has been reached.
544 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
548 vlc_assert_locked( &obj->object_lock );
549 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
550 if( v == 0 ) /* signaled */
551 return obj->b_die ? -1 : 1;
557 * Checks whether an object has been "killed".
558 * The object lock must be held.
560 * Typical code for an object thread could be:
562 vlc_object_lock (self);
564 while (vlc_object_alive (self))
568 if (vlc_object_wait (self))
573 ...deinitialization...
574 vlc_object_unlock (self);
577 * @return true iff the object has not been killed yet
579 bool __vlc_object_alive( vlc_object_t *obj )
581 vlc_assert_locked( &obj->object_lock );
587 * Signals an object for which the lock is held.
589 void __vlc_object_signal_unlocked( vlc_object_t *obj )
591 vlc_assert_locked (&obj->object_lock);
592 vlc_cond_signal( &obj->object_wait );
597 * Requests termination of an object.
598 * If the object is LibVLC, also request to terminate all its children.
600 void __vlc_object_kill( vlc_object_t *p_this )
602 vlc_object_internals_t *internals = vlc_internals( p_this );
605 vlc_mutex_lock( &p_this->object_lock );
606 p_this->b_die = true;
608 vlc_spin_lock (&internals->spin);
609 fd = internals->pipes[1];
610 internals->pipes[1] = -1;
611 vlc_spin_unlock (&internals->spin);
615 msg_Dbg (p_this, "waitpipe: object killed");
619 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
620 for( int i = 0; i < internals->i_children ; i++ )
621 vlc_object_kill( internals->pp_children[i] );
623 vlc_object_signal_unlocked( p_this );
624 vlc_mutex_unlock( &p_this->object_lock );
629 * Find an object given its ID.
631 * This function looks for the object whose i_object_id field is i_id.
632 * This function is slow, and often used to hide bugs. Do not use it.
633 * If you need to retain reference to an object, yield the object pointer with
634 * vlc_object_yield(), use the pointer as your reference, and call
635 * vlc_object_release() when you're done.
637 void * vlc_object_get( int i_id )
639 libvlc_global_data_t *p_libvlc_global = vlc_global();
640 vlc_object_t *obj = NULL;
642 vlc_mutex_lock( &structure_lock );
644 for( obj = vlc_internals (p_libvlc_global)->next;
645 obj != VLC_OBJECT (p_libvlc_global);
646 obj = vlc_internals (obj)->next )
648 if( obj->i_object_id == i_id )
650 vlc_object_yield( obj );
655 vlc_mutex_unlock( &structure_lock );
660 ****************************************************************************
661 * find a typed object and increment its refcount
662 *****************************************************************************
663 * This function recursively looks for a given object type. i_mode can be one
664 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
665 *****************************************************************************/
666 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
668 vlc_object_t *p_found;
670 /* If we are of the requested type ourselves, don't look further */
671 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
673 vlc_object_yield( p_this );
677 /* Otherwise, recursively look for the object */
678 if ((i_mode & 0x000f) == FIND_ANYWHERE)
681 if (i_type == VLC_OBJECT_PLAYLIST)
682 msg_Warn (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
683 "instead of pl_Yield()");
685 return vlc_object_find (p_this->p_libvlc, i_type,
686 (i_mode & ~0x000f)|FIND_CHILD);
689 vlc_mutex_lock( &structure_lock );
690 p_found = FindObject( p_this, i_type, i_mode );
691 vlc_mutex_unlock( &structure_lock );
696 ****************************************************************************
697 * find a named object and increment its refcount
698 *****************************************************************************
699 * This function recursively looks for a given object name. i_mode can be one
700 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
701 *****************************************************************************/
702 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
705 vlc_object_t *p_found;
707 /* If have the requested name ourselves, don't look further */
708 if( !(i_mode & FIND_STRICT)
709 && p_this->psz_object_name
710 && !strcmp( p_this->psz_object_name, psz_name ) )
712 vlc_object_yield( p_this );
716 vlc_mutex_lock( &structure_lock );
718 /* Otherwise, recursively look for the object */
719 if( (i_mode & 0x000f) == FIND_ANYWHERE )
721 vlc_object_t *p_root = p_this;
724 while( p_root->p_parent != NULL &&
725 p_root != VLC_OBJECT( p_this->p_libvlc ) )
727 p_root = p_root->p_parent;
730 p_found = FindObjectName( p_root, psz_name,
731 (i_mode & ~0x000f)|FIND_CHILD );
732 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
734 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
735 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
740 p_found = FindObjectName( p_this, psz_name, i_mode );
743 vlc_mutex_unlock( &structure_lock );
749 * Increment an object reference counter.
751 void __vlc_object_yield( vlc_object_t *p_this )
753 vlc_object_internals_t *internals = vlc_internals( p_this );
755 vlc_spin_lock( &internals->ref_spin );
756 /* Avoid obvious freed object uses */
757 assert( internals->i_refcount > 0 );
758 /* Increment the counter */
759 internals->i_refcount++;
760 vlc_spin_unlock( &internals->ref_spin );
763 /*****************************************************************************
764 * decrement an object refcount
765 * And destroy the object if its refcount reach zero.
766 *****************************************************************************/
767 void __vlc_object_release( vlc_object_t *p_this )
769 vlc_object_internals_t *internals = vlc_internals( p_this );
770 bool b_should_destroy;
772 vlc_spin_lock( &internals->ref_spin );
773 assert( internals->i_refcount > 0 );
775 if( internals->i_refcount > 1 )
778 /* There are still other references to the object */
779 internals->i_refcount--;
780 vlc_spin_unlock( &internals->ref_spin );
783 vlc_spin_unlock( &internals->ref_spin );
786 /* Remember that we cannot hold the spin while waiting on the mutex */
787 vlc_mutex_lock( &structure_lock );
788 /* Take the spin again. Note that another thread may have yielded the
789 * object in the (very short) mean time. */
790 vlc_spin_lock( &internals->ref_spin );
791 b_should_destroy = --internals->i_refcount == 0;
792 vlc_spin_unlock( &internals->ref_spin );
794 if( b_should_destroy )
796 /* Remove the object from object list
797 * so that it cannot be encountered by vlc_object_get() */
798 vlc_internals (internals->next)->prev = internals->prev;
799 vlc_internals (internals->prev)->next = internals->next;
801 /* Detach from parent to protect against FIND_CHILDREN */
802 vlc_object_detach_unlocked (p_this);
803 /* Detach from children to protect against FIND_PARENT */
804 for (int i = 0; i < internals->i_children; i++)
805 internals->pp_children[i]->p_parent = NULL;
808 vlc_mutex_unlock( &structure_lock );
810 if( b_should_destroy )
812 free( internals->pp_children );
813 internals->pp_children = NULL;
814 internals->i_children = 0;
815 vlc_object_destroy( p_this );
820 ****************************************************************************
821 * attach object to a parent object
822 *****************************************************************************
823 * This function sets p_this as a child of p_parent, and p_parent as a parent
824 * of p_this. This link can be undone using vlc_object_detach.
825 *****************************************************************************/
826 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
828 if( !p_this ) return;
830 vlc_mutex_lock( &structure_lock );
832 /* Attach the parent to its child */
833 assert (!p_this->p_parent);
834 p_this->p_parent = p_parent;
836 /* Attach the child to its parent */
837 vlc_object_internals_t *priv = vlc_internals( p_parent );
838 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
841 vlc_mutex_unlock( &structure_lock );
845 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
847 vlc_assert_locked (&structure_lock);
849 if (p_this->p_parent == NULL)
852 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
856 /* Remove p_this's parent */
857 p_this->p_parent = NULL;
859 /* Remove all of p_parent's children which are p_this */
860 for( i_index = priv->i_children ; i_index-- ; )
862 if( priv->pp_children[i_index] == p_this )
865 for( i = i_index ; i < priv->i_children ; i++ )
866 priv->pp_children[i] = priv->pp_children[i+1];
870 if( priv->i_children )
872 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
873 priv->i_children * sizeof(vlc_object_t *) );
877 /* Special case - don't realloc() to zero to avoid leaking */
878 free( priv->pp_children );
879 priv->pp_children = NULL;
885 ****************************************************************************
886 * detach object from its parent
887 *****************************************************************************
888 * This function removes all links between an object and its parent.
889 *****************************************************************************/
890 void __vlc_object_detach( vlc_object_t *p_this )
892 if( !p_this ) return;
894 vlc_mutex_lock( &structure_lock );
895 if( !p_this->p_parent )
896 msg_Err( p_this, "object is not attached" );
898 vlc_object_detach_unlocked( p_this );
899 vlc_mutex_unlock( &structure_lock );
904 ****************************************************************************
905 * find a list typed objects and increment their refcount
906 *****************************************************************************
907 * This function recursively looks for a given object type. i_mode can be one
908 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
909 *****************************************************************************/
910 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
915 /* Look for the objects */
916 switch( i_mode & 0x000f )
919 /* Modules should probably not be object, and the module should perhaps
920 * not be shared across LibVLC instances. In the mean time, this ugly
921 * hack is brought to you by Courmisch. */
922 if (i_type == VLC_OBJECT_MODULE)
923 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
925 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
928 vlc_mutex_lock( &structure_lock );
929 i_count = CountChildren( p_this, i_type );
930 p_list = NewList( i_count );
932 /* Check allocation was successful */
933 if( p_list->i_count != i_count )
935 msg_Err( p_this, "list allocation failed!" );
941 ListChildren( p_list, p_this, i_type );
942 vlc_mutex_unlock( &structure_lock );
946 msg_Err( p_this, "unimplemented!" );
947 p_list = NewList( 0 );
955 * Gets the list of children of an objects, and increment their reference
957 * @return a list (possibly empty) or NULL in case of error.
959 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
962 vlc_object_internals_t *priv = vlc_internals( obj );
964 vlc_mutex_lock( &structure_lock );
965 l = NewList( priv->i_children );
966 for (int i = 0; i < l->i_count; i++)
968 vlc_object_yield( priv->pp_children[i] );
969 l->p_values[i].p_object = priv->pp_children[i];
971 vlc_mutex_unlock( &structure_lock );
975 /*****************************************************************************
976 * DumpCommand: print the current vlc structure
977 *****************************************************************************
978 * This function prints either an ASCII tree showing the connections between
979 * vlc objects, and additional information such as their refcount, thread ID,
980 * etc. (command "tree"), or the same data as a simple list (command "list").
981 *****************************************************************************/
982 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
983 vlc_value_t oldval, vlc_value_t newval, void *p_data )
985 (void)oldval; (void)p_data;
986 if( *psz_cmd == 'l' )
988 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
990 vlc_mutex_lock( &structure_lock );
993 PrintObject (cur, "");
994 cur = vlc_internals (cur)->next;
997 vlc_mutex_unlock( &structure_lock );
1001 vlc_object_t *p_object = NULL;
1003 if( *newval.psz_string )
1006 int i_id = strtol( newval.psz_string, &end, 0 );
1007 if( end != newval.psz_string )
1008 p_object = vlc_object_get( i_id );
1011 /* try using the object's name to find it */
1012 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1015 /* Look in p_libvlc's children tree */
1016 p_object = vlc_object_find_name( p_libvlc,
1019 vlc_object_release( p_libvlc );
1023 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1024 p_object = vlc_object_find_name( p_this,
1036 vlc_mutex_lock( &structure_lock );
1038 if( *psz_cmd == 't' )
1040 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1043 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1046 DumpStructure( p_object, 0, psz_foo );
1048 else if( *psz_cmd == 'v' )
1053 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1055 PrintObject( p_object, "" );
1057 if( !vlc_internals( p_object )->i_vars )
1058 printf( " `-o No variables\n" );
1059 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1061 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1063 const char *psz_type = "unknown";
1064 switch( p_var->i_type & VLC_VAR_TYPE )
1066 #define MYCASE( type, nice ) \
1067 case VLC_VAR_ ## type: \
1070 MYCASE( VOID, "void" );
1071 MYCASE( BOOL, "bool" );
1072 MYCASE( INTEGER, "integer" );
1073 MYCASE( HOTKEY, "hotkey" );
1074 MYCASE( STRING, "string" );
1075 MYCASE( MODULE, "module" );
1076 MYCASE( FILE, "file" );
1077 MYCASE( DIRECTORY, "directory" );
1078 MYCASE( VARIABLE, "variable" );
1079 MYCASE( FLOAT, "float" );
1080 MYCASE( TIME, "time" );
1081 MYCASE( ADDRESS, "address" );
1082 MYCASE( MUTEX, "mutex" );
1083 MYCASE( LIST, "list" );
1086 printf( " %c-o \"%s\" (%s",
1087 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1088 p_var->psz_name, psz_type );
1089 if( p_var->psz_text )
1090 printf( ", %s", p_var->psz_text );
1092 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1093 printf( ", command" );
1094 if( p_var->i_entries )
1095 printf( ", %d callbacks", p_var->i_entries );
1096 switch( p_var->i_type & 0x00f0 )
1102 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1104 case VLC_VAR_INTEGER:
1105 printf( ": %d", p_var->val.i_int );
1107 case VLC_VAR_STRING:
1108 printf( ": \"%s\"", p_var->val.psz_string );
1111 printf( ": %f", p_var->val.f_float );
1114 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1116 case VLC_VAR_ADDRESS:
1117 printf( ": %p", p_var->val.p_address );
1127 vlc_mutex_unlock( &structure_lock );
1129 if( *newval.psz_string )
1131 vlc_object_release( p_object );
1138 /*****************************************************************************
1139 * vlc_list_release: free a list previously allocated by vlc_list_find
1140 *****************************************************************************
1141 * This function decreases the refcount of all objects in the list and
1143 *****************************************************************************/
1144 void vlc_list_release( vlc_list_t *p_list )
1148 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1150 vlc_object_release( p_list->p_values[i_index].p_object );
1153 free( p_list->p_values );
1157 /*****************************************************************************
1158 * dump an object. (Debug function)
1159 *****************************************************************************/
1160 void __vlc_object_dump( vlc_object_t *p_this )
1162 vlc_mutex_lock( &structure_lock );
1163 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1165 DumpStructure( p_this, 0, psz_foo );
1166 vlc_mutex_unlock( &structure_lock );
1169 /* Following functions are local */
1171 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1174 vlc_object_t *p_tmp;
1176 switch( i_mode & 0x000f )
1179 p_tmp = p_this->p_parent;
1182 if( p_tmp->i_object_type == i_type )
1184 vlc_object_yield( p_tmp );
1189 return FindObject( p_tmp, i_type, i_mode );
1195 for( i = vlc_internals( p_this )->i_children; i--; )
1197 p_tmp = vlc_internals( p_this )->pp_children[i];
1198 if( p_tmp->i_object_type == i_type )
1200 vlc_object_yield( p_tmp );
1203 else if( vlc_internals( p_tmp )->i_children )
1205 p_tmp = FindObject( p_tmp, i_type, i_mode );
1215 /* Handled in vlc_object_find */
1222 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1223 const char *psz_name,
1227 vlc_object_t *p_tmp;
1229 switch( i_mode & 0x000f )
1232 p_tmp = p_this->p_parent;
1235 if( p_tmp->psz_object_name
1236 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1238 vlc_object_yield( p_tmp );
1243 return FindObjectName( p_tmp, psz_name, i_mode );
1249 for( i = vlc_internals( p_this )->i_children; i--; )
1251 p_tmp = vlc_internals( p_this )->pp_children[i];
1252 if( p_tmp->psz_object_name
1253 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1255 vlc_object_yield( p_tmp );
1258 else if( vlc_internals( p_tmp )->i_children )
1260 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1270 /* Handled in vlc_object_find */
1278 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1280 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1283 memset( &psz_name, 0, sizeof(psz_name) );
1284 if( p_this->psz_object_name )
1286 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1288 psz_name[48] = '\"';
1291 psz_children[0] = '\0';
1292 switch( vlc_internals( p_this )->i_children )
1297 strcpy( psz_children, ", 1 child" );
1300 snprintf( psz_children, 19, ", %i children",
1301 vlc_internals( p_this )->i_children );
1305 psz_refcount[0] = '\0';
1306 if( vlc_internals( p_this )->i_refcount > 0 )
1307 snprintf( psz_refcount, 19, ", refcount %u",
1308 vlc_internals( p_this )->i_refcount );
1310 psz_thread[0] = '\0';
1311 if( vlc_internals( p_this )->b_thread )
1312 snprintf( psz_thread, 29, " (thread %lu)",
1313 (unsigned long)vlc_internals( p_this )->thread_id );
1315 psz_parent[0] = '\0';
1316 if( p_this->p_parent )
1317 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1319 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1320 p_this->i_object_id, p_this->psz_object_type,
1321 psz_name, psz_thread, psz_refcount, psz_children,
1325 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1328 char i_back = psz_foo[i_level];
1329 psz_foo[i_level] = '\0';
1331 PrintObject( p_this, psz_foo );
1333 psz_foo[i_level] = i_back;
1335 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1337 msg_Warn( p_this, "structure tree is too deep" );
1341 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1345 psz_foo[i_level-1] = ' ';
1347 if( psz_foo[i_level-2] == '`' )
1349 psz_foo[i_level-2] = ' ';
1353 if( i == vlc_internals( p_this )->i_children - 1 )
1355 psz_foo[i_level] = '`';
1359 psz_foo[i_level] = '|';
1362 psz_foo[i_level+1] = '-';
1363 psz_foo[i_level+2] = '\0';
1365 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1370 static vlc_list_t * NewList( int i_count )
1372 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1373 if( p_list == NULL )
1378 p_list->i_count = i_count;
1382 p_list->p_values = NULL;
1386 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1387 if( p_list->p_values == NULL )
1389 p_list->i_count = 0;
1396 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1399 if( p_list == NULL || i_index >= p_list->i_count )
1404 vlc_object_yield( p_object );
1406 p_list->p_values[i_index].p_object = p_object;
1411 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1413 if( p_list == NULL )
1418 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1419 * sizeof( vlc_value_t ) );
1420 if( p_list->p_values == NULL )
1422 p_list->i_count = 0;
1426 vlc_object_yield( p_object );
1428 p_list->p_values[p_list->i_count].p_object = p_object;
1434 static int CountChildren( vlc_object_t *p_this, int i_type )
1436 vlc_object_t *p_tmp;
1439 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1441 p_tmp = vlc_internals( p_this )->pp_children[i];
1443 if( p_tmp->i_object_type == i_type )
1447 i_count += CountChildren( p_tmp, i_type );
1453 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1455 vlc_object_t *p_tmp;
1458 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1460 p_tmp = vlc_internals( p_this )->pp_children[i];
1462 if( p_tmp->i_object_type == i_type )
1463 ListReplace( p_list, p_tmp, p_list->i_count++ );
1465 ListChildren( p_list, p_tmp, i_type );