Rework GstSegment handling
[platform/upstream/gstreamer.git] / gst / gstevent.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstevent.c: GstEvent subsystem
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstevent
26  * @short_description: Structure describing events that are passed up and down
27  *                     a pipeline
28  * @see_also: #GstPad, #GstElement
29  *
30  * The event class provides factory methods to construct events for sending
31  * and functions to query (parse) received events.
32  *
33  * Events are usually created with gst_event_new_*() which takes event-type
34  * specific parameters as arguments.
35  * To send an event application will usually use gst_element_send_event() and
36  * elements will use gst_pad_send_event() or gst_pad_push_event().
37  * The event should be unreffed with gst_event_unref() if it has not been sent.
38  *
39  * Events that have been received can be parsed with their respective 
40  * gst_event_parse_*() functions. It is valid to pass %NULL for unwanted details.
41  *
42  * Events are passed between elements in parallel to the data stream. Some events
43  * are serialized with buffers, others are not. Some events only travel downstream,
44  * others only upstream. Some events can travel both upstream and downstream. 
45  * 
46  * The events are used to signal special conditions in the datastream such as
47  * EOS (end of stream) or the start of a new stream-segment.
48  * Events are also used to flush the pipeline of any pending data.
49  *
50  * Most of the event API is used inside plugins. Applications usually only 
51  * construct and use seek events. 
52  * To do that gst_event_new_seek() is used to create a seek event. It takes
53  * the needed parameters to specity seeking time and mode.
54  * <example>
55  * <title>performing a seek on a pipeline</title>
56  *   <programlisting>
57  *   GstEvent *event;
58  *   gboolean result;
59  *   ...
60  *   // construct a seek event to play the media from second 2 to 5, flush
61  *   // the pipeline to decrease latency.
62  *   event = gst_event_new_seek (1.0, 
63  *      GST_FORMAT_TIME, 
64  *      GST_SEEK_FLAG_FLUSH,
65  *      GST_SEEK_TYPE_SET, 2 * GST_SECOND,
66  *      GST_SEEK_TYPE_SET, 5 * GST_SECOND);
67  *   ...
68  *   result = gst_element_send_event (pipeline, event);
69  *   if (!result)
70  *     g_warning ("seek failed");
71  *   ...
72  *   </programlisting>
73  * </example>
74  *
75  * Last reviewed on 2006-09-6 (0.10.10)
76  */
77
78
79 #include "gst_private.h"
80 #include <string.h>             /* memcpy */
81
82 #include "gstinfo.h"
83 #include "gstevent.h"
84 #include "gstenumtypes.h"
85 #include "gstutils.h"
86 #include "gstquark.h"
87
88 GType _gst_event_type = 0;
89
90 typedef struct
91 {
92   GstQOSType type;
93   gdouble proportion;
94   gint64 diff;
95   GstClockTime timestamp;
96 } GstEventQOSData;
97
98 typedef struct
99 {
100   GstEvent event;
101
102   GstStructure *structure;
103
104   union
105   {
106     GstEventQOSData qos;
107   };
108 } GstEventImpl;
109
110 #define GST_EVENT_STRUCTURE(e)        (((GstEventImpl *)(e))->structure)
111 #define GST_EVENT_IMPL(e,data,field)  (((GstEventImpl *)(e))->data.field)
112
113 typedef struct
114 {
115   const gint type;
116   const gchar *name;
117   GQuark quark;
118 } GstEventQuarks;
119
120 static GstEventQuarks event_quarks[] = {
121   {GST_EVENT_UNKNOWN, "unknown", 0},
122   {GST_EVENT_FLUSH_START, "flush-start", 0},
123   {GST_EVENT_FLUSH_STOP, "flush-stop", 0},
124   {GST_EVENT_EOS, "eos", 0},
125   {GST_EVENT_CAPS, "caps", 0},
126   {GST_EVENT_SEGMENT, "segment", 0},
127   {GST_EVENT_TAG, "tag", 0},
128   {GST_EVENT_BUFFERSIZE, "buffersize", 0},
129   {GST_EVENT_SINK_MESSAGE, "sink-message", 0},
130   {GST_EVENT_QOS, "qos", 0},
131   {GST_EVENT_SEEK, "seek", 0},
132   {GST_EVENT_NAVIGATION, "navigation", 0},
133   {GST_EVENT_LATENCY, "latency", 0},
134   {GST_EVENT_STEP, "step", 0},
135   {GST_EVENT_RECONFIGURE, "reconfigure", 0},
136   {GST_EVENT_CUSTOM_UPSTREAM, "custom-upstream", 0},
137   {GST_EVENT_CUSTOM_DOWNSTREAM, "custom-downstream", 0},
138   {GST_EVENT_CUSTOM_DOWNSTREAM_OOB, "custom-downstream-oob", 0},
139   {GST_EVENT_CUSTOM_BOTH, "custom-both", 0},
140   {GST_EVENT_CUSTOM_BOTH_OOB, "custom-both-oob", 0},
141
142   {0, NULL, 0}
143 };
144
145 void
146 _gst_event_initialize (void)
147 {
148   gint i;
149
150   _gst_event_type = gst_mini_object_register ("GstEvent");
151
152   g_type_class_ref (gst_seek_flags_get_type ());
153   g_type_class_ref (gst_seek_type_get_type ());
154
155   for (i = 0; event_quarks[i].name; i++) {
156     event_quarks[i].quark = g_quark_from_static_string (event_quarks[i].name);
157   }
158 }
159
160 /**
161  * gst_event_type_get_name:
162  * @type: the event type
163  *
164  * Get a printable name for the given event type. Do not modify or free.
165  *
166  * Returns: a reference to the static name of the event.
167  */
168 const gchar *
169 gst_event_type_get_name (GstEventType type)
170 {
171   gint i;
172
173   for (i = 0; event_quarks[i].name; i++) {
174     if (type == event_quarks[i].type)
175       return event_quarks[i].name;
176   }
177   return "unknown";
178 }
179
180 /**
181  * gst_event_type_to_quark:
182  * @type: the event type
183  *
184  * Get the unique quark for the given event type.
185  *
186  * Returns: the quark associated with the event type
187  */
188 GQuark
189 gst_event_type_to_quark (GstEventType type)
190 {
191   gint i;
192
193   for (i = 0; event_quarks[i].name; i++) {
194     if (type == event_quarks[i].type)
195       return event_quarks[i].quark;
196   }
197   return 0;
198 }
199
200 /**
201  * gst_event_type_get_flags:
202  * @type: a #GstEventType
203  *
204  * Gets the #GstEventTypeFlags associated with @type.
205  *
206  * Returns: a #GstEventTypeFlags.
207  */
208 GstEventTypeFlags
209 gst_event_type_get_flags (GstEventType type)
210 {
211   GstEventTypeFlags ret;
212
213   ret = type & ((1 << GST_EVENT_STICKY_SHIFT) - 1);
214
215   return ret;
216 }
217
218 static void
219 _gst_event_free (GstEvent * event)
220 {
221   GstStructure *s;
222
223   g_return_if_fail (event != NULL);
224   g_return_if_fail (GST_IS_EVENT (event));
225
226   GST_CAT_LOG (GST_CAT_EVENT, "freeing event %p type %s", event,
227       GST_EVENT_TYPE_NAME (event));
228
229   s = GST_EVENT_STRUCTURE (event);
230
231   if (s) {
232     gst_structure_set_parent_refcount (s, NULL);
233     gst_structure_free (s);
234   }
235
236   g_slice_free1 (GST_MINI_OBJECT_SIZE (event), event);
237 }
238
239 static GstEvent *
240 _gst_event_copy (GstEventImpl * event)
241 {
242   GstEventImpl *copy;
243   GstStructure *s;
244
245   copy = g_slice_dup (GstEventImpl, event);
246   gst_mini_object_init (GST_MINI_OBJECT_CAST (copy), _gst_event_type,
247       sizeof (GstEventImpl));
248
249   GST_EVENT_TYPE (copy) = GST_EVENT_TYPE (event);
250   GST_EVENT_TIMESTAMP (copy) = GST_EVENT_TIMESTAMP (event);
251   GST_EVENT_SEQNUM (copy) = GST_EVENT_SEQNUM (event);
252
253   s = GST_EVENT_STRUCTURE (event);
254   if (s) {
255     GST_EVENT_STRUCTURE (copy) = gst_structure_copy (s);
256     gst_structure_set_parent_refcount (GST_EVENT_STRUCTURE (copy),
257         &copy->event.mini_object.refcount);
258   }
259   return GST_EVENT_CAST (copy);
260 }
261
262 static void
263 gst_event_init (GstEventImpl * event, gsize size, GstEventType type)
264 {
265   gst_mini_object_init (GST_MINI_OBJECT_CAST (event), _gst_event_type, size);
266
267   event->event.mini_object.copy = (GstMiniObjectCopyFunction) _gst_event_copy;
268   event->event.mini_object.free = (GstMiniObjectFreeFunction) _gst_event_free;
269
270   GST_EVENT_TYPE (event) = type;
271   GST_EVENT_TIMESTAMP (event) = GST_CLOCK_TIME_NONE;
272   GST_EVENT_SEQNUM (event) = gst_util_seqnum_next ();
273 }
274
275 static GstEvent *
276 gst_event_new (GstEventType type)
277 {
278   GstEventImpl *event;
279
280   event = g_slice_new0 (GstEventImpl);
281
282   GST_CAT_DEBUG (GST_CAT_EVENT, "creating new event %p %s %d", event,
283       gst_event_type_get_name (type), type);
284
285   gst_event_init (event, sizeof (GstEventImpl), type);
286
287   return GST_EVENT_CAST (event);
288 }
289
290 /**
291  * gst_event_new_custom:
292  * @type: The type of the new event
293  * @structure: (transfer full): the structure for the event. The event will
294  *     take ownership of the structure.
295  *
296  * Create a new custom-typed event. This can be used for anything not
297  * handled by other event-specific functions to pass an event to another
298  * element.
299  *
300  * Make sure to allocate an event type with the #GST_EVENT_MAKE_TYPE macro,
301  * assigning a free number and filling in the correct direction and
302  * serialization flags.
303  *
304  * New custom events can also be created by subclassing the event type if
305  * needed.
306  *
307  * Returns: (transfer full): the new custom event.
308  */
309 GstEvent *
310 gst_event_new_custom (GstEventType type, GstStructure * structure)
311 {
312   GstEvent *event;
313
314   /* structure must not have a parent */
315   event = gst_event_new (type);
316   if (structure) {
317     if (!gst_structure_set_parent_refcount (structure,
318             &event->mini_object.refcount))
319       goto had_parent;
320
321     GST_EVENT_STRUCTURE (event) = structure;
322   }
323   return event;
324
325   /* ERRORS */
326 had_parent:
327   {
328     gst_event_unref (event);
329     g_warning ("structure is already owned by another object");
330     return NULL;
331   }
332 }
333
334 static inline GstStructure *
335 add_structure (GstEvent * event, GQuark name)
336 {
337   GstStructure *structure;
338
339   structure = gst_structure_id_empty_new (name);
340   gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
341   /* FIXME, concurrent access can make us leak this */
342   GST_EVENT_STRUCTURE (event) = structure;
343
344   return structure;
345 }
346
347 static GstStructure *
348 update_structure (GstEvent * event)
349 {
350   GstStructure *structure;
351
352   structure = GST_EVENT_STRUCTURE (event);
353   switch (GST_EVENT_TYPE (event)) {
354     case GST_EVENT_QOS:
355     {
356       if (structure == NULL)
357         structure = add_structure (event, GST_QUARK (EVENT_QOS));
358
359       gst_structure_id_set (structure,
360           GST_QUARK (TYPE), GST_TYPE_QOS_TYPE, GST_EVENT_IMPL (event, qos,
361               type), GST_QUARK (PROPORTION), G_TYPE_DOUBLE,
362           GST_EVENT_IMPL (event, qos, proportion), GST_QUARK (DIFF),
363           G_TYPE_INT64, GST_EVENT_IMPL (event, qos, diff),
364           GST_QUARK (TIMESTAMP), G_TYPE_UINT64, GST_EVENT_IMPL (event, qos,
365               timestamp), NULL);
366       break;
367     }
368     default:
369       break;
370   }
371   return structure;
372 }
373
374 /**
375  * gst_event_get_structure:
376  * @event: The #GstEvent.
377  *
378  * Access the structure of the event.
379  *
380  * Returns: The structure of the event. The structure is still
381  * owned by the event, which means that you should not free it and
382  * that the pointer becomes invalid when you free the event.
383  *
384  * MT safe.
385  */
386 const GstStructure *
387 gst_event_get_structure (GstEvent * event)
388 {
389   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
390
391   return update_structure (event);
392 }
393
394 /**
395  * gst_event_writable_structure:
396  * @event: The #GstEvent.
397  *
398  * Get a writable version of the structure.
399  *
400  * Returns: The structure of the event. The structure is still
401  * owned by the event, which means that you should not free it and
402  * that the pointer becomes invalid when you free the event.
403  * This function checks if @event is writable and will never return NULL.
404  *
405  * MT safe.
406  */
407 GstStructure *
408 gst_event_writable_structure (GstEvent * event)
409 {
410   GstStructure *structure;
411
412   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
413   g_return_val_if_fail (gst_event_is_writable (event), NULL);
414
415   structure = update_structure (event);
416
417   if (structure == NULL)
418     structure =
419         add_structure (event, gst_event_type_to_quark (GST_EVENT_TYPE (event)));
420
421   return structure;
422 }
423
424 /**
425  * gst_event_has_name:
426  * @event: The #GstEvent.
427  * @name: name to check
428  *
429  * Checks if @event has the given @name. This function is usually used to
430  * check the name of a custom event.
431  *
432  * Returns: %TRUE if @name matches the name of the event structure.
433  *
434  * Since: 0.10.20
435  */
436 gboolean
437 gst_event_has_name (GstEvent * event, const gchar * name)
438 {
439   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
440
441   if (GST_EVENT_STRUCTURE (event) == NULL)
442     return FALSE;
443
444   return gst_structure_has_name (GST_EVENT_STRUCTURE (event), name);
445 }
446
447 /**
448  * gst_event_get_seqnum:
449  * @event: A #GstEvent.
450  *
451  * Retrieve the sequence number of a event.
452  *
453  * Events have ever-incrementing sequence numbers, which may also be set
454  * explicitly via gst_event_set_seqnum(). Sequence numbers are typically used to
455  * indicate that a event corresponds to some other set of events or messages,
456  * for example an EOS event corresponding to a SEEK event. It is considered good
457  * practice to make this correspondence when possible, though it is not
458  * required.
459  *
460  * Note that events and messages share the same sequence number incrementor;
461  * two events or messages will never not have the same sequence number unless
462  * that correspondence was made explicitly.
463  *
464  * Returns: The event's sequence number.
465  *
466  * MT safe.
467  *
468  * Since: 0.10.22
469  */
470 guint32
471 gst_event_get_seqnum (GstEvent * event)
472 {
473   g_return_val_if_fail (GST_IS_EVENT (event), -1);
474
475   return GST_EVENT_SEQNUM (event);
476 }
477
478 /**
479  * gst_event_set_seqnum:
480  * @event: A #GstEvent.
481  * @seqnum: A sequence number.
482  *
483  * Set the sequence number of a event.
484  *
485  * This function might be called by the creator of a event to indicate that the
486  * event relates to other events or messages. See gst_event_get_seqnum() for
487  * more information.
488  *
489  * MT safe.
490  *
491  * Since: 0.10.22
492  */
493 void
494 gst_event_set_seqnum (GstEvent * event, guint32 seqnum)
495 {
496   g_return_if_fail (GST_IS_EVENT (event));
497
498   GST_EVENT_SEQNUM (event) = seqnum;
499 }
500
501 /* FIXME 0.11: It would be nice to have flush events
502  * that don't reset the running time in the sinks
503  */
504
505 /**
506  * gst_event_new_flush_start:
507  *
508  * Allocate a new flush start event. The flush start event can be sent
509  * upstream and downstream and travels out-of-bounds with the dataflow.
510  *
511  * It marks pads as being flushing and will make them return
512  * #GST_FLOW_WRONG_STATE when used for data flow with gst_pad_push(),
513  * gst_pad_chain(), gst_pad_alloc_buffer(), gst_pad_get_range() and
514  * gst_pad_pull_range(). Any event (except a #GST_EVENT_FLUSH_STOP) received
515  * on a flushing pad will return %FALSE immediately.
516  *
517  * Elements should unlock any blocking functions and exit their streaming
518  * functions as fast as possible when this event is received.
519  *
520  * This event is typically generated after a seek to flush out all queued data
521  * in the pipeline so that the new media is played as soon as possible.
522  *
523  * Returns: (transfer full): a new flush start event.
524  */
525 GstEvent *
526 gst_event_new_flush_start (void)
527 {
528   return gst_event_new (GST_EVENT_FLUSH_START);
529 }
530
531 /**
532  * gst_event_new_flush_stop:
533  *
534  * Allocate a new flush stop event. The flush stop event can be sent
535  * upstream and downstream and travels serialized with the dataflow.
536  * It is typically sent after sending a FLUSH_START event to make the
537  * pads accept data again.
538  *
539  * Elements can process this event synchronized with the dataflow since
540  * the preceeding FLUSH_START event stopped the dataflow.
541  *
542  * This event is typically generated to complete a seek and to resume
543  * dataflow.
544  *
545  * Returns: (transfer full): a new flush stop event.
546  */
547 GstEvent *
548 gst_event_new_flush_stop (void)
549 {
550   return gst_event_new (GST_EVENT_FLUSH_STOP);
551 }
552
553 /**
554  * gst_event_new_eos:
555  *
556  * Create a new EOS event. The eos event can only travel downstream
557  * synchronized with the buffer flow. Elements that receive the EOS
558  * event on a pad can return #GST_FLOW_UNEXPECTED as a #GstFlowReturn
559  * when data after the EOS event arrives.
560  *
561  * The EOS event will travel down to the sink elements in the pipeline
562  * which will then post the #GST_MESSAGE_EOS on the bus after they have
563  * finished playing any buffered data.
564  *
565  * When all sinks have posted an EOS message, an EOS message is
566  * forwarded to the application.
567  *
568  * The EOS event itself will not cause any state transitions of the pipeline.
569  *
570  * Returns: (transfer full): the new EOS event.
571  */
572 GstEvent *
573 gst_event_new_eos (void)
574 {
575   return gst_event_new (GST_EVENT_EOS);
576 }
577
578 /**
579  * gst_event_new_caps:
580  * @caps: a #GstCaps
581  *
582  * Create a new CAPS event for @caps. The caps event can only travel downstream
583  * synchronized with the buffer flow and contains the format of the buffers
584  * that will follow after the event.
585  *
586  * Returns: (transfer full): the new CAPS event.
587  */
588 GstEvent *
589 gst_event_new_caps (GstCaps * caps)
590 {
591   GstEvent *event;
592
593   g_return_val_if_fail (caps != NULL, NULL);
594   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
595
596   GST_CAT_INFO (GST_CAT_EVENT, "creating caps event %" GST_PTR_FORMAT, caps);
597
598   event = gst_event_new_custom (GST_EVENT_CAPS,
599       gst_structure_id_new (GST_QUARK (EVENT_CAPS),
600           GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL));
601
602   return event;
603 }
604
605 /**
606  * gst_event_parse_caps:
607  * @event: The event to parse
608  * @caps: (out): A pointer to the caps
609  *
610  * Get the caps from @event. The caps remains valid as long as @event remains
611  * valid.
612  */
613 void
614 gst_event_parse_caps (GstEvent * event, GstCaps ** caps)
615 {
616   GstStructure *structure;
617
618   g_return_if_fail (GST_IS_EVENT (event));
619   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
620
621   structure = GST_EVENT_STRUCTURE (event);
622   if (G_LIKELY (caps))
623     *caps =
624         g_value_get_boxed (gst_structure_id_get_value (structure,
625             GST_QUARK (CAPS)));
626 }
627
628 /**
629  * gst_event_new_segment:
630  * @segment: a #GstSegment
631  *
632  * Create a new SEGMENT event for @segment. The segment event can only travel
633  * downstream synchronized with the buffer flow and contains timing information
634  * and playback properties for the buffers that will follow.
635  *
636  * The newsegment event marks the range of buffers to be processed. All
637  * data not within the segment range is not to be processed. This can be
638  * used intelligently by plugins to apply more efficient methods of skipping
639  * unneeded data. The valid range is expressed with the @start and @stop
640  * values.
641  *
642  * The time value of the segment is used in conjunction with the start
643  * value to convert the buffer timestamps into the stream time. This is
644  * usually done in sinks to report the current stream_time.
645  * @time represents the stream_time of a buffer carrying a timestamp of
646  * @start. @time cannot be -1.
647  *
648  * @start cannot be -1, @stop can be -1. If there
649  * is a valid @stop given, it must be greater or equal the @start, including
650  * when the indicated playback @rate is < 0.
651  *
652  * The @applied_rate value provides information about any rate adjustment that
653  * has already been made to the timestamps and content on the buffers of the
654  * stream. (@rate * @applied_rate) should always equal the rate that has been
655  * requested for playback. For example, if an element has an input segment
656  * with intended playback @rate of 2.0 and applied_rate of 1.0, it can adjust
657  * incoming timestamps and buffer content by half and output a newsegment event
658  * with @rate of 1.0 and @applied_rate of 2.0
659  *
660  * After a newsegment event, the buffer stream time is calculated with:
661  *
662  *   time + (TIMESTAMP(buf) - start) * ABS (rate * applied_rate)
663  *
664  * Returns: (transfer full): the new SEGMENT event.
665  */
666 GstEvent *
667 gst_event_new_segment (GstSegment * segment)
668 {
669   GstEvent *event;
670
671   g_return_val_if_fail (segment != NULL, NULL);
672
673   GST_CAT_INFO (GST_CAT_EVENT, "creating segment event %" GST_PTR_FORMAT,
674       segment);
675
676   event = gst_event_new_custom (GST_EVENT_SEGMENT,
677       gst_structure_id_new (GST_QUARK (EVENT_SEGMENT),
678           GST_QUARK (SEGMENT), GST_TYPE_SEGMENT, segment, NULL));
679
680   return event;
681 }
682
683 /**
684  * gst_event_get_segment:
685  * @event: The event
686  *
687  * Get the segment from @event. The segment remains valid as long as @event remains
688  * valid.
689  *
690  * Returns: the #GstSegment. The segment stays valid for as long as @event is
691  * valid.
692  */
693 const GstSegment *
694 gst_event_get_segment (GstEvent * event)
695 {
696   GstStructure *structure;
697   GstSegment *segment;
698
699   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
700   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT, NULL);
701
702   structure = GST_EVENT_STRUCTURE (event);
703   segment = g_value_get_boxed (gst_structure_id_get_value (structure,
704           GST_QUARK (SEGMENT)));
705
706   return segment;
707 }
708
709 /**
710  * gst_event_parse_segment:
711  * @event: The event to parse
712  * @segment: a #GstSegment
713  *
714  * Copy the segment values from @event into @segment.
715  */
716 void
717 gst_event_parse_segment (GstEvent * event, GstSegment * segment)
718 {
719   const GstSegment *src;
720
721   g_return_if_fail (segment != NULL);
722
723   src = gst_event_get_segment (event);
724   g_return_if_fail (src != NULL);
725
726   gst_segment_copy_into (src, segment);
727 }
728
729 /**
730  * gst_event_new_tag:
731  * @taglist: (transfer full): metadata list. The event will take ownership
732  *     of the taglist.
733  *
734  * Generates a metadata tag event from the given @taglist.
735  *
736  * Returns: (transfer full): a new #GstEvent
737  */
738 GstEvent *
739 gst_event_new_tag (GstTagList * taglist)
740 {
741   g_return_val_if_fail (taglist != NULL, NULL);
742
743   return gst_event_new_custom (GST_EVENT_TAG, (GstStructure *) taglist);
744 }
745
746 /**
747  * gst_event_parse_tag:
748  * @event: a tag event
749  * @taglist: (out) (transfer none): pointer to metadata list
750  *
751  * Parses a tag @event and stores the results in the given @taglist location.
752  * No reference to the taglist will be returned, it remains valid only until
753  * the @event is freed. Don't modify or free the taglist, make a copy if you
754  * want to modify it or store it for later use.
755  */
756 void
757 gst_event_parse_tag (GstEvent * event, GstTagList ** taglist)
758 {
759   g_return_if_fail (GST_IS_EVENT (event));
760   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG);
761
762   if (taglist)
763     *taglist = (GstTagList *) GST_EVENT_STRUCTURE (event);
764 }
765
766 /* buffersize event */
767 /**
768  * gst_event_new_buffer_size:
769  * @format: buffer format
770  * @minsize: minimum buffer size
771  * @maxsize: maximum buffer size
772  * @async: thread behavior
773  *
774  * Create a new buffersize event. The event is sent downstream and notifies
775  * elements that they should provide a buffer of the specified dimensions.
776  *
777  * When the @async flag is set, a thread boundary is prefered.
778  *
779  * Returns: (transfer full): a new #GstEvent
780  */
781 GstEvent *
782 gst_event_new_buffer_size (GstFormat format, gint64 minsize,
783     gint64 maxsize, gboolean async)
784 {
785   GstEvent *event;
786   GstStructure *structure;
787
788   GST_CAT_INFO (GST_CAT_EVENT,
789       "creating buffersize format %s, minsize %" G_GINT64_FORMAT
790       ", maxsize %" G_GINT64_FORMAT ", async %d", gst_format_get_name (format),
791       minsize, maxsize, async);
792
793   structure = gst_structure_id_new (GST_QUARK (EVENT_BUFFER_SIZE),
794       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
795       GST_QUARK (MINSIZE), G_TYPE_INT64, minsize,
796       GST_QUARK (MAXSIZE), G_TYPE_INT64, maxsize,
797       GST_QUARK (ASYNC), G_TYPE_BOOLEAN, async, NULL);
798   event = gst_event_new_custom (GST_EVENT_BUFFERSIZE, structure);
799
800   return event;
801 }
802
803 /**
804  * gst_event_parse_buffer_size:
805  * @event: The event to query
806  * @format: (out): A pointer to store the format in
807  * @minsize: (out): A pointer to store the minsize in
808  * @maxsize: (out): A pointer to store the maxsize in
809  * @async: (out): A pointer to store the async-flag in
810  *
811  * Get the format, minsize, maxsize and async-flag in the buffersize event.
812  */
813 void
814 gst_event_parse_buffer_size (GstEvent * event, GstFormat * format,
815     gint64 * minsize, gint64 * maxsize, gboolean * async)
816 {
817   const GstStructure *structure;
818
819   g_return_if_fail (GST_IS_EVENT (event));
820   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_BUFFERSIZE);
821
822   structure = GST_EVENT_STRUCTURE (event);
823   if (format)
824     *format =
825         g_value_get_enum (gst_structure_id_get_value (structure,
826             GST_QUARK (FORMAT)));
827   if (minsize)
828     *minsize =
829         g_value_get_int64 (gst_structure_id_get_value (structure,
830             GST_QUARK (MINSIZE)));
831   if (maxsize)
832     *maxsize =
833         g_value_get_int64 (gst_structure_id_get_value (structure,
834             GST_QUARK (MAXSIZE)));
835   if (async)
836     *async =
837         g_value_get_boolean (gst_structure_id_get_value (structure,
838             GST_QUARK (ASYNC)));
839 }
840
841 /**
842  * gst_event_new_qos:
843  * @type: the QoS type
844  * @proportion: the proportion of the qos message
845  * @diff: The time difference of the last Clock sync
846  * @timestamp: The timestamp of the buffer
847  *
848  * Allocate a new qos event with the given values.
849  * The QOS event is generated in an element that wants an upstream
850  * element to either reduce or increase its rate because of
851  * high/low CPU load or other resource usage such as network performance or
852  * throttling. Typically sinks generate these events for each buffer
853  * they receive.
854  *
855  * @type indicates the reason for the QoS event. #GST_QOS_TYPE_OVERFLOW is
856  * used when a buffer arrived in time or when the sink cannot keep up with
857  * the upstream datarate. #GST_QOS_TYPE_UNDERFLOW is when the sink is not
858  * receiving buffers fast enough and thus has to drop late buffers. 
859  * #GST_QOS_TYPE_THROTTLE is used when the datarate is artificially limited
860  * by the application, for example to reduce power consumption.
861  *
862  * @proportion indicates the real-time performance of the streaming in the
863  * element that generated the QoS event (usually the sink). The value is
864  * generally computed based on more long term statistics about the streams
865  * timestamps compared to the clock.
866  * A value < 1.0 indicates that the upstream element is producing data faster
867  * than real-time. A value > 1.0 indicates that the upstream element is not
868  * producing data fast enough. 1.0 is the ideal @proportion value. The
869  * proportion value can safely be used to lower or increase the quality of
870  * the element.
871  *
872  * @diff is the difference against the clock in running time of the last
873  * buffer that caused the element to generate the QOS event. A negative value
874  * means that the buffer with @timestamp arrived in time. A positive value
875  * indicates how late the buffer with @timestamp was. When throttling is
876  * enabled, @diff will be set to the requested throttling interval.
877  *
878  * @timestamp is the timestamp of the last buffer that cause the element
879  * to generate the QOS event. It is expressed in running time and thus an ever
880  * increasing value.
881  *
882  * The upstream element can use the @diff and @timestamp values to decide
883  * whether to process more buffers. For possitive @diff, all buffers with
884  * timestamp <= @timestamp + @diff will certainly arrive late in the sink
885  * as well. A (negative) @diff value so that @timestamp + @diff would yield a
886  * result smaller than 0 is not allowed.
887  *
888  * The application can use general event probes to intercept the QoS
889  * event and implement custom application specific QoS handling.
890  *
891  * Returns: (transfer full): a new QOS event.
892  */
893 GstEvent *
894 gst_event_new_qos (GstQOSType type, gdouble proportion,
895     GstClockTimeDiff diff, GstClockTime timestamp)
896 {
897   GstEvent *event;
898
899   /* diff must be positive or timestamp + diff must be positive */
900   g_return_val_if_fail (diff >= 0 || -diff <= timestamp, NULL);
901
902   GST_CAT_INFO (GST_CAT_EVENT,
903       "creating qos type %d, proportion %lf, diff %" G_GINT64_FORMAT
904       ", timestamp %" GST_TIME_FORMAT, type, proportion,
905       diff, GST_TIME_ARGS (timestamp));
906
907   event = gst_event_new (GST_EVENT_QOS);
908
909   GST_EVENT_IMPL (event, qos, type) = type;
910   GST_EVENT_IMPL (event, qos, proportion) = proportion;
911   GST_EVENT_IMPL (event, qos, diff) = diff;
912   GST_EVENT_IMPL (event, qos, timestamp) = timestamp;
913
914   return event;
915 }
916
917 /**
918  * gst_event_parse_qos:
919  * @event: The event to query
920  * @type: (out): A pointer to store the QoS type in
921  * @proportion: (out): A pointer to store the proportion in
922  * @diff: (out): A pointer to store the diff in
923  * @timestamp: (out): A pointer to store the timestamp in
924  *
925  * Get the type, proportion, diff and timestamp in the qos event. See
926  * gst_event_new_qos() for more information about the different QoS values.
927  */
928 void
929 gst_event_parse_qos (GstEvent * event, GstQOSType * type,
930     gdouble * proportion, GstClockTimeDiff * diff, GstClockTime * timestamp)
931 {
932   g_return_if_fail (GST_IS_EVENT (event));
933   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
934
935   if (type)
936     *type = GST_EVENT_IMPL (event, qos, type);
937   if (proportion)
938     *proportion = GST_EVENT_IMPL (event, qos, proportion);
939   if (diff)
940     *diff = GST_EVENT_IMPL (event, qos, diff);
941   if (timestamp)
942     *timestamp = GST_EVENT_IMPL (event, qos, timestamp);
943 }
944
945 /**
946  * gst_event_new_seek:
947  * @rate: The new playback rate
948  * @format: The format of the seek values
949  * @flags: The optional seek flags
950  * @start_type: The type and flags for the new start position
951  * @start: The value of the new start position
952  * @stop_type: The type and flags for the new stop position
953  * @stop: The value of the new stop position
954  *
955  * Allocate a new seek event with the given parameters.
956  *
957  * The seek event configures playback of the pipeline between @start to @stop
958  * at the speed given in @rate, also called a playback segment.
959  * The @start and @stop values are expressed in @format.
960  *
961  * A @rate of 1.0 means normal playback rate, 2.0 means double speed.
962  * Negatives values means backwards playback. A value of 0.0 for the
963  * rate is not allowed and should be accomplished instead by PAUSING the
964  * pipeline.
965  *
966  * A pipeline has a default playback segment configured with a start
967  * position of 0, a stop position of -1 and a rate of 1.0. The currently
968  * configured playback segment can be queried with #GST_QUERY_SEGMENT. 
969  *
970  * @start_type and @stop_type specify how to adjust the currently configured 
971  * start and stop fields in playback segment. Adjustments can be made relative
972  * or absolute to the last configured values. A type of #GST_SEEK_TYPE_NONE
973  * means that the position should not be updated.
974  *
975  * When the rate is positive and @start has been updated, playback will start
976  * from the newly configured start position. 
977  *
978  * For negative rates, playback will start from the newly configured stop
979  * position (if any). If the stop position if updated, it must be different from
980  * -1 for negative rates.
981  *
982  * It is not possible to seek relative to the current playback position, to do
983  * this, PAUSE the pipeline, query the current playback position with
984  * #GST_QUERY_POSITION and update the playback segment current position with a
985  * #GST_SEEK_TYPE_SET to the desired position. 
986  *
987  * Returns: (transfer full): a new seek event.
988  */
989 GstEvent *
990 gst_event_new_seek (gdouble rate, GstFormat format, GstSeekFlags flags,
991     GstSeekType start_type, gint64 start, GstSeekType stop_type, gint64 stop)
992 {
993   GstEvent *event;
994   GstStructure *structure;
995
996   g_return_val_if_fail (rate != 0.0, NULL);
997
998   if (format == GST_FORMAT_TIME) {
999     GST_CAT_INFO (GST_CAT_EVENT,
1000         "creating seek rate %lf, format TIME, flags %d, "
1001         "start_type %d, start %" GST_TIME_FORMAT ", "
1002         "stop_type %d, stop %" GST_TIME_FORMAT,
1003         rate, flags, start_type, GST_TIME_ARGS (start),
1004         stop_type, GST_TIME_ARGS (stop));
1005   } else {
1006     GST_CAT_INFO (GST_CAT_EVENT,
1007         "creating seek rate %lf, format %s, flags %d, "
1008         "start_type %d, start %" G_GINT64_FORMAT ", "
1009         "stop_type %d, stop %" G_GINT64_FORMAT,
1010         rate, gst_format_get_name (format), flags, start_type, start, stop_type,
1011         stop);
1012   }
1013
1014   structure = gst_structure_id_new (GST_QUARK (EVENT_SEEK),
1015       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1016       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1017       GST_QUARK (FLAGS), GST_TYPE_SEEK_FLAGS, flags,
1018       GST_QUARK (CUR_TYPE), GST_TYPE_SEEK_TYPE, start_type,
1019       GST_QUARK (CUR), G_TYPE_INT64, start,
1020       GST_QUARK (STOP_TYPE), GST_TYPE_SEEK_TYPE, stop_type,
1021       GST_QUARK (STOP), G_TYPE_INT64, stop, NULL);
1022   event = gst_event_new_custom (GST_EVENT_SEEK, structure);
1023
1024   return event;
1025 }
1026
1027 /**
1028  * gst_event_parse_seek:
1029  * @event: a seek event
1030  * @rate: (out): result location for the rate
1031  * @format: (out): result location for the stream format
1032  * @flags:  (out): result location for the #GstSeekFlags
1033  * @start_type: (out): result location for the #GstSeekType of the start position
1034  * @start: (out): result location for the start postion expressed in @format
1035  * @stop_type:  (out): result location for the #GstSeekType of the stop position
1036  * @stop: (out): result location for the stop postion expressed in @format
1037  *
1038  * Parses a seek @event and stores the results in the given result locations.
1039  */
1040 void
1041 gst_event_parse_seek (GstEvent * event, gdouble * rate,
1042     GstFormat * format, GstSeekFlags * flags, GstSeekType * start_type,
1043     gint64 * start, GstSeekType * stop_type, gint64 * stop)
1044 {
1045   const GstStructure *structure;
1046
1047   g_return_if_fail (GST_IS_EVENT (event));
1048   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
1049
1050   structure = GST_EVENT_STRUCTURE (event);
1051   if (rate)
1052     *rate =
1053         g_value_get_double (gst_structure_id_get_value (structure,
1054             GST_QUARK (RATE)));
1055   if (format)
1056     *format =
1057         g_value_get_enum (gst_structure_id_get_value (structure,
1058             GST_QUARK (FORMAT)));
1059   if (flags)
1060     *flags =
1061         g_value_get_flags (gst_structure_id_get_value (structure,
1062             GST_QUARK (FLAGS)));
1063   if (start_type)
1064     *start_type =
1065         g_value_get_enum (gst_structure_id_get_value (structure,
1066             GST_QUARK (CUR_TYPE)));
1067   if (start)
1068     *start =
1069         g_value_get_int64 (gst_structure_id_get_value (structure,
1070             GST_QUARK (CUR)));
1071   if (stop_type)
1072     *stop_type =
1073         g_value_get_enum (gst_structure_id_get_value (structure,
1074             GST_QUARK (STOP_TYPE)));
1075   if (stop)
1076     *stop =
1077         g_value_get_int64 (gst_structure_id_get_value (structure,
1078             GST_QUARK (STOP)));
1079 }
1080
1081 /**
1082  * gst_event_new_navigation:
1083  * @structure: (transfer full): description of the event. The event will take
1084  *     ownership of the structure.
1085  *
1086  * Create a new navigation event from the given description.
1087  *
1088  * Returns: (transfer full): a new #GstEvent
1089  */
1090 GstEvent *
1091 gst_event_new_navigation (GstStructure * structure)
1092 {
1093   g_return_val_if_fail (structure != NULL, NULL);
1094
1095   return gst_event_new_custom (GST_EVENT_NAVIGATION, structure);
1096 }
1097
1098 /**
1099  * gst_event_new_latency:
1100  * @latency: the new latency value
1101  *
1102  * Create a new latency event. The event is sent upstream from the sinks and
1103  * notifies elements that they should add an additional @latency to the
1104  * running time before synchronising against the clock.
1105  *
1106  * The latency is mostly used in live sinks and is always expressed in
1107  * the time format.
1108  *
1109  * Returns: (transfer full): a new #GstEvent
1110  *
1111  * Since: 0.10.12
1112  */
1113 GstEvent *
1114 gst_event_new_latency (GstClockTime latency)
1115 {
1116   GstEvent *event;
1117   GstStructure *structure;
1118
1119   GST_CAT_INFO (GST_CAT_EVENT,
1120       "creating latency event %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1121
1122   structure = gst_structure_id_new (GST_QUARK (EVENT_LATENCY),
1123       GST_QUARK (LATENCY), G_TYPE_UINT64, latency, NULL);
1124   event = gst_event_new_custom (GST_EVENT_LATENCY, structure);
1125
1126   return event;
1127 }
1128
1129 /**
1130  * gst_event_parse_latency:
1131  * @event: The event to query
1132  * @latency: (out): A pointer to store the latency in.
1133  *
1134  * Get the latency in the latency event.
1135  *
1136  * Since: 0.10.12
1137  */
1138 void
1139 gst_event_parse_latency (GstEvent * event, GstClockTime * latency)
1140 {
1141   g_return_if_fail (GST_IS_EVENT (event));
1142   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_LATENCY);
1143
1144   if (latency)
1145     *latency =
1146         g_value_get_uint64 (gst_structure_id_get_value (GST_EVENT_STRUCTURE
1147             (event), GST_QUARK (LATENCY)));
1148 }
1149
1150 /**
1151  * gst_event_new_step:
1152  * @format: the format of @amount
1153  * @amount: the amount of data to step
1154  * @rate: the step rate
1155  * @flush: flushing steps
1156  * @intermediate: intermediate steps
1157  *
1158  * Create a new step event. The purpose of the step event is to instruct a sink
1159  * to skip @amount (expressed in @format) of media. It can be used to implement
1160  * stepping through the video frame by frame or for doing fast trick modes.
1161  *
1162  * A rate of <= 0.0 is not allowed, pause the pipeline or reverse the playback
1163  * direction of the pipeline to get the same effect.
1164  *
1165  * The @flush flag will clear any pending data in the pipeline before starting
1166  * the step operation.
1167  *
1168  * The @intermediate flag instructs the pipeline that this step operation is
1169  * part of a larger step operation.
1170  *
1171  * Returns: (transfer full): a new #GstEvent
1172  *
1173  * Since: 0.10.24
1174  */
1175 GstEvent *
1176 gst_event_new_step (GstFormat format, guint64 amount, gdouble rate,
1177     gboolean flush, gboolean intermediate)
1178 {
1179   GstEvent *event;
1180   GstStructure *structure;
1181
1182   g_return_val_if_fail (rate > 0.0, NULL);
1183
1184   GST_CAT_INFO (GST_CAT_EVENT, "creating step event");
1185
1186   structure = gst_structure_id_new (GST_QUARK (EVENT_STEP),
1187       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1188       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
1189       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
1190       GST_QUARK (FLUSH), G_TYPE_BOOLEAN, flush,
1191       GST_QUARK (INTERMEDIATE), G_TYPE_BOOLEAN, intermediate, NULL);
1192   event = gst_event_new_custom (GST_EVENT_STEP, structure);
1193
1194   return event;
1195 }
1196
1197 /**
1198  * gst_event_parse_step:
1199  * @event: The event to query
1200  * @format: (out) (allow-none): a pointer to store the format in
1201  * @amount: (out) (allow-none): a pointer to store the amount in
1202  * @rate: (out) (allow-none): a pointer to store the rate in
1203  * @flush: (out) (allow-none): a pointer to store the flush boolean in
1204  * @intermediate: (out) (allow-none): a pointer to store the intermediate
1205  *     boolean in
1206  *
1207  * Parse the step event.
1208  *
1209  * Since: 0.10.24
1210  */
1211 void
1212 gst_event_parse_step (GstEvent * event, GstFormat * format, guint64 * amount,
1213     gdouble * rate, gboolean * flush, gboolean * intermediate)
1214 {
1215   const GstStructure *structure;
1216
1217   g_return_if_fail (GST_IS_EVENT (event));
1218   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_STEP);
1219
1220   structure = GST_EVENT_STRUCTURE (event);
1221   if (format)
1222     *format = g_value_get_enum (gst_structure_id_get_value (structure,
1223             GST_QUARK (FORMAT)));
1224   if (amount)
1225     *amount = g_value_get_uint64 (gst_structure_id_get_value (structure,
1226             GST_QUARK (AMOUNT)));
1227   if (rate)
1228     *rate = g_value_get_double (gst_structure_id_get_value (structure,
1229             GST_QUARK (RATE)));
1230   if (flush)
1231     *flush = g_value_get_boolean (gst_structure_id_get_value (structure,
1232             GST_QUARK (FLUSH)));
1233   if (intermediate)
1234     *intermediate = g_value_get_boolean (gst_structure_id_get_value (structure,
1235             GST_QUARK (INTERMEDIATE)));
1236 }
1237
1238 /**
1239  * gst_event_new_reconfigure:
1240
1241  * Create a new reconfigure event. The purpose of the reconfingure event is
1242  * to travel upstream and make elements renegotiate their caps or reconfigure
1243  * their buffer pools. This is useful when changing properties on elements
1244  * or changing the topology of the pipeline.
1245  *
1246  * Returns: (transfer full): a new #GstEvent
1247  *
1248  * Since: 0.10.34
1249  */
1250 GstEvent *
1251 gst_event_new_reconfigure (void)
1252 {
1253   GstEvent *event;
1254
1255   GST_CAT_INFO (GST_CAT_EVENT, "creating reconfigure event");
1256
1257   event = gst_event_new_custom (GST_EVENT_RECONFIGURE, NULL);
1258
1259   return event;
1260 }
1261
1262 /**
1263  * gst_event_new_sink_message:
1264  * @msg: (transfer none): the #GstMessage to be posted
1265  *
1266  * Create a new sink-message event. The purpose of the sink-message event is
1267  * to instruct a sink to post the message contained in the event synchronized
1268  * with the stream.
1269  *
1270  * Returns: (transfer full): a new #GstEvent
1271  *
1272  * Since: 0.10.26
1273  */
1274 /* FIXME 0.11: take ownership of msg for consistency? */
1275 GstEvent *
1276 gst_event_new_sink_message (GstMessage * msg)
1277 {
1278   GstEvent *event;
1279   GstStructure *structure;
1280
1281   g_return_val_if_fail (msg != NULL, NULL);
1282
1283   GST_CAT_INFO (GST_CAT_EVENT, "creating sink-message event");
1284
1285   structure = gst_structure_id_new (GST_QUARK (EVENT_SINK_MESSAGE),
1286       GST_QUARK (MESSAGE), GST_TYPE_MESSAGE, msg, NULL);
1287   event = gst_event_new_custom (GST_EVENT_SINK_MESSAGE, structure);
1288
1289   return event;
1290 }
1291
1292 /**
1293  * gst_event_parse_sink_message:
1294  * @event: The event to query
1295  * @msg: (out) (transfer full): a pointer to store the #GstMessage in.
1296  *
1297  * Parse the sink-message event. Unref @msg after usage.
1298  *
1299  * Since: 0.10.26
1300  */
1301 void
1302 gst_event_parse_sink_message (GstEvent * event, GstMessage ** msg)
1303 {
1304   const GstStructure *structure;
1305
1306   g_return_if_fail (GST_IS_EVENT (event));
1307   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE);
1308
1309   structure = GST_EVENT_STRUCTURE (event);
1310   if (msg)
1311     *msg =
1312         GST_MESSAGE (g_value_dup_boxed (gst_structure_id_get_value
1313             (structure, GST_QUARK (MESSAGE))));
1314 }