tools: Count argc after parsing GOption on Windows
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gstevent
26  * @title: GstEvent
27  * @short_description: Structure describing events that are passed up and down
28  *                     a pipeline
29  * @see_also: #GstPad, #GstElement
30  *
31  * The event class provides factory methods to construct events for sending
32  * and functions to query (parse) received events.
33  *
34  * Events are usually created with gst_event_new_*() which takes event-type
35  * specific parameters as arguments.
36  * To send an event application will usually use gst_element_send_event() and
37  * elements will use gst_pad_send_event() or gst_pad_push_event().
38  * The event should be unreffed with gst_event_unref() if it has not been sent.
39  *
40  * Events that have been received can be parsed with their respective
41  * gst_event_parse_*() functions. It is valid to pass %NULL for unwanted details.
42  *
43  * Events are passed between elements in parallel to the data stream. Some events
44  * are serialized with buffers, others are not. Some events only travel downstream,
45  * others only upstream. Some events can travel both upstream and downstream.
46  *
47  * The events are used to signal special conditions in the datastream such as
48  * EOS (end of stream) or the start of a new stream-segment.
49  * Events are also used to flush the pipeline of any pending data.
50  *
51  * Most of the event API is used inside plugins. Applications usually only
52  * construct and use seek events.
53  * To do that gst_event_new_seek() is used to create a seek event. It takes
54  * the needed parameters to specify seeking time and mode.
55  * |[<!-- language="C" -->
56  *   GstEvent *event;
57  *   gboolean result;
58  *   ...
59  *   // construct a seek event to play the media from second 2 to 5, flush
60  *   // the pipeline to decrease latency.
61  *   event = gst_event_new_seek (1.0,
62  *      GST_FORMAT_TIME,
63  *      GST_SEEK_FLAG_FLUSH,
64  *      GST_SEEK_TYPE_SET, 2 * GST_SECOND,
65  *      GST_SEEK_TYPE_SET, 5 * GST_SECOND);
66  *   ...
67  *   result = gst_element_send_event (pipeline, event);
68  *   if (!result)
69  *     g_warning ("seek failed");
70  *   ...
71  * ]|
72  */
73
74 #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
75 #include "gst_private.h"
76 #include <string.h>             /* memcpy */
77
78 #include "gstinfo.h"
79 #include "gstevent.h"
80 #include "gstenumtypes.h"
81 #include "gstutils.h"
82 #include "gstquark.h"
83 #include "gstvalue.h"
84
85 GType _gst_event_type = 0;
86
87 typedef struct
88 {
89   GstEvent event;
90
91   GstStructure *structure;
92   gint64 running_time_offset;
93 } GstEventImpl;
94
95 #define GST_EVENT_STRUCTURE(e)  (((GstEventImpl *)(e))->structure)
96
97 typedef struct
98 {
99   const gint type;
100   const gchar *name;
101   GQuark quark;
102 } GstEventQuarks;
103
104 static GstEventQuarks event_quarks[] = {
105   {GST_EVENT_UNKNOWN, "unknown", 0},
106   {GST_EVENT_FLUSH_START, "flush-start", 0},
107   {GST_EVENT_FLUSH_STOP, "flush-stop", 0},
108   {GST_EVENT_SELECT_STREAMS, "select-streams", 0},
109   {GST_EVENT_STREAM_START, "stream-start", 0},
110   {GST_EVENT_STREAM_COLLECTION, "stream-collection", 0},
111   {GST_EVENT_CAPS, "caps", 0},
112   {GST_EVENT_SEGMENT, "segment", 0},
113   {GST_EVENT_TAG, "tag", 0},
114   {GST_EVENT_TOC, "toc", 0},
115   {GST_EVENT_PROTECTION, "protection", 0},
116   {GST_EVENT_BUFFERSIZE, "buffersize", 0},
117   {GST_EVENT_SINK_MESSAGE, "sink-message", 0},
118   {GST_EVENT_EOS, "eos", 0},
119   {GST_EVENT_SEGMENT_DONE, "segment-done", 0},
120   {GST_EVENT_GAP, "gap", 0},
121   {GST_EVENT_QOS, "qos", 0},
122   {GST_EVENT_SEEK, "seek", 0},
123   {GST_EVENT_NAVIGATION, "navigation", 0},
124   {GST_EVENT_LATENCY, "latency", 0},
125   {GST_EVENT_STEP, "step", 0},
126   {GST_EVENT_RECONFIGURE, "reconfigure", 0},
127   {GST_EVENT_TOC_SELECT, "toc-select", 0},
128   {GST_EVENT_CUSTOM_UPSTREAM, "custom-upstream", 0},
129   {GST_EVENT_CUSTOM_DOWNSTREAM, "custom-downstream", 0},
130   {GST_EVENT_CUSTOM_DOWNSTREAM_OOB, "custom-downstream-oob", 0},
131   {GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, "custom-downstream-sticky", 0},
132   {GST_EVENT_CUSTOM_BOTH, "custom-both", 0},
133   {GST_EVENT_CUSTOM_BOTH_OOB, "custom-both-oob", 0},
134   {GST_EVENT_STREAM_GROUP_DONE, "stream-group-done", 0},
135   {GST_EVENT_INSTANT_RATE_CHANGE, "instant-rate-change", 0},
136   {GST_EVENT_INSTANT_RATE_SYNC_TIME, "instant-rate-sync-time", 0},
137
138   {0, NULL, 0}
139 };
140
141 GST_DEFINE_MINI_OBJECT_TYPE (GstEvent, gst_event);
142
143 void
144 _priv_gst_event_initialize (void)
145 {
146   gint i;
147
148   _gst_event_type = gst_event_get_type ();
149
150   g_type_class_ref (gst_seek_flags_get_type ());
151   g_type_class_ref (gst_seek_type_get_type ());
152
153   for (i = 0; event_quarks[i].name; i++) {
154     event_quarks[i].quark = g_quark_from_static_string (event_quarks[i].name);
155   }
156 }
157
158 /**
159  * gst_event_type_get_name:
160  * @type: the event type
161  *
162  * Get a printable name for the given event type. Do not modify or free.
163  *
164  * Returns: a reference to the static name of the event.
165  */
166 const gchar *
167 gst_event_type_get_name (GstEventType type)
168 {
169   gint i;
170
171   for (i = 0; event_quarks[i].name; i++) {
172     if (type == event_quarks[i].type)
173       return event_quarks[i].name;
174   }
175   return "unknown";
176 }
177
178 /**
179  * gst_event_type_to_quark:
180  * @type: the event type
181  *
182  * Get the unique quark for the given event type.
183  *
184  * Returns: the quark associated with the event type
185  */
186 GQuark
187 gst_event_type_to_quark (GstEventType type)
188 {
189   gint i;
190
191   for (i = 0; event_quarks[i].name; i++) {
192     if (type == event_quarks[i].type)
193       return event_quarks[i].quark;
194   }
195   return 0;
196 }
197
198 /**
199  * gst_event_type_get_flags:
200  * @type: a #GstEventType
201  *
202  * Gets the #GstEventTypeFlags associated with @type.
203  *
204  * Returns: a #GstEventTypeFlags.
205  */
206 GstEventTypeFlags
207 gst_event_type_get_flags (GstEventType type)
208 {
209   GstEventTypeFlags ret;
210
211   ret = type & ((1 << GST_EVENT_NUM_SHIFT) - 1);
212
213   return ret;
214 }
215
216 /**
217  * gst_event_type_to_sticky_ordering
218  * @type: a #GstEventType
219  *
220  * Converts the #GstEventType to an unsigned integer that
221  * represents the ordering of sticky events when re-sending them.
222  * A lower value represents a higher-priority event.
223  *
224  * Returns: an unsigned integer
225  * Since: 1.22
226  */
227 /* FIXME 2.0: Remove the sticky event order overrides once
228  * the event type numbers are fixed */
229 guint
230 gst_event_type_to_sticky_ordering (GstEventType type)
231 {
232   guint sticky_order = type;
233
234   /* Fix up the sticky event ordering for events where the
235    * type was chosen poorly */
236   if (type == GST_EVENT_INSTANT_RATE_CHANGE) {
237     sticky_order = GST_EVENT_SEGMENT + 1;
238   }
239
240   return sticky_order;
241 }
242
243 static void
244 _gst_event_free (GstEvent * event)
245 {
246   GstStructure *s;
247
248   g_return_if_fail (event != NULL);
249   g_return_if_fail (GST_IS_EVENT (event));
250
251   GST_CAT_LOG (GST_CAT_EVENT, "freeing event %p type %s", event,
252       GST_EVENT_TYPE_NAME (event));
253
254   s = GST_EVENT_STRUCTURE (event);
255
256   if (s) {
257     gst_structure_set_parent_refcount (s, NULL);
258     gst_structure_free (s);
259   }
260 #ifdef USE_POISONING
261   memset (event, 0xff, sizeof (GstEventImpl));
262 #endif
263
264   g_slice_free1 (sizeof (GstEventImpl), event);
265 }
266
267 static void gst_event_init (GstEventImpl * event, GstEventType type);
268
269 static GstEvent *
270 _gst_event_copy (GstEvent * event)
271 {
272   GstEventImpl *copy;
273   GstStructure *s;
274
275   copy = g_slice_new0 (GstEventImpl);
276
277   gst_event_init (copy, GST_EVENT_TYPE (event));
278
279   GST_EVENT_TIMESTAMP (copy) = GST_EVENT_TIMESTAMP (event);
280   GST_EVENT_SEQNUM (copy) = GST_EVENT_SEQNUM (event);
281
282   s = GST_EVENT_STRUCTURE (event);
283   if (s) {
284     GST_EVENT_STRUCTURE (copy) = gst_structure_copy (s);
285     gst_structure_set_parent_refcount (GST_EVENT_STRUCTURE (copy),
286         &copy->event.mini_object.refcount);
287   } else {
288     GST_EVENT_STRUCTURE (copy) = NULL;
289   }
290
291   ((GstEventImpl *) copy)->running_time_offset =
292       ((GstEventImpl *) event)->running_time_offset;
293
294   return GST_EVENT_CAST (copy);
295 }
296
297 static void
298 gst_event_init (GstEventImpl * event, GstEventType type)
299 {
300   gst_mini_object_init (GST_MINI_OBJECT_CAST (event), 0, _gst_event_type,
301       (GstMiniObjectCopyFunction) _gst_event_copy, NULL,
302       (GstMiniObjectFreeFunction) _gst_event_free);
303
304   GST_EVENT_TYPE (event) = type;
305   GST_EVENT_TIMESTAMP (event) = GST_CLOCK_TIME_NONE;
306   GST_EVENT_SEQNUM (event) = gst_util_seqnum_next ();
307   event->running_time_offset = 0;
308 }
309
310
311 /**
312  * gst_event_new_custom:
313  * @type: The type of the new event
314  * @structure: (transfer full): the structure for the event. The event will
315  *     take ownership of the structure.
316  *
317  * Create a new custom-typed event. This can be used for anything not
318  * handled by other event-specific functions to pass an event to another
319  * element.
320  *
321  * Make sure to allocate an event type with the #GST_EVENT_MAKE_TYPE macro,
322  * assigning a free number and filling in the correct direction and
323  * serialization flags.
324  *
325  * New custom events can also be created by subclassing the event type if
326  * needed.
327  *
328  * Returns: (transfer full): the new custom event.
329  */
330 GstEvent *
331 gst_event_new_custom (GstEventType type, GstStructure * structure)
332 {
333   GstEventImpl *event;
334
335   event = g_slice_new0 (GstEventImpl);
336
337   GST_CAT_DEBUG (GST_CAT_EVENT, "creating new event %p %s %d", event,
338       gst_event_type_get_name (type), type);
339
340   if (structure) {
341     /* structure must not have a parent */
342     if (!gst_structure_set_parent_refcount (structure,
343             &event->event.mini_object.refcount))
344       goto had_parent;
345
346   }
347   gst_event_init (event, type);
348
349   GST_EVENT_STRUCTURE (event) = structure;
350
351   return GST_EVENT_CAST (event);
352
353   /* ERRORS */
354 had_parent:
355   {
356     g_slice_free1 (sizeof (GstEventImpl), event);
357     g_warning ("structure is already owned by another object");
358     return NULL;
359   }
360 }
361
362 /**
363  * gst_event_get_structure:
364  * @event: The #GstEvent.
365  *
366  * Access the structure of the event.
367  *
368  * Returns: (transfer none) (nullable): The structure of the event. The
369  * structure is still owned by the event, which means that you should not free
370  * it and that the pointer becomes invalid when you free the event.
371  *
372  * MT safe.
373  */
374 const GstStructure *
375 gst_event_get_structure (GstEvent * event)
376 {
377   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
378
379   return GST_EVENT_STRUCTURE (event);
380 }
381
382 /**
383  * gst_event_writable_structure:
384  * @event: A writable #GstEvent.
385  *
386  * Get a writable version of the structure.
387  *
388  * Returns: (transfer none): The structure of the event. The structure
389  * is still owned by the event, which means that you should not free
390  * it and that the pointer becomes invalid when you free the event.
391  * This function ensures that @event is writable, and if so, will
392  * never return %NULL.
393  *
394  * MT safe.
395  */
396 GstStructure *
397 gst_event_writable_structure (GstEvent * event)
398 {
399   GstStructure *structure;
400
401   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
402   g_return_val_if_fail (gst_event_is_writable (event), NULL);
403
404   structure = GST_EVENT_STRUCTURE (event);
405
406   if (structure == NULL) {
407     structure =
408         gst_structure_new_id_empty (gst_event_type_to_quark (GST_EVENT_TYPE
409             (event)));
410     gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
411     GST_EVENT_STRUCTURE (event) = structure;
412   }
413   return structure;
414 }
415
416 /**
417  * gst_event_has_name:
418  * @event: The #GstEvent.
419  * @name: name to check
420  *
421  * Checks if @event has the given @name. This function is usually used to
422  * check the name of a custom event.
423  *
424  * Returns: %TRUE if @name matches the name of the event structure.
425  */
426 gboolean
427 gst_event_has_name (GstEvent * event, const gchar * name)
428 {
429   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
430
431   if (GST_EVENT_STRUCTURE (event) == NULL)
432     return FALSE;
433
434   return gst_structure_has_name (GST_EVENT_STRUCTURE (event), name);
435 }
436
437 /**
438  * gst_event_has_name_id:
439  * @event: The #GstEvent.
440  * @name: name to check as a GQuark
441  *
442  * Checks if @event has the given @name. This function is usually used to
443  * check the name of a custom event.
444  *
445  * Returns: %TRUE if @name matches the name of the event structure.
446  *
447  * Since: 1.18
448  */
449 gboolean
450 gst_event_has_name_id (GstEvent * event, GQuark name)
451 {
452   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
453
454   if (GST_EVENT_STRUCTURE (event) == NULL)
455     return FALSE;
456
457   return (GST_EVENT_STRUCTURE (event)->name == name);
458 }
459
460 /**
461  * gst_event_get_seqnum:
462  * @event: A #GstEvent.
463  *
464  * Retrieve the sequence number of a event.
465  *
466  * Events have ever-incrementing sequence numbers, which may also be set
467  * explicitly via gst_event_set_seqnum(). Sequence numbers are typically used to
468  * indicate that a event corresponds to some other set of events or messages,
469  * for example an EOS event corresponding to a SEEK event. It is considered good
470  * practice to make this correspondence when possible, though it is not
471  * required.
472  *
473  * Note that events and messages share the same sequence number incrementor;
474  * two events or messages will never have the same sequence number unless
475  * that correspondence was made explicitly.
476  *
477  * Returns: The event's sequence number.
478  *
479  * MT safe.
480  */
481 guint32
482 gst_event_get_seqnum (GstEvent * event)
483 {
484   g_return_val_if_fail (GST_IS_EVENT (event), -1);
485
486   return GST_EVENT_SEQNUM (event);
487 }
488
489 /**
490  * gst_event_set_seqnum:
491  * @event: A #GstEvent.
492  * @seqnum: A sequence number.
493  *
494  * Set the sequence number of a event.
495  *
496  * This function might be called by the creator of a event to indicate that the
497  * event relates to other events or messages. See gst_event_get_seqnum() for
498  * more information.
499  *
500  * MT safe.
501  */
502 void
503 gst_event_set_seqnum (GstEvent * event, guint32 seqnum)
504 {
505   g_return_if_fail (GST_IS_EVENT (event));
506   g_return_if_fail (seqnum != GST_SEQNUM_INVALID);
507   g_return_if_fail (gst_event_is_writable (event));
508
509   GST_EVENT_SEQNUM (event) = seqnum;
510 }
511
512 /**
513  * gst_event_get_running_time_offset:
514  * @event: A #GstEvent.
515  *
516  * Retrieve the accumulated running time offset of the event.
517  *
518  * Events passing through #GstPads that have a running time
519  * offset set via gst_pad_set_offset() will get their offset
520  * adjusted according to the pad's offset.
521  *
522  * If the event contains any information that related to the
523  * running time, this information will need to be updated
524  * before usage with this offset.
525  *
526  * Returns: The event's running time offset
527  *
528  * MT safe.
529  *
530  * Since: 1.4
531  */
532 gint64
533 gst_event_get_running_time_offset (GstEvent * event)
534 {
535   g_return_val_if_fail (GST_IS_EVENT (event), 0);
536
537   return ((GstEventImpl *) event)->running_time_offset;
538 }
539
540 /**
541  * gst_event_set_running_time_offset:
542  * @event: A #GstEvent.
543  * @offset: A the new running time offset
544  *
545  * Set the running time offset of a event. See
546  * gst_event_get_running_time_offset() for more information.
547  *
548  * MT safe.
549  *
550  * Since: 1.4
551  */
552 void
553 gst_event_set_running_time_offset (GstEvent * event, gint64 offset)
554 {
555   g_return_if_fail (GST_IS_EVENT (event));
556   g_return_if_fail (gst_event_is_writable (event));
557
558   ((GstEventImpl *) event)->running_time_offset = offset;
559 }
560
561 /**
562  * gst_event_new_flush_start:
563  *
564  * Allocate a new flush start event. The flush start event can be sent
565  * upstream and downstream and travels out-of-bounds with the dataflow.
566  *
567  * It marks pads as being flushing and will make them return
568  * #GST_FLOW_FLUSHING when used for data flow with gst_pad_push(),
569  * gst_pad_chain(), gst_pad_get_range() and gst_pad_pull_range().
570  * Any event (except a #GST_EVENT_FLUSH_STOP) received
571  * on a flushing pad will return %FALSE immediately.
572  *
573  * Elements should unlock any blocking functions and exit their streaming
574  * functions as fast as possible when this event is received.
575  *
576  * This event is typically generated after a seek to flush out all queued data
577  * in the pipeline so that the new media is played as soon as possible.
578  *
579  * Returns: (transfer full): a new flush start event.
580  */
581 GstEvent *
582 gst_event_new_flush_start (void)
583 {
584   return gst_event_new_custom (GST_EVENT_FLUSH_START, NULL);
585 }
586
587 /**
588  * gst_event_new_flush_stop:
589  * @reset_time: if time should be reset
590  *
591  * Allocate a new flush stop event. The flush stop event can be sent
592  * upstream and downstream and travels serialized with the dataflow.
593  * It is typically sent after sending a FLUSH_START event to make the
594  * pads accept data again.
595  *
596  * Elements can process this event synchronized with the dataflow since
597  * the preceding FLUSH_START event stopped the dataflow.
598  *
599  * This event is typically generated to complete a seek and to resume
600  * dataflow.
601  *
602  * Returns: (transfer full): a new flush stop event.
603  */
604 GstEvent *
605 gst_event_new_flush_stop (gboolean reset_time)
606 {
607   GstEvent *event;
608
609   GST_CAT_INFO (GST_CAT_EVENT, "creating flush stop %d", reset_time);
610
611   event = gst_event_new_custom (GST_EVENT_FLUSH_STOP,
612       gst_structure_new_id (GST_QUARK (EVENT_FLUSH_STOP),
613           GST_QUARK (RESET_TIME), G_TYPE_BOOLEAN, reset_time, NULL));
614
615   return event;
616 }
617
618 /**
619  * gst_event_parse_flush_stop:
620  * @event: The event to parse
621  * @reset_time: (out) (optional): if time should be reset
622  *
623  * Parse the FLUSH_STOP event and retrieve the @reset_time member.
624  */
625 void
626 gst_event_parse_flush_stop (GstEvent * event, gboolean * reset_time)
627 {
628   GstStructure *structure;
629
630   g_return_if_fail (GST_IS_EVENT (event));
631   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP);
632
633   structure = GST_EVENT_STRUCTURE (event);
634   if (G_LIKELY (reset_time))
635     *reset_time =
636         g_value_get_boolean (gst_structure_id_get_value (structure,
637             GST_QUARK (RESET_TIME)));
638 }
639
640 /**
641  * gst_event_new_select_streams:
642  * @streams: (element-type utf8) (transfer none): the list of streams to
643  * activate
644  *
645  * Allocate a new select-streams event.
646  *
647  * The select-streams event requests the specified @streams to be activated.
648  *
649  * The list of @streams corresponds to the "Stream ID" of each stream to be
650  * activated. Those ID can be obtained via the #GstStream objects present
651  * in #GST_EVENT_STREAM_START, #GST_EVENT_STREAM_COLLECTION or
652  * #GST_MESSAGE_STREAM_COLLECTION.
653  *
654  * Note: The list of @streams can not be empty.
655  *
656  * Returns: (transfer full): a new select-streams event.
657  *
658  * Since: 1.10
659  */
660 GstEvent *
661 gst_event_new_select_streams (GList * streams)
662 {
663   GstEvent *event;
664   GValue val = G_VALUE_INIT;
665   GstStructure *struc;
666   GList *tmpl;
667
668   g_return_val_if_fail (streams != NULL, NULL);
669
670   GST_CAT_INFO (GST_CAT_EVENT, "Creating new select-streams event");
671   struc = gst_structure_new_id_empty (GST_QUARK (EVENT_SELECT_STREAMS));
672   g_value_init (&val, GST_TYPE_LIST);
673   /* Fill struc with streams */
674   for (tmpl = streams; tmpl; tmpl = tmpl->next) {
675     GValue strval = G_VALUE_INIT;
676     const gchar *str = (const gchar *) tmpl->data;
677     g_value_init (&strval, G_TYPE_STRING);
678     g_value_set_string (&strval, str);
679     gst_value_list_append_and_take_value (&val, &strval);
680   }
681   gst_structure_id_take_value (struc, GST_QUARK (STREAMS), &val);
682   event = gst_event_new_custom (GST_EVENT_SELECT_STREAMS, struc);
683
684   return event;
685 }
686
687 /**
688  * gst_event_parse_select_streams:
689  * @event: The event to parse
690  * @streams: (out) (optional) (element-type utf8) (transfer full): the streams
691  *
692  * Parse the SELECT_STREAMS event and retrieve the contained streams.
693  *
694  * Since: 1.10
695  */
696 void
697 gst_event_parse_select_streams (GstEvent * event, GList ** streams)
698 {
699   GstStructure *structure;
700   GList *res = NULL;
701
702   g_return_if_fail (GST_IS_EVENT (event));
703   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS);
704
705   structure = GST_EVENT_STRUCTURE (event);
706   if (G_LIKELY (streams)) {
707     const GValue *vlist =
708         gst_structure_id_get_value (structure, GST_QUARK (STREAMS));
709     guint i, sz = gst_value_list_get_size (vlist);
710     for (i = 0; i < sz; i++) {
711       const GValue *strv = gst_value_list_get_value (vlist, i);
712       res = g_list_append (res, g_value_dup_string (strv));
713     }
714     *streams = res;
715   }
716 }
717
718
719 /**
720  * gst_event_new_stream_group_done:
721  * @group_id: the group id of the stream group which is ending
722  *
723  * Create a new Stream Group Done event. The stream-group-done event can
724  * only travel downstream synchronized with the buffer flow. Elements
725  * that receive the event on a pad should handle it mostly like EOS,
726  * and emit any data or pending buffers that would depend on more data
727  * arriving and unblock, since there won't be any more data.
728  *
729  * This event is followed by EOS at some point in the future, and is
730  * generally used when switching pads - to unblock downstream so that
731  * new pads can be exposed before sending EOS on the existing pads.
732  *
733  * Returns: (transfer full): the new stream-group-done event.
734  *
735  * Since: 1.10
736  */
737 GstEvent *
738 gst_event_new_stream_group_done (guint group_id)
739 {
740   GstStructure *s;
741
742   g_return_val_if_fail (group_id != GST_GROUP_ID_INVALID, NULL);
743
744   s = gst_structure_new_id (GST_QUARK (EVENT_STREAM_GROUP_DONE),
745       GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id, NULL);
746
747   return gst_event_new_custom (GST_EVENT_STREAM_GROUP_DONE, s);
748 }
749
750 /**
751  * gst_event_parse_stream_group_done:
752  * @event: a stream-group-done event.
753  * @group_id: (out) (optional): address of variable to store the group id into
754  *
755  * Parse a stream-group-done @event and store the result in the given
756  * @group_id location.
757  *
758  * Since: 1.10
759  */
760 void
761 gst_event_parse_stream_group_done (GstEvent * event, guint * group_id)
762 {
763   g_return_if_fail (event != NULL);
764   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_GROUP_DONE);
765
766   if (group_id) {
767     gst_structure_id_get (GST_EVENT_STRUCTURE (event),
768         GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id, NULL);
769   }
770 }
771
772 /**
773  * gst_event_new_eos:
774  *
775  * Create a new EOS event. The eos event can only travel downstream
776  * synchronized with the buffer flow. Elements that receive the EOS
777  * event on a pad can return #GST_FLOW_EOS as a #GstFlowReturn
778  * when data after the EOS event arrives.
779  *
780  * The EOS event will travel down to the sink elements in the pipeline
781  * which will then post the #GST_MESSAGE_EOS on the bus after they have
782  * finished playing any buffered data.
783  *
784  * When all sinks have posted an EOS message, an EOS message is
785  * forwarded to the application.
786  *
787  * The EOS event itself will not cause any state transitions of the pipeline.
788  *
789  * Returns: (transfer full): the new EOS event.
790  */
791 GstEvent *
792 gst_event_new_eos (void)
793 {
794   return gst_event_new_custom (GST_EVENT_EOS, NULL);
795 }
796
797 /**
798  * gst_event_new_gap:
799  * @timestamp: the start time (pts) of the gap
800  * @duration: the duration of the gap
801  *
802  * Create a new GAP event. A gap event can be thought of as conceptually
803  * equivalent to a buffer to signal that there is no data for a certain
804  * amount of time. This is useful to signal a gap to downstream elements
805  * which may wait for data, such as muxers or mixers or overlays, especially
806  * for sparse streams such as subtitle streams.
807  *
808  * Returns: (transfer full): the new GAP event.
809  */
810 GstEvent *
811 gst_event_new_gap (GstClockTime timestamp, GstClockTime duration)
812 {
813   GstEvent *event;
814
815   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
816
817   GST_CAT_TRACE (GST_CAT_EVENT, "creating gap %" GST_TIME_FORMAT " - "
818       "%" GST_TIME_FORMAT " (duration: %" GST_TIME_FORMAT ")",
819       GST_TIME_ARGS (timestamp), GST_TIME_ARGS (timestamp + duration),
820       GST_TIME_ARGS (duration));
821
822   event = gst_event_new_custom (GST_EVENT_GAP,
823       gst_structure_new_id (GST_QUARK (EVENT_GAP),
824           GST_QUARK (TIMESTAMP), GST_TYPE_CLOCK_TIME, timestamp,
825           GST_QUARK (DURATION), GST_TYPE_CLOCK_TIME, duration, NULL));
826
827   return event;
828 }
829
830 /**
831  * gst_event_parse_gap:
832  * @event: a #GstEvent of type #GST_EVENT_GAP
833  * @timestamp: (out) (optional): location where to store the
834  *     start time (pts) of the gap, or %NULL
835  * @duration: (out) (optional): location where to store the duration of
836  *     the gap, or %NULL
837  *
838  * Extract timestamp and duration from a new GAP event.
839  */
840 void
841 gst_event_parse_gap (GstEvent * event, GstClockTime * timestamp,
842     GstClockTime * duration)
843 {
844   GstStructure *structure;
845
846   g_return_if_fail (GST_IS_EVENT (event));
847   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
848
849   structure = GST_EVENT_STRUCTURE (event);
850   gst_structure_id_get (structure,
851       GST_QUARK (TIMESTAMP), GST_TYPE_CLOCK_TIME, timestamp,
852       GST_QUARK (DURATION), GST_TYPE_CLOCK_TIME, duration, NULL);
853 }
854
855 /**
856  * gst_event_set_gap_flags:
857  * @event: a #GstEvent of type #GST_EVENT_GAP
858  * @flags: a #GstGapFlags
859  *
860  * Sets @flags on @event to give additional information about the reason for
861  * the #GST_EVENT_GAP.
862  *
863  * Since: 1.20
864  */
865 void
866 gst_event_set_gap_flags (GstEvent * event, GstGapFlags flags)
867 {
868   g_return_if_fail (event != NULL);
869   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
870   g_return_if_fail (gst_event_is_writable (event));
871
872   gst_structure_id_set (GST_EVENT_STRUCTURE (event),
873       GST_QUARK (GAP_FLAGS), GST_TYPE_GAP_FLAGS, flags, NULL);
874 }
875
876 /**
877  * gst_event_parse_gap_flags:
878  * @event: a #GstEvent of type #GST_EVENT_GAP
879  * @flags: (out) (optional): a #GstGapFlags or %NULL
880  *
881  * Retrieve the gap flags that may have been set on a gap event with
882  * gst_event_set_gap_flags().
883  *
884  * Since: 1.20
885  */
886 void
887 gst_event_parse_gap_flags (GstEvent * event, GstGapFlags * flags)
888 {
889   g_return_if_fail (event != NULL);
890   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
891
892   /* Initialize to 0 for GAP events that don't have the field set */
893   if (flags)
894     *flags = 0;
895
896   gst_structure_id_get (GST_EVENT_STRUCTURE (event),
897       GST_QUARK (GAP_FLAGS), GST_TYPE_GAP_FLAGS, flags, NULL);
898 }
899
900 /**
901  * gst_event_new_caps:
902  * @caps: (transfer none): a #GstCaps
903  *
904  * Create a new CAPS event for @caps. The caps event can only travel downstream
905  * synchronized with the buffer flow and contains the format of the buffers
906  * that will follow after the event.
907  *
908  * Returns: (transfer full): the new CAPS event.
909  */
910 GstEvent *
911 gst_event_new_caps (GstCaps * caps)
912 {
913   GstEvent *event;
914
915   g_return_val_if_fail (caps != NULL, NULL);
916   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
917
918   GST_CAT_INFO (GST_CAT_EVENT, "creating caps event %" GST_PTR_FORMAT, caps);
919
920   event = gst_event_new_custom (GST_EVENT_CAPS,
921       gst_structure_new_id (GST_QUARK (EVENT_CAPS),
922           GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL));
923
924   return event;
925 }
926
927 /**
928  * gst_event_parse_caps:
929  * @event: The event to parse
930  * @caps: (out) (optional) (transfer none): A pointer to the caps
931  *
932  * Get the caps from @event. The caps remains valid as long as @event remains
933  * valid.
934  */
935 void
936 gst_event_parse_caps (GstEvent * event, GstCaps ** caps)
937 {
938   GstStructure *structure;
939
940   g_return_if_fail (GST_IS_EVENT (event));
941   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
942
943   structure = GST_EVENT_STRUCTURE (event);
944   if (G_LIKELY (caps))
945     *caps =
946         g_value_get_boxed (gst_structure_id_get_value (structure,
947             GST_QUARK (CAPS)));
948 }
949
950 /**
951  * gst_event_new_segment:
952  * @segment: (transfer none): a #GstSegment
953  *
954  * Create a new SEGMENT event for @segment. The segment event can only travel
955  * downstream synchronized with the buffer flow and contains timing information
956  * and playback properties for the buffers that will follow.
957  *
958  * The segment event marks the range of buffers to be processed. All
959  * data not within the segment range is not to be processed. This can be
960  * used intelligently by plugins to apply more efficient methods of skipping
961  * unneeded data. The valid range is expressed with the @start and @stop
962  * values.
963  *
964  * The time value of the segment is used in conjunction with the start
965  * value to convert the buffer timestamps into the stream time. This is
966  * usually done in sinks to report the current stream_time.
967  * @time represents the stream_time of a buffer carrying a timestamp of
968  * @start. @time cannot be -1.
969  *
970  * @start cannot be -1, @stop can be -1. If there
971  * is a valid @stop given, it must be greater or equal the @start, including
972  * when the indicated playback @rate is < 0.
973  *
974  * The @applied_rate value provides information about any rate adjustment that
975  * has already been made to the timestamps and content on the buffers of the
976  * stream. (@rate * @applied_rate) should always equal the rate that has been
977  * requested for playback. For example, if an element has an input segment
978  * with intended playback @rate of 2.0 and applied_rate of 1.0, it can adjust
979  * incoming timestamps and buffer content by half and output a segment event
980  * with @rate of 1.0 and @applied_rate of 2.0
981  *
982  * After a segment event, the buffer stream time is calculated with:
983  *
984  *   time + (TIMESTAMP(buf) - start) * ABS (rate * applied_rate)
985  *
986  * Returns: (transfer full): the new SEGMENT event.
987  */
988 GstEvent *
989 gst_event_new_segment (const GstSegment * segment)
990 {
991   GstEvent *event;
992
993   g_return_val_if_fail (segment != NULL, NULL);
994   g_return_val_if_fail (segment->rate != 0.0, NULL);
995   g_return_val_if_fail (segment->applied_rate != 0.0, NULL);
996   g_return_val_if_fail (segment->format != GST_FORMAT_UNDEFINED, NULL);
997
998   GST_CAT_INFO (GST_CAT_EVENT, "creating segment event %" GST_SEGMENT_FORMAT,
999       segment);
1000
1001   event = gst_event_new_custom (GST_EVENT_SEGMENT,
1002       gst_structure_new_id (GST_QUARK (EVENT_SEGMENT),
1003           GST_QUARK (SEGMENT), GST_TYPE_SEGMENT, segment, NULL));
1004
1005   return event;
1006 }
1007
1008 /**
1009  * gst_event_parse_segment:
1010  * @event: The event to parse
1011  * @segment: (out) (optional) (transfer none): a pointer to a #GstSegment
1012  *
1013  * Parses a segment @event and stores the result in the given @segment location.
1014  * @segment remains valid only until the @event is freed. Don't modify the segment
1015  * and make a copy if you want to modify it or store it for later use.
1016  */
1017 void
1018 gst_event_parse_segment (GstEvent * event, const GstSegment ** segment)
1019 {
1020   GstStructure *structure;
1021
1022   g_return_if_fail (GST_IS_EVENT (event));
1023   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
1024
1025   if (segment) {
1026     structure = GST_EVENT_STRUCTURE (event);
1027     *segment = g_value_get_boxed (gst_structure_id_get_value (structure,
1028             GST_QUARK (SEGMENT)));
1029   }
1030 }
1031
1032 /**
1033  * gst_event_copy_segment:
1034  * @event: The event to parse
1035  * @segment: a pointer to a #GstSegment
1036  *
1037  * Parses a segment @event and copies the #GstSegment into the location
1038  * given by @segment.
1039  */
1040 void
1041 gst_event_copy_segment (GstEvent * event, GstSegment * segment)
1042 {
1043   const GstSegment *src;
1044
1045   g_return_if_fail (GST_IS_EVENT (event));
1046   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
1047
1048   if (segment) {
1049     gst_event_parse_segment (event, &src);
1050     gst_segment_copy_into (src, segment);
1051   }
1052 }
1053
1054 /**
1055  * gst_event_new_tag:
1056  * @taglist: (transfer full): metadata list. The event will take ownership
1057  *     of the taglist.
1058  *
1059  * Generates a metadata tag event from the given @taglist.
1060  *
1061  * The scope of the taglist specifies if the taglist applies to the
1062  * complete medium or only to this specific stream. As the tag event
1063  * is a sticky event, elements should merge tags received from
1064  * upstream with a given scope with their own tags with the same
1065  * scope and create a new tag event from it.
1066  *
1067  * Returns: (transfer full): a new #GstEvent
1068  */
1069 GstEvent *
1070 gst_event_new_tag (GstTagList * taglist)
1071 {
1072   GstStructure *s;
1073   GValue val = G_VALUE_INIT;
1074   const gchar *names[] = { "GstTagList-stream", "GstTagList-global" };
1075
1076   g_return_val_if_fail (taglist != NULL, NULL);
1077
1078   s = gst_structure_new_empty (names[gst_tag_list_get_scope (taglist)]);
1079   g_value_init (&val, GST_TYPE_TAG_LIST);
1080   g_value_take_boxed (&val, taglist);
1081   gst_structure_id_take_value (s, GST_QUARK (TAGLIST), &val);
1082   return gst_event_new_custom (GST_EVENT_TAG, s);
1083 }
1084
1085 /**
1086  * gst_event_parse_tag:
1087  * @event: a tag event
1088  * @taglist: (out) (optional) (transfer none): pointer to metadata list
1089  *
1090  * Parses a tag @event and stores the results in the given @taglist location.
1091  * No reference to the taglist will be returned, it remains valid only until
1092  * the @event is freed. Don't modify or free the taglist, make a copy if you
1093  * want to modify it or store it for later use.
1094  */
1095 void
1096 gst_event_parse_tag (GstEvent * event, GstTagList ** taglist)
1097 {
1098   const GValue *val;
1099
1100   g_return_if_fail (GST_IS_EVENT (event));
1101   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG);
1102
1103   val = gst_structure_id_get_value (GST_EVENT_STRUCTURE (event),
1104       GST_QUARK (TAGLIST));
1105
1106   if (taglist)
1107     *taglist = (GstTagList *) g_value_get_boxed (val);
1108 }
1109
1110 /* buffersize event */
1111 /**
1112  * gst_event_new_buffer_size:
1113  * @format: buffer format
1114  * @minsize: minimum buffer size
1115  * @maxsize: maximum buffer size
1116  * @async: thread behavior
1117  *
1118  * Create a new buffersize event. The event is sent downstream and notifies
1119  * elements that they should provide a buffer of the specified dimensions.
1120  *
1121  * When the @async flag is set, a thread boundary is preferred.
1122  *
1123  * Returns: (transfer full): a new #GstEvent
1124  */
1125 GstEvent *
1126 gst_event_new_buffer_size (GstFormat format, gint64 minsize,
1127     gint64 maxsize, gboolean async)
1128 {
1129   GstEvent *event;
1130   GstStructure *structure;
1131
1132   GST_CAT_INFO (GST_CAT_EVENT,
1133       "creating buffersize format %s, minsize %" G_GINT64_FORMAT
1134       ", maxsize %" G_GINT64_FORMAT ", async %d", gst_format_get_name (format),
1135       minsize, maxsize, async);
1136
1137   structure = gst_structure_new_id (GST_QUARK (EVENT_BUFFER_SIZE),
1138       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1139       GST_QUARK (MINSIZE), G_TYPE_INT64, minsize,
1140       GST_QUARK (MAXSIZE), G_TYPE_INT64, maxsize,
1141       GST_QUARK (ASYNC), G_TYPE_BOOLEAN, async, NULL);
1142   event = gst_event_new_custom (GST_EVENT_BUFFERSIZE, structure);
1143
1144   return event;
1145 }
1146
1147 /**
1148  * gst_event_parse_buffer_size:
1149  * @event: The event to query
1150  * @format: (out) (optional): A pointer to store the format in
1151  * @minsize: (out) (optional): A pointer to store the minsize in
1152  * @maxsize: (out) (optional): A pointer to store the maxsize in
1153  * @async: (out) (optional): A pointer to store the async-flag in
1154  *
1155  * Get the format, minsize, maxsize and async-flag in the buffersize event.
1156  */
1157 void
1158 gst_event_parse_buffer_size (GstEvent * event, GstFormat * format,
1159     gint64 * minsize, gint64 * maxsize, gboolean * async)
1160 {
1161   const GstStructure *structure;
1162
1163   g_return_if_fail (GST_IS_EVENT (event));
1164   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_BUFFERSIZE);
1165
1166   structure = GST_EVENT_STRUCTURE (event);
1167   if (format)
1168     *format = (GstFormat)
1169         g_value_get_enum (gst_structure_id_get_value (structure,
1170             GST_QUARK (FORMAT)));
1171   if (minsize)
1172     *minsize =
1173         g_value_get_int64 (gst_structure_id_get_value (structure,
1174             GST_QUARK (MINSIZE)));
1175   if (maxsize)
1176     *maxsize =
1177         g_value_get_int64 (gst_structure_id_get_value (structure,
1178             GST_QUARK (MAXSIZE)));
1179   if (async)
1180     *async =
1181         g_value_get_boolean (gst_structure_id_get_value (structure,
1182             GST_QUARK (ASYNC)));
1183 }
1184
1185 /**
1186  * gst_event_new_qos:
1187  * @type: the QoS type
1188  * @proportion: the proportion of the qos message
1189  * @diff: The time difference of the last Clock sync
1190  * @timestamp: The timestamp of the buffer
1191  *
1192  * Allocate a new qos event with the given values.
1193  * The QOS event is generated in an element that wants an upstream
1194  * element to either reduce or increase its rate because of
1195  * high/low CPU load or other resource usage such as network performance or
1196  * throttling. Typically sinks generate these events for each buffer
1197  * they receive.
1198  *
1199  * @type indicates the reason for the QoS event. #GST_QOS_TYPE_OVERFLOW is
1200  * used when a buffer arrived in time or when the sink cannot keep up with
1201  * the upstream datarate. #GST_QOS_TYPE_UNDERFLOW is when the sink is not
1202  * receiving buffers fast enough and thus has to drop late buffers.
1203  * #GST_QOS_TYPE_THROTTLE is used when the datarate is artificially limited
1204  * by the application, for example to reduce power consumption.
1205  *
1206  * @proportion indicates the real-time performance of the streaming in the
1207  * element that generated the QoS event (usually the sink). The value is
1208  * generally computed based on more long term statistics about the streams
1209  * timestamps compared to the clock.
1210  * A value < 1.0 indicates that the upstream element is producing data faster
1211  * than real-time. A value > 1.0 indicates that the upstream element is not
1212  * producing data fast enough. 1.0 is the ideal @proportion value. The
1213  * proportion value can safely be used to lower or increase the quality of
1214  * the element.
1215  *
1216  * @diff is the difference against the clock in running time of the last
1217  * buffer that caused the element to generate the QOS event. A negative value
1218  * means that the buffer with @timestamp arrived in time. A positive value
1219  * indicates how late the buffer with @timestamp was. When throttling is
1220  * enabled, @diff will be set to the requested throttling interval.
1221  *
1222  * @timestamp is the timestamp of the last buffer that cause the element
1223  * to generate the QOS event. It is expressed in running time and thus an ever
1224  * increasing value.
1225  *
1226  * The upstream element can use the @diff and @timestamp values to decide
1227  * whether to process more buffers. For positive @diff, all buffers with
1228  * timestamp <= @timestamp + @diff will certainly arrive late in the sink
1229  * as well. A (negative) @diff value so that @timestamp + @diff would yield a
1230  * result smaller than 0 is not allowed.
1231  *
1232  * The application can use general event probes to intercept the QoS
1233  * event and implement custom application specific QoS handling.
1234  *
1235  * Returns: (transfer full): a new QOS event.
1236  */
1237 GstEvent *
1238 gst_event_new_qos (GstQOSType type, gdouble proportion,
1239     GstClockTimeDiff diff, GstClockTime timestamp)
1240 {
1241   GstEvent *event;
1242   GstStructure *structure;
1243
1244   /* diff must be positive or timestamp + diff must be positive */
1245   g_return_val_if_fail (diff >= 0 || -diff <= timestamp, NULL);
1246
1247   GST_CAT_LOG (GST_CAT_EVENT,
1248       "creating qos type %d, proportion %lf, diff %" G_GINT64_FORMAT
1249       ", timestamp %" GST_TIME_FORMAT, type, proportion,
1250       diff, GST_TIME_ARGS (timestamp));
1251
1252   structure = gst_structure_new_id (GST_QUARK (EVENT_QOS),
1253       GST_QUARK (TYPE), GST_TYPE_QOS_TYPE, type,
1254       GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
1255       GST_QUARK (DIFF), G_TYPE_INT64, diff,
1256       GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp, NULL);
1257   event = gst_event_new_custom (GST_EVENT_QOS, structure);
1258
1259   return event;
1260 }
1261
1262 /**
1263  * gst_event_parse_qos:
1264  * @event: The event to query
1265  * @type: (out) (optional): A pointer to store the QoS type in
1266  * @proportion: (out) (optional): A pointer to store the proportion in
1267  * @diff: (out) (optional): A pointer to store the diff in
1268  * @timestamp: (out) (optional): A pointer to store the timestamp in
1269  *
1270  * Get the type, proportion, diff and timestamp in the qos event. See
1271  * gst_event_new_qos() for more information about the different QoS values.
1272  *
1273  * @timestamp will be adjusted for any pad offsets of pads it was passing through.
1274  */
1275 void
1276 gst_event_parse_qos (GstEvent * event, GstQOSType * type,
1277     gdouble * proportion, GstClockTimeDiff * diff, GstClockTime * timestamp)
1278 {
1279   const GstStructure *structure;
1280
1281   g_return_if_fail (GST_IS_EVENT (event));
1282   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
1283
1284   structure = GST_EVENT_STRUCTURE (event);
1285   if (type)
1286     *type = (GstQOSType)
1287         g_value_get_enum (gst_structure_id_get_value (structure,
1288             GST_QUARK (TYPE)));
1289   if (proportion)
1290     *proportion =
1291         g_value_get_double (gst_structure_id_get_value (structure,
1292             GST_QUARK (PROPORTION)));
1293   if (diff)
1294     *diff =
1295         g_value_get_int64 (gst_structure_id_get_value (structure,
1296             GST_QUARK (DIFF)));
1297   if (timestamp) {
1298     gint64 offset = gst_event_get_running_time_offset (event);
1299     GstClockTimeDiff diff_ =
1300         g_value_get_int64 (gst_structure_id_get_value (structure,
1301             GST_QUARK (DIFF)));
1302
1303     *timestamp =
1304         g_value_get_uint64 (gst_structure_id_get_value (structure,
1305             GST_QUARK (TIMESTAMP)));
1306     /* Catch underflows */
1307     if (*timestamp > -offset)
1308       *timestamp += offset;
1309     else
1310       *timestamp = 0;
1311
1312     /* Make sure that timestamp + diff is always >= 0. Because
1313      * of the running time offset this might not be true */
1314     if (diff_ < 0 && *timestamp < -diff_)
1315       *timestamp = (GstClockTime) - diff_;
1316   }
1317 }
1318
1319 /**
1320  * gst_event_new_seek:
1321  * @rate: The new playback rate
1322  * @format: The format of the seek values
1323  * @flags: The optional seek flags
1324  * @start_type: The type and flags for the new start position
1325  * @start: The value of the new start position
1326  * @stop_type: The type and flags for the new stop position
1327  * @stop: The value of the new stop position
1328  *
1329  * Allocate a new seek event with the given parameters.
1330  *
1331  * The seek event configures playback of the pipeline between @start to @stop
1332  * at the speed given in @rate, also called a playback segment.
1333  * The @start and @stop values are expressed in @format.
1334  *
1335  * A @rate of 1.0 means normal playback rate, 2.0 means double speed.
1336  * Negatives values means backwards playback. A value of 0.0 for the
1337  * rate is not allowed and should be accomplished instead by PAUSING the
1338  * pipeline.
1339  *
1340  * A pipeline has a default playback segment configured with a start
1341  * position of 0, a stop position of -1 and a rate of 1.0. The currently
1342  * configured playback segment can be queried with #GST_QUERY_SEGMENT.
1343  *
1344  * @start_type and @stop_type specify how to adjust the currently configured
1345  * start and stop fields in playback segment. Adjustments can be made relative
1346  * or absolute to the last configured values. A type of #GST_SEEK_TYPE_NONE
1347  * means that the position should not be updated.
1348  *
1349  * When the rate is positive and @start has been updated, playback will start
1350  * from the newly configured start position.
1351  *
1352  * For negative rates, playback will start from the newly configured stop
1353  * position (if any). If the stop position is updated, it must be different from
1354  * -1 (#GST_CLOCK_TIME_NONE) for negative rates.
1355  *
1356  * It is not possible to seek relative to the current playback position, to do
1357  * this, PAUSE the pipeline, query the current playback position with
1358  * #GST_QUERY_POSITION and update the playback segment current position with a
1359  * #GST_SEEK_TYPE_SET to the desired position.
1360  *
1361  * Returns: (transfer full): a new seek event.
1362  */
1363 GstEvent *
1364 gst_event_new_seek (gdouble rate, GstFormat format, GstSeekFlags flags,
1365     GstSeekType start_type, gint64 start, GstSeekType stop_type, gint64 stop)
1366 {
1367   GstEvent *event;
1368   GstStructure *structure;
1369
1370   g_return_val_if_fail (rate != 0.0, NULL);
1371   g_return_val_if_fail ((flags & GST_SEEK_FLAG_INSTANT_RATE_CHANGE) == 0
1372       || (start_type == GST_SEEK_TYPE_NONE
1373           && stop_type == GST_SEEK_TYPE_NONE
1374           && (flags & GST_SEEK_FLAG_FLUSH) == 0), NULL);
1375
1376   /* SNAP flags only make sense in combination with the KEYUNIT flag. Warn
1377    * and unset the SNAP flags if they're set without the KEYUNIT flag */
1378   if (!(flags & GST_SEEK_FLAG_KEY_UNIT) &&
1379       (flags & (GST_SEEK_FLAG_SNAP_BEFORE | GST_SEEK_FLAG_SNAP_AFTER |
1380               GST_SEEK_FLAG_SNAP_NEAREST))) {
1381     g_warning ("SNAP seeks only work in combination with the KEY_UNIT "
1382         "flag, ignoring SNAP flags");
1383     flags &=
1384         ~(GST_SEEK_FLAG_SNAP_BEFORE | GST_SEEK_FLAG_SNAP_AFTER |
1385         GST_SEEK_FLAG_SNAP_NEAREST);
1386   }
1387
1388   if (format == GST_FORMAT_TIME) {
1389     GST_CAT_INFO (GST_CAT_EVENT,
1390         "creating seek rate %lf, format TIME, flags %d, "
1391         "start_type %d, start %" GST_TIME_FORMAT ", "
1392         "stop_type %d, stop %" GST_TIME_FORMAT,
1393         rate, flags, start_type, GST_TIME_ARGS (start),
1394         stop_type, GST_TIME_ARGS (stop));
1395   } else {
1396     GST_CAT_INFO (GST_CAT_EVENT,
1397         "creating seek rate %lf, format %s, flags %d, "
1398         "start_type %d, start %" G_GINT64_FORMAT ", "
1399         "stop_type %d, stop %" G_GINT64_FORMAT,
1400         rate, gst_format_get_name (format), flags, start_type, start, stop_type,
1401         stop);
1402   }
1403
1404   structure = gst_structure_new_id (GST_QUARK (EVENT_SEEK),
1405       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1406       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1407       GST_QUARK (FLAGS), GST_TYPE_SEEK_FLAGS, flags,
1408       GST_QUARK (CUR_TYPE), GST_TYPE_SEEK_TYPE, start_type,
1409       GST_QUARK (CUR), G_TYPE_INT64, start,
1410       GST_QUARK (STOP_TYPE), GST_TYPE_SEEK_TYPE, stop_type,
1411       GST_QUARK (STOP), G_TYPE_INT64, stop,
1412       GST_QUARK (TRICKMODE_INTERVAL), GST_TYPE_CLOCK_TIME, (GstClockTime) 0,
1413       NULL);
1414   event = gst_event_new_custom (GST_EVENT_SEEK, structure);
1415
1416   return event;
1417 }
1418
1419 /**
1420  * gst_event_parse_seek:
1421  * @event: a seek event
1422  * @rate: (out) (optional): result location for the rate
1423  * @format: (out) (optional): result location for the stream format
1424  * @flags:  (out) (optional): result location for the #GstSeekFlags
1425  * @start_type: (out) (optional): result location for the #GstSeekType of the start position
1426  * @start: (out) (optional): result location for the start position expressed in @format
1427  * @stop_type:  (out) (optional): result location for the #GstSeekType of the stop position
1428  * @stop: (out) (optional): result location for the stop position expressed in @format
1429  *
1430  * Parses a seek @event and stores the results in the given result locations.
1431  */
1432 void
1433 gst_event_parse_seek (GstEvent * event, gdouble * rate,
1434     GstFormat * format, GstSeekFlags * flags, GstSeekType * start_type,
1435     gint64 * start, GstSeekType * stop_type, gint64 * stop)
1436 {
1437   const GstStructure *structure;
1438
1439   g_return_if_fail (GST_IS_EVENT (event));
1440   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
1441
1442   structure = GST_EVENT_STRUCTURE (event);
1443   if (rate)
1444     *rate =
1445         g_value_get_double (gst_structure_id_get_value (structure,
1446             GST_QUARK (RATE)));
1447   if (format)
1448     *format = (GstFormat)
1449         g_value_get_enum (gst_structure_id_get_value (structure,
1450             GST_QUARK (FORMAT)));
1451   if (flags)
1452     *flags = (GstSeekFlags)
1453         g_value_get_flags (gst_structure_id_get_value (structure,
1454             GST_QUARK (FLAGS)));
1455   if (start_type)
1456     *start_type = (GstSeekType)
1457         g_value_get_enum (gst_structure_id_get_value (structure,
1458             GST_QUARK (CUR_TYPE)));
1459   if (start)
1460     *start =
1461         g_value_get_int64 (gst_structure_id_get_value (structure,
1462             GST_QUARK (CUR)));
1463   if (stop_type)
1464     *stop_type = (GstSeekType)
1465         g_value_get_enum (gst_structure_id_get_value (structure,
1466             GST_QUARK (STOP_TYPE)));
1467   if (stop)
1468     *stop =
1469         g_value_get_int64 (gst_structure_id_get_value (structure,
1470             GST_QUARK (STOP)));
1471 }
1472
1473 /**
1474  * gst_event_set_seek_trickmode_interval:
1475  *
1476  * Sets a trickmode interval on a (writable) seek event. Elements
1477  * that support TRICKMODE_KEY_UNITS seeks SHOULD use this as the minimal
1478  * interval between each frame they may output.
1479  *
1480  * Since: 1.16
1481  */
1482 void
1483 gst_event_set_seek_trickmode_interval (GstEvent * event, GstClockTime interval)
1484 {
1485   g_return_if_fail (event != NULL);
1486   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
1487   g_return_if_fail (gst_event_is_writable (event));
1488   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (interval));
1489
1490   gst_structure_id_set (GST_EVENT_STRUCTURE (event),
1491       GST_QUARK (TRICKMODE_INTERVAL), GST_TYPE_CLOCK_TIME, interval, NULL);
1492 }
1493
1494 /**
1495  * gst_event_parse_seek_trickmode_interval:
1496  * @interval: (out) (optional): interval
1497  *
1498  * Retrieve the trickmode interval that may have been set on a
1499  * seek event with gst_event_set_seek_trickmode_interval().
1500  *
1501  * Since: 1.16
1502  */
1503 void
1504 gst_event_parse_seek_trickmode_interval (GstEvent * event,
1505     GstClockTime * interval)
1506 {
1507   g_return_if_fail (event != NULL);
1508   g_return_if_fail (interval != NULL);
1509   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
1510
1511   if (interval)
1512     *interval = GST_CLOCK_TIME_NONE;
1513
1514   gst_structure_id_get (GST_EVENT_STRUCTURE (event),
1515       GST_QUARK (TRICKMODE_INTERVAL), GST_TYPE_CLOCK_TIME, interval, NULL);
1516 }
1517
1518 /**
1519  * gst_event_new_navigation:
1520  * @structure: (transfer full): description of the event. The event will take
1521  *     ownership of the structure. See #GstNavigation for more specific
1522  *     constructors.
1523  *
1524  * Create a new navigation event from the given description.
1525  *
1526  * Returns: (transfer full): a new #GstEvent
1527  */
1528 GstEvent *
1529 gst_event_new_navigation (GstStructure * structure)
1530 {
1531   g_return_val_if_fail (structure != NULL, NULL);
1532
1533   return gst_event_new_custom (GST_EVENT_NAVIGATION, structure);
1534 }
1535
1536 /**
1537  * gst_event_new_latency:
1538  * @latency: the new latency value
1539  *
1540  * Create a new latency event. The event is sent upstream from the sinks and
1541  * notifies elements that they should add an additional @latency to the
1542  * running time before synchronising against the clock.
1543  *
1544  * The latency is mostly used in live sinks and is always expressed in
1545  * the time format.
1546  *
1547  * Returns: (transfer full): a new #GstEvent
1548  */
1549 GstEvent *
1550 gst_event_new_latency (GstClockTime latency)
1551 {
1552   GstEvent *event;
1553   GstStructure *structure;
1554
1555   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (latency), NULL);
1556
1557   GST_CAT_INFO (GST_CAT_EVENT,
1558       "creating latency event %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1559
1560   structure = gst_structure_new_id (GST_QUARK (EVENT_LATENCY),
1561       GST_QUARK (LATENCY), G_TYPE_UINT64, latency, NULL);
1562   event = gst_event_new_custom (GST_EVENT_LATENCY, structure);
1563
1564   return event;
1565 }
1566
1567 /**
1568  * gst_event_parse_latency:
1569  * @event: The event to query
1570  * @latency: (out) (optional): A pointer to store the latency in.
1571  *
1572  * Get the latency in the latency event.
1573  */
1574 void
1575 gst_event_parse_latency (GstEvent * event, GstClockTime * latency)
1576 {
1577   g_return_if_fail (GST_IS_EVENT (event));
1578   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_LATENCY);
1579
1580   if (latency)
1581     *latency =
1582         g_value_get_uint64 (gst_structure_id_get_value (GST_EVENT_STRUCTURE
1583             (event), GST_QUARK (LATENCY)));
1584 }
1585
1586 /**
1587  * gst_event_new_step:
1588  * @format: the format of @amount
1589  * @amount: the amount of data to step
1590  * @rate: the step rate
1591  * @flush: flushing steps
1592  * @intermediate: intermediate steps
1593  *
1594  * Create a new step event. The purpose of the step event is to instruct a sink
1595  * to skip @amount (expressed in @format) of media. It can be used to implement
1596  * stepping through the video frame by frame or for doing fast trick modes.
1597  *
1598  * A rate of <= 0.0 is not allowed. Pause the pipeline, for the effect of rate
1599  * = 0.0 or first reverse the direction of playback using a seek event to get
1600  * the same effect as rate < 0.0.
1601  *
1602  * The @flush flag will clear any pending data in the pipeline before starting
1603  * the step operation.
1604  *
1605  * The @intermediate flag instructs the pipeline that this step operation is
1606  * part of a larger step operation.
1607  *
1608  * Returns: (transfer full): a new #GstEvent
1609  */
1610 GstEvent *
1611 gst_event_new_step (GstFormat format, guint64 amount, gdouble rate,
1612     gboolean flush, gboolean intermediate)
1613 {
1614   GstEvent *event;
1615   GstStructure *structure;
1616
1617   g_return_val_if_fail (rate > 0.0, NULL);
1618
1619   GST_CAT_INFO (GST_CAT_EVENT, "creating step event");
1620
1621   structure = gst_structure_new_id (GST_QUARK (EVENT_STEP),
1622       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1623       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1624       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1625       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1626       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1627   event = gst_event_new_custom (GST_EVENT_STEP, structure);
1628
1629   return event;
1630 }
1631
1632 /**
1633  * gst_event_parse_step:
1634  * @event: The event to query
1635  * @format: (out) (optional): a pointer to store the format in
1636  * @amount: (out) (optional): a pointer to store the amount in
1637  * @rate: (out) (optional): a pointer to store the rate in
1638  * @flush: (out) (optional): a pointer to store the flush boolean in
1639  * @intermediate: (out) (optional): a pointer to store the intermediate
1640  *     boolean in
1641  *
1642  * Parse the step event.
1643  */
1644 void
1645 gst_event_parse_step (GstEvent * event, GstFormat * format, guint64 * amount,
1646     gdouble * rate, gboolean * flush, gboolean * intermediate)
1647 {
1648   const GstStructure *structure;
1649
1650   g_return_if_fail (GST_IS_EVENT (event));
1651   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STEP);
1652
1653   structure = GST_EVENT_STRUCTURE (event);
1654   if (format)
1655     *format =
1656         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1657             GST_QUARK (FORMAT)));
1658   if (amount)
1659     *amount = g_value_get_uint64 (gst_structure_id_get_value (structure,
1660             GST_QUARK (AMOUNT)));
1661   if (rate)
1662     *rate = g_value_get_double (gst_structure_id_get_value (structure,
1663             GST_QUARK (RATE)));
1664   if (flush)
1665     *flush = g_value_get_boolean (gst_structure_id_get_value (structure,
1666             GST_QUARK (FLUSH)));
1667   if (intermediate)
1668     *intermediate = g_value_get_boolean (gst_structure_id_get_value (structure,
1669             GST_QUARK (INTERMEDIATE)));
1670 }
1671
1672 /**
1673  * gst_event_new_reconfigure:
1674
1675  * Create a new reconfigure event. The purpose of the reconfigure event is
1676  * to travel upstream and make elements renegotiate their caps or reconfigure
1677  * their buffer pools. This is useful when changing properties on elements
1678  * or changing the topology of the pipeline.
1679  *
1680  * Returns: (transfer full): a new #GstEvent
1681  */
1682 GstEvent *
1683 gst_event_new_reconfigure (void)
1684 {
1685   GstEvent *event;
1686
1687   GST_CAT_INFO (GST_CAT_EVENT, "creating reconfigure event");
1688
1689   event = gst_event_new_custom (GST_EVENT_RECONFIGURE, NULL);
1690
1691   return event;
1692 }
1693
1694 /**
1695  * gst_event_new_sink_message:
1696  * @name: a name for the event
1697  * @msg: (transfer none): the #GstMessage to be posted
1698  *
1699  * Create a new sink-message event. The purpose of the sink-message event is
1700  * to instruct a sink to post the message contained in the event synchronized
1701  * with the stream.
1702  *
1703  * @name is used to store multiple sticky events on one pad.
1704  *
1705  * Returns: (transfer full): a new #GstEvent
1706  */
1707 /* FIXME 2.0: take ownership of msg for consistency? */
1708 GstEvent *
1709 gst_event_new_sink_message (const gchar * name, GstMessage * msg)
1710 {
1711   GstEvent *event;
1712   GstStructure *structure;
1713
1714   g_return_val_if_fail (msg != NULL, NULL);
1715
1716   GST_CAT_INFO (GST_CAT_EVENT, "creating sink-message event");
1717
1718   structure = gst_structure_new_id (g_quark_from_string (name),
1719       GST_QUARK (MESSAGE), GST_TYPE_MESSAGE, msg, NULL);
1720   event = gst_event_new_custom (GST_EVENT_SINK_MESSAGE, structure);
1721
1722   return event;
1723 }
1724
1725 /**
1726  * gst_event_parse_sink_message:
1727  * @event: The event to query
1728  * @msg: (out) (optional) (transfer full): a pointer to store the #GstMessage in.
1729  *
1730  * Parse the sink-message event. Unref @msg after usage.
1731  */
1732 void
1733 gst_event_parse_sink_message (GstEvent * event, GstMessage ** msg)
1734 {
1735   const GstStructure *structure;
1736
1737   g_return_if_fail (GST_IS_EVENT (event));
1738   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE);
1739
1740   structure = GST_EVENT_STRUCTURE (event);
1741   if (msg)
1742     *msg =
1743         GST_MESSAGE (g_value_dup_boxed (gst_structure_id_get_value
1744             (structure, GST_QUARK (MESSAGE))));
1745 }
1746
1747 /**
1748  * gst_event_new_stream_start:
1749  * @stream_id: Identifier for this stream
1750  *
1751  * Create a new STREAM_START event. The stream start event can only
1752  * travel downstream synchronized with the buffer flow. It is expected
1753  * to be the first event that is sent for a new stream.
1754  *
1755  * Source elements, demuxers and other elements that create new streams
1756  * are supposed to send this event as the first event of a new stream. It
1757  * should not be sent after a flushing seek or in similar situations
1758  * and is used to mark the beginning of a new logical stream. Elements
1759  * combining multiple streams must ensure that this event is only forwarded
1760  * downstream once and not for every single input stream.
1761  *
1762  * The @stream_id should be a unique string that consists of the upstream
1763  * stream-id, / as separator and a unique stream-id for this specific
1764  * stream. A new stream-id should only be created for a stream if the upstream
1765  * stream is split into (potentially) multiple new streams, e.g. in a demuxer,
1766  * but not for every single element in the pipeline.
1767  * gst_pad_create_stream_id() or gst_pad_create_stream_id_printf() can be
1768  * used to create a stream-id.  There are no particular semantics for the
1769  * stream-id, though it should be deterministic (to support stream matching)
1770  * and it might be used to order streams (besides any information conveyed by
1771  * stream flags).
1772  *
1773  * Returns: (transfer full): the new STREAM_START event.
1774  */
1775 GstEvent *
1776 gst_event_new_stream_start (const gchar * stream_id)
1777 {
1778   GstStructure *s;
1779
1780   g_return_val_if_fail (stream_id != NULL, NULL);
1781
1782   s = gst_structure_new_id (GST_QUARK (EVENT_STREAM_START),
1783       GST_QUARK (STREAM_ID), G_TYPE_STRING, stream_id,
1784       GST_QUARK (FLAGS), GST_TYPE_STREAM_FLAGS, GST_STREAM_FLAG_NONE, NULL);
1785
1786   return gst_event_new_custom (GST_EVENT_STREAM_START, s);
1787 }
1788
1789 /**
1790  * gst_event_parse_stream_start:
1791  * @event: a stream-start event.
1792  * @stream_id: (out) (optional) (transfer none): pointer to store the stream-id
1793  *
1794  * Parse a stream-id @event and store the result in the given @stream_id
1795  * location. The string stored in @stream_id must not be modified and will
1796  * remain valid only until @event gets freed. Make a copy if you want to
1797  * modify it or store it for later use.
1798  */
1799 void
1800 gst_event_parse_stream_start (GstEvent * event, const gchar ** stream_id)
1801 {
1802   const GstStructure *structure;
1803   const GValue *val;
1804
1805   g_return_if_fail (event != NULL);
1806   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1807
1808   structure = gst_event_get_structure (event);
1809   val = gst_structure_id_get_value (structure, GST_QUARK (STREAM_ID));
1810
1811   if (stream_id)
1812     *stream_id = g_value_get_string (val);
1813 }
1814
1815 /**
1816  * gst_event_set_stream:
1817  * @event: a stream-start event
1818  * @stream: (transfer none): the stream object to set
1819  *
1820  * Set the @stream on the stream-start @event
1821  *
1822  * Since: 1.10
1823  */
1824 void
1825 gst_event_set_stream (GstEvent * event, GstStream * stream)
1826 {
1827   g_return_if_fail (event != NULL);
1828   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1829   g_return_if_fail (gst_event_is_writable (event));
1830
1831   gst_structure_id_set (GST_EVENT_STRUCTURE (event),
1832       GST_QUARK (STREAM), GST_TYPE_STREAM, stream, NULL);
1833 }
1834
1835 /**
1836  * gst_event_parse_stream:
1837  * @event: a stream-start event
1838  * @stream: (out) (optional) (transfer full): address of variable to store the stream
1839  *
1840  * Parse a stream-start @event and extract the #GstStream from it.
1841  *
1842  * Since: 1.10
1843  */
1844 void
1845 gst_event_parse_stream (GstEvent * event, GstStream ** stream)
1846 {
1847   g_return_if_fail (event != NULL);
1848   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1849
1850   if (stream) {
1851     *stream = NULL;
1852     gst_structure_id_get (GST_EVENT_STRUCTURE (event),
1853         GST_QUARK (STREAM), GST_TYPE_STREAM, stream, NULL);
1854   }
1855 }
1856
1857 /**
1858  * gst_event_set_stream_flags:
1859  * @event: a stream-start event
1860  * @flags: the stream flags to set
1861  *
1862  * Since: 1.2
1863  */
1864 void
1865 gst_event_set_stream_flags (GstEvent * event, GstStreamFlags flags)
1866 {
1867   g_return_if_fail (event != NULL);
1868   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1869   g_return_if_fail (gst_event_is_writable (event));
1870
1871   gst_structure_id_set (GST_EVENT_STRUCTURE (event),
1872       GST_QUARK (FLAGS), GST_TYPE_STREAM_FLAGS, flags, NULL);
1873 }
1874
1875 /**
1876  * gst_event_parse_stream_flags:
1877  * @event: a stream-start event
1878  * @flags: (out) (optional): address of variable where to store the stream flags
1879  *
1880  * Since: 1.2
1881  */
1882 void
1883 gst_event_parse_stream_flags (GstEvent * event, GstStreamFlags * flags)
1884 {
1885   g_return_if_fail (event != NULL);
1886   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1887
1888   if (flags) {
1889     *flags = 0;
1890     gst_structure_id_get (GST_EVENT_STRUCTURE (event),
1891         GST_QUARK (FLAGS), GST_TYPE_STREAM_FLAGS, flags, NULL);
1892   }
1893 }
1894
1895 /**
1896  * gst_event_set_group_id:
1897  * @event: a stream-start event
1898  * @group_id: the group id to set
1899  *
1900  * All streams that have the same group id are supposed to be played
1901  * together, i.e. all streams inside a container file should have the
1902  * same group id but different stream ids. The group id should change
1903  * each time the stream is started, resulting in different group ids
1904  * each time a file is played for example.
1905  *
1906  * Use gst_util_group_id_next() to get a new group id.
1907  *
1908  * Since: 1.2
1909  */
1910 void
1911 gst_event_set_group_id (GstEvent * event, guint group_id)
1912 {
1913   g_return_if_fail (event != NULL);
1914   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1915   g_return_if_fail (gst_event_is_writable (event));
1916   g_return_if_fail (group_id != GST_GROUP_ID_INVALID);
1917
1918   gst_structure_id_set (GST_EVENT_STRUCTURE (event),
1919       GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id, NULL);
1920 }
1921
1922 /**
1923  * gst_event_parse_group_id:
1924  * @event: a stream-start event
1925  * @group_id: (out) (optional): address of variable where to store the group id
1926  *
1927  * Returns: %TRUE if a group id was set on the event and could be parsed,
1928  *   %FALSE otherwise.
1929  *
1930  * Since: 1.2
1931  */
1932 gboolean
1933 gst_event_parse_group_id (GstEvent * event, guint * group_id)
1934 {
1935   g_return_val_if_fail (event != NULL, FALSE);
1936   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START,
1937       FALSE);
1938
1939   if (group_id) {
1940     *group_id = 0;
1941     return gst_structure_id_get (GST_EVENT_STRUCTURE (event),
1942         GST_QUARK (GROUP_ID), G_TYPE_UINT, group_id, NULL);
1943   }
1944
1945   return TRUE;
1946 }
1947
1948 /**
1949  * gst_event_new_stream_collection:
1950  * @collection: (transfer none): Active collection for this data flow
1951  *
1952  * Create a new STREAM_COLLECTION event. The stream collection event can only
1953  * travel downstream synchronized with the buffer flow.
1954  *
1955  * Source elements, demuxers and other elements that manage collections
1956  * of streams and post #GstStreamCollection messages on the bus also send
1957  * this event downstream on each pad involved in the collection, so that
1958  * activation of a new collection can be tracked through the downstream
1959  * data flow.
1960  *
1961  * Returns: (transfer full): the new STREAM_COLLECTION event.
1962  *
1963  * Since: 1.10
1964  */
1965 GstEvent *
1966 gst_event_new_stream_collection (GstStreamCollection * collection)
1967 {
1968   GstStructure *s;
1969
1970   g_return_val_if_fail (collection != NULL, NULL);
1971   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
1972
1973   s = gst_structure_new_id (GST_QUARK (EVENT_STREAM_COLLECTION),
1974       GST_QUARK (COLLECTION), GST_TYPE_STREAM_COLLECTION, collection, NULL);
1975
1976   return gst_event_new_custom (GST_EVENT_STREAM_COLLECTION, s);
1977 }
1978
1979 /**
1980  * gst_event_parse_stream_collection:
1981  * @event: a stream-collection event
1982  * @collection: (out) (optional) (transfer full): pointer to store the collection.
1983  *
1984  * Retrieve new #GstStreamCollection from STREAM_COLLECTION event @event.
1985  *
1986  * Since: 1.10
1987  */
1988 void
1989 gst_event_parse_stream_collection (GstEvent * event,
1990     GstStreamCollection ** collection)
1991 {
1992   const GstStructure *structure;
1993
1994   g_return_if_fail (event != NULL);
1995   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_COLLECTION);
1996
1997   structure = gst_event_get_structure (event);
1998
1999   if (collection) {
2000     gst_structure_id_get (structure,
2001         GST_QUARK (COLLECTION), GST_TYPE_STREAM_COLLECTION, collection, NULL);
2002   }
2003 }
2004
2005 /**
2006  * gst_event_new_toc:
2007  * @toc: (transfer none): #GstToc structure.
2008  * @updated: whether @toc was updated or not.
2009  *
2010  * Generate a TOC event from the given @toc. The purpose of the TOC event is to
2011  * inform elements that some kind of the TOC was found.
2012  *
2013  * Returns: (transfer full): a new #GstEvent.
2014  */
2015 GstEvent *
2016 gst_event_new_toc (GstToc * toc, gboolean updated)
2017 {
2018   GstStructure *toc_struct;
2019   GQuark id;
2020
2021   g_return_val_if_fail (toc != NULL, NULL);
2022
2023   GST_CAT_INFO (GST_CAT_EVENT, "creating toc event");
2024
2025   /* need different structure names so sticky_multi event stuff on pads
2026    * works, i.e. both TOC events are kept around */
2027   if (gst_toc_get_scope (toc) == GST_TOC_SCOPE_GLOBAL)
2028     id = GST_QUARK (EVENT_TOC_GLOBAL);
2029   else
2030     id = GST_QUARK (EVENT_TOC_CURRENT);
2031
2032   toc_struct = gst_structure_new_id (id,
2033       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2034       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2035
2036   return gst_event_new_custom (GST_EVENT_TOC, toc_struct);
2037 }
2038
2039 /**
2040  * gst_event_parse_toc:
2041  * @event: a TOC event.
2042  * @toc: (out) (optional) (transfer full): pointer to #GstToc structure.
2043  * @updated: (out) (optional): pointer to store TOC updated flag.
2044  *
2045  * Parse a TOC @event and store the results in the given @toc and @updated locations.
2046  */
2047 void
2048 gst_event_parse_toc (GstEvent * event, GstToc ** toc, gboolean * updated)
2049 {
2050   const GstStructure *structure;
2051
2052   g_return_if_fail (event != NULL);
2053   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TOC);
2054   g_return_if_fail (toc != NULL);
2055
2056   structure = gst_event_get_structure (event);
2057
2058   gst_structure_id_get (structure,
2059       GST_QUARK (TOC), GST_TYPE_TOC, toc,
2060       GST_QUARK (UPDATED), G_TYPE_BOOLEAN, updated, NULL);
2061 }
2062
2063 /**
2064  * gst_event_new_toc_select:
2065  * @uid: UID in the TOC to start playback from.
2066  *
2067  * Generate a TOC select event with the given @uid. The purpose of the
2068  * TOC select event is to start playback based on the TOC's entry with the
2069  * given @uid.
2070  *
2071  * Returns: a new #GstEvent.
2072  */
2073 GstEvent *
2074 gst_event_new_toc_select (const gchar * uid)
2075 {
2076   GstStructure *structure;
2077
2078   g_return_val_if_fail (uid != NULL, NULL);
2079
2080   GST_CAT_INFO (GST_CAT_EVENT, "creating toc select event for UID: %s", uid);
2081
2082   structure = gst_structure_new_id (GST_QUARK (EVENT_TOC_SELECT),
2083       GST_QUARK (UID), G_TYPE_STRING, uid, NULL);
2084
2085   return gst_event_new_custom (GST_EVENT_TOC_SELECT, structure);
2086 }
2087
2088 /**
2089  * gst_event_parse_toc_select:
2090  * @event: a TOC select event.
2091  * @uid: (out) (transfer full) (optional): storage for the selection UID.
2092  *
2093  * Parse a TOC select @event and store the results in the given @uid location.
2094  */
2095 void
2096 gst_event_parse_toc_select (GstEvent * event, gchar ** uid)
2097 {
2098   const GstStructure *structure;
2099   const GValue *val;
2100
2101   g_return_if_fail (event != NULL);
2102   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TOC_SELECT);
2103
2104   structure = gst_event_get_structure (event);
2105   val = gst_structure_id_get_value (structure, GST_QUARK (UID));
2106
2107   if (uid != NULL)
2108     *uid = g_value_dup_string (val);
2109
2110 }
2111
2112 /**
2113  * gst_event_new_protection:
2114  * @system_id: (transfer none): a string holding a UUID that uniquely
2115  * identifies a protection system.
2116  * @data: (transfer none): a #GstBuffer holding protection system specific
2117  * information. The reference count of the buffer will be incremented by one.
2118  * @origin: a string indicating where the protection
2119  * information carried in the event was extracted from. The allowed values
2120  * of this string will depend upon the protection scheme.
2121  *
2122  * Creates a new event containing information specific to a particular
2123  * protection system (uniquely identified by @system_id), by which that
2124  * protection system can acquire key(s) to decrypt a protected stream.
2125  *
2126  * In order for a decryption element to decrypt media
2127  * protected using a specific system, it first needs all the
2128  * protection system specific information necessary to acquire the decryption
2129  * key(s) for that stream. The functions defined here enable this information
2130  * to be passed in events from elements that extract it
2131  * (e.g., ISOBMFF demuxers, MPEG DASH demuxers) to protection decrypter
2132  * elements that use it.
2133  *
2134  * Events containing protection system specific information are created using
2135  * #gst_event_new_protection, and they can be parsed by downstream elements
2136  * using #gst_event_parse_protection.
2137  *
2138  * In Common Encryption, protection system specific information may be located
2139  * within ISOBMFF files, both in movie (moov) boxes and movie fragment (moof)
2140  * boxes; it may also be contained in ContentProtection elements within MPEG
2141  * DASH MPDs. The events created by #gst_event_new_protection contain data
2142  * identifying from which of these locations the encapsulated protection system
2143  * specific information originated. This origin information is required as
2144  * some protection systems use different encodings depending upon where the
2145  * information originates.
2146  *
2147  * The events returned by gst_event_new_protection() are implemented
2148  * in such a way as to ensure that the most recently-pushed protection info
2149  * event of a particular @origin and @system_id will
2150  * be stuck to the output pad of the sending element.
2151  *
2152  * Returns: (transfer full): a #GST_EVENT_PROTECTION event.
2153  *
2154  * Since: 1.6
2155  */
2156 GstEvent *
2157 gst_event_new_protection (const gchar * system_id,
2158     GstBuffer * data, const gchar * origin)
2159 {
2160   gchar *event_name;
2161   GstEvent *event;
2162   GstStructure *s;
2163
2164   g_return_val_if_fail (system_id != NULL, NULL);
2165   g_return_val_if_fail (data != NULL, NULL);
2166
2167   event_name =
2168       g_strconcat ("GstProtectionEvent", origin ? "-" : "",
2169       origin ? origin : "", "-", system_id, NULL);
2170
2171   GST_CAT_INFO (GST_CAT_EVENT, "creating protection event %s", event_name);
2172
2173   s = gst_structure_new (event_name, "data", GST_TYPE_BUFFER, data,
2174       "system_id", G_TYPE_STRING, system_id, NULL);
2175   if (origin)
2176     gst_structure_set (s, "origin", G_TYPE_STRING, origin, NULL);
2177   event = gst_event_new_custom (GST_EVENT_PROTECTION, s);
2178
2179   g_free (event_name);
2180   return event;
2181 }
2182
2183 /**
2184  * gst_event_parse_protection:
2185  * @event: a #GST_EVENT_PROTECTION event.
2186  * @system_id: (out) (optional) (transfer none): pointer to store the UUID
2187  * string uniquely identifying a content protection system.
2188  * @data: (out) (optional) (transfer none): pointer to store a #GstBuffer
2189  * holding protection system specific information.
2190  * @origin: (out) (optional) (transfer none): pointer to store a value that
2191  * indicates where the protection information carried by @event was extracted
2192  * from.
2193  *
2194  * Parses an event containing protection system specific information and stores
2195  * the results in @system_id, @data and @origin. The data stored in @system_id,
2196  * @origin and @data are valid until @event is released.
2197  *
2198  * Since: 1.6
2199  */
2200 void
2201 gst_event_parse_protection (GstEvent * event, const gchar ** system_id,
2202     GstBuffer ** data, const gchar ** origin)
2203 {
2204   const GstStructure *s;
2205
2206   g_return_if_fail (event != NULL);
2207   g_return_if_fail (GST_IS_EVENT (event));
2208   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_PROTECTION);
2209
2210   s = gst_event_get_structure (event);
2211
2212   if (origin)
2213     *origin = gst_structure_get_string (s, "origin");
2214
2215   if (system_id)
2216     *system_id = gst_structure_get_string (s, "system_id");
2217
2218   if (data) {
2219     const GValue *value = gst_structure_get_value (s, "data");
2220     *data = gst_value_get_buffer (value);
2221   }
2222 }
2223
2224 /**
2225  * gst_event_new_segment_done:
2226  * @format: The format of the position being done
2227  * @position: The position of the segment being done
2228  *
2229  * Create a new segment-done event. This event is sent by elements that
2230  * finish playback of a segment as a result of a segment seek.
2231  *
2232  * Returns: (transfer full): a new #GstEvent
2233  */
2234 GstEvent *
2235 gst_event_new_segment_done (GstFormat format, gint64 position)
2236 {
2237   GstEvent *event;
2238   GstStructure *structure;
2239
2240   GST_CAT_INFO (GST_CAT_EVENT, "creating segment-done event");
2241
2242   structure = gst_structure_new_id (GST_QUARK (EVENT_SEGMENT_DONE),
2243       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
2244       GST_QUARK (POSITION), G_TYPE_INT64, position, NULL);
2245
2246   event = gst_event_new_custom (GST_EVENT_SEGMENT_DONE, structure);
2247
2248   return event;
2249 }
2250
2251 /**
2252  * gst_event_parse_segment_done:
2253  * @event: A valid #GstEvent of type GST_EVENT_SEGMENT_DONE.
2254  * @format: (out) (optional): Result location for the format, or %NULL
2255  * @position: (out) (optional): Result location for the position, or %NULL
2256  *
2257  * Extracts the position and format from the segment done message.
2258  *
2259  */
2260 void
2261 gst_event_parse_segment_done (GstEvent * event, GstFormat * format,
2262     gint64 * position)
2263 {
2264   const GstStructure *structure;
2265   const GValue *val;
2266
2267   g_return_if_fail (event != NULL);
2268   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT_DONE);
2269
2270   structure = gst_event_get_structure (event);
2271
2272   val = gst_structure_id_get_value (structure, GST_QUARK (FORMAT));
2273   if (format != NULL)
2274     *format = g_value_get_enum (val);
2275
2276   val = gst_structure_id_get_value (structure, GST_QUARK (POSITION));
2277   if (position != NULL)
2278     *position = g_value_get_int64 (val);
2279 }
2280
2281 /**
2282  * gst_event_new_instant_rate_change:
2283  * @rate_multiplier: the multiplier to be applied to the playback rate
2284  * @new_flags: A new subset of segment flags to replace in segments
2285  *
2286  * Create a new instant-rate-change event. This event is sent by seek
2287  * handlers (e.g. demuxers) when receiving a seek with the
2288  * %GST_SEEK_FLAG_INSTANT_RATE_CHANGE and signals to downstream elements that
2289  * the playback rate in the existing segment should be immediately multiplied
2290  * by the @rate_multiplier factor.
2291  *
2292  * The flags provided replace any flags in the existing segment, for the
2293  * flags within the %GST_SEGMENT_INSTANT_FLAGS set. Other GstSegmentFlags
2294  * are ignored and not transferred in the event.
2295  *
2296  * Returns: (transfer full): the new instant-rate-change event.
2297  *
2298  * Since: 1.18
2299  */
2300 GstEvent *
2301 gst_event_new_instant_rate_change (gdouble rate_multiplier,
2302     GstSegmentFlags new_flags)
2303 {
2304   GstEvent *event;
2305
2306   g_return_val_if_fail (rate_multiplier != 0.0, NULL);
2307
2308   new_flags &= GST_SEGMENT_INSTANT_FLAGS;
2309
2310   GST_CAT_TRACE (GST_CAT_EVENT, "creating instant-rate-change event %lf %08x",
2311       rate_multiplier, new_flags);
2312
2313   event = gst_event_new_custom (GST_EVENT_INSTANT_RATE_CHANGE,
2314       gst_structure_new_id (GST_QUARK (EVENT_INSTANT_RATE_CHANGE),
2315           GST_QUARK (RATE), G_TYPE_DOUBLE, rate_multiplier,
2316           GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, new_flags, NULL));
2317
2318   return event;
2319 }
2320
2321 /**
2322  * gst_event_parse_instant_rate_change:
2323  * @event: a #GstEvent of type #GST_EVENT_INSTANT_RATE_CHANGE
2324  * @rate_multiplier: (out) (optional): location in which to store the rate
2325  *     multiplier of the instant-rate-change event, or %NULL
2326  * @new_flags: (out) (optional): location in which to store the new
2327  *     segment flags of the instant-rate-change event, or %NULL
2328  *
2329  * Extract rate and flags from an instant-rate-change event.
2330  *
2331  * Since: 1.18
2332  */
2333 void
2334 gst_event_parse_instant_rate_change (GstEvent * event,
2335     gdouble * rate_multiplier, GstSegmentFlags * new_flags)
2336 {
2337   GstStructure *structure;
2338
2339   g_return_if_fail (GST_IS_EVENT (event));
2340   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_INSTANT_RATE_CHANGE);
2341
2342   structure = GST_EVENT_STRUCTURE (event);
2343   gst_structure_id_get (structure, GST_QUARK (RATE), G_TYPE_DOUBLE,
2344       rate_multiplier, GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, new_flags,
2345       NULL);
2346 }
2347
2348 /**
2349  * gst_event_new_instant_rate_sync_time:
2350  * @rate_multiplier: the new playback rate multiplier to be applied
2351  * @running_time: Running time when the rate change should be applied
2352  * @upstream_running_time: The upstream-centric running-time when the
2353  *    rate change should be applied.
2354  *
2355  * Create a new instant-rate-sync-time event. This event is sent by the
2356  * pipeline to notify elements handling the instant-rate-change event about
2357  * the running-time when the new rate should be applied. The running time
2358  * may be in the past when elements handle this event, which can lead to
2359  * switching artifacts. The magnitude of those depends on the exact timing
2360  * of event delivery to each element and the magnitude of the change in
2361  * playback rate being applied.
2362  *
2363  * The @running_time and @upstream_running_time are the same if this
2364  * is the first instant-rate adjustment, but will differ for later ones
2365  * to compensate for the accumulated offset due to playing at a rate
2366  * different to the one indicated in the playback segments.
2367  *
2368  * Returns: (transfer full): the new instant-rate-sync-time event.
2369  *
2370  * Since: 1.18
2371  */
2372 GstEvent *
2373 gst_event_new_instant_rate_sync_time (gdouble rate_multiplier,
2374     GstClockTime running_time, GstClockTime upstream_running_time)
2375 {
2376   GstEvent *event;
2377
2378   g_return_val_if_fail (rate_multiplier != 0.0, NULL);
2379   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (running_time), NULL);
2380   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (upstream_running_time), NULL);
2381
2382   GST_CAT_TRACE (GST_CAT_EVENT,
2383       "creating instant-rate-sync-time event %lf %" GST_TIME_FORMAT
2384       " %" GST_TIME_FORMAT, rate_multiplier,
2385       GST_TIME_ARGS (running_time), GST_TIME_ARGS (upstream_running_time));
2386
2387   event = gst_event_new_custom (GST_EVENT_INSTANT_RATE_SYNC_TIME,
2388       gst_structure_new_id (GST_QUARK (EVENT_INSTANT_RATE_SYNC_TIME),
2389           GST_QUARK (RATE), G_TYPE_DOUBLE, rate_multiplier,
2390           GST_QUARK (RUNNING_TIME), GST_TYPE_CLOCK_TIME, running_time,
2391           GST_QUARK (UPSTREAM_RUNNING_TIME), GST_TYPE_CLOCK_TIME,
2392           upstream_running_time, NULL));
2393
2394   return event;
2395 }
2396
2397 /**
2398  * gst_event_parse_instant_rate_sync_time:
2399  * @event: a #GstEvent of type #GST_EVENT_INSTANT_RATE_CHANGE
2400  * @rate_multiplier: (out) (optional): location where to store the rate of
2401  *     the instant-rate-sync-time event, or %NULL
2402  * @running_time: (out) (optional): location in which to store the running time
2403  *     of the instant-rate-sync-time event, or %NULL
2404  * @upstream_running_time: (out) (optional): location in which to store the
2405  *     upstream running time of the instant-rate-sync-time event, or %NULL
2406  *
2407  * Extract the rate multiplier and running times from an instant-rate-sync-time event.
2408  *
2409  * Since: 1.18
2410  */
2411 void
2412 gst_event_parse_instant_rate_sync_time (GstEvent * event,
2413     gdouble * rate_multiplier, GstClockTime * running_time,
2414     GstClockTime * upstream_running_time)
2415 {
2416   GstStructure *structure;
2417
2418   g_return_if_fail (GST_IS_EVENT (event));
2419   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_INSTANT_RATE_SYNC_TIME);
2420
2421   structure = GST_EVENT_STRUCTURE (event);
2422   gst_structure_id_get (structure, GST_QUARK (RATE), G_TYPE_DOUBLE,
2423       rate_multiplier, GST_QUARK (RUNNING_TIME), GST_TYPE_CLOCK_TIME,
2424       running_time, GST_QUARK (UPSTREAM_RUNNING_TIME), GST_TYPE_CLOCK_TIME,
2425       upstream_running_time, NULL);
2426 }
2427
2428 /**
2429  * gst_event_replace: (skip)
2430  * @old_event: (inout) (transfer full) (nullable): pointer to a
2431  *     pointer to a #GstEvent to be replaced.
2432  * @new_event: (nullable) (transfer none): pointer to a #GstEvent that will
2433  *     replace the event pointed to by @old_event.
2434  *
2435  * Modifies a pointer to a #GstEvent to point to a different #GstEvent. The
2436  * modification is done atomically (so this is useful for ensuring thread safety
2437  * in some cases), and the reference counts are updated appropriately (the old
2438  * event is unreffed, the new one is reffed).
2439  *
2440  * Either @new_event or the #GstEvent pointed to by @old_event may be %NULL.
2441  *
2442  * Returns: %TRUE if @new_event was different from @old_event
2443  */
2444 gboolean
2445 gst_event_replace (GstEvent ** old_event, GstEvent * new_event)
2446 {
2447   return gst_mini_object_replace ((GstMiniObject **) old_event,
2448       (GstMiniObject *) new_event);
2449 }
2450
2451 /**
2452  * gst_event_steal: (skip)
2453  * @old_event: (inout) (transfer full) (nullable): pointer to a
2454  *     pointer to a #GstEvent to be stolen.
2455  *
2456  * Atomically replace the #GstEvent pointed to by @old_event with %NULL and
2457  * return the original event.
2458  *
2459  * Returns: the #GstEvent that was in @old_event
2460  */
2461 GstEvent *
2462 gst_event_steal (GstEvent ** old_event)
2463 {
2464   return GST_EVENT_CAST (gst_mini_object_steal ((GstMiniObject **) old_event));
2465 }
2466
2467 /**
2468  * gst_event_take: (skip)
2469  * @old_event: (inout) (transfer full) (nullable): pointer to a
2470  *     pointer to a #GstEvent to be stolen.
2471  * @new_event: (nullable) (transfer full): pointer to a #GstEvent that will
2472  *     replace the event pointed to by @old_event.
2473  *
2474  * Modifies a pointer to a #GstEvent to point to a different #GstEvent. This
2475  * function is similar to gst_event_replace() except that it takes ownership of
2476  * @new_event.
2477  *
2478  * Either @new_event or the #GstEvent pointed to by @old_event may be %NULL.
2479  *
2480  * Returns: %TRUE if @new_event was different from @old_event
2481  */
2482 gboolean
2483 gst_event_take (GstEvent ** old_event, GstEvent * new_event)
2484 {
2485   return gst_mini_object_take ((GstMiniObject **) old_event,
2486       (GstMiniObject *) new_event);
2487 }
2488
2489 /**
2490  * gst_event_ref: (skip)
2491  * @event: The event to refcount
2492  *
2493  * Increase the refcount of this event.
2494  *
2495  * Returns: (transfer full): @event (for convenience when doing assignments)
2496  */
2497 GstEvent *
2498 gst_event_ref (GstEvent * event)
2499 {
2500   return (GstEvent *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (event));
2501 }
2502
2503 /**
2504  * gst_event_unref: (skip)
2505  * @event: (transfer full): the event to refcount
2506  *
2507  * Decrease the refcount of an event, freeing it if the refcount reaches 0.
2508  */
2509 void
2510 gst_event_unref (GstEvent * event)
2511 {
2512   gst_mini_object_unref (GST_MINI_OBJECT_CAST (event));
2513 }
2514
2515 /**
2516  * gst_clear_event: (skip)
2517  * @event_ptr: a pointer to a #GstEvent reference
2518  *
2519  * Clears a reference to a #GstEvent.
2520  *
2521  * @event_ptr must not be %NULL.
2522  *
2523  * If the reference is %NULL then this function does nothing. Otherwise, the
2524  * reference count of the event is decreased and the pointer is set to %NULL.
2525  *
2526  * Since: 1.16
2527  */
2528 void
2529 gst_clear_event (GstEvent ** event_ptr)
2530 {
2531   gst_clear_mini_object ((GstMiniObject **) event_ptr);
2532 }
2533
2534 /**
2535  * gst_event_copy: (skip)
2536  * @event: The event to copy
2537  *
2538  * Copy the event using the event specific copy function.
2539  *
2540  * Returns: (transfer full): the new event
2541  */
2542 GstEvent *
2543 gst_event_copy (const GstEvent * event)
2544 {
2545   return
2546       GST_EVENT_CAST (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST
2547           (event)));
2548 }