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 "gstpipeline.h"
50 #include "gstsystemclock.h"
52 static GstElementDetails gst_pipeline_details =
53 GST_ELEMENT_DETAILS ("Pipeline object",
55 "Complete pipeline object",
56 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
58 /* Pipeline signals and args */
65 #define DEFAULT_DELAY 0
75 static void gst_pipeline_base_init (gpointer g_class);
76 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
77 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
79 static void gst_pipeline_dispose (GObject * object);
80 static void gst_pipeline_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_pipeline_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static gboolean gst_pipeline_send_event (GstElement * element,
88 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
89 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
90 GstStateChange transition);
92 static GstBinClass *parent_class = NULL;
94 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
97 gst_pipeline_get_type (void)
99 static GType pipeline_type = 0;
101 if (!pipeline_type) {
102 static const GTypeInfo pipeline_info = {
103 sizeof (GstPipelineClass),
104 gst_pipeline_base_init,
106 (GClassInitFunc) gst_pipeline_class_init,
109 sizeof (GstPipeline),
116 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
118 return pipeline_type;
122 gst_pipeline_base_init (gpointer g_class)
124 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
126 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
130 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
133 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
134 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
136 parent_class = g_type_class_peek_parent (klass);
138 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
139 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
141 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
142 g_param_spec_uint64 ("delay", "Delay",
143 "Expected delay needed for elements "
144 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
147 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
149 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
150 gstelement_class->change_state =
151 GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
152 gstelement_class->provide_clock =
153 GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
157 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
159 GstPipeline *pipeline = GST_PIPELINE (instance);
162 pipeline->delay = DEFAULT_DELAY;
164 bus = g_object_new (gst_bus_get_type (), NULL);
165 gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
166 gst_object_unref (bus);
170 gst_pipeline_dispose (GObject * object)
172 GstPipeline *pipeline = GST_PIPELINE (object);
174 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
176 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
178 G_OBJECT_CLASS (parent_class)->dispose (object);
182 gst_pipeline_set_property (GObject * object, guint prop_id,
183 const GValue * value, GParamSpec * pspec)
185 GstPipeline *pipeline = GST_PIPELINE (object);
187 GST_OBJECT_LOCK (pipeline);
190 pipeline->delay = g_value_get_uint64 (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 GST_OBJECT_UNLOCK (pipeline);
200 gst_pipeline_get_property (GObject * object, guint prop_id,
201 GValue * value, GParamSpec * pspec)
203 GstPipeline *pipeline = GST_PIPELINE (object);
205 GST_OBJECT_LOCK (pipeline);
208 g_value_set_uint64 (value, pipeline->delay);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 GST_OBJECT_UNLOCK (pipeline);
218 do_pipeline_seek (GstElement * element, GstEvent * event)
223 gboolean was_playing = FALSE;
226 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
228 flush = flags & GST_SEEK_FLAG_FLUSH;
233 /* need to call _get_state() since a bin state is only updated
235 gst_element_get_state (element, &state, NULL, 0);
236 was_playing = state == GST_STATE_PLAYING;
239 gst_element_set_state (element, GST_STATE_PAUSED);
243 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
248 GST_OBJECT_LOCK (element);
249 need_reset = GST_PIPELINE (element)->stream_time != GST_CLOCK_TIME_NONE;
250 GST_OBJECT_UNLOCK (element);
252 /* need to reset the stream time to 0 after a flushing seek, unless the user
253 explicitly disabled this behavior by setting stream time to NONE */
255 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
258 /* and continue playing */
259 gst_element_set_state (element, GST_STATE_PLAYING);
264 /* sending a seek event on the pipeline pauses the pipeline if it
268 gst_pipeline_send_event (GstElement * element, GstEvent * event)
271 GstEventType event_type = GST_EVENT_TYPE (event);
273 switch (event_type) {
275 res = do_pipeline_seek (element, event);
278 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
287 * @name: name of new pipeline
289 * Create a new pipeline with the given name.
291 * Returns: newly created GstPipeline
296 gst_pipeline_new (const gchar * name)
298 return gst_element_factory_make ("pipeline", name);
302 static GstStateChangeReturn
303 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
305 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
306 GstPipeline *pipeline = GST_PIPELINE (element);
309 switch (transition) {
310 case GST_STATE_CHANGE_NULL_TO_READY:
311 GST_OBJECT_LOCK (element);
313 gst_bus_set_flushing (element->bus, FALSE);
314 GST_OBJECT_UNLOCK (element);
316 case GST_STATE_CHANGE_READY_TO_PAUSED:
318 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
320 GstClockTime new_base_time;
322 /* when going to playing, select a clock */
323 clock = gst_element_provide_clock (element);
326 GstClockTime start_time, stream_time, delay;
329 start_time = gst_clock_get_time (clock);
331 GST_OBJECT_LOCK (element);
332 new_clock = element->clock != clock;
333 stream_time = pipeline->stream_time;
334 delay = pipeline->delay;
335 GST_OBJECT_UNLOCK (element);
338 /* now distribute the clock (which could be NULL I guess) */
339 gst_element_set_clock (element, clock);
341 /* if we selected a new clock, let the app know about it */
342 gst_element_post_message (element,
343 gst_message_new_new_clock (GST_OBJECT_CAST (element), clock));
346 if (stream_time != GST_CLOCK_TIME_NONE)
347 new_base_time = start_time - stream_time + delay;
349 new_base_time = GST_CLOCK_TIME_NONE;
351 gst_object_unref (clock);
353 GST_DEBUG ("no clock, using base time of 0");
357 if (new_base_time != GST_CLOCK_TIME_NONE)
358 gst_element_set_base_time (element, new_base_time);
360 GST_DEBUG_OBJECT (pipeline,
361 "NOT adjusting base time because stream time is NONE");
364 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
365 case GST_STATE_CHANGE_PAUSED_TO_READY:
366 case GST_STATE_CHANGE_READY_TO_NULL:
370 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
372 switch (transition) {
373 case GST_STATE_CHANGE_NULL_TO_READY:
375 case GST_STATE_CHANGE_READY_TO_PAUSED:
379 GST_OBJECT_LOCK (element);
380 need_reset = pipeline->stream_time != GST_CLOCK_TIME_NONE;
381 GST_OBJECT_UNLOCK (element);
384 gst_pipeline_set_new_stream_time (pipeline, 0);
387 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
389 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
390 GST_OBJECT_LOCK (element);
391 if ((clock = element->clock)) {
394 gst_object_ref (clock);
395 GST_OBJECT_UNLOCK (element);
397 /* calculate the time when we stopped */
398 now = gst_clock_get_time (clock);
399 gst_object_unref (clock);
401 GST_OBJECT_LOCK (element);
402 /* store the current stream time */
403 if (pipeline->stream_time != GST_CLOCK_TIME_NONE)
404 pipeline->stream_time = now - element->base_time;
405 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
406 ", base time %" GST_TIME_FORMAT,
407 GST_TIME_ARGS (pipeline->stream_time),
408 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
410 GST_OBJECT_UNLOCK (element);
412 case GST_STATE_CHANGE_PAUSED_TO_READY:
414 case GST_STATE_CHANGE_READY_TO_NULL:
415 GST_OBJECT_LOCK (element);
417 gst_bus_set_flushing (element->bus, TRUE);
419 GST_OBJECT_UNLOCK (element);
426 * gst_pipeline_get_bus:
427 * @pipeline: the pipeline
429 * Gets the #GstBus of this pipeline.
436 gst_pipeline_get_bus (GstPipeline * pipeline)
438 return gst_element_get_bus (GST_ELEMENT (pipeline));
442 * gst_pipeline_set_new_stream_time:
443 * @pipeline: the pipeline
444 * @time: the new stream time to set
446 * Set the new stream time of the pipeline. The stream time is used to
447 * set the base time on the elements (see @gst_element_set_base_time())
448 * in the PAUSED->PLAYING state transition.
450 * Setting @time to #GST_CLOCK_TIME_NONE will disable the pipeline's management
451 * of element base time. The application will then be responsible for
452 * performing base time distribution. This is sometimes useful if you want to
453 * synchronize capture from multiple pipelines, and you can also ensure that the
454 * pipelines have the same clock.
459 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
461 g_return_if_fail (GST_IS_PIPELINE (pipeline));
463 GST_OBJECT_LOCK (pipeline);
464 pipeline->stream_time = time;
465 GST_OBJECT_UNLOCK (pipeline);
467 GST_DEBUG_OBJECT (pipeline, "set new stream_time to %" GST_TIME_FORMAT,
468 GST_TIME_ARGS (time));
470 if (time == GST_CLOCK_TIME_NONE)
471 GST_DEBUG_OBJECT (pipeline, "told not to adjust base time");
475 * gst_pipeline_get_last_stream_time:
476 * @pipeline: the pipeline
478 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
479 * the returned time is the stream time used to configure the elements
480 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
481 * time is the stream time when the pipeline was paused.
483 * Returns: a GstClockTime
488 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
492 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
494 GST_OBJECT_LOCK (pipeline);
495 result = pipeline->stream_time;
496 GST_OBJECT_UNLOCK (pipeline);
502 gst_pipeline_provide_clock_func (GstElement * element)
504 GstClock *clock = NULL;
505 GstPipeline *pipeline = GST_PIPELINE (element);
507 /* if we have a fixed clock, use that one */
508 GST_OBJECT_LOCK (pipeline);
509 if (GST_OBJECT_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
510 clock = pipeline->fixed_clock;
511 gst_object_ref (clock);
512 GST_OBJECT_UNLOCK (pipeline);
514 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
515 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
517 GST_OBJECT_UNLOCK (pipeline);
519 GST_ELEMENT_CLASS (parent_class)->
520 provide_clock (GST_ELEMENT (pipeline));
521 /* no clock, use a system clock */
523 clock = gst_system_clock_obtain ();
525 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
526 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
528 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
529 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
536 * gst_pipeline_get_clock:
537 * @pipeline: the pipeline
539 * Gets the current clock used by the pipeline.
541 * Returns: a GstClock
544 gst_pipeline_get_clock (GstPipeline * pipeline)
546 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
548 return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
553 * gst_pipeline_use_clock:
554 * @pipeline: the pipeline
555 * @clock: the clock to use
557 * Force the pipeline to use the given clock. The pipeline will
558 * always use the given clock even if new clock providers are added
564 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
566 g_return_if_fail (GST_IS_PIPELINE (pipeline));
568 GST_OBJECT_LOCK (pipeline);
569 GST_OBJECT_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
571 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
572 (GstObject *) clock);
573 GST_OBJECT_UNLOCK (pipeline);
575 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
576 (clock ? GST_OBJECT_NAME (clock) : "nil"));
580 * gst_pipeline_set_clock:
581 * @pipeline: the pipeline
582 * @clock: the clock to set
584 * Set the clock for the pipeline. The clock will be distributed
585 * to all the elements managed by the pipeline.
590 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
592 g_return_if_fail (pipeline != NULL);
593 g_return_if_fail (GST_IS_PIPELINE (pipeline));
595 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
599 * gst_pipeline_auto_clock:
600 * @pipeline: the pipeline
602 * Let the pipeline select a clock automatically.
607 gst_pipeline_auto_clock (GstPipeline * pipeline)
609 g_return_if_fail (pipeline != NULL);
610 g_return_if_fail (GST_IS_PIPELINE (pipeline));
612 GST_OBJECT_LOCK (pipeline);
613 GST_OBJECT_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
615 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
616 GST_OBJECT_UNLOCK (pipeline);
618 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");