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 bus = g_object_new (gst_bus_get_type (), NULL);
158 gst_bus_set_sync_handler (bus,
159 (GstBusSyncHandler) pipeline_bus_handler, pipeline);
160 pipeline->eosed = NULL;
161 pipeline->delay = DEFAULT_DELAY;
162 pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
163 /* we are our own manager */
164 GST_ELEMENT_MANAGER (pipeline) = pipeline;
165 gst_element_set_bus (GST_ELEMENT (pipeline), bus);
166 /* set_bus refs the bus via gst_object_replace, we drop our ref */
167 gst_object_unref ((GstObject *) bus);
168 gst_element_set_scheduler (GST_ELEMENT (pipeline), scheduler);
172 gst_pipeline_dispose (GObject * object)
174 GstPipeline *pipeline = GST_PIPELINE (object);
176 gst_element_set_bus (GST_ELEMENT (pipeline), NULL);
177 gst_scheduler_reset (GST_ELEMENT_SCHEDULER (object));
178 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
180 G_OBJECT_CLASS (parent_class)->dispose (object);
184 gst_pipeline_set_property (GObject * object, guint prop_id,
185 const GValue * value, GParamSpec * pspec)
187 GstPipeline *pipeline = GST_PIPELINE (object);
192 pipeline->delay = g_value_get_uint64 (value);
194 case ARG_PLAY_TIMEOUT:
195 pipeline->play_timeout = g_value_get_uint64 (value);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 GST_UNLOCK (pipeline);
205 gst_pipeline_get_property (GObject * object, guint prop_id,
206 GValue * value, GParamSpec * pspec)
208 GstPipeline *pipeline = GST_PIPELINE (object);
213 g_value_set_uint64 (value, pipeline->delay);
215 case ARG_PLAY_TIMEOUT:
216 g_value_set_uint64 (value, pipeline->play_timeout);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 GST_UNLOCK (pipeline);
226 is_eos (GstPipeline * pipeline)
229 gboolean result = TRUE;
230 gboolean done = FALSE;
232 sinks = gst_bin_iterate_sinks (GST_BIN (pipeline));
236 switch (gst_iterator_next (sinks, &data)) {
237 case GST_ITERATOR_OK:
239 GstElement *element = GST_ELEMENT (data);
243 name = gst_element_get_name (element);
244 eosed = g_list_find (pipeline->eosed, element);
246 GST_DEBUG ("element %s did not post EOS yet", name);
250 GST_DEBUG ("element %s posted EOS", name);
253 gst_object_unref (GST_OBJECT (element));
256 case GST_ITERATOR_RESYNC:
258 gst_iterator_resync (sinks);
260 case GST_ITERATOR_DONE:
264 g_assert_not_reached ();
268 gst_iterator_free (sinks);
272 /* sending an event on the pipeline pauses the pipeline if it
276 gst_pipeline_send_event (GstElement * element, GstEvent * event)
278 gboolean was_playing;
280 GstElementState state;
281 GstEventType event_type = GST_EVENT_TYPE (event);
284 /* need to call _get_state() since a bin state is only updated
286 GST_TIME_TO_TIMEVAL (0, timeout);
288 gst_element_get_state (element, &state, NULL, &timeout);
289 was_playing = state == GST_STATE_PLAYING;
291 if (was_playing && event_type == GST_EVENT_SEEK)
292 gst_element_set_state (element, GST_STATE_PAUSED);
294 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
296 if (was_playing && event_type == GST_EVENT_SEEK)
297 gst_element_set_state (element, GST_STATE_PLAYING);
302 /* FIXME, make me threadsafe */
303 static GstBusSyncReply
304 pipeline_bus_handler (GstBus * bus, GstMessage * message,
305 GstPipeline * pipeline)
307 GstBusSyncReply result = GST_BUS_PASS;
308 gboolean posteos = FALSE;
310 /* we don't want messages from the streaming thread while we're doing the
311 * state change. We do want them from the state change functions. */
313 switch (GST_MESSAGE_TYPE (message)) {
314 case GST_MESSAGE_EOS:
315 if (GST_MESSAGE_SRC (message) != GST_OBJECT (pipeline)) {
316 GST_DEBUG ("got EOS message");
319 g_list_prepend (pipeline->eosed, GST_MESSAGE_SRC (message));
321 if (is_eos (pipeline)) {
323 GST_DEBUG ("all sinks posted EOS");
325 /* we drop all EOS messages */
326 result = GST_BUS_DROP;
327 gst_message_unref (message);
330 case GST_MESSAGE_ERROR:
337 gst_bus_post (bus, gst_message_new_eos (GST_OBJECT (pipeline)));
345 * @name: name of new pipeline
347 * Create a new pipeline with the given name.
349 * Returns: newly created GstPipeline
354 gst_pipeline_new (const gchar * name)
356 return gst_element_factory_make ("pipeline", name);
360 static GstElementStateReturn
361 gst_pipeline_change_state (GstElement * element)
363 GstElementStateReturn result = GST_STATE_SUCCESS;
364 GstPipeline *pipeline = GST_PIPELINE (element);
365 gint transition = GST_STATE_TRANSITION (element);
367 switch (transition) {
368 case GST_STATE_NULL_TO_READY:
370 gst_bus_set_flushing (element->bus, FALSE);
371 gst_scheduler_setup (GST_ELEMENT_SCHEDULER (pipeline));
373 case GST_STATE_READY_TO_PAUSED:
377 clock = gst_element_get_clock (element);
378 gst_element_set_clock (element, clock);
379 pipeline->eosed = NULL;
382 case GST_STATE_PAUSED_TO_PLAYING:
383 if (element->clock) {
384 GstClockTime start_time = gst_clock_get_time (element->clock);
386 element->base_time = start_time -
387 pipeline->stream_time + pipeline->delay;
388 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
389 GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
390 GST_TIME_ARGS (pipeline->stream_time),
391 GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
393 element->base_time = 0;
394 GST_DEBUG ("no clock, using base time of 0");
397 case GST_STATE_PLAYING_TO_PAUSED:
398 case GST_STATE_PAUSED_TO_READY:
399 case GST_STATE_READY_TO_NULL:
403 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
405 switch (transition) {
406 case GST_STATE_READY_TO_PAUSED:
407 pipeline->stream_time = 0;
409 case GST_STATE_PAUSED_TO_PLAYING:
411 case GST_STATE_PLAYING_TO_PAUSED:
412 if (element->clock) {
415 now = gst_clock_get_time (element->clock);
416 pipeline->stream_time = now - element->base_time;
417 GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
418 ", base time %" GST_TIME_FORMAT,
419 GST_TIME_ARGS (pipeline->stream_time),
420 GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
423 case GST_STATE_PAUSED_TO_READY:
425 case GST_STATE_READY_TO_NULL:
427 gst_bus_set_flushing (element->bus, TRUE);
432 /* we wait for async state changes ourselves when we are in an
433 * intermediate state.
434 * FIXME this can block forever, better do this in a worker
435 * thread or use a timeout? */
436 if (result == GST_STATE_ASYNC &&
437 (GST_STATE_FINAL (pipeline) != GST_STATE_PENDING (pipeline))) {
438 GTimeVal *timeval, timeout;
440 GST_STATE_UNLOCK (pipeline);
443 if (pipeline->play_timeout > 0) {
444 GST_TIME_TO_TIMEVAL (pipeline->play_timeout, timeout);
449 GST_UNLOCK (pipeline);
451 result = gst_element_get_state (element, NULL, NULL, timeval);
452 GST_STATE_LOCK (pipeline);
459 * gst_pipeline_get_scheduler:
460 * @pipeline: the pipeline
462 * Gets the #GstScheduler of this pipeline.
464 * Returns: a GstScheduler.
469 gst_pipeline_get_scheduler (GstPipeline * pipeline)
471 return gst_element_get_scheduler (GST_ELEMENT (pipeline));
475 * gst_pipeline_get_bus:
476 * @pipeline: the pipeline
478 * Gets the #GstBus of this pipeline.
485 gst_pipeline_get_bus (GstPipeline * pipeline)
487 return gst_element_get_bus (GST_ELEMENT (pipeline));
491 gst_pipeline_get_clock_func (GstElement * element)
493 GstClock *clock = NULL;
494 GstPipeline *pipeline = GST_PIPELINE (element);
496 /* if we have a fixed clock, use that one */
498 if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
499 clock = pipeline->fixed_clock;
500 gst_object_ref (GST_OBJECT (clock));
501 GST_UNLOCK (pipeline);
503 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
504 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
506 GST_UNLOCK (pipeline);
508 GST_ELEMENT_CLASS (parent_class)->get_clock (GST_ELEMENT (pipeline));
509 /* no clock, use a system clock */
511 clock = gst_system_clock_obtain ();
512 /* we unref since this function is not supposed to increase refcount
513 * of clock object returned; this is ok since the systemclock always
514 * has a refcount of at least one in the current code. */
515 gst_object_unref (GST_OBJECT (clock));
516 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
517 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
519 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
520 clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
527 * gst_pipeline_get_clock:
528 * @pipeline: the pipeline
530 * Gets the current clock used by the pipeline.
532 * Returns: a GstClock
535 gst_pipeline_get_clock (GstPipeline * pipeline)
537 g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
539 return gst_pipeline_get_clock_func (GST_ELEMENT (pipeline));
544 * gst_pipeline_use_clock:
545 * @pipeline: the pipeline
546 * @clock: the clock to use
548 * Force the pipeline to use the given clock. The pipeline will
549 * always use the given clock even if new clock providers are added
555 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
557 g_return_if_fail (GST_IS_PIPELINE (pipeline));
560 GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
562 gst_object_replace ((GstObject **) & pipeline->fixed_clock,
563 (GstObject *) clock);
564 GST_UNLOCK (pipeline);
566 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
567 (clock ? GST_OBJECT_NAME (clock) : "nil"));
571 * gst_pipeline_set_clock:
572 * @pipeline: the pipeline
573 * @clock: the clock to set
575 * Set the clock for the pipeline. The clock will be distributed
576 * to all the elements managed by the pipeline.
581 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
583 g_return_if_fail (pipeline != NULL);
584 g_return_if_fail (GST_IS_PIPELINE (pipeline));
586 GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
590 * gst_pipeline_auto_clock:
591 * @pipeline: the pipeline
593 * Let the pipeline select a clock automatically.
598 gst_pipeline_auto_clock (GstPipeline * pipeline)
600 g_return_if_fail (pipeline != NULL);
601 g_return_if_fail (GST_IS_PIPELINE (pipeline));
604 GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
606 gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
607 GST_UNLOCK (pipeline);
609 GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");