docs/design/: Documentation updates and typo fixes.
[platform/upstream/gstreamer.git] / docs / design / part-states.txt
1 States
2 ======
3
4 Both elements and pads can be in different states. The states of the pads are 
5 linked to the state of the element so the design of the states is mainly
6 focused around the element states.
7
8 An element can be in 4 states. NULL, READY, PAUSED and PLAYING. When an element
9 is initially instantiated, it is in the NULL state.
10
11
12 State definitions
13 -----------------
14
15  - NULL:    This is the initial state of an element. 
16  - READY:   The element should be prepared to go to PAUSED.
17  - PAUSED:  The element should be ready to accept and process data. Sink
18             elements however only accept one buffer and then block.
19  - PLAYING: The same as PAUSED except for live sources and sinks. Sinks accept
20             and rendering data. Live sources produce data.
21
22 We call the sequence NULL->PLAYING an upwards state change and PLAYING->NULL
23 a downwards state change.
24
25
26 State transitions
27 -----------------
28
29 the following state changes are possible:
30
31  NULL -> READY
32    - The element must check if the resources it needs are available.
33      Device sinks and -sources typically try to probe the device to constain
34      their caps.
35
36  READY -> PAUSED
37    - The element opens the device and prepares itself for PLAYING.
38    - the element pads are activated in order to receive data in PAUSED.
39      Streaming threads are started.
40    - some elements might need to return ASYNC and complete the state change
41      when they have enough information. It is a requirement for sinks to
42      return ASYNC and complete the state change when they receive the first
43      buffer or EOS event (prerol). Sinks also block the dataflow when in PAUSED.
44    - a pipeline resets the running_time to 0.
45    - live sources return NO_PREROLL and don't generate data.
46  
47  PAUSED -> PLAYING
48    - most elements ignore this state change.
49    - The pipeline selects a clock and distributes this to all the children
50      before setting them to PLAYING. This means that it is only alowed to
51      synchronize on the clock in the PLAYING state.
52    - The pipeline uses the clock and the running_time to calculate the base_time.
53      The base_time is distributed to all children when performing the state
54      change.
55    - sink elements stop blocking on the preroll buffer or event and start
56      rendering the data. 
57    - sinks can post the EOS message in the PLAYING state. It is not allowed to
58      post EOS when not in the PLAYING state.
59    - while streaming in PAUSED or PLAYING elements can create and remove 
60      dynamic pads.
61    - live sources start generating data and return SUCCESS.
62
63  PLAYING -> PAUSED
64    - most elements ignore this state change.
65    - The pipeline calculates the running_time based on the last selected clock
66      and the base_time. It stores this information to continue playback when
67      going back to the PLAYING state.
68    - sinks unblock any clock wait calls.
69    - when a sink did not have a pending buffer to play, it returns ASYNC from 
70      this state change and complete the state change when they receive a new buffer
71      or an EOS event.
72    - any queued EOS messages are removed since they will be reposted when going
73      back to the PLAYING state. The EOS messages are queued in GstBins.
74    - live sources stop generating data and return NO_PREROLL.
75
76  PAUSED -> READY
77    - sinks unblock any waits in the preroll.
78    - elements unblock any waits on devices
79    - chain or get_range functions return WRONG_STATE.
80    - the element pads are deactivated so that streaming becomes impossible and
81      all streaming threads are stopped. 
82  
83  READY -> NULL
84    - element removes any dynamically created pads
85
86
87 State variables
88 ---------------
89
90 An element has 4 state variables that are protected with the object LOCK:
91
92   - STATE
93   - STATE_NEXT
94   - STATE_PENDING 
95   - STATE_RETURN
96
97 The STATE always reflects the current state of the element. 
98 The STATE_NEXT reflects the next state the element will go to.
99 The STATE_PENDING always reflects the required state of the element. 
100 The STATE_RETURN reflects the last return value of a state change.
101
102 The STATE_NEXT and STATE_PENDING can be VOID_PENDING if the element is in 
103 the right state. 
104
105 An element has a special lock to protect against concurrent invocations of
106 _set_state(), called the STATE_LOCK. 
107
108
109 Setting state on elements
110 -------------------------
111
112 The state of an element can be changed with _element_set_state(). When chaning
113 the state of an element all intermediate states will also be set on the element
114 until the final desired state is set.
115
116 The _set_state() function can return 3 possible values:
117
118   GST_STATE_FAILURE: The state change failed for some reason. The plugin should
119                      have posted an error message on the bus with information.
120   
121   GST_STATE_SUCCESS: The state change is completed successfully.
122
123   GST_STATE_ASYNC:   The state change will complete later on. This can happen 
124                      When the element needs a long time to perform the state
125                      change or for sinks that need to receive the first buffer
126                      before they can complete the state change (preroll).
127
128   GST_STATE_NO_PREROLL: The state change is completed successfully but the element
129                      will not be able to produce data in the PAUSED state.
130
131 In the case of an ASYNC state change, it is possible to proceed to the next
132 state before the current state change completed, however, the element will only
133 get to this next state before completing the previous ASYNC state change. 
134 After receiving an ASYNC return value, you can use _element_get_state() to poll 
135 the status of the element. If the polling returns SUCCESS, the element completed
136 the state change to the last requested state with _set_state().
137
138 When setting the state of an element, the STATE_PENDING is set to the required 
139 state. Then the state change function of the element is called and the result of 
140 that function is used to update the STATE and STATE_RETURN fields, STATE_NEXT,
141 STATE_PENDING and STATE_RETURN fields. If the function returned ASYNC, this result
142 is immediatly returned to the caller.
143
144
145 Getting state of elements
146 -------------------------
147
148 The _get_state() function takes 3 arguments, two pointers that will hold the
149 current and pending state and one GstClockTime that holds a timeout value. The 
150 function returns a GstElementStateReturn.
151
152  - If the element returned SUCCESS to the previous _set_state() function, this
153    function will return the last state set on the element and VOID_PENDING in
154    the pending state value. The function returns GST_STATE_SUCCESS.
155
156  - If the element returned NO_PREROLL to the previous _set_state() function, this
157    function will return the last state set on the element and VOID_PENDING in
158    the pending state value. The function returns GST_STATE_NO_PREROLL.
159
160  - If the element returned FAILURE to the previous _set_state() call, this 
161    funciton will return FAILURE with the state set to the current state of
162    the element and the pending state set to the value used in the last call
163    of _set_state().
164
165  - If the element returned ASYNC to the previous _set_state() call, this function
166    will wait for the element to complete its state change up to the amount of time
167    specified in the GstClockTime. 
168
169    * If the element does not complete the state change in the specified amount of 
170      time, this function will return ASYNC with the state set to the current state
171      and the pending state set to the pending state.
172
173    * If the element completes the state change within the specified timeout, this 
174      function returns the updated state and VOID_PENDING as the pending state.
175
176    * If the element aborts the ASYNC state change due to an error within the 
177      specified timeout, this function returns FAILURE with the state set to last
178      successfull state and pending set to the last attempt. The element should 
179      also post an error message on the bus with more information about the problem.
180
181
182 States in GstBin
183 ----------------
184
185 A GstBin manages the state of its children. It does this by propagating the state
186 changes performed on it to all of its children.  The _set_state() function on a 
187 bin will call the _set_state() function on all of its children. 
188
189 The children are iterated from the sink elements to the source elements. This makes
190 sure that when changing the state of an element, the downstream elements are in
191 the correct state to process the eventual buffers. In the case of a downwards
192 state change, the sink elements will shut down first which makes the upstream
193 elements shut down as well since the _push() function returns a GST_FLOW_WRONG_STATE
194 error.
195
196 If all the children return SUCCESS, the function returns SUCCESS as well. 
197
198 If one of the children returns FAILURE, the function returns FAILURE as well. In
199 this state it is possible that some elements successfuly changed state. The 
200 application can check which elements have a changed state, which were in error
201 and which were not affected by iterating the elements and calling _get_state()
202 on the elements.
203
204 If after calling the state function on all children, one of the children returned
205 ASYNC, the function returns ASYNC as well.
206
207 If after calling the state function on all children, one of the children returned
208 NO_PREROLL, the function returns NO_PREROLL as well. 
209
210 If both NO_PREROLL and ASYNC children are present, NO_PREROLL is returned.
211
212 The current state of the bin can be retrieved with _get_state(). 
213
214 If the bin is performing an ASYNC state change, it will automatically update its
215 current state fields when it receives state messages from the children.
216
217
218 Implementing states in elements
219 -------------------------------
220
221 READY
222 -----
223
224
225
226 upward state change
227 -------------------
228
229 Upward state changes always return ASYNC either if the STATE_PENDING is
230 reached or not.
231
232 Element:
233
234   A -> B => SUCCESS 
235     - commit state
236
237   A -> B => ASYNC 
238     - no commit state
239     - element commits state ASYNC
240
241   A -> B while ASYNC
242     - update STATE_PENDING state
243     - no commit state
244     - no change_state called on element
245
246 Bin:
247
248   A->B: all elements SUCCESS
249     - commit state
250
251   A->B: some elements ASYNC
252     - no commit state
253     - listen for commit messages on bus
254     - for each commit message, poll elements, this happens in another
255       thread.
256     - if no ASYNC elements, commit state, continue state change 
257       to STATE_PENDING
258
259 downward state change
260 ----------------------
261
262 Downward state changes only return ASYNC if the final state is ASYNC.
263 This is to make sure that it's not needed to wait for an element to
264 complete the preroll or other ASYNC state changes when one only wants to
265 shut down an element.
266
267 Element:
268
269   A -> B => SUCCESS 
270     - commit state
271
272   A -> B => ASYNC not final state
273     - commit state on behalf of element
274
275   A -> B => ASYNC final state
276     - element will commit ASYNC 
277
278 Bin:
279   
280   A -> B -> SUCCESS
281     - commit state
282
283   A -> B -> ASYNC not final state
284     - commit state on behalf of element, continue state change
285
286   A -> B => ASYNC final state
287     - no commit state
288     - listen for commit messages on bus
289     - for each commit message, poll elements
290     - if no ASYNC elements, commit state
291
292
293 Locking overview (element)
294 --------------------------
295
296 * Element commiting SUCCESS
297
298  - STATE_LOCK is taken in set_state
299  - change state is called if SUCCESS, commit state is called
300  - commit state calls change_state to next state change.
301  - if final state is reached, stack unwinds and result is returned to 
302    set_state and caller.
303
304
305  set_state(element)       change_state (element)   commit_state
306
307       |                         |                       |
308       |                         |                       |
309   STATE_LOCK                    |                       |
310       |                         |                       |
311       |------------------------>|                       | 
312       |                         |                       | 
313       |                         |                       | 
314       |                         | (do state change)     |
315       |                         |                       |
316       |                         |                       |
317       |                         | if SUCCESS            |
318       |                         |---------------------->|
319       |                         |                       | post message
320       |                         |                       |
321       |                         |<----------------------| if (!final) change_state (next)
322       |                         |                       | else SIGNAL
323       |                         |                       |
324       |                         |                       |
325       |                         |                       |
326       |<------------------------|                       |
327       |     SUCCESS               
328       | 
329   STATE_UNLOCK
330       |      
331     SUCCESS   
332            
333
334
335 * Element commiting ASYNC
336
337  - STATE_LOCK is taken in set_state
338  - change state is called and returns ASYNC
339  - ASYNC returned to the caller.
340  - element takes LOCK in streaming thread.
341  - element calls commit_state in streaming thread.
342  - commit state calls change_state to next state change.
343
344
345  set_state(element)       change_state (element)     stream_thread      commit_state (element)
346
347       |                         |                          |                  |
348       |                         |                          |                  |
349   STATE_LOCK                    |                          |                  |
350       |                         |                          |                  |
351       |------------------------>|                          |                  |
352       |                         |                          |                  |
353       |                         |                          |                  |
354       |                         | (start_task)             |                  |
355       |                         |                          |                  |
356       |                         |                     STREAM_LOCK             |
357       |                         |                          |...               |
358       |<------------------------|                          |                  |
359       |     ASYNC                                     STREAM_UNLOCK           |
360   STATE_UNLOCK                                             |                  |       
361       |                .....sync........               STATE_LOCK             |               
362     ASYNC                                                  |----------------->|
363                                                            |                  |
364                                                            |                  |---> post_message()
365                                                            |                  |---> if (!final) change_state (next)
366                                                            |                  |     else SIGNAL
367                                                            |<-----------------|
368                                                        STATE_UNLOCK
369                                                            |
370                                                       STREAM_LOCK
371                                                            | ...
372                                                       STREAM_UNLOCK
373
374 *********************************************
375 *********************************************
376
377 set_state cannot be called from multiple threads at the same time. The STATE_LOCK
378 prevents this.
379
380 state variables are protected with the LOCK.
381
382 calling set_state while gst_state is called should unlock the get_state with
383 an error. The cookie will do that.
384
385
386  set_state(element)
387
388   STATE_LOCK
389
390   LOCK
391   update current, next, pending state
392   cookie++
393   UNLOCK
394
395   change_state
396
397   STATE_UNLOCK
398  
399    
400
401