From bc27017593fd54ea5f1f5aa57cac0a2a80226c22 Mon Sep 17 00:00:00 2001 From: chen0088 Date: Sun, 9 Oct 2022 14:58:57 +0800 Subject: [PATCH] feat:local camera interface modify service Signed-off-by: chen0088 --- .../include/callback/dcamera_input_callback.h | 8 +- .../callback/dcamera_manager_callback.h | 2 +- .../include/callback/dcamera_photo_callback.h | 2 +- .../callback/dcamera_preview_callback.h | 2 +- .../callback/dcamera_session_callback.h | 8 +- .../include/callback/dcamera_video_callback.h | 2 +- .../client/include/dcamera_client.h | 4 +- .../src/callback/dcamera_input_callback.cpp | 39 ---- .../src/callback/dcamera_manager_callback.cpp | 2 +- .../src/callback/dcamera_session_callback.cpp | 41 ++++- .../client/src/dcamera_client.cpp | 44 ++++- .../client/src/dcamera_client_common.cpp | 43 ++++- .../client/test/sample/BUILD.gn | 5 +- .../client/test/sample/dcamera_client_demo.h | 24 +-- .../client/test/sample/main.cpp | 168 ++++++++++++++---- .../handler/include/dcamera_handler.h | 17 +- .../handler/src/dcamera_handler.cpp | 112 +++++++----- .../handler/src/dcamera_handler_common.cpp | 102 +++++++---- 18 files changed, 419 insertions(+), 206 deletions(-) diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_input_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_input_callback.h index 5efe7702..1524b07e 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_input_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_input_callback.h @@ -25,19 +25,13 @@ namespace OHOS { namespace DistributedHardware { -class DCameraInputCallback : public CameraStandard::ErrorCallback, public CameraStandard::FocusCallback { +class DCameraInputCallback : public CameraStandard::ErrorCallback { public: explicit DCameraInputCallback(const std::shared_ptr& callback); void OnError(const int32_t errorType, const int32_t errorMsg) const override; - void OnFocusState(FocusState state) override; private: std::shared_ptr callback_; - const std::map focusStateMap_ = { - {FocusCallback::SCAN, OHOS_CAMERA_FOCUS_STATE_SCAN}, - {FocusCallback::FOCUSED, OHOS_CAMERA_FOCUS_STATE_FOCUSED}, - {FocusCallback::UNFOCUSED, OHOS_CAMERA_FOCUS_STATE_UNFOCUSED} - }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_manager_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_manager_callback.h index 6d2aa46d..f3f4b2be 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_manager_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_manager_callback.h @@ -24,7 +24,7 @@ class DCameraManagerCallback : public CameraStandard::CameraManagerCallback { public: void OnCameraStatusChanged(const CameraStandard::CameraStatusInfo &cameraStatusInfo) const override; void OnFlashlightStatusChanged(const std::string &cameraID, - const CameraStandard::FlashlightStatus flashStatus) const override; + const CameraStandard::FlashStatus flashStatus) const override; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_photo_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_photo_callback.h index 9e8bd211..1268ab0d 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_photo_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_photo_callback.h @@ -22,7 +22,7 @@ namespace OHOS { namespace DistributedHardware { -class DCameraPhotoCallback : public CameraStandard::PhotoCallback { +class DCameraPhotoCallback : public CameraStandard::PhotoStateCallback { public: explicit DCameraPhotoCallback(const std::shared_ptr& callback); void OnCaptureStarted(const int32_t captureID) const override; diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_preview_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_preview_callback.h index fad4a501..4550b499 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_preview_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_preview_callback.h @@ -24,7 +24,7 @@ namespace OHOS { namespace DistributedHardware { -class DCameraPreviewCallback : public CameraStandard::PreviewCallback { +class DCameraPreviewCallback : public CameraStandard::PreviewStateCallback { public: explicit DCameraPreviewCallback(const std::shared_ptr& callback); void OnFrameStarted() const override; diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_session_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_session_callback.h index 07a7b533..c96b8dc9 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_session_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_session_callback.h @@ -24,13 +24,19 @@ namespace OHOS { namespace DistributedHardware { -class DCameraSessionCallback : public CameraStandard::SessionCallback { +class DCameraSessionCallback : public CameraStandard::SessionCallback, public CameraStandard::FocusCallback { public: explicit DCameraSessionCallback(const std::shared_ptr& callback); void OnError(int32_t errorCode) override; + void OnFocusState(FocusState state) override; private: std::shared_ptr callback_; + const std::map focusStateMap_ = { + {FocusCallback::SCAN, OHOS_CAMERA_FOCUS_STATE_SCAN}, + {FocusCallback::FOCUSED, OHOS_CAMERA_FOCUS_STATE_FOCUSED}, + {FocusCallback::UNFOCUSED, OHOS_CAMERA_FOCUS_STATE_UNFOCUSED} + }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/include/callback/dcamera_video_callback.h b/services/cameraservice/cameraoperator/client/include/callback/dcamera_video_callback.h index ff7cdec1..0e45b03e 100644 --- a/services/cameraservice/cameraoperator/client/include/callback/dcamera_video_callback.h +++ b/services/cameraservice/cameraoperator/client/include/callback/dcamera_video_callback.h @@ -22,7 +22,7 @@ namespace OHOS { namespace DistributedHardware { -class DCameraVideoCallback : public CameraStandard::VideoCallback { +class DCameraVideoCallback : public CameraStandard::VideoStateCallback { public: explicit DCameraVideoCallback(const std::shared_ptr& callback); void OnFrameStarted() const override; diff --git a/services/cameraservice/cameraoperator/client/include/dcamera_client.h b/services/cameraservice/cameraoperator/client/include/dcamera_client.h index d0b1df0d..e2b53474 100644 --- a/services/cameraservice/cameraoperator/client/include/dcamera_client.h +++ b/services/cameraservice/cameraoperator/client/include/dcamera_client.h @@ -23,6 +23,7 @@ #include "camera_info.h" #include "camera_input.h" #include "camera_manager.h" +#include "camera_output_capability.h" #include "capture_input.h" #include "capture_output.h" #include "capture_session.h" @@ -66,6 +67,7 @@ private: std::shared_ptr& photoCaptureSetting); void ReleaseCaptureSession(); int32_t CameraServiceErrorType(const int32_t errorType); + CameraStandard::CameraFormat ConvertToCameraFormat(int32_t format); void UpdateSettingCache(const std::string& metadataStr); private: @@ -76,7 +78,7 @@ private: std::queue cameraMetadatas_; sptr photoSurface_; sptr videoSurface_; - sptr cameraInfo_; + sptr cameraInfo_; sptr cameraManager_; sptr captureSession_; sptr cameraInput_; diff --git a/services/cameraservice/cameraoperator/client/src/callback/dcamera_input_callback.cpp b/services/cameraservice/cameraoperator/client/src/callback/dcamera_input_callback.cpp index 37da5e37..a754d185 100644 --- a/services/cameraservice/cameraoperator/client/src/callback/dcamera_input_callback.cpp +++ b/services/cameraservice/cameraoperator/client/src/callback/dcamera_input_callback.cpp @@ -16,10 +16,8 @@ #include "dcamera_input_callback.h" #include "camera_util.h" -#include "dcamera_utils_tools.h" #include "distributed_camera_constants.h" #include "distributed_hardware_log.h" -#include "metadata_utils.h" namespace OHOS { namespace DistributedHardware { @@ -44,42 +42,5 @@ void DCameraInputCallback::OnError(const int32_t errorType, const int32_t errorM } callback_->OnStateChanged(event); } - -void DCameraInputCallback::OnFocusState(FocusState state) -{ - DHLOGI("DCameraInputCallback::OnFocusState, state: %d", state); - if (callback_ == nullptr) { - DHLOGE("DCameraInputCallback::OnFocusState StateCallback is null"); - return; - } - - auto iter = focusStateMap_.find(state); - if (iter == focusStateMap_.end()) { - DHLOGE("DCameraInputCallback::OnFocusState focusStateMap find %d state failed", state); - return; - } - - int32_t itemCapacity = 10; - int32_t dataCapacity = 100; - int32_t dataCount = 1; - uint8_t focusState = iter->second; - std::shared_ptr cameraMetadata = - std::make_shared(itemCapacity, dataCapacity); - if (!cameraMetadata->addEntry(OHOS_CONTROL_FOCUS_STATE, &focusState, dataCount)) { - DHLOGE("DCameraInputCallback::OnFocusState cameraMetadata add entry failed"); - return; - } - - std::string abilityString = Camera::MetadataUtils::EncodeToString(cameraMetadata); - std::string encodeString = Base64Encode(reinterpret_cast(abilityString.c_str()), - abilityString.length()); - - std::shared_ptr dcSetting = std::make_shared(); - dcSetting->type_ = DCSettingsType::METADATA_RESULT; - dcSetting->value_ = encodeString; - std::vector> settings; - settings.push_back(dcSetting); - callback_->OnMetadataResult(settings); -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/src/callback/dcamera_manager_callback.cpp b/services/cameraservice/cameraoperator/client/src/callback/dcamera_manager_callback.cpp index f65adf08..da9d73c0 100644 --- a/services/cameraservice/cameraoperator/client/src/callback/dcamera_manager_callback.cpp +++ b/services/cameraservice/cameraoperator/client/src/callback/dcamera_manager_callback.cpp @@ -27,7 +27,7 @@ void DCameraManagerCallback::OnCameraStatusChanged(const CameraStandard::CameraS } void DCameraManagerCallback::OnFlashlightStatusChanged(const std::string &cameraID, - const CameraStandard::FlashlightStatus flashStatus) const + const CameraStandard::FlashStatus flashStatus) const { DHLOGI("DCameraManagerCallback::OnFlashlightStatusChanged, cameraID: %s, flashStatus: %d", GetAnonyString(cameraID).c_str(), flashStatus); diff --git a/services/cameraservice/cameraoperator/client/src/callback/dcamera_session_callback.cpp b/services/cameraservice/cameraoperator/client/src/callback/dcamera_session_callback.cpp index 1fa78972..3eac26b3 100644 --- a/services/cameraservice/cameraoperator/client/src/callback/dcamera_session_callback.cpp +++ b/services/cameraservice/cameraoperator/client/src/callback/dcamera_session_callback.cpp @@ -15,9 +15,11 @@ #include "dcamera_session_callback.h" +#include "dcamera_event_cmd.h" +#include "dcamera_utils_tools.h" #include "distributed_camera_constants.h" #include "distributed_hardware_log.h" -#include "dcamera_event_cmd.h" +#include "metadata_utils.h" namespace OHOS { namespace DistributedHardware { @@ -38,5 +40,42 @@ void DCameraSessionCallback::OnError(int32_t errorCode) event->eventResult_ = DCAMERA_EVENT_DEVICE_ERROR; callback_->OnStateChanged(event); } + +void DCameraSessionCallback::OnFocusState(FocusState state) +{ + DHLOGI("DCameraSessionCallback::OnFocusState, state: %d", state); + if (callback_ == nullptr) { + DHLOGE("DCameraSessionCallback::OnFocusState StateCallback is null"); + return; + } + + auto iter = focusStateMap_.find(state); + if (iter == focusStateMap_.end()) { + DHLOGE("DCameraSessionCallback::OnFocusState focusStateMap find %d state failed", state); + return; + } + + int32_t itemCapacity = 10; + int32_t dataCapacity = 100; + int32_t dataCount = 1; + uint8_t focusState = iter->second; + std::shared_ptr cameraMetadata = + std::make_shared(itemCapacity, dataCapacity); + if (!cameraMetadata->addEntry(OHOS_CONTROL_FOCUS_STATE, &focusState, dataCount)) { + DHLOGE("DCameraSessionCallback::OnFocusState cameraMetadata add entry failed"); + return; + } + + std::string abilityString = Camera::MetadataUtils::EncodeToString(cameraMetadata); + std::string encodeString = Base64Encode(reinterpret_cast(abilityString.c_str()), + abilityString.length()); + + std::shared_ptr dcSetting = std::make_shared(); + dcSetting->type_ = DCSettingsType::METADATA_RESULT; + dcSetting->value_ = encodeString; + std::vector> settings; + settings.push_back(dcSetting); + callback_->OnMetadataResult(settings); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp index 69f5549f..0550ef47 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client.cpp @@ -55,7 +55,7 @@ int32_t DCameraClient::Init() } cameraManager_->SetCallback(std::make_shared()); - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraClient::Init camera size: %d", cameraList.size()); for (auto& info : cameraList) { if (info->GetID() == cameraId_) { @@ -269,6 +269,7 @@ void DCameraClient::ReleaseCaptureSession() DHLOGE("DCameraClient::StopCapture captureSession stop failed, cameraId: %s, ret: %d", GetAnonyString(cameraId_).c_str(), ret); } + cameraInput_->Close(); DHLOGI("DCameraClient::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str()); captureSession_->Release(); captureSession_ = nullptr; @@ -302,9 +303,9 @@ int32_t DCameraClient::ConfigCaptureSession(std::vector &)cameraInput_)->Open(); std::shared_ptr inputCallback = std::make_shared(stateCallback_); ((sptr &)cameraInput_)->SetErrorCallback(inputCallback); - ((sptr &)cameraInput_)->SetFocusCallback(inputCallback); while (!cameraMetadatas_.empty()) { std::string metadataStr = cameraMetadatas_.front(); @@ -324,7 +325,10 @@ int32_t DCameraClient::ConfigCaptureSession(std::vectorSetCallback(std::make_shared(stateCallback_)); + + std::shared_ptr sessionCallback = std::make_shared(stateCallback_); + captureSession_->SetFocusCallback(sessionCallback); + captureSession_->SetCallback(sessionCallback); int32_t ret = CreateCaptureOutput(captureInfos); if (ret != DCAMERA_OK) { @@ -427,7 +431,10 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in photoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(info->format_)); photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); photoSurface_->RegisterConsumerListener((sptr &)photoListener_); - photoOutput_ = cameraManager_->CreatePhotoOutput(photoSurface_); + CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_); + CameraStandard::Size photoSize = {info->width_, info->height_}; + CameraStandard::Profile photoProfile(photoFormat, photoSize); + photoOutput_ = cameraManager_->CreatePhotoOutput(photoProfile, photoSurface_); if (photoOutput_ == nullptr) { DHLOGE("DCameraClient::CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str()); return DCAMERA_BAD_VALUE; @@ -448,7 +455,11 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in videoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(info->format_)); videoListener_ = new DCameraVideoSurfaceListener(videoSurface_, resultCallback_); videoSurface_->RegisterConsumerListener((sptr &)videoListener_); - videoOutput_ = cameraManager_->CreateVideoOutput(videoSurface_); + CameraStandard::CameraFormat videoFormat = ConvertToCameraFormat(info->format_); + CameraStandard::Size videoSize = {info->width_, info->height_}; + std::vector framerates = {}; + CameraStandard::VideoProfile videoProfile(videoFormat, videoSize, framerates); + videoOutput_ = cameraManager_->CreateVideoOutput(videoProfile, videoSurface_); if (videoOutput_ == nullptr) { DHLOGE("DCameraClient::CreateVideoOutput %s create video output failed", GetAnonyString(cameraId_).c_str()); return DCAMERA_BAD_VALUE; @@ -459,6 +470,29 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in 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 OHOS_CAMERA_FORMAT_RGBA_8888: + ret = CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888; + break; + case OHOS_CAMERA_FORMAT_YCBCR_420_888: + ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888; + break; + case OHOS_CAMERA_FORMAT_YCRCB_420_SP: + ret = CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP; + break; + case 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_) { diff --git a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp index c2f351d8..3ac4afef 100644 --- a/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp +++ b/services/cameraservice/cameraoperator/client/src/dcamera_client_common.cpp @@ -55,7 +55,7 @@ int32_t DCameraClient::Init() } cameraManager_->SetCallback(std::make_shared()); - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraClientCommon::Init camera size: %d", cameraList.size()); for (auto& info : cameraList) { if (info->GetID() == cameraId_) { @@ -225,6 +225,7 @@ void DCameraClient::ReleaseCaptureSession() DHLOGE("DCameraClientCommon::StopCapture captureSession stop failed, cameraId: %s, ret: %d", GetAnonyString(cameraId_).c_str(), ret); } + cameraInput_->Close(); DHLOGI("DCameraClientCommon::StopCapture %s release captureSession", GetAnonyString(cameraId_).c_str()); captureSession_->Release(); captureSession_ = nullptr; @@ -261,9 +262,9 @@ int32_t DCameraClient::ConfigCaptureSession(std::vector &)cameraInput_)->Open(); std::shared_ptr inputCallback = std::make_shared(stateCallback_); ((sptr &)cameraInput_)->SetErrorCallback(inputCallback); - ((sptr &)cameraInput_)->SetFocusCallback(inputCallback); captureSession_ = cameraManager_->CreateCaptureSession(); if (captureSession_ == nullptr) { @@ -271,7 +272,10 @@ int32_t DCameraClient::ConfigCaptureSession(std::vectorSetCallback(std::make_shared(stateCallback_)); + + std::shared_ptr sessionCallback = std::make_shared(stateCallback_); + captureSession_->SetFocusCallback(sessionCallback); + captureSession_->SetCallback(sessionCallback); int32_t ret = CreateCaptureOutput(captureInfos); if (ret != DCAMERA_OK) { @@ -375,7 +379,10 @@ int32_t DCameraClient::CreatePhotoOutput(std::shared_ptr& in photoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888)); photoListener_ = new DCameraPhotoSurfaceListener(photoSurface_, resultCallback_); photoSurface_->RegisterConsumerListener((sptr &)photoListener_); - photoOutput_ = cameraManager_->CreatePhotoOutput(photoSurface_); + CameraStandard::CameraFormat photoFormat = ConvertToCameraFormat(info->format_); + CameraStandard::Size photoSize = {info->width_, info->height_}; + CameraStandard::Profile photoProfile(photoFormat, photoSize); + photoOutput_ = cameraManager_->CreatePhotoOutput(photoProfile, photoSurface_); if (photoOutput_ == nullptr) { DHLOGE("DCameraClientCommon::CreatePhotoOutput %s create photo output failed", GetAnonyString(cameraId_).c_str()); @@ -397,7 +404,10 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in videoSurface_->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888)); videoListener_ = new DCameraVideoSurfaceListener(videoSurface_, resultCallback_); videoSurface_->RegisterConsumerListener((sptr &)videoListener_); - previewOutput_ = cameraManager_->CreateCustomPreviewOutput(videoSurface_, info->width_, info->height_); + CameraStandard::CameraFormat previewFormat = ConvertToCameraFormat(info->format_); + CameraStandard::Size previewSize = {info->width_, info->height_}; + CameraStandard::Profile previewProfile(previewFormat, previewSize); + previewOutput_ = cameraManager_->CreatePreviewOutput(previewProfile, videoSurface_); if (previewOutput_ == nullptr) { DHLOGE("DCameraClientCommon::CreatePreviewOutput %s create preview output failed", GetAnonyString(cameraId_).c_str()); @@ -409,6 +419,29 @@ int32_t DCameraClient::CreateVideoOutput(std::shared_ptr& in 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_) { diff --git a/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn index b17c18fc..2d731e5c 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn +++ b/services/cameraservice/cameraoperator/client/test/sample/BUILD.gn @@ -18,6 +18,8 @@ import( config("module_private_config") { include_dirs = [ + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//base/security/access_token/interfaces/innerkits/token_setproc/include", "//drivers/peripheral/camera/interfaces/metadata/include", "//third_party/jsoncpp/include", "//commonlibrary/c_utils/base/include", @@ -70,13 +72,14 @@ ohos_executable("dcamera_client_demo") { "${graphicstandard_path}:libsurface", "${services_path}/cameraservice/cameraoperator/client:distributed_camera_client", "${services_path}/cameraservice/sinkservice:distributed_camera_sink", + "//base/security/access_token/interfaces/innerkits/nativetoken:libnativetoken", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", "//drivers/peripheral/camera/interfaces/metadata:metadata", "//third_party/jsoncpp:jsoncpp", ] external_deps = [ "access_token:libaccesstoken_sdk", - "access_token:libnativetoken", "access_token:libtoken_setproc", "c_utils:utils", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.0", diff --git a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h index a97e8c47..d86bb827 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h +++ b/services/cameraservice/cameraoperator/client/test/sample/dcamera_client_demo.h @@ -20,10 +20,10 @@ #include "anonymous_string.h" #include "camera_device_ability_items.h" -#include "camera_info.h" #include "camera_input.h" #include "camera_manager.h" #include "camera_metadata_operator.h" +#include "camera_output_capability.h" #include "capture_input.h" #include "capture_output.h" #include "capture_session.h" @@ -192,7 +192,7 @@ protected: } }; -class DemoDCameraPhotoCallback : public CameraStandard::PhotoCallback { +class DemoDCameraPhotoCallback : public CameraStandard::PhotoStateCallback { public: void OnCaptureStarted(const int32_t captureID) const { @@ -215,7 +215,7 @@ public: } }; -class DemoDCameraPreviewCallback : public CameraStandard::PreviewCallback { +class DemoDCameraPreviewCallback : public CameraStandard::PreviewStateCallback { public: void OnFrameStarted() const { @@ -233,7 +233,7 @@ public: } }; -class DemoDCameraVideoCallback : public CameraStandard::VideoCallback { +class DemoDCameraVideoCallback : public CameraStandard::VideoStateCallback { public: void OnFrameStarted() const { @@ -251,17 +251,12 @@ public: } }; -class DemoDCameraInputCallback : public CameraStandard::ErrorCallback, public CameraStandard::FocusCallback { +class DemoDCameraInputCallback : public CameraStandard::ErrorCallback { public: void OnError(const int32_t errorType, const int32_t errorMsg) const { DHLOGI("DemoDCameraInputCallback::OnError errorType: %d errorMsg: %d", errorType, errorMsg); } - - void OnFocusState(FocusState state) - { - DHLOGI("DemoDCameraInputCallback::OnFocusState state: %d", state); - } }; class DemoDCameraManagerCallback : public CameraStandard::CameraManagerCallback { @@ -272,19 +267,24 @@ public: } void OnFlashlightStatusChanged(const std::string &cameraID, - const CameraStandard::FlashlightStatus flashStatus) const + const CameraStandard::FlashStatus flashStatus) const { DHLOGI("DemoDCameraManagerCallback::OnFlashlightStatusChanged cameraID: %s, flashStatus: %d", GetAnonyString(cameraID).c_str(), flashStatus); } }; -class DemoDCameraSessionCallback : public CameraStandard::SessionCallback { +class DemoDCameraSessionCallback : public CameraStandard::SessionCallback, public CameraStandard::FocusCallback { public: void OnError(int32_t errorCode) { DHLOGI("DemoDCameraSessionCallback::OnError errorCode: %d", errorCode); } + + void OnFocusState(FocusState state) + { + DHLOGI("DemoDCameraSessionCallback::OnFocusState state: %d", state); + } }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/cameraoperator/client/test/sample/main.cpp b/services/cameraservice/cameraoperator/client/test/sample/main.cpp index df39e65a..ffc59187 100644 --- a/services/cameraservice/cameraoperator/client/test/sample/main.cpp +++ b/services/cameraservice/cameraoperator/client/test/sample/main.cpp @@ -15,8 +15,10 @@ #include "dcamera_client_demo.h" +#include "access_token.h" #include "accesstoken_kit.h" -#include "nativetoken_kit.h" +#include "hap_token_info.h" +#include "ipc_skeleton.h" #include "token_setproc.h" using namespace OHOS; @@ -36,7 +38,7 @@ constexpr int32_t VIDEO_HEIGTH = 480; constexpr int32_t SLEEP_FIVE_SECOND = 5; constexpr int32_t SLEEP_TWENTY_SECOND = 20; -static sptr g_cameraInfo = nullptr; +static sptr g_cameraInfo = nullptr; static sptr g_cameraManager = nullptr; static sptr g_cameraInput = nullptr; static sptr g_photoOutput = nullptr; @@ -65,13 +67,13 @@ static int32_t InitCameraStandard() g_captureSession = g_cameraManager->CreateCaptureSession(); g_captureSession->SetCallback(std::make_shared()); - std::vector> cameraObjList = g_cameraManager->GetCameras(); + std::vector> cameraObjList = g_cameraManager->GetSupportedCameras(); for (auto info : cameraObjList) { DHLOGI("Camera: %s, position: %d, camera type: %d, connection type: %d", GetAnonyString(info->GetID()).c_str(), info->GetPosition(), info->GetCameraType(), info->GetConnectionType()); // OHOS_CAMERA_POSITION_FRONT or OHOS_CAMERA_POSITION_BACK - if ((info->GetPosition() == OHOS_CAMERA_POSITION_FRONT) && - (info->GetConnectionType() == OHOS_CAMERA_CONNECTION_TYPE_REMOTE)) { + if ((info->GetPosition() == CameraPosition::CAMERA_POSITION_FRONT) && + (info->GetConnectionType() == ConnectionType::CAMERA_CONNECTION_REMOTE)) { g_cameraInfo = info; break; } @@ -83,9 +85,10 @@ static int32_t InitCameraStandard() } g_cameraInput = g_cameraManager->CreateCameraInput(g_cameraInfo); + ((sptr &)g_cameraInput)->Open(); std::shared_ptr inputCallback = std::make_shared(); ((sptr &)g_cameraInput)->SetErrorCallback(inputCallback); - ((sptr &)g_cameraInput)->SetFocusCallback(inputCallback); + g_captureSession->SetFocusCallback(std::make_shared()); return DCAMERA_OK; } @@ -107,6 +110,29 @@ static void InitCaptureInfo() g_videoInfo->format_ = VIDEO_FORMAT; } +static CameraFormat ConvertToCameraFormat(int32_t format) +{ + CameraFormat ret = CameraFormat::CAMERA_FORMAT_INVALID; + DCameraFormat df = static_cast(format); + switch (df) { + case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888: + ret = CameraFormat::CAMERA_FORMAT_RGBA_8888; + break; + case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888: + ret = CameraFormat::CAMERA_FORMAT_YCBCR_420_888; + break; + case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP: + ret = CameraFormat::CAMERA_FORMAT_YUV_420_SP; + break; + case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG: + ret = CameraFormat::CAMERA_FORMAT_JPEG; + break; + default: + break; + } + return ret; +} + static void InitPhotoOutput() { DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %d, height = %d, format = %d", @@ -116,7 +142,10 @@ static void InitPhotoOutput() photoSurface->SetDefaultWidthAndHeight(g_photoInfo->width_, g_photoInfo->height_); photoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_photoInfo->format_)); photoSurface->RegisterConsumerListener(photoListener); - g_photoOutput = g_cameraManager->CreatePhotoOutput(photoSurface); + CameraFormat photoFormat = ConvertToCameraFormat(g_photoInfo->format_); + Size photoSize = {g_photoInfo->width_, g_photoInfo->height_}; + Profile photoProfile(photoFormat, photoSize); + g_photoOutput = g_cameraManager->CreatePhotoOutput(photoProfile, photoSurface); ((sptr &)g_photoOutput)->SetCallback(std::make_shared()); } @@ -129,7 +158,10 @@ static void InitPreviewOutput() previewSurface->SetDefaultWidthAndHeight(g_previewInfo->width_, g_previewInfo->height_); previewSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_previewInfo->format_)); previewSurface->RegisterConsumerListener(previewListener); - g_previewOutput = g_cameraManager->CreatePreviewOutput(previewSurface); + CameraFormat previewFormat = ConvertToCameraFormat(g_previewInfo->format_); + Size previewSize = {g_previewInfo->width_, g_previewInfo->height_}; + Profile previewProfile(previewFormat, previewSize); + g_previewOutput = g_cameraManager->CreatePreviewOutput(previewProfile, previewSurface); ((sptr &)g_previewOutput)->SetCallback(std::make_shared()); } @@ -142,7 +174,11 @@ static void InitVideoOutput() videoSurface->SetDefaultWidthAndHeight(g_videoInfo->width_, g_videoInfo->height_); videoSurface->SetUserData(CAMERA_SURFACE_FORMAT, std::to_string(g_videoInfo->format_)); videoSurface->RegisterConsumerListener(videoListener); - g_videoOutput = g_cameraManager->CreateVideoOutput(videoSurface); + CameraFormat videoFormat = ConvertToCameraFormat(g_videoInfo->format_); + Size videoSize = {g_videoInfo->width_, g_videoInfo->height_}; + std::vector framerates = {}; + VideoProfile videoProfile(videoFormat, videoSize, framerates); + g_videoOutput = g_cameraManager->CreateVideoOutput(videoProfile, videoSurface); ((sptr &)g_videoOutput)->SetCallback(std::make_shared()); } @@ -164,19 +200,19 @@ static void ConfigCaptureSession() static void ConfigFocusAndExposure() { - ((sptr &)g_cameraInput)->LockForControl(); - camera_focus_mode_enum_t focusMode = OHOS_CAMERA_FOCUS_MODE_AUTO; - camera_exposure_mode_enum_t exposureMode = OHOS_CAMERA_EXPOSURE_MODE_AUTO; + g_captureSession->LockForControl(); + FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO; + ExposureMode exposureMode = EXPOSURE_MODE_AUTO; int32_t exposureValue = 0; - std::vector biasRange = ((sptr &)g_cameraInput)->GetExposureBiasRange(); + std::vector biasRange = g_captureSession->GetExposureBiasRange(); if (!biasRange.empty()) { DHLOGI("Distributed Camera Demo: get %d exposure compensation range", biasRange.size()); exposureValue = biasRange[0]; } - ((sptr &)g_cameraInput)->SetFocusMode(focusMode); - ((sptr &)g_cameraInput)->SetExposureMode(exposureMode); - ((sptr &)g_cameraInput)->SetExposureBias(exposureValue); - ((sptr &)g_cameraInput)->UnlockForControl(); + g_captureSession->SetFocusMode(focusMode); + g_captureSession->SetExposureMode(exposureMode); + g_captureSession->SetExposureBias(exposureValue); + g_captureSession->UnlockForControl(); } static std::shared_ptr ConfigPhotoCaptureSetting() @@ -186,7 +222,7 @@ static std::shared_ptr ConfigPhotoCaptureSetting() PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0; photoCaptureSettings->SetRotation(rotation); // QualityLevel - PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::HIGH_QUALITY; + PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH; photoCaptureSettings->SetQuality(quality); // Location std::unique_ptr location = std::make_unique(); @@ -197,25 +233,86 @@ static std::shared_ptr ConfigPhotoCaptureSetting() return photoCaptureSettings; } +static std::string permissionName = "ohos.permission.CAMERA"; +static OHOS::Security::AccessToken::HapInfoParams g_infoManagerTestInfoParms = { + .userID = 1, + .bundleName = permissionName, + .instIndex = 0, + .appIDDesc = "testtesttesttest" +}; + +static OHOS::Security::AccessToken::PermissionDef g_infoManagerTestPermDef1 = { + .permissionName = "ohos.permission.CAMERA", + .bundleName = "ohos.permission.CAMERA", + .grantMode = 1, + .availableLevel = OHOS::Security::AccessToken::ATokenAplEnum::APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "dcamera client test", + .descriptionId = 1 +}; + +static OHOS::Security::AccessToken::PermissionStateFull g_infoManagerTestState1 = { + .permissionName = "ohos.permission.CAMERA", + .isGeneral = true, + .resDeviceID = {"local"}, + .grantStatus = {OHOS::Security::AccessToken::PermissionState::PERMISSION_GRANTED}, + .grantFlags = {1} +}; + +static OHOS::Security::AccessToken::HapPolicyParams g_infoManagerTestPolicyPrams = { + .apl = OHOS::Security::AccessToken::ATokenAplEnum::APL_NORMAL, + .domain = "test.domain", + .permList = {g_infoManagerTestPermDef1}, + .permStateList = {g_infoManagerTestState1} +}; + int main() { - uint64_t tokenId; - const char *perms[2]; - perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC"; - perms[1] = "ohos.permission.CAMERA"; - NativeTokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 2, - .aclsNum = 0, - .dcaps = NULL, - .perms = perms, - .acls = NULL, - .processName = "dcamera_client_demo", - .aplStr = "system_basic", - }; - tokenId = GetAccessTokenId(&infoInstance); - SetSelfTokenID(tokenId); - OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + /* Grant the permission so that create camera test can be success */ + int32_t rc = -1; + OHOS::Security::AccessToken::AccessTokenIDEx tokenIdEx = {0}; + tokenIdEx = OHOS::Security::AccessToken::AccessTokenKit::AllocHapToken( + g_infoManagerTestInfoParms, + g_infoManagerTestPolicyPrams); + if (tokenIdEx.tokenIdExStruct.tokenID == 0) { + unsigned int tokenIdOld = 0; + DHLOGI("Alloc TokenID failure, cleaning the old token ID \n"); + tokenIdOld = OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenID( + 1, permissionName, 0); + if (tokenIdOld == 0) { + DHLOGI("Unable to get the Old Token ID, need to reflash the board"); + return 0; + } + rc = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(tokenIdOld); + if (rc != 0) { + DHLOGI("Unable to delete the Old Token ID, need to reflash the board"); + return 0; + } + + /* Retry the token allocation again */ + tokenIdEx = OHOS::Security::AccessToken::AccessTokenKit::AllocHapToken( + g_infoManagerTestInfoParms, + g_infoManagerTestPolicyPrams); + if (tokenIdEx.tokenIdExStruct.tokenID == 0) { + DHLOGI("Alloc TokenID failure, need to reflash the board \n"); + return 0; + } + } + + (void)SetSelfTokenID(tokenIdEx.tokenIdExStruct.tokenID); + + rc = Security::AccessToken::AccessTokenKit::GrantPermission( + tokenIdEx.tokenIdExStruct.tokenID, + permissionName, OHOS::Security::AccessToken::PERMISSION_USER_FIXED); + if (rc != 0) { + DHLOGI("GrantPermission() failed"); + (void)OHOS::Security::AccessToken::AccessTokenKit::DeleteToken( + tokenIdEx.tokenIdExStruct.tokenID); + return 0; + } else { + DHLOGI("GrantPermission() success"); + } DHLOGI("========== Distributed Camera Demo Start =========="); int32_t ret = InitCameraStandard(); @@ -246,6 +343,7 @@ int main() sleep(SLEEP_FIVE_SECOND); g_captureSession->Stop(); + g_cameraInput->Close(); g_captureSession->Release(); g_cameraInput->Release(); DHLOGI("========== Distributed Camera Demo End =========="); diff --git a/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h b/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h index 83a77072..cbe196d5 100644 --- a/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h +++ b/services/cameraservice/cameraoperator/handler/include/dcamera_handler.h @@ -23,6 +23,7 @@ #include "camera_info.h" #include "camera_input.h" #include "camera_manager.h" +#include "camera_output_capability.h" #include "json/json.h" #include "single_instance.h" #include "v1_0/dcamera_types.h" @@ -30,11 +31,6 @@ namespace OHOS { namespace DistributedHardware { using namespace OHOS::HDI::DistributedCamera::V1_0; -struct ConfigInfo { - DCStreamType type; - std::string formatKey; - sptr cameraInput; -}; class DCameraHandler : public IHardwareHandler { DECLARE_SINGLE_INSTANCE_BASE(DCameraHandler); @@ -53,11 +49,12 @@ private: ~DCameraHandler(); private: - DHItem CreateDHItem(sptr& info); - std::string GetCameraPosition(camera_position_enum_t position); - void ConfigFormatAndResolution(ConfigInfo& info, Json::Value& outputFormat, Json::Value& resolution, - std::vector& formatList, std::set& formatSet); - bool IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size); + DHItem CreateDHItem(sptr& info); + std::string GetCameraPosition(CameraStandard::CameraPosition position); + void ConfigFormatAndResolution(const DCStreamType type, Json::Value& root, + std::vector& profileList); + bool IsValid(const DCStreamType type, const CameraStandard::Size& size); + int32_t CovertToDcameraFormat(CameraStandard::CameraFormat format); sptr cameraManager_; std::shared_ptr pluginListener_; diff --git a/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp b/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp index 3f647dc3..44b62e20 100644 --- a/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp +++ b/services/cameraservice/cameraoperator/handler/src/dcamera_handler.cpp @@ -53,14 +53,14 @@ int32_t DCameraHandler::Initialize() std::vector DCameraHandler::Query() { std::vector itemList; - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraHandler::Query get %d cameras", cameraList.size()); if (cameraList.empty()) { DHLOGE("DCameraHandler::Query no camera device"); return itemList; } for (auto& info : cameraList) { - if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) { + if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) { DHLOGI("DCameraHandler::Query connection type: %d", info->GetConnectionType()); continue; } @@ -104,14 +104,14 @@ void DCameraHandler::UnRegisterPluginListener() std::vector DCameraHandler::GetCameras() { std::vector cameras; - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraHandler::GetCameras get %d cameras", cameraList.size()); if (cameraList.empty()) { DHLOGE("DCameraHandler::GetCameras no camera device"); return cameras; } for (auto& info : cameraList) { - if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) { + if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) { DHLOGI("DCameraHandler::GetCameras connection type: %d", info->GetConnectionType()); continue; } @@ -124,7 +124,7 @@ std::vector DCameraHandler::GetCameras() return cameras; } -DHItem DCameraHandler::CreateDHItem(sptr& info) +DHItem DCameraHandler::CreateDHItem(sptr& info) { DHItem item; std::string id = info->GetID(); @@ -144,31 +144,19 @@ DHItem DCameraHandler::CreateDHItem(sptr& info) DHLOGI("DCameraHandler::CreateDHItem codec type: %s", name.c_str()); } + sptr capability = cameraManager_->GetSupportedOutputCapability(info); + std::vector photoProfiles = capability->GetPhotoProfiles(); + ConfigFormatAndResolution(SNAPSHOT_FRAME, root, photoProfiles); + + std::vector previewProfiles = capability->GetPreviewProfiles(); + ConfigFormatAndResolution(CONTINUOUS_FRAME, root, previewProfiles); + sptr cameraInput = cameraManager_->CreateCameraInput(info); if (cameraInput == nullptr) { DHLOGE("DCameraHandler::CreateDHItem create cameraInput failed"); return item; } - Json::Value outputFormat; - Json::Value resolution; - std::set formatSet; - - std::vector videoFormats = cameraInput->GetSupportedVideoFormats(); - ConfigInfo videoConfig = {CONTINUOUS_FRAME, CAMERA_FORMAT_VIDEO, cameraInput}; - ConfigFormatAndResolution(videoConfig, outputFormat, resolution, videoFormats, formatSet); - - std::vector previewFormats = cameraInput->GetSupportedPreviewFormats(); - ConfigInfo previewInfo = {CONTINUOUS_FRAME, CAMERA_FORMAT_PREVIEW, cameraInput}; - ConfigFormatAndResolution(previewInfo, outputFormat, resolution, previewFormats, formatSet); - - std::vector photoFormats = cameraInput->GetSupportedPhotoFormats(); - ConfigInfo photoConfig = {SNAPSHOT_FRAME, CAMERA_FORMAT_PHOTO, cameraInput}; - ConfigFormatAndResolution(photoConfig, outputFormat, resolution, photoFormats, formatSet); - - root[CAMERA_FORMAT_KEY] = outputFormat; - root[CAMERA_RESOLUTION_KEY] = resolution; - std::hash h; std::string abilityString = cameraInput->GetCameraSettings(); DHLOGI("DCameraHandler::CreateDHItem abilityString hash: %zu, length: %zu", @@ -185,20 +173,20 @@ DHItem DCameraHandler::CreateDHItem(sptr& info) return item; } -std::string DCameraHandler::GetCameraPosition(camera_position_enum_t position) +std::string DCameraHandler::GetCameraPosition(CameraStandard::CameraPosition position) { DHLOGI("DCameraHandler::GetCameraPosition position: %d", position); std::string ret = ""; switch (position) { - case OHOS_CAMERA_POSITION_BACK: { + case CameraStandard::CameraPosition::CAMERA_POSITION_BACK: { ret = CAMERA_POSITION_BACK; break; } - case OHOS_CAMERA_POSITION_FRONT: { + case CameraStandard::CameraPosition::CAMERA_POSITION_FRONT: { ret = CAMERA_POSITION_FRONT; break; } - case OHOS_CAMERA_POSITION_OTHER: { + case CameraStandard::CameraPosition::CAMERA_POSITION_UNSPECIFIED: { ret = CAMERA_POSITION_UNSPECIFIED; break; } @@ -211,29 +199,63 @@ std::string DCameraHandler::GetCameraPosition(camera_position_enum_t position) return ret; } -void DCameraHandler::ConfigFormatAndResolution(ConfigInfo& info, Json::Value& outputFormat, Json::Value& resolution, - std::vector& formatList, std::set& formatSet) +void DCameraHandler::ConfigFormatAndResolution(const DCStreamType type, Json::Value& root, + std::vector& profileList) { - DHLOGI("DCameraHandler::ConfigFormatAndResolution camera format size: %d", formatList.size()); - for (auto& format : formatList) { - DHLOGI("DCameraHandler::ConfigFormatAndResolution %s format: %d", info.formatKey.c_str(), format); - outputFormat[info.formatKey].append(format); - if (formatSet.insert(format).second) { - std::vector picSizes = info.cameraInput->getSupportedSizes(format); - std::string keyName = std::to_string(format); - DHLOGI("DCameraHandler::ConfigFormatAndResolution get %d supported camera pic size", picSizes.size()); - for (auto& size : picSizes) { - if (IsValid(info.type, size)) { - std::string value = std::to_string(size.width) + "*" + std::to_string(size.height); - resolution[keyName].append(value); - DHLOGI("DCameraHandler::ConfigFormatAndResolution format %d resolution %s", format, value.c_str()); - } + DHLOGI("DCameraHandler::ConfigFormatAndResolution camera Profile size: %d", profileList.size()); + std::set formatSet; + for (auto& profile : profileList) { + CameraStandard::CameraFormat format = profile.GetCameraFormat(); + CameraStandard::Size picSize = profile.GetSize(); + int32_t dformat = CovertToDcameraFormat(format); + formatSet.insert(dformat); + std::string formatName = std::to_string(dformat); + if (IsValid(type, picSize)) { + std::string resolutionValue = std::to_string(picSize.width) + "*" + std::to_string(picSize.height); + if (type == SNAPSHOT_FRAME) { + root[CAMERA_FORMAT_PHOTO][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue); + } else if (type == CONTINUOUS_FRAME) { + root[CAMERA_FORMAT_PREVIEW][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue); + root[CAMERA_FORMAT_VIDEO][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue); } } } + + for (auto format : formatSet) { + if (type == SNAPSHOT_FRAME) { + root[CAMERA_FORMAT_PHOTO][CAMERA_FORMAT_KEY].append(format); + } else if (type == CONTINUOUS_FRAME) { + root[CAMERA_FORMAT_PREVIEW][CAMERA_FORMAT_KEY].append(format); + root[CAMERA_FORMAT_VIDEO][CAMERA_FORMAT_KEY].append(format); + } + } +} + +int32_t DCameraHandler::CovertToDcameraFormat(CameraStandard::CameraFormat format) +{ + DHLOGI("DCameraHandler::CovertToDcameraFormat format: %d", format); + int32_t ret = -1; + switch (format) { + case CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888: + ret = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888: + ret = camera_format_t::OHOS_CAMERA_FORMAT_YCBCR_420_888; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP: + ret = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG: + ret = camera_format_t::OHOS_CAMERA_FORMAT_JPEG; + break; + default: + DHLOGE("DCameraHandler::CovertToDcameraFormat invalid camera format"); + break; + } + return ret; } -bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size) +bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::Size& size) { bool ret = false; switch (type) { diff --git a/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp b/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp index 572c5eb9..af591921 100644 --- a/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp +++ b/services/cameraservice/cameraoperator/handler/src/dcamera_handler_common.cpp @@ -53,14 +53,14 @@ int32_t DCameraHandler::Initialize() std::vector DCameraHandler::Query() { std::vector itemList; - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraHandlerCommon::Query get %d cameras", cameraList.size()); if (cameraList.empty()) { DHLOGE("DCameraHandlerCommon::Query no camera device"); return itemList; } for (auto& info : cameraList) { - if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) { + if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) { DHLOGI("DCameraHandlerCommon::Query connection type: %d", info->GetConnectionType()); continue; } @@ -104,14 +104,14 @@ void DCameraHandler::UnRegisterPluginListener() std::vector DCameraHandler::GetCameras() { std::vector cameras; - std::vector> cameraList = cameraManager_->GetCameras(); + std::vector> cameraList = cameraManager_->GetSupportedCameras(); DHLOGI("DCameraHandlerCommon::GetCameras get %d cameras", cameraList.size()); if (cameraList.empty()) { DHLOGE("DCameraHandlerCommon::GetCameras no camera device"); return cameras; } for (auto& info : cameraList) { - if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) { + if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) { DHLOGI("DCameraHandlerCommon::GetCameras connection type: %d", info->GetConnectionType()); continue; } @@ -124,7 +124,7 @@ std::vector DCameraHandler::GetCameras() return cameras; } -DHItem DCameraHandler::CreateDHItem(sptr& info) +DHItem DCameraHandler::CreateDHItem(sptr& info) { DHItem item; std::string id = info->GetID(); @@ -142,27 +142,12 @@ DHItem DCameraHandler::CreateDHItem(sptr& info) return item; } - Json::Value outputFormat; - Json::Value resolution; - std::set formatSet; + sptr capability = cameraManager_->GetSupportedOutputCapability(info); + std::vector photoProfiles = capability->GetPhotoProfiles(); + ConfigFormatAndResolution(SNAPSHOT_FRAME, root, photoProfiles); - std::vector videoFormats; - videoFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); - ConfigInfo videoConfig = {CONTINUOUS_FRAME, CAMERA_FORMAT_VIDEO, cameraInput}; - ConfigFormatAndResolution(videoConfig, outputFormat, resolution, videoFormats, formatSet); - - std::vector previewFormats; - previewFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); - ConfigInfo previewInfo = {CONTINUOUS_FRAME, CAMERA_FORMAT_PREVIEW, cameraInput}; - ConfigFormatAndResolution(previewInfo, outputFormat, resolution, previewFormats, formatSet); - - std::vector photoFormats; - photoFormats.emplace_back(camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888); - ConfigInfo photoConfig = {SNAPSHOT_FRAME, CAMERA_FORMAT_PHOTO, cameraInput}; - ConfigFormatAndResolution(photoConfig, outputFormat, resolution, photoFormats, formatSet); - - root[CAMERA_FORMAT_KEY] = outputFormat; - root[CAMERA_RESOLUTION_KEY] = resolution; + std::vector previewProfiles = capability->GetPreviewProfiles(); + ConfigFormatAndResolution(CONTINUOUS_FRAME, root, previewProfiles); std::hash h; std::string abilityString = cameraInput->GetCameraSettings(); @@ -180,20 +165,20 @@ DHItem DCameraHandler::CreateDHItem(sptr& info) return item; } -std::string DCameraHandler::GetCameraPosition(camera_position_enum_t position) +std::string DCameraHandler::GetCameraPosition(CameraStandard::CameraPosition position) { DHLOGI("DCameraHandler::GetCameraPosition position: %d", position); std::string ret = ""; switch (position) { - case OHOS_CAMERA_POSITION_BACK: { + case CameraStandard::CameraPosition::CAMERA_POSITION_BACK: { ret = CAMERA_POSITION_BACK; break; } - case OHOS_CAMERA_POSITION_FRONT: { + case CameraStandard::CameraPosition::CAMERA_POSITION_FRONT: { ret = CAMERA_POSITION_FRONT; break; } - case OHOS_CAMERA_POSITION_OTHER: { + case CameraStandard::CameraPosition::CAMERA_POSITION_UNSPECIFIED: { ret = CAMERA_POSITION_UNSPECIFIED; break; } @@ -206,21 +191,60 @@ std::string DCameraHandler::GetCameraPosition(camera_position_enum_t position) return ret; } -void DCameraHandler::ConfigFormatAndResolution(ConfigInfo& info, Json::Value& outputFormat, Json::Value& resolution, - std::vector& formatList, std::set& formatSet) +void DCameraHandler::ConfigFormatAndResolution(const DCStreamType type, Json::Value& root, + std::vector& profileList) { - DHLOGI("DCameraHandlerCommon::ConfigFormatAndResolution camera format size: %d", formatList.size()); - for (auto& format : formatList) { - DHLOGI("DCameraHandlerCommon::ConfigFormatAndResolution %s format: %d", info.formatKey.c_str(), format); - outputFormat[info.formatKey].append(format); - if (formatSet.insert(format).second) { - std::string keyName = std::to_string(format); - resolution[keyName].append("640*480"); + DHLOGI("DCameraHandler::ConfigFormatAndResolution camera Profile size: %d", profileList.size()); + std::set formatSet; + for (auto& profile : profileList) { + CameraStandard::CameraFormat format = profile.GetCameraFormat(); + format = CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888; + int32_t dformat = CovertToDcameraFormat(format); + formatSet.insert(dformat); + std::string formatName = std::to_string(dformat); + if (type == SNAPSHOT_FRAME) { + root[CAMERA_FORMAT_PHOTO][CAMERA_RESOLUTION_KEY][formatName].append("640*480"); + } else if (type == CONTINUOUS_FRAME) { + root[CAMERA_FORMAT_PREVIEW][CAMERA_RESOLUTION_KEY][formatName].append("640*480"); + root[CAMERA_FORMAT_VIDEO][CAMERA_RESOLUTION_KEY][formatName].append("640*480"); + } + } + + for (auto format : formatSet) { + if (type == SNAPSHOT_FRAME) { + root[CAMERA_FORMAT_PHOTO][CAMERA_FORMAT_KEY].append(format); + } else if (type == CONTINUOUS_FRAME) { + root[CAMERA_FORMAT_PREVIEW][CAMERA_FORMAT_KEY].append(format); + root[CAMERA_FORMAT_VIDEO][CAMERA_FORMAT_KEY].append(format); } } } -bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::CameraPicSize& size) +int32_t DCameraHandler::CovertToDcameraFormat(CameraStandard::CameraFormat format) +{ + DHLOGI("DCameraHandler::CovertToDcameraFormat format: %d", format); + int32_t ret = -1; + switch (format) { + case CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888: + ret = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888: + ret = camera_format_t::OHOS_CAMERA_FORMAT_YCBCR_420_888; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP: + ret = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP; + break; + case CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG: + ret = camera_format_t::OHOS_CAMERA_FORMAT_JPEG; + break; + default: + DHLOGE("DCameraHandler::CovertToDcameraFormat invalid camera format"); + break; + } + return ret; +} + +bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::Size& size) { bool ret = false; switch (type) { -- Gitee