message: Application and element messages should not have NULL structures
[platform/upstream/gstreamer.git] / gst / gstmessage.c
1 /* GStreamer
2  * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3  *
4  * gstmessage.c: 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gstmessage
24  * @short_description: Lightweight objects to signal the application of
25  *                     pipeline events
26  * @see_also: #GstBus, #GstMiniObject, #GstElement
27  *
28  * Messages are implemented as a subclass of #GstMiniObject with a generic
29  * #GstStructure as the content. This allows for writing custom messages without
30  * requiring an API change while allowing a wide range of different types
31  * of messages.
32  *
33  * Messages are posted by objects in the pipeline and are passed to the
34  * application using the #GstBus.
35  *
36  * The basic use pattern of posting a message on a #GstBus is as follows:
37  * |[
38  *   gst_bus_post (bus, gst_message_new_eos());
39  * ]|
40  *
41  * A #GstElement usually posts messages on the bus provided by the parent
42  * container using gst_element_post_message().
43  */
44
45
46 #include "gst_private.h"
47 #include <string.h>             /* memcpy */
48 #include "gsterror.h"
49 #include "gstenumtypes.h"
50 #include "gstinfo.h"
51 #include "gstmessage.h"
52 #include "gsttaglist.h"
53 #include "gstutils.h"
54 #include "gstquark.h"
55
56
57 typedef struct
58 {
59   GstMessage message;
60
61   GstStructure *structure;
62 } GstMessageImpl;
63
64 #define GST_MESSAGE_STRUCTURE(m)  (((GstMessageImpl *)(m))->structure)
65
66 typedef struct
67 {
68   const gint type;
69   const gchar *name;
70   GQuark quark;
71 } GstMessageQuarks;
72
73 static GstMessageQuarks message_quarks[] = {
74   {GST_MESSAGE_UNKNOWN, "unknown", 0},
75   {GST_MESSAGE_EOS, "eos", 0},
76   {GST_MESSAGE_ERROR, "error", 0},
77   {GST_MESSAGE_WARNING, "warning", 0},
78   {GST_MESSAGE_INFO, "info", 0},
79   {GST_MESSAGE_TAG, "tag", 0},
80   {GST_MESSAGE_BUFFERING, "buffering", 0},
81   {GST_MESSAGE_STATE_CHANGED, "state-changed", 0},
82   {GST_MESSAGE_STATE_DIRTY, "state-dirty", 0},
83   {GST_MESSAGE_STEP_DONE, "step-done", 0},
84   {GST_MESSAGE_CLOCK_PROVIDE, "clock-provide", 0},
85   {GST_MESSAGE_CLOCK_LOST, "clock-lost", 0},
86   {GST_MESSAGE_NEW_CLOCK, "new-clock", 0},
87   {GST_MESSAGE_STRUCTURE_CHANGE, "structure-change", 0},
88   {GST_MESSAGE_STREAM_STATUS, "stream-status", 0},
89   {GST_MESSAGE_APPLICATION, "application", 0},
90   {GST_MESSAGE_ELEMENT, "element", 0},
91   {GST_MESSAGE_SEGMENT_START, "segment-start", 0},
92   {GST_MESSAGE_SEGMENT_DONE, "segment-done", 0},
93   {GST_MESSAGE_DURATION_CHANGED, "duration-changed", 0},
94   {GST_MESSAGE_LATENCY, "latency", 0},
95   {GST_MESSAGE_ASYNC_START, "async-start", 0},
96   {GST_MESSAGE_ASYNC_DONE, "async-done", 0},
97   {GST_MESSAGE_REQUEST_STATE, "request-state", 0},
98   {GST_MESSAGE_STEP_START, "step-start", 0},
99   {GST_MESSAGE_QOS, "qos", 0},
100   {GST_MESSAGE_PROGRESS, "progress", 0},
101   {GST_MESSAGE_TOC, "toc", 0},
102   {GST_MESSAGE_RESET_TIME, "reset-time", 0},
103   {GST_MESSAGE_STREAM_START, "stream-start", 0},
104   {GST_MESSAGE_NEED_CONTEXT, "need-context", 0},
105   {GST_MESSAGE_HAVE_CONTEXT, "have-context", 0},
106   {GST_MESSAGE_DEVICE_ADDED, "device-added", 0},
107   {GST_MESSAGE_DEVICE_REMOVED, "device-removed", 0},
108   {0, NULL, 0}
109 };
110
111 GType _gst_message_type = 0;
112 GST_DEFINE_MINI_OBJECT_TYPE (GstMessage, gst_message);
113
114 void
115 _priv_gst_message_initialize (void)
116 {
117   gint i;
118
119   GST_CAT_INFO (GST_CAT_GST_INIT, "init messages");
120
121   /* the GstMiniObject types need to be class_ref'd once before it can be
122    * done from multiple threads;
123    * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
124   gst_message_get_type ();
125
126   for (i = 0; message_quarks[i].name; i++) {
127     message_quarks[i].quark =
128         g_quark_from_static_string (message_quarks[i].name);
129   }
130
131   _gst_message_type = gst_message_get_type ();
132 }
133
134 /**
135  * gst_message_type_get_name:
136  * @type: the message type
137  *
138  * Get a printable name for the given message type. Do not modify or free.
139  *
140  * Returns: a reference to the static name of the message.
141  */
142 const gchar *
143 gst_message_type_get_name (GstMessageType type)
144 {
145   gint i;
146
147   for (i = 0; message_quarks[i].name; i++) {
148     if (type == message_quarks[i].type)
149       return message_quarks[i].name;
150   }
151   return "unknown";
152 }
153
154 /**
155  * gst_message_type_to_quark:
156  * @type: the message type
157  *
158  * Get the unique quark for the given message type.
159  *
160  * Returns: the quark associated with the message type
161  */
162 GQuark
163 gst_message_type_to_quark (GstMessageType type)
164 {
165   gint i;
166
167   for (i = 0; message_quarks[i].name; i++) {
168     if (type == message_quarks[i].type)
169       return message_quarks[i].quark;
170   }
171   return 0;
172 }
173
174 static void
175 _gst_message_free (GstMessage * message)
176 {
177   GstStructure *structure;
178
179   g_return_if_fail (message != NULL);
180
181   GST_CAT_LOG (GST_CAT_MESSAGE, "finalize message %p, %s from %s", message,
182       GST_MESSAGE_TYPE_NAME (message), GST_MESSAGE_SRC_NAME (message));
183
184   if (GST_MESSAGE_SRC (message)) {
185     gst_object_unref (GST_MESSAGE_SRC (message));
186     GST_MESSAGE_SRC (message) = NULL;
187   }
188
189   if (message->lock.p) {
190     GST_MESSAGE_LOCK (message);
191     GST_MESSAGE_SIGNAL (message);
192     GST_MESSAGE_UNLOCK (message);
193   }
194
195   structure = GST_MESSAGE_STRUCTURE (message);
196   if (structure) {
197     gst_structure_set_parent_refcount (structure, NULL);
198     gst_structure_free (structure);
199   }
200
201   g_slice_free1 (sizeof (GstMessageImpl), message);
202 }
203
204 static void
205 gst_message_init (GstMessageImpl * message, GstMessageType type,
206     GstObject * src);
207
208 static GstMessage *
209 _gst_message_copy (GstMessage * message)
210 {
211   GstMessageImpl *copy;
212   GstStructure *structure;
213
214   GST_CAT_LOG (GST_CAT_MESSAGE, "copy message %p, %s from %s", message,
215       GST_MESSAGE_TYPE_NAME (message),
216       GST_OBJECT_NAME (GST_MESSAGE_SRC (message)));
217
218   copy = g_slice_new0 (GstMessageImpl);
219
220   gst_message_init (copy, GST_MESSAGE_TYPE (message),
221       GST_MESSAGE_SRC (message));
222
223   GST_MESSAGE_TIMESTAMP (copy) = GST_MESSAGE_TIMESTAMP (message);
224   GST_MESSAGE_SEQNUM (copy) = GST_MESSAGE_SEQNUM (message);
225
226   structure = GST_MESSAGE_STRUCTURE (message);
227   if (structure) {
228     GST_MESSAGE_STRUCTURE (copy) = gst_structure_copy (structure);
229     gst_structure_set_parent_refcount (GST_MESSAGE_STRUCTURE (copy),
230         &copy->message.mini_object.refcount);
231   } else {
232     GST_MESSAGE_STRUCTURE (copy) = NULL;
233   }
234
235   return GST_MESSAGE_CAST (copy);
236 }
237
238 static void
239 gst_message_init (GstMessageImpl * message, GstMessageType type,
240     GstObject * src)
241 {
242   gst_mini_object_init (GST_MINI_OBJECT_CAST (message), 0, _gst_message_type,
243       (GstMiniObjectCopyFunction) _gst_message_copy, NULL,
244       (GstMiniObjectFreeFunction) _gst_message_free);
245
246   GST_MESSAGE_TYPE (message) = type;
247   if (src)
248     gst_object_ref (src);
249   GST_MESSAGE_SRC (message) = src;
250   GST_MESSAGE_TIMESTAMP (message) = GST_CLOCK_TIME_NONE;
251   GST_MESSAGE_SEQNUM (message) = gst_util_seqnum_next ();
252 }
253
254
255 /**
256  * gst_message_new_custom:
257  * @type: The #GstMessageType to distinguish messages
258  * @src: The object originating the message.
259  * @structure: (transfer full) (allow-none): the structure for the
260  *     message. The message will take ownership of the structure.
261  *
262  * Create a new custom-typed message. This can be used for anything not
263  * handled by other message-specific functions to pass a message to the
264  * app. The structure field can be %NULL.
265  *
266  * Returns: (transfer full): The new message.
267  *
268  * MT safe.
269  */
270 GstMessage *
271 gst_message_new_custom (GstMessageType type, GstObject * src,
272     GstStructure * structure)
273 {
274   GstMessageImpl *message;
275
276   message = g_slice_new0 (GstMessageImpl);
277
278   GST_CAT_LOG (GST_CAT_MESSAGE, "source %s: creating new message %p %s",
279       (src ? GST_OBJECT_NAME (src) : "NULL"), message,
280       gst_message_type_get_name (type));
281
282   if (structure) {
283     /* structure must not have a parent */
284     if (!gst_structure_set_parent_refcount (structure,
285             &message->message.mini_object.refcount))
286       goto had_parent;
287   }
288   gst_message_init (message, type, src);
289
290   GST_MESSAGE_STRUCTURE (message) = structure;
291
292   return GST_MESSAGE_CAST (message);
293
294   /* ERRORS */
295 had_parent:
296   {
297     g_slice_free1 (sizeof (GstMessageImpl), message);
298     g_warning ("structure is already owned by another object");
299     return NULL;
300   }
301 }
302
303 /**
304  * gst_message_get_seqnum:
305  * @message: A #GstMessage.
306  *
307  * Retrieve the sequence number of a message.
308  *
309  * Messages have ever-incrementing sequence numbers, which may also be set
310  * explicitly via gst_message_set_seqnum(). Sequence numbers are typically used
311  * to indicate that a message corresponds to some other set of messages or
312  * events, for example a SEGMENT_DONE message corresponding to a SEEK event. It
313  * is considered good practice to make this correspondence when possible, though
314  * it is not required.
315  *
316  * Note that events and messages share the same sequence number incrementor;
317  * two events or messages will never have the same sequence number unless
318  * that correspondence was made explicitly.
319  *
320  * Returns: The message's sequence number.
321  *
322  * MT safe.
323  */
324 guint32
325 gst_message_get_seqnum (GstMessage * message)
326 {
327   g_return_val_if_fail (GST_IS_MESSAGE (message), -1);
328
329   return GST_MESSAGE_SEQNUM (message);
330 }
331
332 /**
333  * gst_message_set_seqnum:
334  * @message: A #GstMessage.
335  * @seqnum: A sequence number.
336  *
337  * Set the sequence number of a message.
338  *
339  * This function might be called by the creator of a message to indicate that
340  * the message relates to other messages or events. See gst_message_get_seqnum()
341  * for more information.
342  *
343  * MT safe.
344  */
345 void
346 gst_message_set_seqnum (GstMessage * message, guint32 seqnum)
347 {
348   g_return_if_fail (GST_IS_MESSAGE (message));
349
350   GST_MESSAGE_SEQNUM (message) = seqnum;
351 }
352
353 /**
354  * gst_message_new_eos:
355  * @src: (transfer none): The object originating the message.
356  *
357  * Create a new eos message. This message is generated and posted in
358  * the sink elements of a GstBin. The bin will only forward the EOS
359  * message to the application if all sinks have posted an EOS message.
360  *
361  * Returns: (transfer full): The new eos message.
362  *
363  * MT safe.
364  */
365 GstMessage *
366 gst_message_new_eos (GstObject * src)
367 {
368   GstMessage *message;
369
370   message = gst_message_new_custom (GST_MESSAGE_EOS, src, NULL);
371
372   return message;
373 }
374
375 /**
376  * gst_message_new_error:
377  * @src: (transfer none): The object originating the message.
378  * @error: (transfer none): The GError for this message.
379  * @debug: A debugging string.
380  *
381  * Create a new error message. The message will copy @error and
382  * @debug. This message is posted by element when a fatal event
383  * occurred. The pipeline will probably (partially) stop. The application
384  * receiving this message should stop the pipeline.
385  *
386  * Returns: (transfer full): the new error message.
387  *
388  * MT safe.
389  */
390 GstMessage *
391 gst_message_new_error (GstObject * src, GError * error, const gchar * debug)
392 {
393   GstMessage *message;
394   GstStructure *structure;
395
396   structure = gst_structure_new_id (GST_QUARK (MESSAGE_ERROR),
397       GST_QUARK (GERROR), G_TYPE_ERROR, error,
398       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
399   message = gst_message_new_custom (GST_MESSAGE_ERROR, src, structure);
400
401   return message;
402 }
403
404 /**
405  * gst_message_new_warning:
406  * @src: (transfer none): The object originating the message.
407  * @error: (transfer none): The GError for this message.
408  * @debug: A debugging string.
409  *
410  * Create a new warning message. The message will make copies of @error and
411  * @debug.
412  *
413  * Returns: (transfer full): The new warning message.
414  *
415  * MT safe.
416  */
417 GstMessage *
418 gst_message_new_warning (GstObject * src, GError * error, const gchar * debug)
419 {
420   GstMessage *message;
421   GstStructure *structure;
422
423   structure = gst_structure_new_id (GST_QUARK (MESSAGE_WARNING),
424       GST_QUARK (GERROR), G_TYPE_ERROR, error,
425       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
426   message = gst_message_new_custom (GST_MESSAGE_WARNING, src, structure);
427
428   return message;
429 }
430
431 /**
432  * gst_message_new_info:
433  * @src: (transfer none): The object originating the message.
434  * @error: (transfer none): The GError for this message.
435  * @debug: A debugging string.
436  *
437  * Create a new info message. The message will make copies of @error and
438  * @debug.
439  *
440  * MT safe.
441  *
442  * Returns: (transfer full): the new info message.
443  */
444 GstMessage *
445 gst_message_new_info (GstObject * src, GError * error, const gchar * debug)
446 {
447   GstMessage *message;
448   GstStructure *structure;
449
450   structure = gst_structure_new_id (GST_QUARK (MESSAGE_INFO),
451       GST_QUARK (GERROR), G_TYPE_ERROR, error,
452       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
453   message = gst_message_new_custom (GST_MESSAGE_INFO, src, structure);
454
455   return message;
456 }
457
458 /**
459  * gst_message_new_tag:
460  * @src: (transfer none): The object originating the message.
461  * @tag_list: (transfer full): the tag list for the message.
462  *
463  * Create a new tag message. The message will take ownership of the tag list.
464  * The message is posted by elements that discovered a new taglist.
465  *
466  * Returns: (transfer full): the new tag message.
467  *
468  * MT safe.
469  */
470 GstMessage *
471 gst_message_new_tag (GstObject * src, GstTagList * tag_list)
472 {
473   GstStructure *s;
474   GstMessage *message;
475   GValue val = G_VALUE_INIT;
476
477   g_return_val_if_fail (GST_IS_TAG_LIST (tag_list), NULL);
478
479   s = gst_structure_new_id_empty (GST_QUARK (MESSAGE_TAG));
480   g_value_init (&val, GST_TYPE_TAG_LIST);
481   g_value_take_boxed (&val, tag_list);
482   gst_structure_id_take_value (s, GST_QUARK (TAGLIST), &val);
483   message = gst_message_new_custom (GST_MESSAGE_TAG, src, s);
484   return message;
485 }
486
487 /**
488  * gst_message_new_buffering:
489  * @src: (transfer none): The object originating the message.
490  * @percent: The buffering percent
491  *
492  * Create a new buffering message. This message can be posted by an element that
493  * needs to buffer data before it can continue processing. @percent should be a
494  * value between 0 and 100. A value of 100 means that the buffering completed.
495  *
496  * When @percent is < 100 the application should PAUSE a PLAYING pipeline. When
497  * @percent is 100, the application can set the pipeline (back) to PLAYING.
498  * The application must be prepared to receive BUFFERING messages in the
499  * PREROLLING state and may only set the pipeline to PLAYING after receiving a
500  * message with @percent set to 100, which can happen after the pipeline
501  * completed prerolling.
502  *
503  * MT safe.
504  *
505  * Returns: (transfer full): The new buffering message.
506  */
507 GstMessage *
508 gst_message_new_buffering (GstObject * src, gint percent)
509 {
510   GstMessage *message;
511   GstStructure *structure;
512   gint64 buffering_left;
513
514   g_return_val_if_fail (percent >= 0 && percent <= 100, NULL);
515
516   buffering_left = (percent == 100 ? 0 : -1);
517
518   structure = gst_structure_new_id (GST_QUARK (MESSAGE_BUFFERING),
519       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent,
520       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
521       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
522       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
523       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
524   message = gst_message_new_custom (GST_MESSAGE_BUFFERING, src, structure);
525
526   return message;
527 }
528
529 /**
530  * gst_message_new_state_changed:
531  * @src: (transfer none): the object originating the message
532  * @oldstate: the previous state
533  * @newstate: the new (current) state
534  * @pending: the pending (target) state
535  *
536  * Create a state change message. This message is posted whenever an element
537  * changed its state.
538  *
539  * Returns: (transfer full): the new state change message.
540  *
541  * MT safe.
542  */
543 GstMessage *
544 gst_message_new_state_changed (GstObject * src,
545     GstState oldstate, GstState newstate, GstState pending)
546 {
547   GstMessage *message;
548   GstStructure *structure;
549
550   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STATE_CHANGED),
551       GST_QUARK (OLD_STATE), GST_TYPE_STATE, (gint) oldstate,
552       GST_QUARK (NEW_STATE), GST_TYPE_STATE, (gint) newstate,
553       GST_QUARK (PENDING_STATE), GST_TYPE_STATE, (gint) pending, NULL);
554   message = gst_message_new_custom (GST_MESSAGE_STATE_CHANGED, src, structure);
555
556   return message;
557 }
558
559 /**
560  * gst_message_new_state_dirty:
561  * @src: (transfer none): the object originating the message
562  *
563  * Create a state dirty message. This message is posted whenever an element
564  * changed its state asynchronously and is used internally to update the
565  * states of container objects.
566  *
567  * Returns: (transfer full): the new state dirty message.
568  *
569  * MT safe.
570  */
571 GstMessage *
572 gst_message_new_state_dirty (GstObject * src)
573 {
574   GstMessage *message;
575
576   message = gst_message_new_custom (GST_MESSAGE_STATE_DIRTY, src, NULL);
577
578   return message;
579 }
580
581 /**
582  * gst_message_new_clock_provide:
583  * @src: (transfer none): the object originating the message.
584  * @clock: (transfer none): the clock it provides
585  * @ready: %TRUE if the sender can provide a clock
586  *
587  * Create a clock provide message. This message is posted whenever an
588  * element is ready to provide a clock or lost its ability to provide
589  * a clock (maybe because it paused or became EOS).
590  *
591  * This message is mainly used internally to manage the clock
592  * selection.
593  *
594  * Returns: (transfer full): the new provide clock message.
595  *
596  * MT safe.
597  */
598 GstMessage *
599 gst_message_new_clock_provide (GstObject * src, GstClock * clock,
600     gboolean ready)
601 {
602   GstMessage *message;
603   GstStructure *structure;
604
605   structure = gst_structure_new_id (GST_QUARK (MESSAGE_CLOCK_PROVIDE),
606       GST_QUARK (CLOCK), GST_TYPE_CLOCK, clock,
607       GST_QUARK (READY), G_TYPE_BOOLEAN, ready, NULL);
608   message = gst_message_new_custom (GST_MESSAGE_CLOCK_PROVIDE, src, structure);
609
610   return message;
611 }
612
613 /**
614  * gst_message_new_clock_lost:
615  * @src: (transfer none): the object originating the message.
616  * @clock: (transfer none): the clock that was lost
617  *
618  * Create a clock lost message. This message is posted whenever the
619  * clock is not valid anymore.
620  *
621  * If this message is posted by the pipeline, the pipeline will
622  * select a new clock again when it goes to PLAYING. It might therefore
623  * be needed to set the pipeline to PAUSED and PLAYING again.
624  *
625  * Returns: (transfer full): The new clock lost message.
626  *
627  * MT safe.
628  */
629 GstMessage *
630 gst_message_new_clock_lost (GstObject * src, GstClock * clock)
631 {
632   GstMessage *message;
633   GstStructure *structure;
634
635   structure = gst_structure_new_id (GST_QUARK (MESSAGE_CLOCK_LOST),
636       GST_QUARK (CLOCK), GST_TYPE_CLOCK, clock, NULL);
637   message = gst_message_new_custom (GST_MESSAGE_CLOCK_LOST, src, structure);
638
639   return message;
640 }
641
642 /**
643  * gst_message_new_new_clock:
644  * @src: (transfer none): The object originating the message.
645  * @clock: (transfer none): the new selected clock
646  *
647  * Create a new clock message. This message is posted whenever the
648  * pipeline selects a new clock for the pipeline.
649  *
650  * Returns: (transfer full): The new new clock message.
651  *
652  * MT safe.
653  */
654 GstMessage *
655 gst_message_new_new_clock (GstObject * src, GstClock * clock)
656 {
657   GstMessage *message;
658   GstStructure *structure;
659
660   structure = gst_structure_new_id (GST_QUARK (MESSAGE_NEW_CLOCK),
661       GST_QUARK (CLOCK), GST_TYPE_CLOCK, clock, NULL);
662   message = gst_message_new_custom (GST_MESSAGE_NEW_CLOCK, src, structure);
663
664   return message;
665 }
666
667 /**
668  * gst_message_new_structure_change:
669  * @src: (transfer none): The object originating the message.
670  * @type: The change type.
671  * @owner: (transfer none): The owner element of @src.
672  * @busy: Whether the structure change is busy.
673  *
674  * Create a new structure change message. This message is posted when the
675  * structure of a pipeline is in the process of being changed, for example
676  * when pads are linked or unlinked.
677  *
678  * @src should be the sinkpad that unlinked or linked.
679  *
680  * Returns: (transfer full): the new structure change message.
681  *
682  * MT safe.
683  */
684 GstMessage *
685 gst_message_new_structure_change (GstObject * src, GstStructureChangeType type,
686     GstElement * owner, gboolean busy)
687 {
688   GstMessage *message;
689   GstStructure *structure;
690
691   g_return_val_if_fail (GST_IS_PAD (src), NULL);
692   /* g_return_val_if_fail (GST_PAD_DIRECTION (src) == GST_PAD_SINK, NULL); */
693   g_return_val_if_fail (GST_IS_ELEMENT (owner), NULL);
694
695   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STRUCTURE_CHANGE),
696       GST_QUARK (TYPE), GST_TYPE_STRUCTURE_CHANGE_TYPE, type,
697       GST_QUARK (OWNER), GST_TYPE_ELEMENT, owner,
698       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy, NULL);
699
700   message = gst_message_new_custom (GST_MESSAGE_STRUCTURE_CHANGE, src,
701       structure);
702
703   return message;
704 }
705
706 /**
707  * gst_message_new_segment_start:
708  * @src: (transfer none): The object originating the message.
709  * @format: The format of the position being played
710  * @position: The position of the segment being played
711  *
712  * Create a new segment message. This message is posted by elements that
713  * start playback of a segment as a result of a segment seek. This message
714  * is not received by the application but is used for maintenance reasons in
715  * container elements.
716  *
717  * Returns: (transfer full): the new segment start message.
718  *
719  * MT safe.
720  */
721 GstMessage *
722 gst_message_new_segment_start (GstObject * src, GstFormat format,
723     gint64 position)
724 {
725   GstMessage *message;
726   GstStructure *structure;
727
728   structure = gst_structure_new_id (GST_QUARK (MESSAGE_SEGMENT_START),
729       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
730       GST_QUARK (POSITION), G_TYPE_INT64, position, NULL);
731   message = gst_message_new_custom (GST_MESSAGE_SEGMENT_START, src, structure);
732
733   return message;
734 }
735
736 /**
737  * gst_message_new_segment_done:
738  * @src: (transfer none): the object originating the message.
739  * @format: The format of the position being done
740  * @position: The position of the segment being done
741  *
742  * Create a new segment done message. This message is posted by elements that
743  * finish playback of a segment as a result of a segment seek. This message
744  * is received by the application after all elements that posted a segment_start
745  * have posted the segment_done.
746  *
747  * Returns: (transfer full): the new segment done message.
748  *
749  * MT safe.
750  */
751 GstMessage *
752 gst_message_new_segment_done (GstObject * src, GstFormat format,
753     gint64 position)
754 {
755   GstMessage *message;
756   GstStructure *structure;
757
758   structure = gst_structure_new_id (GST_QUARK (MESSAGE_SEGMENT_DONE),
759       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
760       GST_QUARK (POSITION), G_TYPE_INT64, position, NULL);
761   message = gst_message_new_custom (GST_MESSAGE_SEGMENT_DONE, src, structure);
762
763   return message;
764 }
765
766 /**
767  * gst_message_new_application:
768  * @src: (transfer none): the object originating the message.
769  * @structure: (transfer full): the structure for the message. The message
770  *     will take ownership of the structure.
771  *
772  * Create a new application-typed message. GStreamer will never create these
773  * messages; they are a gift from us to you. Enjoy.
774  *
775  * Returns: (transfer full): The new application message.
776  *
777  * MT safe.
778  */
779 GstMessage *
780 gst_message_new_application (GstObject * src, GstStructure * structure)
781 {
782   g_return_val_if_fail (structure != NULL, NULL);
783
784   return gst_message_new_custom (GST_MESSAGE_APPLICATION, src, structure);
785 }
786
787 /**
788  * gst_message_new_element:
789  * @src: (transfer none): The object originating the message.
790  * @structure: (transfer full): The structure for the
791  *     message. The message will take ownership of the structure.
792  *
793  * Create a new element-specific message. This is meant as a generic way of
794  * allowing one-way communication from an element to an application, for example
795  * "the firewire cable was unplugged". The format of the message should be
796  * documented in the element's documentation. The structure field can be %NULL.
797  *
798  * Returns: (transfer full): The new element message.
799  *
800  * MT safe.
801  */
802 GstMessage *
803 gst_message_new_element (GstObject * src, GstStructure * structure)
804 {
805   g_return_val_if_fail (structure != NULL, NULL);
806
807   return gst_message_new_custom (GST_MESSAGE_ELEMENT, src, structure);
808 }
809
810 /**
811  * gst_message_new_duration_changed:
812  * @src: (transfer none): The object originating the message.
813  *
814  * Create a new duration changed message. This message is posted by elements
815  * that know the duration of a stream when the duration changes. This message
816  * is received by bins and is used to calculate the total duration of a
817  * pipeline. Elements may post a duration message with a duration of
818  * GST_CLOCK_TIME_NONE to indicate that the duration has changed and the 
819  * cached duration should be discarded. The new duration can then be 
820  * retrieved via a query.
821  *
822  * Returns: (transfer full): The new duration-changed message.
823  *
824  * MT safe.
825  */
826 GstMessage *
827 gst_message_new_duration_changed (GstObject * src)
828 {
829   GstMessage *message;
830
831   message = gst_message_new_custom (GST_MESSAGE_DURATION_CHANGED, src,
832       gst_structure_new_id_empty (GST_QUARK (MESSAGE_DURATION_CHANGED)));
833
834   return message;
835 }
836
837 /**
838  * gst_message_new_async_start:
839  * @src: (transfer none): The object originating the message.
840  *
841  * This message is posted by elements when they start an ASYNC state change.
842  *
843  * Returns: (transfer full): The new async_start message.
844  *
845  * MT safe.
846  */
847 GstMessage *
848 gst_message_new_async_start (GstObject * src)
849 {
850   GstMessage *message;
851
852   message = gst_message_new_custom (GST_MESSAGE_ASYNC_START, src, NULL);
853
854   return message;
855 }
856
857 /**
858  * gst_message_new_async_done:
859  * @src: (transfer none): The object originating the message.
860  * @running_time: the desired running_time
861  *
862  * The message is posted when elements completed an ASYNC state change.
863  * @running_time contains the time of the desired running_time when this
864  * elements goes to PLAYING. A value of #GST_CLOCK_TIME_NONE for @running_time
865  * means that the element has no clock interaction and thus doesn't care about
866  * the running_time of the pipeline.
867  *
868  * Returns: (transfer full): The new async_done message.
869  *
870  * MT safe.
871  */
872 GstMessage *
873 gst_message_new_async_done (GstObject * src, GstClockTime running_time)
874 {
875   GstMessage *message;
876   GstStructure *structure;
877
878   structure = gst_structure_new_id (GST_QUARK (MESSAGE_ASYNC_DONE),
879       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time, NULL);
880   message = gst_message_new_custom (GST_MESSAGE_ASYNC_DONE, src, structure);
881
882   return message;
883 }
884
885 /**
886  * gst_message_new_latency:
887  * @src: (transfer none): The object originating the message.
888  *
889  * This message can be posted by elements when their latency requirements have
890  * changed.
891  *
892  * Returns: (transfer full): The new latency message.
893  *
894  * MT safe.
895  */
896 GstMessage *
897 gst_message_new_latency (GstObject * src)
898 {
899   GstMessage *message;
900
901   message = gst_message_new_custom (GST_MESSAGE_LATENCY, src, NULL);
902
903   return message;
904 }
905
906 /**
907  * gst_message_new_request_state:
908  * @src: (transfer none): the object originating the message.
909  * @state: The new requested state
910  *
911  * This message can be posted by elements when they want to have their state
912  * changed. A typical use case would be an audio server that wants to pause the
913  * pipeline because a higher priority stream is being played.
914  *
915  * Returns: (transfer full): the new request state message.
916  *
917  * MT safe.
918  */
919 GstMessage *
920 gst_message_new_request_state (GstObject * src, GstState state)
921 {
922   GstMessage *message;
923   GstStructure *structure;
924
925   structure = gst_structure_new_id (GST_QUARK (MESSAGE_REQUEST_STATE),
926       GST_QUARK (NEW_STATE), GST_TYPE_STATE, (gint) state, NULL);
927   message = gst_message_new_custom (GST_MESSAGE_REQUEST_STATE, src, structure);
928
929   return message;
930 }
931
932 /**
933  * gst_message_get_structure:
934  * @message: The #GstMessage.
935  *
936  * Access the structure of the message.
937  *
938  * Returns: (transfer none): The structure of the message. The structure is
939  * still owned by the message, which means that you should not free it and
940  * that the pointer becomes invalid when you free the message.
941  *
942  * MT safe.
943  */
944 const GstStructure *
945 gst_message_get_structure (GstMessage * message)
946 {
947   g_return_val_if_fail (GST_IS_MESSAGE (message), NULL);
948
949   return GST_MESSAGE_STRUCTURE (message);
950 }
951
952 /**
953  * gst_message_has_name:
954  * @message: The #GstMessage.
955  * @name: name to check
956  *
957  * Checks if @message has the given @name. This function is usually used to
958  * check the name of a custom message.
959  *
960  * Returns: %TRUE if @name matches the name of the message structure.
961  */
962 gboolean
963 gst_message_has_name (GstMessage * message, const gchar * name)
964 {
965   GstStructure *structure;
966
967   g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
968
969   structure = GST_MESSAGE_STRUCTURE (message);
970   if (structure == NULL)
971     return FALSE;
972
973   return gst_structure_has_name (structure, name);
974 }
975
976 /**
977  * gst_message_parse_tag:
978  * @message: A valid #GstMessage of type GST_MESSAGE_TAG.
979  * @tag_list: (out callee-allocates): return location for the tag-list.
980  *
981  * Extracts the tag list from the GstMessage. The tag list returned in the
982  * output argument is a copy; the caller must free it when done.
983  *
984  * Typical usage of this function might be:
985  * |[
986  *   ...
987  *   switch (GST_MESSAGE_TYPE (msg)) {
988  *     case GST_MESSAGE_TAG: {
989  *       GstTagList *tags = NULL;
990  *       
991  *       gst_message_parse_tag (msg, &amp;tags);
992  *       g_print ("Got tags from element %s\n", GST_OBJECT_NAME (msg->src));
993  *       handle_tags (tags);
994  *       gst_tag_list_unref (tags);
995  *       break;
996  *     }
997  *     ...
998  *   }
999  *   ...
1000  * ]|
1001  *
1002  * MT safe.
1003  */
1004 void
1005 gst_message_parse_tag (GstMessage * message, GstTagList ** tag_list)
1006 {
1007   g_return_if_fail (GST_IS_MESSAGE (message));
1008   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_TAG);
1009   g_return_if_fail (tag_list != NULL);
1010
1011   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1012       GST_QUARK (TAGLIST), GST_TYPE_TAG_LIST, tag_list, NULL);
1013 }
1014
1015 /**
1016  * gst_message_parse_buffering:
1017  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1018  * @percent: (out) (allow-none): Return location for the percent.
1019  *
1020  * Extracts the buffering percent from the GstMessage. see also
1021  * gst_message_new_buffering().
1022  *
1023  * MT safe.
1024  */
1025 void
1026 gst_message_parse_buffering (GstMessage * message, gint * percent)
1027 {
1028   g_return_if_fail (GST_IS_MESSAGE (message));
1029   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1030
1031   if (percent)
1032     *percent =
1033         g_value_get_int (gst_structure_id_get_value (GST_MESSAGE_STRUCTURE
1034             (message), GST_QUARK (BUFFER_PERCENT)));
1035 }
1036
1037 /**
1038  * gst_message_set_buffering_stats:
1039  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1040  * @mode: a buffering mode 
1041  * @avg_in: the average input rate
1042  * @avg_out: the average output rate
1043  * @buffering_left: amount of buffering time left in milliseconds
1044  *
1045  * Configures the buffering stats values in @message.
1046  */
1047 void
1048 gst_message_set_buffering_stats (GstMessage * message, GstBufferingMode mode,
1049     gint avg_in, gint avg_out, gint64 buffering_left)
1050 {
1051   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1052
1053   gst_structure_id_set (GST_MESSAGE_STRUCTURE (message),
1054       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1055       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1056       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1057       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1058 }
1059
1060 /**
1061  * gst_message_parse_buffering_stats:
1062  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1063  * @mode: (out) (allow-none): a buffering mode, or %NULL
1064  * @avg_in: (out) (allow-none): the average input rate, or %NULL
1065  * @avg_out: (out) (allow-none): the average output rate, or %NULL
1066  * @buffering_left: (out) (allow-none): amount of buffering time left in
1067  *     milliseconds, or %NULL
1068  *
1069  * Extracts the buffering stats values from @message.
1070  */
1071 void
1072 gst_message_parse_buffering_stats (GstMessage * message,
1073     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1074     gint64 * buffering_left)
1075 {
1076   GstStructure *structure;
1077
1078   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1079
1080   structure = GST_MESSAGE_STRUCTURE (message);
1081   if (mode)
1082     *mode = (GstBufferingMode)
1083         g_value_get_enum (gst_structure_id_get_value (structure,
1084             GST_QUARK (BUFFERING_MODE)));
1085   if (avg_in)
1086     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1087             GST_QUARK (AVG_IN_RATE)));
1088   if (avg_out)
1089     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1090             GST_QUARK (AVG_OUT_RATE)));
1091   if (buffering_left)
1092     *buffering_left =
1093         g_value_get_int64 (gst_structure_id_get_value (structure,
1094             GST_QUARK (BUFFERING_LEFT)));
1095 }
1096
1097 /**
1098  * gst_message_parse_state_changed:
1099  * @message: a valid #GstMessage of type GST_MESSAGE_STATE_CHANGED
1100  * @oldstate: (out) (allow-none): the previous state, or %NULL
1101  * @newstate: (out) (allow-none): the new (current) state, or %NULL
1102  * @pending: (out) (allow-none): the pending (target) state, or %NULL
1103  *
1104  * Extracts the old and new states from the GstMessage.
1105  *
1106  * Typical usage of this function might be:
1107  * |[
1108  *   ...
1109  *   switch (GST_MESSAGE_TYPE (msg)) {
1110  *     case GST_MESSAGE_STATE_CHANGED: {
1111  *       GstState old_state, new_state;
1112  *       
1113  *       gst_message_parse_state_changed (msg, &amp;old_state, &amp;new_state, NULL);
1114  *       g_print ("Element %s changed state from %s to %s.\n",
1115  *           GST_OBJECT_NAME (msg->src),
1116  *           gst_element_state_get_name (old_state),
1117  *           gst_element_state_get_name (new_state));
1118  *       break;
1119  *     }
1120  *     ...
1121  *   }
1122  *   ...
1123  * ]|
1124  *
1125  * MT safe.
1126  */
1127 void
1128 gst_message_parse_state_changed (GstMessage * message,
1129     GstState * oldstate, GstState * newstate, GstState * pending)
1130 {
1131   GstStructure *structure;
1132
1133   g_return_if_fail (GST_IS_MESSAGE (message));
1134   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED);
1135
1136   structure = GST_MESSAGE_STRUCTURE (message);
1137   if (oldstate)
1138     *oldstate = (GstState)
1139         g_value_get_enum (gst_structure_id_get_value (structure,
1140             GST_QUARK (OLD_STATE)));
1141   if (newstate)
1142     *newstate = (GstState)
1143         g_value_get_enum (gst_structure_id_get_value (structure,
1144             GST_QUARK (NEW_STATE)));
1145   if (pending)
1146     *pending = (GstState)
1147         g_value_get_enum (gst_structure_id_get_value (structure,
1148             GST_QUARK (PENDING_STATE)));
1149 }
1150
1151 /**
1152  * gst_message_parse_clock_provide:
1153  * @message: A valid #GstMessage of type GST_MESSAGE_CLOCK_PROVIDE.
1154  * @clock: (out) (allow-none) (transfer none): a pointer to  hold a clock
1155  *     object, or %NULL
1156  * @ready: (out) (allow-none): a pointer to hold the ready flag, or %NULL
1157  *
1158  * Extracts the clock and ready flag from the GstMessage.
1159  * The clock object returned remains valid until the message is freed.
1160  *
1161  * MT safe.
1162  */
1163 void
1164 gst_message_parse_clock_provide (GstMessage * message, GstClock ** clock,
1165     gboolean * ready)
1166 {
1167   const GValue *clock_gvalue;
1168   GstStructure *structure;
1169
1170   g_return_if_fail (GST_IS_MESSAGE (message));
1171   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_CLOCK_PROVIDE);
1172
1173   structure = GST_MESSAGE_STRUCTURE (message);
1174   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1175   g_return_if_fail (clock_gvalue != NULL);
1176   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1177
1178   if (ready)
1179     *ready =
1180         g_value_get_boolean (gst_structure_id_get_value (structure,
1181             GST_QUARK (READY)));
1182   if (clock)
1183     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1184 }
1185
1186 /**
1187  * gst_message_parse_clock_lost:
1188  * @message: A valid #GstMessage of type GST_MESSAGE_CLOCK_LOST.
1189  * @clock: (out) (allow-none) (transfer none): a pointer to hold the lost clock
1190  *
1191  * Extracts the lost clock from the GstMessage.
1192  * The clock object returned remains valid until the message is freed.
1193  *
1194  * MT safe.
1195  */
1196 void
1197 gst_message_parse_clock_lost (GstMessage * message, GstClock ** clock)
1198 {
1199   const GValue *clock_gvalue;
1200   GstStructure *structure;
1201
1202   g_return_if_fail (GST_IS_MESSAGE (message));
1203   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_CLOCK_LOST);
1204
1205   structure = GST_MESSAGE_STRUCTURE (message);
1206   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1207   g_return_if_fail (clock_gvalue != NULL);
1208   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1209
1210   if (clock)
1211     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1212 }
1213
1214 /**
1215  * gst_message_parse_new_clock:
1216  * @message: A valid #GstMessage of type GST_MESSAGE_NEW_CLOCK.
1217  * @clock: (out) (allow-none) (transfer none): a pointer to hold the selected
1218  *     new clock
1219  *
1220  * Extracts the new clock from the GstMessage.
1221  * The clock object returned remains valid until the message is freed.
1222  *
1223  * MT safe.
1224  */
1225 void
1226 gst_message_parse_new_clock (GstMessage * message, GstClock ** clock)
1227 {
1228   const GValue *clock_gvalue;
1229   GstStructure *structure;
1230
1231   g_return_if_fail (GST_IS_MESSAGE (message));
1232   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEW_CLOCK);
1233
1234   structure = GST_MESSAGE_STRUCTURE (message);
1235   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1236   g_return_if_fail (clock_gvalue != NULL);
1237   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1238
1239   if (clock)
1240     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1241 }
1242
1243 /**
1244  * gst_message_parse_structure_change:
1245  * @message: A valid #GstMessage of type GST_MESSAGE_STRUCTURE_CHANGE.
1246  * @type: (out): A pointer to hold the change type
1247  * @owner: (out) (allow-none) (transfer none): The owner element of the
1248  *     message source
1249  * @busy: (out) (allow-none): a pointer to hold whether the change is in
1250  *     progress or has been completed
1251  *
1252  * Extracts the change type and completion status from the GstMessage.
1253  *
1254  * MT safe.
1255  */
1256 void
1257 gst_message_parse_structure_change (GstMessage * message,
1258     GstStructureChangeType * type, GstElement ** owner, gboolean * busy)
1259 {
1260   const GValue *owner_gvalue;
1261   GstStructure *structure;
1262
1263   g_return_if_fail (GST_IS_MESSAGE (message));
1264   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STRUCTURE_CHANGE);
1265
1266   structure = GST_MESSAGE_STRUCTURE (message);
1267   owner_gvalue = gst_structure_id_get_value (structure, GST_QUARK (OWNER));
1268   g_return_if_fail (owner_gvalue != NULL);
1269   g_return_if_fail (G_VALUE_TYPE (owner_gvalue) == GST_TYPE_ELEMENT);
1270
1271   if (type)
1272     *type = (GstStructureChangeType)
1273         g_value_get_enum (gst_structure_id_get_value (structure,
1274             GST_QUARK (TYPE)));
1275   if (owner)
1276     *owner = (GstElement *) g_value_get_object (owner_gvalue);
1277   if (busy)
1278     *busy =
1279         g_value_get_boolean (gst_structure_id_get_value (structure,
1280             GST_QUARK (BUSY)));
1281 }
1282
1283 /**
1284  * gst_message_parse_error:
1285  * @message: A valid #GstMessage of type GST_MESSAGE_ERROR.
1286  * @gerror: (out) (allow-none) (transfer full): location for the GError
1287  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1288  *     or %NULL
1289  *
1290  * Extracts the GError and debug string from the GstMessage. The values returned
1291  * in the output arguments are copies; the caller must free them when done.
1292  *
1293  * Typical usage of this function might be:
1294  * |[
1295  *   ...
1296  *   switch (GST_MESSAGE_TYPE (msg)) {
1297  *     case GST_MESSAGE_ERROR: {
1298  *       GError *err = NULL;
1299  *       gchar *dbg_info = NULL;
1300  *       
1301  *       gst_message_parse_error (msg, &amp;err, &amp;dbg_info);
1302  *       g_printerr ("ERROR from element %s: %s\n",
1303  *           GST_OBJECT_NAME (msg->src), err->message);
1304  *       g_printerr ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
1305  *       g_error_free (err);
1306  *       g_free (dbg_info);
1307  *       break;
1308  *     }
1309  *     ...
1310  *   }
1311  *   ...
1312  * ]|
1313  *
1314  * MT safe.
1315  */
1316 void
1317 gst_message_parse_error (GstMessage * message, GError ** gerror, gchar ** debug)
1318 {
1319   g_return_if_fail (GST_IS_MESSAGE (message));
1320   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR);
1321
1322   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1323       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1324       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1325 }
1326
1327 /**
1328  * gst_message_parse_warning:
1329  * @message: A valid #GstMessage of type GST_MESSAGE_WARNING.
1330  * @gerror: (out) (allow-none) (transfer full): location for the GError
1331  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1332  *     or %NULL
1333  *
1334  * Extracts the GError and debug string from the GstMessage. The values returned
1335  * in the output arguments are copies; the caller must free them when done.
1336  *
1337  * MT safe.
1338  */
1339 void
1340 gst_message_parse_warning (GstMessage * message, GError ** gerror,
1341     gchar ** debug)
1342 {
1343   g_return_if_fail (GST_IS_MESSAGE (message));
1344   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING);
1345
1346   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1347       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1348       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1349 }
1350
1351 /**
1352  * gst_message_parse_info:
1353  * @message: A valid #GstMessage of type GST_MESSAGE_INFO.
1354  * @gerror: (out) (allow-none) (transfer full): location for the GError
1355  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1356  *     or %NULL
1357  *
1358  * Extracts the GError and debug string from the GstMessage. The values returned
1359  * in the output arguments are copies; the caller must free them when done.
1360  *
1361  * MT safe.
1362  */
1363 void
1364 gst_message_parse_info (GstMessage * message, GError ** gerror, gchar ** debug)
1365 {
1366   g_return_if_fail (GST_IS_MESSAGE (message));
1367   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_INFO);
1368
1369   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1370       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1371       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1372 }
1373
1374 /**
1375  * gst_message_parse_segment_start:
1376  * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_START.
1377  * @format: (out) (allow-none): Result location for the format, or %NULL
1378  * @position: (out) (allow-none): Result location for the position, or %NULL
1379  *
1380  * Extracts the position and format from the segment start message.
1381  *
1382  * MT safe.
1383  */
1384 void
1385 gst_message_parse_segment_start (GstMessage * message, GstFormat * format,
1386     gint64 * position)
1387 {
1388   GstStructure *structure;
1389
1390   g_return_if_fail (GST_IS_MESSAGE (message));
1391   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_START);
1392
1393   structure = GST_MESSAGE_STRUCTURE (message);
1394   if (format)
1395     *format = (GstFormat)
1396         g_value_get_enum (gst_structure_id_get_value (structure,
1397             GST_QUARK (FORMAT)));
1398   if (position)
1399     *position =
1400         g_value_get_int64 (gst_structure_id_get_value (structure,
1401             GST_QUARK (POSITION)));
1402 }
1403
1404 /**
1405  * gst_message_parse_segment_done:
1406  * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_DONE.
1407  * @format: (out) (allow-none): Result location for the format, or %NULL
1408  * @position: (out) (allow-none): Result location for the position, or %NULL
1409  *
1410  * Extracts the position and format from the segment done message.
1411  *
1412  * MT safe.
1413  */
1414 void
1415 gst_message_parse_segment_done (GstMessage * message, GstFormat * format,
1416     gint64 * position)
1417 {
1418   GstStructure *structure;
1419
1420   g_return_if_fail (GST_IS_MESSAGE (message));
1421   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_DONE);
1422
1423   structure = GST_MESSAGE_STRUCTURE (message);
1424   if (format)
1425     *format = (GstFormat)
1426         g_value_get_enum (gst_structure_id_get_value (structure,
1427             GST_QUARK (FORMAT)));
1428   if (position)
1429     *position =
1430         g_value_get_int64 (gst_structure_id_get_value (structure,
1431             GST_QUARK (POSITION)));
1432 }
1433
1434 /**
1435  * gst_message_parse_async_done:
1436  * @message: A valid #GstMessage of type GST_MESSAGE_ASYNC_DONE.
1437  * @running_time: (out) (allow-none): Result location for the running_time or %NULL
1438  *
1439  * Extract the running_time from the async_done message.
1440  *
1441  * MT safe.
1442  */
1443 void
1444 gst_message_parse_async_done (GstMessage * message, GstClockTime * running_time)
1445 {
1446   GstStructure *structure;
1447
1448   g_return_if_fail (GST_IS_MESSAGE (message));
1449   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ASYNC_DONE);
1450
1451   structure = GST_MESSAGE_STRUCTURE (message);
1452   if (running_time)
1453     *running_time =
1454         g_value_get_uint64 (gst_structure_id_get_value (structure,
1455             GST_QUARK (RUNNING_TIME)));
1456 }
1457
1458 /**
1459  * gst_message_parse_request_state:
1460  * @message: A valid #GstMessage of type GST_MESSAGE_REQUEST_STATE.
1461  * @state: (out) (allow-none): Result location for the requested state or %NULL
1462  *
1463  * Extract the requested state from the request_state message.
1464  *
1465  * MT safe.
1466  */
1467 void
1468 gst_message_parse_request_state (GstMessage * message, GstState * state)
1469 {
1470   GstStructure *structure;
1471
1472   g_return_if_fail (GST_IS_MESSAGE (message));
1473   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_REQUEST_STATE);
1474
1475   structure = GST_MESSAGE_STRUCTURE (message);
1476   if (state)
1477     *state = (GstState)
1478         g_value_get_enum (gst_structure_id_get_value (structure,
1479             GST_QUARK (NEW_STATE)));
1480 }
1481
1482 /**
1483  * gst_message_new_stream_status:
1484  * @src: The object originating the message.
1485  * @type: The stream status type.
1486  * @owner: (transfer none): the owner element of @src.
1487  *
1488  * Create a new stream status message. This message is posted when a streaming
1489  * thread is created/destroyed or when the state changed.
1490  * 
1491  * Returns: (transfer full): the new stream status message.
1492  *
1493  * MT safe.
1494  */
1495 GstMessage *
1496 gst_message_new_stream_status (GstObject * src, GstStreamStatusType type,
1497     GstElement * owner)
1498 {
1499   GstMessage *message;
1500   GstStructure *structure;
1501
1502   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STREAM_STATUS),
1503       GST_QUARK (TYPE), GST_TYPE_STREAM_STATUS_TYPE, (gint) type,
1504       GST_QUARK (OWNER), GST_TYPE_ELEMENT, owner, NULL);
1505   message = gst_message_new_custom (GST_MESSAGE_STREAM_STATUS, src, structure);
1506
1507   return message;
1508 }
1509
1510 /**
1511  * gst_message_parse_stream_status:
1512  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1513  * @type: (out): A pointer to hold the status type
1514  * @owner: (out) (transfer none): The owner element of the message source
1515  *
1516  * Extracts the stream status type and owner the GstMessage. The returned
1517  * owner remains valid for as long as the reference to @message is valid and
1518  * should thus not be unreffed.
1519  *
1520  * MT safe.
1521  */
1522 void
1523 gst_message_parse_stream_status (GstMessage * message,
1524     GstStreamStatusType * type, GstElement ** owner)
1525 {
1526   const GValue *owner_gvalue;
1527   GstStructure *structure;
1528
1529   g_return_if_fail (GST_IS_MESSAGE (message));
1530   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS);
1531
1532   structure = GST_MESSAGE_STRUCTURE (message);
1533   owner_gvalue = gst_structure_id_get_value (structure, GST_QUARK (OWNER));
1534   g_return_if_fail (owner_gvalue != NULL);
1535
1536   if (type)
1537     *type = (GstStreamStatusType)
1538         g_value_get_enum (gst_structure_id_get_value (structure,
1539             GST_QUARK (TYPE)));
1540   if (owner)
1541     *owner = (GstElement *) g_value_get_object (owner_gvalue);
1542 }
1543
1544 /**
1545  * gst_message_set_stream_status_object:
1546  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1547  * @object: the object controlling the streaming
1548  *
1549  * Configures the object handling the streaming thread. This is usually a
1550  * GstTask object but other objects might be added in the future.
1551  */
1552 void
1553 gst_message_set_stream_status_object (GstMessage * message,
1554     const GValue * object)
1555 {
1556   GstStructure *structure;
1557
1558   g_return_if_fail (GST_IS_MESSAGE (message));
1559   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS);
1560
1561   structure = GST_MESSAGE_STRUCTURE (message);
1562   gst_structure_id_set_value (structure, GST_QUARK (OBJECT), object);
1563 }
1564
1565 /**
1566  * gst_message_get_stream_status_object:
1567  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1568  *
1569  * Extracts the object managing the streaming thread from @message.
1570  *
1571  * Returns: a GValue containing the object that manages the streaming thread.
1572  * This object is usually of type GstTask but other types can be added in the
1573  * future. The object remains valid as long as @message is valid.
1574  */
1575 const GValue *
1576 gst_message_get_stream_status_object (GstMessage * message)
1577 {
1578   const GValue *result;
1579   GstStructure *structure;
1580
1581   g_return_val_if_fail (GST_IS_MESSAGE (message), NULL);
1582   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS,
1583       NULL);
1584
1585   structure = GST_MESSAGE_STRUCTURE (message);
1586   result = gst_structure_id_get_value (structure, GST_QUARK (OBJECT));
1587
1588   return result;
1589 }
1590
1591 /**
1592  * gst_message_new_step_done:
1593  * @src: The object originating the message.
1594  * @format: the format of @amount
1595  * @amount: the amount of stepped data
1596  * @rate: the rate of the stepped amount
1597  * @flush: is this an flushing step
1598  * @intermediate: is this an intermediate step
1599  * @duration: the duration of the data
1600  * @eos: the step caused EOS
1601  *
1602  * This message is posted by elements when they complete a part, when @intermediate set
1603  * to %TRUE, or a complete step operation.
1604  *
1605  * @duration will contain the amount of time (in GST_FORMAT_TIME) of the stepped
1606  * @amount of media in format @format.
1607  *
1608  * Returns: (transfer full): the new step_done message.
1609  *
1610  * MT safe.
1611  */
1612 GstMessage *
1613 gst_message_new_step_done (GstObject * src, GstFormat format, guint64 amount,
1614     gdouble rate, gboolean flush, gboolean intermediate, guint64 duration,
1615     gboolean eos)
1616 {
1617   GstMessage *message;
1618   GstStructure *structure;
1619
1620   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STEP_DONE),
1621       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1622       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1623       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1624       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1625       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate,
1626       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1627       GST_QUARK (EOS), G_TYPE_BOOLEAN, eos, NULL);
1628   message = gst_message_new_custom (GST_MESSAGE_STEP_DONE, src, structure);
1629
1630   return message;
1631 }
1632
1633 /**
1634  * gst_message_parse_step_done:
1635  * @message: A valid #GstMessage of type GST_MESSAGE_STEP_DONE.
1636  * @format: (out) (allow-none): result location for the format
1637  * @amount: (out) (allow-none): result location for the amount
1638  * @rate: (out) (allow-none): result location for the rate
1639  * @flush: (out) (allow-none): result location for the flush flag
1640  * @intermediate: (out) (allow-none): result location for the intermediate flag
1641  * @duration: (out) (allow-none): result location for the duration
1642  * @eos: (out) (allow-none): result location for the EOS flag
1643  *
1644  * Extract the values the step_done message.
1645  *
1646  * MT safe.
1647  */
1648 void
1649 gst_message_parse_step_done (GstMessage * message, GstFormat * format,
1650     guint64 * amount, gdouble * rate, gboolean * flush, gboolean * intermediate,
1651     guint64 * duration, gboolean * eos)
1652 {
1653   GstStructure *structure;
1654
1655   g_return_if_fail (GST_IS_MESSAGE (message));
1656   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STEP_DONE);
1657
1658   structure = GST_MESSAGE_STRUCTURE (message);
1659   gst_structure_id_get (structure,
1660       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1661       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1662       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1663       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1664       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate,
1665       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1666       GST_QUARK (EOS), G_TYPE_BOOLEAN, eos, NULL);
1667 }
1668
1669 /**
1670  * gst_message_new_step_start:
1671  * @src: The object originating the message.
1672  * @active: if the step is active or queued
1673  * @format: the format of @amount
1674  * @amount: the amount of stepped data
1675  * @rate: the rate of the stepped amount
1676  * @flush: is this an flushing step
1677  * @intermediate: is this an intermediate step
1678  *
1679  * This message is posted by elements when they accept or activate a new step
1680  * event for @amount in @format. 
1681  *
1682  * @active is set to %FALSE when the element accepted the new step event and has
1683  * queued it for execution in the streaming threads.
1684  *
1685  * @active is set to %TRUE when the element has activated the step operation and
1686  * is now ready to start executing the step in the streaming thread. After this
1687  * message is emitted, the application can queue a new step operation in the
1688  * element.
1689  *
1690  * Returns: (transfer full): The new step_start message. 
1691  *
1692  * MT safe.
1693  */
1694 GstMessage *
1695 gst_message_new_step_start (GstObject * src, gboolean active, GstFormat format,
1696     guint64 amount, gdouble rate, gboolean flush, gboolean intermediate)
1697 {
1698   GstMessage *message;
1699   GstStructure *structure;
1700
1701   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STEP_START),
1702       GST_QUARK (ACTIVE), G_TYPE_BOOLEAN, active,
1703       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1704       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1705       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1706       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1707       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1708   message = gst_message_new_custom (GST_MESSAGE_STEP_START, src, structure);
1709
1710   return message;
1711 }
1712
1713 /**
1714  * gst_message_parse_step_start:
1715  * @message: A valid #GstMessage of type GST_MESSAGE_STEP_DONE.
1716  * @active: (out) (allow-none): result location for the active flag
1717  * @format: (out) (allow-none): result location for the format
1718  * @amount: (out) (allow-none): result location for the amount
1719  * @rate: (out) (allow-none): result location for the rate
1720  * @flush: (out) (allow-none): result location for the flush flag
1721  * @intermediate: (out) (allow-none): result location for the intermediate flag
1722  *
1723  * Extract the values from step_start message.
1724  *
1725  * MT safe.
1726  */
1727 void
1728 gst_message_parse_step_start (GstMessage * message, gboolean * active,
1729     GstFormat * format, guint64 * amount, gdouble * rate, gboolean * flush,
1730     gboolean * intermediate)
1731 {
1732   GstStructure *structure;
1733
1734   g_return_if_fail (GST_IS_MESSAGE (message));
1735   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STEP_START);
1736
1737   structure = GST_MESSAGE_STRUCTURE (message);
1738   gst_structure_id_get (structure,
1739       GST_QUARK (ACTIVE), G_TYPE_BOOLEAN, active,
1740       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1741       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1742       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1743       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1744       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1745 }
1746
1747 /**
1748  * gst_message_new_qos:
1749  * @src: The object originating the message.
1750  * @live: if the message was generated by a live element
1751  * @running_time: the running time of the buffer that generated the message
1752  * @stream_time: the stream time of the buffer that generated the message
1753  * @timestamp: the timestamps of the buffer that generated the message
1754  * @duration: the duration of the buffer that generated the message
1755  *
1756  * A QOS message is posted on the bus whenever an element decides to drop a
1757  * buffer because of QoS reasons or whenever it changes its processing strategy
1758  * because of QoS reasons (quality adjustments such as processing at lower
1759  * accuracy).
1760  *
1761  * This message can be posted by an element that performs synchronisation against the
1762  * clock (live) or it could be dropped by an element that performs QoS because of QOS
1763  * events received from a downstream element (!live).
1764  *
1765  * @running_time, @stream_time, @timestamp, @duration should be set to the
1766  * respective running-time, stream-time, timestamp and duration of the (dropped)
1767  * buffer that generated the QoS event. Values can be left to
1768  * GST_CLOCK_TIME_NONE when unknown.
1769  *
1770  * Returns: (transfer full): The new qos message.
1771  *
1772  * MT safe.
1773  */
1774 GstMessage *
1775 gst_message_new_qos (GstObject * src, gboolean live, guint64 running_time,
1776     guint64 stream_time, guint64 timestamp, guint64 duration)
1777 {
1778   GstMessage *message;
1779   GstStructure *structure;
1780
1781   structure = gst_structure_new_id (GST_QUARK (MESSAGE_QOS),
1782       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
1783       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time,
1784       GST_QUARK (STREAM_TIME), G_TYPE_UINT64, stream_time,
1785       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp,
1786       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1787       GST_QUARK (JITTER), G_TYPE_INT64, (gint64) 0,
1788       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, (gdouble) 1.0,
1789       GST_QUARK (QUALITY), G_TYPE_INT, (gint) 1000000,
1790       GST_QUARK (FORMAT), GST_TYPE_FORMAT, GST_FORMAT_UNDEFINED,
1791       GST_QUARK (PROCESSED), G_TYPE_UINT64, (guint64) - 1,
1792       GST_QUARK (DROPPED), G_TYPE_UINT64, (guint64) - 1, NULL);
1793   message = gst_message_new_custom (GST_MESSAGE_QOS, src, structure);
1794
1795   return message;
1796 }
1797
1798 /**
1799  * gst_message_set_qos_values:
1800  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1801  * @jitter: The difference of the running-time against the deadline.
1802  * @proportion: Long term prediction of the ideal rate relative to normal rate
1803  * to get optimal quality.
1804  * @quality: An element dependent integer value that specifies the current
1805  * quality level of the element. The default maximum quality is 1000000.
1806  *
1807  * Set the QoS values that have been calculated/analysed from the QoS data
1808  *
1809  * MT safe.
1810  */
1811 void
1812 gst_message_set_qos_values (GstMessage * message, gint64 jitter,
1813     gdouble proportion, gint quality)
1814 {
1815   GstStructure *structure;
1816
1817   g_return_if_fail (GST_IS_MESSAGE (message));
1818   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1819
1820   structure = GST_MESSAGE_STRUCTURE (message);
1821   gst_structure_id_set (structure,
1822       GST_QUARK (JITTER), G_TYPE_INT64, jitter,
1823       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1824       GST_QUARK (QUALITY), G_TYPE_INT, quality, NULL);
1825 }
1826
1827 /**
1828  * gst_message_set_qos_stats:
1829  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1830  * @format: Units of the 'processed' and 'dropped' fields. Video sinks and video
1831  * filters will use GST_FORMAT_BUFFERS (frames). Audio sinks and audio filters
1832  * will likely use GST_FORMAT_DEFAULT (samples).
1833  * @processed: Total number of units correctly processed since the last state
1834  * change to READY or a flushing operation.
1835  * @dropped: Total number of units dropped since the last state change to READY
1836  * or a flushing operation.
1837  *
1838  * Set the QoS stats representing the history of the current continuous pipeline
1839  * playback period.
1840  *
1841  * When @format is @GST_FORMAT_UNDEFINED both @dropped and @processed are
1842  * invalid. Values of -1 for either @processed or @dropped mean unknown values.
1843  *
1844  * MT safe.
1845  */
1846 void
1847 gst_message_set_qos_stats (GstMessage * message, GstFormat format,
1848     guint64 processed, guint64 dropped)
1849 {
1850   GstStructure *structure;
1851
1852   g_return_if_fail (GST_IS_MESSAGE (message));
1853   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1854
1855   structure = GST_MESSAGE_STRUCTURE (message);
1856   gst_structure_id_set (structure,
1857       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1858       GST_QUARK (PROCESSED), G_TYPE_UINT64, processed,
1859       GST_QUARK (DROPPED), G_TYPE_UINT64, dropped, NULL);
1860 }
1861
1862 /**
1863  * gst_message_parse_qos:
1864  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1865  * @live: (out) (allow-none): if the message was generated by a live element
1866  * @running_time: (out) (allow-none): the running time of the buffer that
1867  *     generated the message
1868  * @stream_time: (out) (allow-none): the stream time of the buffer that
1869  *     generated the message
1870  * @timestamp: (out) (allow-none): the timestamps of the buffer that
1871  *     generated the message
1872  * @duration: (out) (allow-none): the duration of the buffer that
1873  *     generated the message
1874  *
1875  * Extract the timestamps and live status from the QoS message.
1876  *
1877  * The returned values give the running_time, stream_time, timestamp and
1878  * duration of the dropped buffer. Values of GST_CLOCK_TIME_NONE mean unknown
1879  * values.
1880  *
1881  * MT safe.
1882  */
1883 void
1884 gst_message_parse_qos (GstMessage * message, gboolean * live,
1885     guint64 * running_time, guint64 * stream_time, guint64 * timestamp,
1886     guint64 * duration)
1887 {
1888   GstStructure *structure;
1889
1890   g_return_if_fail (GST_IS_MESSAGE (message));
1891   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1892
1893   structure = GST_MESSAGE_STRUCTURE (message);
1894   gst_structure_id_get (structure,
1895       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
1896       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time,
1897       GST_QUARK (STREAM_TIME), G_TYPE_UINT64, stream_time,
1898       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp,
1899       GST_QUARK (DURATION), G_TYPE_UINT64, duration, NULL);
1900 }
1901
1902 /**
1903  * gst_message_parse_qos_values:
1904  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1905  * @jitter: (out) (allow-none): The difference of the running-time against
1906  *     the deadline.
1907  * @proportion: (out) (allow-none): Long term prediction of the ideal rate
1908  *     relative to normal rate to get optimal quality.
1909  * @quality: (out) (allow-none): An element dependent integer value that
1910  *     specifies the current quality level of the element. The default
1911  *     maximum quality is 1000000.
1912  *
1913  * Extract the QoS values that have been calculated/analysed from the QoS data
1914  *
1915  * MT safe.
1916  */
1917 void
1918 gst_message_parse_qos_values (GstMessage * message, gint64 * jitter,
1919     gdouble * proportion, gint * quality)
1920 {
1921   GstStructure *structure;
1922
1923   g_return_if_fail (GST_IS_MESSAGE (message));
1924   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1925
1926   structure = GST_MESSAGE_STRUCTURE (message);
1927   gst_structure_id_get (structure,
1928       GST_QUARK (JITTER), G_TYPE_INT64, jitter,
1929       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1930       GST_QUARK (QUALITY), G_TYPE_INT, quality, NULL);
1931 }
1932
1933 /**
1934  * gst_message_parse_qos_stats:
1935  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1936  * @format: (out) (allow-none): Units of the 'processed' and 'dropped' fields.
1937  *     Video sinks and video filters will use GST_FORMAT_BUFFERS (frames).
1938  *     Audio sinks and audio filters will likely use GST_FORMAT_DEFAULT
1939  *     (samples).
1940  * @processed: (out) (allow-none): Total number of units correctly processed
1941  *     since the last state change to READY or a flushing operation.
1942  * @dropped: (out) (allow-none): Total number of units dropped since the last
1943  *     state change to READY or a flushing operation.
1944  *
1945  * Extract the QoS stats representing the history of the current continuous
1946  * pipeline playback period.
1947  *
1948  * When @format is @GST_FORMAT_UNDEFINED both @dropped and @processed are
1949  * invalid. Values of -1 for either @processed or @dropped mean unknown values.
1950  *
1951  * MT safe.
1952  */
1953 void
1954 gst_message_parse_qos_stats (GstMessage * message, GstFormat * format,
1955     guint64 * processed, guint64 * dropped)
1956 {
1957   GstStructure *structure;
1958
1959   g_return_if_fail (GST_IS_MESSAGE (message));
1960   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1961
1962   structure = GST_MESSAGE_STRUCTURE (message);
1963   gst_structure_id_get (structure,
1964       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1965       GST_QUARK (PROCESSED), G_TYPE_UINT64, processed,
1966       GST_QUARK (DROPPED), G_TYPE_UINT64, dropped, NULL);
1967 }
1968
1969 /**
1970  * gst_message_new_progress:
1971  * @src: The object originating the message.
1972  * @type: a #GstProgressType
1973  * @code: a progress code
1974  * @text: free, user visible text describing the progress
1975  *
1976  * Progress messages are posted by elements when they use an asynchronous task
1977  * to perform actions triggered by a state change.
1978  *
1979  * @code contains a well defined string describing the action.
1980  * @test should contain a user visible string detailing the current action.
1981  *
1982  * Returns: (transfer full): The new qos message.
1983  */
1984 GstMessage *
1985 gst_message_new_progress (GstObject * src, GstProgressType type,
1986     const gchar * code, const gchar * text)
1987 {
1988   GstMessage *message;
1989   GstStructure *structure;
1990   gint percent = 100, timeout = -1;
1991
1992   g_return_val_if_fail (code != NULL, NULL);
1993   g_return_val_if_fail (text != NULL, NULL);
1994
1995   if (type == GST_PROGRESS_TYPE_START || type == GST_PROGRESS_TYPE_CONTINUE)
1996     percent = 0;
1997
1998   structure = gst_structure_new_id (GST_QUARK (MESSAGE_PROGRESS),
1999       GST_QUARK (TYPE), GST_TYPE_PROGRESS_TYPE, type,
2000       GST_QUARK (CODE), G_TYPE_STRING, code,
2001       GST_QUARK (TEXT), G_TYPE_STRING, text,
2002       GST_QUARK (PERCENT), G_TYPE_INT, percent,
2003       GST_QUARK (TIMEOUT), G_TYPE_INT, timeout, NULL);
2004   message = gst_message_new_custom (GST_MESSAGE_PROGRESS, src, structure);
2005
2006   return message;
2007 }
2008
2009 /**
2010  * gst_message_parse_progress:
2011  * @message: A valid #GstMessage of type GST_MESSAGE_PROGRESS.
2012  * @type: (out) (allow-none): location for the type
2013  * @code: (out) (allow-none) (transfer full): location for the code
2014  * @text: (out) (allow-none) (transfer full): location for the text
2015  *
2016  * Parses the progress @type, @code and @text.
2017  */
2018 void
2019 gst_message_parse_progress (GstMessage * message, GstProgressType * type,
2020     gchar ** code, gchar ** text)
2021 {
2022   GstStructure *structure;
2023
2024   g_return_if_fail (GST_IS_MESSAGE (message));
2025   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_PROGRESS);
2026
2027   structure = GST_MESSAGE_STRUCTURE (message);
2028   gst_structure_id_get (structure,
2029       GST_QUARK (TYPE), GST_TYPE_PROGRESS_TYPE, type,
2030       GST_QUARK (CODE), G_TYPE_STRING, code,
2031       GST_QUARK (TEXT), G_TYPE_STRING, text, NULL);
2032 }
2033
2034 /**
2035  * gst_message_new_toc:
2036  * @src: the object originating the message.
2037  * @toc: (transfer none): #GstToc structure for the message.
2038  * @updated: whether TOC was updated or not.
2039  *
2040  * Create a new TOC message. The message is posted by elements
2041  * that discovered or updated a TOC.
2042  *
2043  * Returns: (transfer full): a new TOC message.
2044  *
2045  * MT safe.
2046  */
2047 GstMessage *
2048 gst_message_new_toc (GstObject * src, GstToc * toc, gboolean updated)
2049 {
2050   GstStructure *toc_struct;
2051
2052   g_return_val_if_fail (toc != NULL, NULL);
2053
2054   toc_struct = gst_structure_new_id (GST_QUARK (MESSAGE_TOC),
2055       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2056       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2057
2058   return gst_message_new_custom (GST_MESSAGE_TOC, src, toc_struct);
2059 }
2060
2061 /**
2062  * gst_message_parse_toc:
2063  * @message: a valid #GstMessage of type GST_MESSAGE_TOC.
2064  * @toc: (out) (transfer full): return location for the TOC.
2065  * @updated: (out): return location for the updated flag.
2066  *
2067  * Extract the TOC from the #GstMessage. The TOC returned in the
2068  * output argument is a copy; the caller must free it with
2069  * gst_toc_unref() when done.
2070  *
2071  * MT safe.
2072  */
2073 void
2074 gst_message_parse_toc (GstMessage * message, GstToc ** toc, gboolean * updated)
2075 {
2076   g_return_if_fail (GST_IS_MESSAGE (message));
2077   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_TOC);
2078   g_return_if_fail (toc != NULL);
2079
2080   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2081       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2082       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2083 }
2084
2085 /**
2086  * gst_message_new_reset_time:
2087  * @src: (transfer none): The object originating the message.
2088  * @running_time: the requested running-time
2089  *
2090  * This message is posted when the pipeline running-time should be reset to
2091  * @running_time, like after a flushing seek.
2092  *
2093  * Returns: (transfer full): The new reset_time message.
2094  *
2095  * MT safe.
2096  */
2097 GstMessage *
2098 gst_message_new_reset_time (GstObject * src, GstClockTime running_time)
2099 {
2100   GstMessage *message;
2101   GstStructure *structure;
2102
2103   structure = gst_structure_new_id (GST_QUARK (MESSAGE_RESET_TIME),
2104       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time, NULL);
2105   message = gst_message_new_custom (GST_MESSAGE_RESET_TIME, src, structure);
2106
2107   return message;
2108 }
2109
2110 /**
2111  * gst_message_parse_reset_time:
2112  * @message: A valid #GstMessage of type GST_MESSAGE_RESET_TIME.
2113  * @running_time: (out) (allow-none): Result location for the running_time or
2114  *      %NULL
2115  *
2116  * Extract the running-time from the RESET_TIME message.
2117  *
2118  * MT safe.
2119  */
2120 void
2121 gst_message_parse_reset_time (GstMessage * message, GstClockTime * running_time)
2122 {
2123   GstStructure *structure;
2124
2125   g_return_if_fail (GST_IS_MESSAGE (message));
2126   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_RESET_TIME);
2127
2128   structure = GST_MESSAGE_STRUCTURE (message);
2129   if (running_time)
2130     *running_time =
2131         g_value_get_uint64 (gst_structure_id_get_value (structure,
2132             GST_QUARK (RUNNING_TIME)));
2133 }
2134
2135 /**
2136  * gst_message_new_stream_start:
2137  * @src: (transfer none): The object originating the message.
2138  *
2139  * Create a new stream_start message. This message is generated and posted in
2140  * the sink elements of a GstBin. The bin will only forward the STREAM_START
2141  * message to the application if all sinks have posted an STREAM_START message.
2142  *
2143  * Returns: (transfer full): The new stream_start message.
2144  *
2145  * MT safe.
2146  */
2147 GstMessage *
2148 gst_message_new_stream_start (GstObject * src)
2149 {
2150   GstMessage *message;
2151   GstStructure *s;
2152
2153   s = gst_structure_new_id_empty (GST_QUARK (MESSAGE_STREAM_START));
2154   message = gst_message_new_custom (GST_MESSAGE_STREAM_START, src, s);
2155
2156   return message;
2157 }
2158
2159
2160 /**
2161  * gst_message_set_group_id:
2162  * @message: the message
2163  * @group_id: the group id
2164  *
2165  * Sets the group id on the stream-start message.
2166  *
2167  * All streams that have the same group id are supposed to be played
2168  * together, i.e. all streams inside a container file should have the
2169  * same group id but different stream ids. The group id should change
2170  * each time the stream is started, resulting in different group ids
2171  * each time a file is played for example.
2172  *
2173  * MT safe.
2174  *
2175  * Since: 1.2
2176  */
2177 void
2178 gst_message_set_group_id (GstMessage * message, guint group_id)
2179 {
2180   GstStructure *structure;
2181
2182   g_return_if_fail (GST_IS_MESSAGE (message));
2183   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START);
2184   g_return_if_fail (gst_message_is_writable (message));
2185
2186   structure = GST_MESSAGE_STRUCTURE (message);
2187   gst_structure_id_set (structure, GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id,
2188       NULL);
2189 }
2190
2191 /**
2192  * gst_message_parse_group_id:
2193  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_START.
2194  * @group_id: (out) (allow-none): Result location for the group id or
2195  *      %NULL
2196  *
2197  * Extract the group from the STREAM_START message.
2198  *
2199  * Returns: %TRUE if the message had a group id set, %FALSE otherwise
2200  *
2201  * MT safe.
2202  *
2203  * Since: 1.2
2204  */
2205 gboolean
2206 gst_message_parse_group_id (GstMessage * message, guint * group_id)
2207 {
2208   GstStructure *structure;
2209   const GValue *v;
2210
2211   g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
2212   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START,
2213       FALSE);
2214
2215   if (!group_id)
2216     return TRUE;
2217
2218   structure = GST_MESSAGE_STRUCTURE (message);
2219
2220   v = gst_structure_id_get_value (structure, GST_QUARK (GROUP_ID));
2221   if (!v)
2222     return FALSE;
2223
2224   *group_id = g_value_get_uint (v);
2225   return TRUE;
2226 }
2227
2228 /**
2229  * gst_message_new_need_context:
2230  * @src: (transfer none): The object originating the message.
2231  * @context_type: The context type that is needed
2232  *
2233  * This message is posted when an element needs a specific #GstContext.
2234  *
2235  * Returns: (transfer full): The new need-context message.
2236  *
2237  * MT safe.
2238  *
2239  * Since: 1.2
2240  */
2241 GstMessage *
2242 gst_message_new_need_context (GstObject * src, const gchar * context_type)
2243 {
2244   GstMessage *message;
2245   GstStructure *structure;
2246
2247   g_return_val_if_fail (context_type != NULL, NULL);
2248
2249   structure = gst_structure_new_id (GST_QUARK (MESSAGE_NEED_CONTEXT),
2250       GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
2251   message = gst_message_new_custom (GST_MESSAGE_NEED_CONTEXT, src, structure);
2252
2253   return message;
2254 }
2255
2256 /**
2257  * gst_message_parse_context_type:
2258  * @message: a GST_MESSAGE_NEED_CONTEXT type message
2259  * @context_type: (out) (allow-none): the context type, or %NULL
2260  *
2261  * Parse a context type from an existing GST_MESSAGE_NEED_CONTEXT message.
2262  *
2263  * Returns: a #gboolean indicating if the parsing succeeded.
2264  *
2265  * Since: 1.2
2266  */
2267 gboolean
2268 gst_message_parse_context_type (GstMessage * message,
2269     const gchar ** context_type)
2270 {
2271   GstStructure *structure;
2272   const GValue *value;
2273
2274   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT,
2275       FALSE);
2276
2277   structure = GST_MESSAGE_STRUCTURE (message);
2278
2279   if (context_type) {
2280     value = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT_TYPE));
2281     *context_type = g_value_get_string (value);
2282   }
2283
2284   return TRUE;
2285 }
2286
2287 /**
2288  * gst_message_new_have_context:
2289  * @src: (transfer none): The object originating the message.
2290  * @context: (transfer full): the context
2291  *
2292  * This message is posted when an element has a new local #GstContext.
2293  *
2294  * Returns: (transfer full): The new have-context message.
2295  *
2296  * MT safe.
2297  *
2298  * Since: 1.2
2299  */
2300 GstMessage *
2301 gst_message_new_have_context (GstObject * src, GstContext * context)
2302 {
2303   GstMessage *message;
2304   GstStructure *structure;
2305
2306   structure = gst_structure_new_id (GST_QUARK (MESSAGE_HAVE_CONTEXT),
2307       GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2308   message = gst_message_new_custom (GST_MESSAGE_HAVE_CONTEXT, src, structure);
2309   gst_context_unref (context);
2310
2311   return message;
2312 }
2313
2314 /**
2315  * gst_message_parse_have_context:
2316  * @message: A valid #GstMessage of type GST_MESSAGE_HAVE_CONTEXT.
2317  * @context: (out) (transfer full) (allow-none): Result location for the
2318  *      context or %NULL
2319  *
2320  * Extract the context from the HAVE_CONTEXT message.
2321  *
2322  * MT safe.
2323  *
2324  * Since: 1.2
2325  */
2326 void
2327 gst_message_parse_have_context (GstMessage * message, GstContext ** context)
2328 {
2329   g_return_if_fail (GST_IS_MESSAGE (message));
2330   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_HAVE_CONTEXT);
2331
2332   if (context)
2333     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2334         GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2335 }
2336
2337 /**
2338  * gst_message_new_device_added:
2339  * @src: The #GstObject that created the message
2340  * @device: (transfer none): The new #GstDevice
2341  *
2342  * Creates a new device-added message. The device-added message is produced by
2343  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. They announce the appearance
2344  * of monitored devices.
2345  *
2346  * Returns: a newly allocated #GstMessage
2347  *
2348  * Since: 1.4
2349  */
2350 GstMessage *
2351 gst_message_new_device_added (GstObject * src, GstDevice * device)
2352 {
2353   GstMessage *message;
2354   GstStructure *structure;
2355
2356   g_return_val_if_fail (device != NULL, NULL);
2357   g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
2358
2359   structure = gst_structure_new_id (GST_QUARK (MESSAGE_DEVICE_ADDED),
2360       GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2361   message = gst_message_new_custom (GST_MESSAGE_DEVICE_ADDED, src, structure);
2362
2363   return message;
2364 }
2365
2366 /**
2367  * gst_message_parse_device_added:
2368  * @message: a #GstMessage of type %GST_MESSAGE_DEVICE_ADDED
2369  * @device: (out) (allow-none) (transfer none): A location where to store a
2370  *  pointer to the new #GstDevice, or %NULL
2371  * 
2372  * Parses a device-added message. The device-added message is produced by
2373  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. It announces the appearance
2374  * of monitored devices.
2375  *
2376  * Since: 1.4
2377  */
2378 void
2379 gst_message_parse_device_added (GstMessage * message, GstDevice ** device)
2380 {
2381   g_return_if_fail (GST_IS_MESSAGE (message));
2382   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DEVICE_ADDED);
2383
2384   if (device)
2385     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2386         GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2387 }
2388
2389 /**
2390  * gst_message_new_device_removed:
2391  * @src: The #GstObject that created the message
2392  * @device: (transfer none): The removed #GstDevice
2393  *
2394  * Creates a new device-removed message. The device-removed message is produced
2395  * by #GstDeviceMonitor or a #GstGlobalDeviceMonitor. They announce the
2396  * disappearance of monitored devices.
2397  *
2398  * Returns: a newly allocated #GstMessage
2399  *
2400  * Since: 1.4
2401  */
2402 GstMessage *
2403 gst_message_new_device_removed (GstObject * src, GstDevice * device)
2404 {
2405   GstMessage *message;
2406   GstStructure *structure;
2407
2408   g_return_val_if_fail (device != NULL, NULL);
2409   g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
2410
2411   structure = gst_structure_new_id (GST_QUARK (MESSAGE_DEVICE_REMOVED),
2412       GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2413   message = gst_message_new_custom (GST_MESSAGE_DEVICE_REMOVED, src, structure);
2414
2415   return message;
2416 }
2417
2418 /**
2419  * gst_message_parse_device_removed:
2420  * @message: a #GstMessage of type %GST_MESSAGE_DEVICE_REMOVED
2421  * @device: (out) (allow-none) (transfer none): A location where to store a
2422  *  pointer to the removed #GstDevice, or %NULL
2423  *
2424  * Parses a device-removed message. The device-removed message is produced by
2425  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. It announces the
2426  * disappearance of monitored devices.
2427  *
2428  * Since: 1.4
2429  */
2430 void
2431 gst_message_parse_device_removed (GstMessage * message, GstDevice ** device)
2432 {
2433   g_return_if_fail (GST_IS_MESSAGE (message));
2434   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DEVICE_REMOVED);
2435
2436   if (device)
2437     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2438         GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2439 }