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::recursive_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::recursive_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::recursive_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::recursive_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
SetCallback(const MetaDataCb cb)90 RetCode StreamPipelineCore::SetCallback(const MetaDataCb cb)
91 {
92 CAMERA_LOGE("StreamPipelineCore %{public}s: line: %{public}d", __FUNCTION__, __LINE__);
93 std::lock_guard<std::recursive_mutex> l(mutex_);
94 RetCode re = RC_OK;
95 dispatcher_->SetCallback(cb);
96 return re;
97 }
98
Stop(const std::vector<int> & streamIds)99 RetCode StreamPipelineCore::Stop(const std::vector<int>& streamIds)
100 {
101 RetCode re = RC_OK;
102 for (const auto& it : streamIds) {
103 CAMERA_LOGV("stop stream %{public}d begin", it);
104 re = dispatcher_->Stop(it) | re;
105 CAMERA_LOGV("stop stream %{public}d end", it);
106 }
107 return re;
108 }
109
Config(const std::vector<int> & streamIds,const CaptureMeta & meta)110 RetCode StreamPipelineCore::Config(const std::vector<int>& streamIds, const CaptureMeta& meta)
111 {
112 std::lock_guard<std::recursive_mutex> l(mutex_);
113 RetCode re = RC_OK;
114 for (const auto& it : streamIds) {
115 re = dispatcher_->Config(it, meta) | re;
116 }
117 return re;
118 }
119
UpdateSettingsConfig(const CaptureMeta & meta)120 RetCode StreamPipelineCore::UpdateSettingsConfig(const CaptureMeta& meta)
121 {
122 CAMERA_LOGE("StreamPipelineCore %{public}s: line: %{public}d", __FUNCTION__, __LINE__);
123 std::lock_guard<std::recursive_mutex> l(mutex_);
124 RetCode re = RC_OK;
125 dispatcher_->UpdateSettingsConfig(meta);
126 return re;
127 }
128
Capture(const std::vector<int> & streamIds,const int32_t captureId)129 RetCode StreamPipelineCore::Capture(const std::vector<int>& streamIds, const int32_t captureId)
130 {
131 std::lock_guard<std::recursive_mutex> l(mutex_);
132 RetCode re = RC_OK;
133 for (const auto& it : streamIds) {
134 re = dispatcher_->Capture(it, captureId) | re;
135 }
136 return re;
137 }
138
CancelCapture(const std::vector<int> & streamIds)139 RetCode StreamPipelineCore::CancelCapture(const std::vector<int>& streamIds)
140 {
141 std::lock_guard<std::recursive_mutex> l(mutex_);
142 RetCode re = RC_OK;
143 for (const auto& it : streamIds) {
144 re = dispatcher_->CancelCapture(it) | re;
145 }
146 return re;
147 }
148
Flush(const std::vector<int> & streamIds)149 RetCode StreamPipelineCore::Flush(const std::vector<int>& streamIds)
150 {
151 std::lock_guard<std::recursive_mutex> l(mutex_);
152 RetCode re = RC_OK;
153 for (const auto& it : streamIds) {
154 CAMERA_LOGV("flush stream %{public}d begin", it);
155 re = dispatcher_->Flush(it) | re;
156 CAMERA_LOGV("flush stream %{public}d end", it);
157 }
158 return re;
159 }
160
GetOfflinePipeline(const int32_t id)161 std::shared_ptr<OfflinePipeline> StreamPipelineCore::GetOfflinePipeline(const int32_t id)
162 {
163 std::lock_guard<std::recursive_mutex> l(mutex_);
164 std::shared_ptr<INode> node = dispatcher_->GetNode(id, "ipp");
165 return std::static_pointer_cast<IppNode>(node);
166 }
167
GetCurrentMode() const168 OperationMode StreamPipelineCore::GetCurrentMode() const
169 {
170 return mode_;
171 }
172
CheckStreamsSupported(OperationMode mode,const ModeMeta & meta,const std::vector<StreamConfiguration> & configs)173 DynamicStreamSwitchMode StreamPipelineCore::CheckStreamsSupported(OperationMode mode,
174 const ModeMeta& meta,
175 const std::vector<StreamConfiguration>& configs)
176 {
177 // check metadata
178 CHECK_IF_PTR_NULL_RETURN_VALUE(meta, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
179 CHECK_IF_EQUAL_RETURN_VALUE(configs.empty(), true, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
180
181 std::vector<DeviceStreamSetting> settings = {};
182 std::vector<int32_t> ids = {};
183 context_->streamMgr_->GetStreamIds(ids);
184
185 std::vector<int32_t> types = {};
186 std::transform(configs.begin(), configs.end(), std::back_inserter(types),
187 [](auto &it) { return static_cast<std::underlying_type<StreamIntent>::type>(it.type); });
188 std::sort(types.begin(), types.end(), [](const int32_t& f, const int32_t& n) { return f < n; });
189
190 bool isSupport = strategy_->CheckPipelineSpecExist(mode, types) == RC_OK ? true : false;
191 if (isSupport && !ids.empty()) {
192 return DYNAMIC_STREAM_SWITCH_NEED_INNER_RESTART;
193 } else if (isSupport && ids.empty()) {
194 return DYNAMIC_STREAM_SWITCH_SUPPORT;
195 } else {
196 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
197 }
198
199 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
200 }
201
Create(const std::shared_ptr<NodeContext> & c)202 std::shared_ptr<IStreamPipelineCore> IStreamPipelineCore::Create(const std::shared_ptr<NodeContext>& c)
203 {
204 return std::make_shared<StreamPipelineCore>(c);
205 }
206 } // namespace OHOS::Camera
207