From db8bc1af2d1e44997e09c36e820f7639bfb32a3d Mon Sep 17 00:00:00 2001 From: Palakis Date: Fri, 27 Oct 2017 14:18:45 +0200 Subject: [PATCH] requests: add GetSourceSettings and SetSourceSettings --- WSRequestHandler.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++- WSRequestHandler.h | 2 + 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/WSRequestHandler.cpp b/WSRequestHandler.cpp index fc3fdc1f..8fa7799b 100644 --- a/WSRequestHandler.cpp +++ b/WSRequestHandler.cpp @@ -89,6 +89,8 @@ WSRequestHandler::WSRequestHandler(QWebSocket* client) : messageMap["SetSyncOffset"] = WSRequestHandler::HandleSetSyncOffset; messageMap["GetSyncOffset"] = WSRequestHandler::HandleGetSyncOffset; messageMap["GetSpecialSources"] = WSRequestHandler::HandleGetSpecialSources; + messageMap["GetSourceSettings"] = WSRequestHandler::HandleGetSourceSettings; + messageMap["SetSourceSettings"] = WSRequestHandler::HandleSetSourceSettings; messageMap["SetCurrentSceneCollection"] = WSRequestHandler::HandleSetCurrentSceneCollection; messageMap["GetCurrentSceneCollection"] = WSRequestHandler::HandleGetCurrentSceneCollection; @@ -2135,7 +2137,7 @@ void WSRequestHandler::HandleGetSpecialSources(WSRequestHandler* req) { /** * Change the current recording folder. * - * @param {Stsring} `rec-folder` Path of the recording folder. + * @param {String} `rec-folder` Path of the recording folder. * * @api requests * @name SetRecordingFolder @@ -2697,3 +2699,116 @@ void WSRequestHandler::HandleResetSceneItem(WSRequestHandler* req) { obs_source_release(scene); } +* Get settings of the specified source +* +* @param {String} `sourceName` Name of the source item. +* @param {String (optional) `sourceType` Type of the specified source. Useful for type-checking if you expect a specific settings schema. +* +* @return {String} `sourceName` Source name +* @return {String} `sourceType` Type of the specified source +* @return {Object} `sourceSettings` Source settings. Varying between source types. +* +* @api requests +* @name GetSourcesList +* @category sources +* @since unreleased +*/ +void WSRequestHandler::HandleGetSourceSettings(WSRequestHandler* req) { + if (!req->hasField("sourceName")) { + req->SendErrorResponse("missing request parameters"); + return; + } + + const char* sourceName = obs_data_get_string(req->data, "sourceName"); + obs_source_t* source = obs_get_source_by_name(sourceName); + if (!source) { + req->SendErrorResponse("specified source doesn't exist"); + return; + } + + if (req->hasField("sourceType")) { + QString actualSourceType = obs_source_get_id(source); + QString requestedType = obs_data_get_string(req->data, "sourceType"); + + if (actualSourceType != requestedType) { + req->SendErrorResponse("specified source exists but is not of expected type"); + obs_source_release(source); + return; + } + } + + obs_data_t* sourceSettings = obs_source_get_settings(source); + + obs_data_t* response = obs_data_create(); + obs_data_set_string(response, "sourceName", obs_source_get_name(source)); + obs_data_set_string(response, "sourceType", obs_source_get_id(source)); + obs_data_set_obj(response, "sourceSettings", sourceSettings); + req->SendOKResponse(response); + + obs_data_release(response); + obs_data_release(sourceSettings); + obs_source_release(source); +} + +/** +* Set settings of the specified source. +* +* @param {String} `sourceName` Name of the source item. +* @param {String (optional)} `sourceType` Type of the specified source. Useful for type-checking to avoid settings a set of settings incompatible with the actual source's type. +* @param {Object} `sourceSettings` Source settings. Varying between source types. +* +* @return {String} `sourceName` Source name +* @return {String} `sourceType` Type of the specified source +* @return {Object} `sourceSettings` Source settings. Varying between source types. +* +* @api requests +* @name GetSourcesList +* @category sources +* @since unreleased +*/ +void WSRequestHandler::HandleSetSourceSettings(WSRequestHandler* req) { + if (!req->hasField("sourceName") || !req->hasField("sourceSettings")) { + req->SendErrorResponse("missing request parameters"); + return; + } + + const char* sourceName = obs_data_get_string(req->data, "sourceName"); + obs_source_t* source = obs_get_source_by_name(sourceName); + if (!source) { + req->SendErrorResponse("specified source doesn't exist"); + return; + } + + if (req->hasField("sourceType")) { + QString actualSourceType = obs_source_get_id(source); + QString requestedType = obs_data_get_string(req->data, "sourceType"); + + if (actualSourceType != requestedType) { + req->SendErrorResponse("specified source exists but is not of expected type"); + obs_source_release(source); + return; + } + } + + obs_data_t* currentSettings = obs_source_get_settings(source); + obs_data_t* newSettings = obs_data_get_obj(req->data, "sourceSettings"); + + obs_data_t* sourceSettings = obs_data_create(); + obs_data_apply(sourceSettings, currentSettings); + obs_data_apply(sourceSettings, newSettings); + + obs_source_update(source, sourceSettings); + obs_source_update_properties(source); + + obs_data_t* response = obs_data_create(); + obs_data_set_string(response, "sourceName", obs_source_get_name(source)); + obs_data_set_string(response, "sourceType", obs_source_get_id(source)); + obs_data_set_obj(response, "sourceSettings", nullptr); + req->SendOKResponse(response); + + obs_data_release(response); + obs_data_release(sourceSettings); + obs_data_release(newSettings); + obs_data_release(currentSettings); + obs_source_release(source); +} diff --git a/WSRequestHandler.h b/WSRequestHandler.h index 53eb56a2..8ece80f0 100644 --- a/WSRequestHandler.h +++ b/WSRequestHandler.h @@ -95,6 +95,8 @@ class WSRequestHandler : public QObject { static void HandleSetSyncOffset(WSRequestHandler* req); static void HandleGetSyncOffset(WSRequestHandler* req); static void HandleGetSpecialSources(WSRequestHandler* req); + static void HandleGetSourceSettings(WSRequestHandler* req); + static void HandleSetSourceSettings(WSRequestHandler* req); static void HandleSetCurrentSceneCollection(WSRequestHandler* req); static void HandleGetCurrentSceneCollection(WSRequestHandler* req);