[ML][pipeline] Implement Switch::{getPadList, select} 12/251612/6
authorPawel Wasowski <p.wasowski2@samsung.com>
Tue, 12 Jan 2021 17:03:25 +0000 (18:03 +0100)
committerPawel Wasowski <p.wasowski2@samsung.com>
Tue, 19 Jan 2021 05:13:56 +0000 (06:13 +0100)
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 <p.wasowski2@samsung.com>
src/ml/js/ml_pipeline.js
src/ml/ml_instance.cc
src/ml/ml_instance.h
src/ml/ml_pipeline.cc
src/ml/ml_pipeline.h
src/ml/ml_pipeline_manager.cc
src/ml/ml_pipeline_manager.h
src/ml/ml_pipeline_switch.cc
src/ml/ml_pipeline_switch.h

index 2cfb8be..6ca9366 100755 (executable)
@@ -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
index f1ce1f9..3b54b0c 100644 (file)
@@ -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<std::string>();
-  auto pipeline_id = args.get(kId).get<double>();
+  auto pipeline_id = static_cast<int>(args.get(kId).get<double>());
   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<int>(args.get(kId).get<double>());
+  auto& switch_name = args.get(kName).get<std::string>();
+
+  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<double>();
+  auto& switch_name = args.get(kName).get<std::string>();
+  auto& pad_name = args.get(kPadName).get<std::string>();
+
+  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
index 08e6dc8..6a3b523 100644 (file)
@@ -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
index de5edb1..ded7436 100644 (file)
@@ -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
 
index 9d3df8c..fda943a 100644 (file)
@@ -119,6 +119,7 @@ class Pipeline {
   // Source::inputData() end
 
   // Switch::getPadList() begin
+  PlatformResult GetSwitch(const std::string& name, Switch** out);
 
   // Switch::getPadList() end
 
index 20bae15..3ee54e3 100644 (file)
@@ -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
index f031fc8..668933a 100644 (file)
@@ -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
index 43a8220..c604530 100644 (file)
@@ -14,6 +14,8 @@
  *    limitations under the License.
  */
 
+#include <glib.h>
+
 #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
index 3175387..48553c9 100644 (file)
@@ -36,6 +36,8 @@ class Switch {
                                      std::unique_ptr<Switch>* out);
 
   std::string GetType() const;
+  PlatformResult GetPadList(picojson::array* out) const;
+  PlatformResult Select(const std::string& pad_name) const;
 
   ~Switch();