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