1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.BufferPool;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gstreamer.AllocationParams;
31 private import gstreamer.Allocator;
32 private import gstreamer.Buffer;
33 private import gstreamer.Caps;
34 private import gstreamer.ObjectGst;
35 private import gstreamer.Structure;
36 private import gstreamerc.gstreamer;
37 public  import gstreamerc.gstreamertypes;
38 
39 
40 /**
41  * A #GstBufferPool is an object that can be used to pre-allocate and recycle
42  * buffers of the same size and with the same properties.
43  * 
44  * A #GstBufferPool is created with gst_buffer_pool_new().
45  * 
46  * Once a pool is created, it needs to be configured. A call to
47  * gst_buffer_pool_get_config() returns the current configuration structure from
48  * the pool. With gst_buffer_pool_config_set_params() and
49  * gst_buffer_pool_config_set_allocator() the bufferpool parameters and
50  * allocator can be configured. Other properties can be configured in the pool
51  * depending on the pool implementation.
52  * 
53  * A bufferpool can have extra options that can be enabled with
54  * gst_buffer_pool_config_add_option(). The available options can be retrieved
55  * with gst_buffer_pool_get_options(). Some options allow for additional
56  * configuration properties to be set.
57  * 
58  * After the configuration structure has been configured,
59  * gst_buffer_pool_set_config() updates the configuration in the pool. This can
60  * fail when the configuration structure is not accepted.
61  * 
62  * After the a pool has been configured, it can be activated with
63  * gst_buffer_pool_set_active(). This will preallocate the configured resources
64  * in the pool.
65  * 
66  * When the pool is active, gst_buffer_pool_acquire_buffer() can be used to
67  * retrieve a buffer from the pool.
68  * 
69  * Buffers allocated from a bufferpool will automatically be returned to the
70  * pool with gst_buffer_pool_release_buffer() when their refcount drops to 0.
71  * 
72  * The bufferpool can be deactivated again with gst_buffer_pool_set_active().
73  * All further gst_buffer_pool_acquire_buffer() calls will return an error. When
74  * all buffers are returned to the pool they will be freed.
75  * 
76  * Use gst_object_unref() to release the reference to a bufferpool. If the
77  * refcount of the pool reaches 0, the pool will be freed.
78  */
79 public class BufferPool : ObjectGst
80 {
81 	/** the main Gtk struct */
82 	protected GstBufferPool* gstBufferPool;
83 
84 	/** Get the main Gtk struct */
85 	public GstBufferPool* getBufferPoolStruct()
86 	{
87 		return gstBufferPool;
88 	}
89 
90 	/** the main Gtk struct as a void* */
91 	protected override void* getStruct()
92 	{
93 		return cast(void*)gstBufferPool;
94 	}
95 
96 	protected override void setStruct(GObject* obj)
97 	{
98 		gstBufferPool = cast(GstBufferPool*)obj;
99 		super.setStruct(obj);
100 	}
101 
102 	/**
103 	 * Sets our main struct and passes it to the parent class.
104 	 */
105 	public this (GstBufferPool* gstBufferPool, bool ownedRef = false)
106 	{
107 		this.gstBufferPool = gstBufferPool;
108 		super(cast(GstObject*)gstBufferPool, ownedRef);
109 	}
110 
111 	/**
112 	 */
113 
114 	public static GType getType()
115 	{
116 		return gst_buffer_pool_get_type();
117 	}
118 
119 	/**
120 	 * Creates a new #GstBufferPool instance.
121 	 *
122 	 * Return: a new #GstBufferPool instance
123 	 *
124 	 * Throws: ConstructionException GTK+ fails to create the object.
125 	 */
126 	public this()
127 	{
128 		auto p = gst_buffer_pool_new();
129 		
130 		if(p is null)
131 		{
132 			throw new ConstructionException("null returned by new");
133 		}
134 		
135 		this(cast(GstBufferPool*) p);
136 	}
137 
138 	/**
139 	 * Enabled the option in @config. This will instruct the @bufferpool to enable
140 	 * the specified option on the buffers that it allocates.
141 	 *
142 	 * The supported options by @pool can be retrieved with gst_buffer_pool_get_options().
143 	 *
144 	 * Params:
145 	 *     config = a #GstBufferPool configuration
146 	 *     option = an option to add
147 	 */
148 	public static void configAddOption(Structure config, string option)
149 	{
150 		gst_buffer_pool_config_add_option((config is null) ? null : config.getStructureStruct(), Str.toStringz(option));
151 	}
152 
153 	/**
154 	 * Get the @allocator and @params from @config.
155 	 *
156 	 * Params:
157 	 *     config = a #GstBufferPool configuration
158 	 *     allocator = a #GstAllocator, or %NULL
159 	 *     params = #GstAllocationParams, or %NULL
160 	 *
161 	 * Return: %TRUE, if the values are set.
162 	 */
163 	public static bool configGetAllocator(Structure config, ref Allocator allocator, out AllocationParams params)
164 	{
165 		GstAllocator* outallocator = allocator.getAllocatorStruct();
166 		GstAllocationParams* outparams = new GstAllocationParams;
167 		
168 		auto p = gst_buffer_pool_config_get_allocator((config is null) ? null : config.getStructureStruct(), &outallocator, outparams) != 0;
169 		
170 		allocator = ObjectG.getDObject!(Allocator)(outallocator);
171 		params = ObjectG.getDObject!(AllocationParams)(outparams);
172 		
173 		return p;
174 	}
175 
176 	/**
177 	 * Parse an available @config and get the option at @index of the options API
178 	 * array.
179 	 *
180 	 * Params:
181 	 *     config = a #GstBufferPool configuration
182 	 *     index = position in the option array to read
183 	 *
184 	 * Return: a #gchar of the option at @index.
185 	 */
186 	public static string configGetOption(Structure config, uint index)
187 	{
188 		return Str.toString(gst_buffer_pool_config_get_option((config is null) ? null : config.getStructureStruct(), index));
189 	}
190 
191 	/**
192 	 * Get the configuration values from @config.
193 	 *
194 	 * Params:
195 	 *     config = a #GstBufferPool configuration
196 	 *     caps = the caps of buffers
197 	 *     size = the size of each buffer, not including prefix and padding
198 	 *     minBuffers = the minimum amount of buffers to allocate.
199 	 *     maxBuffers = the maximum amount of buffers to allocate or 0 for unlimited.
200 	 *
201 	 * Return: %TRUE if all parameters could be fetched.
202 	 */
203 	public static bool configGetParams(Structure config, out Caps caps, out uint size, out uint minBuffers, out uint maxBuffers)
204 	{
205 		GstCaps* outcaps = null;
206 		
207 		auto p = gst_buffer_pool_config_get_params((config is null) ? null : config.getStructureStruct(), &outcaps, &size, &minBuffers, &maxBuffers) != 0;
208 		
209 		caps = ObjectG.getDObject!(Caps)(outcaps);
210 		
211 		return p;
212 	}
213 
214 	/**
215 	 * Check if @config contains @option.
216 	 *
217 	 * Params:
218 	 *     config = a #GstBufferPool configuration
219 	 *     option = an option
220 	 *
221 	 * Return: %TRUE if the options array contains @option.
222 	 */
223 	public static bool configHasOption(Structure config, string option)
224 	{
225 		return gst_buffer_pool_config_has_option((config is null) ? null : config.getStructureStruct(), Str.toStringz(option)) != 0;
226 	}
227 
228 	/**
229 	 * Retrieve the number of values currently stored in the options array of the
230 	 * @config structure.
231 	 *
232 	 * Params:
233 	 *     config = a #GstBufferPool configuration
234 	 *
235 	 * Return: the options array size as a #guint.
236 	 */
237 	public static uint configNOptions(Structure config)
238 	{
239 		return gst_buffer_pool_config_n_options((config is null) ? null : config.getStructureStruct());
240 	}
241 
242 	/**
243 	 * Set the @allocator and @params on @config.
244 	 *
245 	 * One of @allocator and @params can be %NULL, but not both. When @allocator
246 	 * is %NULL, the default allocator of the pool will use the values in @param
247 	 * to perform its allocation. When @param is %NULL, the pool will use the
248 	 * provided @allocator with its default #GstAllocationParams.
249 	 *
250 	 * A call to gst_buffer_pool_set_config() can update the allocator and params
251 	 * with the values that it is able to do. Some pools are, for example, not able
252 	 * to operate with different allocators or cannot allocate with the values
253 	 * specified in @params. Use gst_buffer_pool_get_config() to get the currently
254 	 * used values.
255 	 *
256 	 * Params:
257 	 *     config = a #GstBufferPool configuration
258 	 *     allocator = a #GstAllocator
259 	 *     params = #GstAllocationParams
260 	 */
261 	public static void configSetAllocator(Structure config, Allocator allocator, AllocationParams params)
262 	{
263 		gst_buffer_pool_config_set_allocator((config is null) ? null : config.getStructureStruct(), (allocator is null) ? null : allocator.getAllocatorStruct(), (params is null) ? null : params.getAllocationParamsStruct());
264 	}
265 
266 	/**
267 	 * Configure @config with the given parameters.
268 	 *
269 	 * Params:
270 	 *     config = a #GstBufferPool configuration
271 	 *     caps = caps for the buffers
272 	 *     size = the size of each buffer, not including prefix and padding
273 	 *     minBuffers = the minimum amount of buffers to allocate.
274 	 *     maxBuffers = the maximum amount of buffers to allocate or 0 for unlimited.
275 	 */
276 	public static void configSetParams(Structure config, Caps caps, uint size, uint minBuffers, uint maxBuffers)
277 	{
278 		gst_buffer_pool_config_set_params((config is null) ? null : config.getStructureStruct(), (caps is null) ? null : caps.getCapsStruct(), size, minBuffers, maxBuffers);
279 	}
280 
281 	/**
282 	 * Validate that changes made to @config are still valid in the context of the
283 	 * expected parameters. This function is a helper that can be used to validate
284 	 * changes made by a pool to a config when gst_buffer_pool_set_config()
285 	 * returns %FALSE. This expects that @caps haven't changed and that
286 	 * @min_buffers aren't lower then what we initially expected.
287 	 * This does not check if options or allocator parameters are still valid,
288 	 * won't check if size have changed, since changing the size is valid to adapt
289 	 * padding.
290 	 *
291 	 * Params:
292 	 *     config = a #GstBufferPool configuration
293 	 *     caps = the excepted caps of buffers
294 	 *     size = the expected size of each buffer, not including prefix and padding
295 	 *     minBuffers = the expected minimum amount of buffers to allocate.
296 	 *     maxBuffers = the expect maximum amount of buffers to allocate or 0 for unlimited.
297 	 *
298 	 * Return: %TRUE, if the parameters are valid in this context.
299 	 *
300 	 * Since: 1.4
301 	 */
302 	public static bool configValidateParams(Structure config, Caps caps, uint size, uint minBuffers, uint maxBuffers)
303 	{
304 		return gst_buffer_pool_config_validate_params((config is null) ? null : config.getStructureStruct(), (caps is null) ? null : caps.getCapsStruct(), size, minBuffers, maxBuffers) != 0;
305 	}
306 
307 	/**
308 	 * Acquire a buffer from @pool. @buffer should point to a memory location that
309 	 * can hold a pointer to the new buffer.
310 	 *
311 	 * @params can be %NULL or contain optional parameters to influence the
312 	 * allocation.
313 	 *
314 	 * Params:
315 	 *     buffer = a location for a #GstBuffer
316 	 *     params = parameters.
317 	 *
318 	 * Return: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the pool is
319 	 *     inactive.
320 	 */
321 	public GstFlowReturn acquireBuffer(out Buffer buffer, GstBufferPoolAcquireParams* params)
322 	{
323 		GstBuffer* outbuffer = null;
324 		
325 		auto p = gst_buffer_pool_acquire_buffer(gstBufferPool, &outbuffer, params);
326 		
327 		buffer = ObjectG.getDObject!(Buffer)(outbuffer);
328 		
329 		return p;
330 	}
331 
332 	/**
333 	 * Get a copy of the current configuration of the pool. This configuration
334 	 * can either be modified and used for the gst_buffer_pool_set_config() call
335 	 * or it must be freed after usage.
336 	 *
337 	 * Return: a copy of the current configuration of @pool. use
338 	 *     gst_structure_free() after usage or gst_buffer_pool_set_config().
339 	 */
340 	public Structure getConfig()
341 	{
342 		auto p = gst_buffer_pool_get_config(gstBufferPool);
343 		
344 		if(p is null)
345 		{
346 			return null;
347 		}
348 		
349 		return ObjectG.getDObject!(Structure)(cast(GstStructure*) p);
350 	}
351 
352 	/**
353 	 * Get a %NULL terminated array of string with supported bufferpool options for
354 	 * @pool. An option would typically be enabled with
355 	 * gst_buffer_pool_config_add_option().
356 	 *
357 	 * Return: a %NULL terminated array
358 	 *     of strings.
359 	 */
360 	public string[] getOptions()
361 	{
362 		return Str.toStringArray(gst_buffer_pool_get_options(gstBufferPool));
363 	}
364 
365 	/**
366 	 * Check if the bufferpool supports @option.
367 	 *
368 	 * Params:
369 	 *     option = an option
370 	 *
371 	 * Return: %TRUE if the buffer pool contains @option.
372 	 */
373 	public bool hasOption(string option)
374 	{
375 		return gst_buffer_pool_has_option(gstBufferPool, Str.toStringz(option)) != 0;
376 	}
377 
378 	/**
379 	 * Check if @pool is active. A pool can be activated with the
380 	 * gst_buffer_pool_set_active() call.
381 	 *
382 	 * Return: %TRUE when the pool is active.
383 	 */
384 	public bool isActive()
385 	{
386 		return gst_buffer_pool_is_active(gstBufferPool) != 0;
387 	}
388 
389 	/**
390 	 * Release @buffer to @pool. @buffer should have previously been allocated from
391 	 * @pool with gst_buffer_pool_acquire_buffer().
392 	 *
393 	 * This function is usually called automatically when the last ref on @buffer
394 	 * disappears.
395 	 *
396 	 * Params:
397 	 *     buffer = a #GstBuffer
398 	 */
399 	public void releaseBuffer(Buffer buffer)
400 	{
401 		gst_buffer_pool_release_buffer(gstBufferPool, (buffer is null) ? null : buffer.getBufferStruct());
402 	}
403 
404 	/**
405 	 * Control the active state of @pool. When the pool is inactive, new calls to
406 	 * gst_buffer_pool_acquire_buffer() will return with %GST_FLOW_FLUSHING.
407 	 *
408 	 * Activating the bufferpool will preallocate all resources in the pool based on
409 	 * the configuration of the pool.
410 	 *
411 	 * Deactivating will free the resources again when there are no outstanding
412 	 * buffers. When there are outstanding buffers, they will be freed as soon as
413 	 * they are all returned to the pool.
414 	 *
415 	 * Params:
416 	 *     active = the new active state
417 	 *
418 	 * Return: %FALSE when the pool was not configured or when preallocation of the
419 	 *     buffers failed.
420 	 */
421 	public bool setActive(bool active)
422 	{
423 		return gst_buffer_pool_set_active(gstBufferPool, active) != 0;
424 	}
425 
426 	/**
427 	 * Set the configuration of the pool. If the pool is already configured, and
428 	 * the configuration haven't change, this function will return %TRUE. If the
429 	 * pool is active, this method will return %FALSE and active configuration
430 	 * will remain. Buffers allocated form this pool must be returned or else this
431 	 * function will do nothing and return %FALSE.
432 	 *
433 	 * @config is a #GstStructure that contains the configuration parameters for
434 	 * the pool. A default and mandatory set of parameters can be configured with
435 	 * gst_buffer_pool_config_set_params(), gst_buffer_pool_config_set_allocator()
436 	 * and gst_buffer_pool_config_add_option().
437 	 *
438 	 * If the parameters in @config can not be set exactly, this function returns
439 	 * %FALSE and will try to update as much state as possible. The new state can
440 	 * then be retrieved and refined with gst_buffer_pool_get_config().
441 	 *
442 	 * This function takes ownership of @config.
443 	 *
444 	 * Params:
445 	 *     config = a #GstStructure
446 	 *
447 	 * Return: %TRUE when the configuration could be set.
448 	 */
449 	public bool setConfig(Structure config)
450 	{
451 		return gst_buffer_pool_set_config(gstBufferPool, (config is null) ? null : config.getStructureStruct()) != 0;
452 	}
453 
454 	/**
455 	 * Enable or disable the flushing state of a @pool without freeing or
456 	 * allocating buffers.
457 	 *
458 	 * Params:
459 	 *     flushing = whether to start or stop flushing
460 	 *
461 	 * Since: 1.4
462 	 */
463 	public void setFlushing(bool flushing)
464 	{
465 		gst_buffer_pool_set_flushing(gstBufferPool, flushing);
466 	}
467 }