Debian Codegraph @

gstreamer/src/task.hg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008-2015 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gst/gst.h>
21 #include <gstreamermm/object.h>
22 #include <glibmm/threads.h>
23 #include <memory>
24
25 _DEFS(gstreamermm,gst)
26
27 namespace Gst
28 {
29
30 _WRAP_ENUM(TaskState, GstTaskState)
31
32 class TaskPool;
33
34 /** A class representing GStreamer streaming threads.
35 * Gst::Task is used by Gst::Element and Gst::Pad to provide the data passing
36 * threads in a Gst::Pipeline.
37 *
38 * A Gst::Pad will typically start a Gst::Task to push or pull data to/from the
39 * peer pads. Most source elements start a Gst::Task to push data. In some
40 * cases a demuxer element can start a Gst::Task to pull data from a peer
41 * element. This is typically done when the demuxer can perform random access
42 * on the upstream peer element for improved performance.
43 *
44 * Although convenience functions exist on Gst::Pad to start/pause/stop tasks,
45 * it might sometimes be needed to create a Gst::Task manually if it is not
46 * related to a Gst::Pad.
47 *
48 * Before the Gst::Task can be run, it needs a Glib::StaticRecMutex that can be
49 * set with set_lock().
50 *
51 * The task can be started, paused and stopped with start(), pause() and stop()
52 * respectively.
53 *
54 * A Gst::Task will repeadedly call the Gst::Task::SlotTask that was provided
55 * when creating the task with create(). Before calling the function it will
56 * acquire the provided lock.
57 *
58 * Stopping a task with stop() will not immediatly make sure the task is not
59 * running anymore. Use join() to make sure the task is completely stopped and
60 * the thread is stopped.
61 *
62 * Task functions can send a Gst::Message to send out-of-band data to the
63 * application. The application can receive messages from the Gst::Bus in its
64 * mainloop.
65 *
66 * For debugging purposes, the task will configure its object name as the thread
67 * name on Linux. Please note that the object name should be configured before the
68 * task is started; changing the object name after the task has been started, has
69 * no effect on the thread name.
70 *
71 * Last reviewed on 2015-10-14 (1.5.2)
72 */
73 class Task : public Object
74 {
75 _CLASS_GOBJECT(Task, GstTask, GST_TASK, Object, GstObject)
76
77 public:
78 /** For example,
79 * void on_do_task();.
80 */
81 typedef sigc::slot<void> SlotTask;
82
83 /**
84 * Bidirectional slot
85 */
86 typedef sigc::slot<void, const Glib::RefPtr<Gst::Task>&,
87 Glib::Threads::Thread*> TaskThreadSlot;
88 /** For example,
89 * void on_enter(const Glib::RefPtr<Gst::Task>& task, Glib::Threads::Thread&
90 * thread);.
91 * A thread is entered, this slot is called when the new thread enters its
92 * function.
93 */
94 typedef TaskThreadSlot SlotEnter;
95
96 /** For example,
97 * void on_leave(const Glib::RefPtr<Gst::Task>& task, Glib::Threads::Thread&
98 * thread);.
99 * A thread is exiting, this is called when the thread is about to leave its
100 * function.
101 */
102 typedef TaskThreadSlot SlotLeave;
103
104 protected:
105 explicit Task(const SlotTask& task_slot);
106
107 public:
108 /** Create a new Gst::Task that will repeatedly call the provided slot as a
109 * parameter. Typically the task will run in a new thread.
110 *
111 * The slot cannot be changed after the task has been created. You must
112 * create a new Gst::Task to change the slot.
113 *
114 * @param task_slot The Gst::Task::SlotTask to use.
115 * @return A new Gst::Task. MT safe.
116 */
117 _WRAP_CREATE(const SlotTask& task_slot)
118
119 _WRAP_METHOD(void set_lock(Glib::Threads::RecMutex& mutex), gst_task_set_lock)
120 _WRAP_METHOD(void set_pool(const Glib::RefPtr<Gst::TaskPool>& pool), gst_task_set_pool)
121 _WRAP_METHOD(Glib::RefPtr<Gst::TaskPool> get_pool(), gst_task_get_pool)
122 _WRAP_METHOD(Glib::RefPtr<const Gst::TaskPool> get_pool() const, gst_task_get_pool)
123
124 /** Call enter_slot when the task function of task is entered.
125 *
126 * @param enter_slot
127 */
128 void set_enter_slot(const SlotEnter& enter_slot);
129 _IGNORE(gst_task_set_enter_callback)
130
131 /** Call leave_func when the task function of task is left.
132 *
133 * @param leave_slot
134 */
135 void set_leave_slot(const SlotLeave& leave_slot);
136 _IGNORE(gst_task_set_leave_callback)
137
138 _WRAP_METHOD(TaskState get_state() const, gst_task_get_state)
139 _WRAP_METHOD(void set_state(Gst::TaskState state), gst_task_set_state)
140 _WRAP_METHOD(bool pause(), gst_task_pause)
141 _WRAP_METHOD(bool start(), gst_task_start)
142 _WRAP_METHOD(bool stop(), gst_task_stop)
143 _WRAP_METHOD(bool join(), gst_task_join)
144 _WRAP_METHOD(static void cleanup_all(), gst_task_cleanup_all)
145
146 #ifndef DOXYGEN_SHOULD_SKIP_THIS
147 private:
148 // todo this slot should be moved in move constructor, but for now it's
149 // impossible to provide custom move constructor
150 // (see https://bugzilla.gnome.org/show_bug.cgi?id=756593).
151 // However, task should be managed by RefPtr class, so move constructor
152 // and move assignment operator will never be called.
153 std::unique_ptr<SlotTask> m_slot;
154 #endif
155 };
156
157 } // namespace Gst