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