-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
603 lines (525 loc) · 21.7 KB
/
Copy pathplugin.cpp
File metadata and controls
603 lines (525 loc) · 21.7 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
* StreamWizard OBS Stats — headless plugin that broadcasts OBS performance and
* streaming stats over the existing obs-websocket server as vendor events.
*
* It emits two vendor events on a fixed interval:
* - "SourceStats" : a per-scene/source/filter load tree (CPU/GPU/tick/render).
* - "OutputStats" : instance health (streaming bitrate/drops, recording,
* render/encoding lag, CPU/memory, session/encoder state).
*
* There is no UI/dock. Broadcasting is on by default but can be turned off
* (which also stops the per-frame source profiler entirely) via a small JSON
* config file or live over obs-websocket vendor requests. See WEBSOCKET.md for
* the message schema and SETTINGS.md for the configuration options.
*/
#include <obs-module.h>
#include <obs-frontend-api.h>
#include <obs-websocket-api.h>
#include <util/platform.h>
#include "util/source-profiler.h"
#include "version.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <mutex>
#include <thread>
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("streamwizard-obs-stats", "en-US")
#define VENDOR_NAME "streamwizard-stats"
#define LOG_PREFIX "[StreamWizard Stats] "
/* How often stats are emitted, in milliseconds. Hardcoded for now. */
static const int BROADCAST_INTERVAL_MS = 1000;
/* obs-websocket vendor handle; null when obs-websocket is not present. */
static obs_websocket_vendor vendor = nullptr;
/* Runtime settings (loaded from config.json, overridable via vendor requests).
* `stats_enabled` gates all work AND the per-frame source profiler — turning it
* off makes the plugin truly inert (and stops the profiler's memory use).
* `gpu_profiling` separately gates GPU timing. It defaults OFF: OBS's per-source
* GPU timers only produce data on the Direct3D backend (on OpenGL the disjoint/
* frequency path is a no-op, so results are never read and gpuPercentage is
* always 0), yet the continuous GL_TIMESTAMP queries leak memory in the driver
* under headless EGL (~0.5 MB/s). So on Linux/cloud it is pure cost for no data.
* Enable it only on Windows/D3D where GPU load is real. */
static std::atomic<bool> stats_enabled{true};
static std::atomic<bool> gpu_profiling{false};
static std::atomic<bool> profiler_running{false};
/* Ticker thread: never touches OBS directly, only schedules the work onto the
* OBS UI thread, so joining it is always immediate (no deadlock at shutdown). */
static std::thread ticker;
static std::atomic<bool> running{false};
static std::atomic<bool> job_in_flight{false};
static std::mutex cv_mutex;
static std::condition_variable cv;
/* The following are only ever touched on the OBS UI thread (inside the job), so
* they need no locking. */
static uint64_t last_stream_bytes = 0;
static uint64_t last_stream_time = 0;
static os_cpu_usage_info_t *cpu_info = nullptr;
static inline double ns_to_ms(uint64_t ns)
{
return (double)ns / 1000000.0;
}
/* ===================== Settings / profiler control ===================== */
/* Apply the current enabled/gpu state to the libobs source profiler. Safe to
* call from any thread (libobs applies the change on the next frame). Idempotent
* — calling repeatedly with the same state is harmless. */
static void apply_profiler_state()
{
const bool en = stats_enabled.load();
source_profiler_enable(en);
#ifndef __APPLE__
source_profiler_gpu_enable(en && gpu_profiling.load());
#endif
profiler_running.store(en);
}
/* Read config.json from the module's plugin_config dir. Missing file / missing
* keys keep the defaults (enabled). */
static void load_settings()
{
char *path = obs_module_config_path("config.json");
if (!path)
return;
obs_data_t *cfg = obs_data_create_from_json_file(path);
bfree(path);
if (!cfg)
return;
obs_data_set_default_bool(cfg, "enabled", true);
obs_data_set_default_bool(cfg, "gpuProfiling", false);
stats_enabled.store(obs_data_get_bool(cfg, "enabled"));
gpu_profiling.store(obs_data_get_bool(cfg, "gpuProfiling"));
obs_data_release(cfg);
}
/* Persist the current settings to config.json so they survive a restart. */
static void save_settings()
{
char *dir = obs_module_config_path("");
if (dir) {
os_mkdirs(dir);
bfree(dir);
}
char *path = obs_module_config_path("config.json");
if (!path)
return;
obs_data_t *cfg = obs_data_create();
obs_data_set_bool(cfg, "enabled", stats_enabled.load());
obs_data_set_bool(cfg, "gpuProfiling", gpu_profiling.load());
obs_data_save_json(cfg, path);
obs_data_release(cfg);
bfree(path);
}
/* Stable, locale-independent category key for a source. */
static const char *category_key(obs_source_t *source)
{
switch (obs_source_get_type(source)) {
case OBS_SOURCE_TYPE_INPUT:
return "source";
case OBS_SOURCE_TYPE_FILTER:
return "filter";
case OBS_SOURCE_TYPE_TRANSITION:
return "transition";
case OBS_SOURCE_TYPE_SCENE:
return obs_source_is_group(source) ? "group" : "scene";
default:
return "unknown";
}
}
/* ===================== SourceStats (load tree) ===================== */
/* Write the profiler timing/load fields for one source into `d`. Mirrors the
* dock's per-node math. Filters subtract their target's render time so the
* number reflects the filter's own cost, not everything downstream. */
static void fill_perf(obs_source_t *source, obs_data_t *d, bool is_filter, bool async)
{
profiler_result_t perf;
memset(&perf, 0, sizeof(perf));
source_profiler_fill_result(source, &perf);
if (is_filter && (obs_source_get_output_flags(source) & OBS_SOURCE_ASYNC_VIDEO) != OBS_SOURCE_ASYNC_VIDEO) {
obs_source_t *target = obs_filter_get_target(source);
while (target && !obs_source_enabled(target))
target = obs_filter_get_target(target);
if (target) {
profiler_result_t diff;
memset(&diff, 0, sizeof(diff));
source_profiler_fill_result(target, &diff);
if (perf.render_avg >= diff.render_avg)
perf.render_avg -= diff.render_avg;
if (perf.render_max >= diff.render_max)
perf.render_max -= diff.render_max;
if (perf.render_gpu_avg >= diff.render_gpu_avg)
perf.render_gpu_avg -= diff.render_gpu_avg;
if (perf.render_gpu_max >= diff.render_gpu_max)
perf.render_gpu_max -= diff.render_gpu_max;
if (perf.render_sum >= diff.render_sum)
perf.render_sum -= diff.render_sum;
if (perf.render_gpu_sum >= diff.render_gpu_sum)
perf.render_gpu_sum -= diff.render_gpu_sum;
}
}
const double frame_ns = (double)obs_get_frame_interval_ns();
obs_data_set_double(d, "tickAvg", ns_to_ms(perf.tick_avg));
obs_data_set_double(d, "tickMax", ns_to_ms(perf.tick_max));
obs_data_set_double(d, "renderAvg", ns_to_ms(perf.render_avg));
obs_data_set_double(d, "renderMax", ns_to_ms(perf.render_max));
obs_data_set_double(d, "renderTotal", ns_to_ms(perf.render_sum));
obs_data_set_double(d, "renderGpuAvg", ns_to_ms(perf.render_gpu_avg));
obs_data_set_double(d, "renderGpuMax", ns_to_ms(perf.render_gpu_max));
obs_data_set_double(d, "renderGpuTotal", ns_to_ms(perf.render_gpu_sum));
obs_data_set_double(d, "total", ns_to_ms(perf.tick_avg + perf.render_sum + perf.render_gpu_sum));
if (frame_ns > 0.0) {
obs_data_set_double(d, "cpuPercentage", (double)(perf.render_sum + perf.tick_avg) / frame_ns * 100.0);
obs_data_set_double(d, "gpuPercentage", (double)perf.render_gpu_sum / frame_ns * 100.0);
obs_data_set_double(d, "totalPercentage",
(double)(perf.tick_avg + perf.render_sum + perf.render_gpu_sum) / frame_ns * 100.0);
}
if (async) {
obs_data_set_double(d, "asyncInputFps", perf.async_input);
obs_data_set_double(d, "asyncRenderedFps", perf.async_rendered);
obs_data_set_double(d, "asyncInputBest", ns_to_ms(perf.async_input_best));
obs_data_set_double(d, "asyncInputWorst", ns_to_ms(perf.async_input_worst));
obs_data_set_double(d, "asyncRenderedBest", ns_to_ms(perf.async_rendered_best));
obs_data_set_double(d, "asyncRenderedWorst", ns_to_ms(perf.async_rendered_worst));
}
}
static obs_data_t *build_source_node(obs_source_t *source);
/* obs_source_enum_filters callback: append a filter node to the children array. */
static void enum_filter_cb(obs_source_t *, obs_source_t *child, void *param)
{
if (obs_source_get_type(child) != OBS_SOURCE_TYPE_FILTER)
return;
auto children = (obs_data_array_t *)param;
obs_data_t *node = build_source_node(child);
obs_data_array_push_back(children, node);
obs_data_release(node);
}
/* obs_scene_enum_items callback: append a scene-item node, carrying the item's
* own visibility (which the bare source can't tell us). */
static bool enum_item_cb(obs_scene_t *, obs_sceneitem_t *item, void *param)
{
auto children = (obs_data_array_t *)param;
obs_source_t *source = obs_sceneitem_get_source(item);
if (!source)
return true;
obs_data_t *node = build_source_node(source);
obs_data_set_bool(node, "enabled", obs_sceneitem_visible(item));
obs_data_array_push_back(children, node);
obs_data_release(node);
return true;
}
/* Serialize one source (recursively: scene/group items + filters) into a node. */
static obs_data_t *build_source_node(obs_source_t *source)
{
obs_data_t *d = obs_data_create();
const bool is_filter = obs_source_get_type(source) == OBS_SOURCE_TYPE_FILTER;
const bool async =
!is_filter && ((obs_source_get_output_flags(source) & OBS_SOURCE_ASYNC_VIDEO) == OBS_SOURCE_ASYNC_VIDEO);
const char *unversioned = obs_source_get_unversioned_id(source);
obs_data_set_string(d, "name", obs_source_get_name(source));
obs_data_set_string(d, "category", category_key(source));
obs_data_set_string(d, "displayName", obs_source_get_display_name(unversioned));
if (obs_source_get_type(source) == OBS_SOURCE_TYPE_INPUT)
obs_data_set_string(d, "kindId", unversioned);
const char *uuid = obs_source_get_uuid(source);
if (uuid)
obs_data_set_string(d, "uuid", uuid);
obs_data_set_int(d, "width", obs_source_get_width(source));
obs_data_set_int(d, "height", obs_source_get_height(source));
obs_data_set_bool(d, "active", obs_source_active(source));
obs_data_set_bool(d, "rendered", obs_source_showing(source));
obs_data_set_bool(d, "enabled", is_filter ? obs_source_enabled(source) : true);
obs_data_set_bool(d, "async", async);
obs_data_set_bool(d, "filter", is_filter);
obs_data_set_bool(d, "private", obs_obj_is_private(source));
fill_perf(source, d, is_filter, async);
obs_data_array_t *children = obs_data_array_create();
/* Scene or group: recurse into its items. */
obs_scene_t *scene = obs_source_is_group(source) ? obs_group_from_source(source) : obs_scene_from_source(source);
if (scene)
obs_scene_enum_items(scene, enum_item_cb, children);
/* Any source: recurse into its filters. */
if (obs_source_filter_count(source) > 0)
obs_source_enum_filters(source, enum_filter_cb, children);
obs_data_set_int(d, "childCount", (long long)obs_data_array_count(children));
obs_data_set_array(d, "children", children);
obs_data_array_release(children);
return d;
}
/* obs_enum_scenes callback: each top-level scene becomes a root node. */
static bool enum_scene_cb(void *param, obs_source_t *source)
{
auto sources = (obs_data_array_t *)param;
obs_data_t *node = build_source_node(source);
obs_data_array_push_back(sources, node);
obs_data_release(node);
return true;
}
static void emit_source_stats()
{
obs_data_t *payload = obs_data_create();
obs_data_array_t *sources = obs_data_array_create();
obs_enum_scenes(enum_scene_cb, sources);
obs_data_set_array(payload, "sources", sources);
obs_data_set_double(payload, "frameTime", ns_to_ms(obs_get_frame_interval_ns()));
obs_websocket_vendor_emit_event(vendor, "SourceStats", payload);
obs_data_array_release(sources);
obs_data_release(payload);
}
/* ===================== OutputStats (instance health) ===================== */
/* Build an encoder sub-object. `encoder` is a non-incremented reference — do
* not release it. */
static obs_data_t *encoder_data(obs_encoder_t *encoder)
{
obs_data_t *e = obs_data_create();
obs_data_set_string(e, "id", obs_encoder_get_id(encoder));
obs_data_set_string(e, "codec", obs_encoder_get_codec(encoder));
if (obs_encoder_get_type(encoder) == OBS_ENCODER_VIDEO) {
obs_data_set_int(e, "width", obs_encoder_get_width(encoder));
obs_data_set_int(e, "height", obs_encoder_get_height(encoder));
}
obs_data_t *settings = obs_encoder_get_settings(encoder);
if (settings) {
obs_data_set_int(e, "bitrate", obs_data_get_int(settings, "bitrate"));
obs_data_release(settings);
}
return e;
}
static void emit_output_stats()
{
obs_data_t *payload = obs_data_create();
obs_output_t *stream = obs_frontend_get_streaming_output();
obs_output_t *record = obs_frontend_get_recording_output();
/* ---- streaming ---- */
obs_data_t *streaming = obs_data_create();
obs_data_set_bool(streaming, "active", stream && obs_output_active(stream));
if (stream) {
obs_data_set_bool(streaming, "reconnecting", obs_output_reconnecting(stream));
uint64_t bytes = obs_output_get_total_bytes(stream);
uint64_t now = os_gettime_ns();
double kbps = 0.0;
if (last_stream_time && now > last_stream_time && bytes >= last_stream_bytes) {
double bits = (double)(bytes - last_stream_bytes) * 8.0;
double secs = (double)(now - last_stream_time) / 1000000000.0;
if (secs > 0.0)
kbps = bits / secs / 1000.0;
}
last_stream_bytes = bytes;
last_stream_time = now;
int dropped = obs_output_get_frames_dropped(stream);
int total = obs_output_get_total_frames(stream);
obs_data_set_double(streaming, "kbps", kbps);
obs_data_set_int(streaming, "totalBytes", (long long)bytes);
obs_data_set_int(streaming, "droppedFrames", dropped);
obs_data_set_int(streaming, "totalFrames", total);
obs_data_set_double(streaming, "dropPercent", total > 0 ? (double)dropped / total * 100.0 : 0.0);
obs_data_set_double(streaming, "congestion", obs_output_get_congestion(stream));
obs_data_set_int(streaming, "connectTimeMs", obs_output_get_connect_time_ms(stream));
/* Encoding lag (frames skipped because the encoder couldn't keep up). */
video_t *video = obs_output_video(stream);
if (video) {
uint32_t skipped = video_output_get_skipped_frames(video);
uint32_t vtotal = video_output_get_total_frames(video);
obs_data_set_int(streaming, "encodeSkippedFrames", skipped);
obs_data_set_int(streaming, "encodeTotalFrames", vtotal);
obs_data_set_double(streaming, "encodeLagPercent",
vtotal > 0 ? (double)skipped / vtotal * 100.0 : 0.0);
}
obs_encoder_t *venc = obs_output_get_video_encoder(stream);
if (venc) {
obs_data_t *e = encoder_data(venc);
obs_data_set_obj(streaming, "videoEncoder", e);
obs_data_release(e);
}
obs_encoder_t *aenc = obs_output_get_audio_encoder(stream, 0);
if (aenc) {
obs_data_t *e = encoder_data(aenc);
obs_data_set_obj(streaming, "audioEncoder", e);
obs_data_release(e);
}
} else {
/* No output handle yet: reset the bitrate baseline so the first
* sample after going live isn't a huge bogus delta. */
last_stream_bytes = 0;
last_stream_time = 0;
}
obs_data_set_obj(payload, "streaming", streaming);
obs_data_release(streaming);
/* ---- recording ---- */
obs_data_t *recording = obs_data_create();
obs_data_set_bool(recording, "active", obs_frontend_recording_active());
if (record)
obs_data_set_int(recording, "totalBytes", (long long)obs_output_get_total_bytes(record));
char *record_path = obs_frontend_get_current_record_output_path();
if (record_path) {
obs_data_set_int(recording, "diskFreeBytes", (long long)os_get_free_disk_space(record_path));
bfree(record_path);
}
obs_data_set_obj(payload, "recording", recording);
obs_data_release(recording);
/* ---- render performance ---- */
obs_data_t *render = obs_data_create();
uint64_t frame_ns = obs_get_frame_interval_ns();
uint32_t rtotal = obs_get_total_frames();
uint32_t rlagged = obs_get_lagged_frames();
obs_data_set_double(render, "activeFps", obs_get_active_fps());
obs_data_set_double(render, "targetFps", frame_ns > 0 ? 1000000000.0 / (double)frame_ns : 0.0);
obs_data_set_double(render, "avgFrameRenderMs", ns_to_ms(obs_get_average_frame_time_ns()));
obs_data_set_int(render, "totalFrames", rtotal);
obs_data_set_int(render, "laggedFrames", rlagged);
obs_data_set_double(render, "renderLagPercent", rtotal > 0 ? (double)rlagged / rtotal * 100.0 : 0.0);
obs_data_set_obj(payload, "render", render);
obs_data_release(render);
/* ---- system ---- */
obs_data_t *system = obs_data_create();
if (cpu_info)
obs_data_set_double(system, "cpuPercent", os_cpu_usage_info_query(cpu_info));
obs_data_set_int(system, "memoryRssBytes", (long long)os_get_proc_resident_size());
obs_data_set_int(system, "sysTotalBytes", (long long)os_get_sys_total_size());
obs_data_set_int(system, "sysFreeBytes", (long long)os_get_sys_free_size());
obs_data_set_obj(payload, "system", system);
obs_data_release(system);
/* ---- session ---- */
obs_data_t *session = obs_data_create();
obs_data_set_bool(session, "streaming", obs_frontend_streaming_active());
obs_data_set_bool(session, "recording", obs_frontend_recording_active());
obs_data_set_bool(session, "studioMode", obs_frontend_preview_program_mode_active());
obs_source_t *program = obs_frontend_get_current_scene();
if (program) {
obs_data_set_string(session, "programScene", obs_source_get_name(program));
obs_source_release(program);
}
obs_source_t *preview = obs_frontend_get_current_preview_scene();
if (preview) {
obs_data_set_string(session, "previewScene", obs_source_get_name(preview));
obs_source_release(preview);
}
obs_source_t *transition = obs_frontend_get_current_transition();
if (transition) {
obs_data_set_string(session, "transition", obs_source_get_name(transition));
obs_source_release(transition);
}
obs_data_set_obj(payload, "session", session);
obs_data_release(session);
obs_websocket_vendor_emit_event(vendor, "OutputStats", payload);
obs_data_release(payload);
if (stream)
obs_output_release(stream);
if (record)
obs_output_release(record);
}
/* ===================== Scheduling ===================== */
/* Runs on the OBS UI thread (scheduled via obs_queue_task). */
static void emit_all_stats(void *)
{
if (running.load() && vendor && stats_enabled.load()) {
emit_source_stats();
emit_output_stats();
}
job_in_flight.store(false);
}
static void ticker_loop()
{
while (running.load()) {
{
std::unique_lock<std::mutex> lk(cv_mutex);
cv.wait_for(lk, std::chrono::milliseconds(BROADCAST_INTERVAL_MS),
[] { return !running.load(); });
}
if (!running.load())
break;
/* When broadcasting is off, don't even wake the UI thread. */
if (!stats_enabled.load())
continue;
/* Skip this tick if the previous job hasn't been drained yet, so
* a busy UI thread can't make jobs pile up. */
bool expected = false;
if (job_in_flight.compare_exchange_strong(expected, true))
obs_queue_task(OBS_TASK_UI, emit_all_stats, nullptr, false);
}
}
/* ===================== Vendor requests (live settings) ===================== */
/* CallVendorRequest "GetStatus" → current settings + version. */
static void req_get_status(obs_data_t *, obs_data_t *res, void *)
{
obs_data_set_bool(res, "enabled", stats_enabled.load());
obs_data_set_bool(res, "gpuProfiling", gpu_profiling.load());
obs_data_set_int(res, "intervalMs", BROADCAST_INTERVAL_MS);
obs_data_set_string(res, "version", PROJECT_VERSION);
}
/* CallVendorRequest "SetEnabled" with requestData { enabled?: bool,
* gpuProfiling?: bool } → toggle broadcasting / GPU profiling, persist, and
* apply to the profiler. Returns the resulting state. */
static void req_set_enabled(obs_data_t *req, obs_data_t *res, void *)
{
bool changed = false;
if (req && obs_data_has_user_value(req, "enabled")) {
stats_enabled.store(obs_data_get_bool(req, "enabled"));
changed = true;
}
if (req && obs_data_has_user_value(req, "gpuProfiling")) {
gpu_profiling.store(obs_data_get_bool(req, "gpuProfiling"));
changed = true;
}
if (changed) {
apply_profiler_state();
save_settings();
blog(LOG_INFO, LOG_PREFIX "settings changed via websocket: enabled=%d gpuProfiling=%d",
(int)stats_enabled.load(), (int)gpu_profiling.load());
}
obs_data_set_bool(res, "enabled", stats_enabled.load());
obs_data_set_bool(res, "gpuProfiling", gpu_profiling.load());
}
/* ===================== Module lifecycle ===================== */
bool obs_module_load(void)
{
blog(LOG_INFO, LOG_PREFIX "loaded version %s", PROJECT_VERSION);
return true;
}
void obs_module_post_load(void)
{
/* Runs after all modules load, so obs-websocket is available. */
load_settings();
vendor = obs_websocket_register_vendor(VENDOR_NAME);
if (!vendor) {
blog(LOG_WARNING, LOG_PREFIX "obs-websocket not found; stats broadcast disabled");
return;
}
blog(LOG_INFO, LOG_PREFIX "registered obs-websocket vendor '%s' (enabled=%d gpuProfiling=%d)", VENDOR_NAME,
(int)stats_enabled.load(), (int)gpu_profiling.load());
obs_websocket_vendor_register_request(vendor, "GetStatus", req_get_status, nullptr);
obs_websocket_vendor_register_request(vendor, "SetEnabled", req_set_enabled, nullptr);
/* Only spin up the profiler if broadcasting is on; otherwise the plugin
* stays fully inert until enabled via vendor request. */
apply_profiler_state();
cpu_info = os_cpu_usage_info_start();
running.store(true);
ticker = std::thread(ticker_loop);
}
void obs_module_unload(void)
{
running.store(false);
cv.notify_all();
if (ticker.joinable())
ticker.join();
if (!vendor)
return;
obs_websocket_vendor_unregister_request(vendor, "GetStatus");
obs_websocket_vendor_unregister_request(vendor, "SetEnabled");
if (profiler_running.load()) {
#ifndef __APPLE__
source_profiler_gpu_enable(false);
#endif
source_profiler_enable(false);
profiler_running.store(false);
}
if (cpu_info) {
os_cpu_usage_info_destroy(cpu_info);
cpu_info = nullptr;
}
}
MODULE_EXPORT const char *obs_module_description(void)
{
return "Broadcasts OBS source/scene/filter load and output stats over obs-websocket.";
}
MODULE_EXPORT const char *obs_module_name(void)
{
return "StreamWizard OBS Stats";
}