[ML][pipeline] Implement Pipeline::dispose() 70/251070/8
authorPawel Wasowski <p.wasowski2@samsung.com>
Tue, 5 Jan 2021 21:47:17 +0000 (22:47 +0100)
committerPawel Wasowski <p.wasowski2@samsung.com>
Mon, 18 Jan 2021 09:59:49 +0000 (10:59 +0100)
ACR: TWDAPI-274

[Verification] Tested in Chrome DevTools with the snippet below, works
fine.

var pipeline = tizen.ml.pipeline.createPipeline('videotestsrc ! tizenwlsink',
                                                function(state) {console.log(state);})
// test screen appears

pipeline.dispose() // no errors, test screen disappears

Change-Id: I5401c3210f73619d5577380abf17139fadc390f1
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

index 3472be1fe5cc80e138c9aa8113fc4e2a40484d37..e9debd3e9b87fa25bc65d12603bb530a3dcf23a9 100755 (executable)
@@ -17,6 +17,8 @@
 var kPipelineStateChangeListenerNamePrefix = 'MLPipelineStateChangeListener';
 
 //PipelineManager::createPipeline() begin
+var ValidPipelineDisposeExceptions = ['NotFoundError', 'NotSupportedError', 'AbortError'];
+
 var nextPipelineId = 1;
 function NextPipelineId() {
     return nextPipelineId++;
@@ -120,7 +122,17 @@ var Pipeline = function(id) {
 //Pipeline::stop() end
 
 //Pipeline::dispose() begin
+Pipeline.prototype.dispose = function() {
+    var result = native_.callSync('MLPipelineDispose', { id: this._id });
 
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            ValidPipelineDisposeExceptions,
+            AbortError
+        );
+    }
+};
 //Pipeline::dispose() end
 
 //Pipeline::getNodeInfo() begin
index 0576dac0cd235adddd71d50931cc4d72df43c831..17415507a503172f350ceba32f8f50ed108dfbf6 100644 (file)
@@ -41,7 +41,7 @@ MlInstance::MlInstance() : pipeline_manager_{this} {
   // Pipeline API begin
   REGISTER_METHOD(MLPipelineManagerCreatePipeline);
   REGISTER_METHOD(MLPipelineGetState);
-
+  REGISTER_METHOD(MLPipelineDispose);
 // Pipeline API end
 
 #undef REGISTER_METHOD
@@ -145,7 +145,25 @@ void MlInstance::MLPipelineGetState(const picojson::value& args, picojson::objec
 // Pipeline::stop() end
 
 // Pipeline::dispose() begin
+void MlInstance::MLPipelineDispose(const picojson::value& args, picojson::object& out) {
+  ScopeLogger("args: %s", args.serialize().c_str());
+
+  if (!args.get(kId).is<double>()) {
+    LoggerD("id is not a number");
+    ReportError(PlatformResult{ErrorCode::ABORT_ERR, "Invalid pipeline"}, &out);
+    return;
+  }
+
+  auto id = static_cast<int>(args.get(kId).get<double>());
+  auto ret = pipeline_manager_.DisposePipeline(id);
 
+  if (!ret) {
+    ReportError(ret, &out);
+    return;
+  }
+
+  ReportSuccess(out);
+}
 // Pipeline::dispose() end
 
 // Pipeline::getNodeInfo() begin
@@ -203,7 +221,6 @@ void MlInstance::MLPipelineGetState(const picojson::value& args, picojson::objec
 // Valve::setOpen() begin
 
 // Valve::setOpen() end
-
 // Pipeline API end
 
 }  // namespace ml
index 7bb28f444cf41082bfc68c02e47918e95fe7ea63..975fd7a54caf205fd5c2ae052ddcfa7296e8dc01 100644 (file)
@@ -60,7 +60,7 @@ class MlInstance : public common::ParsedInstance {
   // Pipeline::stop() end
 
   // Pipeline::dispose() begin
-
+  void MLPipelineDispose(const picojson::value& args, picojson::object& out);
   // Pipeline::dispose() end
 
   // Pipeline::getNodeInfo() begin
index 6faec4473a5aa898b8437d3b71174cfb7eda211f..440d77348bf7022738fb2168c7d6e98237bc78a1 100644 (file)
@@ -112,6 +112,11 @@ PlatformResult Pipeline::CreatePipeline(int id, const std::string& definition,
 Pipeline::~Pipeline() {
   ScopeLogger("Destroying pipeline: [%d]", id_);
 
+  if (!pipeline_) {
+    LoggerD("pipeline_ has already been destroyed");
+    return;
+  }
+
   auto ret = ml_pipeline_destroy(pipeline_);
   if (ML_ERROR_NONE != ret) {
     LoggerE("ml_pipeline_destroy() failed: [%d] (%s)", ret, get_error_message(ret));
@@ -158,7 +163,20 @@ PlatformResult Pipeline::GetState(std::string* out) {
 // Pipeline::stop() end
 
 // Pipeline::dispose() begin
+PlatformResult Pipeline::Dispose() {
+  ScopeLogger("id_: [%d]", id_);
+
+  auto ret = ml_pipeline_destroy(pipeline_);
+  if (ML_ERROR_NONE != ret) {
+    LoggerE("ml_pipeline_destroy() failed: [%d] (%s)", ret, get_error_message(ret));
+    return util::ToPlatformResult(ret, "Could not dispose the pipeline");
+  }
+  LoggerD("ml_pipeline_destroy() succeeded");
 
+  pipeline_ = nullptr;
+
+  return PlatformResult{};
+}
 // Pipeline::dispose() end
 
 // Pipeline::getNodeInfo() begin
index c29f244f0c91b58dbd9d5c0b9a5b0b922afc67ce..92e2fa936113453cd077ae2bb058f13c0139b384 100644 (file)
@@ -64,7 +64,7 @@ class Pipeline {
   // Pipeline::stop() end
 
   // Pipeline::dispose() begin
-
+  PlatformResult Dispose();
   // Pipeline::dispose() end
 
   // Pipeline::getNodeInfo() begin
index 2d4781ceedfcf7672b175b31d6f1dcdf4813063e..553770a940be65aa626f4952f34d9bb2cf5636f2 100644 (file)
@@ -82,7 +82,27 @@ PlatformResult PipelineManager::GetPipelineState(int id, std::string* out) {
 // Pipeline::stop() end
 
 // Pipeline::dispose() begin
+PlatformResult PipelineManager::DisposePipeline(int id) {
+  ScopeLogger("id: [%d]", id);
+
+  auto pipeline_it = pipelines_.find(id);
+  if (pipelines_.end() == pipeline_it) {
+    return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
+  }
 
+  /*
+   * Native ml_pipeline_destroy() may fail (I've checked its implementation),
+   * so we shouldn't just call pipelines_.erase(id) and let the ~Pipeline()
+   * destroy the pipeline, but only call pipelines_.erase(id) when
+   * pipeline->Dispose() succeeds.
+   */
+  auto ret = pipeline_it->second->Dispose();
+  if (ret) {
+    pipelines_.erase(id);
+  }
+
+  return ret;
+}
 // Pipeline::dispose() end
 
 // Pipeline::getNodeInfo() begin
index f3e0293b042f8499151413d06ad4efa4e5ef66e4..f5d0af3e9e04c839ced8d6c589bf596efcc319e2 100644 (file)
@@ -56,7 +56,7 @@ class PipelineManager {
   // Pipeline::stop() end
 
   // Pipeline::dispose() begin
-
+  PlatformResult DisposePipeline(int id);
   // Pipeline::dispose() end
 
   // Pipeline::getNodeInfo() begin