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 
SetOcclusionDetectCallback(std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback)311 void CameraInput::SetOcclusionDetectCallback(
312     std::shared_ptr<CameraOcclusionDetectCallback> cameraOcclusionDetectCallback)
313 {
314     if (cameraOcclusionDetectCallback == nullptr) {
315         MEDIA_ERR_LOG("SetOcclusionDetectCallback:SetOcclusionDetectCallback error cameraOcclusionDetectCallback");
316     }
317     MEDIA_DEBUG_LOG("CameraInput::SetOcclusionDetectCallback callback");
318     std::lock_guard<std::mutex> lock(occlusionCallbackMutex_);
319     cameraOcclusionDetectCallback_ = cameraOcclusionDetectCallback;
320     return;
321 }
322 
GetCameraId()323 std::string CameraInput::GetCameraId()
324 {
325     auto cameraObject = GetCameraDeviceInfo();
326     CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, nullptr, "CameraInput::GetCameraId cameraObject is null");
327 
328     return cameraObject->GetID();
329 }
330 
GetCameraDevice()331 sptr<ICameraDeviceService> CameraInput::GetCameraDevice()
332 {
333     std::lock_guard<std::mutex> lock(deviceObjMutex_);
334     return deviceObj_;
335 }
336 
SetCameraDevice(sptr<ICameraDeviceService> deviceObj)337 void CameraInput::SetCameraDevice(sptr<ICameraDeviceService> deviceObj)
338 {
339     std::lock_guard<std::mutex> lock(deviceObjMutex_);
340     deviceObj_ = deviceObj;
341     return;
342 }
343 
GetErrorCallback()344 std::shared_ptr<ErrorCallback> CameraInput::GetErrorCallback()
345 {
346     std::lock_guard<std::mutex> lock(errorCallbackMutex_);
347     return errorCallback_;
348 }
349 
GetResultCallback()350 std::shared_ptr<ResultCallback> CameraInput::GetResultCallback()
351 {
352     std::lock_guard<std::mutex> lock(resultCallbackMutex_);
353     MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetResultCallback");
354     return resultCallback_;
355 }
356 
GetOcclusionDetectCallback()357 std::shared_ptr<CameraOcclusionDetectCallback> CameraInput::GetOcclusionDetectCallback()
358 {
359     std::lock_guard<std::mutex> lock(occlusionCallbackMutex_);
360     return cameraOcclusionDetectCallback_;
361 }
362 
GetCameraDeviceInfo()363 sptr<CameraDevice> CameraInput::GetCameraDeviceInfo()
364 {
365     std::lock_guard<std::mutex> lock(cameraDeviceInfoMutex_);
366     return cameraObj_;
367 }
368 
ProcessCallbackUpdates(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)369 void CameraInput::ProcessCallbackUpdates(
370     const uint64_t timestamp, const std::shared_ptr<OHOS::Camera::CameraMetadata>& result)
371 {
372     auto metadataResultProcessor = GetMetadataResultProcessor();
373     CHECK_ERROR_RETURN(metadataResultProcessor == nullptr);
374     metadataResultProcessor->ProcessCallbacks(timestamp, result);
375 }
376 
UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)377 int32_t CameraInput::UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)
378 {
379     CAMERA_SYNC_TRACE;
380     CHECK_ERROR_RETURN_RET(changedMetadata == nullptr, CAMERA_INVALID_ARG);
381     CHECK_ERROR_RETURN_RET_LOG(!OHOS::Camera::GetCameraMetadataItemCount(changedMetadata->get()), CAMERA_OK,
382         "CameraInput::UpdateSetting No configuration to update");
383 
384     std::lock_guard<std::mutex> lock(interfaceMutex_);
385     auto deviceObj = GetCameraDevice();
386     CHECK_ERROR_RETURN_RET_LOG(!deviceObj, ServiceToCameraError(CAMERA_INVALID_ARG),
387         "CameraInput::UpdateSetting() deviceObj is nullptr");
388     int32_t ret = deviceObj->UpdateSetting(changedMetadata);
389     CHECK_ERROR_RETURN_RET_LOG(ret != CAMERA_OK, ret, "CameraInput::UpdateSetting Failed to update settings");
390 
391     auto cameraObject = GetCameraDeviceInfo();
392     CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, CAMERA_INVALID_ARG,
393         "CameraInput::UpdateSetting cameraObject is null");
394     std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObject->GetMetadata();
395     bool mergeResult = MergeMetadata(changedMetadata, baseMetadata);
396     CHECK_ERROR_RETURN_RET_LOG(!mergeResult, CAMERA_INVALID_ARG,
397         "CameraInput::UpdateSetting() baseMetadata or itemEntry is nullptr");
398     return CAMERA_OK;
399 }
400 
MergeMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata,std::shared_ptr<OHOS::Camera::CameraMetadata> dstMetadata)401 bool CameraInput::MergeMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata,
402     std::shared_ptr<OHOS::Camera::CameraMetadata> dstMetadata)
403 {
404     CHECK_ERROR_RETURN_RET(srcMetadata == nullptr || dstMetadata == nullptr, false);
405     auto srcHeader = srcMetadata->get();
406     CHECK_ERROR_RETURN_RET(srcHeader == nullptr, false);
407     auto dstHeader = dstMetadata->get();
408     CHECK_ERROR_RETURN_RET(dstHeader == nullptr, false);
409 
410     auto srcItemCount = srcHeader->item_count;
411     camera_metadata_item_t srcItem;
412     for (uint32_t index = 0; index < srcItemCount; index++) {
413         int ret = OHOS::Camera::GetCameraMetadataItem(srcHeader, index, &srcItem);
414         CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, false,
415             "Failed to get metadata item at index: %{public}d", index);
416         bool status = false;
417         uint32_t currentIndex;
418         ret = OHOS::Camera::FindCameraMetadataItemIndex(dstHeader, srcItem.item, &currentIndex);
419         if (ret == CAM_META_ITEM_NOT_FOUND) {
420             status = dstMetadata->addEntry(srcItem.item, srcItem.data.u8, srcItem.count);
421         } else if (ret == CAM_META_SUCCESS) {
422             status = dstMetadata->updateEntry(srcItem.item, srcItem.data.u8, srcItem.count);
423         }
424         CHECK_ERROR_RETURN_RET_LOG(!status, false, "Failed to update metadata item: %{public}d", srcItem.item);
425     }
426     return true;
427 }
428 
GetCameraSettings()429 std::string CameraInput::GetCameraSettings()
430 {
431     auto cameraObject = GetCameraDeviceInfo();
432     CHECK_ERROR_RETURN_RET_LOG(cameraObject == nullptr, nullptr, "GetCameraSettings cameraObject is null");
433     return OHOS::Camera::MetadataUtils::EncodeToString(cameraObject->GetMetadata());
434 }
435 
SetCameraSettings(std::string setting)436 int32_t CameraInput::SetCameraSettings(std::string setting)
437 {
438     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = OHOS::Camera::MetadataUtils::DecodeFromString(setting);
439     CHECK_ERROR_RETURN_RET_LOG(metadata == nullptr, CAMERA_INVALID_ARG,
440         "CameraInput::SetCameraSettings Failed to decode metadata setting from string");
441     return UpdateSetting(metadata);
442 }
443 
GetMetaSetting(uint32_t metaTag)444 std::shared_ptr<camera_metadata_item_t> CameraInput::GetMetaSetting(uint32_t metaTag)
445 {
446     CHECK_ERROR_RETURN_RET_LOG(cameraObj_ == nullptr, nullptr,
447         "CameraInput::GetMetaSetting cameraObj has release!");
448     std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObj_->GetMetadata();
449     CHECK_ERROR_RETURN_RET_LOG(baseMetadata == nullptr, nullptr,
450         "CameraInput::GetMetaSetting Failed to find baseMetadata");
451     std::shared_ptr<camera_metadata_item_t> item = MetadataCommonUtils::GetCapabilityEntry(baseMetadata, metaTag);
452     CHECK_ERROR_RETURN_RET_LOG(item == nullptr || item->count == 0, nullptr,
453         "CameraInput::GetMetaSetting  Failed to find meta item: metaTag = %{public}u", metaTag);
454     return item;
455 }
456 
GetCameraAllVendorTags(std::vector<vendorTag_t> & infos)457 int32_t CameraInput::GetCameraAllVendorTags(std::vector<vendorTag_t> &infos) __attribute__((no_sanitize("cfi")))
458 {
459     infos.clear();
460     MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags called!");
461     int32_t ret = OHOS::Camera::GetAllVendorTags(infos);
462     CHECK_ERROR_RETURN_RET_LOG(ret != CAM_META_SUCCESS, CAMERA_UNKNOWN_ERROR,
463         "CameraInput::GetCameraAllVendorTags failed! because of hdi error, ret = %{public}d", ret);
464     MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags success! vendors size = %{public}zu!", infos.size());
465     MEDIA_INFO_LOG("CameraInput::GetCameraAllVendorTags end!");
466     return CAMERA_OK;
467 }
468 
469 
SwitchCameraDevice(sptr<ICameraDeviceService> & deviceObj,sptr<CameraDevice> & cameraObj)470 void CameraInput::SwitchCameraDevice(sptr<ICameraDeviceService> &deviceObj, sptr<CameraDevice> &cameraObj)
471 {
472     SetCameraDeviceInfo(cameraObj);
473     SetCameraDevice(deviceObj);
474     InitCameraInput();
475 }
476 } // namespace CameraStandard
477 } // namespace OHOS
478