VLC  4.0.0-dev
vlc_objects.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_objects.h: vlc_object_t definition and manipulation methods
3  *****************************************************************************
4  * Copyright (C) 2002-2008 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 /**
24  * \defgroup vlc_object VLC objects
25  * \ingroup vlc
26  * @{
27  * \file
28  * Common VLC object defintions
29  */
30 
31 struct vlc_logger;
33 struct vlc_object_marker;
34 
35 /**
36  * VLC object common members
37  *
38  * Common public properties for all VLC objects.
39  * Object also have private properties maintained by the core, see
40  * \ref vlc_object_internals_t
41  */
42 struct vlc_object_t
43 {
44  struct vlc_logger *logger;
45  union {
46  struct vlc_object_internals *priv;
47  struct vlc_object_marker *obj;
48  };
49 
50  bool no_interact;
51 
52  /** Module probe flag
53  *
54  * A boolean during module probing when the probe is "forced".
55  * See \ref module_need().
56  */
57  bool force;
58 };
59 
60 /**
61  * Type-safe vlc_object_t cast
62  *
63  * This macro attempts to cast a pointer to a compound type to a
64  * \ref vlc_object_t pointer in a type-safe manner.
65  * It checks if the compound type actually starts with an embedded
66  * \ref vlc_object_t structure.
67  */
68 #if !defined(__cplusplus)
69 # define VLC_OBJECT(x) \
70  _Generic((x)->obj, \
71  struct vlc_object_marker *: (x), \
72  default: (&((x)->obj)) \
73  )
74 # define vlc_object_cast(t)
75 #else
76 static inline vlc_object_t *VLC_OBJECT(vlc_object_t *o)
77 {
78  return o;
79 }
80 
81 # define vlc_object_cast(t) \
82 struct t; \
83 static inline struct vlc_object_t *VLC_OBJECT(struct t *d) \
84 { \
85  return (struct vlc_object_t *)d; \
86 }
87 #endif
88 
110 
111 /* The root object */
112 struct libvlc_int_t
113 {
114  struct vlc_object_t obj;
115 };
116 
117 /**
118  * Allocates and initializes a vlc object.
119  *
120  * @param i_size object byte size
121  *
122  * @return the new object, or NULL on error.
123  */
125 
126 /**
127  * Drops the strong reference to an object.
128  *
129  * This removes the initial strong reference to a given object. This must be
130  * called exactly once per allocated object after it is no longer needed,
131  * matching vlc_object_create() or vlc_custom_create().
132  */
134 #define vlc_object_delete(obj) vlc_object_delete(VLC_OBJECT(obj))
137 
138 /**
139  * Returns the object type name.
140  *
141  * This returns a nul-terminated string identifying the object type.
142  * The string is valid for at least as long as the object reference.
143  *
144  * \param obj object whose type name to get
145  */
146 VLC_API const char *vlc_object_typename(const vlc_object_t *obj) VLC_USED;
147 
148 /**
149  * Gets the parent of an object.
150  *
151  * \return the parent object (NULL if none)
152  *
153  * \note The returned parent object pointer is valid as long as the child is.
154  */
156 #define vlc_object_parent(o) vlc_object_parent(VLC_OBJECT(o))
158 static inline struct vlc_logger *vlc_object_logger(vlc_object_t *obj)
159 {
160  return obj->logger;
161 }
162 #define vlc_object_logger(o) vlc_object_logger(VLC_OBJECT(o))
164 /**
165  * Tries to get the name of module bound to an object.
166  *
167  * \warning This function is intrinsically race-prone, as a module may be
168  * bound or unbound asynchronously by another thread.
169  * Do not trust the result for any purpose other than debugging/tracing.
170  *
171  * \return Normally, this returns a heap-allocated nul-terminated string
172  * which is the name of the module. If no module are bound to the object, it
173  * returns NULL. It also returns NULL on error.
174  */
175 #define vlc_object_get_name(obj) var_GetString(obj, "module-name")
177 #define vlc_object_create(a,b) vlc_object_create( VLC_OBJECT(a), b )
179 #define vlc_object_find_name(a,b) \
180  vlc_object_find_name( VLC_OBJECT(a),b)
181 
182 VLC_USED
183 static inline libvlc_int_t *vlc_object_instance(vlc_object_t *obj)
184 {
185  vlc_object_t *parent;
186 
187  do
188  parent = obj;
189  while ((obj = vlc_object_parent(obj)) != NULL);
190 
191  return (libvlc_int_t *)parent;
192 }
193 #define vlc_object_instance(o) vlc_object_instance(VLC_OBJECT(o))
195 /* Here for backward compatibility. TODO: Move to <vlc_vout.h>! */
198 
199 /* Here for backward compatibility. TODO: Move to <vlc_aout.h>! */
202 
203 /* TODO: remove vlc_object_hold/_release() for GUIs, remove this */
204 VLC_DEPRECATED static inline void *vlc_object_hold(vlc_object_t *o)
205 {
206  const char *tn = vlc_object_typename(o);
207 
208  if (!strcmp(tn, "audio output"))
210  if (!strcmp(tn, "video output"))
211  vout_Hold((vout_thread_t *)o);
212  return o;
213 }
214 
215 static inline void vlc_object_release(vlc_object_t *o)
216 {
217  const char *tn = vlc_object_typename(o);
218 
219  if (!strcmp(tn, "audio output"))
221  if (!strcmp(tn, "video output"))
223 }
224 
225 /**
226  * @defgroup objres Object resources
227  *
228  * The object resource functions tie resource allocation to an instance of
229  * a module through a VLC object.
230  * Such resource will be automatically freed, in first in last out order,
231  * when the module instance associated with the VLC object is terminated.
232  *
233  * Specifically, if the module instance activation/probe function fails, the
234  * resource will be freed immediately after the failure. If the activation
235  * succeeds, the resource will be freed when the module instance is terminated.
236  *
237  * This is a convenience mechanism to save explicit clean-up function calls
238  * in modules.
239  *
240  * @{
241  */
242 
243 /**
244  * Allocates memory for a module.
245  *
246  * This function allocates memory from the heap for a module instance.
247  * The memory is uninitialized.
248  *
249  * @param obj VLC object to tie the memory allocation to
250  * @param size byte size of the memory allocation
251  *
252  * @return a pointer to the allocated memory, or NULL on error (errno is set).
253  */
254 VLC_API VLC_MALLOC void *vlc_obj_malloc(vlc_object_t *obj, size_t size);
255 
256 /**
257  * Allocates a zero-initialized table for a module.
258  *
259  * This function allocates a table from the heap for a module instance.
260  * The memory is initialized to all zeroes.
261  *
262  * @param obj VLC object to tie the memory allocation to
263  * @param nmemb number of table entries
264  * @param size byte size of a table entry
265  *
266  * @return a pointer to the allocated memory, or NULL on error (errno is set).
267  */
268 VLC_API VLC_MALLOC void *vlc_obj_calloc(vlc_object_t *obj, size_t nmemb,
269  size_t size);
270 
271 /**
272  * Duplicates a string for a module.
273  *
274  * This function allocates a copy of a nul-terminated string for a module
275  * instance.
276  *
277  * @param obj VLC object to tie the memory allocation to
278  * @param str string to copy
279  *
280  * @return a pointer to the copy, or NULL on error (errno is set).
281  */
282 VLC_API VLC_MALLOC char *vlc_obj_strdup(vlc_object_t *obj, const char *str);
283 
284 /**
285  * Manually frees module memory.
286  *
287  * This function manually frees a resource allocated with vlc_obj_malloc(),
288  * vlc_obj_calloc() or vlc_obj_strdup() before the module instance is
289  * terminated. This is seldom necessary.
290  *
291  * @param obj VLC object that the allocation was tied to
292  * @param ptr pointer to the allocated resource
293  */
294 VLC_API void vlc_obj_free(vlc_object_t *obj, void *ptr);
295 
296 /** @} */
297 /** @} */
#define vlc_object_logger(o)
Definition: vlc_objects.h:163
static void vlc_object_release(vlc_object_t *o)
Definition: vlc_objects.h:216
bool no_interact
Definition: vlc_objects.h:51
Definition: player.h:201
#define vlc_object_cast(t)
Definition: vlc_objects.h:75
void aout_Release(audio_output_t *aout)
Definition: output.c:377
static void * vlc_object_hold(vlc_object_t *o)
Definition: vlc_objects.h:205
#define VLC_DEPRECATED
Deprecated functions or compound members annotation.
Definition: vlc_common.h:119
void vout_Release(vout_thread_t *vout)
Definition: video_output.c:1790
Definition: vlc_objects.h:113
Main service discovery structure to build a SD module.
Definition: vlc_services_discovery.h:59
size_t vlc_list_children(vlc_object_t *, vlc_object_t **, size_t)
void * vlc_obj_malloc(vlc_object_t *obj, size_t size)
Allocates memory for a module.
Definition: objres.c:137
Definition: variables.h:35
struct vlc_object_internals * priv
Definition: vlc_objects.h:47
#define VLC_MALLOC
Heap allocated result function annotation.
Definition: vlc_common.h:167
Stream output access_output.
Definition: vlc_sout.h:69
const char * vlc_object_typename(const vlc_object_t *obj)
Returns the object type name.
Definition: objects.c:110
struct vlc_object_marker * obj
Definition: vlc_objects.h:48
audio_output_t * aout_Hold(audio_output_t *aout)
Definition: output.c:344
Video output thread descriptor.
Definition: vlc_vout.h:60
struct playlist_t playlist_t
Definition: vlc_common.h:344
#define vlc_object_create(a, b)
Definition: vlc_objects.h:178
Describe all interface-specific data of the interface thread.
Definition: vlc_interface.h:48
Definition: vlc_fingerprinter.h:70
Definition: vlc_demux.h:55
Definition: vlc_media_library.h:742
vout_thread_t * vout_Hold(vout_thread_t *vout)
Definition: video_output.c:1935
Definition: messages.c:54
char * vlc_obj_strdup(vlc_object_t *obj, const char *str)
Duplicates a string for a module.
Definition: objres.c:168
Definition: vlc_sout.h:195
#define VLC_API
Definition: fourcc_gen.c:31
#define VLC_OBJECT(x)
Type-safe vlc_object_t cast.
Definition: vlc_objects.h:70
Audio output object.
Definition: vlc_aout.h:140
stream_t definition
Definition: vlc_stream.h:46
Definition: vlc_codec.h:103
Structure describing a filter.
Definition: vlc_filter.h:68
Definition: vlc_renderer_discovery.h:165
#define vlc_object_instance(o)
Definition: vlc_objects.h:194
#define vlc_object_delete(obj)
Definition: vlc_objects.h:135
Definition: vlc_xml.h:37
Stream output instance (FIXME: should be private to src/ to avoid invalid unsynchronized access) ...
Definition: vlc_sout.h:48
bool force
Module probe flag.
Definition: vlc_objects.h:58
struct vlc_logger * logger
Definition: vlc_objects.h:45
Window object.
Definition: vlc_vout_window.h:336
Extensions manager object.
Definition: vlc_extensions.h:53
VLC object common members.
Definition: vlc_objects.h:43
void vlc_obj_free(vlc_object_t *obj, void *ptr)
Manually frees module memory.
Definition: objres.c:173
void * vlc_obj_calloc(vlc_object_t *obj, size_t nmemb, size_t size)
Allocates a zero-initialized table for a module.
Definition: objres.c:145
Definition: vlc_vout_display.h:219
#define vlc_object_parent(o)
Definition: vlc_objects.h:157
#define VLC_USED
Definition: fourcc_gen.c:32