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(const std::string & cameraId)19 RetCode StreamPipelineCore::Init(const std::string &cameraId)
20 {
21 strategy_ = StreamPipelineStrategy::Create(context_->streamMgr_);
22 builder_ = StreamPipelineBuilder::Create(context_->streamMgr_);
23 dispatcher_ = StreamPipelineDispatcher::Create();
24 cameraId_ = cameraId;
25 return RC_OK;
26 }
27
PreConfig(const ModeMeta & meta)28 RetCode StreamPipelineCore::PreConfig(const ModeMeta& meta)
29 {
30 auto deviceManager = IDeviceManager::GetInstance();
31 CHECK_IF_PTR_NULL_RETURN_VALUE(deviceManager, RC_ERROR);
32
33 std::vector<DeviceStreamSetting> settings = {};
34 std::vector<int32_t> ids = {};
35 context_->streamMgr_->GetStreamIds(ids);
36 for (auto i : ids) {
37 auto info = context_->streamMgr_->GetStreamInfo(i);
38 DeviceStreamSetting setting = {info.streamId_, info.bufferCount_, info.width_, info.height_,
39 info.format_, info.usage_, static_cast<CameraEncodeType>(info.encodeType_)};
40 settings.emplace_back(setting);
41 }
42 return deviceManager->PreConfig(meta, settings);
43 }
44
CreatePipeline(const int32_t & mode)45 RetCode StreamPipelineCore::CreatePipeline(const int32_t& mode)
46 {
47 std::lock_guard<std::recursive_mutex> l(mutex_);
48 std::shared_ptr<PipelineSpec> spec = strategy_->GeneratePipelineSpec(mode);
49 if (spec == nullptr) {
50 return RC_ERROR;
51 }
52 std::shared_ptr<Pipeline> pipeline = builder_->Build(spec, cameraId_);
53 if (pipeline == nullptr) {
54 return RC_ERROR;
55 }
56 return dispatcher_->Update(pipeline);
57 }
58
DestroyPipeline(const std::vector<int> & streamIds)59 RetCode StreamPipelineCore::DestroyPipeline(const std::vector<int>& streamIds)
60 {
61 std::lock_guard<std::recursive_mutex> l(mutex_);
62 RetCode re = RC_OK;
63 for (const auto& it : streamIds) {
64 re = dispatcher_->Destroy(it) | re;
65 re = builder_->Destroy(it) | re;
66 re = strategy_->Destroy(it) | re;
67 }
68 return re;
69 }
70
Prepare(const std::vector<int> & streamIds)71 RetCode StreamPipelineCore::Prepare(const std::vector<int>& streamIds)
72 {
73 std::lock_guard<std::recursive_mutex> l(mutex_);
74 RetCode re = RC_OK;
75 for (const auto& it : streamIds) {
76 re = dispatcher_->Prepare(it) | re;
77 }
78 return re;
79 }
80
Start(const std::vector<int> & streamIds)81 RetCode StreamPipelineCore::Start(const std::vector<int>& streamIds)
82 {
83 std::lock_guard<std::recursive_mutex> l(mutex_);
84 RetCode re = RC_OK;
85 for (const auto& it : streamIds) {
86 re = dispatcher_->Start(it) | re;
87 }
88 return re;
89 }
90
SetCallback(const MetaDataCb cb)91 RetCode StreamPipelineCore::SetCallback(const MetaDataCb cb)
92 {
93 CAMERA_LOGE("StreamPipelineCore %{public}s: line: %{public}d", __FUNCTION__, __LINE__);
94 std::lock_guard<std::recursive_mutex> l(mutex_);
95 RetCode re = RC_OK;
96 dispatcher_->SetCallback(cb);
97 return re;
98 }
99
Stop(const std::vector<int> & streamIds)100 RetCode StreamPipelineCore::Stop(const std::vector<int>& streamIds)
101 {
102 RetCode re = RC_OK;
103 for (const auto& it : streamIds) {
104 CAMERA_LOGV("stop stream %{public}d begin", it);
105 re = dispatcher_->Stop(it) | re;
106 CAMERA_LOGV("stop stream %{public}d end", it);
107 }
108 return re;
109 }
110
Config(const std::vector<int> & streamIds,const CaptureMeta & meta)111 RetCode StreamPipelineCore::Config(const std::vector<int>& streamIds, const CaptureMeta& meta)
112 {
113 std::lock_guard<std::recursive_mutex> l(mutex_);
114 RetCode re = RC_OK;
115 for (const auto& it : streamIds) {
116 re = dispatcher_->Config(it, meta) | re;
117 }
118 return re;
119 }
120
Capture(const std::vector<int> & streamIds,const int32_t captureId)121 RetCode StreamPipelineCore::Capture(const std::vector<int>& streamIds, const int32_t captureId)
122 {
123 std::lock_guard<std::recursive_mutex> l(mutex_);
124 RetCode re = RC_OK;
125 for (const auto& it : streamIds) {
126 re = dispatcher_->Capture(it, captureId) | re;
127 }
128 return re;
129 }
130
CancelCapture(const std::vector<int> & streamIds)131 RetCode StreamPipelineCore::CancelCapture(const std::vector<int>& streamIds)
132 {
133 std::lock_guard<std::recursive_mutex> l(mutex_);
134 RetCode re = RC_OK;
135 for (const auto& it : streamIds) {
136 re = dispatcher_->CancelCapture(it) | re;
137 }
138 return re;
139 }
140
Flush(const std::vector<int> & streamIds)141 RetCode StreamPipelineCore::Flush(const std::vector<int>& streamIds)
142 {
143 std::lock_guard<std::recursive_mutex> l(mutex_);
144 RetCode re = RC_OK;
145 for (const auto& it : streamIds) {
146 CAMERA_LOGV("flush stream %{public}d begin", it);
147 re = dispatcher_->Flush(it) | re;
148 CAMERA_LOGV("flush stream %{public}d end", it);
149 }
150 return re;
151 }
152
GetOfflinePipeline(const int32_t id)153 std::shared_ptr<OfflinePipeline> StreamPipelineCore::GetOfflinePipeline(const int32_t id)
154 {
155 std::lock_guard<std::recursive_mutex> l(mutex_);
156 std::shared_ptr<INode> node = dispatcher_->GetNode(id, "ipp");
157 return std::static_pointer_cast<IppNode>(node);
158 }
159
GetCurrentMode() const160 VdiOperationMode StreamPipelineCore::GetCurrentMode() const
161 {
162 return mode_;
163 }
164
CheckStreamsSupported(VdiOperationMode mode,const ModeMeta & meta,const std::vector<StreamConfiguration> & configs)165 DynamicStreamSwitchMode StreamPipelineCore::CheckStreamsSupported(VdiOperationMode mode,
166 const ModeMeta& meta,
167 const std::vector<StreamConfiguration>& configs)
168 {
169 // check metadata
170 CHECK_IF_PTR_NULL_RETURN_VALUE(meta, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
171 CHECK_IF_EQUAL_RETURN_VALUE(configs.empty(), true, DYNAMIC_STREAM_SWITCH_NOT_SUPPORT);
172
173 std::vector<DeviceStreamSetting> settings = {};
174 std::vector<int32_t> ids = {};
175 context_->streamMgr_->GetStreamIds(ids);
176
177 std::vector<int32_t> types = {};
178 std::transform(configs.begin(), configs.end(), std::back_inserter(types),
179 [](auto &it) { return static_cast<std::underlying_type<VdiStreamIntent>::type>(it.type); });
180 std::sort(types.begin(), types.end(), [](const int32_t& f, const int32_t& n) { return f < n; });
181
182 bool isSupport = strategy_->CheckPipelineSpecExist(mode, types) == RC_OK ? true : false;
183 if (isSupport && !ids.empty()) {
184 return DYNAMIC_STREAM_SWITCH_NEED_INNER_RESTART;
185 } else if (isSupport && ids.empty()) {
186 return DYNAMIC_STREAM_SWITCH_SUPPORT;
187 } else {
188 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
189 }
190
191 return DYNAMIC_STREAM_SWITCH_NOT_SUPPORT;
192 }
193
Create(const std::shared_ptr<NodeContext> & c)194 std::shared_ptr<IStreamPipelineCore> IStreamPipelineCore::Create(const std::shared_ptr<NodeContext>& c)
195 {
196 return std::make_shared<StreamPipelineCore>(c);
197 }
198 } // namespace OHOS::Camera
199