1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: objects.c,v 1.38 2003/09/02 20:19:26 gbazin 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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 # include <stdlib.h> /* realloc() */
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
37 #include "vlc_video.h"
38 #include "video_output.h"
40 #include "audio_output.h"
41 #include "aout_internal.h"
42 #include "stream_output.h"
44 #include "vlc_playlist.h"
45 #include "vlc_interface.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int DumpCommand( vlc_object_t *, char const *,
52 vlc_value_t, vlc_value_t, void * );
54 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
55 static void DetachObject ( vlc_object_t * );
56 static void PrintObject ( vlc_object_t *, const char * );
57 static void DumpStructure ( vlc_object_t *, int, char * );
58 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
59 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
61 static vlc_list_t * NewList ( int );
62 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
63 static void ListAppend ( vlc_list_t *, vlc_object_t * );
64 static int CountChildren ( vlc_object_t *, int );
65 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
67 /*****************************************************************************
68 * Local structure lock
69 *****************************************************************************/
70 static vlc_mutex_t structure_lock;
72 /*****************************************************************************
73 * vlc_object_create: initialize a vlc object
74 *****************************************************************************
75 * This function allocates memory for a vlc object and initializes it. If
76 * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
77 * so on, vlc_object_create will use its value for the object size.
78 *****************************************************************************/
79 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
88 i_size = sizeof(libvlc_t);
92 i_size = sizeof(vlc_t);
95 case VLC_OBJECT_MODULE:
96 i_size = sizeof(module_t);
100 i_size = sizeof(intf_thread_t);
101 psz_type = "interface";
103 case VLC_OBJECT_PLAYLIST:
104 i_size = sizeof(playlist_t);
105 psz_type = "playlist";
107 case VLC_OBJECT_INPUT:
108 i_size = sizeof(input_thread_t);
111 case VLC_OBJECT_DECODER:
112 i_size = sizeof(decoder_t);
113 psz_type = "decoder";
115 case VLC_OBJECT_DECODER_FIFO: /* tmp for backward compat */
116 i_size = sizeof(decoder_fifo_t);
117 psz_type = "decoder";
119 case VLC_OBJECT_VOUT:
120 i_size = sizeof(vout_thread_t);
121 psz_type = "video output";
123 case VLC_OBJECT_AOUT:
124 i_size = sizeof(aout_instance_t);
125 psz_type = "audio output";
127 case VLC_OBJECT_SOUT:
128 i_size = sizeof(sout_instance_t);
129 psz_type = "stream output";
131 case VLC_OBJECT_HTTPD:
132 i_size = sizeof( httpd_t );
133 psz_type = "http daemon";
137 ? i_type > (int)sizeof(vlc_object_t)
138 ? i_type : (int)sizeof(vlc_object_t)
139 : (int)sizeof(vlc_object_t);
140 i_type = VLC_OBJECT_GENERIC;
141 psz_type = "generic";
145 if( i_type == VLC_OBJECT_ROOT )
151 p_new = malloc( i_size );
158 memset( p_new, 0, i_size );
161 p_new->i_object_type = i_type;
162 p_new->psz_object_type = psz_type;
164 p_new->psz_object_name = NULL;
166 p_new->b_die = VLC_FALSE;
167 p_new->b_error = VLC_FALSE;
168 p_new->b_dead = VLC_FALSE;
169 p_new->b_attached = VLC_FALSE;
172 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
180 if( i_type == VLC_OBJECT_ROOT )
182 /* If i_type is root, then p_new is actually p_libvlc */
183 p_new->p_libvlc = (libvlc_t*)p_new;
186 p_new->p_libvlc->i_counter = 0;
187 p_new->i_object_id = 0;
189 p_new->p_libvlc->i_objects = 1;
190 p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
191 p_new->p_libvlc->pp_objects[0] = p_new;
192 p_new->b_attached = VLC_TRUE;
196 p_new->p_libvlc = p_this->p_libvlc;
197 p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
200 vlc_mutex_lock( &structure_lock );
202 p_new->p_libvlc->i_counter++;
203 p_new->i_object_id = p_new->p_libvlc->i_counter;
205 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
206 * useless to try and recover anything if pp_objects gets smashed. */
207 INSERT_ELEM( p_new->p_libvlc->pp_objects,
208 p_new->p_libvlc->i_objects,
209 p_new->p_libvlc->i_objects,
212 vlc_mutex_unlock( &structure_lock );
215 p_new->i_refcount = 0;
216 p_new->p_parent = NULL;
217 p_new->pp_children = NULL;
218 p_new->i_children = 0;
220 p_new->p_private = NULL;
222 /* Initialize mutexes and condvars */
223 vlc_mutex_init( p_new, &p_new->object_lock );
224 vlc_cond_init( p_new, &p_new->object_wait );
225 vlc_mutex_init( p_new, &p_new->var_lock );
227 if( i_type == VLC_OBJECT_ROOT )
229 vlc_mutex_init( p_new, &structure_lock );
231 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
232 var_AddCallback( p_new, "list", DumpCommand, NULL );
233 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
234 var_AddCallback( p_new, "tree", DumpCommand, NULL );
240 /*****************************************************************************
241 * vlc_object_destroy: destroy a vlc object
242 *****************************************************************************
243 * This function destroys an object that has been previously allocated with
244 * vlc_object_create. The object's refcount must be zero and it must not be
245 * attached to other objects in any way.
246 *****************************************************************************/
247 void __vlc_object_destroy( vlc_object_t *p_this )
251 if( p_this->i_children )
253 msg_Err( p_this, "cannot delete object with children" );
257 if( p_this->p_parent )
259 msg_Err( p_this, "cannot delete object with a parent" );
263 while( p_this->i_refcount )
267 /* Don't warn immediately ... 100ms seems OK */
270 msg_Warn( p_this, "refcount is %i, delaying before deletion",
271 p_this->i_refcount );
273 else if( i_delay == 12 )
275 msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
276 p_this->i_refcount );
278 else if( i_delay == 42 )
280 msg_Err( p_this, "we waited too long, cancelling destruction" );
287 /* Destroy the associated variables, starting from the end so that
288 * no memmove calls have to be done. */
289 while( p_this->i_vars )
291 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
294 free( p_this->p_vars );
295 vlc_mutex_destroy( &p_this->var_lock );
297 if( p_this->i_object_type == VLC_OBJECT_ROOT )
299 /* We are the root object ... no need to lock. */
300 free( p_this->p_libvlc->pp_objects );
301 p_this->p_libvlc->pp_objects = NULL;
302 p_this->p_libvlc->i_objects--;
304 vlc_mutex_destroy( &structure_lock );
310 vlc_mutex_lock( &structure_lock );
312 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
313 * useless to try and recover anything if pp_objects gets smashed. */
314 i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
315 p_this->p_libvlc->i_objects );
316 REMOVE_ELEM( p_this->p_libvlc->pp_objects,
317 p_this->p_libvlc->i_objects, i_index );
319 vlc_mutex_unlock( &structure_lock );
322 vlc_mutex_destroy( &p_this->object_lock );
323 vlc_cond_destroy( &p_this->object_wait );
328 /*****************************************************************************
329 * vlc_object_get: find an object given its ID
330 *****************************************************************************
331 * This function looks for the object whose i_object_id field is i_id. We
332 * use a dichotomy so that lookups are in log2(n).
333 *****************************************************************************/
334 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
337 vlc_object_t **pp_objects;
339 vlc_mutex_lock( &structure_lock );
341 pp_objects = p_this->p_libvlc->pp_objects;
343 /* Perform our dichotomy */
344 for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
346 i_middle = i_max / 2;
348 if( pp_objects[i_middle]->i_object_id > i_id )
352 else if( pp_objects[i_middle]->i_object_id < i_id )
356 pp_objects += i_middle;
361 /* This happens when there are only two remaining objects */
362 if( pp_objects[i_middle+1]->i_object_id == i_id )
364 vlc_mutex_unlock( &structure_lock );
365 pp_objects[i_middle+1]->i_refcount++;
366 return pp_objects[i_middle+1];
373 vlc_mutex_unlock( &structure_lock );
374 pp_objects[i_middle]->i_refcount++;
375 return pp_objects[i_middle];
380 /* this means that i_max == i_middle, and since we have already
381 * tested pp_objects[i_middle]), p_found is properly set. */
386 vlc_mutex_unlock( &structure_lock );
390 /*****************************************************************************
391 * vlc_object_find: find a typed object and increment its refcount
392 *****************************************************************************
393 * This function recursively looks for a given object type. i_mode can be one
394 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
395 *****************************************************************************/
396 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
398 vlc_object_t *p_found;
400 vlc_mutex_lock( &structure_lock );
402 /* If we are of the requested type ourselves, don't look further */
403 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
405 p_this->i_refcount++;
406 vlc_mutex_unlock( &structure_lock );
410 /* Otherwise, recursively look for the object */
411 if( (i_mode & 0x000f) == FIND_ANYWHERE )
413 p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
414 (i_mode & ~0x000f) | FIND_CHILD );
418 p_found = FindObject( p_this, i_type, i_mode );
421 vlc_mutex_unlock( &structure_lock );
426 /*****************************************************************************
427 * vlc_object_yield: increment an object refcount
428 *****************************************************************************/
429 void __vlc_object_yield( vlc_object_t *p_this )
431 vlc_mutex_lock( &structure_lock );
432 p_this->i_refcount++;
433 vlc_mutex_unlock( &structure_lock );
436 /*****************************************************************************
437 * vlc_object_release: decrement an object refcount
438 *****************************************************************************/
439 void __vlc_object_release( vlc_object_t *p_this )
441 vlc_mutex_lock( &structure_lock );
442 p_this->i_refcount--;
443 vlc_mutex_unlock( &structure_lock );
446 /*****************************************************************************
447 * vlc_object_attach: attach object to a parent object
448 *****************************************************************************
449 * This function sets p_this as a child of p_parent, and p_parent as a parent
450 * of p_this. This link can be undone using vlc_object_detach.
451 *****************************************************************************/
452 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
454 vlc_mutex_lock( &structure_lock );
456 /* Attach the parent to its child */
457 p_this->p_parent = p_parent;
459 /* Attach the child to its parent */
460 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
461 p_parent->i_children, p_this );
463 /* Climb up the tree to see whether we are connected with the root */
464 if( p_parent->b_attached )
466 SetAttachment( p_this, VLC_TRUE );
469 vlc_mutex_unlock( &structure_lock );
472 /*****************************************************************************
473 * vlc_object_detach: detach object from its parent
474 *****************************************************************************
475 * This function removes all links between an object and its parent.
476 *****************************************************************************/
477 void __vlc_object_detach( vlc_object_t *p_this )
479 vlc_mutex_lock( &structure_lock );
480 if( !p_this->p_parent )
482 msg_Err( p_this, "object is not attached" );
483 vlc_mutex_unlock( &structure_lock );
487 /* Climb up the tree to see whether we are connected with the root */
488 if( p_this->p_parent->b_attached )
490 SetAttachment( p_this, VLC_FALSE );
493 DetachObject( p_this );
494 vlc_mutex_unlock( &structure_lock );
497 /*****************************************************************************
498 * vlc_list_find: find a list typed objects and increment their refcount
499 *****************************************************************************
500 * This function recursively looks for a given object type. i_mode can be one
501 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
502 *****************************************************************************/
503 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
506 vlc_object_t **pp_current, **pp_end;
507 int i_count = 0, i_index = 0;
509 vlc_mutex_lock( &structure_lock );
511 /* Look for the objects */
512 switch( i_mode & 0x000f )
515 pp_current = p_this->p_libvlc->pp_objects;
516 pp_end = pp_current + p_this->p_libvlc->i_objects;
518 for( ; pp_current < pp_end ; pp_current++ )
520 if( (*pp_current)->b_attached
521 && (*pp_current)->i_object_type == i_type )
527 p_list = NewList( i_count );
528 pp_current = p_this->p_libvlc->pp_objects;
530 for( ; pp_current < pp_end ; pp_current++ )
532 if( (*pp_current)->b_attached
533 && (*pp_current)->i_object_type == i_type )
535 ListReplace( p_list, *pp_current, i_index );
536 if( i_index < i_count ) i_index++;
542 i_count = CountChildren( p_this, i_type );
543 p_list = NewList( i_count );
545 /* Check allocation was successful */
546 if( p_list->i_count != i_count )
548 msg_Err( p_this, "list allocation failed!" );
554 ListChildren( p_list, p_this, i_type );
558 msg_Err( p_this, "unimplemented!" );
559 p_list = NewList( 0 );
563 vlc_mutex_unlock( &structure_lock );
568 /*****************************************************************************
569 * DumpCommand: print the current vlc structure
570 *****************************************************************************
571 * This function prints either an ASCII tree showing the connections between
572 * vlc objects, and additional information such as their refcount, thread ID,
573 * etc. (command "tree"), or the same data as a simple list (command "list").
574 *****************************************************************************/
575 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
576 vlc_value_t oldval, vlc_value_t newval, void *p_data )
578 if( *psz_cmd == 't' )
580 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
581 vlc_object_t *p_object;
583 if( *newval.psz_string )
585 p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
594 p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
597 vlc_mutex_lock( &structure_lock );
600 DumpStructure( p_object, 0, psz_foo );
602 vlc_mutex_unlock( &structure_lock );
604 if( *newval.psz_string )
606 vlc_object_release( p_this );
609 else if( *psz_cmd == 'l' )
611 vlc_object_t **pp_current, **pp_end;
613 vlc_mutex_lock( &structure_lock );
615 pp_current = p_this->p_libvlc->pp_objects;
616 pp_end = pp_current + p_this->p_libvlc->i_objects;
618 for( ; pp_current < pp_end ; pp_current++ )
620 if( (*pp_current)->b_attached )
622 PrintObject( *pp_current, "" );
626 printf( " o %.8i %s (not attached)\n",
627 (*pp_current)->i_object_id,
628 (*pp_current)->psz_object_type );
632 vlc_mutex_unlock( &structure_lock );
638 /*****************************************************************************
639 * vlc_list_release: free a list previously allocated by vlc_list_find
640 *****************************************************************************
641 * This function decreases the refcount of all objects in the list and
643 *****************************************************************************/
644 void vlc_list_release( vlc_list_t *p_list )
648 for( i_index = 0; i_index < p_list->i_count; i_index++ )
650 vlc_mutex_lock( &structure_lock );
652 p_list->p_values[i_index].p_object->i_refcount--;
654 vlc_mutex_unlock( &structure_lock );
657 free( p_list->p_values );
661 /* Following functions are local */
663 /*****************************************************************************
664 * FindIndex: find the index of an object in an array of objects
665 *****************************************************************************
666 * This function assumes that p_this can be found in pp_objects. It will not
667 * crash if p_this cannot be found, but will return a wrong value. It is your
668 * duty to check the return value if you are not certain that the object could
670 *****************************************************************************/
671 static int FindIndex( vlc_object_t *p_this,
672 vlc_object_t **pp_objects, int i_count )
674 int i_middle = i_count / 2;
681 if( pp_objects[i_middle] == p_this )
691 /* We take advantage of the sorted array */
692 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
694 return i_middle + FindIndex( p_this, pp_objects + i_middle,
695 i_count - i_middle );
699 return FindIndex( p_this, pp_objects, i_middle );
703 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
708 switch( i_mode & 0x000f )
711 p_tmp = p_this->p_parent;
714 if( p_tmp->i_object_type == i_type )
721 return FindObject( p_tmp, i_type, i_mode );
727 for( i = p_this->i_children; i--; )
729 p_tmp = p_this->pp_children[i];
730 if( p_tmp->i_object_type == i_type )
735 else if( p_tmp->i_children )
737 p_tmp = FindObject( p_tmp, i_type, i_mode );
747 /* Handled in vlc_object_find */
754 static void DetachObject( vlc_object_t *p_this )
756 vlc_object_t *p_parent = p_this->p_parent;
759 /* Remove p_this's parent */
760 p_this->p_parent = NULL;
762 /* Remove all of p_parent's children which are p_this */
763 for( i_index = p_parent->i_children ; i_index-- ; )
765 if( p_parent->pp_children[i_index] == p_this )
767 p_parent->i_children--;
768 for( i = i_index ; i < p_parent->i_children ; i++ )
770 p_parent->pp_children[i] = p_parent->pp_children[i+1];
775 if( p_parent->i_children )
777 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
778 p_parent->i_children * sizeof(vlc_object_t *) );
782 free( p_parent->pp_children );
783 p_parent->pp_children = NULL;
787 /*****************************************************************************
788 * SetAttachment: recursively set the b_attached flag of a subtree.
789 *****************************************************************************
790 * This function is used by the attach and detach functions to propagate
791 * the b_attached flag in a subtree.
792 *****************************************************************************/
793 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
797 for( i_index = p_this->i_children ; i_index-- ; )
799 SetAttachment( p_this->pp_children[i_index], b_attached );
802 p_this->b_attached = b_attached;
805 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
807 char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
810 if( p_this->psz_object_name )
812 snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
817 psz_children[0] = '\0';
818 switch( p_this->i_children )
823 strcpy( psz_children, ", 1 child" );
826 snprintf( psz_children, 20,
827 ", %i children", p_this->i_children );
828 psz_children[19] = '\0';
832 psz_refcount[0] = '\0';
833 if( p_this->i_refcount )
835 snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
836 psz_refcount[19] = '\0';
839 psz_thread[0] = '\0';
840 if( p_this->b_thread )
842 snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
843 psz_thread[19] = '\0';
846 printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
847 p_this->i_object_id, p_this->psz_object_type,
848 psz_name, psz_thread, psz_refcount, psz_children );
851 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
854 char i_back = psz_foo[i_level];
855 psz_foo[i_level] = '\0';
857 PrintObject( p_this, psz_foo );
859 psz_foo[i_level] = i_back;
861 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
863 msg_Warn( p_this, "structure tree is too deep" );
867 for( i = 0 ; i < p_this->i_children ; i++ )
871 psz_foo[i_level-1] = ' ';
873 if( psz_foo[i_level-2] == '`' )
875 psz_foo[i_level-2] = ' ';
879 if( i == p_this->i_children - 1 )
881 psz_foo[i_level] = '`';
885 psz_foo[i_level] = '|';
888 psz_foo[i_level+1] = '-';
889 psz_foo[i_level+2] = '\0';
891 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
895 static vlc_list_t * NewList( int i_count )
897 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
903 p_list->i_count = i_count;
907 p_list->p_values = NULL;
911 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
912 if( p_list->p_values == NULL )
921 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
924 if( p_list == NULL || i_index >= p_list->i_count )
929 p_object->i_refcount++;
931 p_list->p_values[i_index].p_object = p_object;
936 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
943 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
944 * sizeof( vlc_value_t ) );
945 if( p_list->p_values == NULL )
951 p_object->i_refcount++;
953 p_list->p_values[p_list->i_count].p_object = p_object;
959 static int CountChildren( vlc_object_t *p_this, int i_type )
964 for( i = 0; i < p_this->i_children; i++ )
966 p_tmp = p_this->pp_children[i];
968 if( p_tmp->i_object_type == i_type )
973 if( p_tmp->i_children )
975 i_count += CountChildren( p_tmp, i_type );
982 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
987 for( i = 0; i < p_this->i_children; i++ )
989 p_tmp = p_this->pp_children[i];
991 if( p_tmp->i_object_type == i_type )
993 ListReplace( p_list, p_tmp, p_list->i_count++ );
996 if( p_tmp->i_children )
998 ListChildren( p_list, p_tmp, i_type );