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