• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "dcamera_client.h"
17 
18 #include "anonymous_string.h"
19 #include "camera_util.h"
20 #include "camera_metadata_operator.h"
21 #include "dcamera_input_callback.h"
22 #include "dcamera_manager_callback.h"
23 #include "dcamera_photo_callback.h"
24 #include "dcamera_preview_callback.h"
25 #include "dcamera_session_callback.h"
26 #include "dcamera_utils_tools.h"
27 #include "dcamera_video_callback.h"
28 #include "distributed_camera_constants.h"
29 #include "distributed_camera_errno.h"
30 #include "distributed_hardware_log.h"
31 #include "metadata_utils.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
DCameraClient(const std::string & dhId)35 DCameraClient::DCameraClient(const std::string& dhId)
36 {
37     DHLOGI("DCameraClient Constructor dhId: %s", GetAnonyString(dhId).c_str());
38     cameraId_ = dhId.substr(CAMERA_ID_PREFIX.size());
39     isInit_ = false;
40 }
41 
~DCameraClient()42 DCameraClient::~DCameraClient()
43 {
44     if (isInit_) {
45         UnInit();
46     }
47 }
48 
Init()49 int32_t DCameraClient::Init()
50 {
51     DHLOGI("Init cameraId: %s", GetAnonyString(cameraId_).c_str());
52     cameraManager_ = CameraStandard::CameraManager::GetInstance();
53     if (cameraManager_ == nullptr) {
54         DHLOGE("Init cameraManager getInstance failed");
55         return DCAMERA_BAD_VALUE;
56     }
57     cameraManager_->SetCallback(std::make_shared<DCameraManagerCallback>());
58 
59     std::vector<sptr<CameraStandard::CameraDevice>> cameraList = cameraManager_->GetSupportedCameras();
60     if (cameraList.empty()) {
61         DHLOGE("Init no camera device");
62         return DCAMERA_BAD_VALUE;
63     }
64     DHLOGI("Init camera size: %d", cameraList.size());
65     for (auto& info : cameraList) {
66         if (info->GetID() == cameraId_) {
67             DHLOGI("Init cameraInfo get id: %s", GetAnonyString(info->GetID()).c_str());
68             cameraInfo_ = info;
69             break;
70         }
71     }
72     if (cameraInfo_ == nullptr) {
73         DHLOGE("Init cameraInfo is null");
74         return DCAMERA_BAD_VALUE;
75     }
76 
77     isInit_ = true;
78     DHLOGI("Init %s success", GetAnonyString(cameraId_).c_str());
79     return DCAMERA_OK;
80 }
81 
UnInit()82 int32_t DCameraClient::UnInit()
83 {
84     DHLOGI("UnInit cameraId: %s", GetAnonyString(cameraId_).c_str());
85     if (cameraManager_ != nullptr) {
86         DHLOGI("UnInit unregister cameraManager callback");
87         cameraManager_->SetCallback(nullptr);
88     }
89 
90     isInit_ = false;
91     cameraInfo_ = nullptr;
92     cameraManager_ = nullptr;
93     DHLOGI("UnInit %s success", GetAnonyString(cameraId_).c_str());
94     return DCAMERA_OK;
95 }
96 
UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>> & settings)97 int32_t DCameraClient::UpdateSettings(std::vector<std::shared_ptr<DCameraSettings>>& settings)
98 {
99     DHLOGI("UpdateSettings cameraId: %s", GetAnonyString(cameraId_).c_str());
100     for (auto& setting : settings) {
101         switch (setting->type_) {
102             case UPDATE_METADATA: {
103                 DHLOGI("UpdateSettings %s update metadata settings", GetAnonyString(cameraId_).c_str());
104                 std::string dcSettingValue = setting->value_;
105                 std::string metadataStr = Base64Decode(dcSettingValue);
106                 FindCameraMetadata(metadataStr);
107 
108                 if (cameraInput_ == nullptr) {
109                     DHLOGE("UpdateSettings %s cameraInput is null", GetAnonyString(cameraId_).c_str());
110                     UpdateSettingCache(metadataStr);
111                     return DCAMERA_OK;
112                 }
113 
114                 int32_t ret = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetCameraSettings(metadataStr);
115                 if (ret != DCAMERA_OK) {
116                     DHLOGE("UpdateSettings %s update metadata settings failed, ret: %d",
117                         GetAnonyString(cameraId_).c_str(), ret);
118                     return ret;
119                 }
120                 break;
121             }
122             default: {
123                 DHLOGE("UpdateSettings unknown setting type");
124                 break;
125             }
126         }
127     }
128     DHLOGI("UpdateSettings %s success", GetAnonyString(cameraId_).c_str());
129     return DCAMERA_OK;
130 }
131 
UpdateSettingCache(const std::string & metadataStr)132 void DCameraClient::UpdateSettingCache(const std::string& metadataStr)
133 {
134     if (cameraMetadatas_.size() == DCAMERA_MAX_METADATA_SIZE) {
135         DHLOGE("UpdateSettingCache %s camera metadata oversize",
136             GetAnonyString(cameraId_).c_str());
137         cameraMetadatas_.pop();
138     }
139     cameraMetadatas_.push(metadataStr);
140 }
141 
FindCameraMetadata(const std::string & metadataStr)142 void DCameraClient::FindCameraMetadata(const std::string& metadataStr)
143 {
144     std::shared_ptr<Camera::CameraMetadata> cameraMetadata = Camera::MetadataUtils::DecodeFromString(metadataStr);
145     camera_metadata_item_t focusItem;
146     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_FOCUS_MODE, &focusItem);
147     if (ret == CAM_META_SUCCESS) {
148         DHLOGI("FindCameraMetadata focus mode: %d", focusItem.data.u8[0]);
149     } else {
150         DHLOGE("FindCameraMetadata %s find focus mode failed, ret: %d",
151             GetAnonyString(cameraId_).c_str(), ret);
152     }
153 
154     camera_metadata_item_t exposureItem;
155     ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_EXPOSURE_MODE, &exposureItem);
156     if (ret == CAM_META_SUCCESS) {
157         DHLOGI("FindCameraMetadata exposure mode: %d", exposureItem.data.u8[0]);
158     } else {
159         DHLOGE("FindCameraMetadata %s find exposure mode failed, ret: %d",
160             GetAnonyString(cameraId_).c_str(), ret);
161     }
162 
163     camera_metadata_item_t stabilityItem;
164     ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &stabilityItem);
165     if (ret == CAM_META_SUCCESS) {
166         DHLOGI("FindCameraMetadata stabilization mode: %d", stabilityItem.data.u8[0]);
167     } else {
168         DHLOGE("FindCameraMetadata %s find stabilization mode failed, ret: %d",
169             GetAnonyString(cameraId_).c_str(), ret);
170     }
171 }
172 
StartCapture(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos,sptr<Surface> & surface)173 int32_t DCameraClient::StartCapture(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos,
174     sptr<Surface>& surface)
175 {
176     DHLOGI("StartCapture cameraId: %s", GetAnonyString(cameraId_).c_str());
177     if ((photoOutput_ == nullptr) && (previewOutput_ == nullptr)) {
178         DHLOGI("StartCapture %s config capture session", GetAnonyString(cameraId_).c_str());
179         if (surface == nullptr) {
180             DHLOGE("StartCapture: input surface is nullptr.");
181             return DCAMERA_BAD_VALUE;
182         }
183         previewSurface_ = surface;
184         int32_t ret = ConfigCaptureSession(captureInfos);
185         if (ret != DCAMERA_OK) {
186             DHLOGE("StartCapture config capture session failed, cameraId: %s, ret: %d",
187                    GetAnonyString(cameraId_).c_str(), ret);
188             return CameraServiceErrorType(ret);
189         }
190     }
191 
192     for (auto& info : captureInfos) {
193         if ((info->streamType_ == CONTINUOUS_FRAME) || (!info->isCapture_)) {
194             continue;
195         }
196         int32_t ret = StartCaptureInner(info);
197         if (ret != DCAMERA_OK) {
198             DHLOGE("StartCapture failed, cameraId: %s, ret: %d",
199                 GetAnonyString(cameraId_).c_str(), ret);
200             return CameraServiceErrorType(ret);
201         }
202     }
203     DHLOGI("StartCapture %s success", GetAnonyString(cameraId_).c_str());
204     return DCAMERA_OK;
205 }
206 
CameraServiceErrorType(const int32_t errorType)207 int32_t DCameraClient::CameraServiceErrorType(const int32_t errorType)
208 {
209     if (errorType == CameraStandard::CamServiceError::CAMERA_ALLOC_ERROR) {
210         return DCAMERA_ALLOC_ERROR;
211     } else if (errorType == CameraStandard::CamServiceError::CAMERA_DEVICE_BUSY) {
212         return DCAMERA_DEVICE_BUSY;
213     }
214     return errorType;
215 }
216 
StopCapture()217 int32_t DCameraClient::StopCapture()
218 {
219     DHLOGI("StopCapture cameraId: %s", GetAnonyString(cameraId_).c_str());
220     if (previewOutput_ != nullptr) {
221         DHLOGI("StopCapture %s stop previewOutput", GetAnonyString(cameraId_).c_str());
222         int32_t ret = ((sptr<CameraStandard::PreviewOutput> &)previewOutput_)->Stop();
223         if (ret != DCAMERA_OK) {
224             DHLOGE("StopCapture videoOutput stop failed, cameraId: %s, ret: %d",
225                    GetAnonyString(cameraId_).c_str(), ret);
226         }
227         DHLOGI("StopCapture %s release previewOutput", GetAnonyString(cameraId_).c_str());
228         ret = previewOutput_->Release();
229         if (ret != DCAMERA_OK) {
230             DHLOGE("StopCapture previewOutput Release failed, cameraId: %s, ret: %d",
231                    GetAnonyString(cameraId_).c_str(), ret);
232         }
233         previewOutput_ = nullptr;
234     }
235 
236     if (photoOutput_ != nullptr) {
237         DHLOGI("StopCapture %s release photoOutput", GetAnonyString(cameraId_).c_str());
238         int32_t ret = photoOutput_->Release();
239         if (ret != DCAMERA_OK) {
240             DHLOGE("StopCapture photoOutput Release failed, cameraId: %s, ret: %d",
241                    GetAnonyString(cameraId_).c_str(), ret);
242         }
243         photoOutput_ = nullptr;
244     }
245 
246     if (cameraInput_ != nullptr) {
247         DHLOGI("StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str());
248         int32_t ret = cameraInput_->Close();
249         if (ret != DCAMERA_OK) {
250             DHLOGE("StopCapture cameraInput Close failed, cameraId: %s, ret: %d",
251                 GetAnonyString(cameraId_).c_str(), ret);
252         }
253         ret = cameraInput_->Release();
254         if (ret != DCAMERA_OK) {
255             DHLOGE("StopCapture cameraInput Release failed, cameraId: %s, ret: %d",
256                 GetAnonyString(cameraId_).c_str(), ret);
257         }
258         cameraInput_ = nullptr;
259     }
260     ReleaseCaptureSession();
261 
262     if (previewSurface_ != nullptr) {
263         DHLOGI("StopCapture %s preview surface unregister consumer listener",
264             GetAnonyString(cameraId_).c_str());
265         previewSurface_ = nullptr;
266     }
267 
268     if (photoSurface_ != nullptr) {
269         DHLOGI("StopCapture %s photo surface unregister consumer listener",
270             GetAnonyString(cameraId_).c_str());
271         int32_t ret = photoSurface_->UnregisterConsumerListener();
272         if (ret != DCAMERA_OK) {
273             DHLOGE("StopCapture %s photo surface unregister consumer listener failed, ret: %d",
274                 GetAnonyString(cameraId_).c_str(), ret);
275         }
276         photoListener_ = nullptr;
277         photoSurface_ = nullptr;
278     }
279 
280     DHLOGI("StopCapture %s success", GetAnonyString(cameraId_).c_str());
281     return DCAMERA_OK;
282 }
283 
ReleaseCaptureSession()284 void DCameraClient::ReleaseCaptureSession()
285 {
286     if (captureSession_ == nullptr) {
287         return;
288     }
289     DHLOGI("StopCapture %s stop captureSession", GetAnonyString(cameraId_).c_str());
290     int32_t ret = captureSession_->Stop();
291     if (ret != DCAMERA_OK) {
292         DHLOGE("StopCapture captureSession stop failed, cameraId: %s, ret: %d",
293                GetAnonyString(cameraId_).c_str(), ret);
294     }
295     DHLOGI("StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str());
296     ret = captureSession_->Release();
297     if (ret != DCAMERA_OK) {
298         DHLOGE("StopCapture captureSession Release failed, cameraId: %s, ret: %d",
299                GetAnonyString(cameraId_).c_str(), ret);
300     }
301     captureSession_ = nullptr;
302 }
303 
SetStateCallback(std::shared_ptr<StateCallback> & callback)304 int32_t DCameraClient::SetStateCallback(std::shared_ptr<StateCallback>& callback)
305 {
306     DHLOGI("SetStateCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
307     if (callback == nullptr) {
308         DHLOGE("SetStateCallback %s unregistering state callback", GetAnonyString(cameraId_).c_str());
309     }
310     stateCallback_ = callback;
311     return DCAMERA_OK;
312 }
313 
SetResultCallback(std::shared_ptr<ResultCallback> & callback)314 int32_t DCameraClient::SetResultCallback(std::shared_ptr<ResultCallback>& callback)
315 {
316     DHLOGI("SetResultCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
317     if (callback == nullptr) {
318         DHLOGE("SetResultCallback %s unregistering result callback", GetAnonyString(cameraId_).c_str());
319     }
320     resultCallback_ = callback;
321     return DCAMERA_OK;
322 }
323 
ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)324 int32_t DCameraClient::ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
325 {
326     DHLOGI("ConfigCaptureSession cameraId: %s", GetAnonyString(cameraId_).c_str());
327     int rv = cameraManager_->CreateCameraInput(cameraInfo_, &((sptr<CameraStandard::CameraInput> &)cameraInput_));
328     if (rv != DCAMERA_OK) {
329         DHLOGE("ConfigCaptureSession %s create cameraInput failed", GetAnonyString(cameraId_).c_str());
330         return DCAMERA_BAD_VALUE;
331     }
332     int32_t rc = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->Open();
333     if (rc != DCAMERA_OK) {
334         DHLOGE("ConfigCaptureSession cameraInput_ Open failed, cameraId: %s, ret: %d",
335             GetAnonyString(cameraId_).c_str(), rc);
336         return DCAMERA_BAD_VALUE;
337     }
338     std::shared_ptr<DCameraInputCallback> inputCallback = std::make_shared<DCameraInputCallback>(stateCallback_);
339     ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetErrorCallback(inputCallback);
340 
341     while (!cameraMetadatas_.empty()) {
342         std::string metadataStr = cameraMetadatas_.front();
343         FindCameraMetadata(metadataStr);
344         int32_t ret = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetCameraSettings(metadataStr);
345         if (ret != DCAMERA_OK) {
346             DHLOGE("ConfigCaptureSession %s set camera settings failed, ret: %d",
347                 GetAnonyString(cameraId_).c_str(), ret);
348             return ret;
349         }
350         cameraMetadatas_.pop();
351     }
352 
353     rv = cameraManager_->CreateCaptureSession(&captureSession_);
354     if (rv != DCAMERA_OK) {
355         DHLOGE("ConfigCaptureSession %s create captureSession failed",
356                GetAnonyString(cameraId_).c_str());
357         return DCAMERA_BAD_VALUE;
358     }
359 
360     std::shared_ptr<DCameraSessionCallback> sessionCallback = std::make_shared<DCameraSessionCallback>(stateCallback_);
361     captureSession_->SetFocusCallback(sessionCallback);
362     captureSession_->SetCallback(sessionCallback);
363 
364     int32_t ret = CreateCaptureOutput(captureInfos);
365     if (ret != DCAMERA_OK) {
366         DHLOGE("ConfigCaptureSession create capture output failed, cameraId: %s, ret: %d",
367                GetAnonyString(cameraId_).c_str(), ret);
368         return ret;
369     }
370 
371     return ConfigCaptureSessionInner();
372 }
373 
ConfigCaptureSessionInner()374 int32_t DCameraClient::ConfigCaptureSessionInner()
375 {
376     int32_t ret = captureSession_->BeginConfig();
377     if (ret != DCAMERA_OK) {
378         DHLOGE("ConfigCaptureSession %s config captureSession failed, ret: %d",
379                GetAnonyString(cameraId_).c_str(), ret);
380         return ret;
381     }
382 
383     ret = captureSession_->AddInput(cameraInput_);
384     if (ret != DCAMERA_OK) {
385         DHLOGE("ConfigCaptureSession %s add cameraInput to captureSession failed, ret: %d",
386                GetAnonyString(cameraId_).c_str(), ret);
387         return ret;
388     }
389 
390     if (photoOutput_ != nullptr) {
391         ret = captureSession_->AddOutput(photoOutput_);
392         if (ret != DCAMERA_OK) {
393             DHLOGE("ConfigCaptureSession %s add photoOutput to captureSession failed, ret: %d",
394                    GetAnonyString(cameraId_).c_str(), ret);
395             return ret;
396         }
397     }
398 
399     if (previewOutput_ != nullptr) {
400         ret = captureSession_->AddOutput(previewOutput_);
401         if (ret != DCAMERA_OK) {
402             DHLOGE("ConfigCaptureSession %s add previewOutput to captureSession failed, ret: %d",
403                    GetAnonyString(cameraId_).c_str(), ret);
404             return ret;
405         }
406     }
407 
408     ret = captureSession_->CommitConfig();
409     if (ret != DCAMERA_OK) {
410         DHLOGE("ConfigCaptureSession %s commit captureSession failed, ret: %d",
411                GetAnonyString(cameraId_).c_str(), ret);
412         return ret;
413     }
414 
415     ret = captureSession_->Start();
416     if (ret != DCAMERA_OK) {
417         DHLOGE("ConfigCaptureSession %s start captureSession failed, ret: %d",
418                GetAnonyString(cameraId_).c_str(), ret);
419     }
420 
421     DHLOGI("ConfigCaptureSession %s success", GetAnonyString(cameraId_).c_str());
422     return DCAMERA_OK;
423 }
424 
CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)425 int32_t DCameraClient::CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
426 {
427     if (captureInfos.empty()) {
428         DHLOGE("CreateCaptureOutput no capture info, cameraId: %s", GetAnonyString(cameraId_).c_str());
429         return DCAMERA_BAD_VALUE;
430     }
431 
432     for (auto& info : captureInfos) {
433         if (info->streamType_ == SNAPSHOT_FRAME) {
434             int32_t ret = CreatePhotoOutput(info);
435             if (ret != DCAMERA_OK) {
436                 DHLOGE("CreateCaptureOutput %s create photo output failed, ret: %d",
437                        GetAnonyString(cameraId_).c_str(), ret);
438                 return ret;
439             }
440         } else if (info->streamType_ == CONTINUOUS_FRAME) {
441             int32_t ret = CreatePreviewOutput(info);
442             if (ret != DCAMERA_OK) {
443                 DHLOGE("CreateCaptureOutput %s create video output failed, ret: %d",
444                        GetAnonyString(cameraId_).c_str(), ret);
445                 return ret;
446             }
447         } else {
448             DHLOGE("CreateCaptureOutput unknown stream type");
449             return DCAMERA_BAD_VALUE;
450         }
451     }
452     return DCAMERA_OK;
453 }
454 
CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)455 int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
456 {
457     DHLOGI("CreatePhotoOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d",
458         GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_,
459         info->isCapture_);
460     photoSurface_ = IConsumerSurface::Create();
461     photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_);
462     photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoListener_);
463     CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_);
464     CameraStandard::Size photoSize = {info->width_, info->height_};
465     CameraStandard::Profile photoProfile(photoFormat, photoSize);
466     sptr<IBufferProducer> bp = photoSurface_->GetProducer();
467     int32_t rv = cameraManager_->CreatePhotoOutput(
468         photoProfile, bp, &((sptr<CameraStandard::PhotoOutput> &)photoOutput_));
469     if (rv != DCAMERA_OK) {
470         DHLOGE("CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str());
471         return DCAMERA_BAD_VALUE;
472     }
473     std::shared_ptr<DCameraPhotoCallback> photoCallback = std::make_shared<DCameraPhotoCallback>(stateCallback_);
474     ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->SetCallback(photoCallback);
475     DHLOGI("CreatePhotoOutput %s success", GetAnonyString(cameraId_).c_str());
476     return DCAMERA_OK;
477 }
478 
CreatePreviewOutput(std::shared_ptr<DCameraCaptureInfo> & info)479 int32_t DCameraClient::CreatePreviewOutput(std::shared_ptr<DCameraCaptureInfo>& info)
480 {
481     DHLOGI("CreatePreviewOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d",
482         GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_,
483         info->isCapture_);
484     CameraStandard::CameraFormat previewFormat = ConvertToCameraFormat(info->format_);
485     CameraStandard::Size previewSize = {info->width_, info->height_};
486     CameraStandard::Profile previewProfile(previewFormat, previewSize);
487     int32_t rv = cameraManager_->CreatePreviewOutput(
488         previewProfile, previewSurface_, &((sptr<CameraStandard::PreviewOutput> &)previewOutput_));
489     if (rv != DCAMERA_OK) {
490         DHLOGE("CreatePreviewOutput %s create preview output failed", GetAnonyString(cameraId_).c_str());
491         return DCAMERA_BAD_VALUE;
492     }
493     auto previewCallback = std::make_shared<DCameraPreviewCallback>(stateCallback_);
494     ((sptr<CameraStandard::PreviewOutput> &)previewOutput_)->SetCallback(previewCallback);
495     DHLOGI("CreatePreviewOutput %s success", GetAnonyString(cameraId_).c_str());
496     return DCAMERA_OK;
497 }
498 
ConvertToCameraFormat(int32_t format)499 CameraStandard::CameraFormat DCameraClient::ConvertToCameraFormat(int32_t format)
500 {
501     CameraStandard::CameraFormat ret = CameraStandard::CameraFormat::CAMERA_FORMAT_INVALID;
502     DCameraFormat df = static_cast<DCameraFormat>(format);
503     switch (df) {
504         case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888:
505             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888;
506             break;
507         case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888:
508             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888;
509             break;
510         case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP:
511             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP;
512             break;
513         case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG:
514             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG;
515             break;
516         default:
517             break;
518     }
519     return ret;
520 }
521 
StartCaptureInner(std::shared_ptr<DCameraCaptureInfo> & info)522 int32_t DCameraClient::StartCaptureInner(std::shared_ptr<DCameraCaptureInfo>& info)
523 {
524     if (info->streamType_ != SNAPSHOT_FRAME) {
525         DHLOGE("StartCaptureInner unknown stream type");
526         return DCAMERA_BAD_VALUE;
527     }
528     return StartPhotoOutput(info);
529 }
530 
StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)531 int32_t DCameraClient::StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
532 {
533     DHLOGI("StartPhotoOutput cameraId: %s", GetAnonyString(cameraId_).c_str());
534     if (photoOutput_ == nullptr) {
535         DHLOGE("StartPhotoOutput photoOutput is null");
536         return DCAMERA_BAD_VALUE;
537     }
538 
539     std::vector<std::shared_ptr<DCameraSettings>> captureSettings = info->captureSettings_;
540     std::string metadataSetting;
541     for (const auto& setting : captureSettings) {
542         if (setting->type_ == UPDATE_METADATA) {
543             DHLOGI("StartPhotoOutput %s update metadata settings", GetAnonyString(cameraId_).c_str());
544             metadataSetting = setting->value_;
545         }
546     }
547 
548     if (metadataSetting.empty()) {
549         DHLOGE("StartPhotoOutput no metadata settings to update");
550         int32_t ret = ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->Capture();
551         if (ret != DCAMERA_OK) {
552             DHLOGE("StartPhotoOutput %s photoOutput capture failed, ret: %d",
553                 GetAnonyString(cameraId_).c_str(), ret);
554             return ret;
555         }
556         return DCAMERA_OK;
557     }
558 
559     std::string metadataStr = Base64Decode(metadataSetting);
560     std::shared_ptr<Camera::CameraMetadata> cameraMetadata = Camera::MetadataUtils::DecodeFromString(metadataStr);
561     std::shared_ptr<CameraStandard::PhotoCaptureSetting> photoCaptureSetting =
562         std::make_shared<CameraStandard::PhotoCaptureSetting>();
563     SetPhotoCaptureRotation(cameraMetadata, photoCaptureSetting);
564     SetPhotoCaptureQuality(cameraMetadata, photoCaptureSetting);
565     SetPhotoCaptureLocation(cameraMetadata, photoCaptureSetting);
566     int32_t ret = ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->Capture(photoCaptureSetting);
567     if (ret != DCAMERA_OK) {
568         DHLOGE("StartPhotoOutput %s photoOutput capture failed, ret: %d",
569             GetAnonyString(cameraId_).c_str(), ret);
570         return ret;
571     }
572     return DCAMERA_OK;
573 }
574 
SetPhotoCaptureRotation(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)575 void DCameraClient::SetPhotoCaptureRotation(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
576     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
577 {
578     uint32_t rotationCount = 1;
579     camera_metadata_item_t item;
580     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_ORIENTATION, &item);
581     if ((ret == CAM_META_SUCCESS) && (rotationCount == item.count)) {
582         int32_t oriSetRotate = item.data.i32[0];
583         int32_t setRotate = oriSetRotate;
584         int32_t sensorRotate = 0;
585         std::string abilityString = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->GetCameraSettings();
586         std::shared_ptr<Camera::CameraMetadata> cameraAbility = Camera::MetadataUtils::DecodeFromString(abilityString);
587         if (cameraAbility != nullptr) {
588             camera_metadata_item_t itemSensor;
589             ret = Camera::FindCameraMetadataItem(cameraAbility->get(), OHOS_SENSOR_ORIENTATION, &itemSensor);
590             if ((ret == CAM_META_SUCCESS) && (rotationCount == itemSensor.count)) {
591                 sensorRotate = itemSensor.data.i32[0];
592                 setRotate = setRotate - sensorRotate;
593             }
594         }
595 
596         if (setRotate < 0) {
597             setRotate += DCAMERA_CAPTURE_ROTATE_360;
598         }
599 
600         CameraStandard::PhotoCaptureSetting::RotationConfig rotation =
601             static_cast<CameraStandard::PhotoCaptureSetting::RotationConfig>(setRotate);
602         photoCaptureSetting->SetRotation(rotation);
603         DHLOGI("SetPhotoCaptureRotation %s photo set %d rotation: %d setRotate: %d sensorRotate: %d oriSetRotate: %d",
604             GetAnonyString(cameraId_).c_str(), item.count, rotation, setRotate, sensorRotate, oriSetRotate);
605     }
606 }
607 
SetPhotoCaptureQuality(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)608 void DCameraClient::SetPhotoCaptureQuality(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
609     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
610 {
611     uint32_t qualityCount = 1;
612     camera_metadata_item_t item;
613     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_QUALITY, &item);
614     if ((ret == CAM_META_SUCCESS) && (qualityCount == item.count)) {
615         CameraStandard::PhotoCaptureSetting::QualityLevel quality =
616             static_cast<CameraStandard::PhotoCaptureSetting::QualityLevel>(item.data.u8[0]);
617         photoCaptureSetting->SetQuality(quality);
618         DHLOGI("SetPhotoCaptureQuality %s photo capture settings set %d quality: %d",
619             GetAnonyString(cameraId_).c_str(), item.count, quality);
620     }
621 }
622 
SetPhotoCaptureLocation(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)623 void DCameraClient::SetPhotoCaptureLocation(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
624     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
625 {
626     uint32_t locationCount = 3;
627     camera_metadata_item_t item;
628     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_GPS_COORDINATES, &item);
629     if ((ret == CAM_META_SUCCESS) && (locationCount == item.count)) {
630         int32_t latitudeIndex = 0;
631         int32_t longitudeIndex = 1;
632         int32_t altitudeIndex = 2;
633         std::unique_ptr<CameraStandard::Location> location = std::make_unique<CameraStandard::Location>();
634         location->latitude = item.data.d[latitudeIndex];
635         location->longitude = item.data.d[longitudeIndex];
636         location->altitude = item.data.d[altitudeIndex];
637         photoCaptureSetting->SetLocation(location);
638         DHLOGI("SetPhotoCaptureLocation %s photo capture settings set %d location: " +
639             "latitude=%f, longitude=%f, altitude=%f", GetAnonyString(cameraId_).c_str(), item.count,
640             item.data.d[latitudeIndex], item.data.d[longitudeIndex], item.data.d[altitudeIndex]);
641     }
642 }
643 } // namespace DistributedHardware
644 } // namespace OHOS
645