gstreamer/auto/task.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
4// DO NOT EDIT
5
6use crate::{ffi, Object, TaskPool, TaskState};
7use glib::{prelude::*, translate::*};
8
9glib::wrapper! {
10 /// [`Task`][crate::Task] is used by [`Element`][crate::Element] and [`Pad`][crate::Pad] to provide the data passing
11 /// threads in a [`Pipeline`][crate::Pipeline].
12 ///
13 /// A [`Pad`][crate::Pad] will typically start a [`Task`][crate::Task] to push or pull data to/from the
14 /// peer pads. Most source elements start a [`Task`][crate::Task] to push data. In some cases
15 /// a demuxer element can start a [`Task`][crate::Task] to pull data from a peer element. This
16 /// is typically done when the demuxer can perform random access on the upstream
17 /// peer element for improved performance.
18 ///
19 /// Although convenience functions exist on [`Pad`][crate::Pad] to start/pause/stop tasks, it
20 /// might sometimes be needed to create a [`Task`][crate::Task] manually if it is not related to
21 /// a [`Pad`][crate::Pad].
22 ///
23 /// Before the [`Task`][crate::Task] can be run, it needs a `GRecMutex` that can be set with
24 /// [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()].
25 ///
26 /// The task can be started, paused and stopped with [`TaskExt::start()`][crate::prelude::TaskExt::start()], [`TaskExt::pause()`][crate::prelude::TaskExt::pause()]
27 /// and [`TaskExt::stop()`][crate::prelude::TaskExt::stop()] respectively or with the [`TaskExt::set_state()`][crate::prelude::TaskExt::set_state()] function.
28 ///
29 /// A [`Task`][crate::Task] will repeatedly call the `GstTaskFunction` with the user data
30 /// that was provided when creating the task with [`new()`][Self::new()]. While calling
31 /// the function it will acquire the provided lock. The provided lock is released
32 /// when the task pauses or stops.
33 ///
34 /// Stopping a task with [`TaskExt::stop()`][crate::prelude::TaskExt::stop()] will not immediately make sure the task is
35 /// not running anymore. Use [`TaskExt::join()`][crate::prelude::TaskExt::join()] to make sure the task is completely
36 /// stopped and the thread is stopped.
37 ///
38 /// After creating a [`Task`][crate::Task], use `gst_object_unref()` to free its resources. This can
39 /// only be done when the task is not running anymore.
40 ///
41 /// Task functions can send a [`Message`][crate::Message] to send out-of-band data to the
42 /// application. The application can receive messages from the [`Bus`][crate::Bus] in its
43 /// mainloop.
44 ///
45 /// For debugging purposes, the task will configure its object name as the thread
46 /// name on Linux. Please note that the object name should be configured before the
47 /// task is started; changing the object name after the task has been started, has
48 /// no effect on the thread name.
49 ///
50 /// # Implements
51 ///
52 /// [`TaskExt`][trait@crate::prelude::TaskExt], [`GstObjectExt`][trait@crate::prelude::GstObjectExt], [`trait@glib::ObjectExt`]
53 #[doc(alias = "GstTask")]
54 pub struct Task(Object<ffi::GstTask, ffi::GstTaskClass>) @extends Object;
55
56 match fn {
57 type_ => || ffi::gst_task_get_type(),
58 }
59}
60
61impl Task {
62 pub const NONE: Option<&'static Task> = None;
63
64 /// Wait for all tasks to be stopped. This is mainly used internally
65 /// to ensure proper cleanup of internal data structures in test suites.
66 ///
67 /// MT safe.
68 #[doc(alias = "gst_task_cleanup_all")]
69 pub fn cleanup_all() {
70 assert_initialized_main_thread!();
71 unsafe {
72 ffi::gst_task_cleanup_all();
73 }
74 }
75}
76
77unsafe impl Send for Task {}
78unsafe impl Sync for Task {}
79
80/// Trait containing all [`struct@Task`] methods.
81///
82/// # Implementors
83///
84/// [`Task`][struct@crate::Task]
85pub trait TaskExt: IsA<Task> + 'static {
86 /// Get the [`TaskPool`][crate::TaskPool] that this task will use for its streaming
87 /// threads.
88 ///
89 /// MT safe.
90 ///
91 /// # Returns
92 ///
93 /// the [`TaskPool`][crate::TaskPool] used by `self`. `gst_object_unref()`
94 /// after usage.
95 #[doc(alias = "gst_task_get_pool")]
96 #[doc(alias = "get_pool")]
97 fn pool(&self) -> TaskPool {
98 unsafe { from_glib_full(ffi::gst_task_get_pool(self.as_ref().to_glib_none().0)) }
99 }
100
101 /// Get the current state of the task.
102 ///
103 /// # Returns
104 ///
105 /// The [`TaskState`][crate::TaskState] of the task
106 ///
107 /// MT safe.
108 #[doc(alias = "gst_task_get_state")]
109 #[doc(alias = "get_state")]
110 fn state(&self) -> TaskState {
111 unsafe { from_glib(ffi::gst_task_get_state(self.as_ref().to_glib_none().0)) }
112 }
113
114 /// Joins `self`. After this call, it is safe to unref the task
115 /// and clean up the lock set with [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()].
116 ///
117 /// The task will automatically be stopped with this call.
118 ///
119 /// This function cannot be called from within a task function as this
120 /// would cause a deadlock. The function will detect this and print a
121 /// g_warning.
122 ///
123 /// # Returns
124 ///
125 /// [`true`] if the task could be joined.
126 ///
127 /// MT safe.
128 #[doc(alias = "gst_task_join")]
129 fn join(&self) -> Result<(), glib::error::BoolError> {
130 unsafe {
131 glib::result_from_gboolean!(
132 ffi::gst_task_join(self.as_ref().to_glib_none().0),
133 "Failed to join task"
134 )
135 }
136 }
137
138 /// Pauses `self`. This method can also be called on a task in the
139 /// stopped state, in which case a thread will be started and will remain
140 /// in the paused state. This function does not wait for the task to complete
141 /// the paused state.
142 ///
143 /// # Returns
144 ///
145 /// [`true`] if the task could be paused.
146 ///
147 /// MT safe.
148 #[doc(alias = "gst_task_pause")]
149 fn pause(&self) -> Result<(), glib::error::BoolError> {
150 unsafe {
151 glib::result_from_gboolean!(
152 ffi::gst_task_pause(self.as_ref().to_glib_none().0),
153 "Failed to pause task"
154 )
155 }
156 }
157
158 /// Resume `self` in case it was paused. If the task was stopped, it will
159 /// remain in that state and this function will return [`false`].
160 ///
161 /// # Returns
162 ///
163 /// [`true`] if the task could be resumed.
164 ///
165 /// MT safe.
166 #[cfg(feature = "v1_18")]
167 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
168 #[doc(alias = "gst_task_resume")]
169 fn resume(&self) -> Result<(), glib::error::BoolError> {
170 unsafe {
171 glib::result_from_gboolean!(
172 ffi::gst_task_resume(self.as_ref().to_glib_none().0),
173 "Failed to resume task"
174 )
175 }
176 }
177
178 /// Set `pool` as the new GstTaskPool for `self`. Any new streaming threads that
179 /// will be created by `self` will now use `pool`.
180 ///
181 /// MT safe.
182 /// ## `pool`
183 /// a [`TaskPool`][crate::TaskPool]
184 #[doc(alias = "gst_task_set_pool")]
185 fn set_pool(&self, pool: &impl IsA<TaskPool>) {
186 unsafe {
187 ffi::gst_task_set_pool(
188 self.as_ref().to_glib_none().0,
189 pool.as_ref().to_glib_none().0,
190 );
191 }
192 }
193
194 /// Sets the state of `self` to `state`.
195 ///
196 /// The `self` must have a lock associated with it using
197 /// [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()] when going to GST_TASK_STARTED or GST_TASK_PAUSED or
198 /// this function will return [`false`].
199 ///
200 /// MT safe.
201 /// ## `state`
202 /// the new task state
203 ///
204 /// # Returns
205 ///
206 /// [`true`] if the state could be changed.
207 #[doc(alias = "gst_task_set_state")]
208 fn set_state(&self, state: TaskState) -> Result<(), glib::error::BoolError> {
209 unsafe {
210 glib::result_from_gboolean!(
211 ffi::gst_task_set_state(self.as_ref().to_glib_none().0, state.into_glib()),
212 "Failed to set task state"
213 )
214 }
215 }
216
217 /// Starts `self`. The `self` must have a lock associated with it using
218 /// [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()] or this function will return [`false`].
219 ///
220 /// # Returns
221 ///
222 /// [`true`] if the task could be started.
223 ///
224 /// MT safe.
225 #[doc(alias = "gst_task_start")]
226 fn start(&self) -> Result<(), glib::error::BoolError> {
227 unsafe {
228 glib::result_from_gboolean!(
229 ffi::gst_task_start(self.as_ref().to_glib_none().0),
230 "Failed to start task"
231 )
232 }
233 }
234
235 /// Stops `self`. This method merely schedules the task to stop and
236 /// will not wait for the task to have completely stopped. Use
237 /// [`join()`][Self::join()] to stop and wait for completion.
238 ///
239 /// # Returns
240 ///
241 /// [`true`] if the task could be stopped.
242 ///
243 /// MT safe.
244 #[doc(alias = "gst_task_stop")]
245 fn stop(&self) -> Result<(), glib::error::BoolError> {
246 unsafe {
247 glib::result_from_gboolean!(
248 ffi::gst_task_stop(self.as_ref().to_glib_none().0),
249 "Failed to stop task"
250 )
251 }
252 }
253}
254
255impl<O: IsA<Task>> TaskExt for O {}