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