From b65eb2c6edc9d7f1aeb9dfd5aaa4344eeed6ee64 Mon Sep 17 00:00:00 2001 From: Pawel Wasowski Date: Tue, 5 Jan 2021 22:47:17 +0100 Subject: [PATCH] [ML][pipeline] Implement Pipeline::dispose() 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 --- src/ml/js/ml_pipeline.js | 12 ++++++++++++ src/ml/ml_instance.cc | 21 +++++++++++++++++++-- src/ml/ml_instance.h | 2 +- src/ml/ml_pipeline.cc | 18 ++++++++++++++++++ src/ml/ml_pipeline.h | 2 +- src/ml/ml_pipeline_manager.cc | 20 ++++++++++++++++++++ src/ml/ml_pipeline_manager.h | 2 +- 7 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/ml/js/ml_pipeline.js b/src/ml/js/ml_pipeline.js index 3472be1f..e9debd3e 100755 --- a/src/ml/js/ml_pipeline.js +++ b/src/ml/js/ml_pipeline.js @@ -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 diff --git a/src/ml/ml_instance.cc b/src/ml/ml_instance.cc index 0576dac0..17415507 100644 --- a/src/ml/ml_instance.cc +++ b/src/ml/ml_instance.cc @@ -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()) { + LoggerD("id is not a number"); + ReportError(PlatformResult{ErrorCode::ABORT_ERR, "Invalid pipeline"}, &out); + return; + } + + auto id = static_cast(args.get(kId).get()); + 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 diff --git a/src/ml/ml_instance.h b/src/ml/ml_instance.h index 7bb28f44..975fd7a5 100644 --- a/src/ml/ml_instance.h +++ b/src/ml/ml_instance.h @@ -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 diff --git a/src/ml/ml_pipeline.cc b/src/ml/ml_pipeline.cc index 6faec447..440d7734 100644 --- a/src/ml/ml_pipeline.cc +++ b/src/ml/ml_pipeline.cc @@ -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 diff --git a/src/ml/ml_pipeline.h b/src/ml/ml_pipeline.h index c29f244f..92e2fa93 100644 --- a/src/ml/ml_pipeline.h +++ b/src/ml/ml_pipeline.h @@ -64,7 +64,7 @@ class Pipeline { // Pipeline::stop() end // Pipeline::dispose() begin - + PlatformResult Dispose(); // Pipeline::dispose() end // Pipeline::getNodeInfo() begin diff --git a/src/ml/ml_pipeline_manager.cc b/src/ml/ml_pipeline_manager.cc index 2d4781ce..553770a9 100644 --- a/src/ml/ml_pipeline_manager.cc +++ b/src/ml/ml_pipeline_manager.cc @@ -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 diff --git a/src/ml/ml_pipeline_manager.h b/src/ml/ml_pipeline_manager.h index f3e0293b..f5d0af3e 100644 --- a/src/ml/ml_pipeline_manager.h +++ b/src/ml/ml_pipeline_manager.h @@ -56,7 +56,7 @@ class PipelineManager { // Pipeline::stop() end // Pipeline::dispose() begin - + PlatformResult DisposePipeline(int id); // Pipeline::dispose() end // Pipeline::getNodeInfo() begin -- 2.34.1