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.
23 #include "gst_private.h"
25 #include "gstpipeline.h"
27 #include "gstsystemclock.h"
29 static GstElementDetails gst_pipeline_details =
30 GST_ELEMENT_DETAILS ("Pipeline object",
32 "Complete pipeline object",
33 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
35 /* Pipeline signals and args */
42 #define DEFAULT_DELAY 0
43 #define DEFAULT_PLAY_TIMEOUT (2*GST_SECOND)
53 static void gst_pipeline_base_init (gpointer g_class);
54 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
55 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
57 static void gst_pipeline_dispose (GObject * object);
58 static void gst_pipeline_set_property (GObject * object, guint prop_id,
59 const GValue * value, GParamSpec * pspec);
60 static void gst_pipeline_get_property (GObject * object, guint prop_id,
61 GValue * value, GParamSpec * pspec);
63 static gboolean gst_pipeline_send_event (GstElement * element,
66 static GstClock *gst_pipeline_get_clock_func (GstElement * element);
67 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
68 GstStateChange transition);
70 static GstBinClass *parent_class = NULL;
72 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
75 gst_pipeline_get_type (void)
77 static GType pipeline_type = 0;
80 static const GTypeInfo pipeline_info = {
81 sizeof (GstPipelineClass),
82 gst_pipeline_base_init,
84 (GClassInitFunc) gst_pipeline_class_init,
94 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
100 gst_pipeline_base_init (gpointer g_class)
102 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
104 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
108 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
110 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
111 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
112 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
114 parent_class = g_type_class_peek_parent (klass);
116 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
117 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
119 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DELAY,
120 g_param_spec_uint64 ("delay", "Delay",
121 "Expected delay needed for elements "
122 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
124 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLAY_TIMEOUT,
125 g_param_spec_uint64 ("play-timeout", "Play Timeout",
126 "Max timeout for going to PLAYING in nanoseconds", 0, G_MAXUINT64,
127 DEFAULT_PLAY_TIMEOUT, G_PARAM_READWRITE));
129 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
131 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
132 gstelement_class->change_state =
133 GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
134 gstelement_class->get_clock = GST_DEBUG_FUNCPTR (gst_pipeline_get_clock_func);
138 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
140 GstPipeline *pipeline = GST_PIPELINE (instance);
142 pipeline->delay = DEFAULT_DELAY;
143 pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
147 gst_pipeline_dispose (GObject * object)
149 GstPipeline *pipeline = GST_PIPELINE (object);
151 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
153 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
155 G_OBJECT_CLASS (parent_class)->dispose (object);
159 gst_pipeline_set_property (GObject * object, guint prop_id,
160 const GValue * value, GParamSpec * pspec)
162 GstPipeline *pipeline = GST_PIPELINE (object);
167 pipeline->delay = g_value_get_uint64 (value);
169 case ARG_PLAY_TIMEOUT:
170 pipeline->play_timeout = g_value_get_uint64 (value);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 GST_UNLOCK (pipeline);
180 gst_pipeline_get_property (GObject * object, guint prop_id,
181 GValue * value, GParamSpec * pspec)
183 GstPipeline *pipeline = GST_PIPELINE (object);
188 g_value_set_uint64 (value, pipeline->delay);
190 case ARG_PLAY_TIMEOUT:
191 g_value_set_uint64 (value, pipeline->play_timeout);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197 GST_UNLOCK (pipeline);
201 do_pipeline_seek (GstElement * element, GstEvent * event)
206 gboolean was_playing = FALSE;
209 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
211 flush = flags & GST_SEEK_FLAG_FLUSH;
217 GST_TIME_TO_TIMEVAL (0, timeout);
218 /* need to call _get_state() since a bin state is only updated
220 gst_element_get_state (element, &state, NULL, &timeout);
221 was_playing = state == GST_STATE_PLAYING;
224 gst_element_set_state (element, GST_STATE_PAUSED);
227 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
230 /* need to reset the stream time to 0 after a flushing seek */
231 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
233 /* and continue playing */
234 gst_element_set_state (element, GST_STATE_PLAYING);
240 /* sending a seek event on the pipeline pauses the pipeline if it
244 gst_pipeline_send_event (GstElement * element, GstEvent * event)
247 GstEventType event_type = GST_EVENT_TYPE (event);
249 switch (event_type) {
251 res = do_pipeline_seek (element, event);
254 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
263 * @name: name of new pipeline
265 * Create a new pipeline with the given name.
267 * Returns: newly created GstPipeline
272 gst_pipeline_new (const gchar * name)
274 return gst_element_factory_make ("pipeline", name);
278 static GstStateChangeReturn
279 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
281 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
282 GstPipeline *pipeline = GST_PIPELINE (element);
283 GstClockTime play_timeout;
286 switch (transition) {
287 case GST_STATE_CHANGE_NULL_TO_READY:
290 gst_bus_set_flushing (element->bus, FALSE);
291 GST_UNLOCK (element);
293 case GST_STATE_CHANGE_READY_TO_PAUSED:
295 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
296 /* when going to playing, select a clock */
297 if ((clock = gst_element_get_clock (element))) {
298 GstClockTime start_time;
300 /* distribute the clock */
301 gst_element_set_clock (element, clock);
304 start_time = gst_clock_get_time (clock);
305 gst_object_unref (clock);
308 element->base_time = start_time -
309 pipeline->stream_time + pipeline->delay;
310 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
311 GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
312 GST_TIME_ARGS (pipeline->stream_time),
313 GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
314 GST_UNLOCK (element);
316 GST_UNLOCK (element);
317 GST_DEBUG ("no clock, using base time of 0");
318 gst_element_set_base_time (element, 0);
321 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
322 case GST_STATE_CHANGE_PAUSED_TO_READY:
323 case GST_STATE_CHANGE_READY_TO_NULL:
327 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
329 switch (transition) {
330 case GST_STATE_CHANGE_NULL_TO_READY:
332 case GST_STATE_CHANGE_READY_TO_PAUSED:
333 gst_pipeline_set_new_stream_time (pipeline, 0);
335 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
337 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
339 if ((clock = element->clock)) {
342 gst_object_ref (clock);
343 GST_UNLOCK (element);
345 /* calculate the time when we stopped */
346 now = gst_clock_get_time (clock);
347 gst_object_unref (clock);
350 /* store the current stream time */
351 pipeline->stream_time = now - element->base_time;
352 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
353 ", base time %" GST_TIME_FORMAT,
354 GST_TIME_ARGS (pipeline->stream_time),
355 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
357 GST_UNLOCK (element);
359 case GST_STATE_CHANGE_PAUSED_TO_READY:
361 case GST_STATE_CHANGE_READY_TO_NULL:
364 gst_bus_set_flushing (element->bus, TRUE);
366 GST_UNLOCK (element);
370 if (result == GST_STATE_CHANGE_ASYNC) {
372 play_timeout = pipeline->play_timeout;
373 GST_UNLOCK (pipeline);
378 /* we wait for async state changes ourselves when we are in an
379 * intermediate state. */
380 if (play_timeout > 0) {
381 GTimeVal *timeval, timeout;
383 GST_STATE_UNLOCK (pipeline);
385 if (play_timeout == G_MAXUINT64) {
388 GST_TIME_TO_TIMEVAL (play_timeout, timeout);
392 result = gst_element_get_state (element, NULL, NULL, timeval);
393 if (result == GST_STATE_CHANGE_ASYNC) {
394 GST_WARNING_OBJECT (pipeline,
395 "timeout in PREROLL, forcing next state change");
396 g_warning ("timeout in PREROLL, forcing next state change");
397 result = GST_STATE_CHANGE_SUCCESS;
400 GST_STATE_LOCK (pipeline);
407 * gst_pipeline_get_bus:
408 * @pipeline: the pipeline
410 * Gets the #GstBus of this pipeline.
417 gst_pipeline_get_bus (GstPipeline * pipeline)
419 return gst_element_get_bus (GST_ELEMENT (pipeline));
423 * gst_pipeline_set_new_stream_time:
424 * @pipeline: the pipeline
425 * @GstClockTime: the new stream time to set
427 * Set the new stream time of the pipeline. The stream time is used to
428 * set the base time on the elements (see @gst_element_set_base_time())
429 * in the PAUSED->PLAYING state transition.
434 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
436 g_return_if_fail (GST_IS_PIPELINE (pipeline));
439 pipeline->stream_time = time;
440 GST_DEBUG ("%s: set new stream_time to %" GST_TIME_FORMAT,
441 GST_ELEMENT_NAME (pipeline), GST_TIME_ARGS (time));
442 GST_UNLOCK (pipeline);
446 * gst_pipeline_get_last_stream_time:
447 * @pipeline: the pipeline
449 * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
450 * the returned time is the stream time used to configure the elements
451 * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
452 * time is the stream time when the pipeline was paused.
454 * Returns: a GstClockTime
459 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
463 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
466 result = pipeline->stream_time;
467 GST_UNLOCK (pipeline);
473 gst_pipeline_get_clock_func (GstElement * element)
475 GstClock *clock = NULL;
476 GstPipeline *pipeline = GST_PIPELINE (element);
478 /* if we have a fixed clock, use that one */
480 if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
481 clock = pipeline->fixed_clock;
482 gst_object_ref (clock);
483 GST_UNLOCK (pipeline);
485 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
486 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
488 GST_UNLOCK (pipeline);
490 GST_ELEMENT_CLASS (parent_class)->get_clock (GST_ELEMENT (pipeline));
491 /* no clock, use a system clock */
493 clock = gst_system_clock_obtain ();
495 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
496 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
498 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
499 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
506 * gst_pipeline_get_clock:
507 * @pipeline: the pipeline
509 * Gets the current clock used by the pipeline.
511 * Returns: a GstClock
514 gst_pipeline_get_clock (GstPipeline * pipeline)
516 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
518 return gst_pipeline_get_clock_func (GST_ELEMENT (pipeline));
523 * gst_pipeline_use_clock:
524 * @pipeline: the pipeline
525 * @clock: the clock to use
527 * Force the pipeline to use the given clock. The pipeline will
528 * always use the given clock even if new clock providers are added
534 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
536 g_return_if_fail (GST_IS_PIPELINE (pipeline));
539 GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
541 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
542 (GstObject *) clock);
543 GST_UNLOCK (pipeline);
545 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
546 (clock ? GST_OBJECT_NAME (clock) : "nil"));
550 * gst_pipeline_set_clock:
551 * @pipeline: the pipeline
552 * @clock: the clock to set
554 * Set the clock for the pipeline. The clock will be distributed
555 * to all the elements managed by the pipeline.
560 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
562 g_return_if_fail (pipeline != NULL);
563 g_return_if_fail (GST_IS_PIPELINE (pipeline));
565 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
569 * gst_pipeline_auto_clock:
570 * @pipeline: the pipeline
572 * Let the pipeline select a clock automatically.
577 gst_pipeline_auto_clock (GstPipeline * pipeline)
579 g_return_if_fail (pipeline != NULL);
580 g_return_if_fail (GST_IS_PIPELINE (pipeline));
583 GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
585 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
586 GST_UNLOCK (pipeline);
588 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");