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
63 #define DEFAULT_PLAY_TIMEOUT (2*GST_SECOND)
73 static void gst_pipeline_base_init (gpointer g_class);
74 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
75 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
77 static void gst_pipeline_dispose (GObject * object);
78 static void gst_pipeline_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
80 static void gst_pipeline_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
83 static gboolean gst_pipeline_send_event (GstElement * element,
86 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
87 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
88 GstStateChange transition);
90 static GstBinClass *parent_class = NULL;
92 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
95 gst_pipeline_get_type (void)
97 static GType pipeline_type = 0;
100 static const GTypeInfo pipeline_info = {
101 sizeof (GstPipelineClass),
102 gst_pipeline_base_init,
104 (GClassInitFunc) gst_pipeline_class_init,
107 sizeof (GstPipeline),
114 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
116 return pipeline_type;
120 gst_pipeline_base_init (gpointer g_class)
122 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
124 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
128 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
131 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
132 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
134 parent_class = g_type_class_peek_parent (klass);
136 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
137 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
139 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
140 g_param_spec_uint64 ("delay", "Delay",
141 "Expected delay needed for elements "
142 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
144 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PLAY_TIMEOUT,
145 g_param_spec_uint64 ("play-timeout", "Play Timeout",
146 "Max timeout for going to PLAYING in nanoseconds", 0, G_MAXUINT64,
147 DEFAULT_PLAY_TIMEOUT, G_PARAM_READWRITE));
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;
165 pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
167 bus = g_object_new (gst_bus_get_type (), NULL);
168 gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
169 gst_object_unref (bus);
173 gst_pipeline_dispose (GObject * object)
175 GstPipeline *pipeline = GST_PIPELINE (object);
177 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
179 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
181 G_OBJECT_CLASS (parent_class)->dispose (object);
185 gst_pipeline_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec)
188 GstPipeline *pipeline = GST_PIPELINE (object);
193 pipeline->delay = g_value_get_uint64 (value);
195 case PROP_PLAY_TIMEOUT:
196 pipeline->play_timeout = g_value_get_uint64 (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 GST_UNLOCK (pipeline);
206 gst_pipeline_get_property (GObject * object, guint prop_id,
207 GValue * value, GParamSpec * pspec)
209 GstPipeline *pipeline = GST_PIPELINE (object);
214 g_value_set_uint64 (value, pipeline->delay);
216 case PROP_PLAY_TIMEOUT:
217 g_value_set_uint64 (value, pipeline->play_timeout);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 GST_UNLOCK (pipeline);
227 do_pipeline_seek (GstElement * element, GstEvent * event)
232 gboolean was_playing = FALSE;
235 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
237 flush = flags & GST_SEEK_FLAG_FLUSH;
243 GST_TIME_TO_TIMEVAL (0, timeout);
244 /* need to call _get_state() since a bin state is only updated
246 gst_element_get_state (element, &state, NULL, &timeout);
247 was_playing = state == GST_STATE_PLAYING;
250 gst_element_set_state (element, GST_STATE_PAUSED);
253 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
256 /* need to reset the stream time to 0 after a flushing seek */
257 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
259 /* and continue playing */
260 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);
309 GstClockTime play_timeout;
312 switch (transition) {
313 case GST_STATE_CHANGE_NULL_TO_READY:
316 gst_bus_set_flushing (element->bus, FALSE);
317 GST_UNLOCK (element);
319 case GST_STATE_CHANGE_READY_TO_PAUSED:
321 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
322 /* when going to playing, select a clock */
323 if ((clock = gst_element_provide_clock (element))) {
324 GstClockTime start_time;
328 start_time = gst_clock_get_time (clock);
331 new_clock = element->clock != clock;
332 element->base_time = start_time -
333 pipeline->stream_time + pipeline->delay;
334 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
335 GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
336 GST_TIME_ARGS (pipeline->stream_time),
337 GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
338 GST_UNLOCK (element);
340 /* now distribute the clock */
341 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 gst_object_unref (clock);
351 GST_DEBUG ("no clock, using base time of 0");
352 gst_element_set_base_time (element, 0);
355 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
356 case GST_STATE_CHANGE_PAUSED_TO_READY:
357 case GST_STATE_CHANGE_READY_TO_NULL:
361 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
363 switch (transition) {
364 case GST_STATE_CHANGE_NULL_TO_READY:
366 case GST_STATE_CHANGE_READY_TO_PAUSED:
367 gst_pipeline_set_new_stream_time (pipeline, 0);
369 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
371 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
373 if ((clock = element->clock)) {
376 gst_object_ref (clock);
377 GST_UNLOCK (element);
379 /* calculate the time when we stopped */
380 now = gst_clock_get_time (clock);
381 gst_object_unref (clock);
384 /* store the current stream time */
385 pipeline->stream_time = now - element->base_time;
386 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
387 ", base time %" GST_TIME_FORMAT,
388 GST_TIME_ARGS (pipeline->stream_time),
389 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
391 GST_UNLOCK (element);
393 case GST_STATE_CHANGE_PAUSED_TO_READY:
395 case GST_STATE_CHANGE_READY_TO_NULL:
398 gst_bus_set_flushing (element->bus, TRUE);
400 GST_UNLOCK (element);
404 if (result == GST_STATE_CHANGE_ASYNC) {
406 play_timeout = pipeline->play_timeout;
407 GST_UNLOCK (pipeline);
412 /* we wait for async state changes ourselves when we are in an
413 * intermediate state. */
414 if (play_timeout > 0) {
415 GTimeVal *timeval, timeout;
417 GST_STATE_UNLOCK (pipeline);
419 if (play_timeout == G_MAXUINT64) {
422 GST_TIME_TO_TIMEVAL (play_timeout, timeout);
426 result = gst_element_get_state (element, NULL, NULL, timeval);
427 if (result == GST_STATE_CHANGE_ASYNC) {
428 GST_WARNING_OBJECT (pipeline,
429 "timeout in PREROLL, forcing next state change");
430 g_warning ("timeout in PREROLL, forcing next state change");
431 result = GST_STATE_CHANGE_SUCCESS;
434 GST_STATE_LOCK (pipeline);
441 * gst_pipeline_get_bus:
442 * @pipeline: the pipeline
444 * Gets the #GstBus of this pipeline.
451 gst_pipeline_get_bus (GstPipeline * pipeline)
453 return gst_element_get_bus (GST_ELEMENT (pipeline));
457 * gst_pipeline_set_new_stream_time:
458 * @pipeline: the pipeline
459 * @time: the new stream time to set
461 * Set the new stream time of the pipeline. The stream time is used to
462 * set the base time on the elements (see @gst_element_set_base_time())
463 * in the PAUSED->PLAYING state transition.
468 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
470 g_return_if_fail (GST_IS_PIPELINE (pipeline));
473 pipeline->stream_time = time;
474 GST_DEBUG ("%s: set new stream_time to %" GST_TIME_FORMAT,
475 GST_ELEMENT_NAME (pipeline), GST_TIME_ARGS (time));
476 GST_UNLOCK (pipeline);
480 * gst_pipeline_get_last_stream_time:
481 * @pipeline: the pipeline
483 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
484 * the returned time is the stream time used to configure the elements
485 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
486 * time is the stream time when the pipeline was paused.
488 * Returns: a GstClockTime
493 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
497 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
500 result = pipeline->stream_time;
501 GST_UNLOCK (pipeline);
507 gst_pipeline_provide_clock_func (GstElement * element)
509 GstClock *clock = NULL;
510 GstPipeline *pipeline = GST_PIPELINE (element);
512 /* if we have a fixed clock, use that one */
514 if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
515 clock = pipeline->fixed_clock;
516 gst_object_ref (clock);
517 GST_UNLOCK (pipeline);
519 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
520 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
522 GST_UNLOCK (pipeline);
524 GST_ELEMENT_CLASS (parent_class)->
525 provide_clock (GST_ELEMENT (pipeline));
526 /* no clock, use a system clock */
528 clock = gst_system_clock_obtain ();
530 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
531 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
533 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
534 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
541 * gst_pipeline_get_clock:
542 * @pipeline: the pipeline
544 * Gets the current clock used by the pipeline.
546 * Returns: a GstClock
549 gst_pipeline_get_clock (GstPipeline * pipeline)
551 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
553 return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
558 * gst_pipeline_use_clock:
559 * @pipeline: the pipeline
560 * @clock: the clock to use
562 * Force the pipeline to use the given clock. The pipeline will
563 * always use the given clock even if new clock providers are added
569 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
571 g_return_if_fail (GST_IS_PIPELINE (pipeline));
574 GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
576 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
577 (GstObject *) clock);
578 GST_UNLOCK (pipeline);
580 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
581 (clock ? GST_OBJECT_NAME (clock) : "nil"));
585 * gst_pipeline_set_clock:
586 * @pipeline: the pipeline
587 * @clock: the clock to set
589 * Set the clock for the pipeline. The clock will be distributed
590 * to all the elements managed by the pipeline.
595 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
597 g_return_if_fail (pipeline != NULL);
598 g_return_if_fail (GST_IS_PIPELINE (pipeline));
600 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
604 * gst_pipeline_auto_clock:
605 * @pipeline: the pipeline
607 * Let the pipeline select a clock automatically.
612 gst_pipeline_auto_clock (GstPipeline * pipeline)
614 g_return_if_fail (pipeline != NULL);
615 g_return_if_fail (GST_IS_PIPELINE (pipeline));
618 GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
620 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
621 GST_UNLOCK (pipeline);
623 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");