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_service_ipc_interface_code.h"
24 #include "camera_util.h"
25 #include "hstream_common.h"
26 #include "ipc_skeleton.h"
27 #include "istream_depth_data_callback.h"
28 #include "metadata_utils.h"
29 #include "camera_report_uitls.h"
30
31 namespace OHOS {
32 namespace CameraStandard {
33 using namespace OHOS::HDI::Camera::V1_0;
HStreamDepthData(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)34 HStreamDepthData::HStreamDepthData(
35 sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
36 : HStreamCommon(StreamType::DEPTH, producer, format, width, height)
37 {
38 MEDIA_INFO_LOG("HStreamDepthData::HStreamDepthData construct, format:%{public}d, size:%{public}dx%{public}d, "
39 "streamId:%{public}d",
40 format, width, height, GetFwkStreamId());
41 }
42
~HStreamDepthData()43 HStreamDepthData::~HStreamDepthData()
44 {
45 MEDIA_INFO_LOG("HStreamDepthData::~HStreamDepthData deconstruct, format:%{public}d size:%{public}dx%{public}d "
46 "streamId:%{public}d, hdiStreamId:%{public}d",
47 format_, width_, height_, GetFwkStreamId(), GetHdiStreamId());
48 }
49
LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)50 int32_t HStreamDepthData::LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
51 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
52 {
53 MEDIA_INFO_LOG("HStreamDepthData::LinkInput streamId:%{public}d", GetFwkStreamId());
54 int32_t ret = HStreamCommon::LinkInput(streamOperator, cameraAbility);
55 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, ret,
56 "HStreamDepthData::LinkInput err, streamId:%{public}d ,err:%{public}d", GetFwkStreamId(), ret);
57 return CAMERA_OK;
58 }
59
SetStreamInfo(StreamInfo_V1_1 & streamInfo)60 void HStreamDepthData::SetStreamInfo(StreamInfo_V1_1& streamInfo)
61 {
62 HStreamCommon::SetStreamInfo(streamInfo);
63 streamInfo.v1_0.intent_ =
64 static_cast<OHOS::HDI::Camera::V1_0::StreamIntent>(OHOS::HDI::Camera::V1_3::StreamType::STREAM_TYPE_DEPTH);
65 }
66
SetDataAccuracy(int32_t accuracy)67 int32_t HStreamDepthData::SetDataAccuracy(int32_t accuracy)
68 {
69 MEDIA_INFO_LOG("HStreamDepthData::SetDataAccuracy accuracy: %{public}d", accuracy);
70 streamDepthDataAccuracy_ = {accuracy};
71 std::vector<uint8_t> ability;
72 std::vector<uint8_t> depthSettings;
73 {
74 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
75 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
76 std::shared_ptr<OHOS::Camera::CameraMetadata> dynamicSetting = nullptr;
77 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(ability, dynamicSetting);
78 if (dynamicSetting == nullptr) {
79 dynamicSetting = std::make_shared<OHOS::Camera::CameraMetadata>(0, 0);
80 }
81 camera_metadata_item_t item;
82 CHECK_ERROR_RETURN_RET_LOG(dynamicSetting == nullptr, CAMERA_INVALID_ARG,
83 "HStreamDepthData::SetDataAccuracy dynamicSetting is nullptr.");
84 int ret = OHOS::Camera::FindCameraMetadataItem(dynamicSetting->get(), OHOS_CONTROL_DEPTH_DATA_ACCURACY, &item);
85 bool status = false;
86 if (ret == CAM_META_ITEM_NOT_FOUND) {
87 MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy Failed to find data accuracy");
88 status = dynamicSetting->addEntry(
89 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
90 } else if (ret == CAM_META_SUCCESS) {
91 MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy success to find data accuracy");
92 status = dynamicSetting->updateEntry(
93 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
94 }
95 CHECK_ERROR_PRINT_LOG(!status, "HStreamDepthData::SetDataAccuracy Failed to set data accuracy");
96 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(dynamicSetting, depthSettings);
97 }
98
99 auto streamOperator = GetStreamOperator();
100
101 CamRetCode rc = HDI::Camera::V1_0::NO_ERROR;
102 if (streamOperator != nullptr) {
103 std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
104 CaptureInfo captureInfo;
105 captureInfo.streamIds_ = {GetHdiStreamId()};
106 captureInfo.captureSetting_ = depthSettings;
107 captureInfo.enableShutterCallback_ = false;
108 int32_t currentCaptureId = GetPreparedCaptureId();
109 MEDIA_INFO_LOG("HStreamDepthData::SetDataAccuracy stream:%{public}d, with settingCapture ID:%{public}d",
110 GetFwkStreamId(), currentCaptureId);
111 rc = (CamRetCode)(streamOperator->Capture(currentCaptureId, captureInfo, true));
112 CHECK_ERROR_PRINT_LOG(rc != HDI::Camera::V1_0::NO_ERROR,
113 "HStreamDepthData::SetDataAccuracy Failed with error Code:%{public}d", rc);
114 }
115 return rc;
116 }
117
Start()118 int32_t HStreamDepthData::Start()
119 {
120 CAMERA_SYNC_TRACE;
121 auto streamOperator = GetStreamOperator();
122 CHECK_ERROR_RETURN_RET(streamOperator == nullptr, CAMERA_INVALID_STATE);
123
124 auto preparedCaptureId = GetPreparedCaptureId();
125 CHECK_ERROR_RETURN_RET_LOG(preparedCaptureId != CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
126 "HStreamDepthData::Start, Already started with captureID: %{public}d", preparedCaptureId);
127
128 int32_t ret = PrepareCaptureId();
129 preparedCaptureId = GetPreparedCaptureId();
130 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET, ret,
131 "HStreamDepthData::Start Failed to allocate a captureId");
132
133 std::vector<uint8_t> ability;
134 {
135 std::lock_guard<std::mutex> lock(cameraAbilityLock_);
136 OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
137 }
138
139 CaptureInfo captureInfo;
140 captureInfo.streamIds_ = { GetHdiStreamId() };
141 captureInfo.captureSetting_ = ability;
142 captureInfo.enableShutterCallback_ = false;
143 MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
144 GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
145
146 std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
147 HStreamCommon::PrintCaptureDebugLog(cameraAbility_);
148 CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfo, true));
149 if (rc != HDI::Camera::V1_0::NO_ERROR) {
150 ResetCaptureId();
151 MEDIA_ERR_LOG("HStreamDepthData::Start Failed with error Code:%{public}d", rc);
152 CameraReportUtils::ReportCameraError(
153 "HStreamDepthData::Start", rc, true, CameraReportUtils::GetCallerInfo());
154 ret = HdiToServiceError(rc);
155 } else {
156 depthDataStreamStatus_ = DepthDataStreamStatus::STARTED;
157 }
158
159 return ret;
160 }
161
Stop()162 int32_t HStreamDepthData::Stop()
163 {
164 CAMERA_SYNC_TRACE;
165 auto streamOperator = GetStreamOperator();
166 if (streamOperator == nullptr) {
167 MEDIA_INFO_LOG("HStreamDepthData::Stop streamOperator is null");
168 return CAMERA_INVALID_STATE;
169 }
170 auto preparedCaptureId = GetPreparedCaptureId();
171 MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
172 GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
173 CHECK_ERROR_RETURN_RET_LOG(preparedCaptureId == CAPTURE_ID_UNSET, CAMERA_INVALID_STATE,
174 "HStreamDepthData::Stop, Stream not started yet");
175 int32_t ret = CAMERA_OK;
176 {
177 std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
178 ret = StopStream();
179 if (ret != CAMERA_OK) {
180 MEDIA_ERR_LOG("HStreamDepthData::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
181 ret, preparedCaptureId);
182 } else {
183 depthDataStreamStatus_ = DepthDataStreamStatus::STOPED;
184 }
185 }
186 return ret;
187 }
188
Release()189 int32_t HStreamDepthData::Release()
190 {
191 return ReleaseStream(false);
192 }
193
ReleaseStream(bool isDelay)194 int32_t HStreamDepthData::ReleaseStream(bool isDelay)
195 {
196 {
197 std::lock_guard<std::mutex> lock(callbackLock_);
198 streamDepthDataCallback_ = nullptr;
199 }
200 return HStreamCommon::ReleaseStream(isDelay);
201 }
202
SetCallback(sptr<IStreamDepthDataCallback> & callback)203 int32_t HStreamDepthData::SetCallback(sptr<IStreamDepthDataCallback>& callback)
204 {
205 CHECK_ERROR_RETURN_RET_LOG(callback == nullptr, CAMERA_INVALID_ARG,
206 "HStreamDepthData::SetCallback callback is null");
207 std::lock_guard<std::mutex> lock(callbackLock_);
208 streamDepthDataCallback_ = callback;
209 return CAMERA_OK;
210 }
211
UnSetCallback()212 int32_t HStreamDepthData::UnSetCallback()
213 {
214 std::lock_guard<std::mutex> lock(callbackLock_);
215 streamDepthDataCallback_ = nullptr;
216 return CAMERA_OK;
217 }
218
OnDepthDataError(int32_t errorType)219 int32_t HStreamDepthData::OnDepthDataError(int32_t errorType)
220 {
221 std::lock_guard<std::mutex> lock(callbackLock_);
222 if (streamDepthDataCallback_ != nullptr) {
223 int32_t depthDataErrorCode;
224 if (errorType == BUFFER_LOST) {
225 depthDataErrorCode = CAMERA_STREAM_BUFFER_LOST;
226 } else {
227 depthDataErrorCode = CAMERA_UNKNOWN_ERROR;
228 }
229 CAMERA_SYSEVENT_FAULT(CreateMsg("Depth OnDepthDataError! errorCode:%d", depthDataErrorCode));
230 streamDepthDataCallback_->OnDepthDataError(depthDataErrorCode);
231 }
232 return CAMERA_OK;
233 }
234
DumpStreamInfo(CameraInfoDumper & infoDumper)235 void HStreamDepthData::DumpStreamInfo(CameraInfoDumper& infoDumper)
236 {
237 infoDumper.Title("depth stream");
238 HStreamCommon::DumpStreamInfo(infoDumper);
239 }
240
OperatePermissionCheck(uint32_t interfaceCode)241 int32_t HStreamDepthData::OperatePermissionCheck(uint32_t interfaceCode)
242 {
243 switch (static_cast<StreamDepthDataInterfaceCode>(interfaceCode)) {
244 case StreamDepthDataInterfaceCode::CAMERA_STREAM_DEPTH_DATA_START: {
245 auto callerToken = IPCSkeleton::GetCallingTokenID();
246 CHECK_ERROR_RETURN_RET_LOG(callerToken_ != callerToken, CAMERA_OPERATION_NOT_ALLOWED,
247 "HStreamDepthData::OperatePermissionCheck fail, callerToken invalid!");
248 break;
249 }
250 default:
251 break;
252 }
253 return CAMERA_OK;
254 }
255 } // namespace CameraStandard
256 } // namespace OHOS
257