• 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     StopOutput();
221 
222     if (cameraInput_ != nullptr) {
223         DHLOGI("StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str());
224         int32_t ret = cameraInput_->Close();
225         if (ret != DCAMERA_OK) {
226             DHLOGE("StopCapture cameraInput Close failed, cameraId: %s, ret: %d",
227                 GetAnonyString(cameraId_).c_str(), ret);
228         }
229         ret = cameraInput_->Release();
230         if (ret != DCAMERA_OK) {
231             DHLOGE("StopCapture cameraInput Release failed, cameraId: %s, ret: %d",
232                 GetAnonyString(cameraId_).c_str(), ret);
233         }
234         cameraInput_ = nullptr;
235     }
236     ReleaseCaptureSession();
237 
238     if (previewSurface_ != nullptr) {
239         DHLOGI("StopCapture %s preview surface unregister consumer listener",
240             GetAnonyString(cameraId_).c_str());
241         previewSurface_ = nullptr;
242     }
243 
244     if (photoSurface_ != nullptr) {
245         DHLOGI("StopCapture %s photo surface unregister consumer listener",
246             GetAnonyString(cameraId_).c_str());
247         int32_t ret = photoSurface_->UnregisterConsumerListener();
248         if (ret != DCAMERA_OK) {
249             DHLOGE("StopCapture %s photo surface unregister consumer listener failed, ret: %d",
250                 GetAnonyString(cameraId_).c_str(), ret);
251         }
252         photoListener_ = nullptr;
253         photoSurface_ = nullptr;
254     }
255 
256     DHLOGI("StopCapture %s success", GetAnonyString(cameraId_).c_str());
257     return DCAMERA_OK;
258 }
259 
StopOutput()260 void DCameraClient::StopOutput()
261 {
262     if (previewOutput_ != nullptr) {
263         DHLOGI("StopCapture %s stop previewOutput", GetAnonyString(cameraId_).c_str());
264         int32_t ret = ((sptr<CameraStandard::PreviewOutput> &)previewOutput_)->Stop();
265         if (ret != DCAMERA_OK) {
266             DHLOGE("StopCapture videoOutput stop failed, cameraId: %s, ret: %d",
267                    GetAnonyString(cameraId_).c_str(), ret);
268         }
269         DHLOGI("StopCapture %s release previewOutput", GetAnonyString(cameraId_).c_str());
270         ret = previewOutput_->Release();
271         if (ret != DCAMERA_OK) {
272             DHLOGE("StopCapture previewOutput Release failed, cameraId: %s, ret: %d",
273                    GetAnonyString(cameraId_).c_str(), ret);
274         }
275         previewOutput_ = nullptr;
276     }
277 
278     if (photoOutput_ != nullptr) {
279         DHLOGI("StopCapture %s release photoOutput", GetAnonyString(cameraId_).c_str());
280         int32_t ret = photoOutput_->Release();
281         if (ret != DCAMERA_OK) {
282             DHLOGE("StopCapture photoOutput Release failed, cameraId: %s, ret: %d",
283                    GetAnonyString(cameraId_).c_str(), ret);
284         }
285         photoOutput_ = nullptr;
286     }
287 }
288 
ReleaseCaptureSession()289 void DCameraClient::ReleaseCaptureSession()
290 {
291     if (captureSession_ == nullptr) {
292         return;
293     }
294     DHLOGI("StopCapture %s stop captureSession", GetAnonyString(cameraId_).c_str());
295     int32_t ret = captureSession_->Stop();
296     if (ret != DCAMERA_OK) {
297         DHLOGE("StopCapture captureSession stop failed, cameraId: %s, ret: %d",
298                GetAnonyString(cameraId_).c_str(), ret);
299     }
300     DHLOGI("StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str());
301     ret = captureSession_->Release();
302     if (ret != DCAMERA_OK) {
303         DHLOGE("StopCapture captureSession Release failed, cameraId: %s, ret: %d",
304                GetAnonyString(cameraId_).c_str(), ret);
305     }
306     captureSession_ = nullptr;
307 }
308 
SetStateCallback(std::shared_ptr<StateCallback> & callback)309 int32_t DCameraClient::SetStateCallback(std::shared_ptr<StateCallback>& callback)
310 {
311     DHLOGI("SetStateCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
312     if (callback == nullptr) {
313         DHLOGE("SetStateCallback %s unregistering state callback", GetAnonyString(cameraId_).c_str());
314     }
315     stateCallback_ = callback;
316     return DCAMERA_OK;
317 }
318 
SetResultCallback(std::shared_ptr<ResultCallback> & callback)319 int32_t DCameraClient::SetResultCallback(std::shared_ptr<ResultCallback>& callback)
320 {
321     DHLOGI("SetResultCallback cameraId: %s", GetAnonyString(cameraId_).c_str());
322     if (callback == nullptr) {
323         DHLOGE("SetResultCallback %s unregistering result callback", GetAnonyString(cameraId_).c_str());
324     }
325     resultCallback_ = callback;
326     return DCAMERA_OK;
327 }
328 
ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)329 int32_t DCameraClient::ConfigCaptureSession(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
330 {
331     DHLOGI("ConfigCaptureSession cameraId: %s", GetAnonyString(cameraId_).c_str());
332     int rv = cameraManager_->CreateCameraInput(cameraInfo_, &((sptr<CameraStandard::CameraInput> &)cameraInput_));
333     if (rv != DCAMERA_OK) {
334         DHLOGE("ConfigCaptureSession %s create cameraInput failed", GetAnonyString(cameraId_).c_str());
335         return DCAMERA_BAD_VALUE;
336     }
337     int32_t rc = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->Open();
338     if (rc != DCAMERA_OK) {
339         DHLOGE("ConfigCaptureSession cameraInput_ Open failed, cameraId: %s, ret: %d",
340             GetAnonyString(cameraId_).c_str(), rc);
341         return DCAMERA_BAD_VALUE;
342     }
343     std::shared_ptr<DCameraInputCallback> inputCallback = std::make_shared<DCameraInputCallback>(stateCallback_);
344     ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetErrorCallback(inputCallback);
345 
346     while (!cameraMetadatas_.empty()) {
347         std::string metadataStr = cameraMetadatas_.front();
348         FindCameraMetadata(metadataStr);
349         int32_t ret = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->SetCameraSettings(metadataStr);
350         if (ret != DCAMERA_OK) {
351             DHLOGE("ConfigCaptureSession %s set camera settings failed, ret: %d",
352                 GetAnonyString(cameraId_).c_str(), ret);
353             return ret;
354         }
355         cameraMetadatas_.pop();
356     }
357 
358     rv = cameraManager_->CreateCaptureSession(&captureSession_);
359     if (rv != DCAMERA_OK) {
360         DHLOGE("ConfigCaptureSession %s create captureSession failed",
361                GetAnonyString(cameraId_).c_str());
362         return DCAMERA_BAD_VALUE;
363     }
364 
365     std::shared_ptr<DCameraSessionCallback> sessionCallback = std::make_shared<DCameraSessionCallback>(stateCallback_);
366     captureSession_->SetFocusCallback(sessionCallback);
367     captureSession_->SetCallback(sessionCallback);
368 
369     int32_t ret = CreateCaptureOutput(captureInfos);
370     if (ret != DCAMERA_OK) {
371         DHLOGE("ConfigCaptureSession create capture output failed, cameraId: %s, ret: %d",
372                GetAnonyString(cameraId_).c_str(), ret);
373         return ret;
374     }
375 
376     return ConfigCaptureSessionInner();
377 }
378 
ConfigCaptureSessionInner()379 int32_t DCameraClient::ConfigCaptureSessionInner()
380 {
381     int32_t ret = captureSession_->BeginConfig();
382     if (ret != DCAMERA_OK) {
383         DHLOGE("ConfigCaptureSession %s config captureSession failed, ret: %d",
384                GetAnonyString(cameraId_).c_str(), ret);
385         return ret;
386     }
387 
388     ret = captureSession_->AddInput(cameraInput_);
389     if (ret != DCAMERA_OK) {
390         DHLOGE("ConfigCaptureSession %s add cameraInput to captureSession failed, ret: %d",
391                GetAnonyString(cameraId_).c_str(), ret);
392         return ret;
393     }
394 
395     if (photoOutput_ != nullptr) {
396         ret = captureSession_->AddOutput(photoOutput_);
397         if (ret != DCAMERA_OK) {
398             DHLOGE("ConfigCaptureSession %s add photoOutput to captureSession failed, ret: %d",
399                    GetAnonyString(cameraId_).c_str(), ret);
400             return ret;
401         }
402     }
403 
404     if (previewOutput_ != nullptr) {
405         ret = captureSession_->AddOutput(previewOutput_);
406         if (ret != DCAMERA_OK) {
407             DHLOGE("ConfigCaptureSession %s add previewOutput to captureSession failed, ret: %d",
408                    GetAnonyString(cameraId_).c_str(), ret);
409             return ret;
410         }
411     }
412 
413     ret = captureSession_->CommitConfig();
414     if (ret != DCAMERA_OK) {
415         DHLOGE("ConfigCaptureSession %s commit captureSession failed, ret: %d",
416                GetAnonyString(cameraId_).c_str(), ret);
417         return ret;
418     }
419 
420     ret = captureSession_->Start();
421     if (ret != DCAMERA_OK) {
422         DHLOGE("ConfigCaptureSession %s start captureSession failed, ret: %d",
423                GetAnonyString(cameraId_).c_str(), ret);
424     }
425 
426     DHLOGI("ConfigCaptureSession %s success", GetAnonyString(cameraId_).c_str());
427     return DCAMERA_OK;
428 }
429 
CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>> & captureInfos)430 int32_t DCameraClient::CreateCaptureOutput(std::vector<std::shared_ptr<DCameraCaptureInfo>>& captureInfos)
431 {
432     if (captureInfos.empty()) {
433         DHLOGE("CreateCaptureOutput no capture info, cameraId: %s", GetAnonyString(cameraId_).c_str());
434         return DCAMERA_BAD_VALUE;
435     }
436 
437     for (auto& info : captureInfos) {
438         if (info->streamType_ == SNAPSHOT_FRAME) {
439             int32_t ret = CreatePhotoOutput(info);
440             if (ret != DCAMERA_OK) {
441                 DHLOGE("CreateCaptureOutput %s create photo output failed, ret: %d",
442                        GetAnonyString(cameraId_).c_str(), ret);
443                 return ret;
444             }
445         } else if (info->streamType_ == CONTINUOUS_FRAME) {
446             int32_t ret = CreatePreviewOutput(info);
447             if (ret != DCAMERA_OK) {
448                 DHLOGE("CreateCaptureOutput %s create video output failed, ret: %d",
449                        GetAnonyString(cameraId_).c_str(), ret);
450                 return ret;
451             }
452         } else {
453             DHLOGE("CreateCaptureOutput unknown stream type");
454             return DCAMERA_BAD_VALUE;
455         }
456     }
457     return DCAMERA_OK;
458 }
459 
CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)460 int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
461 {
462     DHLOGI("CreatePhotoOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d",
463         GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_,
464         info->isCapture_);
465     photoSurface_ = IConsumerSurface::Create();
466     photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_);
467     photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoListener_);
468     CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_);
469     CameraStandard::Size photoSize = {info->width_, info->height_};
470     CameraStandard::Profile photoProfile(photoFormat, photoSize);
471     sptr<IBufferProducer> bp = photoSurface_->GetProducer();
472     int32_t rv = cameraManager_->CreatePhotoOutput(
473         photoProfile, bp, &((sptr<CameraStandard::PhotoOutput> &)photoOutput_));
474     if (rv != DCAMERA_OK) {
475         DHLOGE("CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str());
476         return DCAMERA_BAD_VALUE;
477     }
478     std::shared_ptr<DCameraPhotoCallback> photoCallback = std::make_shared<DCameraPhotoCallback>(stateCallback_);
479     ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->SetCallback(photoCallback);
480     DHLOGI("CreatePhotoOutput %s success", GetAnonyString(cameraId_).c_str());
481     return DCAMERA_OK;
482 }
483 
CreatePreviewOutput(std::shared_ptr<DCameraCaptureInfo> & info)484 int32_t DCameraClient::CreatePreviewOutput(std::shared_ptr<DCameraCaptureInfo>& info)
485 {
486     DHLOGI("CreatePreviewOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d",
487         GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_,
488         info->isCapture_);
489     CameraStandard::CameraFormat previewFormat = ConvertToCameraFormat(info->format_);
490     CameraStandard::Size previewSize = {info->width_, info->height_};
491     CameraStandard::Profile previewProfile(previewFormat, previewSize);
492     int32_t rv = cameraManager_->CreatePreviewOutput(
493         previewProfile, previewSurface_, &((sptr<CameraStandard::PreviewOutput> &)previewOutput_));
494     if (rv != DCAMERA_OK) {
495         DHLOGE("CreatePreviewOutput %s create preview output failed", GetAnonyString(cameraId_).c_str());
496         return DCAMERA_BAD_VALUE;
497     }
498     auto previewCallback = std::make_shared<DCameraPreviewCallback>(stateCallback_);
499     ((sptr<CameraStandard::PreviewOutput> &)previewOutput_)->SetCallback(previewCallback);
500     DHLOGI("CreatePreviewOutput %s success", GetAnonyString(cameraId_).c_str());
501     return DCAMERA_OK;
502 }
503 
ConvertToCameraFormat(int32_t format)504 CameraStandard::CameraFormat DCameraClient::ConvertToCameraFormat(int32_t format)
505 {
506     CameraStandard::CameraFormat ret = CameraStandard::CameraFormat::CAMERA_FORMAT_INVALID;
507     DCameraFormat df = static_cast<DCameraFormat>(format);
508     switch (df) {
509         case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888:
510             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888;
511             break;
512         case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888:
513             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888;
514             break;
515         case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP:
516             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP;
517             break;
518         case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG:
519             ret = CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG;
520             break;
521         default:
522             break;
523     }
524     return ret;
525 }
526 
StartCaptureInner(std::shared_ptr<DCameraCaptureInfo> & info)527 int32_t DCameraClient::StartCaptureInner(std::shared_ptr<DCameraCaptureInfo>& info)
528 {
529     if (info->streamType_ != SNAPSHOT_FRAME) {
530         DHLOGE("StartCaptureInner unknown stream type");
531         return DCAMERA_BAD_VALUE;
532     }
533     return StartPhotoOutput(info);
534 }
535 
StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo> & info)536 int32_t DCameraClient::StartPhotoOutput(std::shared_ptr<DCameraCaptureInfo>& info)
537 {
538     DHLOGI("StartPhotoOutput cameraId: %s", GetAnonyString(cameraId_).c_str());
539     if (photoOutput_ == nullptr) {
540         DHLOGE("StartPhotoOutput photoOutput is null");
541         return DCAMERA_BAD_VALUE;
542     }
543 
544     std::vector<std::shared_ptr<DCameraSettings>> captureSettings = info->captureSettings_;
545     std::string metadataSetting;
546     for (const auto& setting : captureSettings) {
547         if (setting->type_ == UPDATE_METADATA) {
548             DHLOGI("StartPhotoOutput %s update metadata settings", GetAnonyString(cameraId_).c_str());
549             metadataSetting = setting->value_;
550         }
551     }
552 
553     if (metadataSetting.empty()) {
554         DHLOGE("StartPhotoOutput no metadata settings to update");
555         int32_t ret = ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->Capture();
556         if (ret != DCAMERA_OK) {
557             DHLOGE("StartPhotoOutput %s photoOutput capture failed, ret: %d",
558                 GetAnonyString(cameraId_).c_str(), ret);
559             return ret;
560         }
561         return DCAMERA_OK;
562     }
563 
564     std::string metadataStr = Base64Decode(metadataSetting);
565     std::shared_ptr<Camera::CameraMetadata> cameraMetadata = Camera::MetadataUtils::DecodeFromString(metadataStr);
566     std::shared_ptr<CameraStandard::PhotoCaptureSetting> photoCaptureSetting =
567         std::make_shared<CameraStandard::PhotoCaptureSetting>();
568     SetPhotoCaptureRotation(cameraMetadata, photoCaptureSetting);
569     SetPhotoCaptureQuality(cameraMetadata, photoCaptureSetting);
570     SetPhotoCaptureLocation(cameraMetadata, photoCaptureSetting);
571     int32_t ret = ((sptr<CameraStandard::PhotoOutput> &)photoOutput_)->Capture(photoCaptureSetting);
572     if (ret != DCAMERA_OK) {
573         DHLOGE("StartPhotoOutput %s photoOutput capture failed, ret: %d",
574             GetAnonyString(cameraId_).c_str(), ret);
575         return ret;
576     }
577     return DCAMERA_OK;
578 }
579 
SetPhotoCaptureRotation(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)580 void DCameraClient::SetPhotoCaptureRotation(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
581     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
582 {
583     uint32_t rotationCount = 1;
584     camera_metadata_item_t item;
585     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_ORIENTATION, &item);
586     if ((ret == CAM_META_SUCCESS) && (rotationCount == item.count)) {
587         int32_t oriSetRotate = item.data.i32[0];
588         int32_t setRotate = oriSetRotate;
589         int32_t sensorRotate = 0;
590         std::string abilityString = ((sptr<CameraStandard::CameraInput> &)cameraInput_)->GetCameraSettings();
591         std::shared_ptr<Camera::CameraMetadata> cameraAbility = Camera::MetadataUtils::DecodeFromString(abilityString);
592         if (cameraAbility != nullptr) {
593             camera_metadata_item_t itemSensor;
594             ret = Camera::FindCameraMetadataItem(cameraAbility->get(), OHOS_SENSOR_ORIENTATION, &itemSensor);
595             if ((ret == CAM_META_SUCCESS) && (rotationCount == itemSensor.count)) {
596                 sensorRotate = itemSensor.data.i32[0];
597                 setRotate = setRotate - sensorRotate;
598             }
599         }
600 
601         if (setRotate < 0) {
602             setRotate += DCAMERA_CAPTURE_ROTATE_360;
603         }
604 
605         CameraStandard::PhotoCaptureSetting::RotationConfig rotation =
606             static_cast<CameraStandard::PhotoCaptureSetting::RotationConfig>(setRotate);
607         photoCaptureSetting->SetRotation(rotation);
608         DHLOGI("SetPhotoCaptureRotation %s photo set %d rotation: %d setRotate: %d sensorRotate: %d oriSetRotate: %d",
609             GetAnonyString(cameraId_).c_str(), item.count, rotation, setRotate, sensorRotate, oriSetRotate);
610     }
611 }
612 
SetPhotoCaptureQuality(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)613 void DCameraClient::SetPhotoCaptureQuality(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
614     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
615 {
616     uint32_t qualityCount = 1;
617     camera_metadata_item_t item;
618     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_QUALITY, &item);
619     if ((ret == CAM_META_SUCCESS) && (qualityCount == item.count)) {
620         CameraStandard::PhotoCaptureSetting::QualityLevel quality =
621             static_cast<CameraStandard::PhotoCaptureSetting::QualityLevel>(item.data.u8[0]);
622         photoCaptureSetting->SetQuality(quality);
623         DHLOGI("SetPhotoCaptureQuality %s photo capture settings set %d quality: %d",
624             GetAnonyString(cameraId_).c_str(), item.count, quality);
625     }
626 }
627 
SetPhotoCaptureLocation(const std::shared_ptr<Camera::CameraMetadata> & cameraMetadata,std::shared_ptr<CameraStandard::PhotoCaptureSetting> & photoCaptureSetting)628 void DCameraClient::SetPhotoCaptureLocation(const std::shared_ptr<Camera::CameraMetadata>& cameraMetadata,
629     std::shared_ptr<CameraStandard::PhotoCaptureSetting>& photoCaptureSetting)
630 {
631     uint32_t locationCount = 3;
632     camera_metadata_item_t item;
633     int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_GPS_COORDINATES, &item);
634     if ((ret == CAM_META_SUCCESS) && (locationCount == item.count)) {
635         int32_t latitudeIndex = 0;
636         int32_t longitudeIndex = 1;
637         int32_t altitudeIndex = 2;
638         std::unique_ptr<CameraStandard::Location> location = std::make_unique<CameraStandard::Location>();
639         location->latitude = item.data.d[latitudeIndex];
640         location->longitude = item.data.d[longitudeIndex];
641         location->altitude = item.data.d[altitudeIndex];
642         photoCaptureSetting->SetLocation(location);
643         DHLOGI("SetPhotoCaptureLocation %s photo capture settings set %d location: " +
644             "latitude=%f, longitude=%f, altitude=%f", GetAnonyString(cameraId_).c_str(), item.count,
645             item.data.d[latitudeIndex], item.data.d[longitudeIndex], item.data.d[altitudeIndex]);
646     }
647 }
648 
PauseCapture()649 int32_t DCameraClient::PauseCapture()
650 {
651     if (captureSession_ == nullptr) {
652         DHLOGE("PauseCapture captureSession_ is nullptr.");
653         return DCAMERA_BAD_VALUE;
654     }
655     int32_t ret = captureSession_->Stop();
656     if (ret != DCAMERA_OK) {
657         DHLOGE("PauseCapture captureSession stop failed, cameraId: %s, ret: %d",
658                GetAnonyString(cameraId_).c_str(), ret);
659     }
660     return ret;
661 }
662 
ResumeCapture()663 int32_t DCameraClient::ResumeCapture()
664 {
665     if (captureSession_ == nullptr) {
666         DHLOGE("ResumeCapture captureSession_ is nullptr.");
667         return DCAMERA_BAD_VALUE;
668     }
669     int32_t ret = captureSession_->Start();
670     if (ret != DCAMERA_OK) {
671         DHLOGE("ResumeCapture captureSession Start failed, cameraId: %s, ret: %d",
672             GetAnonyString(cameraId_).c_str(), ret);
673     }
674     return ret;
675 }
676 } // namespace DistributedHardware
677 } // namespace OHOS
678