-
Notifications
You must be signed in to change notification settings - Fork 10
/
video_presets.cpp
287 lines (229 loc) Β· 6.82 KB
/
video_presets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* 2020 Tarpeeksi Hyvae Soft
*
* Software: VCS
*
*/
#include <algorithm>
#include <vector>
#include "capture/video_presets.h"
#include "capture/capture.h"
vcs_event_c<const video_preset_s*> kc_ev_video_preset_params_changed;
static std::vector<video_preset_s*> PRESETS;
// While true, no preset will activate. If a preset is already active, it'll
// remain so.
static bool LOCKED = false;
// Incremented for each new preset added, and used as the id for that preset.
static unsigned RUNNING_PRESET_ID = 0;
static const video_preset_s *MOST_RECENT_ACTIVE_PRESET = nullptr;
static video_preset_s* strongest_activating_preset(void)
{
if (!kc_has_signal())
{
return nullptr;
}
const auto resolution = resolution_s::from_capture_device_properties();
const auto refreshRate = refresh_rate_s::from_capture_device_properties();
std::vector<std::pair<unsigned/*preset id*/, int/*preset activation level*/>> activationLevels;
int strongestPresetId = -1;
int highestActivationLevel = 0;
for (auto *preset: PRESETS)
{
const int activationLevel = preset->activation_level(resolution, refreshRate);
if (activationLevel > highestActivationLevel)
{
highestActivationLevel = activationLevel;
strongestPresetId = int(preset->id);
}
}
// If no presets activated strongly enough.
if ((highestActivationLevel <= 0) ||
(strongestPresetId < 0))
{
return nullptr;
}
else
{
return kvideopreset_get_preset_ptr(unsigned(strongestPresetId));
}
}
subsystem_releaser_t kvideopreset_initialize(void)
{
DEBUG(("Initializing the video presets subsystem."));
// Listen for app events.
{
ev_new_video_mode.listen(kvideopreset_apply_current_active_preset);
kc_ev_video_preset_params_changed.listen([](const video_preset_s *preset)
{
k_assert(preset, "Expected a non-null preset.");
if (preset == MOST_RECENT_ACTIVE_PRESET)
{
video_properties_s::to_capture_device_properties(preset->properties);
}
});
ev_video_preset_activated.listen([](const video_preset_s *preset)
{
MOST_RECENT_ACTIVE_PRESET = preset;
});
}
return []{};
}
void kvideopreset_lock(const bool isLocked)
{
LOCKED = isLocked;
return;
}
void kvideopreset_activate_preset(const video_preset_s *const preset)
{
if (
!preset ||
(LOCKED && (preset != MOST_RECENT_ACTIVE_PRESET))
){
return;
}
video_properties_s::to_capture_device_properties(preset->properties);
ev_video_preset_activated.fire(preset);
return;
}
bool kvideopreset_is_preset_active(const video_preset_s *const preset)
{
return (
MOST_RECENT_ACTIVE_PRESET &&
(preset == MOST_RECENT_ACTIVE_PRESET)
);
}
bool kvideopreset_remove_preset(const unsigned presetId)
{
const auto targetPreset = std::find_if(PRESETS.begin(), PRESETS.end(), [=](video_preset_s *p){return p->id == presetId;});
if (targetPreset == PRESETS.end())
{
return false;
}
if ((*targetPreset) == MOST_RECENT_ACTIVE_PRESET)
{
MOST_RECENT_ACTIVE_PRESET = nullptr;
}
delete (*targetPreset);
PRESETS.erase(targetPreset);
return true;
}
void kvideopreset_remove_all_presets(void)
{
for (auto *preset: PRESETS)
{
delete preset;
}
PRESETS.clear();
RUNNING_PRESET_ID = 0;
MOST_RECENT_ACTIVE_PRESET = nullptr;
return;
}
void kvideopreset_apply_current_active_preset(void)
{
if (!kc_has_signal())
{
return;
}
const video_preset_s *activePreset = (LOCKED? MOST_RECENT_ACTIVE_PRESET : strongest_activating_preset());
if (activePreset)
{
video_properties_s::to_capture_device_properties(activePreset->properties);
}
else
{
video_properties_s::to_capture_device_properties(video_properties_s::from_capture_device_properties(": default"));
}
ev_video_preset_activated.fire(activePreset);
return;
}
const std::vector<video_preset_s*>& kvideopreset_all_presets(void)
{
return PRESETS;
}
video_preset_s* kvideopreset_get_preset_ptr(const unsigned presetId)
{
for (auto *preset: PRESETS)
{
if (preset->id == presetId)
{
return preset;
}
}
return nullptr;
}
void kvideopreset_activate_keyboard_shortcut(const std::string &shortcutString)
{
if (LOCKED || !kc_has_signal())
{
return;
}
for (auto *preset: PRESETS)
{
if (preset->activates_with_shortcut(shortcutString))
{
kvideopreset_activate_preset(preset);
return;
}
}
return;
}
void kvideopreset_assign_presets(const std::vector<video_preset_s*> &presets)
{
kvideopreset_remove_all_presets();
PRESETS = presets;
kvideopreset_apply_current_active_preset();
RUNNING_PRESET_ID = (1 + (*std::max_element(PRESETS.begin(), PRESETS.end(), [](const video_preset_s *a, const video_preset_s *b){return a->id < b->id;}))->id);
return;
}
video_preset_s* kvideopreset_create_new_preset(const video_preset_s *const duplicateDataSrc)
{
video_preset_s *const preset = new video_preset_s;
const std::string baseName = (duplicateDataSrc? "Duplicate preset" : "Preset");
if (duplicateDataSrc)
{
*preset = *duplicateDataSrc;
}
else
{
preset->activatesWithResolution = false;
preset->activationResolution = {.w = 640, .h = 480};
preset->properties = video_properties_s::from_capture_device_properties(": default");
}
preset->id = RUNNING_PRESET_ID++;
preset->name = (baseName + " " + std::to_string(RUNNING_PRESET_ID));
PRESETS.push_back(preset);
return preset;
}
const video_preset_s* kvideopreset_current_active_preset()
{
return MOST_RECENT_ACTIVE_PRESET;
}
properties_map_t video_properties_s::from_capture_device_properties(const std::string &nameSpace)
{
properties_map_t properties;
for (const std::string propName: kc_supported_video_preset_properties())
{
properties[propName] = kc_device_property(propName + nameSpace);
}
return properties;
}
void video_properties_s::to_capture_device_properties(
const properties_map_t &properties,
const std::string &nameSpace
)
{
#if defined(CAPTURE_BACKEND_VISION_V4L) || defined(CAPTURE_BACKEND_GENERIC_V4L)
// If the namespace is empty, the values would modify the current capture
// parameters. But if there's no signal, the parameters won't be available
// for modification via V4L and we'd be wasting time trying to change them.
if (!kc_has_signal() && nameSpace.empty())
{
return;
}
#endif
for (const auto &[propName, value]: properties)
{
kc_set_device_property((propName + nameSpace), value);
}
return;
}