event: remove the sticky event index
[platform/upstream/gstreamer.git] / gst / gstevent.c
index 97eeb35..cad2018 100644 (file)
@@ -50,7 +50,7 @@
  * Most of the event API is used inside plugins. Applications usually only 
  * construct and use seek events. 
  * To do that gst_event_new_seek() is used to create a seek event. It takes
- * the needed parameters to specity seeking time and mode.
+ * the needed parameters to specify seeking time and mode.
  * <example>
  * <title>performing a seek on a pipeline</title>
  *   <programlisting>
@@ -89,26 +89,12 @@ GType _gst_event_type = 0;
 
 typedef struct
 {
-  GstQOSType type;
-  gdouble proportion;
-  gint64 diff;
-  GstClockTime timestamp;
-} GstEventQOSData;
-
-typedef struct
-{
   GstEvent event;
 
   GstStructure *structure;
-
-  union
-  {
-    GstEventQOSData qos;
-  };
 } GstEventImpl;
 
-#define GST_EVENT_STRUCTURE(e)        (((GstEventImpl *)(e))->structure)
-#define GST_EVENT_IMPL(e,data,field)  (((GstEventImpl *)(e))->data.field)
+#define GST_EVENT_STRUCTURE(e)  (((GstEventImpl *)(e))->structure)
 
 typedef struct
 {
@@ -136,18 +122,21 @@ static GstEventQuarks event_quarks[] = {
   {GST_EVENT_CUSTOM_UPSTREAM, "custom-upstream", 0},
   {GST_EVENT_CUSTOM_DOWNSTREAM, "custom-downstream", 0},
   {GST_EVENT_CUSTOM_DOWNSTREAM_OOB, "custom-downstream-oob", 0},
+  {GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, "custom-downstream-sticky", 0},
   {GST_EVENT_CUSTOM_BOTH, "custom-both", 0},
   {GST_EVENT_CUSTOM_BOTH_OOB, "custom-both-oob", 0},
 
   {0, NULL, 0}
 };
 
+GST_DEFINE_MINI_OBJECT_TYPE (GstEvent, gst_event);
+
 void
-_gst_event_initialize (void)
+_priv_gst_event_initialize (void)
 {
   gint i;
 
-  _gst_event_type = gst_mini_object_register ("GstEvent");
+  _gst_event_type = gst_event_get_type ();
 
   g_type_class_ref (gst_seek_flags_get_type ());
   g_type_class_ref (gst_seek_type_get_type ());
@@ -210,7 +199,7 @@ gst_event_type_get_flags (GstEventType type)
 {
   GstEventTypeFlags ret;
 
-  ret = type & ((1 << GST_EVENT_STICKY_SHIFT) - 1);
+  ret = type & ((1 << GST_EVENT_NUM_SHIFT) - 1);
 
   return ret;
 }
@@ -236,17 +225,19 @@ _gst_event_free (GstEvent * event)
   g_slice_free1 (GST_MINI_OBJECT_SIZE (event), event);
 }
 
+static void gst_event_init (GstEventImpl * event, gsize size,
+    GstEventType type);
+
 static GstEvent *
-_gst_event_copy (GstEventImpl * event)
+_gst_event_copy (GstEvent * event)
 {
   GstEventImpl *copy;
   GstStructure *s;
 
-  copy = g_slice_dup (GstEventImpl, event);
-  gst_mini_object_init (GST_MINI_OBJECT_CAST (copy), _gst_event_type,
-      sizeof (GstEventImpl));
+  copy = g_slice_new0 (GstEventImpl);
+
+  gst_event_init (copy, sizeof (GstEventImpl), GST_EVENT_TYPE (event));
 
-  GST_EVENT_TYPE (copy) = GST_EVENT_TYPE (event);
   GST_EVENT_TIMESTAMP (copy) = GST_EVENT_TIMESTAMP (event);
   GST_EVENT_SEQNUM (copy) = GST_EVENT_SEQNUM (event);
 
@@ -331,46 +322,6 @@ had_parent:
   }
 }
 
-static inline GstStructure *
-add_structure (GstEvent * event, GQuark name)
-{
-  GstStructure *structure;
-
-  structure = gst_structure_id_empty_new (name);
-  gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
-  /* FIXME, concurrent access can make us leak this */
-  GST_EVENT_STRUCTURE (event) = structure;
-
-  return structure;
-}
-
-static GstStructure *
-update_structure (GstEvent * event)
-{
-  GstStructure *structure;
-
-  structure = GST_EVENT_STRUCTURE (event);
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_QOS:
-    {
-      if (structure == NULL)
-        structure = add_structure (event, GST_QUARK (EVENT_QOS));
-
-      gst_structure_id_set (structure,
-          GST_QUARK (TYPE), GST_TYPE_QOS_TYPE, GST_EVENT_IMPL (event, qos,
-              type), GST_QUARK (PROPORTION), G_TYPE_DOUBLE,
-          GST_EVENT_IMPL (event, qos, proportion), GST_QUARK (DIFF),
-          G_TYPE_INT64, GST_EVENT_IMPL (event, qos, diff),
-          GST_QUARK (TIMESTAMP), G_TYPE_UINT64, GST_EVENT_IMPL (event, qos,
-              timestamp), NULL);
-      break;
-    }
-    default:
-      break;
-  }
-  return structure;
-}
-
 /**
  * gst_event_get_structure:
  * @event: The #GstEvent.
@@ -388,7 +339,7 @@ gst_event_get_structure (GstEvent * event)
 {
   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
 
-  return update_structure (event);
+  return GST_EVENT_STRUCTURE (event);
 }
 
 /**
@@ -412,12 +363,15 @@ gst_event_writable_structure (GstEvent * event)
   g_return_val_if_fail (GST_IS_EVENT (event), NULL);
   g_return_val_if_fail (gst_event_is_writable (event), NULL);
 
-  structure = update_structure (event);
+  structure = GST_EVENT_STRUCTURE (event);
 
-  if (structure == NULL)
+  if (structure == NULL) {
     structure =
-        add_structure (event, gst_event_type_to_quark (GST_EVENT_TYPE (event)));
-
+        gst_structure_new_id_empty (gst_event_type_to_quark (GST_EVENT_TYPE
+            (event)));
+    gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
+    GST_EVENT_STRUCTURE (event) = structure;
+  }
   return structure;
 }
 
@@ -458,7 +412,7 @@ gst_event_has_name (GstEvent * event, const gchar * name)
  * required.
  *
  * Note that events and messages share the same sequence number incrementor;
- * two events or messages will never not have the same sequence number unless
+ * two events or messages will never have the same sequence number unless
  * that correspondence was made explicitly.
  *
  * Returns: The event's sequence number.
@@ -530,6 +484,7 @@ gst_event_new_flush_start (void)
 
 /**
  * gst_event_new_flush_stop:
+ * @reset_time: if time should be reset
  *
  * Allocate a new flush stop event. The flush stop event can be sent
  * upstream and downstream and travels serialized with the dataflow.
@@ -545,9 +500,39 @@ gst_event_new_flush_start (void)
  * Returns: (transfer full): a new flush stop event.
  */
 GstEvent *
-gst_event_new_flush_stop (void)
+gst_event_new_flush_stop (gboolean reset_time)
 {
-  return gst_event_new (GST_EVENT_FLUSH_STOP);
+  GstEvent *event;
+
+  GST_CAT_INFO (GST_CAT_EVENT, "creating flush stop %d", reset_time);
+
+  event = gst_event_new_custom (GST_EVENT_FLUSH_STOP,
+      gst_structure_new_id (GST_QUARK (EVENT_FLUSH_STOP),
+          GST_QUARK (RESET_TIME), G_TYPE_BOOLEAN, reset_time, NULL));
+
+  return event;
+}
+
+/**
+ * gst_event_parse_flush_stop:
+ * @event: The event to parse
+ * @reset_time: (out): if time should be reset
+ *
+ * Parse the FLUSH_STOP event and retrieve the @reset_time member.
+ */
+void
+gst_event_parse_flush_stop (GstEvent * event, gboolean * reset_time)
+{
+  GstStructure *structure;
+
+  g_return_if_fail (GST_IS_EVENT (event));
+  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP);
+
+  structure = GST_EVENT_STRUCTURE (event);
+  if (G_LIKELY (reset_time))
+    *reset_time =
+        g_value_get_boolean (gst_structure_id_get_value (structure,
+            GST_QUARK (RESET_TIME)));
 }
 
 /**
@@ -577,7 +562,7 @@ gst_event_new_eos (void)
 
 /**
  * gst_event_new_caps:
- * @caps: a #GstCaps
+ * @caps: (transfer none): a #GstCaps
  *
  * Create a new CAPS event for @caps. The caps event can only travel downstream
  * synchronized with the buffer flow and contains the format of the buffers
@@ -596,7 +581,7 @@ gst_event_new_caps (GstCaps * caps)
   GST_CAT_INFO (GST_CAT_EVENT, "creating caps event %" GST_PTR_FORMAT, caps);
 
   event = gst_event_new_custom (GST_EVENT_CAPS,
-      gst_structure_id_new (GST_QUARK (EVENT_CAPS),
+      gst_structure_new_id (GST_QUARK (EVENT_CAPS),
           GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL));
 
   return event;
@@ -627,7 +612,7 @@ gst_event_parse_caps (GstEvent * event, GstCaps ** caps)
 
 /**
  * gst_event_new_segment:
- * @segment: a #GstSegment
+ * @segment: (transfer none): a #GstSegment
  *
  * Create a new SEGMENT event for @segment. The segment event can only travel
  * downstream synchronized with the buffer flow and contains timing information
@@ -664,66 +649,69 @@ gst_event_parse_caps (GstEvent * event, GstCaps ** caps)
  * Returns: (transfer full): the new SEGMENT event.
  */
 GstEvent *
-gst_event_new_segment (GstSegment * segment)
+gst_event_new_segment (const GstSegment * segment)
 {
   GstEvent *event;
 
   g_return_val_if_fail (segment != NULL, NULL);
+  g_return_val_if_fail (segment->rate != 0.0, NULL);
+  g_return_val_if_fail (segment->applied_rate != 0.0, NULL);
+  g_return_val_if_fail (segment->format != GST_FORMAT_UNDEFINED, NULL);
 
-  GST_CAT_INFO (GST_CAT_EVENT, "creating segment event %" GST_PTR_FORMAT,
+  GST_CAT_INFO (GST_CAT_EVENT, "creating segment event %" GST_SEGMENT_FORMAT,
       segment);
 
   event = gst_event_new_custom (GST_EVENT_SEGMENT,
-      gst_structure_id_new (GST_QUARK (EVENT_SEGMENT),
+      gst_structure_new_id (GST_QUARK (EVENT_SEGMENT),
           GST_QUARK (SEGMENT), GST_TYPE_SEGMENT, segment, NULL));
 
   return event;
 }
 
 /**
- * gst_event_get_segment:
- * @event: The event
- *
- * Get the segment from @event. The segment remains valid as long as @event remains
- * valid.
+ * gst_event_parse_segment:
+ * @event: The event to parse
+ * @segment: (out) (transfer none): a pointer to a #GstSegment
  *
- * Returns: the #GstSegment. The segment stays valid for as long as @event is
- * valid.
+ * Parses a segment @event and stores the result in the given @segment location.
+ * @segment remains valid only until the @event is freed. Don't modify the segment
+ * and make a copy if you want to modify it or store it for later use.
  */
-const GstSegment *
-gst_event_get_segment (GstEvent * event)
+void
+gst_event_parse_segment (GstEvent * event, const GstSegment ** segment)
 {
   GstStructure *structure;
-  GstSegment *segment;
-
-  g_return_val_if_fail (GST_IS_EVENT (event), NULL);
-  g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT, NULL);
 
-  structure = GST_EVENT_STRUCTURE (event);
-  segment = g_value_get_boxed (gst_structure_id_get_value (structure,
-          GST_QUARK (SEGMENT)));
+  g_return_if_fail (GST_IS_EVENT (event));
+  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
 
-  return segment;
+  if (segment) {
+    structure = GST_EVENT_STRUCTURE (event);
+    *segment = g_value_get_boxed (gst_structure_id_get_value (structure,
+            GST_QUARK (SEGMENT)));
+  }
 }
 
 /**
- * gst_event_parse_segment:
+ * gst_event_copy_segment:
  * @event: The event to parse
- * @segment: a #GstSegment
+ * @segment: a pointer to a #GstSegment
  *
- * Copy the segment values from @event into @segment.
+ * Parses a segment @event and copies the #GstSegment into the location
+ * given by @segment.
  */
 void
-gst_event_parse_segment (GstEvent * event, GstSegment * segment)
+gst_event_copy_segment (GstEvent * event, GstSegment * segment)
 {
   const GstSegment *src;
 
-  g_return_if_fail (segment != NULL);
-
-  src = gst_event_get_segment (event);
-  g_return_if_fail (src != NULL);
+  g_return_if_fail (GST_IS_EVENT (event));
+  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
 
-  gst_segment_copy_into (src, segment);
+  if (segment) {
+    gst_event_parse_segment (event, &src);
+    gst_segment_copy_into (src, segment);
+  }
 }
 
 /**
@@ -774,7 +762,7 @@ gst_event_parse_tag (GstEvent * event, GstTagList ** taglist)
  * Create a new buffersize event. The event is sent downstream and notifies
  * elements that they should provide a buffer of the specified dimensions.
  *
- * When the @async flag is set, a thread boundary is prefered.
+ * When the @async flag is set, a thread boundary is preferred.
  *
  * Returns: (transfer full): a new #GstEvent
  */
@@ -790,7 +778,7 @@ gst_event_new_buffer_size (GstFormat format, gint64 minsize,
       ", maxsize %" G_GINT64_FORMAT ", async %d", gst_format_get_name (format),
       minsize, maxsize, async);
 
-  structure = gst_structure_id_new (GST_QUARK (EVENT_BUFFER_SIZE),
+  structure = gst_structure_new_id (GST_QUARK (EVENT_BUFFER_SIZE),
       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
       GST_QUARK (MINSIZE), G_TYPE_INT64, minsize,
       GST_QUARK (MAXSIZE), G_TYPE_INT64, maxsize,
@@ -821,7 +809,7 @@ gst_event_parse_buffer_size (GstEvent * event, GstFormat * format,
 
   structure = GST_EVENT_STRUCTURE (event);
   if (format)
-    *format =
+    *format = (GstFormat)
         g_value_get_enum (gst_structure_id_get_value (structure,
             GST_QUARK (FORMAT)));
   if (minsize)
@@ -895,21 +883,22 @@ gst_event_new_qos (GstQOSType type, gdouble proportion,
     GstClockTimeDiff diff, GstClockTime timestamp)
 {
   GstEvent *event;
+  GstStructure *structure;
 
   /* diff must be positive or timestamp + diff must be positive */
   g_return_val_if_fail (diff >= 0 || -diff <= timestamp, NULL);
 
-  GST_CAT_INFO (GST_CAT_EVENT,
+  GST_CAT_LOG (GST_CAT_EVENT,
       "creating qos type %d, proportion %lf, diff %" G_GINT64_FORMAT
       ", timestamp %" GST_TIME_FORMAT, type, proportion,
       diff, GST_TIME_ARGS (timestamp));
 
-  event = gst_event_new (GST_EVENT_QOS);
-
-  GST_EVENT_IMPL (event, qos, type) = type;
-  GST_EVENT_IMPL (event, qos, proportion) = proportion;
-  GST_EVENT_IMPL (event, qos, diff) = diff;
-  GST_EVENT_IMPL (event, qos, timestamp) = timestamp;
+  structure = gst_structure_new_id (GST_QUARK (EVENT_QOS),
+      GST_QUARK (TYPE), GST_TYPE_QOS_TYPE, type,
+      GST_QUARK (PROPORTION), G_TYPE_DOUBLE, proportion,
+      GST_QUARK (DIFF), G_TYPE_INT64, diff,
+      GST_QUARK (TIMESTAMP), G_TYPE_UINT64, timestamp, NULL);
+  event = gst_event_new_custom (GST_EVENT_QOS, structure);
 
   return event;
 }
@@ -929,17 +918,28 @@ void
 gst_event_parse_qos (GstEvent * event, GstQOSType * type,
     gdouble * proportion, GstClockTimeDiff * diff, GstClockTime * timestamp)
 {
+  const GstStructure *structure;
+
   g_return_if_fail (GST_IS_EVENT (event));
   g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
 
+  structure = GST_EVENT_STRUCTURE (event);
   if (type)
-    *type = GST_EVENT_IMPL (event, qos, type);
+    *type = (GstQOSType)
+        g_value_get_enum (gst_structure_id_get_value (structure,
+            GST_QUARK (TYPE)));
   if (proportion)
-    *proportion = GST_EVENT_IMPL (event, qos, proportion);
+    *proportion =
+        g_value_get_double (gst_structure_id_get_value (structure,
+            GST_QUARK (PROPORTION)));
   if (diff)
-    *diff = GST_EVENT_IMPL (event, qos, diff);
+    *diff =
+        g_value_get_int64 (gst_structure_id_get_value (structure,
+            GST_QUARK (DIFF)));
   if (timestamp)
-    *timestamp = GST_EVENT_IMPL (event, qos, timestamp);
+    *timestamp =
+        g_value_get_uint64 (gst_structure_id_get_value (structure,
+            GST_QUARK (TIMESTAMP)));
 }
 
 /**
@@ -1011,7 +1011,7 @@ gst_event_new_seek (gdouble rate, GstFormat format, GstSeekFlags flags,
         stop);
   }
 
-  structure = gst_structure_id_new (GST_QUARK (EVENT_SEEK),
+  structure = gst_structure_new_id (GST_QUARK (EVENT_SEEK),
       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
       GST_QUARK (FLAGS), GST_TYPE_SEEK_FLAGS, flags,
@@ -1053,15 +1053,15 @@ gst_event_parse_seek (GstEvent * event, gdouble * rate,
         g_value_get_double (gst_structure_id_get_value (structure,
             GST_QUARK (RATE)));
   if (format)
-    *format =
+    *format = (GstFormat)
         g_value_get_enum (gst_structure_id_get_value (structure,
             GST_QUARK (FORMAT)));
   if (flags)
-    *flags =
+    *flags = (GstSeekFlags)
         g_value_get_flags (gst_structure_id_get_value (structure,
             GST_QUARK (FLAGS)));
   if (start_type)
-    *start_type =
+    *start_type = (GstSeekType)
         g_value_get_enum (gst_structure_id_get_value (structure,
             GST_QUARK (CUR_TYPE)));
   if (start)
@@ -1069,7 +1069,7 @@ gst_event_parse_seek (GstEvent * event, gdouble * rate,
         g_value_get_int64 (gst_structure_id_get_value (structure,
             GST_QUARK (CUR)));
   if (stop_type)
-    *stop_type =
+    *stop_type = (GstSeekType)
         g_value_get_enum (gst_structure_id_get_value (structure,
             GST_QUARK (STOP_TYPE)));
   if (stop)
@@ -1119,7 +1119,7 @@ gst_event_new_latency (GstClockTime latency)
   GST_CAT_INFO (GST_CAT_EVENT,
       "creating latency event %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
 
-  structure = gst_structure_id_new (GST_QUARK (EVENT_LATENCY),
+  structure = gst_structure_new_id (GST_QUARK (EVENT_LATENCY),
       GST_QUARK (LATENCY), G_TYPE_UINT64, latency, NULL);
   event = gst_event_new_custom (GST_EVENT_LATENCY, structure);
 
@@ -1159,8 +1159,9 @@ gst_event_parse_latency (GstEvent * event, GstClockTime * latency)
  * to skip @amount (expressed in @format) of media. It can be used to implement
  * stepping through the video frame by frame or for doing fast trick modes.
  *
- * A rate of <= 0.0 is not allowed, pause the pipeline or reverse the playback
- * direction of the pipeline to get the same effect.
+ * A rate of <= 0.0 is not allowed. Pause the pipeline, for the effect of rate
+ * = 0.0 or first reverse the direction of playback using a seek event to get
+ * the same effect as rate < 0.0.
  *
  * The @flush flag will clear any pending data in the pipeline before starting
  * the step operation.
@@ -1183,7 +1184,7 @@ gst_event_new_step (GstFormat format, guint64 amount, gdouble rate,
 
   GST_CAT_INFO (GST_CAT_EVENT, "creating step event");
 
-  structure = gst_structure_id_new (GST_QUARK (EVENT_STEP),
+  structure = gst_structure_new_id (GST_QUARK (EVENT_STEP),
       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
       GST_QUARK (AMOUNT), G_TYPE_UINT64, amount,
       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
@@ -1219,7 +1220,8 @@ gst_event_parse_step (GstEvent * event, GstFormat * format, guint64 * amount,
 
   structure = GST_EVENT_STRUCTURE (event);
   if (format)
-    *format = g_value_get_enum (gst_structure_id_get_value (structure,
+    *format =
+        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
             GST_QUARK (FORMAT)));
   if (amount)
     *amount = g_value_get_uint64 (gst_structure_id_get_value (structure,
@@ -1245,7 +1247,7 @@ gst_event_parse_step (GstEvent * event, GstFormat * format, guint64 * amount,
  *
  * Returns: (transfer full): a new #GstEvent
  *
- * Since: 0.10.34
+ * Since: 0.11.0
  */
 GstEvent *
 gst_event_new_reconfigure (void)
@@ -1282,7 +1284,7 @@ gst_event_new_sink_message (GstMessage * msg)
 
   GST_CAT_INFO (GST_CAT_EVENT, "creating sink-message event");
 
-  structure = gst_structure_id_new (GST_QUARK (EVENT_SINK_MESSAGE),
+  structure = gst_structure_new_id (GST_QUARK (EVENT_SINK_MESSAGE),
       GST_QUARK (MESSAGE), GST_TYPE_MESSAGE, msg, NULL);
   event = gst_event_new_custom (GST_EVENT_SINK_MESSAGE, structure);