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 	/**
98 	 * Sets our main struct and passes it to the parent class.
99 	 */
100 	public this (GstTask* gstTask, bool ownedRef = false)
101 	{
102 		this.gstTask = gstTask;
103 		super(cast(GstObject*)gstTask, ownedRef);
104 	}
105 
106 
107 	/** */
108 	public static GType getType()
109 	{
110 		return gst_task_get_type();
111 	}
112 
113 	/**
114 	 * Create a new Task that will repeatedly call the provided @func
115 	 * with @user_data as a parameter. Typically the task will run in
116 	 * a new thread.
117 	 *
118 	 * The function cannot be changed after the task has been created. You
119 	 * must create a new #GstTask to change the function.
120 	 *
121 	 * This function will not yet create and start a thread. Use gst_task_start() or
122 	 * gst_task_pause() to create and start the GThread.
123 	 *
124 	 * Before the task can be used, a #GRecMutex must be configured using the
125 	 * gst_task_set_lock() function. This lock will always be acquired while
126 	 * @func is called.
127 	 *
128 	 * Params:
129 	 *     func = The #GstTaskFunction to use
130 	 *     userData = User data to pass to @func
131 	 *     notify = the function to call when @user_data is no longer needed.
132 	 *
133 	 * Returns: A new #GstTask.
134 	 *
135 	 *     MT safe.
136 	 *
137 	 * Throws: ConstructionException GTK+ fails to create the object.
138 	 */
139 	public this(GstTaskFunction func, void* userData, GDestroyNotify notify)
140 	{
141 		auto p = gst_task_new(func, userData, notify);
142 
143 		if(p is null)
144 		{
145 			throw new ConstructionException("null returned by new");
146 		}
147 
148 		this(cast(GstTask*) p, true);
149 	}
150 
151 	/**
152 	 * Wait for all tasks to be stopped. This is mainly used internally
153 	 * to ensure proper cleanup of internal data structures in test suites.
154 	 *
155 	 * MT safe.
156 	 */
157 	public static void cleanupAll()
158 	{
159 		gst_task_cleanup_all();
160 	}
161 
162 	/**
163 	 * Get the #GstTaskPool that this task will use for its streaming
164 	 * threads.
165 	 *
166 	 * MT safe.
167 	 *
168 	 * Returns: the #GstTaskPool used by @task. gst_object_unref()
169 	 *     after usage.
170 	 */
171 	public TaskPool getPool()
172 	{
173 		auto p = gst_task_get_pool(gstTask);
174 
175 		if(p is null)
176 		{
177 			return null;
178 		}
179 
180 		return ObjectG.getDObject!(TaskPool)(cast(GstTaskPool*) p, true);
181 	}
182 
183 	/**
184 	 * Get the current state of the task.
185 	 *
186 	 * Returns: The #GstTaskState of the task
187 	 *
188 	 *     MT safe.
189 	 */
190 	public GstTaskState getState()
191 	{
192 		return gst_task_get_state(gstTask);
193 	}
194 
195 	/**
196 	 * Joins @task. After this call, it is safe to unref the task
197 	 * and clean up the lock set with gst_task_set_lock().
198 	 *
199 	 * The task will automatically be stopped with this call.
200 	 *
201 	 * This function cannot be called from within a task function as this
202 	 * would cause a deadlock. The function will detect this and print a
203 	 * g_warning.
204 	 *
205 	 * Returns: %TRUE if the task could be joined.
206 	 *
207 	 *     MT safe.
208 	 */
209 	public bool join()
210 	{
211 		return gst_task_join(gstTask) != 0;
212 	}
213 
214 	/**
215 	 * Pauses @task. This method can also be called on a task in the
216 	 * stopped state, in which case a thread will be started and will remain
217 	 * in the paused state. This function does not wait for the task to complete
218 	 * the paused state.
219 	 *
220 	 * Returns: %TRUE if the task could be paused.
221 	 *
222 	 *     MT safe.
223 	 */
224 	public bool pause()
225 	{
226 		return gst_task_pause(gstTask) != 0;
227 	}
228 
229 	/**
230 	 * Call @enter_func when the task function of @task is entered. @user_data will
231 	 * be passed to @enter_func and @notify will be called when @user_data is no
232 	 * longer referenced.
233 	 *
234 	 * Params:
235 	 *     enterFunc = a #GstTaskThreadFunc
236 	 *     userData = user data passed to @enter_func
237 	 *     notify = called when @user_data is no longer referenced
238 	 */
239 	public void setEnterCallback(GstTaskThreadFunc enterFunc, void* userData, GDestroyNotify notify)
240 	{
241 		gst_task_set_enter_callback(gstTask, enterFunc, userData, notify);
242 	}
243 
244 	/**
245 	 * Call @leave_func when the task function of @task is left. @user_data will
246 	 * be passed to @leave_func and @notify will be called when @user_data is no
247 	 * longer referenced.
248 	 *
249 	 * Params:
250 	 *     leaveFunc = a #GstTaskThreadFunc
251 	 *     userData = user data passed to @leave_func
252 	 *     notify = called when @user_data is no longer referenced
253 	 */
254 	public void setLeaveCallback(GstTaskThreadFunc leaveFunc, void* userData, GDestroyNotify notify)
255 	{
256 		gst_task_set_leave_callback(gstTask, leaveFunc, userData, notify);
257 	}
258 
259 	/**
260 	 * Set the mutex used by the task. The mutex will be acquired before
261 	 * calling the #GstTaskFunction.
262 	 *
263 	 * This function has to be called before calling gst_task_pause() or
264 	 * gst_task_start().
265 	 *
266 	 * MT safe.
267 	 *
268 	 * Params:
269 	 *     mutex = The #GRecMutex to use
270 	 */
271 	public void setLock(RecMutex mutex)
272 	{
273 		gst_task_set_lock(gstTask, (mutex is null) ? null : mutex.getRecMutexStruct());
274 	}
275 
276 	/**
277 	 * Set @pool as the new GstTaskPool for @task. Any new streaming threads that
278 	 * will be created by @task will now use @pool.
279 	 *
280 	 * MT safe.
281 	 *
282 	 * Params:
283 	 *     pool = a #GstTaskPool
284 	 */
285 	public void setPool(TaskPool pool)
286 	{
287 		gst_task_set_pool(gstTask, (pool is null) ? null : pool.getTaskPoolStruct());
288 	}
289 
290 	/**
291 	 * Sets the state of @task to @state.
292 	 *
293 	 * The @task must have a lock associated with it using
294 	 * gst_task_set_lock() when going to GST_TASK_STARTED or GST_TASK_PAUSED or
295 	 * this function will return %FALSE.
296 	 *
297 	 * MT safe.
298 	 *
299 	 * Params:
300 	 *     state = the new task state
301 	 *
302 	 * Returns: %TRUE if the state could be changed.
303 	 */
304 	public bool setState(GstTaskState state)
305 	{
306 		return gst_task_set_state(gstTask, state) != 0;
307 	}
308 
309 	/**
310 	 * Starts @task. The @task must have a lock associated with it using
311 	 * gst_task_set_lock() or this function will return %FALSE.
312 	 *
313 	 * Returns: %TRUE if the task could be started.
314 	 *
315 	 *     MT safe.
316 	 */
317 	public bool start()
318 	{
319 		return gst_task_start(gstTask) != 0;
320 	}
321 
322 	/**
323 	 * Stops @task. This method merely schedules the task to stop and
324 	 * will not wait for the task to have completely stopped. Use
325 	 * gst_task_join() to stop and wait for completion.
326 	 *
327 	 * Returns: %TRUE if the task could be stopped.
328 	 *
329 	 *     MT safe.
330 	 */
331 	public bool stop()
332 	{
333 		return gst_task_stop(gstTask) != 0;
334 	}
335 }