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 "gstscheduler.h"
28 #include "gstsystemclock.h"
30 static GstElementDetails gst_pipeline_details =
31 GST_ELEMENT_DETAILS ("Pipeline object",
33 "Complete pipeline object",
34 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
36 /* Pipeline signals and args */
43 #define DEFAULT_DELAY 0
44 #define DEFAULT_PLAY_TIMEOUT (2*GST_SECOND)
54 static void gst_pipeline_base_init (gpointer g_class);
55 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
56 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
58 static void gst_pipeline_dispose (GObject * object);
59 static void gst_pipeline_set_property (GObject * object, guint prop_id,
60 const GValue * value, GParamSpec * pspec);
61 static void gst_pipeline_get_property (GObject * object, guint prop_id,
62 GValue * value, GParamSpec * pspec);
64 static gboolean gst_pipeline_send_event (GstElement * element,
66 static GstBusSyncReply pipeline_bus_handler (GstBus * bus, GstMessage * message,
67 GstPipeline * pipeline);
69 static GstClock *gst_pipeline_get_clock_func (GstElement * element);
70 static GstElementStateReturn gst_pipeline_change_state (GstElement * element);
72 static GstBinClass *parent_class = NULL;
74 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
77 gst_pipeline_get_type (void)
79 static GType pipeline_type = 0;
82 static const GTypeInfo pipeline_info = {
83 sizeof (GstPipelineClass),
84 gst_pipeline_base_init,
86 (GClassInitFunc) gst_pipeline_class_init,
96 g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
102 gst_pipeline_base_init (gpointer g_class)
104 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
106 gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
110 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
113 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
114 GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
116 parent_class = g_type_class_peek_parent (klass);
118 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
119 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
121 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DELAY,
122 g_param_spec_uint64 ("delay", "Delay",
123 "Expected delay needed for elements "
124 "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
126 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLAY_TIMEOUT,
127 g_param_spec_uint64 ("play-timeout", "Play Timeout",
128 "Max timeout for going to PLAYING in nanoseconds", 0, G_MAXUINT64,
129 DEFAULT_PLAY_TIMEOUT, G_PARAM_READWRITE));
131 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
133 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
134 gstelement_class->change_state =
135 GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
136 gstelement_class->get_clock = GST_DEBUG_FUNCPTR (gst_pipeline_get_clock_func);
140 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
142 GstScheduler *scheduler;
143 GstPipeline *pipeline = GST_PIPELINE (instance);
146 /* get an instance of the default scheduler */
147 scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (pipeline));
149 /* FIXME need better error handling */
150 if (scheduler == NULL) {
151 const gchar *name = gst_scheduler_factory_get_default_name ();
153 g_error ("Critical error: could not get scheduler \"%s\"\n"
154 "Are you sure you have a registry ?\n"
155 "Run gst-register as root if you haven't done so yet.", name);
157 gst_element_set_scheduler (GST_ELEMENT (pipeline), scheduler);
158 /* set_scheduler refs the bus via gst_object_replace, we drop our ref */
159 gst_object_unref ((GstObject *) scheduler);
162 pipeline->eosed = NULL;
163 pipeline->delay = DEFAULT_DELAY;
164 pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
165 /* we are our own manager */
166 GST_ELEMENT_MANAGER (pipeline) = pipeline;
168 bus = g_object_new (gst_bus_get_type (), NULL);
169 gst_bus_set_sync_handler (bus,
170 (GstBusSyncHandler) pipeline_bus_handler, pipeline);
171 gst_element_set_bus (GST_ELEMENT (pipeline), bus);
172 /* set_bus refs the bus via gst_object_replace, we drop our ref */
173 gst_object_unref ((GstObject *) bus);
177 gst_pipeline_dispose (GObject * object)
179 GstPipeline *pipeline = GST_PIPELINE (object);
181 gst_element_set_bus (GST_ELEMENT (pipeline), NULL);
182 gst_scheduler_reset (GST_ELEMENT_SCHEDULER (object));
183 gst_element_set_scheduler (GST_ELEMENT (pipeline), NULL);
184 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
186 G_OBJECT_CLASS (parent_class)->dispose (object);
190 gst_pipeline_set_property (GObject * object, guint prop_id,
191 const GValue * value, GParamSpec * pspec)
193 GstPipeline *pipeline = GST_PIPELINE (object);
198 pipeline->delay = g_value_get_uint64 (value);
200 case ARG_PLAY_TIMEOUT:
201 pipeline->play_timeout = g_value_get_uint64 (value);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 GST_UNLOCK (pipeline);
211 gst_pipeline_get_property (GObject * object, guint prop_id,
212 GValue * value, GParamSpec * pspec)
214 GstPipeline *pipeline = GST_PIPELINE (object);
219 g_value_set_uint64 (value, pipeline->delay);
221 case ARG_PLAY_TIMEOUT:
222 g_value_set_uint64 (value, pipeline->play_timeout);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 GST_UNLOCK (pipeline);
232 is_eos (GstPipeline * pipeline)
235 gboolean result = TRUE;
236 gboolean done = FALSE;
238 sinks = gst_bin_iterate_sinks (GST_BIN (pipeline));
242 switch (gst_iterator_next (sinks, &data)) {
243 case GST_ITERATOR_OK:
245 GstElement *element = GST_ELEMENT (data);
249 name = gst_element_get_name (element);
250 eosed = g_list_find (pipeline->eosed, element);
252 GST_DEBUG ("element %s did not post EOS yet", name);
256 GST_DEBUG ("element %s posted EOS", name);
259 gst_object_unref (GST_OBJECT (element));
262 case GST_ITERATOR_RESYNC:
264 gst_iterator_resync (sinks);
266 case GST_ITERATOR_DONE:
270 g_assert_not_reached ();
274 gst_iterator_free (sinks);
278 /* sending an event on the pipeline pauses the pipeline if it
282 gst_pipeline_send_event (GstElement * element, GstEvent * event)
284 gboolean was_playing;
286 GstElementState state;
287 GstEventType event_type = GST_EVENT_TYPE (event);
290 /* need to call _get_state() since a bin state is only updated
292 GST_TIME_TO_TIMEVAL (0, timeout);
294 gst_element_get_state (element, &state, NULL, &timeout);
295 was_playing = state == GST_STATE_PLAYING;
297 if (was_playing && event_type == GST_EVENT_SEEK)
298 gst_element_set_state (element, GST_STATE_PAUSED);
300 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
302 if (was_playing && event_type == GST_EVENT_SEEK)
303 gst_element_set_state (element, GST_STATE_PLAYING);
308 /* FIXME, make me threadsafe */
309 static GstBusSyncReply
310 pipeline_bus_handler (GstBus * bus, GstMessage * message,
311 GstPipeline * pipeline)
313 GstBusSyncReply result = GST_BUS_PASS;
314 gboolean posteos = FALSE;
316 /* we don't want messages from the streaming thread while we're doing the
317 * state change. We do want them from the state change functions. */
319 switch (GST_MESSAGE_TYPE (message)) {
320 case GST_MESSAGE_EOS:
321 if (GST_MESSAGE_SRC (message) != GST_OBJECT (pipeline)) {
322 GST_DEBUG ("got EOS message");
325 g_list_prepend (pipeline->eosed, GST_MESSAGE_SRC (message));
327 if (is_eos (pipeline)) {
329 GST_DEBUG ("all sinks posted EOS");
331 /* we drop all EOS messages */
332 result = GST_BUS_DROP;
333 gst_message_unref (message);
336 case GST_MESSAGE_ERROR:
343 gst_bus_post (bus, gst_message_new_eos (GST_OBJECT (pipeline)));
351 * @name: name of new pipeline
353 * Create a new pipeline with the given name.
355 * Returns: newly created GstPipeline
360 gst_pipeline_new (const gchar * name)
362 return gst_element_factory_make ("pipeline", name);
366 static GstElementStateReturn
367 gst_pipeline_change_state (GstElement * element)
369 GstElementStateReturn result = GST_STATE_SUCCESS;
370 GstPipeline *pipeline = GST_PIPELINE (element);
371 gint transition = GST_STATE_TRANSITION (element);
373 switch (transition) {
374 case GST_STATE_NULL_TO_READY:
376 gst_bus_set_flushing (element->bus, FALSE);
377 gst_scheduler_setup (GST_ELEMENT_SCHEDULER (pipeline));
379 case GST_STATE_READY_TO_PAUSED:
383 clock = gst_element_get_clock (element);
384 gst_element_set_clock (element, clock);
385 pipeline->eosed = NULL;
388 case GST_STATE_PAUSED_TO_PLAYING:
389 if (element->clock) {
390 GstClockTime start_time = gst_clock_get_time (element->clock);
392 element->base_time = start_time -
393 pipeline->stream_time + pipeline->delay;
394 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
395 GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
396 GST_TIME_ARGS (pipeline->stream_time),
397 GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
399 element->base_time = 0;
400 GST_DEBUG ("no clock, using base time of 0");
403 case GST_STATE_PLAYING_TO_PAUSED:
404 case GST_STATE_PAUSED_TO_READY:
405 case GST_STATE_READY_TO_NULL:
409 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
411 switch (transition) {
412 case GST_STATE_READY_TO_PAUSED:
413 pipeline->stream_time = 0;
415 case GST_STATE_PAUSED_TO_PLAYING:
417 case GST_STATE_PLAYING_TO_PAUSED:
418 if (element->clock) {
421 now = gst_clock_get_time (element->clock);
422 pipeline->stream_time = now - element->base_time;
423 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
424 ", base time %" GST_TIME_FORMAT,
425 GST_TIME_ARGS (pipeline->stream_time),
426 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
429 case GST_STATE_PAUSED_TO_READY:
431 case GST_STATE_READY_TO_NULL:
433 gst_bus_set_flushing (element->bus, TRUE);
438 /* we wait for async state changes ourselves when we are in an
439 * intermediate state.
440 * FIXME this can block forever, better do this in a worker
441 * thread or use a timeout? */
442 if (result == GST_STATE_ASYNC &&
443 (GST_STATE_FINAL (pipeline) != GST_STATE_PENDING (pipeline))) {
444 GTimeVal *timeval, timeout;
446 GST_STATE_UNLOCK (pipeline);
449 if (pipeline->play_timeout > 0) {
450 GST_TIME_TO_TIMEVAL (pipeline->play_timeout, timeout);
455 GST_UNLOCK (pipeline);
457 result = gst_element_get_state (element, NULL, NULL, timeval);
458 GST_STATE_LOCK (pipeline);
465 * gst_pipeline_get_scheduler:
466 * @pipeline: the pipeline
468 * Gets the #GstScheduler of this pipeline.
470 * Returns: a GstScheduler.
475 gst_pipeline_get_scheduler (GstPipeline * pipeline)
477 return gst_element_get_scheduler (GST_ELEMENT (pipeline));
481 * gst_pipeline_get_bus:
482 * @pipeline: the pipeline
484 * Gets the #GstBus of this pipeline.
491 gst_pipeline_get_bus (GstPipeline * pipeline)
493 return gst_element_get_bus (GST_ELEMENT (pipeline));
497 gst_pipeline_get_clock_func (GstElement * element)
499 GstClock *clock = NULL;
500 GstPipeline *pipeline = GST_PIPELINE (element);
502 /* if we have a fixed clock, use that one */
504 if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
505 clock = pipeline->fixed_clock;
506 gst_object_ref (GST_OBJECT (clock));
507 GST_UNLOCK (pipeline);
509 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
510 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
512 GST_UNLOCK (pipeline);
514 GST_ELEMENT_CLASS (parent_class)->get_clock (GST_ELEMENT (pipeline));
515 /* no clock, use a system clock */
517 clock = gst_system_clock_obtain ();
518 /* we unref since this function is not supposed to increase refcount
519 * of clock object returned; this is ok since the systemclock always
520 * has a refcount of at least one in the current code. */
521 gst_object_unref (GST_OBJECT (clock));
522 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
523 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
525 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
526 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
533 * gst_pipeline_get_clock:
534 * @pipeline: the pipeline
536 * Gets the current clock used by the pipeline.
538 * Returns: a GstClock
541 gst_pipeline_get_clock (GstPipeline * pipeline)
543 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
545 return gst_pipeline_get_clock_func (GST_ELEMENT (pipeline));
550 * gst_pipeline_use_clock:
551 * @pipeline: the pipeline
552 * @clock: the clock to use
554 * Force the pipeline to use the given clock. The pipeline will
555 * always use the given clock even if new clock providers are added
561 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
563 g_return_if_fail (GST_IS_PIPELINE (pipeline));
566 GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
568 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
569 (GstObject *) clock);
570 GST_UNLOCK (pipeline);
572 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
573 (clock ? GST_OBJECT_NAME (clock) : "nil"));
577 * gst_pipeline_set_clock:
578 * @pipeline: the pipeline
579 * @clock: the clock to set
581 * Set the clock for the pipeline. The clock will be distributed
582 * to all the elements managed by the pipeline.
587 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
589 g_return_if_fail (pipeline != NULL);
590 g_return_if_fail (GST_IS_PIPELINE (pipeline));
592 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
596 * gst_pipeline_auto_clock:
597 * @pipeline: the pipeline
599 * Let the pipeline select a clock automatically.
604 gst_pipeline_auto_clock (GstPipeline * pipeline)
606 g_return_if_fail (pipeline != NULL);
607 g_return_if_fail (GST_IS_PIPELINE (pipeline));
610 GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
612 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
613 GST_UNLOCK (pipeline);
615 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");