gst-inspect: fix unused-const-variable error in windows
[platform/upstream/gstreamer.git] / gst / gstpipeline.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2004,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstpipeline.c: Overall pipeline management element
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:gstpipeline
25  * @title: GstPipeline
26  * @short_description: Top-level bin with clocking and bus management
27                        functionality.
28  * @see_also: #GstElement, #GstBin, #GstClock, #GstBus
29  *
30  * A #GstPipeline is a special #GstBin used as the toplevel container for
31  * the filter graph. The #GstPipeline will manage the selection and
32  * distribution of a global #GstClock as well as provide a #GstBus to the
33  * application.
34  *
35  * gst_pipeline_new() is used to create a pipeline. when you are done with
36  * the pipeline, use gst_object_unref() to free its resources including all
37  * added #GstElement objects (if not otherwise referenced).
38  *
39  * Elements are added and removed from the pipeline using the #GstBin
40  * methods like gst_bin_add() and gst_bin_remove() (see #GstBin).
41  *
42  * Before changing the state of the #GstPipeline (see #GstElement) a #GstBus
43  * can be retrieved with gst_pipeline_get_bus(). This bus can then be
44  * used to receive #GstMessage from the elements in the pipeline.
45  *
46  * By default, a #GstPipeline will automatically flush the pending #GstBus
47  * messages when going to the NULL state to ensure that no circular
48  * references exist when no messages are read from the #GstBus. This
49  * behaviour can be changed with gst_pipeline_set_auto_flush_bus().
50  *
51  * When the #GstPipeline performs the PAUSED to PLAYING state change it will
52  * select a clock for the elements. The clock selection algorithm will by
53  * default select a clock provided by an element that is most upstream
54  * (closest to the source). For live pipelines (ones that return
55  * #GST_STATE_CHANGE_NO_PREROLL from the gst_element_set_state() call) this
56  * will select the clock provided by the live source. For normal pipelines
57  * this will select a clock provided by the sinks (most likely the audio
58  * sink). If no element provides a clock, a default #GstSystemClock is used.
59  *
60  * The clock selection can be controlled with the gst_pipeline_use_clock()
61  * method, which will enforce a given clock on the pipeline. With
62  * gst_pipeline_auto_clock() the default clock selection algorithm can be
63  * restored.
64  *
65  * A #GstPipeline maintains a running time for the elements. The running
66  * time is defined as the difference between the current clock time and
67  * the base time. When the pipeline goes to READY or a flushing seek is
68  * performed on it, the running time is reset to 0. When the pipeline is
69  * set from PLAYING to PAUSED, the current clock time is sampled and used to
70  * configure the base time for the elements when the pipeline is set
71  * to PLAYING again. The effect is that the running time (as the difference
72  * between the clock time and the base time) will count how much time was spent
73  * in the PLAYING state. This default behaviour can be changed with the
74  * gst_element_set_start_time() method.
75  */
76
77 #include "gst_private.h"
78 #include "gsterror.h"
79 #include "gst-i18n-lib.h"
80
81 #include "gstpipeline.h"
82 #include "gstinfo.h"
83 #include "gstsystemclock.h"
84 #include "gstutils.h"
85
86 GST_DEBUG_CATEGORY_STATIC (pipeline_debug);
87 #define GST_CAT_DEFAULT pipeline_debug
88
89 /* Pipeline signals and args */
90 enum
91 {
92   /* FILL ME */
93   LAST_SIGNAL
94 };
95
96 #define DEFAULT_DELAY           0
97 #define DEFAULT_AUTO_FLUSH_BUS  TRUE
98 #define DEFAULT_LATENCY         GST_CLOCK_TIME_NONE
99
100 enum
101 {
102   PROP_0,
103   PROP_DELAY,
104   PROP_AUTO_FLUSH_BUS,
105   PROP_LATENCY
106 };
107
108 struct _GstPipelinePrivate
109 {
110   /* with LOCK */
111   gboolean auto_flush_bus;
112
113   /* when we need to update stream_time or clock when going back to
114    * PLAYING*/
115   GstClockTime last_start_time;
116   gboolean update_clock;
117
118   GstClockTime latency;
119 };
120
121
122 static void gst_pipeline_dispose (GObject * object);
123 static void gst_pipeline_set_property (GObject * object, guint prop_id,
124     const GValue * value, GParamSpec * pspec);
125 static void gst_pipeline_get_property (GObject * object, guint prop_id,
126     GValue * value, GParamSpec * pspec);
127
128 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
129 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
130     GstStateChange transition);
131
132 static void gst_pipeline_handle_message (GstBin * bin, GstMessage * message);
133 static gboolean gst_pipeline_do_latency (GstBin * bin);
134
135 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
136
137 #define _do_init \
138 { \
139   GST_DEBUG_CATEGORY_INIT (pipeline_debug, "pipeline", GST_DEBUG_BOLD, \
140       "debugging info for the 'pipeline' container element"); \
141 }
142
143 #define gst_pipeline_parent_class parent_class
144 G_DEFINE_TYPE_WITH_CODE (GstPipeline, gst_pipeline, GST_TYPE_BIN,
145     G_ADD_PRIVATE (GstPipeline) _do_init);
146
147 static void
148 gst_pipeline_class_init (GstPipelineClass * klass)
149 {
150   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
152   GstBinClass *gstbin_class = GST_BIN_CLASS (klass);
153
154   gobject_class->set_property = gst_pipeline_set_property;
155   gobject_class->get_property = gst_pipeline_get_property;
156
157   /**
158    * GstPipeline:delay:
159    *
160    * The expected delay needed for elements to spin up to the
161    * PLAYING state expressed in nanoseconds.
162    * see gst_pipeline_set_delay() for more information on this option.
163    **/
164   g_object_class_install_property (gobject_class, PROP_DELAY,
165       g_param_spec_uint64 ("delay", "Delay",
166           "Expected delay needed for elements "
167           "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
168           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169
170   /**
171    * GstPipeline:auto-flush-bus:
172    *
173    * Whether or not to automatically flush all messages on the
174    * pipeline's bus when going from READY to NULL state. Please see
175    * gst_pipeline_set_auto_flush_bus() for more information on this option.
176    **/
177   g_object_class_install_property (gobject_class, PROP_AUTO_FLUSH_BUS,
178       g_param_spec_boolean ("auto-flush-bus", "Auto Flush Bus",
179           "Whether to automatically flush the pipeline's bus when going "
180           "from READY into NULL state", DEFAULT_AUTO_FLUSH_BUS,
181           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183   /**
184    * GstPipeline:latency:
185    *
186    * Latency to configure on the pipeline. See gst_pipeline_set_latency().
187    *
188    * Since: 1.6
189    **/
190   g_object_class_install_property (gobject_class, PROP_LATENCY,
191       g_param_spec_uint64 ("latency", "Latency",
192           "Latency to configure on the pipeline", 0, G_MAXUINT64,
193           DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194
195   gobject_class->dispose = gst_pipeline_dispose;
196
197   gst_element_class_set_static_metadata (gstelement_class, "Pipeline object",
198       "Generic/Bin",
199       "Complete pipeline object",
200       "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
201
202   gstelement_class->change_state =
203       GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
204   gstelement_class->provide_clock =
205       GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
206   gstbin_class->handle_message =
207       GST_DEBUG_FUNCPTR (gst_pipeline_handle_message);
208   gstbin_class->do_latency = GST_DEBUG_FUNCPTR (gst_pipeline_do_latency);
209 }
210
211 static void
212 gst_pipeline_init (GstPipeline * pipeline)
213 {
214   GstBus *bus;
215
216   pipeline->priv = gst_pipeline_get_instance_private (pipeline);
217
218   /* set default property values */
219   pipeline->priv->auto_flush_bus = DEFAULT_AUTO_FLUSH_BUS;
220   pipeline->delay = DEFAULT_DELAY;
221   pipeline->priv->latency = DEFAULT_LATENCY;
222
223   /* create and set a default bus */
224   bus = gst_bus_new ();
225 #if 0
226   /* FIXME, disabled for 0.10.5 release as it caused to many regressions */
227   /* Start our bus in flushing if appropriate */
228   if (pipeline->priv->auto_flush_bus)
229     gst_bus_set_flushing (bus, TRUE);
230 #endif
231
232   gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
233   GST_DEBUG_OBJECT (pipeline, "set bus %" GST_PTR_FORMAT " on pipeline", bus);
234   gst_object_unref (bus);
235 }
236
237 static void
238 gst_pipeline_dispose (GObject * object)
239 {
240   GstPipeline *pipeline = GST_PIPELINE (object);
241   GstClock **clock_p = &pipeline->fixed_clock;
242
243   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "%p dispose", pipeline);
244
245   /* clear and unref any fixed clock */
246   gst_object_replace ((GstObject **) clock_p, NULL);
247
248   G_OBJECT_CLASS (parent_class)->dispose (object);
249 }
250
251 static void
252 gst_pipeline_set_property (GObject * object, guint prop_id,
253     const GValue * value, GParamSpec * pspec)
254 {
255   GstPipeline *pipeline = GST_PIPELINE (object);
256
257   switch (prop_id) {
258     case PROP_DELAY:
259       gst_pipeline_set_delay (pipeline, g_value_get_uint64 (value));
260       break;
261     case PROP_AUTO_FLUSH_BUS:
262       gst_pipeline_set_auto_flush_bus (pipeline, g_value_get_boolean (value));
263       break;
264     case PROP_LATENCY:
265       gst_pipeline_set_latency (pipeline, g_value_get_uint64 (value));
266       break;
267     default:
268       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269       break;
270   }
271 }
272
273 static void
274 gst_pipeline_get_property (GObject * object, guint prop_id,
275     GValue * value, GParamSpec * pspec)
276 {
277   GstPipeline *pipeline = GST_PIPELINE (object);
278
279   switch (prop_id) {
280     case PROP_DELAY:
281       g_value_set_uint64 (value, gst_pipeline_get_delay (pipeline));
282       break;
283     case PROP_AUTO_FLUSH_BUS:
284       g_value_set_boolean (value, gst_pipeline_get_auto_flush_bus (pipeline));
285       break;
286     case PROP_LATENCY:
287       g_value_set_uint64 (value, gst_pipeline_get_latency (pipeline));
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292   }
293 }
294
295 /* set the start_time to 0, this will cause us to select a new base_time and
296  * make the running_time start from 0 again. */
297 static void
298 reset_start_time (GstPipeline * pipeline, GstClockTime start_time)
299 {
300   GST_OBJECT_LOCK (pipeline);
301   if (GST_ELEMENT_START_TIME (pipeline) != GST_CLOCK_TIME_NONE) {
302     GST_DEBUG_OBJECT (pipeline, "reset start_time to 0");
303     GST_ELEMENT_START_TIME (pipeline) = start_time;
304     pipeline->priv->last_start_time = -1;
305   } else {
306     GST_DEBUG_OBJECT (pipeline, "application asked to not reset stream_time");
307   }
308   GST_OBJECT_UNLOCK (pipeline);
309 }
310
311 /**
312  * gst_pipeline_new:
313  * @name: (allow-none): name of new pipeline
314  *
315  * Create a new pipeline with the given name.
316  *
317  * Returns: (transfer floating): newly created GstPipeline
318  *
319  * MT safe.
320  */
321 GstElement *
322 gst_pipeline_new (const gchar * name)
323 {
324   return gst_element_factory_make ("pipeline", name);
325 }
326
327 /* takes a snapshot of the running_time of the pipeline and store this as the
328  * element start_time. This is the time we will set as the running_time of the
329  * pipeline when we go to PLAYING next. */
330 static void
331 pipeline_update_start_time (GstElement * element)
332 {
333   GstPipeline *pipeline = GST_PIPELINE_CAST (element);
334   GstClock *clock;
335
336   GST_OBJECT_LOCK (element);
337   if ((clock = element->clock)) {
338     GstClockTime now;
339
340     gst_object_ref (clock);
341     GST_OBJECT_UNLOCK (element);
342
343     /* calculate the time when we stopped */
344     now = gst_clock_get_time (clock);
345     gst_object_unref (clock);
346
347     GST_OBJECT_LOCK (element);
348     /* store the current running time */
349     if (GST_ELEMENT_START_TIME (pipeline) != GST_CLOCK_TIME_NONE) {
350       if (now != GST_CLOCK_TIME_NONE)
351         GST_ELEMENT_START_TIME (pipeline) = now - element->base_time;
352       else
353         GST_WARNING_OBJECT (element,
354             "Clock %s returned invalid time, can't calculate "
355             "running_time when going to the PAUSED state",
356             GST_OBJECT_NAME (clock));
357
358       /* we went to PAUSED, when going to PLAYING select clock and new
359        * base_time */
360       pipeline->priv->update_clock = TRUE;
361     }
362     GST_DEBUG_OBJECT (element,
363         "start_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
364         ", base_time %" GST_TIME_FORMAT,
365         GST_TIME_ARGS (GST_ELEMENT_START_TIME (pipeline)),
366         GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
367   }
368   GST_OBJECT_UNLOCK (element);
369 }
370
371 /* MT safe */
372 static GstStateChangeReturn
373 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
374 {
375   GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
376   GstPipeline *pipeline = GST_PIPELINE_CAST (element);
377   GstClock *clock;
378
379   switch (transition) {
380     case GST_STATE_CHANGE_NULL_TO_NULL:
381       break;
382     case GST_STATE_CHANGE_READY_TO_READY:
383       break;
384     case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
385       break;
386     case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
387       break;
388     case GST_STATE_CHANGE_NULL_TO_READY:
389       GST_OBJECT_LOCK (element);
390       if (element->bus)
391         gst_bus_set_flushing (element->bus, FALSE);
392       GST_OBJECT_UNLOCK (element);
393       break;
394     case GST_STATE_CHANGE_READY_TO_PAUSED:
395       GST_OBJECT_LOCK (element);
396       pipeline->priv->update_clock = TRUE;
397       GST_OBJECT_UNLOCK (element);
398
399       /* READY to PAUSED starts running_time from 0 */
400       reset_start_time (pipeline, 0);
401       break;
402     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
403     {
404       GstClockTime now, start_time, last_start_time, delay;
405       gboolean update_clock;
406       GstClock *cur_clock;
407
408       GST_DEBUG_OBJECT (element, "selecting clock and base_time");
409
410       GST_OBJECT_LOCK (element);
411       cur_clock = element->clock;
412       if (cur_clock)
413         gst_object_ref (cur_clock);
414       /* get the desired running_time of the first buffer aka the start_time */
415       start_time = GST_ELEMENT_START_TIME (pipeline);
416       last_start_time = pipeline->priv->last_start_time;
417       pipeline->priv->last_start_time = start_time;
418       /* see if we need to update the clock */
419       update_clock = pipeline->priv->update_clock;
420       pipeline->priv->update_clock = FALSE;
421       delay = pipeline->delay;
422       GST_OBJECT_UNLOCK (element);
423
424       /* running time changed, either with a PAUSED or a flush, we need to check
425        * if there is a new clock & update the base time */
426       /* only do this for top-level, however */
427       if (GST_OBJECT_PARENT (element) == NULL &&
428           (update_clock || last_start_time != start_time)) {
429         GST_DEBUG_OBJECT (pipeline, "Need to update start_time");
430
431         /* when going to PLAYING, select a clock when needed. If we just got
432          * flushed, we don't reselect the clock. */
433         if (update_clock) {
434           GST_DEBUG_OBJECT (pipeline, "Need to update clock.");
435           clock = gst_element_provide_clock (element);
436         } else {
437           GST_DEBUG_OBJECT (pipeline,
438               "Don't need to update clock, using old clock.");
439           /* only try to ref if cur_clock is not NULL */
440           if (cur_clock)
441             gst_object_ref (cur_clock);
442           clock = cur_clock;
443         }
444
445         if (clock) {
446           now = gst_clock_get_time (clock);
447         } else {
448           GST_DEBUG_OBJECT (pipeline, "no clock, using base time of NONE");
449           now = GST_CLOCK_TIME_NONE;
450         }
451
452         if (clock != cur_clock) {
453           /* now distribute the clock (which could be NULL). If some
454            * element refuses the clock, this will return FALSE and
455            * we effectively fail the state change. */
456           if (!gst_element_set_clock (element, clock))
457             goto invalid_clock;
458
459           /* if we selected and distributed a new clock, let the app
460            * know about it */
461           gst_element_post_message (element,
462               gst_message_new_new_clock (GST_OBJECT_CAST (element), clock));
463         }
464
465         if (clock)
466           gst_object_unref (clock);
467
468         if (start_time != GST_CLOCK_TIME_NONE && now != GST_CLOCK_TIME_NONE) {
469           GstClockTime new_base_time = now - start_time + delay;
470           GST_DEBUG_OBJECT (element,
471               "start_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
472               ", base_time %" GST_TIME_FORMAT,
473               GST_TIME_ARGS (start_time), GST_TIME_ARGS (now),
474               GST_TIME_ARGS (new_base_time));
475
476           gst_element_set_base_time (element, new_base_time);
477         } else {
478           GST_DEBUG_OBJECT (pipeline,
479               "NOT adjusting base_time because start_time is NONE");
480         }
481       } else {
482         GST_DEBUG_OBJECT (pipeline,
483             "NOT adjusting base_time because we selected one before");
484       }
485
486       if (cur_clock)
487         gst_object_unref (cur_clock);
488       break;
489     }
490     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
491     {
492       /* we take a start_time snapshot before calling the children state changes
493        * so that they know about when the pipeline PAUSED. */
494       pipeline_update_start_time (element);
495       break;
496     }
497     case GST_STATE_CHANGE_PAUSED_TO_READY:
498       reset_start_time (pipeline, 0);
499       break;
500     case GST_STATE_CHANGE_READY_TO_NULL:
501       break;
502   }
503
504   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
505
506   switch (transition) {
507     case GST_STATE_CHANGE_NULL_TO_NULL:
508       break;
509     case GST_STATE_CHANGE_READY_TO_READY:
510       break;
511     case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
512       break;
513     case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
514       break;
515     case GST_STATE_CHANGE_NULL_TO_READY:
516       break;
517     case GST_STATE_CHANGE_READY_TO_PAUSED:
518       break;
519     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
520       break;
521     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
522     {
523       /* Take a new snapshot of the start_time after calling the state change on
524        * all children. This will be the running_time of the pipeline when we go
525        * back to PLAYING */
526       pipeline_update_start_time (element);
527       break;
528     }
529     case GST_STATE_CHANGE_PAUSED_TO_READY:
530       break;
531     case GST_STATE_CHANGE_READY_TO_NULL:
532     {
533       GstBus *bus;
534       gboolean auto_flush;
535
536       /* grab some stuff before we release the lock to flush out the bus */
537       GST_OBJECT_LOCK (element);
538       if ((bus = element->bus))
539         gst_object_ref (bus);
540       auto_flush = pipeline->priv->auto_flush_bus;
541       GST_OBJECT_UNLOCK (element);
542
543       if (bus) {
544         if (auto_flush) {
545           gst_bus_set_flushing (bus, TRUE);
546         } else {
547           GST_INFO_OBJECT (element, "not flushing bus, auto-flushing disabled");
548         }
549         gst_object_unref (bus);
550       }
551       break;
552     }
553   }
554   return result;
555
556   /* ERRORS */
557 invalid_clock:
558   {
559     /* we generate this error when the selected clock was not
560      * accepted by some element */
561     GST_ELEMENT_ERROR (pipeline, CORE, CLOCK,
562         (_("Selected clock cannot be used in pipeline.")),
563         ("Pipeline cannot operate with selected clock"));
564     GST_DEBUG_OBJECT (pipeline,
565         "Pipeline cannot operate with selected clock %p", clock);
566     if (clock)
567       gst_object_unref (clock);
568     return GST_STATE_CHANGE_FAILURE;
569   }
570 }
571
572 /* intercept the bus messages from our children. We watch for the ASYNC_START
573  * message with is posted by the elements (sinks) that require a reset of the
574  * running_time after a flush. ASYNC_START also brings the pipeline back into
575  * the PAUSED, pending PAUSED state. When the ASYNC_DONE message is received the
576  * pipeline will redistribute the new base_time and will bring the elements back
577  * to the desired state of the pipeline. */
578 static void
579 gst_pipeline_handle_message (GstBin * bin, GstMessage * message)
580 {
581   GstPipeline *pipeline = GST_PIPELINE_CAST (bin);
582
583   switch (GST_MESSAGE_TYPE (message)) {
584     case GST_MESSAGE_RESET_TIME:
585     {
586       GstClockTime running_time;
587
588       gst_message_parse_reset_time (message, &running_time);
589
590       /* reset our running time if we need to distribute a new base_time to the
591        * children. */
592       reset_start_time (pipeline, running_time);
593       break;
594     }
595     case GST_MESSAGE_CLOCK_LOST:
596     {
597       GstClock *clock;
598
599       gst_message_parse_clock_lost (message, &clock);
600
601       GST_OBJECT_LOCK (bin);
602       if (clock == GST_ELEMENT_CAST (bin)->clock) {
603         GST_DEBUG_OBJECT (bin, "Used clock '%s' got lost",
604             GST_OBJECT_NAME (clock));
605         pipeline->priv->update_clock = TRUE;
606       }
607       GST_OBJECT_UNLOCK (bin);
608     }
609     default:
610       break;
611   }
612   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
613 }
614
615 static gboolean
616 gst_pipeline_do_latency (GstBin * bin)
617 {
618   GstPipeline *pipeline = GST_PIPELINE (bin);
619   GstQuery *query;
620   GstClockTime latency;
621   GstClockTime min_latency, max_latency;
622   gboolean res;
623
624   GST_OBJECT_LOCK (pipeline);
625   latency = pipeline->priv->latency;
626   GST_OBJECT_UNLOCK (pipeline);
627
628   if (latency == GST_CLOCK_TIME_NONE)
629     return GST_BIN_CLASS (parent_class)->do_latency (bin);
630
631   GST_DEBUG_OBJECT (pipeline, "querying latency");
632
633   query = gst_query_new_latency ();
634   if ((res = gst_element_query (GST_ELEMENT_CAST (pipeline), query))) {
635     gboolean live;
636
637     gst_query_parse_latency (query, &live, &min_latency, &max_latency);
638
639     GST_DEBUG_OBJECT (pipeline,
640         "got min latency %" GST_TIME_FORMAT ", max latency %"
641         GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
642         GST_TIME_ARGS (max_latency), live);
643
644     if (max_latency < min_latency) {
645       /* this is an impossible situation, some parts of the pipeline might not
646        * work correctly. We post a warning for now. */
647       GST_ELEMENT_WARNING (pipeline, CORE, CLOCK, (NULL),
648           ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
649               GST_TIME_FORMAT ". Add queues or other buffering elements.",
650               GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
651     }
652
653     if (latency < min_latency) {
654       /* This is a problematic situation as we will most likely drop lots of
655        * data if we configure a too low latency */
656       GST_ELEMENT_WARNING (pipeline, CORE, CLOCK, (NULL),
657           ("Configured latency is lower than detected minimum latency: configured %"
658               GST_TIME_FORMAT " < min %" GST_TIME_FORMAT,
659               GST_TIME_ARGS (latency), GST_TIME_ARGS (min_latency)));
660     }
661   } else {
662     /* this is not a real problem, we just don't configure any latency. */
663     GST_WARNING_OBJECT (pipeline, "failed to query latency");
664   }
665   gst_query_unref (query);
666
667
668   /* configure latency on elements */
669   res =
670       gst_element_send_event (GST_ELEMENT_CAST (pipeline),
671       gst_event_new_latency (latency));
672   if (res) {
673     GST_INFO_OBJECT (pipeline, "configured latency of %" GST_TIME_FORMAT,
674         GST_TIME_ARGS (latency));
675   } else {
676     GST_WARNING_OBJECT (pipeline,
677         "did not really configure latency of %" GST_TIME_FORMAT,
678         GST_TIME_ARGS (latency));
679   }
680
681   return res;
682 }
683
684 /**
685  * gst_pipeline_get_bus:
686  * @pipeline: a #GstPipeline
687  *
688  * Gets the #GstBus of @pipeline. The bus allows applications to receive
689  * #GstMessage packets.
690  *
691  * Returns: (transfer full): a #GstBus, unref after usage.
692  *
693  * MT safe.
694  */
695 GstBus *
696 gst_pipeline_get_bus (GstPipeline * pipeline)
697 {
698   return gst_element_get_bus (GST_ELEMENT_CAST (pipeline));
699 }
700
701 static GstClock *
702 gst_pipeline_provide_clock_func (GstElement * element)
703 {
704   GstClock *clock = NULL;
705   GstPipeline *pipeline = GST_PIPELINE (element);
706
707   /* if we have a fixed clock, use that one */
708   GST_OBJECT_LOCK (pipeline);
709   if (GST_OBJECT_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
710     clock = pipeline->fixed_clock;
711     if (clock)
712       gst_object_ref (clock);
713     GST_OBJECT_UNLOCK (pipeline);
714
715     GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
716         clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
717   } else {
718     GST_OBJECT_UNLOCK (pipeline);
719     /* let the parent bin select a clock */
720     clock =
721         GST_ELEMENT_CLASS (parent_class)->provide_clock (GST_ELEMENT
722         (pipeline));
723     /* no clock, use a system clock */
724     if (!clock) {
725       clock = gst_system_clock_obtain ();
726
727       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
728           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
729     } else {
730       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
731           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
732     }
733   }
734   return clock;
735 }
736
737 /**
738  * gst_pipeline_get_clock: (skip)
739  * @pipeline: a #GstPipeline
740  *
741  * Gets the current clock used by @pipeline. Users of object
742  * oriented languages should use gst_pipeline_get_pipeline_clock()
743  * to avoid confusion with gst_element_get_clock() which has a different behavior.
744  *
745  * Unlike gst_element_get_clock(), this function will always return a
746  * clock, even if the pipeline is not in the PLAYING state.
747  *
748  * Returns: (transfer full): a #GstClock, unref after usage.
749  */
750 GstClock *
751 gst_pipeline_get_clock (GstPipeline * pipeline)
752 {
753   return gst_pipeline_get_pipeline_clock (pipeline);
754 }
755
756 /**
757  * gst_pipeline_get_pipeline_clock:
758  * @pipeline: a #GstPipeline
759  *
760  * Gets the current clock used by @pipeline.
761  *
762  * Unlike gst_element_get_clock(), this function will always return a
763  * clock, even if the pipeline is not in the PLAYING state.
764  *
765  * Returns: (transfer full): a #GstClock, unref after usage.
766  *
767  * Since: 1.6
768  */
769 GstClock *
770 gst_pipeline_get_pipeline_clock (GstPipeline * pipeline)
771 {
772   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
773
774   return gst_pipeline_provide_clock_func (GST_ELEMENT_CAST (pipeline));
775 }
776
777
778 /**
779  * gst_pipeline_use_clock:
780  * @pipeline: a #GstPipeline
781  * @clock: (transfer none) (allow-none): the clock to use
782  *
783  * Force @pipeline to use the given @clock. The pipeline will
784  * always use the given clock even if new clock providers are added
785  * to this pipeline.
786  *
787  * If @clock is %NULL all clocking will be disabled which will make
788  * the pipeline run as fast as possible.
789  *
790  * MT safe.
791  */
792 void
793 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
794 {
795   GstClock **clock_p;
796
797   g_return_if_fail (GST_IS_PIPELINE (pipeline));
798
799   GST_OBJECT_LOCK (pipeline);
800   GST_OBJECT_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
801
802   clock_p = &pipeline->fixed_clock;
803   gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
804   GST_OBJECT_UNLOCK (pipeline);
805
806   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
807       (clock ? GST_OBJECT_NAME (clock) : "nil"));
808 }
809
810 /**
811  * gst_pipeline_set_clock: (skip)
812  * @pipeline: a #GstPipeline
813  * @clock: (transfer none): the clock to set
814  *
815  * Set the clock for @pipeline. The clock will be distributed
816  * to all the elements managed by the pipeline.
817  *
818  * Returns: %TRUE if the clock could be set on the pipeline. %FALSE if
819  *   some element did not accept the clock.
820  *
821  * MT safe.
822  */
823 gboolean
824 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
825 {
826   g_return_val_if_fail (pipeline != NULL, FALSE);
827   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), FALSE);
828
829   return
830       GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT_CAST (pipeline),
831       clock);
832 }
833
834 /**
835  * gst_pipeline_auto_clock:
836  * @pipeline: a #GstPipeline
837  *
838  * Let @pipeline select a clock automatically. This is the default
839  * behaviour.
840  *
841  * Use this function if you previous forced a fixed clock with
842  * gst_pipeline_use_clock() and want to restore the default
843  * pipeline clock selection algorithm.
844  *
845  * MT safe.
846  */
847 void
848 gst_pipeline_auto_clock (GstPipeline * pipeline)
849 {
850   GstClock **clock_p;
851
852   g_return_if_fail (pipeline != NULL);
853   g_return_if_fail (GST_IS_PIPELINE (pipeline));
854
855   GST_OBJECT_LOCK (pipeline);
856   GST_OBJECT_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
857
858   clock_p = &pipeline->fixed_clock;
859   gst_object_replace ((GstObject **) clock_p, NULL);
860   GST_OBJECT_UNLOCK (pipeline);
861
862   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");
863 }
864
865 /**
866  * gst_pipeline_set_delay:
867  * @pipeline: a #GstPipeline
868  * @delay: the delay
869  *
870  * Set the expected delay needed for all elements to perform the
871  * PAUSED to PLAYING state change. @delay will be added to the
872  * base time of the elements so that they wait an additional @delay
873  * amount of time before starting to process buffers and cannot be
874  * #GST_CLOCK_TIME_NONE.
875  *
876  * This option is used for tuning purposes and should normally not be
877  * used.
878  *
879  * MT safe.
880  */
881 void
882 gst_pipeline_set_delay (GstPipeline * pipeline, GstClockTime delay)
883 {
884   g_return_if_fail (GST_IS_PIPELINE (pipeline));
885   g_return_if_fail (delay != GST_CLOCK_TIME_NONE);
886
887   GST_OBJECT_LOCK (pipeline);
888   pipeline->delay = delay;
889   GST_OBJECT_UNLOCK (pipeline);
890 }
891
892 /**
893  * gst_pipeline_get_delay:
894  * @pipeline: a #GstPipeline
895  *
896  * Get the configured delay (see gst_pipeline_set_delay()).
897  *
898  * Returns: The configured delay.
899  *
900  * MT safe.
901  */
902 GstClockTime
903 gst_pipeline_get_delay (GstPipeline * pipeline)
904 {
905   GstClockTime res;
906
907   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
908
909   GST_OBJECT_LOCK (pipeline);
910   res = pipeline->delay;
911   GST_OBJECT_UNLOCK (pipeline);
912
913   return res;
914 }
915
916 /**
917  * gst_pipeline_set_auto_flush_bus:
918  * @pipeline: a #GstPipeline
919  * @auto_flush: whether or not to automatically flush the bus when
920  * the pipeline goes from READY to NULL state
921  *
922  * Usually, when a pipeline goes from READY to NULL state, it automatically
923  * flushes all pending messages on the bus, which is done for refcounting
924  * purposes, to break circular references.
925  *
926  * This means that applications that update state using (async) bus messages
927  * (e.g. do certain things when a pipeline goes from PAUSED to READY) might
928  * not get to see messages when the pipeline is shut down, because they might
929  * be flushed before they can be dispatched in the main thread. This behaviour
930  * can be disabled using this function.
931  *
932  * It is important that all messages on the bus are handled when the
933  * automatic flushing is disabled else memory leaks will be introduced.
934  *
935  * MT safe.
936  */
937 void
938 gst_pipeline_set_auto_flush_bus (GstPipeline * pipeline, gboolean auto_flush)
939 {
940   g_return_if_fail (GST_IS_PIPELINE (pipeline));
941
942   GST_OBJECT_LOCK (pipeline);
943   pipeline->priv->auto_flush_bus = auto_flush;
944   GST_OBJECT_UNLOCK (pipeline);
945 }
946
947 /**
948  * gst_pipeline_get_auto_flush_bus:
949  * @pipeline: a #GstPipeline
950  *
951  * Check if @pipeline will automatically flush messages when going to
952  * the NULL state.
953  *
954  * Returns: whether the pipeline will automatically flush its bus when
955  * going from READY to NULL state or not.
956  *
957  * MT safe.
958  */
959 gboolean
960 gst_pipeline_get_auto_flush_bus (GstPipeline * pipeline)
961 {
962   gboolean res;
963
964   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), FALSE);
965
966   GST_OBJECT_LOCK (pipeline);
967   res = pipeline->priv->auto_flush_bus;
968   GST_OBJECT_UNLOCK (pipeline);
969
970   return res;
971 }
972
973 /**
974  * gst_pipeline_set_latency:
975  * @pipeline: a #GstPipeline
976  * @latency: latency to configure
977  *
978  * Sets the latency that should be configured on the pipeline. Setting
979  * GST_CLOCK_TIME_NONE will restore the default behaviour of using the minimum
980  * latency from the LATENCY query. Setting this is usually not required and
981  * the pipeline will figure out an appropriate latency automatically.
982  *
983  * Setting a too low latency, especially lower than the minimum latency from
984  * the LATENCY query, will most likely cause the pipeline to fail.
985  *
986  * Since: 1.6
987  */
988 void
989 gst_pipeline_set_latency (GstPipeline * pipeline, GstClockTime latency)
990 {
991   gboolean changed;
992
993   g_return_if_fail (GST_IS_PIPELINE (pipeline));
994
995   GST_OBJECT_LOCK (pipeline);
996   changed = (pipeline->priv->latency != latency);
997   pipeline->priv->latency = latency;
998   GST_OBJECT_UNLOCK (pipeline);
999
1000   if (changed)
1001     gst_bin_recalculate_latency (GST_BIN_CAST (pipeline));
1002 }
1003
1004 /**
1005  * gst_pipeline_get_latency:
1006  * @pipeline: a #GstPipeline
1007  *
1008  * Gets the latency that should be configured on the pipeline. See
1009  * gst_pipeline_set_latency().
1010  *
1011  * Returns: Latency to configure on the pipeline or GST_CLOCK_TIME_NONE
1012  *
1013  * Since: 1.6
1014  */
1015
1016 GstClockTime
1017 gst_pipeline_get_latency (GstPipeline * pipeline)
1018 {
1019   GstClockTime latency;
1020
1021   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
1022
1023   GST_OBJECT_LOCK (pipeline);
1024   latency = pipeline->priv->latency;
1025   GST_OBJECT_UNLOCK (pipeline);
1026
1027   return latency;
1028 }