Sayonara Player
Loading...
Searching...
No Matches
EngineUtils.h
1/* EngineUtils.h */
2
3/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef SAYONARA_PLAYER_ENGINE_UTILS_H
22#define SAYONARA_PLAYER_ENGINE_UTILS_H
23
24#include "Utils/typedefs.h"
25
26#include <type_traits>
27#include <utility>
28#include <memory>
29#include <iostream>
30#include <gst/gst.h>
31
32template<typename T>
33class QList;
34
35namespace Engine::Utils
36{
37 using Elements = QList<GstElement*>;
38
39 template<typename T>
40 struct GObjectAutoFree
41 {
42 T* obj = nullptr;
43
44 explicit GObjectAutoFree(T* obj) :
45 obj(obj) {}
46
47 ~GObjectAutoFree()
48 {
49 g_free(obj);
50 obj = nullptr;
51 }
52
53 T* data() const { return obj; }
54
55 T* operator*() const { return data(); }
56 };
57
58 template<typename T>
59 class AutoUnref
60 {
61 public:
62 AutoUnref(T* element) : // NOLINT(google-explicit-constructor)
63 m_element {element} {}
64
65 ~AutoUnref()
66 {
67 gst_object_unref(m_element);
68 }
69
70 T* operator*() const { return m_element; }
71
72 private:
73 T* m_element;
74 };
75
76 using GStringAutoFree = GObjectAutoFree<gchar>;
77
78 void configureQueue(GstElement* queue, guint64 maxTimeMs = 100);
79
80 void configureSink(GstElement* sink);
81
82 void configureLame(GstElement* lame);
83
84 bool connectTee(GstElement* tee, GstElement* queue, const QString& queue_name);
85
86 bool hasElement(GstBin* bin, GstElement* element);
87
88 bool testAndError(void* element, const QString& errorstr);
89
90 bool testAndErrorBool(bool b, const QString& errorstr);
91
92 bool createElement(GstElement** elem, const QString& elementType);
93
94 bool createElement(GstElement** elem, const QString& elementType, const QString& prefix);
95
96 GValue getInt64(gint64 value);
97
98 GValue getUint64(guint64 value);
99
100 GValue getUint(guint value);
101
102 GValue getInt(gint value);
103
104 constexpr inline MilliSeconds getUpdateInterval() { return 50; }; // NOLINT(readability-magic-numbers)
105
106 template<typename T>
107 struct Dont_Use_Integers_In_GObject_Set
108 {
109 Dont_Use_Integers_In_GObject_Set(T value)
110 {
111 std::string("There's a wrong value somewhere") + value;
112 }
113 };
114
115 template<typename GlibObject, typename T>
116 void setValue(GlibObject* /*object*/, const gchar* /*key*/, T /*value*/, std::true_type)
117 {
119 }
120
121 template<typename GlibObject, typename T>
122 void setValue(GlibObject* object, const gchar* key, T value, std::false_type)
123 {
124 g_object_set(G_OBJECT(object), key, value, nullptr);
125 }
126
127 template<typename GlibObject, typename T>
128 void setValue(GlibObject* object, const gchar* key, T value)
129 {
130 constexpr bool b = (std::is_integral<T>::value) && (sizeof(T) > sizeof(bool));
131 setValue(object, key, value, std::integral_constant<bool, b>());
132 }
133
134 template<typename GlibObject, typename First>
135 void setValues(GlibObject* object, const gchar* key, First value)
136 {
137 setValue(object, key, value);
138 }
139
140 template<typename GlibObject, typename First, typename... Args>
141 void setValues(GlibObject* object, const gchar* key, First value, Args... args)
142 {
143 setValue(object, key, value);
144 setValues(object, std::forward<Args>(args)...);
145 }
146
147 template<typename GlibObject>
148 void setInt64Value(GlibObject* object, const gchar* key, gint64 value)
149 {
150 const auto val = getInt64(value);
151 g_object_set_property(G_OBJECT(object), key, &val);
152 }
153
154 template<typename GlibObject>
155 void setIntValue(GlibObject* object, const gchar* key, gint value)
156 {
157 const auto val = getInt(value);
158 g_object_set_property(G_OBJECT(object), key, &val);
159 }
160
161 template<typename GlibObject>
162 void setUint64Value(GlibObject* object, const gchar* key, guint64 value)
163 {
164 const auto val = getUint64(value);
165 g_object_set_property(G_OBJECT(object), key, &val);
166 }
167
168 template<typename GlibObject>
169 void setUintValue(GlibObject* object, const gchar* key, guint value)
170 {
171 const auto val = getUint(value);
172 g_object_set_property(G_OBJECT(object), key, &val);
173 }
174
175 MilliSeconds getDurationMs(GstElement* element);
176
177 MilliSeconds getPositionMs(GstElement* element);
178
179 MilliSeconds getTimeToGo(GstElement* element);
180
181 GstState getState(GstElement* element);
182
183 bool setState(GstElement* element, GstState state);
184
185 bool isPluginAvailable(const gchar* str);
186
187 bool isLameAvailable();
188
189 bool isPitchAvailable();
190
191 bool createBin(GstElement** bin, const Elements& elements, const QString& prefix);
192
193 bool createGhostPad(GstBin* bin, GstElement* e);
194
195 bool linkElements(const Elements& elements);
196
197 void unlinkElements(const Elements& elements);
198
199 bool addElements(GstBin* bin, const Elements& elements);
200
201 void removeElements(GstBin* bin, const Elements& elements);
202
203 void unrefElements(const Elements& elements);
204
205 enum PadType
206 {
207 src = 0,
208 sink
209 };
210 [[nodiscard]] AutoUnref<GstPad> getStaticPad(GstElement* element, PadType padType);
211
212 [[nodiscard]] QString getElementName(const GstElement* element);
213
214 [[nodiscard]] QString getObjectName(GstObject* element);
215}
216
217#endif // SAYONARA_PLAYER_ENGINE_UTILS_H
Definition EngineUtils.h:60
Definition EngineUtils.h:33
Definition EngineUtils.h:41