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