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
80mod sealed {
81    pub trait Sealed {}
82    impl<T: super::IsA<super::Task>> Sealed for T {}
83}
84
85/// Trait containing all [`struct@Task`] methods.
86///
87/// # Implementors
88///
89/// [`Task`][struct@crate::Task]
90pub trait TaskExt: IsA<Task> + sealed::Sealed + 'static {
91    /// Get the [`TaskPool`][crate::TaskPool] that this task will use for its streaming
92    /// threads.
93    ///
94    /// MT safe.
95    ///
96    /// # Returns
97    ///
98    /// the [`TaskPool`][crate::TaskPool] used by `self`. `gst_object_unref()`
99    /// after usage.
100    #[doc(alias = "gst_task_get_pool")]
101    #[doc(alias = "get_pool")]
102    fn pool(&self) -> TaskPool {
103        unsafe { from_glib_full(ffi::gst_task_get_pool(self.as_ref().to_glib_none().0)) }
104    }
105
106    /// Get the current state of the task.
107    ///
108    /// # Returns
109    ///
110    /// The [`TaskState`][crate::TaskState] of the task
111    ///
112    /// MT safe.
113    #[doc(alias = "gst_task_get_state")]
114    #[doc(alias = "get_state")]
115    fn state(&self) -> TaskState {
116        unsafe { from_glib(ffi::gst_task_get_state(self.as_ref().to_glib_none().0)) }
117    }
118
119    /// Joins `self`. After this call, it is safe to unref the task
120    /// and clean up the lock set with [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()].
121    ///
122    /// The task will automatically be stopped with this call.
123    ///
124    /// This function cannot be called from within a task function as this
125    /// would cause a deadlock. The function will detect this and print a
126    /// g_warning.
127    ///
128    /// # Returns
129    ///
130    /// [`true`] if the task could be joined.
131    ///
132    /// MT safe.
133    #[doc(alias = "gst_task_join")]
134    fn join(&self) -> Result<(), glib::error::BoolError> {
135        unsafe {
136            glib::result_from_gboolean!(
137                ffi::gst_task_join(self.as_ref().to_glib_none().0),
138                "Failed to join task"
139            )
140        }
141    }
142
143    /// Pauses `self`. This method can also be called on a task in the
144    /// stopped state, in which case a thread will be started and will remain
145    /// in the paused state. This function does not wait for the task to complete
146    /// the paused state.
147    ///
148    /// # Returns
149    ///
150    /// [`true`] if the task could be paused.
151    ///
152    /// MT safe.
153    #[doc(alias = "gst_task_pause")]
154    fn pause(&self) -> Result<(), glib::error::BoolError> {
155        unsafe {
156            glib::result_from_gboolean!(
157                ffi::gst_task_pause(self.as_ref().to_glib_none().0),
158                "Failed to pause task"
159            )
160        }
161    }
162
163    /// Resume `self` in case it was paused. If the task was stopped, it will
164    /// remain in that state and this function will return [`false`].
165    ///
166    /// # Returns
167    ///
168    /// [`true`] if the task could be resumed.
169    ///
170    /// MT safe.
171    #[cfg(feature = "v1_18")]
172    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
173    #[doc(alias = "gst_task_resume")]
174    fn resume(&self) -> Result<(), glib::error::BoolError> {
175        unsafe {
176            glib::result_from_gboolean!(
177                ffi::gst_task_resume(self.as_ref().to_glib_none().0),
178                "Failed to resume task"
179            )
180        }
181    }
182
183    /// Set `pool` as the new GstTaskPool for `self`. Any new streaming threads that
184    /// will be created by `self` will now use `pool`.
185    ///
186    /// MT safe.
187    /// ## `pool`
188    /// a [`TaskPool`][crate::TaskPool]
189    #[doc(alias = "gst_task_set_pool")]
190    fn set_pool(&self, pool: &impl IsA<TaskPool>) {
191        unsafe {
192            ffi::gst_task_set_pool(
193                self.as_ref().to_glib_none().0,
194                pool.as_ref().to_glib_none().0,
195            );
196        }
197    }
198
199    /// Sets the state of `self` to `state`.
200    ///
201    /// The `self` must have a lock associated with it using
202    /// [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()] when going to GST_TASK_STARTED or GST_TASK_PAUSED or
203    /// this function will return [`false`].
204    ///
205    /// MT safe.
206    /// ## `state`
207    /// the new task state
208    ///
209    /// # Returns
210    ///
211    /// [`true`] if the state could be changed.
212    #[doc(alias = "gst_task_set_state")]
213    fn set_state(&self, state: TaskState) -> Result<(), glib::error::BoolError> {
214        unsafe {
215            glib::result_from_gboolean!(
216                ffi::gst_task_set_state(self.as_ref().to_glib_none().0, state.into_glib()),
217                "Failed to set task state"
218            )
219        }
220    }
221
222    /// Starts `self`. The `self` must have a lock associated with it using
223    /// [`TaskExtManual::set_lock()`][crate::prelude::TaskExtManual::set_lock()] or this function will return [`false`].
224    ///
225    /// # Returns
226    ///
227    /// [`true`] if the task could be started.
228    ///
229    /// MT safe.
230    #[doc(alias = "gst_task_start")]
231    fn start(&self) -> Result<(), glib::error::BoolError> {
232        unsafe {
233            glib::result_from_gboolean!(
234                ffi::gst_task_start(self.as_ref().to_glib_none().0),
235                "Failed to start task"
236            )
237        }
238    }
239
240    /// Stops `self`. This method merely schedules the task to stop and
241    /// will not wait for the task to have completely stopped. Use
242    /// [`join()`][Self::join()] to stop and wait for completion.
243    ///
244    /// # Returns
245    ///
246    /// [`true`] if the task could be stopped.
247    ///
248    /// MT safe.
249    #[doc(alias = "gst_task_stop")]
250    fn stop(&self) -> Result<(), glib::error::BoolError> {
251        unsafe {
252            glib::result_from_gboolean!(
253                ffi::gst_task_stop(self.as_ref().to_glib_none().0),
254                "Failed to stop task"
255            )
256        }
257    }
258}
259
260impl<O: IsA<Task>> TaskExt for O {}