• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hstream_depth_data.h"
17 
18 #include <cstdint>
19 
20 #include "camera_device_ability_items.h"
21 #include "camera_log.h"
22 #include "camera_metadata_operator.h"
23 #include "camera_util.h"
24 #include "hstream_common.h"
25 #include "ipc_skeleton.h"
26 #include "istream_depth_data_callback.h"
27 #include "metadata_utils.h"
28 #include "camera_report_uitls.h"
29 
30 namespace OHOS {
31 namespace CameraStandard {
32 using namespace OHOS::HDI::Camera::V1_0;
HStreamDepthData(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)33 HStreamDepthData::HStreamDepthData(
34     sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
35     : HStreamCommon(StreamType::DEPTH, producer, format, width, height)
36 {
37     MEDIA_INFO_LOG("HStreamDepthData::HStreamDepthData construct, format:%{public}d, size:%{public}dx%{public}d, "
38         "streamId:%{public}d",
39         format, width, height, GetFwkStreamId());
40 }
41 
~HStreamDepthData()42 HStreamDepthData::~HStreamDepthData()
43 {
44     MEDIA_INFO_LOG("HStreamDepthData::~HStreamDepthData deconstruct, format:%{public}d size:%{public}dx%{public}d "
45                    "streamId:%{public}d, hdiStreamId:%{public}d",
46         format_, width_, height_, GetFwkStreamId(), GetHdiStreamId());
47 }
48 
LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)49 int32_t HStreamDepthData::LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
50     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
51 {
52     MEDIA_INFO_LOG("HStreamDepthData::LinkInput streamId:%{public}d", GetFwkStreamId());
53     int32_t ret = HStreamCommon::LinkInput(streamOperator, cameraAbility);
54     CHECK_RETURN_RET_ELOG(ret != CAMERA_OK, ret,
55         "HStreamDepthData::LinkInput err, streamId:%{public}d ,err:%{public}d", GetFwkStreamId(), ret);
56     return CAMERA_OK;
57 }
58 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)59 void HStreamDepthData::SetStreamInfo(StreamInfo_V1_1& streamInfo)
60 {
61     HStreamCommon::SetStreamInfo(streamInfo);
62     streamInfo.v1_0.intent_ =
63         static_cast<OHOS::HDI::Camera::V1_0::StreamIntent>(OHOS::HDI::Camera::V1_3::StreamType::STREAM_TYPE_DEPTH);
64 }
65 
SetDataAccuracy(int32_t accuracy)66 int32_t HStreamDepthData::SetDataAccuracy(int32_t accuracy)
67 {
68     MEDIA_INFO_LOG("HStreamDepthData::SetDataAccuracy accuracy: %{public}d", accuracy);
69     streamDepthDataAccuracy_ = {accuracy};
70     std::vector<uint8_t> ability;
71     std::vector<uint8_t> depthSettings;
72     {
73         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
74         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
75         std::shared_ptr<OHOS::Camera::CameraMetadata> dynamicSetting = nullptr;
76         OHOS::Camera::MetadataUtils::ConvertVecToMetadata(ability, dynamicSetting);
77         if (dynamicSetting == nullptr) {
78             dynamicSetting = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
79         }
80         camera_metadata_item_t item;
81         CHECK_RETURN_RET_ELOG(dynamicSetting == nullptr, CAMERA_INVALID_ARG,
82             "HStreamDepthData::SetDataAccuracy dynamicSetting is nullptr.");
83         int ret = OHOS::Camera::FindCameraMetadataItem(dynamicSetting->get(), OHOS_CONTROL_DEPTH_DATA_ACCURACY, &item);
84         bool status = false;
85         if (ret == CAM_META_ITEM_NOT_FOUND) {
86             MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy Failed to find data accuracy");
87             status = dynamicSetting->addEntry(
88                 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
89         } else if (ret == CAM_META_SUCCESS) {
90             MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy success to find data accuracy");
91             status = dynamicSetting->updateEntry(
92                 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
93         }
94         CHECK_PRINT_ELOG(!status, "HStreamDepthData::SetDataAccuracy Failed to set data accuracy");
95         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(dynamicSetting, depthSettings);
96     }
97 
98     auto streamOperator = GetStreamOperator();
99 
100     CamRetCode rc = HDI::Camera::V1_0::NO_ERROR;
101     if (streamOperator != nullptr) {
102         std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
103         CaptureInfo captureInfo;
104         captureInfo.streamIds_ = {GetHdiStreamId()};
105         captureInfo.captureSetting_ = depthSettings;
106         captureInfo.enableShutterCallback_ = false;
107         int32_t currentCaptureId = GetPreparedCaptureId();
108         MEDIA_INFO_LOG("HStreamDepthData::SetDataAccuracy stream:%{public}d, with settingCapture ID:%{public}d",
109             GetFwkStreamId(), currentCaptureId);
110         rc = (CamRetCode)(streamOperator->Capture(currentCaptureId, captureInfo, true));
111         CHECK_PRINT_ELOG(rc != HDI::Camera::V1_0::NO_ERROR,
112             "HStreamDepthData::SetDataAccuracy Failed with error Code:%{public}d", rc);
113     }
114     return rc;
115 }
116 
Start()117 int32_t HStreamDepthData::Start()
118 {
119     CAMERA_SYNC_TRACE;
120     auto streamOperator = GetStreamOperator();
121     CHECK_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
122 
123     auto preparedCaptureId = GetPreparedCaptureId();
124     CHECK_RETURN_RET_ELOG(preparedCaptureId != CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
125         "HStreamDepthData::Start, Already started with captureID: %{public}d", preparedCaptureId);
126 
127     int32_t ret = PrepareCaptureId();
128     preparedCaptureId = GetPreparedCaptureId();
129     CHECK_RETURN_RET_ELOG(ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET, ret,
130         "HStreamDepthData::Start Failed to allocate a captureId");
131 
132     std::vector<uint8_t> ability;
133     {
134         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
135         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
136     }
137 
138     CaptureInfo captureInfo;
139     captureInfo.streamIds_ = { GetHdiStreamId() };
140     captureInfo.captureSetting_ = ability;
141     captureInfo.enableShutterCallback_ = false;
142     MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
143         GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
144 
145     std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
146     HStreamCommon::PrintCaptureDebugLog(cameraAbility_);
147     CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfo, true));
148     if (rc != HDI::Camera::V1_0::NO_ERROR) {
149         ResetCaptureId();
150         MEDIA_ERR_LOG("HStreamDepthData::Start Failed with error Code:%{public}d", rc);
151         CameraReportUtils::ReportCameraError(
152             "HStreamDepthData::Start", rc, true, CameraReportUtils::GetCallerInfo());
153         ret = HdiToServiceError(rc);
154     } else {
155         depthDataStreamStatus_ = DepthDataStreamStatus::STARTED;
156     }
157 
158     return ret;
159 }
160 
Stop()161 int32_t HStreamDepthData::Stop()
162 {
163     CAMERA_SYNC_TRACE;
164     auto streamOperator = GetStreamOperator();
165     if (streamOperator == nullptr) {
166         MEDIA_INFO_LOG("HStreamDepthData::Stop streamOperator is null");
167         return CAMERA_INVALID_STATE;
168     }
169     auto preparedCaptureId = GetPreparedCaptureId();
170     MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
171         GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
172     CHECK_RETURN_RET_ELOG(preparedCaptureId == CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
173         "HStreamDepthData::Stop, Stream not started yet");
174     int32_t ret = CAMERA_OK;
175     {
176         std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
177         ret = StopStream();
178         if (ret != CAMERA_OK) {
179             MEDIA_ERR_LOG("HStreamDepthData::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
180                           ret, preparedCaptureId);
181         } else {
182             depthDataStreamStatus_ = DepthDataStreamStatus::STOPED;
183         }
184     }
185     return ret;
186 }
187 
Release()188 int32_t HStreamDepthData::Release()
189 {
190     return ReleaseStream(false);
191 }
192 
ReleaseStream(bool isDelay)193 int32_t HStreamDepthData::ReleaseStream(bool isDelay)
194 {
195     {
196         std::lock_guard<std::mutex> lock(callbackLock_);
197         streamDepthDataCallback_ = nullptr;
198     }
199     return HStreamCommon::ReleaseStream(isDelay);
200 }
201 
SetCallback(const sptr<IStreamDepthDataCallback> & callback)202 int32_t HStreamDepthData::SetCallback(const sptr<IStreamDepthDataCallback>& callback)
203 {
204     CHECK_RETURN_RET_ELOG(callback == nullptr, CAMERA_INVALID_ARG,
205         "HStreamDepthData::SetCallback callback is null");
206     std::lock_guard<std::mutex> lock(callbackLock_);
207     streamDepthDataCallback_ = callback;
208     return CAMERA_OK;
209 }
210 
UnSetCallback()211 int32_t HStreamDepthData::UnSetCallback()
212 {
213     std::lock_guard<std::mutex> lock(callbackLock_);
214     streamDepthDataCallback_ = nullptr;
215     return CAMERA_OK;
216 }
217 
OnDepthDataError(int32_t errorType)218 int32_t HStreamDepthData::OnDepthDataError(int32_t errorType)
219 {
220     std::lock_guard<std::mutex> lock(callbackLock_);
221     if (streamDepthDataCallback_ != nullptr) {
222         int32_t depthDataErrorCode;
223         if (errorType == BUFFER_LOST) {
224             depthDataErrorCode = CAMERA_STREAM_BUFFER_LOST;
225         } else {
226             depthDataErrorCode = CAMERA_UNKNOWN_ERROR;
227         }
228         CAMERA_SYSEVENT_FAULT(CreateMsg("Depth OnDepthDataError! errorCode:%d", depthDataErrorCode));
229         streamDepthDataCallback_->OnDepthDataError(depthDataErrorCode);
230     }
231     return CAMERA_OK;
232 }
233 
DumpStreamInfo(CameraInfoDumper & infoDumper)234 void HStreamDepthData::DumpStreamInfo(CameraInfoDumper& infoDumper)
235 {
236     infoDumper.Title("depth stream");
237     HStreamCommon::DumpStreamInfo(infoDumper);
238 }
239 
OperatePermissionCheck(uint32_t interfaceCode)240 int32_t HStreamDepthData::OperatePermissionCheck(uint32_t interfaceCode)
241 {
242     switch (static_cast<IStreamDepthDataIpcCode>(interfaceCode)) {
243         case IStreamDepthDataIpcCode::COMMAND_START: {
244             auto callerToken = IPCSkeleton::GetCallingTokenID();
245             CHECK_RETURN_RET_ELOG(callerToken_ != callerToken, CAMERA_OPERATION_NOT_ALLOWED,
246                 "HStreamDepthData::OperatePermissionCheck fail, callerToken invalid!");
247             break;
248         }
249         default:
250             break;
251     }
252     return CAMERA_OK;
253 }
254 
CallbackEnter(uint32_t code)255 int32_t HStreamDepthData::CallbackEnter([[maybe_unused]] uint32_t code)
256 {
257     MEDIA_DEBUG_LOG("start, code:%{public}u", code);
258     DisableJeMalloc();
259     return OperatePermissionCheck(code);
260 }
261 
CallbackExit(uint32_t code,int32_t result)262 int32_t HStreamDepthData::CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
263 {
264     MEDIA_DEBUG_LOG("leave, code:%{public}u, result:%{public}d", code, result);
265     return CAMERA_OK;
266 }
267 } // namespace CameraStandard
268 } // namespace OHOS
269