utils: Move from explicit enum converters to nlohmann macros

This commit is contained in:
tt2468 2022-06-08 00:33:41 -07:00
parent 17292520f2
commit 22fee0312d
13 changed files with 86 additions and 189 deletions

View File

@ -108,7 +108,6 @@ target_sources(
src/utils/Json.h
src/utils/Obs.cpp
src/utils/Obs_StringHelper.cpp
src/utils/Obs_EnumHelper.cpp
src/utils/Obs_NumberHelper.cpp
src/utils/Obs_ArrayHelper.cpp
src/utils/Obs_ObjectHelper.cpp

View File

@ -373,11 +373,9 @@ void EventHandler::HandleInputAudioMonitorTypeChanged(void *param, calldata_t *d
enum obs_monitoring_type monitorType = (obs_monitoring_type)calldata_int(data, "type");
std::string monitorTypeString = Utils::Obs::StringHelper::GetInputMonitorType(monitorType);
json eventData;
eventData["inputName"] = obs_source_get_name(source);
eventData["monitorType"] = monitorTypeString;
eventData["monitorType"] = monitorType;
eventHandler->BroadcastEvent(EventSubscription::Inputs, "InputAudioMonitorTypeChanged", eventData);
}

View File

@ -53,7 +53,7 @@ void EventHandler::HandleStreamStateChanged(ObsOutputState state)
{
json eventData;
eventData["outputActive"] = GetOutputStateActive(state);
eventData["outputState"] = Utils::Obs::StringHelper::GetOutputState(state);
eventData["outputState"] = state;
BroadcastEvent(EventSubscription::Outputs, "StreamStateChanged", eventData);
}
@ -75,7 +75,7 @@ void EventHandler::HandleRecordStateChanged(ObsOutputState state)
{
json eventData;
eventData["outputActive"] = GetOutputStateActive(state);
eventData["outputState"] = Utils::Obs::StringHelper::GetOutputState(state);
eventData["outputState"] = state;
BroadcastEvent(EventSubscription::Outputs, "RecordStateChanged", eventData);
}
@ -97,7 +97,7 @@ void EventHandler::HandleReplayBufferStateChanged(ObsOutputState state)
{
json eventData;
eventData["outputActive"] = GetOutputStateActive(state);
eventData["outputState"] = Utils::Obs::StringHelper::GetOutputState(state);
eventData["outputState"] = state;
BroadcastEvent(EventSubscription::Outputs, "ReplayBufferStateChanged", eventData);
}
@ -119,7 +119,7 @@ void EventHandler::HandleVirtualcamStateChanged(ObsOutputState state)
{
json eventData;
eventData["outputActive"] = GetOutputStateActive(state);
eventData["outputState"] = Utils::Obs::StringHelper::GetOutputState(state);
eventData["outputState"] = state;
BroadcastEvent(EventSubscription::Outputs, "VirtualcamStateChanged", eventData);
}

View File

@ -177,7 +177,8 @@ void SettingsDialog::SaveFormData()
}
}
bool needsRestart = (conf->ServerEnabled != ui->enableWebSocketServerCheckBox->isChecked()) ||
bool needsRestart =
(conf->ServerEnabled != ui->enableWebSocketServerCheckBox->isChecked()) ||
(conf->ServerPort != ui->serverPortSpinBox->value()) ||
(ui->enableAuthenticationCheckBox->isChecked() && conf->ServerPassword != ui->serverPasswordLineEdit->text());

View File

@ -706,7 +706,7 @@ RequestResult RequestHandler::GetInputAudioMonitorType(const Request &request)
return RequestResult::Error(RequestStatus::InvalidResourceState, "The specified input does not support audio.");
json responseData;
responseData["monitorType"] = Utils::Obs::StringHelper::GetInputMonitorType(input);
responseData["monitorType"] = obs_source_get_monitoring_type(input);
return RequestResult::Success(responseData);
}

View File

@ -61,7 +61,8 @@ RequestResult RequestHandler::GetMediaInputStatus(const Request &request)
return RequestResult::Error(statusCode, comment);
json responseData;
responseData["mediaState"] = Utils::Obs::StringHelper::GetMediaInputState(input);
responseData["mediaState"] = obs_source_media_get_state(input);
;
if (IsMediaTimeValid(input)) {
responseData["mediaDuration"] = obs_source_media_get_duration(input);
@ -168,8 +169,7 @@ RequestResult RequestHandler::TriggerMediaInputAction(const Request &request)
if (!(input && request.ValidateString("mediaAction", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string mediaActionString = request.RequestData["mediaAction"];
auto mediaAction = Utils::Obs::EnumHelper::GetMediaInputAction(mediaActionString);
enum ObsMediaInputAction mediaAction = request.RequestData["mediaAction"];
switch (mediaAction) {
default:

View File

@ -396,9 +396,8 @@ RequestResult RequestHandler::SetSceneItemTransform(const Request &request)
if (r.Contains("boundsType")) {
if (!r.ValidateOptionalString("boundsType", statusCode, comment))
return RequestResult::Error(statusCode, comment);
std::string boundsTypeString = r.RequestData["boundsType"];
enum obs_bounds_type boundsType = Utils::Obs::EnumHelper::GetSceneItemBoundsType(boundsTypeString);
if (boundsType == OBS_BOUNDS_NONE && boundsTypeString != "OBS_BOUNDS_NONE")
enum obs_bounds_type boundsType = r.RequestData["boundsType"];
if (boundsType == OBS_BOUNDS_NONE && r.RequestData["boundsType"] != "OBS_BOUNDS_NONE")
return RequestResult::Error(RequestStatus::InvalidRequestField,
"The field boundsType has an invalid value.");
sceneItemTransform.bounds_type = boundsType;
@ -695,7 +694,7 @@ RequestResult RequestHandler::GetSceneItemBlendMode(const Request &request)
auto blendMode = obs_sceneitem_get_blending_mode(sceneItem);
json responseData;
responseData["sceneItemBlendMode"] = Utils::Obs::StringHelper::GetSceneItemBlendMode(blendMode);
responseData["sceneItemBlendMode"] = blendMode;
return RequestResult::Success(responseData);
}
@ -725,10 +724,8 @@ RequestResult RequestHandler::SetSceneItemBlendMode(const Request &request)
if (!(sceneItem && request.ValidateString("sceneItemBlendMode", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string blendModeString = request.RequestData["sceneItemBlendMode"];
auto blendMode = Utils::Obs::EnumHelper::GetSceneItemBlendMode(blendModeString);
if (blendMode == OBS_BLEND_NORMAL && blendModeString != "OBS_BLEND_NORMAL")
enum obs_blending_type blendMode = request.RequestData["sceneItemBlendMode"];
if (blendMode == OBS_BLEND_NORMAL && request.RequestData["sceneItemBlendMode"] != "OBS_BLEND_NORMAL")
return RequestResult::Error(RequestStatus::InvalidRequestField,
"The field sceneItemBlendMode has an invalid value.");

View File

@ -25,6 +25,51 @@ with this program. If not, see <https://www.gnu.org/licenses/>
using json = nlohmann::json;
NLOHMANN_JSON_SERIALIZE_ENUM(obs_source_type, {
{OBS_SOURCE_TYPE_INPUT, "OBS_SOURCE_TYPE_INPUT"},
{OBS_SOURCE_TYPE_FILTER, "OBS_SOURCE_TYPE_FILTER"},
{OBS_SOURCE_TYPE_TRANSITION, "OBS_SOURCE_TYPE_TRANSITION"},
{OBS_SOURCE_TYPE_SCENE, "OBS_SOURCE_TYPE_SCENE"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(obs_monitoring_type,
{
{OBS_MONITORING_TYPE_NONE, "OBS_MONITORING_TYPE_NONE"},
{OBS_MONITORING_TYPE_MONITOR_ONLY, "OBS_MONITORING_TYPE_MONITOR_ONLY"},
{OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT, "OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(obs_media_state, {
{OBS_MEDIA_STATE_NONE, "OBS_MEDIA_STATE_NONE"},
{OBS_MEDIA_STATE_PLAYING, "OBS_MEDIA_STATE_PLAYING"},
{OBS_MEDIA_STATE_OPENING, "OBS_MEDIA_STATE_OPENING"},
{OBS_MEDIA_STATE_BUFFERING, "OBS_MEDIA_STATE_BUFFERING"},
{OBS_MEDIA_STATE_PAUSED, "OBS_MEDIA_STATE_PAUSED"},
{OBS_MEDIA_STATE_STOPPED, "OBS_MEDIA_STATE_STOPPED"},
{OBS_MEDIA_STATE_ENDED, "OBS_MEDIA_STATE_ENDED"},
{OBS_MEDIA_STATE_ERROR, "OBS_MEDIA_STATE_ERROR"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(obs_bounds_type, {
{OBS_BOUNDS_NONE, "OBS_BOUNDS_NONE"},
{OBS_BOUNDS_STRETCH, "OBS_BOUNDS_STRETCH"},
{OBS_BOUNDS_SCALE_INNER, "OBS_BOUNDS_SCALE_INNER"},
{OBS_BOUNDS_SCALE_OUTER, "OBS_BOUNDS_SCALE_OUTER"},
{OBS_BOUNDS_SCALE_TO_WIDTH, "OBS_BOUNDS_SCALE_TO_WIDTH"},
{OBS_BOUNDS_SCALE_TO_HEIGHT, "OBS_BOUNDS_SCALE_TO_HEIGHT"},
{OBS_BOUNDS_MAX_ONLY, "OBS_BOUNDS_MAX_ONLY"},
})
NLOHMANN_JSON_SERIALIZE_ENUM(obs_blending_type, {
{OBS_BLEND_NORMAL, "OBS_BLEND_NORMAL"},
{OBS_BLEND_ADDITIVE, "OBS_BLEND_ADDITIVE"},
{OBS_BLEND_SUBTRACT, "OBS_BLEND_SUBTRACT"},
{OBS_BLEND_SCREEN, "OBS_BLEND_SCREEN"},
{OBS_BLEND_MULTIPLY, "OBS_BLEND_MULTIPLY"},
{OBS_BLEND_LIGHTEN, "OBS_BLEND_LIGHTEN"},
{OBS_BLEND_DARKEN, "OBS_BLEND_DARKEN"},
})
namespace Utils {
namespace Json {
bool JsonArrayIsValidObsArray(const json &j);

View File

@ -77,6 +77,16 @@ enum ObsOutputState {
OBS_WEBSOCKET_OUTPUT_RESUMED,
};
NLOHMANN_JSON_SERIALIZE_ENUM(ObsOutputState, {
{OBS_WEBSOCKET_OUTPUT_UNKNOWN, "OBS_WEBSOCKET_OUTPUT_UNKNOWN"},
{OBS_WEBSOCKET_OUTPUT_STARTING, "OBS_WEBSOCKET_OUTPUT_STARTING"},
{OBS_WEBSOCKET_OUTPUT_STARTED, "OBS_WEBSOCKET_OUTPUT_STARTED"},
{OBS_WEBSOCKET_OUTPUT_STOPPING, "OBS_WEBSOCKET_OUTPUT_STOPPING"},
{OBS_WEBSOCKET_OUTPUT_STOPPED, "OBS_WEBSOCKET_OUTPUT_STOPPED"},
{OBS_WEBSOCKET_OUTPUT_PAUSED, "OBS_WEBSOCKET_OUTPUT_PAUSED"},
{OBS_WEBSOCKET_OUTPUT_RESUMED, "OBS_WEBSOCKET_OUTPUT_RESUMED"},
})
enum ObsMediaInputAction {
/**
* No action.
@ -150,6 +160,17 @@ enum ObsMediaInputAction {
OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS,
};
NLOHMANN_JSON_SERIALIZE_ENUM(ObsMediaInputAction,
{
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NONE, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NONE"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PLAY, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PLAY"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_STOP, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_STOP"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NEXT, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NEXT"},
{OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS, "OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS"},
})
namespace Utils {
namespace Obs {
namespace StringHelper {
@ -158,21 +179,8 @@ std::string GetCurrentSceneCollection();
std::string GetCurrentProfile();
std::string GetCurrentProfilePath();
std::string GetCurrentRecordOutputPath();
std::string GetSourceType(obs_source_t *source);
std::string GetInputMonitorType(enum obs_monitoring_type monitorType);
std::string GetInputMonitorType(obs_source_t *input);
std::string GetMediaInputState(obs_source_t *input);
std::string GetLastReplayBufferFilePath();
std::string GetSceneItemBoundsType(enum obs_bounds_type type);
std::string GetSceneItemBlendMode(enum obs_blending_type mode);
std::string DurationToTimecode(uint64_t);
std::string GetOutputState(ObsOutputState state);
}
namespace EnumHelper {
enum obs_bounds_type GetSceneItemBoundsType(std::string boundsType);
enum ObsMediaInputAction GetMediaInputAction(std::string mediaAction);
enum obs_blending_type GetSceneItemBlendMode(std::string mode);
}
namespace NumberHelper {

View File

@ -146,7 +146,7 @@ std::vector<json> Utils::Obs::ArrayHelper::GetSceneItemList(obs_scene_t *scene,
if (!enumData->second) {
OBSSource itemSource = obs_sceneitem_get_source(sceneItem);
item["sourceName"] = obs_source_get_name(itemSource);
item["sourceType"] = StringHelper::GetSourceType(itemSource);
item["sourceType"] = obs_source_get_type(itemSource);
if (obs_source_get_type(itemSource) == OBS_SOURCE_TYPE_INPUT)
item["inputKind"] = obs_source_get_id(itemSource);
else

View File

@ -1,62 +0,0 @@
/*
obs-websocket
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/>
*/
#include "Obs.h"
#include "../plugin-macros.generated.h"
#define RET_COMPARE(str, x) \
if (str == #x) \
return x;
enum obs_bounds_type Utils::Obs::EnumHelper::GetSceneItemBoundsType(std::string boundsType)
{
RET_COMPARE(boundsType, OBS_BOUNDS_NONE)
RET_COMPARE(boundsType, OBS_BOUNDS_STRETCH)
RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_INNER)
RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_OUTER)
RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_WIDTH)
RET_COMPARE(boundsType, OBS_BOUNDS_SCALE_TO_HEIGHT)
RET_COMPARE(boundsType, OBS_BOUNDS_MAX_ONLY)
return OBS_BOUNDS_NONE;
}
enum ObsMediaInputAction Utils::Obs::EnumHelper::GetMediaInputAction(std::string mediaAction)
{
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PLAY)
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PAUSE)
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_STOP)
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_RESTART)
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NEXT)
RET_COMPARE(mediaAction, OBS_WEBSOCKET_MEDIA_INPUT_ACTION_PREVIOUS)
return OBS_WEBSOCKET_MEDIA_INPUT_ACTION_NONE;
}
enum obs_blending_type Utils::Obs::EnumHelper::GetSceneItemBlendMode(std::string mode)
{
RET_COMPARE(mode, OBS_BLEND_NORMAL)
RET_COMPARE(mode, OBS_BLEND_ADDITIVE)
RET_COMPARE(mode, OBS_BLEND_SUBTRACT)
RET_COMPARE(mode, OBS_BLEND_SCREEN)
RET_COMPARE(mode, OBS_BLEND_MULTIPLY)
RET_COMPARE(mode, OBS_BLEND_LIGHTEN)
RET_COMPARE(mode, OBS_BLEND_DARKEN)
return OBS_BLEND_NORMAL;
}

View File

@ -73,7 +73,7 @@ json Utils::Obs::ObjectHelper::GetSceneItemTransform(obs_sceneitem_t *item)
ret["alignment"] = osi.alignment;
ret["boundsType"] = StringHelper::GetSceneItemBoundsType(osi.bounds_type);
ret["boundsType"] = osi.bounds_type;
ret["boundsAlignment"] = osi.bounds_alignment;
ret["boundsWidth"] = osi.bounds.x;
ret["boundsHeight"] = osi.bounds.y;

View File

@ -72,53 +72,6 @@ std::string Utils::Obs::StringHelper::GetCurrentRecordOutputPath()
return ret;
}
std::string Utils::Obs::StringHelper::GetSourceType(obs_source_t *source)
{
obs_source_type sourceType = obs_source_get_type(source);
switch (sourceType) {
default:
CASE(OBS_SOURCE_TYPE_INPUT)
CASE(OBS_SOURCE_TYPE_FILTER)
CASE(OBS_SOURCE_TYPE_TRANSITION)
CASE(OBS_SOURCE_TYPE_SCENE)
}
}
std::string Utils::Obs::StringHelper::GetInputMonitorType(enum obs_monitoring_type monitorType)
{
switch (monitorType) {
default:
CASE(OBS_MONITORING_TYPE_NONE)
CASE(OBS_MONITORING_TYPE_MONITOR_ONLY)
CASE(OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT)
}
}
std::string Utils::Obs::StringHelper::GetInputMonitorType(obs_source_t *input)
{
obs_monitoring_type monitorType = obs_source_get_monitoring_type(input);
return GetInputMonitorType(monitorType);
}
std::string Utils::Obs::StringHelper::GetMediaInputState(obs_source_t *input)
{
obs_media_state mediaState = obs_source_media_get_state(input);
switch (mediaState) {
default:
CASE(OBS_MEDIA_STATE_NONE)
CASE(OBS_MEDIA_STATE_PLAYING)
CASE(OBS_MEDIA_STATE_OPENING)
CASE(OBS_MEDIA_STATE_BUFFERING)
CASE(OBS_MEDIA_STATE_PAUSED)
CASE(OBS_MEDIA_STATE_STOPPED)
CASE(OBS_MEDIA_STATE_ENDED)
CASE(OBS_MEDIA_STATE_ERROR)
}
}
std::string Utils::Obs::StringHelper::GetLastReplayBufferFilePath()
{
OBSOutputAutoRelease output = obs_frontend_get_replay_buffer_output();
@ -137,34 +90,6 @@ std::string Utils::Obs::StringHelper::GetLastReplayBufferFilePath()
return savedReplayPath;
}
std::string Utils::Obs::StringHelper::GetSceneItemBoundsType(enum obs_bounds_type type)
{
switch (type) {
default:
CASE(OBS_BOUNDS_NONE)
CASE(OBS_BOUNDS_STRETCH)
CASE(OBS_BOUNDS_SCALE_INNER)
CASE(OBS_BOUNDS_SCALE_OUTER)
CASE(OBS_BOUNDS_SCALE_TO_WIDTH)
CASE(OBS_BOUNDS_SCALE_TO_HEIGHT)
CASE(OBS_BOUNDS_MAX_ONLY)
}
}
std::string Utils::Obs::StringHelper::GetSceneItemBlendMode(enum obs_blending_type mode)
{
switch (mode) {
default:
CASE(OBS_BLEND_NORMAL)
CASE(OBS_BLEND_ADDITIVE)
CASE(OBS_BLEND_SUBTRACT)
CASE(OBS_BLEND_SCREEN)
CASE(OBS_BLEND_MULTIPLY)
CASE(OBS_BLEND_LIGHTEN)
CASE(OBS_BLEND_DARKEN)
}
}
std::string Utils::Obs::StringHelper::DurationToTimecode(uint64_t ms)
{
uint64_t secs = ms / 1000ULL;
@ -179,17 +104,3 @@ std::string Utils::Obs::StringHelper::DurationToTimecode(uint64_t ms)
QString::asprintf("%02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 ".%03" PRIu64, hoursPart, minutesPart, secsPart, msPart);
return formatted.toStdString();
}
std::string Utils::Obs::StringHelper::GetOutputState(ObsOutputState state)
{
switch (state) {
default:
CASE(OBS_WEBSOCKET_OUTPUT_UNKNOWN)
CASE(OBS_WEBSOCKET_OUTPUT_STARTING)
CASE(OBS_WEBSOCKET_OUTPUT_STARTED)
CASE(OBS_WEBSOCKET_OUTPUT_STOPPING)
CASE(OBS_WEBSOCKET_OUTPUT_STOPPED)
CASE(OBS_WEBSOCKET_OUTPUT_PAUSED)
CASE(OBS_WEBSOCKET_OUTPUT_RESUMED)
}
}