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);
87 /*****************************************************************************
88 * Local structure lock
89 *****************************************************************************/
90 static vlc_mutex_t structure_lock;
91 static unsigned object_counter = 0;
93 static void close_nocancel (int fd)
95 int canc = vlc_savecancel ();
97 vlc_restorecancel (canc);
100 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
101 int i_type, const char *psz_type )
104 vlc_object_internals_t *p_priv;
107 * VLC objects are laid out as follow:
108 * - first the LibVLC-private per-object data,
109 * - then VLC_COMMON members from vlc_object_t,
110 * - finally, the type-specific data (if any).
112 * This function initializes the LibVLC and common data,
113 * and zeroes the rest.
115 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
119 assert (i_size >= sizeof (vlc_object_t));
120 p_new = (vlc_object_t *)(p_priv + 1);
122 p_new->i_object_type = i_type;
123 p_new->psz_object_type = psz_type;
124 p_new->psz_object_name = NULL;
126 p_new->b_die = false;
127 p_new->b_error = false;
128 p_new->b_dead = false;
129 p_new->b_force = false;
131 p_new->psz_header = NULL;
134 p_new->i_flags = p_this->i_flags
135 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
137 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
139 if( !p_priv->p_vars )
145 libvlc_global_data_t *p_libvlc_global;
148 /* Only the global root object is created out of the blue */
149 p_libvlc_global = (libvlc_global_data_t *)p_new;
150 p_new->p_libvlc = NULL;
152 object_counter = 0; /* reset */
153 p_priv->next = p_priv->prev = p_new;
154 vlc_mutex_init( &structure_lock );
158 p_libvlc_global = vlc_global();
159 if( i_type == VLC_OBJECT_LIBVLC )
160 p_new->p_libvlc = (libvlc_int_t*)p_new;
162 p_new->p_libvlc = p_this->p_libvlc;
165 vlc_spin_init( &p_priv->ref_spin );
166 p_priv->i_refcount = 1;
167 p_priv->pf_destructor = NULL;
168 p_priv->b_thread = false;
169 p_new->p_parent = NULL;
170 p_priv->pp_children = NULL;
171 p_priv->i_children = 0;
173 p_new->p_private = NULL;
175 /* Initialize mutexes and condvars */
176 vlc_mutex_init( &p_priv->lock );
177 vlc_cond_init( p_new, &p_priv->wait );
178 vlc_mutex_init( &p_priv->var_lock );
179 vlc_spin_init( &p_priv->spin );
180 p_priv->pipes[0] = p_priv->pipes[1] = -1;
182 p_priv->next = VLC_OBJECT (p_libvlc_global);
183 vlc_mutex_lock( &structure_lock );
184 p_priv->prev = vlc_internals (p_libvlc_global)->prev;
185 vlc_internals (p_libvlc_global)->prev = p_new;
186 vlc_internals (p_priv->prev)->next = p_new;
187 p_new->i_object_id = object_counter++; /* fetch THEN increment */
188 vlc_mutex_unlock( &structure_lock );
190 if( i_type == VLC_OBJECT_LIBVLC )
192 int canc = vlc_savecancel ();
193 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "list", DumpCommand, NULL );
195 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196 var_AddCallback( p_new, "tree", DumpCommand, NULL );
197 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
198 var_AddCallback( p_new, "vars", DumpCommand, NULL );
199 vlc_restorecancel (canc);
207 * Allocates and initializes a vlc object.
209 * @param i_type known object type (all of them are negative integer values),
210 * or object byte size (always positive).
212 * @return the new object, or NULL on error.
214 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
216 const char * psz_type;
221 case VLC_OBJECT_INTF:
222 i_size = sizeof(intf_thread_t);
223 psz_type = "interface";
225 case VLC_OBJECT_DECODER:
226 i_size = sizeof(decoder_t);
227 psz_type = "decoder";
229 case VLC_OBJECT_PACKETIZER:
230 i_size = sizeof(decoder_t);
231 psz_type = "packetizer";
233 case VLC_OBJECT_ENCODER:
234 i_size = sizeof(encoder_t);
235 psz_type = "encoder";
237 case VLC_OBJECT_AOUT:
238 i_size = sizeof(aout_instance_t);
239 psz_type = "audio output";
241 case VLC_OBJECT_OPENGL:
242 i_size = sizeof( vout_thread_t );
245 case VLC_OBJECT_ANNOUNCE:
246 i_size = sizeof( announce_handler_t );
247 psz_type = "announce";
250 assert( i_type > 0 ); /* unknown type?! */
252 i_type = VLC_OBJECT_GENERIC;
253 psz_type = "generic";
257 return vlc_custom_create( p_this, i_size, i_type, psz_type );
262 ****************************************************************************
263 * Set the destructor of a vlc object
265 * This function sets the destructor of the vlc object. It will be called
266 * when the object is destroyed when the its refcount reaches 0.
267 * (It is called by the internal function vlc_object_destroy())
268 *****************************************************************************/
269 void __vlc_object_set_destructor( vlc_object_t *p_this,
270 vlc_destructor_t pf_destructor )
272 vlc_object_internals_t *p_priv = vlc_internals(p_this );
273 p_priv->pf_destructor = pf_destructor;
277 ****************************************************************************
278 * Destroy a vlc object (Internal)
280 * This function destroys an object that has been previously allocated with
281 * vlc_object_create. The object's refcount must be zero and it must not be
282 * attached to other objects in any way.
284 * This function must be called with cancellation disabled (currently).
285 *****************************************************************************/
286 static void vlc_object_destroy( vlc_object_t *p_this )
288 vlc_object_internals_t *p_priv = vlc_internals( p_this );
290 /* Objects are always detached beforehand */
291 assert( !p_this->p_parent );
293 /* Send a kill to the object's thread if applicable */
294 vlc_object_kill( p_this );
296 /* Call the custom "subclass" destructor */
297 if( p_priv->pf_destructor )
298 p_priv->pf_destructor( p_this );
300 /* Any thread must have been cleaned up at this point. */
301 assert( !p_priv->b_thread );
303 /* Destroy the associated variables, starting from the end so that
304 * no memmove calls have to be done. */
305 while( p_priv->i_vars )
307 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
310 free( p_priv->p_vars );
311 vlc_mutex_destroy( &p_priv->var_lock );
313 free( p_this->psz_header );
315 if( p_this->p_libvlc == NULL )
318 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
320 assert( p_global == vlc_global() );
322 if (p_priv->next != p_this)
324 vlc_object_t *leaked = p_priv->next, *first = leaked;
327 /* We are leaking this object */
329 "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
330 leaked->i_object_id, leaked->psz_object_type,
331 leaked->psz_object_name );
332 /* Dump libvlc object to ease debugging */
333 vlc_object_dump( leaked );
335 leaked = vlc_internals (leaked)->next;
337 while (leaked != first);
339 /* Dump global object to ease debugging */
340 vlc_object_dump( p_this );
341 /* Strongly abort, cause we want these to be fixed */
346 /* We are the global object ... no need to lock. */
347 vlc_mutex_destroy( &structure_lock );
350 FREENULL( p_this->psz_object_name );
352 #if defined(WIN32) || defined(UNDER_CE)
353 /* if object has an associated thread, close it now */
354 if( p_priv->thread_id )
355 CloseHandle(p_priv->thread_id);
358 vlc_spin_destroy( &p_priv->ref_spin );
359 vlc_mutex_destroy( &p_priv->lock );
360 vlc_cond_destroy( &p_priv->wait );
361 vlc_spin_destroy( &p_priv->spin );
362 if( p_priv->pipes[1] != -1 )
363 close( p_priv->pipes[1] );
364 if( p_priv->pipes[0] != -1 )
365 close( p_priv->pipes[0] );
371 /** Inter-object signaling */
373 void __vlc_object_lock( vlc_object_t *obj )
375 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
378 void __vlc_object_unlock( vlc_object_t *obj )
380 vlc_assert_locked( &(vlc_internals(obj)->lock) );
381 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
385 # include <winsock2.h>
386 # include <ws2tcpip.h>
389 * select()-able pipes emulated using Winsock
391 static int pipe (int fd[2])
394 int addrlen = sizeof (addr);
396 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
397 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
398 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
401 memset (&addr, 0, sizeof (addr));
402 addr.sin_family = AF_INET;
403 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
404 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
405 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
407 || connect (c, (PSOCKADDR)&addr, addrlen))
410 a = accept (l, NULL, NULL);
411 if (a == INVALID_SOCKET)
422 if (l != INVALID_SOCKET)
424 if (c != INVALID_SOCKET)
430 #define read( a, b, c ) recv (a, b, c, 0)
432 #define write( a, b, c ) send (a, b, c, 0)
434 #define close( a ) closesocket (a)
438 * Returns the readable end of a pipe that becomes readable once termination
439 * of the object is requested (vlc_object_kill()).
440 * This can be used to wake-up out of a select() or poll() event loop, such
441 * typically when doing network I/O.
443 * Note that the pipe will remain the same for the lifetime of the object.
444 * DO NOT read the pipe nor close it yourself. Ever.
446 * @param obj object that would be "killed"
447 * @return a readable pipe descriptor, or -1 on error.
449 int __vlc_object_waitpipe( vlc_object_t *obj )
451 int pfd[2] = { -1, -1 };
452 vlc_object_internals_t *internals = vlc_internals( obj );
455 vlc_spin_lock (&internals->spin);
456 if (internals->pipes[0] == -1)
458 /* This can only ever happen if someone killed us without locking: */
459 assert (internals->pipes[1] == -1);
460 vlc_spin_unlock (&internals->spin);
465 vlc_spin_lock (&internals->spin);
466 if (internals->pipes[0] == -1)
468 internals->pipes[0] = pfd[0];
469 internals->pipes[1] = pfd[1];
470 pfd[0] = pfd[1] = -1;
474 vlc_spin_unlock (&internals->spin);
478 /* Race condition: vlc_object_kill() already invoked! */
481 vlc_spin_lock (&internals->spin);
482 fd = internals->pipes[1];
483 internals->pipes[1] = -1;
484 vlc_spin_unlock (&internals->spin);
486 msg_Dbg (obj, "waitpipe: object already dying");
491 /* Race condition: two threads call pipe() - unlikely */
497 return internals->pipes[0];
502 * Waits for the object to be signaled (using vlc_object_signal()).
503 * It is assumed that the caller has locked the object. This function will
504 * unlock the object, and lock it again before returning.
505 * If the object was signaled before the caller locked the object, it is
506 * undefined whether the signal will be lost or will wake the process.
508 * This function is a cancellation point. In case of cancellation, the object
509 * will be in locked state.
511 * @return true if the object is dying and should terminate.
513 void __vlc_object_wait( vlc_object_t *obj )
515 vlc_object_internals_t *priv = vlc_internals( obj );
516 vlc_assert_locked( &priv->lock);
517 vlc_cond_wait( &priv->wait, &priv->lock );
522 * Waits for the object to be signaled (using vlc_object_signal()), or for
523 * a timer to expire. It is asserted that the caller holds the object lock.
525 * This function is a cancellation point. In case of cancellation, the object
526 * will be in locked state.
528 * @return 0 if the object was signaled before the timer expiration, or
529 * ETIMEDOUT if the timer expired without any signal.
531 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
533 vlc_object_internals_t *priv = vlc_internals( obj );
534 vlc_assert_locked( &priv->lock);
535 return vlc_cond_timedwait( &priv->wait, &priv->lock, deadline );
540 * Signals an object for which the lock is held.
541 * At least one thread currently sleeping in vlc_object_wait() or
542 * vlc_object_timedwait() will wake up, assuming that there is at least one
543 * such thread in the first place. Otherwise, it is undefined whether the
544 * signal will be lost or will wake up one or more thread later.
546 void __vlc_object_signal_unlocked( vlc_object_t *obj )
548 vlc_assert_locked (&(vlc_internals(obj)->lock));
549 vlc_cond_signal( &(vlc_internals(obj)->wait) );
554 * Requests termination of an object.
555 * If the object is LibVLC, also request to terminate all its children.
557 void __vlc_object_kill( vlc_object_t *p_this )
559 vlc_object_internals_t *priv = vlc_internals( p_this );
562 vlc_thread_cancel( p_this );
563 vlc_object_lock( p_this );
564 p_this->b_die = true;
566 vlc_spin_lock (&priv->spin);
569 vlc_spin_unlock (&priv->spin);
573 msg_Dbg (p_this, "waitpipe: object killed");
577 vlc_object_signal_unlocked( p_this );
578 /* This also serves as a memory barrier toward vlc_object_alive(): */
579 vlc_object_unlock( p_this );
584 * Find an object given its ID.
586 * This function looks for the object whose i_object_id field is i_id.
587 * This function is slow, and often used to hide bugs. Do not use it.
588 * If you need to retain reference to an object, yield the object pointer with
589 * vlc_object_yield(), use the pointer as your reference, and call
590 * vlc_object_release() when you're done.
592 void * vlc_object_get( int i_id )
594 libvlc_global_data_t *p_libvlc_global = vlc_global();
595 vlc_object_t *obj = NULL;
597 int canc = vlc_savecancel ();
598 fprintf (stderr, "Use of deprecated vlc_object_get(%d)\n", i_id);
599 vlc_restorecancel (canc);
601 vlc_mutex_lock( &structure_lock );
603 for( obj = vlc_internals (p_libvlc_global)->next;
604 obj != VLC_OBJECT (p_libvlc_global);
605 obj = vlc_internals (obj)->next )
607 if( obj->i_object_id == i_id )
609 vlc_object_yield( obj );
615 fprintf (stderr, "Object %d does not exist\n", i_id);
618 vlc_mutex_unlock( &structure_lock );
623 ****************************************************************************
624 * find a typed object and increment its refcount
625 *****************************************************************************
626 * This function recursively looks for a given object type. i_mode can be one
627 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
628 *****************************************************************************/
629 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
631 vlc_object_t *p_found;
633 /* If we are of the requested type ourselves, don't look further */
634 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
636 vlc_object_yield( p_this );
640 /* Otherwise, recursively look for the object */
641 if ((i_mode & 0x000f) == FIND_ANYWHERE)
644 if (i_type == VLC_OBJECT_PLAYLIST)
645 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
646 "instead of pl_Yield()");
648 return vlc_object_find (p_this->p_libvlc, i_type,
649 (i_mode & ~0x000f)|FIND_CHILD);
652 vlc_mutex_lock( &structure_lock );
653 p_found = FindObject( p_this, i_type, i_mode );
654 vlc_mutex_unlock( &structure_lock );
659 ****************************************************************************
660 * find a named object and increment its refcount
661 *****************************************************************************
662 * This function recursively looks for a given object name. i_mode can be one
663 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
664 *****************************************************************************/
665 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
668 vlc_object_t *p_found;
670 /* If have the requested name ourselves, don't look further */
671 if( !(i_mode & FIND_STRICT)
672 && p_this->psz_object_name
673 && !strcmp( p_this->psz_object_name, psz_name ) )
675 vlc_object_yield( p_this );
679 vlc_mutex_lock( &structure_lock );
681 /* Otherwise, recursively look for the object */
682 if( (i_mode & 0x000f) == FIND_ANYWHERE )
684 vlc_object_t *p_root = p_this;
687 while( p_root->p_parent != NULL &&
688 p_root != VLC_OBJECT( p_this->p_libvlc ) )
690 p_root = p_root->p_parent;
693 p_found = FindObjectName( p_root, psz_name,
694 (i_mode & ~0x000f)|FIND_CHILD );
695 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
697 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
698 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
703 p_found = FindObjectName( p_this, psz_name, i_mode );
706 vlc_mutex_unlock( &structure_lock );
712 * Increment an object reference counter.
714 void __vlc_object_yield( vlc_object_t *p_this )
716 vlc_object_internals_t *internals = vlc_internals( p_this );
718 vlc_spin_lock( &internals->ref_spin );
719 /* Avoid obvious freed object uses */
720 assert( internals->i_refcount > 0 );
721 /* Increment the counter */
722 internals->i_refcount++;
723 vlc_spin_unlock( &internals->ref_spin );
726 /*****************************************************************************
727 * Decrement an object refcount
728 * And destroy the object if its refcount reach zero.
729 *****************************************************************************/
730 void __vlc_object_release( vlc_object_t *p_this )
732 vlc_object_internals_t *internals = vlc_internals( p_this );
733 bool b_should_destroy;
735 vlc_spin_lock( &internals->ref_spin );
736 assert( internals->i_refcount > 0 );
738 if( internals->i_refcount > 1 )
741 /* There are still other references to the object */
742 internals->i_refcount--;
743 vlc_spin_unlock( &internals->ref_spin );
746 vlc_spin_unlock( &internals->ref_spin );
749 /* Remember that we cannot hold the spin while waiting on the mutex */
750 vlc_mutex_lock( &structure_lock );
751 /* Take the spin again. Note that another thread may have yielded the
752 * object in the (very short) mean time. */
753 vlc_spin_lock( &internals->ref_spin );
754 b_should_destroy = --internals->i_refcount == 0;
755 vlc_spin_unlock( &internals->ref_spin );
757 if( b_should_destroy )
759 /* Remove the object from object list
760 * so that it cannot be encountered by vlc_object_get() */
761 vlc_internals (internals->next)->prev = internals->prev;
762 vlc_internals (internals->prev)->next = internals->next;
764 /* Detach from parent to protect against FIND_CHILDREN */
765 vlc_object_detach_unlocked (p_this);
766 /* Detach from children to protect against FIND_PARENT */
767 for (int i = 0; i < internals->i_children; i++)
768 internals->pp_children[i]->p_parent = NULL;
771 vlc_mutex_unlock( &structure_lock );
773 if( b_should_destroy )
777 free( internals->pp_children );
778 internals->pp_children = NULL;
779 internals->i_children = 0;
780 canc = vlc_savecancel ();
781 vlc_object_destroy( p_this );
782 vlc_restorecancel (canc);
787 ****************************************************************************
788 * attach object to a parent object
789 *****************************************************************************
790 * This function sets p_this as a child of p_parent, and p_parent as a parent
791 * of p_this. This link can be undone using vlc_object_detach.
792 *****************************************************************************/
793 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
795 if( !p_this ) return;
797 vlc_mutex_lock( &structure_lock );
799 /* Attach the parent to its child */
800 assert (!p_this->p_parent);
801 p_this->p_parent = p_parent;
803 /* Attach the child to its parent */
804 vlc_object_internals_t *priv = vlc_internals( p_parent );
805 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
808 vlc_mutex_unlock( &structure_lock );
812 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
814 vlc_assert_locked (&structure_lock);
816 if (p_this->p_parent == NULL)
819 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
823 /* Remove p_this's parent */
824 p_this->p_parent = NULL;
826 /* Remove all of p_parent's children which are p_this */
827 for( i_index = priv->i_children ; i_index-- ; )
829 if( priv->pp_children[i_index] == p_this )
832 for( i = i_index ; i < priv->i_children ; i++ )
833 priv->pp_children[i] = priv->pp_children[i+1];
837 if( priv->i_children )
839 priv->pp_children = (vlc_object_t **)realloc( priv->pp_children,
840 priv->i_children * sizeof(vlc_object_t *) );
844 /* Special case - don't realloc() to zero to avoid leaking */
845 free( priv->pp_children );
846 priv->pp_children = NULL;
852 ****************************************************************************
853 * detach object from its parent
854 *****************************************************************************
855 * This function removes all links between an object and its parent.
856 *****************************************************************************/
857 void __vlc_object_detach( vlc_object_t *p_this )
859 if( !p_this ) return;
861 vlc_mutex_lock( &structure_lock );
862 if( !p_this->p_parent )
863 msg_Err( p_this, "object is not attached" );
865 vlc_object_detach_unlocked( p_this );
866 vlc_mutex_unlock( &structure_lock );
871 ****************************************************************************
872 * find a list typed objects and increment their refcount
873 *****************************************************************************
874 * This function recursively looks for a given object type. i_mode can be one
875 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
876 *****************************************************************************/
877 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
882 /* Look for the objects */
883 switch( i_mode & 0x000f )
886 /* Modules should probably not be object, and the module should perhaps
887 * not be shared across LibVLC instances. In the mean time, this ugly
888 * hack is brought to you by Courmisch. */
889 if (i_type == VLC_OBJECT_MODULE)
890 return vlc_list_find ((vlc_object_t *)vlc_global ()->p_module_bank,
892 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
895 vlc_mutex_lock( &structure_lock );
896 i_count = CountChildren( p_this, i_type );
897 p_list = NewList( i_count );
899 /* Check allocation was successful */
900 if( p_list->i_count != i_count )
902 vlc_mutex_unlock( &structure_lock );
903 msg_Err( p_this, "list allocation failed!" );
909 ListChildren( p_list, p_this, i_type );
910 vlc_mutex_unlock( &structure_lock );
914 msg_Err( p_this, "unimplemented!" );
915 p_list = NewList( 0 );
923 * Gets the list of children of an objects, and increment their reference
925 * @return a list (possibly empty) or NULL in case of error.
927 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
930 vlc_object_internals_t *priv = vlc_internals( obj );
932 vlc_mutex_lock( &structure_lock );
933 l = NewList( priv->i_children );
934 for (int i = 0; i < l->i_count; i++)
936 vlc_object_yield( priv->pp_children[i] );
937 l->p_values[i].p_object = priv->pp_children[i];
939 vlc_mutex_unlock( &structure_lock );
943 /*****************************************************************************
944 * DumpCommand: print the current vlc structure
945 *****************************************************************************
946 * This function prints either an ASCII tree showing the connections between
947 * vlc objects, and additional information such as their refcount, thread ID,
948 * etc. (command "tree"), or the same data as a simple list (command "list").
949 *****************************************************************************/
950 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
951 vlc_value_t oldval, vlc_value_t newval, void *p_data )
953 (void)oldval; (void)p_data;
954 if( *psz_cmd == 'l' )
956 vlc_object_t *root = VLC_OBJECT (vlc_global ()), *cur = root;
958 vlc_mutex_lock( &structure_lock );
961 PrintObject (cur, "");
962 cur = vlc_internals (cur)->next;
965 vlc_mutex_unlock( &structure_lock );
969 vlc_object_t *p_object = NULL;
971 if( *newval.psz_string )
974 int i_id = strtol( newval.psz_string, &end, 0 );
975 if( end != newval.psz_string )
976 p_object = vlc_object_get( i_id );
978 /* try using the object's name to find it */
979 p_object = vlc_object_find_name( p_this, newval.psz_string,
988 vlc_mutex_lock( &structure_lock );
990 if( *psz_cmd == 't' )
992 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
995 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
998 DumpStructure( p_object, 0, psz_foo );
1000 else if( *psz_cmd == 'v' )
1005 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1007 PrintObject( p_object, "" );
1009 if( !vlc_internals( p_object )->i_vars )
1010 printf( " `-o No variables\n" );
1011 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
1013 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
1015 const char *psz_type = "unknown";
1016 switch( p_var->i_type & VLC_VAR_TYPE )
1018 #define MYCASE( type, nice ) \
1019 case VLC_VAR_ ## type: \
1022 MYCASE( VOID, "void" );
1023 MYCASE( BOOL, "bool" );
1024 MYCASE( INTEGER, "integer" );
1025 MYCASE( HOTKEY, "hotkey" );
1026 MYCASE( STRING, "string" );
1027 MYCASE( MODULE, "module" );
1028 MYCASE( FILE, "file" );
1029 MYCASE( DIRECTORY, "directory" );
1030 MYCASE( VARIABLE, "variable" );
1031 MYCASE( FLOAT, "float" );
1032 MYCASE( TIME, "time" );
1033 MYCASE( ADDRESS, "address" );
1034 MYCASE( MUTEX, "mutex" );
1035 MYCASE( LIST, "list" );
1038 printf( " %c-o \"%s\" (%s",
1039 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
1040 p_var->psz_name, psz_type );
1041 if( p_var->psz_text )
1042 printf( ", %s", p_var->psz_text );
1044 if( p_var->i_type & VLC_VAR_HASCHOICE )
1045 printf( ", has choices" );
1046 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1047 printf( ", command" );
1048 if( p_var->i_entries )
1049 printf( ", %d callbacks", p_var->i_entries );
1050 switch( p_var->i_type & 0x00f0 )
1056 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1058 case VLC_VAR_INTEGER:
1059 printf( ": %d", p_var->val.i_int );
1061 case VLC_VAR_STRING:
1062 printf( ": \"%s\"", p_var->val.psz_string );
1065 printf( ": %f", p_var->val.f_float );
1068 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
1070 case VLC_VAR_ADDRESS:
1071 printf( ": %p", p_var->val.p_address );
1081 vlc_mutex_unlock( &structure_lock );
1083 if( *newval.psz_string )
1085 vlc_object_release( p_object );
1092 /*****************************************************************************
1093 * vlc_list_release: free a list previously allocated by vlc_list_find
1094 *****************************************************************************
1095 * This function decreases the refcount of all objects in the list and
1097 *****************************************************************************/
1098 void vlc_list_release( vlc_list_t *p_list )
1102 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1104 vlc_object_release( p_list->p_values[i_index].p_object );
1107 free( p_list->p_values );
1111 /*****************************************************************************
1112 * dump an object. (Debug function)
1113 *****************************************************************************/
1114 void __vlc_object_dump( vlc_object_t *p_this )
1116 vlc_mutex_lock( &structure_lock );
1117 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1119 DumpStructure( p_this, 0, psz_foo );
1120 vlc_mutex_unlock( &structure_lock );
1123 /* Following functions are local */
1125 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1128 vlc_object_t *p_tmp;
1130 switch( i_mode & 0x000f )
1133 p_tmp = p_this->p_parent;
1136 if( p_tmp->i_object_type == i_type )
1138 vlc_object_yield( p_tmp );
1143 return FindObject( p_tmp, i_type, i_mode );
1149 for( i = vlc_internals( p_this )->i_children; i--; )
1151 p_tmp = vlc_internals( p_this )->pp_children[i];
1152 if( p_tmp->i_object_type == i_type )
1154 vlc_object_yield( p_tmp );
1157 else if( vlc_internals( p_tmp )->i_children )
1159 p_tmp = FindObject( p_tmp, i_type, i_mode );
1169 /* Handled in vlc_object_find */
1176 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1177 const char *psz_name,
1181 vlc_object_t *p_tmp;
1183 switch( i_mode & 0x000f )
1186 p_tmp = p_this->p_parent;
1189 if( p_tmp->psz_object_name
1190 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1192 vlc_object_yield( p_tmp );
1197 return FindObjectName( p_tmp, psz_name, i_mode );
1203 for( i = vlc_internals( p_this )->i_children; i--; )
1205 p_tmp = vlc_internals( p_this )->pp_children[i];
1206 if( p_tmp->psz_object_name
1207 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1209 vlc_object_yield( p_tmp );
1212 else if( vlc_internals( p_tmp )->i_children )
1214 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1224 /* Handled in vlc_object_find */
1232 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1234 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1237 int canc = vlc_savecancel ();
1238 memset( &psz_name, 0, sizeof(psz_name) );
1239 if( p_this->psz_object_name )
1241 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1243 psz_name[48] = '\"';
1246 psz_children[0] = '\0';
1247 switch( vlc_internals( p_this )->i_children )
1252 strcpy( psz_children, ", 1 child" );
1255 snprintf( psz_children, 19, ", %i children",
1256 vlc_internals( p_this )->i_children );
1260 psz_refcount[0] = '\0';
1261 if( vlc_internals( p_this )->i_refcount > 0 )
1262 snprintf( psz_refcount, 19, ", refcount %u",
1263 vlc_internals( p_this )->i_refcount );
1265 psz_thread[0] = '\0';
1266 if( vlc_internals( p_this )->b_thread )
1267 snprintf( psz_thread, 29, " (thread %lu)",
1268 (unsigned long)vlc_internals( p_this )->thread_id );
1270 psz_parent[0] = '\0';
1271 if( p_this->p_parent )
1272 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1274 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1275 p_this->i_object_id, p_this->psz_object_type,
1276 psz_name, psz_thread, psz_refcount, psz_children,
1278 vlc_restorecancel (canc);
1281 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1284 char i_back = psz_foo[i_level];
1285 psz_foo[i_level] = '\0';
1287 PrintObject( p_this, psz_foo );
1289 psz_foo[i_level] = i_back;
1291 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1293 msg_Warn( p_this, "structure tree is too deep" );
1297 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1301 psz_foo[i_level-1] = ' ';
1303 if( psz_foo[i_level-2] == '`' )
1305 psz_foo[i_level-2] = ' ';
1309 if( i == vlc_internals( p_this )->i_children - 1 )
1311 psz_foo[i_level] = '`';
1315 psz_foo[i_level] = '|';
1318 psz_foo[i_level+1] = '-';
1319 psz_foo[i_level+2] = '\0';
1321 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1326 static vlc_list_t * NewList( int i_count )
1328 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1329 if( p_list == NULL )
1334 p_list->i_count = i_count;
1338 p_list->p_values = NULL;
1342 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1343 if( p_list->p_values == NULL )
1345 p_list->i_count = 0;
1352 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1355 if( p_list == NULL || i_index >= p_list->i_count )
1360 vlc_object_yield( p_object );
1362 p_list->p_values[i_index].p_object = p_object;
1367 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1369 if( p_list == NULL )
1374 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1375 * sizeof( vlc_value_t ) );
1376 if( p_list->p_values == NULL )
1378 p_list->i_count = 0;
1382 vlc_object_yield( p_object );
1384 p_list->p_values[p_list->i_count].p_object = p_object;
1390 static int CountChildren( vlc_object_t *p_this, int i_type )
1392 vlc_object_t *p_tmp;
1395 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1397 p_tmp = vlc_internals( p_this )->pp_children[i];
1399 if( p_tmp->i_object_type == i_type )
1403 i_count += CountChildren( p_tmp, i_type );
1409 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1411 vlc_object_t *p_tmp;
1414 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1416 p_tmp = vlc_internals( p_this )->pp_children[i];
1418 if( p_tmp->i_object_type == i_type )
1419 ListReplace( p_list, p_tmp, p_list->i_count++ );
1421 ListChildren( p_list, p_tmp, i_type );