2021-11-20 01:32:22 +00:00
|
|
|
/*
|
|
|
|
obs-websocket
|
|
|
|
Copyright (C) 2016-2021 Stephane Lepin <stephane.lepin@gmail.com>
|
|
|
|
Copyright (C) 2020-2021 Kyle Manning <tt2468@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program. If not, see <https://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2021-03-08 11:56:43 +00:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtWidgets/QAction>
|
|
|
|
#include <QtWidgets/QMainWindow>
|
2021-06-22 22:51:52 +00:00
|
|
|
#include <obs-module.h>
|
|
|
|
#include <obs-data.h>
|
|
|
|
#include <obs-frontend-api.h>
|
2021-03-08 11:56:43 +00:00
|
|
|
|
|
|
|
#include "obs-websocket.h"
|
2021-04-27 02:59:50 +00:00
|
|
|
#include "Config.h"
|
2021-11-20 09:26:50 +00:00
|
|
|
#include "WebSocketApi.h"
|
2021-11-18 08:29:28 +00:00
|
|
|
#include "websocketserver/WebSocketServer.h"
|
2021-05-10 22:48:41 +00:00
|
|
|
#include "eventhandler/EventHandler.h"
|
2021-04-27 02:59:50 +00:00
|
|
|
#include "forms/SettingsDialog.h"
|
2021-03-08 11:56:43 +00:00
|
|
|
|
|
|
|
OBS_DECLARE_MODULE()
|
|
|
|
OBS_MODULE_USE_DEFAULT_LOCALE("obs-websocket", "en-US")
|
|
|
|
|
2021-04-27 02:59:50 +00:00
|
|
|
ConfigPtr _config;
|
2021-11-20 09:26:50 +00:00
|
|
|
WebSocketApiPtr _webSocketApi;
|
2021-04-27 16:18:06 +00:00
|
|
|
WebSocketServerPtr _webSocketServer;
|
2021-05-10 23:54:48 +00:00
|
|
|
EventHandlerPtr _eventHandler;
|
2021-04-27 03:55:18 +00:00
|
|
|
SettingsDialog *_settingsDialog = nullptr;
|
2021-08-27 11:19:42 +00:00
|
|
|
os_cpu_usage_info_t* _cpuUsageInfo;
|
2021-04-27 02:59:50 +00:00
|
|
|
|
2021-07-22 10:24:53 +00:00
|
|
|
void ___source_dummy_addref(obs_source_t*) {}
|
2021-11-13 01:19:57 +00:00
|
|
|
void ___scene_dummy_addref(obs_scene_t*) {};
|
2021-07-22 10:24:53 +00:00
|
|
|
void ___sceneitem_dummy_addref(obs_sceneitem_t*) {};
|
|
|
|
void ___data_dummy_addref(obs_data_t*) {};
|
|
|
|
void ___data_array_dummy_addref(obs_data_array_t*) {};
|
|
|
|
void ___output_dummy_addref(obs_output_t*) {};
|
|
|
|
void ___data_item_dummy_addref(obs_data_item_t*) {};
|
|
|
|
void ___data_item_release(obs_data_item_t* dataItem){ obs_data_item_release(&dataItem); };
|
2021-08-30 20:55:05 +00:00
|
|
|
void ___properties_dummy_addref(obs_properties_t*) {};
|
2021-07-22 10:24:53 +00:00
|
|
|
|
2021-11-20 09:26:50 +00:00
|
|
|
void WebSocketApiEventCallback(std::string vendorName, std::string eventType, obs_data_t *obsEventData);
|
|
|
|
|
2021-04-27 16:18:06 +00:00
|
|
|
bool obs_module_load(void)
|
|
|
|
{
|
2021-04-28 18:43:39 +00:00
|
|
|
blog(LOG_INFO, "[obs_module_load] you can haz websockets (Version: %s | RPC Version: %d)", OBS_WEBSOCKET_VERSION, OBS_WEBSOCKET_RPC_VERSION);
|
2021-09-04 17:47:51 +00:00
|
|
|
blog(LOG_INFO, "[obs_module_load] Qt version (compile-time): %s | Qt version (run-time): %s", QT_VERSION_STR, qVersion());
|
2021-03-08 11:56:43 +00:00
|
|
|
|
2021-05-08 08:42:06 +00:00
|
|
|
// Create the config object then load the parameters from storage
|
2021-04-27 03:55:18 +00:00
|
|
|
_config = ConfigPtr(new Config());
|
|
|
|
_config->Load();
|
|
|
|
|
2021-09-04 17:04:00 +00:00
|
|
|
// Initialize event handler before server, as the server configures the event handler.
|
|
|
|
_eventHandler = EventHandlerPtr(new EventHandler());
|
2021-04-27 16:18:06 +00:00
|
|
|
|
2021-11-20 09:26:50 +00:00
|
|
|
_webSocketApi = WebSocketApiPtr(new WebSocketApi(WebSocketApiEventCallback));
|
|
|
|
|
2021-09-04 17:04:00 +00:00
|
|
|
_webSocketServer = WebSocketServerPtr(new WebSocketServer());
|
2021-05-10 23:54:48 +00:00
|
|
|
|
2021-04-27 03:55:18 +00:00
|
|
|
obs_frontend_push_ui_translation(obs_module_get_string);
|
2021-08-27 08:20:13 +00:00
|
|
|
QMainWindow* mainWindow = reinterpret_cast<QMainWindow*>(obs_frontend_get_main_window());
|
2021-04-27 03:55:18 +00:00
|
|
|
_settingsDialog = new SettingsDialog(mainWindow);
|
|
|
|
obs_frontend_pop_ui_translation();
|
|
|
|
|
|
|
|
const char* menuActionText = obs_module_text("OBSWebSocket.Settings.DialogTitle");
|
|
|
|
QAction* menuAction = (QAction*)obs_frontend_add_tools_menu_qaction(menuActionText);
|
|
|
|
QObject::connect(menuAction, &QAction::triggered, [] { _settingsDialog->ToggleShowHide(); });
|
|
|
|
|
2021-08-27 11:19:42 +00:00
|
|
|
_cpuUsageInfo = os_cpu_usage_info_start();
|
2021-03-08 11:56:43 +00:00
|
|
|
|
2021-08-27 11:19:42 +00:00
|
|
|
// Loading finished
|
|
|
|
blog(LOG_INFO, "[obs_module_load] Module loaded.");
|
|
|
|
|
2021-03-08 11:56:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-27 16:18:06 +00:00
|
|
|
void obs_module_unload()
|
|
|
|
{
|
2021-04-28 18:43:39 +00:00
|
|
|
blog(LOG_INFO, "[obs_module_unload] Shutting down...");
|
2021-04-28 17:27:32 +00:00
|
|
|
|
2021-04-27 22:25:11 +00:00
|
|
|
if (_webSocketServer->IsListening()) {
|
2021-04-28 18:43:39 +00:00
|
|
|
blog(LOG_INFO, "[obs_module_unload] WebSocket server is running. Stopping...");
|
2021-04-27 22:25:11 +00:00
|
|
|
_webSocketServer->Stop();
|
|
|
|
}
|
2021-05-10 23:54:48 +00:00
|
|
|
_webSocketServer.reset();
|
|
|
|
|
|
|
|
_eventHandler.reset();
|
2021-04-27 22:25:11 +00:00
|
|
|
|
2021-11-20 09:26:50 +00:00
|
|
|
_webSocketApi.reset();
|
|
|
|
|
2021-05-10 23:54:48 +00:00
|
|
|
_config->Save();
|
2021-04-27 03:55:18 +00:00
|
|
|
_config.reset();
|
2021-05-10 23:54:48 +00:00
|
|
|
|
2021-08-27 11:19:42 +00:00
|
|
|
os_cpu_usage_info_destroy(_cpuUsageInfo);
|
|
|
|
|
2021-04-28 18:43:39 +00:00
|
|
|
blog(LOG_INFO, "[obs_module_unload] Finished shutting down.");
|
2021-04-27 02:59:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 16:18:06 +00:00
|
|
|
ConfigPtr GetConfig()
|
|
|
|
{
|
2021-04-27 02:59:50 +00:00
|
|
|
return _config;
|
2021-04-27 16:18:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 09:26:50 +00:00
|
|
|
WebSocketApiPtr GetWebSocketApi()
|
|
|
|
{
|
|
|
|
return _webSocketApi;
|
|
|
|
}
|
|
|
|
|
2021-04-27 16:18:06 +00:00
|
|
|
WebSocketServerPtr GetWebSocketServer()
|
|
|
|
{
|
|
|
|
return _webSocketServer;
|
2021-04-27 21:52:48 +00:00
|
|
|
}
|
2021-05-10 23:54:48 +00:00
|
|
|
|
|
|
|
EventHandlerPtr GetEventHandler()
|
|
|
|
{
|
|
|
|
return _eventHandler;
|
|
|
|
}
|
2021-08-27 11:19:42 +00:00
|
|
|
|
|
|
|
os_cpu_usage_info_t* GetCpuUsageInfo()
|
|
|
|
{
|
|
|
|
return _cpuUsageInfo;
|
|
|
|
}
|
2021-10-01 02:14:17 +00:00
|
|
|
|
2021-11-20 09:26:50 +00:00
|
|
|
bool IsDebugEnabled()
|
2021-10-01 02:14:17 +00:00
|
|
|
{
|
|
|
|
return !_config || _config->DebugEnabled;
|
2021-11-20 09:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketApiEventCallback(std::string vendorName, std::string eventType, obs_data_t *obsEventData)
|
|
|
|
{
|
|
|
|
json eventData = Utils::Json::ObsDataToJson(obsEventData);
|
|
|
|
|
|
|
|
json broadcastEventData;
|
|
|
|
broadcastEventData["vendorName"] = vendorName;
|
|
|
|
broadcastEventData["eventType"] = eventType;
|
|
|
|
broadcastEventData["eventData"] = eventData;
|
|
|
|
|
|
|
|
_webSocketServer->BroadcastEvent(EventSubscription::ExternalPlugins, "ExternalPluginEvent", broadcastEventData);
|
|
|
|
}
|