diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicedcameranotify_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicedcameranotify_fuzzer/BUILD.gn index 782645acd31795bbcd97793879b3fbb93a4ae23e..a16df560aa870aea5d5fff76b2df2e855615bea2 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicedcameranotify_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicedcameranotify_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 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 @@ -65,7 +65,9 @@ ohos_fuzztest("SourceServiceDCameraNotifyFuzzTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "eventhandler:libeventhandler", "hilog:libhilog", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceinitsource_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceinitsource_fuzzer/BUILD.gn index b8a2fb7dc9775ce98c18017eb681a79c9ea847e9..f7bfe008d0029dc5c248af788ff40ab3619b2fd3 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceinitsource_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceinitsource_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 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 @@ -66,7 +66,9 @@ ohos_fuzztest("SourceServiceInitSourceFuzzTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "eventhandler:libeventhandler", "hilog:libhilog", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceregisterdistributedhardware_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceregisterdistributedhardware_fuzzer/BUILD.gn index 46f67176aec150a54549b00e55820fe253f4aae7..6cdd9889af7eec575587fffcc8e95cd14363aa27 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceregisterdistributedhardware_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceregisterdistributedhardware_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 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 @@ -66,7 +66,9 @@ ohos_fuzztest("SourceServiceRegisterDistributedHardwareFuzzTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "eventhandler:libeventhandler", "hilog:libhilog", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicereleasesource_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicereleasesource_fuzzer/BUILD.gn index bd7857b517b4eb8aad47ea59fff0b69836dc9b8e..1783b4b52612b96ff6936d207c6f763d1fbfb321 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicereleasesource_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceservicereleasesource_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 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 @@ -65,7 +65,9 @@ ohos_fuzztest("SourceServiceReleaseSourceFuzzTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "eventhandler:libeventhandler", "hilog:libhilog", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceunregisterdistributedhardware_fuzzer/BUILD.gn b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceunregisterdistributedhardware_fuzzer/BUILD.gn index 9e83115e0a0a851e0d0b7f25d5a1f088909609e2..65a1cfe0e918f1132710c630f3b3fab74bfdad1e 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceunregisterdistributedhardware_fuzzer/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/sourceserviceunregisterdistributedhardware_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 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 @@ -66,7 +66,9 @@ ohos_fuzztest("SourceServiceUnregisterDistributedHardwareFuzzTest") { external_deps = [ "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "eventhandler:libeventhandler", "hilog:libhilog", diff --git a/services/cameraservice/sourceservice/BUILD.gn b/services/cameraservice/sourceservice/BUILD.gn index 4b6bc9309ea45436809d35b1fcdab4cf38b0d449..205ef6391272798e2c11abd6a52c80de0ad9e29a 100644 --- a/services/cameraservice/sourceservice/BUILD.gn +++ b/services/cameraservice/sourceservice/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2024 Huawei Device Co., Ltd. +# Copyright (c) 2021-2025 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 @@ -84,7 +84,6 @@ ohos_shared_library("distributed_camera_source") { "src/distributedcameramgr/dcameradata/feedingsmoother/derived/dcamera_feeding_smoother.cpp", "src/distributedcameramgr/dcameradata/feedingsmoother/derived/dcamera_time_statistician.cpp", "src/distributedcameramgr/dcameradata/feedingsmoother/feeding_smoother_listener.cpp", - "src/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.cpp", "src/distributedcameramgr/dcamerahdf/dcamera_provider_callback_impl.cpp", "src/distributedcameramgr/dcamerastate/dcamera_source_capture_state.cpp", "src/distributedcameramgr/dcamerastate/dcamera_source_config_stream_state.cpp", diff --git a/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h b/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h index fc341c5719d9cf99e526f5a371bb5d191be95a2b..d84296737fe0355a33b5feae8909d5cc37a8d076 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -31,6 +31,7 @@ #include "dcamera_source_dev.h" #include "distributed_camera_constants.h" #include "distributed_camera_source_stub.h" +#include "distributed_hardware_fwk_kit.h" namespace OHOS { namespace DistributedHardware { @@ -67,6 +68,7 @@ private: int32_t LoadDCameraHDF(); int32_t UnLoadCameraHDF(); std::string GetCodecInfo(); + std::shared_ptr GetDHFwkKit(); bool registerToService_ = false; DCameraServiceState state_ = DCameraServiceState::DCAMERA_SRV_STATE_NOT_START; @@ -80,6 +82,8 @@ private: static std::mutex camDevMutex_; std::thread hicollieThread_; std::atomic isHicollieRunning_ = false; + std::shared_ptr dHFwkKit_; + std::mutex dHFwkKitMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp index db51843b9e35097c864d0187e32e2ae855b89d0b..2a5f434fbe994f5660bcbbdb109a9f5dde6cf6dc 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -26,7 +26,6 @@ #include "anonymous_string.h" #include "avcodec_info.h" #include "avcodec_list.h" -#include "dcamera_hdf_operate.h" #include "dcamera_hisysevent_adapter.h" #include "dcamera_hitrace_adapter.h" #include "dcamera_radar.h" @@ -215,6 +214,17 @@ std::string DistributedCameraSourceService::GetCodecInfo() return sourceAttrs; } +std::shared_ptr DistributedCameraSourceService::GetDHFwkKit() +{ + if (dHFwkKit_ == nullptr) { + std::lock_guard lock(dHFwkKitMutex_); + if (dHFwkKit_ == nullptr) { + dHFwkKit_ = std::make_shared(); + } + } + return dHFwkKit_; +} + int32_t DistributedCameraSourceService::RegisterDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& reqId, const EnableParam& param) { @@ -297,7 +307,12 @@ int32_t DistributedCameraSourceService::LoadDCameraHDF() { DCAMERA_SYNC_TRACE(DCAMERA_LOAD_HDF); DHLOGI("load hdf driver start"); - int32_t ret = DCameraHdfOperate::GetInstance().LoadDcameraHDFImpl(); + auto dHFwkKit = GetDHFwkKit(); + if (dHFwkKit == nullptr) { + DHLOGE("Get dHFwkKit is null when load hdf driver."); + return DCAMERA_BAD_VALUE; + } + int32_t ret = dHFwkKit->LoadDistributedHDF(DHType::CAMERA); if (ret != DCAMERA_OK) { DHLOGE("load hdf driver failed, ret %{public}d", ret); ReportDcamerInitFail(DCAMERA_INIT_FAIL, DCAMERA_HDF_ERROR, CreateMsg("dcamera load hdf driver fail.")); @@ -310,7 +325,12 @@ int32_t DistributedCameraSourceService::LoadDCameraHDF() int32_t DistributedCameraSourceService::UnLoadCameraHDF() { DHLOGI("unload hdf driver start"); - int32_t ret = DCameraHdfOperate::GetInstance().UnLoadDcameraHDFImpl(); + auto dHFwkKit = GetDHFwkKit(); + if (dHFwkKit == nullptr) { + DHLOGE("Get dHFwkKit is null when unload hdf driver."); + return DCAMERA_BAD_VALUE; + } + int32_t ret = dHFwkKit->UnLoadDistributedHDF(DHType::CAMERA); if (ret != DCAMERA_OK) { DHLOGE("unload hdf driver failed, ret %{public}d", ret); return ret; diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/BUILD.gn b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/BUILD.gn index 4686f06e17e2349b406d1effbcc997debc98f6f7..dff8a3743e946669142dd27eb526a53e78b88cd0 100644 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/BUILD.gn +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/BUILD.gn @@ -52,11 +52,13 @@ ohos_unittest("DCameraSourceTest") { module_out_path = module_out_path sources = [ + "dcamera_hdf_operate.cpp", "dcamera_service_state_listener_test.cpp", "dcamera_source_callback_proxy_test.cpp", "dcamera_source_hidumper_test.cpp", "distributed_camera_source_service_test.cpp", "distributed_camera_source_stub_test.cpp", + "mock_distributed_hardware_fwk_kit.cpp", ] cflags = [ @@ -79,11 +81,16 @@ ohos_unittest("DCameraSourceTest") { "access_token:libtokensetproc_shared", "cJSON:cjson", "c_utils:utils", + "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributedhardwareutils", + "distributed_hardware_fwk:libdhfwk_sdk", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.1", "dsoftbus:softbus_client", "eventhandler:libeventhandler", "graphic_surface:surface", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdi", + "hdf_core:libpub_utils", "hicollie:libhicollie", "hilog:libhilog", "ipc:ipc_core", diff --git a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.cpp b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.cpp similarity index 43% rename from services/cameraservice/sourceservice/src/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.cpp rename to services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.cpp index 713942b3c25fff965c39953816bfad2a4c6cbfad..95d814f0b0a6c007d1e356a9d6fe6f92b4a8ae45 100644 --- a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.cpp +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -19,36 +19,104 @@ #include #include "anonymous_string.h" -#include "distributed_camera_errno.h" +#include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#undef DH_LOG_TAG +#define DH_LOG_TAG "DCameraHdfOperate" + namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(DCameraHdfOperate); +int32_t DCameraHdfOperate::LoadDcameraHDFImpl() +{ + DHLOGI("Load camera hdf impl begin!"); + std::unique_lock loadRefLocker(hdfLoadRefMutex_); + if (hdfLoadRef_ > 0) { + hdfLoadRef_++; + DHLOGI("The camera hdf impl has been loaded, just inc ref!"); + return DH_FWK_SUCCESS; + } + int32_t ret = LoadDevice(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("LoadDevice failed, ret: %{public}d.", ret); + return ret; + } + ret = RegisterHdfListener(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("RegisterHdfListener failed, ret: %{public}d.", ret); + UnLoadDevice(); + return ret; + } + hdfLoadRef_++; + DHLOGI("Load camera hdf impl end!"); + return DH_FWK_SUCCESS; +} -void DCameraHdfServStatListener::OnReceive(const ServiceStatus& status) +int32_t DCameraHdfOperate::UnLoadDcameraHDFImpl() { - DHLOGI("service status on receive"); - if (status.serviceName == CAMERA_SERVICE_NAME || status.serviceName == PROVIDER_SERVICE_NAME) { - callback_(status); + DHLOGI("UnLoad camera hdf impl begin!"); + std::unique_lock loadRefLocker(hdfLoadRefMutex_); + if (hdfLoadRef_ == 0) { + DHLOGI("The camera hdf impl has been unloaded!"); + return DH_FWK_SUCCESS; + } + if (hdfLoadRef_ > 1) { + hdfLoadRef_--; + DHLOGI("The camera hdf impl has been loaded, just dec ref!"); + return DH_FWK_SUCCESS; } + int32_t ret = UnRegisterHdfListener(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UnRegisterHdfListener failed, ret: %{public}d.", ret); + } + ret = UnLoadDevice(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UnLoadDevice failed, ret: %{public}d.", ret); + } + hdfLoadRef_--; + DHLOGI("UnLoad camera hdf impl end!"); + return DH_FWK_SUCCESS; } -int32_t DCameraHdfOperate::LoadDcameraHDFImpl() +void DCameraHdfOperate::ResetRefCount() +{ + DHLOGI("Reset reference count for dcamera."); + std::unique_lock loadRefLocker(hdfLoadRefMutex_); + hdfLoadRef_ = 0; +} + +int32_t DCameraHdfOperate::WaitLoadCameraService() { - if (cameraServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START && - providerServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) { - DHLOGI("service has already start"); - return DCAMERA_OK; + DHLOGI("wait Load camera service."); + std::unique_lock lock(hdfOperateMutex_); + hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(CAMERA_WAIT_TIME), [this] { + return (this->cameraServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); + }); + if (cameraServStatus_.load() != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) { + DHLOGE("wait load cameraService failed, status %{public}d", cameraServStatus_.load()); + return ERR_DH_FWK_LOAD_HDF_TIMEOUT; } - OHOS::sptr servMgr = IServiceManager::Get(); - OHOS::sptr devmgr = IDeviceManager::Get(); - if (servMgr == nullptr || devmgr == nullptr) { - DHLOGE("get hdi service manager or device manager failed!"); - return DCAMERA_BAD_VALUE; + return DH_FWK_SUCCESS; +} + +int32_t DCameraHdfOperate::WaitLoadProviderService() +{ + DHLOGI("wait Load provider service."); + std::unique_lock lock(hdfOperateMutex_); + hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(CAMERA_WAIT_TIME), [this] { + return (this->providerServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); + }); + if (providerServStatus_.load() != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) { + DHLOGE("wait load providerService failed, status %{public}d", providerServStatus_.load()); + return ERR_DH_FWK_LOAD_HDF_TIMEOUT; } + return DH_FWK_SUCCESS; +} - ::OHOS::sptr listener( +OHOS::sptr DCameraHdfOperate::MakeServStatListener() +{ + return OHOS::sptr( new DCameraHdfServStatListener(DCameraHdfServStatListener::StatusCallback([&](const ServiceStatus& status) { DHLOGI("LoadCameraService service status callback, serviceName: %{public}s, status: %{public}d", status.serviceName.c_str(), status.status); @@ -60,77 +128,62 @@ int32_t DCameraHdfOperate::LoadDcameraHDFImpl() providerServStatus_.store(status.status); hdfOperateCon_.notify_one(); } - }))); + })) + ); +} + +int32_t DCameraHdfOperate::LoadDevice() +{ + DHLOGI("LoadDevice for camera begin!"); + OHOS::sptr servMgr = IServiceManager::Get(); + OHOS::sptr devmgr = IDeviceManager::Get(); + if (servMgr == nullptr || devmgr == nullptr) { + DHLOGE("get hdi service manager or device manager failed!"); + return ERR_DH_FWK_POINTER_IS_NULL; + } + OHOS::sptr listener = MakeServStatListener(); if (servMgr->RegisterServiceStatusListener(listener, DEVICE_CLASS_CAMERA) != 0) { DHLOGE("RegisterServiceStatusListener failed!"); - return DCAMERA_BAD_VALUE; + return ERR_DH_FWK_REGISTER_HDF_LISTENER_FAIL; } - DHLOGI("Load camera service."); int32_t ret = devmgr->LoadDevice(CAMERA_SERVICE_NAME); if (ret != HDF_SUCCESS && ret != HDF_ERR_DEVICE_BUSY) { - return DCAMERA_BAD_OPERATE; + DHLOGE("Load camera service failed!"); + servMgr->UnregisterServiceStatusListener(listener); + return ERR_DH_FWK_LOAD_HDF_FAIL; } - if (WaitLoadCameraService() != DCAMERA_OK) { - return DCAMERA_BAD_OPERATE; + if (WaitLoadCameraService() != DH_FWK_SUCCESS) { + DHLOGE("Wait load camera service failed!"); + servMgr->UnregisterServiceStatusListener(listener); + return ERR_DH_FWK_LOAD_HDF_TIMEOUT; } - ret = devmgr->LoadDevice(PROVIDER_SERVICE_NAME); if (ret != HDF_SUCCESS && ret != HDF_ERR_DEVICE_BUSY) { - DHLOGE("Load provider service failed!"); - return DCAMERA_BAD_OPERATE; + DHLOGE("Load camera provider service failed!"); + servMgr->UnregisterServiceStatusListener(listener); + return ERR_DH_FWK_LOAD_HDF_FAIL; } - if (WaitLoadProviderService() != DCAMERA_OK) { - return DCAMERA_BAD_OPERATE; + if (WaitLoadProviderService() != DH_FWK_SUCCESS) { + DHLOGE("Wait load camera provider service failed!"); + servMgr->UnregisterServiceStatusListener(listener); + return ERR_DH_FWK_LOAD_HDF_TIMEOUT; } - if (servMgr->UnregisterServiceStatusListener(listener) != 0) { DHLOGE("UnregisterServiceStatusListener failed!"); } - return DCAMERA_OK; -} - -int32_t DCameraHdfOperate::WaitLoadCameraService() -{ - DHLOGI("wait Load camera service."); - std::unique_lock lock(hdfOperateMutex_); - hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME), [this] { - return (this->cameraServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); - }); - - if (cameraServStatus_.load() != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) { - DHLOGE("wait load cameraService failed, status %{public}d", cameraServStatus_.load()); - return DCAMERA_BAD_OPERATE; - } - - return DCAMERA_OK; + DHLOGI("LoadDevice for camera end!"); + return DH_FWK_SUCCESS; } -int32_t DCameraHdfOperate::WaitLoadProviderService() +int32_t DCameraHdfOperate::UnLoadDevice() { - DHLOGI("wait Load provider service."); - std::unique_lock lock(hdfOperateMutex_); - hdfOperateCon_.wait_for(lock, std::chrono::milliseconds(WAIT_TIME), [this] { - return (this->providerServStatus_.load() == OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); - }); - - if (providerServStatus_.load() != OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START) { - DHLOGE("wait load providerService failed, status %{public}d", providerServStatus_.load()); - return DCAMERA_BAD_OPERATE; - } - - return DCAMERA_OK; -} - -int32_t DCameraHdfOperate::UnLoadDcameraHDFImpl() -{ - DHLOGI("UnLoadCameraHDFImpl begin!"); + DHLOGI("UnLoadDevice for camera begin!"); OHOS::sptr devmgr = IDeviceManager::Get(); if (devmgr == nullptr) { DHLOGE("get hdi device manager failed!"); - return DCAMERA_BAD_VALUE; + return ERR_DH_FWK_POINTER_IS_NULL; } - int32_t ret = devmgr->UnloadDevice(CAMERA_SERVICE_NAME); if (ret != 0) { DHLOGE("Unload camera service failed, ret: %{public}d", ret); @@ -139,10 +192,66 @@ int32_t DCameraHdfOperate::UnLoadDcameraHDFImpl() if (ret != 0) { DHLOGE("Unload provider service failed, ret: %d", ret); } - cameraServStatus_.store(INVALID_VALUE); - providerServStatus_.store(INVALID_VALUE); - DHLOGI("UnLoadCameraHDFImpl end!"); - return DCAMERA_OK; + cameraServStatus_.store(CAMERA_INVALID_VALUE); + providerServStatus_.store(CAMERA_INVALID_VALUE); + DHLOGI("UnLoadDevice for camera end!"); + return DH_FWK_SUCCESS; +} + +int32_t DCameraHdfOperate::RegisterHdfListener() +{ + DHLOGI("RegisterHdfListener for camera begin!"); + sptr camHdiProvider = IDCameraProvider::Get(PROVIDER_SERVICE_NAME); + if (camHdiProvider == nullptr) { + DHLOGE("Get hdi camera provider failed."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + if (fwkDCameraHdfCallback_ == nullptr) { + fwkDCameraHdfCallback_ = new FwkDCameraHdfCallback(); + if (fwkDCameraHdfCallback_ == nullptr) { + DHLOGE("Create FwkDCameraHdfCallback failed."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + } + int32_t ret = camHdiProvider->RegisterCameraHdfListener("DHFWK", fwkDCameraHdfCallback_); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Call hdf proxy RegisterCameraHdfListener failed, ret: %{public}d.", ret); + return ret; + } + DHLOGI("RegisterHdfListener for camera end!"); + return DH_FWK_SUCCESS; +} + +int32_t DCameraHdfOperate::UnRegisterHdfListener() +{ + DHLOGI("UnRegisterHdfListener for camera begin!"); + sptr camHdiProvider = IDCameraProvider::Get(PROVIDER_SERVICE_NAME); + if (camHdiProvider == nullptr) { + DHLOGE("Get hdi camera provider failed."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + int32_t ret = camHdiProvider->UnRegisterCameraHdfListener("DHFWK"); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Call hdf proxy UnRegisterCameraHdfListener failed, ret: %{public}d.", ret); + return ret; + } + DHLOGI("UnRegisterHdfListener for camera end!"); + return DH_FWK_SUCCESS; +} + +void DCameraHdfServStatListener::OnReceive(const ServiceStatus& status) +{ + DHLOGI("service status on receive"); + if (status.serviceName == CAMERA_SERVICE_NAME || status.serviceName == PROVIDER_SERVICE_NAME) { + callback_(status); + } +} + +int32_t FwkDCameraHdfCallback::NotifyEvent(int32_t devId, const DCameraHDFEvent& event) +{ + (void)devId; + (void)event; + return DH_FWK_SUCCESS; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.h b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.h similarity index 67% rename from services/cameraservice/sourceservice/include/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.h rename to services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.h index 3fa88200ff43dfc27fcedd9646dbb92ceec34441..f7cf7099081b68713d42667ef789facb5b8f114a 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamerahdf/dcamera_hdf_operate.h +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/dcamera_hdf_operate.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -24,35 +24,49 @@ #include "idevmgr_hdi.h" #include "iservmgr_hdi.h" #include "single_instance.h" +#include "v1_1/id_camera_provider.h" +namespace OHOS { +namespace DistributedHardware { using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; using OHOS::HDI::ServiceManager::V1_0::IServiceManager; using OHOS::HDI::ServiceManager::V1_0::IServStatListener; using OHOS::HDI::ServiceManager::V1_0::ServiceStatus; using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub; +using OHOS::HDI::DistributedCamera::V1_1::IDCameraProvider; +using OHOS::HDI::DistributedCamera::V1_1::IDCameraHdfCallback; +using OHOS::HDI::DistributedCamera::V1_1::DCameraHDFEvent; const std::string CAMERA_SERVICE_NAME = "distributed_camera_service"; const std::string PROVIDER_SERVICE_NAME = "distributed_camera_provider_service"; -constexpr uint16_t INVALID_VALUE = 0xffff; -constexpr int32_t WAIT_TIME = 2000; +constexpr uint16_t CAMERA_INVALID_VALUE = 0xffff; +constexpr int32_t CAMERA_WAIT_TIME = 2000; -namespace OHOS { -namespace DistributedHardware { +class FwkDCameraHdfCallback; class DCameraHdfOperate { DECLARE_SINGLE_INSTANCE(DCameraHdfOperate); public: int32_t LoadDcameraHDFImpl(); int32_t UnLoadDcameraHDFImpl(); + void ResetRefCount(); private: int32_t WaitLoadCameraService(); int32_t WaitLoadProviderService(); + OHOS::sptr MakeServStatListener(); + int32_t LoadDevice(); + int32_t UnLoadDevice(); + int32_t RegisterHdfListener(); + int32_t UnRegisterHdfListener(); private: - std::atomic cameraServStatus_ = INVALID_VALUE; - std::atomic providerServStatus_ = INVALID_VALUE; + OHOS::sptr fwkDCameraHdfCallback_; + std::atomic cameraServStatus_ = CAMERA_INVALID_VALUE; + std::atomic providerServStatus_ = CAMERA_INVALID_VALUE; std::condition_variable hdfOperateCon_; std::mutex hdfOperateMutex_; + int32_t hdfLoadRef_ = 0; + std::mutex hdfLoadRefMutex_; }; class DCameraHdfServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub { @@ -67,6 +81,11 @@ public: private: StatusCallback callback_; }; + +class FwkDCameraHdfCallback : public IDCameraHdfCallback { +protected: + int32_t NotifyEvent(int32_t devId, const DCameraHDFEvent& event) override; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DCAMERA_HDF_OPERATE_H \ No newline at end of file diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/mock_distributed_hardware_fwk_kit.cpp b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/mock_distributed_hardware_fwk_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe1bbf34db1c1642213740799ac69fc331dab50b --- /dev/null +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcamera/mock_distributed_hardware_fwk_kit.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 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 + +#include "dcamera_hdf_operate.h" +#include "distributed_camera_source_service.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +int32_t DistributedHardwareFwkKit::LoadDistributedHDF(const DHType dhType) +{ + return DCameraHdfOperate::GetInstance().LoadDcameraHDFImpl(); +} + +int32_t DistributedHardwareFwkKit::UnLoadDistributedHDF(const DHType dhType) +{ + return DCameraHdfOperate::GetInstance().UnLoadDcameraHDFImpl(); +} +} // DistributedHardware +} // OHOS diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/BUILD.gn b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/BUILD.gn index 81fa3d002d2eb6d8bae8c71ef3857a1a142b9b71..3f8247559bd060428be0e798cb0191068edebe6e 100644 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/BUILD.gn +++ b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/BUILD.gn @@ -59,7 +59,6 @@ ohos_unittest("DCameraSourceMgrTest") { sources = [ "dcamera_feeding_smoother_test.cpp", - "dcamera_hdf_operate_test.cpp", "dcamera_provider_callback_impl_test.cpp", "dcamera_source_config_stream_state_test.cpp", "dcamera_source_controller_test.cpp", diff --git a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_hdf_operate_test.cpp b/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_hdf_operate_test.cpp deleted file mode 100644 index 8d5843ff35ae5b13703183a4a8ba798d7fbe552a..0000000000000000000000000000000000000000 --- a/services/cameraservice/sourceservice/test/unittest/common/distributedcameramgr/dcamera_hdf_operate_test.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 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 - -#define private public -#include "dcamera_hdf_operate.h" -#undef private - -#include "distributed_camera_errno.h" -#include "distributed_hardware_log.h" - -using namespace testing::ext; - -namespace OHOS { -namespace DistributedHardware { -class DCameraHdfOperateTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void DCameraHdfOperateTest::SetUpTestCase(void) -{ - DHLOGI("DCameraHdfOperateTest::SetUpTestCase"); -} - -void DCameraHdfOperateTest::TearDownTestCase(void) -{ - DHLOGI("DCameraHdfOperateTest::TearDownTestCase"); -} - -void DCameraHdfOperateTest::SetUp(void) -{ - DHLOGI("DCameraHdfOperateTest::SetUp"); -} - -void DCameraHdfOperateTest::TearDown(void) -{ - DHLOGI("DCameraHdfOperateTest::TearDown"); -} - -/** - * @tc.name: dcamera_hdf_operate_test_001 - * @tc.desc: Verify LoadDcameraHDFImpl func. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(DCameraHdfOperateTest, dcamera_hdf_operate_test_001, TestSize.Level1) -{ - DHLOGI("DCameraHdfOperateTest::dcamera_hdf_operate_test_001"); - int32_t ret = DCameraHdfOperate::GetInstance().LoadDcameraHDFImpl(); - EXPECT_EQ(DCAMERA_OK, ret); -} - -/** - * @tc.name: dcamera_hdf_operate_test_002 - * @tc.desc: Verify UnLoadDcameraHDFImpl func. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(DCameraHdfOperateTest, dcamera_hdf_operate_test_002, TestSize.Level1) -{ - DHLOGI("DCameraHdfOperateTest::dcamera_hdf_operate_test_002"); - int32_t ret = DCameraHdfOperate::GetInstance().UnLoadDcameraHDFImpl(); - EXPECT_EQ(DCAMERA_OK, ret); -} - -/** - * @tc.name: dcamera_hdf_operate_test_003 - * @tc.desc: Verify LoadDcameraHDFImpl func. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(DCameraHdfOperateTest, dcamera_hdf_operate_test_003, TestSize.Level1) -{ - DHLOGI("DCameraHdfOperateTest::dcamera_hdf_operate_test_003"); - DCameraHdfOperate::GetInstance().cameraServStatus_.store(OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); - DCameraHdfOperate::GetInstance().providerServStatus_.store(OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_START); - int32_t ret = DCameraHdfOperate::GetInstance().LoadDcameraHDFImpl(); - EXPECT_EQ(DCAMERA_OK, ret); -} - -/** - * @tc.name: dcamera_hdf_operate_test_004 - * @tc.desc: Verify UnLoadDcameraHDFImpl func. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(DCameraHdfOperateTest, dcamera_hdf_operate_test_004, TestSize.Level1) -{ - DHLOGI("DCameraHdfOperateTest::dcamera_hdf_operate_test_004"); - DCameraHdfOperate::GetInstance().cameraServStatus_.store(OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_STOP); - int32_t ret = DCameraHdfOperate::GetInstance().WaitLoadCameraService(); - EXPECT_EQ(DCAMERA_BAD_OPERATE, ret); -} - -/** - * @tc.name: dcamera_hdf_operate_test_005 - * @tc.desc: Verify WaitLoadProviderService func. - * @tc.type: FUNC - * @tc.require: issue - */ -HWTEST_F(DCameraHdfOperateTest, dcamera_hdf_operate_test_005, TestSize.Level1) -{ - DHLOGI("DCameraHdfOperateTest::dcamera_hdf_operate_test_005"); - DCameraHdfOperate::GetInstance().providerServStatus_.store(OHOS::HDI::ServiceManager::V1_0::SERVIE_STATUS_STOP); - int32_t ret = DCameraHdfOperate::GetInstance().WaitLoadProviderService(); - EXPECT_EQ(DCAMERA_BAD_OPERATE, ret); -} -} // namespace DistributedHardware -} // namespace OHOS