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.Task;
26 
27 private import glib.ConstructionException;
28 private import glib.RecMutex;
29 private import gobject.ObjectG;
30 private import gstreamer.ObjectGst;
31 private import gstreamer.TaskPool;
32 private import gstreamer.c.functions;
33 public  import gstreamer.c.types;
34 public  import gstreamerc.gstreamertypes;
35 
36 
37 /**
38  * #GstTask is used by #GstElement and #GstPad to provide the data passing
39  * threads in a #GstPipeline.
40  * 
41  * A #GstPad will typically start a #GstTask to push or pull data to/from the
42  * peer pads. Most source elements start a #GstTask to push data. In some cases
43  * a demuxer element can start a #GstTask to pull data from a peer element. This
44  * is typically done when the demuxer can perform random access on the upstream
45  * peer element for improved performance.
46  * 
47  * Although convenience functions exist on #GstPad to start/pause/stop tasks, it
48  * might sometimes be needed to create a #GstTask manually if it is not related to
49  * a #GstPad.
50  * 
51  * Before the #GstTask can be run, it needs a #GRecMutex that can be set with
52  * gst_task_set_lock().
53  * 
54  * The task can be started, paused and stopped with gst_task_start(), gst_task_pause()
55  * and gst_task_stop() respectively or with the gst_task_set_state() function.
56  * 
57  * A #GstTask will repeatedly call the #GstTaskFunction with the user data
58  * that was provided when creating the task with gst_task_new(). While calling
59  * the function it will acquire the provided lock. The provided lock is released
60  * when the task pauses or stops.
61  * 
62  * Stopping a task with gst_task_stop() will not immediately make sure the task is
63  * not running anymore. Use gst_task_join() to make sure the task is completely
64  * stopped and the thread is stopped.
65  * 
66  * After creating a #GstTask, use gst_object_unref() to free its resources. This can
67  * only be done when the task is not running anymore.
68  * 
69  * Task functions can send a #GstMessage to send out-of-band data to the
70  * application. The application can receive messages from the #GstBus in its
71  * mainloop.
72  * 
73  * For debugging purposes, the task will configure its object name as the thread
74  * name on Linux. Please note that the object name should be configured before the
75  * task is started; changing the object name after the task has been started, has
76  * no effect on the thread name.
77  */
78 public class Task : ObjectGst
79 {
80 	/** the main Gtk struct */
81 	protected GstTask* gstTask;
82 
83 	/** Get the main Gtk struct */
84 	public GstTask* getTaskStruct(bool transferOwnership = false)
85 	{
86 		if (transferOwnership)
87 			ownedRef = false;
88 		return gstTask;
89 	}
90 
91 	/** the main Gtk struct as a void* */
92 	protected override void* getStruct()
93 	{
94 		return cast(void*)gstTask;
95 	}
96 
97 	protected override void setStruct(GObject* obj)
98 	{
99 		gstTask = cast(GstTask*)obj;
100 		super.setStruct(obj);
101 	}
102 
103 	/**
104 	 * Sets our main struct and passes it to the parent class.
105 	 */
106 	public this (GstTask* gstTask, bool ownedRef = false)
107 	{
108 		this.gstTask = gstTask;
109 		super(cast(GstObject*)gstTask, ownedRef);
110 	}
111 
112 
113 	/** */
114 	public static GType getType()
115 	{
116 		return gst_task_get_type();
117 	}
118 
119 	/**
120 	 * Create a new Task that will repeatedly call the provided @func
121 	 * with @user_data as a parameter. Typically the task will run in
122 	 * a new thread.
123 	 *
124 	 * The function cannot be changed after the task has been created. You
125 	 * must create a new #GstTask to change the function.
126 	 *
127 	 * This function will not yet create and start a thread. Use gst_task_start() or
128 	 * gst_task_pause() to create and start the GThread.
129 	 *
130 	 * Before the task can be used, a #GRecMutex must be configured using the
131 	 * gst_task_set_lock() function. This lock will always be acquired while
132 	 * @func is called.
133 	 *
134 	 * Params:
135 	 *     func = The #GstTaskFunction to use
136 	 *     userData = User data to pass to @func
137 	 *     notify = the function to call when @user_data is no longer needed.
138 	 *
139 	 * Returns: A new #GstTask.
140 	 *
141 	 *     MT safe.
142 	 *
143 	 * Throws: ConstructionException GTK+ fails to create the object.
144 	 */
145 	public this(GstTaskFunction func, void* userData, GDestroyNotify notify)
146 	{
147 		auto p = gst_task_new(func, userData, notify);
148 
149 		if(p is null)
150 		{
151 			throw new ConstructionException("null returned by new");
152 		}
153 
154 		this(cast(GstTask*) p, true);
155 	}
156 
157 	/**
158 	 * Wait for all tasks to be stopped. This is mainly used internally
159 	 * to ensure proper cleanup of internal data structures in test suites.
160 	 *
161 	 * MT safe.
162 	 */
163 	public static void cleanupAll()
164 	{
165 		gst_task_cleanup_all();
166 	}
167 
168 	/**
169 	 * Get the #GstTaskPool that this task will use for its streaming
170 	 * threads.
171 	 *
172 	 * MT safe.
173 	 *
174 	 * Returns: the #GstTaskPool used by @task. gst_object_unref()
175 	 *     after usage.
176 	 */
177 	public TaskPool getPool()
178 	{
179 		auto p = gst_task_get_pool(gstTask);
180 
181 		if(p is null)
182 		{
183 			return null;
184 		}
185 
186 		return ObjectG.getDObject!(TaskPool)(cast(GstTaskPool*) p, true);
187 	}
188 
189 	/**
190 	 * Get the current state of the task.
191 	 *
192 	 * Returns: The #GstTaskState of the task
193 	 *
194 	 *     MT safe.
195 	 */
196 	public GstTaskState getState()
197 	{
198 		return gst_task_get_state(gstTask);
199 	}
200 
201 	/**
202 	 * Joins @task. After this call, it is safe to unref the task
203 	 * and clean up the lock set with gst_task_set_lock().
204 	 *
205 	 * The task will automatically be stopped with this call.
206 	 *
207 	 * This function cannot be called from within a task function as this
208 	 * would cause a deadlock. The function will detect this and print a
209 	 * g_warning.
210 	 *
211 	 * Returns: %TRUE if the task could be joined.
212 	 *
213 	 *     MT safe.
214 	 */
215 	public bool join()
216 	{
217 		return gst_task_join(gstTask) != 0;
218 	}
219 
220 	/**
221 	 * Pauses @task. This method can also be called on a task in the
222 	 * stopped state, in which case a thread will be started and will remain
223 	 * in the paused state. This function does not wait for the task to complete
224 	 * the paused state.
225 	 *
226 	 * Returns: %TRUE if the task could be paused.
227 	 *
228 	 *     MT safe.
229 	 */
230 	public bool pause()
231 	{
232 		return gst_task_pause(gstTask) != 0;
233 	}
234 
235 	/**
236 	 * Call @enter_func when the task function of @task is entered. @user_data will
237 	 * be passed to @enter_func and @notify will be called when @user_data is no
238 	 * longer referenced.
239 	 *
240 	 * Params:
241 	 *     enterFunc = a #GstTaskThreadFunc
242 	 *     userData = user data passed to @enter_func
243 	 *     notify = called when @user_data is no longer referenced
244 	 */
245 	public void setEnterCallback(GstTaskThreadFunc enterFunc, void* userData, GDestroyNotify notify)
246 	{
247 		gst_task_set_enter_callback(gstTask, enterFunc, userData, notify);
248 	}
249 
250 	/**
251 	 * Call @leave_func when the task function of @task is left. @user_data will
252 	 * be passed to @leave_func and @notify will be called when @user_data is no
253 	 * longer referenced.
254 	 *
255 	 * Params:
256 	 *     leaveFunc = a #GstTaskThreadFunc
257 	 *     userData = user data passed to @leave_func
258 	 *     notify = called when @user_data is no longer referenced
259 	 */
260 	public void setLeaveCallback(GstTaskThreadFunc leaveFunc, void* userData, GDestroyNotify notify)
261 	{
262 		gst_task_set_leave_callback(gstTask, leaveFunc, userData, notify);
263 	}
264 
265 	/**
266 	 * Set the mutex used by the task. The mutex will be acquired before
267 	 * calling the #GstTaskFunction.
268 	 *
269 	 * This function has to be called before calling gst_task_pause() or
270 	 * gst_task_start().
271 	 *
272 	 * MT safe.
273 	 *
274 	 * Params:
275 	 *     mutex = The #GRecMutex to use
276 	 */
277 	public void setLock(RecMutex mutex)
278 	{
279 		gst_task_set_lock(gstTask, (mutex is null) ? null : mutex.getRecMutexStruct());
280 	}
281 
282 	/**
283 	 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
284 	 * will be created by @task will now use @pool.
285 	 *
286 	 * MT safe.
287 	 *
288 	 * Params:
289 	 *     pool = a #GstTaskPool
290 	 */
291 	public void setPool(TaskPool pool)
292 	{
293 		gst_task_set_pool(gstTask, (pool is null) ? null : pool.getTaskPoolStruct());
294 	}
295 
296 	/**
297 	 * Sets the state of @task to @state.
298 	 *
299 	 * The @task must have a lock associated with it using
300 	 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
301 	 * this function will return %FALSE.
302 	 *
303 	 * MT safe.
304 	 *
305 	 * Params:
306 	 *     state = the new task state
307 	 *
308 	 * Returns: %TRUE if the state could be changed.
309 	 */
310 	public bool setState(GstTaskState state)
311 	{
312 		return gst_task_set_state(gstTask, state) != 0;
313 	}
314 
315 	/**
316 	 * Starts @task. The @task must have a lock associated with it using
317 	 * gst_task_set_lock() or this function will return %FALSE.
318 	 *
319 	 * Returns: %TRUE if the task could be started.
320 	 *
321 	 *     MT safe.
322 	 */
323 	public bool start()
324 	{
325 		return gst_task_start(gstTask) != 0;
326 	}
327 
328 	/**
329 	 * Stops @task. This method merely schedules the task to stop and
330 	 * will not wait for the task to have completely stopped. Use
331 	 * gst_task_join() to stop and wait for completion.
332 	 *
333 	 * Returns: %TRUE if the task could be stopped.
334 	 *
335 	 *     MT safe.
336 	 */
337 	public bool stop()
338 	{
339 		return gst_task_stop(gstTask) != 0;
340 	}
341 }