gst/: Small docs and debug updates.
[platform/upstream/gstreamer.git] / gst / gstpipeline.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2004,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstpipeline.c: Overall pipeline management element
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:gstpipeline
24  * @short_description: Top-level bin with clocking and bus management functionality.
25  * @see_also: #GstBin
26  *
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.
30  *
31  * The pipeline will also use the selected clock to calculate the stream time of
32  * the pipeline.
33  *
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
36  * after the seek.
37  *
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).
41  */
42
43 #include "gst_private.h"
44
45 #include "gstpipeline.h"
46 #include "gstinfo.h"
47 #include "gstsystemclock.h"
48
49 static GstElementDetails gst_pipeline_details =
50 GST_ELEMENT_DETAILS ("Pipeline object",
51     "Generic/Bin",
52     "Complete pipeline object",
53     "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
54
55 /* Pipeline signals and args */
56 enum
57 {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 #define DEFAULT_DELAY 0
63 #define DEFAULT_PLAY_TIMEOUT  (2*GST_SECOND)
64 enum
65 {
66   PROP_0,
67   PROP_DELAY,
68   PROP_PLAY_TIMEOUT,
69   /* FILL ME */
70 };
71
72
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);
76
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);
82
83 static gboolean gst_pipeline_send_event (GstElement * element,
84     GstEvent * event);
85
86 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
87 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
88     GstStateChange transition);
89
90 static GstBinClass *parent_class = NULL;
91
92 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
93
94 GType
95 gst_pipeline_get_type (void)
96 {
97   static GType pipeline_type = 0;
98
99   if (!pipeline_type) {
100     static const GTypeInfo pipeline_info = {
101       sizeof (GstPipelineClass),
102       gst_pipeline_base_init,
103       NULL,
104       (GClassInitFunc) gst_pipeline_class_init,
105       NULL,
106       NULL,
107       sizeof (GstPipeline),
108       0,
109       gst_pipeline_init,
110       NULL
111     };
112
113     pipeline_type =
114         g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
115   }
116   return pipeline_type;
117 }
118
119 static void
120 gst_pipeline_base_init (gpointer g_class)
121 {
122   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
123
124   gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
125 }
126
127 static void
128 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
129 {
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);
133
134   parent_class = g_type_class_peek_parent (klass);
135
136   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
137   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
138
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,
143           G_PARAM_READWRITE));
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));
148
149   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
150
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);
156 }
157
158 static void
159 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
160 {
161   GstPipeline *pipeline = GST_PIPELINE (instance);
162   GstBus *bus;
163
164   pipeline->delay = DEFAULT_DELAY;
165   pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
166
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);
170 }
171
172 static void
173 gst_pipeline_dispose (GObject * object)
174 {
175   GstPipeline *pipeline = GST_PIPELINE (object);
176
177   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
178
179   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
180
181   G_OBJECT_CLASS (parent_class)->dispose (object);
182 }
183
184 static void
185 gst_pipeline_set_property (GObject * object, guint prop_id,
186     const GValue * value, GParamSpec * pspec)
187 {
188   GstPipeline *pipeline = GST_PIPELINE (object);
189
190   GST_LOCK (pipeline);
191   switch (prop_id) {
192     case PROP_DELAY:
193       pipeline->delay = g_value_get_uint64 (value);
194       break;
195     case PROP_PLAY_TIMEOUT:
196       pipeline->play_timeout = g_value_get_uint64 (value);
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202   GST_UNLOCK (pipeline);
203 }
204
205 static void
206 gst_pipeline_get_property (GObject * object, guint prop_id,
207     GValue * value, GParamSpec * pspec)
208 {
209   GstPipeline *pipeline = GST_PIPELINE (object);
210
211   GST_LOCK (pipeline);
212   switch (prop_id) {
213     case PROP_DELAY:
214       g_value_set_uint64 (value, pipeline->delay);
215       break;
216     case PROP_PLAY_TIMEOUT:
217       g_value_set_uint64 (value, pipeline->play_timeout);
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223   GST_UNLOCK (pipeline);
224 }
225
226 static gboolean
227 do_pipeline_seek (GstElement * element, GstEvent * event)
228 {
229   gdouble rate;
230   GstSeekFlags flags;
231   gboolean flush;
232   gboolean was_playing = FALSE;
233   gboolean res;
234
235   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
236
237   flush = flags & GST_SEEK_FLAG_FLUSH;
238
239   if (flush) {
240     GstState state;
241     GTimeVal timeout;
242
243     GST_TIME_TO_TIMEVAL (0, timeout);
244     /* need to call _get_state() since a bin state is only updated
245      * with this call. */
246     gst_element_get_state (element, &state, NULL, &timeout);
247     was_playing = state == GST_STATE_PLAYING;
248
249     if (was_playing)
250       gst_element_set_state (element, GST_STATE_PAUSED);
251   }
252
253   res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
254
255   if (flush && res) {
256     /* need to reset the stream time to 0 after a flushing seek */
257     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
258     if (was_playing) {
259       /* and continue playing */
260       gst_element_set_state (element, GST_STATE_PLAYING);
261     }
262   }
263   return res;
264 }
265
266 /* sending a seek event on the pipeline pauses the pipeline if it
267  * was playing.
268  */
269 static gboolean
270 gst_pipeline_send_event (GstElement * element, GstEvent * event)
271 {
272   gboolean res;
273   GstEventType event_type = GST_EVENT_TYPE (event);
274
275   switch (event_type) {
276     case GST_EVENT_SEEK:
277       res = do_pipeline_seek (element, event);
278       break;
279     default:
280       res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
281       break;
282   }
283
284   return res;
285 }
286
287 /**
288  * gst_pipeline_new:
289  * @name: name of new pipeline
290  *
291  * Create a new pipeline with the given name.
292  *
293  * Returns: newly created GstPipeline
294  *
295  * MT safe.
296  */
297 GstElement *
298 gst_pipeline_new (const gchar * name)
299 {
300   return gst_element_factory_make ("pipeline", name);
301 }
302
303 /* MT safe */
304 static GstStateChangeReturn
305 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
306 {
307   GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
308   GstPipeline *pipeline = GST_PIPELINE (element);
309   GstClockTime play_timeout;
310   GstClock *clock;
311
312   switch (transition) {
313     case GST_STATE_CHANGE_NULL_TO_READY:
314       GST_LOCK (element);
315       if (element->bus)
316         gst_bus_set_flushing (element->bus, FALSE);
317       GST_UNLOCK (element);
318       break;
319     case GST_STATE_CHANGE_READY_TO_PAUSED:
320       break;
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;
325         gboolean new_clock;
326
327         /* get start time */
328         start_time = gst_clock_get_time (clock);
329
330         GST_LOCK (element);
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);
339
340         /* now distribute the clock */
341         gst_element_set_clock (element, clock);
342
343         if (new_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));
347         }
348
349         gst_object_unref (clock);
350       } else {
351         GST_DEBUG ("no clock, using base time of 0");
352         gst_element_set_base_time (element, 0);
353       }
354       break;
355     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
356     case GST_STATE_CHANGE_PAUSED_TO_READY:
357     case GST_STATE_CHANGE_READY_TO_NULL:
358       break;
359   }
360
361   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
362
363   switch (transition) {
364     case GST_STATE_CHANGE_NULL_TO_READY:
365       break;
366     case GST_STATE_CHANGE_READY_TO_PAUSED:
367       gst_pipeline_set_new_stream_time (pipeline, 0);
368       break;
369     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
370       break;
371     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
372       GST_LOCK (element);
373       if ((clock = element->clock)) {
374         GstClockTime now;
375
376         gst_object_ref (clock);
377         GST_UNLOCK (element);
378
379         /* calculate the time when we stopped */
380         now = gst_clock_get_time (clock);
381         gst_object_unref (clock);
382
383         GST_LOCK (element);
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));
390       }
391       GST_UNLOCK (element);
392       break;
393     case GST_STATE_CHANGE_PAUSED_TO_READY:
394       break;
395     case GST_STATE_CHANGE_READY_TO_NULL:
396       GST_LOCK (element);
397       if (element->bus) {
398         gst_bus_set_flushing (element->bus, TRUE);
399       }
400       GST_UNLOCK (element);
401       break;
402   }
403
404   if (result == GST_STATE_CHANGE_ASYNC) {
405     GST_LOCK (pipeline);
406     play_timeout = pipeline->play_timeout;
407     GST_UNLOCK (pipeline);
408   } else {
409     play_timeout = 0;
410   }
411
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;
416
417     GST_STATE_UNLOCK (pipeline);
418
419     if (play_timeout == G_MAXUINT64) {
420       timeval = NULL;
421     } else {
422       GST_TIME_TO_TIMEVAL (play_timeout, timeout);
423       timeval = &timeout;
424     }
425
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;
432     }
433
434     GST_STATE_LOCK (pipeline);
435   }
436
437   return result;
438 }
439
440 /**
441  * gst_pipeline_get_bus:
442  * @pipeline: the pipeline
443  *
444  * Gets the #GstBus of this pipeline.
445  *
446  * Returns: a GstBus
447  *
448  * MT safe.
449  */
450 GstBus *
451 gst_pipeline_get_bus (GstPipeline * pipeline)
452 {
453   return gst_element_get_bus (GST_ELEMENT (pipeline));
454 }
455
456 /**
457  * gst_pipeline_set_new_stream_time:
458  * @pipeline: the pipeline
459  * @time: the new stream time to set
460  *
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.
464  *
465  * MT safe.
466  */
467 void
468 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
469 {
470   g_return_if_fail (GST_IS_PIPELINE (pipeline));
471
472   GST_LOCK (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);
477 }
478
479 /**
480  * gst_pipeline_get_last_stream_time:
481  * @pipeline: the pipeline
482  *
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.
487  *
488  * Returns: a GstClockTime
489  *
490  * MT safe.
491  */
492 GstClockTime
493 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
494 {
495   GstClockTime result;
496
497   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
498
499   GST_LOCK (pipeline);
500   result = pipeline->stream_time;
501   GST_UNLOCK (pipeline);
502
503   return result;
504 }
505
506 static GstClock *
507 gst_pipeline_provide_clock_func (GstElement * element)
508 {
509   GstClock *clock = NULL;
510   GstPipeline *pipeline = GST_PIPELINE (element);
511
512   /* if we have a fixed clock, use that one */
513   GST_LOCK (pipeline);
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);
518
519     GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
520         clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
521   } else {
522     GST_UNLOCK (pipeline);
523     clock =
524         GST_ELEMENT_CLASS (parent_class)->
525         provide_clock (GST_ELEMENT (pipeline));
526     /* no clock, use a system clock */
527     if (!clock) {
528       clock = gst_system_clock_obtain ();
529
530       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
531           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
532     } else {
533       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
534           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
535     }
536   }
537   return clock;
538 }
539
540 /**
541  * gst_pipeline_get_clock:
542  * @pipeline: the pipeline
543  *
544  * Gets the current clock used by the pipeline.
545  *
546  * Returns: a GstClock
547  */
548 GstClock *
549 gst_pipeline_get_clock (GstPipeline * pipeline)
550 {
551   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
552
553   return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
554 }
555
556
557 /**
558  * gst_pipeline_use_clock:
559  * @pipeline: the pipeline
560  * @clock: the clock to use
561  *
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
564  * to this pipeline.
565  *
566  * MT safe.
567  */
568 void
569 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
570 {
571   g_return_if_fail (GST_IS_PIPELINE (pipeline));
572
573   GST_LOCK (pipeline);
574   GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
575
576   gst_object_replace ((GstObject **) & pipeline->fixed_clock,
577       (GstObject *) clock);
578   GST_UNLOCK (pipeline);
579
580   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
581       (clock ? GST_OBJECT_NAME (clock) : "nil"));
582 }
583
584 /**
585  * gst_pipeline_set_clock:
586  * @pipeline: the pipeline
587  * @clock: the clock to set
588  *
589  * Set the clock for the pipeline. The clock will be distributed
590  * to all the elements managed by the pipeline.
591  *
592  * MT safe.
593  */
594 void
595 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
596 {
597   g_return_if_fail (pipeline != NULL);
598   g_return_if_fail (GST_IS_PIPELINE (pipeline));
599
600   GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
601 }
602
603 /**
604  * gst_pipeline_auto_clock:
605  * @pipeline: the pipeline
606  *
607  * Let the pipeline select a clock automatically.
608  *
609  * MT safe.
610  */
611 void
612 gst_pipeline_auto_clock (GstPipeline * pipeline)
613 {
614   g_return_if_fail (pipeline != NULL);
615   g_return_if_fail (GST_IS_PIPELINE (pipeline));
616
617   GST_LOCK (pipeline);
618   GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
619
620   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
621   GST_UNLOCK (pipeline);
622
623   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");
624 }