1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.39 2003/09/18 17:54:02 zorglub Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
36 # include <stdlib.h> /* realloc() */
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
43 #include "vlc_video.h"
44 #include "video_output.h"
46 #include "audio_output.h"
47 #include "aout_internal.h"
48 #include "stream_output.h"
50 #include "vlc_playlist.h"
51 #include "vlc_interface.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static int DumpCommand( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
61 static void DetachObject ( vlc_object_t * );
62 static void PrintObject ( vlc_object_t *, const char * );
63 static void DumpStructure ( vlc_object_t *, int, char * );
64 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
65 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
67 static vlc_list_t * NewList ( int );
68 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
69 static void ListAppend ( vlc_list_t *, vlc_object_t * );
70 static int CountChildren ( vlc_object_t *, int );
71 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
73 /*****************************************************************************
74 * Local structure lock
75 *****************************************************************************/
76 static vlc_mutex_t structure_lock;
78 /*****************************************************************************
79 * vlc_object_create: initialize a vlc object
80 *****************************************************************************
81 * This function allocates memory for a vlc object and initializes it. If
82 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
83 * so on, vlc_object_create will use its value for the object size.
84 *****************************************************************************/
87 * Initialize a vlc object
89 * This function allocates memory for a vlc object and initializes it. If
90 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
91 * so on, vlc_object_create will use its value for the object size.
93 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
101 case VLC_OBJECT_ROOT:
102 i_size = sizeof(libvlc_t);
106 i_size = sizeof(vlc_t);
109 case VLC_OBJECT_MODULE:
110 i_size = sizeof(module_t);
113 case VLC_OBJECT_INTF:
114 i_size = sizeof(intf_thread_t);
115 psz_type = "interface";
117 case VLC_OBJECT_PLAYLIST:
118 i_size = sizeof(playlist_t);
119 psz_type = "playlist";
121 case VLC_OBJECT_INPUT:
122 i_size = sizeof(input_thread_t);
125 case VLC_OBJECT_DECODER:
126 i_size = sizeof(decoder_t);
127 psz_type = "decoder";
129 case VLC_OBJECT_DECODER_FIFO: /* tmp for backward compat */
130 i_size = sizeof(decoder_fifo_t);
131 psz_type = "decoder";
133 case VLC_OBJECT_VOUT:
134 i_size = sizeof(vout_thread_t);
135 psz_type = "video output";
137 case VLC_OBJECT_AOUT:
138 i_size = sizeof(aout_instance_t);
139 psz_type = "audio output";
141 case VLC_OBJECT_SOUT:
142 i_size = sizeof(sout_instance_t);
143 psz_type = "stream output";
145 case VLC_OBJECT_HTTPD:
146 i_size = sizeof( httpd_t );
147 psz_type = "http daemon";
151 ? i_type > (int)sizeof(vlc_object_t)
152 ? i_type : (int)sizeof(vlc_object_t)
153 : (int)sizeof(vlc_object_t);
154 i_type = VLC_OBJECT_GENERIC;
155 psz_type = "generic";
159 if( i_type == VLC_OBJECT_ROOT )
165 p_new = malloc( i_size );
172 memset( p_new, 0, i_size );
175 p_new->i_object_type = i_type;
176 p_new->psz_object_type = psz_type;
178 p_new->psz_object_name = NULL;
180 p_new->b_die = VLC_FALSE;
181 p_new->b_error = VLC_FALSE;
182 p_new->b_dead = VLC_FALSE;
183 p_new->b_attached = VLC_FALSE;
186 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
194 if( i_type == VLC_OBJECT_ROOT )
196 /* If i_type is root, then p_new is actually p_libvlc */
197 p_new->p_libvlc = (libvlc_t*)p_new;
200 p_new->p_libvlc->i_counter = 0;
201 p_new->i_object_id = 0;
203 p_new->p_libvlc->i_objects = 1;
204 p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
205 p_new->p_libvlc->pp_objects[0] = p_new;
206 p_new->b_attached = VLC_TRUE;
210 p_new->p_libvlc = p_this->p_libvlc;
211 p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
214 vlc_mutex_lock( &structure_lock );
216 p_new->p_libvlc->i_counter++;
217 p_new->i_object_id = p_new->p_libvlc->i_counter;
219 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
220 * useless to try and recover anything if pp_objects gets smashed. */
221 INSERT_ELEM( p_new->p_libvlc->pp_objects,
222 p_new->p_libvlc->i_objects,
223 p_new->p_libvlc->i_objects,
226 vlc_mutex_unlock( &structure_lock );
229 p_new->i_refcount = 0;
230 p_new->p_parent = NULL;
231 p_new->pp_children = NULL;
232 p_new->i_children = 0;
234 p_new->p_private = NULL;
236 /* Initialize mutexes and condvars */
237 vlc_mutex_init( p_new, &p_new->object_lock );
238 vlc_cond_init( p_new, &p_new->object_wait );
239 vlc_mutex_init( p_new, &p_new->var_lock );
241 if( i_type == VLC_OBJECT_ROOT )
243 vlc_mutex_init( p_new, &structure_lock );
245 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
246 var_AddCallback( p_new, "list", DumpCommand, NULL );
247 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
248 var_AddCallback( p_new, "tree", DumpCommand, NULL );
255 ****************************************************************************
256 * Destroy a vlc object
258 * This function destroys an object that has been previously allocated with
259 * vlc_object_create. The object's refcount must be zero and it must not be
260 * attached to other objects in any way.
261 *****************************************************************************/
262 void __vlc_object_destroy( vlc_object_t *p_this )
266 if( p_this->i_children )
268 msg_Err( p_this, "cannot delete object with children" );
272 if( p_this->p_parent )
274 msg_Err( p_this, "cannot delete object with a parent" );
278 while( p_this->i_refcount )
282 /* Don't warn immediately ... 100ms seems OK */
285 msg_Warn( p_this, "refcount is %i, delaying before deletion",
286 p_this->i_refcount );
288 else if( i_delay == 12 )
290 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
291 p_this->i_refcount );
293 else if( i_delay == 42 )
295 msg_Err( p_this, "we waited too long, cancelling destruction" );
302 /* Destroy the associated variables, starting from the end so that
303 * no memmove calls have to be done. */
304 while( p_this->i_vars )
306 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
309 free( p_this->p_vars );
310 vlc_mutex_destroy( &p_this->var_lock );
312 if( p_this->i_object_type == VLC_OBJECT_ROOT )
314 /* We are the root object ... no need to lock. */
315 free( p_this->p_libvlc->pp_objects );
316 p_this->p_libvlc->pp_objects = NULL;
317 p_this->p_libvlc->i_objects--;
319 vlc_mutex_destroy( &structure_lock );
325 vlc_mutex_lock( &structure_lock );
327 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
328 * useless to try and recover anything if pp_objects gets smashed. */
329 i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
330 p_this->p_libvlc->i_objects );
331 REMOVE_ELEM( p_this->p_libvlc->pp_objects,
332 p_this->p_libvlc->i_objects, i_index );
334 vlc_mutex_unlock( &structure_lock );
337 vlc_mutex_destroy( &p_this->object_lock );
338 vlc_cond_destroy( &p_this->object_wait );
344 * find an object given its ID
346 * This function looks for the object whose i_object_id field is i_id. We
347 * use a dichotomy so that lookups are in log2(n).
348 *****************************************************************************/
349 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
352 vlc_object_t **pp_objects;
354 vlc_mutex_lock( &structure_lock );
356 pp_objects = p_this->p_libvlc->pp_objects;
358 /* Perform our dichotomy */
359 for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
361 i_middle = i_max / 2;
363 if( pp_objects[i_middle]->i_object_id > i_id )
367 else if( pp_objects[i_middle]->i_object_id < i_id )
371 pp_objects += i_middle;
376 /* This happens when there are only two remaining objects */
377 if( pp_objects[i_middle+1]->i_object_id == i_id )
379 vlc_mutex_unlock( &structure_lock );
380 pp_objects[i_middle+1]->i_refcount++;
381 return pp_objects[i_middle+1];
388 vlc_mutex_unlock( &structure_lock );
389 pp_objects[i_middle]->i_refcount++;
390 return pp_objects[i_middle];
395 /* this means that i_max == i_middle, and since we have already
396 * tested pp_objects[i_middle]), p_found is properly set. */
401 vlc_mutex_unlock( &structure_lock );
406 ****************************************************************************
407 * find a typed object and increment its refcount
408 *****************************************************************************
409 * This function recursively looks for a given object type. i_mode can be one
410 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
411 *****************************************************************************/
412 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
414 vlc_object_t *p_found;
416 vlc_mutex_lock( &structure_lock );
418 /* If we are of the requested type ourselves, don't look further */
419 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
421 p_this->i_refcount++;
422 vlc_mutex_unlock( &structure_lock );
426 /* Otherwise, recursively look for the object */
427 if( (i_mode & 0x000f) == FIND_ANYWHERE )
429 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
430 (i_mode & ~0x000f) | FIND_CHILD );
434 p_found = FindObject( p_this, i_type, i_mode );
437 vlc_mutex_unlock( &structure_lock );
443 ****************************************************************************
444 * increment an object refcount
445 *****************************************************************************/
446 void __vlc_object_yield( vlc_object_t *p_this )
448 vlc_mutex_lock( &structure_lock );
449 p_this->i_refcount++;
450 vlc_mutex_unlock( &structure_lock );
454 ****************************************************************************
455 * decrement an object refcount
456 *****************************************************************************/
457 void __vlc_object_release( vlc_object_t *p_this )
459 vlc_mutex_lock( &structure_lock );
460 p_this->i_refcount--;
461 vlc_mutex_unlock( &structure_lock );
465 ****************************************************************************
466 * attach object to a parent object
467 *****************************************************************************
468 * This function sets p_this as a child of p_parent, and p_parent as a parent
469 * of p_this. This link can be undone using vlc_object_detach.
470 *****************************************************************************/
471 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
473 vlc_mutex_lock( &structure_lock );
475 /* Attach the parent to its child */
476 p_this->p_parent = p_parent;
478 /* Attach the child to its parent */
479 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
480 p_parent->i_children, p_this );
482 /* Climb up the tree to see whether we are connected with the root */
483 if( p_parent->b_attached )
485 SetAttachment( p_this, VLC_TRUE );
488 vlc_mutex_unlock( &structure_lock );
492 ****************************************************************************
493 * detach object from its parent
494 *****************************************************************************
495 * This function removes all links between an object and its parent.
496 *****************************************************************************/
497 void __vlc_object_detach( vlc_object_t *p_this )
499 vlc_mutex_lock( &structure_lock );
500 if( !p_this->p_parent )
502 msg_Err( p_this, "object is not attached" );
503 vlc_mutex_unlock( &structure_lock );
507 /* Climb up the tree to see whether we are connected with the root */
508 if( p_this->p_parent->b_attached )
510 SetAttachment( p_this, VLC_FALSE );
513 DetachObject( p_this );
514 vlc_mutex_unlock( &structure_lock );
518 ****************************************************************************
519 * find a list typed objects and increment their refcount
520 *****************************************************************************
521 * This function recursively looks for a given object type. i_mode can be one
522 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
523 *****************************************************************************/
524 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
527 vlc_object_t **pp_current, **pp_end;
528 int i_count = 0, i_index = 0;
530 vlc_mutex_lock( &structure_lock );
532 /* Look for the objects */
533 switch( i_mode & 0x000f )
536 pp_current = p_this->p_libvlc->pp_objects;
537 pp_end = pp_current + p_this->p_libvlc->i_objects;
539 for( ; pp_current < pp_end ; pp_current++ )
541 if( (*pp_current)->b_attached
542 && (*pp_current)->i_object_type == i_type )
548 p_list = NewList( i_count );
549 pp_current = p_this->p_libvlc->pp_objects;
551 for( ; pp_current < pp_end ; pp_current++ )
553 if( (*pp_current)->b_attached
554 && (*pp_current)->i_object_type == i_type )
556 ListReplace( p_list, *pp_current, i_index );
557 if( i_index < i_count ) i_index++;
563 i_count = CountChildren( p_this, i_type );
564 p_list = NewList( i_count );
566 /* Check allocation was successful */
567 if( p_list->i_count != i_count )
569 msg_Err( p_this, "list allocation failed!" );
575 ListChildren( p_list, p_this, i_type );
579 msg_Err( p_this, "unimplemented!" );
580 p_list = NewList( 0 );
584 vlc_mutex_unlock( &structure_lock );
589 /*****************************************************************************
590 * DumpCommand: print the current vlc structure
591 *****************************************************************************
592 * This function prints either an ASCII tree showing the connections between
593 * vlc objects, and additional information such as their refcount, thread ID,
594 * etc. (command "tree"), or the same data as a simple list (command "list").
595 *****************************************************************************/
596 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
597 vlc_value_t oldval, vlc_value_t newval, void *p_data )
599 if( *psz_cmd == 't' )
601 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
602 vlc_object_t *p_object;
604 if( *newval.psz_string )
606 p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
615 p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
618 vlc_mutex_lock( &structure_lock );
621 DumpStructure( p_object, 0, psz_foo );
623 vlc_mutex_unlock( &structure_lock );
625 if( *newval.psz_string )
627 vlc_object_release( p_this );
630 else if( *psz_cmd == 'l' )
632 vlc_object_t **pp_current, **pp_end;
634 vlc_mutex_lock( &structure_lock );
636 pp_current = p_this->p_libvlc->pp_objects;
637 pp_end = pp_current + p_this->p_libvlc->i_objects;
639 for( ; pp_current < pp_end ; pp_current++ )
641 if( (*pp_current)->b_attached )
643 PrintObject( *pp_current, "" );
647 printf( " o %.8i %s (not attached)\n",
648 (*pp_current)->i_object_id,
649 (*pp_current)->psz_object_type );
653 vlc_mutex_unlock( &structure_lock );
659 /*****************************************************************************
660 * vlc_list_release: free a list previously allocated by vlc_list_find
661 *****************************************************************************
662 * This function decreases the refcount of all objects in the list and
664 *****************************************************************************/
665 void vlc_list_release( vlc_list_t *p_list )
669 for( i_index = 0; i_index < p_list->i_count; i_index++ )
671 vlc_mutex_lock( &structure_lock );
673 p_list->p_values[i_index].p_object->i_refcount--;
675 vlc_mutex_unlock( &structure_lock );
678 free( p_list->p_values );
682 /* Following functions are local */
684 /*****************************************************************************
685 * FindIndex: find the index of an object in an array of objects
686 *****************************************************************************
687 * This function assumes that p_this can be found in pp_objects. It will not
688 * crash if p_this cannot be found, but will return a wrong value. It is your
689 * duty to check the return value if you are not certain that the object could
691 *****************************************************************************/
692 static int FindIndex( vlc_object_t *p_this,
693 vlc_object_t **pp_objects, int i_count )
695 int i_middle = i_count / 2;
702 if( pp_objects[i_middle] == p_this )
712 /* We take advantage of the sorted array */
713 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
715 return i_middle + FindIndex( p_this, pp_objects + i_middle,
716 i_count - i_middle );
720 return FindIndex( p_this, pp_objects, i_middle );
724 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
729 switch( i_mode & 0x000f )
732 p_tmp = p_this->p_parent;
735 if( p_tmp->i_object_type == i_type )
742 return FindObject( p_tmp, i_type, i_mode );
748 for( i = p_this->i_children; i--; )
750 p_tmp = p_this->pp_children[i];
751 if( p_tmp->i_object_type == i_type )
756 else if( p_tmp->i_children )
758 p_tmp = FindObject( p_tmp, i_type, i_mode );
768 /* Handled in vlc_object_find */
775 static void DetachObject( vlc_object_t *p_this )
777 vlc_object_t *p_parent = p_this->p_parent;
780 /* Remove p_this's parent */
781 p_this->p_parent = NULL;
783 /* Remove all of p_parent's children which are p_this */
784 for( i_index = p_parent->i_children ; i_index-- ; )
786 if( p_parent->pp_children[i_index] == p_this )
788 p_parent->i_children--;
789 for( i = i_index ; i < p_parent->i_children ; i++ )
791 p_parent->pp_children[i] = p_parent->pp_children[i+1];
796 if( p_parent->i_children )
798 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
799 p_parent->i_children * sizeof(vlc_object_t *) );
803 free( p_parent->pp_children );
804 p_parent->pp_children = NULL;
808 /*****************************************************************************
809 * SetAttachment: recursively set the b_attached flag of a subtree.
810 *****************************************************************************
811 * This function is used by the attach and detach functions to propagate
812 * the b_attached flag in a subtree.
813 *****************************************************************************/
814 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
818 for( i_index = p_this->i_children ; i_index-- ; )
820 SetAttachment( p_this->pp_children[i_index], b_attached );
823 p_this->b_attached = b_attached;
826 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
828 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
831 if( p_this->psz_object_name )
833 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
838 psz_children[0] = '\0';
839 switch( p_this->i_children )
844 strcpy( psz_children, ", 1 child" );
847 snprintf( psz_children, 20,
848 ", %i children", p_this->i_children );
849 psz_children[19] = '\0';
853 psz_refcount[0] = '\0';
854 if( p_this->i_refcount )
856 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
857 psz_refcount[19] = '\0';
860 psz_thread[0] = '\0';
861 if( p_this->b_thread )
863 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
864 psz_thread[19] = '\0';
867 printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
868 p_this->i_object_id, p_this->psz_object_type,
869 psz_name, psz_thread, psz_refcount, psz_children );
872 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
875 char i_back = psz_foo[i_level];
876 psz_foo[i_level] = '\0';
878 PrintObject( p_this, psz_foo );
880 psz_foo[i_level] = i_back;
882 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
884 msg_Warn( p_this, "structure tree is too deep" );
888 for( i = 0 ; i < p_this->i_children ; i++ )
892 psz_foo[i_level-1] = ' ';
894 if( psz_foo[i_level-2] == '`' )
896 psz_foo[i_level-2] = ' ';
900 if( i == p_this->i_children - 1 )
902 psz_foo[i_level] = '`';
906 psz_foo[i_level] = '|';
909 psz_foo[i_level+1] = '-';
910 psz_foo[i_level+2] = '\0';
912 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
916 static vlc_list_t * NewList( int i_count )
918 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
924 p_list->i_count = i_count;
928 p_list->p_values = NULL;
932 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
933 if( p_list->p_values == NULL )
942 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
945 if( p_list == NULL || i_index >= p_list->i_count )
950 p_object->i_refcount++;
952 p_list->p_values[i_index].p_object = p_object;
957 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
964 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
965 * sizeof( vlc_value_t ) );
966 if( p_list->p_values == NULL )
972 p_object->i_refcount++;
974 p_list->p_values[p_list->i_count].p_object = p_object;
980 static int CountChildren( vlc_object_t *p_this, int i_type )
985 for( i = 0; i < p_this->i_children; i++ )
987 p_tmp = p_this->pp_children[i];
989 if( p_tmp->i_object_type == i_type )
994 if( p_tmp->i_children )
996 i_count += CountChildren( p_tmp, i_type );
1003 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1005 vlc_object_t *p_tmp;
1008 for( i = 0; i < p_this->i_children; i++ )
1010 p_tmp = p_this->pp_children[i];
1012 if( p_tmp->i_object_type == i_type )
1014 ListReplace( p_list, p_tmp, p_list->i_count++ );
1017 if( p_tmp->i_children )
1019 ListChildren( p_list, p_tmp, i_type );