diff --git a/common/include/dinput_errcode.h b/common/include/dinput_errcode.h index d353981..64905e6 100644 --- a/common/include/dinput_errcode.h +++ b/common/include/dinput_errcode.h @@ -32,6 +32,9 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_HUB_GET_EVENT_FAIL = -60006; constexpr int32_t ERR_DH_INPUT_HUB_DEVICE_ENABLE_FAIL = -60007; constexpr int32_t ERR_DH_INPUT_HUB_IS_VIRTUAL_DEVICE = -60008; + constexpr int32_t ERR_DH_INPUT_DLOPEN_FAIL = -60009; + constexpr int32_t ERR_DH_INPUT_DLSYM_FAIL = -60010; + constexpr int32_t ERR_DH_INPUT_LOW_LATENCY_LIB_NULL = -60011; // whilte list error code constexpr int32_t ERR_DH_INPUT_WHILTELIST_INIT_FAIL = -61001; constexpr int32_t ERR_DH_INPUT_WHILTELIST_GET_WHILTELIST_FAIL = -61002; diff --git a/common/include/dinput_sa_process_state.cpp b/common/include/dinput_sa_process_state.cpp index abb4949..9ab7a71 100644 --- a/common/include/dinput_sa_process_state.cpp +++ b/common/include/dinput_sa_process_state.cpp @@ -20,6 +20,7 @@ #include "distributed_hardware_log.h" #include "constants_dinput.h" +#include "dinput_low_latency_utils.h" #include "hisysevent_util.h" namespace OHOS { @@ -45,6 +46,7 @@ void SetSinkProcessExit() } DHLOGI("exit sa process success."); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_EXIT, "dinput sink sa exit success."); + DInputLowLatencyUtils::GetInstance().DisableSinkLowLatency(); exit(0); } @@ -59,6 +61,7 @@ void SetSourceProcessExit() } DHLOGI("exit sa process success."); HisyseventUtil::GetInstance().SysEventWriteBehavior(DINPUT_EXIT, "dinput source sa exit success."); + DInputLowLatencyUtils::GetInstance().DisableSourceLowLatency(); exit(0); } } // namespace DistributedInput diff --git a/services/sink/transport/BUILD.gn b/services/sink/transport/BUILD.gn index c569c1d..fe57e71 100644 --- a/services/sink/transport/BUILD.gn +++ b/services/sink/transport/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("libdinput_sink_trans") { "//foundation/communication/dsoftbus/interfaces/kits/transport", "${service_common}/include", "${dfx_utils_path}/include", + "${utils_path}/include", ] sources = [ @@ -48,6 +49,7 @@ ohos_shared_library("libdinput_sink_trans") { "${fwk_utils_path}:distributedhardwareutils", "//foundation/communication/dsoftbus/sdk:softbus_client", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index 3b2e81f..71439ec 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -25,6 +25,7 @@ #include "constants_dinput.h" #include "dinput_errcode.h" +#include "dinput_low_latency_utils.h" #include "hidumper.h" #include "session.h" #include "softbus_bus_center.h" @@ -255,6 +256,8 @@ int32_t DistributedInputSinkTransport::OnSessionOpened(int32_t sessionId, int32_ return DH_SUCCESS; } + DInputLowLatencyUtils::GetInstance().EnableSinkLowLatency(); + // return 1 is client int32_t sessionSide = GetSessionSide(sessionId); DHLOGI("session open succeed, sessionId %s, sessionSide %d", GetAnonyInt32(sessionId).c_str(), sessionSide); @@ -287,6 +290,8 @@ void DistributedInputSinkTransport::OnSessionClosed(int32_t sessionId) { DHLOGI("OnSessionClosed, sessionId:%s", GetAnonyInt32(sessionId).c_str()); + DInputLowLatencyUtils::GetInstance().DisableSinkLowLatency(); + char peerDevId[DEVICE_ID_SIZE_MAX] = ""; int ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); if (ret != DH_SUCCESS) { diff --git a/services/sink/transport/test/sinktransunittest/BUILD.gn b/services/sink/transport/test/sinktransunittest/BUILD.gn index b0020fb..9587628 100644 --- a/services/sink/transport/test/sinktransunittest/BUILD.gn +++ b/services/sink/transport/test/sinktransunittest/BUILD.gn @@ -39,6 +39,7 @@ ohos_unittest("distributed_input_sinktrans_test") { "//third_party/json/include", "//foundation/distributedhardware/distributed_input/services/sink/transport/test/sinktransunittest/mock", "${dfx_utils_path}/include", + "${utils_path}/include", ] sources = [ @@ -67,6 +68,7 @@ ohos_unittest("distributed_input_sinktrans_test") { "${fwk_utils_path}:distributedhardwareutils", "//foundation/communication/dsoftbus/sdk:softbus_client", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn index be7e46a..4d37271 100644 --- a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn +++ b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn @@ -47,6 +47,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { "${services_source_path}/transport/include", "${dfx_utils_path}/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", + "${utils_path}/include", ] sources = [ @@ -106,6 +107,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { "//foundation/communication/dsoftbus/sdk:softbus_client", "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/services/source/transport/BUILD.gn b/services/source/transport/BUILD.gn index 58faf9e..2cbc97c 100644 --- a/services/source/transport/BUILD.gn +++ b/services/source/transport/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("libdinput_source_trans") { "${services_source_path}/inputinject/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "${dfx_utils_path}/include", + "${utils_path}/include", ] sources = [ @@ -51,6 +52,7 @@ ohos_shared_library("libdinput_source_trans") { "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index 63c3d4e..b7fe385 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -27,6 +27,7 @@ #include "constants_dinput.h" #include "dinput_errcode.h" #include "dinput_hitrace.h" +#include "dinput_low_latency_utils.h" #include "dinput_softbus_define.h" #include "distributed_input_inject.h" #include "hidumper.h" @@ -184,6 +185,7 @@ int32_t DistributedInputSourceTransport::OpenInputSoftbus(const std::string &rem DHLOGI("OpenSession success, remoteDevId:%s, sessionId:%s", GetAnonyString(remoteDevId).c_str(), GetAnonyInt32(sessionId).c_str()); + DInputLowLatencyUtils::GetInstance().EnableSourceLowLatency(); HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::OPENED); return DH_SUCCESS; } @@ -205,6 +207,7 @@ void DistributedInputSourceTransport::CloseInputSoftbus(const std::string &remot sessionDevMap_.erase(remoteDevId); channelStatusMap_.erase(remoteDevId); DistributedInputInject::GetInstance().StopInjectThread(); + DInputLowLatencyUtils::GetInstance().DisableSourceLowLatency(); HiDumper::GetInstance().SetSessionStatus(remoteDevId, SessionStatus::CLOSED); HiDumper::GetInstance().DeleteSessionInfo(remoteDevId); } diff --git a/services/source/transport/test/sourcetransunittest/BUILD.gn b/services/source/transport/test/sourcetransunittest/BUILD.gn index f0d73cf..c677372 100644 --- a/services/source/transport/test/sourcetransunittest/BUILD.gn +++ b/services/source/transport/test/sourcetransunittest/BUILD.gn @@ -41,6 +41,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { "//foundation/communication/dsoftbus/interfaces/kits/transport", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "${dfx_utils_path}/include", + "${utils_path}/include", ] sources = [ @@ -70,6 +71,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", "//foundation/communication/dsoftbus/sdk:softbus_client", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn b/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn index 6d1e52a..a8ed0f1 100644 --- a/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputsinktransport_fuzzer/BUILD.gn @@ -33,6 +33,7 @@ ohos_fuzztest("DistributedInputSinkTransportFuzzTest") { "${service_common}/include", "//third_party/json/include", "${dfx_utils_path}/include", + "${utils_path}/include", ] cflags = [ @@ -53,6 +54,7 @@ ohos_fuzztest("DistributedInputSinkTransportFuzzTest") { "${fwk_utils_path}:distributedhardwareutils", "//foundation/communication/dsoftbus/sdk:softbus_client", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn index 066c156..00418e0 100644 --- a/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn +++ b/test/fuzztest/distributedinputsourcetransport_fuzzer/BUILD.gn @@ -36,6 +36,7 @@ ohos_fuzztest("DistributedInputSourceTransportFuzzTest") { "${services_source_path}/transport/include", "//third_party/json/include", "${dfx_utils_path}/include", + "${utils_path}/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", ] @@ -59,6 +60,7 @@ ohos_fuzztest("DistributedInputSourceTransportFuzzTest") { "//foundation/distributedhardware/distributed_input/services/source/transport:libdinput_source_trans", "//foundation/distributedhardware/distributed_input/services/source/inputinject:libdinput_inject", "${dfx_utils_path}:libdinput_dfx_utils", + "${utils_path}:libdinput_utils", ] external_deps = [ diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 66162fd..a4ccf5f 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -16,6 +16,7 @@ import("//foundation/distributedhardware/distributed_input/distributedinput.gni" ohos_shared_library("libdinput_utils") { include_dirs = [ + "${fwk_common_path}/utils/include", "${fwk_common_path}/log/include", "${fwk_utils_path}/include", "${fwk_utils_path}/include/log", @@ -25,6 +26,7 @@ ohos_shared_library("libdinput_utils") { sources = [ "src/dinput_utils_tool.cpp", + "src/dinput_low_latency_utils.cpp", ] defines = [ diff --git a/utils/include/dinput_low_latency_utils.h b/utils/include/dinput_low_latency_utils.h new file mode 100644 index 0000000..9e92248 --- /dev/null +++ b/utils/include/dinput_low_latency_utils.h @@ -0,0 +1,48 @@ +/* + * 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 DINPUT_LOW_LATENCY_UTILS_H +#define DINPUT_LOW_LATENCY_UTILS_H + +#include +#include + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DInputLowLatencyUtils { +DECLARE_SINGLE_INSTANCE_BASE(DInputLowLatencyUtils); +public: + void EnableSourceLowLatency(); + void DisableSourceLowLatency(); + void EnableSinkLowLatency(); + void DisableSinkLowLatency(); +private: + DInputLowLatencyUtils() = default; + ~DInputLowLatencyUtils(); + int32_t LoadLibrary(); + void CloseLibrary(); + +private: + void *handler_ { nullptr }; + std::mutex mutex_; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS + +#endif // DINPUT_LOW_LATENCY_UTILS_H \ No newline at end of file diff --git a/utils/src/dinput_low_latency_utils.cpp b/utils/src/dinput_low_latency_utils.cpp new file mode 100644 index 0000000..31d5997 --- /dev/null +++ b/utils/src/dinput_low_latency_utils.cpp @@ -0,0 +1,136 @@ +/* + * 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 "dinput_low_latency_utils.h" + +#include +#include +#include + +#include "distributed_hardware_log.h" + +#include "dinput_errcode.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +IMPLEMENT_SINGLE_INSTANCE(DInputLowLatencyUtils); +const std::string LIB_NAME = "libdinput_low_latency.z.so"; +const std::string FUNC_GET_ENABLE_SOURCE = "EnableSourceLowLatency"; +const std::string FUNC_GET_DISABLE_SOURCE = "DisableSourceLowLatency"; +const std::string FUNC_GET_ENABLE_SINK = "EnableSinkLowLatency"; +const std::string FUNC_GET_DISABLE_SINK = "DisableSinkLowLatency"; + +using GetLowLatencyFunc = void *(*)(); + +DInputLowLatencyUtils::~DInputLowLatencyUtils() +{ + CloseLibrary(); +} + +void DInputLowLatencyUtils::EnableSourceLowLatency() +{ + std::lock_guard lock(mutex_); + if (LoadLibrary() != DH_SUCCESS) { + DHLOGE("load library failed"); + return; + } + auto enableSourceLowLatency = reinterpret_cast(dlsym(handler_, FUNC_GET_ENABLE_SOURCE.c_str())); + if (enableSourceLowLatency == nullptr) { + DHLOGE("can not find %s, failed reason : %s", FUNC_GET_ENABLE_SOURCE.c_str(), dlerror()); + return; + } + + enableSourceLowLatency(); +} + +void DInputLowLatencyUtils::DisableSourceLowLatency() +{ + std::lock_guard lock(mutex_); + if (LoadLibrary() != DH_SUCCESS) { + DHLOGE("load library failed"); + return; + } + auto disableSourceLowLatency = + reinterpret_cast(dlsym(handler_, FUNC_GET_DISABLE_SOURCE.c_str())); + if (disableSourceLowLatency == nullptr) { + DHLOGE("can not find %s, failed reason : %s", FUNC_GET_DISABLE_SOURCE.c_str(), dlerror()); + return; + } + + disableSourceLowLatency(); +} + +void DInputLowLatencyUtils::EnableSinkLowLatency() +{ + std::lock_guard lock(mutex_); + if (LoadLibrary() != DH_SUCCESS) { + DHLOGE("load library failed"); + return; + } + auto enableSinkLowLatency = reinterpret_cast(dlsym(handler_, FUNC_GET_ENABLE_SINK.c_str())); + if (enableSinkLowLatency == nullptr) { + DHLOGE("can not find %s, failed reason : %s", FUNC_GET_ENABLE_SINK.c_str(), dlerror()); + return; + } + + enableSinkLowLatency(); +} + +void DInputLowLatencyUtils::DisableSinkLowLatency() +{ + std::lock_guard lock(mutex_); + if (LoadLibrary() != DH_SUCCESS) { + DHLOGE("load library failed"); + return; + } + auto disableSinkLowLatency = reinterpret_cast(dlsym(handler_, FUNC_GET_DISABLE_SINK.c_str())); + if (disableSinkLowLatency == nullptr) { + DHLOGE("can not find %s, failed reason : %s", FUNC_GET_DISABLE_SINK.c_str(), dlerror()); + return; + } + + disableSinkLowLatency(); +} + +int32_t DInputLowLatencyUtils::LoadLibrary() +{ + DHLOGI("start."); + if (handler_ != nullptr) { + DHLOGE("DInputLowLatencyUtils handler has loaded."); + return DH_SUCCESS; + } + + handler_ = dlopen(LIB_NAME.c_str(), RTLD_NOW | RTLD_NODELETE); + if (handler_ == nullptr) { + DHLOGE("open %s failed, fail reason : %s", LIB_NAME.c_str(), dlerror()); + return ERR_DH_INPUT_DLOPEN_FAIL; + } + return DH_SUCCESS; +} + +void DInputLowLatencyUtils::CloseLibrary() +{ + if (handler_ == nullptr) { + DHLOGI("%s is already closed.", LIB_NAME.c_str()); + return; + } + dlclose(handler_); + handler_ = nullptr; + DHLOGI("%s is closed.", LIB_NAME.c_str()); +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file