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 otherwise 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 = gst_bus_new ();
167 gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
168 GST_DEBUG_OBJECT (pipeline, "set bus %" GST_PTR_FORMAT " on pipeline", bus);
170 gst_object_unref (bus);
174 gst_pipeline_dispose (GObject * object)
176 GstPipeline *pipeline = GST_PIPELINE (object);
178 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
180 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
182 G_OBJECT_CLASS (parent_class)->dispose (object);
186 gst_pipeline_set_property (GObject * object, guint prop_id,
187 const GValue * value, GParamSpec * pspec)
189 GstPipeline *pipeline = GST_PIPELINE (object);
191 GST_OBJECT_LOCK (pipeline);
194 pipeline->delay = g_value_get_uint64 (value);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 GST_OBJECT_UNLOCK (pipeline);
204 gst_pipeline_get_property (GObject * object, guint prop_id,
205 GValue * value, GParamSpec * pspec)
207 GstPipeline *pipeline = GST_PIPELINE (object);
209 GST_OBJECT_LOCK (pipeline);
212 g_value_set_uint64 (value, pipeline->delay);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 GST_OBJECT_UNLOCK (pipeline);
222 do_pipeline_seek (GstElement * element, GstEvent * event)
227 gboolean was_playing = FALSE;
230 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
232 flush = flags & GST_SEEK_FLAG_FLUSH;
237 /* need to call _get_state() since a bin state is only updated
239 gst_element_get_state (element, &state, NULL, 0);
240 was_playing = state == GST_STATE_PLAYING;
243 gst_element_set_state (element, GST_STATE_PAUSED);
247 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
252 GST_OBJECT_LOCK (element);
253 need_reset = GST_PIPELINE (element)->stream_time != GST_CLOCK_TIME_NONE;
254 GST_OBJECT_UNLOCK (element);
256 /* need to reset the stream time to 0 after a flushing seek, unless the user
257 explicitly disabled this behavior by setting stream time to NONE */
259 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
262 /* and continue playing */
263 gst_element_set_state (element, GST_STATE_PLAYING);
268 /* sending a seek event on the pipeline pauses the pipeline if it
272 gst_pipeline_send_event (GstElement * element, GstEvent * event)
275 GstEventType event_type = GST_EVENT_TYPE (event);
277 switch (event_type) {
279 res = do_pipeline_seek (element, event);
282 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
291 * @name: name of new pipeline
293 * Create a new pipeline with the given name.
295 * Returns: newly created GstPipeline
300 gst_pipeline_new (const gchar * name)
302 return gst_element_factory_make ("pipeline", name);
306 static GstStateChangeReturn
307 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
309 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
310 GstPipeline *pipeline = GST_PIPELINE (element);
313 switch (transition) {
314 case GST_STATE_CHANGE_NULL_TO_READY:
315 GST_OBJECT_LOCK (element);
317 gst_bus_set_flushing (element->bus, FALSE);
318 GST_OBJECT_UNLOCK (element);
320 case GST_STATE_CHANGE_READY_TO_PAUSED:
322 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
324 GstClockTime new_base_time;
326 /* when going to playing, select a clock */
327 clock = gst_element_provide_clock (element);
330 GstClockTime start_time, stream_time, delay;
333 start_time = gst_clock_get_time (clock);
335 GST_OBJECT_LOCK (element);
336 new_clock = element->clock != clock;
337 stream_time = pipeline->stream_time;
338 delay = pipeline->delay;
339 GST_OBJECT_UNLOCK (element);
342 /* now distribute the clock (which could be NULL I guess) */
343 if (!gst_element_set_clock (element, clock))
346 /* if we selected a new clock, let the app know about it */
347 gst_element_post_message (element,
348 gst_message_new_new_clock (GST_OBJECT_CAST (element), clock));
351 if (stream_time != GST_CLOCK_TIME_NONE)
352 new_base_time = start_time - stream_time + delay;
354 new_base_time = GST_CLOCK_TIME_NONE;
356 gst_object_unref (clock);
358 GST_DEBUG ("no clock, using base time of 0");
362 if (new_base_time != GST_CLOCK_TIME_NONE)
363 gst_element_set_base_time (element, new_base_time);
365 GST_DEBUG_OBJECT (pipeline,
366 "NOT adjusting base time because stream time is NONE");
369 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
370 case GST_STATE_CHANGE_PAUSED_TO_READY:
371 case GST_STATE_CHANGE_READY_TO_NULL:
375 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
377 switch (transition) {
378 case GST_STATE_CHANGE_NULL_TO_READY:
380 case GST_STATE_CHANGE_READY_TO_PAUSED:
384 GST_OBJECT_LOCK (element);
385 need_reset = pipeline->stream_time != GST_CLOCK_TIME_NONE;
386 GST_OBJECT_UNLOCK (element);
389 gst_pipeline_set_new_stream_time (pipeline, 0);
392 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
394 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
395 GST_OBJECT_LOCK (element);
396 if ((clock = element->clock)) {
399 gst_object_ref (clock);
400 GST_OBJECT_UNLOCK (element);
402 /* calculate the time when we stopped */
403 now = gst_clock_get_time (clock);
404 gst_object_unref (clock);
406 GST_OBJECT_LOCK (element);
407 /* store the current stream time */
408 if (pipeline->stream_time != GST_CLOCK_TIME_NONE)
409 pipeline->stream_time = now - element->base_time;
410 GST_DEBUG_OBJECT (element,
411 "stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
412 ", base time %" GST_TIME_FORMAT,
413 GST_TIME_ARGS (pipeline->stream_time), GST_TIME_ARGS (now),
414 GST_TIME_ARGS (element->base_time));
416 GST_OBJECT_UNLOCK (element);
418 case GST_STATE_CHANGE_PAUSED_TO_READY:
420 case GST_STATE_CHANGE_READY_TO_NULL:
421 GST_OBJECT_LOCK (element);
423 gst_bus_set_flushing (element->bus, TRUE);
425 GST_OBJECT_UNLOCK (element);
432 GST_ELEMENT_ERROR (pipeline, CORE, CLOCK,
433 (_("Selected clock cannot be used in pipeline.")),
434 ("Pipeline cannot operate with selected clock"));
435 GST_DEBUG_OBJECT (pipeline,
436 "Pipeline cannot operate with selected clock %p", clock);
437 return GST_STATE_CHANGE_FAILURE;
442 * gst_pipeline_get_bus:
443 * @pipeline: the pipeline
445 * Gets the #GstBus of this pipeline.
452 gst_pipeline_get_bus (GstPipeline * pipeline)
454 return gst_element_get_bus (GST_ELEMENT (pipeline));
458 * gst_pipeline_set_new_stream_time:
459 * @pipeline: the pipeline
460 * @time: the new stream time to set
462 * Set the new stream time of the pipeline. The stream time is used to
463 * set the base time on the elements (see @gst_element_set_base_time())
464 * in the PAUSED->PLAYING state transition.
466 * Setting @time to #GST_CLOCK_TIME_NONE will disable the pipeline's management
467 * of element base time. The application will then be responsible for
468 * performing base time distribution. This is sometimes useful if you want to
469 * synchronize capture from multiple pipelines, and you can also ensure that the
470 * pipelines have the same clock.
475 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
477 g_return_if_fail (GST_IS_PIPELINE (pipeline));
479 GST_OBJECT_LOCK (pipeline);
480 pipeline->stream_time = time;
481 GST_OBJECT_UNLOCK (pipeline);
483 GST_DEBUG_OBJECT (pipeline, "set new stream_time to %" GST_TIME_FORMAT,
484 GST_TIME_ARGS (time));
486 if (time == GST_CLOCK_TIME_NONE)
487 GST_DEBUG_OBJECT (pipeline, "told not to adjust base time");
491 * gst_pipeline_get_last_stream_time:
492 * @pipeline: the pipeline
494 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
495 * the returned time is the stream time used to configure the elements
496 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
497 * time is the stream time when the pipeline was paused.
499 * Returns: a GstClockTime
504 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
508 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
510 GST_OBJECT_LOCK (pipeline);
511 result = pipeline->stream_time;
512 GST_OBJECT_UNLOCK (pipeline);
518 gst_pipeline_provide_clock_func (GstElement * element)
520 GstClock *clock = NULL;
521 GstPipeline *pipeline = GST_PIPELINE (element);
523 /* if we have a fixed clock, use that one */
524 GST_OBJECT_LOCK (pipeline);
525 if (GST_OBJECT_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
526 clock = pipeline->fixed_clock;
527 gst_object_ref (clock);
528 GST_OBJECT_UNLOCK (pipeline);
530 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
531 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
533 GST_OBJECT_UNLOCK (pipeline);
535 GST_ELEMENT_CLASS (parent_class)->
536 provide_clock (GST_ELEMENT (pipeline));
537 /* no clock, use a system clock */
539 clock = gst_system_clock_obtain ();
541 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
542 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
544 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
545 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
552 * gst_pipeline_get_clock:
553 * @pipeline: the pipeline
555 * Gets the current clock used by the pipeline.
557 * Returns: a GstClock
560 gst_pipeline_get_clock (GstPipeline * pipeline)
562 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
564 return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
569 * gst_pipeline_use_clock:
570 * @pipeline: the pipeline
571 * @clock: the clock to use
573 * Force the pipeline to use the given clock. The pipeline will
574 * always use the given clock even if new clock providers are added
580 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
582 g_return_if_fail (GST_IS_PIPELINE (pipeline));
584 GST_OBJECT_LOCK (pipeline);
585 GST_OBJECT_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
587 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
588 (GstObject *) clock);
589 GST_OBJECT_UNLOCK (pipeline);
591 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
592 (clock ? GST_OBJECT_NAME (clock) : "nil"));
596 * gst_pipeline_set_clock:
597 * @pipeline: the pipeline
598 * @clock: the clock to set
600 * Set the clock for the pipeline. The clock will be distributed
601 * to all the elements managed by the pipeline.
603 * Returns: TRUE if the clock could be set on the pipeline.
608 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
610 g_return_val_if_fail (pipeline != NULL, FALSE);
611 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), FALSE);
613 return GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline),
618 * gst_pipeline_auto_clock:
619 * @pipeline: the pipeline
621 * Let the pipeline select a clock automatically.
626 gst_pipeline_auto_clock (GstPipeline * pipeline)
628 g_return_if_fail (pipeline != NULL);
629 g_return_if_fail (GST_IS_PIPELINE (pipeline));
631 GST_OBJECT_LOCK (pipeline);
632 GST_OBJECT_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
634 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
635 GST_OBJECT_UNLOCK (pipeline);
637 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");