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