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 typedef enum vlc_lock_state_t
84 static int DumpCommand( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
87 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
88 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
89 static void DetachObject ( vlc_object_t * );
90 static void PrintObject ( vlc_object_t *, const char * );
91 static void DumpStructure ( vlc_object_t *, int, char * );
92 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
93 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
95 static vlc_list_t * NewList ( int );
96 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
97 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
98 static int CountChildren ( vlc_object_t *, int );
99 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
101 static void vlc_object_destroy( vlc_object_t *p_this );
102 static void vlc_object_release_locked( vlc_object_t *p_this, vlc_lock_state_t locked );
104 /*****************************************************************************
105 * Local structure lock
106 *****************************************************************************/
107 static vlc_mutex_t structure_lock;
108 static vlc_object_internals_t global_internals;
110 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
111 int i_type, const char *psz_type )
114 vlc_object_internals_t *p_priv;
116 if( i_type == VLC_OBJECT_GLOBAL )
119 p_priv = &global_internals;
120 memset( p_priv, 0, sizeof( *p_priv ) );
124 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
128 p_new = (vlc_object_t *)(p_priv + 1);
131 p_new->p_internals = p_priv;
132 p_new->i_object_type = i_type;
133 p_new->psz_object_type = psz_type;
135 p_new->psz_object_name = NULL;
137 p_new->b_die = VLC_FALSE;
138 p_new->b_error = VLC_FALSE;
139 p_new->b_dead = VLC_FALSE;
140 p_priv->b_attached = VLC_FALSE;
141 p_new->b_force = VLC_FALSE;
143 p_new->psz_header = NULL;
145 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
146 p_new->i_flags |= OBJECT_FLAGS_NODBG;
147 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
148 p_new->i_flags |= OBJECT_FLAGS_QUIET;
149 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
150 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
152 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
154 if( !p_priv->p_vars )
156 if( i_type != VLC_OBJECT_GLOBAL )
161 if( i_type == VLC_OBJECT_GLOBAL )
163 /* If i_type is global, then p_new is actually p_libvlc_global */
164 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
165 p_new->p_libvlc = NULL;
167 p_libvlc_global->i_counter = 0;
168 p_new->i_object_id = 0;
170 p_libvlc_global->i_objects = 1;
171 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
172 p_libvlc_global->pp_objects[0] = p_new;
173 p_priv->b_attached = VLC_TRUE;
177 libvlc_global_data_t *p_libvlc_global = vlc_global();
178 if( i_type == VLC_OBJECT_LIBVLC )
180 p_new->p_libvlc = (libvlc_int_t*)p_new;
181 p_priv->b_attached = VLC_TRUE;
185 p_new->p_libvlc = p_this->p_libvlc;
188 vlc_mutex_lock( &structure_lock );
190 p_libvlc_global->i_counter++;
191 p_new->i_object_id = p_libvlc_global->i_counter;
193 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
194 * useless to try and recover anything if pp_objects gets smashed. */
195 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
198 vlc_mutex_unlock( &structure_lock );
201 p_priv->i_refcount = 1;
202 p_new->p_parent = NULL;
203 p_new->pp_children = NULL;
204 p_new->i_children = 0;
206 p_new->p_private = NULL;
208 /* Initialize mutexes and condvars */
209 vlc_mutex_init( p_new, &p_new->object_lock );
210 vlc_cond_init( p_new, &p_new->object_wait );
211 vlc_mutex_init( p_new, &p_priv->var_lock );
212 vlc_spin_init( &p_priv->spin );
213 p_priv->pipes[0] = p_priv->pipes[1] = -1;
215 if( i_type == VLC_OBJECT_GLOBAL )
217 vlc_mutex_init( p_new, &structure_lock );
220 if( i_type == VLC_OBJECT_LIBVLC )
222 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
223 var_AddCallback( p_new, "list", DumpCommand, NULL );
224 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
225 var_AddCallback( p_new, "tree", DumpCommand, NULL );
226 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
227 var_AddCallback( p_new, "vars", DumpCommand, NULL );
235 * Allocates and initializes a vlc object.
237 * @param i_type known object type (all of them are negative integer values),
238 * or object byte size (always positive).
240 * @return the new object, or NULL on error.
242 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
244 const char * psz_type;
249 case VLC_OBJECT_GLOBAL:
250 i_size = sizeof(libvlc_global_data_t);
253 case VLC_OBJECT_LIBVLC:
254 i_size = sizeof(libvlc_int_t);
257 case VLC_OBJECT_INTF:
258 i_size = sizeof(intf_thread_t);
259 psz_type = "interface";
261 case VLC_OBJECT_DIALOGS:
262 i_size = sizeof(intf_thread_t);
263 psz_type = "dialogs";
265 case VLC_OBJECT_PLAYLIST:
266 i_size = sizeof(playlist_t);
267 psz_type = "playlist";
270 i_size = sizeof(services_discovery_t);
271 psz_type = "services discovery";
273 case VLC_OBJECT_INPUT:
274 i_size = sizeof(input_thread_t);
277 case VLC_OBJECT_DEMUX:
278 i_size = sizeof(demux_t);
281 case VLC_OBJECT_ACCESS:
282 i_size = sizeof(access_t);
285 case VLC_OBJECT_DECODER:
286 i_size = sizeof(decoder_t);
287 psz_type = "decoder";
289 case VLC_OBJECT_PACKETIZER:
290 i_size = sizeof(decoder_t);
291 psz_type = "packetizer";
293 case VLC_OBJECT_ENCODER:
294 i_size = sizeof(encoder_t);
295 psz_type = "encoder";
297 case VLC_OBJECT_FILTER:
298 i_size = sizeof(filter_t);
301 case VLC_OBJECT_VOUT:
302 i_size = sizeof(vout_thread_t);
303 psz_type = "video output";
306 i_size = sizeof(spu_t);
307 psz_type = "subpicture";
309 case VLC_OBJECT_AOUT:
310 i_size = sizeof(aout_instance_t);
311 psz_type = "audio output";
313 case VLC_OBJECT_SOUT:
314 i_size = sizeof(sout_instance_t);
315 psz_type = "stream output";
318 i_size = sizeof( vlm_t );
319 psz_type = "vlm dameon";
322 i_size = sizeof( vod_t );
323 psz_type = "vod server";
326 i_size = sizeof( xml_t );
329 case VLC_OBJECT_OPENGL:
330 i_size = sizeof( vout_thread_t );
333 case VLC_OBJECT_ANNOUNCE:
334 i_size = sizeof( announce_handler_t );
335 psz_type = "announce";
337 case VLC_OBJECT_META_ENGINE:
338 i_size = sizeof( meta_engine_t );
339 psz_type = "meta engine";
341 case VLC_OBJECT_OSDMENU:
342 i_size = sizeof( osd_menu_t );
343 psz_type = "osd menu";
346 i_size = i_type > (int)sizeof(vlc_object_t)
347 ? i_type : (int)sizeof(vlc_object_t);
348 i_type = VLC_OBJECT_GENERIC;
349 psz_type = "generic";
353 return vlc_custom_create( p_this, i_size, i_type, psz_type );
358 ****************************************************************************
359 * Destroy a vlc object (Internal)
361 * This function destroys an object that has been previously allocated with
362 * vlc_object_create. The object's refcount must be zero and it must not be
363 * attached to other objects in any way.
364 *****************************************************************************/
365 static void vlc_object_destroy( vlc_object_t *p_this )
367 vlc_object_internals_t *p_priv = vlc_internals( p_this );
369 /* FIXME: ugly hack - we cannot use the message queue after
371 vlc_object_t *logger = p_this;
372 if( (vlc_object_t *)p_this->p_libvlc == p_this )
376 if( p_this->i_children )
380 fprintf( stderr, "ERROR: cannot delete object (%i, %s) with %d children\n",
381 p_this->i_object_id, p_this->psz_object_name, p_this->i_children );
383 for( i = 0; i < p_this->i_children; i++ )
385 fprintf( stderr, "ERROR: Remaining children object (id:%i, type:%s, name:%s)\n",
386 p_this->pp_children[i]->i_object_id,
387 p_this->pp_children[i]->psz_object_type,
388 p_this->pp_children[i]->psz_object_name );
394 if( p_this->p_parent )
396 fprintf( stderr, "ERROR: cannot delete object (id:%i, type:%s, name:%s) with a parent (id:%i, type:%s, name:%s)\n",
398 p_this->psz_object_type,
399 p_this->psz_object_name,
400 p_this->p_parent->i_object_id,
401 p_this->p_parent->psz_object_type,
402 p_this->p_parent->psz_object_name );
407 /* Destroy the associated variables, starting from the end so that
408 * no memmove calls have to be done. */
409 while( p_priv->i_vars )
411 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
414 free( p_priv->p_vars );
415 vlc_mutex_destroy( &p_priv->var_lock );
417 if( p_this->psz_header ) free( p_this->psz_header );
419 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
421 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
424 if( p_global->i_objects > 0 )
427 for( i = 0; i < p_global->i_objects; i++ )
429 /* We are leaking this object */
431 "ERROR: We are leaking object (id:%i, type:%s, name:%s)\n",
432 p_global->pp_objects[i]->i_object_id,
433 p_global->pp_objects[i]->psz_object_type,
434 p_global->pp_objects[i]->psz_object_name );
437 /* Strongly abort, cause we want these to be fixed */
441 /* We are the global object ... no need to lock. */
442 free( p_global->pp_objects );
443 p_global->pp_objects = NULL;
445 vlc_mutex_destroy( &structure_lock );
448 #if defined(WIN32) || defined(UNDER_CE)
449 /* if object has an associated thread, close it now */
450 if( p_priv->thread_id.hThread )
451 CloseHandle(p_priv->thread_id.hThread);
454 vlc_mutex_destroy( &p_this->object_lock );
455 vlc_cond_destroy( &p_this->object_wait );
456 vlc_spin_destroy( &p_priv->spin );
457 if( p_priv->pipes[1] != -1 )
458 close( p_priv->pipes[1] );
459 if( p_priv->pipes[0] != -1 )
460 close( p_priv->pipes[0] );
462 /* global is not dynamically allocated by vlc_object_create */
463 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
468 /** Inter-object signaling */
470 void __vlc_object_lock( vlc_object_t *obj )
472 vlc_mutex_lock( &obj->object_lock );
475 void __vlc_object_unlock( vlc_object_t *obj )
477 vlc_assert_locked( &obj->object_lock );
478 vlc_mutex_unlock( &obj->object_lock );
482 # include <winsock2.h>
483 # include <ws2tcpip.h>
486 * select()-able pipes emulated using Winsock
488 static int pipe (int fd[2])
491 int addrlen = sizeof (addr);
493 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
494 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
495 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
498 memset (&addr, 0, sizeof (addr));
499 addr.sin_family = AF_INET;
500 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
501 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
502 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
504 || connect (c, (PSOCKADDR)&addr, addrlen))
507 a = accept (l, NULL, NULL);
508 if (a == INVALID_SOCKET)
519 if (l != INVALID_SOCKET)
521 if (c != INVALID_SOCKET)
527 #define read( a, b, c ) recv (a, b, c, 0)
529 #define write( a, b, c ) send (a, b, c, 0)
531 #define close( a ) closesocket (a)
535 * Returns the readable end of a pipe that becomes readable once termination
536 * of the object is requested (vlc_object_kill()).
537 * This can be used to wake-up out of a select() or poll() event loop, such
538 * typically when doing network I/O.
540 * Note that the pipe will remain the same for the lifetime of the object.
541 * DO NOT read the pipe nor close it yourself. Ever.
543 * @param obj object that would be "killed"
544 * @return a readable pipe descriptor, or -1 on error.
546 int __vlc_object_waitpipe( vlc_object_t *obj )
548 int pfd[2] = { -1, -1 };
549 struct vlc_object_internals_t *internals = obj->p_internals;
550 vlc_bool_t killed = VLC_FALSE;
552 vlc_spin_lock (&internals->spin);
553 if (internals->pipes[0] == -1)
555 /* This can only ever happen if someone killed us without locking: */
556 assert (internals->pipes[1] == -1);
557 vlc_spin_unlock (&internals->spin);
562 vlc_spin_lock (&internals->spin);
563 if (internals->pipes[0] == -1)
565 internals->pipes[0] = pfd[0];
566 internals->pipes[1] = pfd[1];
567 pfd[0] = pfd[1] = -1;
571 vlc_spin_unlock (&internals->spin);
575 /* Race condition: vlc_object_kill() already invoked! */
578 vlc_spin_lock (&internals->spin);
579 fd = internals->pipes[1];
580 internals->pipes[1] = -1;
581 vlc_spin_unlock (&internals->spin);
583 msg_Dbg (obj, "waitpipe: object already dying");
588 /* Race condition: two threads call pipe() - unlikely */
594 return internals->pipes[0];
599 * Waits for the object to be signaled (using vlc_object_signal()).
600 * If the object already has a signal pending, this function will return
601 * immediately. It is asserted that the caller holds the object lock.
603 * @return true if the object is dying and should terminate.
605 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
607 vlc_assert_locked( &obj->object_lock );
608 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
614 * Waits for the object to be signaled (using vlc_object_signal()), or for
616 * If the object already has a signal pending, this function will return
617 * immediately. It is asserted that the caller holds the object lock.
619 * @return negative if the object is dying and should terminate,
620 * positive if the the object has been signaled but is not dying,
621 * 0 if timeout has been reached.
623 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
627 vlc_assert_locked( &obj->object_lock );
628 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
629 if( v == 0 ) /* signaled */
630 return obj->b_die ? -1 : 1;
636 * Checks whether an object has been "killed".
637 * The object lock must be held.
639 * Typical code for an object thread could be:
641 vlc_object_lock (self);
643 while (vlc_object_alive (self))
647 if (vlc_object_wait (self))
652 ...deinitialization...
653 vlc_object_unlock (self);
656 * @return true iff the object has not been killed yet
658 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
660 vlc_assert_locked( &obj->object_lock );
666 * Signals an object for which the lock is held.
668 void __vlc_object_signal_unlocked( vlc_object_t *obj )
670 vlc_assert_locked (&obj->object_lock);
671 vlc_cond_signal( &obj->object_wait );
676 * Requests termination of an object.
677 * If the object is LibVLC, also request to terminate all its children.
679 void __vlc_object_kill( vlc_object_t *p_this )
681 struct vlc_object_internals_t *internals = p_this->p_internals;
684 vlc_mutex_lock( &p_this->object_lock );
685 p_this->b_die = VLC_TRUE;
687 vlc_spin_lock (&internals->spin);
688 fd = internals->pipes[1];
689 internals->pipes[1] = -1;
690 vlc_spin_unlock (&internals->spin);
694 msg_Dbg (p_this, "waitpipe: object killed");
698 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
699 for( int i = 0; i < p_this->i_children ; i++ )
700 vlc_object_kill( p_this->pp_children[i] );
702 vlc_object_signal_unlocked( p_this );
703 vlc_mutex_unlock( &p_this->object_lock );
708 * find an object given its ID
710 * This function looks for the object whose i_object_id field is i_id. We
711 * use a dichotomy so that lookups are in log2(n).
712 *****************************************************************************/
713 void * vlc_object_get( int i_id )
716 vlc_object_t **pp_objects;
717 libvlc_global_data_t *p_libvlc_global = vlc_global();
719 vlc_mutex_lock( &structure_lock );
721 pp_objects = p_libvlc_global->pp_objects;
723 /* Perform our dichotomy */
724 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
726 i_middle = i_max / 2;
728 if( pp_objects[i_middle]->i_object_id > i_id )
732 else if( pp_objects[i_middle]->i_object_id < i_id )
736 pp_objects += i_middle;
741 /* This happens when there are only two remaining objects */
742 if( pp_objects[i_middle+1]->i_object_id == i_id )
744 vlc_mutex_unlock( &structure_lock );
745 pp_objects[i_middle+1]->p_internals->i_refcount++;
746 return pp_objects[i_middle+1];
753 vlc_mutex_unlock( &structure_lock );
754 pp_objects[i_middle]->p_internals->i_refcount++;
755 return pp_objects[i_middle];
760 /* this means that i_max == i_middle, and since we have already
761 * tested pp_objects[i_middle]), p_found is properly set. */
766 vlc_mutex_unlock( &structure_lock );
771 ****************************************************************************
772 * find a typed object and increment its refcount
773 *****************************************************************************
774 * This function recursively looks for a given object type. i_mode can be one
775 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
776 *****************************************************************************/
777 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
779 vlc_object_t *p_found;
781 vlc_mutex_lock( &structure_lock );
783 /* If we are of the requested type ourselves, don't look further */
784 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
786 p_this->p_internals->i_refcount++;
787 vlc_mutex_unlock( &structure_lock );
791 /* Otherwise, recursively look for the object */
792 if( (i_mode & 0x000f) == FIND_ANYWHERE )
794 vlc_object_t *p_root = p_this;
797 while( p_root->p_parent != NULL &&
798 p_root != VLC_OBJECT( p_this->p_libvlc ) )
800 p_root = p_root->p_parent;
803 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
804 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
806 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
807 i_type, (i_mode & ~0x000f)|FIND_CHILD );
812 p_found = FindObject( p_this, i_type, i_mode );
815 vlc_mutex_unlock( &structure_lock );
821 ****************************************************************************
822 * find a named object and increment its refcount
823 *****************************************************************************
824 * This function recursively looks for a given object name. i_mode can be one
825 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
826 *****************************************************************************/
827 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
830 vlc_object_t *p_found;
832 vlc_mutex_lock( &structure_lock );
834 /* If have the requested name ourselves, don't look further */
835 if( !(i_mode & FIND_STRICT)
836 && p_this->psz_object_name
837 && !strcmp( p_this->psz_object_name, psz_name ) )
839 p_this->p_internals->i_refcount++;
840 vlc_mutex_unlock( &structure_lock );
844 /* Otherwise, recursively look for the object */
845 if( (i_mode & 0x000f) == FIND_ANYWHERE )
847 vlc_object_t *p_root = p_this;
850 while( p_root->p_parent != NULL &&
851 p_root != VLC_OBJECT( p_this->p_libvlc ) )
853 p_root = p_root->p_parent;
856 p_found = FindObjectName( p_root, psz_name,
857 (i_mode & ~0x000f)|FIND_CHILD );
858 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
860 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
861 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
866 p_found = FindObjectName( p_this, psz_name, i_mode );
869 vlc_mutex_unlock( &structure_lock );
875 ****************************************************************************
876 * increment an object refcount
877 *****************************************************************************/
878 void __vlc_object_yield( vlc_object_t *p_this )
880 vlc_mutex_lock( &structure_lock );
881 p_this->p_internals->i_refcount++;
882 vlc_mutex_unlock( &structure_lock );
885 /*****************************************************************************
886 * decrement an object refcount
887 * And destroy the object if its refcount reach zero.
888 *****************************************************************************/
889 void __vlc_object_release( vlc_object_t *p_this )
891 vlc_object_release_locked( p_this, vlc_Unlocked );
894 /* Version without the lock */
895 static void vlc_object_release_locked( vlc_object_t *p_this, vlc_lock_state_t locked )
897 vlc_bool_t b_should_destroy;
899 if(locked == vlc_Unlocked) vlc_mutex_lock( &structure_lock );
901 assert( p_this->p_internals->i_refcount > 0 );
902 p_this->p_internals->i_refcount--;
903 b_should_destroy = (p_this->p_internals->i_refcount == 0);
905 if( b_should_destroy )
907 /* Make sure this object can't be obtained via vlc_find_object now that
909 libvlc_global_data_t *p_libvlc_global = vlc_global();
912 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
913 * useless to try and recover anything if pp_objects gets smashed. */
914 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
915 p_libvlc_global->i_objects );
916 REMOVE_ELEM( p_libvlc_global->pp_objects,
917 p_libvlc_global->i_objects, i_index );
920 if(locked == vlc_Unlocked) vlc_mutex_unlock( &structure_lock );
922 if( b_should_destroy )
923 vlc_object_destroy( p_this );
927 ****************************************************************************
928 * attach object to a parent object
929 *****************************************************************************
930 * This function sets p_this as a child of p_parent, and p_parent as a parent
931 * of p_this. This link can be undone using vlc_object_detach.
932 *****************************************************************************/
933 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
935 if( !p_this ) return;
937 vlc_mutex_lock( &structure_lock );
939 /* Attach the parent to its child */
940 p_this->p_parent = p_parent;
942 /* Attach the child to its parent */
943 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
944 p_parent->i_children, p_this );
946 /* Climb up the tree to see whether we are connected with the root */
947 if( p_parent->p_internals->b_attached )
949 SetAttachment( p_this, VLC_TRUE );
952 vlc_mutex_unlock( &structure_lock );
956 ****************************************************************************
957 * detach object from its parent
958 *****************************************************************************
959 * This function removes all links between an object and its parent.
960 *****************************************************************************/
961 void __vlc_object_detach( vlc_object_t *p_this )
963 if( !p_this ) return;
965 vlc_mutex_lock( &structure_lock );
966 if( !p_this->p_parent )
968 msg_Err( p_this, "object is not attached" );
969 vlc_mutex_unlock( &structure_lock );
973 /* Climb up the tree to see whether we are connected with the root */
974 if( p_this->p_parent->p_internals->b_attached )
976 SetAttachment( p_this, VLC_FALSE );
979 DetachObject( p_this );
980 vlc_mutex_unlock( &structure_lock );
985 ****************************************************************************
986 * find a list typed objects and increment their refcount
987 *****************************************************************************
988 * This function recursively looks for a given object type. i_mode can be one
989 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
990 *****************************************************************************/
991 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
994 vlc_object_t **pp_current, **pp_end;
995 int i_count = 0, i_index = 0;
996 libvlc_global_data_t *p_libvlc_global = vlc_global();
998 vlc_mutex_lock( &structure_lock );
1000 /* Look for the objects */
1001 switch( i_mode & 0x000f )
1004 pp_current = p_libvlc_global->pp_objects;
1005 pp_end = pp_current + p_libvlc_global->i_objects;
1007 for( ; pp_current < pp_end ; pp_current++ )
1009 if( (*pp_current)->p_internals->b_attached
1010 && (*pp_current)->i_object_type == i_type )
1016 p_list = NewList( i_count );
1017 pp_current = p_libvlc_global->pp_objects;
1019 for( ; pp_current < pp_end ; pp_current++ )
1021 if( (*pp_current)->p_internals->b_attached
1022 && (*pp_current)->i_object_type == i_type )
1024 ListReplace( p_list, *pp_current, i_index );
1025 if( i_index < i_count ) i_index++;
1031 i_count = CountChildren( p_this, i_type );
1032 p_list = NewList( i_count );
1034 /* Check allocation was successful */
1035 if( p_list->i_count != i_count )
1037 msg_Err( p_this, "list allocation failed!" );
1038 p_list->i_count = 0;
1042 p_list->i_count = 0;
1043 ListChildren( p_list, p_this, i_type );
1047 msg_Err( p_this, "unimplemented!" );
1048 p_list = NewList( 0 );
1052 vlc_mutex_unlock( &structure_lock );
1057 /*****************************************************************************
1058 * DumpCommand: print the current vlc structure
1059 *****************************************************************************
1060 * This function prints either an ASCII tree showing the connections between
1061 * vlc objects, and additional information such as their refcount, thread ID,
1062 * etc. (command "tree"), or the same data as a simple list (command "list").
1063 *****************************************************************************/
1064 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1065 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1067 libvlc_global_data_t *p_libvlc_global = vlc_global();
1069 (void)oldval; (void)p_data;
1070 if( *psz_cmd == 'l' )
1072 vlc_mutex_lock( &structure_lock );
1074 vlc_object_t **pp_current, **pp_end;
1076 pp_current = p_libvlc_global->pp_objects;
1077 pp_end = pp_current + p_libvlc_global->i_objects;
1079 for( ; pp_current < pp_end ; pp_current++ )
1081 if( (*pp_current)->p_internals->b_attached )
1083 PrintObject( *pp_current, "" );
1087 printf( " o %.8i %s (not attached)\n",
1088 (*pp_current)->i_object_id,
1089 (*pp_current)->psz_object_type );
1093 vlc_mutex_unlock( &structure_lock );
1097 vlc_object_t *p_object = NULL;
1099 if( *newval.psz_string )
1102 int i_id = strtol( newval.psz_string, &end, 0 );
1103 if( end != newval.psz_string )
1104 p_object = vlc_object_get( i_id );
1107 /* try using the object's name to find it */
1108 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1111 /* Look in p_libvlc's children tree */
1112 p_object = vlc_object_find_name( p_libvlc,
1115 vlc_object_release( p_libvlc );
1119 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1120 p_object = vlc_object_find_name( p_this,
1132 vlc_mutex_lock( &structure_lock );
1134 if( *psz_cmd == 't' )
1136 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1139 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1142 DumpStructure( p_object, 0, psz_foo );
1144 else if( *psz_cmd == 'v' )
1149 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1151 PrintObject( p_object, "" );
1153 if( !p_object->p_internals->i_vars )
1154 printf( " `-o No variables\n" );
1155 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1157 variable_t *p_var = p_object->p_internals->p_vars + i;
1159 const char *psz_type = "unknown";
1160 switch( p_var->i_type & VLC_VAR_TYPE )
1162 #define MYCASE( type, nice ) \
1163 case VLC_VAR_ ## type: \
1166 MYCASE( VOID, "void" );
1167 MYCASE( BOOL, "bool" );
1168 MYCASE( INTEGER, "integer" );
1169 MYCASE( HOTKEY, "hotkey" );
1170 MYCASE( STRING, "string" );
1171 MYCASE( MODULE, "module" );
1172 MYCASE( FILE, "file" );
1173 MYCASE( DIRECTORY, "directory" );
1174 MYCASE( VARIABLE, "variable" );
1175 MYCASE( FLOAT, "float" );
1176 MYCASE( TIME, "time" );
1177 MYCASE( ADDRESS, "address" );
1178 MYCASE( MUTEX, "mutex" );
1179 MYCASE( LIST, "list" );
1182 printf( " %c-o \"%s\" (%s",
1183 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1184 p_var->psz_name, psz_type );
1185 if( p_var->psz_text )
1186 printf( ", %s", p_var->psz_text );
1188 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1189 printf( ", command" );
1190 if( p_var->i_entries )
1191 printf( ", %d callbacks", p_var->i_entries );
1192 switch( p_var->i_type & 0x00f0 )
1198 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1200 case VLC_VAR_INTEGER:
1201 printf( ": %d", p_var->val.i_int );
1203 case VLC_VAR_STRING:
1204 printf( ": \"%s\"", p_var->val.psz_string );
1207 printf( ": %f", p_var->val.f_float );
1210 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1212 case VLC_VAR_ADDRESS:
1213 printf( ": %p", p_var->val.p_address );
1223 vlc_mutex_unlock( &structure_lock );
1225 if( *newval.psz_string )
1227 vlc_object_release( p_object );
1234 /*****************************************************************************
1235 * vlc_list_release: free a list previously allocated by vlc_list_find
1236 *****************************************************************************
1237 * This function decreases the refcount of all objects in the list and
1239 *****************************************************************************/
1240 void vlc_list_release( vlc_list_t *p_list )
1244 vlc_mutex_lock( &structure_lock );
1245 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1247 vlc_object_release_locked( p_list->p_values[i_index].p_object, vlc_Locked );
1249 vlc_mutex_unlock( &structure_lock );
1251 free( p_list->p_values );
1255 /* Following functions are local */
1257 /*****************************************************************************
1258 * FindIndex: find the index of an object in an array of objects
1259 *****************************************************************************
1260 * This function assumes that p_this can be found in pp_objects. It will not
1261 * crash if p_this cannot be found, but will return a wrong value. It is your
1262 * duty to check the return value if you are not certain that the object could
1263 * be found for sure.
1264 *****************************************************************************/
1265 static int FindIndex( vlc_object_t *p_this,
1266 vlc_object_t **pp_objects, int i_count )
1268 int i_middle = i_count / 2;
1275 if( pp_objects[i_middle] == p_this )
1285 /* We take advantage of the sorted array */
1286 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1288 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1289 i_count - i_middle );
1293 return FindIndex( p_this, pp_objects, i_middle );
1297 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1300 vlc_object_t *p_tmp;
1302 switch( i_mode & 0x000f )
1305 p_tmp = p_this->p_parent;
1308 if( p_tmp->i_object_type == i_type )
1310 p_tmp->p_internals->i_refcount++;
1315 return FindObject( p_tmp, i_type, i_mode );
1321 for( i = p_this->i_children; i--; )
1323 p_tmp = p_this->pp_children[i];
1324 if( p_tmp->i_object_type == i_type )
1326 p_tmp->p_internals->i_refcount++;
1329 else if( p_tmp->i_children )
1331 p_tmp = FindObject( p_tmp, i_type, i_mode );
1341 /* Handled in vlc_object_find */
1348 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1349 const char *psz_name,
1353 vlc_object_t *p_tmp;
1355 switch( i_mode & 0x000f )
1358 p_tmp = p_this->p_parent;
1361 if( p_tmp->psz_object_name
1362 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1364 p_tmp->p_internals->i_refcount++;
1369 return FindObjectName( p_tmp, psz_name, i_mode );
1375 for( i = p_this->i_children; i--; )
1377 p_tmp = p_this->pp_children[i];
1378 if( p_tmp->psz_object_name
1379 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1381 p_tmp->p_internals->i_refcount++;
1384 else if( p_tmp->i_children )
1386 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1396 /* Handled in vlc_object_find */
1403 static void DetachObject( vlc_object_t *p_this )
1405 vlc_object_t *p_parent = p_this->p_parent;
1408 /* Remove p_this's parent */
1409 p_this->p_parent = NULL;
1411 /* Remove all of p_parent's children which are p_this */
1412 for( i_index = p_parent->i_children ; i_index-- ; )
1414 if( p_parent->pp_children[i_index] == p_this )
1416 p_parent->i_children--;
1417 for( i = i_index ; i < p_parent->i_children ; i++ )
1419 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1424 if( p_parent->i_children )
1426 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1427 p_parent->i_children * sizeof(vlc_object_t *) );
1431 free( p_parent->pp_children );
1432 p_parent->pp_children = NULL;
1436 /*****************************************************************************
1437 * SetAttachment: recursively set the b_attached flag of a subtree.
1438 *****************************************************************************
1439 * This function is used by the attach and detach functions to propagate
1440 * the b_attached flag in a subtree.
1441 *****************************************************************************/
1442 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1446 for( i_index = p_this->i_children ; i_index-- ; )
1448 SetAttachment( p_this->pp_children[i_index], b_attached );
1451 p_this->p_internals->b_attached = b_attached;
1454 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1456 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1460 if( p_this->psz_object_name )
1462 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1464 psz_name[48] = '\"';
1467 psz_children[0] = '\0';
1468 switch( p_this->i_children )
1473 strcpy( psz_children, ", 1 child" );
1476 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1480 psz_refcount[0] = '\0';
1481 if( p_this->p_internals->i_refcount > 0 )
1482 snprintf( psz_refcount, 19, ", refcount %u",
1483 p_this->p_internals->i_refcount );
1485 psz_thread[0] = '\0';
1486 if( p_this->p_internals->b_thread )
1487 snprintf( psz_thread, 29, " (thread %u)",
1488 #if defined(WIN32) || defined(UNDER_CE)
1489 (unsigned)p_this->p_internals->thread_id.id );
1491 (unsigned)p_this->p_internals->thread_id );
1494 psz_parent[0] = '\0';
1495 if( p_this->p_parent )
1496 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1498 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1499 p_this->i_object_id, p_this->psz_object_type,
1500 psz_name, psz_thread, psz_refcount, psz_children,
1504 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1507 char i_back = psz_foo[i_level];
1508 psz_foo[i_level] = '\0';
1510 PrintObject( p_this, psz_foo );
1512 psz_foo[i_level] = i_back;
1514 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1516 msg_Warn( p_this, "structure tree is too deep" );
1520 for( i = 0 ; i < p_this->i_children ; i++ )
1524 psz_foo[i_level-1] = ' ';
1526 if( psz_foo[i_level-2] == '`' )
1528 psz_foo[i_level-2] = ' ';
1532 if( i == p_this->i_children - 1 )
1534 psz_foo[i_level] = '`';
1538 psz_foo[i_level] = '|';
1541 psz_foo[i_level+1] = '-';
1542 psz_foo[i_level+2] = '\0';
1544 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1548 static vlc_list_t * NewList( int i_count )
1550 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1551 if( p_list == NULL )
1556 p_list->i_count = i_count;
1560 p_list->p_values = NULL;
1564 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1565 if( p_list->p_values == NULL )
1567 p_list->i_count = 0;
1574 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1577 if( p_list == NULL || i_index >= p_list->i_count )
1582 p_object->p_internals->i_refcount++;
1584 p_list->p_values[i_index].p_object = p_object;
1589 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1591 if( p_list == NULL )
1596 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1597 * sizeof( vlc_value_t ) );
1598 if( p_list->p_values == NULL )
1600 p_list->i_count = 0;
1604 p_object->p_internals->i_refcount++;
1606 p_list->p_values[p_list->i_count].p_object = p_object;
1612 static int CountChildren( vlc_object_t *p_this, int i_type )
1614 vlc_object_t *p_tmp;
1617 for( i = 0; i < p_this->i_children; i++ )
1619 p_tmp = p_this->pp_children[i];
1621 if( p_tmp->i_object_type == i_type )
1626 if( p_tmp->i_children )
1628 i_count += CountChildren( p_tmp, i_type );
1635 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1637 vlc_object_t *p_tmp;
1640 for( i = 0; i < p_this->i_children; i++ )
1642 p_tmp = p_this->pp_children[i];
1644 if( p_tmp->i_object_type == i_type )
1646 ListReplace( p_list, p_tmp, p_list->i_count++ );
1649 if( p_tmp->i_children )
1651 ListChildren( p_list, p_tmp, i_type );