Git init
[framework/multimedia/gstreamer0.10.git] / docs / design / part-clocks.txt
1 Clocks
2 ------
3
4 The GstClock returns a monotonically increasing time with the method 
5 _get_time(). Its accuracy and base time depends on the specific clock 
6 implementation but time is always expessed in nanoseconds. Since the
7 baseline of the clock is undefined, the clock time returned is not
8 meaningful in itself, what matters are the deltas between two clock
9 times.
10 The time reported by the clock is called the absolute_time.
11
12
13 Clock Selection
14 ~~~~~~~~~~~~~~~
15
16 To synchronize the different elements, the GstPipeline is responsible for
17 selecting and distributing a global GstClock for all the elements in it.
18
19 This selection happens whenever the pipeline goes to PLAYING. Whenever an
20 element is added/removed from the pipeline, this selection will be redone in the
21 next state change to PLAYING. Adding an element that can provide a clock will
22 post a GST_MESSAGE_CLOCK_PROVIDE message on the bus to inform parent bins of the
23 fact that a clock recalculation is needed. 
24
25 When a clock is selected, a NEW_CLOCK message is posted on the bus signaling the
26 clock to the application.
27
28 When the element that provided the clock is removed from the pipeline, a
29 CLOCK_LOST message is posted. The application must then set the pipeline to
30 PAUSED and PLAYING again in order to let the pipeline select a new clock
31 and distribute a new base time.
32
33 The clock selection is performed as part of the state change from PAUSED to
34 PLAYING and is described in part-states.txt.
35
36
37 Clock features
38 ~~~~~~~~~~~~~~
39
40 The clock supports periodic and single shot clock notifications both
41 synchronous and asynchronous.
42
43 One first needs to create a GstClockID for the periodic or single shot
44 notification using _clock_new_single_shot_id() or _clock_new_periodic_id().
45
46 To perform a blocking wait for the specific time of the GstClockID use the
47 gst_clock_id_wait(). To receive a callback when the specific time is reached
48 in the clock use gst_clock_id_wait_async(). Both these calls can be interrupted
49 with the gst_clock_id_unschedule() call. If the blocking wait is unscheduled
50 a return value of GST_CLOCK_UNSCHEDULED is returned.
51
52 The async callbacks can happen from any thread, either provided by the
53 core or from a streaming thread. The application should be prepared for this.
54
55 A GstClockID that has been unscheduled cannot be used again for any wait
56 operation. 
57
58 It is possible to perform a blocking wait on the same ID from multiple 
59 threads. However, registering the same ID for multiple async notifications is 
60 not possible, the callback will only be called once.
61
62 None of the wait operations unref the GstClockID, the owner is 
63 responsible for unreffing the ids itself. This holds for both periodic and
64 single shot notifications. The reason being that the owner of the ClockID
65 has to keep a handle to the ID to unblock the wait on FLUSHING events
66 or state changes and if we unref it automatically, the handle might be
67 invalid.
68
69 These clock operations do not operate on the stream time, so the callbacks
70 will also occur when not in PLAYING state as if the clock just keeps on 
71 running. Some clocks however do not progress when the element that provided
72 the clock is not PLAYING.
73
74
75 Clock implementations
76 ~~~~~~~~~~~~~~~~~~~~~
77
78 The GStreamer core provides a GstSystemClock based on the system time. 
79 Asynchronous callbacks are scheduled from an internal thread.
80
81 Clock implementors are encouraged to subclass this systemclock as it
82 implements the async notification.
83
84 Subclasses can however override all of the important methods for sync and
85 async notifications to implement their own callback methods or blocking
86 wait operations. 
87
88