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