Requests: More requests and code cleanup

(plus very slight performance improvement)
This commit is contained in:
tt2468 2021-09-03 12:52:50 -07:00
parent 25b3bd44ba
commit 96dcc49adb
6 changed files with 142 additions and 57 deletions

View File

@ -71,6 +71,12 @@ const std::map<std::string, RequestMethodHandler> RequestHandler::_handlerMap
{"GetGroupSceneItemList", &RequestHandler::GetGroupSceneItemList},
{"CreateSceneItem", &RequestHandler::CreateSceneItem},
{"RemoveSceneItem", &RequestHandler::RemoveSceneItem},
{"GetSceneItemEnabled", &RequestHandler::GetSceneItemEnabled},
{"SetSceneItemEnabled", &RequestHandler::SetSceneItemEnabled},
{"GetSceneItemLocked", &RequestHandler::GetSceneItemLocked},
{"SetSceneItemLocked", &RequestHandler::SetSceneItemLocked},
{"GetSceneItemIndex", &RequestHandler::GetSceneItemIndex},
{"SetSceneItemIndex", &RequestHandler::SetSceneItemIndex},
// Stream
{"GetStreamStatus", &RequestHandler::GetStreamStatus},

View File

@ -87,6 +87,12 @@ class RequestHandler {
RequestResult GetGroupSceneItemList(const Request&);
RequestResult CreateSceneItem(const Request&);
RequestResult RemoveSceneItem(const Request&);
RequestResult GetSceneItemEnabled(const Request&);
RequestResult SetSceneItemEnabled(const Request&);
RequestResult GetSceneItemLocked(const Request&);
RequestResult SetSceneItemLocked(const Request&);
RequestResult GetSceneItemIndex(const Request&);
RequestResult SetSceneItemIndex(const Request&);
// Stream
RequestResult GetStreamStatus(const Request&);

View File

@ -267,16 +267,16 @@ RequestResult RequestHandler::SetVideoSettings(const Request& request)
RequestStatus::RequestStatus statusCode = RequestStatus::NoError;
std::string comment;
bool changeFps = (request.ValidateNumber("fpsNumerator", statusCode, comment, 1) && request.ValidateNumber("fpsDenominator", statusCode, comment, 1));
if (!changeFps && statusCode != RequestStatus::MissingRequestParameter)
bool changeFps = (request.Contains("fpsNumerator") && request.Contains("fpsDenominator"));
if (changeFps && !(request.ValidateOptionalNumber("fpsNumerator", statusCode, comment, 1) && request.ValidateOptionalNumber("fpsDenominator", statusCode, comment, 1)))
return RequestResult::Error(statusCode, comment);
bool changeBaseRes = (request.ValidateNumber("baseWidth", statusCode, comment, 8, 4096) && request.ValidateNumber("baseHeight", statusCode, comment, 8, 4096));
if (!changeBaseRes && statusCode != RequestStatus::MissingRequestParameter)
bool changeBaseRes = (request.Contains("baseWidth") && request.Contains("baseHeight"));
if (changeBaseRes && !(request.ValidateOptionalNumber("baseWidth", statusCode, comment, 8, 4096) && request.ValidateOptionalNumber("baseHeight", statusCode, comment, 8, 4096)))
return RequestResult::Error(statusCode, comment);
bool changeOutputRes = (request.ValidateNumber("outputWidth", statusCode, comment, 8, 4096) && request.ValidateNumber("outputHeight", statusCode, comment, 8, 4096));
if (!changeOutputRes && statusCode != RequestStatus::MissingRequestParameter)
bool changeOutputRes = (request.Contains("outputWidth") && request.Contains("outputHeight"));
if (changeOutputRes && !(request.ValidateOptionalNumber("outputWidth", statusCode, comment, 8, 4096) && request.ValidateOptionalNumber("outputHeight", statusCode, comment, 8, 4096)))
return RequestResult::Error(statusCode, comment);
config_t *config = obs_frontend_get_profile_config();

View File

@ -4,10 +4,10 @@ RequestResult RequestHandler::GetInputList(const Request& request)
{
std::string inputKind;
if (request.RequestData.contains("inputKind") && !request.RequestData["inputKind"].is_null()) {
if (request.Contains("inputKind")) {
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("inputKind", statusCode, comment))
if (!request.ValidateOptionalString("inputKind", statusCode, comment))
return RequestResult::Error(statusCode, comment);
inputKind = request.RequestData["inputKind"];
@ -22,10 +22,10 @@ RequestResult RequestHandler::GetInputKindList(const Request& request)
{
bool unversioned = false;
if (request.RequestData.contains("unversioned") && !request.RequestData["unversioned"].is_null()) {
if (request.Contains("unversioned")) {
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateBoolean("unversioned", statusCode, comment))
if (!request.ValidateOptionalBoolean("unversioned", statusCode, comment))
return RequestResult::Error(statusCode, comment);
unversioned = request.RequestData["unversioned"];
@ -41,9 +41,7 @@ RequestResult RequestHandler::CreateInput(const Request& request)
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease sceneSource = request.ValidateScene("sceneName", statusCode, comment);
if (!(request.ValidateString("inputName", statusCode, comment) &&
request.ValidateString("inputKind", statusCode, comment) &&
sceneSource))
if (!(sceneSource && request.ValidateString("inputName", statusCode, comment) && request.ValidateString("inputKind", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string inputName = request.RequestData["inputName"];
@ -52,14 +50,13 @@ RequestResult RequestHandler::CreateInput(const Request& request)
return RequestResult::Error(RequestStatus::ResourceAlreadyExists, "A source already exists by that input name.");
std::string inputKind = request.RequestData["inputKind"];
auto kinds = Utils::Obs::ListHelper::GetInputKindList();
if (std::find(kinds.begin(), kinds.end(), inputKind) == kinds.end())
return RequestResult::Error(RequestStatus::InvalidInputKind, "Your specified input kind is not supported by OBS. Check that your specified kind is properly versioned and that any necessary plugins are loaded.");
OBSDataAutoRelease inputSettings = nullptr;
if (request.RequestData.contains("inputSettings") && !request.RequestData["inputSettings"].is_null()) {
if (!request.ValidateObject("inputSettings", statusCode, comment, true))
if (request.Contains("inputSettings")) {
if (!request.ValidateOptionalObject("inputSettings", statusCode, comment, true))
return RequestResult::Error(statusCode, comment);
inputSettings = Utils::Json::JsonToObsData(request.RequestData["inputSettings"]);
@ -68,8 +65,8 @@ RequestResult RequestHandler::CreateInput(const Request& request)
OBSScene scene = obs_scene_from_source(sceneSource);
bool sceneItemEnabled = true;
if (request.RequestData.contains("sceneItemEnabled") && !request.RequestData["sceneItemEnabled"].is_null()) {
if (!request.ValidateBoolean("sceneItemEnabled", statusCode, comment))
if (request.Contains("sceneItemEnabled")) {
if (!request.ValidateOptionalBoolean("sceneItemEnabled", statusCode, comment))
return RequestResult::Error(statusCode, comment);
sceneItemEnabled = request.RequestData["sceneItemEnabled"];
@ -148,8 +145,8 @@ RequestResult RequestHandler::SetInputSettings(const Request& request)
return RequestResult::Error(statusCode, comment);
bool overlay = true;
if (request.RequestData.contains("overlay") && !request.RequestData["overlay"].is_null()) {
if (!request.ValidateBoolean("overlay", statusCode, comment))
if (request.Contains("overlay")) {
if (!request.ValidateOptionalBoolean("overlay", statusCode, comment))
return RequestResult::Error(statusCode, comment);
overlay = request.RequestData["overlay"];
@ -243,12 +240,12 @@ RequestResult RequestHandler::SetInputVolume(const Request& request)
if (!input)
return RequestResult::Error(statusCode, comment);
bool hasMul = request.ValidateNumber("inputVolumeMul", statusCode, comment, 0, 20);
if (!hasMul && statusCode != RequestStatus::MissingRequestParameter)
bool hasMul = request.Contains("inputVolumeMul");
if (hasMul && !request.ValidateOptionalNumber("inputVolumeMul", statusCode, comment, 0, 20))
return RequestResult::Error(statusCode, comment);
bool hasDb = request.ValidateNumber("inputVolumeDb", statusCode, comment, -100, 26);
if (!hasDb && statusCode != RequestStatus::MissingRequestParameter)
bool hasDb = request.Contains("inputVolumeDb");
if (hasDb && !request.ValidateOptionalNumber("inputVolumeDb", statusCode, comment, -100, 26))
return RequestResult::Error(statusCode, comment);
if (hasMul && hasDb)
@ -257,7 +254,7 @@ RequestResult RequestHandler::SetInputVolume(const Request& request)
if (!hasMul && !hasDb)
return RequestResult::Error(RequestStatus::MissingRequestParameter, "You must specify one volume parameter.");
float inputVolumeMul = 0.0;
float inputVolumeMul;
if (hasMul)
inputVolumeMul = request.RequestData["inputVolumeMul"];
else

View File

@ -69,3 +69,90 @@ RequestResult RequestHandler::RemoveSceneItem(const Request& request)
return RequestResult::Success();
}
RequestResult RequestHandler::GetSceneItemEnabled(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!sceneItem)
return RequestResult::Error(statusCode, comment);
json responseData;
responseData["sceneItemEnabled"] = obs_sceneitem_visible(sceneItem);
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetSceneItemEnabled(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!(sceneItem && request.ValidateBoolean("sceneItemEnabled", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
bool sceneItemEnabled = request.RequestData["sceneItemEnabled"];
obs_sceneitem_set_visible(sceneItem, sceneItemEnabled);
return RequestResult::Success();
}
RequestResult RequestHandler::GetSceneItemLocked(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!sceneItem)
return RequestResult::Error(statusCode, comment);
json responseData;
responseData["sceneItemLocked"] = obs_sceneitem_locked(sceneItem);
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetSceneItemLocked(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!(sceneItem && request.ValidateBoolean("sceneItemLocked", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
bool sceneItemLocked = request.RequestData["sceneItemLocked"];
obs_sceneitem_set_locked(sceneItem, sceneItemLocked);
return RequestResult::Success();
}
RequestResult RequestHandler::GetSceneItemIndex(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!sceneItem)
return RequestResult::Error(statusCode, comment);
json responseData;
responseData["sceneItemIndex"] = obs_sceneitem_get_order_position(sceneItem);
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetSceneItemIndex(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSceneItemAutoRelease sceneItem = request.ValidateSceneItem("sceneName", "sceneItemId", statusCode, comment);
if (!(sceneItem && request.ValidateNumber("sceneItemIndex", statusCode, comment, 0, 8192)))
return RequestResult::Error(statusCode, comment);
int sceneItemIndex = request.RequestData["sceneItemIndex"];
obs_sceneitem_set_order_position(sceneItem, sceneItemIndex);
return RequestResult::Success();
}

View File

@ -116,15 +116,10 @@ RequestResult RequestHandler::GetSourceScreenshot(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!(request.ValidateString("sourceName", statusCode, comment) && request.ValidateString("imageFormat", statusCode, comment)))
OBSSourceAutoRelease source = request.ValidateSource("sourceName", statusCode, comment);
if (!(source && request.ValidateString("imageFormat", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string sourceName = request.RequestData["sourceName"];
OBSSourceAutoRelease source = obs_get_source_by_name(sourceName.c_str());
if (!source)
return RequestResult::Error(RequestStatus::ResourceNotFound);
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT && obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE)
return RequestResult::Error(RequestStatus::InvalidResourceType, "The specified source is not an input or a scene.");
@ -137,22 +132,22 @@ RequestResult RequestHandler::GetSourceScreenshot(const Request& request)
uint32_t requestedHeight{0};
int compressionQuality{-1};
if (request.RequestData.contains("imageWidth") && !request.RequestData["imageWidth"].is_null()) {
if (!request.ValidateNumber("imageWidth", statusCode, comment, 8, 4096))
if (request.Contains("imageWidth")) {
if (!request.ValidateOptionalNumber("imageWidth", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedWidth = request.RequestData["imageWidth"];
}
if (request.RequestData.contains("imageHeight") && !request.RequestData["imageHeight"].is_null()) {
if (!request.ValidateNumber("imageHeight", statusCode, comment, 8, 4096))
if (request.Contains("imageHeight")) {
if (!request.ValidateOptionalNumber("imageHeight", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedHeight = request.RequestData["imageHeight"];
}
if (request.RequestData.contains("imageCompressionQuality") && !request.RequestData["imageCompressionQuality"].is_null()) {
if (!request.ValidateNumber("imageCompressionQuality", statusCode, comment, -1, 100))
if (request.Contains("imageCompressionQuality")) {
if (!request.ValidateOptionalNumber("imageCompressionQuality", statusCode, comment, -1, 100))
return RequestResult::Error(statusCode, comment);
compressionQuality = request.RequestData["imageCompressionQuality"];
@ -184,43 +179,43 @@ RequestResult RequestHandler::SaveSourceScreenshot(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!(request.ValidateString("sourceName", statusCode, comment) && request.ValidateString("imageFilePath", statusCode, comment) && request.ValidateString("imageFormat", statusCode, comment)))
OBSSourceAutoRelease source = request.ValidateSource("sourceName", statusCode, comment);
if (!(source && request.ValidateString("imageFormat", statusCode, comment) && request.ValidateString("imageFilePath", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string sourceName = request.RequestData["sourceName"];
OBSSourceAutoRelease source = obs_get_source_by_name(sourceName.c_str());
if (!source)
return RequestResult::Error(RequestStatus::ResourceNotFound, "No source was found by that name.");
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT && obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE)
return RequestResult::Error(RequestStatus::InvalidResourceType, "The specified source is not an input or a scene.");
std::string imageFormat = request.RequestData["imageFormat"];
std::string imageFilePath = request.RequestData["imageFilePath"];
if (!IsImageFormatValid(imageFormat))
return RequestResult::Error(RequestStatus::InvalidRequestParameter, "Your specified image format is invalid or not supported by this system.");
QFileInfo filePathInfo(QString::fromStdString(imageFilePath));
if (!filePathInfo.absoluteDir().exists())
return RequestResult::Error(RequestStatus::ResourceNotFound, "The directory for your file path does not exist.");
uint32_t requestedWidth{0};
uint32_t requestedHeight{0};
int compressionQuality{-1};
if (request.RequestData.contains("imageWidth") && !request.RequestData["imageWidth"].is_null()) {
if (!request.ValidateNumber("imageWidth", statusCode, comment, 8, 4096))
if (request.Contains("imageWidth")) {
if (!request.ValidateOptionalNumber("imageWidth", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedWidth = request.RequestData["imageWidth"];
}
if (request.RequestData.contains("imageHeight") && !request.RequestData["imageHeight"].is_null()) {
if (!request.ValidateNumber("imageHeight", statusCode, comment, 8, 4096))
if (request.Contains("imageHeight")) {
if (!request.ValidateOptionalNumber("imageHeight", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedHeight = request.RequestData["imageHeight"];
}
if (request.RequestData.contains("imageCompressionQuality") && !request.RequestData["imageCompressionQuality"].is_null()) {
if (!request.ValidateNumber("imageCompressionQuality", statusCode, comment, -1, 100))
if (request.Contains("imageCompressionQuality")) {
if (!request.ValidateOptionalNumber("imageCompressionQuality", statusCode, comment, -1, 100))
return RequestResult::Error(statusCode, comment);
compressionQuality = request.RequestData["imageCompressionQuality"];
@ -232,12 +227,6 @@ RequestResult RequestHandler::SaveSourceScreenshot(const Request& request)
if (!success)
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Failed to render screenshot.");
std::string imageFilePath = request.RequestData["imageFilePath"];
QFileInfo filePathInfo(QString::fromStdString(imageFilePath));
if (!filePathInfo.absoluteDir().exists())
return RequestResult::Error(RequestStatus::ResourceNotFound, "The directory for your file path does not exist.");
QString absoluteFilePath = filePathInfo.absoluteFilePath();
if (!renderedImage.save(absoluteFilePath, imageFormat.c_str(), compressionQuality))