forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-interface.cpp
384 lines (316 loc) · 9 KB
/
api-interface.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <obs-frontend-internal.hpp>
#include "obs-app.hpp"
#include "qt-wrappers.hpp"
#include "window-basic-main.hpp"
#include "window-basic-main-outputs.hpp"
#include <functional>
using namespace std;
Q_DECLARE_METATYPE(OBSScene);
Q_DECLARE_METATYPE(OBSSource);
template <typename T>
static T GetOBSRef(QListWidgetItem *item)
{
return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
}
void EnumProfiles(function<bool (const char *, const char *)> &&cb);
void EnumSceneCollections(function<bool (const char *, const char *)> &&cb);
/* ------------------------------------------------------------------------- */
template<typename T> struct OBSStudioCallback {
T callback;
void *private_data;
inline OBSStudioCallback(T cb, void *p) :
callback(cb), private_data(p)
{}
};
template <typename T> inline size_t GetCallbackIdx(
vector<OBSStudioCallback<T>> &callbacks,
T callback, void *private_data)
{
for (size_t i = 0; i < callbacks.size(); i++) {
OBSStudioCallback<T> curCB = callbacks[i];
if (curCB.callback == callback &&
curCB.private_data == private_data)
return i;
}
return (size_t)-1;
}
struct OBSStudioAPI : obs_frontend_callbacks {
OBSBasic *main;
vector<OBSStudioCallback<obs_frontend_event_cb>> callbacks;
vector<OBSStudioCallback<obs_frontend_save_cb>> saveCallbacks;
inline OBSStudioAPI(OBSBasic *main_) : main(main_) {}
void *obs_frontend_get_main_window(void) override
{
return (void*)main;
}
void *obs_frontend_get_main_window_handle(void) override
{
return (void*)main->winId();
}
void obs_frontend_get_scenes(
struct obs_frontend_source_list *sources) override
{
for (int i = 0; i < main->ui->scenes->count(); i++) {
QListWidgetItem *item = main->ui->scenes->item(i);
OBSScene scene = GetOBSRef<OBSScene>(item);
obs_source_t *source = obs_scene_get_source(scene);
obs_source_addref(source);
da_push_back(sources->sources, &source);
}
}
obs_source_t *obs_frontend_get_current_scene(void) override
{
OBSSource source;
if (main->IsPreviewProgramMode()) {
source = obs_weak_source_get_source(main->programScene);
} else {
source = main->GetCurrentSceneSource();
obs_source_addref(source);
}
return source;
}
void obs_frontend_set_current_scene(obs_source_t *scene) override
{
if (main->IsPreviewProgramMode()) {
QMetaObject::invokeMethod(main, "TransitionToScene",
Q_ARG(OBSSource, OBSSource(scene)));
} else {
QMetaObject::invokeMethod(main, "SetCurrentScene",
Q_ARG(OBSSource, OBSSource(scene)),
Q_ARG(bool, false));
}
}
void obs_frontend_get_transitions(
struct obs_frontend_source_list *sources) override
{
for (int i = 0; i < main->ui->transitions->count(); i++) {
OBSSource tr = main->ui->transitions->itemData(i)
.value<OBSSource>();
obs_source_addref(tr);
da_push_back(sources->sources, &tr);
}
}
obs_source_t *obs_frontend_get_current_transition(void) override
{
OBSSource tr = main->GetCurrentTransition();
obs_source_addref(tr);
return tr;
}
void obs_frontend_set_current_transition(
obs_source_t *transition) override
{
QMetaObject::invokeMethod(main, "SetTransition",
Q_ARG(OBSSource, OBSSource(transition)));
}
void obs_frontend_get_scene_collections(
std::vector<std::string> &strings) override
{
auto addCollection = [&](const char *name, const char *)
{
strings.emplace_back(name);
return true;
};
EnumSceneCollections(addCollection);
}
char *obs_frontend_get_current_scene_collection(void) override
{
const char *cur_name = config_get_string(App()->GlobalConfig(),
"Basic", "SceneCollection");
return bstrdup(cur_name);
}
void obs_frontend_set_current_scene_collection(
const char *collection) override
{
QList<QAction*> menuActions =
main->ui->sceneCollectionMenu->actions();
QString qstrCollection = QT_UTF8(collection);
for (int i = 0; i < menuActions.count(); i++) {
QAction *action = menuActions[i];
QVariant v = action->property("file_name");
if (v.typeName() != nullptr) {
if (action->text() == qstrCollection) {
action->trigger();
break;
}
}
}
}
void obs_frontend_get_profiles(
std::vector<std::string> &strings) override
{
auto addProfile = [&](const char *name, const char *)
{
strings.emplace_back(name);
return true;
};
EnumProfiles(addProfile);
}
char *obs_frontend_get_current_profile(void) override
{
const char *name = config_get_string(App()->GlobalConfig(),
"Basic", "Profile");
return bstrdup(name);
}
void obs_frontend_set_current_profile(const char *profile) override
{
QList<QAction*> menuActions =
main->ui->profileMenu->actions();
QString qstrProfile = QT_UTF8(profile);
for (int i = 0; i < menuActions.count(); i++) {
QAction *action = menuActions[i];
QVariant v = action->property("file_name");
if (v.typeName() != nullptr) {
if (action->text() == qstrProfile) {
action->trigger();
break;
}
}
}
}
void obs_frontend_streaming_start(void) override
{
QMetaObject::invokeMethod(main, "StartStreaming");
}
void obs_frontend_streaming_stop(void) override
{
QMetaObject::invokeMethod(main, "StopStreaming");
}
bool obs_frontend_streaming_active(void) override
{
return main->outputHandler->StreamingActive();
}
void obs_frontend_recording_start(void) override
{
QMetaObject::invokeMethod(main, "StartRecording");
}
void obs_frontend_recording_stop(void) override
{
QMetaObject::invokeMethod(main, "StopRecording");
}
bool obs_frontend_recording_active(void) override
{
return main->outputHandler->RecordingActive();
}
void obs_frontend_replay_buffer_start(void) override
{
QMetaObject::invokeMethod(main, "StartReplayBuffer");
}
void obs_frontend_replay_buffer_stop(void) override
{
QMetaObject::invokeMethod(main, "StopReplayBuffer");
}
bool obs_frontend_replay_buffer_active(void) override
{
return main->outputHandler->ReplayBufferActive();
}
void *obs_frontend_add_tools_menu_qaction(const char *name) override
{
main->ui->menuTools->setEnabled(true);
return (void*)main->ui->menuTools->addAction(QT_UTF8(name));
}
void obs_frontend_add_tools_menu_item(const char *name,
obs_frontend_cb callback, void *private_data) override
{
main->ui->menuTools->setEnabled(true);
auto func = [private_data, callback] ()
{
callback(private_data);
};
QAction *action = main->ui->menuTools->addAction(QT_UTF8(name));
QObject::connect(action, &QAction::triggered, func);
}
void obs_frontend_add_event_callback(obs_frontend_event_cb callback,
void *private_data) override
{
size_t idx = GetCallbackIdx(callbacks, callback, private_data);
if (idx == (size_t)-1)
callbacks.emplace_back(callback, private_data);
}
void obs_frontend_remove_event_callback(obs_frontend_event_cb callback,
void *private_data) override
{
size_t idx = GetCallbackIdx(callbacks, callback, private_data);
if (idx == (size_t)-1)
return;
callbacks.erase(callbacks.begin() + idx);
}
obs_output_t *obs_frontend_get_streaming_output(void) override
{
OBSOutput output = main->outputHandler->streamOutput;
obs_output_addref(output);
return output;
}
obs_output_t *obs_frontend_get_recording_output(void) override
{
OBSOutput out = main->outputHandler->fileOutput;
obs_output_addref(out);
return out;
}
obs_output_t *obs_frontend_get_replay_buffer_output(void) override
{
OBSOutput out = main->outputHandler->replayBuffer;
obs_output_addref(out);
return out;
}
config_t *obs_frontend_get_profile_config(void) override
{
return main->basicConfig;
}
config_t *obs_frontend_get_global_config(void) override
{
return App()->GlobalConfig();
}
void obs_frontend_save(void) override
{
main->SaveProject();
}
void obs_frontend_add_save_callback(obs_frontend_save_cb callback,
void *private_data) override
{
size_t idx = GetCallbackIdx(saveCallbacks, callback,
private_data);
if (idx == (size_t)-1)
saveCallbacks.emplace_back(callback, private_data);
}
void obs_frontend_remove_save_callback(obs_frontend_save_cb callback,
void *private_data) override
{
size_t idx = GetCallbackIdx(saveCallbacks, callback,
private_data);
if (idx == (size_t)-1)
return;
saveCallbacks.erase(saveCallbacks.begin() + idx);
}
void obs_frontend_push_ui_translation(
obs_frontend_translate_ui_cb translate) override
{
App()->PushUITranslation(translate);
}
void obs_frontend_pop_ui_translation(void) override
{
App()->PopUITranslation();
}
void on_load(obs_data_t *settings) override
{
for (auto cb : saveCallbacks)
cb.callback(settings, false, cb.private_data);
}
void on_save(obs_data_t *settings) override
{
for (auto cb : saveCallbacks)
cb.callback(settings, true, cb.private_data);
}
void on_event(enum obs_frontend_event event) override
{
if (main->disableSaving)
return;
for (auto cb : callbacks)
cb.callback(event, cb.private_data);
}
};
obs_frontend_callbacks *InitializeAPIInterface(OBSBasic *main)
{
obs_frontend_callbacks *api = new OBSStudioAPI(main);
obs_frontend_set_callbacks_internal(api);
return api;
}