docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[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 static 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): the structure for the message. The message
260  *     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   return gst_message_new_custom (GST_MESSAGE_APPLICATION, src, structure);
783 }
784
785 /**
786  * gst_message_new_element:
787  * @src: (transfer none): The object originating the message.
788  * @structure: (transfer full): The structure for the message. The message
789  *     will take ownership of the structure.
790  *
791  * Create a new element-specific message. This is meant as a generic way of
792  * allowing one-way communication from an element to an application, for example
793  * "the firewire cable was unplugged". The format of the message should be
794  * documented in the element's documentation. The structure field can be %NULL.
795  *
796  * Returns: (transfer full): The new element message.
797  *
798  * MT safe.
799  */
800 GstMessage *
801 gst_message_new_element (GstObject * src, GstStructure * structure)
802 {
803   return gst_message_new_custom (GST_MESSAGE_ELEMENT, src, structure);
804 }
805
806 /**
807  * gst_message_new_duration_changed:
808  * @src: (transfer none): The object originating the message.
809  *
810  * Create a new duration changed message. This message is posted by elements
811  * that know the duration of a stream when the duration changes. This message
812  * is received by bins and is used to calculate the total duration of a
813  * pipeline. Elements may post a duration message with a duration of
814  * GST_CLOCK_TIME_NONE to indicate that the duration has changed and the 
815  * cached duration should be discarded. The new duration can then be 
816  * retrieved via a query.
817  *
818  * Returns: (transfer full): The new duration-changed message.
819  *
820  * MT safe.
821  */
822 GstMessage *
823 gst_message_new_duration_changed (GstObject * src)
824 {
825   GstMessage *message;
826
827   message = gst_message_new_custom (GST_MESSAGE_DURATION_CHANGED, src,
828       gst_structure_new_id_empty (GST_QUARK (MESSAGE_DURATION_CHANGED)));
829
830   return message;
831 }
832
833 /**
834  * gst_message_new_async_start:
835  * @src: (transfer none): The object originating the message.
836  *
837  * This message is posted by elements when they start an ASYNC state change.
838  *
839  * Returns: (transfer full): The new async_start message.
840  *
841  * MT safe.
842  */
843 GstMessage *
844 gst_message_new_async_start (GstObject * src)
845 {
846   GstMessage *message;
847
848   message = gst_message_new_custom (GST_MESSAGE_ASYNC_START, src, NULL);
849
850   return message;
851 }
852
853 /**
854  * gst_message_new_async_done:
855  * @src: (transfer none): The object originating the message.
856  * @running_time: the desired running_time
857  *
858  * The message is posted when elements completed an ASYNC state change.
859  * @running_time contains the time of the desired running_time when this
860  * elements goes to PLAYING. A value of #GST_CLOCK_TIME_NONE for @running_time
861  * means that the element has no clock interaction and thus doesn't care about
862  * the running_time of the pipeline.
863  *
864  * Returns: (transfer full): The new async_done message.
865  *
866  * MT safe.
867  */
868 GstMessage *
869 gst_message_new_async_done (GstObject * src, GstClockTime running_time)
870 {
871   GstMessage *message;
872   GstStructure *structure;
873
874   structure = gst_structure_new_id (GST_QUARK (MESSAGE_ASYNC_DONE),
875       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time, NULL);
876   message = gst_message_new_custom (GST_MESSAGE_ASYNC_DONE, src, structure);
877
878   return message;
879 }
880
881 /**
882  * gst_message_new_latency:
883  * @src: (transfer none): The object originating the message.
884  *
885  * This message can be posted by elements when their latency requirements have
886  * changed.
887  *
888  * Returns: (transfer full): The new latency message.
889  *
890  * MT safe.
891  */
892 GstMessage *
893 gst_message_new_latency (GstObject * src)
894 {
895   GstMessage *message;
896
897   message = gst_message_new_custom (GST_MESSAGE_LATENCY, src, NULL);
898
899   return message;
900 }
901
902 /**
903  * gst_message_new_request_state:
904  * @src: (transfer none): the object originating the message.
905  * @state: The new requested state
906  *
907  * This message can be posted by elements when they want to have their state
908  * changed. A typical use case would be an audio server that wants to pause the
909  * pipeline because a higher priority stream is being played.
910  *
911  * Returns: (transfer full): the new request state message.
912  *
913  * MT safe.
914  */
915 GstMessage *
916 gst_message_new_request_state (GstObject * src, GstState state)
917 {
918   GstMessage *message;
919   GstStructure *structure;
920
921   structure = gst_structure_new_id (GST_QUARK (MESSAGE_REQUEST_STATE),
922       GST_QUARK (NEW_STATE), GST_TYPE_STATE, (gint) state, NULL);
923   message = gst_message_new_custom (GST_MESSAGE_REQUEST_STATE, src, structure);
924
925   return message;
926 }
927
928 /**
929  * gst_message_get_structure:
930  * @message: The #GstMessage.
931  *
932  * Access the structure of the message.
933  *
934  * Returns: (transfer none): The structure of the message. The structure is
935  * still owned by the message, which means that you should not free it and
936  * that the pointer becomes invalid when you free the message.
937  *
938  * MT safe.
939  */
940 const GstStructure *
941 gst_message_get_structure (GstMessage * message)
942 {
943   g_return_val_if_fail (GST_IS_MESSAGE (message), NULL);
944
945   return GST_MESSAGE_STRUCTURE (message);
946 }
947
948 /**
949  * gst_message_has_name:
950  * @message: The #GstMessage.
951  * @name: name to check
952  *
953  * Checks if @message has the given @name. This function is usually used to
954  * check the name of a custom message.
955  *
956  * Returns: %TRUE if @name matches the name of the message structure.
957  */
958 gboolean
959 gst_message_has_name (GstMessage * message, const gchar * name)
960 {
961   GstStructure *structure;
962
963   g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
964
965   structure = GST_MESSAGE_STRUCTURE (message);
966   if (structure == NULL)
967     return FALSE;
968
969   return gst_structure_has_name (structure, name);
970 }
971
972 /**
973  * gst_message_parse_tag:
974  * @message: A valid #GstMessage of type GST_MESSAGE_TAG.
975  * @tag_list: (out callee-allocates): return location for the tag-list.
976  *
977  * Extracts the tag list from the GstMessage. The tag list returned in the
978  * output argument is a copy; the caller must free it when done.
979  *
980  * Typical usage of this function might be:
981  * |[
982  *   ...
983  *   switch (GST_MESSAGE_TYPE (msg)) {
984  *     case GST_MESSAGE_TAG: {
985  *       GstTagList *tags = NULL;
986  *       
987  *       gst_message_parse_tag (msg, &amp;tags);
988  *       g_print ("Got tags from element %s\n", GST_OBJECT_NAME (msg->src));
989  *       handle_tags (tags);
990  *       gst_tag_list_unref (tags);
991  *       break;
992  *     }
993  *     ...
994  *   }
995  *   ...
996  * ]|
997  *
998  * MT safe.
999  */
1000 void
1001 gst_message_parse_tag (GstMessage * message, GstTagList ** tag_list)
1002 {
1003   g_return_if_fail (GST_IS_MESSAGE (message));
1004   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_TAG);
1005   g_return_if_fail (tag_list != NULL);
1006
1007   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1008       GST_QUARK (TAGLIST), GST_TYPE_TAG_LIST, tag_list, NULL);
1009 }
1010
1011 /**
1012  * gst_message_parse_buffering:
1013  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1014  * @percent: (out) (allow-none): Return location for the percent.
1015  *
1016  * Extracts the buffering percent from the GstMessage. see also
1017  * gst_message_new_buffering().
1018  *
1019  * MT safe.
1020  */
1021 void
1022 gst_message_parse_buffering (GstMessage * message, gint * percent)
1023 {
1024   g_return_if_fail (GST_IS_MESSAGE (message));
1025   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1026
1027   if (percent)
1028     *percent =
1029         g_value_get_int (gst_structure_id_get_value (GST_MESSAGE_STRUCTURE
1030             (message), GST_QUARK (BUFFER_PERCENT)));
1031 }
1032
1033 /**
1034  * gst_message_set_buffering_stats:
1035  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1036  * @mode: a buffering mode 
1037  * @avg_in: the average input rate
1038  * @avg_out: the average output rate
1039  * @buffering_left: amount of buffering time left in milliseconds
1040  *
1041  * Configures the buffering stats values in @message.
1042  */
1043 void
1044 gst_message_set_buffering_stats (GstMessage * message, GstBufferingMode mode,
1045     gint avg_in, gint avg_out, gint64 buffering_left)
1046 {
1047   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1048
1049   gst_structure_id_set (GST_MESSAGE_STRUCTURE (message),
1050       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1051       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1052       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1053       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1054 }
1055
1056 /**
1057  * gst_message_parse_buffering_stats:
1058  * @message: A valid #GstMessage of type GST_MESSAGE_BUFFERING.
1059  * @mode: (out) (allow-none): a buffering mode, or %NULL
1060  * @avg_in: (out) (allow-none): the average input rate, or %NULL
1061  * @avg_out: (out) (allow-none): the average output rate, or %NULL
1062  * @buffering_left: (out) (allow-none): amount of buffering time left in
1063  *     milliseconds, or %NULL
1064  *
1065  * Extracts the buffering stats values from @message.
1066  */
1067 void
1068 gst_message_parse_buffering_stats (GstMessage * message,
1069     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1070     gint64 * buffering_left)
1071 {
1072   GstStructure *structure;
1073
1074   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_BUFFERING);
1075
1076   structure = GST_MESSAGE_STRUCTURE (message);
1077   if (mode)
1078     *mode = (GstBufferingMode)
1079         g_value_get_enum (gst_structure_id_get_value (structure,
1080             GST_QUARK (BUFFERING_MODE)));
1081   if (avg_in)
1082     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1083             GST_QUARK (AVG_IN_RATE)));
1084   if (avg_out)
1085     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1086             GST_QUARK (AVG_OUT_RATE)));
1087   if (buffering_left)
1088     *buffering_left =
1089         g_value_get_int64 (gst_structure_id_get_value (structure,
1090             GST_QUARK (BUFFERING_LEFT)));
1091 }
1092
1093 /**
1094  * gst_message_parse_state_changed:
1095  * @message: a valid #GstMessage of type GST_MESSAGE_STATE_CHANGED
1096  * @oldstate: (out) (allow-none): the previous state, or %NULL
1097  * @newstate: (out) (allow-none): the new (current) state, or %NULL
1098  * @pending: (out) (allow-none): the pending (target) state, or %NULL
1099  *
1100  * Extracts the old and new states from the GstMessage.
1101  *
1102  * Typical usage of this function might be:
1103  * |[
1104  *   ...
1105  *   switch (GST_MESSAGE_TYPE (msg)) {
1106  *     case GST_MESSAGE_STATE_CHANGED: {
1107  *       GstState old_state, new_state;
1108  *       
1109  *       gst_message_parse_state_changed (msg, &amp;old_state, &amp;new_state, NULL);
1110  *       g_print ("Element %s changed state from %s to %s.\n",
1111  *           GST_OBJECT_NAME (msg->src),
1112  *           gst_element_state_get_name (old_state),
1113  *           gst_element_state_get_name (new_state));
1114  *       break;
1115  *     }
1116  *     ...
1117  *   }
1118  *   ...
1119  * ]|
1120  *
1121  * MT safe.
1122  */
1123 void
1124 gst_message_parse_state_changed (GstMessage * message,
1125     GstState * oldstate, GstState * newstate, GstState * pending)
1126 {
1127   GstStructure *structure;
1128
1129   g_return_if_fail (GST_IS_MESSAGE (message));
1130   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED);
1131
1132   structure = GST_MESSAGE_STRUCTURE (message);
1133   if (oldstate)
1134     *oldstate = (GstState)
1135         g_value_get_enum (gst_structure_id_get_value (structure,
1136             GST_QUARK (OLD_STATE)));
1137   if (newstate)
1138     *newstate = (GstState)
1139         g_value_get_enum (gst_structure_id_get_value (structure,
1140             GST_QUARK (NEW_STATE)));
1141   if (pending)
1142     *pending = (GstState)
1143         g_value_get_enum (gst_structure_id_get_value (structure,
1144             GST_QUARK (PENDING_STATE)));
1145 }
1146
1147 /**
1148  * gst_message_parse_clock_provide:
1149  * @message: A valid #GstMessage of type GST_MESSAGE_CLOCK_PROVIDE.
1150  * @clock: (out) (allow-none) (transfer none): a pointer to  hold a clock
1151  *     object, or %NULL
1152  * @ready: (out) (allow-none): a pointer to hold the ready flag, or %NULL
1153  *
1154  * Extracts the clock and ready flag from the GstMessage.
1155  * The clock object returned remains valid until the message is freed.
1156  *
1157  * MT safe.
1158  */
1159 void
1160 gst_message_parse_clock_provide (GstMessage * message, GstClock ** clock,
1161     gboolean * ready)
1162 {
1163   const GValue *clock_gvalue;
1164   GstStructure *structure;
1165
1166   g_return_if_fail (GST_IS_MESSAGE (message));
1167   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_CLOCK_PROVIDE);
1168
1169   structure = GST_MESSAGE_STRUCTURE (message);
1170   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1171   g_return_if_fail (clock_gvalue != NULL);
1172   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1173
1174   if (ready)
1175     *ready =
1176         g_value_get_boolean (gst_structure_id_get_value (structure,
1177             GST_QUARK (READY)));
1178   if (clock)
1179     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1180 }
1181
1182 /**
1183  * gst_message_parse_clock_lost:
1184  * @message: A valid #GstMessage of type GST_MESSAGE_CLOCK_LOST.
1185  * @clock: (out) (allow-none) (transfer none): a pointer to hold the lost clock
1186  *
1187  * Extracts the lost clock from the GstMessage.
1188  * The clock object returned remains valid until the message is freed.
1189  *
1190  * MT safe.
1191  */
1192 void
1193 gst_message_parse_clock_lost (GstMessage * message, GstClock ** clock)
1194 {
1195   const GValue *clock_gvalue;
1196   GstStructure *structure;
1197
1198   g_return_if_fail (GST_IS_MESSAGE (message));
1199   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_CLOCK_LOST);
1200
1201   structure = GST_MESSAGE_STRUCTURE (message);
1202   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1203   g_return_if_fail (clock_gvalue != NULL);
1204   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1205
1206   if (clock)
1207     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1208 }
1209
1210 /**
1211  * gst_message_parse_new_clock:
1212  * @message: A valid #GstMessage of type GST_MESSAGE_NEW_CLOCK.
1213  * @clock: (out) (allow-none) (transfer none): a pointer to hold the selected
1214  *     new clock
1215  *
1216  * Extracts the new clock from the GstMessage.
1217  * The clock object returned remains valid until the message is freed.
1218  *
1219  * MT safe.
1220  */
1221 void
1222 gst_message_parse_new_clock (GstMessage * message, GstClock ** clock)
1223 {
1224   const GValue *clock_gvalue;
1225   GstStructure *structure;
1226
1227   g_return_if_fail (GST_IS_MESSAGE (message));
1228   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEW_CLOCK);
1229
1230   structure = GST_MESSAGE_STRUCTURE (message);
1231   clock_gvalue = gst_structure_id_get_value (structure, GST_QUARK (CLOCK));
1232   g_return_if_fail (clock_gvalue != NULL);
1233   g_return_if_fail (G_VALUE_TYPE (clock_gvalue) == GST_TYPE_CLOCK);
1234
1235   if (clock)
1236     *clock = (GstClock *) g_value_get_object (clock_gvalue);
1237 }
1238
1239 /**
1240  * gst_message_parse_structure_change:
1241  * @message: A valid #GstMessage of type GST_MESSAGE_STRUCTURE_CHANGE.
1242  * @type: (out): A pointer to hold the change type
1243  * @owner: (out) (allow-none) (transfer none): The owner element of the
1244  *     message source
1245  * @busy: (out) (allow-none): a pointer to hold whether the change is in
1246  *     progress or has been completed
1247  *
1248  * Extracts the change type and completion status from the GstMessage.
1249  *
1250  * MT safe.
1251  */
1252 void
1253 gst_message_parse_structure_change (GstMessage * message,
1254     GstStructureChangeType * type, GstElement ** owner, gboolean * busy)
1255 {
1256   const GValue *owner_gvalue;
1257   GstStructure *structure;
1258
1259   g_return_if_fail (GST_IS_MESSAGE (message));
1260   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STRUCTURE_CHANGE);
1261
1262   structure = GST_MESSAGE_STRUCTURE (message);
1263   owner_gvalue = gst_structure_id_get_value (structure, GST_QUARK (OWNER));
1264   g_return_if_fail (owner_gvalue != NULL);
1265   g_return_if_fail (G_VALUE_TYPE (owner_gvalue) == GST_TYPE_ELEMENT);
1266
1267   if (type)
1268     *type = (GstStructureChangeType)
1269         g_value_get_enum (gst_structure_id_get_value (structure,
1270             GST_QUARK (TYPE)));
1271   if (owner)
1272     *owner = (GstElement *) g_value_get_object (owner_gvalue);
1273   if (busy)
1274     *busy =
1275         g_value_get_boolean (gst_structure_id_get_value (structure,
1276             GST_QUARK (BUSY)));
1277 }
1278
1279 /**
1280  * gst_message_parse_error:
1281  * @message: A valid #GstMessage of type GST_MESSAGE_ERROR.
1282  * @gerror: (out) (allow-none) (transfer full): location for the GError
1283  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1284  *     or %NULL
1285  *
1286  * Extracts the GError and debug string from the GstMessage. The values returned
1287  * in the output arguments are copies; the caller must free them when done.
1288  *
1289  * Typical usage of this function might be:
1290  * |[
1291  *   ...
1292  *   switch (GST_MESSAGE_TYPE (msg)) {
1293  *     case GST_MESSAGE_ERROR: {
1294  *       GError *err = NULL;
1295  *       gchar *dbg_info = NULL;
1296  *       
1297  *       gst_message_parse_error (msg, &amp;err, &amp;dbg_info);
1298  *       g_printerr ("ERROR from element %s: %s\n",
1299  *           GST_OBJECT_NAME (msg->src), err->message);
1300  *       g_printerr ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
1301  *       g_error_free (err);
1302  *       g_free (dbg_info);
1303  *       break;
1304  *     }
1305  *     ...
1306  *   }
1307  *   ...
1308  * ]|
1309  *
1310  * MT safe.
1311  */
1312 void
1313 gst_message_parse_error (GstMessage * message, GError ** gerror, gchar ** debug)
1314 {
1315   g_return_if_fail (GST_IS_MESSAGE (message));
1316   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR);
1317
1318   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1319       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1320       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1321 }
1322
1323 /**
1324  * gst_message_parse_warning:
1325  * @message: A valid #GstMessage of type GST_MESSAGE_WARNING.
1326  * @gerror: (out) (allow-none) (transfer full): location for the GError
1327  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1328  *     or %NULL
1329  *
1330  * Extracts the GError and debug string from the GstMessage. The values returned
1331  * in the output arguments are copies; the caller must free them when done.
1332  *
1333  * MT safe.
1334  */
1335 void
1336 gst_message_parse_warning (GstMessage * message, GError ** gerror,
1337     gchar ** debug)
1338 {
1339   g_return_if_fail (GST_IS_MESSAGE (message));
1340   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING);
1341
1342   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1343       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1344       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1345 }
1346
1347 /**
1348  * gst_message_parse_info:
1349  * @message: A valid #GstMessage of type GST_MESSAGE_INFO.
1350  * @gerror: (out) (allow-none) (transfer full): location for the GError
1351  * @debug: (out) (allow-none) (transfer full): location for the debug message,
1352  *     or %NULL
1353  *
1354  * Extracts the GError and debug string from the GstMessage. The values returned
1355  * in the output arguments are copies; the caller must free them when done.
1356  *
1357  * MT safe.
1358  */
1359 void
1360 gst_message_parse_info (GstMessage * message, GError ** gerror, gchar ** debug)
1361 {
1362   g_return_if_fail (GST_IS_MESSAGE (message));
1363   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_INFO);
1364
1365   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
1366       GST_QUARK (GERROR), G_TYPE_ERROR, gerror,
1367       GST_QUARK (DEBUG), G_TYPE_STRING, debug, NULL);
1368 }
1369
1370 /**
1371  * gst_message_parse_segment_start:
1372  * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_START.
1373  * @format: (out) (allow-none): Result location for the format, or %NULL
1374  * @position: (out) (allow-none): Result location for the position, or %NULL
1375  *
1376  * Extracts the position and format from the segment start message.
1377  *
1378  * MT safe.
1379  */
1380 void
1381 gst_message_parse_segment_start (GstMessage * message, GstFormat * format,
1382     gint64 * position)
1383 {
1384   GstStructure *structure;
1385
1386   g_return_if_fail (GST_IS_MESSAGE (message));
1387   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_START);
1388
1389   structure = GST_MESSAGE_STRUCTURE (message);
1390   if (format)
1391     *format = (GstFormat)
1392         g_value_get_enum (gst_structure_id_get_value (structure,
1393             GST_QUARK (FORMAT)));
1394   if (position)
1395     *position =
1396         g_value_get_int64 (gst_structure_id_get_value (structure,
1397             GST_QUARK (POSITION)));
1398 }
1399
1400 /**
1401  * gst_message_parse_segment_done:
1402  * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_DONE.
1403  * @format: (out) (allow-none): Result location for the format, or %NULL
1404  * @position: (out) (allow-none): Result location for the position, or %NULL
1405  *
1406  * Extracts the position and format from the segment done message.
1407  *
1408  * MT safe.
1409  */
1410 void
1411 gst_message_parse_segment_done (GstMessage * message, GstFormat * format,
1412     gint64 * position)
1413 {
1414   GstStructure *structure;
1415
1416   g_return_if_fail (GST_IS_MESSAGE (message));
1417   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_DONE);
1418
1419   structure = GST_MESSAGE_STRUCTURE (message);
1420   if (format)
1421     *format = (GstFormat)
1422         g_value_get_enum (gst_structure_id_get_value (structure,
1423             GST_QUARK (FORMAT)));
1424   if (position)
1425     *position =
1426         g_value_get_int64 (gst_structure_id_get_value (structure,
1427             GST_QUARK (POSITION)));
1428 }
1429
1430 /**
1431  * gst_message_parse_async_done:
1432  * @message: A valid #GstMessage of type GST_MESSAGE_ASYNC_DONE.
1433  * @running_time: (out) (allow-none): Result location for the running_time or %NULL
1434  *
1435  * Extract the running_time from the async_done message.
1436  *
1437  * MT safe.
1438  */
1439 void
1440 gst_message_parse_async_done (GstMessage * message, GstClockTime * running_time)
1441 {
1442   GstStructure *structure;
1443
1444   g_return_if_fail (GST_IS_MESSAGE (message));
1445   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ASYNC_DONE);
1446
1447   structure = GST_MESSAGE_STRUCTURE (message);
1448   if (running_time)
1449     *running_time =
1450         g_value_get_uint64 (gst_structure_id_get_value (structure,
1451             GST_QUARK (RUNNING_TIME)));
1452 }
1453
1454 /**
1455  * gst_message_parse_request_state:
1456  * @message: A valid #GstMessage of type GST_MESSAGE_REQUEST_STATE.
1457  * @state: (out) (allow-none): Result location for the requested state or %NULL
1458  *
1459  * Extract the requested state from the request_state message.
1460  *
1461  * MT safe.
1462  */
1463 void
1464 gst_message_parse_request_state (GstMessage * message, GstState * state)
1465 {
1466   GstStructure *structure;
1467
1468   g_return_if_fail (GST_IS_MESSAGE (message));
1469   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_REQUEST_STATE);
1470
1471   structure = GST_MESSAGE_STRUCTURE (message);
1472   if (state)
1473     *state = (GstState)
1474         g_value_get_enum (gst_structure_id_get_value (structure,
1475             GST_QUARK (NEW_STATE)));
1476 }
1477
1478 /**
1479  * gst_message_new_stream_status:
1480  * @src: The object originating the message.
1481  * @type: The stream status type.
1482  * @owner: (transfer none): the owner element of @src.
1483  *
1484  * Create a new stream status message. This message is posted when a streaming
1485  * thread is created/destroyed or when the state changed.
1486  * 
1487  * Returns: (transfer full): the new stream status message.
1488  *
1489  * MT safe.
1490  */
1491 GstMessage *
1492 gst_message_new_stream_status (GstObject * src, GstStreamStatusType type,
1493     GstElement * owner)
1494 {
1495   GstMessage *message;
1496   GstStructure *structure;
1497
1498   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STREAM_STATUS),
1499       GST_QUARK (TYPE), GST_TYPE_STREAM_STATUS_TYPE, (gint) type,
1500       GST_QUARK (OWNER), GST_TYPE_ELEMENT, owner, NULL);
1501   message = gst_message_new_custom (GST_MESSAGE_STREAM_STATUS, src, structure);
1502
1503   return message;
1504 }
1505
1506 /**
1507  * gst_message_parse_stream_status:
1508  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1509  * @type: (out): A pointer to hold the status type
1510  * @owner: (out) (transfer none): The owner element of the message source
1511  *
1512  * Extracts the stream status type and owner the GstMessage. The returned
1513  * owner remains valid for as long as the reference to @message is valid and
1514  * should thus not be unreffed.
1515  *
1516  * MT safe.
1517  */
1518 void
1519 gst_message_parse_stream_status (GstMessage * message,
1520     GstStreamStatusType * type, GstElement ** owner)
1521 {
1522   const GValue *owner_gvalue;
1523   GstStructure *structure;
1524
1525   g_return_if_fail (GST_IS_MESSAGE (message));
1526   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS);
1527
1528   structure = GST_MESSAGE_STRUCTURE (message);
1529   owner_gvalue = gst_structure_id_get_value (structure, GST_QUARK (OWNER));
1530   g_return_if_fail (owner_gvalue != NULL);
1531
1532   if (type)
1533     *type = (GstStreamStatusType)
1534         g_value_get_enum (gst_structure_id_get_value (structure,
1535             GST_QUARK (TYPE)));
1536   if (owner)
1537     *owner = (GstElement *) g_value_get_object (owner_gvalue);
1538 }
1539
1540 /**
1541  * gst_message_set_stream_status_object:
1542  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1543  * @object: the object controlling the streaming
1544  *
1545  * Configures the object handling the streaming thread. This is usually a
1546  * GstTask object but other objects might be added in the future.
1547  */
1548 void
1549 gst_message_set_stream_status_object (GstMessage * message,
1550     const GValue * object)
1551 {
1552   GstStructure *structure;
1553
1554   g_return_if_fail (GST_IS_MESSAGE (message));
1555   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS);
1556
1557   structure = GST_MESSAGE_STRUCTURE (message);
1558   gst_structure_id_set_value (structure, GST_QUARK (OBJECT), object);
1559 }
1560
1561 /**
1562  * gst_message_get_stream_status_object:
1563  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_STATUS.
1564  *
1565  * Extracts the object managing the streaming thread from @message.
1566  *
1567  * Returns: a GValue containing the object that manages the streaming thread.
1568  * This object is usually of type GstTask but other types can be added in the
1569  * future. The object remains valid as long as @message is valid.
1570  */
1571 const GValue *
1572 gst_message_get_stream_status_object (GstMessage * message)
1573 {
1574   const GValue *result;
1575   GstStructure *structure;
1576
1577   g_return_val_if_fail (GST_IS_MESSAGE (message), NULL);
1578   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_STATUS,
1579       NULL);
1580
1581   structure = GST_MESSAGE_STRUCTURE (message);
1582   result = gst_structure_id_get_value (structure, GST_QUARK (OBJECT));
1583
1584   return result;
1585 }
1586
1587 /**
1588  * gst_message_new_step_done:
1589  * @src: The object originating the message.
1590  * @format: the format of @amount
1591  * @amount: the amount of stepped data
1592  * @rate: the rate of the stepped amount
1593  * @flush: is this an flushing step
1594  * @intermediate: is this an intermediate step
1595  * @duration: the duration of the data
1596  * @eos: the step caused EOS
1597  *
1598  * This message is posted by elements when they complete a part, when @intermediate set
1599  * to %TRUE, or a complete step operation.
1600  *
1601  * @duration will contain the amount of time (in GST_FORMAT_TIME) of the stepped
1602  * @amount of media in format @format.
1603  *
1604  * Returns: (transfer full): the new step_done message.
1605  *
1606  * MT safe.
1607  */
1608 GstMessage *
1609 gst_message_new_step_done (GstObject * src, GstFormat format, guint64 amount,
1610     gdouble rate, gboolean flush, gboolean intermediate, guint64 duration,
1611     gboolean eos)
1612 {
1613   GstMessage *message;
1614   GstStructure *structure;
1615
1616   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STEP_DONE),
1617       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1618       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1619       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1620       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1621       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate,
1622       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1623       GST_QUARK (EOS), G_TYPE_BOOLEAN, eos, NULL);
1624   message = gst_message_new_custom (GST_MESSAGE_STEP_DONE, src, structure);
1625
1626   return message;
1627 }
1628
1629 /**
1630  * gst_message_parse_step_done:
1631  * @message: A valid #GstMessage of type GST_MESSAGE_STEP_DONE.
1632  * @format: (out) (allow-none): result location for the format
1633  * @amount: (out) (allow-none): result location for the amount
1634  * @rate: (out) (allow-none): result location for the rate
1635  * @flush: (out) (allow-none): result location for the flush flag
1636  * @intermediate: (out) (allow-none): result location for the intermediate flag
1637  * @duration: (out) (allow-none): result location for the duration
1638  * @eos: (out) (allow-none): result location for the EOS flag
1639  *
1640  * Extract the values the step_done message.
1641  *
1642  * MT safe.
1643  */
1644 void
1645 gst_message_parse_step_done (GstMessage * message, GstFormat * format,
1646     guint64 * amount, gdouble * rate, gboolean * flush, gboolean * intermediate,
1647     guint64 * duration, gboolean * eos)
1648 {
1649   GstStructure *structure;
1650
1651   g_return_if_fail (GST_IS_MESSAGE (message));
1652   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STEP_DONE);
1653
1654   structure = GST_MESSAGE_STRUCTURE (message);
1655   gst_structure_id_get (structure,
1656       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1657       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1658       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1659       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1660       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate,
1661       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1662       GST_QUARK (EOS), G_TYPE_BOOLEAN, eos, NULL);
1663 }
1664
1665 /**
1666  * gst_message_new_step_start:
1667  * @src: The object originating the message.
1668  * @active: if the step is active or queued
1669  * @format: the format of @amount
1670  * @amount: the amount of stepped data
1671  * @rate: the rate of the stepped amount
1672  * @flush: is this an flushing step
1673  * @intermediate: is this an intermediate step
1674  *
1675  * This message is posted by elements when they accept or activate a new step
1676  * event for @amount in @format. 
1677  *
1678  * @active is set to %FALSE when the element accepted the new step event and has
1679  * queued it for execution in the streaming threads.
1680  *
1681  * @active is set to %TRUE when the element has activated the step operation and
1682  * is now ready to start executing the step in the streaming thread. After this
1683  * message is emitted, the application can queue a new step operation in the
1684  * element.
1685  *
1686  * Returns: (transfer full): The new step_start message. 
1687  *
1688  * MT safe.
1689  */
1690 GstMessage *
1691 gst_message_new_step_start (GstObject * src, gboolean active, GstFormat format,
1692     guint64 amount, gdouble rate, gboolean flush, gboolean intermediate)
1693 {
1694   GstMessage *message;
1695   GstStructure *structure;
1696
1697   structure = gst_structure_new_id (GST_QUARK (MESSAGE_STEP_START),
1698       GST_QUARK (ACTIVE), G_TYPE_BOOLEAN, active,
1699       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1700       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1701       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1702       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1703       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1704   message = gst_message_new_custom (GST_MESSAGE_STEP_START, src, structure);
1705
1706   return message;
1707 }
1708
1709 /**
1710  * gst_message_parse_step_start:
1711  * @message: A valid #GstMessage of type GST_MESSAGE_STEP_DONE.
1712  * @active: (out) (allow-none): result location for the active flag
1713  * @format: (out) (allow-none): result location for the format
1714  * @amount: (out) (allow-none): result location for the amount
1715  * @rate: (out) (allow-none): result location for the rate
1716  * @flush: (out) (allow-none): result location for the flush flag
1717  * @intermediate: (out) (allow-none): result location for the intermediate flag
1718  *
1719  * Extract the values from step_start message.
1720  *
1721  * MT safe.
1722  */
1723 void
1724 gst_message_parse_step_start (GstMessage * message, gboolean * active,
1725     GstFormat * format, guint64 * amount, gdouble * rate, gboolean * flush,
1726     gboolean * intermediate)
1727 {
1728   GstStructure *structure;
1729
1730   g_return_if_fail (GST_IS_MESSAGE (message));
1731   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STEP_START);
1732
1733   structure = GST_MESSAGE_STRUCTURE (message);
1734   gst_structure_id_get (structure,
1735       GST_QUARK (ACTIVE), G_TYPE_BOOLEAN, active,
1736       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1737       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1738       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1739       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1740       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1741 }
1742
1743 /**
1744  * gst_message_new_qos:
1745  * @src: The object originating the message.
1746  * @live: if the message was generated by a live element
1747  * @running_time: the running time of the buffer that generated the message
1748  * @stream_time: the stream time of the buffer that generated the message
1749  * @timestamp: the timestamps of the buffer that generated the message
1750  * @duration: the duration of the buffer that generated the message
1751  *
1752  * A QOS message is posted on the bus whenever an element decides to drop a
1753  * buffer because of QoS reasons or whenever it changes its processing strategy
1754  * because of QoS reasons (quality adjustments such as processing at lower
1755  * accuracy).
1756  *
1757  * This message can be posted by an element that performs synchronisation against the
1758  * clock (live) or it could be dropped by an element that performs QoS because of QOS
1759  * events received from a downstream element (!live).
1760  *
1761  * @running_time, @stream_time, @timestamp, @duration should be set to the
1762  * respective running-time, stream-time, timestamp and duration of the (dropped)
1763  * buffer that generated the QoS event. Values can be left to
1764  * GST_CLOCK_TIME_NONE when unknown.
1765  *
1766  * Returns: (transfer full): The new qos message.
1767  *
1768  * MT safe.
1769  */
1770 GstMessage *
1771 gst_message_new_qos (GstObject * src, gboolean live, guint64 running_time,
1772     guint64 stream_time, guint64 timestamp, guint64 duration)
1773 {
1774   GstMessage *message;
1775   GstStructure *structure;
1776
1777   structure = gst_structure_new_id (GST_QUARK (MESSAGE_QOS),
1778       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
1779       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time,
1780       GST_QUARK (STREAM_TIME), G_TYPE_UINT64, stream_time,
1781       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp,
1782       GST_QUARK (DURATION), G_TYPE_UINT64, duration,
1783       GST_QUARK (JITTER), G_TYPE_INT64, (gint64) 0,
1784       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, (gdouble) 1.0,
1785       GST_QUARK (QUALITY), G_TYPE_INT, (gint) 1000000,
1786       GST_QUARK (FORMAT), GST_TYPE_FORMAT, GST_FORMAT_UNDEFINED,
1787       GST_QUARK (PROCESSED), G_TYPE_UINT64, (guint64) - 1,
1788       GST_QUARK (DROPPED), G_TYPE_UINT64, (guint64) - 1, NULL);
1789   message = gst_message_new_custom (GST_MESSAGE_QOS, src, structure);
1790
1791   return message;
1792 }
1793
1794 /**
1795  * gst_message_set_qos_values:
1796  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1797  * @jitter: The difference of the running-time against the deadline.
1798  * @proportion: Long term prediction of the ideal rate relative to normal rate
1799  * to get optimal quality.
1800  * @quality: An element dependent integer value that specifies the current
1801  * quality level of the element. The default maximum quality is 1000000.
1802  *
1803  * Set the QoS values that have been calculated/analysed from the QoS data
1804  *
1805  * MT safe.
1806  */
1807 void
1808 gst_message_set_qos_values (GstMessage * message, gint64 jitter,
1809     gdouble proportion, gint quality)
1810 {
1811   GstStructure *structure;
1812
1813   g_return_if_fail (GST_IS_MESSAGE (message));
1814   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1815
1816   structure = GST_MESSAGE_STRUCTURE (message);
1817   gst_structure_id_set (structure,
1818       GST_QUARK (JITTER), G_TYPE_INT64, jitter,
1819       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1820       GST_QUARK (QUALITY), G_TYPE_INT, quality, NULL);
1821 }
1822
1823 /**
1824  * gst_message_set_qos_stats:
1825  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1826  * @format: Units of the 'processed' and 'dropped' fields. Video sinks and video
1827  * filters will use GST_FORMAT_BUFFERS (frames). Audio sinks and audio filters
1828  * will likely use GST_FORMAT_DEFAULT (samples).
1829  * @processed: Total number of units correctly processed since the last state
1830  * change to READY or a flushing operation.
1831  * @dropped: Total number of units dropped since the last state change to READY
1832  * or a flushing operation.
1833  *
1834  * Set the QoS stats representing the history of the current continuous pipeline
1835  * playback period.
1836  *
1837  * When @format is @GST_FORMAT_UNDEFINED both @dropped and @processed are
1838  * invalid. Values of -1 for either @processed or @dropped mean unknown values.
1839  *
1840  * MT safe.
1841  */
1842 void
1843 gst_message_set_qos_stats (GstMessage * message, GstFormat format,
1844     guint64 processed, guint64 dropped)
1845 {
1846   GstStructure *structure;
1847
1848   g_return_if_fail (GST_IS_MESSAGE (message));
1849   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1850
1851   structure = GST_MESSAGE_STRUCTURE (message);
1852   gst_structure_id_set (structure,
1853       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1854       GST_QUARK (PROCESSED), G_TYPE_UINT64, processed,
1855       GST_QUARK (DROPPED), G_TYPE_UINT64, dropped, NULL);
1856 }
1857
1858 /**
1859  * gst_message_parse_qos:
1860  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1861  * @live: (out) (allow-none): if the message was generated by a live element
1862  * @running_time: (out) (allow-none): the running time of the buffer that
1863  *     generated the message
1864  * @stream_time: (out) (allow-none): the stream time of the buffer that
1865  *     generated the message
1866  * @timestamp: (out) (allow-none): the timestamps of the buffer that
1867  *     generated the message
1868  * @duration: (out) (allow-none): the duration of the buffer that
1869  *     generated the message
1870  *
1871  * Extract the timestamps and live status from the QoS message.
1872  *
1873  * The returned values give the running_time, stream_time, timestamp and
1874  * duration of the dropped buffer. Values of GST_CLOCK_TIME_NONE mean unknown
1875  * values.
1876  *
1877  * MT safe.
1878  */
1879 void
1880 gst_message_parse_qos (GstMessage * message, gboolean * live,
1881     guint64 * running_time, guint64 * stream_time, guint64 * timestamp,
1882     guint64 * duration)
1883 {
1884   GstStructure *structure;
1885
1886   g_return_if_fail (GST_IS_MESSAGE (message));
1887   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1888
1889   structure = GST_MESSAGE_STRUCTURE (message);
1890   gst_structure_id_get (structure,
1891       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
1892       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time,
1893       GST_QUARK (STREAM_TIME), G_TYPE_UINT64, stream_time,
1894       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp,
1895       GST_QUARK (DURATION), G_TYPE_UINT64, duration, NULL);
1896 }
1897
1898 /**
1899  * gst_message_parse_qos_values:
1900  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1901  * @jitter: (out) (allow-none): The difference of the running-time against
1902  *     the deadline.
1903  * @proportion: (out) (allow-none): Long term prediction of the ideal rate
1904  *     relative to normal rate to get optimal quality.
1905  * @quality: (out) (allow-none): An element dependent integer value that
1906  *     specifies the current quality level of the element. The default
1907  *     maximum quality is 1000000.
1908  *
1909  * Extract the QoS values that have been calculated/analysed from the QoS data
1910  *
1911  * MT safe.
1912  */
1913 void
1914 gst_message_parse_qos_values (GstMessage * message, gint64 * jitter,
1915     gdouble * proportion, gint * quality)
1916 {
1917   GstStructure *structure;
1918
1919   g_return_if_fail (GST_IS_MESSAGE (message));
1920   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1921
1922   structure = GST_MESSAGE_STRUCTURE (message);
1923   gst_structure_id_get (structure,
1924       GST_QUARK (JITTER), G_TYPE_INT64, jitter,
1925       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1926       GST_QUARK (QUALITY), G_TYPE_INT, quality, NULL);
1927 }
1928
1929 /**
1930  * gst_message_parse_qos_stats:
1931  * @message: A valid #GstMessage of type GST_MESSAGE_QOS.
1932  * @format: (out) (allow-none): Units of the 'processed' and 'dropped' fields.
1933  *     Video sinks and video filters will use GST_FORMAT_BUFFERS (frames).
1934  *     Audio sinks and audio filters will likely use GST_FORMAT_DEFAULT
1935  *     (samples).
1936  * @processed: (out) (allow-none): Total number of units correctly processed
1937  *     since the last state change to READY or a flushing operation.
1938  * @dropped: (out) (allow-none): Total number of units dropped since the last
1939  *     state change to READY or a flushing operation.
1940  *
1941  * Extract the QoS stats representing the history of the current continuous
1942  * pipeline playback period.
1943  *
1944  * When @format is @GST_FORMAT_UNDEFINED both @dropped and @processed are
1945  * invalid. Values of -1 for either @processed or @dropped mean unknown values.
1946  *
1947  * MT safe.
1948  */
1949 void
1950 gst_message_parse_qos_stats (GstMessage * message, GstFormat * format,
1951     guint64 * processed, guint64 * dropped)
1952 {
1953   GstStructure *structure;
1954
1955   g_return_if_fail (GST_IS_MESSAGE (message));
1956   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS);
1957
1958   structure = GST_MESSAGE_STRUCTURE (message);
1959   gst_structure_id_get (structure,
1960       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1961       GST_QUARK (PROCESSED), G_TYPE_UINT64, processed,
1962       GST_QUARK (DROPPED), G_TYPE_UINT64, dropped, NULL);
1963 }
1964
1965 /**
1966  * gst_message_new_progress:
1967  * @src: The object originating the message.
1968  * @type: a #GstProgressType
1969  * @code: a progress code
1970  * @text: free, user visible text describing the progress
1971  *
1972  * Progress messages are posted by elements when they use an asynchronous task
1973  * to perform actions triggered by a state change.
1974  *
1975  * @code contains a well defined string describing the action.
1976  * @test should contain a user visible string detailing the current action.
1977  *
1978  * Returns: (transfer full): The new qos message.
1979  */
1980 GstMessage *
1981 gst_message_new_progress (GstObject * src, GstProgressType type,
1982     const gchar * code, const gchar * text)
1983 {
1984   GstMessage *message;
1985   GstStructure *structure;
1986   gint percent = 100, timeout = -1;
1987
1988   g_return_val_if_fail (code != NULL, NULL);
1989   g_return_val_if_fail (text != NULL, NULL);
1990
1991   if (type == GST_PROGRESS_TYPE_START || type == GST_PROGRESS_TYPE_CONTINUE)
1992     percent = 0;
1993
1994   structure = gst_structure_new_id (GST_QUARK (MESSAGE_PROGRESS),
1995       GST_QUARK (TYPE), GST_TYPE_PROGRESS_TYPE, type,
1996       GST_QUARK (CODE), G_TYPE_STRING, code,
1997       GST_QUARK (TEXT), G_TYPE_STRING, text,
1998       GST_QUARK (PERCENT), G_TYPE_INT, percent,
1999       GST_QUARK (TIMEOUT), G_TYPE_INT, timeout, NULL);
2000   message = gst_message_new_custom (GST_MESSAGE_PROGRESS, src, structure);
2001
2002   return message;
2003 }
2004
2005 /**
2006  * gst_message_parse_progress:
2007  * @message: A valid #GstMessage of type GST_MESSAGE_PROGRESS.
2008  * @type: (out) (allow-none): location for the type
2009  * @code: (out) (allow-none) (transfer full): location for the code
2010  * @text: (out) (allow-none) (transfer full): location for the text
2011  *
2012  * Parses the progress @type, @code and @text.
2013  */
2014 void
2015 gst_message_parse_progress (GstMessage * message, GstProgressType * type,
2016     gchar ** code, gchar ** text)
2017 {
2018   GstStructure *structure;
2019
2020   g_return_if_fail (GST_IS_MESSAGE (message));
2021   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_PROGRESS);
2022
2023   structure = GST_MESSAGE_STRUCTURE (message);
2024   gst_structure_id_get (structure,
2025       GST_QUARK (TYPE), GST_TYPE_PROGRESS_TYPE, type,
2026       GST_QUARK (CODE), G_TYPE_STRING, code,
2027       GST_QUARK (TEXT), G_TYPE_STRING, text, NULL);
2028 }
2029
2030 /**
2031  * gst_message_new_toc:
2032  * @src: the object originating the message.
2033  * @toc: (transfer none): #GstToc structure for the message.
2034  * @updated: whether TOC was updated or not.
2035  *
2036  * Create a new TOC message. The message is posted by elements
2037  * that discovered or updated a TOC.
2038  *
2039  * Returns: (transfer full): a new TOC message.
2040  *
2041  * MT safe.
2042  */
2043 GstMessage *
2044 gst_message_new_toc (GstObject * src, GstToc * toc, gboolean updated)
2045 {
2046   GstStructure *toc_struct;
2047
2048   g_return_val_if_fail (toc != NULL, NULL);
2049
2050   toc_struct = gst_structure_new_id (GST_QUARK (MESSAGE_TOC),
2051       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2052       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2053
2054   return gst_message_new_custom (GST_MESSAGE_TOC, src, toc_struct);
2055 }
2056
2057 /**
2058  * gst_message_parse_toc:
2059  * @message: a valid #GstMessage of type GST_MESSAGE_TOC.
2060  * @toc: (out) (transfer full): return location for the TOC.
2061  * @updated: (out): return location for the updated flag.
2062  *
2063  * Extract the TOC from the #GstMessage. The TOC returned in the
2064  * output argument is a copy; the caller must free it with
2065  * gst_toc_unref() when done.
2066  *
2067  * MT safe.
2068  */
2069 void
2070 gst_message_parse_toc (GstMessage * message, GstToc ** toc, gboolean * updated)
2071 {
2072   g_return_if_fail (GST_IS_MESSAGE (message));
2073   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_TOC);
2074   g_return_if_fail (toc != NULL);
2075
2076   gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2077       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2078       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2079 }
2080
2081 /**
2082  * gst_message_new_reset_time:
2083  * @src: (transfer none): The object originating the message.
2084  * @running_time: the requested running-time
2085  *
2086  * This message is posted when the pipeline running-time should be reset to
2087  * @running_time, like after a flushing seek.
2088  *
2089  * Returns: (transfer full): The new reset_time message.
2090  *
2091  * MT safe.
2092  */
2093 GstMessage *
2094 gst_message_new_reset_time (GstObject * src, GstClockTime running_time)
2095 {
2096   GstMessage *message;
2097   GstStructure *structure;
2098
2099   structure = gst_structure_new_id (GST_QUARK (MESSAGE_RESET_TIME),
2100       GST_QUARK (RUNNING_TIME), G_TYPE_UINT64, running_time, NULL);
2101   message = gst_message_new_custom (GST_MESSAGE_RESET_TIME, src, structure);
2102
2103   return message;
2104 }
2105
2106 /**
2107  * gst_message_parse_reset_time:
2108  * @message: A valid #GstMessage of type GST_MESSAGE_RESET_TIME.
2109  * @running_time: (out) (allow-none): Result location for the running_time or
2110  *      %NULL
2111  *
2112  * Extract the running-time from the RESET_TIME message.
2113  *
2114  * MT safe.
2115  */
2116 void
2117 gst_message_parse_reset_time (GstMessage * message, GstClockTime * running_time)
2118 {
2119   GstStructure *structure;
2120
2121   g_return_if_fail (GST_IS_MESSAGE (message));
2122   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_RESET_TIME);
2123
2124   structure = GST_MESSAGE_STRUCTURE (message);
2125   if (running_time)
2126     *running_time =
2127         g_value_get_uint64 (gst_structure_id_get_value (structure,
2128             GST_QUARK (RUNNING_TIME)));
2129 }
2130
2131 /**
2132  * gst_message_new_stream_start:
2133  * @src: (transfer none): The object originating the message.
2134  *
2135  * Create a new stream_start message. This message is generated and posted in
2136  * the sink elements of a GstBin. The bin will only forward the STREAM_START
2137  * message to the application if all sinks have posted an STREAM_START message.
2138  *
2139  * Returns: (transfer full): The new stream_start message.
2140  *
2141  * MT safe.
2142  */
2143 GstMessage *
2144 gst_message_new_stream_start (GstObject * src)
2145 {
2146   GstMessage *message;
2147   GstStructure *s;
2148
2149   s = gst_structure_new_id_empty (GST_QUARK (MESSAGE_STREAM_START));
2150   message = gst_message_new_custom (GST_MESSAGE_STREAM_START, src, s);
2151
2152   return message;
2153 }
2154
2155
2156 /**
2157  * gst_message_set_group_id:
2158  * @message: the message
2159  * @group_id: the group id
2160  *
2161  * Sets the group id on the stream-start message.
2162  *
2163  * All streams that have the same group id are supposed to be played
2164  * together, i.e. all streams inside a container file should have the
2165  * same group id but different stream ids. The group id should change
2166  * each time the stream is started, resulting in different group ids
2167  * each time a file is played for example.
2168  *
2169  * MT safe.
2170  *
2171  * Since: 1.2
2172  */
2173 void
2174 gst_message_set_group_id (GstMessage * message, guint group_id)
2175 {
2176   GstStructure *structure;
2177
2178   g_return_if_fail (GST_IS_MESSAGE (message));
2179   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START);
2180   g_return_if_fail (gst_message_is_writable (message));
2181
2182   structure = GST_MESSAGE_STRUCTURE (message);
2183   gst_structure_id_set (structure, GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id,
2184       NULL);
2185 }
2186
2187 /**
2188  * gst_message_parse_group_id:
2189  * @message: A valid #GstMessage of type GST_MESSAGE_STREAM_START.
2190  * @group_id: (out) (allow-none): Result location for the group id or
2191  *      %NULL
2192  *
2193  * Extract the group from the STREAM_START message.
2194  *
2195  * Returns: %TRUE if the message had a group id set, %FALSE otherwise
2196  *
2197  * MT safe.
2198  *
2199  * Since: 1.2
2200  */
2201 gboolean
2202 gst_message_parse_group_id (GstMessage * message, guint * group_id)
2203 {
2204   GstStructure *structure;
2205   const GValue *v;
2206
2207   g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
2208   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STREAM_START,
2209       FALSE);
2210
2211   if (!group_id)
2212     return TRUE;
2213
2214   structure = GST_MESSAGE_STRUCTURE (message);
2215
2216   v = gst_structure_id_get_value (structure, GST_QUARK (GROUP_ID));
2217   if (!v)
2218     return FALSE;
2219
2220   *group_id = g_value_get_uint (v);
2221   return TRUE;
2222 }
2223
2224 /**
2225  * gst_message_new_need_context:
2226  * @src: (transfer none): The object originating the message.
2227  * @context_type: The context type that is needed
2228  *
2229  * This message is posted when an element needs a specific #GstContext.
2230  *
2231  * Returns: (transfer full): The new need-context message.
2232  *
2233  * MT safe.
2234  *
2235  * Since: 1.2
2236  */
2237 GstMessage *
2238 gst_message_new_need_context (GstObject * src, const gchar * context_type)
2239 {
2240   GstMessage *message;
2241   GstStructure *structure;
2242
2243   g_return_val_if_fail (context_type != NULL, NULL);
2244
2245   structure = gst_structure_new_id (GST_QUARK (MESSAGE_NEED_CONTEXT),
2246       GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
2247   message = gst_message_new_custom (GST_MESSAGE_NEED_CONTEXT, src, structure);
2248
2249   return message;
2250 }
2251
2252 /**
2253  * gst_message_parse_context_type:
2254  * @message: a GST_MESSAGE_NEED_CONTEXT type message
2255  * @context_type: (out) (allow-none): the context type, or %NULL
2256  *
2257  * Parse a context type from an existing GST_MESSAGE_NEED_CONTEXT message.
2258  *
2259  * Returns: a #gboolean indicating if the parsing succeeded.
2260  *
2261  * Since: 1.2
2262  */
2263 gboolean
2264 gst_message_parse_context_type (GstMessage * message,
2265     const gchar ** context_type)
2266 {
2267   GstStructure *structure;
2268   const GValue *value;
2269
2270   g_return_val_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT,
2271       FALSE);
2272
2273   structure = GST_MESSAGE_STRUCTURE (message);
2274
2275   if (context_type) {
2276     value = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT_TYPE));
2277     *context_type = g_value_get_string (value);
2278   }
2279
2280   return TRUE;
2281 }
2282
2283 /**
2284  * gst_message_new_have_context:
2285  * @src: (transfer none): The object originating the message.
2286  * @context: (transfer full): the context
2287  *
2288  * This message is posted when an element has a new local #GstContext.
2289  *
2290  * Returns: (transfer full): The new have-context message.
2291  *
2292  * MT safe.
2293  *
2294  * Since: 1.2
2295  */
2296 GstMessage *
2297 gst_message_new_have_context (GstObject * src, GstContext * context)
2298 {
2299   GstMessage *message;
2300   GstStructure *structure;
2301
2302   structure = gst_structure_new_id (GST_QUARK (MESSAGE_HAVE_CONTEXT),
2303       GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2304   message = gst_message_new_custom (GST_MESSAGE_HAVE_CONTEXT, src, structure);
2305   gst_context_unref (context);
2306
2307   return message;
2308 }
2309
2310 /**
2311  * gst_message_parse_have_context:
2312  * @message: A valid #GstMessage of type GST_MESSAGE_HAVE_CONTEXT.
2313  * @context: (out) (transfer full) (allow-none): Result location for the
2314  *      context or %NULL
2315  *
2316  * Extract the context from the HAVE_CONTEXT message.
2317  *
2318  * MT safe.
2319  *
2320  * Since: 1.2
2321  */
2322 void
2323 gst_message_parse_have_context (GstMessage * message, GstContext ** context)
2324 {
2325   g_return_if_fail (GST_IS_MESSAGE (message));
2326   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_HAVE_CONTEXT);
2327
2328   if (context)
2329     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2330         GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2331 }
2332
2333 /**
2334  * gst_message_new_device_added:
2335  * @src: The #GstObject that created the message
2336  * @device: (transfer none): The new #GstDevice
2337  *
2338  * Creates a new device-added message. The device-added message is produced by
2339  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. They announce the appearance
2340  * of monitored devices.
2341  *
2342  * Returns: a newly allocated #GstMessage
2343  *
2344  * Since: 1.4
2345  */
2346 GstMessage *
2347 gst_message_new_device_added (GstObject * src, GstDevice * device)
2348 {
2349   GstMessage *message;
2350   GstStructure *structure;
2351
2352   g_return_val_if_fail (device != NULL, NULL);
2353   g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
2354
2355   structure = gst_structure_new_id (GST_QUARK (MESSAGE_DEVICE_ADDED),
2356       GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2357   message = gst_message_new_custom (GST_MESSAGE_DEVICE_ADDED, src, structure);
2358
2359   return message;
2360 }
2361
2362 /**
2363  * gst_message_parse_device_added:
2364  * @device: (out) (allow-none) (transfer none): A location where to store a
2365  *  pointer to the new #GstDevice, or %NULL
2366  * 
2367  * Parses a device-added message. The device-added message is produced by
2368  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. It announces the appearance
2369  * of monitored devices.
2370  *
2371  * Since: 1.4
2372  */
2373 void
2374 gst_message_parse_device_added (GstMessage * message, GstDevice ** device)
2375 {
2376   g_return_if_fail (GST_IS_MESSAGE (message));
2377   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DEVICE_ADDED);
2378
2379   if (device)
2380     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2381         GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2382 }
2383
2384 /**
2385  * gst_message_new_device_removed:
2386  * @src: The #GstObject that created the message
2387  * @device: (transfer none): The removed #GstDevice
2388  *
2389  * Creates a new device-removed message. The device-removed message is produced
2390  * by #GstDeviceMonitor or a #GstGlobalDeviceMonitor. They announce the
2391  * disappearance of monitored devices.
2392  *
2393  * Returns: a newly allocated #GstMessage
2394  *
2395  * Since: 1.4
2396  */
2397 GstMessage *
2398 gst_message_new_device_removed (GstObject * src, GstDevice * device)
2399 {
2400   GstMessage *message;
2401   GstStructure *structure;
2402
2403   g_return_val_if_fail (device != NULL, NULL);
2404   g_return_val_if_fail (GST_IS_DEVICE (device), NULL);
2405
2406   structure = gst_structure_new_id (GST_QUARK (MESSAGE_DEVICE_REMOVED),
2407       GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2408   message = gst_message_new_custom (GST_MESSAGE_DEVICE_REMOVED, src, structure);
2409
2410   return message;
2411 }
2412
2413 /**
2414  * gst_message_parse_device_removed:
2415  * @device: (out) (allow-none) (transfer none): A location where to store a
2416  *  pointer to the removed #GstDevice, or %NULL
2417  *
2418  * Parses a device-removed message. The device-removed message is produced by
2419  * #GstDeviceMonitor or a #GstGlobalDeviceMonitor. It announces the
2420  * disappearance of monitored devices.
2421  *
2422  * Since: 1.4
2423  */
2424 void
2425 gst_message_parse_device_removed (GstMessage * message, GstDevice ** device)
2426 {
2427   g_return_if_fail (GST_IS_MESSAGE (message));
2428   g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DEVICE_REMOVED);
2429
2430   if (device)
2431     gst_structure_id_get (GST_MESSAGE_STRUCTURE (message),
2432         GST_QUARK (DEVICE), GST_TYPE_DEVICE, device, NULL);
2433 }