2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "ml_pipeline_manager.h"
18 #include "common/tools.h"
19 #include "ml_pipeline_switch.h"
21 using common::PlatformResult;
22 using common::ErrorCode;
23 using common::tools::ReportError;
24 using common::tools::ReportSuccess;
29 PipelineManager::PipelineManager(common::Instance* instance_ptr) : instance_ptr_{instance_ptr} {
33 PipelineManager::~PipelineManager() {
37 // PipelineManager::createPipeline() begin
38 PlatformResult PipelineManager::CreatePipeline(int id, const std::string& definition,
39 const std::string& state_change_listener_name) {
40 ScopeLogger("id: [%d], definition: [%s], state_change_listener_name: [%s]", id,
41 definition.c_str(), state_change_listener_name.c_str());
43 if (pipelines_.count(id)) {
44 LoggerD("The pipeline already exists: [%d]", id);
45 return PlatformResult{ErrorCode::ABORT_ERR, "Could not create pipeline"};
48 std::unique_ptr<Pipeline> pipeline_ptr;
49 auto ret = Pipeline::CreatePipeline(id, definition, state_change_listener_name, instance_ptr_,
55 pipelines_.insert({id, std::move(pipeline_ptr)});
57 return PlatformResult{};
59 // PipelineManager::createPipeline() end
61 // Pipeline::state begin
62 PlatformResult PipelineManager::GetPipelineState(int id, std::string* out) {
63 ScopeLogger("id: [%d]", id);
65 auto pipeline_it = pipelines_.find(id);
66 if (pipelines_.end() == pipeline_it) {
67 LoggerD("Pipeline not found: [%d]", id);
69 return PlatformResult{};
72 auto ret = pipeline_it->second->GetState(out);
75 // Pipeline::state end
77 // Pipeline::start() begin
78 PlatformResult PipelineManager::Start(int id) {
79 ScopeLogger("id: [%d]", id);
81 auto pipeline_it = pipelines_.find(id);
82 if (pipelines_.end() == pipeline_it) {
83 LoggerD("Pipeline not found: [%d]", id);
84 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
87 auto ret = pipeline_it->second->Start();
90 // Pipeline::start() end
92 // Pipeline::stop() begin
93 PlatformResult PipelineManager::Stop(int id) {
94 ScopeLogger("id: [%d]", id);
96 auto pipeline_it = pipelines_.find(id);
97 if (pipelines_.end() == pipeline_it) {
98 LoggerD("Pipeline not found: [%d]", id);
99 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
102 auto ret = pipeline_it->second->Stop();
105 // Pipeline::stop() end
107 // Pipeline::dispose() begin
108 PlatformResult PipelineManager::DisposePipeline(int id) {
109 ScopeLogger("id: [%d]", id);
111 auto pipeline_it = pipelines_.find(id);
112 if (pipelines_.end() == pipeline_it) {
113 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
117 * Native ml_pipeline_destroy() may fail (I've checked its implementation),
118 * so we shouldn't just call pipelines_.erase(id) and let the ~Pipeline()
119 * destroy the pipeline, but only call pipelines_.erase(id) when
120 * pipeline->Dispose() succeeds.
122 auto ret = pipeline_it->second->Dispose();
124 pipelines_.erase(id);
129 // Pipeline::dispose() end
131 // Pipeline::getNodeInfo() begin
132 PlatformResult PipelineManager::GetNodeInfo(int id, std::string& name) {
133 ScopeLogger("id: [%d], name [%s]", id, name.c_str());
135 auto pipeline_it = pipelines_.find(id);
136 if (pipelines_.end() == pipeline_it) {
137 LoggerD("Pipeline not found: [%d]", id);
138 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
141 auto ret = pipeline_it->second->GetNodeInfo(name);
144 // Pipeline::getNodeInfo() end
146 // Pipeline::getSource() begin
148 // Pipeline::getSource() end
150 // Pipeline::getSwitch() begin
151 PlatformResult PipelineManager::GetSwitch(const std::string& name, int pipeline_id,
153 ScopeLogger("name: [%s], pipeline_id: [%d]", name.c_str(), pipeline_id);
155 auto pipeline_it = pipelines_.find(pipeline_id);
156 if (pipelines_.end() == pipeline_it) {
157 LoggerD("Pipeline not found: [%d]", pipeline_id);
158 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
161 return pipeline_it->second->GetSwitch(name, type);
163 // Pipeline::getSwitch() end
165 // Pipeline::getValve() begin
166 PlatformResult PipelineManager::GetValve(const std::string& name, int pipeline_id) {
167 ScopeLogger("name: [%s], pipeline_id: [%d]", name.c_str(), pipeline_id);
169 auto pipeline_it = pipelines_.find(pipeline_id);
170 if (pipelines_.end() == pipeline_it) {
171 LoggerD("Pipeline not found: [%d]", pipeline_id);
172 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
175 return pipeline_it->second->GetValve(name);
177 // Pipeline::getValve() end
179 // Pipeline::registerSinkCallback() begin
181 // Pipeline::registerSinkCallback() end
183 // Pipeline::unregisterSinkCallback() begin
185 // Pipeline::unregisterSinkCallback() end
187 // Pipeline::registerCustomFilter() begin
189 // Pipeline::registerCustomFilter() end
191 // Pipeline::unregisterCustomFilter() begin
193 // Pipeline::unregisterCustomFilter() end
195 // NodeInfo::getProperty() begin
196 PlatformResult PipelineManager::getProperty(int id, const std::string& node_name,
197 const std::string& name, const std::string& type,
198 picojson::object* property) {
199 ScopeLogger("id: [%d], name [%s], nodeName [%s], type [%s] ", id, name.c_str(), node_name.c_str(),
202 auto pipeline_it = pipelines_.find(id);
203 if (pipelines_.end() == pipeline_it) {
204 LoggerD("Pipeline not found: [%d]", id);
205 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
208 return pipeline_it->second->getProperty(node_name, name, type, property);
210 // NodeInfo::getProperty() end
212 // NodeInfo::setProperty() begin
213 PlatformResult PipelineManager::setProperty(int id, const std::string& node_name,
214 const std::string& name, const std::string& type,
215 const picojson::value& property) {
216 ScopeLogger("id: [%d], name [%s], nodeName [%s], type [%s] ", id, name.c_str(), node_name.c_str(),
219 auto pipeline_it = pipelines_.find(id);
220 if (pipelines_.end() == pipeline_it) {
221 LoggerD("Pipeline not found: [%d]", id);
222 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
225 return pipeline_it->second->setProperty(node_name, name, type, property);
227 // NodeInfo::setProperty() end
229 // Source::inputTensorsInfo begin
231 // Source::inputTensorsInfo end
233 // Source::inputData() begin
235 // Source::inputData() end
237 // Switch::getPadList() begin
238 PlatformResult PipelineManager::SwitchGetPadList(int pipeline_id, const std::string& switch_name,
239 picojson::array* out) {
240 ScopeLogger("pipeline_id: [%d], switch_name: [%s]", pipeline_id, switch_name.c_str());
242 auto pipeline_it = pipelines_.find(pipeline_id);
243 if (pipelines_.end() == pipeline_it) {
244 LoggerD("Pipeline not found: [%d]", pipeline_id);
245 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
248 Switch* switch_ptr = nullptr;
249 auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr);
254 return switch_ptr->GetPadList(out);
256 // Switch::getPadList() end
258 // Switch::select() begin
259 PlatformResult PipelineManager::SwitchSelect(int pipeline_id, const std::string& switch_name,
260 const std::string& pad_name) {
261 ScopeLogger("pipeline_id: [%d], switch_name: [%s], pad_name: [%s]", pipeline_id,
262 switch_name.c_str(), pad_name.c_str());
264 auto pipeline_it = pipelines_.find(pipeline_id);
265 if (pipelines_.end() == pipeline_it) {
266 LoggerD("Pipeline not found: [%d]", pipeline_id);
267 return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
270 Switch* switch_ptr = nullptr;
271 auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr);
276 return switch_ptr->Select(pad_name);
278 // Switch::select() end
280 // Valve::setOpen() begin
282 // Valve::setOpen() end
285 } // namespace extension