5b56ed0101905b611eff5869031714102b19401d
[platform/upstream/gstreamer.git] / gst / gstelement.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2004 Wim Taymans <wim@fluendo.com>
4  *
5  * gstelement.h: Header for GstElement
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifndef __GST_ELEMENT_H__
25 #define __GST_ELEMENT_H__
26
27 #include <gst/gstconfig.h>
28 #include <gst/gsttypes.h>
29 #include <gst/gstobject.h>
30 #include <gst/gstpad.h>
31 #include <gst/gstclock.h>
32 #include <gst/gstplugin.h>
33 #include <gst/gstpluginfeature.h>
34 #include <gst/gstindex.h>
35 #include <gst/gstiterator.h>
36 #include <gst/gstmessage.h>
37 #include <gst/gsttag.h>
38
39 G_BEGIN_DECLS
40
41 typedef struct _GstElementDetails GstElementDetails;
42
43 /* FIXME: need translatable stuff in here (how handle in registry)? */
44 struct _GstElementDetails
45 {
46   /*< public > */
47   gchar *longname;              /* long, english name */
48   gchar *klass;                 /* type of element, as hierarchy */
49   gchar *description;           /* insights of one form or another */
50   gchar *author;                /* who wrote this thing? */
51
52   /*< private > */
53   gpointer _gst_reserved[GST_PADDING];
54 };
55
56 #define GST_ELEMENT_DETAILS(longname,klass,description,author)          \
57   { longname, klass, description, author, GST_PADDING_INIT }
58 #define GST_IS_ELEMENT_DETAILS(details) (                                       \
59   (details) && ((details)->longname != NULL) && ((details)->klass != NULL)      \
60   && ((details)->description != NULL) && ((details)->author != NULL))
61
62 #define GST_NUM_STATES 4
63
64 /* NOTE: this probably should be done with an #ifdef to decide 
65  * whether to safe-cast or to just do the non-checking cast.
66  */
67 #define GST_STATE(obj)                  (GST_ELEMENT(obj)->current_state)
68 #define GST_STATE_PENDING(obj)          (GST_ELEMENT(obj)->pending_state)
69 #define GST_STATE_ERROR(obj)            (GST_ELEMENT(obj)->state_error)
70
71 /* Note: using 8 bit shift mostly "just because", it leaves us enough room to grow <g> */
72 #define GST_STATE_TRANSITION(obj)       ((GST_STATE(obj)<<8) | GST_STATE_PENDING(obj))
73 #define GST_STATE_NULL_TO_READY         ((GST_STATE_NULL<<8) | GST_STATE_READY)
74 #define GST_STATE_READY_TO_PAUSED       ((GST_STATE_READY<<8) | GST_STATE_PAUSED)
75 #define GST_STATE_PAUSED_TO_PLAYING     ((GST_STATE_PAUSED<<8) | GST_STATE_PLAYING)
76 #define GST_STATE_PLAYING_TO_PAUSED     ((GST_STATE_PLAYING<<8) | GST_STATE_PAUSED)
77 #define GST_STATE_PAUSED_TO_READY       ((GST_STATE_PAUSED<<8) | GST_STATE_READY)
78 #define GST_STATE_READY_TO_NULL         ((GST_STATE_READY<<8) | GST_STATE_NULL)
79
80 GST_EXPORT GType _gst_element_type;
81
82 #define GST_TYPE_ELEMENT                (_gst_element_type)
83 #define GST_IS_ELEMENT(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_ELEMENT))
84 #define GST_IS_ELEMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_ELEMENT))
85 #define GST_ELEMENT_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_ELEMENT, GstElementClass))
86 #define GST_ELEMENT(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_ELEMENT, GstElement))
87 #define GST_ELEMENT_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_ELEMENT, GstElementClass))
88 #define GST_ELEMENT_CAST(obj)           ((GstElement*)(obj))
89
90 /* convenience functions */
91 #ifdef G_HAVE_ISO_VARARGS
92 #define GST_ELEMENT_QUERY_TYPE_FUNCTION(functionname, ...) \
93         GST_QUERY_TYPE_FUNCTION (GstElement*, functionname, __VA_ARGS__);
94 #define GST_ELEMENT_FORMATS_FUNCTION(functionname, ...)    \
95         GST_FORMATS_FUNCTION (GstElement*, functionname, __VA_ARGS__);
96 #define GST_ELEMENT_EVENT_MASK_FUNCTION(functionname, ...) \
97         GST_EVENT_MASK_FUNCTION (GstElement*, functionname, __VA_ARGS__);
98 #elif defined(G_HAVE_GNUC_VARARGS)
99 #define GST_ELEMENT_QUERY_TYPE_FUNCTION(functionname, a...) \
100         GST_QUERY_TYPE_FUNCTION (GstElement*, functionname, a);
101 #define GST_ELEMENT_FORMATS_FUNCTION(functionname, a...)    \
102         GST_FORMATS_FUNCTION (GstElement*, functionname, a);
103 #define GST_ELEMENT_EVENT_MASK_FUNCTION(functionname, a...) \
104         GST_EVENT_MASK_FUNCTION (GstElement*, functionname, a);
105 #endif
106
107 typedef enum
108 {
109   /* private flags that can be used by the scheduler */
110   GST_ELEMENT_SCHEDULER_PRIVATE1,
111   GST_ELEMENT_SCHEDULER_PRIVATE2,
112
113   /* ignore state changes from parent */
114   GST_ELEMENT_LOCKED_STATE,
115
116   /* the element is a sink */
117   GST_ELEMENT_IS_SINK,
118
119   /* use some padding for future expansion */
120   GST_ELEMENT_FLAG_LAST         = GST_OBJECT_FLAG_LAST + 16
121 } GstElementFlags;
122
123 #define GST_ELEMENT_IS_LOCKED_STATE(obj)        (GST_FLAG_IS_SET(obj,GST_ELEMENT_LOCKED_STATE))
124
125 #define GST_ELEMENT_NAME(obj)                   (GST_OBJECT_NAME(obj))
126 #define GST_ELEMENT_PARENT(obj)                 (GST_ELEMENT_CAST(GST_OBJECT_PARENT(obj)))
127 #define GST_ELEMENT_MANAGER(obj)                (GST_ELEMENT_CAST(obj)->manager)
128 #define GST_ELEMENT_BUS(obj)                    (GST_ELEMENT_CAST(obj)->bus)
129 #define GST_ELEMENT_SCHEDULER(obj)              (GST_ELEMENT_CAST(obj)->scheduler)
130 #define GST_ELEMENT_CLOCK(obj)                  (GST_ELEMENT_CAST(obj)->clock)
131 #define GST_ELEMENT_PADS(obj)                   (GST_ELEMENT_CAST(obj)->pads)
132
133 /**
134  * GST_ELEMENT_ERROR:
135  * @el: the element that throws the error
136  * @domain: like CORE, LIBRARY, RESOURCE or STREAM (see #GstError)
137  * @code: error code defined for that domain (see #GstError)
138  * @message: the message to display (format string and args enclosed in round brackets)
139  * @debug: debugging information for the message (format string and args enclosed in round brackets)
140  *
141  * Utility function that elements can use in case they encountered a fatal
142  * data processing error. The pipeline will throw an error signal and the
143  * application will be requested to stop further media processing.
144  */
145 #define GST_ELEMENT_ERROR(el, domain, code, text, debug)                \
146 G_STMT_START {                                                          \
147   gchar *__txt = _gst_element_error_printf text;                        \
148   gchar *__dbg = _gst_element_error_printf debug;                       \
149   if (__txt)                                                            \
150     GST_ERROR_OBJECT (el, "%s", __txt);                                 \
151   if (__dbg)                                                            \
152     GST_ERROR_OBJECT (el, "%s", __dbg);                                 \
153   gst_element_message_full (GST_ELEMENT(el), GST_MESSAGE_ERROR,         \
154     GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code,        \
155     __txt, __dbg, __FILE__, GST_FUNCTION, __LINE__);                    \
156 } G_STMT_END
157
158 /* log a (non-fatal) warning message and post it on the bus */
159 #define GST_ELEMENT_WARNING(el, domain, code, text, debug)              \
160 G_STMT_START {                                                          \
161   gchar *__txt = _gst_element_error_printf text;                        \
162   gchar *__dbg = _gst_element_error_printf debug;                       \
163   if (__txt)                                                            \
164     GST_WARNING_OBJECT (el, "%s", __txt);                               \
165   if (__dbg)                                                            \
166     GST_WARNING_OBJECT (el, "%s", __dbg);                               \
167   gst_element_message_full (GST_ELEMENT(el), GST_MESSAGE_WARNING,       \
168     GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code,        \
169   __txt, __dbg, __FILE__, GST_FUNCTION, __LINE__);                      \
170 } G_STMT_END
171
172 /* the state change mutexes and conds */
173 #define GST_STATE_GET_LOCK(elem)               (GST_ELEMENT_CAST(elem)->state_lock)
174 #define GST_STATE_LOCK(elem)                   g_mutex_lock(GST_STATE_GET_LOCK(elem))
175 #define GST_STATE_TRYLOCK(elem)                g_mutex_trylock(GST_STATE_GET_LOCK(elem))
176 #define GST_STATE_UNLOCK(elem)                 g_mutex_unlock(GST_STATE_GET_LOCK(elem))
177 #define GST_STATE_GET_COND(elem)               (GST_ELEMENT_CAST(elem)->state_cond)
178 #define GST_STATE_WAIT(elem)                   g_cond_wait (GST_STATE_GET_COND (elem), GST_STATE_GET_LOCK (elem))
179 #define GST_STATE_TIMED_WAIT(elem, timeval)    g_cond_timed_wait (GST_STATE_GET_COND (elem), GST_STATE_GET_LOCK (elem),\
180                                                                 timeval)
181 #define GST_STATE_SIGNAL(elem)                 g_cond_signal (GST_STATE_GET_COND (elem));
182 #define GST_STATE_BROADCAST(elem)              g_cond_broadcast (GST_STATE_GET_COND (elem));
183
184 typedef struct _GstElementFactory GstElementFactory;
185 typedef struct _GstElementFactoryClass GstElementFactoryClass;
186
187 struct _GstElement
188 {
189   GstObject             object;
190
191   /*< public >*/ /* with STATE_LOCK */
192   /* element state */
193   GMutex               *state_lock;
194   GCond                *state_cond;
195   guint8                current_state;
196   guint8                pending_state;
197   gboolean              state_error; /* flag is set when the element has an error in the last state
198                                         change. it is cleared when doing another state change. */
199   /*< public >*/ /* with LOCK */
200   /* element manager */
201   GstPipeline  *manager;
202   GstBus       *bus;
203   GstScheduler         *scheduler;
204   /* private pointer for the scheduler */
205   gpointer              sched_private;
206
207   /* allocated clock */
208   GstClock             *clock;
209   GstClockTimeDiff      base_time; /* NULL/READY: 0 - PAUSED: current time - PLAYING: difference to clock */
210
211   /* element pads, these lists can only be iterated while holding
212    * the LOCK or checking the cookie after each LOCK. */
213   guint16               numpads;
214   GList                *pads;
215   guint16               numsrcpads;
216   GList                *srcpads;
217   guint16               numsinkpads;
218   GList                *sinkpads;
219   guint32               pads_cookie;
220
221   /*< private >*/
222   gpointer _gst_reserved[GST_PADDING];
223 };
224
225 struct _GstElementClass
226 {
227   GstObjectClass         parent_class;
228
229   /*< public >*/
230   /* the element details */
231   GstElementDetails      details;
232
233   /* factory that the element was created from */
234   GstElementFactory     *elementfactory;
235
236   /* templates for our pads */
237   GList                 *padtemplates;
238   gint                   numpadtemplates;
239   guint32                pad_templ_cookie;
240
241   /* signal callbacks */
242   void (*state_change)  (GstElement *element, GstElementState old, GstElementState state);
243   void (*new_pad)       (GstElement *element, GstPad *pad);
244   void (*pad_removed)   (GstElement *element, GstPad *pad);
245   void (*no_more_pads)  (GstElement *element);
246
247   /*< protected >*/
248   /* vtable */
249
250   /* request/release pads */
251   GstPad*               (*request_new_pad)      (GstElement *element, GstPadTemplate *templ, const gchar* name);
252   void                  (*release_pad)          (GstElement *element, GstPad *pad);
253
254   /* state changes */
255   GstElementStateReturn (*get_state)            (GstElement * element, GstElementState * state,
256                                                  GstElementState * pending, GTimeVal * timeout);
257   GstElementStateReturn (*change_state)         (GstElement *element);
258
259   /* manager */
260   void                  (*set_manager)          (GstElement * element, GstPipeline * pipeline);
261   void                  (*set_bus)              (GstElement * element, GstBus * bus);
262   void                  (*set_scheduler)        (GstElement *element, GstScheduler *scheduler);
263
264   /* set/get clocks */
265   GstClock*             (*get_clock)            (GstElement *element);
266   void                  (*set_clock)            (GstElement *element, GstClock *clock);
267
268   /* index */
269   GstIndex*             (*get_index)            (GstElement *element);
270   void                  (*set_index)            (GstElement *element, GstIndex *index);
271
272   /* query functions */
273   gboolean              (*send_event)           (GstElement *element, GstEvent *event);
274
275   const GstQueryType*   (*get_query_types)      (GstElement *element);
276   gboolean              (*query)                (GstElement *element, GstQuery *query);
277
278   /*< private >*/
279   gpointer _gst_reserved[GST_PADDING];
280 };
281
282 /* element class pad templates */
283 void                    gst_element_class_add_pad_template      (GstElementClass *klass, GstPadTemplate *templ);
284 GstPadTemplate*         gst_element_class_get_pad_template      (GstElementClass *element_class, const gchar *name);
285 GList*                  gst_element_class_get_pad_template_list (GstElementClass *element_class);
286 void                    gst_element_class_set_details           (GstElementClass *klass,
287                                                                  const GstElementDetails *details);
288
289 /* element instance */
290 GType                   gst_element_get_type            (void);
291
292 /* basic name and parentage stuff from GstObject */
293 #define                 gst_element_get_name(elem)      gst_object_get_name(GST_OBJECT(elem))
294 #define                 gst_element_set_name(elem,name) gst_object_set_name(GST_OBJECT(elem),name)
295 #define                 gst_element_get_parent(elem)    gst_object_get_parent(GST_OBJECT(elem))
296 #define                 gst_element_set_parent(elem,parent)     gst_object_set_parent(GST_OBJECT(elem),parent)
297
298 /* clocking */
299 gboolean                gst_element_requires_clock      (GstElement *element);
300 gboolean                gst_element_provides_clock      (GstElement *element);
301 GstClock*               gst_element_get_clock           (GstElement *element);
302 void                    gst_element_set_clock           (GstElement *element, GstClock *clock);
303
304 /* indexes */
305 gboolean                gst_element_is_indexable        (GstElement *element);
306 void                    gst_element_set_index           (GstElement *element, GstIndex *index);
307 GstIndex*               gst_element_get_index           (GstElement *element);
308
309 /* manager and tasks */
310 void                    gst_element_set_manager         (GstElement * element, GstPipeline * pipeline);
311 GstPipeline *           gst_element_get_manager         (GstElement * element);
312 void                    gst_element_set_bus             (GstElement * element, GstBus * bus);
313 GstBus *                gst_element_get_bus             (GstElement * element);
314 void                    gst_element_set_scheduler       (GstElement *element, GstScheduler *sched);
315 GstScheduler*           gst_element_get_scheduler       (GstElement *element);
316
317 /* pad management */
318 gboolean                gst_element_add_pad             (GstElement *element, GstPad *pad);
319 gboolean                gst_element_remove_pad          (GstElement *element, GstPad *pad);
320 GstPad *                gst_element_add_ghost_pad       (GstElement *element, GstPad *pad, const gchar *name);
321 void                    gst_element_no_more_pads        (GstElement *element);
322
323 GstPad*                 gst_element_get_pad             (GstElement *element, const gchar *name);
324 GstPad*                 gst_element_get_static_pad      (GstElement *element, const gchar *name);
325 GstPad*                 gst_element_get_request_pad     (GstElement *element, const gchar *name);
326 void                    gst_element_release_request_pad (GstElement *element, GstPad *pad);
327
328 GstIterator *           gst_element_iterate_pads        (GstElement * element);
329
330 /* event/query/format stuff */
331 gboolean                gst_element_send_event          (GstElement *element, GstEvent *event);
332 gboolean                gst_element_seek                (GstElement *element, GstSeekType seek_type,
333                                                          guint64 offset);
334 G_CONST_RETURN GstQueryType*
335                         gst_element_get_query_types     (GstElement *element);
336 gboolean                gst_element_query               (GstElement *element, GstQuery *query);
337
338 /* messages */
339 gboolean                gst_element_post_message        (GstElement * element, GstMessage * message);
340
341 /* error handling */
342 gchar *                 _gst_element_error_printf       (const gchar *format, ...);
343 void                    gst_element_message_full        (GstElement * element, GstMessageType type,
344                                                          GQuark domain, gint code, gchar * text, 
345                                                          gchar * debug, const gchar * file,
346                                                          const gchar * function, gint line);
347
348 /* state management */
349 gboolean                gst_element_is_locked_state     (GstElement *element);
350 gboolean                gst_element_set_locked_state    (GstElement *element, gboolean locked_state);
351 gboolean                gst_element_sync_state_with_parent (GstElement *element);
352
353 GstElementStateReturn   gst_element_get_state           (GstElement * element, 
354                                                          GstElementState * state,
355                                                          GstElementState * pending, 
356                                                          GTimeVal * timeout);
357 GstElementStateReturn   gst_element_set_state           (GstElement *element, GstElementState state);
358
359 void                    gst_element_abort_state         (GstElement * element);
360 void                    gst_element_commit_state        (GstElement * element);
361 void                    gst_element_lost_state          (GstElement * element);
362
363 /* factory management */
364 GstElementFactory*      gst_element_get_factory         (GstElement *element);
365
366 /*
367  *
368  * factories stuff
369  *
370  **/
371
372 #define GST_TYPE_ELEMENT_FACTORY                (gst_element_factory_get_type())
373 #define GST_ELEMENT_FACTORY(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ELEMENT_FACTORY,\
374                                                  GstElementFactory))
375 #define GST_ELEMENT_FACTORY_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ELEMENT_FACTORY,\
376                                                  GstElementFactoryClass))
377 #define GST_IS_ELEMENT_FACTORY(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ELEMENT_FACTORY))
378 #define GST_IS_ELEMENT_FACTORY_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ELEMENT_FACTORY))
379
380 struct _GstElementFactory {
381   GstPluginFeature      parent;
382
383   GType                 type;                   /* unique GType of element or 0 if not loaded */
384
385   GstElementDetails     details;
386
387   GList *               staticpadtemplates;
388   guint                 numpadtemplates;
389
390   /* URI interface stuff */
391   guint                 uri_type;
392   gchar **              uri_protocols;
393   
394   GList *               interfaces;             /* interfaces this element implements */
395
396   gpointer _gst_reserved[GST_PADDING];
397 };
398
399 struct _GstElementFactoryClass {
400   GstPluginFeatureClass parent_class;
401
402   gpointer _gst_reserved[GST_PADDING];
403 };
404
405 GType                   gst_element_factory_get_type            (void);
406
407 gboolean                gst_element_register                    (GstPlugin *plugin,
408                                                                  const gchar *name,
409                                                                  guint rank,
410                                                                  GType type);
411
412 GstElementFactory *     gst_element_factory_find                (const gchar *name);
413 GType                   gst_element_factory_get_element_type    (GstElementFactory *factory);
414 G_CONST_RETURN gchar *  gst_element_factory_get_longname        (GstElementFactory *factory);
415 G_CONST_RETURN gchar *  gst_element_factory_get_klass           (GstElementFactory *factory);
416 G_CONST_RETURN gchar *  gst_element_factory_get_description     (GstElementFactory *factory);
417 G_CONST_RETURN gchar *  gst_element_factory_get_author          (GstElementFactory *factory);
418 guint                   gst_element_factory_get_num_pad_templates (GstElementFactory *factory);
419 G_CONST_RETURN GList *  gst_element_factory_get_static_pad_templates (GstElementFactory *factory);
420 guint                   gst_element_factory_get_uri_type        (GstElementFactory *factory);           
421 gchar **                gst_element_factory_get_uri_protocols   (GstElementFactory *factory);           
422
423 GstElement*             gst_element_factory_create              (GstElementFactory *factory,
424                                                                  const gchar *name);
425 GstElement*             gst_element_factory_make                (const gchar *factoryname, const gchar *name);
426
427 void                    __gst_element_factory_add_static_pad_template (GstElementFactory *elementfactory,
428                                                                  GstStaticPadTemplate *templ);
429 void                    __gst_element_factory_add_interface     (GstElementFactory *elementfactory,
430                                                                  const gchar *interfacename);
431
432 G_END_DECLS
433
434 #endif /* __GST_ELEMENT_H__ */