2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004,2005 Wim Taymans <wim@fluendo.com>
5 * gstpipeline.c: Overall pipeline management element
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.
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.
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Top-level bin with clocking and bus management
29 * In almost all cases, you'll want to use a GstPipeline when creating a filter
30 * graph. The GstPipeline will manage the selection and distribution of a
32 * clock as well as provide a GstBus to the application.
34 * The pipeline will also use the selected clock to calculate the stream time
37 * When sending a seek event to a GstPipeline, it will make sure that the
38 * pipeline is properly PAUSED and resumed as well as update the new stream
39 * time after the seek.
41 * gst_pipeline_new() is used to create a pipeline. when you are done with
42 * the pipeline, use gst_object_unref() to free its resources including all
43 * added #GstElement objects (if not otherwiese referenced).
46 #include "gst_private.h"
48 #include "gst-i18n-lib.h"
50 #include "gstpipeline.h"
52 #include "gstsystemclock.h"
54 static GstElementDetails gst_pipeline_details =
55 GST_ELEMENT_DETAILS ("Pipeline object",
57 "Complete pipeline object",
58 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
60 /* Pipeline signals and args */
67 #define DEFAULT_DELAY 0
77 static void gst_pipeline_base_init (gpointer g_class);
78 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
79 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
81 static void gst_pipeline_dispose (GObject * object);
82 static void gst_pipeline_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_pipeline_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
87 static gboolean gst_pipeline_send_event (GstElement * element,
90 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
91 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
92 GstStateChange transition);
94 static GstBinClass *parent_class = NULL;
96 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
99 gst_pipeline_get_type (void)
101 static GType pipeline_type = 0;
103 if (!pipeline_type) {
104 static const GTypeInfo pipeline_info = {
105 sizeof (GstPipelineClass),
106 gst_pipeline_base_init,
108 (GClassInitFunc) gst_pipeline_class_init,
111 sizeof (GstPipeline),
118 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
120 return pipeline_type;
124 gst_pipeline_base_init (gpointer g_class)
126 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
128 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
132 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
135 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
136 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
138 parent_class = g_type_class_peek_parent (klass);
140 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
141 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
143 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
144 g_param_spec_uint64 ("delay", "Delay",
145 "Expected delay needed for elements "
146 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
149 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
151 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
152 gstelement_class->change_state =
153 GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
154 gstelement_class->provide_clock =
155 GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
159 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
161 GstPipeline *pipeline = GST_PIPELINE (instance);
164 pipeline->delay = DEFAULT_DELAY;
166 bus = g_object_new (gst_bus_get_type (), NULL);
167 gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
168 gst_object_unref (bus);
172 gst_pipeline_dispose (GObject * object)
174 GstPipeline *pipeline = GST_PIPELINE (object);
176 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
178 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
180 G_OBJECT_CLASS (parent_class)->dispose (object);
184 gst_pipeline_set_property (GObject * object, guint prop_id,
185 const GValue * value, GParamSpec * pspec)
187 GstPipeline *pipeline = GST_PIPELINE (object);
189 GST_OBJECT_LOCK (pipeline);
192 pipeline->delay = g_value_get_uint64 (value);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 GST_OBJECT_UNLOCK (pipeline);
202 gst_pipeline_get_property (GObject * object, guint prop_id,
203 GValue * value, GParamSpec * pspec)
205 GstPipeline *pipeline = GST_PIPELINE (object);
207 GST_OBJECT_LOCK (pipeline);
210 g_value_set_uint64 (value, pipeline->delay);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 GST_OBJECT_UNLOCK (pipeline);
220 do_pipeline_seek (GstElement * element, GstEvent * event)
225 gboolean was_playing = FALSE;
228 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
230 flush = flags & GST_SEEK_FLAG_FLUSH;
235 /* need to call _get_state() since a bin state is only updated
237 gst_element_get_state (element, &state, NULL, 0);
238 was_playing = state == GST_STATE_PLAYING;
241 gst_element_set_state (element, GST_STATE_PAUSED);
245 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
250 GST_OBJECT_LOCK (element);
251 need_reset = GST_PIPELINE (element)->stream_time != GST_CLOCK_TIME_NONE;
252 GST_OBJECT_UNLOCK (element);
254 /* need to reset the stream time to 0 after a flushing seek, unless the user
255 explicitly disabled this behavior by setting stream time to NONE */
257 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
260 /* and continue playing */
261 gst_element_set_state (element, GST_STATE_PLAYING);
266 /* sending a seek event on the pipeline pauses the pipeline if it
270 gst_pipeline_send_event (GstElement * element, GstEvent * event)
273 GstEventType event_type = GST_EVENT_TYPE (event);
275 switch (event_type) {
277 res = do_pipeline_seek (element, event);
280 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
289 * @name: name of new pipeline
291 * Create a new pipeline with the given name.
293 * Returns: newly created GstPipeline
298 gst_pipeline_new (const gchar * name)
300 return gst_element_factory_make ("pipeline", name);
304 static GstStateChangeReturn
305 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
307 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
308 GstPipeline *pipeline = GST_PIPELINE (element);
311 switch (transition) {
312 case GST_STATE_CHANGE_NULL_TO_READY:
313 GST_OBJECT_LOCK (element);
315 gst_bus_set_flushing (element->bus, FALSE);
316 GST_OBJECT_UNLOCK (element);
318 case GST_STATE_CHANGE_READY_TO_PAUSED:
320 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
322 GstClockTime new_base_time;
324 /* when going to playing, select a clock */
325 clock = gst_element_provide_clock (element);
328 GstClockTime start_time, stream_time, delay;
331 start_time = gst_clock_get_time (clock);
333 GST_OBJECT_LOCK (element);
334 new_clock = element->clock != clock;
335 stream_time = pipeline->stream_time;
336 delay = pipeline->delay;
337 GST_OBJECT_UNLOCK (element);
340 /* now distribute the clock (which could be NULL I guess) */
341 if (!gst_element_set_clock (element, clock))
344 /* if we selected a new clock, let the app know about it */
345 gst_element_post_message (element,
346 gst_message_new_new_clock (GST_OBJECT_CAST (element), clock));
349 if (stream_time != GST_CLOCK_TIME_NONE)
350 new_base_time = start_time - stream_time + delay;
352 new_base_time = GST_CLOCK_TIME_NONE;
354 gst_object_unref (clock);
356 GST_DEBUG ("no clock, using base time of 0");
360 if (new_base_time != GST_CLOCK_TIME_NONE)
361 gst_element_set_base_time (element, new_base_time);
363 GST_DEBUG_OBJECT (pipeline,
364 "NOT adjusting base time because stream time is NONE");
367 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
368 case GST_STATE_CHANGE_PAUSED_TO_READY:
369 case GST_STATE_CHANGE_READY_TO_NULL:
373 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
375 switch (transition) {
376 case GST_STATE_CHANGE_NULL_TO_READY:
378 case GST_STATE_CHANGE_READY_TO_PAUSED:
382 GST_OBJECT_LOCK (element);
383 need_reset = pipeline->stream_time != GST_CLOCK_TIME_NONE;
384 GST_OBJECT_UNLOCK (element);
387 gst_pipeline_set_new_stream_time (pipeline, 0);
390 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
392 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
393 GST_OBJECT_LOCK (element);
394 if ((clock = element->clock)) {
397 gst_object_ref (clock);
398 GST_OBJECT_UNLOCK (element);
400 /* calculate the time when we stopped */
401 now = gst_clock_get_time (clock);
402 gst_object_unref (clock);
404 GST_OBJECT_LOCK (element);
405 /* store the current stream time */
406 if (pipeline->stream_time != GST_CLOCK_TIME_NONE)
407 pipeline->stream_time = now - element->base_time;
408 GST_DEBUG_OBJECT (element,
409 "stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
410 ", base time %" GST_TIME_FORMAT,
411 GST_TIME_ARGS (pipeline->stream_time), GST_TIME_ARGS (now),
412 GST_TIME_ARGS (element->base_time));
414 GST_OBJECT_UNLOCK (element);
416 case GST_STATE_CHANGE_PAUSED_TO_READY:
418 case GST_STATE_CHANGE_READY_TO_NULL:
419 GST_OBJECT_LOCK (element);
421 gst_bus_set_flushing (element->bus, TRUE);
423 GST_OBJECT_UNLOCK (element);
430 GST_ELEMENT_ERROR (pipeline, CORE, CLOCK,
431 (_("Selected clock cannot be used in pipeline.")),
432 ("Pipeline cannot operate with selected clock"));
433 GST_DEBUG_OBJECT (pipeline,
434 "Pipeline cannot operate with selected clock %p", clock);
435 return GST_STATE_CHANGE_FAILURE;
440 * gst_pipeline_get_bus:
441 * @pipeline: the pipeline
443 * Gets the #GstBus of this pipeline.
450 gst_pipeline_get_bus (GstPipeline * pipeline)
452 return gst_element_get_bus (GST_ELEMENT (pipeline));
456 * gst_pipeline_set_new_stream_time:
457 * @pipeline: the pipeline
458 * @time: the new stream time to set
460 * Set the new stream time of the pipeline. The stream time is used to
461 * set the base time on the elements (see @gst_element_set_base_time())
462 * in the PAUSED->PLAYING state transition.
464 * Setting @time to #GST_CLOCK_TIME_NONE will disable the pipeline's management
465 * of element base time. The application will then be responsible for
466 * performing base time distribution. This is sometimes useful if you want to
467 * synchronize capture from multiple pipelines, and you can also ensure that the
468 * pipelines have the same clock.
473 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
475 g_return_if_fail (GST_IS_PIPELINE (pipeline));
477 GST_OBJECT_LOCK (pipeline);
478 pipeline->stream_time = time;
479 GST_OBJECT_UNLOCK (pipeline);
481 GST_DEBUG_OBJECT (pipeline, "set new stream_time to %" GST_TIME_FORMAT,
482 GST_TIME_ARGS (time));
484 if (time == GST_CLOCK_TIME_NONE)
485 GST_DEBUG_OBJECT (pipeline, "told not to adjust base time");
489 * gst_pipeline_get_last_stream_time:
490 * @pipeline: the pipeline
492 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
493 * the returned time is the stream time used to configure the elements
494 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
495 * time is the stream time when the pipeline was paused.
497 * Returns: a GstClockTime
502 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
506 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
508 GST_OBJECT_LOCK (pipeline);
509 result = pipeline->stream_time;
510 GST_OBJECT_UNLOCK (pipeline);
516 gst_pipeline_provide_clock_func (GstElement * element)
518 GstClock *clock = NULL;
519 GstPipeline *pipeline = GST_PIPELINE (element);
521 /* if we have a fixed clock, use that one */
522 GST_OBJECT_LOCK (pipeline);
523 if (GST_OBJECT_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
524 clock = pipeline->fixed_clock;
525 gst_object_ref (clock);
526 GST_OBJECT_UNLOCK (pipeline);
528 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
529 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
531 GST_OBJECT_UNLOCK (pipeline);
533 GST_ELEMENT_CLASS (parent_class)->
534 provide_clock (GST_ELEMENT (pipeline));
535 /* no clock, use a system clock */
537 clock = gst_system_clock_obtain ();
539 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
540 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
542 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
543 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
550 * gst_pipeline_get_clock:
551 * @pipeline: the pipeline
553 * Gets the current clock used by the pipeline.
555 * Returns: a GstClock
558 gst_pipeline_get_clock (GstPipeline * pipeline)
560 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
562 return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
567 * gst_pipeline_use_clock:
568 * @pipeline: the pipeline
569 * @clock: the clock to use
571 * Force the pipeline to use the given clock. The pipeline will
572 * always use the given clock even if new clock providers are added
578 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
580 g_return_if_fail (GST_IS_PIPELINE (pipeline));
582 GST_OBJECT_LOCK (pipeline);
583 GST_OBJECT_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
585 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
586 (GstObject *) clock);
587 GST_OBJECT_UNLOCK (pipeline);
589 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
590 (clock ? GST_OBJECT_NAME (clock) : "nil"));
594 * gst_pipeline_set_clock:
595 * @pipeline: the pipeline
596 * @clock: the clock to set
598 * Set the clock for the pipeline. The clock will be distributed
599 * to all the elements managed by the pipeline.
601 * Returns: TRUE if the clock could be set on the pipeline.
606 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
608 g_return_val_if_fail (pipeline != NULL, FALSE);
609 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), FALSE);
611 return GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline),
616 * gst_pipeline_auto_clock:
617 * @pipeline: the pipeline
619 * Let the pipeline select a clock automatically.
624 gst_pipeline_auto_clock (GstPipeline * pipeline)
626 g_return_if_fail (pipeline != NULL);
627 g_return_if_fail (GST_IS_PIPELINE (pipeline));
629 GST_OBJECT_LOCK (pipeline);
630 GST_OBJECT_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
632 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
633 GST_OBJECT_UNLOCK (pipeline);
635 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");