diff --git a/figures/distributedcamera_arch.png b/figures/distributedcamera_arch.png index ed769ce56875747c86851dd4157fd82745eb8f26..fdffabcc739952de65f5d137a2793044ef0ebdca 100644 Binary files a/figures/distributedcamera_arch.png and b/figures/distributedcamera_arch.png differ diff --git a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_source/dcamera_source_handler_test.cpp b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_source/dcamera_source_handler_test.cpp index 549fedebd2909a696a8f1f3e7167638fc76421d5..d11abb9f34c190a70b38e3174d57833b69b8eaef 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/common/camera_source/dcamera_source_handler_test.cpp +++ b/interfaces/inner_kits/native_cpp/test/unittest/common/camera_source/dcamera_source_handler_test.cpp @@ -133,7 +133,7 @@ HWTEST_F(DCameraSourceHandlerTest, dcamera_source_handler_test_004, TestSize.Lev int32_t ret = DCameraSourceHandler::GetInstance().UnregisterDistributedHardware(devId, dhId, uncallback); ret = DCameraSourceHandler::GetInstance().ReleaseSource(); - EXPECT_NE(DCAMERA_OK, ret); + EXPECT_EQ(DCAMERA_OK, ret); } /** diff --git a/services/cameraservice/cameraoperator/client/BUILD.gn b/services/cameraservice/cameraoperator/client/BUILD.gn index d01bd7a08b5e1d6482f5ca2cfa53e0ad13716bc5..d513c1af1f17912c9bb653f372b9381d0ccf9574 100644 --- a/services/cameraservice/cameraoperator/client/BUILD.gn +++ b/services/cameraservice/cameraoperator/client/BUILD.gn @@ -51,11 +51,7 @@ ohos_shared_library("distributed_camera_client") { "src/listener/dcamera_photo_surface_listener.cpp", ] - if (!distributed_camera_common) { - sources += [ "src/dcamera_client.cpp" ] - } else { - sources += [ "src/dcamera_client_common.cpp" ] - } + sources += [ "src/dcamera_client.cpp" ] deps = [ "${camerastandard_path}/frameworks/native/camera:camera_framework", diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp index 364cec1e288cbd906b96d1d3c990d9e8079565f4..f369e15b31b5e534222a2c0564de31be3f7b8482 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp @@ -174,7 +174,7 @@ int32_t DCameraClient::StartCapture(std::vector& surface) { DHLOGI("DCameraClient::StartCapture cameraId: %s", GetAnonyString(cameraId_).c_str()); - if ((photoOutput_ == nullptr) && (videoOutput_ == nullptr)) { + if ((photoOutput_ == nullptr) && (previewOutput_ == nullptr)) { DHLOGI("DCameraClient::StartCapture %s config capture session", GetAnonyString(cameraId_).c_str()); if (surface == nullptr) { DHLOGE("DCameraClient::StartCapture: input surface is nullptr."); @@ -190,7 +190,7 @@ int32_t DCameraClient::StartCapture(std::vectorisCapture_)) { + if ((info->streamType_ == CONTINUOUS_FRAME) || (!info->isCapture_)) { continue; } int32_t ret = StartCaptureInner(info); @@ -217,32 +217,6 @@ int32_t DCameraClient::CameraServiceErrorType(const int32_t errorType) int32_t DCameraClient::StopCapture() { DHLOGI("DCameraClient::StopCapture cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (videoOutput_ != nullptr) { - DHLOGI("DCameraClient::StopCapture %s stop videoOutput", GetAnonyString(cameraId_).c_str()); - int32_t ret = ((sptr &)videoOutput_)->Stop(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture videoOutput stop failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - DHLOGI("DCameraClient::StopCapture %s release videoOutput", GetAnonyString(cameraId_).c_str()); - ret = videoOutput_->Release(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture videoOutput Release failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - videoOutput_ = nullptr; - } - - if (photoOutput_ != nullptr) { - DHLOGI("DCameraClient::StopCapture %s release photoOutput", GetAnonyString(cameraId_).c_str()); - int32_t ret = photoOutput_->Release(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture photoOutput Release failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - photoOutput_ = nullptr; - } - if (cameraInput_ != nullptr) { DHLOGI("DCameraClient::StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str()); int32_t ret = cameraInput_->Close(); @@ -262,11 +236,6 @@ int32_t DCameraClient::StopCapture() if (videoSurface_ != nullptr) { DHLOGI("DCameraClient::StopCapture %s video surface unregister consumer listener", GetAnonyString(cameraId_).c_str()); - int32_t ret = videoSurface_->UnregisterConsumerListener(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture %s video surface unregister consumer listener failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } videoSurface_ = nullptr; } @@ -282,6 +251,8 @@ int32_t DCameraClient::StopCapture() photoSurface_ = nullptr; } + photoOutput_ = nullptr; + previewOutput_ = nullptr; DHLOGI("DCameraClient::StopCapture %s success", GetAnonyString(cameraId_).c_str()); return DCAMERA_OK; } @@ -401,10 +372,10 @@ int32_t DCameraClient::ConfigCaptureSessionInner() } } - if (videoOutput_ != nullptr) { - ret = captureSession_->AddOutput(videoOutput_); + if (previewOutput_ != nullptr) { + ret = captureSession_->AddOutput(previewOutput_); if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s add videoOutput to captureSession failed, ret: %d", + DHLOGE("DCameraClient::ConfigCaptureSession %s add previewOutput to captureSession failed, ret: %d", GetAnonyString(cameraId_).c_str(), ret); return ret; } @@ -462,14 +433,15 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in DHLOGI("DCameraClient::CreatePhotoOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d", GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_, info->isCapture_); - photoSurface_ = Surface::CreateSurfaceAsConsumer(); + photoSurface_ = IConsumerSurface::Create(); photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); photoSurface_->RegisterConsumerListener((sptr &)photoListener_); CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_); CameraStandard::Size photoSize = {info->width_, info->height_}; CameraStandard::Profile photoProfile(photoFormat, photoSize); + sptr bp = photoSurface_->GetProducer(); int rv = cameraManager_->CreatePhotoOutput( - photoProfile, photoSurface_, &((sptr &)photoOutput_)); + photoProfile, bp, &((sptr &)photoOutput_)); if (rv != DCAMERA_OK) { DHLOGE("DCameraClient::CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str()); return DCAMERA_BAD_VALUE; @@ -482,22 +454,22 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& info) { - DHLOGI("DCameraClient::CreateVideoOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d", + DHLOGI("DCameraClient::CreatePreviewOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d", GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_, info->isCapture_); - CameraStandard::CameraFormat videoFormat = ConvertToCameraFormat(info->format_); - CameraStandard::Size videoSize = {info->width_, info->height_}; - std::vector framerates = {}; - CameraStandard::VideoProfile videoSettings(videoFormat, videoSize, framerates); - int rv = cameraManager_->CreateVideoOutput( - videoSettings, videoSurface_, &((sptr &)videoOutput_)); + CameraStandard::CameraFormat previewFormat = ConvertToCameraFormat(info->format_); + CameraStandard::Size previewSize = {info->width_, info->height_}; + CameraStandard::Profile previewProfile(previewFormat, previewSize); + int rv = cameraManager_->CreatePreviewOutput( + previewProfile, videoSurface_, &((sptr &)previewOutput_)); if (rv != DCAMERA_OK) { - DHLOGE("DCameraClient::CreateVideoOutput %s create video output failed", GetAnonyString(cameraId_).c_str()); + DHLOGE("DCameraClient::CreatePreviewOutput %s create preview output failed", + GetAnonyString(cameraId_).c_str()); return DCAMERA_BAD_VALUE; } - std::shared_ptr videoCallback = std::make_shared(stateCallback_); - ((sptr &)videoOutput_)->SetCallback(videoCallback); - DHLOGI("DCameraClient::CreateVideoOutput %s success", GetAnonyString(cameraId_).c_str()); + std::shared_ptr previewCallback = std::make_shared(stateCallback_); + ((sptr &)previewOutput_)->SetCallback(previewCallback); + DHLOGI("DCameraClient::CreatePreviewOutput %s success", GetAnonyString(cameraId_).c_str()); return DCAMERA_OK; } diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp deleted file mode 100644 index 23bc2c1f5ede90762ebee8d26acdfff13e4d2dd6..0000000000000000000000000000000000000000 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp +++ /dev/null @@ -1,631 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "dcamera_client.h" - -#include "anonymous_string.h" -#include "camera_util.h" -#include "camera_metadata_operator.h" -#include "dcamera_input_callback.h" -#include "dcamera_manager_callback.h" -#include "dcamera_photo_callback.h" -#include "dcamera_preview_callback.h" -#include "dcamera_session_callback.h" -#include "dcamera_utils_tools.h" -#include "dcamera_video_callback.h" -#include "distributed_camera_constants.h" -#include "distributed_camera_errno.h" -#include "distributed_hardware_log.h" -#include "metadata_utils.h" - -namespace OHOS { -namespace DistributedHardware { -DCameraClient::DCameraClient(const std::string& dhId) -{ - DHLOGI("DCameraClient Constructor dhId: %s", GetAnonyString(dhId).c_str()); - cameraId_ = dhId.substr(CAMERA_ID_PREFIX.size()); - isInit_ = false; -} - -DCameraClient::~DCameraClient() -{ - if (isInit_) { - UnInit(); - } -} - -int32_t DCameraClient::Init() -{ - DHLOGI("DCameraClient::Init cameraId: %s", GetAnonyString(cameraId_).c_str()); - cameraManager_ = CameraStandard::CameraManager::GetInstance(); - if (cameraManager_ == nullptr) { - DHLOGE("DCameraClient::Init cameraManager getInstance failed"); - return DCAMERA_BAD_VALUE; - } - cameraManager_->SetCallback(std::make_shared()); - - std::vector> cameraList = cameraManager_->GetSupportedCameras(); - if (cameraList.empty()) { - DHLOGE("DCameraClient::Init no camera device"); - return DCAMERA_BAD_VALUE; - } - DHLOGI("DCameraClient::Init camera size: %d", cameraList.size()); - for (auto& info : cameraList) { - if (info->GetID() == cameraId_) { - DHLOGI("DCameraClient::Init cameraInfo get id: %s", GetAnonyString(info->GetID()).c_str()); - cameraInfo_ = info; - break; - } - } - if (cameraInfo_ == nullptr) { - DHLOGE("DCameraClient::Init cameraInfo is null"); - return DCAMERA_BAD_VALUE; - } - - isInit_ = true; - DHLOGI("DCameraClient::Init %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -int32_t DCameraClient::UnInit() -{ - DHLOGI("DCameraClient::UnInit cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (cameraManager_ != nullptr) { - DHLOGI("DCameraClient::UnInit unregister cameraManager callback"); - cameraManager_->SetCallback(nullptr); - } - - isInit_ = false; - cameraInfo_ = nullptr; - cameraManager_ = nullptr; - DHLOGI("DCameraClient::UnInit %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -int32_t DCameraClient::UpdateSettings(std::vector>& settings) -{ - DHLOGI("DCameraClient::UpdateSettings cameraId: %s", GetAnonyString(cameraId_).c_str()); - for (auto& setting : settings) { - switch (setting->type_) { - case UPDATE_METADATA: { - DHLOGI("DCameraClient::UpdateSettings %s update metadata settings", GetAnonyString(cameraId_).c_str()); - std::string dcSettingValue = setting->value_; - std::string metadataStr = Base64Decode(dcSettingValue); - FindCameraMetadata(metadataStr); - - if (cameraInput_ == nullptr) { - DHLOGE("DCameraClient::UpdateSettings %s cameraInput is null", GetAnonyString(cameraId_).c_str()); - UpdateSettingCache(metadataStr); - return DCAMERA_OK; - } - - int32_t ret = ((sptr &)cameraInput_)->SetCameraSettings(metadataStr); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::UpdateSettings %s update metadata settings failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - break; - } - default: { - DHLOGE("DCameraClient::UpdateSettings unknown setting type"); - break; - } - } - } - DHLOGI("DCameraClient::UpdateSettings %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -void DCameraClient::UpdateSettingCache(const std::string& metadataStr) -{ - if (cameraMetadatas_.size() == DCAMERA_MAX_METADATA_SIZE) { - DHLOGE("DCameraClient::UpdateSettingCache %s camera metadata oversize", - GetAnonyString(cameraId_).c_str()); - cameraMetadatas_.pop(); - } - cameraMetadatas_.push(metadataStr); -} - -void DCameraClient::FindCameraMetadata(const std::string& metadataStr) -{ - std::shared_ptr cameraMetadata = Camera::MetadataUtils::DecodeFromString(metadataStr); - camera_metadata_item_t focusItem; - int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_FOCUS_MODE, &focusItem); - if (ret == CAM_META_SUCCESS) { - DHLOGI("DCameraClient::FindCameraMetadata focus mode: %d", focusItem.data.u8[0]); - } else { - DHLOGE("DCameraClient::FindCameraMetadata %s find focus mode failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - - camera_metadata_item_t exposureItem; - ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_EXPOSURE_MODE, &exposureItem); - if (ret == CAM_META_SUCCESS) { - DHLOGI("DCameraClient::FindCameraMetadata exposure mode: %d", exposureItem.data.u8[0]); - } else { - DHLOGE("DCameraClient::FindCameraMetadata %s find exposure mode failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - - camera_metadata_item_t stabilityItem; - ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &stabilityItem); - if (ret == CAM_META_SUCCESS) { - DHLOGI("DCameraClient::FindCameraMetadata stabilization mode: %d", stabilityItem.data.u8[0]); - } else { - DHLOGE("DCameraClient::FindCameraMetadata %s find stabilization mode failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } -} - -int32_t DCameraClient::StartCapture(std::vector>& captureInfos, - sptr& surface) -{ - DHLOGI("DCameraClient::StartCapture cameraId: %s", GetAnonyString(cameraId_).c_str()); - if ((photoOutput_ == nullptr) && (previewOutput_ == nullptr)) { - DHLOGI("DCameraClient::StartCapture %s config capture session", GetAnonyString(cameraId_).c_str()); - if (surface == nullptr) { - DHLOGE("DCameraClient::StartCapture: input surface is nullptr."); - return DCAMERA_BAD_VALUE; - } - videoSurface_ = surface; - int32_t ret = ConfigCaptureSession(captureInfos); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StartCapture config capture session failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return CameraServiceErrorType(ret); - } - } - - for (auto& info : captureInfos) { - if ((info->streamType_ == CONTINUOUS_FRAME) || (!info->isCapture_)) { - continue; - } - int32_t ret = StartCaptureInner(info); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StartCapture failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return CameraServiceErrorType(ret); - } - } - DHLOGI("DCameraClient::StartCapture %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -int32_t DCameraClient::CameraServiceErrorType(const int32_t errorType) -{ - if (errorType == CameraStandard::CamServiceError::CAMERA_ALLOC_ERROR) { - return DCAMERA_ALLOC_ERROR; - } else if (errorType == CameraStandard::CamServiceError::CAMERA_DEVICE_BUSY) { - return DCAMERA_DEVICE_BUSY; - } - return errorType; -} - -int32_t DCameraClient::StopCapture() -{ - DHLOGI("DCameraClient::StopCapture cameraId: %s", GetAnonyString(cameraId_).c_str()); - - if (cameraInput_ != nullptr) { - DHLOGI("DCameraClient::StopCapture %s release cameraInput", GetAnonyString(cameraId_).c_str()); - int32_t ret = cameraInput_->Close(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture cameraInput Close failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - ret = cameraInput_->Release(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture cameraInput Release failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - cameraInput_ = nullptr; - } - ReleaseCaptureSession(); - - if (videoSurface_ != nullptr) { - DHLOGI("DCameraClient::StopCapture %s video surface unregister consumer listener", - GetAnonyString(cameraId_).c_str()); - int32_t ret = videoSurface_->UnregisterConsumerListener(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture %s video surface unregister consumer listener failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - videoSurface_ = nullptr; - } - - if (photoSurface_ != nullptr) { - DHLOGI("DCameraClient::StopCapture %s photo surface unregister consumer listener", - GetAnonyString(cameraId_).c_str()); - int32_t ret = photoSurface_->UnregisterConsumerListener(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture %s photo surface unregister consumer listener failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - photoListener_ = nullptr; - photoSurface_ = nullptr; - } - - photoOutput_ = nullptr; - previewOutput_ = nullptr; - DHLOGI("DCameraClient::StopCapture %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -void DCameraClient::ReleaseCaptureSession() -{ - if (captureSession_ == nullptr) { - return; - } - DHLOGI("DCameraClient::StopCapture %s stop captureSession", GetAnonyString(cameraId_).c_str()); - int32_t ret = captureSession_->Stop(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture captureSession stop failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - DHLOGI("DCameraClient::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str()); - ret = captureSession_->Release(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StopCapture captureSession Release failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - captureSession_ = nullptr; -} - -int32_t DCameraClient::SetStateCallback(std::shared_ptr& callback) -{ - DHLOGI("DCameraClient::SetStateCallback cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (callback == nullptr) { - DHLOGE("DCameraClient::SetStateCallback %s unregistering state callback", GetAnonyString(cameraId_).c_str()); - } - stateCallback_ = callback; - return DCAMERA_OK; -} - -int32_t DCameraClient::SetResultCallback(std::shared_ptr& callback) -{ - DHLOGI("DCameraClient::SetResultCallback cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (callback == nullptr) { - DHLOGE("DCameraClient::SetResultCallback %s unregistering result callback", GetAnonyString(cameraId_).c_str()); - } - resultCallback_ = callback; - return DCAMERA_OK; -} - -int32_t DCameraClient::ConfigCaptureSession(std::vector>& captureInfos) -{ - DHLOGI("DCameraClient::ConfigCaptureSession cameraId: %s", GetAnonyString(cameraId_).c_str()); - int rv = cameraManager_->CreateCameraInput(cameraInfo_, &((sptr &)cameraInput_)); - if (rv != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s create cameraInput failed", GetAnonyString(cameraId_).c_str()); - return DCAMERA_BAD_VALUE; - } - int32_t rc = ((sptr &)cameraInput_)->Open(); - if (rc != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession cameraInput_ Open failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), rc); - return DCAMERA_BAD_VALUE; - } - std::shared_ptr inputCallback = std::make_shared(stateCallback_); - ((sptr &)cameraInput_)->SetErrorCallback(inputCallback); - - while (!cameraMetadatas_.empty()) { - std::string metadataStr = cameraMetadatas_.front(); - FindCameraMetadata(metadataStr); - int32_t ret = ((sptr &)cameraInput_)->SetCameraSettings(metadataStr); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s set camera settings failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - cameraMetadatas_.pop(); - } - - rv = cameraManager_->CreateCaptureSession(&captureSession_); - if (rv != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s create captureSession failed", - GetAnonyString(cameraId_).c_str()); - return DCAMERA_BAD_VALUE; - } - - std::shared_ptr sessionCallback = std::make_shared(stateCallback_); - captureSession_->SetFocusCallback(sessionCallback); - captureSession_->SetCallback(sessionCallback); - - int32_t ret = CreateCaptureOutput(captureInfos); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession create capture output failed, cameraId: %s, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - - return ConfigCaptureSessionInner(); -} - -int32_t DCameraClient::ConfigCaptureSessionInner() -{ - int32_t ret = captureSession_->BeginConfig(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s config captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - - ret = captureSession_->AddInput(cameraInput_); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s add cameraInput to captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - - if (photoOutput_ != nullptr) { - ret = captureSession_->AddOutput(photoOutput_); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s add photoOutput to captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - } - - if (previewOutput_ != nullptr) { - ret = captureSession_->AddOutput(previewOutput_); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s add previewOutput to captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - } - - ret = captureSession_->CommitConfig(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s commit captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - - ret = captureSession_->Start(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::ConfigCaptureSession %s start captureSession failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - } - - DHLOGI("DCameraClient::ConfigCaptureSession %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -int32_t DCameraClient::CreateCaptureOutput(std::vector>& captureInfos) -{ - if (captureInfos.empty()) { - DHLOGE("DCameraClient::CreateCaptureOutput no capture info, cameraId: %s", GetAnonyString(cameraId_).c_str()); - return DCAMERA_BAD_VALUE; - } - - for (auto& info : captureInfos) { - if (info->streamType_ == SNAPSHOT_FRAME) { - int32_t ret = CreatePhotoOutput(info); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::CreateCaptureOutput %s create photo output failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - } else if (info->streamType_ == CONTINUOUS_FRAME) { - int32_t ret = CreateVideoOutput(info); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::CreateCaptureOutput %s create video output failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - } else { - DHLOGE("DCameraClient::CreateCaptureOutput unknown stream type"); - return DCAMERA_BAD_VALUE; - } - } - return DCAMERA_OK; -} - -int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& info) -{ - DHLOGI("DCameraClient::CreatePhotoOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d", - GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_, - info->isCapture_); - photoSurface_ = Surface::CreateSurfaceAsConsumer(); - photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); - photoSurface_->RegisterConsumerListener((sptr &)photoListener_); - CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_); - CameraStandard::Size photoSize = {info->width_, info->height_}; - CameraStandard::Profile photoProfile(photoFormat, photoSize); - int rv = cameraManager_->CreatePhotoOutput( - photoProfile, photoSurface_, &((sptr &)photoOutput_)); - if (rv != DCAMERA_OK) { - DHLOGE("DCameraClient::CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str()); - return DCAMERA_BAD_VALUE; - } - std::shared_ptr photoCallback = std::make_shared(stateCallback_); - ((sptr &)photoOutput_)->SetCallback(photoCallback); - DHLOGI("DCameraClient::CreatePhotoOutput %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& info) -{ - DHLOGI("DCameraClient::CreatePreviewOutput dhId: %s, width: %d, height: %d, format: %d, stream: %d, isCapture: %d", - GetAnonyString(cameraId_).c_str(), info->width_, info->height_, info->format_, info->streamType_, - info->isCapture_); - CameraStandard::CameraFormat previewFormat = ConvertToCameraFormat(info->format_); - CameraStandard::Size previewSize = {info->width_, info->height_}; - CameraStandard::Profile previewProfile(previewFormat, previewSize); - int rv = cameraManager_->CreatePreviewOutput( - previewProfile, videoSurface_, &((sptr &)previewOutput_)); - if (rv != DCAMERA_OK) { - DHLOGE("DCameraClient::CreatePreviewOutput %s create preview output failed", - GetAnonyString(cameraId_).c_str()); - return DCAMERA_BAD_VALUE; - } - std::shared_ptr previewCallback = std::make_shared(stateCallback_); - ((sptr &)previewOutput_)->SetCallback(previewCallback); - DHLOGI("DCameraClient::CreatePreviewOutput %s success", GetAnonyString(cameraId_).c_str()); - return DCAMERA_OK; -} - -CameraStandard::CameraFormat DCameraClient::ConvertToCameraFormat(int32_t format) -{ - CameraStandard::CameraFormat ret = CameraStandard::CameraFormat::CAMERA_FORMAT_INVALID; - DCameraFormat df = static_cast(format); - switch (df) { - case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888: - ret = CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888; - break; - case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888: - ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888; - break; - case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP: - ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP; - break; - case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG: - ret = CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG; - break; - default: - break; - } - return ret; -} - -int32_t DCameraClient::StartCaptureInner(std::shared_ptr& info) -{ - switch (info->streamType_) { - case CONTINUOUS_FRAME: { - return StartVideoOutput(); - } - case SNAPSHOT_FRAME: { - return StartPhotoOutput(info); - } - default: { - DHLOGE("DCameraClient::StartCaptureInner unknown stream type"); - return DCAMERA_BAD_VALUE; - } - } -} - -int32_t DCameraClient::StartPhotoOutput(std::shared_ptr& info) -{ - DHLOGI("DCameraClient::StartPhotoOutput cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (photoOutput_ == nullptr) { - DHLOGE("DCameraClient::StartPhotoOutput photoOutput is null"); - return DCAMERA_BAD_VALUE; - } - - std::vector> captureSettings = info->captureSettings_; - std::string metadataSetting; - for (const auto& setting : captureSettings) { - if (setting->type_ == UPDATE_METADATA) { - DHLOGI("DCameraClient::StartPhotoOutput %s update metadata settings", GetAnonyString(cameraId_).c_str()); - metadataSetting = setting->value_; - } - } - - if (metadataSetting.empty()) { - DHLOGE("DCameraClient::StartPhotoOutput no metadata settings to update"); - int32_t ret = ((sptr &)photoOutput_)->Capture(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StartPhotoOutput %s photoOutput capture failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - return DCAMERA_OK; - } - - std::string metadataStr = Base64Decode(metadataSetting); - std::shared_ptr cameraMetadata = Camera::MetadataUtils::DecodeFromString(metadataStr); - std::shared_ptr photoCaptureSetting = - std::make_shared(); - SetPhotoCaptureRotation(cameraMetadata, photoCaptureSetting); - SetPhotoCaptureQuality(cameraMetadata, photoCaptureSetting); - SetPhotoCaptureLocation(cameraMetadata, photoCaptureSetting); - int32_t ret = ((sptr &)photoOutput_)->Capture(photoCaptureSetting); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StartPhotoOutput %s photoOutput capture failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - return DCAMERA_OK; -} - -void DCameraClient::SetPhotoCaptureRotation(const std::shared_ptr& cameraMetadata, - std::shared_ptr& photoCaptureSetting) -{ - uint32_t rotationCount = 1; - camera_metadata_item_t item; - int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_ORIENTATION, &item); - if ((ret == CAM_META_SUCCESS) && (rotationCount == item.count)) { - CameraStandard::PhotoCaptureSetting::RotationConfig rotation = - static_cast(item.data.i32[0]); - photoCaptureSetting->SetRotation(rotation); - DHLOGI("DCameraClient::SetPhotoCaptureRotation %s photo capture settings set %d rotation: %d", - GetAnonyString(cameraId_).c_str(), item.count, rotation); - } -} - -void DCameraClient::SetPhotoCaptureQuality(const std::shared_ptr& cameraMetadata, - std::shared_ptr& photoCaptureSetting) -{ - uint32_t qualityCount = 1; - camera_metadata_item_t item; - int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_QUALITY, &item); - if ((ret == CAM_META_SUCCESS) && (qualityCount == item.count)) { - CameraStandard::PhotoCaptureSetting::QualityLevel quality = - static_cast(item.data.u8[0]); - photoCaptureSetting->SetQuality(quality); - DHLOGI("DCameraClient::SetPhotoCaptureQuality %s photo capture settings set %d quality: %d", - GetAnonyString(cameraId_).c_str(), item.count, quality); - } -} - -void DCameraClient::SetPhotoCaptureLocation(const std::shared_ptr& cameraMetadata, - std::shared_ptr& photoCaptureSetting) -{ - uint32_t locationCount = 3; - camera_metadata_item_t item; - int32_t ret = Camera::FindCameraMetadataItem(cameraMetadata->get(), OHOS_JPEG_GPS_COORDINATES, &item); - if ((ret == CAM_META_SUCCESS) && (locationCount == item.count)) { - int32_t latitudeIndex = 0; - int32_t longitudeIndex = 1; - int32_t altitudeIndex = 2; - std::unique_ptr location = std::make_unique(); - location->latitude = item.data.d[latitudeIndex]; - location->longitude = item.data.d[longitudeIndex]; - location->altitude = item.data.d[altitudeIndex]; - photoCaptureSetting->SetLocation(location); - DHLOGI("DCameraClient::SetPhotoCaptureLocation %s photo capture settings set %d location: " + - "latitude=%f, longitude=%f, altitude=%f", GetAnonyString(cameraId_).c_str(), item.count, - item.data.d[latitudeIndex], item.data.d[longitudeIndex], item.data.d[altitudeIndex]); - } -} - -int32_t DCameraClient::StartVideoOutput() -{ - DHLOGI("DCameraClient::StartVideoOutput cameraId: %s", GetAnonyString(cameraId_).c_str()); - if (videoOutput_ == nullptr) { - DHLOGE("DCameraClient::StartVideoOutput videoOutput is null"); - return DCAMERA_BAD_VALUE; - } - int32_t ret = ((sptr &)videoOutput_)->Start(); - if (ret != DCAMERA_OK) { - DHLOGE("DCameraClient::StartVideoOutput %s videoOutput start failed, ret: %d", - GetAnonyString(cameraId_).c_str(), ret); - return ret; - } - return DCAMERA_OK; -} -} // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file