1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2007 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_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
65 #include "variables.h"
71 # include <errno.h> /* ENOSYS */
75 /*****************************************************************************
77 *****************************************************************************/
78 static int DumpCommand( vlc_object_t *, char const *,
79 vlc_value_t, vlc_value_t, void * );
81 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
82 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
83 static void DetachObject ( vlc_object_t * );
84 static void PrintObject ( vlc_object_t *, const char * );
85 static void DumpStructure ( vlc_object_t *, int, char * );
86 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
87 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
89 static vlc_list_t * NewList ( int );
90 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
91 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
92 static int CountChildren ( vlc_object_t *, int );
93 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
95 static void vlc_object_destroy( vlc_object_t *p_this );
97 /*****************************************************************************
98 * Local structure lock
99 *****************************************************************************/
100 static vlc_mutex_t structure_lock;
101 static vlc_object_internals_t global_internals;
103 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
104 int i_type, const char *psz_type )
107 vlc_object_internals_t *p_priv;
109 if( i_type == VLC_OBJECT_GLOBAL )
112 p_priv = &global_internals;
113 memset( p_priv, 0, sizeof( *p_priv ) );
117 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
121 p_new = (vlc_object_t *)(p_priv + 1);
124 p_new->p_internals = p_priv;
125 p_new->i_object_type = i_type;
126 p_new->psz_object_type = psz_type;
128 p_new->psz_object_name = NULL;
130 p_new->b_die = VLC_FALSE;
131 p_new->b_error = VLC_FALSE;
132 p_new->b_dead = VLC_FALSE;
133 p_priv->b_attached = VLC_FALSE;
134 p_new->b_force = VLC_FALSE;
136 p_new->psz_header = NULL;
138 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
139 p_new->i_flags |= OBJECT_FLAGS_NODBG;
140 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
141 p_new->i_flags |= OBJECT_FLAGS_QUIET;
142 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
143 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
145 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
147 if( !p_priv->p_vars )
149 if( i_type != VLC_OBJECT_GLOBAL )
154 if( i_type == VLC_OBJECT_GLOBAL )
156 /* If i_type is global, then p_new is actually p_libvlc_global */
157 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
158 p_new->p_libvlc = NULL;
160 p_libvlc_global->i_counter = 0;
161 p_new->i_object_id = 0;
163 p_libvlc_global->i_objects = 1;
164 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
165 p_libvlc_global->pp_objects[0] = p_new;
166 p_priv->b_attached = VLC_TRUE;
170 libvlc_global_data_t *p_libvlc_global = vlc_global();
171 if( i_type == VLC_OBJECT_LIBVLC )
173 p_new->p_libvlc = (libvlc_int_t*)p_new;
174 p_priv->b_attached = VLC_TRUE;
178 p_new->p_libvlc = p_this->p_libvlc;
181 vlc_mutex_lock( &structure_lock );
183 p_libvlc_global->i_counter++;
184 p_new->i_object_id = p_libvlc_global->i_counter;
186 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
187 * useless to try and recover anything if pp_objects gets smashed. */
188 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
191 vlc_mutex_unlock( &structure_lock );
194 p_priv->i_refcount = 1;
195 p_new->p_parent = NULL;
196 p_new->pp_children = NULL;
197 p_new->i_children = 0;
199 p_new->p_private = NULL;
201 /* Initialize mutexes and condvars */
202 vlc_mutex_init( p_new, &p_new->object_lock );
203 vlc_cond_init( p_new, &p_new->object_wait );
204 vlc_mutex_init( p_new, &p_priv->var_lock );
205 vlc_spin_init( &p_priv->spin );
206 p_priv->pipes[0] = p_priv->pipes[1] = -1;
208 if( i_type == VLC_OBJECT_GLOBAL )
210 vlc_mutex_init( p_new, &structure_lock );
213 if( i_type == VLC_OBJECT_LIBVLC )
215 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
216 var_AddCallback( p_new, "list", DumpCommand, NULL );
217 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
218 var_AddCallback( p_new, "tree", DumpCommand, NULL );
219 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
220 var_AddCallback( p_new, "vars", DumpCommand, NULL );
228 * Allocates and initializes a vlc object.
230 * @param i_type known object type (all of them are negative integer values),
231 * or object byte size (always positive).
233 * @return the new object, or NULL on error.
235 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
237 const char * psz_type;
242 case VLC_OBJECT_GLOBAL:
243 i_size = sizeof(libvlc_global_data_t);
246 case VLC_OBJECT_LIBVLC:
247 i_size = sizeof(libvlc_int_t);
250 case VLC_OBJECT_INTF:
251 i_size = sizeof(intf_thread_t);
252 psz_type = "interface";
254 case VLC_OBJECT_DIALOGS:
255 i_size = sizeof(intf_thread_t);
256 psz_type = "dialogs";
258 case VLC_OBJECT_PLAYLIST:
259 i_size = sizeof(playlist_t);
260 psz_type = "playlist";
263 i_size = sizeof(services_discovery_t);
264 psz_type = "services discovery";
266 case VLC_OBJECT_INPUT:
267 i_size = sizeof(input_thread_t);
270 case VLC_OBJECT_DEMUX:
271 i_size = sizeof(demux_t);
274 case VLC_OBJECT_ACCESS:
275 i_size = sizeof(access_t);
278 case VLC_OBJECT_DECODER:
279 i_size = sizeof(decoder_t);
280 psz_type = "decoder";
282 case VLC_OBJECT_PACKETIZER:
283 i_size = sizeof(decoder_t);
284 psz_type = "packetizer";
286 case VLC_OBJECT_ENCODER:
287 i_size = sizeof(encoder_t);
288 psz_type = "encoder";
290 case VLC_OBJECT_FILTER:
291 i_size = sizeof(filter_t);
294 case VLC_OBJECT_VOUT:
295 i_size = sizeof(vout_thread_t);
296 psz_type = "video output";
299 i_size = sizeof(spu_t);
300 psz_type = "subpicture";
302 case VLC_OBJECT_AOUT:
303 i_size = sizeof(aout_instance_t);
304 psz_type = "audio output";
306 case VLC_OBJECT_SOUT:
307 i_size = sizeof(sout_instance_t);
308 psz_type = "stream output";
311 i_size = sizeof( vlm_t );
312 psz_type = "vlm dameon";
315 i_size = sizeof( vod_t );
316 psz_type = "vod server";
319 i_size = sizeof( xml_t );
322 case VLC_OBJECT_OPENGL:
323 i_size = sizeof( vout_thread_t );
326 case VLC_OBJECT_ANNOUNCE:
327 i_size = sizeof( announce_handler_t );
328 psz_type = "announce";
330 case VLC_OBJECT_META_ENGINE:
331 i_size = sizeof( meta_engine_t );
332 psz_type = "meta engine";
334 case VLC_OBJECT_OSDMENU:
335 i_size = sizeof( osd_menu_t );
336 psz_type = "osd menu";
339 i_size = i_type > (int)sizeof(vlc_object_t)
340 ? i_type : (int)sizeof(vlc_object_t);
341 i_type = VLC_OBJECT_GENERIC;
342 psz_type = "generic";
346 return vlc_custom_create( p_this, i_size, i_type, psz_type );
351 ****************************************************************************
352 * Destroy a vlc object (Internal)
354 * This function destroys an object that has been previously allocated with
355 * vlc_object_create. The object's refcount must be zero and it must not be
356 * attached to other objects in any way.
357 *****************************************************************************/
358 static void vlc_object_destroy( vlc_object_t *p_this )
360 vlc_object_internals_t *p_priv = vlc_internals( p_this );
362 /* FIXME: ugly hack - we cannot use the message queue after
364 vlc_object_t *logger = p_this;
365 if( p_this->p_libvlc == p_this )
368 if( p_this->i_children )
371 "ERROR: cannot delete object (%i, %s) with children\n",
372 p_this->i_object_id, p_this->psz_object_name );
377 if( p_this->p_parent )
380 "ERROR: cannot delete object (%i, %s) with a parent\n",
381 p_this->i_object_id, p_this->psz_object_name );
386 /* Destroy the associated variables, starting from the end so that
387 * no memmove calls have to be done. */
388 while( p_priv->i_vars )
390 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
393 free( p_priv->p_vars );
394 vlc_mutex_destroy( &p_priv->var_lock );
396 if( p_this->psz_header ) free( p_this->psz_header );
398 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
400 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
402 /* Remove ourselves */
403 int i_index = FindIndex( p_this, p_global->pp_objects,
404 p_global->i_objects );
405 REMOVE_ELEM( p_global->pp_objects,
406 p_global->i_objects, i_index );
409 if( p_global->i_objects > 0 )
412 for( i = 0; i < p_global->i_objects; i++ )
414 /* We are leaking this object */
416 "ERROR: We are leaking object (id:%i, type:%s, name:%s)\n",
417 p_global->pp_objects[i]->i_object_id,
418 p_global->pp_objects[i]->psz_object_type,
419 p_global->pp_objects[i]->psz_object_name );
422 /* Strongly abort, cause we want these to be fixed */
426 /* We are the global object ... no need to lock. */
427 free( p_global->pp_objects );
428 p_global->pp_objects = NULL;
430 vlc_mutex_destroy( &structure_lock );
434 libvlc_global_data_t *p_libvlc_global = vlc_global();
437 vlc_mutex_lock( &structure_lock );
439 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
440 * useless to try and recover anything if pp_objects gets smashed. */
441 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
442 p_libvlc_global->i_objects );
443 REMOVE_ELEM( p_libvlc_global->pp_objects,
444 p_libvlc_global->i_objects, i_index );
446 vlc_mutex_unlock( &structure_lock );
449 #if defined(WIN32) || defined(UNDER_CE)
450 /* if object has an associated thread, close it now */
451 if( p_priv->thread_id.hThread )
452 CloseHandle(p_priv->thread_id.hThread);
455 vlc_mutex_destroy( &p_this->object_lock );
456 vlc_cond_destroy( &p_this->object_wait );
457 vlc_spin_destroy( &p_priv->spin );
458 if( p_priv->pipes[1] != -1 )
459 close( p_priv->pipes[1] );
460 if( p_priv->pipes[0] != -1 )
461 close( p_priv->pipes[0] );
463 /* global is not dynamically allocated by vlc_object_create */
464 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
469 /** Inter-object signaling */
471 void __vlc_object_lock( vlc_object_t *obj )
473 vlc_mutex_lock( &obj->object_lock );
476 void __vlc_object_unlock( vlc_object_t *obj )
478 vlc_assert_locked( &obj->object_lock );
479 vlc_mutex_unlock( &obj->object_lock );
483 # include <winsock2.h>
484 # include <ws2tcpip.h>
487 * select()-able pipes emulated using Winsock
489 static int pipe (int fd[2])
492 int addrlen = sizeof (addr);
494 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
495 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
496 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
499 memset (&addr, 0, sizeof (addr));
500 addr.sin_family = AF_INET;
501 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
502 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
503 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
505 || connect (c, (PSOCKADDR)&addr, addrlen))
508 a = accept (l, NULL, NULL);
509 if (a == INVALID_SOCKET)
520 if (l != INVALID_SOCKET)
522 if (c != INVALID_SOCKET)
528 #define read( a, b, c ) recv (a, b, c, 0)
530 #define write( a, b, c ) send (a, b, c, 0)
532 #define close( a ) closesocket (a)
536 * Returns the readable end of a pipe that becomes readable once termination
537 * of the object is requested (vlc_object_kill()).
538 * This can be used to wake-up out of a select() or poll() event loop, such
539 * typically when doing network I/O.
541 * Note that the pipe will remain the same for the lifetime of the object.
542 * DO NOT read the pipe nor close it yourself. Ever.
544 * @param obj object that would be "killed"
545 * @return a readable pipe descriptor, or -1 on error.
547 int __vlc_object_waitpipe( vlc_object_t *obj )
549 int pfd[2] = { -1, -1 };
550 struct vlc_object_internals_t *internals = obj->p_internals;
551 vlc_bool_t killed = VLC_FALSE;
553 vlc_spin_lock (&internals->spin);
554 if (internals->pipes[0] == -1)
556 /* This can only ever happen if someone killed us without locking: */
557 assert (internals->pipes[1] == -1);
558 vlc_spin_unlock (&internals->spin);
563 vlc_spin_lock (&internals->spin);
564 if (internals->pipes[0] == -1)
566 internals->pipes[0] = pfd[0];
567 internals->pipes[1] = pfd[1];
568 pfd[0] = pfd[1] = -1;
572 vlc_spin_unlock (&internals->spin);
576 /* Race condition: vlc_object_kill() already invoked! */
579 vlc_spin_lock (&internals->spin);
580 fd = internals->pipes[1];
581 internals->pipes[1] = -1;
582 vlc_spin_unlock (&internals->spin);
584 msg_Dbg (obj, "waitpipe: object already dying");
589 /* Race condition: two threads call pipe() - unlikely */
595 return internals->pipes[0];
600 * Waits for the object to be signaled (using vlc_object_signal()).
601 * If the object already has a signal pending, this function will return
602 * immediately. It is asserted that the caller holds the object lock.
604 * @return true if the object is dying and should terminate.
606 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
608 vlc_assert_locked( &obj->object_lock );
609 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
615 * Waits for the object to be signaled (using vlc_object_signal()), or for
617 * If the object already has a signal pending, this function will return
618 * immediately. It is asserted that the caller holds the object lock.
620 * @return negative if the object is dying and should terminate,
621 * positive if the the object has been signaled but is not dying,
622 * 0 if timeout has been reached.
624 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
628 vlc_assert_locked( &obj->object_lock );
629 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
630 if( v == 0 ) /* signaled */
631 return obj->b_die ? -1 : 1;
637 * Checks whether an object has been "killed".
638 * The object lock must be held.
640 * Typical code for an object thread could be:
642 vlc_object_lock (self);
644 while (vlc_object_alive (self))
648 if (vlc_object_wait (self))
653 ...deinitialization...
654 vlc_object_unlock (self);
657 * @return true iff the object has not been killed yet
659 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
661 vlc_assert_locked( &obj->object_lock );
667 * Signals an object for which the lock is held.
669 void __vlc_object_signal_unlocked( vlc_object_t *obj )
671 vlc_assert_locked (&obj->object_lock);
672 vlc_cond_signal( &obj->object_wait );
677 * Requests termination of an object.
678 * If the object is LibVLC, also request to terminate all its children.
680 void __vlc_object_kill( vlc_object_t *p_this )
682 struct vlc_object_internals_t *internals = p_this->p_internals;
685 vlc_mutex_lock( &p_this->object_lock );
686 p_this->b_die = VLC_TRUE;
688 vlc_spin_lock (&internals->spin);
689 fd = internals->pipes[1];
690 internals->pipes[1] = -1;
691 vlc_spin_unlock (&internals->spin);
695 msg_Dbg (p_this, "waitpipe: object killed");
699 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
700 for( int i = 0; i < p_this->i_children ; i++ )
701 vlc_object_kill( p_this->pp_children[i] );
703 vlc_object_signal_unlocked( p_this );
704 vlc_mutex_unlock( &p_this->object_lock );
709 * find an object given its ID
711 * This function looks for the object whose i_object_id field is i_id. We
712 * use a dichotomy so that lookups are in log2(n).
713 *****************************************************************************/
714 void * vlc_object_get( int i_id )
717 vlc_object_t **pp_objects;
718 libvlc_global_data_t *p_libvlc_global = vlc_global();
720 vlc_mutex_lock( &structure_lock );
722 pp_objects = p_libvlc_global->pp_objects;
724 /* Perform our dichotomy */
725 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
727 i_middle = i_max / 2;
729 if( pp_objects[i_middle]->i_object_id > i_id )
733 else if( pp_objects[i_middle]->i_object_id < i_id )
737 pp_objects += i_middle;
742 /* This happens when there are only two remaining objects */
743 if( pp_objects[i_middle+1]->i_object_id == i_id )
745 vlc_mutex_unlock( &structure_lock );
746 pp_objects[i_middle+1]->p_internals->i_refcount++;
747 return pp_objects[i_middle+1];
754 vlc_mutex_unlock( &structure_lock );
755 pp_objects[i_middle]->p_internals->i_refcount++;
756 return pp_objects[i_middle];
761 /* this means that i_max == i_middle, and since we have already
762 * tested pp_objects[i_middle]), p_found is properly set. */
767 vlc_mutex_unlock( &structure_lock );
772 ****************************************************************************
773 * find a typed object and increment its refcount
774 *****************************************************************************
775 * This function recursively looks for a given object type. i_mode can be one
776 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
777 *****************************************************************************/
778 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
780 vlc_object_t *p_found;
782 vlc_mutex_lock( &structure_lock );
784 /* If we are of the requested type ourselves, don't look further */
785 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
787 p_this->p_internals->i_refcount++;
788 vlc_mutex_unlock( &structure_lock );
792 /* Otherwise, recursively look for the object */
793 if( (i_mode & 0x000f) == FIND_ANYWHERE )
795 vlc_object_t *p_root = p_this;
798 while( p_root->p_parent != NULL &&
799 p_root != VLC_OBJECT( p_this->p_libvlc ) )
801 p_root = p_root->p_parent;
804 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
805 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
807 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
808 i_type, (i_mode & ~0x000f)|FIND_CHILD );
813 p_found = FindObject( p_this, i_type, i_mode );
816 vlc_mutex_unlock( &structure_lock );
822 ****************************************************************************
823 * find a named object and increment its refcount
824 *****************************************************************************
825 * This function recursively looks for a given object name. i_mode can be one
826 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
827 *****************************************************************************/
828 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
831 vlc_object_t *p_found;
833 vlc_mutex_lock( &structure_lock );
835 /* If have the requested name ourselves, don't look further */
836 if( !(i_mode & FIND_STRICT)
837 && p_this->psz_object_name
838 && !strcmp( p_this->psz_object_name, psz_name ) )
840 p_this->p_internals->i_refcount++;
841 vlc_mutex_unlock( &structure_lock );
845 /* Otherwise, recursively look for the object */
846 if( (i_mode & 0x000f) == FIND_ANYWHERE )
848 vlc_object_t *p_root = p_this;
851 while( p_root->p_parent != NULL &&
852 p_root != VLC_OBJECT( p_this->p_libvlc ) )
854 p_root = p_root->p_parent;
857 p_found = FindObjectName( p_root, psz_name,
858 (i_mode & ~0x000f)|FIND_CHILD );
859 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
861 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
862 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
867 p_found = FindObjectName( p_this, psz_name, i_mode );
870 vlc_mutex_unlock( &structure_lock );
876 ****************************************************************************
877 * increment an object refcount
878 *****************************************************************************/
879 void __vlc_object_yield( vlc_object_t *p_this )
881 vlc_mutex_lock( &structure_lock );
882 p_this->p_internals->i_refcount++;
883 vlc_mutex_unlock( &structure_lock );
886 /*****************************************************************************
887 * decrement an object refcount
888 * And destroy the object if its refcount reach zero.
889 *****************************************************************************/
890 void __vlc_object_release( vlc_object_t *p_this )
892 vlc_bool_t b_should_destroy;
894 vlc_mutex_lock( &structure_lock );
896 assert( p_this->p_internals->i_refcount > 0 );
897 p_this->p_internals->i_refcount--;
898 b_should_destroy = (p_this->p_internals->i_refcount == 0);
900 vlc_mutex_unlock( &structure_lock );
902 if( b_should_destroy )
903 vlc_object_destroy( p_this );
906 /* Version without the lock */
907 static void vlc_object_release_locked( vlc_object_t *p_this )
909 vlc_bool_t b_should_destroy;
911 assert( p_this->p_internals->i_refcount > 0 );
912 p_this->p_internals->i_refcount--;
913 b_should_destroy = (p_this->p_internals->i_refcount == 0);
915 if( b_should_destroy )
916 vlc_object_destroy( p_this );
920 ****************************************************************************
921 * attach object to a parent object
922 *****************************************************************************
923 * This function sets p_this as a child of p_parent, and p_parent as a parent
924 * of p_this. This link can be undone using vlc_object_detach.
925 *****************************************************************************/
926 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
928 if( !p_this ) return;
930 vlc_mutex_lock( &structure_lock );
932 /* Attach the parent to its child */
933 p_this->p_parent = p_parent;
935 /* Attach the child to its parent */
936 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
937 p_parent->i_children, p_this );
939 /* Climb up the tree to see whether we are connected with the root */
940 if( p_parent->p_internals->b_attached )
942 SetAttachment( p_this, VLC_TRUE );
945 vlc_mutex_unlock( &structure_lock );
949 ****************************************************************************
950 * detach object from its parent
951 *****************************************************************************
952 * This function removes all links between an object and its parent.
953 *****************************************************************************/
954 void __vlc_object_detach( vlc_object_t *p_this )
956 if( !p_this ) return;
958 vlc_mutex_lock( &structure_lock );
959 if( !p_this->p_parent )
961 msg_Err( p_this, "object is not attached" );
962 vlc_mutex_unlock( &structure_lock );
966 /* Climb up the tree to see whether we are connected with the root */
967 if( p_this->p_parent->p_internals->b_attached )
969 SetAttachment( p_this, VLC_FALSE );
972 DetachObject( p_this );
973 vlc_mutex_unlock( &structure_lock );
978 ****************************************************************************
979 * find a list typed objects and increment their refcount
980 *****************************************************************************
981 * This function recursively looks for a given object type. i_mode can be one
982 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
983 *****************************************************************************/
984 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
987 vlc_object_t **pp_current, **pp_end;
988 int i_count = 0, i_index = 0;
989 libvlc_global_data_t *p_libvlc_global = vlc_global();
991 vlc_mutex_lock( &structure_lock );
993 /* Look for the objects */
994 switch( i_mode & 0x000f )
997 pp_current = p_libvlc_global->pp_objects;
998 pp_end = pp_current + p_libvlc_global->i_objects;
1000 for( ; pp_current < pp_end ; pp_current++ )
1002 if( (*pp_current)->p_internals->b_attached
1003 && (*pp_current)->i_object_type == i_type )
1009 p_list = NewList( i_count );
1010 pp_current = p_libvlc_global->pp_objects;
1012 for( ; pp_current < pp_end ; pp_current++ )
1014 if( (*pp_current)->p_internals->b_attached
1015 && (*pp_current)->i_object_type == i_type )
1017 ListReplace( p_list, *pp_current, i_index );
1018 if( i_index < i_count ) i_index++;
1024 i_count = CountChildren( p_this, i_type );
1025 p_list = NewList( i_count );
1027 /* Check allocation was successful */
1028 if( p_list->i_count != i_count )
1030 msg_Err( p_this, "list allocation failed!" );
1031 p_list->i_count = 0;
1035 p_list->i_count = 0;
1036 ListChildren( p_list, p_this, i_type );
1040 msg_Err( p_this, "unimplemented!" );
1041 p_list = NewList( 0 );
1045 vlc_mutex_unlock( &structure_lock );
1050 /*****************************************************************************
1051 * DumpCommand: print the current vlc structure
1052 *****************************************************************************
1053 * This function prints either an ASCII tree showing the connections between
1054 * vlc objects, and additional information such as their refcount, thread ID,
1055 * etc. (command "tree"), or the same data as a simple list (command "list").
1056 *****************************************************************************/
1057 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1058 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1060 libvlc_global_data_t *p_libvlc_global = vlc_global();
1062 (void)oldval; (void)p_data;
1063 if( *psz_cmd == 'l' )
1065 vlc_mutex_lock( &structure_lock );
1067 vlc_object_t **pp_current, **pp_end;
1069 pp_current = p_libvlc_global->pp_objects;
1070 pp_end = pp_current + p_libvlc_global->i_objects;
1072 for( ; pp_current < pp_end ; pp_current++ )
1074 if( (*pp_current)->p_internals->b_attached )
1076 PrintObject( *pp_current, "" );
1080 printf( " o %.8i %s (not attached)\n",
1081 (*pp_current)->i_object_id,
1082 (*pp_current)->psz_object_type );
1086 vlc_mutex_unlock( &structure_lock );
1090 vlc_object_t *p_object = NULL;
1092 if( *newval.psz_string )
1095 int i_id = strtol( newval.psz_string, &end, 0 );
1096 if( end != newval.psz_string )
1097 p_object = vlc_object_get( i_id );
1100 /* try using the object's name to find it */
1101 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1104 /* Look in p_libvlc's children tree */
1105 p_object = vlc_object_find_name( p_libvlc,
1108 vlc_object_release( p_libvlc );
1112 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1113 p_object = vlc_object_find_name( p_this,
1125 vlc_mutex_lock( &structure_lock );
1127 if( *psz_cmd == 't' )
1129 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1132 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1135 DumpStructure( p_object, 0, psz_foo );
1137 else if( *psz_cmd == 'v' )
1142 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1144 PrintObject( p_object, "" );
1146 if( !p_object->p_internals->i_vars )
1147 printf( " `-o No variables\n" );
1148 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1150 variable_t *p_var = p_object->p_internals->p_vars + i;
1152 const char *psz_type = "unknown";
1153 switch( p_var->i_type & VLC_VAR_TYPE )
1155 #define MYCASE( type, nice ) \
1156 case VLC_VAR_ ## type: \
1159 MYCASE( VOID, "void" );
1160 MYCASE( BOOL, "bool" );
1161 MYCASE( INTEGER, "integer" );
1162 MYCASE( HOTKEY, "hotkey" );
1163 MYCASE( STRING, "string" );
1164 MYCASE( MODULE, "module" );
1165 MYCASE( FILE, "file" );
1166 MYCASE( DIRECTORY, "directory" );
1167 MYCASE( VARIABLE, "variable" );
1168 MYCASE( FLOAT, "float" );
1169 MYCASE( TIME, "time" );
1170 MYCASE( ADDRESS, "address" );
1171 MYCASE( MUTEX, "mutex" );
1172 MYCASE( LIST, "list" );
1175 printf( " %c-o \"%s\" (%s",
1176 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1177 p_var->psz_name, psz_type );
1178 if( p_var->psz_text )
1179 printf( ", %s", p_var->psz_text );
1181 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1182 printf( ", command" );
1183 if( p_var->i_entries )
1184 printf( ", %d callbacks", p_var->i_entries );
1185 switch( p_var->i_type & 0x00f0 )
1191 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1193 case VLC_VAR_INTEGER:
1194 printf( ": %d", p_var->val.i_int );
1196 case VLC_VAR_STRING:
1197 printf( ": \"%s\"", p_var->val.psz_string );
1200 printf( ": %f", p_var->val.f_float );
1203 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1205 case VLC_VAR_ADDRESS:
1206 printf( ": %p", p_var->val.p_address );
1216 vlc_mutex_unlock( &structure_lock );
1218 if( *newval.psz_string )
1220 vlc_object_release( p_object );
1227 /*****************************************************************************
1228 * vlc_list_release: free a list previously allocated by vlc_list_find
1229 *****************************************************************************
1230 * This function decreases the refcount of all objects in the list and
1232 *****************************************************************************/
1233 void vlc_list_release( vlc_list_t *p_list )
1237 vlc_mutex_lock( &structure_lock );
1238 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1240 vlc_object_release_locked( p_list->p_values[i_index].p_object );
1242 vlc_mutex_unlock( &structure_lock );
1244 free( p_list->p_values );
1248 /* Following functions are local */
1250 /*****************************************************************************
1251 * FindIndex: find the index of an object in an array of objects
1252 *****************************************************************************
1253 * This function assumes that p_this can be found in pp_objects. It will not
1254 * crash if p_this cannot be found, but will return a wrong value. It is your
1255 * duty to check the return value if you are not certain that the object could
1256 * be found for sure.
1257 *****************************************************************************/
1258 static int FindIndex( vlc_object_t *p_this,
1259 vlc_object_t **pp_objects, int i_count )
1261 int i_middle = i_count / 2;
1268 if( pp_objects[i_middle] == p_this )
1278 /* We take advantage of the sorted array */
1279 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1281 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1282 i_count - i_middle );
1286 return FindIndex( p_this, pp_objects, i_middle );
1290 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1293 vlc_object_t *p_tmp;
1295 switch( i_mode & 0x000f )
1298 p_tmp = p_this->p_parent;
1301 if( p_tmp->i_object_type == i_type )
1303 p_tmp->p_internals->i_refcount++;
1308 return FindObject( p_tmp, i_type, i_mode );
1314 for( i = p_this->i_children; i--; )
1316 p_tmp = p_this->pp_children[i];
1317 if( p_tmp->i_object_type == i_type )
1319 p_tmp->p_internals->i_refcount++;
1322 else if( p_tmp->i_children )
1324 p_tmp = FindObject( p_tmp, i_type, i_mode );
1334 /* Handled in vlc_object_find */
1341 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1342 const char *psz_name,
1346 vlc_object_t *p_tmp;
1348 switch( i_mode & 0x000f )
1351 p_tmp = p_this->p_parent;
1354 if( p_tmp->psz_object_name
1355 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1357 p_tmp->p_internals->i_refcount++;
1362 return FindObjectName( p_tmp, psz_name, i_mode );
1368 for( i = p_this->i_children; i--; )
1370 p_tmp = p_this->pp_children[i];
1371 if( p_tmp->psz_object_name
1372 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1374 p_tmp->p_internals->i_refcount++;
1377 else if( p_tmp->i_children )
1379 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1389 /* Handled in vlc_object_find */
1396 static void DetachObject( vlc_object_t *p_this )
1398 vlc_object_t *p_parent = p_this->p_parent;
1401 /* Remove p_this's parent */
1402 p_this->p_parent = NULL;
1404 /* Remove all of p_parent's children which are p_this */
1405 for( i_index = p_parent->i_children ; i_index-- ; )
1407 if( p_parent->pp_children[i_index] == p_this )
1409 p_parent->i_children--;
1410 for( i = i_index ; i < p_parent->i_children ; i++ )
1412 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1417 if( p_parent->i_children )
1419 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1420 p_parent->i_children * sizeof(vlc_object_t *) );
1424 free( p_parent->pp_children );
1425 p_parent->pp_children = NULL;
1429 /*****************************************************************************
1430 * SetAttachment: recursively set the b_attached flag of a subtree.
1431 *****************************************************************************
1432 * This function is used by the attach and detach functions to propagate
1433 * the b_attached flag in a subtree.
1434 *****************************************************************************/
1435 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1439 for( i_index = p_this->i_children ; i_index-- ; )
1441 SetAttachment( p_this->pp_children[i_index], b_attached );
1444 p_this->p_internals->b_attached = b_attached;
1447 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1449 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1453 if( p_this->psz_object_name )
1455 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1457 psz_name[48] = '\"';
1460 psz_children[0] = '\0';
1461 switch( p_this->i_children )
1466 strcpy( psz_children, ", 1 child" );
1469 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1473 psz_refcount[0] = '\0';
1474 if( p_this->p_internals->i_refcount > 0 )
1475 snprintf( psz_refcount, 19, ", refcount %u",
1476 p_this->p_internals->i_refcount );
1478 psz_thread[0] = '\0';
1479 if( p_this->p_internals->b_thread )
1480 snprintf( psz_thread, 29, " (thread %u)",
1481 #if defined(WIN32) || defined(UNDER_CE)
1482 (unsigned)p_this->p_internals->thread_id.id );
1484 (unsigned)p_this->p_internals->thread_id );
1487 psz_parent[0] = '\0';
1488 if( p_this->p_parent )
1489 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1491 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1492 p_this->i_object_id, p_this->psz_object_type,
1493 psz_name, psz_thread, psz_refcount, psz_children,
1497 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1500 char i_back = psz_foo[i_level];
1501 psz_foo[i_level] = '\0';
1503 PrintObject( p_this, psz_foo );
1505 psz_foo[i_level] = i_back;
1507 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1509 msg_Warn( p_this, "structure tree is too deep" );
1513 for( i = 0 ; i < p_this->i_children ; i++ )
1517 psz_foo[i_level-1] = ' ';
1519 if( psz_foo[i_level-2] == '`' )
1521 psz_foo[i_level-2] = ' ';
1525 if( i == p_this->i_children - 1 )
1527 psz_foo[i_level] = '`';
1531 psz_foo[i_level] = '|';
1534 psz_foo[i_level+1] = '-';
1535 psz_foo[i_level+2] = '\0';
1537 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1541 static vlc_list_t * NewList( int i_count )
1543 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1544 if( p_list == NULL )
1549 p_list->i_count = i_count;
1553 p_list->p_values = NULL;
1557 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1558 if( p_list->p_values == NULL )
1560 p_list->i_count = 0;
1567 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1570 if( p_list == NULL || i_index >= p_list->i_count )
1575 p_object->p_internals->i_refcount++;
1577 p_list->p_values[i_index].p_object = p_object;
1582 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1584 if( p_list == NULL )
1589 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1590 * sizeof( vlc_value_t ) );
1591 if( p_list->p_values == NULL )
1593 p_list->i_count = 0;
1597 p_object->p_internals->i_refcount++;
1599 p_list->p_values[p_list->i_count].p_object = p_object;
1605 static int CountChildren( vlc_object_t *p_this, int i_type )
1607 vlc_object_t *p_tmp;
1610 for( i = 0; i < p_this->i_children; i++ )
1612 p_tmp = p_this->pp_children[i];
1614 if( p_tmp->i_object_type == i_type )
1619 if( p_tmp->i_children )
1621 i_count += CountChildren( p_tmp, i_type );
1628 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1630 vlc_object_t *p_tmp;
1633 for( i = 0; i < p_this->i_children; i++ )
1635 p_tmp = p_this->pp_children[i];
1637 if( p_tmp->i_object_type == i_type )
1639 ListReplace( p_list, p_tmp, p_list->i_count++ );
1642 if( p_tmp->i_children )
1644 ListChildren( p_list, p_tmp, i_type );