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 gst.base.DataQueue;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gst.base.c.functions;
31 public  import gst.base.c.types;
32 private import std.algorithm;
33 
34 
35 /**
36  * #GstDataQueue is an object that handles threadsafe queueing of objects. It
37  * also provides size-related functionality. This object should be used for
38  * any #GstElement that wishes to provide some sort of queueing functionality.
39  */
40 public class DataQueue : ObjectG
41 {
42 	/** the main Gtk struct */
43 	protected GstDataQueue* gstDataQueue;
44 
45 	/** Get the main Gtk struct */
46 	public GstDataQueue* getDataQueueStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return gstDataQueue;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected override void* getStruct()
55 	{
56 		return cast(void*)gstDataQueue;
57 	}
58 
59 	protected override void setStruct(GObject* obj)
60 	{
61 		gstDataQueue = cast(GstDataQueue*)obj;
62 		super.setStruct(obj);
63 	}
64 
65 	/**
66 	 * Sets our main struct and passes it to the parent class.
67 	 */
68 	public this (GstDataQueue* gstDataQueue, bool ownedRef = false)
69 	{
70 		this.gstDataQueue = gstDataQueue;
71 		super(cast(GObject*)gstDataQueue, ownedRef);
72 	}
73 
74 
75 	/** */
76 	public static GType getType()
77 	{
78 		return gst_data_queue_get_type();
79 	}
80 
81 	/**
82 	 * Creates a new #GstDataQueue. If @fullcallback or @emptycallback are supplied, then
83 	 * the #GstDataQueue will call the respective callback to signal full or empty condition.
84 	 * If the callbacks are NULL the #GstDataQueue will instead emit 'full' and 'empty'
85 	 * signals.
86 	 *
87 	 * Params:
88 	 *     checkfull = the callback used to tell if the element considers the queue full
89 	 *         or not.
90 	 *     fullcallback = the callback which will be called when the queue is considered full.
91 	 *     emptycallback = the callback which will be called when the queue is considered empty.
92 	 *     checkdata = a #gpointer that will be passed to the @checkfull, @fullcallback,
93 	 *         and @emptycallback callbacks.
94 	 *
95 	 * Returns: a new #GstDataQueue.
96 	 *
97 	 * Since: 1.2
98 	 *
99 	 * Throws: ConstructionException GTK+ fails to create the object.
100 	 */
101 	public this(GstDataQueueCheckFullFunction checkfull, GstDataQueueFullCallback fullcallback, GstDataQueueEmptyCallback emptycallback, void* checkdata)
102 	{
103 		auto p = gst_data_queue_new(checkfull, fullcallback, emptycallback, checkdata);
104 
105 		if(p is null)
106 		{
107 			throw new ConstructionException("null returned by new");
108 		}
109 
110 		this(cast(GstDataQueue*) p, true);
111 	}
112 
113 	/**
114 	 * Pop and unref the head-most #GstMiniObject with the given #GType.
115 	 *
116 	 * Params:
117 	 *     type = The #GType of the item to drop.
118 	 *
119 	 * Returns: %TRUE if an element was removed.
120 	 *
121 	 * Since: 1.2
122 	 */
123 	public bool dropHead(GType type)
124 	{
125 		return gst_data_queue_drop_head(gstDataQueue, type) != 0;
126 	}
127 
128 	/**
129 	 * Flushes all the contents of the @queue. Any call to #gst_data_queue_push and
130 	 * #gst_data_queue_pop will be released.
131 	 * MT safe.
132 	 *
133 	 * Since: 1.2
134 	 */
135 	public void flush()
136 	{
137 		gst_data_queue_flush(gstDataQueue);
138 	}
139 
140 	/**
141 	 * Get the current level of the queue.
142 	 *
143 	 * Params:
144 	 *     level = the location to store the result
145 	 *
146 	 * Since: 1.2
147 	 */
148 	public void getLevel(out GstDataQueueSize level)
149 	{
150 		gst_data_queue_get_level(gstDataQueue, &level);
151 	}
152 
153 	/**
154 	 * Queries if there are any items in the @queue.
155 	 * MT safe.
156 	 *
157 	 * Returns: %TRUE if @queue is empty.
158 	 *
159 	 * Since: 1.2
160 	 */
161 	public bool isEmpty()
162 	{
163 		return gst_data_queue_is_empty(gstDataQueue) != 0;
164 	}
165 
166 	/**
167 	 * Queries if @queue is full. This check will be done using the
168 	 * #GstDataQueueCheckFullFunction registered with @queue.
169 	 * MT safe.
170 	 *
171 	 * Returns: %TRUE if @queue is full.
172 	 *
173 	 * Since: 1.2
174 	 */
175 	public bool isFull()
176 	{
177 		return gst_data_queue_is_full(gstDataQueue) != 0;
178 	}
179 
180 	/**
181 	 * Inform the queue that the limits for the fullness check have changed and that
182 	 * any blocking gst_data_queue_push() should be unblocked to recheck the limits.
183 	 *
184 	 * Since: 1.2
185 	 */
186 	public void limitsChanged()
187 	{
188 		gst_data_queue_limits_changed(gstDataQueue);
189 	}
190 
191 	/**
192 	 * Retrieves the first @item available on the @queue without removing it.
193 	 * If the queue is currently empty, the call will block until at least
194 	 * one item is available, OR the @queue is set to the flushing state.
195 	 * MT safe.
196 	 *
197 	 * Params:
198 	 *     item = pointer to store the returned #GstDataQueueItem.
199 	 *
200 	 * Returns: %TRUE if an @item was successfully retrieved from the @queue.
201 	 *
202 	 * Since: 1.2
203 	 */
204 	public bool peek(out GstDataQueueItem* item)
205 	{
206 		return gst_data_queue_peek(gstDataQueue, &item) != 0;
207 	}
208 
209 	/**
210 	 * Retrieves the first @item available on the @queue. If the queue is currently
211 	 * empty, the call will block until at least one item is available, OR the
212 	 * @queue is set to the flushing state.
213 	 * MT safe.
214 	 *
215 	 * Params:
216 	 *     item = pointer to store the returned #GstDataQueueItem.
217 	 *
218 	 * Returns: %TRUE if an @item was successfully retrieved from the @queue.
219 	 *
220 	 * Since: 1.2
221 	 */
222 	public bool pop(out GstDataQueueItem* item)
223 	{
224 		return gst_data_queue_pop(gstDataQueue, &item) != 0;
225 	}
226 
227 	/**
228 	 * Pushes a #GstDataQueueItem (or a structure that begins with the same fields)
229 	 * on the @queue. If the @queue is full, the call will block until space is
230 	 * available, OR the @queue is set to flushing state.
231 	 * MT safe.
232 	 *
233 	 * Note that this function has slightly different semantics than gst_pad_push()
234 	 * and gst_pad_push_event(): this function only takes ownership of @item and
235 	 * the #GstMiniObject contained in @item if the push was successful. If %FALSE
236 	 * is returned, the caller is responsible for freeing @item and its contents.
237 	 *
238 	 * Params:
239 	 *     item = a #GstDataQueueItem.
240 	 *
241 	 * Returns: %TRUE if the @item was successfully pushed on the @queue.
242 	 *
243 	 * Since: 1.2
244 	 */
245 	public bool push(GstDataQueueItem* item)
246 	{
247 		return gst_data_queue_push(gstDataQueue, item) != 0;
248 	}
249 
250 	/**
251 	 * Pushes a #GstDataQueueItem (or a structure that begins with the same fields)
252 	 * on the @queue. It ignores if the @queue is full or not and forces the @item
253 	 * to be pushed anyway.
254 	 * MT safe.
255 	 *
256 	 * Note that this function has slightly different semantics than gst_pad_push()
257 	 * and gst_pad_push_event(): this function only takes ownership of @item and
258 	 * the #GstMiniObject contained in @item if the push was successful. If %FALSE
259 	 * is returned, the caller is responsible for freeing @item and its contents.
260 	 *
261 	 * Params:
262 	 *     item = a #GstDataQueueItem.
263 	 *
264 	 * Returns: %TRUE if the @item was successfully pushed on the @queue.
265 	 *
266 	 * Since: 1.2
267 	 */
268 	public bool pushForce(GstDataQueueItem* item)
269 	{
270 		return gst_data_queue_push_force(gstDataQueue, item) != 0;
271 	}
272 
273 	/**
274 	 * Sets the queue to flushing state if @flushing is %TRUE. If set to flushing
275 	 * state, any incoming data on the @queue will be discarded. Any call currently
276 	 * blocking on #gst_data_queue_push or #gst_data_queue_pop will return straight
277 	 * away with a return value of %FALSE. While the @queue is in flushing state,
278 	 * all calls to those two functions will return %FALSE.
279 	 *
280 	 * MT Safe.
281 	 *
282 	 * Params:
283 	 *     flushing = a #gboolean stating if the queue will be flushing or not.
284 	 *
285 	 * Since: 1.2
286 	 */
287 	public void setFlushing(bool flushing)
288 	{
289 		gst_data_queue_set_flushing(gstDataQueue, flushing);
290 	}
291 
292 	protected class OnEmptyDelegateWrapper
293 	{
294 		void delegate(DataQueue) dlg;
295 		gulong handlerId;
296 
297 		this(void delegate(DataQueue) dlg)
298 		{
299 			this.dlg = dlg;
300 			onEmptyListeners ~= this;
301 		}
302 
303 		void remove(OnEmptyDelegateWrapper source)
304 		{
305 			foreach(index, wrapper; onEmptyListeners)
306 			{
307 				if (wrapper.handlerId == source.handlerId)
308 				{
309 					onEmptyListeners[index] = null;
310 					onEmptyListeners = std.algorithm.remove(onEmptyListeners, index);
311 					break;
312 				}
313 			}
314 		}
315 	}
316 	OnEmptyDelegateWrapper[] onEmptyListeners;
317 
318 	/**
319 	 * Reports that the queue became empty (empty).
320 	 * A queue is empty if the total amount of visible items inside it (num-visible, time,
321 	 * size) is lower than the boundary values which can be set through the GObject
322 	 * properties.
323 	 */
324 	gulong addOnEmpty(void delegate(DataQueue) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
325 	{
326 		auto wrapper = new OnEmptyDelegateWrapper(dlg);
327 		wrapper.handlerId = Signals.connectData(
328 			this,
329 			"empty",
330 			cast(GCallback)&callBackEmpty,
331 			cast(void*)wrapper,
332 			cast(GClosureNotify)&callBackEmptyDestroy,
333 			connectFlags);
334 		return wrapper.handlerId;
335 	}
336 
337 	extern(C) static void callBackEmpty(GstDataQueue* dataqueueStruct, OnEmptyDelegateWrapper wrapper)
338 	{
339 		wrapper.dlg(wrapper.outer);
340 	}
341 
342 	extern(C) static void callBackEmptyDestroy(OnEmptyDelegateWrapper wrapper, GClosure* closure)
343 	{
344 		wrapper.remove(wrapper);
345 	}
346 
347 	protected class OnFullDelegateWrapper
348 	{
349 		void delegate(DataQueue) dlg;
350 		gulong handlerId;
351 
352 		this(void delegate(DataQueue) dlg)
353 		{
354 			this.dlg = dlg;
355 			onFullListeners ~= this;
356 		}
357 
358 		void remove(OnFullDelegateWrapper source)
359 		{
360 			foreach(index, wrapper; onFullListeners)
361 			{
362 				if (wrapper.handlerId == source.handlerId)
363 				{
364 					onFullListeners[index] = null;
365 					onFullListeners = std.algorithm.remove(onFullListeners, index);
366 					break;
367 				}
368 			}
369 		}
370 	}
371 	OnFullDelegateWrapper[] onFullListeners;
372 
373 	/**
374 	 * Reports that the queue became full (full).
375 	 * A queue is full if the total amount of data inside it (num-visible, time,
376 	 * size) is higher than the boundary values which can be set through the GObject
377 	 * properties.
378 	 */
379 	gulong addOnFull(void delegate(DataQueue) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
380 	{
381 		auto wrapper = new OnFullDelegateWrapper(dlg);
382 		wrapper.handlerId = Signals.connectData(
383 			this,
384 			"full",
385 			cast(GCallback)&callBackFull,
386 			cast(void*)wrapper,
387 			cast(GClosureNotify)&callBackFullDestroy,
388 			connectFlags);
389 		return wrapper.handlerId;
390 	}
391 
392 	extern(C) static void callBackFull(GstDataQueue* dataqueueStruct, OnFullDelegateWrapper wrapper)
393 	{
394 		wrapper.dlg(wrapper.outer);
395 	}
396 
397 	extern(C) static void callBackFullDestroy(OnFullDelegateWrapper wrapper, GClosure* closure)
398 	{
399 		wrapper.remove(wrapper);
400 	}
401 }