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