1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14 #include "stream_pipeline_core.h"
15 #include "idevice_manager.h"
16 #include "ipp_node.h"
17
18 namespace OHOS::Camera {
Init()19 RetCode StreamPipelineCore::Init()
20 {
21 strategy_ = StreamPipelineStrategy::Create(context_->streamMgr_);
22 builder_ = StreamPipelineBuilder::Create(context_->streamMgr_);
23 dispatcher_ = StreamPipelineDispatcher::Create();
24 return RC_OK;
25 }
26
PreConfig(const ModeMeta & meta)27 RetCode StreamPipelineCore::PreConfig(const ModeMeta& meta)
28 {
29 auto deviceManager = IDeviceManager::GetInstance();
30 CHECK_IF_PTR_NULL_RETURN_VALUE(deviceManager, RC_ERROR);
31
32 std::vector<DeviceStreamSetting> settings = {};
33 std::vector<int32_t> ids = {};
34 context_->streamMgr_->GetStreamIds(ids);
35 for (auto i : ids) {
36 auto info = context_->streamMgr_->GetStreamInfo(i);
37 DeviceStreamSetting setting = {info.streamId_, info.bufferCount_, info.width_, info.height_,
38 info.format_, info.usage_, static_cast<CameraEncodeType>(info.encodeType_)};
39 settings.emplace_back(setting);
40 }
41 return deviceManager->PreConfig(meta, settings);
42 }
43
CreatePipeline(const int32_t & mode)44 RetCode StreamPipelineCore::CreatePipeline(const int32_t& mode)
45 {
46 std::lock_guard<std::mutex> l(mutex_);
47 std::shared_ptr<PipelineSpec> spec = strategy_->GeneratePipelineSpec(mode);
48 if (spec == nullptr) {
49 return RC_ERROR;
50 }
51 std::shared_ptr<Pipeline> pipeline = builder_->Build(spec);
52 if (pipeline == nullptr) {
53 return RC_ERROR;
54 }
55 return dispatcher_->Update(pipeline);
56 }
57
DestroyPipeline(const std::vector<int> & streamIds)58 RetCode StreamPipelineCore::DestroyPipeline(const std::vector<int>& streamIds)
59 {
60 std::lock_guard<std::mutex> l(mutex_);
61 RetCode re = RC_OK;
62 for (const auto& it : streamIds) {
63 re = dispatcher_->Destroy(it) | re;
64 re = builder_->Destroy(it) | re;
65 re = strategy_->Destroy(it) | re;
66 }
67 return re;
68 }
69
Prepare(const std::vector<int> & streamIds)70 RetCode StreamPipelineCore::Prepare(const std::vector<int>& streamIds)
71 {
72 std::lock_guard<std::mutex> l(mutex_);
73 RetCode re = RC_OK;
74 for (const auto& it : streamIds) {
75 re = dispatcher_->Prepare(it) | re;
76 }
77 return re;
78 }
79
Start(const std::vector<int> & streamIds)80 RetCode StreamPipelineCore::Start(const std::vector<int>& streamIds)
81 {
82 std::lock_guard<std::mutex> l(mutex_);
83 RetCode re = RC_OK;
84 for (const auto& it : streamIds) {
85 re = dispatcher_->Start(it) | re;
86 }
87 return re;
88 }
89
Stop(const std::vector<int> & streamIds)90 RetCode StreamPipelineCore::Stop(const std::vector<int>& streamIds)
91 {
92 RetCode re = RC_OK;
93 for (const auto& it : streamIds) {
94 CAMERA_LOGV("stop stream %{public}d begin", it);
95 re = dispatcher_->Stop(it) | re;
96 CAMERA_LOGV("stop stream %{public}d end", it);
97 }
98 return re;
99 }
100
Config(const std::vector<int> & streamIds,const CaptureMeta & meta)101 RetCode StreamPipelineCore::Config(const std::vector<int>& streamIds, const CaptureMeta& meta)
102 {
103 std::lock_guard<std::mutex> l(mutex_);
104 RetCode re = RC_OK;
105 for (const auto& it : streamIds) {
106 re = dispatcher_->Config(it, meta) | re;
107 }
108 return re;
109 }
110
Capture(const std::vector<int> & streamIds,const int32_t captureId)111 RetCode StreamPipelineCore::Capture(const std::vector<int>& streamIds, const int32_t captureId)
112 {
113 std::lock_guard<std::mutex> l(mutex_);
114 RetCode re = RC_OK;
115 for (const auto& it : streamIds) {
116 re = dispatcher_->Capture(it, captureId) | re;
117 }
118 return re;
119 }
120
CancelCapture(const std::vector<int> & streamIds)121 RetCode StreamPipelineCore::CancelCapture(const std::vector<int>& streamIds)
122 {
123 std::lock_guard<std::mutex> l(mutex_);
124 RetCode re = RC_OK;
125 for (const auto& it : streamIds) {
126 re = dispatcher_->CancelCapture(it) | re;
127 }
128 return re;
129 }
130
Flush(const std::vector<int> & streamIds)131 RetCode StreamPipelineCore::Flush(const std::vector<int>& streamIds)
132 {
133 std::lock_guard<std::mutex> l(mutex_);
134 RetCode re = RC_OK;
135 for (const auto& it : streamIds) {
136 CAMERA_LOGV("flush stream %{public}d begin", it);
137 re = dispatcher_->Flush(it) | re;
138 CAMERA_LOGV("flush stream %{public}d end", it);
139 }
140 return re;
141 }
142
GetOfflinePipeline(const int32_t id)143 std::shared_ptr<OfflinePipeline> StreamPipelineCore::GetOfflinePipeline(const int32_t id)
144 {
145 std::lock_guard<std::mutex> l(mutex_);
146 std::shared_ptr<INode> node = dispatcher_->GetNode(id, "ipp");
147 return std::static_pointer_cast<IppNode>(node);
148 }
149
GetCurrentMode() const150 OperationMode StreamPipelineCore::GetCurrentMode() const
151 {
152 return mode_;
153 }
154
CheckStreamsSupported(OperationMode mode,const ModeMeta & meta,const std::vector<StreamConfiguration> & configs)155 DynamicStreamSwitchMode StreamPipelineCore::CheckStreamsSupported(OperationMode mode,
156 const ModeMeta& meta,
157 const std::vector<StreamConfiguration>& configs)
158 {
159 // check metadata
160 CHECK_IF_PTR_NULL_RETURN_VALUE(meta, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
161 CHECK_IF_EQUAL_RETURN_VALUE(configs.empty(), true, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
162
163 std::vector<DeviceStreamSetting> settings = {};
164 std::vector<int32_t> ids = {};
165 context_->streamMgr_->GetStreamIds(ids);
166
167 std::vector<int32_t> types = {};
168 for (const auto it : configs) {
169 types.emplace_back(static_cast<std::underlying_type<StreamIntent>::type>(it.type));
170 }
171 std::sort(types.begin(), types.end(), [](const int32_t& f, const int32_t& n) { return f < n; });
172
173 bool isSupport = strategy_->CheckPipelineSpecExist(mode, types) == RC_OK ? true : false;
174 if (isSupport && !ids.empty()) {
175 return DYNAMIC_STREAM_SWITCH_NEED_INNER_RESTART;
176 } else if (isSupport && ids.empty()) {
177 return DYNAMIC_STREAM_SWITCH_SUPPORT;
178 } else {
179 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
180 }
181
182 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
183 }
184
Create(const std::shared_ptr<NodeContext> & c)185 std::shared_ptr<IStreamPipelineCore> IStreamPipelineCore::Create(const std::shared_ptr<NodeContext>& c)
186 {
187 return std::make_shared<StreamPipelineCore>(c);
188 }
189 } // namespace OHOS::Camera
190