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