From 487d02207420021213bb996b646ade8621757cbf Mon Sep 17 00:00:00 2001 From: Clone_Zhang Date: Fri, 9 Dec 2022 17:13:08 +0800 Subject: [PATCH] add low latency strategy Signed-off-by: Clone_Zhang --- distributedcamera.gni | 2 + services/cameraservice/sinkservice/BUILD.gn | 3 + .../dcamera_sink_controller.cpp | 3 + services/cameraservice/sourceservice/BUILD.gn | 3 + .../dcamera_source_controller.cpp | 4 +- services/channel/BUILD.gn | 8 +- .../channel/include/dcamera_low_latency.h | 44 ++++++++++ services/channel/src/dcamera_low_latency.cpp | 87 +++++++++++++++++++ 8 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 services/channel/include/dcamera_low_latency.h create mode 100644 services/channel/src/dcamera_low_latency.cpp diff --git a/distributedcamera.gni b/distributedcamera.gni index fd4c992c..45a7cba7 100644 --- a/distributedcamera.gni +++ b/distributedcamera.gni @@ -34,6 +34,8 @@ fwk_utils_path = "${distributedhardwarefwk_path}/utils" fwk_common_path = "${distributedhardwarefwk_path}/common" +fwk_innerkits_path = "${distributedhardwarefwk_path}/interfaces/inner_kits" + build_flags = [ "-Werror" ] declare_args() { diff --git a/services/cameraservice/sinkservice/BUILD.gn b/services/cameraservice/sinkservice/BUILD.gn index 5a3f0447..25b69bfc 100644 --- a/services/cameraservice/sinkservice/BUILD.gn +++ b/services/cameraservice/sinkservice/BUILD.gn @@ -57,6 +57,9 @@ ohos_shared_library("distributed_camera_sink") { "${services_path}/data_process/include/interfaces", "${services_path}/data_process/include/pipeline", "${services_path}/data_process/include/utils", + "${fwk_common_path}/utils/include", + "${fwk_innerkits_path}/include", + "//third_party/json/include", ] sources = [ diff --git a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp index a0f4ea03..b34120d0 100644 --- a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp +++ b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_controller.cpp @@ -36,6 +36,7 @@ #include "distributed_hardware_log.h" #include "idistributed_camera_source.h" #include "json/json.h" +#include "dcamera_low_latency.h" namespace OHOS { namespace DistributedHardware { @@ -186,6 +187,7 @@ int32_t DCameraSinkController::OpenChannel(std::shared_ptr& ope GetAnonyString(dhId_).c_str(), sessionState_); return DCAMERA_WRONG_STATE; } + DCameraLowLatency::GetInstance().EnableLowLatency(); srcDevId_ = openInfo->sourceDevId_; std::vector indexs; indexs.push_back(DCameraIndex(srcDevId_, dhId_)); @@ -206,6 +208,7 @@ int32_t DCameraSinkController::CloseChannel() { DHLOGI("DCameraSinkController::CloseChannel dhId: %s", GetAnonyString(dhId_).c_str()); std::lock_guard autoLock(channelLock_); + DCameraLowLatency::GetInstance().DisableLowLatency(); DCameraSinkServiceIpc::GetInstance().DeleteSourceRemoteCamSrv(srcDevId_); srcDevId_.clear(); int32_t ret = channel_->ReleaseSession(); diff --git a/services/cameraservice/sourceservice/BUILD.gn b/services/cameraservice/sourceservice/BUILD.gn index d260b73c..db3c44e1 100644 --- a/services/cameraservice/sourceservice/BUILD.gn +++ b/services/cameraservice/sourceservice/BUILD.gn @@ -46,6 +46,9 @@ ohos_shared_library("distributed_camera_source") { "${services_path}/data_process/include/pipeline", "${services_path}/data_process/include/utils", "${graphicstandard_path}/frameworks/surface/include", + "${fwk_common_path}/utils/include", + "${fwk_innerkits_path}/include", + "//third_party/json/include", ] sources = [ diff --git a/services/cameraservice/sourceservice/src/distributedcameramgr/dcameracontrol/dcamera_source_controller.cpp b/services/cameraservice/sourceservice/src/distributedcameramgr/dcameracontrol/dcamera_source_controller.cpp index 7af9fc22..21d19cbe 100644 --- a/services/cameraservice/sourceservice/src/distributedcameramgr/dcameracontrol/dcamera_source_controller.cpp +++ b/services/cameraservice/sourceservice/src/distributedcameramgr/dcameracontrol/dcamera_source_controller.cpp @@ -31,6 +31,7 @@ #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" #include "idistributed_camera_sink.h" +#include "dcamera_low_latency.h" namespace OHOS { namespace DistributedHardware { @@ -325,7 +326,7 @@ int32_t DCameraSourceController::CloseChannel() DHLOGE("DCameraSourceController CloseChannel not support operate %d camera", indexs_.size()); return DCAMERA_BAD_OPERATE; } - + DCameraLowLatency::GetInstance().DisableLowLatency(); std::string dhId = indexs_.begin()->dhId_; std::string devId = indexs_.begin()->devId_; DHLOGI("DCameraSourceController CloseChannel devId: %s, dhId: %s", GetAnonyString(devId).c_str(), @@ -491,6 +492,7 @@ int32_t DCameraSourceController::WaitforSessionResult() PostChannelDisconnectedEvent(); return DCAMERA_BAD_VALUE; } + DCameraLowLatency::GetInstance().EnableLowLatency(); return DCAMERA_OK; } diff --git a/services/channel/BUILD.gn b/services/channel/BUILD.gn index 58e06cba..b2ea5dd9 100644 --- a/services/channel/BUILD.gn +++ b/services/channel/BUILD.gn @@ -27,16 +27,22 @@ ohos_shared_library("distributed_camera_channel") { "include", "${common_path}/include/constants", "${common_path}/include/utils", + "${fwk_innerkits_path}/include", + "//third_party/json/include", ] sources = [ "src/dcamera_channel_sink_impl.cpp", "src/dcamera_channel_source_impl.cpp", + "src/dcamera_low_latency.cpp", "src/dcamera_softbus_adapter.cpp", "src/dcamera_softbus_session.cpp", ] - deps = [ "${common_path}:distributed_camera_utils" ] + deps = [ + "${common_path}:distributed_camera_utils", + "${fwk_innerkits_path}:libdhfwk_sdk", + ] defines = [ "HI_LOG_ENABLE", diff --git a/services/channel/include/dcamera_low_latency.h b/services/channel/include/dcamera_low_latency.h new file mode 100644 index 00000000..ce89428a --- /dev/null +++ b/services/channel/include/dcamera_low_latency.h @@ -0,0 +1,44 @@ +/* + * 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. + */ + +#ifndef DCAMERA_LOW_LATENCY_H +#define DCAMERA_LOW_LATENCY_H + +#include "distributed_hardware_fwk_kit.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class DCameraLowLatency { +DECLARE_SINGLE_INSTANCE_BASE(DCameraLowLatency); +public: + int32_t EnableLowLatency(); + int32_t DisableLowLatency(); + +private: + DCameraLowLatency() = default; + ~DCameraLowLatency() = default; + std::shared_ptr GetDHFwkKit(); + +private: + constexpr static int32_t REF_INITIAL = 0; + constexpr static int32_t REF_NORMAL = 1; + std::shared_ptr dHFwkKit_; + std::mutex dHFwkKitMutex_; + std::atomic refCount_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DCAMERA_LOW_LATENCY_H \ No newline at end of file diff --git a/services/channel/src/dcamera_low_latency.cpp b/services/channel/src/dcamera_low_latency.cpp new file mode 100644 index 00000000..8a356556 --- /dev/null +++ b/services/channel/src/dcamera_low_latency.cpp @@ -0,0 +1,87 @@ +/* + * 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 "constants.h" +#include "dcamera_low_latency.h" +#include "device_type.h" +#include "distributed_camera_errno.h" +#include "distributed_hardware_log.h" +#include "ipublisher_listener.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DCameraLowLatency); + +int32_t DCameraLowLatency::EnableLowLatency() +{ + DHLOGD("Enable low latency start."); + if (refCount_ > REF_INITIAL) { + refCount_++; + DHLOGD("No need to enable low latency, refCount just plus one and now is: %d.", refCount_.load()); + return DCAMERA_OK; + } + std::shared_ptr dHFwkKit = GetDHFwkKit(); + if (dHFwkKit == nullptr) { + DHLOGE("Get dHFwkKit is null when enable low latency."); + return DCAMERA_BAD_VALUE; + } + nlohmann::json enable = { + {DH_TYPE, DHType::CAMERA}, + {LOW_LATENCY_ENABLE, true}, + }; + dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, enable.dump()); + refCount_++; + DHLOGD("Enable low latency success and now refCount is: %d", refCount_.load()); + return DCAMERA_OK; +} + +int32_t DCameraLowLatency::DisableLowLatency() +{ + DHLOGD("Disable low latency start."); + if (refCount_ == REF_INITIAL) { + DHLOGD("No need to disable low latency, refCount is zero."); + return DCAMERA_OK; + } + if (refCount_ > REF_NORMAL) { + refCount_--; + DHLOGD("No need to disable low latency, refCount just minus one and now is: %d.", refCount_.load()); + return DCAMERA_OK; + } + std::shared_ptr dHFwkKit = GetDHFwkKit(); + if (dHFwkKit == nullptr) { + DHLOGE("Get dHFwkKit is null when disable low latency."); + return DCAMERA_BAD_VALUE; + } + nlohmann::json disable = { + {DH_TYPE, DHType::CAMERA}, + {LOW_LATENCY_ENABLE, false}, + }; + dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, disable.dump()); + refCount_--; + DHLOGD("Disable low latency success."); + return DCAMERA_OK; +} + +std::shared_ptr DCameraLowLatency::GetDHFwkKit() +{ + std::lock_guard lock(dHFwkKitMutex_); + if (dHFwkKit_ == nullptr) { + dHFwkKit_ = std::make_shared(); + } + return dHFwkKit_; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file -- Gitee