f609ad429a2ab2cf9c00c7e941cfcaa46991a666
[platform/upstream/gstreamer.git] / docs / random / events
1 EVENTS RFC
2 ==========
3
4
5 Scope
6 -----
7 This document tries to describe a possible implementation of an event
8 system for GStreamer that is able to handle all known problems and works
9 better than the current (0.3.1) system which evolved over time.
10
11
12 Definition 
13 ----------
14
15 The event system is designed to be a mechanism for communication between 
16 elements. They are used to get information to the right point when this
17 point cannot be known in advance.
18 Events can be generated by either an element or the application and are
19 processed by elements.
20
21
22 Event Handling
23 --------------
24
25 Events may be inserted into a pipeline in the PAUSED or PLAYING state. Some
26 events may travel during the PAUSED state, others may only travel when PLAYING.
27 The insertion of events during the NULL or READY state should be supported if
28 at all possible. Events may not be processed in those states though.
29 After an event is inserted into the pipeline, no assumption may be made on how
30 the event will be processed. It is eg wrong to assume that an event that is 
31 inserted at the beginning of the pipeline may come out at the end.
32 There are 3 different directions an event can be processed.
33
34 * downstream events
35
36   Downstream events are inserted on source pads and travel along the pipeline.
37   They are handled like buffers and processed in order. If event x is inserted
38   into a pipeline after buffer y, they are guaranteed to travel in that order.
39   Downstream events therefore only travel when the pipeline is PLAYING.
40   Downstream events must be handled by the routines that handle buffers, too.
41   An example downstream event is the event signalling that the stream has ended.
42   Please keep in mind that downstream events take the same way as buffers. So a
43   ghost pad will never receive these events.
44
45 * upstream events
46   
47   Upstream events are inserted on sink pads and travel backwards through the 
48   pipeline. They travel as fast as possible. Source pads must have a handler
49   function in place to process events. A default handler is implemented.
50   An example upstream event is an event that seeks inside the stream.
51   Please keep in mind that upstream events take the same way as buffers in reverse
52   direction. This means that ghost pads will never receive them.
53
54 * vertical events
55   
56   Vertical events travel from elements to their parents. They are targetted at
57   the application. Vertical events should be used for information that an 
58   application cannot receive in an easy way by using callbacks or properties.
59   Vertical events are send to the application by the pipeline that collects those
60   events and supplies a callback for the application. Vertical events are also only
61   happening when the pipeline is in PAUSED or PLAZING state.
62   An example vertical event is the error event informing the application about
63   unexpected behaviour.
64
65
66 The GstEvent object
67 -------------------
68
69 struct _GstEvent {
70   GstData data;
71
72   GstEventType  type;
73   guint64       timestamp;
74   GstObject     *src;
75
76   union {
77     ...
78   } event_data;
79 };
80
81 data:       The parent object.
82 type:       The type of the event. GStreamer aims to keep the number of different
83             plugin types as small as possible.
84 timestamp:  The time when the event was created. This property is used to identify
85             duplicated events. If the application inserts an event, the timestamp 
86             is set by the element receiving the event from the application.
87 src:        The element that created the event. If an application inserts an event,
88             the element that received the event from the application sets itself as
89             the source.
90 event_data: data specific to the event type.
91
92
93 The different event types
94 -------------------------
95
96 The following names in brackets correspong to the event's type property.
97
98 GST_EVENT_DISCONTINUOUS
99 direction(s): downstream
100 event_data: struct {
101               GstDiscontType type;
102             } discontinuous;
103 This event is used to indicate that the current stream does not continue. Possible 
104 indications are a new stream (type = GST_DISCONT_NEW), the happening of a seek 
105 (type = GST_DISCONT_SEEK) or the end of the stream when no more data is available.
106 (type = GST_DISCONT_EOS)
107
108 GST_EVENT_SEEK
109 direction(s): upstream
110 event_data: struct {
111               GstSeekType type;
112               gint64 offset;
113               gboolean flush;
114             } seek;
115 This event is used if a seek is needed. Uses include applications or the avi demuxer
116 element requesting the end of the stream first. The seek can happen absolute (SET),
117 relative to the current position (CUR) or relative to the end (END). It is possible
118 to seek by frames (FRAME), time in microseconds (TIME) or bytes (BYTE). This is
119 indicated by the type field, which takes the values 
120 GST_SEEK_FRAME/TIME/BYTEOFFSET_SET/CUR/END. The offset field indicates how many units 
121 should be seeked. Negative values indicate seeking backwards from the indicated position.
122 The flush field indicates if buffered data shuold be flushed or discarded.
123
124 GST_EVENT_FLUSH
125 direction(s): upstream
126 event_data: none
127 This event indicates that all buffered data should be flushed out immediately.
128
129 GST_EVENT_INFO
130 direction(s): downstream, vertical
131 event_data: struct {
132               GstProps *props;
133             } info;
134 The Info event is used to transport meta information like bitrate, author, title, 
135 interpret or stream length. Most info events will be emitted vertical and downstream
136 at the same time. Vertical emission ensures that an application knows about those
137 properties and downstream emission ensures that elements can compute own information
138 from these infos. (eg converting stream length in bytes to song length in 
139 microseconds).
140 Props consist of key / value pairs, where the key is a string identifier and the value
141 is a GstPropEntry. Many key strings are predefined to allow consistency between elements.
142 Elements should try to suppy any information they can as soon as possible.
143
144 GST_EVENT_HAS_INFO
145 direction(s): upstream
146 void (*GstHasInfoCallback) (gchar *name, GstPropsEntry *info, gpointer data);
147 event_data: struct {
148               GList *info;
149               GstHasInfoCallback callback;
150               gpointer data;
151             } has_info;
152 The has_info event might be inserted by an application to find out if a pipeline can supply
153 the specified infos. the info list contains all information that the application is 
154 interested in. If an element can supply information it calls the supplied callback with the
155 name of the information it can supply, the information if it is already available or NULL and
156 the data. If this event is destroyed, it will call the callback with name = NULL to indicate
157 that no more data will be received.
158 This event will for example be used by playlists when they generate information.
159
160 GST_EVENT_ERROR
161 direction(s): vertical
162 event_data: struct {
163               gchar *message
164             } error;
165 An error event is emitted, whenever a recoverable error occurs that the application
166 should know about. The usage should be similar to GLibs GError. An example would be
167 "connection closed" for a host to host plugin.
168
169
170 Reference Counting
171 ------------------
172
173 References to events are handled similar to buffers. An element receives an event with
174 a single reference. If it forwards the event, this reference is lost.
175 Events own a reference to the element that created them. They take care of all of all
176 data inside them too (strings, props). So elements and applications that want to keep
177 this informations need to copy or add a reference them.
178
179
180 Changing Events
181 ---------------
182 It is not allowed to change any data inside an event. Changing events can only be 
183 accomplished by removing the reference to them and not forwarding the event and then
184 creating a new one.
185
186
187 Default Behaviour
188 -----------------
189
190 * downstream events
191
192   These are not handled by default, because they must be handled by the chain handler
193   of the sink pad. There is however a function called gst_pad_event_default(GstPad *, 
194   GstData *) that will take care of events if your code doesn't want to handle them.
195   But your code must be aware that not everything that your chain function receives
196   is a buffer. It could be an event.
197   
198 * upstream events
199
200   Upstream events are handled by a default handler function that is inserted on sink
201   pads when they are created. This function simply forwards the event to all connected
202   sink pads of the element. You are free to change this handler.
203   
204 * vertical events
205
206   Vertical events can not be received by elements. Bins have a default handler function
207   that simply forwards the event to their parent. Pipelines offer callbacks for events.
208   You may change this handler for your custom bins.
209   
210   
211 Use Cases
212 ---------
213
214 Following are some simple use cases describing how events are generated. The pipeline
215 decriptions use gst-launch syntax. "..." indicates that something follows there but is 
216 not important for the example.
217
218 * filesrc ! fakesink
219  
220   - When starting the pipeline, filesrc will emit a DISCONTINUOUS event of type NEW 
221     indicating a new stream.
222   - Following that event will be an INFO event containing the length of the file/stream
223     in bytes.
224   - After the file was played, the filesrc emits a "DISCONTINUOUS" of type EOS.
225
226 * filesrc ! mad ! ...
227
228   - When starting, filesrc emits a DISCONTINUOUS event followed by an INFO event (see above).
229     The mad plugin remembers the length of the file and removes the INFO element as it
230     is no longer of use. The DISCONTINUOUS event has passed mad after making sure, that all
231     buffers are cleared.
232   - Mad will emit a SEEK event to BYTEOFFSET_END; offset = -sizeof(ID3_info) to read out the ID3 
233     information.
234   - Filesrc emits a DISCONTINUOUS event of type SEEK to indicate that it seeked to the end.
235     This event will not be passed on by mad.
236   - after receiving the ID 3 information, mad will issue an INFO event containing all data
237     it extracted. This event will probably only be passed vertical as ID3 information is of
238     no use to other elements.
239   - mad then ISSUES a SEEK event of type BYTEOFFSET_SET; offset = 0 to make the filesrc start
240     playing the file.
241   - The filesrc will reset its offset and issue a DISCONTINUOUS event of type SEEK. This event
242     will not be forwarded by mad.
243   - When playing starts, mad is able to compute bitrate and other information including playing
244     time with the help of the previous length information supplied by the filesrc. It will then
245     issue another INFO event with that informations. This one will be send downstream and vertical.
246     
247 * ... ! avimux ! disksink
248
249   This example is showing a more exotic way of using events. The reader should be aware that AVI
250   files have a limited filesize. Only 4 GB are allowed. We now show what happens when the avimux
251   encoder hits that limit.
252   - When the internal counter of avimux shows that it is approaching the filesize limit, the 
253     avimux element pushes a buffer containig the footer to the disksink.
254   - After that it issues a DISCONTINUOUS event of the type DISCONT_NEW indicating a new stream.
255     The disksink will close the file and reopen a new one.
256   - The avimux plugin resets its internal size counter and restarts sending data to the new file.
257
258 * filesrc ! gunzip ! mikmod ! osssink
259
260   (please note that this example is purely theoretical. It should just show ideas)
261   During playback, an application is interested in "interpret", "title", "length_time",
262   "length_size" and "URI" of the current stream. 
263   - The appplication creates a HAS_INFO event and inserts it at the end of the pipeline into the
264     osssink.
265   - The osssink cannot supply any info so it forwards the event to the mikmod element.
266   - The mikmod element can supply "title" and "length_time". It calls the supplied callback twice
267     and gives these two options. It forwards the event to the gunzip element.
268   - The gunzip element has already decoded the whole data so it knows the size of the stream. It
269     calls the callback for "length_size" and forwards the event.
270   - The filesrc supplies the "URI" and the "length_size" for a second time. It is now up to the
271     application's callback function to handle this second occurence of "length_size" information.
272     The filesrc does not forward the event and dereferences it.
273   - During disposal of the event, the callback function is called again with name=NULL. The
274     application now knows that no "title" can be supplied.  
275   
276   
277 Open Questions
278 --------------
279
280 Open questions are issues that should probably be solved by events but can not be solved
281 currently.
282
283 * A disksink needs to be able to inform elements of a restricted file size. Simply closing
284   the file and opening a new one might not work because elements might need to supply a 
285   footer. (eg avimux)
286
287
288
289 Issues / changes (to be deleted in final version)
290 ----------------
291
292 ? Are the event directions distinct? Or is it possible that some type of event
293   can travel eg upstream _and_ vertical?
294 ? How are upstream/vertical events supposed to be handled if they occur when 
295   the element is READY or NULL? Buffer the event? How many events should be 
296   buffered? Maybe a READY element is attached to a PLAYING/PAUSED one and
297   constantly receiving events, no?
298 ! The EOS event was merged with the DISCONTINUOUS event.
299 ? Does the DISCONTINUOUS event need a "flush" option?
300 ? Should chain funcs be changed to expect GstData instead of GstBuffer?
301   It's a little bit misleading if events can arrive there.
302 ! added information about timestamp.
303 ? Should timestamps of "follow up" events (eg conversion of seek) keep the 
304   timestamp?
305 ? src = NULL, when app inserts event?
306 ? How do elements handle events they cannot use? (eg filesrc getting timebased 
307   seek request)
308 ? Somebody fix the GST_EVENT_FLUSH part.
309 ? GValue or GstProps for INFO events? First requires to open up the props headers
310   and writing some API to ease the retrieval of the elements, the second requires
311   a rewrite of GST_EVENT_INFO.
312 ? GQuark/GValue possible in INFO events?
313 ! Merged INFO and PROPS event. They are nearly the same. Added documentation.
314 ? Need to work out the ERROR event.
315 ! changed prototype for gst_pad_event_default to accept buffers so the function checks
316   if it is an event and not every chain handler has to.
317 ! added HAS_INFO event. An alternative to the callback function could be another vertical
318   event.
319 ? Should HAS_INFO callback supply the element calling the function?
320 ? Use case one: start with discont event?
321 ? Do we need a state change event?
322 ? Should we make elements supply information as soon as possible or only upon HAS_INFO 
323   request?
324 ? should the second example be done with region requesting instead of events?
325 ? "location" or "URI"?
326 ? What about suggesting buffer sizes?
327 ? What about QoS?