Add message to request a state change
[platform/upstream/gstreamer.git] / gst / gstmessage.h
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  *
4  * gstmessage.h: Header for GstMessage subsystem
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef __GST_MESSAGE_H__
23 #define __GST_MESSAGE_H__
24
25 G_BEGIN_DECLS
26
27 typedef struct _GstMessage GstMessage;
28 typedef struct _GstMessageClass GstMessageClass;
29
30 /**
31  * GstMessageType:
32  * @GST_MESSAGE_UNKNOWN: an undefined message
33  * @GST_MESSAGE_EOS: end-of-stream reached in a pipeline. The application will
34  * only receive this message in the PLAYING state and every time it sets a
35  * pipeline to PLAYING that is in the EOS state. The application can perform a
36  * flushing seek in the pipeline, which will undo the EOS state again. 
37  * @GST_MESSAGE_ERROR: an error occured. Whe the application receives an error
38  * message it should stop playback of the pipeline and not assume that more
39  * data will be played.
40  * @GST_MESSAGE_WARNING: a warning occured.
41  * @GST_MESSAGE_INFO: an info message occured
42  * @GST_MESSAGE_TAG: a tag was found.
43  * @GST_MESSAGE_BUFFERING: the pipeline is buffering. When the application
44  * receives a buffering message in the PLAYING state for a non-live pipeline it
45  * must PAUSE the pipeline until the buffering completes, when the percentage
46  * field in the message is 100%. For live pipelines, no action must be
47  * performed and the buffering percentage can be used to inform the user about
48  * the progress.
49  * @GST_MESSAGE_STATE_CHANGED: a state change happened
50  * @GST_MESSAGE_STATE_DIRTY: an element changed state in a streaming thread.
51  * This message is deprecated.
52  * @GST_MESSAGE_STEP_DONE: a framestep finished. This message is not yet
53  * implemented.
54  * @GST_MESSAGE_CLOCK_PROVIDE: an element notifies its capability of providing
55  *                             a clock. This message is used internally and
56  *                             never forwarded to the application.
57  * @GST_MESSAGE_CLOCK_LOST: The current clock as selected by the pipeline became
58  *                          unusable. The pipeline will select a new clock on
59  *                          the next PLAYING state change.
60  * @GST_MESSAGE_NEW_CLOCK: a new clock was selected in the pipeline.
61  * @GST_MESSAGE_STRUCTURE_CHANGE: the structure of the pipeline changed. This
62  * message is used internally and never forwarded to the application.
63  * @GST_MESSAGE_STREAM_STATUS: status about a stream, emitted when it starts,
64  *                             stops, errors, etc.. Not implemented yet.
65  * @GST_MESSAGE_APPLICATION: message posted by the application, possibly
66  *                           via an application-specific element.
67  * @GST_MESSAGE_ELEMENT: element-specific message, see the specific element's
68  *                       documentation
69  * @GST_MESSAGE_SEGMENT_START: pipeline started playback of a segment. This
70  * message is used internally and never forwarded to the application.
71  * @GST_MESSAGE_SEGMENT_DONE: pipeline completed playback of a segment. This
72  * message is forwarded to the application after all elements that posted
73  * @GST_MESSAGE_SEGMENT_START posted a GST_MESSAGE_SEGMENT_DONE message.
74  * @GST_MESSAGE_DURATION: The duration of a pipeline changed. The application
75  * can get the new duration with a duration query.
76  * @GST_MESSAGE_ASYNC_START: Posted by elements when they start an ASYNC state
77  * change. This message is not forwarded to the application but is used
78  * internally. Since: 0.10.13. 
79  * @GST_MESSAGE_ASYNC_DONE: Posted by elements when they complete an ASYNC state
80  * change. The application will only receive this message from the toplevel
81  * pipeline. Since: 0.10.13
82  * @GST_MESSAGE_LATENCY: Posted by elements when their latency changes. The
83  * pipeline will calculate and distribute a new latency. Since: 0.10.12
84  * @GST_MESSAGE_REQUEST_STATE: Posted by elements when they want the pipeline to
85  * change state. This message is a suggestion to the application which can
86  * decide to perform the state change on (part of) the pipeline. Since: 0.10.23.
87  * @GST_MESSAGE_ANY: mask for all of the above messages.
88  *
89  * The different message types that are available.
90  */
91 /* NOTE: keep in sync with quark registration in gstmessage.c 
92  * NOTE: keep GST_MESSAGE_ANY a valid gint to avoid compiler warnings.
93  */ 
94 typedef enum
95 {
96   GST_MESSAGE_UNKNOWN           = 0,
97   GST_MESSAGE_EOS               = (1 << 0),
98   GST_MESSAGE_ERROR             = (1 << 1),
99   GST_MESSAGE_WARNING           = (1 << 2),
100   GST_MESSAGE_INFO              = (1 << 3),
101   GST_MESSAGE_TAG               = (1 << 4),
102   GST_MESSAGE_BUFFERING         = (1 << 5),
103   GST_MESSAGE_STATE_CHANGED     = (1 << 6),
104   GST_MESSAGE_STATE_DIRTY       = (1 << 7),
105   GST_MESSAGE_STEP_DONE         = (1 << 8),
106   GST_MESSAGE_CLOCK_PROVIDE     = (1 << 9),
107   GST_MESSAGE_CLOCK_LOST        = (1 << 10),
108   GST_MESSAGE_NEW_CLOCK         = (1 << 11),
109   GST_MESSAGE_STRUCTURE_CHANGE  = (1 << 12),
110   GST_MESSAGE_STREAM_STATUS     = (1 << 13),
111   GST_MESSAGE_APPLICATION       = (1 << 14),
112   GST_MESSAGE_ELEMENT           = (1 << 15),
113   GST_MESSAGE_SEGMENT_START     = (1 << 16),
114   GST_MESSAGE_SEGMENT_DONE      = (1 << 17),
115   GST_MESSAGE_DURATION          = (1 << 18),
116   GST_MESSAGE_LATENCY           = (1 << 19),
117   GST_MESSAGE_ASYNC_START       = (1 << 20),
118   GST_MESSAGE_ASYNC_DONE        = (1 << 21),
119   GST_MESSAGE_REQUEST_STATE     = (1 << 22),
120   GST_MESSAGE_ANY               = ~0
121 } GstMessageType;
122
123 #include <gst/gstminiobject.h>
124 #include <gst/gstobject.h>
125 #include <gst/gstelement.h>
126 #include <gst/gsttaglist.h>
127 #include <gst/gststructure.h>
128
129 /**
130  * GST_MESSAGE_TRACE_NAME:
131  *
132  * The name used for memory allocation tracing
133  */
134 #define GST_MESSAGE_TRACE_NAME  "GstMessage"
135
136 #define GST_TYPE_MESSAGE                         (gst_message_get_type())
137 #define GST_IS_MESSAGE(obj)                      (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MESSAGE))
138 #define GST_IS_MESSAGE_CLASS(klass)              (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MESSAGE))
139 #define GST_MESSAGE_GET_CLASS(obj)               (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_MESSAGE, GstMessageClass))
140 #define GST_MESSAGE(obj)                         (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MESSAGE, GstMessage))
141 #define GST_MESSAGE_CLASS(klass)                 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MESSAGE, GstMessageClass))
142 #define GST_MESSAGE_CAST(obj)                    ((GstMessage*)(obj))
143
144 /* the lock is used to handle the synchronous handling of messages,
145  * the emiting thread is block until the handling thread processed
146  * the message using this mutex/cond pair */
147 #define GST_MESSAGE_GET_LOCK(message)   (GST_MESSAGE(message)->lock)
148 #define GST_MESSAGE_LOCK(message)       g_mutex_lock(GST_MESSAGE_GET_LOCK(message))
149 #define GST_MESSAGE_UNLOCK(message)     g_mutex_unlock(GST_MESSAGE_GET_LOCK(message))
150 #define GST_MESSAGE_COND(message)       (GST_MESSAGE(message)->cond)
151 #define GST_MESSAGE_WAIT(message)       g_cond_wait(GST_MESSAGE_COND(message),GST_MESSAGE_GET_LOCK(message))
152 #define GST_MESSAGE_SIGNAL(message)     g_cond_signal(GST_MESSAGE_COND(message))
153
154 /**
155  * GST_MESSAGE_TYPE:
156  * @message: a #GstMessage
157  *
158  * Get the #GstMessageType of @message.
159  */
160 #define GST_MESSAGE_TYPE(message)       (GST_MESSAGE(message)->type)
161 /**
162  * GST_MESSAGE_TYPE_NAME:
163  * @message: a #GstMessage
164  *
165  * Get a constant string representation of the #GstMessageType of @message.
166  *
167  * Since: 0.10.4
168  */
169 #define GST_MESSAGE_TYPE_NAME(message)  gst_message_type_get_name(GST_MESSAGE_TYPE(message))
170 /**
171  * GST_MESSAGE_TIMESTAMP:
172  * @message: a #GstMessage
173  *
174  * Get the timestamp of @message. This is the timestamp when the message
175  * was created.
176  */
177 #define GST_MESSAGE_TIMESTAMP(message)  (GST_MESSAGE(message)->timestamp)
178 /**
179  * GST_MESSAGE_SRC:
180  * @message: a #GstMessage
181  *
182  * Get the object that posted @message.
183  */
184 #define GST_MESSAGE_SRC(message)        (GST_MESSAGE(message)->src)
185
186 /**
187  * GstStructureChangeType:
188  * @GST_STRUCTURE_CHANGE_TYPE_PAD_LINK: Pad linking is starting or done.
189  * @GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK: Pad unlinking is starting or done.
190  *
191  * The type of a #GstMessageStructureChange.
192  *
193  * Since: 0.10.22
194  */
195 typedef enum {
196   GST_STRUCTURE_CHANGE_TYPE_PAD_LINK   = 0,
197   GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK = 1
198 } GstStructureChangeType;
199
200 /**
201  * GstMessage:
202  * @mini_object: the parent structure
203  * @type: the #GstMessageType of the message
204  * @timestamp: the timestamp of the message
205  * @src: the src of the message
206  * @structure: the #GstStructure containing the message info.
207  *
208  * A #GstMessage.
209  */
210 struct _GstMessage
211 {
212   GstMiniObject mini_object;
213
214   /*< private > *//* with MESSAGE_LOCK */
215   GMutex *lock;                 /* lock and cond for async delivery */
216   GCond *cond;
217
218   /*< public > *//* with COW */
219   GstMessageType type;
220   guint64 timestamp;
221   GstObject *src;
222
223   GstStructure *structure;
224
225   /*< private > */
226   union {
227     struct {
228       guint32 seqnum;
229     } ABI;
230     /* + 0 to mark ABI change for future greppage */
231     gpointer _gst_reserved[GST_PADDING + 0];
232   } abidata;
233 };
234
235 struct _GstMessageClass {
236   GstMiniObjectClass mini_object_class;
237
238   /*< private > */
239   gpointer _gst_reserved[GST_PADDING];
240 };
241
242 GType           gst_message_get_type            (void);
243
244 const gchar*    gst_message_type_get_name       (GstMessageType type);
245 GQuark          gst_message_type_to_quark       (GstMessageType type);
246
247 /* refcounting */
248 /**
249  * gst_message_ref:
250  * @msg: the message to ref
251  *
252  * Convenience macro to increase the reference count of the message.
253  *
254  * Returns: @msg (for convenience when doing assignments)
255  */
256 #ifdef _FOOL_GTK_DOC_
257 G_INLINE_FUNC GstMessage * gst_message_ref (GstMessage * msg);
258 #endif
259
260 static inline GstMessage *
261 gst_message_ref (GstMessage * msg)
262 {
263   /* not using a macro here because gcc-4.1 will complain
264    * if the return value isn't used (because of the cast) */
265   return (GstMessage *) gst_mini_object_ref (GST_MINI_OBJECT (msg));
266 }
267
268 /**
269  * gst_message_unref:
270  * @msg: the message to unref
271  *
272  * Convenience macro to decrease the reference count of the message, possibly
273  * freeing it.
274  */
275 #define         gst_message_unref(msg)          gst_mini_object_unref (GST_MINI_OBJECT (msg))
276 /* copy message */
277 /**
278  * gst_message_copy:
279  * @msg: the message to copy
280  *
281  * Creates a copy of the message. Returns a copy of the message.
282  *
283  * MT safe
284  */
285 #define         gst_message_copy(msg)           GST_MESSAGE (gst_mini_object_copy (GST_MINI_OBJECT (msg)))
286 /**
287  * gst_message_make_writable:
288  * @msg: the message to make writable
289  *
290  * Checks if a message is writable. If not, a writable copy is made and
291  * returned. Returns a message (possibly a duplicate) that is writable.
292  *
293  * MT safe
294  */
295 #define         gst_message_make_writable(msg)  GST_MESSAGE (gst_mini_object_make_writable (GST_MINI_OBJECT (msg)))
296
297 /* identifiers for events and messages */
298 guint32         gst_message_get_seqnum          (GstMessage *message);
299 void            gst_message_set_seqnum          (GstMessage *message, guint32 seqnum);
300
301 /* EOS */
302 GstMessage *    gst_message_new_eos             (GstObject * src);
303
304 /* ERROR */
305
306 GstMessage *    gst_message_new_error           (GstObject * src, GError * error, const gchar * debug);
307 void            gst_message_parse_error         (GstMessage *message, GError **gerror, gchar **debug);
308
309 /* WARNING */
310 GstMessage *    gst_message_new_warning         (GstObject * src, GError * error, const gchar * debug);
311 void            gst_message_parse_warning       (GstMessage *message, GError **gerror, gchar **debug);
312
313 /* INFO */
314 GstMessage *    gst_message_new_info            (GstObject * src, GError * error, const gchar * debug);
315 void            gst_message_parse_info          (GstMessage *message, GError **gerror, gchar **debug);
316
317 /* TAG */
318 GstMessage *    gst_message_new_tag             (GstObject * src, GstTagList * tag_list);
319 void            gst_message_parse_tag           (GstMessage *message, GstTagList **tag_list);
320
321 /* BUFFERING */
322 GstMessage *    gst_message_new_buffering       (GstObject * src, gint percent);
323 void            gst_message_parse_buffering     (GstMessage *message, gint *percent);
324 void            gst_message_set_buffering_stats (GstMessage *message, GstBufferingMode mode,
325                                                  gint avg_in, gint avg_out,
326                                                  gint64 buffering_left);
327 void            gst_message_parse_buffering_stats (GstMessage *message, GstBufferingMode *mode,
328                                                    gint *avg_in, gint *avg_out,
329                                                    gint64 *buffering_left);
330
331 /* STATE_CHANGED */
332 GstMessage *    gst_message_new_state_changed   (GstObject * src, GstState oldstate,
333                                                  GstState newstate, GstState pending);
334 void            gst_message_parse_state_changed (GstMessage *message, GstState *oldstate,
335                                                  GstState *newstate, GstState *pending);
336
337 /* STATE_DIRTY */
338 GstMessage *    gst_message_new_state_dirty     (GstObject * src);
339
340 /* CLOCK_PROVIDE */
341 GstMessage *    gst_message_new_clock_provide   (GstObject * src, GstClock *clock, gboolean ready);
342 void            gst_message_parse_clock_provide (GstMessage *message, GstClock **clock,
343                                                  gboolean *ready);
344
345 /* CLOCK_LOST */
346 GstMessage *    gst_message_new_clock_lost      (GstObject * src, GstClock *clock);
347 void            gst_message_parse_clock_lost    (GstMessage *message, GstClock **clock);
348
349 /* NEW_CLOCK */
350 GstMessage *    gst_message_new_new_clock       (GstObject * src, GstClock *clock);
351 void            gst_message_parse_new_clock     (GstMessage *message, GstClock **clock);
352
353 /* APPLICATION */
354 GstMessage *    gst_message_new_application     (GstObject * src, GstStructure * structure);
355
356 /* ELEMENT */
357 GstMessage *    gst_message_new_element         (GstObject * src, GstStructure * structure);
358
359 /* SEGMENT_START */
360 GstMessage *    gst_message_new_segment_start   (GstObject * src, GstFormat format, gint64 position);
361 void            gst_message_parse_segment_start (GstMessage *message, GstFormat *format,
362                                                  gint64 *position);
363
364 /* SEGMENT_DONE */
365 GstMessage *    gst_message_new_segment_done    (GstObject * src, GstFormat format, gint64 position);
366 void            gst_message_parse_segment_done  (GstMessage *message, GstFormat *format,
367                                                  gint64 *position);
368
369 /* DURATION */
370 GstMessage *    gst_message_new_duration        (GstObject * src, GstFormat format, gint64 duration);
371 void            gst_message_parse_duration      (GstMessage *message, GstFormat *format,
372                                                  gint64 *duration);
373
374 /* LATENCY */
375 GstMessage *    gst_message_new_latency         (GstObject * src);
376
377 /* ASYNC_START */
378 GstMessage *    gst_message_new_async_start     (GstObject * src, gboolean new_base_time);
379 void            gst_message_parse_async_start   (GstMessage *message, gboolean *new_base_time);
380
381 /* ASYNC_DONE */
382 GstMessage *    gst_message_new_async_done      (GstObject * src);
383
384 /* STRUCTURE CHANGE */
385 GstMessage *    gst_message_new_structure_change   (GstObject * src, GstStructureChangeType type,
386                                                     GstElement *owner, gboolean busy);
387 void            gst_message_parse_structure_change (GstMessage *message, GstStructureChangeType *type,
388                                                     GstElement **owner, gboolean *busy);
389
390 /* REQUEST_STATE */
391 GstMessage *    gst_message_new_request_state   (GstObject * src, GstState state);
392 void            gst_message_parse_request_state (GstMessage * message, GstState *state);
393
394 /* custom messages */
395 GstMessage *    gst_message_new_custom          (GstMessageType type,
396                                                  GstObject    * src,
397                                                  GstStructure * structure);
398 const GstStructure *  gst_message_get_structure (GstMessage *message);
399
400 G_END_DECLS
401
402 #endif /* __GST_MESSAGE_H__ */