gst/gstpipeline.c: Fix small typo in docs.
[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 /**
24  * SECTION:gstpipeline
25  * @short_description: Top-level bin with clocking and bus management
26                        functionality.
27  * @see_also: #GstBin
28  *
29  * In almost all cases, you'll want to use a GstPipeline when creating a filter
30  * graph.  The GstPipeline will manage the selection and distribution of a
31  * global
32  * clock as well as provide a GstBus to the application.
33  *
34  * The pipeline will also use the selected clock to calculate the stream time
35  * of the pipeline.
36  *
37  * When sending a seek event to a GstPipeline, it will make sure that the
38  * pipeline is properly PAUSED and resumed as well as update the new stream
39  * time after the seek.
40  *
41  * gst_pipeline_new() is used to create a pipeline. when you are done with
42  * the pipeline, use gst_object_unref() to free its resources including all
43  * added #GstElement objects (if not otherwise referenced).
44  */
45
46 #include "gst_private.h"
47 #include "gsterror.h"
48 #include "gst-i18n-lib.h"
49
50 #include "gstpipeline.h"
51 #include "gstinfo.h"
52 #include "gstsystemclock.h"
53
54 static GstElementDetails gst_pipeline_details =
55 GST_ELEMENT_DETAILS ("Pipeline object",
56     "Generic/Bin",
57     "Complete pipeline object",
58     "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
59
60 /* Pipeline signals and args */
61 enum
62 {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66
67 #define DEFAULT_DELAY 0
68
69 enum
70 {
71   PROP_0,
72   PROP_DELAY,
73   /* FILL ME */
74 };
75
76
77 static void gst_pipeline_base_init (gpointer g_class);
78 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
79 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
80
81 static void gst_pipeline_dispose (GObject * object);
82 static void gst_pipeline_set_property (GObject * object, guint prop_id,
83     const GValue * value, GParamSpec * pspec);
84 static void gst_pipeline_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86
87 static gboolean gst_pipeline_send_event (GstElement * element,
88     GstEvent * event);
89
90 static GstClock *gst_pipeline_provide_clock_func (GstElement * element);
91 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
92     GstStateChange transition);
93
94 static GstBinClass *parent_class = NULL;
95
96 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
97
98 GType
99 gst_pipeline_get_type (void)
100 {
101   static GType pipeline_type = 0;
102
103   if (!pipeline_type) {
104     static const GTypeInfo pipeline_info = {
105       sizeof (GstPipelineClass),
106       gst_pipeline_base_init,
107       NULL,
108       (GClassInitFunc) gst_pipeline_class_init,
109       NULL,
110       NULL,
111       sizeof (GstPipeline),
112       0,
113       gst_pipeline_init,
114       NULL
115     };
116
117     pipeline_type =
118         g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
119   }
120   return pipeline_type;
121 }
122
123 static void
124 gst_pipeline_base_init (gpointer g_class)
125 {
126   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
127
128   gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
129 }
130
131 static void
132 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
135   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
136   GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
137
138   parent_class = g_type_class_peek_parent (klass);
139
140   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
141   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
142
143   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
144       g_param_spec_uint64 ("delay", "Delay",
145           "Expected delay needed for elements "
146           "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
147           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
166   bus = g_object_new (gst_bus_get_type (), NULL);
167   gst_element_set_bus (GST_ELEMENT_CAST (pipeline), bus);
168   gst_object_unref (bus);
169 }
170
171 static void
172 gst_pipeline_dispose (GObject * object)
173 {
174   GstPipeline *pipeline = GST_PIPELINE (object);
175
176   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
177
178   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
179
180   G_OBJECT_CLASS (parent_class)->dispose (object);
181 }
182
183 static void
184 gst_pipeline_set_property (GObject * object, guint prop_id,
185     const GValue * value, GParamSpec * pspec)
186 {
187   GstPipeline *pipeline = GST_PIPELINE (object);
188
189   GST_OBJECT_LOCK (pipeline);
190   switch (prop_id) {
191     case PROP_DELAY:
192       pipeline->delay = g_value_get_uint64 (value);
193       break;
194     default:
195       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196       break;
197   }
198   GST_OBJECT_UNLOCK (pipeline);
199 }
200
201 static void
202 gst_pipeline_get_property (GObject * object, guint prop_id,
203     GValue * value, GParamSpec * pspec)
204 {
205   GstPipeline *pipeline = GST_PIPELINE (object);
206
207   GST_OBJECT_LOCK (pipeline);
208   switch (prop_id) {
209     case PROP_DELAY:
210       g_value_set_uint64 (value, pipeline->delay);
211       break;
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214       break;
215   }
216   GST_OBJECT_UNLOCK (pipeline);
217 }
218
219 static gboolean
220 do_pipeline_seek (GstElement * element, GstEvent * event)
221 {
222   gdouble rate;
223   GstSeekFlags flags;
224   gboolean flush;
225   gboolean was_playing = FALSE;
226   gboolean res;
227
228   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
229
230   flush = flags & GST_SEEK_FLAG_FLUSH;
231
232   if (flush) {
233     GstState state;
234
235     /* need to call _get_state() since a bin state is only updated
236      * with this call. */
237     gst_element_get_state (element, &state, NULL, 0);
238     was_playing = state == GST_STATE_PLAYING;
239
240     if (was_playing) {
241       gst_element_set_state (element, GST_STATE_PAUSED);
242     }
243   }
244
245   res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
246
247   if (flush && res) {
248     gboolean need_reset;
249
250     GST_OBJECT_LOCK (element);
251     need_reset = GST_PIPELINE (element)->stream_time != GST_CLOCK_TIME_NONE;
252     GST_OBJECT_UNLOCK (element);
253
254     /* need to reset the stream time to 0 after a flushing seek, unless the user
255        explicitly disabled this behavior by setting stream time to NONE */
256     if (need_reset)
257       gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
258
259     if (was_playing)
260       /* and continue playing */
261       gst_element_set_state (element, GST_STATE_PLAYING);
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   GstClock *clock;
310
311   switch (transition) {
312     case GST_STATE_CHANGE_NULL_TO_READY:
313       GST_OBJECT_LOCK (element);
314       if (element->bus)
315         gst_bus_set_flushing (element->bus, FALSE);
316       GST_OBJECT_UNLOCK (element);
317       break;
318     case GST_STATE_CHANGE_READY_TO_PAUSED:
319       break;
320     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
321     {
322       GstClockTime new_base_time;
323
324       /* when going to playing, select a clock */
325       clock = gst_element_provide_clock (element);
326
327       if (clock) {
328         GstClockTime start_time, stream_time, delay;
329         gboolean new_clock;
330
331         start_time = gst_clock_get_time (clock);
332
333         GST_OBJECT_LOCK (element);
334         new_clock = element->clock != clock;
335         stream_time = pipeline->stream_time;
336         delay = pipeline->delay;
337         GST_OBJECT_UNLOCK (element);
338
339         if (new_clock) {
340           /* now distribute the clock (which could be NULL I guess) */
341           if (!gst_element_set_clock (element, clock))
342             goto invalid_clock;
343
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         if (stream_time != GST_CLOCK_TIME_NONE)
350           new_base_time = start_time - stream_time + delay;
351         else
352           new_base_time = GST_CLOCK_TIME_NONE;
353
354         gst_object_unref (clock);
355       } else {
356         GST_DEBUG ("no clock, using base time of 0");
357         new_base_time = 0;
358       }
359
360       if (new_base_time != GST_CLOCK_TIME_NONE)
361         gst_element_set_base_time (element, new_base_time);
362       else
363         GST_DEBUG_OBJECT (pipeline,
364             "NOT adjusting base time because stream time is NONE");
365     }
366       break;
367     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
368     case GST_STATE_CHANGE_PAUSED_TO_READY:
369     case GST_STATE_CHANGE_READY_TO_NULL:
370       break;
371   }
372
373   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
374
375   switch (transition) {
376     case GST_STATE_CHANGE_NULL_TO_READY:
377       break;
378     case GST_STATE_CHANGE_READY_TO_PAUSED:
379     {
380       gboolean need_reset;
381
382       GST_OBJECT_LOCK (element);
383       need_reset = pipeline->stream_time != GST_CLOCK_TIME_NONE;
384       GST_OBJECT_UNLOCK (element);
385
386       if (need_reset)
387         gst_pipeline_set_new_stream_time (pipeline, 0);
388     }
389       break;
390     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
391       break;
392     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
393       GST_OBJECT_LOCK (element);
394       if ((clock = element->clock)) {
395         GstClockTime now;
396
397         gst_object_ref (clock);
398         GST_OBJECT_UNLOCK (element);
399
400         /* calculate the time when we stopped */
401         now = gst_clock_get_time (clock);
402         gst_object_unref (clock);
403
404         GST_OBJECT_LOCK (element);
405         /* store the current stream time */
406         if (pipeline->stream_time != GST_CLOCK_TIME_NONE)
407           pipeline->stream_time = now - element->base_time;
408         GST_DEBUG_OBJECT (element,
409             "stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
410             ", base time %" GST_TIME_FORMAT,
411             GST_TIME_ARGS (pipeline->stream_time), GST_TIME_ARGS (now),
412             GST_TIME_ARGS (element->base_time));
413       }
414       GST_OBJECT_UNLOCK (element);
415       break;
416     case GST_STATE_CHANGE_PAUSED_TO_READY:
417       break;
418     case GST_STATE_CHANGE_READY_TO_NULL:
419       GST_OBJECT_LOCK (element);
420       if (element->bus) {
421         gst_bus_set_flushing (element->bus, TRUE);
422       }
423       GST_OBJECT_UNLOCK (element);
424       break;
425   }
426   return result;
427
428 invalid_clock:
429   {
430     GST_ELEMENT_ERROR (pipeline, CORE, CLOCK,
431         (_("Selected clock cannot be used in pipeline.")),
432         ("Pipeline cannot operate with selected clock"));
433     GST_DEBUG_OBJECT (pipeline,
434         "Pipeline cannot operate with selected clock %p", clock);
435     return GST_STATE_CHANGE_FAILURE;
436   }
437 }
438
439 /**
440  * gst_pipeline_get_bus:
441  * @pipeline: the pipeline
442  *
443  * Gets the #GstBus of this pipeline.
444  *
445  * Returns: a GstBus
446  *
447  * MT safe.
448  */
449 GstBus *
450 gst_pipeline_get_bus (GstPipeline * pipeline)
451 {
452   return gst_element_get_bus (GST_ELEMENT (pipeline));
453 }
454
455 /**
456  * gst_pipeline_set_new_stream_time:
457  * @pipeline: the pipeline
458  * @time: the new stream time to set
459  *
460  * Set the new stream time of the pipeline. The stream time is used to
461  * set the base time on the elements (see @gst_element_set_base_time())
462  * in the PAUSED->PLAYING state transition.
463  *
464  * Setting @time to #GST_CLOCK_TIME_NONE will disable the pipeline's management
465  * of element base time. The application will then be responsible for
466  * performing base time distribution. This is sometimes useful if you want to
467  * synchronize capture from multiple pipelines, and you can also ensure that the
468  * pipelines have the same clock.
469  *
470  * MT safe.
471  */
472 void
473 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
474 {
475   g_return_if_fail (GST_IS_PIPELINE (pipeline));
476
477   GST_OBJECT_LOCK (pipeline);
478   pipeline->stream_time = time;
479   GST_OBJECT_UNLOCK (pipeline);
480
481   GST_DEBUG_OBJECT (pipeline, "set new stream_time to %" GST_TIME_FORMAT,
482       GST_TIME_ARGS (time));
483
484   if (time == GST_CLOCK_TIME_NONE)
485     GST_DEBUG_OBJECT (pipeline, "told not to adjust base time");
486 }
487
488 /**
489  * gst_pipeline_get_last_stream_time:
490  * @pipeline: the pipeline
491  *
492  * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
493  * the returned time is the stream time used to configure the elements
494  * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
495  * time is the stream time when the pipeline was paused.
496  *
497  * Returns: a GstClockTime
498  *
499  * MT safe.
500  */
501 GstClockTime
502 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
503 {
504   GstClockTime result;
505
506   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
507
508   GST_OBJECT_LOCK (pipeline);
509   result = pipeline->stream_time;
510   GST_OBJECT_UNLOCK (pipeline);
511
512   return result;
513 }
514
515 static GstClock *
516 gst_pipeline_provide_clock_func (GstElement * element)
517 {
518   GstClock *clock = NULL;
519   GstPipeline *pipeline = GST_PIPELINE (element);
520
521   /* if we have a fixed clock, use that one */
522   GST_OBJECT_LOCK (pipeline);
523   if (GST_OBJECT_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
524     clock = pipeline->fixed_clock;
525     gst_object_ref (clock);
526     GST_OBJECT_UNLOCK (pipeline);
527
528     GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
529         clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
530   } else {
531     GST_OBJECT_UNLOCK (pipeline);
532     clock =
533         GST_ELEMENT_CLASS (parent_class)->
534         provide_clock (GST_ELEMENT (pipeline));
535     /* no clock, use a system clock */
536     if (!clock) {
537       clock = gst_system_clock_obtain ();
538
539       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
540           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
541     } else {
542       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
543           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
544     }
545   }
546   return clock;
547 }
548
549 /**
550  * gst_pipeline_get_clock:
551  * @pipeline: the pipeline
552  *
553  * Gets the current clock used by the pipeline.
554  *
555  * Returns: a GstClock
556  */
557 GstClock *
558 gst_pipeline_get_clock (GstPipeline * pipeline)
559 {
560   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
561
562   return gst_pipeline_provide_clock_func (GST_ELEMENT (pipeline));
563 }
564
565
566 /**
567  * gst_pipeline_use_clock:
568  * @pipeline: the pipeline
569  * @clock: the clock to use
570  *
571  * Force the pipeline to use the given clock. The pipeline will
572  * always use the given clock even if new clock providers are added
573  * to this pipeline.
574  *
575  * MT safe.
576  */
577 void
578 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
579 {
580   g_return_if_fail (GST_IS_PIPELINE (pipeline));
581
582   GST_OBJECT_LOCK (pipeline);
583   GST_OBJECT_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
584
585   gst_object_replace ((GstObject **) & pipeline->fixed_clock,
586       (GstObject *) clock);
587   GST_OBJECT_UNLOCK (pipeline);
588
589   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
590       (clock ? GST_OBJECT_NAME (clock) : "nil"));
591 }
592
593 /**
594  * gst_pipeline_set_clock:
595  * @pipeline: the pipeline
596  * @clock: the clock to set
597  *
598  * Set the clock for the pipeline. The clock will be distributed
599  * to all the elements managed by the pipeline.
600  *
601  * Returns: TRUE if the clock could be set on the pipeline.
602  *
603  * MT safe.
604  */
605 gboolean
606 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
607 {
608   g_return_val_if_fail (pipeline != NULL, FALSE);
609   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), FALSE);
610
611   return GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline),
612       clock);
613 }
614
615 /**
616  * gst_pipeline_auto_clock:
617  * @pipeline: the pipeline
618  *
619  * Let the pipeline select a clock automatically.
620  *
621  * MT safe.
622  */
623 void
624 gst_pipeline_auto_clock (GstPipeline * pipeline)
625 {
626   g_return_if_fail (pipeline != NULL);
627   g_return_if_fail (GST_IS_PIPELINE (pipeline));
628
629   GST_OBJECT_LOCK (pipeline);
630   GST_OBJECT_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
631
632   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
633   GST_OBJECT_UNLOCK (pipeline);
634
635   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");
636 }