1 <chapter id="chapter-statemanage-states">
6 A state describes whether the element instance is initialized, whether it
7 is ready to transfer data and whether it is currently handling data. There
8 are four states defined in &GStreamer;: <classname>GST_STATE_NULL</classname>,
9 <classname>GST_STATE_READY</classname>, <classname>GST_STATE_PAUSED</classname>
10 and <classname>GST_STATE_PLAYING</classname>.
13 <classname>GST_STATE_NULL</classname> (from now on referred to as
14 <quote>NULL</quote>) is the default state of an element. In this state, it
15 has not allocated any runtime resources, it has not loaded any runtime
16 libraries and it can obviously not handle data.
19 <classname>GST_STATE_READY</classname> (from now on referred to as
20 <quote>READY</quote>) is the next state that an element can be in. In the
21 READY state, an element has all default resources (runtime-libraries,
22 runtime-memory) allocated. However, it has not yet allocated or defined
23 anything that is stream-specific. When going from NULL to READY state
24 (<classname>GST_STATE_NULL_TO_READY</classname>), an element should
25 allocate any non-stream-specific resources and should load runtime-loadable
26 libraries (if any). When going the other way around (from READY to NULL,
27 <classname>GST_STATE_READY_TO_NULL</classname>), an element should unload
28 these libraries and free all allocated resources. Examples of such
29 resources are hardware devices. Note that files are generally streams,
30 and these should thus be considered as stream-specific resources; therefore,
31 they should <emphasis>not</emphasis> be allocated in this state.
34 <classname>GST_STATE_PAUSED</classname> (from now on referred to as
35 <quote>PAUSED</quote>) is a state in which an element is by all means able
36 to handle data; the only 'but' here is that it doesn't actually handle
37 any data. When going from the READY state into the PAUSED state
38 (<classname>GST_STATE_READY_TO_PAUSED</classname>), the element will
39 usually not do anything at all: all stream-specific info is generally
40 handled in the <function>_link ()</function>, which is called during caps
41 negotiation. Exceptions to this rule are, for example, files: these are
42 considered stream-specific data (since one file is one stream), and should
43 thus be opened in this state change. When going from the PAUSED back to
44 READY (<classname>GST_STATE_PAUSED_TO_READY</classname>), all
45 stream-specific data should be discarded.
48 <classname>GST_STATE_PLAYING</classname> (from now on referred to as
49 <quote>PLAYING</quote>) is the highest state that an element can be in. It
50 is similar to PAUSED, except that now, data is actually passing over the
51 pipeline. The transition from PAUSED to PLAYING
52 (<classname>GST_STATE_PAUSED_TO_PLAYING</classname>) should be as small
53 as possible and would ideally cause no delay at all. The same goes for the
54 reverse transition (<classname>GST_STATE_PLAYING_TO_PAUSED</classname>).
57 <sect1 id="section-statemanage-filters">
59 Mangaging filter state
62 An element can be notified of state changes through a virtual function
63 pointer. Inside this function, the element can initialize any sort of
64 specific data needed by the element, and it can optionally fail to
65 go from one state to another.
68 static GstElementStateReturn
69 gst_my_filter_change_state (GstElement *element);
72 gst_my_filter_class_init (GstMyFilterClass *klass)
74 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
76 element_class->change_state = gst_my_filter_change_state;
79 static GstElementStateReturn
80 gst_my_filter_change_state (GstElement *element)
82 GstMyFilter *filter = GST_MY_FILTER (element);
84 switch (GST_STATE_TRANSITION (element)) {
85 case GST_STATE_NULL_TO_READY:
86 if (!gst_my_filter_allocate_memory (filter))
87 return GST_STATE_FAILURE;
89 case GST_STATE_READY_TO_NULL:
90 gst_my_filter_free_memory (filter);
96 if (GST_ELEMENT_CLASS (parent_class)->change_state)
97 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
99 return GST_STATE_SUCCESS;