* ./src/misc/objects.c: two big changes in the object API: now objects can
[vlc.git] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: objects.c,v 1.16 2002/08/12 09:34:15 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #ifdef HAVE_STDLIB_H
30 #   include <stdlib.h>                                          /* realloc() */
31 #endif
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36
37 #include "video.h"
38 #include "video_output.h"
39
40 #include "audio_output.h"
41 #include "aout_internal.h"
42
43 #include "vlc_playlist.h"
44 #include "interface.h"
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static vlc_object_t * vlc_object_find_inner( vlc_object_t *, int, int );
50 static void vlc_object_detach_inner( vlc_object_t * );
51 static void vlc_dumpstructure_inner( vlc_object_t *, int, char * );
52 static int  find_index_inner( vlc_object_t *, vlc_object_t **, int );
53 static void set_attachment_flag( vlc_object_t *, vlc_bool_t );
54
55 /*****************************************************************************
56  * vlc_object_create: initialize a vlc object
57  *****************************************************************************
58  * This function allocates memory for a vlc object and initializes it. If
59  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
60  * so on, vlc_object_create will use its value for the object size.
61  *****************************************************************************/
62 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
63 {
64     vlc_object_t * p_new;
65     char *         psz_type;
66     size_t         i_size;
67
68     switch( i_type )
69     {
70         case VLC_OBJECT_ROOT:
71             i_size = sizeof(vlc_t);
72             psz_type = "root";
73             break;
74         case VLC_OBJECT_MODULE:
75             i_size = sizeof(module_t);
76             psz_type = "module";
77             break;
78         case VLC_OBJECT_INTF:
79             i_size = sizeof(intf_thread_t);
80             psz_type = "interface";
81             break;
82         case VLC_OBJECT_PLAYLIST:
83             i_size = sizeof(playlist_t);
84             psz_type = "playlist";
85             break;
86         case VLC_OBJECT_INPUT:
87             i_size = sizeof(input_thread_t);
88             psz_type = "input";
89             break;
90         case VLC_OBJECT_DECODER:
91             i_size = sizeof(decoder_fifo_t);
92             psz_type = "decoder";
93             break;
94         case VLC_OBJECT_VOUT:
95             i_size = sizeof(vout_thread_t);
96             psz_type = "video output";
97             break;
98         case VLC_OBJECT_AOUT:
99             i_size = sizeof(aout_instance_t);
100             psz_type = "audio output";
101             break;
102         default:
103             i_size = i_type > sizeof(vlc_object_t)
104                    ? i_type : sizeof(vlc_object_t);
105             i_type = VLC_OBJECT_GENERIC;
106             psz_type = "generic";
107             break;
108     }
109
110     p_new = malloc( i_size );
111
112     if( !p_new )
113     {
114         return NULL;
115     }
116
117     memset( p_new, 0, i_size );
118
119     p_new->i_object_type = i_type;
120     p_new->psz_object_type = psz_type;
121
122     p_new->psz_object_name = NULL;
123
124     p_new->i_refcount = 0;
125     p_new->b_die = VLC_FALSE;
126     p_new->b_error = VLC_FALSE;
127     p_new->b_dead = VLC_FALSE;
128     p_new->b_attached = VLC_FALSE;
129
130     /* If i_type is root, then p_new is our own p_vlc */
131     if( i_type == VLC_OBJECT_ROOT )
132     {
133         /* We are the first object ... no need to lock. */
134         p_new->p_vlc = (vlc_t*)p_new;
135
136         p_new->p_vlc->i_counter = 0;
137         p_new->i_object_id = 0;
138
139         p_new->p_vlc->i_objects = 1;
140         p_new->p_vlc->pp_objects = malloc( sizeof(vlc_object_t *) );
141         p_new->p_vlc->pp_objects[0] = p_new;
142         p_new->b_attached = VLC_TRUE;
143     }
144     else
145     {
146         p_new->p_vlc = p_this->p_vlc;
147
148         vlc_mutex_lock( &p_this->p_vlc->structure_lock );
149
150         p_new->p_vlc->i_counter++;
151         p_new->i_object_id = p_new->p_vlc->i_counter;
152
153         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
154          * useless to try and recover anything if pp_objects gets smashed. */
155         p_new->p_vlc->i_objects++;
156         p_new->p_vlc->pp_objects =
157                   realloc( p_new->p_vlc->pp_objects,
158                            p_new->p_vlc->i_objects * sizeof(vlc_object_t *) );
159         p_new->p_vlc->pp_objects[ p_new->p_vlc->i_objects - 1 ] = p_new;
160
161         vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
162     }
163
164     p_new->p_parent = NULL;
165     p_new->pp_children = NULL;
166     p_new->i_children = 0;
167
168     p_new->p_private = NULL;
169
170     vlc_mutex_init( p_new, &p_new->object_lock );
171     vlc_cond_init( p_new, &p_new->object_wait );
172
173     return p_new;
174 }
175
176 /*****************************************************************************
177  * vlc_object_destroy: destroy a vlc object
178  *****************************************************************************
179  * This function destroys an object that has been previously allocated with
180  * vlc_object_create. The object's refcount must be zero and it must not be
181  * attached to other objects in any way.
182  *****************************************************************************/
183 void __vlc_object_destroy( vlc_object_t *p_this )
184 {
185     int i_delay = 0;
186
187     if( p_this->i_children )
188     {
189         msg_Err( p_this, "cannot delete object with children" );
190         vlc_dumpstructure( p_this );
191         return;
192     }
193
194     if( p_this->p_parent )
195     {
196         msg_Err( p_this, "cannot delete object with a parent" );
197         vlc_dumpstructure( p_this );
198         return;
199     }
200
201     while( p_this->i_refcount )
202     {
203         i_delay++;
204
205         /* Don't warn immediately ... 100ms seems OK */
206         if( i_delay == 2 )
207         {
208             msg_Warn( p_this, "refcount is %i, delaying before deletion",
209                               p_this->i_refcount );
210         }
211         else if( i_delay == 12 )
212         {
213             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
214                              p_this->i_refcount );
215         }
216         else if( i_delay == 42 )
217         {
218             msg_Err( p_this, "we waited too long, cancelling destruction" );
219             return;
220         }
221
222         msleep( 100000 );
223     }
224
225     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
226
227     /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
228      * useless to try and recover anything if pp_objects gets smashed. */
229     if( p_this->p_vlc->i_objects > 1 )
230     {
231         int i_index = find_index_inner( p_this, p_this->p_vlc->pp_objects,
232                                                 p_this->p_vlc->i_objects );
233         memmove( p_this->p_vlc->pp_objects + i_index,
234                  p_this->p_vlc->pp_objects + i_index + 1,
235                  p_this->p_vlc->i_objects - i_index - 1 );
236
237         p_this->p_vlc->pp_objects =
238             realloc( p_this->p_vlc->pp_objects,
239                      (p_this->p_vlc->i_objects - 1) * sizeof(vlc_object_t *) );
240     }
241     else
242     {
243         free( p_this->p_vlc->pp_objects );
244         p_this->p_vlc->pp_objects = NULL;
245     }
246
247     p_this->p_vlc->i_objects--;
248
249     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
250
251     vlc_mutex_destroy( &p_this->object_lock );
252     vlc_cond_destroy( &p_this->object_wait );
253
254     free( p_this );
255 }
256
257 /*****************************************************************************
258  * find_index_inner: find the index of an object in an array of objects
259  *****************************************************************************
260  * This function assumes that p_this can be found in pp_objects. It will not
261  * crash if p_this cannot be found, but will return a random value. It is your
262  * duty to check the return value if you are not certain that the object could
263  * be found for sure.
264  *****************************************************************************/
265 static int find_index_inner( vlc_object_t *p_this,
266                              vlc_object_t **pp_objects, int i_count )
267 {
268     int i_middle = i_count / 2;
269
270     if( i_count == 0 )
271     {
272         return 0;
273     }
274
275     if( pp_objects[i_middle] == p_this )
276     {
277         return i_middle;
278     }
279
280     if( i_count == 1 )
281     {
282         return 0;
283     }
284
285     /* We take advantage of the sorted array */
286     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
287     {
288         return i_middle + find_index_inner( p_this, pp_objects + i_middle,
289                                                     i_count - i_middle );
290     }
291     else
292     {
293         return find_index_inner( p_this, pp_objects, i_middle );
294     }
295 }
296
297 /*****************************************************************************
298  * vlc_object_find: find a typed object and increment its refcount
299  *****************************************************************************
300  * This function recursively looks for a given object type. i_mode can be one
301  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
302  *****************************************************************************/
303 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
304 {
305     vlc_object_t *p_found;
306
307     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
308
309     /* If we are of the requested type ourselves, don't look further */
310     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
311     {
312         p_this->i_refcount++;
313         vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
314         return p_this;
315     }
316
317     /* Otherwise, recursively look for the object */
318     if( (i_mode & 0x000f) == FIND_ANYWHERE )
319     {
320         p_found = vlc_object_find_inner( VLC_OBJECT(p_this->p_vlc),
321                                          i_type,
322                                          (i_mode & ~0x000f) | FIND_CHILD );
323     }
324     else
325     {
326         p_found = vlc_object_find_inner( p_this, i_type, i_mode );
327     }
328
329     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
330
331     return p_found;
332 }
333
334 static vlc_object_t * vlc_object_find_inner( vlc_object_t *p_this,
335                                              int i_type, int i_mode )
336 {
337     int i;
338     vlc_object_t *p_tmp;
339
340     switch( i_mode & 0x000f )
341     {
342     case FIND_PARENT:
343         p_tmp = p_this->p_parent;
344         if( p_tmp )
345         {
346             if( p_tmp->i_object_type == i_type )
347             {
348                 p_tmp->i_refcount++;
349                 return p_tmp;
350             }
351             else
352             {
353                 return vlc_object_find_inner( p_tmp, i_type, i_mode );
354             }
355         }
356         break;
357
358     case FIND_CHILD:
359         for( i = p_this->i_children; i--; )
360         {
361             p_tmp = p_this->pp_children[i];
362             if( p_tmp->i_object_type == i_type )
363             {
364                 p_tmp->i_refcount++;
365                 return p_tmp;
366             }
367             else if( p_tmp->i_children )
368             {
369                 p_tmp = vlc_object_find_inner( p_tmp, i_type, i_mode );
370                 if( p_tmp )
371                 {
372                     return p_tmp;
373                 }
374             }
375         }
376         break;
377
378     case FIND_ANYWHERE:
379         /* Handled in vlc_object_find */
380         break;
381     }
382
383     return NULL;
384 }
385
386 /*****************************************************************************
387  * vlc_object_yield: increment an object refcount
388  *****************************************************************************/
389 void __vlc_object_yield( vlc_object_t *p_this )
390 {
391     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
392     p_this->i_refcount++;
393     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
394 }
395
396 /*****************************************************************************
397  * vlc_object_release: decrement an object refcount
398  *****************************************************************************/
399 void __vlc_object_release( vlc_object_t *p_this )
400 {
401     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
402     p_this->i_refcount--;
403     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
404 }
405
406 /*****************************************************************************
407  * vlc_object_attach: attach object to a parent object
408  *****************************************************************************
409  * This function sets p_this as a child of p_parent, and p_parent as a parent
410  * of p_this. This link can be undone using vlc_object_detach.
411  *****************************************************************************/
412 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
413 {
414     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
415
416     /* Attach the parent to its child */
417     p_this->p_parent = p_parent;
418
419     /* Attach the child to its parent */
420     p_parent->i_children++;
421     p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
422                                p_parent->i_children * sizeof(vlc_object_t *) );
423     p_parent->pp_children[p_parent->i_children - 1] = p_this;
424
425     /* Climb up the tree to see whether we are connected with the root */
426     if( p_parent->b_attached )
427     {
428         set_attachment_flag( p_this, VLC_TRUE );
429     }
430
431     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
432 }
433
434 /*****************************************************************************
435  * vlc_object_detach: detach object from its parent
436  *****************************************************************************
437  * This function removes all links between an object and its parent.
438  *****************************************************************************/
439 void __vlc_object_detach( vlc_object_t *p_this )
440 {
441     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
442     if( !p_this->p_parent )
443     {
444         msg_Err( p_this, "object is not attached" );
445         vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
446         return;
447     }
448
449     /* Climb up the tree to see whether we are connected with the root */
450     if( p_this->p_parent->b_attached )
451     {
452         set_attachment_flag( p_this, VLC_FALSE );
453     }
454
455     vlc_object_detach_inner( p_this );
456     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
457 }
458
459 static void vlc_object_detach_inner( vlc_object_t *p_this )
460 {
461     vlc_object_t *p_parent = p_this->p_parent;
462     int i_index, i;
463
464     /* Remove p_this's parent */
465     p_this->p_parent = NULL;
466
467     /* Remove all of p_parent's children which are p_this */
468     for( i_index = p_parent->i_children ; i_index-- ; )
469     {
470         if( p_parent->pp_children[i_index] == p_this )
471         {
472             p_parent->i_children--;
473             for( i = i_index ; i < p_parent->i_children ; i++ )
474             {
475                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
476             }
477         }
478     }
479
480     if( p_parent->i_children )
481     {
482         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
483                                p_parent->i_children * sizeof(vlc_object_t *) );
484     }
485     else
486     {
487         free( p_parent->pp_children );
488         p_parent->pp_children = NULL;
489     }
490 }
491
492 /*****************************************************************************
493  * set_attachment_flag: recursively set the b_attached flag of a subtree.
494  *****************************************************************************
495  * This function is used by the attach and detach functions to propagate
496  * the b_attached flag in a subtree.
497  *****************************************************************************/
498 static void set_attachment_flag( vlc_object_t *p_this, vlc_bool_t b_attached )
499 {
500     int i_index;
501
502     for( i_index = p_this->i_children ; i_index-- ; )
503     {
504         set_attachment_flag( p_this->pp_children[i_index], b_attached );
505     }
506
507     p_this->b_attached = b_attached;
508 }
509
510 /*****************************************************************************
511  * vlc_dumpstructure: print the current vlc structure
512  *****************************************************************************
513  * This function prints an ASCII tree showing the connections between vlc
514  * objects, and additional information such as their refcount, thread ID,
515  * address, etc.
516  *****************************************************************************/
517 void __vlc_dumpstructure( vlc_object_t *p_this )
518 {
519     char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
520
521     vlc_mutex_lock( &p_this->p_vlc->structure_lock );
522     psz_foo[0] = '|';
523     vlc_dumpstructure_inner( p_this, 0, psz_foo );
524     vlc_mutex_unlock( &p_this->p_vlc->structure_lock );
525 }
526
527 static void vlc_dumpstructure_inner( vlc_object_t *p_this,
528                                      int i_level, char *psz_foo )
529 {
530     int i;
531     char i_back = psz_foo[i_level];
532     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
533
534     psz_name[0] = '\0';
535     if( p_this->psz_object_name )
536     {
537         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
538         psz_name[48] = '\"';
539         psz_name[49] = '\0';
540     }
541
542     psz_children[0] = '\0';
543     switch( p_this->i_children )
544     {
545         case 0:
546             break;
547         case 1:
548             strcpy( psz_children, ", 1 child" );
549             break;
550         default:
551             snprintf( psz_children, 20,
552                       ", %i children", p_this->i_children );
553             psz_children[19] = '\0';
554             break;
555     }
556
557     psz_refcount[0] = '\0';
558     if( p_this->i_refcount )
559     {
560         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
561         psz_refcount[19] = '\0';
562     }
563
564     psz_thread[0] = '\0';
565     if( p_this->b_thread )
566     {
567         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
568         psz_thread[19] = '\0';
569     }
570
571     psz_foo[i_level] = '\0';
572     msg_Info( p_this, "%so %s %p%s%s%s%s", psz_foo, p_this->psz_object_type,
573               p_this, psz_name, psz_thread, psz_refcount, psz_children );
574     psz_foo[i_level] = i_back;
575
576     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
577     {
578         msg_Warn( p_this, "structure tree is too deep" );
579         return;
580     }
581
582     for( i = 0 ; i < p_this->i_children ; i++ )
583     {
584         if( i_level )
585         {
586             psz_foo[i_level-1] = ' ';
587
588             if( psz_foo[i_level-2] == '`' )
589             {
590                 psz_foo[i_level-2] = ' ';
591             }
592         }
593
594         if( i == p_this->i_children - 1 )
595         {
596             psz_foo[i_level] = '`';
597         }
598         else
599         {
600             psz_foo[i_level] = '|';
601         }
602
603         psz_foo[i_level+1] = '-';
604         psz_foo[i_level+2] = '\0';
605
606         vlc_dumpstructure_inner( p_this->pp_children[i], i_level + 2, psz_foo );
607     }
608 }
609