gst/gstbin.c: Some documentation 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 scheduling and pipeline 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 all the scheduling issues, including
29  * threading, as well as provide simple interfaces to common functions, like
30  * 'Play'.
31  *
32  * gst_pipeline_new() is used to create a pipeline. when you are done with 
33  * the pipeline, use gst_object_unref() to free its resources including all
34  * added #GstElement objects (if not otherwiese referenced).
35  */
36
37 #include "gst_private.h"
38
39 #include "gstpipeline.h"
40 #include "gstinfo.h"
41 #include "gstsystemclock.h"
42
43 static GstElementDetails gst_pipeline_details =
44 GST_ELEMENT_DETAILS ("Pipeline object",
45     "Generic/Bin",
46     "Complete pipeline object",
47     "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
48
49 /* Pipeline signals and args */
50 enum
51 {
52   /* FILL ME */
53   LAST_SIGNAL
54 };
55
56 #define DEFAULT_DELAY 0
57 #define DEFAULT_PLAY_TIMEOUT  (2*GST_SECOND)
58 enum
59 {
60   ARG_0,
61   ARG_DELAY,
62   ARG_PLAY_TIMEOUT,
63   /* FILL ME */
64 };
65
66
67 static void gst_pipeline_base_init (gpointer g_class);
68 static void gst_pipeline_class_init (gpointer g_class, gpointer class_data);
69 static void gst_pipeline_init (GTypeInstance * instance, gpointer g_class);
70
71 static void gst_pipeline_dispose (GObject * object);
72 static void gst_pipeline_set_property (GObject * object, guint prop_id,
73     const GValue * value, GParamSpec * pspec);
74 static void gst_pipeline_get_property (GObject * object, guint prop_id,
75     GValue * value, GParamSpec * pspec);
76
77 static gboolean gst_pipeline_send_event (GstElement * element,
78     GstEvent * event);
79
80 static GstClock *gst_pipeline_get_clock_func (GstElement * element);
81 static GstStateChangeReturn gst_pipeline_change_state (GstElement * element,
82     GstStateChange transition);
83
84 static GstBinClass *parent_class = NULL;
85
86 /* static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 }; */
87
88 GType
89 gst_pipeline_get_type (void)
90 {
91   static GType pipeline_type = 0;
92
93   if (!pipeline_type) {
94     static const GTypeInfo pipeline_info = {
95       sizeof (GstPipelineClass),
96       gst_pipeline_base_init,
97       NULL,
98       (GClassInitFunc) gst_pipeline_class_init,
99       NULL,
100       NULL,
101       sizeof (GstPipeline),
102       0,
103       gst_pipeline_init,
104       NULL
105     };
106
107     pipeline_type =
108         g_type_register_static (GST_TYPE_BIN, "GstPipeline", &pipeline_info, 0);
109   }
110   return pipeline_type;
111 }
112
113 static void
114 gst_pipeline_base_init (gpointer g_class)
115 {
116   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
117
118   gst_element_class_set_details (gstelement_class, &gst_pipeline_details);
119 }
120
121 static void
122 gst_pipeline_class_init (gpointer g_class, gpointer class_data)
123 {
124   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
125   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
126   GstPipelineClass *klass = GST_PIPELINE_CLASS (g_class);
127
128   parent_class = g_type_class_peek_parent (klass);
129
130   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pipeline_set_property);
131   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pipeline_get_property);
132
133   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DELAY,
134       g_param_spec_uint64 ("delay", "Delay",
135           "Expected delay needed for elements "
136           "to spin up to PLAYING in nanoseconds", 0, G_MAXUINT64, DEFAULT_DELAY,
137           G_PARAM_READWRITE));
138   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLAY_TIMEOUT,
139       g_param_spec_uint64 ("play-timeout", "Play Timeout",
140           "Max timeout for going to PLAYING in nanoseconds", 0, G_MAXUINT64,
141           DEFAULT_PLAY_TIMEOUT, G_PARAM_READWRITE));
142
143   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pipeline_dispose);
144
145   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_pipeline_send_event);
146   gstelement_class->change_state =
147       GST_DEBUG_FUNCPTR (gst_pipeline_change_state);
148   gstelement_class->get_clock = GST_DEBUG_FUNCPTR (gst_pipeline_get_clock_func);
149 }
150
151 static void
152 gst_pipeline_init (GTypeInstance * instance, gpointer g_class)
153 {
154   GstPipeline *pipeline = GST_PIPELINE (instance);
155
156   pipeline->delay = DEFAULT_DELAY;
157   pipeline->play_timeout = DEFAULT_PLAY_TIMEOUT;
158 }
159
160 static void
161 gst_pipeline_dispose (GObject * object)
162 {
163   GstPipeline *pipeline = GST_PIPELINE (object);
164
165   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pipeline, "dispose");
166
167   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
168
169   G_OBJECT_CLASS (parent_class)->dispose (object);
170 }
171
172 static void
173 gst_pipeline_set_property (GObject * object, guint prop_id,
174     const GValue * value, GParamSpec * pspec)
175 {
176   GstPipeline *pipeline = GST_PIPELINE (object);
177
178   GST_LOCK (pipeline);
179   switch (prop_id) {
180     case ARG_DELAY:
181       pipeline->delay = g_value_get_uint64 (value);
182       break;
183     case ARG_PLAY_TIMEOUT:
184       pipeline->play_timeout = g_value_get_uint64 (value);
185       break;
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189   }
190   GST_UNLOCK (pipeline);
191 }
192
193 static void
194 gst_pipeline_get_property (GObject * object, guint prop_id,
195     GValue * value, GParamSpec * pspec)
196 {
197   GstPipeline *pipeline = GST_PIPELINE (object);
198
199   GST_LOCK (pipeline);
200   switch (prop_id) {
201     case ARG_DELAY:
202       g_value_set_uint64 (value, pipeline->delay);
203       break;
204     case ARG_PLAY_TIMEOUT:
205       g_value_set_uint64 (value, pipeline->play_timeout);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211   GST_UNLOCK (pipeline);
212 }
213
214 static gboolean
215 do_pipeline_seek (GstElement * element, GstEvent * event)
216 {
217   gdouble rate;
218   GstSeekFlags flags;
219   gboolean flush;
220   gboolean was_playing = FALSE;
221   gboolean res;
222
223   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
224
225   flush = flags & GST_SEEK_FLAG_FLUSH;
226
227   if (flush) {
228     GstState state;
229     GTimeVal timeout;
230
231     GST_TIME_TO_TIMEVAL (0, timeout);
232     /* need to call _get_state() since a bin state is only updated
233      * with this call. */
234     gst_element_get_state (element, &state, NULL, &timeout);
235     was_playing = state == GST_STATE_PLAYING;
236
237     if (was_playing)
238       gst_element_set_state (element, GST_STATE_PAUSED);
239   }
240
241   res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
242
243   if (flush && res) {
244     /* need to reset the stream time to 0 after a flushing seek */
245     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
246     if (was_playing) {
247       /* and continue playing */
248       gst_element_set_state (element, GST_STATE_PLAYING);
249     }
250   }
251   return res;
252 }
253
254 /* sending a seek event on the pipeline pauses the pipeline if it
255  * was playing.
256  */
257 static gboolean
258 gst_pipeline_send_event (GstElement * element, GstEvent * event)
259 {
260   gboolean res;
261   GstEventType event_type = GST_EVENT_TYPE (event);
262
263   switch (event_type) {
264     case GST_EVENT_SEEK:
265       res = do_pipeline_seek (element, event);
266       break;
267     default:
268       res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
269       break;
270   }
271
272   return res;
273 }
274
275 /**
276  * gst_pipeline_new:
277  * @name: name of new pipeline
278  *
279  * Create a new pipeline with the given name.
280  *
281  * Returns: newly created GstPipeline
282  *
283  * MT safe.
284  */
285 GstElement *
286 gst_pipeline_new (const gchar * name)
287 {
288   return gst_element_factory_make ("pipeline", name);
289 }
290
291 /* MT safe */
292 static GstStateChangeReturn
293 gst_pipeline_change_state (GstElement * element, GstStateChange transition)
294 {
295   GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
296   GstPipeline *pipeline = GST_PIPELINE (element);
297   GstClockTime play_timeout;
298   GstClock *clock;
299
300   switch (transition) {
301     case GST_STATE_CHANGE_NULL_TO_READY:
302       GST_LOCK (element);
303       if (element->bus)
304         gst_bus_set_flushing (element->bus, FALSE);
305       GST_UNLOCK (element);
306       break;
307     case GST_STATE_CHANGE_READY_TO_PAUSED:
308       break;
309     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
310       /* when going to playing, select a clock */
311       if ((clock = gst_element_get_clock (element))) {
312         GstClockTime start_time;
313
314         /* distribute the clock */
315         gst_element_set_clock (element, clock);
316
317         /* get start time */
318         start_time = gst_clock_get_time (clock);
319         gst_object_unref (clock);
320
321         GST_LOCK (element);
322         element->base_time = start_time -
323             pipeline->stream_time + pipeline->delay;
324         GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", start_time=%"
325             GST_TIME_FORMAT ", base time %" GST_TIME_FORMAT,
326             GST_TIME_ARGS (pipeline->stream_time),
327             GST_TIME_ARGS (start_time), GST_TIME_ARGS (element->base_time));
328         GST_UNLOCK (element);
329       } else {
330         GST_DEBUG ("no clock, using base time of 0");
331         gst_element_set_base_time (element, 0);
332       }
333       break;
334     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
335     case GST_STATE_CHANGE_PAUSED_TO_READY:
336     case GST_STATE_CHANGE_READY_TO_NULL:
337       break;
338   }
339
340   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
341
342   switch (transition) {
343     case GST_STATE_CHANGE_NULL_TO_READY:
344       break;
345     case GST_STATE_CHANGE_READY_TO_PAUSED:
346       gst_pipeline_set_new_stream_time (pipeline, 0);
347       break;
348     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
349       break;
350     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
351       GST_LOCK (element);
352       if ((clock = element->clock)) {
353         GstClockTime now;
354
355         gst_object_ref (clock);
356         GST_UNLOCK (element);
357
358         /* calculate the time when we stopped */
359         now = gst_clock_get_time (clock);
360         gst_object_unref (clock);
361
362         GST_LOCK (element);
363         /* store the current stream time */
364         pipeline->stream_time = now - element->base_time;
365         GST_DEBUG ("stream_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
366             ", base time %" GST_TIME_FORMAT,
367             GST_TIME_ARGS (pipeline->stream_time),
368             GST_TIME_ARGS (now), GST_TIME_ARGS (element->base_time));
369       }
370       GST_UNLOCK (element);
371       break;
372     case GST_STATE_CHANGE_PAUSED_TO_READY:
373       break;
374     case GST_STATE_CHANGE_READY_TO_NULL:
375       GST_LOCK (element);
376       if (element->bus) {
377         gst_bus_set_flushing (element->bus, TRUE);
378       }
379       GST_UNLOCK (element);
380       break;
381   }
382
383   if (result == GST_STATE_CHANGE_ASYNC) {
384     GST_LOCK (pipeline);
385     play_timeout = pipeline->play_timeout;
386     GST_UNLOCK (pipeline);
387   } else {
388     play_timeout = 0;
389   }
390
391   /* we wait for async state changes ourselves when we are in an
392    * intermediate state. */
393   if (play_timeout > 0) {
394     GTimeVal *timeval, timeout;
395
396     GST_STATE_UNLOCK (pipeline);
397
398     if (play_timeout == G_MAXUINT64) {
399       timeval = NULL;
400     } else {
401       GST_TIME_TO_TIMEVAL (play_timeout, timeout);
402       timeval = &timeout;
403     }
404
405     result = gst_element_get_state (element, NULL, NULL, timeval);
406     if (result == GST_STATE_CHANGE_ASYNC) {
407       GST_WARNING_OBJECT (pipeline,
408           "timeout in PREROLL, forcing next state change");
409       g_warning ("timeout in PREROLL, forcing next state change");
410       result = GST_STATE_CHANGE_SUCCESS;
411     }
412
413     GST_STATE_LOCK (pipeline);
414   }
415
416   return result;
417 }
418
419 /**
420  * gst_pipeline_get_bus:
421  * @pipeline: the pipeline
422  *
423  * Gets the #GstBus of this pipeline.
424  *
425  * Returns: a GstBus
426  *
427  * MT safe.
428  */
429 GstBus *
430 gst_pipeline_get_bus (GstPipeline * pipeline)
431 {
432   return gst_element_get_bus (GST_ELEMENT (pipeline));
433 }
434
435 /**
436  * gst_pipeline_set_new_stream_time:
437  * @pipeline: the pipeline
438  * @time: the new stream time to set
439  *
440  * Set the new stream time of the pipeline. The stream time is used to
441  * set the base time on the elements (see @gst_element_set_base_time())
442  * in the PAUSED->PLAYING state transition.
443  *
444  * MT safe.
445  */
446 void
447 gst_pipeline_set_new_stream_time (GstPipeline * pipeline, GstClockTime time)
448 {
449   g_return_if_fail (GST_IS_PIPELINE (pipeline));
450
451   GST_LOCK (pipeline);
452   pipeline->stream_time = time;
453   GST_DEBUG ("%s: set new stream_time to %" GST_TIME_FORMAT,
454       GST_ELEMENT_NAME (pipeline), GST_TIME_ARGS (time));
455   GST_UNLOCK (pipeline);
456 }
457
458 /**
459  * gst_pipeline_get_last_stream_time:
460  * @pipeline: the pipeline
461  *
462  * Gets the last stream time of the pipeline. If the pipeline is PLAYING,
463  * the returned time is the stream time used to configure the elements
464  * in the PAUSED->PLAYING state. If the pipeline is PAUSED, the returned
465  * time is the stream time when the pipeline was paused.
466  *
467  * Returns: a GstClockTime
468  *
469  * MT safe.
470  */
471 GstClockTime
472 gst_pipeline_get_last_stream_time (GstPipeline * pipeline)
473 {
474   GstClockTime result;
475
476   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), GST_CLOCK_TIME_NONE);
477
478   GST_LOCK (pipeline);
479   result = pipeline->stream_time;
480   GST_UNLOCK (pipeline);
481
482   return result;
483 }
484
485 static GstClock *
486 gst_pipeline_get_clock_func (GstElement * element)
487 {
488   GstClock *clock = NULL;
489   GstPipeline *pipeline = GST_PIPELINE (element);
490
491   /* if we have a fixed clock, use that one */
492   GST_LOCK (pipeline);
493   if (GST_FLAG_IS_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK)) {
494     clock = pipeline->fixed_clock;
495     gst_object_ref (clock);
496     GST_UNLOCK (pipeline);
497
498     GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)",
499         clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
500   } else {
501     GST_UNLOCK (pipeline);
502     clock =
503         GST_ELEMENT_CLASS (parent_class)->get_clock (GST_ELEMENT (pipeline));
504     /* no clock, use a system clock */
505     if (!clock) {
506       clock = gst_system_clock_obtain ();
507
508       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained system clock: %p (%s)",
509           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
510     } else {
511       GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline obtained clock: %p (%s)",
512           clock, clock ? GST_STR_NULL (GST_OBJECT_NAME (clock)) : "-");
513     }
514   }
515   return clock;
516 }
517
518 /**
519  * gst_pipeline_get_clock:
520  * @pipeline: the pipeline
521  *
522  * Gets the current clock used by the pipeline.
523  *
524  * Returns: a GstClock
525  */
526 GstClock *
527 gst_pipeline_get_clock (GstPipeline * pipeline)
528 {
529   g_return_val_if_fail (GST_IS_PIPELINE (pipeline), NULL);
530
531   return gst_pipeline_get_clock_func (GST_ELEMENT (pipeline));
532 }
533
534
535 /**
536  * gst_pipeline_use_clock:
537  * @pipeline: the pipeline
538  * @clock: the clock to use
539  *
540  * Force the pipeline to use the given clock. The pipeline will
541  * always use the given clock even if new clock providers are added
542  * to this pipeline.
543  *
544  * MT safe.
545  */
546 void
547 gst_pipeline_use_clock (GstPipeline * pipeline, GstClock * clock)
548 {
549   g_return_if_fail (GST_IS_PIPELINE (pipeline));
550
551   GST_LOCK (pipeline);
552   GST_FLAG_SET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
553
554   gst_object_replace ((GstObject **) & pipeline->fixed_clock,
555       (GstObject *) clock);
556   GST_UNLOCK (pipeline);
557
558   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using fixed clock %p (%s)", clock,
559       (clock ? GST_OBJECT_NAME (clock) : "nil"));
560 }
561
562 /**
563  * gst_pipeline_set_clock:
564  * @pipeline: the pipeline
565  * @clock: the clock to set
566  *
567  * Set the clock for the pipeline. The clock will be distributed
568  * to all the elements managed by the pipeline.
569  *
570  * MT safe.
571  */
572 void
573 gst_pipeline_set_clock (GstPipeline * pipeline, GstClock * clock)
574 {
575   g_return_if_fail (pipeline != NULL);
576   g_return_if_fail (GST_IS_PIPELINE (pipeline));
577
578   GST_ELEMENT_CLASS (parent_class)->set_clock (GST_ELEMENT (pipeline), clock);
579 }
580
581 /**
582  * gst_pipeline_auto_clock:
583  * @pipeline: the pipeline
584  *
585  * Let the pipeline select a clock automatically.
586  *
587  * MT safe.
588  */
589 void
590 gst_pipeline_auto_clock (GstPipeline * pipeline)
591 {
592   g_return_if_fail (pipeline != NULL);
593   g_return_if_fail (GST_IS_PIPELINE (pipeline));
594
595   GST_LOCK (pipeline);
596   GST_FLAG_UNSET (pipeline, GST_PIPELINE_FLAG_FIXED_CLOCK);
597
598   gst_object_replace ((GstObject **) & pipeline->fixed_clock, NULL);
599   GST_UNLOCK (pipeline);
600
601   GST_CAT_DEBUG (GST_CAT_CLOCK, "pipeline using automatic clock");
602 }