Add/Move helpers to register OBS websocket vendor requests

Adds websocket vendor requests to start and stop the plugin.
Adds vendor events to indicate when the plugin is started or stopped.

These are ntended to be used for the StreamDeck plugin support.
This commit is contained in:
WarmUpTill
2024-09-01 22:15:25 +02:00
committed by WarmUpTill
parent 624b841ca1
commit 91912f2aa5
6 changed files with 201 additions and 69 deletions

114
lib/utils/websocket-api.cpp Normal file
View File

@@ -0,0 +1,114 @@
#include "websocket-api.hpp"
#include "log-helper.hpp"
#include "obs-websocket-api.h"
#include "plugin-state-helpers.hpp"
#include <mutex>
namespace advss {
static std::vector<std::function<void(obs_data_t *, obs_data_t *)>> callbacks;
constexpr char VendorName[] = "AdvancedSceneSwitcher";
constexpr char VendorRequestStart[] = "AdvancedSceneSwitcherStart";
constexpr char VendorRequestStop[] = "AdvancedSceneSwitcherStop";
constexpr char VendorRequestStatus[] = "IsAdvancedSceneSwitcherRunning";
obs_websocket_vendor vendor;
static void registerWebsocketVendor();
static bool setup();
static bool setupDone = setup();
bool setup()
{
AddPluginPostLoadStep(registerWebsocketVendor);
return true;
}
static void
registerWebsocketVendorRequest(const char *requestName,
obs_websocket_request_callback_function callback)
{
if (!obs_websocket_vendor_register_request(vendor, requestName,
callback, NULL)) {
blog(LOG_ERROR,
"Failed to register \"%s\" request with obs-websocket.",
requestName);
}
}
static void registerWebsocketVendor()
{
vendor = obs_websocket_register_vendor(VendorName);
if (!vendor) {
blog(LOG_ERROR,
"Vendor registration failed! (obs-websocket should have logged something if installed properly.)");
return;
}
auto api_version = obs_websocket_get_api_version();
if (api_version == 0) {
blog(LOG_ERROR,
"Unable to fetch obs-websocket plugin API version.");
return;
} else if (api_version == 1) {
blog(LOG_WARNING,
"Unsupported obs-websocket plugin API version for calling requests.");
return;
}
registerWebsocketVendorRequest(VendorRequestStart,
[](obs_data_t *, obs_data_t *, void *) {
StartPlugin();
});
registerWebsocketVendorRequest(VendorRequestStop,
[](obs_data_t *, obs_data_t *, void *) {
StopPlugin();
});
registerWebsocketVendorRequest(
VendorRequestStatus,
[](obs_data_t *, obs_data_t *response, void *) {
obs_data_set_bool(response, "isRunning",
PluginIsRunning());
});
}
const char *GetWebsocketVendorName()
{
return VendorName;
}
void RegisterWebsocketRequest(
const std::string &name,
const std::function<void(obs_data_t *, obs_data_t *)> &callback)
{
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
callbacks.emplace_back(callback);
auto handleRequest = [](obs_data *requestData, obs_data *other,
void *cbPtr) {
auto cb = *(static_cast<
std::function<void(obs_data *, obs_data *)> *>(
cbPtr));
cb(requestData, other);
};
AddPluginPostLoadStep([name, handleRequest]() {
if (!obs_websocket_vendor_register_request(vendor, name.c_str(),
handleRequest,
&callbacks.back())) {
blog(LOG_ERROR,
"Failed to register \"%s\" request with obs-websocket.",
name.c_str());
}
});
}
void SendWebsocketVendorEvent(const std::string &eventName, obs_data_t *data)
{
obs_websocket_vendor_emit_event(vendor, eventName.c_str(), data);
}
} // namespace advss