• 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_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         camera_metadata_item_t item;
83         CHECK_AND_RETURN_RET_LOG(dynamicSetting != nullptr, CAMERA_INVALID_ARG,
84             "HStreamDepthData::SetDataAccuracy dynamicSetting is nullptr.");
85         int ret = OHOS::Camera::FindCameraMetadataItem(dynamicSetting->get(), OHOS_CONTROL_DEPTH_DATA_ACCURACY, &item);
86         bool status = false;
87         if (ret == CAM_META_ITEM_NOT_FOUND) {
88             MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy Failed to find data accuracy");
89             status = dynamicSetting->addEntry(
90                 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
91         } else if (ret == CAM_META_SUCCESS) {
92             MEDIA_DEBUG_LOG("HStreamDepthData::SetDataAccuracy success to find data accuracy");
93             status = dynamicSetting->updateEntry(
94                 OHOS_CONTROL_DEPTH_DATA_ACCURACY, streamDepthDataAccuracy_.data(), streamDepthDataAccuracy_.size());
95         }
96         if (!status) {
97             MEDIA_ERR_LOG("HStreamDepthData::SetDataAccuracy Failed to set data accuracy");
98         }
99         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(dynamicSetting, depthSettings);
100     }
101 
102     auto streamOperator = GetStreamOperator();
103 
104     CamRetCode rc = HDI::Camera::V1_0::NO_ERROR;
105     if (streamOperator != nullptr) {
106         std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
107         CaptureInfo captureInfo;
108         captureInfo.streamIds_ = {GetHdiStreamId()};
109         captureInfo.captureSetting_ = depthSettings;
110         captureInfo.enableShutterCallback_ = false;
111         int32_t currentCaptureId = GetPreparedCaptureId();
112         MEDIA_INFO_LOG("HStreamDepthData::SetDataAccuracy stream:%{public}d, with settingCapture ID:%{public}d",
113             GetFwkStreamId(), currentCaptureId);
114         rc = (CamRetCode)(streamOperator->Capture(currentCaptureId, captureInfo, true));
115         if (rc != HDI::Camera::V1_0::NO_ERROR) {
116             MEDIA_ERR_LOG("HStreamDepthData::SetDataAccuracy Failed with error Code:%{public}d", rc);
117         }
118     }
119     return rc;
120 }
121 
Start()122 int32_t HStreamDepthData::Start()
123 {
124     CAMERA_SYNC_TRACE;
125     auto streamOperator = GetStreamOperator();
126     if (streamOperator == nullptr) {
127         return CAMERA_INVALID_STATE;
128     }
129 
130     auto preparedCaptureId = GetPreparedCaptureId();
131     if (preparedCaptureId != CAPTURE_ID_UNSET) {
132         MEDIA_ERR_LOG("HStreamDepthData::Start, Already started with captureID: %{public}d", preparedCaptureId);
133         return CAMERA_INVALID_STATE;
134     }
135 
136     int32_t ret = PrepareCaptureId();
137     preparedCaptureId = GetPreparedCaptureId();
138     if (ret != CAMERA_OK || preparedCaptureId == CAPTURE_ID_UNSET) {
139         MEDIA_ERR_LOG("HStreamDepthData::Start Failed to allocate a captureId");
140         return ret;
141     }
142 
143     std::vector<uint8_t> ability;
144     {
145         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
146         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
147     }
148 
149     CaptureInfo captureInfo;
150     captureInfo.streamIds_ = { GetHdiStreamId() };
151     captureInfo.captureSetting_ = ability;
152     captureInfo.enableShutterCallback_ = false;
153     MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
154         GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
155 
156     std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
157     HStreamCommon::PrintCaptureDebugLog(cameraAbility_);
158     CamRetCode rc = (CamRetCode)(streamOperator->Capture(preparedCaptureId, captureInfo, true));
159     if (rc != HDI::Camera::V1_0::NO_ERROR) {
160         ResetCaptureId();
161         MEDIA_ERR_LOG("HStreamDepthData::Start Failed with error Code:%{public}d", rc);
162         CameraReportUtils::ReportCameraError(
163             "HStreamDepthData::Start", rc, true, CameraReportUtils::GetCallerInfo());
164         ret = HdiToServiceError(rc);
165     } else {
166         depthDataStreamStatus_ = DepthDataStreamStatus::STARTED;
167     }
168 
169     return ret;
170 }
171 
Stop()172 int32_t HStreamDepthData::Stop()
173 {
174     CAMERA_SYNC_TRACE;
175     auto streamOperator = GetStreamOperator();
176     if (streamOperator == nullptr) {
177         MEDIA_INFO_LOG("HStreamDepthData::Stop streamOperator is null");
178         return CAMERA_INVALID_STATE;
179     }
180     auto preparedCaptureId = GetPreparedCaptureId();
181     MEDIA_INFO_LOG("HStreamDepthData::Start streamId:%{public}d hdiStreamId:%{public}d With capture ID: %{public}d",
182         GetFwkStreamId(), GetHdiStreamId(), preparedCaptureId);
183     if (preparedCaptureId == CAPTURE_ID_UNSET) {
184         MEDIA_ERR_LOG("HStreamDepthData::Stop, Stream not started yet");
185         return CAMERA_INVALID_STATE;
186     }
187     int32_t ret = CAMERA_OK;
188     {
189         std::lock_guard<std::mutex> startStopLock(streamStartStopLock_);
190         ret = StopStream();
191         if (ret != CAMERA_OK) {
192             MEDIA_ERR_LOG("HStreamDepthData::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
193                           ret, preparedCaptureId);
194         } else {
195             depthDataStreamStatus_ = DepthDataStreamStatus::STOPED;
196         }
197     }
198     return ret;
199 }
200 
Release()201 int32_t HStreamDepthData::Release()
202 {
203     return ReleaseStream(false);
204 }
205 
ReleaseStream(bool isDelay)206 int32_t HStreamDepthData::ReleaseStream(bool isDelay)
207 {
208     {
209         std::lock_guard<std::mutex> lock(callbackLock_);
210         streamDepthDataCallback_ = nullptr;
211     }
212     return HStreamCommon::ReleaseStream(isDelay);
213 }
214 
SetCallback(sptr<IStreamDepthDataCallback> & callback)215 int32_t HStreamDepthData::SetCallback(sptr<IStreamDepthDataCallback>& callback)
216 {
217     if (callback == nullptr) {
218         MEDIA_ERR_LOG("HStreamDepthData::SetCallback callback is null");
219         return CAMERA_INVALID_ARG;
220     }
221     std::lock_guard<std::mutex> lock(callbackLock_);
222     streamDepthDataCallback_ = callback;
223     return CAMERA_OK;
224 }
225 
OnDepthDataError(int32_t errorType)226 int32_t HStreamDepthData::OnDepthDataError(int32_t errorType)
227 {
228     std::lock_guard<std::mutex> lock(callbackLock_);
229     if (streamDepthDataCallback_ != nullptr) {
230         int32_t depthDataErrorCode;
231         if (errorType == BUFFER_LOST) {
232             depthDataErrorCode = CAMERA_STREAM_BUFFER_LOST;
233         } else {
234             depthDataErrorCode = CAMERA_UNKNOWN_ERROR;
235         }
236         CAMERA_SYSEVENT_FAULT(CreateMsg("Depth OnDepthDataError! errorCode:%d", depthDataErrorCode));
237         streamDepthDataCallback_->OnDepthDataError(depthDataErrorCode);
238     }
239     return CAMERA_OK;
240 }
241 
DumpStreamInfo(CameraInfoDumper & infoDumper)242 void HStreamDepthData::DumpStreamInfo(CameraInfoDumper& infoDumper)
243 {
244     infoDumper.Title("depth stream");
245     HStreamCommon::DumpStreamInfo(infoDumper);
246 }
247 
OperatePermissionCheck(uint32_t interfaceCode)248 int32_t HStreamDepthData::OperatePermissionCheck(uint32_t interfaceCode)
249 {
250     switch (static_cast<StreamDepthDataInterfaceCode>(interfaceCode)) {
251         case StreamDepthDataInterfaceCode::CAMERA_STREAM_DEPTH_DATA_START: {
252             auto callerToken = IPCSkeleton::GetCallingTokenID();
253             if (callerToken_ != callerToken) {
254                 MEDIA_ERR_LOG("HStreamDepthData::OperatePermissionCheck fail, callerToken not legal");
255                 return CAMERA_OPERATION_NOT_ALLOWED;
256             }
257             break;
258         }
259         default:
260             break;
261     }
262     return CAMERA_OK;
263 }
264 } // namespace CameraStandard
265 } // namespace OHOS