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.
24 * @short_description: Top-level bin with clocking and bus management functionality.
27 * In almost all cases, you'll want to use a GstPipeline when creating a filter
28 * graph. The GstPipeline will manage the selection and distribution of a global
29 * clock as well as provide a GstBus to the application.
31 * The pipeline will also use the selected clock to calculate the stream time of
34 * When sending a seek event to a GstPipeline, it will make sure that the
35 * pipeline is properly PAUSED and resumed as well as update the new stream time
38 * gst_pipeline_new() is used to create a pipeline. when you are done with
39 * the pipeline, use gst_object_unref() to free its resources including all
40 * added #GstElement objects (if not otherwiese referenced).
43 #include "gst_private.h"
45 #include "gstpipeline.h"
47 #include "gstsystemclock.h"
49 static GstElementDetails gst_pipeline_details =
50 GST_ELEMENT_DETAILS ("Pipeline object",
52 "Complete pipeline object",
53 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
55 /* Pipeline signals and args */
62 #define DEFAULT_DELAY 0
72 static void gst_pipeline_base_init (gpointer g_class);
73 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
74 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
76 static void gst_pipeline_dispose (GObject * object);
77 static void gst_pipeline_set_property (GObject * object, guint prop_id,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_pipeline_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
82 static gboolean gst_pipeline_send_event (GstElement * element,
85 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
86 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
87 GstStateChange transition);
89 static GstBinClass *parent_class = NULL;
91 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
94 gst_pipeline_get_type (void)
96 static GType pipeline_type = 0;
99 static const GTypeInfo pipeline_info = {
100 sizeof (GstPipelineClass),
101 gst_pipeline_base_init,
103 (GClassInitFunc) gst_pipeline_class_init,
106 sizeof (GstPipeline),
113 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
115 return pipeline_type;
119 gst_pipeline_base_init (gpointer g_class)
121 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
123 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
127 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
130 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
131 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
133 parent_class = g_type_class_peek_parent (klass);
135 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
136 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
138 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
139 g_param_spec_uint64 ("delay", "Delay",
140 "Expected delay needed for elements "
141 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
144 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
146 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
147 gstelement_class->change_state =
148 GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
149 gstelement_class->provide_clock =
150 GST_DEBUG_FUNCPTR (gst_pipeline_provide_clock_func);
154 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
156 GstPipeline *pipeline = GST_PIPELINE (instance);
159 pipeline->delay = DEFAULT_DELAY;
161 bus = g_object_new (gst_bus_get_type (), NULL);
162 gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
163 gst_object_unref (bus);
167 gst_pipeline_dispose (GObject * object)
169 GstPipeline *pipeline = GST_PIPELINE (object);
171 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
173 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
175 G_OBJECT_CLASS (parent_class)->dispose (object);
179 gst_pipeline_set_property (GObject * object, guint prop_id,
180 const GValue * value, GParamSpec * pspec)
182 GstPipeline *pipeline = GST_PIPELINE (object);
187 pipeline->delay = g_value_get_uint64 (value);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 GST_UNLOCK (pipeline);
197 gst_pipeline_get_property (GObject * object, guint prop_id,
198 GValue * value, GParamSpec * pspec)
200 GstPipeline *pipeline = GST_PIPELINE (object);
205 g_value_set_uint64 (value, pipeline->delay);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211 GST_UNLOCK (pipeline);
215 do_pipeline_seek (GstElement * element, GstEvent * event)
220 gboolean was_playing = FALSE;
223 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
225 flush = flags & GST_SEEK_FLAG_FLUSH;
231 GST_TIME_TO_TIMEVAL (0, timeout);
232 /* need to call _get_state() since a bin state is only updated
234 gst_element_get_state (element, &state, NULL, &timeout);
235 was_playing = state == GST_STATE_PLAYING;
238 gst_element_set_state (element, GST_STATE_PAUSED);
242 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
245 /* need to reset the stream time to 0 after a flushing seek */
246 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
248 /* and continue playing */
249 gst_element_set_state (element, GST_STATE_PLAYING);
254 /* sending a seek event on the pipeline pauses the pipeline if it
258 gst_pipeline_send_event (GstElement * element, GstEvent * event)
261 GstEventType event_type = GST_EVENT_TYPE (event);
263 switch (event_type) {
265 res = do_pipeline_seek (element, event);
268 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
277 * @name: name of new pipeline
279 * Create a new pipeline with the given name.
281 * Returns: newly created GstPipeline
286 gst_pipeline_new (const gchar * name)
288 return gst_element_factory_make ("pipeline", name);
292 static GstStateChangeReturn
293 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
295 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
296 GstPipeline *pipeline = GST_PIPELINE (element);
299 switch (transition) {
300 case GST_STATE_CHANGE_NULL_TO_READY:
303 gst_bus_set_flushing (element->bus, FALSE);
304 GST_UNLOCK (element);
306 case GST_STATE_CHANGE_READY_TO_PAUSED:
308 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
309 /* when going to playing, select a clock */
310 if ((clock = gst_element_provide_clock (element))) {
311 GstClockTime start_time;
315 start_time = gst_clock_get_time (clock);
318 new_clock = element->clock != clock;
319 element->base_time = start_time -
320 pipeline->stream_time + pipeline->delay;
321 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
322 GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
323 GST_TIME_ARGS (pipeline->stream_time),
324 GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
325 GST_UNLOCK (element);
327 /* now distribute the clock */
328 gst_element_set_clock (element, clock);
331 /* if we selected a new clock, let the app know about it */
332 gst_element_post_message (element,
333 gst_message_new_new_clock (GST_OBJECT_CAST (element), clock));
336 gst_object_unref (clock);
338 GST_DEBUG ("no clock, using base time of 0");
339 gst_element_set_base_time (element, 0);
342 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
343 case GST_STATE_CHANGE_PAUSED_TO_READY:
344 case GST_STATE_CHANGE_READY_TO_NULL:
348 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
350 switch (transition) {
351 case GST_STATE_CHANGE_NULL_TO_READY:
353 case GST_STATE_CHANGE_READY_TO_PAUSED:
354 gst_pipeline_set_new_stream_time (pipeline, 0);
356 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
358 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
360 if ((clock = element->clock)) {
363 gst_object_ref (clock);
364 GST_UNLOCK (element);
366 /* calculate the time when we stopped */
367 now = gst_clock_get_time (clock);
368 gst_object_unref (clock);
371 /* store the current stream time */
372 pipeline->stream_time = now - element->base_time;
373 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
374 ", base time %" GST_TIME_FORMAT,
375 GST_TIME_ARGS (pipeline->stream_time),
376 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
378 GST_UNLOCK (element);
380 case GST_STATE_CHANGE_PAUSED_TO_READY:
382 case GST_STATE_CHANGE_READY_TO_NULL:
385 gst_bus_set_flushing (element->bus, TRUE);
387 GST_UNLOCK (element);
394 * gst_pipeline_get_bus:
395 * @pipeline: the pipeline
397 * Gets the #GstBus of this pipeline.
404 gst_pipeline_get_bus (GstPipeline * pipeline)
406 return gst_element_get_bus (GST_ELEMENT (pipeline));
410 * gst_pipeline_set_new_stream_time:
411 * @pipeline: the pipeline
412 * @time: the new stream time to set
414 * Set the new stream time of the pipeline. The stream time is used to
415 * set the base time on the elements (see @gst_element_set_base_time())
416 * in the PAUSED->PLAYING state transition.
421 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
423 g_return_if_fail (GST_IS_PIPELINE (pipeline));
426 pipeline->stream_time = time;
427 GST_DEBUG ("%s: set new stream_time to %" GST_TIME_FORMAT,
428 GST_ELEMENT_NAME (pipeline), GST_TIME_ARGS (time));
429 GST_UNLOCK (pipeline);
433 * gst_pipeline_get_last_stream_time:
434 * @pipeline: the pipeline
436 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
437 * the returned time is the stream time used to configure the elements
438 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
439 * time is the stream time when the pipeline was paused.
441 * Returns: a GstClockTime
446 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
450 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
453 result = pipeline->stream_time;
454 GST_UNLOCK (pipeline);
460 gst_pipeline_provide_clock_func (GstElement * element)
462 GstClock *clock = NULL;
463 GstPipeline *pipeline = GST_PIPELINE (element);
465 /* if we have a fixed clock, use that one */
467 if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
468 clock = pipeline->fixed_clock;
469 gst_object_ref (clock);
470 GST_UNLOCK (pipeline);
472 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
473 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
475 GST_UNLOCK (pipeline);
477 GST_ELEMENT_CLASS (parent_class)->
478 provide_clock (GST_ELEMENT (pipeline));
479 /* no clock, use a system clock */
481 clock = gst_system_clock_obtain ();
483 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
484 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
486 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
487 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
494 * gst_pipeline_get_clock:
495 * @pipeline: the pipeline
497 * Gets the current clock used by the pipeline.
499 * Returns: a GstClock
502 gst_pipeline_get_clock (GstPipeline * pipeline)
504 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
506 return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
511 * gst_pipeline_use_clock:
512 * @pipeline: the pipeline
513 * @clock: the clock to use
515 * Force the pipeline to use the given clock. The pipeline will
516 * always use the given clock even if new clock providers are added
522 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
524 g_return_if_fail (GST_IS_PIPELINE (pipeline));
527 GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
529 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
530 (GstObject *) clock);
531 GST_UNLOCK (pipeline);
533 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
534 (clock ? GST_OBJECT_NAME (clock) : "nil"));
538 * gst_pipeline_set_clock:
539 * @pipeline: the pipeline
540 * @clock: the clock to set
542 * Set the clock for the pipeline. The clock will be distributed
543 * to all the elements managed by the pipeline.
548 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
550 g_return_if_fail (pipeline != NULL);
551 g_return_if_fail (GST_IS_PIPELINE (pipeline));
553 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
557 * gst_pipeline_auto_clock:
558 * @pipeline: the pipeline
560 * Let the pipeline select a clock automatically.
565 gst_pipeline_auto_clock (GstPipeline * pipeline)
567 g_return_if_fail (pipeline != NULL);
568 g_return_if_fail (GST_IS_PIPELINE (pipeline));
571 GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
573 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
574 GST_UNLOCK (pipeline);
576 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");