Skip to content

Commit 179e197

Browse files
committed
base: Many random fixups preparing for WebSocketApi event callbacks
1 parent 5fc39ef commit 179e197

File tree

7 files changed

+65
-46
lines changed

7 files changed

+65
-46
lines changed

src/WebSocketApi.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>
1818

1919
#include "WebSocketApi.h"
2020
#include "requesthandler/RequestHandler.h"
21-
#include "obs-websocket.h"
2221
#include "utils/Json.h"
2322

2423
#define RETURN_STATUS(status) \
@@ -79,9 +78,12 @@ WebSocketApi::~WebSocketApi()
7978
blog_debug("[WebSocketApi::~WebSocketApi] Finished.");
8079
}
8180

82-
void WebSocketApi::SetEventCallback(EventCallback cb)
81+
void WebSocketApi::BroadcastEvent(uint64_t requiredIntent, const std::string &eventType, const json &eventData, uint8_t rpcVersion)
8382
{
84-
_eventCallback = cb;
83+
UNUSED_PARAMETER(requiredIntent);
84+
UNUSED_PARAMETER(eventType);
85+
UNUSED_PARAMETER(eventData);
86+
UNUSED_PARAMETER(rpcVersion);
8587
}
8688

8789
enum WebSocketApi::RequestReturnCode WebSocketApi::PerformVendorRequest(std::string vendorName, std::string requestType,
@@ -289,10 +291,10 @@ void WebSocketApi::vendor_event_emit_cb(void *priv_data, calldata_t *cd)
289291

290292
auto eventData = static_cast<obs_data_t *>(voidEventData);
291293

292-
if (!c->_eventCallback)
294+
if (!c->_vendorEventCallback)
293295
RETURN_FAILURE();
294296

295-
c->_eventCallback(v->_name, eventType, eventData);
297+
c->_vendorEventCallback(v->_name, eventType, eventData);
296298

297299
RETURN_SUCCESS();
298300
}

src/WebSocketApi.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ with this program. If not, see <https://www.gnu.org/licenses/>
2323
#include <map>
2424
#include <mutex>
2525
#include <shared_mutex>
26+
#include <atomic>
2627
#include <obs.h>
2728
#include <obs-websocket-api.h>
2829

30+
#include "utils/Json.h"
31+
#include "plugin-macros.generated.h"
32+
2933
class WebSocketApi {
3034
public:
3135
enum RequestReturnCode {
@@ -34,8 +38,6 @@ class WebSocketApi {
3438
NoVendorRequest,
3539
};
3640

37-
typedef std::function<void(std::string, std::string, obs_data_t *)> EventCallback;
38-
3941
struct Vendor {
4042
std::shared_mutex _mutex;
4143
std::string _name;
@@ -44,12 +46,17 @@ class WebSocketApi {
4446

4547
WebSocketApi();
4648
~WebSocketApi();
47-
48-
void SetEventCallback(EventCallback cb);
49-
49+
void BroadcastEvent(uint64_t requiredIntent, const std::string &eventType, const json &eventData = nullptr,
50+
uint8_t rpcVersion = 0);
51+
void SetObsReady(bool ready) { _obsReady = ready; }
5052
enum RequestReturnCode PerformVendorRequest(std::string vendorName, std::string requestName, obs_data_t *requestData,
5153
obs_data_t *responseData);
5254

55+
// Callback for when a vendor emits an event
56+
typedef std::function<void(std::string, std::string, obs_data_t *)> VendorEventCallback;
57+
inline void SetVendorEventCallback(VendorEventCallback cb) { _vendorEventCallback = cb; }
58+
59+
private:
5360
static void get_ph_cb(void *priv_data, calldata_t *cd);
5461
static void get_api_version(void *, calldata_t *cd);
5562
static void call_request(void *, calldata_t *cd);
@@ -58,9 +65,11 @@ class WebSocketApi {
5865
static void vendor_request_unregister_cb(void *priv_data, calldata_t *cd);
5966
static void vendor_event_emit_cb(void *priv_data, calldata_t *cd);
6067

61-
private:
6268
std::shared_mutex _mutex;
63-
EventCallback _eventCallback;
6469
proc_handler_t *_procHandler;
6570
std::map<std::string, Vendor *> _vendors;
71+
72+
std::atomic<bool> _obsReady = false;
73+
74+
VendorEventCallback _vendorEventCallback;
6675
};

src/eventhandler/EventHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ void EventHandler::ProcessSubscriptionChange(bool type, uint64_t eventSubscripti
110110
// Function required in order to use default arguments
111111
void EventHandler::BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion)
112112
{
113-
if (!_broadcastCallback)
113+
if (!_eventCallback)
114114
return;
115115

116-
_broadcastCallback(requiredIntent, eventType, eventData, rpcVersion);
116+
_eventCallback(requiredIntent, eventType, eventData, rpcVersion);
117117
}
118118

119119
// Connect source signals for Inputs, Scenes, and Transitions. Filters are automatically connected.

src/eventhandler/EventHandler.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ class EventHandler {
3434
EventHandler();
3535
~EventHandler();
3636

37+
void ProcessSubscriptionChange(bool type, uint64_t eventSubscriptions);
38+
39+
// Callback when an event fires
3740
typedef std::function<void(uint64_t, std::string, json, uint8_t)>
38-
BroadcastCallback; // uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion
39-
inline void SetBroadcastCallback(BroadcastCallback cb) { _broadcastCallback = cb; }
41+
EventCallback; // uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion
42+
inline void SetEventCallback(EventCallback cb) { _eventCallback = cb; }
4043

44+
// Callback when OBS becomes ready or non-ready
4145
typedef std::function<void(bool)> ObsReadyCallback; // bool ready
4246
inline void SetObsReadyCallback(ObsReadyCallback cb) { _obsReadyCallback = cb; }
4347

44-
void ProcessSubscriptionChange(bool type, uint64_t eventSubscriptions);
45-
4648
private:
47-
BroadcastCallback _broadcastCallback;
49+
EventCallback _eventCallback;
4850
ObsReadyCallback _obsReadyCallback;
4951

5052
std::atomic<bool> _obsReady = false;

src/obs-websocket.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ WebSocketApiPtr _webSocketApi;
4848
WebSocketServerPtr _webSocketServer;
4949
SettingsDialog *_settingsDialog = nullptr;
5050

51-
void WebSocketApiEventCallback(std::string vendorName, std::string eventType, obs_data_t *obsEventData);
51+
void OnWebSocketApiVendorEvent(std::string vendorName, std::string eventType, obs_data_t *obsEventData);
52+
void OnEvent(uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion);
53+
void OnObsReady(bool ready);
5254

5355
bool obs_module_load(void)
5456
{
@@ -73,19 +75,15 @@ bool obs_module_load(void)
7375

7476
// Initialize the event handler
7577
_eventHandler = std::make_shared<EventHandler>();
78+
_eventHandler->SetEventCallback(OnEvent);
79+
_eventHandler->SetObsReadyCallback(OnObsReady);
7680

7781
// Initialize the plugin/script API
7882
_webSocketApi = std::make_shared<WebSocketApi>();
79-
_webSocketApi->SetEventCallback(WebSocketApiEventCallback);
83+
_webSocketApi->SetVendorEventCallback(OnWebSocketApiVendorEvent);
8084

8185
// Initialize the WebSocket server
8286
_webSocketServer = std::make_shared<WebSocketServer>();
83-
84-
// Attach event handlers between WebSocket server and event handler
85-
_eventHandler->SetBroadcastCallback(std::bind(&WebSocketServer::BroadcastEvent, _webSocketServer.get(),
86-
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
87-
std::placeholders::_4));
88-
_eventHandler->SetObsReadyCallback(std::bind(&WebSocketServer::SetObsReady, _webSocketServer.get(), std::placeholders::_1));
8987
_webSocketServer->SetClientSubscriptionCallback(std::bind(&EventHandler::ProcessSubscriptionChange, _eventHandler.get(),
9088
std::placeholders::_1, std::placeholders::_2));
9189

@@ -131,18 +129,16 @@ void obs_module_unload(void)
131129
_webSocketServer->Stop();
132130
}
133131

134-
// Disconnect event handler from WebSocket server
135-
_eventHandler->SetObsReadyCallback(nullptr);
136-
_eventHandler->SetBroadcastCallback(nullptr);
137-
_webSocketServer->SetClientSubscriptionCallback(nullptr);
138-
139132
// Release the WebSocket server
133+
_webSocketServer->SetClientSubscriptionCallback(nullptr);
140134
_webSocketServer = nullptr;
141135

142136
// Release the plugin/script api
143137
_webSocketApi = nullptr;
144138

145139
// Release the event handler
140+
_eventHandler->SetObsReadyCallback(nullptr);
141+
_eventHandler->SetEventCallback(nullptr);
146142
_eventHandler = nullptr;
147143

148144
// Release the config manager
@@ -202,7 +198,7 @@ bool IsDebugEnabled()
202198
* @api events
203199
* @category general
204200
*/
205-
void WebSocketApiEventCallback(std::string vendorName, std::string eventType, obs_data_t *obsEventData)
201+
void OnWebSocketApiVendorEvent(std::string vendorName, std::string eventType, obs_data_t *obsEventData)
206202
{
207203
json eventData = Utils::Json::ObsDataToJson(obsEventData);
208204

@@ -214,6 +210,24 @@ void WebSocketApiEventCallback(std::string vendorName, std::string eventType, ob
214210
_webSocketServer->BroadcastEvent(EventSubscription::Vendors, "VendorEvent", broadcastEventData);
215211
}
216212

213+
// Sent from: EventHandler
214+
void OnEvent(uint64_t requiredIntent, std::string eventType, json eventData, uint8_t rpcVersion)
215+
{
216+
if (_webSocketServer)
217+
_webSocketServer->BroadcastEvent(requiredIntent, eventType, eventData, rpcVersion);
218+
if (_webSocketApi)
219+
_webSocketApi->BroadcastEvent(requiredIntent, eventType, eventData, rpcVersion);
220+
}
221+
222+
// Sent from: EventHandler
223+
void OnObsReady(bool ready)
224+
{
225+
if (_webSocketServer)
226+
_webSocketServer->SetObsReady(ready);
227+
if (_webSocketApi)
228+
_webSocketApi->SetObsReady(ready);
229+
}
230+
217231
#ifdef PLUGIN_TESTS
218232

219233
static void test_vendor_request_cb(obs_data_t *requestData, obs_data_t *responseData, void *priv_data)

src/websocketserver/WebSocketServer.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ std::vector<WebSocketServer::WebSocketSessionState> WebSocketServer::GetWebSocke
201201
return webSocketSessions;
202202
}
203203

204-
void WebSocketServer::SetObsReady(bool ready)
205-
{
206-
_obsReady = ready;
207-
}
208-
209204
bool WebSocketServer::onValidate(websocketpp::connection_hdl hdl)
210205
{
211206
auto conn = _server.get_con_from_hdl(hdl);

src/websocketserver/WebSocketServer.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ with this program. If not, see <https://www.gnu.org/licenses/>
3030
#include "rpc/WebSocketSession.h"
3131
#include "types/WebSocketCloseCode.h"
3232
#include "types/WebSocketOpCode.h"
33-
#include "../utils/Json.h"
3433
#include "../requesthandler/rpc/Request.h"
34+
#include "../utils/Json.h"
3535
#include "plugin-macros.generated.h"
3636

3737
class WebSocketServer : QObject {
@@ -57,18 +57,15 @@ class WebSocketServer : QObject {
5757
void InvalidateSession(websocketpp::connection_hdl hdl);
5858
void BroadcastEvent(uint64_t requiredIntent, const std::string &eventType, const json &eventData = nullptr,
5959
uint8_t rpcVersion = 0);
60-
void SetObsReady(bool ready);
60+
inline void SetObsReady(bool ready) { _obsReady = ready; }
61+
inline bool IsListening() { return _server.is_listening(); }
62+
std::vector<WebSocketSessionState> GetWebSocketSessions();
63+
inline QThreadPool *GetThreadPool() { return &_threadPool; }
6164

6265
// Callback for when a client subscribes or unsubscribes. `true` for sub, `false` for unsub
6366
typedef std::function<void(bool, uint64_t)> ClientSubscriptionCallback; // bool type, uint64_t eventSubscriptions
6467
inline void SetClientSubscriptionCallback(ClientSubscriptionCallback cb) { _clientSubscriptionCallback = cb; }
6568

66-
inline bool IsListening() { return _server.is_listening(); }
67-
68-
std::vector<WebSocketSessionState> GetWebSocketSessions();
69-
70-
inline QThreadPool *GetThreadPool() { return &_threadPool; }
71-
7269
signals:
7370
void ClientConnected(WebSocketSessionState state);
7471
void ClientDisconnected(WebSocketSessionState state, uint16_t closeCode);

0 commit comments

Comments
 (0)