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 "input/camera_input.h"
17
18 #include <cinttypes>
19 #include <mutex>
20 #include <securec.h>
21 #include "camera_device.h"
22 #include "camera_device_ability_items.h"
23 #include "camera_log.h"
24 #include "camera_manager.h"
25 #include "camera_util.h"
26 #include "hcamera_device_callback_stub.h"
27 #include "icamera_util.h"
28 #include "metadata_common_utils.h"
29 #include "metadata_utils.h"
30 #include "output/metadata_output.h"
31 #include "session/capture_session.h"
32
33 namespace OHOS {
34 namespace CameraStandard {
35 using OHOS::HDI::Camera::V1_3::OperationMode;
OnError(const int32_t errorType,const int32_t errorMsg)36 int32_t CameraDeviceServiceCallback::OnError(const int32_t errorType, const int32_t errorMsg)
37 {
38 std::lock_guard<std::mutex> lock(deviceCallbackMutex_);
39 auto camInputSptr = camInput_.promote();
40 MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnError() is called!, errorType: %{public}d, errorMsg: %{public}d",
41 errorType, errorMsg);
42 if (camInputSptr != nullptr && camInputSptr->GetErrorCallback() != nullptr) {
43 int32_t serviceErrorType = ServiceToCameraError(errorType);
44 camInputSptr->GetErrorCallback()->OnError(serviceErrorType, errorMsg);
45 } else {
46 MEDIA_INFO_LOG("CameraDeviceServiceCallback::ErrorCallback not set!, Discarding callback");
47 }
48 return CAMERA_OK;
49 }
50
OnResult(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)51 int32_t CameraDeviceServiceCallback::OnResult(const uint64_t timestamp,
52 const std::shared_ptr<OHOS::Camera::CameraMetadata> &result)
53 {
54 CHECK_ERROR_RETURN_RET_LOG(result == nullptr, CAMERA_INVALID_ARG, "OnResult get null meta from server");
55 std::lock_guard<std::mutex> lock(deviceCallbackMutex_);
56 auto camInputSptr = camInput_.promote();
57 CHECK_ERROR_RETURN_RET_LOG(camInputSptr == nullptr, CAMERA_OK,
58 "CameraDeviceServiceCallback::OnResult() camInput_ is null!");
59 auto cameraObject = camInputSptr->GetCameraDeviceInfo();
60 if (cameraObject == nullptr) {
61 MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnResult() camInput_->GetCameraDeviceInfo() is null!");
62 } else {
63 MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::OnResult()"
64 "is called!, cameraId: %{public}s, timestamp: %{public}"
65 PRIu64, cameraObject->GetID().c_str(), timestamp);
66 }
67 if (camInputSptr->GetResultCallback() != nullptr) {
68 camInputSptr->GetResultCallback()->OnResult(timestamp, result);
69 }
70
71 auto pfnOcclusionDetectCallback = camInputSptr->GetOcclusionDetectCallback();
72 if (pfnOcclusionDetectCallback != nullptr) {
73 camera_metadata_item itemOcclusion;
74 int32_t retOcclusion = OHOS::Camera::FindCameraMetadataItem(result->get(),
75 OHOS_STATUS_CAMERA_OCCLUSION_DETECTION, &itemOcclusion);
76 bool foundOcclusion = (retOcclusion == CAM_META_SUCCESS && itemOcclusion.count != 0);
77 uint8_t occlusion = foundOcclusion ? static_cast<uint8_t>(itemOcclusion.data.i32[0]) : 0;
78
79 camera_metadata_item itemLensDirty;
80 int32_t retLensDirty = OHOS::Camera::FindCameraMetadataItem(result->get(),
81 OHOS_STATUS_CAMERA_LENS_DIRTY_DETECTION, &itemLensDirty);
82 bool foundLensDirty = (retLensDirty == CAM_META_SUCCESS && itemLensDirty.count != 0);
83 uint8_t lensDirty = foundLensDirty ? itemLensDirty.data.u8[0] : 0;
84
85 if (foundOcclusion || foundLensDirty) {
86 MEDIA_DEBUG_LOG("occlusion found:%{public}d val:%{public}u; lensDirty found:%{public}d val:%{public}u",
87 foundOcclusion, occlusion, foundLensDirty, lensDirty);
88 pfnOcclusionDetectCallback->OnCameraOcclusionDetected(occlusion, lensDirty);
89 }
90 }
91
92 camInputSptr->ProcessCallbackUpdates(timestamp, result);
93 return CAMERA_OK;
94 }
95
CameraInput(sptr<ICameraDeviceService> & deviceObj,sptr<CameraDevice> & cameraObj)96 CameraInput::CameraInput(sptr<ICameraDeviceService> &deviceObj,
97 sptr<CameraDevice> &cameraObj) : deviceObj_(deviceObj), cameraObj_(cameraObj)
98 {
99 MEDIA_INFO_LOG("CameraInput::CameraInput Contructor!");
100 InitCameraInput();
101 }
102
InitCameraInput()103 void CameraInput::InitCameraInput()
104 {
105 auto cameraObj = GetCameraDeviceInfo();
106 auto deviceObj = GetCameraDevice();
107 if (cameraObj) {
108 MEDIA_INFO_LOG("CameraInput::CameraInput Contructor Camera: %{public}s", cameraObj->GetID().c_str());
109 }
110 CameraDeviceSvcCallback_ = new(std::nothrow) CameraDeviceServiceCallback(this);
111 CHECK_ERROR_RETURN_LOG(CameraDeviceSvcCallback_ == nullptr, "Failed to new CameraDeviceSvcCallback_!");
112 CHECK_ERROR_RETURN_LOG(!deviceObj, "CameraInput::CameraInput() deviceObj is nullptr");
113 deviceObj->SetCallback(CameraDeviceSvcCallback_);
114 sptr<IRemoteObject> object = deviceObj->AsObject();
115 CHECK_ERROR_RETURN(object == nullptr);
116 pid_t pid = 0;
117 deathRecipient_ = new(std::nothrow) CameraDeathRecipient(pid);
118 CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new CameraDeathRecipient.");
119 auto thisPtr = wptr<CameraInput>(this);
120 deathRecipient_->SetNotifyCb([thisPtr](pid_t pid) {
121 auto ptr = thisPtr.promote();
122 if (ptr != nullptr) {
123 ptr->CameraServerDied(pid);
124 }
125 });
126 bool result = object->AddDeathRecipient(deathRecipient_);
127 CHECK_ERROR_RETURN_LOG(!result, "CameraInput::CameraInput failed to add deathRecipient");
128 }
129
CameraServerDied(pid_t pid)130 void CameraInput::CameraServerDied(pid_t pid)
131 {
132 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
133 {
134 std::lock_guard<std::mutex> errLock(errorCallbackMutex_);
135 if (errorCallback_ != nullptr) {
136 MEDIA_DEBUG_LOG("appCallback not nullptr");
137 int32_t serviceErrorType = ServiceToCameraError(CAMERA_INVALID_STATE);
138 int32_t serviceErrorMsg = 0;
139 MEDIA_DEBUG_LOG("serviceErrorType:%{public}d!, serviceErrorMsg:%{public}d!", serviceErrorType,
140 serviceErrorMsg);
141 errorCallback_->OnError(serviceErrorType, serviceErrorMsg);
142 }
143 }
144 std::lock_guard<std::mutex> interfaceLock(interfaceMutex_);
145 InputRemoveDeathRecipient();
146 }
147
InputRemoveDeathRecipient()148 void CameraInput::InputRemoveDeathRecipient()
149 {
150 auto deviceObj = GetCameraDevice();
151 if (deviceObj != nullptr) {
152 (void)deviceObj->AsObject()->RemoveDeathRecipient(deathRecipient_);
153 SetCameraDevice(nullptr);
154 }
155 deathRecipient_ = nullptr;
156 }
157
~CameraInput()158 CameraInput::~CameraInput()
159 {
160 MEDIA_INFO_LOG("CameraInput::CameraInput Destructor!");
161 std::lock_guard<std::mutex> lock(interfaceMutex_);
162 if (cameraObj_) {
163 MEDIA_INFO_LOG("CameraInput::CameraInput Destructor Camera: %{public}s", cameraObj_->GetID().c_str());
164 }
165 InputRemoveDeathRecipient();
166 }
167
Open()168 int CameraInput::Open()
169 {
170 std::lock_guard<std::mutex> lock(interfaceMutex_);
171 MEDIA_DEBUG_LOG("Enter Into CameraInput::Open");
172 int32_t retCode = CAMERA_UNKNOWN_ERROR;
173 auto deviceObj = GetCameraDevice();
174 if (deviceObj) {
175 retCode = deviceObj->Open();
176 CHECK_ERROR_PRINT_LOG(retCode != CAMERA_OK, "Failed to open Camera Input, retCode: %{public}d", retCode);
177 } else {
178 MEDIA_ERR_LOG("CameraInput::Open() deviceObj is nullptr");
179 }
180 return ServiceToCameraError(retCode);
181 }
182
Open(bool isEnableSecureCamera,uint64_t * secureSeqId)183 int CameraInput::Open(bool isEnableSecureCamera, uint64_t* secureSeqId)
184 {
185 std::lock_guard<std::mutex> lock(interfaceMutex_);
186 MEDIA_DEBUG_LOG("Enter Into CameraInput::OpenSecureCamera");
187 int32_t retCode = CAMERA_UNKNOWN_ERROR;
188 bool isSupportSecCamera = false;
189 auto cameraObject = GetCameraDeviceInfo();
190 if (isEnableSecureCamera && cameraObject) {
191 std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObject->GetMetadata();
192 CHECK_ERROR_RETURN_RET_LOG(baseMetadata == nullptr, retCode,
193 "CameraInput::GetMetaSetting Failed to find baseMetadata");
194 camera_metadata_item_t item;
195 retCode = OHOS::Camera::FindCameraMetadataItem(baseMetadata->get(), OHOS_ABILITY_CAMERA_MODES, &item);
196 CHECK_ERROR_RETURN_RET_LOG(retCode != CAM_META_SUCCESS || item.count == 0, retCode,
197 "CaptureSession::GetSupportedModes Failed with return code %{public}d", retCode);
198 for (uint32_t i = 0; i < item.count; i++) {
199 if (item.data.u8[i] == SECURE) {
200 isSupportSecCamera = true;
201 }
202 }
203 }
204
205 auto deviceObj = GetCameraDevice();
206 if (deviceObj) {
207 retCode = isSupportSecCamera ? (deviceObj->OpenSecureCamera(secureSeqId)) : (deviceObj->Open());
208 CHECK_ERROR_PRINT_LOG(retCode != CAMERA_OK,
209 "Failed to open Camera Input, retCode: %{public}d, isSupportSecCamera is %{public}d",
210 retCode, isSupportSecCamera);
211 } else {
212 MEDIA_ERR_LOG("CameraInput::OpenSecureCamera() deviceObj is nullptr");
213 }
214 MEDIA_INFO_LOG("Enter Into CameraInput::OpenSecureCamera secureSeqId = %{public}" PRIu64, *secureSeqId);
215 return ServiceToCameraError(retCode);
216 }
217
Close()218 int CameraInput::Close()
219 {
220 std::lock_guard<std::mutex> lock(interfaceMutex_);
221 MEDIA_DEBUG_LOG("Enter Into CameraInput::Close");
222 int32_t retCode = CAMERA_UNKNOWN_ERROR;
223 auto deviceObj = GetCameraDevice();
224 if (deviceObj) {
225 retCode = deviceObj->Close();
226 CHECK_ERROR_PRINT_LOG(retCode != CAMERA_OK, "Failed to close Camera Input, retCode: %{public}d", retCode);
227 } else {
228 MEDIA_ERR_LOG("CameraInput::Close() deviceObj is nullptr");
229 }
230 SetCameraDeviceInfo(nullptr);
231 InputRemoveDeathRecipient();
232 CameraDeviceSvcCallback_ = nullptr;
233 return ServiceToCameraError(retCode);
234 }
235
Release()236 int CameraInput::Release()
237 {
238 std::lock_guard<std::mutex> lock(interfaceMutex_);
239 MEDIA_DEBUG_LOG("Enter Into CameraInput::Release");
240 int32_t retCode = CAMERA_UNKNOWN_ERROR;
241 auto deviceObj = GetCameraDevice();
242 if (deviceObj) {
243 retCode = deviceObj->Release();
244 CHECK_ERROR_PRINT_LOG(retCode != CAMERA_OK, "Failed to release Camera Input, retCode: %{public}d", retCode);
245 } else {
246 MEDIA_ERR_LOG("CameraInput::Release() deviceObj is nullptr");
247 }
248 SetCameraDeviceInfo(nullptr);
249 InputRemoveDeathRecipient();
250 CameraDeviceSvcCallback_ = nullptr;
251 return ServiceToCameraError(retCode);
252 }
253
SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)254 void CameraInput::SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)
255 {
256 CHECK_ERROR_PRINT_LOG(errorCallback == nullptr, "SetErrorCallback: Unregistering error callback");
257 std::lock_guard<std::mutex> lock(errorCallbackMutex_);
258 errorCallback_ = errorCallback;
259 return;
260 }
261
SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)262 void CameraInput::SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)
263 {
264 CHECK_ERROR_PRINT_LOG(resultCallback == nullptr, "SetResultCallback: Unregistering error resultCallback");
265 MEDIA_DEBUG_LOG("CameraInput::setresult callback");
266 std::lock_guard<std::mutex> lock(resultCallbackMutex_);
267 resultCallback_ = resultCallback;
268 return;
269 }
270
SetCameraDeviceInfo(sptr<CameraDevice> cameraObj)271 void CameraInput::SetCameraDeviceInfo(sptr<CameraDevice> cameraObj)
272 {
273 MEDIA_ERR_LOG("CameraInput::SetCameraDeviceInfo");
274 std::lock_guard<std::mutex> lock(cameraDeviceInfoMutex_);
275 cameraObj_ = cameraObj;
276 return;
277 }
278
createPositionMapping()279 std::map<CameraPosition, camera_position_enum> createPositionMapping()
280 {
281 std::map<CameraPosition, camera_position_enum> enumMapping;
282 enumMapping[CameraPosition::CAMERA_POSITION_UNSPECIFIED] = camera_position_enum::OHOS_CAMERA_POSITION_OTHER;
283 enumMapping[CameraPosition::CAMERA_POSITION_BACK] = camera_position_enum::OHOS_CAMERA_POSITION_BACK;
284 enumMapping[CameraPosition::CAMERA_POSITION_FRONT] = camera_position_enum::OHOS_CAMERA_POSITION_FRONT;
285 return enumMapping;
286 }
287
SetInputUsedAsPosition(CameraPosition usedAsPosition)288 void CameraInput::SetInputUsedAsPosition(CameraPosition usedAsPosition)
289 {
290 MEDIA_INFO_LOG("CameraInput::SetInputUsedAsPosition params: %{public}u", usedAsPosition);
291 std::lock_guard<std::mutex> lock(cameraDeviceInfoMutex_);
292 uint8_t translatePos = OHOS_CAMERA_POSITION_OTHER;
293 if (positionMapping.empty()) {
294 positionMapping = createPositionMapping();
295 }
296 translatePos = positionMapping[usedAsPosition];
297
298 auto metadata = std::make_shared<Camera::CameraMetadata>(1, 1);
299 MEDIA_INFO_LOG("CameraInput::SetInputUsedAsPosition fr: %{public}u, to: %{public}u", usedAsPosition, translatePos);
300 if (!AddOrUpdateMetadata(metadata, OHOS_CONTROL_CAMERA_USED_AS_POSITION, &translatePos, 1)) {
301 MEDIA_ERR_LOG("CameraInput::SetInputUsedAsPosition Failed to set metadata");
302 }
303 auto deviceObj = GetCameraDevice();
304 CHECK_ERROR_RETURN_LOG(deviceObj == nullptr, "deviceObj is nullptr");
305 deviceObj->SetUsedAsPosition(translatePos);
306 deviceObj->UpdateSetting(metadata);
307 CHECK_ERROR_RETURN_LOG(cameraObj_ == nullptr, "cameraObj_ is nullptr");
308 cameraObj_->SetCameraDeviceUsedAsPosition(usedAsPosition);
309 }
310
ControlAuxiliary(AuxiliaryType type,AuxiliaryStatus status)311 void CameraInput::ControlAuxiliary(AuxiliaryType type, AuxiliaryStatus status)
312 {
313 MEDIA_INFO_LOG("CameraInput::ControlAuxiliary type: %{public}u, status:%{public}u", type, status);
314 if (type == AuxiliaryType::CONTRACTLENS && status == AuxiliaryStatus::AUXILIARY_ON) {
315 uint8_t value = 1;
316 uint32_t count = 1;
317 constexpr int32_t DEFAULT_ITEMS = 1;
318 constexpr int32_t DEFAULT_DATA_LENGTH = 1;
319 auto metadata = std::make_shared<Camera::CameraMetadata>(DEFAULT_ITEMS, DEFAULT_DATA_LENGTH);
320 if (!AddOrUpdateMetadata(metadata, OHOS_CONTROL_EJECT_RETRY, &value, count)) {
321 MEDIA_ERR_LOG("CameraInput::ControlAuxiliary Failed to set metadata");
322 return;
323 }
324 auto deviceObj = GetCameraDevice();
325 CHECK_ERROR_RETURN_LOG(deviceObj == nullptr, "deviceObj is nullptr");
326 deviceObj->UpdateSetting(metadata);
327 auto serviceProxy = CameraManager::GetInstance()->GetServiceProxy();
328 CHECK_ERROR_RETURN_LOG(serviceProxy == nullptr, "ControlAuxiliary serviceProxy is null");
329 serviceProxy->SetDeviceRetryTime();
330 }
331 }
332
SetOcclusionDetectCallback(std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback)333 void CameraInput::SetOcclusionDetectCallback(
334 std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback)
335 {
336 if (cameraOcclusionDetectCallback == nullptr) {
337 MEDIA_ERR_LOG("SetOcclusionDetectCallback:SetOcclusionDetectCallback error cameraOcclusionDetectCallback");
338 }
339 MEDIA_DEBUG_LOG("CameraInput::SetOcclusionDetectCallback callback");
340 std::lock_guard<std::mutex> lock(occlusionCallbackMutex_);
341 cameraOcclusionDetectCallback_ = cameraOcclusionDetectCallback;
342 return;
343 }
344
GetCameraId()345 std::string CameraInput::GetCameraId()
346 {
347 auto cameraObject = GetCameraDeviceInfo();
348 CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, nullptr, "CameraInput::GetCameraId cameraObject is null");
349
350 return cameraObject->GetID();
351 }
352
GetCameraDevice()353 sptr<ICameraDeviceService> CameraInput::GetCameraDevice()
354 {
355 std::lock_guard<std::mutex> lock(deviceObjMutex_);
356 return deviceObj_;
357 }
358
SetCameraDevice(sptr<ICameraDeviceService> deviceObj)359 void CameraInput::SetCameraDevice(sptr<ICameraDeviceService> deviceObj)
360 {
361 std::lock_guard<std::mutex> lock(deviceObjMutex_);
362 deviceObj_ = deviceObj;
363 return;
364 }
365
GetErrorCallback()366 std::shared_ptr<ErrorCallback> CameraInput::GetErrorCallback()
367 {
368 std::lock_guard<std::mutex> lock(errorCallbackMutex_);
369 return errorCallback_;
370 }
371
GetResultCallback()372 std::shared_ptr<ResultCallback> CameraInput::GetResultCallback()
373 {
374 std::lock_guard<std::mutex> lock(resultCallbackMutex_);
375 MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetResultCallback");
376 return resultCallback_;
377 }
378
GetOcclusionDetectCallback()379 std::shared_ptr<CameraOcclusionDetectCallback> CameraInput::GetOcclusionDetectCallback()
380 {
381 std::lock_guard<std::mutex> lock(occlusionCallbackMutex_);
382 return cameraOcclusionDetectCallback_;
383 }
384
GetCameraDeviceInfo()385 sptr<CameraDevice> CameraInput::GetCameraDeviceInfo()
386 {
387 std::lock_guard<std::mutex> lock(cameraDeviceInfoMutex_);
388 return cameraObj_;
389 }
390
ProcessCallbackUpdates(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)391 void CameraInput::ProcessCallbackUpdates(
392 const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
393 {
394 auto metadataResultProcessor = GetMetadataResultProcessor();
395 CHECK_ERROR_RETURN(metadataResultProcessor == nullptr);
396 metadataResultProcessor->ProcessCallbacks(timestamp, result);
397 }
398
UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)399 int32_t CameraInput::UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)
400 {
401 CAMERA_SYNC_TRACE;
402 CHECK_ERROR_RETURN_RET(changedMetadata == nullptr, CAMERA_INVALID_ARG);
403 CHECK_ERROR_RETURN_RET_LOG(!OHOS::Camera::GetCameraMetadataItemCount(changedMetadata->get()), CAMERA_OK,
404 "CameraInput::UpdateSetting No configuration to update");
405
406 std::lock_guard<std::mutex> lock(interfaceMutex_);
407 auto deviceObj = GetCameraDevice();
408 CHECK_ERROR_RETURN_RET_LOG(!deviceObj, ServiceToCameraError(CAMERA_INVALID_ARG),
409 "CameraInput::UpdateSetting() deviceObj is nullptr");
410 int32_t ret = deviceObj->UpdateSetting(changedMetadata);
411 CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, ret, "CameraInput::UpdateSetting Failed to update settings");
412
413 auto cameraObject = GetCameraDeviceInfo();
414 CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, CAMERA_INVALID_ARG,
415 "CameraInput::UpdateSetting cameraObject is null");
416 std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObject->GetMetadata();
417 bool mergeResult = MergeMetadata(changedMetadata, baseMetadata);
418 CHECK_ERROR_RETURN_RET_LOG(!mergeResult, CAMERA_INVALID_ARG,
419 "CameraInput::UpdateSetting() baseMetadata or itemEntry is nullptr");
420 return CAMERA_OK;
421 }
422
MergeMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata,std::shared_ptr<OHOS::Camera::CameraMetadata> dstMetadata)423 bool CameraInput::MergeMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata,
424 std::shared_ptr<OHOS::Camera::CameraMetadata> dstMetadata)
425 {
426 CHECK_ERROR_RETURN_RET(srcMetadata == nullptr || dstMetadata == nullptr, false);
427 auto srcHeader = srcMetadata->get();
428 CHECK_ERROR_RETURN_RET(srcHeader == nullptr, false);
429 auto dstHeader = dstMetadata->get();
430 CHECK_ERROR_RETURN_RET(dstHeader == nullptr, false);
431
432 auto srcItemCount = srcHeader->item_count;
433 camera_metadata_item_t srcItem;
434 for (uint32_t index = 0; index < srcItemCount; index++) {
435 int ret = OHOS::Camera::GetCameraMetadataItem(srcHeader, index, &srcItem);
436 CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, false,
437 "Failed to get metadata item at index: %{public}d", index);
438 bool status = false;
439 uint32_t currentIndex;
440 ret = OHOS::Camera::FindCameraMetadataItemIndex(dstHeader, srcItem.item, ¤tIndex);
441 if (ret == CAM_META_ITEM_NOT_FOUND) {
442 status = dstMetadata->addEntry(srcItem.item, srcItem.data.u8, srcItem.count);
443 } else if (ret == CAM_META_SUCCESS) {
444 status = dstMetadata->updateEntry(srcItem.item, srcItem.data.u8, srcItem.count);
445 }
446 CHECK_ERROR_RETURN_RET_LOG(!status, false, "Failed to update metadata item: %{public}d", srcItem.item);
447 }
448 return true;
449 }
450
GetCameraSettings()451 std::string CameraInput::GetCameraSettings()
452 {
453 auto cameraObject = GetCameraDeviceInfo();
454 CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, nullptr, "GetCameraSettings cameraObject is null");
455 return OHOS::Camera::MetadataUtils::EncodeToString(cameraObject->GetMetadata());
456 }
457
SetCameraSettings(std::string setting)458 int32_t CameraInput::SetCameraSettings(std::string setting)
459 {
460 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = OHOS::Camera::MetadataUtils::DecodeFromString(setting);
461 CHECK_ERROR_RETURN_RET_LOG(metadata == nullptr, CAMERA_INVALID_ARG,
462 "CameraInput::SetCameraSettings Failed to decode metadata setting from string");
463 return UpdateSetting(metadata);
464 }
465
GetMetaSetting(uint32_t metaTag)466 std::shared_ptr<camera_metadata_item_t> CameraInput::GetMetaSetting(uint32_t metaTag)
467 {
468 CHECK_ERROR_RETURN_RET_LOG(cameraObj_ == nullptr, nullptr,
469 "CameraInput::GetMetaSetting cameraObj has release!");
470 std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObj_->GetMetadata();
471 CHECK_ERROR_RETURN_RET_LOG(baseMetadata == nullptr, nullptr,
472 "CameraInput::GetMetaSetting Failed to find baseMetadata");
473 std::shared_ptr<camera_metadata_item_t> item = MetadataCommonUtils::GetCapabilityEntry(baseMetadata, metaTag);
474 CHECK_ERROR_RETURN_RET_LOG(item == nullptr || item->count == 0, nullptr,
475 "CameraInput::GetMetaSetting Failed to find meta item: metaTag = %{public}u", metaTag);
476 return item;
477 }
478
GetCameraAllVendorTags(std::vector<vendorTag_t> & infos)479 int32_t CameraInput::GetCameraAllVendorTags(std::vector<vendorTag_t> &infos) __attribute__((no_sanitize("cfi")))
480 {
481 infos.clear();
482 MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags called!");
483 int32_t ret = OHOS::Camera::GetAllVendorTags(infos);
484 CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, CAMERA_UNKNOWN_ERROR,
485 "CameraInput::GetCameraAllVendorTags failed! because of hdi error, ret = %{public}d", ret);
486 MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags success! vendors size = %{public}zu!", infos.size());
487 MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags end!");
488 return CAMERA_OK;
489 }
490
491
SwitchCameraDevice(sptr<ICameraDeviceService> & deviceObj,sptr<CameraDevice> & cameraObj)492 void CameraInput::SwitchCameraDevice(sptr<ICameraDeviceService> &deviceObj, sptr<CameraDevice> &cameraObj)
493 {
494 SetCameraDeviceInfo(cameraObj);
495 SetCameraDevice(deviceObj);
496 InitCameraInput();
497 }
498 } // namespace CameraStandard
499 } // namespace OHOS
500