gst-inspect: fix unused-const-variable error in windows
[platform/upstream/gstreamer.git] / gst / gstpipeline.c
index eb55a5f..ca9091b 100644 (file)
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
  * SECTION:gstpipeline
+ * @title: GstPipeline
  * @short_description: Top-level bin with clocking and bus management
                        functionality.
  * @see_also: #GstElement, #GstBin, #GstClock, #GstBus
@@ -29,8 +30,7 @@
  * A #GstPipeline is a special #GstBin used as the toplevel container for
  * the filter graph. The #GstPipeline will manage the selection and
  * distribution of a global #GstClock as well as provide a #GstBus to the
- * application. It will also implement a default behaviour for managing
- * seek events (see gst_element_seek()).
+ * application.
  *
  * gst_pipeline_new() is used to create a pipeline. when you are done with
  * the pipeline, use gst_object_unref() to free its resources including all
  * between the clock time and the base time) will count how much time was spent
  * in the PLAYING state. This default behaviour can be changed with the
  * gst_element_set_start_time() method.
- *
- * When sending a flushing seek event to a GstPipeline (see
- * gst_element_seek()), it will make sure that the pipeline is properly
- * PAUSED and resumed as well as set the new running time to 0 when the
- * seek succeeded.
- *
- * Last reviewed on 2009-05-29 (0.10.24)
  */
 
 #include "gst_private.h"
@@ -102,17 +95,16 @@ enum
 
 #define DEFAULT_DELAY           0
 #define DEFAULT_AUTO_FLUSH_BUS  TRUE
+#define DEFAULT_LATENCY         GST_CLOCK_TIME_NONE
 
 enum
 {
   PROP_0,
   PROP_DELAY,
-  PROP_AUTO_FLUSH_BUS
+  PROP_AUTO_FLUSH_BUS,
+  PROP_LATENCY
 };
 
-#define GST_PIPELINE_GET_PRIVATE(obj)  \
-   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PIPELINE, GstPipelinePrivate))
-
 struct _GstPipelinePrivate
 {
   /* with LOCK */
@@ -122,6 +114,8 @@ struct _GstPipelinePrivate
    * PLAYING*/
   GstClockTime last_start_time;
   gboolean update_clock;
+
+  GstClockTime latency;
 };
 
 
@@ -136,6 +130,7 @@ static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
     GstStateChange transition);
 
 static void gst_pipeline_handle_message (GstBin * bin, GstMessage * message);
+static gboolean gst_pipeline_do_latency (GstBin * bin);
 
 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
 
@@ -146,7 +141,8 @@ static void gst_pipeline_handle_message (GstBin * bin, GstMessage * message);
 }
 
 #define gst_pipeline_parent_class parent_class
-G_DEFINE_TYPE_WITH_CODE (GstPipeline, gst_pipeline, GST_TYPE_BIN, _do_init);
+G_DEFINE_TYPE_WITH_CODE (GstPipeline, gst_pipeline, GST_TYPE_BIN,
+    G_ADD_PRIVATE (GstPipeline) _do_init);
 
 static void
 gst_pipeline_class_init (GstPipelineClass * klass)
@@ -155,19 +151,15 @@ gst_pipeline_class_init (GstPipelineClass * klass)
   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   GstBinClass *gstbin_class = GST_BIN_CLASS (klass);
 
-  g_type_class_add_private (klass, sizeof (GstPipelinePrivate));
-
   gobject_class->set_property = gst_pipeline_set_property;
   gobject_class->get_property = gst_pipeline_get_property;
 
   /**
-   * GstPipeline:delay
+   * GstPipeline:delay:
    *
    * The expected delay needed for elements to spin up to the
    * PLAYING state expressed in nanoseconds.
    * see gst_pipeline_set_delay() for more information on this option.
-   *
-   * Since: 0.10.5
    **/
   g_object_class_install_property (gobject_class, PROP_DELAY,
       g_param_spec_uint64 ("delay", "Delay",
@@ -181,8 +173,6 @@ gst_pipeline_class_init (GstPipelineClass * klass)
    * Whether or not to automatically flush all messages on the
    * pipeline's bus when going from READY to NULL state. Please see
    * gst_pipeline_set_auto_flush_bus() for more information on this option.
-   *
-   * Since: 0.10.4
    **/
   g_object_class_install_property (gobject_class, PROP_AUTO_FLUSH_BUS,
       g_param_spec_boolean ("auto-flush-bus", "Auto Flush Bus",
@@ -190,9 +180,21 @@ gst_pipeline_class_init (GstPipelineClass * klass)
           "from READY into NULL state", DEFAULT_AUTO_FLUSH_BUS,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GstPipeline:latency:
+   *
+   * Latency to configure on the pipeline. See gst_pipeline_set_latency().
+   *
+   * Since: 1.6
+   **/
+  g_object_class_install_property (gobject_class, PROP_LATENCY,
+      g_param_spec_uint64 ("latency", "Latency",
+          "Latency to configure on the pipeline", 0, G_MAXUINT64,
+          DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gobject_class->dispose = gst_pipeline_dispose;
 
-  gst_element_class_set_metadata (gstelement_class, "Pipeline object",
+  gst_element_class_set_static_metadata (gstelement_class, "Pipeline object",
       "Generic/Bin",
       "Complete pipeline object",
       "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
@@ -203,6 +205,7 @@ gst_pipeline_class_init (GstPipelineClass * klass)
       GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
   gstbin_class->handle_message =
       GST_DEBUG_FUNCPTR (gst_pipeline_handle_message);
+  gstbin_class->do_latency = GST_DEBUG_FUNCPTR (gst_pipeline_do_latency);
 }
 
 static void
@@ -210,11 +213,12 @@ gst_pipeline_init (GstPipeline * pipeline)
 {
   GstBus *bus;
 
-  pipeline->priv = GST_PIPELINE_GET_PRIVATE (pipeline);
+  pipeline->priv = gst_pipeline_get_instance_private (pipeline);
 
   /* set default property values */
   pipeline->priv->auto_flush_bus = DEFAULT_AUTO_FLUSH_BUS;
   pipeline->delay = DEFAULT_DELAY;
+  pipeline->priv->latency = DEFAULT_LATENCY;
 
   /* create and set a default bus */
   bus = gst_bus_new ();
@@ -236,7 +240,7 @@ gst_pipeline_dispose (GObject * object)
   GstPipeline *pipeline = GST_PIPELINE (object);
   GstClock **clock_p = &pipeline->fixed_clock;
 
-  GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
+  GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "%p dispose", pipeline);
 
   /* clear and unref any fixed clock */
   gst_object_replace ((GstObject **) clock_p, NULL);
@@ -257,6 +261,9 @@ gst_pipeline_set_property (GObject * object, guint prop_id,
     case PROP_AUTO_FLUSH_BUS:
       gst_pipeline_set_auto_flush_bus (pipeline, g_value_get_boolean (value));
       break;
+    case PROP_LATENCY:
+      gst_pipeline_set_latency (pipeline, g_value_get_uint64 (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -276,6 +283,9 @@ gst_pipeline_get_property (GObject * object, guint prop_id,
     case PROP_AUTO_FLUSH_BUS:
       g_value_set_boolean (value, gst_pipeline_get_auto_flush_bus (pipeline));
       break;
+    case PROP_LATENCY:
+      g_value_set_uint64 (value, gst_pipeline_get_latency (pipeline));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -285,12 +295,12 @@ gst_pipeline_get_property (GObject * object, guint prop_id,
 /* set the start_time to 0, this will cause us to select a new base_time and
  * make the running_time start from 0 again. */
 static void
-reset_start_time (GstPipeline * pipeline)
+reset_start_time (GstPipeline * pipeline, GstClockTime start_time)
 {
   GST_OBJECT_LOCK (pipeline);
   if (GST_ELEMENT_START_TIME (pipeline) != GST_CLOCK_TIME_NONE) {
     GST_DEBUG_OBJECT (pipeline, "reset start_time to 0");
-    GST_ELEMENT_START_TIME (pipeline) = 0;
+    GST_ELEMENT_START_TIME (pipeline) = start_time;
     pipeline->priv->last_start_time = -1;
   } else {
     GST_DEBUG_OBJECT (pipeline, "application asked to not reset stream_time");
@@ -300,11 +310,11 @@ reset_start_time (GstPipeline * pipeline)
 
 /**
  * gst_pipeline_new:
- * @name: name of new pipeline
+ * @name: (allow-none): name of new pipeline
  *
  * Create a new pipeline with the given name.
  *
- * Returns: (transfer full): newly created GstPipeline
+ * Returns: (transfer floating): newly created GstPipeline
  *
  * MT safe.
  */
@@ -367,6 +377,14 @@ gst_pipeline_change_state (GstElement * element, GstStateChange transition)
   GstClock *clock;
 
   switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_NULL:
+      break;
+    case GST_STATE_CHANGE_READY_TO_READY:
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
+      break;
+    case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
+      break;
     case GST_STATE_CHANGE_NULL_TO_READY:
       GST_OBJECT_LOCK (element);
       if (element->bus)
@@ -377,6 +395,9 @@ gst_pipeline_change_state (GstElement * element, GstStateChange transition)
       GST_OBJECT_LOCK (element);
       pipeline->priv->update_clock = TRUE;
       GST_OBJECT_UNLOCK (element);
+
+      /* READY to PAUSED starts running_time from 0 */
+      reset_start_time (pipeline, 0);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
     {
@@ -415,7 +436,10 @@ gst_pipeline_change_state (GstElement * element, GstStateChange transition)
         } else {
           GST_DEBUG_OBJECT (pipeline,
               "Don't need to update clock, using old clock.");
-          clock = gst_object_ref (cur_clock);
+          /* only try to ref if cur_clock is not NULL */
+          if (cur_clock)
+            gst_object_ref (cur_clock);
+          clock = cur_clock;
         }
 
         if (clock) {
@@ -471,6 +495,8 @@ gst_pipeline_change_state (GstElement * element, GstStateChange transition)
       break;
     }
     case GST_STATE_CHANGE_PAUSED_TO_READY:
+      reset_start_time (pipeline, 0);
+      break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       break;
   }
@@ -478,13 +504,18 @@ gst_pipeline_change_state (GstElement * element, GstStateChange transition)
   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
 
   switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_NULL:
+      break;
+    case GST_STATE_CHANGE_READY_TO_READY:
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
+      break;
+    case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
+      break;
     case GST_STATE_CHANGE_NULL_TO_READY:
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
-    {
-      reset_start_time (pipeline);
       break;
-    }
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
       break;
     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
@@ -550,17 +581,15 @@ gst_pipeline_handle_message (GstBin * bin, GstMessage * message)
   GstPipeline *pipeline = GST_PIPELINE_CAST (bin);
 
   switch (GST_MESSAGE_TYPE (message)) {
-    case GST_MESSAGE_ASYNC_DONE:
+    case GST_MESSAGE_RESET_TIME:
     {
-      gboolean reset_time;
+      GstClockTime running_time;
 
-      gst_message_parse_async_done (message, &reset_time);
+      gst_message_parse_reset_time (message, &running_time);
 
       /* reset our running time if we need to distribute a new base_time to the
        * children. */
-      if (reset_time)
-        reset_start_time (pipeline);
-
+      reset_start_time (pipeline, running_time);
       break;
     }
     case GST_MESSAGE_CLOCK_LOST:
@@ -583,6 +612,75 @@ gst_pipeline_handle_message (GstBin * bin, GstMessage * message)
   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
 }
 
+static gboolean
+gst_pipeline_do_latency (GstBin * bin)
+{
+  GstPipeline *pipeline = GST_PIPELINE (bin);
+  GstQuery *query;
+  GstClockTime latency;
+  GstClockTime min_latency, max_latency;
+  gboolean res;
+
+  GST_OBJECT_LOCK (pipeline);
+  latency = pipeline->priv->latency;
+  GST_OBJECT_UNLOCK (pipeline);
+
+  if (latency == GST_CLOCK_TIME_NONE)
+    return GST_BIN_CLASS (parent_class)->do_latency (bin);
+
+  GST_DEBUG_OBJECT (pipeline, "querying latency");
+
+  query = gst_query_new_latency ();
+  if ((res = gst_element_query (GST_ELEMENT_CAST (pipeline), query))) {
+    gboolean live;
+
+    gst_query_parse_latency (query, &live, &min_latency, &max_latency);
+
+    GST_DEBUG_OBJECT (pipeline,
+        "got min latency %" GST_TIME_FORMAT ", max latency %"
+        GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
+        GST_TIME_ARGS (max_latency), live);
+
+    if (max_latency < min_latency) {
+      /* this is an impossible situation, some parts of the pipeline might not
+       * work correctly. We post a warning for now. */
+      GST_ELEMENT_WARNING (pipeline, CORE, CLOCK, (NULL),
+          ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
+              GST_TIME_FORMAT ". Add queues or other buffering elements.",
+              GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
+    }
+
+    if (latency < min_latency) {
+      /* This is a problematic situation as we will most likely drop lots of
+       * data if we configure a too low latency */
+      GST_ELEMENT_WARNING (pipeline, CORE, CLOCK, (NULL),
+          ("Configured latency is lower than detected minimum latency: configured %"
+              GST_TIME_FORMAT " < min %" GST_TIME_FORMAT,
+              GST_TIME_ARGS (latency), GST_TIME_ARGS (min_latency)));
+    }
+  } else {
+    /* this is not a real problem, we just don't configure any latency. */
+    GST_WARNING_OBJECT (pipeline, "failed to query latency");
+  }
+  gst_query_unref (query);
+
+
+  /* configure latency on elements */
+  res =
+      gst_element_send_event (GST_ELEMENT_CAST (pipeline),
+      gst_event_new_latency (latency));
+  if (res) {
+    GST_INFO_OBJECT (pipeline, "configured latency of %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (latency));
+  } else {
+    GST_WARNING_OBJECT (pipeline,
+        "did not really configure latency of %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (latency));
+  }
+
+  return res;
+}
+
 /**
  * gst_pipeline_get_bus:
  * @pipeline: a #GstPipeline
@@ -637,16 +735,40 @@ gst_pipeline_provide_clock_func (GstElement * element)
 }
 
 /**
- * gst_pipeline_get_clock:
+ * gst_pipeline_get_clock: (skip)
  * @pipeline: a #GstPipeline
  *
- * Gets the current clock used by @pipeline.
+ * Gets the current clock used by @pipeline. Users of object
+ * oriented languages should use gst_pipeline_get_pipeline_clock()
+ * to avoid confusion with gst_element_get_clock() which has a different behavior.
+ *
+ * Unlike gst_element_get_clock(), this function will always return a
+ * clock, even if the pipeline is not in the PLAYING state.
  *
  * Returns: (transfer full): a #GstClock, unref after usage.
  */
 GstClock *
 gst_pipeline_get_clock (GstPipeline * pipeline)
 {
+  return gst_pipeline_get_pipeline_clock (pipeline);
+}
+
+/**
+ * gst_pipeline_get_pipeline_clock:
+ * @pipeline: a #GstPipeline
+ *
+ * Gets the current clock used by @pipeline.
+ *
+ * Unlike gst_element_get_clock(), this function will always return a
+ * clock, even if the pipeline is not in the PLAYING state.
+ *
+ * Returns: (transfer full): a #GstClock, unref after usage.
+ *
+ * Since: 1.6
+ */
+GstClock *
+gst_pipeline_get_pipeline_clock (GstPipeline * pipeline)
+{
   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
 
   return gst_pipeline_provide_clock_func (GST_ELEMENT_CAST (pipeline));
@@ -656,13 +778,13 @@ gst_pipeline_get_clock (GstPipeline * pipeline)
 /**
  * gst_pipeline_use_clock:
  * @pipeline: a #GstPipeline
- * @clock: (transfer none): the clock to use
+ * @clock: (transfer none) (allow-none): the clock to use
  *
  * Force @pipeline to use the given @clock. The pipeline will
  * always use the given clock even if new clock providers are added
  * to this pipeline.
  *
- * If @clock is NULL all clocking will be disabled which will make
+ * If @clock is %NULL all clocking will be disabled which will make
  * the pipeline run as fast as possible.
  *
  * MT safe.
@@ -686,14 +808,14 @@ gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
 }
 
 /**
- * gst_pipeline_set_clock:
+ * gst_pipeline_set_clock: (skip)
  * @pipeline: a #GstPipeline
  * @clock: (transfer none): the clock to set
  *
  * Set the clock for @pipeline. The clock will be distributed
  * to all the elements managed by the pipeline.
  *
- * Returns: TRUE if the clock could be set on the pipeline. FALSE if
+ * Returns: %TRUE if the clock could be set on the pipeline. %FALSE if
  *   some element did not accept the clock.
  *
  * MT safe.
@@ -755,8 +877,6 @@ gst_pipeline_auto_clock (GstPipeline * pipeline)
  * used.
  *
  * MT safe.
- *
- * Since: 0.10.5
  */
 void
 gst_pipeline_set_delay (GstPipeline * pipeline, GstClockTime delay)
@@ -778,8 +898,6 @@ gst_pipeline_set_delay (GstPipeline * pipeline, GstClockTime delay)
  * Returns: The configured delay.
  *
  * MT safe.
- *
- * Since: 0.10.5
  */
 GstClockTime
 gst_pipeline_get_delay (GstPipeline * pipeline)
@@ -815,8 +933,6 @@ gst_pipeline_get_delay (GstPipeline * pipeline)
  * automatic flushing is disabled else memory leaks will be introduced.
  *
  * MT safe.
- *
- * Since: 0.10.4
  */
 void
 gst_pipeline_set_auto_flush_bus (GstPipeline * pipeline, gboolean auto_flush)
@@ -839,8 +955,6 @@ gst_pipeline_set_auto_flush_bus (GstPipeline * pipeline, gboolean auto_flush)
  * going from READY to NULL state or not.
  *
  * MT safe.
- *
- * Since: 0.10.4
  */
 gboolean
 gst_pipeline_get_auto_flush_bus (GstPipeline * pipeline)
@@ -855,3 +969,60 @@ gst_pipeline_get_auto_flush_bus (GstPipeline * pipeline)
 
   return res;
 }
+
+/**
+ * gst_pipeline_set_latency:
+ * @pipeline: a #GstPipeline
+ * @latency: latency to configure
+ *
+ * Sets the latency that should be configured on the pipeline. Setting
+ * GST_CLOCK_TIME_NONE will restore the default behaviour of using the minimum
+ * latency from the LATENCY query. Setting this is usually not required and
+ * the pipeline will figure out an appropriate latency automatically.
+ *
+ * Setting a too low latency, especially lower than the minimum latency from
+ * the LATENCY query, will most likely cause the pipeline to fail.
+ *
+ * Since: 1.6
+ */
+void
+gst_pipeline_set_latency (GstPipeline * pipeline, GstClockTime latency)
+{
+  gboolean changed;
+
+  g_return_if_fail (GST_IS_PIPELINE (pipeline));
+
+  GST_OBJECT_LOCK (pipeline);
+  changed = (pipeline->priv->latency != latency);
+  pipeline->priv->latency = latency;
+  GST_OBJECT_UNLOCK (pipeline);
+
+  if (changed)
+    gst_bin_recalculate_latency (GST_BIN_CAST (pipeline));
+}
+
+/**
+ * gst_pipeline_get_latency:
+ * @pipeline: a #GstPipeline
+ *
+ * Gets the latency that should be configured on the pipeline. See
+ * gst_pipeline_set_latency().
+ *
+ * Returns: Latency to configure on the pipeline or GST_CLOCK_TIME_NONE
+ *
+ * Since: 1.6
+ */
+
+GstClockTime
+gst_pipeline_get_latency (GstPipeline * pipeline)
+{
+  GstClockTime latency;
+
+  g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
+
+  GST_OBJECT_LOCK (pipeline);
+  latency = pipeline->priv->latency;
+  GST_OBJECT_UNLOCK (pipeline);
+
+  return latency;
+}