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