docs: improve API docs
[platform/upstream/gstreamer.git] / gst / gstevent.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstevent.c: GstEvent subsystem
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstevent
26  * @short_description: Structure describing events that are passed up and down
27  *                     a pipeline
28  * @see_also: #GstPad, #GstElement
29  *
30  * The event class provides factory methods to construct events for sending
31  * and functions to query (parse) received events.
32  *
33  * Events are usually created with gst_event_new_*() which takes event-type
34  * specific parameters as arguments.
35  * To send an event application will usually use gst_element_send_event() and
36  * elements will use gst_pad_send_event() or gst_pad_push_event().
37  * The event should be unreffed with gst_event_unref() if it has not been sent.
38  *
39  * Events that have been received can be parsed with their respective
40  * gst_event_parse_*() functions. It is valid to pass %NULL for unwanted details.
41  *
42  * Events are passed between elements in parallel to the data stream. Some events
43  * are serialized with buffers, others are not. Some events only travel downstream,
44  * others only upstream. Some events can travel both upstream and downstream.
45  *
46  * The events are used to signal special conditions in the datastream such as
47  * EOS (end of stream) or the start of a new stream-segment.
48  * Events are also used to flush the pipeline of any pending data.
49  *
50  * Most of the event API is used inside plugins. Applications usually only
51  * construct and use seek events.
52  * To do that gst_event_new_seek() is used to create a seek event. It takes
53  * the needed parameters to specify seeking time and mode.
54  * <example>
55  * <title>performing a seek on a pipeline</title>
56  *   <programlisting>
57  *   GstEvent *event;
58  *   gboolean result;
59  *   ...
60  *   // construct a seek event to play the media from second 2 to 5, flush
61  *   // the pipeline to decrease latency.
62  *   event = gst_event_new_seek (1.0, 
63  *      GST_FORMAT_TIME, 
64  *      GST_SEEK_FLAG_FLUSH,
65  *      GST_SEEK_TYPE_SET, 2 * GST_SECOND,
66  *      GST_SEEK_TYPE_SET, 5 * GST_SECOND);
67  *   ...
68  *   result = gst_element_send_event (pipeline, event);
69  *   if (!result)
70  *     g_warning ("seek failed");
71  *   ...
72  *   </programlisting>
73  * </example>
74  *
75  * Last reviewed on 2012-03-28 (0.11.3)
76  */
77
78
79 #include "gst_private.h"
80 #include <string.h>             /* memcpy */
81
82 #include "gstinfo.h"
83 #include "gstevent.h"
84 #include "gstenumtypes.h"
85 #include "gstutils.h"
86 #include "gstquark.h"
87 #include "gstvalue.h"
88
89 GType _gst_event_type = 0;
90
91 typedef struct
92 {
93   GstEvent event;
94
95   GstStructure *structure;
96 } GstEventImpl;
97
98 #define GST_EVENT_STRUCTURE(e)  (((GstEventImpl *)(e))->structure)
99
100 typedef struct
101 {
102   const gint type;
103   const gchar *name;
104   GQuark quark;
105 } GstEventQuarks;
106
107 static GstEventQuarks event_quarks[] = {
108   {GST_EVENT_UNKNOWN, "unknown", 0},
109   {GST_EVENT_FLUSH_START, "flush-start", 0},
110   {GST_EVENT_FLUSH_STOP, "flush-stop", 0},
111   {GST_EVENT_STREAM_START, "stream-start", 0},
112   {GST_EVENT_CAPS, "caps", 0},
113   {GST_EVENT_STREAM_CONFIG, "stream-config", 0},
114   {GST_EVENT_SEGMENT, "segment", 0},
115   {GST_EVENT_TAG, "tag", 0},
116   {GST_EVENT_TOC, "toc", 0},
117   {GST_EVENT_BUFFERSIZE, "buffersize", 0},
118   {GST_EVENT_SINK_MESSAGE, "sink-message", 0},
119   {GST_EVENT_EOS, "eos", 0},
120   {GST_EVENT_SEGMENT_DONE, "segment-done", 0},
121   {GST_EVENT_GAP, "gap", 0},
122   {GST_EVENT_QOS, "qos", 0},
123   {GST_EVENT_SEEK, "seek", 0},
124   {GST_EVENT_NAVIGATION, "navigation", 0},
125   {GST_EVENT_LATENCY, "latency", 0},
126   {GST_EVENT_STEP, "step", 0},
127   {GST_EVENT_RECONFIGURE, "reconfigure", 0},
128   {GST_EVENT_TOC_SELECT, "toc-select", 0},
129   {GST_EVENT_CUSTOM_UPSTREAM, "custom-upstream", 0},
130   {GST_EVENT_CUSTOM_DOWNSTREAM, "custom-downstream", 0},
131   {GST_EVENT_CUSTOM_DOWNSTREAM_OOB, "custom-downstream-oob", 0},
132   {GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, "custom-downstream-sticky", 0},
133   {GST_EVENT_CUSTOM_BOTH, "custom-both", 0},
134   {GST_EVENT_CUSTOM_BOTH_OOB, "custom-both-oob", 0},
135
136   {0, NULL, 0}
137 };
138
139 GST_DEFINE_MINI_OBJECT_TYPE (GstEvent, gst_event);
140
141 void
142 _priv_gst_event_initialize (void)
143 {
144   gint i;
145
146   _gst_event_type = gst_event_get_type ();
147
148   g_type_class_ref (gst_seek_flags_get_type ());
149   g_type_class_ref (gst_seek_type_get_type ());
150
151   for (i = 0; event_quarks[i].name; i++) {
152     event_quarks[i].quark = g_quark_from_static_string (event_quarks[i].name);
153   }
154 }
155
156 /**
157  * gst_event_type_get_name:
158  * @type: the event type
159  *
160  * Get a printable name for the given event type. Do not modify or free.
161  *
162  * Returns: a reference to the static name of the event.
163  */
164 const gchar *
165 gst_event_type_get_name (GstEventType type)
166 {
167   gint i;
168
169   for (i = 0; event_quarks[i].name; i++) {
170     if (type == event_quarks[i].type)
171       return event_quarks[i].name;
172   }
173   return "unknown";
174 }
175
176 /**
177  * gst_event_type_to_quark:
178  * @type: the event type
179  *
180  * Get the unique quark for the given event type.
181  *
182  * Returns: the quark associated with the event type
183  */
184 GQuark
185 gst_event_type_to_quark (GstEventType type)
186 {
187   gint i;
188
189   for (i = 0; event_quarks[i].name; i++) {
190     if (type == event_quarks[i].type)
191       return event_quarks[i].quark;
192   }
193   return 0;
194 }
195
196 /**
197  * gst_event_type_get_flags:
198  * @type: a #GstEventType
199  *
200  * Gets the #GstEventTypeFlags associated with @type.
201  *
202  * Returns: a #GstEventTypeFlags.
203  */
204 GstEventTypeFlags
205 gst_event_type_get_flags (GstEventType type)
206 {
207   GstEventTypeFlags ret;
208
209   ret = type & ((1 << GST_EVENT_NUM_SHIFT) - 1);
210
211   return ret;
212 }
213
214 static void
215 _gst_event_free (GstEvent * event)
216 {
217   GstStructure *s;
218
219   g_return_if_fail (event != NULL);
220   g_return_if_fail (GST_IS_EVENT (event));
221
222   GST_CAT_LOG (GST_CAT_EVENT, "freeing event %p type %s", event,
223       GST_EVENT_TYPE_NAME (event));
224
225   s = GST_EVENT_STRUCTURE (event);
226
227   if (s) {
228     gst_structure_set_parent_refcount (s, NULL);
229     gst_structure_free (s);
230   }
231
232   g_slice_free1 (sizeof (GstEventImpl), event);
233 }
234
235 static void gst_event_init (GstEventImpl * event, GstEventType type);
236
237 static GstEvent *
238 _gst_event_copy (GstEvent * event)
239 {
240   GstEventImpl *copy;
241   GstStructure *s;
242
243   copy = g_slice_new0 (GstEventImpl);
244
245   gst_event_init (copy, GST_EVENT_TYPE (event));
246
247   GST_EVENT_TIMESTAMP (copy) = GST_EVENT_TIMESTAMP (event);
248   GST_EVENT_SEQNUM (copy) = GST_EVENT_SEQNUM (event);
249
250   s = GST_EVENT_STRUCTURE (event);
251   if (s) {
252     GST_EVENT_STRUCTURE (copy) = gst_structure_copy (s);
253     gst_structure_set_parent_refcount (GST_EVENT_STRUCTURE (copy),
254         &copy->event.mini_object.refcount);
255   } else {
256     GST_EVENT_STRUCTURE (copy) = NULL;
257   }
258   return GST_EVENT_CAST (copy);
259 }
260
261 static void
262 gst_event_init (GstEventImpl * event, GstEventType type)
263 {
264   gst_mini_object_init (GST_MINI_OBJECT_CAST (event), _gst_event_type);
265
266   event->event.mini_object.copy = (GstMiniObjectCopyFunction) _gst_event_copy;
267   event->event.mini_object.free = (GstMiniObjectFreeFunction) _gst_event_free;
268
269   GST_EVENT_TYPE (event) = type;
270   GST_EVENT_TIMESTAMP (event) = GST_CLOCK_TIME_NONE;
271   GST_EVENT_SEQNUM (event) = gst_util_seqnum_next ();
272 }
273
274
275 /**
276  * gst_event_new_custom:
277  * @type: The type of the new event
278  * @structure: (transfer full): the structure for the event. The event will
279  *     take ownership of the structure.
280  *
281  * Create a new custom-typed event. This can be used for anything not
282  * handled by other event-specific functions to pass an event to another
283  * element.
284  *
285  * Make sure to allocate an event type with the #GST_EVENT_MAKE_TYPE macro,
286  * assigning a free number and filling in the correct direction and
287  * serialization flags.
288  *
289  * New custom events can also be created by subclassing the event type if
290  * needed.
291  *
292  * Returns: (transfer full): the new custom event.
293  */
294 GstEvent *
295 gst_event_new_custom (GstEventType type, GstStructure * structure)
296 {
297   GstEventImpl *event;
298
299   event = g_slice_new0 (GstEventImpl);
300
301   GST_CAT_DEBUG (GST_CAT_EVENT, "creating new event %p %s %d", event,
302       gst_event_type_get_name (type), type);
303
304   if (structure) {
305     /* structure must not have a parent */
306     if (!gst_structure_set_parent_refcount (structure,
307             &event->event.mini_object.refcount))
308       goto had_parent;
309
310   }
311   gst_event_init (event, type);
312
313   GST_EVENT_STRUCTURE (event) = structure;
314
315   return GST_EVENT_CAST (event);
316
317   /* ERRORS */
318 had_parent:
319   {
320     g_slice_free1 (sizeof (GstEventImpl), event);
321     g_warning ("structure is already owned by another object");
322     return NULL;
323   }
324 }
325
326 /**
327  * gst_event_get_structure:
328  * @event: The #GstEvent.
329  *
330  * Access the structure of the event.
331  *
332  * Returns: The structure of the event. The structure is still
333  * owned by the event, which means that you should not free it and
334  * that the pointer becomes invalid when you free the event.
335  *
336  * MT safe.
337  */
338 const GstStructure *
339 gst_event_get_structure (GstEvent * event)
340 {
341   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
342
343   return GST_EVENT_STRUCTURE (event);
344 }
345
346 /**
347  * gst_event_writable_structure:
348  * @event: The #GstEvent.
349  *
350  * Get a writable version of the structure.
351  *
352  * Returns: The structure of the event. The structure is still
353  * owned by the event, which means that you should not free it and
354  * that the pointer becomes invalid when you free the event.
355  * This function checks if @event is writable and will never return NULL.
356  *
357  * MT safe.
358  */
359 GstStructure *
360 gst_event_writable_structure (GstEvent * event)
361 {
362   GstStructure *structure;
363
364   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
365   g_return_val_if_fail (gst_event_is_writable (event), NULL);
366
367   structure = GST_EVENT_STRUCTURE (event);
368
369   if (structure == NULL) {
370     structure =
371         gst_structure_new_id_empty (gst_event_type_to_quark (GST_EVENT_TYPE
372             (event)));
373     gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
374     GST_EVENT_STRUCTURE (event) = structure;
375   }
376   return structure;
377 }
378
379 /**
380  * gst_event_has_name:
381  * @event: The #GstEvent.
382  * @name: name to check
383  *
384  * Checks if @event has the given @name. This function is usually used to
385  * check the name of a custom event.
386  *
387  * Returns: %TRUE if @name matches the name of the event structure.
388  *
389  * Since: 0.10.20
390  */
391 gboolean
392 gst_event_has_name (GstEvent * event, const gchar * name)
393 {
394   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
395
396   if (GST_EVENT_STRUCTURE (event) == NULL)
397     return FALSE;
398
399   return gst_structure_has_name (GST_EVENT_STRUCTURE (event), name);
400 }
401
402 /**
403  * gst_event_get_seqnum:
404  * @event: A #GstEvent.
405  *
406  * Retrieve the sequence number of a event.
407  *
408  * Events have ever-incrementing sequence numbers, which may also be set
409  * explicitly via gst_event_set_seqnum(). Sequence numbers are typically used to
410  * indicate that a event corresponds to some other set of events or messages,
411  * for example an EOS event corresponding to a SEEK event. It is considered good
412  * practice to make this correspondence when possible, though it is not
413  * required.
414  *
415  * Note that events and messages share the same sequence number incrementor;
416  * two events or messages will never have the same sequence number unless
417  * that correspondence was made explicitly.
418  *
419  * Returns: The event's sequence number.
420  *
421  * MT safe.
422  *
423  * Since: 0.10.22
424  */
425 guint32
426 gst_event_get_seqnum (GstEvent * event)
427 {
428   g_return_val_if_fail (GST_IS_EVENT (event), -1);
429
430   return GST_EVENT_SEQNUM (event);
431 }
432
433 /**
434  * gst_event_set_seqnum:
435  * @event: A #GstEvent.
436  * @seqnum: A sequence number.
437  *
438  * Set the sequence number of a event.
439  *
440  * This function might be called by the creator of a event to indicate that the
441  * event relates to other events or messages. See gst_event_get_seqnum() for
442  * more information.
443  *
444  * MT safe.
445  *
446  * Since: 0.10.22
447  */
448 void
449 gst_event_set_seqnum (GstEvent * event, guint32 seqnum)
450 {
451   g_return_if_fail (GST_IS_EVENT (event));
452
453   GST_EVENT_SEQNUM (event) = seqnum;
454 }
455
456 /**
457  * gst_event_new_flush_start:
458  *
459  * Allocate a new flush start event. The flush start event can be sent
460  * upstream and downstream and travels out-of-bounds with the dataflow.
461  *
462  * It marks pads as being flushing and will make them return
463  * #GST_FLOW_FLUSHING when used for data flow with gst_pad_push(),
464  * gst_pad_chain(), gst_pad_get_range() and gst_pad_pull_range().
465  * Any event (except a #GST_EVENT_FLUSH_STOP) received
466  * on a flushing pad will return %FALSE immediately.
467  *
468  * Elements should unlock any blocking functions and exit their streaming
469  * functions as fast as possible when this event is received.
470  *
471  * This event is typically generated after a seek to flush out all queued data
472  * in the pipeline so that the new media is played as soon as possible.
473  *
474  * Returns: (transfer full): a new flush start event.
475  */
476 GstEvent *
477 gst_event_new_flush_start (void)
478 {
479   return gst_event_new_custom (GST_EVENT_FLUSH_START, NULL);
480 }
481
482 /**
483  * gst_event_new_flush_stop:
484  * @reset_time: if time should be reset
485  *
486  * Allocate a new flush stop event. The flush stop event can be sent
487  * upstream and downstream and travels serialized with the dataflow.
488  * It is typically sent after sending a FLUSH_START event to make the
489  * pads accept data again.
490  *
491  * Elements can process this event synchronized with the dataflow since
492  * the preceeding FLUSH_START event stopped the dataflow.
493  *
494  * This event is typically generated to complete a seek and to resume
495  * dataflow.
496  *
497  * Returns: (transfer full): a new flush stop event.
498  */
499 GstEvent *
500 gst_event_new_flush_stop (gboolean reset_time)
501 {
502   GstEvent *event;
503
504   GST_CAT_INFO (GST_CAT_EVENT, "creating flush stop %d", reset_time);
505
506   event = gst_event_new_custom (GST_EVENT_FLUSH_STOP,
507       gst_structure_new_id (GST_QUARK (EVENT_FLUSH_STOP),
508           GST_QUARK (RESET_TIME), G_TYPE_BOOLEAN, reset_time, NULL));
509
510   return event;
511 }
512
513 /**
514  * gst_event_parse_flush_stop:
515  * @event: The event to parse
516  * @reset_time: (out): if time should be reset
517  *
518  * Parse the FLUSH_STOP event and retrieve the @reset_time member.
519  */
520 void
521 gst_event_parse_flush_stop (GstEvent * event, gboolean * reset_time)
522 {
523   GstStructure *structure;
524
525   g_return_if_fail (GST_IS_EVENT (event));
526   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP);
527
528   structure = GST_EVENT_STRUCTURE (event);
529   if (G_LIKELY (reset_time))
530     *reset_time =
531         g_value_get_boolean (gst_structure_id_get_value (structure,
532             GST_QUARK (RESET_TIME)));
533 }
534
535 /**
536  * gst_event_new_eos:
537  *
538  * Create a new EOS event. The eos event can only travel downstream
539  * synchronized with the buffer flow. Elements that receive the EOS
540  * event on a pad can return #GST_FLOW_EOS as a #GstFlowReturn
541  * when data after the EOS event arrives.
542  *
543  * The EOS event will travel down to the sink elements in the pipeline
544  * which will then post the #GST_MESSAGE_EOS on the bus after they have
545  * finished playing any buffered data.
546  *
547  * When all sinks have posted an EOS message, an EOS message is
548  * forwarded to the application.
549  *
550  * The EOS event itself will not cause any state transitions of the pipeline.
551  *
552  * Returns: (transfer full): the new EOS event.
553  */
554 GstEvent *
555 gst_event_new_eos (void)
556 {
557   return gst_event_new_custom (GST_EVENT_EOS, NULL);
558 }
559
560 /**
561  * gst_event_new_gap:
562  * @timestamp: the start time (pts) of the gap
563  * @duration: the duration of the gap
564  *
565  * Create a new GAP event. A gap event can be thought of as conceptually
566  * equivalent to a buffer to signal that there is no data for a certain
567  * amount of time. This is useful to signal a gap to downstream elements
568  * which may wait for data, such as muxers or mixers or overlays, especially
569  * for sparse streams such as subtitle streams.
570  *
571  * Returns: (transfer full): the new GAP event.
572  */
573 GstEvent *
574 gst_event_new_gap (GstClockTime timestamp, GstClockTime duration)
575 {
576   GstEvent *event;
577
578   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
579   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (duration), NULL);
580
581   GST_CAT_TRACE (GST_CAT_EVENT, "creating gap %" GST_TIME_FORMAT " - "
582       "%" GST_TIME_FORMAT " (duration: %" GST_TIME_FORMAT ")",
583       GST_TIME_ARGS (timestamp), GST_TIME_ARGS (timestamp + duration),
584       GST_TIME_ARGS (duration));
585
586   event = gst_event_new_custom (GST_EVENT_GAP,
587       gst_structure_new_id (GST_QUARK (EVENT_GAP),
588           GST_QUARK (TIMESTAMP), GST_TYPE_CLOCK_TIME, timestamp,
589           GST_QUARK (DURATION), GST_TYPE_CLOCK_TIME, duration, NULL));
590
591   return event;
592 }
593
594 /**
595  * gst_event_parse_gap:
596  * @event: a #GstEvent of type #GST_EVENT_GAP
597  * @timestamp: (out) (allow-none): location where to store the
598  *     start time (pts) of the gap, or %NULL
599  * @duration: (out) (allow-none): location where to store the duration of
600  *     the gap, or %NULL
601  *
602  * Extract timestamp and duration from a new GAP event.
603  */
604 void
605 gst_event_parse_gap (GstEvent * event, GstClockTime * timestamp,
606     GstClockTime * duration)
607 {
608   GstStructure *structure;
609
610   g_return_if_fail (GST_IS_EVENT (event));
611   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
612
613   structure = GST_EVENT_STRUCTURE (event);
614   gst_structure_id_get (structure,
615       GST_QUARK (TIMESTAMP), GST_TYPE_CLOCK_TIME, timestamp,
616       GST_QUARK (DURATION), GST_TYPE_CLOCK_TIME, duration, NULL);
617 }
618
619 /**
620  * gst_event_new_caps:
621  * @caps: (transfer none): a #GstCaps
622  *
623  * Create a new CAPS event for @caps. The caps event can only travel downstream
624  * synchronized with the buffer flow and contains the format of the buffers
625  * that will follow after the event.
626  *
627  * Returns: (transfer full): the new CAPS event.
628  */
629 GstEvent *
630 gst_event_new_caps (GstCaps * caps)
631 {
632   GstEvent *event;
633
634   g_return_val_if_fail (caps != NULL, NULL);
635   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
636
637   GST_CAT_INFO (GST_CAT_EVENT, "creating caps event %" GST_PTR_FORMAT, caps);
638
639   event = gst_event_new_custom (GST_EVENT_CAPS,
640       gst_structure_new_id (GST_QUARK (EVENT_CAPS),
641           GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL));
642
643   return event;
644 }
645
646 /**
647  * gst_event_parse_caps:
648  * @event: The event to parse
649  * @caps: (out): A pointer to the caps
650  *
651  * Get the caps from @event. The caps remains valid as long as @event remains
652  * valid.
653  */
654 void
655 gst_event_parse_caps (GstEvent * event, GstCaps ** caps)
656 {
657   GstStructure *structure;
658
659   g_return_if_fail (GST_IS_EVENT (event));
660   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
661
662   structure = GST_EVENT_STRUCTURE (event);
663   if (G_LIKELY (caps))
664     *caps =
665         g_value_get_boxed (gst_structure_id_get_value (structure,
666             GST_QUARK (CAPS)));
667 }
668
669 /**
670  * gst_event_new_stream_config:
671  * @flags: the stream config flags
672  *
673  * Create a new STREAM CONFIG event. The stream config event travels
674  * downstream synchronized with the buffer flow and contains stream
675  * configuration information for the stream, such as stream-headers
676  * or setup-data. It is optional and should be sent after the CAPS
677  * event.
678  *
679  * Returns: (transfer full): the new STREAM CONFIG event.
680  */
681 GstEvent *
682 gst_event_new_stream_config (GstStreamConfigFlags flags)
683 {
684   GstEvent *event;
685
686   GST_CAT_INFO (GST_CAT_EVENT, "creating stream info event, flags=0x%x", flags);
687
688   event = gst_event_new_custom (GST_EVENT_STREAM_CONFIG,
689       gst_structure_new_id (GST_QUARK (EVENT_STREAM_CONFIG),
690           GST_QUARK (FLAGS), GST_TYPE_STREAM_CONFIG_FLAGS, flags, NULL));
691
692   return event;
693 }
694
695 /**
696  * gst_event_parse_stream_config:
697  * @event: The event to parse
698  * @flags: (out): a pointer to a variable to store the stream config flags
699  *
700  * Get the stream config flags from @event.
701  */
702 void
703 gst_event_parse_stream_config (GstEvent * event, GstStreamConfigFlags * flags)
704 {
705   GstStructure *structure;
706
707   g_return_if_fail (GST_IS_EVENT (event));
708   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG);
709
710   structure = GST_EVENT_STRUCTURE (event);
711   if (G_LIKELY (flags != NULL)) {
712     *flags =
713         g_value_get_enum (gst_structure_id_get_value (structure,
714             GST_QUARK (FLAGS)));
715   }
716 }
717
718 /**
719  * gst_event_set_stream_config_setup_data:
720  * @event: a stream config event
721  * @buf: a #GstBuffer with setup data
722  *
723  * Set setup data on the stream info event to signal out of bound setup data
724  * to downstream elements. Unlike stream headers, setup data contains data
725  * that is required to interpret the data stream, but is not valid as-is
726  * inside the data stream and thus can't just be prepended to or inserted
727  * into the data stream.
728  */
729 void
730 gst_event_set_stream_config_setup_data (GstEvent * event, GstBuffer * buf)
731 {
732   GstStructure *s;
733
734   g_return_if_fail (GST_IS_EVENT (event));
735   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG);
736   g_return_if_fail (GST_IS_BUFFER (buf) && gst_buffer_get_size (buf) > 0);
737
738   s = GST_EVENT_STRUCTURE (event);
739   gst_structure_id_set (s, GST_QUARK (SETUP_DATA), GST_TYPE_BUFFER, buf, NULL);
740 }
741
742 /**
743  * gst_event_parse_stream_config_setup_data:
744  * @event: a stream config event
745  * @buf: (out) (transfer none): location where to store the #GstBuffer with setup data
746  *
747  * Extracts the setup data buffer from the stream info event. Will store
748  * %NULL in @buf if the event contains no setup data. The buffer returned
749  * will remain valid as long as @event remains valid. The caller should
750  * acquire a reference to to @buf if needed.
751  *
752  * Returns: TRUE if @event contained setup data and @buf has been set,
753  *     otherwise FALSE.
754  */
755 gboolean
756 gst_event_parse_stream_config_setup_data (GstEvent * event, GstBuffer ** buf)
757 {
758   const GValue *val;
759   GstStructure *s;
760
761   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
762   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG,
763       FALSE);
764   g_return_val_if_fail (buf != NULL, FALSE);
765
766   s = GST_EVENT_STRUCTURE (event);
767   val = gst_structure_id_get_value (s, GST_QUARK (SETUP_DATA));
768   if (val != NULL)
769     *buf = g_value_get_boxed (val);
770   else
771     *buf = NULL;
772
773   return (*buf != NULL);
774 }
775
776 /**
777  * gst_event_add_stream_config_header:
778  * @event: a stream config event
779  * @buf: a #GstBuffer with stream header data
780  *
781  * Adds a stream header to the stream info event to signal stream headers to
782  * to downstream elements such as multifilesink, tcpserversink etc. Stream
783  * headers can be and should usually be prepended to the data stream at any
784  * point in the stream (which requires a streamable format), e.g. to a new
785  * client connecting, or when starting a new file segment. stream header
786  * buffers will all be used together in the order they were added to the
787  * stream config event. Stream headers are sent as buffers at the beginning
788  * of the data flow in addition to the stream config event. Elements that
789  * care about stream headers need to make sure that they don't insert or
790  * interpret these header buffers twice if they interpret them.
791  */
792 void
793 gst_event_add_stream_config_header (GstEvent * event, GstBuffer * buf)
794 {
795   GstStructure *s;
796   GValue buf_val = { 0, };
797   GValue *val;
798
799   g_return_if_fail (GST_IS_EVENT (event));
800   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG);
801   g_return_if_fail (GST_IS_BUFFER (buf) && gst_buffer_get_size (buf) > 0);
802
803   g_value_init (&buf_val, GST_TYPE_BUFFER);
804   g_value_set_boxed (&buf_val, buf);
805
806   s = GST_EVENT_STRUCTURE (event);
807   val = (GValue *) gst_structure_id_get_value (s, GST_QUARK (STREAM_HEADERS));
808   if (val == NULL) {
809     GValue new_array = { 0, };
810
811     g_value_init (&new_array, GST_TYPE_ARRAY);
812     gst_value_array_append_value (&new_array, &buf_val);
813     gst_structure_id_take_value (s, GST_QUARK (STREAM_HEADERS), &new_array);
814   } else {
815     gst_value_array_append_value (val, &buf_val);
816   }
817   g_value_unset (&buf_val);
818 }
819
820 /**
821  * gst_event_get_n_stream_config_headers:
822  * @event: a stream config event
823  *
824  * Extract the number of stream header buffers.
825  *
826  * Returns: the number of stream header buffers attached to the stream info
827  * @event.
828  */
829 guint
830 gst_event_get_n_stream_config_headers (GstEvent * event)
831 {
832   const GValue *val;
833   GstStructure *s;
834   guint num = 0;
835
836   g_return_val_if_fail (GST_IS_EVENT (event), 0);
837   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG, 0);
838
839   s = GST_EVENT_STRUCTURE (event);
840   val = gst_structure_id_get_value (s, GST_QUARK (STREAM_HEADERS));
841
842   if (val != NULL)
843     num = gst_value_array_get_size (val);
844
845   return num;
846 }
847
848 /**
849  * gst_event_parse_nth_stream_config_header:
850  * @event: a stream config event
851  * @index: number of the stream header to retrieve
852  * @buf: (out) (transfer none): location where to store the n-th stream
853  *     header #GstBuffer
854  *
855  * Retrieves the n-th stream header buffer attached to the stream config
856  * event and stores it in @buf. Will store %NULL in @buf if there is no such
857  * stream header.
858  *
859  * Returns: TRUE if @event contained a stream header at @index and @buf has
860  *    been set, otherwise FALSE.
861  */
862 gboolean
863 gst_event_parse_nth_stream_config_header (GstEvent * event, guint index,
864     GstBuffer ** buf)
865 {
866   const GValue *val, *buf_val;
867   GstStructure *s;
868   GstBuffer *ret = NULL;
869
870   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
871   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_CONFIG,
872       FALSE);
873   g_return_val_if_fail (buf != NULL, FALSE);
874
875   s = GST_EVENT_STRUCTURE (event);
876   val = gst_structure_id_get_value (s, GST_QUARK (STREAM_HEADERS));
877
878   if (val != NULL) {
879     buf_val = gst_value_array_get_value (val, index);
880     if (buf_val != NULL)
881       ret = g_value_get_boxed (buf_val);
882   }
883
884   *buf = ret;
885   return (ret != NULL);
886 }
887
888 /**
889  * gst_event_new_segment:
890  * @segment: (transfer none): a #GstSegment
891  *
892  * Create a new SEGMENT event for @segment. The segment event can only travel
893  * downstream synchronized with the buffer flow and contains timing information
894  * and playback properties for the buffers that will follow.
895  *
896  * The newsegment event marks the range of buffers to be processed. All
897  * data not within the segment range is not to be processed. This can be
898  * used intelligently by plugins to apply more efficient methods of skipping
899  * unneeded data. The valid range is expressed with the @start and @stop
900  * values.
901  *
902  * The time value of the segment is used in conjunction with the start
903  * value to convert the buffer timestamps into the stream time. This is
904  * usually done in sinks to report the current stream_time.
905  * @time represents the stream_time of a buffer carrying a timestamp of
906  * @start. @time cannot be -1.
907  *
908  * @start cannot be -1, @stop can be -1. If there
909  * is a valid @stop given, it must be greater or equal the @start, including
910  * when the indicated playback @rate is < 0.
911  *
912  * The @applied_rate value provides information about any rate adjustment that
913  * has already been made to the timestamps and content on the buffers of the
914  * stream. (@rate * @applied_rate) should always equal the rate that has been
915  * requested for playback. For example, if an element has an input segment
916  * with intended playback @rate of 2.0 and applied_rate of 1.0, it can adjust
917  * incoming timestamps and buffer content by half and output a newsegment event
918  * with @rate of 1.0 and @applied_rate of 2.0
919  *
920  * After a newsegment event, the buffer stream time is calculated with:
921  *
922  *   time + (TIMESTAMP(buf) - start) * ABS (rate * applied_rate)
923  *
924  * Returns: (transfer full): the new SEGMENT event.
925  */
926 GstEvent *
927 gst_event_new_segment (const GstSegment * segment)
928 {
929   GstEvent *event;
930
931   g_return_val_if_fail (segment != NULL, NULL);
932   g_return_val_if_fail (segment->rate != 0.0, NULL);
933   g_return_val_if_fail (segment->applied_rate != 0.0, NULL);
934   g_return_val_if_fail (segment->format != GST_FORMAT_UNDEFINED, NULL);
935
936   GST_CAT_INFO (GST_CAT_EVENT, "creating segment event %" GST_SEGMENT_FORMAT,
937       segment);
938
939   event = gst_event_new_custom (GST_EVENT_SEGMENT,
940       gst_structure_new_id (GST_QUARK (EVENT_SEGMENT),
941           GST_QUARK (SEGMENT), GST_TYPE_SEGMENT, segment, NULL));
942
943   return event;
944 }
945
946 /**
947  * gst_event_parse_segment:
948  * @event: The event to parse
949  * @segment: (out) (transfer none): a pointer to a #GstSegment
950  *
951  * Parses a segment @event and stores the result in the given @segment location.
952  * @segment remains valid only until the @event is freed. Don't modify the segment
953  * and make a copy if you want to modify it or store it for later use.
954  */
955 void
956 gst_event_parse_segment (GstEvent * event, const GstSegment ** segment)
957 {
958   GstStructure *structure;
959
960   g_return_if_fail (GST_IS_EVENT (event));
961   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
962
963   if (segment) {
964     structure = GST_EVENT_STRUCTURE (event);
965     *segment = g_value_get_boxed (gst_structure_id_get_value (structure,
966             GST_QUARK (SEGMENT)));
967   }
968 }
969
970 /**
971  * gst_event_copy_segment:
972  * @event: The event to parse
973  * @segment: a pointer to a #GstSegment
974  *
975  * Parses a segment @event and copies the #GstSegment into the location
976  * given by @segment.
977  */
978 void
979 gst_event_copy_segment (GstEvent * event, GstSegment * segment)
980 {
981   const GstSegment *src;
982
983   g_return_if_fail (GST_IS_EVENT (event));
984   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
985
986   if (segment) {
987     gst_event_parse_segment (event, &src);
988     gst_segment_copy_into (src, segment);
989   }
990 }
991
992 /**
993  * gst_event_new_tag:
994  * @name: (transfer none): the name of the event
995  * @taglist: (transfer full): metadata list. The event will take ownership
996  *     of the taglist.
997  *
998  * Generates a metadata tag event from the given @taglist.
999  *
1000  * Since the TAG event has the %GST_EVENT_TYPE_STICKY_MULTI flag set, the
1001  * @name will be used to keep track of multiple tag events.
1002  *
1003  * Returns: (transfer full): a new #GstEvent
1004  */
1005 GstEvent *
1006 gst_event_new_tag (const gchar * name, GstTagList * taglist)
1007 {
1008   GstStructure *s;
1009   GValue val = G_VALUE_INIT;
1010
1011   g_return_val_if_fail (taglist != NULL, NULL);
1012
1013   s = gst_structure_new_empty (name);
1014   g_value_init (&val, GST_TYPE_TAG_LIST);
1015   g_value_take_boxed (&val, taglist);
1016   gst_structure_id_take_value (s, GST_QUARK (TAGLIST), &val);
1017   return gst_event_new_custom (GST_EVENT_TAG, s);
1018 }
1019
1020 /**
1021  * gst_event_parse_tag:
1022  * @event: a tag event
1023  * @taglist: (out) (transfer none): pointer to metadata list
1024  *
1025  * Parses a tag @event and stores the results in the given @taglist location.
1026  * No reference to the taglist will be returned, it remains valid only until
1027  * the @event is freed. Don't modify or free the taglist, make a copy if you
1028  * want to modify it or store it for later use.
1029  */
1030 void
1031 gst_event_parse_tag (GstEvent * event, GstTagList ** taglist)
1032 {
1033   const GValue *val;
1034
1035   g_return_if_fail (GST_IS_EVENT (event));
1036   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG);
1037
1038   val = gst_structure_id_get_value (GST_EVENT_STRUCTURE (event),
1039       GST_QUARK (TAGLIST));
1040
1041   if (taglist)
1042     *taglist = (GstTagList *) g_value_get_boxed (val);
1043 }
1044
1045 /* buffersize event */
1046 /**
1047  * gst_event_new_buffer_size:
1048  * @format: buffer format
1049  * @minsize: minimum buffer size
1050  * @maxsize: maximum buffer size
1051  * @async: thread behavior
1052  *
1053  * Create a new buffersize event. The event is sent downstream and notifies
1054  * elements that they should provide a buffer of the specified dimensions.
1055  *
1056  * When the @async flag is set, a thread boundary is preferred.
1057  *
1058  * Returns: (transfer full): a new #GstEvent
1059  */
1060 GstEvent *
1061 gst_event_new_buffer_size (GstFormat format, gint64 minsize,
1062     gint64 maxsize, gboolean async)
1063 {
1064   GstEvent *event;
1065   GstStructure *structure;
1066
1067   GST_CAT_INFO (GST_CAT_EVENT,
1068       "creating buffersize format %s, minsize %" G_GINT64_FORMAT
1069       ", maxsize %" G_GINT64_FORMAT ", async %d", gst_format_get_name (format),
1070       minsize, maxsize, async);
1071
1072   structure = gst_structure_new_id (GST_QUARK (EVENT_BUFFER_SIZE),
1073       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1074       GST_QUARK (MINSIZE), G_TYPE_INT64, minsize,
1075       GST_QUARK (MAXSIZE), G_TYPE_INT64, maxsize,
1076       GST_QUARK (ASYNC), G_TYPE_BOOLEAN, async, NULL);
1077   event = gst_event_new_custom (GST_EVENT_BUFFERSIZE, structure);
1078
1079   return event;
1080 }
1081
1082 /**
1083  * gst_event_parse_buffer_size:
1084  * @event: The event to query
1085  * @format: (out): A pointer to store the format in
1086  * @minsize: (out): A pointer to store the minsize in
1087  * @maxsize: (out): A pointer to store the maxsize in
1088  * @async: (out): A pointer to store the async-flag in
1089  *
1090  * Get the format, minsize, maxsize and async-flag in the buffersize event.
1091  */
1092 void
1093 gst_event_parse_buffer_size (GstEvent * event, GstFormat * format,
1094     gint64 * minsize, gint64 * maxsize, gboolean * async)
1095 {
1096   const GstStructure *structure;
1097
1098   g_return_if_fail (GST_IS_EVENT (event));
1099   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_BUFFERSIZE);
1100
1101   structure = GST_EVENT_STRUCTURE (event);
1102   if (format)
1103     *format = (GstFormat)
1104         g_value_get_enum (gst_structure_id_get_value (structure,
1105             GST_QUARK (FORMAT)));
1106   if (minsize)
1107     *minsize =
1108         g_value_get_int64 (gst_structure_id_get_value (structure,
1109             GST_QUARK (MINSIZE)));
1110   if (maxsize)
1111     *maxsize =
1112         g_value_get_int64 (gst_structure_id_get_value (structure,
1113             GST_QUARK (MAXSIZE)));
1114   if (async)
1115     *async =
1116         g_value_get_boolean (gst_structure_id_get_value (structure,
1117             GST_QUARK (ASYNC)));
1118 }
1119
1120 /**
1121  * gst_event_new_qos:
1122  * @type: the QoS type
1123  * @proportion: the proportion of the qos message
1124  * @diff: The time difference of the last Clock sync
1125  * @timestamp: The timestamp of the buffer
1126  *
1127  * Allocate a new qos event with the given values.
1128  * The QOS event is generated in an element that wants an upstream
1129  * element to either reduce or increase its rate because of
1130  * high/low CPU load or other resource usage such as network performance or
1131  * throttling. Typically sinks generate these events for each buffer
1132  * they receive.
1133  *
1134  * @type indicates the reason for the QoS event. #GST_QOS_TYPE_OVERFLOW is
1135  * used when a buffer arrived in time or when the sink cannot keep up with
1136  * the upstream datarate. #GST_QOS_TYPE_UNDERFLOW is when the sink is not
1137  * receiving buffers fast enough and thus has to drop late buffers. 
1138  * #GST_QOS_TYPE_THROTTLE is used when the datarate is artificially limited
1139  * by the application, for example to reduce power consumption.
1140  *
1141  * @proportion indicates the real-time performance of the streaming in the
1142  * element that generated the QoS event (usually the sink). The value is
1143  * generally computed based on more long term statistics about the streams
1144  * timestamps compared to the clock.
1145  * A value < 1.0 indicates that the upstream element is producing data faster
1146  * than real-time. A value > 1.0 indicates that the upstream element is not
1147  * producing data fast enough. 1.0 is the ideal @proportion value. The
1148  * proportion value can safely be used to lower or increase the quality of
1149  * the element.
1150  *
1151  * @diff is the difference against the clock in running time of the last
1152  * buffer that caused the element to generate the QOS event. A negative value
1153  * means that the buffer with @timestamp arrived in time. A positive value
1154  * indicates how late the buffer with @timestamp was. When throttling is
1155  * enabled, @diff will be set to the requested throttling interval.
1156  *
1157  * @timestamp is the timestamp of the last buffer that cause the element
1158  * to generate the QOS event. It is expressed in running time and thus an ever
1159  * increasing value.
1160  *
1161  * The upstream element can use the @diff and @timestamp values to decide
1162  * whether to process more buffers. For possitive @diff, all buffers with
1163  * timestamp <= @timestamp + @diff will certainly arrive late in the sink
1164  * as well. A (negative) @diff value so that @timestamp + @diff would yield a
1165  * result smaller than 0 is not allowed.
1166  *
1167  * The application can use general event probes to intercept the QoS
1168  * event and implement custom application specific QoS handling.
1169  *
1170  * Returns: (transfer full): a new QOS event.
1171  */
1172 GstEvent *
1173 gst_event_new_qos (GstQOSType type, gdouble proportion,
1174     GstClockTimeDiff diff, GstClockTime timestamp)
1175 {
1176   GstEvent *event;
1177   GstStructure *structure;
1178
1179   /* diff must be positive or timestamp + diff must be positive */
1180   g_return_val_if_fail (diff >= 0 || -diff <= timestamp, NULL);
1181
1182   GST_CAT_LOG (GST_CAT_EVENT,
1183       "creating qos type %d, proportion %lf, diff %" G_GINT64_FORMAT
1184       ", timestamp %" GST_TIME_FORMAT, type, proportion,
1185       diff, GST_TIME_ARGS (timestamp));
1186
1187   structure = gst_structure_new_id (GST_QUARK (EVENT_QOS),
1188       GST_QUARK (TYPE), GST_TYPE_QOS_TYPE, type,
1189       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1190       GST_QUARK (DIFF), G_TYPE_INT64, diff,
1191       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp, NULL);
1192   event = gst_event_new_custom (GST_EVENT_QOS, structure);
1193
1194   return event;
1195 }
1196
1197 /**
1198  * gst_event_parse_qos:
1199  * @event: The event to query
1200  * @type: (out): A pointer to store the QoS type in
1201  * @proportion: (out): A pointer to store the proportion in
1202  * @diff: (out): A pointer to store the diff in
1203  * @timestamp: (out): A pointer to store the timestamp in
1204  *
1205  * Get the type, proportion, diff and timestamp in the qos event. See
1206  * gst_event_new_qos() for more information about the different QoS values.
1207  */
1208 void
1209 gst_event_parse_qos (GstEvent * event, GstQOSType * type,
1210     gdouble * proportion, GstClockTimeDiff * diff, GstClockTime * timestamp)
1211 {
1212   const GstStructure *structure;
1213
1214   g_return_if_fail (GST_IS_EVENT (event));
1215   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1216
1217   structure = GST_EVENT_STRUCTURE (event);
1218   if (type)
1219     *type = (GstQOSType)
1220         g_value_get_enum (gst_structure_id_get_value (structure,
1221             GST_QUARK (TYPE)));
1222   if (proportion)
1223     *proportion =
1224         g_value_get_double (gst_structure_id_get_value (structure,
1225             GST_QUARK (PROPORTION)));
1226   if (diff)
1227     *diff =
1228         g_value_get_int64 (gst_structure_id_get_value (structure,
1229             GST_QUARK (DIFF)));
1230   if (timestamp)
1231     *timestamp =
1232         g_value_get_uint64 (gst_structure_id_get_value (structure,
1233             GST_QUARK (TIMESTAMP)));
1234 }
1235
1236 /**
1237  * gst_event_new_seek:
1238  * @rate: The new playback rate
1239  * @format: The format of the seek values
1240  * @flags: The optional seek flags
1241  * @start_type: The type and flags for the new start position
1242  * @start: The value of the new start position
1243  * @stop_type: The type and flags for the new stop position
1244  * @stop: The value of the new stop position
1245  *
1246  * Allocate a new seek event with the given parameters.
1247  *
1248  * The seek event configures playback of the pipeline between @start to @stop
1249  * at the speed given in @rate, also called a playback segment.
1250  * The @start and @stop values are expressed in @format.
1251  *
1252  * A @rate of 1.0 means normal playback rate, 2.0 means double speed.
1253  * Negatives values means backwards playback. A value of 0.0 for the
1254  * rate is not allowed and should be accomplished instead by PAUSING the
1255  * pipeline.
1256  *
1257  * A pipeline has a default playback segment configured with a start
1258  * position of 0, a stop position of -1 and a rate of 1.0. The currently
1259  * configured playback segment can be queried with #GST_QUERY_SEGMENT. 
1260  *
1261  * @start_type and @stop_type specify how to adjust the currently configured 
1262  * start and stop fields in playback segment. Adjustments can be made relative
1263  * or absolute to the last configured values. A type of #GST_SEEK_TYPE_NONE
1264  * means that the position should not be updated.
1265  *
1266  * When the rate is positive and @start has been updated, playback will start
1267  * from the newly configured start position. 
1268  *
1269  * For negative rates, playback will start from the newly configured stop
1270  * position (if any). If the stop position is updated, it must be different from
1271  * -1 (#GST_CLOCK_TIME_NONE) for negative rates.
1272  *
1273  * It is not possible to seek relative to the current playback position, to do
1274  * this, PAUSE the pipeline, query the current playback position with
1275  * #GST_QUERY_POSITION and update the playback segment current position with a
1276  * #GST_SEEK_TYPE_SET to the desired position.
1277  *
1278  * Returns: (transfer full): a new seek event.
1279  */
1280 GstEvent *
1281 gst_event_new_seek (gdouble rate, GstFormat format, GstSeekFlags flags,
1282     GstSeekType start_type, gint64 start, GstSeekType stop_type, gint64 stop)
1283 {
1284   GstEvent *event;
1285   GstStructure *structure;
1286
1287   g_return_val_if_fail (rate != 0.0, NULL);
1288
1289   if (format == GST_FORMAT_TIME) {
1290     GST_CAT_INFO (GST_CAT_EVENT,
1291         "creating seek rate %lf, format TIME, flags %d, "
1292         "start_type %d, start %" GST_TIME_FORMAT ", "
1293         "stop_type %d, stop %" GST_TIME_FORMAT,
1294         rate, flags, start_type, GST_TIME_ARGS (start),
1295         stop_type, GST_TIME_ARGS (stop));
1296   } else {
1297     GST_CAT_INFO (GST_CAT_EVENT,
1298         "creating seek rate %lf, format %s, flags %d, "
1299         "start_type %d, start %" G_GINT64_FORMAT ", "
1300         "stop_type %d, stop %" G_GINT64_FORMAT,
1301         rate, gst_format_get_name (format), flags, start_type, start, stop_type,
1302         stop);
1303   }
1304
1305   structure = gst_structure_new_id (GST_QUARK (EVENT_SEEK),
1306       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1307       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1308       GST_QUARK (FLAGS), GST_TYPE_SEEK_FLAGS, flags,
1309       GST_QUARK (CUR_TYPE), GST_TYPE_SEEK_TYPE, start_type,
1310       GST_QUARK (CUR), G_TYPE_INT64, start,
1311       GST_QUARK (STOP_TYPE), GST_TYPE_SEEK_TYPE, stop_type,
1312       GST_QUARK (STOP), G_TYPE_INT64, stop, NULL);
1313   event = gst_event_new_custom (GST_EVENT_SEEK, structure);
1314
1315   return event;
1316 }
1317
1318 /**
1319  * gst_event_parse_seek:
1320  * @event: a seek event
1321  * @rate: (out): result location for the rate
1322  * @format: (out): result location for the stream format
1323  * @flags:  (out): result location for the #GstSeekFlags
1324  * @start_type: (out): result location for the #GstSeekType of the start position
1325  * @start: (out): result location for the start postion expressed in @format
1326  * @stop_type:  (out): result location for the #GstSeekType of the stop position
1327  * @stop: (out): result location for the stop postion expressed in @format
1328  *
1329  * Parses a seek @event and stores the results in the given result locations.
1330  */
1331 void
1332 gst_event_parse_seek (GstEvent * event, gdouble * rate,
1333     GstFormat * format, GstSeekFlags * flags, GstSeekType * start_type,
1334     gint64 * start, GstSeekType * stop_type, gint64 * stop)
1335 {
1336   const GstStructure *structure;
1337
1338   g_return_if_fail (GST_IS_EVENT (event));
1339   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
1340
1341   structure = GST_EVENT_STRUCTURE (event);
1342   if (rate)
1343     *rate =
1344         g_value_get_double (gst_structure_id_get_value (structure,
1345             GST_QUARK (RATE)));
1346   if (format)
1347     *format = (GstFormat)
1348         g_value_get_enum (gst_structure_id_get_value (structure,
1349             GST_QUARK (FORMAT)));
1350   if (flags)
1351     *flags = (GstSeekFlags)
1352         g_value_get_flags (gst_structure_id_get_value (structure,
1353             GST_QUARK (FLAGS)));
1354   if (start_type)
1355     *start_type = (GstSeekType)
1356         g_value_get_enum (gst_structure_id_get_value (structure,
1357             GST_QUARK (CUR_TYPE)));
1358   if (start)
1359     *start =
1360         g_value_get_int64 (gst_structure_id_get_value (structure,
1361             GST_QUARK (CUR)));
1362   if (stop_type)
1363     *stop_type = (GstSeekType)
1364         g_value_get_enum (gst_structure_id_get_value (structure,
1365             GST_QUARK (STOP_TYPE)));
1366   if (stop)
1367     *stop =
1368         g_value_get_int64 (gst_structure_id_get_value (structure,
1369             GST_QUARK (STOP)));
1370 }
1371
1372 /**
1373  * gst_event_new_navigation:
1374  * @structure: (transfer full): description of the event. The event will take
1375  *     ownership of the structure.
1376  *
1377  * Create a new navigation event from the given description.
1378  *
1379  * Returns: (transfer full): a new #GstEvent
1380  */
1381 GstEvent *
1382 gst_event_new_navigation (GstStructure * structure)
1383 {
1384   g_return_val_if_fail (structure != NULL, NULL);
1385
1386   return gst_event_new_custom (GST_EVENT_NAVIGATION, structure);
1387 }
1388
1389 /**
1390  * gst_event_new_latency:
1391  * @latency: the new latency value
1392  *
1393  * Create a new latency event. The event is sent upstream from the sinks and
1394  * notifies elements that they should add an additional @latency to the
1395  * running time before synchronising against the clock.
1396  *
1397  * The latency is mostly used in live sinks and is always expressed in
1398  * the time format.
1399  *
1400  * Returns: (transfer full): a new #GstEvent
1401  *
1402  * Since: 0.10.12
1403  */
1404 GstEvent *
1405 gst_event_new_latency (GstClockTime latency)
1406 {
1407   GstEvent *event;
1408   GstStructure *structure;
1409
1410   GST_CAT_INFO (GST_CAT_EVENT,
1411       "creating latency event %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1412
1413   structure = gst_structure_new_id (GST_QUARK (EVENT_LATENCY),
1414       GST_QUARK (LATENCY), G_TYPE_UINT64, latency, NULL);
1415   event = gst_event_new_custom (GST_EVENT_LATENCY, structure);
1416
1417   return event;
1418 }
1419
1420 /**
1421  * gst_event_parse_latency:
1422  * @event: The event to query
1423  * @latency: (out): A pointer to store the latency in.
1424  *
1425  * Get the latency in the latency event.
1426  *
1427  * Since: 0.10.12
1428  */
1429 void
1430 gst_event_parse_latency (GstEvent * event, GstClockTime * latency)
1431 {
1432   g_return_if_fail (GST_IS_EVENT (event));
1433   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_LATENCY);
1434
1435   if (latency)
1436     *latency =
1437         g_value_get_uint64 (gst_structure_id_get_value (GST_EVENT_STRUCTURE
1438             (event), GST_QUARK (LATENCY)));
1439 }
1440
1441 /**
1442  * gst_event_new_step:
1443  * @format: the format of @amount
1444  * @amount: the amount of data to step
1445  * @rate: the step rate
1446  * @flush: flushing steps
1447  * @intermediate: intermediate steps
1448  *
1449  * Create a new step event. The purpose of the step event is to instruct a sink
1450  * to skip @amount (expressed in @format) of media. It can be used to implement
1451  * stepping through the video frame by frame or for doing fast trick modes.
1452  *
1453  * A rate of <= 0.0 is not allowed. Pause the pipeline, for the effect of rate
1454  * = 0.0 or first reverse the direction of playback using a seek event to get
1455  * the same effect as rate < 0.0.
1456  *
1457  * The @flush flag will clear any pending data in the pipeline before starting
1458  * the step operation.
1459  *
1460  * The @intermediate flag instructs the pipeline that this step operation is
1461  * part of a larger step operation.
1462  *
1463  * Returns: (transfer full): a new #GstEvent
1464  *
1465  * Since: 0.10.24
1466  */
1467 GstEvent *
1468 gst_event_new_step (GstFormat format, guint64 amount, gdouble rate,
1469     gboolean flush, gboolean intermediate)
1470 {
1471   GstEvent *event;
1472   GstStructure *structure;
1473
1474   g_return_val_if_fail (rate > 0.0, NULL);
1475
1476   GST_CAT_INFO (GST_CAT_EVENT, "creating step event");
1477
1478   structure = gst_structure_new_id (GST_QUARK (EVENT_STEP),
1479       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1480       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1481       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1482       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1483       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1484   event = gst_event_new_custom (GST_EVENT_STEP, structure);
1485
1486   return event;
1487 }
1488
1489 /**
1490  * gst_event_parse_step:
1491  * @event: The event to query
1492  * @format: (out) (allow-none): a pointer to store the format in
1493  * @amount: (out) (allow-none): a pointer to store the amount in
1494  * @rate: (out) (allow-none): a pointer to store the rate in
1495  * @flush: (out) (allow-none): a pointer to store the flush boolean in
1496  * @intermediate: (out) (allow-none): a pointer to store the intermediate
1497  *     boolean in
1498  *
1499  * Parse the step event.
1500  *
1501  * Since: 0.10.24
1502  */
1503 void
1504 gst_event_parse_step (GstEvent * event, GstFormat * format, guint64 * amount,
1505     gdouble * rate, gboolean * flush, gboolean * intermediate)
1506 {
1507   const GstStructure *structure;
1508
1509   g_return_if_fail (GST_IS_EVENT (event));
1510   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STEP);
1511
1512   structure = GST_EVENT_STRUCTURE (event);
1513   if (format)
1514     *format =
1515         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1516             GST_QUARK (FORMAT)));
1517   if (amount)
1518     *amount = g_value_get_uint64 (gst_structure_id_get_value (structure,
1519             GST_QUARK (AMOUNT)));
1520   if (rate)
1521     *rate = g_value_get_double (gst_structure_id_get_value (structure,
1522             GST_QUARK (RATE)));
1523   if (flush)
1524     *flush = g_value_get_boolean (gst_structure_id_get_value (structure,
1525             GST_QUARK (FLUSH)));
1526   if (intermediate)
1527     *intermediate = g_value_get_boolean (gst_structure_id_get_value (structure,
1528             GST_QUARK (INTERMEDIATE)));
1529 }
1530
1531 /**
1532  * gst_event_new_reconfigure:
1533
1534  * Create a new reconfigure event. The purpose of the reconfingure event is
1535  * to travel upstream and make elements renegotiate their caps or reconfigure
1536  * their buffer pools. This is useful when changing properties on elements
1537  * or changing the topology of the pipeline.
1538  *
1539  * Returns: (transfer full): a new #GstEvent
1540  *
1541  * Since: 0.11.0
1542  */
1543 GstEvent *
1544 gst_event_new_reconfigure (void)
1545 {
1546   GstEvent *event;
1547
1548   GST_CAT_INFO (GST_CAT_EVENT, "creating reconfigure event");
1549
1550   event = gst_event_new_custom (GST_EVENT_RECONFIGURE, NULL);
1551
1552   return event;
1553 }
1554
1555 /**
1556  * gst_event_new_sink_message:
1557  * @name: a name for the event
1558  * @msg: (transfer none): the #GstMessage to be posted
1559  *
1560  * Create a new sink-message event. The purpose of the sink-message event is
1561  * to instruct a sink to post the message contained in the event synchronized
1562  * with the stream.
1563  *
1564  * @name is used to store multiple sticky events on one pad.
1565  *
1566  * Returns: (transfer full): a new #GstEvent
1567  *
1568  * Since: 0.10.26
1569  */
1570 /* FIXME 0.11: take ownership of msg for consistency? */
1571 GstEvent *
1572 gst_event_new_sink_message (const gchar * name, GstMessage * msg)
1573 {
1574   GstEvent *event;
1575   GstStructure *structure;
1576
1577   g_return_val_if_fail (msg != NULL, NULL);
1578
1579   GST_CAT_INFO (GST_CAT_EVENT, "creating sink-message event");
1580
1581   structure = gst_structure_new_id (g_quark_from_string (name),
1582       GST_QUARK (MESSAGE), GST_TYPE_MESSAGE, msg, NULL);
1583   event = gst_event_new_custom (GST_EVENT_SINK_MESSAGE, structure);
1584
1585   return event;
1586 }
1587
1588 /**
1589  * gst_event_parse_sink_message:
1590  * @event: The event to query
1591  * @msg: (out) (transfer full): a pointer to store the #GstMessage in.
1592  *
1593  * Parse the sink-message event. Unref @msg after usage.
1594  *
1595  * Since: 0.10.26
1596  */
1597 void
1598 gst_event_parse_sink_message (GstEvent * event, GstMessage ** msg)
1599 {
1600   const GstStructure *structure;
1601
1602   g_return_if_fail (GST_IS_EVENT (event));
1603   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE);
1604
1605   structure = GST_EVENT_STRUCTURE (event);
1606   if (msg)
1607     *msg =
1608         GST_MESSAGE (g_value_dup_boxed (gst_structure_id_get_value
1609             (structure, GST_QUARK (MESSAGE))));
1610 }
1611
1612 /**
1613  * gst_event_new_stream_start
1614  *
1615  * Create a new STREAM_START event. The stream start event can only
1616  * travel downstream synchronized with the buffer flow. It is expected
1617  * to be the first event that is sent for a new stream.
1618  *
1619  * Source elements, demuxers and other elements that create new streams
1620  * are supposed to send this event as the first event of a new stream. It
1621  * should not be send after a flushing seek or in similar situations
1622  * and is used to mark the beginning of a new logical stream. Elements
1623  * combining multiple streams must ensure that this event is only forwarded
1624  * downstream once and not for every single input stream.
1625  *
1626  * Returns: (transfer full): the new STREAM_START event.
1627  */
1628 GstEvent *
1629 gst_event_new_stream_start (void)
1630 {
1631   return gst_event_new_custom (GST_EVENT_STREAM_START, NULL);
1632 }
1633
1634 /**
1635  * gst_event_new_toc:
1636  * @toc: #GstToc structure.
1637  * @updated: whether @toc was updated or not.
1638  *
1639  * Generate a TOC event from the given @toc. The purpose of the TOC event is to
1640  * inform elements that some kind of the TOC was found.
1641  *
1642  * Returns: a new #GstEvent.
1643  *
1644  * Since: 0.10.37
1645  */
1646 GstEvent *
1647 gst_event_new_toc (GstToc * toc, gboolean updated)
1648 {
1649   GstStructure *toc_struct;
1650
1651   g_return_val_if_fail (toc != NULL, NULL);
1652
1653   GST_CAT_INFO (GST_CAT_EVENT, "creating toc event");
1654
1655   toc_struct = __gst_toc_to_structure (toc);
1656
1657   if (G_LIKELY (toc_struct != NULL)) {
1658     __gst_toc_structure_set_updated (toc_struct, updated);
1659     return gst_event_new_custom (GST_EVENT_TOC, toc_struct);
1660   } else
1661     return NULL;
1662 }
1663
1664 /**
1665  * gst_event_parse_toc:
1666  * @event: a TOC event.
1667  * @toc: (out): pointer to #GstToc structure.
1668  * @updated: (out): pointer to store TOC updated flag.
1669  *
1670  * Parse a TOC @event and store the results in the given @toc and @updated locations.
1671  *
1672  * Since: 0.10.37
1673  */
1674 void
1675 gst_event_parse_toc (GstEvent * event, GstToc ** toc, gboolean * updated)
1676 {
1677   const GstStructure *structure;
1678
1679   g_return_if_fail (event != NULL);
1680   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TOC);
1681   g_return_if_fail (toc != NULL);
1682
1683   structure = gst_event_get_structure (event);
1684   *toc = __gst_toc_from_structure (structure);
1685
1686   if (updated != NULL)
1687     *updated = __gst_toc_structure_get_updated (structure);
1688 }
1689
1690 /**
1691  * gst_event_new_toc_select:
1692  * @uid: UID in the TOC to start playback from.
1693  *
1694  * Generate a TOC select event with the given @uid. The purpose of the
1695  * TOC select event is to start playback based on the TOC's entry with the
1696  * given @uid.
1697  *
1698  * Returns: a new #GstEvent.
1699  *
1700  * Since: 0.10.37
1701  */
1702 GstEvent *
1703 gst_event_new_toc_select (const gchar * uid)
1704 {
1705   GstStructure *structure;
1706
1707   g_return_val_if_fail (uid != NULL, NULL);
1708
1709   GST_CAT_INFO (GST_CAT_EVENT, "creating toc select event for UID: %s", uid);
1710
1711   structure = gst_structure_new_id (GST_QUARK (EVENT_TOC_SELECT),
1712       GST_QUARK (UID), G_TYPE_STRING, uid, NULL);
1713
1714   return gst_event_new_custom (GST_EVENT_TOC_SELECT, structure);
1715 }
1716
1717 /**
1718  * gst_event_parse_toc_select:
1719  * @event: a TOC select event.
1720  * @uid: (out): storage for the selection UID.
1721  *
1722  * Parse a TOC select @event and store the results in the given @uid location.
1723  *
1724  * Since: 0.10.37
1725  */
1726 void
1727 gst_event_parse_toc_select (GstEvent * event, gchar ** uid)
1728 {
1729   const GstStructure *structure;
1730   const GValue *val;
1731
1732   g_return_if_fail (event != NULL);
1733   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TOC_SELECT);
1734
1735   structure = gst_event_get_structure (event);
1736   val = gst_structure_id_get_value (structure, GST_QUARK (UID));
1737
1738   if (uid != NULL)
1739     *uid = g_strdup (g_value_get_string (val));
1740
1741 }