From: Pawel Wasowski Date: Tue, 12 Jan 2021 17:03:25 +0000 (+0100) Subject: [ML][pipeline] Implement Switch::{getPadList, select} X-Git-Tag: submit/tizen/20210128.113801~8^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=96a01af309620538f9c139eb5542dd6a936f8f84;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [ML][pipeline] Implement Switch::{getPadList, select} ACR: TWDAPI-274 [Verification] Tested in Chrome DevTools with the snippets below, works fine. var pipeline_def = "videotestsrc is-live=true" + " ! videoconvert" + " ! tensor_converter" + " ! output-selector name=outs outs.src_0" + " ! tensor_sink name=sink0 async=false outs.src_1" + " ! tensor_sink name=sink1 async=false" var pipeline = tizen.ml.pipeline.createPipeline(pipeline_def, function(state) {console.log(state);}); pipeline.getSwitch('outs').getPadList() // ["src_0", "src_1"] pipeline.getSwitch('outs').select('src_1') // dlog: I/nnstreamer( 4274): Switched to [src_1] successfully at switch [outs]. pipeline.getSwitch('outs').select('src_3') // WebAPIException: InvalidValuesError pipeline.getSwitch('outs').select() // WebAPIException: InvalidValuesError Change-Id: I992e17841a8f8bec19aa60383e9d3ad0baf62975 Signed-off-by: Pawel Wasowski --- diff --git a/src/ml/js/ml_pipeline.js b/src/ml/js/ml_pipeline.js index 2cfb8bee..6ca93666 100755 --- a/src/ml/js/ml_pipeline.js +++ b/src/ml/js/ml_pipeline.js @@ -297,9 +297,71 @@ Pipeline.prototype.getSwitch = function() { //Source::inputData() end //Switch::getPadList() begin +var ValidSwitchGetPadListExceptions = [ + 'InvalidStateError', + 'NotFoundError', + 'NotSupportedError', + 'AbortError' +]; +Switch.prototype.getPadList = function() { + var nativeArgs = { + name: this.name, + id: this._pipeline_id + }; + + var result = native_.callSync('MLPipelineSwitchGetPadList', nativeArgs); + + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + ValidSwitchGetPadListExceptions, + AbortError + ); + } + return result.result; +}; //Switch::getPadList() end +//Switch::select() begin +var ValidSwitchSelectExceptions = [ + 'InvalidValuesError', + 'NotFoundError', + 'NotSupportedError', + 'AbortError' +]; +Switch.prototype.select = function() { + var args = validator_.validateArgs(arguments, [ + { + name: 'padName', + type: validator_.Types.STRING + } + ]); + + if (!args.has.padName) { + throw new WebAPIException( + WebAPIException.INVALID_VALUES_ERR, + 'Invalid parameter: pad name is mandatory' + ); + } + + var nativeArgs = { + id: this._pipeline_id, + name: this.name, + padName: args.padName + }; + + var result = native_.callSync('MLPipelineSwitchSelect', nativeArgs); + if (native_.isFailure(result)) { + throw native_.getErrorObjectAndValidate( + result, + ValidSwitchSelectExceptions, + AbortError + ); + } +}; +//Switch::select() end + //Valve::setOpen() begin //Valve::setOpen() end diff --git a/src/ml/ml_instance.cc b/src/ml/ml_instance.cc index f1ce1f9e..3b54b0c7 100644 --- a/src/ml/ml_instance.cc +++ b/src/ml/ml_instance.cc @@ -37,6 +37,7 @@ const std::string kId = "id"; const std::string kDefinition = "definition"; const std::string kPipelineStateChangeListenerName = "listenerName"; const std::string kOtherId = "otherId"; +const std::string kPadName = "padName"; } // namespace using namespace common; @@ -100,6 +101,8 @@ MlInstance::MlInstance() : pipeline_manager_{this} { REGISTER_METHOD(MLPipelineStop); REGISTER_METHOD(MLPipelineGetNodeInfo); REGISTER_METHOD(MLPipelineGetSwitch); + REGISTER_METHOD(MLPipelineSwitchGetPadList); + REGISTER_METHOD(MLPipelineSwitchSelect); // Pipeline API end #undef REGISTER_METHOD @@ -712,7 +715,7 @@ void MlInstance::MLPipelineGetSwitch(const picojson::value& args, picojson::obje } auto name = args.get(kName).get(); - auto pipeline_id = args.get(kId).get(); + auto pipeline_id = static_cast(args.get(kId).get()); std::string type; auto ret = pipeline_manager_.GetSwitch(name, pipeline_id, &type); @@ -763,9 +766,48 @@ void MlInstance::MLPipelineGetSwitch(const picojson::value& args, picojson::obje // Source::inputData() end // Switch::getPadList() begin +void MlInstance::MLPipelineSwitchGetPadList(const picojson::value& args, picojson::object& out) { + ScopeLogger("args: [%s]", args.serialize().c_str()); + + CHECK_ARGS(args, kId, double, out); + CHECK_ARGS(args, kName, std::string, out); + + auto pipeline_id = static_cast(args.get(kId).get()); + auto& switch_name = args.get(kName).get(); + + picojson::array pad_list; + auto ret = pipeline_manager_.SwitchGetPadList(pipeline_id, switch_name, &pad_list); + if (!ret) { + LogAndReportError(ret, &out); + return; + } + ReportSuccess(picojson::value{std::move(pad_list)}, out); +} // Switch::getPadList() end +// Switch::select() end +void MlInstance::MLPipelineSwitchSelect(const picojson::value& args, picojson::object& out) { + ScopeLogger("args: [%s]", args.serialize().c_str()); + + CHECK_ARGS(args, kId, double, out); + CHECK_ARGS(args, kName, std::string, out); + CHECK_ARGS(args, kPadName, std::string, out); + + auto pipeline_id = args.get(kId).get(); + auto& switch_name = args.get(kName).get(); + auto& pad_name = args.get(kPadName).get(); + + auto ret = pipeline_manager_.SwitchSelect(pipeline_id, switch_name, pad_name); + if (!ret) { + LogAndReportError(ret, &out); + return; + } + + ReportSuccess(out); +} +// Switch::select() end + // Valve::setOpen() begin // Valve::setOpen() end diff --git a/src/ml/ml_instance.h b/src/ml/ml_instance.h index 08e6dc82..6a3b5235 100644 --- a/src/ml/ml_instance.h +++ b/src/ml/ml_instance.h @@ -129,9 +129,13 @@ class MlInstance : public common::ParsedInstance { // Source::inputData() end // Switch::getPadList() begin - + void MLPipelineSwitchGetPadList(const picojson::value& args, picojson::object& out); // Switch::getPadList() end + // Switch::select() begin + void MLPipelineSwitchSelect(const picojson::value& args, picojson::object& out); + // Switch::select() end + // Valve::setOpen() begin // Valve::setOpen() end diff --git a/src/ml/ml_pipeline.cc b/src/ml/ml_pipeline.cc index de5edb1d..ded74368 100644 --- a/src/ml/ml_pipeline.cc +++ b/src/ml/ml_pipeline.cc @@ -293,6 +293,18 @@ PlatformResult Pipeline::GetSwitch(const std::string& name, std::string* type) { // Source::inputData() end // Switch::getPadList() begin +PlatformResult Pipeline::GetSwitch(const std::string& name, Switch** out) { + ScopeLogger("id: [%d], name: [%s]", id_, name.c_str()); + + auto switch_it = switches_.find(name); + if (switches_.end() != switch_it) { + LoggerD("Switch [%s] found", name.c_str()); + *out = switch_it->second.get(); + return PlatformResult{}; + } + LoggerE("Switch [%s] not found", name.c_str()); + return PlatformResult{ErrorCode::ABORT_ERR, "Switch does not exist"}; +} // Switch::getPadList() end diff --git a/src/ml/ml_pipeline.h b/src/ml/ml_pipeline.h index 9d3df8ca..fda943a0 100644 --- a/src/ml/ml_pipeline.h +++ b/src/ml/ml_pipeline.h @@ -119,6 +119,7 @@ class Pipeline { // Source::inputData() end // Switch::getPadList() begin + PlatformResult GetSwitch(const std::string& name, Switch** out); // Switch::getPadList() end diff --git a/src/ml/ml_pipeline_manager.cc b/src/ml/ml_pipeline_manager.cc index 20bae151..3ee54e3a 100644 --- a/src/ml/ml_pipeline_manager.cc +++ b/src/ml/ml_pipeline_manager.cc @@ -199,9 +199,48 @@ PlatformResult PipelineManager::GetSwitch(const std::string& name, int pipeline_ // Source::inputData() end // Switch::getPadList() begin +PlatformResult PipelineManager::SwitchGetPadList(int pipeline_id, const std::string& switch_name, + picojson::array* out) { + ScopeLogger("pipeline_id: [%d], switch_name: [%s]", pipeline_id, switch_name.c_str()); + auto pipeline_it = pipelines_.find(pipeline_id); + if (pipelines_.end() == pipeline_it) { + LoggerD("Pipeline not found: [%d]", pipeline_id); + return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"}; + } + + Switch* switch_ptr = nullptr; + auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr); + if (!ret) { + return ret; + } + + return switch_ptr->GetPadList(out); +} // Switch::getPadList() end +// Switch::select() begin +PlatformResult PipelineManager::SwitchSelect(int pipeline_id, const std::string& switch_name, + const std::string& pad_name) { + ScopeLogger("pipeline_id: [%d], switch_name: [%s], pad_name: [%s]", pipeline_id, + switch_name.c_str(), pad_name.c_str()); + + auto pipeline_it = pipelines_.find(pipeline_id); + if (pipelines_.end() == pipeline_it) { + LoggerD("Pipeline not found: [%d]", pipeline_id); + return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"}; + } + + Switch* switch_ptr = nullptr; + auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr); + if (!ret) { + return ret; + } + + return switch_ptr->Select(pad_name); +} +// Switch::select() end + // Valve::setOpen() begin // Valve::setOpen() end diff --git a/src/ml/ml_pipeline_manager.h b/src/ml/ml_pipeline_manager.h index f031fc89..668933a0 100644 --- a/src/ml/ml_pipeline_manager.h +++ b/src/ml/ml_pipeline_manager.h @@ -108,9 +108,15 @@ class PipelineManager { // Source::inputData() end // Switch::getPadList() begin - + PlatformResult SwitchGetPadList(int pipeline_id, const std::string& switch_name, + picojson::array* out); // Switch::getPadList() end + // Switch::select() begin + PlatformResult SwitchSelect(int pipeline_id, const std::string& switch_name, + const std::string& pad_name); + // Switch::select() end + // Valve::setOpen() begin // Valve::setOpen() end diff --git a/src/ml/ml_pipeline_switch.cc b/src/ml/ml_pipeline_switch.cc index 43a82205..c6045303 100644 --- a/src/ml/ml_pipeline_switch.cc +++ b/src/ml/ml_pipeline_switch.cc @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + #include "ml_pipeline_switch.h" #include "ml_utils.h" @@ -86,6 +88,45 @@ std::string Switch::GetType() const { return type_; } +PlatformResult Switch::GetPadList(picojson::array* out) const { + ScopeLogger("name: [%s]", name_.c_str()); + + gchar** list = nullptr; + auto ret = ml_pipeline_switch_get_pad_list(switch_, &list); + if (ML_ERROR_NONE != ret) { + LoggerE("ml_pipeline_switch_get_pad_list() failed: [%d] (%s)", ret, get_error_message(ret)); + return util::ToPlatformResult(ret, "Could not get pad list"); + } + LoggerD("ml_pipeline_switch_get_pad_list() succeeded"); + + if (!list) { + LoggerD("Pad list empty"); + return PlatformResult{}; + } + + for (int i = 0; nullptr != list[i]; ++i) { + LoggerD("list[%d]: [%s]", i, list[i]); + out->push_back(picojson::value{std::string{list[i]}}); + g_free(list[i]); + } + g_free(list); + + return PlatformResult{}; +} + +PlatformResult Switch::Select(const std::string& pad_name) const { + ScopeLogger("name: [%s], pad_name: [%s]", name_.c_str(), pad_name.c_str()); + + auto ret = ml_pipeline_switch_select(switch_, pad_name.c_str()); + if (ML_ERROR_NONE != ret) { + LoggerE("ml_pipeline_switch_select() failed: [%d] (%s)", ret, get_error_message(ret)); + return util::ToPlatformResult(ret, "Could not select pad"); + } + LoggerD("ml_pipeline_switch_select() succeeded"); + + return PlatformResult{}; +} + } // namespace pipeline } // namespace ml } // namespace extension \ No newline at end of file diff --git a/src/ml/ml_pipeline_switch.h b/src/ml/ml_pipeline_switch.h index 3175387a..48553c94 100644 --- a/src/ml/ml_pipeline_switch.h +++ b/src/ml/ml_pipeline_switch.h @@ -36,6 +36,8 @@ class Switch { std::unique_ptr* out); std::string GetType() const; + PlatformResult GetPadList(picojson::array* out) const; + PlatformResult Select(const std::string& pad_name) const; ~Switch();