• 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 "hcamera_device.h"
17 
18 #include "camera_util.h"
19 #include "camera_log.h"
20 #include "ipc_skeleton.h"
21 #include "metadata_utils.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
HCameraDevice(sptr<HCameraHostManager> & cameraHostManager,std::string cameraID,const uint32_t callingTokenId)25 HCameraDevice::HCameraDevice(sptr<HCameraHostManager> &cameraHostManager,
26                              std::string cameraID,
27                              const uint32_t callingTokenId)
28 {
29     cameraHostManager_ = cameraHostManager;
30     cameraID_ = cameraID;
31     streamOperator_ = nullptr;
32     isReleaseCameraDevice_ = false;
33     isOpenedCameraDevice_ = false;
34     callerToken_ = callingTokenId;
35 }
36 
~HCameraDevice()37 HCameraDevice::~HCameraDevice()
38 {
39     hdiCameraDevice_ = nullptr;
40     streamOperator_ = nullptr;
41     if (cameraHostManager_) {
42         cameraHostManager_->RemoveCameraDevice(cameraID_);
43         cameraHostManager_ = nullptr;
44     }
45     deviceHDICallback_ = nullptr;
46     deviceSvcCallback_ = nullptr;
47 }
48 
GetCameraId()49 std::string HCameraDevice::GetCameraId()
50 {
51     return cameraID_;
52 }
53 
SetReleaseCameraDevice(bool isRelease)54 int32_t HCameraDevice::SetReleaseCameraDevice(bool isRelease)
55 {
56     isReleaseCameraDevice_ = isRelease;
57     return CAMERA_OK;
58 }
59 
IsReleaseCameraDevice()60 bool HCameraDevice::IsReleaseCameraDevice()
61 {
62     return isReleaseCameraDevice_;
63 }
64 
IsOpenedCameraDevice()65 bool HCameraDevice::IsOpenedCameraDevice()
66 {
67     return isOpenedCameraDevice_;
68 }
69 
GetSettings()70 std::shared_ptr<OHOS::Camera::CameraMetadata> HCameraDevice::GetSettings()
71 {
72     int32_t errCode;
73     std::shared_ptr<OHOS::Camera::CameraMetadata> ability = nullptr;
74     errCode = cameraHostManager_->GetCameraAbility(cameraID_, ability);
75     if (errCode != CAMERA_OK) {
76         MEDIA_ERR_LOG("HCameraDevice::GetSettings Failed to get Camera Ability: %{public}d", errCode);
77         return nullptr;
78     }
79 
80     if (!videoFrameRateRange_.empty() && ability != nullptr) {
81         bool status = false;
82         camera_metadata_item_t item;
83         int ret = OHOS::Camera::FindCameraMetadataItem(ability->get(), OHOS_CONTROL_FPS_RANGES, &item);
84         if (ret == CAM_META_ITEM_NOT_FOUND) {
85             status = ability->addEntry(OHOS_CONTROL_FPS_RANGES,
86                 videoFrameRateRange_.data(), videoFrameRateRange_.size());
87         } else if (ret == CAM_META_SUCCESS) {
88             status = ability->updateEntry(OHOS_CONTROL_FPS_RANGES,
89                 videoFrameRateRange_.data(), videoFrameRateRange_.size());
90         }
91         if (!status) {
92             MEDIA_ERR_LOG("HCameraDevice::Set fps renges Failed");
93         }
94     }
95     return ability;
96 }
97 
Open()98 int32_t HCameraDevice::Open()
99 {
100     CAMERA_SYNC_TRACE;
101     int32_t errorCode;
102     if (isOpenedCameraDevice_) {
103         MEDIA_ERR_LOG("HCameraDevice::Open failed, camera is busy");
104     }
105     if (deviceHDICallback_ == nullptr) {
106         deviceHDICallback_ = new(std::nothrow) CameraDeviceCallback(this);
107         if (deviceHDICallback_ == nullptr) {
108             MEDIA_ERR_LOG("HCameraDevice::Open CameraDeviceCallback allocation failed");
109             return CAMERA_ALLOC_ERROR;
110         }
111     }
112     bool isAllowed = true;
113     if (IsValidTokenId(callerToken_)) {
114         isAllowed = Security::AccessToken::PrivacyKit::IsAllowedUsingPermission(callerToken_, ACCESS_CAMERA);
115     }
116     if (!isAllowed) {
117         MEDIA_ERR_LOG("HCameraDevice::Open IsAllowedUsingPermission failed");
118         return CAMERA_ALLOC_ERROR;
119     }
120 
121     auto conflictDevices = cameraHostManager_->CameraConflictDetection(cameraID_);
122     // Destory conflict devices
123     for (auto &i : conflictDevices) {
124         i->Close();
125     }
126     MEDIA_INFO_LOG("HCameraDevice::Open Opening camera device: %{public}s", cameraID_.c_str());
127     errorCode = cameraHostManager_->OpenCameraDevice(cameraID_, deviceHDICallback_, hdiCameraDevice_);
128     if (errorCode == CAMERA_OK) {
129         isOpenedCameraDevice_ = true;
130         if (updateSettings_ != nullptr && hdiCameraDevice_ != nullptr) {
131             std::vector<uint8_t> setting;
132             OHOS::Camera::MetadataUtils::ConvertMetadataToVec(updateSettings_, setting);
133             CamRetCode rc = (CamRetCode)(hdiCameraDevice_->UpdateSettings(setting));
134             if (rc != HDI::Camera::V1_0::NO_ERROR) {
135                 MEDIA_ERR_LOG("HCameraDevice::Open Update setting failed with error Code: %{public}d", rc);
136                 return HdiToServiceError(rc);
137             }
138             updateSettings_ = nullptr;
139             MEDIA_DEBUG_LOG("HCameraDevice::Open Updated device settings");
140         }
141         if (hdiCameraDevice_) {
142             errorCode = HdiToServiceError((CamRetCode)(hdiCameraDevice_->SetResultMode(ON_CHANGED)));
143             cameraHostManager_->AddCameraDevice(cameraID_, this);
144             (void)OnCameraStatus(cameraID_, CAMERA_STATUS_UNAVAILABLE);
145         }
146     } else {
147         MEDIA_ERR_LOG("HCameraDevice::Open Failed to open camera");
148     }
149     return errorCode;
150 }
151 
Close()152 int32_t HCameraDevice::Close()
153 {
154     CAMERA_SYNC_TRACE;
155     std::lock_guard<std::mutex> lock(deviceLock_);
156     if (hdiCameraDevice_ != nullptr) {
157         MEDIA_INFO_LOG("HCameraDevice::Close Closing camera device: %{public}s", cameraID_.c_str());
158         hdiCameraDevice_->Close();
159         (void)OnCameraStatus(cameraID_, CAMERA_STATUS_AVAILABLE);
160     }
161     isOpenedCameraDevice_ = false;
162     hdiCameraDevice_ = nullptr;
163     if (streamOperator_) {
164         streamOperator_ = nullptr;
165     }
166     if (cameraHostManager_) {
167         cameraHostManager_->RemoveCameraDevice(cameraID_);
168     }
169     deviceHDICallback_ = nullptr;
170     deviceSvcCallback_ = nullptr;
171     return CAMERA_OK;
172 }
173 
Release()174 int32_t HCameraDevice::Release()
175 {
176     if (hdiCameraDevice_ != nullptr) {
177         Close();
178     }
179     return CAMERA_OK;
180 }
181 
GetEnabledResults(std::vector<int32_t> & results)182 int32_t HCameraDevice::GetEnabledResults(std::vector<int32_t> &results)
183 {
184     if (hdiCameraDevice_) {
185         CamRetCode rc = (CamRetCode)(hdiCameraDevice_->GetEnabledResults(results));
186         if (rc != HDI::Camera::V1_0::NO_ERROR) {
187             MEDIA_ERR_LOG("HCameraDevice::GetEnabledResults failed with error Code:%{public}d", rc);
188             return HdiToServiceError(rc);
189         }
190     } else {
191         MEDIA_ERR_LOG("HCameraDevice::GetEnabledResults GetEnabledResults hdiCameraDevice_ is nullptr");
192         return CAMERA_UNKNOWN_ERROR;
193     }
194     return CAMERA_OK;
195 }
196 
UpdateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)197 int32_t HCameraDevice::UpdateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings)
198 {
199     CAMERA_SYNC_TRACE;
200     if (settings == nullptr) {
201         MEDIA_ERR_LOG("HCameraDevice::UpdateSetting settings is null");
202         return CAMERA_INVALID_ARG;
203     }
204 
205     uint32_t count = OHOS::Camera::GetCameraMetadataItemCount(settings->get());
206     if (!count) {
207         MEDIA_DEBUG_LOG("HCameraDevice::UpdateSetting Nothing to update");
208         return CAMERA_OK;
209     }
210     if (updateSettings_) {
211         camera_metadata_item_t metadataItem;
212         for (uint32_t index = 0; index < count; index++) {
213             int ret = OHOS::Camera::GetCameraMetadataItem(settings->get(), index, &metadataItem);
214             if (ret != CAM_META_SUCCESS) {
215                 MEDIA_ERR_LOG("HCameraDevice::UpdateSetting Failed to get metadata item at index: %{public}d", index);
216                 return CAMERA_INVALID_ARG;
217             }
218             bool status = false;
219             uint32_t currentIndex;
220             ret = OHOS::Camera::FindCameraMetadataItemIndex(updateSettings_->get(), metadataItem.item, &currentIndex);
221             if (ret == CAM_META_ITEM_NOT_FOUND) {
222                 status = updateSettings_->addEntry(metadataItem.item, metadataItem.data.u8, metadataItem.count);
223             } else if (ret == CAM_META_SUCCESS) {
224                 status = updateSettings_->updateEntry(metadataItem.item, metadataItem.data.u8, metadataItem.count);
225             }
226             if (!status) {
227                 MEDIA_ERR_LOG("HCameraDevice::UpdateSetting Failed to update metadata item: %{public}d",
228                               metadataItem.item);
229                 return CAMERA_UNKNOWN_ERROR;
230             }
231         }
232     } else {
233         updateSettings_ = settings;
234     }
235     if (hdiCameraDevice_ != nullptr) {
236         std::vector<uint8_t> setting;
237         OHOS::Camera::MetadataUtils::ConvertMetadataToVec(updateSettings_, setting);
238         ReportMetadataDebugLog(updateSettings_);
239         GetFrameRateSetting(updateSettings_);
240 
241         CamRetCode rc = (CamRetCode)(hdiCameraDevice_->UpdateSettings(setting));
242         if (rc != HDI::Camera::V1_0::NO_ERROR) {
243             MEDIA_ERR_LOG("HCameraDevice::UpdateSetting failed with error Code: %{public}d", rc);
244             return HdiToServiceError(rc);
245         }
246         ReportFlashEvent(updateSettings_);
247         updateSettings_ = nullptr;
248     }
249     MEDIA_DEBUG_LOG("HCameraDevice::UpdateSetting Updated device settings");
250     return CAMERA_OK;
251 }
252 
ReportMetadataDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)253 void HCameraDevice::ReportMetadataDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings)
254 {
255     // debug log for focus mode
256     camera_metadata_item_t item;
257     int ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_FOCUS_MODE, &item);
258     if (ret != CAM_META_SUCCESS) {
259         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_FOCUS_MODE tag");
260     } else {
261         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_FOCUS_MODE value = %{public}d", item.data.u8[0]);
262     }
263 
264     // debug log for af regions
265     ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_AF_REGIONS, &item);
266     if (ret != CAM_META_SUCCESS) {
267         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_AF_REGIONS tag");
268     } else {
269         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_AF_REGIONS x = %{public}f, y = %{public}f",
270             item.data.f[0], item.data.f[1]);
271     }
272 
273     // debug log for af regions
274     ret = OHOS::Camera::FindCameraMetadataItem(settings->get(),
275         OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &item);
276     if (ret != CAM_META_SUCCESS) {
277         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_VIDEO_STABILIZATION_MODE tag");
278     } else {
279         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_VIDEO_STABILIZATION_MODE value = %{public}d",
280             item.data.u8[0]);
281     }
282 
283     // debug log for exposure mode
284     ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_EXPOSURE_MODE, &item);
285     if (ret != CAM_META_SUCCESS) {
286         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_EXPOSURE_MODE tag");
287     } else {
288         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_EXPOSURE_MODE value = %{public}d", item.data.u8[0]);
289     }
290 
291     // debug log for ae regions
292     ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_AE_REGIONS, &item);
293     if (ret != CAM_META_SUCCESS) {
294         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_AE_REGIONS tag");
295     } else {
296         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_AE_REGIONS x = %{public}f, y = %{public}f",
297             item.data.f[0], item.data.f[1]);
298     }
299 
300     // debug log for ae exposure compensation
301     ret = OHOS::Camera::FindCameraMetadataItem(settings->get(),
302         OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &item);
303     if (ret != CAM_META_SUCCESS) {
304         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_AE_EXPOSURE_COMPENSATION tag");
305     } else {
306         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_AE_EXPOSURE_COMPENSATION value = %{public}d",
307             item.data.u8[0]);
308     }
309 }
310 
GetFrameRateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)311 void HCameraDevice::GetFrameRateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings)
312 {
313     camera_metadata_item_t item;
314     int ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_FPS_RANGES, &item);
315     if (ret != CAM_META_SUCCESS) {
316         MEDIA_DEBUG_LOG("HCameraDevice::Failed to find OHOS_CONTROL_FPS_RANGES tag");
317     } else {
318         videoFrameRateRange_ = {item.data.i32[0], item.data.i32[1]};
319         MEDIA_DEBUG_LOG("HCameraDevice::find OHOS_CONTROL_FPS_RANGES min = %{public}d, max = %{public}d",
320             item.data.i32[0], item.data.i32[1]);
321     }
322 }
323 
ReportFlashEvent(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)324 void HCameraDevice::ReportFlashEvent(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings)
325 {
326     camera_metadata_item_t item;
327     camera_flash_mode_enum_t flashMode = OHOS_CAMERA_FLASH_MODE_ALWAYS_OPEN;
328     int ret = OHOS::Camera::FindCameraMetadataItem(settings->get(), OHOS_CONTROL_FLASH_MODE, &item);
329     if (ret == CAM_META_SUCCESS) {
330         flashMode = static_cast<camera_flash_mode_enum_t>(item.data.u8[0]);
331     } else {
332         MEDIA_ERR_LOG("CameraInput::GetFlashMode Failed with return code %{public}d", ret);
333     }
334 
335     if (flashMode == OHOS_CAMERA_FLASH_MODE_CLOSE) {
336         POWERMGR_SYSEVENT_FLASH_OFF();
337     } else {
338         POWERMGR_SYSEVENT_FLASH_ON();
339     }
340 }
341 
EnableResult(std::vector<int32_t> & results)342 int32_t HCameraDevice::EnableResult(std::vector<int32_t> &results)
343 {
344     if (results.empty()) {
345         MEDIA_ERR_LOG("HCameraDevice::EnableResult results vector empty");
346         return CAMERA_INVALID_ARG;
347     }
348 
349     if (hdiCameraDevice_ == nullptr) {
350         MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
351         return CAMERA_UNKNOWN_ERROR;
352     }
353 
354     CamRetCode rc = (CamRetCode)(hdiCameraDevice_->EnableResult(results));
355     if (rc != HDI::Camera::V1_0::NO_ERROR) {
356         MEDIA_ERR_LOG("HCameraDevice::EnableResult failed with error Code:%{public}d", rc);
357         return HdiToServiceError(rc);
358     }
359 
360     return CAMERA_OK;
361 }
362 
DisableResult(std::vector<int32_t> & results)363 int32_t HCameraDevice::DisableResult(std::vector<int32_t> &results)
364 {
365     if (results.empty()) {
366         MEDIA_ERR_LOG("HCameraDevice::DisableResult results vector empty");
367         return CAMERA_INVALID_ARG;
368     }
369 
370     if (hdiCameraDevice_ == nullptr) {
371         MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
372         return CAMERA_UNKNOWN_ERROR;
373     }
374 
375     CamRetCode rc = (CamRetCode)(hdiCameraDevice_->DisableResult(results));
376     if (rc != HDI::Camera::V1_0::NO_ERROR) {
377         MEDIA_ERR_LOG("HCameraDevice::DisableResult failed with error Code:%{public}d", rc);
378         return HdiToServiceError(rc);
379     }
380     return CAMERA_OK;
381 }
382 
SetCallback(sptr<ICameraDeviceServiceCallback> & callback)383 int32_t HCameraDevice::SetCallback(sptr<ICameraDeviceServiceCallback> &callback)
384 {
385     if (callback == nullptr) {
386         MEDIA_ERR_LOG("HCameraDevice::SetCallback callback is null");
387         return CAMERA_INVALID_ARG;
388     }
389     deviceSvcCallback_ = callback;
390     return CAMERA_OK;
391 }
392 
SetStatusCallback(std::map<int32_t,sptr<ICameraServiceCallback>> & callbacks)393 int32_t HCameraDevice::SetStatusCallback(std::map<int32_t, sptr<ICameraServiceCallback>> &callbacks)
394 {
395     std::lock_guard<std::mutex> lock(statusCbLock_);
396     MEDIA_INFO_LOG("HCameraDevice::SetStatusCallback callbacks size = %{public}zu",
397                    callbacks.size());
398     if (!statusSvcCallbacks_.empty()) {
399         MEDIA_ERR_LOG("HCameraDevice::SetStatusCallback statusSvcCallbacks_ is not empty, reset it");
400         statusSvcCallbacks_.clear();
401     }
402     for (auto it : callbacks) {
403         statusSvcCallbacks_[it.first] = it.second;
404     }
405     MEDIA_INFO_LOG("HCameraDevice::SetStatusCallback statusSvcCallbacks_ size = %{public}zu",
406                    statusSvcCallbacks_.size());
407     return CAMERA_OK;
408 }
409 
GetStreamOperator(sptr<IStreamOperatorCallback> callback,sptr<IStreamOperator> & streamOperator)410 int32_t HCameraDevice::GetStreamOperator(sptr<IStreamOperatorCallback> callback,
411     sptr<IStreamOperator> &streamOperator)
412 {
413     if (callback == nullptr) {
414         MEDIA_ERR_LOG("HCameraDevice::GetStreamOperator callback is null");
415         return CAMERA_INVALID_ARG;
416     }
417 
418     if (hdiCameraDevice_ == nullptr) {
419         MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
420         return CAMERA_UNKNOWN_ERROR;
421     }
422 
423     CamRetCode rc = (CamRetCode)(hdiCameraDevice_->GetStreamOperator(callback, streamOperator));
424     if (rc != HDI::Camera::V1_0::NO_ERROR) {
425         MEDIA_ERR_LOG("HCameraDevice::GetStreamOperator failed with error Code:%{public}d", rc);
426         return HdiToServiceError(rc);
427     }
428     streamOperator_ = streamOperator;
429     return CAMERA_OK;
430 }
431 
GetStreamOperator()432 sptr<IStreamOperator> HCameraDevice::GetStreamOperator()
433 {
434     return streamOperator_;
435 }
436 
OnError(const ErrorType type,const int32_t errorMsg)437 int32_t HCameraDevice::OnError(const ErrorType type, const int32_t errorMsg)
438 {
439     if (deviceSvcCallback_ != nullptr) {
440         int32_t errorType;
441         if (type == REQUEST_TIMEOUT) {
442             errorType = CAMERA_DEVICE_REQUEST_TIMEOUT;
443         } else if (type == DEVICE_PREEMPT) {
444             errorType = CAMERA_DEVICE_PREEMPTED;
445         } else {
446             errorType = CAMERA_UNKNOWN_ERROR;
447         }
448         deviceSvcCallback_->OnError(errorType, errorMsg);
449         CAMERA_SYSEVENT_FAULT(CreateMsg("CameraDeviceServiceCallback::OnError() is called!, errorType: %d,"
450                                         "errorMsg: %d", errorType, errorMsg));
451     }
452     return CAMERA_OK;
453 }
454 
OnCameraStatus(const std::string & cameraId,CameraStatus status)455 int32_t HCameraDevice::OnCameraStatus(const std::string& cameraId, CameraStatus status)
456 {
457     std::lock_guard<std::mutex> lock(statusCbLock_);
458     MEDIA_INFO_LOG("HCameraDevice::OnCameraStatus statusSvcCallbacks_ size = %{public}zu",
459                    statusSvcCallbacks_.size());
460     std::string callbackPids = "[";
461     for (auto it : statusSvcCallbacks_) {
462         auto item = it.second.promote();
463         if (item != nullptr) {
464             callbackPids += " " + std::to_string((int)it.first);
465             item->OnCameraStatusChanged(cameraId, status);
466         }
467     }
468     callbackPids += " ]";
469     MEDIA_INFO_LOG("HCameraDevice::OnCameraStatus OnCameraStatusChanged callbackPids = %{public}s, "
470                    "cameraId = %{public}s, cameraStatus = %{public}d",
471                    callbackPids.c_str(), cameraId.c_str(), status);
472     return CAMERA_OK;
473 }
474 
OnResult(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)475 int32_t HCameraDevice::OnResult(const uint64_t timestamp,
476                                 const std::shared_ptr<OHOS::Camera::CameraMetadata> &result)
477 {
478     if (deviceSvcCallback_ != nullptr) {
479         deviceSvcCallback_->OnResult(timestamp, result);
480     }
481     camera_metadata_item_t item;
482     common_metadata_header_t* metadata = result->get();
483     int ret = OHOS::Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FLASH_MODE, &item);
484     if (ret == 0) {
485         MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() OHOS_CONTROL_FLASH_MODE is %{public}d",
486                        item.data.u8[0]);
487     }
488     ret = OHOS::Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FLASH_STATE, &item);
489     if (ret == 0) {
490         MEDIA_INFO_LOG("CameraDeviceServiceCallback::OnResult() OHOS_CONTROL_FLASH_STATE is %{public}d",
491                        item.data.u8[0]);
492     }
493 
494     ret = OHOS::Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FOCUS_MODE, &item);
495     if (ret == CAM_META_SUCCESS) {
496         MEDIA_DEBUG_LOG("Focus mode: %{public}d", item.data.u8[0]);
497     }
498     ret = OHOS::Camera::FindCameraMetadataItem(metadata, OHOS_CONTROL_FOCUS_STATE, &item);
499     if (ret == CAM_META_SUCCESS) {
500         MEDIA_INFO_LOG("Focus state: %{public}d", item.data.u8[0]);
501     }
502     return CAMERA_OK;
503 }
504 
CameraDeviceCallback(sptr<HCameraDevice> hCameraDevice)505 CameraDeviceCallback::CameraDeviceCallback(sptr<HCameraDevice> hCameraDevice)
506 {
507     hCameraDevice_ = hCameraDevice;
508 }
509 
OnError(const ErrorType type,const int32_t errorCode)510 int32_t CameraDeviceCallback::OnError(const ErrorType type, const int32_t errorCode)
511 {
512     auto item = hCameraDevice_.promote();
513     if (item != nullptr) {
514         item->OnError(type, errorCode);
515     }
516     return CAMERA_OK;
517 }
518 
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)519 int32_t CameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result)
520 {
521     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult;
522     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(result, cameraResult);
523     auto item = hCameraDevice_.promote();
524     if (item != nullptr) {
525         item->OnResult(timestamp, cameraResult);
526     }
527     return CAMERA_OK;
528 }
529 } // namespace CameraStandard
530 } // namespace OHOS
531