1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
This short document gives some guidelines to plugin writers.
chain-based plugins
===================
- the plugin has a chain function on each sink pad (can be the same function)
!
! chain (GstPad *pad, GstBuffer *buffer)
! {
! MyElement *elem = MY_ELEMENT (gst_pad_get_parent (pad));
! GstBuffer *new_buffer;
!
! ...
! (process the buffer)
! ...
!
! /* you can push as many buffers (0 or more) as you want */
! while (!done) {
! ...
! (process some more
! ...
! gst_pad_push (elem->some_sink_pad, new_buffer);
! ...
! /* if you're like going to send a large amount of buffers
! * it's a good idea to call _yield from time to time after
! * the buffer has been pushed */
! (optional gst_element_yield (GST_ELEMENT (elem)); )
! }
! }
!
- chain based functions are usually easy and recommended.
- use gst_element_yield if you are going to push a lot of buffers.
loop-based plugins
==================
- one loop function for the element.
- required for bytestream based plugins.
simple case
-----------
- one pull at the start
- if you do something like this, you really should consider using a
chain function as it can be significantly optimised by the scheduler.
!
! loop (GstElement *element)
! {
! MyElement *elem = MY_ELEMENT (element);
! GstBuffer *buffer;
! GstBuffer *new_buffer;
!
! buffer = gst_pad_pull (elem->sinkpad);
!
! ...
! (do something)
! ...
!
! /* you can push as many buffers (0 or more) as you want */
! while (!done) {
! ...
! (process some more
! ...
! gst_pad_push (elem->some_sink_pad, new_buffer);
! ...
! /* if you're like going to send a large amount of buffers
! * it's a good idea to call _yield from time to time after
! * the buffer has been pushed */
! (optional gst_element_yield (GST_ELEMENT (elem)); )
! }
! }
!
DONT!!
- infinite loop
!
! loop (element)
! {
! while (TRUE) {
! ...
! _pull ()
! ...
!
! ...
! _push ()
! ...
! }
! }
!
* you can fix this by either:
- setting the GST_ELEMENT_INFINITE_LOOP flag on the element. this is
not recommended, if all plugins in the pipeline (or depending on the
pipeline, some plugins) have this flag, the pipeline will not run.
- calling break; from time to time to get out of the loop. (duh, then
it's not an infinite loop anymore). beware that the next time the loop
function is called, it will be started from the top.
- calling gst_element_yield() from time to time (see NOTES).
- this is fine (albeit rather useless, use a chain function):
!
! loop (element)
! {
! ...
! _pull ()
! ...
!
! ...
! _push ()
! ...
! }
!
- so is this (albeit rather useless, consider removing the while and the _yield):
!
! loop (element)
! {
! while (TRUE) {
! ...
! _pull ()
! ...
!
! ...
! _push ()
! ...
! gst_element_yield (element);
! }
! }
!
DONT!!
- allocate data, loop, free data
!
! loop (element)
! {
!
! (my funky malloc)
!
! while (TRUE) {
!
! ...
! _pull ()
! ...
!
! ...
! _push ()
! ...
!
! _yield ()
! }
!
! (my funky free)
!
! }
!
- the free will NEVER happen!.
* You can fix this by:
- allocating/freeing data in the state change function
- you could think the following code would work too:
!
! (*WRONG* example follows)
!
! loop (element)
! {
!
! (my funky malloc)
!
! while (TRUE) {
!
! ...
! _pull ()
! if (EOS)
! break;
!
! ...
! _push ()
! ...
!
! _yield ()
! }
!
! (my funky free)
!
! }
!
but it'll only free the data if the pipeline was shut down with
and EOS so don't try it. Besides, on EOS, a state change will happen
anyway so free the data there.
bytestream/multiple pull case
-----------------------------
- same as the simple case, but you can't use a chain based function unless
you want to make things a little harder then they should be.
complicated case
----------------
- push and pull are completely mixed.
- the flow is usually something like this:
!
! loop (element)
! {
!
! while (TRUE) {
!
! while (something) {
! ...
! do some _pull ()
! ...
! do some _push ()
! ...
! while (something_else) {
! ...
! if (testing)
! do some _pull ()
! ...
! do some _push ()
! }
! }
!
! while (something_useful) {
! ...
! _push ()
! ...
! }
! }
! }
!
(example made complicated on purpose, but vorbisdec comes close)
- you cannot call break to avoid infinite loops and there are loops that
take a significant amount of time to execute, possibly pushing/pulling
a lot of buffers.
* You can fix this by:
- inserting gst_element_yield () in sane places, don't exaggerate because
every yield can potentially never return so you need to keep track of
allocations (see the NOTES below).
NOTES:
======
- a call to _yield() can never return. if you have data allocated on the
stack before the yield, keep a pointer to it in the element struct
and free it in the state change function.
IMPLEMENTATION DETAILS
======================
The infinite loops are only problematic if the scheduler chooses to select
the plugin as an entry point in the chain. _yield() will be a nop if this is
not the case. The scheduler will not select plugins with the INFINITE_LOOP
flag set as entries in a chain.
A _yield in an entry will hand over control to the main thread context, allowing
state changes and other actions to be performed. It will basically exit the
_iterate() function. spending a long time in a loop will degrade app responsiveness
because _iterate will take a long time.
Calling yield, pulling, pushing can potentially never return because a state change
might have happened, killing off execution of the plugin. pulling/pushing buffers
will cause no leaks in this case because the core will free pending buffers in a
state change to READY. The plugin must free allocated data/buffers itself in the state
change function if the yield didn't return.
|