From ba7859e4affbaf9f648a0777d926b7a5567fea8f Mon Sep 17 00:00:00 2001 From: txl Date: Wed, 17 Jan 2024 12:48:10 +0800 Subject: [PATCH 01/12] googletest_1.13.0 Signed-off-by: txl --- utils/test/unittest/common/eventbus/event_bus_test.cpp | 1 - utils/test/unittest/common/eventbus/event_bus_test.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/test/unittest/common/eventbus/event_bus_test.cpp b/utils/test/unittest/common/eventbus/event_bus_test.cpp index 400eafef..78b2b81c 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.cpp +++ b/utils/test/unittest/common/eventbus/event_bus_test.cpp @@ -15,7 +15,6 @@ #include "event_bus_test.h" -#include #include #include #include diff --git a/utils/test/unittest/common/eventbus/event_bus_test.h b/utils/test/unittest/common/eventbus/event_bus_test.h index 3d953e75..e3be69e1 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.h +++ b/utils/test/unittest/common/eventbus/event_bus_test.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H #include +#include #include #include -- Gitee From a915c42fc775072ce04bee30265e41ca9a49af31 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Fri, 26 Jan 2024 08:51:03 +0000 Subject: [PATCH 02/12] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!4?= =?UTF-8?q?89=20:=20distributedhardware=5Fdistributed=5Fhardware=5Ffwk?= =?UTF-8?q?=E4=BB=93=E9=80=82=E9=85=8Dgoogletest1.13.0'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/test/unittest/common/eventbus/event_bus_test.cpp | 1 + utils/test/unittest/common/eventbus/event_bus_test.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/test/unittest/common/eventbus/event_bus_test.cpp b/utils/test/unittest/common/eventbus/event_bus_test.cpp index 78b2b81c..400eafef 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.cpp +++ b/utils/test/unittest/common/eventbus/event_bus_test.cpp @@ -15,6 +15,7 @@ #include "event_bus_test.h" +#include #include #include #include diff --git a/utils/test/unittest/common/eventbus/event_bus_test.h b/utils/test/unittest/common/eventbus/event_bus_test.h index e3be69e1..3d953e75 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.h +++ b/utils/test/unittest/common/eventbus/event_bus_test.h @@ -17,7 +17,6 @@ #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H #include -#include #include #include -- Gitee From e29b1321e6f1ab6d6649da409825ec64f449aa36 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 27 Jan 2024 19:01:37 +0800 Subject: [PATCH 03/12] modify hot plugin trigger system awake problem Signed-off-by: hwzhangchuang --- bundle.json | 3 +- .../distributedhardwarefwkservice/BUILD.gn | 1 + .../include/utils/dh_context.h | 20 ++++++++- .../plugin_listener_impl.cpp | 8 ++++ .../src/utils/dh_context.cpp | 41 +++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/bundle.json b/bundle.json index 2bcebbe9..034171df 100644 --- a/bundle.json +++ b/bundle.json @@ -50,7 +50,8 @@ "graphic_2d", "graphic_surface", "media_foundation", - "bounds_checking_function" + "bounds_checking_function", + "power_manager" ], "third_party": [ "json", diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 117a506a..8074d244 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -130,6 +130,7 @@ ohos_shared_library("distributedhardwarefwksvr") { "init:libbegetutil", "ipc:ipc_core", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "resource_schedule_service:ressched_client", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/distributedhardwarefwkservice/include/utils/dh_context.h b/services/distributedhardwarefwkservice/include/utils/dh_context.h index a62afa03..c7869ea0 100644 --- a/services/distributedhardwarefwkservice/include/utils/dh_context.h +++ b/services/distributedhardwarefwkservice/include/utils/dh_context.h @@ -16,11 +16,15 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_DHCONTEXT_H #define OHOS_DISTRIBUTED_HARDWARE_DHCONTEXT_H +#include #include #include #include #include +#include "power_mgr_client.h" +#include "power_state_callback_stub.h" + #include "device_type.h" #include "event_handler.h" #include "single_instance.h" @@ -55,18 +59,30 @@ public: }; std::shared_ptr GetEventHandler(); + bool IsSleeping(); + void SetIsSleeping(bool isSleeping); + +private: + class DHFWKPowerStateCallback : public OHOS::PowerMgr::PowerStateCallbackStub { + public: + void OnPowerStateChanged(OHOS::PowerMgr::PowerState state) override; + }; + void RegisterPowerStateLinstener(); + private: DeviceInfo devInfo_ { "", "", "", 0 }; std::mutex devMutex_; /* Save online device uuid and networkId */ - std::unordered_map onlineDeviceMap_; + std::unordered_map onlineDeviceMap_ = {}; /* Save online device hashed uuid and uuid */ - std::unordered_map deviceIdUUIDMap_; + std::unordered_map deviceIdUUIDMap_ = {}; std::shared_mutex onlineDevMutex_; std::shared_ptr eventHandler_; + /* true for system in sleeping, false for NOT in sleeping */ + std::atomic isSleeping_ = false; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp index fe50486f..5424af28 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp @@ -36,6 +36,10 @@ void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::stri return; } DHLOGI("plugin start, dhId: %s", GetAnonyString(dhId).c_str()); + if (DHContext::GetInstance().IsSleeping()) { + DHLOGI("System is in sleeping, drop it"); + return; + } std::vector> capabilityInfos; std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName; @@ -56,6 +60,10 @@ void PluginListenerImpl::UnPluginHardware(const std::string &dhId) return; } DHLOGI("unplugin start, dhId: %s", GetAnonyString(dhId).c_str()); + if (DHContext::GetInstance().IsSleeping()) { + DHLOGI("System is in sleeping, drop it"); + return; + } std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; std::shared_ptr capability = nullptr; auto ret = CapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capability); diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 57c066c9..0deec491 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -30,6 +30,7 @@ DHContext::DHContext() DHLOGI("Ctor DHContext"); std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); eventHandler_ = std::make_shared(runner); + RegisterPowerStateLinstener(); } DHContext::~DHContext() @@ -37,6 +38,46 @@ DHContext::~DHContext() DHLOGI("Dtor DHContext"); } +void DHContext::RegisterPowerStateLinstener() +{ + sptr powerStateCallback_(new DHFWKPowerStateCallback()); + if (powerStateCallback_ == nullptr) { + DHLOGE("DHFWK subscribe create power state callback Create Error"); + return; + } + + bool ret = PowerMgr::PowerMgrClient::GetInstance().RegisterPowerStateCallback(powerStateCallback_); + if (!ret) { + DHLOGE("DHFWK register power state callback failed"); + } else { + DHLOGE("DHFWK register power state callback success"); + } +} + +void DHContext::DHFWKPowerStateCallback::OnPowerStateChanged(PowerMgr::PowerState state) +{ + DHLOGI("DHFWK OnPowerStateChanged state: %u", static_cast(state)); + if (state == PowerMgr::PowerState::SLEEP || state == PowerMgr::PowerState::HIBERNATE || + state == PowerMgr::PowerState::SHUTDOWN) { + DHLOGI("DHFWK set in sleeping"); + DHContext::GetInstance().SetIsSleeping(true); + return; + } + + DHLOGI("DHFWK set NOT in sleeping"); + DHContext::GetInstance().SetIsSleeping(false); +} + +bool DHContext::IsSleeping() +{ + return isSleeping_; +} + +void DHContext::SetIsSleeping(bool isSleeping) +{ + isSleeping_ = isSleeping; +} + DHContext::CommonEventHandler::CommonEventHandler(const std::shared_ptr &runner) : AppExecFwk::EventHandler(runner) { -- Gitee From 5cf3ea14ad02dcca648697f285021f3c15748a69 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 27 Jan 2024 21:36:21 +0800 Subject: [PATCH 04/12] modify Signed-off-by: hwzhangchuang --- .../test/unittest/common/accessmanager/BUILD.gn | 1 + .../test/unittest/common/componentloader/BUILD.gn | 1 + .../unittest/common/componentmanager/component_manager/BUILD.gn | 1 + .../test/unittest/common/dbadapter/BUILD.gn | 1 + .../test/unittest/common/distributedhardwaremanager/BUILD.gn | 1 + .../test/unittest/common/distributedhardwareservice/BUILD.gn | 1 + .../common/localhardwaremanager/localhardwaremanager/BUILD.gn | 1 + .../test/unittest/common/resourcemanager/BUILD.gn | 1 + .../test/unittest/common/versioninfomanager/versioninfo/BUILD.gn | 1 + .../common/versioninfomanager/versioninfomanager/BUILD.gn | 1 + utils/test/unittest/common/eventtrance/BUILD.gn | 1 + 11 files changed, 11 insertions(+) diff --git a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn index 490c99d5..dcbc89f5 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn @@ -61,6 +61,7 @@ ohos_unittest("AccessManagerTest") { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", + "power_manager:powermgr_client", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn index 35a03a68..193efc05 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn @@ -54,6 +54,7 @@ ohos_unittest("ComponentLoaderTest") { "config_policy:configpolicy_util", "hitrace:hitrace_meter", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", ] defines = [ diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn index afc1fb71..25f16a77 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn @@ -67,6 +67,7 @@ ohos_unittest("ComponentManagerTest") { "dsoftbus:softbus_client", "eventhandler:libeventhandler", "ipc:ipc_single", + "power_manager:powermgr_client", "kv_store:distributeddata_inner", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn index 2e404e16..370032e4 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn @@ -61,6 +61,7 @@ ohos_unittest("DbAdapterTest") { "c_utils:utils", "eventhandler:libeventhandler", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn index caa0a7b9..9017a515 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn @@ -49,6 +49,7 @@ ohos_unittest("DistributedHardwareManagerTest") { external_deps = [ "c_utils:utils", "eventhandler:libeventhandler", + "power_manager:powermgr_client", ] defines = [ diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn index 432b0905..b1ffab8b 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn @@ -58,6 +58,7 @@ ohos_unittest("DistributedHardwareServiceTest") { "c_utils:utils", "eventhandler:libeventhandler", "ipc:ipc_core", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/BUILD.gn index aa5f9773..7905c5e8 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/localhardwaremanager/BUILD.gn @@ -57,6 +57,7 @@ ohos_unittest("LocalHardwareManagerTest") { "eventhandler:libeventhandler", "hisysevent:libhisysevent", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", ] defines = [ diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn index 18d2e280..15157c63 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn @@ -51,6 +51,7 @@ ohos_unittest("ResourceManagerTest") { "c_utils:utils", "eventhandler:libeventhandler", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn index e4b51703..ac50f7da 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfo/BUILD.gn @@ -51,6 +51,7 @@ ohos_unittest("VersionInfoTest") { "c_utils:utils", "eventhandler:libeventhandler", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn index 38a9c45d..d9cfa77b 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/BUILD.gn @@ -52,6 +52,7 @@ ohos_unittest("VersioninfoManagerTest") { "c_utils:utils", "eventhandler:libeventhandler", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } diff --git a/utils/test/unittest/common/eventtrance/BUILD.gn b/utils/test/unittest/common/eventtrance/BUILD.gn index 4ff0134b..e240e684 100644 --- a/utils/test/unittest/common/eventtrance/BUILD.gn +++ b/utils/test/unittest/common/eventtrance/BUILD.gn @@ -51,6 +51,7 @@ ohos_unittest("EventTranceTest") { "c_utils:utils", "hisysevent:libhisysevent", "hitrace:hitrace_meter", + "power_manager:powermgr_client", "safwk:system_ability_fwk", ] } -- Gitee From 0af67a45b97b418a3eede2fd5e39c26248daed29 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 27 Jan 2024 22:34:38 +0800 Subject: [PATCH 05/12] modify Signed-off-by: hwzhangchuang --- .../test/fuzztest/resourcemanager_fuzzer/BUILD.gn | 1 + .../unittest/common/componentmanager/component_manager/BUILD.gn | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn index 9acd72e5..1032e672 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn @@ -56,6 +56,7 @@ ohos_fuzztest("ResourcemanagerFuzzTest") { "eventhandler:libeventhandler", "ipc:ipc_single", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn index 25f16a77..5c83232c 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn @@ -67,8 +67,8 @@ ohos_unittest("ComponentManagerTest") { "dsoftbus:softbus_client", "eventhandler:libeventhandler", "ipc:ipc_single", - "power_manager:powermgr_client", "kv_store:distributeddata_inner", + "power_manager:powermgr_client", ] defines = [ -- Gitee From 3a2f1bd055b128fdcf9f6f1fe1ce75a0af129a05 Mon Sep 17 00:00:00 2001 From: s30051537 Date: Thu, 25 Jan 2024 17:42:20 +0800 Subject: [PATCH 06/12] =?UTF-8?q?SA=E5=B4=A9=E6=BA=83=E5=90=8E=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E4=B8=9A=E5=8A=A1=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: s30051537 --- .../include/distributed_hardware_fwk_kit.h | 2 - .../inner_kits/include/ipc/dhfwk_sa_manager.h | 10 ++++ .../src/distributed_hardware_fwk_kit.cpp | 51 +++++++------------ .../inner_kits/src/ipc/dhfwk_sa_manager.cpp | 40 +++++++++++++++ 4 files changed, 69 insertions(+), 34 deletions(-) diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h index 1e22a572..e4d34565 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -173,8 +173,6 @@ private: bool IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec); private: - std::mutex listenerMutex_; - std::unordered_map>> listenerMap_; std::atomic isDHFWKOnLine_; }; } // namespace DistributedHardware diff --git a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h index ef35c38a..9dce2eaa 100644 --- a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h +++ b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "refbase.h" #include "system_ability_status_change_stub.h" @@ -46,6 +47,15 @@ public: void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; }; +private: + static int32_t RestoreListener(); + +public: + static std::mutex publisherListenersMutex_; + static std::unordered_map>> publisherListenersCahce_; + static std::mutex avTransControlCenterCbMutex_; + static std::unordered_map> avTransControlCenterCbCache_; + private: std::atomic dhfwkOnLine_; std::atomic isSubscribeDHFWKSAChangeListener; diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 97ea5d58..8f9d0d11 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -26,7 +26,8 @@ namespace OHOS { namespace DistributedHardware { -DistributedHardwareFwkKit::DistributedHardwareFwkKit() : listenerMap_({}), isDHFWKOnLine_(false) + +DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false) { DHLOGI("Ctor DistributedHardwareFwkKit"); DHFWKSAManager::GetInstance().RegisterSAStateCallback( @@ -49,19 +50,27 @@ int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic } int32_t ret = DH_FWK_SUCCESS; + std::lock_guard publisherListenersLock(DHFWKSAManager::publisherListenersMutex_); if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret); + if (DHFWKSAManager::publisherListenersCahce_.size() >= MAX_TOPIC_SIZE || + DHFWKSAManager::publisherListenersCahce_[topic].size() >= MAX_LISTENER_SIZE) { + DHLOGE("listeners are over size!"); + return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; + } + if (ret == DH_FWK_SUCCESS) { + DHFWKSAManager::publisherListenersCahce_[topic].insert(listener); + } } else { DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); - std::lock_guard lock(listenerMutex_); - if (listenerMap_.size() >= MAX_TOPIC_SIZE || listenerMap_[topic].size() >= MAX_LISTENER_SIZE) { + if (DHFWKSAManager::publisherListenersCahce_.size() >= MAX_TOPIC_SIZE || + DHFWKSAManager::publisherListenersCahce_[topic].size() >= MAX_LISTENER_SIZE) { DHLOGE("listeners are over size!"); return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; } - listenerMap_[topic].insert(listener); + DHFWKSAManager::publisherListenersCahce_[topic].insert(listener); } - return ret; } @@ -79,9 +88,8 @@ int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic top ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret); } - std::lock_guard lock(listenerMutex_); - listenerMap_[topic].erase(listener); - + std::lock_guard publisherListenersLock(DHFWKSAManager::publisherListenersMutex_); + DHFWKSAManager::publisherListenersCahce_[topic].erase(listener); return ret; } @@ -117,29 +125,6 @@ void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine) { DHLOGI("Receive DHFWK online callback, %s", (isOnLine ? "true" : "false")); isDHFWKOnLine_ = isOnLine; - - if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { - return; - } - - DHLOGI("DHFWK online, register saved listener to it"); - std::unordered_map>> regSuccListeners; - std::lock_guard lock(listenerMutex_); - for (const auto &entry : listenerMap_) { - for (const auto &listener : entry.second) { - int32_t ret = - DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); - if (ret == DH_FWK_SUCCESS) { - regSuccListeners[entry.first].insert(listener); - } - } - } - - for (const auto &succ : regSuccListeners) { - for (const auto &listener : succ.second) { - listenerMap_[succ.first].erase(listener); - } - } } bool DistributedHardwareFwkKit::IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec) @@ -221,7 +206,8 @@ int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId, DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); return ERR_DH_FWK_POINTER_IS_NULL; } - + std::lock_guard avTransControlCenterCbLock(DHFWKSAManager::avTransControlCenterCbMutex_); + DHFWKSAManager::avTransControlCenterCbCache_[engineId] = callback; return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback); } @@ -260,5 +246,6 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); } + } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp index 556d566f..ee75ea8c 100644 --- a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -22,9 +22,16 @@ #include "distributed_hardware_log.h" #include "distributed_hardware_proxy.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_fwk_kit.h" namespace OHOS { namespace DistributedHardware { +std::mutex DHFWKSAManager::publisherListenersMutex_; +std::unordered_map>> DHFWKSAManager::publisherListenersCahce_; +std::mutex DHFWKSAManager::avTransControlCenterCbMutex_; +std::unordered_map> DHFWKSAManager::avTransControlCenterCbCache_; + IMPLEMENT_SINGLE_INSTANCE(DHFWKSAManager); DHFWKSAManager::DHFWKSAManager() : dhfwkOnLine_(false), isSubscribeDHFWKSAChangeListener(false), dhfwkProxy_(nullptr), @@ -112,6 +119,9 @@ void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbi DHFWKSAManager::GetInstance().saStateCallback(true); } } + if (RestoreListener() != DH_FWK_SUCCESS) { + DHLOGE("Partial listeners failed to restore"); + } DHLOGI("sa %" PRId32 " started", systemAbilityId); } @@ -136,5 +146,35 @@ void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t system } DHLOGI("sa %" PRId32 " stopped", systemAbilityId); } + +int32_t DHFWKSAManager::RestoreListener() +{ + DHLOGI("Restore the failed listeners due to sa crash"); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + return ERR_DH_FWK_POINTER_IS_NULL; + } + int32_t ret = DH_FWK_SUCCESS; + std::lock_guard publisherListenersLock(publisherListenersMutex_); + for (const auto &entry : publisherListenersCahce_) { + for (const auto &listener : entry.second) { + int32_t innerRet = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); + if (ret != DH_FWK_SUCCESS) { + ret = innerRet; + DHLOGE("Register publisher listener failed, topic: %" PRIu32, (uint32_t)entry.first); + } + } + } + std::lock_guard avTransControlCenterCbLock(avTransControlCenterCbMutex_); + for (auto &entry : avTransControlCenterCbCache_) { + int32_t innerRet = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(entry.first, entry.second); + if (innerRet != DH_FWK_SUCCESS) { + ret = innerRet; + DHLOGE("Restore register av control center callback failed, engineId: %" PRId32, entry.first); + } + } + return ret; +} } // DistributedHardware } // OHOS \ No newline at end of file -- Gitee From 82214c3399b9c4b97eaadeecf2cfb25bd1f0e237 Mon Sep 17 00:00:00 2001 From: s30051537 Date: Mon, 29 Jan 2024 11:49:07 +0800 Subject: [PATCH 07/12] =?UTF-8?q?SA=E5=B4=A9=E6=BA=83=E5=90=8E=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E4=B8=9A=E5=8A=A1=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: s30051537 --- .../dscreen_input_test/dscreen_input_test.cpp | 1 - .../inner_kits/include/ipc/dhfwk_sa_manager.h | 17 +++-- .../src/distributed_hardware_fwk_kit.cpp | 28 +++----- .../inner_kits/src/ipc/dhfwk_sa_manager.cpp | 71 +++++++++++++------ 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/av_transport/av_trans_engine/plugin/test/av_trans_input/dscreen_input_test/dscreen_input_test.cpp b/av_transport/av_trans_engine/plugin/test/av_trans_input/dscreen_input_test/dscreen_input_test.cpp index 63105586..79b41272 100644 --- a/av_transport/av_trans_engine/plugin/test/av_trans_input/dscreen_input_test/dscreen_input_test.cpp +++ b/av_transport/av_trans_engine/plugin/test/av_trans_input/dscreen_input_test/dscreen_input_test.cpp @@ -67,6 +67,5 @@ HWTEST_F(DscreenInputTest, PushData_001, TestSize.Level0) ret = plugin->PushData("", buffer, 0); EXPECT_EQ(Status::OK, ret); } - } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h index 9dce2eaa..ecf21521 100644 --- a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h +++ b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h @@ -40,6 +40,11 @@ public: sptr GetDHFWKProxy(); void RegisterSAStateCallback(DHFWKSAStateCb callback); + int32_t AddPublisherListenerToCache(const DHTopic topic, sptr listener); + void RemovePublisherListenerFromCache(const DHTopic topic, sptr listener); + void AddAVTransControlCenterCbToCache(int32_t engineId, const sptr callback); + void RemoveAVTransControlCenterCbFromCache(int32_t engineId); + public: class SystemAbilityListener : public SystemAbilityStatusChangeStub { public: @@ -48,13 +53,7 @@ public: }; private: - static int32_t RestoreListener(); - -public: - static std::mutex publisherListenersMutex_; - static std::unordered_map>> publisherListenersCahce_; - static std::mutex avTransControlCenterCbMutex_; - static std::unordered_map> avTransControlCenterCbCache_; + int32_t RestoreListener(); private: std::atomic dhfwkOnLine_; @@ -64,6 +63,10 @@ private: sptr saListener_; std::mutex saStatCbMutex_; DHFWKSAStateCb saStateCallback; + std::mutex publisherListenersMutex_; + std::unordered_map>> publisherListenersCache_; + std::mutex avTransControlCenterCbMutex_; + std::unordered_map> avTransControlCenterCbCache_; }; } // DistributedHardware } // OHOS diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 8f9d0d11..0ff888a2 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -26,7 +26,6 @@ namespace OHOS { namespace DistributedHardware { - DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false) { DHLOGI("Ctor DistributedHardwareFwkKit"); @@ -50,27 +49,17 @@ int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic } int32_t ret = DH_FWK_SUCCESS; - std::lock_guard publisherListenersLock(DHFWKSAManager::publisherListenersMutex_); if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret); - if (DHFWKSAManager::publisherListenersCahce_.size() >= MAX_TOPIC_SIZE || - DHFWKSAManager::publisherListenersCahce_[topic].size() >= MAX_LISTENER_SIZE) { - DHLOGE("listeners are over size!"); - return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; - } if (ret == DH_FWK_SUCCESS) { - DHFWKSAManager::publisherListenersCahce_[topic].insert(listener); + return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener); } } else { DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); - if (DHFWKSAManager::publisherListenersCahce_.size() >= MAX_TOPIC_SIZE || - DHFWKSAManager::publisherListenersCahce_[topic].size() >= MAX_LISTENER_SIZE) { - DHLOGE("listeners are over size!"); - return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; - } - DHFWKSAManager::publisherListenersCahce_[topic].insert(listener); + return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener); } + return ret; } @@ -88,8 +77,8 @@ int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic top ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret); } - std::lock_guard publisherListenersLock(DHFWKSAManager::publisherListenersMutex_); - DHFWKSAManager::publisherListenersCahce_[topic].erase(listener); + + DHFWKSAManager::GetInstance().RemovePublisherListenerFromCache(topic, listener); return ret; } @@ -168,7 +157,8 @@ int32_t DistributedHardwareFwkKit::ReleaseAVCenter(int32_t engineId) DHLOGI("DHFWK not online or get proxy failed, can not release av control center"); return ERR_DH_FWK_POINTER_IS_NULL; } - + + DHFWKSAManager::GetInstance().RemoveAVTransControlCenterCbFromCache(engineId); return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ReleaseAVCenter(engineId); } @@ -206,8 +196,7 @@ int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId, DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); return ERR_DH_FWK_POINTER_IS_NULL; } - std::lock_guard avTransControlCenterCbLock(DHFWKSAManager::avTransControlCenterCbMutex_); - DHFWKSAManager::avTransControlCenterCbCache_[engineId] = callback; + DHFWKSAManager::GetInstance().AddAVTransControlCenterCbToCache(engineId, callback); return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback); } @@ -246,6 +235,5 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); } - } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp index ee75ea8c..ec82da2a 100644 --- a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -20,22 +20,18 @@ #include "iservice_registry.h" #include "system_ability_definition.h" +#include "constants.h" #include "distributed_hardware_log.h" #include "distributed_hardware_proxy.h" #include "distributed_hardware_errno.h" -#include "distributed_hardware_fwk_kit.h" namespace OHOS { namespace DistributedHardware { -std::mutex DHFWKSAManager::publisherListenersMutex_; -std::unordered_map>> DHFWKSAManager::publisherListenersCahce_; -std::mutex DHFWKSAManager::avTransControlCenterCbMutex_; -std::unordered_map> DHFWKSAManager::avTransControlCenterCbCache_; - IMPLEMENT_SINGLE_INSTANCE(DHFWKSAManager); DHFWKSAManager::DHFWKSAManager() : dhfwkOnLine_(false), isSubscribeDHFWKSAChangeListener(false), dhfwkProxy_(nullptr), - saListener_(new SystemAbilityListener()), saStateCallback(nullptr) + saListener_(new SystemAbilityListener()), saStateCallback(nullptr), + publisherListenersCache_({}), avTransControlCenterCbCache_({}) { DHLOGI("Ctor DHFWKSAManager"); } @@ -119,7 +115,7 @@ void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbi DHFWKSAManager::GetInstance().saStateCallback(true); } } - if (RestoreListener() != DH_FWK_SUCCESS) { + if (DHFWKSAManager::GetInstance().RestoreListener() != DH_FWK_SUCCESS) { DHLOGE("Partial listeners failed to restore"); } DHLOGI("sa %" PRId32 " started", systemAbilityId); @@ -154,27 +150,62 @@ int32_t DHFWKSAManager::RestoreListener() return ERR_DH_FWK_POINTER_IS_NULL; } int32_t ret = DH_FWK_SUCCESS; - std::lock_guard publisherListenersLock(publisherListenersMutex_); - for (const auto &entry : publisherListenersCahce_) { - for (const auto &listener : entry.second) { - int32_t innerRet = - DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); + { + std::lock_guard publisherListenersLock(publisherListenersMutex_); + for (const auto &entry : publisherListenersCache_) { + for (const auto &listener : entry.second) { + int32_t innerRet = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); if (ret != DH_FWK_SUCCESS) { ret = innerRet; DHLOGE("Register publisher listener failed, topic: %" PRIu32, (uint32_t)entry.first); + } } } } - std::lock_guard avTransControlCenterCbLock(avTransControlCenterCbMutex_); - for (auto &entry : avTransControlCenterCbCache_) { - int32_t innerRet = - DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(entry.first, entry.second); - if (innerRet != DH_FWK_SUCCESS) { - ret = innerRet; - DHLOGE("Restore register av control center callback failed, engineId: %" PRId32, entry.first); + { + std::lock_guard avTransControlCenterCbLock(avTransControlCenterCbMutex_); + for (auto &entry : avTransControlCenterCbCache_) { + int32_t innerRet = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(entry.first, entry.second); + if (innerRet != DH_FWK_SUCCESS) { + ret = innerRet; + DHLOGE("Restore register av control center callback failed, engineId: %" PRId32, entry.first); + } } } return ret; } + +int32_t DHFWKSAManager::AddPublisherListenerToCache(const DHTopic topic, sptr listener) +{ + std::lock_guard publisherListenersLock(publisherListenersMutex_); + if (publisherListenersCache_.size() >= MAX_TOPIC_SIZE || + publisherListenersCache_[topic].size() >= MAX_LISTENER_SIZE) { + DHLOGE("listeners are over size!"); + return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; + } + publisherListenersCache_[topic].insert(listener); + return DH_FWK_SUCCESS; +} + +void DHFWKSAManager::RemovePublisherListenerFromCache(const DHTopic topic, sptr listener) +{ + std::lock_guard publisherListenersLock(publisherListenersMutex_); + publisherListenersCache_[topic].erase(listener); +} + +void DHFWKSAManager::AddAVTransControlCenterCbToCache(int32_t engineId, + const sptr callback) +{ + std::lock_guard avTransControlCenterCbLock(avTransControlCenterCbMutex_); + avTransControlCenterCbCache_[engineId] = callback; +} + +void DHFWKSAManager::RemoveAVTransControlCenterCbFromCache(int32_t engineId) +{ + std::lock_guard avTransControlCenterCbLock(avTransControlCenterCbMutex_); + avTransControlCenterCbCache_.erase(engineId); +} } // DistributedHardware } // OHOS \ No newline at end of file -- Gitee From 4f357452733d1a03cdb105d0beb635fedb7e37c3 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Mon, 29 Jan 2024 19:26:10 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=B1=E8=B4=A5UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../component_privacy/component_privacy_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_privacy/component_privacy_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_privacy/component_privacy_test.cpp index 87b917e4..19025e5e 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_privacy/component_privacy_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_privacy/component_privacy_test.cpp @@ -71,7 +71,7 @@ HWTEST_F(ComponentPrivacyTest, StartPrivacePage_001, TestSize.Level0) std::string subtype = "mic"; std::string networkId = "networkId_test"; int32_t ret = compPrivacy_->StartPrivacePage(subtype, networkId); - EXPECT_EQ(AAFwk::RESOLVE_ABILITY_ERR, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } HWTEST_F(ComponentPrivacyTest, StartPrivacePage_002, TestSize.Level0) @@ -79,21 +79,21 @@ HWTEST_F(ComponentPrivacyTest, StartPrivacePage_002, TestSize.Level0) std::string subtype = "camera"; std::string networkId = "networkId_test"; int32_t ret = compPrivacy_->StartPrivacePage(subtype, networkId); - EXPECT_EQ(AAFwk::RESOLVE_ABILITY_ERR, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } HWTEST_F(ComponentPrivacyTest, StopPrivacePage_001, TestSize.Level0) { std::string subtype = "mic"; int32_t ret = compPrivacy_->StopPrivacePage(subtype); - EXPECT_EQ(AAFwk::RESOLVE_ABILITY_ERR, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } HWTEST_F(ComponentPrivacyTest, StopPrivacePage_002, TestSize.Level0) { std::string subtype = "camera"; int32_t ret = compPrivacy_->StopPrivacePage(subtype); - EXPECT_EQ(AAFwk::RESOLVE_ABILITY_ERR, ret); + EXPECT_EQ(DH_FWK_SUCCESS, ret); } HWTEST_F(ComponentPrivacyTest, DeviceTypeToString_001, TestSize.Level0) -- Gitee From 04f46ffa5a7ab3c79dae889ed875a3d7507d13f0 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Wed, 31 Jan 2024 06:39:08 +0000 Subject: [PATCH 09/12] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!5?= =?UTF-8?q?00=20:=20=E5=9B=9E=E9=80=80=20'Pull=20Request=20!489=20:=20dist?= =?UTF-8?q?ributedhardware=5Fdistributed=5Fhardware=5Ffwk=E4=BB=93?= =?UTF-8?q?=E9=80=82=E9=85=8Dgoogletest1.13.0''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/test/unittest/common/eventbus/event_bus_test.cpp | 1 - utils/test/unittest/common/eventbus/event_bus_test.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/test/unittest/common/eventbus/event_bus_test.cpp b/utils/test/unittest/common/eventbus/event_bus_test.cpp index 400eafef..78b2b81c 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.cpp +++ b/utils/test/unittest/common/eventbus/event_bus_test.cpp @@ -15,7 +15,6 @@ #include "event_bus_test.h" -#include #include #include #include diff --git a/utils/test/unittest/common/eventbus/event_bus_test.h b/utils/test/unittest/common/eventbus/event_bus_test.h index 3d953e75..e3be69e1 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.h +++ b/utils/test/unittest/common/eventbus/event_bus_test.h @@ -17,6 +17,7 @@ #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H #include +#include #include #include -- Gitee From 80135bee6474bc45f932228d7b548e92f97c3d29 Mon Sep 17 00:00:00 2001 From: s30051537 Date: Tue, 6 Feb 2024 11:22:52 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E6=95=8F=E6=84=9F=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: s30051537 --- interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp | 6 +++--- .../src/componentmanager/component_manager.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 0ff888a2..294dabdf 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -202,7 +202,7 @@ int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId, int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId) { - DHLOGI("Pause distributed hardware dhType %u, networkId %s", (uint32_t)dhType, networkId.c_str()); + DHLOGI("Pause distributed hardware dhType %u, networkId %s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); @@ -214,7 +214,7 @@ int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId) { - DHLOGI("Resume distributed hardware dhType %u, networkId %s", (uint32_t)dhType, networkId.c_str()); + DHLOGI("Resume distributed hardware dhType %u, networkId %s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); @@ -226,7 +226,7 @@ int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, cons int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId) { - DHLOGI("Stop distributed hardware dhType %u, networkId %s", (uint32_t)dhType, networkId.c_str()); + DHLOGI("Stop distributed hardware dhType %u, networkId %s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 35eae657..4b021b66 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -446,7 +446,7 @@ int32_t ComponentManager::Disable(const std::string &networkId, const std::strin { auto find = compSource_.find(dhType); if (find == compSource_.end()) { - DHLOGE("can not find handler for dhId = %s.", dhId.c_str()); + DHLOGE("can not find handler for dhId = %s.", GetAnonyString(dhId).c_str()); return ERR_DH_FWK_PARA_INVALID; } -- Gitee From 05cad9c774fc2f0462856285ddf50cfc668a7a99 Mon Sep 17 00:00:00 2001 From: yangli <1220629463@qq.com> Date: Tue, 20 Feb 2024 17:04:16 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9audio=E6=A6=82=E7=8E=87?= =?UTF-8?q?crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangli <1220629463@qq.com> --- .../av_transport_input_filter.cpp | 46 +++++++++---------- .../av_transport_input_filter.h | 4 +- .../av_transport_output_filter.cpp | 36 +++++++-------- .../av_transport_output_filter.h | 4 +- .../av_transport_input_filter_test.cpp | 9 ++-- .../av_transport_output_filter_test.cpp | 2 +- .../daudio_input/daudio_input_plugin.cpp | 10 ++-- .../daudio_input/daudio_input_plugin.h | 2 +- .../dscreen_input/dscreen_input_plugin.cpp | 8 ++-- .../dscreen_input/dscreen_input_plugin.h | 2 +- .../dsoftbus_input/dsoftbus_input_plugin.cpp | 35 +++++++------- .../dsoftbus_input/dsoftbus_input_plugin.h | 16 ++++++- .../dsoftbus_input_audio_plugin.cpp | 41 +++++++++-------- .../dsoftbus_input_audio_plugin.h | 15 +++++- .../daudio_output/daudio_output_plugin.cpp | 39 ++++++++-------- .../daudio_output/daudio_output_plugin.h | 15 +++++- .../dscreen_output/dscreen_output_plugin.cpp | 43 ++++++----------- .../dscreen_output/dscreen_output_plugin.h | 15 +++++- .../dsoftbus_output_plugin.cpp | 36 +++++++-------- .../dsoftbus_output/dsoftbus_output_plugin.h | 16 ++++++- .../dsoftbus_output_audio_plugin.cpp | 36 +++++++-------- .../dsoftbus_output_audio_plugin.h | 16 ++++++- .../common/include/av_trans_constants.h | 2 +- .../common/src/softbus_channel_adapter.cpp | 19 ++++++-- 24 files changed, 265 insertions(+), 202 deletions(-) diff --git a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp index 9610da89..058a8e88 100644 --- a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp +++ b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp @@ -54,7 +54,7 @@ ErrorCode AVInputFilter::SetParameter(int32_t key, const Any& value) plugin_->SetParameter(static_cast(key), value); } { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); paramsMap_[tag] = value; } return ErrorCode::SUCCESS; @@ -68,7 +68,7 @@ ErrorCode AVInputFilter::GetParameter(int32_t key, Any& value) return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; } { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); value = paramsMap_[tag]; } return ErrorCode::SUCCESS; @@ -108,7 +108,7 @@ ErrorCode AVInputFilter::Prepare() ErrorCode AVInputFilter::Start() { AVTRANS_LOGI("Start"); - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (state_ != FilterState::READY && state_ != FilterState::PAUSED) { AVTRANS_LOGE("The current state is invalid"); return ErrorCode::ERROR_INVALID_STATE; @@ -128,18 +128,18 @@ ErrorCode AVInputFilter::Start() ErrorCode AVInputFilter::Stop() { AVTRANS_LOGI("Stop"); - OSAL::ScopedLock lock(inputFilterMutex_); - if (state_ != FilterState::RUNNING && state_ != FilterState::PAUSED) { - AVTRANS_LOGE("The current state is invalid"); - return ErrorCode::ERROR_INVALID_STATE; - } + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } + if (state_ != FilterState::RUNNING && state_ != FilterState::PAUSED) { + AVTRANS_LOGE("The current state is invalid"); + plugin_->Deinit(); + return ErrorCode::SUCCESS; + } if (TranslatePluginStatus(plugin_->Stop()) != ErrorCode::SUCCESS) { AVTRANS_LOGE("The plugin stop fail!"); - return ErrorCode::ERROR_INVALID_OPERATION; } plugin_->Deinit(); plugin_ = nullptr; @@ -150,7 +150,7 @@ ErrorCode AVInputFilter::Stop() ErrorCode AVInputFilter::Pause() { AVTRANS_LOGI("Pause"); - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (state_ == FilterState::PAUSED) { return ErrorCode::SUCCESS; } @@ -174,7 +174,7 @@ ErrorCode AVInputFilter::Pause() ErrorCode AVInputFilter::Resume() { AVTRANS_LOGI("Resume"); - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -193,14 +193,14 @@ void AVInputFilter::InitPorts() AVTRANS_LOGI("InitPorts"); auto outPort = std::make_shared(this); { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); outPorts_.push_back(outPort); } } ErrorCode AVInputFilter::FindPlugin() { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); std::string mime; if (paramsMap_.find(Tag::MIME) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MIME])) { @@ -307,7 +307,7 @@ ErrorCode AVInputFilter::DoConfigure() ErrorCode AVInputFilter::MergeMeta(const Plugin::Meta& meta, Plugin::Meta& targetMeta) { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (!MergeMetaWithCapability(meta, capNegWithDownstream_, targetMeta)) { AVTRANS_LOGE("cannot find available capability of plugin %s", pluginInfo_->name.c_str()); return ErrorCode::ERROR_INVALID_OPERATION; @@ -318,7 +318,7 @@ ErrorCode AVInputFilter::MergeMeta(const Plugin::Meta& meta, Plugin::Meta& targe ErrorCode AVInputFilter::ConfigMeta(Plugin::Meta& meta) { AVTRANS_LOGI("ConfigMeta start!"); - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); if (paramsMap_.find(Tag::MEDIA_TYPE) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MEDIA_TYPE])) { AVTRANS_LOGE("MEDIA_TYPE in ParamsMap is not exist!"); @@ -459,7 +459,7 @@ ErrorCode AVInputFilter::ConfigDownStream(const Plugin::Meta& meta) { Meta upstreamParams; Meta downstreamParams; - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (outPorts_.size() == 0 || outPorts_[0] == nullptr) { AVTRANS_LOGE("outPorts is empty or invalid!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -474,7 +474,7 @@ ErrorCode AVInputFilter::ConfigDownStream(const Plugin::Meta& meta) ErrorCode AVInputFilter::InitPlugin() { AVTRANS_LOGI("InitPlugin"); - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -505,7 +505,7 @@ ErrorCode AVInputFilter::ConfigPlugin() ErrorCode AVInputFilter::SetPluginParams() { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -525,7 +525,7 @@ ErrorCode AVInputFilter::SetPluginParams() ErrorCode AVInputFilter::PreparePlugin() { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -535,7 +535,7 @@ ErrorCode AVInputFilter::PreparePlugin() ErrorCode AVInputFilter::PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (name_.compare(inPort) != 0) { AVTRANS_LOGE("FilterName is not targetName!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -569,7 +569,7 @@ ErrorCode AVInputFilter::PushData(const std::string& inPort, const AVBufferPtr& ErrorCode AVInputFilter::SetEventCallBack() { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER ; @@ -579,7 +579,7 @@ ErrorCode AVInputFilter::SetEventCallBack() ErrorCode AVInputFilter::SetDataCallBack() { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -590,7 +590,7 @@ ErrorCode AVInputFilter::SetDataCallBack() void AVInputFilter::OnDataCallback(std::shared_ptr buffer) { - OSAL::ScopedLock lock(inputFilterMutex_); + std::lock_guard lock(inputFilterMutex_); if (buffer == nullptr) { AVTRANS_LOGE("buffer is nullptr!"); return; diff --git a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h index 8debcd5f..a0104db9 100644 --- a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h +++ b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h @@ -18,6 +18,7 @@ #include #include +#include #include "foundation/osal/utils/util.h" #include "foundation/utils/constants.h" @@ -75,7 +76,8 @@ private: std::shared_ptr pluginInfo_ {nullptr}; Capability capNegWithDownstream_ {}; std::unordered_map paramsMap_; - OSAL::Mutex inputFilterMutex_ {}; + std::mutex inputFilterMutex_; + std::mutex paramsMapMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp index ebae8f18..ba9c21ef 100644 --- a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp +++ b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp @@ -54,7 +54,7 @@ ErrorCode AVOutputFilter::SetParameter(int32_t key, const Any& value) plugin_->SetParameter(static_cast(key), value); } { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); paramsMap_[tag] = value; } return ErrorCode::SUCCESS; @@ -68,7 +68,7 @@ ErrorCode AVOutputFilter::GetParameter(int32_t key, Any& value) return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; } { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); value = paramsMap_[tag]; } return ErrorCode::SUCCESS; @@ -114,7 +114,7 @@ ErrorCode AVOutputFilter::Prepare() ErrorCode AVOutputFilter::Start() { AVTRANS_LOGI("Start"); - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (state_ != FilterState::READY && state_ != FilterState::PAUSED) { AVTRANS_LOGE("The current state is invalid"); return ErrorCode::ERROR_INVALID_STATE; @@ -134,18 +134,18 @@ ErrorCode AVOutputFilter::Start() ErrorCode AVOutputFilter::Stop() { AVTRANS_LOGI("Stop"); - OSAL::ScopedLock lock(outputFilterMutex_); - if (state_ != FilterState::RUNNING) { - AVTRANS_LOGE("The current state is invalid"); - return ErrorCode::ERROR_INVALID_STATE; - } + std::lock_guard lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } + if (state_ != FilterState::RUNNING) { + AVTRANS_LOGE("The current state is invalid"); + plugin_->Deinit(); + return ErrorCode::SUCCESS; + } if (TranslatePluginStatus(plugin_->Stop()) != ErrorCode::SUCCESS) { AVTRANS_LOGE("The plugin stop fail!"); - return ErrorCode::ERROR_INVALID_OPERATION; } plugin_->Deinit(); plugin_ = nullptr; @@ -170,14 +170,14 @@ void AVOutputFilter::InitPorts() AVTRANS_LOGI("InitPorts"); auto inPort = std::make_shared(this); { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); inPorts_.push_back(inPort); } } ErrorCode AVOutputFilter::FindPlugin() { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); std::string mime; if (paramsMap_.find(Tag::MIME) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MIME])) { @@ -248,7 +248,7 @@ bool AVOutputFilter::Configure(const std::string& inPort, const std::shared_ptr< ErrorCode AVOutputFilter::InitPlugin() { AVTRANS_LOGI("InitPlugin"); - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -279,7 +279,7 @@ ErrorCode AVOutputFilter::ConfigPlugin() ErrorCode AVOutputFilter::PreparePlugin() { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; @@ -289,7 +289,7 @@ ErrorCode AVOutputFilter::PreparePlugin() ErrorCode AVOutputFilter::PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (buffer == nullptr || plugin_ == nullptr) { AVTRANS_LOGE("buffer or plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; @@ -301,7 +301,7 @@ ErrorCode AVOutputFilter::PushData(const std::string& inPort, const AVBufferPtr& ErrorCode AVOutputFilter::SetPluginParams() { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(paramsMapMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -329,7 +329,7 @@ ErrorCode AVOutputFilter::SetPluginParams() ErrorCode AVOutputFilter::SetEventCallBack() { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -340,7 +340,7 @@ ErrorCode AVOutputFilter::SetEventCallBack() ErrorCode AVOutputFilter::SetDataCallBack() { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -351,7 +351,7 @@ ErrorCode AVOutputFilter::SetDataCallBack() void AVOutputFilter::OnDataCallback(std::shared_ptr buffer) { - OSAL::ScopedLock lock(outputFilterMutex_); + std::lock_guard lock(outputFilterMutex_); if (buffer == nullptr) { AVTRANS_LOGE("buffer is nullptr!"); return; diff --git a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h index 5390ae79..6609f898 100644 --- a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h +++ b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h @@ -18,6 +18,7 @@ #include #include +#include #include "foundation/osal/utils/util.h" #include "foundation/utils/constants.h" @@ -70,7 +71,8 @@ private: std::shared_ptr plugin_ {nullptr}; std::shared_ptr pluginInfo_ {nullptr}; std::unordered_map paramsMap_; - OSAL::Mutex outputFilterMutex_ {}; + std::mutex outputFilterMutex_; + std::mutex paramsMapMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp b/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp index 1485c9f4..c6ed6794 100644 --- a/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp +++ b/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp @@ -140,15 +140,12 @@ HWTEST_F(AvTransportInputFilterTest, Stop_001, testing::ext::TestSize.Level1) std::shared_ptr avInputTest_ = FilterFactory::Instance().CreateFilterWithType(AVINPUT_NAME, FILTERNAME); ErrorCode ret = avInputTest_->Stop(); - EXPECT_EQ(ErrorCode::ERROR_INVALID_STATE, ret); - - avInputTest_->state_ = FilterState::RUNNING; - ret = avInputTest_->Stop(); EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); - + avInputTest_->plugin_ = + PluginManager::Instance().CreateGenericPlugin("AVTransDaudioInputPlugin"); avInputTest_->state_ = FilterState::PAUSED; ret = avInputTest_->Stop(); - EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); + EXPECT_EQ(ErrorCode::SUCCESS, ret); } HWTEST_F(AvTransportInputFilterTest, Stop_002, testing::ext::TestSize.Level1) diff --git a/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp b/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp index eee47e2f..ed063b7c 100644 --- a/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp +++ b/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp @@ -136,7 +136,7 @@ HWTEST_F(AvTransportOutputFilterTest, Stop_001, testing::ext::TestSize.Level1) std::shared_ptr avOutputTest_ = FilterFactory::Instance().CreateFilterWithType(AVOUTPUT_NAME, FILTERNAME); ErrorCode ret = avOutputTest_->Stop(); - EXPECT_EQ(ErrorCode::ERROR_INVALID_STATE, ret); + EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); } HWTEST_F(AvTransportOutputFilterTest, Stop_002, testing::ext::TestSize.Level1) diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp index 47773c31..dfa0f686 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp @@ -75,7 +75,7 @@ Status DaudioInputPlugin::Deinit() Status DaudioInputPlugin::Reset() { AVTRANS_LOGI("Reset enter."); - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(tagMapMutex_); tagMap_.clear(); frameNumber_.store(0); return Status::OK; @@ -84,7 +84,6 @@ Status DaudioInputPlugin::Reset() Status DaudioInputPlugin::Pause() { AVTRANS_LOGI("Pause enter."); - Media::OSAL::ScopedLock lock(operationMutes_); if ((sharedMemory_.fd > 0) && (sharedMemory_.size > 0) && !sharedMemory_.name.empty()) { ResetSharedMemory(sharedMemory_); } @@ -100,7 +99,7 @@ Status DaudioInputPlugin::Resume() Status DaudioInputPlugin::GetParameter(Tag tag, ValueType &value) { { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(tagMapMutex_); auto iter = tagMap_.find(tag); if (iter != tagMap_.end()) { value = iter->second; @@ -112,7 +111,7 @@ Status DaudioInputPlugin::GetParameter(Tag tag, ValueType &value) Status DaudioInputPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(tagMapMutex_); tagMap_.insert(std::make_pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { sharedMemory_ = UnmarshalSharedMemory(Media::Plugin::AnyCast(value)); @@ -122,9 +121,8 @@ Status DaudioInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DaudioInputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(tagMapMutex_); TRUE_RETURN_V(buffer == nullptr, Status::ERROR_NULL_POINTER); - if (buffer->IsEmpty()) { AVTRANS_LOGE("bufferData is Empty."); return Status::ERROR_INVALID_PARAMETER; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h index 3596e470..396d9e8c 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h @@ -63,8 +63,8 @@ public: private: std::atomic frameNumber_; + std::mutex tagMapMutex_; std::map tagMap_; - OSAL::Mutex operationMutes_ {}; AVTransSharedMemory sharedMemory_ = AVTransSharedMemory{ 0, 0, "" }; }; } // namespace DistributedHardware diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp index 35e914de..d24a8aa9 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp @@ -74,7 +74,7 @@ Status DscreenInputPlugin::Deinit() Status DscreenInputPlugin::Reset() { AVTRANS_LOGI("Reset"); - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); paramsMap_.clear(); frameNumber_.store(0); return Status::OK; @@ -94,7 +94,7 @@ Status DscreenInputPlugin::Resume() Status DscreenInputPlugin::GetParameter(Tag tag, ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -105,7 +105,7 @@ Status DscreenInputPlugin::GetParameter(Tag tag, ValueType &value) Status DscreenInputPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); paramsMap_.insert(std::pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { sharedMemory_ = UnmarshalSharedMemory(Media::Plugin::AnyCast(value)); @@ -115,7 +115,7 @@ Status DscreenInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DscreenInputPlugin::PushData(const std::string& inPort, std::shared_ptr buffer, int32_t offset) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); if (!buffer || buffer->IsEmpty()) { AVTRANS_LOGE("buffer is nullptr or empty."); return Status::ERROR_NULL_POINTER; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h index f8b8f110..9e00b5dc 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h @@ -57,7 +57,7 @@ public: private: std::atomic frameNumber_; - Media::OSAL::Mutex operationMutes_ {}; + std::mutex paramsMapMutex_; std::map paramsMap_; AVTransSharedMemory sharedMemory_ = AVTransSharedMemory{ 0, 0, "" }; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp index 30cc3c2f..61785037 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp @@ -74,10 +74,9 @@ DsoftbusInputPlugin::~DsoftbusInputPlugin() Status DsoftbusInputPlugin::Init() { AVTRANS_LOGI("Init"); - Media::OSAL::ScopedLock lock(operationMutes_); dumpFlag_.store(false); reDumpFlag_.store(false); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -90,8 +89,7 @@ Status DsoftbusInputPlugin::Deinit() Status DsoftbusInputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::INITIALIZED) { + if (GetCurrentState() != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -111,16 +109,16 @@ Status DsoftbusInputPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { HandleData(); }); } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); return Status::OK; } Status DsoftbusInputPlugin::Reset() { AVTRANS_LOGI("Reset"); - Media::OSAL::ScopedLock lock(operationMutes_); eventsCb_ = nullptr; dataCb_ = nullptr; + std::lock_guard lock(paramsMapMutex_); paramsMap_.clear(); if (bufferPopTask_) { bufferPopTask_->Stop(); @@ -129,33 +127,31 @@ Status DsoftbusInputPlugin::Reset() DataQueueClear(dataQueue_); SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } Status DsoftbusInputPlugin::Start() { AVTRANS_LOGI("Start"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::PREPARED) { + if (GetCurrentState() != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); return Status::OK; } Status DsoftbusInputPlugin::Stop() { AVTRANS_LOGI("Stop"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); bufferPopTask_->Stop(); DataQueueClear(dataQueue_); return Status::OK; @@ -175,6 +171,7 @@ Status DsoftbusInputPlugin::Resume() Status DsoftbusInputPlugin::GetParameter(Tag tag, ValueType &value) { + std::lock_guard lock(paramsMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -185,7 +182,7 @@ Status DsoftbusInputPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusInputPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -201,7 +198,7 @@ Status DsoftbusInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusInputPlugin::SetCallback(Callback *cb) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -213,7 +210,7 @@ Status DsoftbusInputPlugin::SetCallback(Callback *cb) Status DsoftbusInputPlugin::SetDataCallback(AVDataCallback callback) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -301,6 +298,7 @@ void DsoftbusInputPlugin::DataEnqueue(std::shared_ptr &buffer) } else { AVTRANS_LOGE("DumpFlag = false."); } + std::lock_guard lock(dataQueueMtx_); while (dataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("Data queue overflow."); dataQueue_.pop(); @@ -311,13 +309,13 @@ void DsoftbusInputPlugin::DataEnqueue(std::shared_ptr &buffer) void DsoftbusInputPlugin::HandleData() { - while (state_ == State::RUNNING) { + while (GetCurrentState() == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -336,6 +334,7 @@ void DsoftbusInputPlugin::HandleData() void DsoftbusInputPlugin::DataQueueClear(std::queue> &queue) { + std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h index 99147b6a..ff5c8605 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h @@ -81,6 +81,17 @@ private: void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: std::atomic dumpFlag_ ; @@ -90,11 +101,12 @@ private: std::string peerDevId_; std::condition_variable dataCond_; std::shared_ptr bufferPopTask_; + std::mutex stateMutex_; std::mutex dataQueueMtx_; - Media::OSAL::Mutex operationMutes_ {}; + std::mutex paramsMapMutex_; std::queue> dataQueue_; std::map paramsMap_; - State state_ {State::CREATED}; + std::atomic state_ = State::CREATED; Callback* eventsCb_ = nullptr; AVDataCallback dataCb_; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp index 9c6a470c..c13fb2fa 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp @@ -63,8 +63,7 @@ DsoftbusInputAudioPlugin::~DsoftbusInputAudioPlugin() Status DsoftbusInputAudioPlugin::Init() { AVTRANS_LOGI("Init"); - Media::OSAL::ScopedLock lock(operationMutes_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -77,8 +76,7 @@ Status DsoftbusInputAudioPlugin::Deinit() Status DsoftbusInputAudioPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::INITIALIZED) { + if (GetCurrentState() != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -99,17 +97,19 @@ Status DsoftbusInputAudioPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { HandleData(); }); } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); return Status::OK; } Status DsoftbusInputAudioPlugin::Reset() { AVTRANS_LOGI("Reset"); - Media::OSAL::ScopedLock lock(operationMutes_); eventsCb_ = nullptr; dataCb_ = nullptr; - paramsMap_.clear(); + { + std::lock_guard lock(paramsMapMutex_); + paramsMap_.clear(); + } if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -117,35 +117,33 @@ Status DsoftbusInputAudioPlugin::Reset() DataQueueClear(dataQueue_); SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } Status DsoftbusInputAudioPlugin::Start() { AVTRANS_LOGI("Start"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::PREPARED) { + if (GetCurrentState() != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); return Status::OK; } Status DsoftbusInputAudioPlugin::Stop() { AVTRANS_LOGI("Stop"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - state_ = State::PREPARED; - bufferPopTask_->Stop(); + SetCurrentState(State::PREPARED); DataQueueClear(dataQueue_); + bufferPopTask_->Stop(); return Status::OK; } @@ -163,6 +161,7 @@ Status DsoftbusInputAudioPlugin::Resume() Status DsoftbusInputAudioPlugin::GetParameter(Tag tag, ValueType &value) { + std::lock_guard lock(paramsMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -173,7 +172,7 @@ Status DsoftbusInputAudioPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusInputAudioPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -183,7 +182,7 @@ Status DsoftbusInputAudioPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusInputAudioPlugin::SetCallback(Callback *cb) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -195,7 +194,7 @@ Status DsoftbusInputAudioPlugin::SetCallback(Callback *cb) Status DsoftbusInputAudioPlugin::SetDataCallback(AVDataCallback callback) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -264,6 +263,7 @@ std::shared_ptr DsoftbusInputAudioPlugin::CreateBuffer(uint32_t metaType void DsoftbusInputAudioPlugin::DataEnqueue(std::shared_ptr &buffer) { + std::lock_guard lock(dataQueueMtx_); while (dataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("Data queue overflow."); dataQueue_.pop(); @@ -274,13 +274,13 @@ void DsoftbusInputAudioPlugin::DataEnqueue(std::shared_ptr &buffer) void DsoftbusInputAudioPlugin::HandleData() { - while (state_ == State::RUNNING) { + while (GetCurrentState() == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -299,6 +299,7 @@ void DsoftbusInputAudioPlugin::HandleData() void DsoftbusInputAudioPlugin::DataQueueClear(std::queue> &queue) { + std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h index 0828187e..9ea9d88c 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h @@ -75,6 +75,17 @@ private: void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: std::string ownerName_; @@ -82,11 +93,13 @@ private: std::string peerDevId_; std::condition_variable dataCond_; std::shared_ptr bufferPopTask_; + std::mutex stateMutex_; std::mutex dataQueueMtx_; + std::mutex paramsMapMutex_; Media::OSAL::Mutex operationMutes_ {}; std::queue> dataQueue_; std::map paramsMap_; - State state_ {State::CREATED}; + std::atomic state_ = State::CREATED; Callback* eventsCb_ = nullptr; AVDataCallback dataCb_; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp index adbc026c..d8fee901 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp @@ -64,8 +64,7 @@ DaudioOutputPlugin::~DaudioOutputPlugin() Status DaudioOutputPlugin::Init() { AVTRANS_LOGI("Init."); - OSAL::ScopedLock lock(operationMutes_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -95,8 +94,7 @@ void DaudioOutputPlugin::RampleInit(uint32_t channels, uint32_t sampleRate, uint Status DaudioOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::INITIALIZED) { + if (GetCurrentState() != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -126,14 +124,13 @@ Status DaudioOutputPlugin::Prepare() } AVTRANS_LOGI("channels = %d, sampleRate = %d, channelLayout = %d.", channels, sampleRate, channelsLayout); RampleInit(channels, sampleRate, channelsLayout); - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); return Status::OK; } Status DaudioOutputPlugin::Reset() { AVTRANS_LOGI("Reset enter"); - OSAL::ScopedLock lock(operationMutes_); eventcallback_ = nullptr; if (sendPlayTask_) { sendPlayTask_->Stop(); @@ -143,14 +140,18 @@ Status DaudioOutputPlugin::Reset() resample_.reset(); } smIndex_ = 0; - paramsMap_.clear(); + { + std::lock_guard lock(paramsMapMutex_); + paramsMap_.clear(); + } DataQueueClear(outputBuffer_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } Status DaudioOutputPlugin::GetParameter(Tag tag, ValueType &value) { + std::lock_guard lock(paramsMapMutex_); auto iter = paramsMap_.find(tag); if (iter != paramsMap_.end()) { value = iter->second; @@ -161,7 +162,7 @@ Status DaudioOutputPlugin::GetParameter(Tag tag, ValueType &value) Status DaudioOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramsMapMutex_); paramsMap_.insert(std::make_pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { std::unique_lock lock(sharedMemMtx_); @@ -177,26 +178,24 @@ Status DaudioOutputPlugin::SetParameter(Tag tag, const ValueType &value) Status DaudioOutputPlugin::Start() { AVTRANS_LOGI("Start enter"); - OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::PREPARED) { + if (GetCurrentState() != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(outputBuffer_); sendPlayTask_->Start(); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); return Status::OK; } Status DaudioOutputPlugin::Stop() { AVTRANS_LOGI("Stop enter"); - OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); sendPlayTask_->Stop(); DataQueueClear(outputBuffer_); return Status::OK; @@ -204,7 +203,6 @@ Status DaudioOutputPlugin::Stop() Status DaudioOutputPlugin::SetCallback(Callback *cb) { - OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, cb is nullptr."); return Status::ERROR_NULL_POINTER; @@ -216,7 +214,6 @@ Status DaudioOutputPlugin::SetCallback(Callback *cb) Status DaudioOutputPlugin::SetDataCallback(AVDataCallback callback) { - OSAL::ScopedLock lock(operationMutes_); if (callback == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -242,8 +239,7 @@ Status DaudioOutputPlugin::PushData(const std::string &inPort, std::shared_ptr

GetMemory()->GetSize()); } - - OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(dataQueueMtx_); while (outputBuffer_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("outputBuffer_ queue overflow."); outputBuffer_.pop(); @@ -255,13 +251,13 @@ Status DaudioOutputPlugin::PushData(const std::string &inPort, std::shared_ptr

buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !outputBuffer_.empty(); }); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { return; } if (outputBuffer_.empty()) { @@ -312,6 +308,7 @@ void DaudioOutputPlugin::WriteMasterClockToMemory(const std::shared_ptr> &q) { + std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, q); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h index 38ba0e28..cb71fded 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h @@ -72,16 +72,27 @@ private: void DataQueueClear(std::queue> &q); void RampleInit(uint32_t channels, uint32_t sampleRate, uint32_t channelLayout); void WriteMasterClockToMemory(const std::shared_ptr &buffer); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: std::condition_variable dataCond_; + std::mutex paramsMapMutex_; std::map paramsMap_; std::mutex dataQueueMtx_; std::queue> outputBuffer_; std::shared_ptr sendPlayTask_; - Media::OSAL::Mutex operationMutes_ {}; - State state_ {State::CREATED}; + std::mutex stateMutex_; + std::atomic state_ = State::CREATED; Callback *eventcallback_ = nullptr; AVDataCallback datacallback_ = nullptr; std::shared_ptr resample_ {nullptr}; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp index 8ca47a61..6426d0e3 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp @@ -59,9 +59,8 @@ DscreenOutputPlugin::~DscreenOutputPlugin() Status DscreenOutputPlugin::Init() { AVTRANS_LOGI("Init."); - Media::OSAL::ScopedLock lock(operationMutes_); InitOutputController(); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -75,10 +74,10 @@ Status DscreenOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((state_ != State::INITIALIZED), Status::ERROR_WRONG_STATE, "The state is wrong."); + TRUE_RETURN_V_MSG_E((GetCurrentState() != State::INITIALIZED), Status::ERROR_WRONG_STATE, + "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); } controller_->PrepareControl(); return Status::OK; @@ -88,8 +87,7 @@ Status DscreenOutputPlugin::Reset() { AVTRANS_LOGI("Reset"); { - Media::OSAL::ScopedLock lock(operationMutes_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); eventsCb_ = nullptr; dataCb_ = nullptr; } @@ -103,19 +101,13 @@ Status DscreenOutputPlugin::Reset() Status DscreenOutputPlugin::GetParameter(Tag tag, ValueType &value) { - { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - } + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); return controller_->GetParameter(tag, value); } Status DscreenOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - } + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); return controller_->SetParameter(tag, value); } @@ -123,10 +115,9 @@ Status DscreenOutputPlugin::Start() { AVTRANS_LOGI("Start"); { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((state_ != State::PREPARED), Status::ERROR_WRONG_STATE, "The state is wrong."); + TRUE_RETURN_V_MSG_E((GetCurrentState() != State::PREPARED), Status::ERROR_WRONG_STATE, "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); } controller_->StartControl(); return Status::OK; @@ -136,10 +127,9 @@ Status DscreenOutputPlugin::Stop() { AVTRANS_LOGI("Stop"); { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((state_ != State::RUNNING), Status::ERROR_WRONG_STATE, "The state is wrong."); + TRUE_RETURN_V_MSG_E((GetCurrentState() != State::RUNNING), Status::ERROR_WRONG_STATE, "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); } controller_->StopControl(); return Status::OK; @@ -147,7 +137,6 @@ Status DscreenOutputPlugin::Stop() Status DscreenOutputPlugin::SetCallback(Callback *cb) { - Media::OSAL::ScopedLock lock(operationMutes_); TRUE_RETURN_V_MSG_E((!cb), Status::ERROR_NULL_POINTER, "SetCallback failed, cb is nullptr."); eventsCb_ = cb; AVTRANS_LOGI("SetCallback success."); @@ -156,7 +145,6 @@ Status DscreenOutputPlugin::SetCallback(Callback *cb) Status DscreenOutputPlugin::SetDataCallback(AVDataCallback callback) { - Media::OSAL::ScopedLock lock(operationMutes_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -164,12 +152,9 @@ Status DscreenOutputPlugin::SetDataCallback(AVDataCallback callback) Status DscreenOutputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - { - Media::OSAL::ScopedLock lock(operationMutes_); - TRUE_RETURN_V_MSG_E((buffer == nullptr || buffer->IsEmpty()), Status::ERROR_NULL_POINTER, - "AVBuffer is nullptr."); - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - } + TRUE_RETURN_V_MSG_E((buffer == nullptr || buffer->IsEmpty()), Status::ERROR_NULL_POINTER, + "AVBuffer is nullptr."); + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); controller_->PushData(buffer); return Status::OK; } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h index 7ff6534c..93b8ba4b 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h @@ -68,10 +68,21 @@ public: private: void InitOutputController(); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: - Media::OSAL::Mutex operationMutes_ {}; - State state_ {State::CREATED}; + std::mutex stateMutex_; + std::atomic state_ = State::CREATED; Callback *eventsCb_ = nullptr; AVDataCallback dataCb_; std::condition_variable dataCond_; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp index dae28ff0..6fa15b8f 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp @@ -71,10 +71,9 @@ DsoftbusOutputPlugin::~DsoftbusOutputPlugin() Status DsoftbusOutputPlugin::Init() { AVTRANS_LOGI("Init Dsoftbus Output Plugin."); - Media::OSAL::ScopedLock lock(operationMutes_); dumpFlag_.store(false); reDumpFlag_.store(false); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -87,8 +86,7 @@ Status DsoftbusOutputPlugin::Deinit() Status DsoftbusOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::INITIALIZED) { + if (GetCurrentState() != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -98,15 +96,17 @@ Status DsoftbusOutputPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { FeedChannelData(); }); } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); return Status::OK; } Status DsoftbusOutputPlugin::Reset() { AVTRANS_LOGI("Reset"); - Media::OSAL::ScopedLock lock(operationMutes_); - paramsMap_.clear(); + { + std::lock_guard lock(paramMapMutex_); + paramsMap_.clear(); + } if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -114,15 +114,14 @@ Status DsoftbusOutputPlugin::Reset() DataQueueClear(dataQueue_); eventsCb_ = nullptr; SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } Status DsoftbusOutputPlugin::Start() { AVTRANS_LOGI("Dsoftbus Output Plugin start."); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::PREPARED) { + if (GetCurrentState() != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -133,18 +132,18 @@ Status DsoftbusOutputPlugin::Start() } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); return Status::OK; } Status DsoftbusOutputPlugin::Stop() { AVTRANS_LOGI("Dsoftbus Output Plugin stop."); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); bufferPopTask_->Stop(); DataQueueClear(dataQueue_); CloseSoftbusChannel(); @@ -153,6 +152,7 @@ Status DsoftbusOutputPlugin::Stop() Status DsoftbusOutputPlugin::GetParameter(Tag tag, ValueType &value) { + std::lock_guard lock(paramMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -163,7 +163,7 @@ Status DsoftbusOutputPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramMapMutex_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -179,7 +179,6 @@ Status DsoftbusOutputPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusOutputPlugin::SetCallback(Callback *cb) { - Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallback failed, cb is nullptr."); return Status::ERROR_INVALID_OPERATION; @@ -246,7 +245,7 @@ void DsoftbusOutputPlugin::OnStreamReceived(const StreamData *data, const Stream Status DsoftbusOutputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(dataQueueMtx_); if (buffer == nullptr || buffer->IsEmpty()) { AVTRANS_LOGE("Buffer is nullptr."); return Status::ERROR_NULL_POINTER; @@ -273,13 +272,13 @@ Status DsoftbusOutputPlugin::PushData(const std::string &inPort, std::shared_ptr void DsoftbusOutputPlugin::FeedChannelData() { - while (state_ == State::RUNNING) { + while (GetCurrentState() == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -335,6 +334,7 @@ void DsoftbusOutputPlugin::SendDataToSoftbus(std::shared_ptr &buffer) void DsoftbusOutputPlugin::DataQueueClear(std::queue> &queue) { + std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h index 9fa03f39..c4c15196 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h @@ -79,6 +79,17 @@ private: void DataQueueClear(std::queue> &queue); void CloseSoftbusChannel(); void FeedChannelData(); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: std::atomic dumpFlag_ ; @@ -87,12 +98,13 @@ private: std::string sessionName_; std::string peerDevId_; std::condition_variable dataCond_; + std::mutex stateMutex_; std::mutex dataQueueMtx_; - Media::OSAL::Mutex operationMutes_ {}; + std::mutex paramMapMutex_; std::shared_ptr bufferPopTask_; std::queue> dataQueue_; std::map paramsMap_; - State state_ {State::CREATED}; + std::atomic state_ = State::CREATED; Callback* eventsCb_ = nullptr; }; } // namespace DistributedHardware diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp index 853b802a..8d7b7f62 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp @@ -63,8 +63,7 @@ DsoftbusOutputAudioPlugin::~DsoftbusOutputAudioPlugin() Status DsoftbusOutputAudioPlugin::Init() { AVTRANS_LOGI("Init."); - Media::OSAL::ScopedLock lock(operationMutes_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } @@ -77,8 +76,7 @@ Status DsoftbusOutputAudioPlugin::Deinit() Status DsoftbusOutputAudioPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::INITIALIZED) { + if (GetCurrentState() != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -100,15 +98,17 @@ Status DsoftbusOutputAudioPlugin::Prepare() sampleRate_ = static_cast(Plugin::AnyCast(sampleRateValue)); AVTRANS_LOGI("channels_ = %u, sampleRate_ = %u.", channels_, sampleRate_); - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); return Status::OK; } Status DsoftbusOutputAudioPlugin::Reset() { AVTRANS_LOGI("Reset"); - Media::OSAL::ScopedLock lock(operationMutes_); - paramsMap_.clear(); + { + std::lock_guard lock(paramMapMutex_); + paramsMap_.clear(); + } if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -117,15 +117,14 @@ Status DsoftbusOutputAudioPlugin::Reset() eventsCb_ = nullptr; SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - state_ = State::INITIALIZED; + SetCurrentState(State::INITIALIZED); return Status::OK; } Status DsoftbusOutputAudioPlugin::Start() { AVTRANS_LOGI("Dsoftbus Output Plugin start."); - Media::OSAL::ScopedLock lock(operationMutes_); - if (state_ != State::PREPARED) { + if (GetCurrentState() != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -136,18 +135,18 @@ Status DsoftbusOutputAudioPlugin::Start() } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - state_ = State::RUNNING; + SetCurrentState(State::RUNNING); return Status::OK; } Status DsoftbusOutputAudioPlugin::Stop() { AVTRANS_LOGI("Dsoftbus Output Plugin stop."); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - state_ = State::PREPARED; + SetCurrentState(State::PREPARED); bufferPopTask_->Stop(); DataQueueClear(dataQueue_); CloseSoftbusChannel(); @@ -156,6 +155,7 @@ Status DsoftbusOutputAudioPlugin::Stop() Status DsoftbusOutputAudioPlugin::GetParameter(Tag tag, ValueType &value) { + std::lock_guard lock(paramMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -166,7 +166,7 @@ Status DsoftbusOutputAudioPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusOutputAudioPlugin::SetParameter(Tag tag, const ValueType &value) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(paramMapMutex_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -176,7 +176,6 @@ Status DsoftbusOutputAudioPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusOutputAudioPlugin::SetCallback(Callback *cb) { - Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallback failed, cb is nullptr."); return Status::ERROR_INVALID_OPERATION; @@ -243,7 +242,7 @@ void DsoftbusOutputAudioPlugin::OnStreamReceived(const StreamData *data, const S Status DsoftbusOutputAudioPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - Media::OSAL::ScopedLock lock(operationMutes_); + std::lock_guard lock(dataQueueMtx_); if (buffer == nullptr || buffer->IsEmpty()) { AVTRANS_LOGE("Buffer is nullptr."); return Status::ERROR_NULL_POINTER; @@ -266,13 +265,13 @@ Status DsoftbusOutputAudioPlugin::PushData(const std::string &inPort, std::share void DsoftbusOutputAudioPlugin::FeedChannelData() { - while (state_ == State::RUNNING) { + while (GetCurrentState() == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (state_ != State::RUNNING) { + if (GetCurrentState() != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -329,6 +328,7 @@ void DsoftbusOutputAudioPlugin::SendDataToSoftbus(std::shared_ptr &buffe void DsoftbusOutputAudioPlugin::DataQueueClear(std::queue> &queue) { + std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h index 77ee3492..02246647 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h @@ -75,18 +75,30 @@ private: void DataQueueClear(std::queue> &queue); void CloseSoftbusChannel(); void FeedChannelData(); + State GetCurrentState() + { + std::lock_guard lock(stateMutex_); + return state_; + } + + void SetCurrentState(State state) + { + std::lock_guard lock(stateMutex_); + state_ = state; + } private: std::string ownerName_; std::string sessionName_; std::string peerDevId_; std::condition_variable dataCond_; + std::mutex stateMutex_; std::mutex dataQueueMtx_; - Media::OSAL::Mutex operationMutes_ {}; + std::mutex paramMapMutex_; std::shared_ptr bufferPopTask_; std::queue> dataQueue_; std::map paramsMap_; - State state_ {State::CREATED}; + std::atomic state_ = State::CREATED; Callback* eventsCb_ = nullptr; uint32_t sampleRate_ {0}; uint32_t channels_ {0}; diff --git a/av_transport/common/include/av_trans_constants.h b/av_transport/common/include/av_trans_constants.h index 022a5853..8673cdb1 100644 --- a/av_transport/common/include/av_trans_constants.h +++ b/av_transport/common/include/av_trans_constants.h @@ -30,7 +30,7 @@ const int32_t INVALID_SESSION_ID = -1; const int32_t SESSION_WAIT_SECONDS = 5; const int32_t INTERCERT_STRING_LENGTH = 20; const int32_t PLUGIN_RANK = 100; -const int32_t PLUGIN_TASK_WAIT_TIME = 20; +const int32_t PLUGIN_TASK_WAIT_TIME = 10; const uint32_t VIDEO_H264_LEVEL = 32; const uint32_t MAX_DEVICE_ID_LEN = 100; diff --git a/av_transport/common/src/softbus_channel_adapter.cpp b/av_transport/common/src/softbus_channel_adapter.cpp index c0f3a180..6345aa1c 100644 --- a/av_transport/common/src/softbus_channel_adapter.cpp +++ b/av_transport/common/src/softbus_channel_adapter.cpp @@ -333,7 +333,13 @@ int32_t SoftbusChannelAdapter::SendBytesData(const std::string& sessName, const TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty."); TRUE_RETURN_V_MSG_E(data.empty(), ERR_DH_AVT_INVALID_PARAM, "input data string is empty."); - int32_t ret = SendBytes(GetSessIdBySessName(sessName, peerDevId), data.c_str(), strlen(data.c_str())); + int32_t existSessId = GetSessIdBySessName(sessName, peerDevId); + if (existSessId < 0) { + AVTRANS_LOGI("Can not find sessionId for mySessName:%s, peerDevId:%s.", + sessName.c_str(), GetAnonyString(peerDevId).c_str()); + return ERR_DH_AVT_SEND_DATA_FAILED; + } + int32_t ret = SendBytes(existSessId, data.c_str(), strlen(data.c_str())); if (ret != DH_AVT_SUCCESS) { AVTRANS_LOGE("Send bytes data failed ret:%" PRId32, ret); return ERR_DH_AVT_SEND_DATA_FAILED; @@ -350,7 +356,13 @@ int32_t SoftbusChannelAdapter::SendStreamData(const std::string& sessName, const TRUE_RETURN_V_MSG_E(ext == nullptr, ERR_DH_AVT_INVALID_PARAM, "input ext data is nullptr."); StreamFrameInfo frameInfo = {0}; - int32_t ret = SendStream(GetSessIdBySessName(sessName, peerDevId), data, ext, &frameInfo); + int32_t existSessId = GetSessIdBySessName(sessName, peerDevId); + if (existSessId < 0) { + AVTRANS_LOGI("Can not find sessionId for mySessName:%s, peerDevId:%s.", + sessName.c_str(), GetAnonyString(peerDevId).c_str()); + return ERR_DH_AVT_SEND_DATA_FAILED; + } + int32_t ret = SendStream(existSessId, data, ext, &frameInfo); if (ret != DH_AVT_SUCCESS) { AVTRANS_LOGE("Send stream data failed ret:%" PRId32, ret); return ERR_DH_AVT_SEND_DATA_FAILED; @@ -493,7 +505,6 @@ void SoftbusChannelAdapter::OnSoftbusChannelClosed(int32_t sessionId, ShutdownRe for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end();) { if (it->second == sessionId) { event.content = GetOwnerFromSessName(it->first); - AVTRANS_LOGI("find sessName is: %s", it->first.c_str()); std::thread(&SoftbusChannelAdapter::SendChannelEvent, this, it->first, event).detach(); it = devId2SessIdMap_.erase(it); } else { @@ -596,7 +607,7 @@ std::string SoftbusChannelAdapter::GetOwnerFromSessName(const std::string &sessN void SoftbusChannelAdapter::SendChannelEvent(const std::string &sessName, const AVTransEvent event) { - AVTRANS_LOGI("SendChannelEvent event.type_%" PRId32", sessName: %s", event.type, sessName.c_str()); + AVTRANS_LOGI("SendChannelEvent event.type_%" PRId32, event.type); pthread_setname_np(pthread_self(), SEND_CHANNEL_EVENT); ISoftbusChannelListener *listener = nullptr; -- Gitee From 1d24fdabe2cf260e317db635fe4adba98af271a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=AB=8B?= Date: Tue, 20 Feb 2024 11:04:58 +0000 Subject: [PATCH 12/12] =?UTF-8?q?Revert=20"=E4=BF=AE=E6=94=B9audio?= =?UTF-8?q?=E6=A6=82=E7=8E=87crash"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 05cad9c774fc2f0462856285ddf50cfc668a7a99. --- .../av_transport_input_filter.cpp | 46 +++++++++---------- .../av_transport_input_filter.h | 4 +- .../av_transport_output_filter.cpp | 36 +++++++-------- .../av_transport_output_filter.h | 4 +- .../av_transport_input_filter_test.cpp | 9 ++-- .../av_transport_output_filter_test.cpp | 2 +- .../daudio_input/daudio_input_plugin.cpp | 10 ++-- .../daudio_input/daudio_input_plugin.h | 2 +- .../dscreen_input/dscreen_input_plugin.cpp | 8 ++-- .../dscreen_input/dscreen_input_plugin.h | 2 +- .../dsoftbus_input/dsoftbus_input_plugin.cpp | 35 +++++++------- .../dsoftbus_input/dsoftbus_input_plugin.h | 16 +------ .../dsoftbus_input_audio_plugin.cpp | 41 ++++++++--------- .../dsoftbus_input_audio_plugin.h | 15 +----- .../daudio_output/daudio_output_plugin.cpp | 39 ++++++++-------- .../daudio_output/daudio_output_plugin.h | 15 +----- .../dscreen_output/dscreen_output_plugin.cpp | 43 +++++++++++------ .../dscreen_output/dscreen_output_plugin.h | 15 +----- .../dsoftbus_output_plugin.cpp | 36 +++++++-------- .../dsoftbus_output/dsoftbus_output_plugin.h | 16 +------ .../dsoftbus_output_audio_plugin.cpp | 36 +++++++-------- .../dsoftbus_output_audio_plugin.h | 16 +------ .../common/include/av_trans_constants.h | 2 +- .../common/src/softbus_channel_adapter.cpp | 19 ++------ 24 files changed, 202 insertions(+), 265 deletions(-) diff --git a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp index 058a8e88..9610da89 100644 --- a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp +++ b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.cpp @@ -54,7 +54,7 @@ ErrorCode AVInputFilter::SetParameter(int32_t key, const Any& value) plugin_->SetParameter(static_cast(key), value); } { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); paramsMap_[tag] = value; } return ErrorCode::SUCCESS; @@ -68,7 +68,7 @@ ErrorCode AVInputFilter::GetParameter(int32_t key, Any& value) return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; } { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); value = paramsMap_[tag]; } return ErrorCode::SUCCESS; @@ -108,7 +108,7 @@ ErrorCode AVInputFilter::Prepare() ErrorCode AVInputFilter::Start() { AVTRANS_LOGI("Start"); - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (state_ != FilterState::READY && state_ != FilterState::PAUSED) { AVTRANS_LOGE("The current state is invalid"); return ErrorCode::ERROR_INVALID_STATE; @@ -128,18 +128,18 @@ ErrorCode AVInputFilter::Start() ErrorCode AVInputFilter::Stop() { AVTRANS_LOGI("Stop"); - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); + if (state_ != FilterState::RUNNING && state_ != FilterState::PAUSED) { + AVTRANS_LOGE("The current state is invalid"); + return ErrorCode::ERROR_INVALID_STATE; + } if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } - if (state_ != FilterState::RUNNING && state_ != FilterState::PAUSED) { - AVTRANS_LOGE("The current state is invalid"); - plugin_->Deinit(); - return ErrorCode::SUCCESS; - } if (TranslatePluginStatus(plugin_->Stop()) != ErrorCode::SUCCESS) { AVTRANS_LOGE("The plugin stop fail!"); + return ErrorCode::ERROR_INVALID_OPERATION; } plugin_->Deinit(); plugin_ = nullptr; @@ -150,7 +150,7 @@ ErrorCode AVInputFilter::Stop() ErrorCode AVInputFilter::Pause() { AVTRANS_LOGI("Pause"); - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (state_ == FilterState::PAUSED) { return ErrorCode::SUCCESS; } @@ -174,7 +174,7 @@ ErrorCode AVInputFilter::Pause() ErrorCode AVInputFilter::Resume() { AVTRANS_LOGI("Resume"); - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -193,14 +193,14 @@ void AVInputFilter::InitPorts() AVTRANS_LOGI("InitPorts"); auto outPort = std::make_shared(this); { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); outPorts_.push_back(outPort); } } ErrorCode AVInputFilter::FindPlugin() { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); std::string mime; if (paramsMap_.find(Tag::MIME) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MIME])) { @@ -307,7 +307,7 @@ ErrorCode AVInputFilter::DoConfigure() ErrorCode AVInputFilter::MergeMeta(const Plugin::Meta& meta, Plugin::Meta& targetMeta) { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (!MergeMetaWithCapability(meta, capNegWithDownstream_, targetMeta)) { AVTRANS_LOGE("cannot find available capability of plugin %s", pluginInfo_->name.c_str()); return ErrorCode::ERROR_INVALID_OPERATION; @@ -318,7 +318,7 @@ ErrorCode AVInputFilter::MergeMeta(const Plugin::Meta& meta, Plugin::Meta& targe ErrorCode AVInputFilter::ConfigMeta(Plugin::Meta& meta) { AVTRANS_LOGI("ConfigMeta start!"); - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (paramsMap_.find(Tag::MEDIA_TYPE) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MEDIA_TYPE])) { AVTRANS_LOGE("MEDIA_TYPE in ParamsMap is not exist!"); @@ -459,7 +459,7 @@ ErrorCode AVInputFilter::ConfigDownStream(const Plugin::Meta& meta) { Meta upstreamParams; Meta downstreamParams; - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (outPorts_.size() == 0 || outPorts_[0] == nullptr) { AVTRANS_LOGE("outPorts is empty or invalid!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -474,7 +474,7 @@ ErrorCode AVInputFilter::ConfigDownStream(const Plugin::Meta& meta) ErrorCode AVInputFilter::InitPlugin() { AVTRANS_LOGI("InitPlugin"); - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -505,7 +505,7 @@ ErrorCode AVInputFilter::ConfigPlugin() ErrorCode AVInputFilter::SetPluginParams() { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -525,7 +525,7 @@ ErrorCode AVInputFilter::SetPluginParams() ErrorCode AVInputFilter::PreparePlugin() { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -535,7 +535,7 @@ ErrorCode AVInputFilter::PreparePlugin() ErrorCode AVInputFilter::PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (name_.compare(inPort) != 0) { AVTRANS_LOGE("FilterName is not targetName!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -569,7 +569,7 @@ ErrorCode AVInputFilter::PushData(const std::string& inPort, const AVBufferPtr& ErrorCode AVInputFilter::SetEventCallBack() { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER ; @@ -579,7 +579,7 @@ ErrorCode AVInputFilter::SetEventCallBack() ErrorCode AVInputFilter::SetDataCallBack() { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -590,7 +590,7 @@ ErrorCode AVInputFilter::SetDataCallBack() void AVInputFilter::OnDataCallback(std::shared_ptr buffer) { - std::lock_guard lock(inputFilterMutex_); + OSAL::ScopedLock lock(inputFilterMutex_); if (buffer == nullptr) { AVTRANS_LOGE("buffer is nullptr!"); return; diff --git a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h index a0104db9..8debcd5f 100644 --- a/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h +++ b/av_transport/av_trans_engine/filters/av_transport_input/av_transport_input_filter.h @@ -18,7 +18,6 @@ #include #include -#include #include "foundation/osal/utils/util.h" #include "foundation/utils/constants.h" @@ -76,8 +75,7 @@ private: std::shared_ptr pluginInfo_ {nullptr}; Capability capNegWithDownstream_ {}; std::unordered_map paramsMap_; - std::mutex inputFilterMutex_; - std::mutex paramsMapMutex_; + OSAL::Mutex inputFilterMutex_ {}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp index ba9c21ef..ebae8f18 100644 --- a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp +++ b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.cpp @@ -54,7 +54,7 @@ ErrorCode AVOutputFilter::SetParameter(int32_t key, const Any& value) plugin_->SetParameter(static_cast(key), value); } { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); paramsMap_[tag] = value; } return ErrorCode::SUCCESS; @@ -68,7 +68,7 @@ ErrorCode AVOutputFilter::GetParameter(int32_t key, Any& value) return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; } { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); value = paramsMap_[tag]; } return ErrorCode::SUCCESS; @@ -114,7 +114,7 @@ ErrorCode AVOutputFilter::Prepare() ErrorCode AVOutputFilter::Start() { AVTRANS_LOGI("Start"); - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (state_ != FilterState::READY && state_ != FilterState::PAUSED) { AVTRANS_LOGE("The current state is invalid"); return ErrorCode::ERROR_INVALID_STATE; @@ -134,18 +134,18 @@ ErrorCode AVOutputFilter::Start() ErrorCode AVOutputFilter::Stop() { AVTRANS_LOGI("Stop"); - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); + if (state_ != FilterState::RUNNING) { + AVTRANS_LOGE("The current state is invalid"); + return ErrorCode::ERROR_INVALID_STATE; + } if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } - if (state_ != FilterState::RUNNING) { - AVTRANS_LOGE("The current state is invalid"); - plugin_->Deinit(); - return ErrorCode::SUCCESS; - } if (TranslatePluginStatus(plugin_->Stop()) != ErrorCode::SUCCESS) { AVTRANS_LOGE("The plugin stop fail!"); + return ErrorCode::ERROR_INVALID_OPERATION; } plugin_->Deinit(); plugin_ = nullptr; @@ -170,14 +170,14 @@ void AVOutputFilter::InitPorts() AVTRANS_LOGI("InitPorts"); auto inPort = std::make_shared(this); { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); inPorts_.push_back(inPort); } } ErrorCode AVOutputFilter::FindPlugin() { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); std::string mime; if (paramsMap_.find(Tag::MIME) == paramsMap_.end() || !Plugin::Any::IsSameTypeWith(paramsMap_[Tag::MIME])) { @@ -248,7 +248,7 @@ bool AVOutputFilter::Configure(const std::string& inPort, const std::shared_ptr< ErrorCode AVOutputFilter::InitPlugin() { AVTRANS_LOGI("InitPlugin"); - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -279,7 +279,7 @@ ErrorCode AVOutputFilter::ConfigPlugin() ErrorCode AVOutputFilter::PreparePlugin() { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; @@ -289,7 +289,7 @@ ErrorCode AVOutputFilter::PreparePlugin() ErrorCode AVOutputFilter::PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (buffer == nullptr || plugin_ == nullptr) { AVTRANS_LOGE("buffer or plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_TYPE; @@ -301,7 +301,7 @@ ErrorCode AVOutputFilter::PushData(const std::string& inPort, const AVBufferPtr& ErrorCode AVOutputFilter::SetPluginParams() { - std::lock_guard lock(paramsMapMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -329,7 +329,7 @@ ErrorCode AVOutputFilter::SetPluginParams() ErrorCode AVOutputFilter::SetEventCallBack() { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -340,7 +340,7 @@ ErrorCode AVOutputFilter::SetEventCallBack() ErrorCode AVOutputFilter::SetDataCallBack() { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (plugin_ == nullptr) { AVTRANS_LOGE("plugin is nullptr!"); return ErrorCode::ERROR_INVALID_PARAMETER_VALUE; @@ -351,7 +351,7 @@ ErrorCode AVOutputFilter::SetDataCallBack() void AVOutputFilter::OnDataCallback(std::shared_ptr buffer) { - std::lock_guard lock(outputFilterMutex_); + OSAL::ScopedLock lock(outputFilterMutex_); if (buffer == nullptr) { AVTRANS_LOGE("buffer is nullptr!"); return; diff --git a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h index 6609f898..5390ae79 100644 --- a/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h +++ b/av_transport/av_trans_engine/filters/av_transport_output/av_transport_output_filter.h @@ -18,7 +18,6 @@ #include #include -#include #include "foundation/osal/utils/util.h" #include "foundation/utils/constants.h" @@ -71,8 +70,7 @@ private: std::shared_ptr plugin_ {nullptr}; std::shared_ptr pluginInfo_ {nullptr}; std::unordered_map paramsMap_; - std::mutex outputFilterMutex_; - std::mutex paramsMapMutex_; + OSAL::Mutex outputFilterMutex_ {}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp b/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp index c6ed6794..1485c9f4 100644 --- a/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp +++ b/av_transport/av_trans_engine/filters/test/av_transport_input_filter_test/av_transport_input_filter_test.cpp @@ -140,12 +140,15 @@ HWTEST_F(AvTransportInputFilterTest, Stop_001, testing::ext::TestSize.Level1) std::shared_ptr avInputTest_ = FilterFactory::Instance().CreateFilterWithType(AVINPUT_NAME, FILTERNAME); ErrorCode ret = avInputTest_->Stop(); + EXPECT_EQ(ErrorCode::ERROR_INVALID_STATE, ret); + + avInputTest_->state_ = FilterState::RUNNING; + ret = avInputTest_->Stop(); EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); - avInputTest_->plugin_ = - PluginManager::Instance().CreateGenericPlugin("AVTransDaudioInputPlugin"); + avInputTest_->state_ = FilterState::PAUSED; ret = avInputTest_->Stop(); - EXPECT_EQ(ErrorCode::SUCCESS, ret); + EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); } HWTEST_F(AvTransportInputFilterTest, Stop_002, testing::ext::TestSize.Level1) diff --git a/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp b/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp index ed063b7c..eee47e2f 100644 --- a/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp +++ b/av_transport/av_trans_engine/filters/test/av_transport_output_filter_test/av_transport_output_filter_test.cpp @@ -136,7 +136,7 @@ HWTEST_F(AvTransportOutputFilterTest, Stop_001, testing::ext::TestSize.Level1) std::shared_ptr avOutputTest_ = FilterFactory::Instance().CreateFilterWithType(AVOUTPUT_NAME, FILTERNAME); ErrorCode ret = avOutputTest_->Stop(); - EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); + EXPECT_EQ(ErrorCode::ERROR_INVALID_STATE, ret); } HWTEST_F(AvTransportOutputFilterTest, Stop_002, testing::ext::TestSize.Level1) diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp index dfa0f686..47773c31 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.cpp @@ -75,7 +75,7 @@ Status DaudioInputPlugin::Deinit() Status DaudioInputPlugin::Reset() { AVTRANS_LOGI("Reset enter."); - std::lock_guard lock(tagMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); tagMap_.clear(); frameNumber_.store(0); return Status::OK; @@ -84,6 +84,7 @@ Status DaudioInputPlugin::Reset() Status DaudioInputPlugin::Pause() { AVTRANS_LOGI("Pause enter."); + Media::OSAL::ScopedLock lock(operationMutes_); if ((sharedMemory_.fd > 0) && (sharedMemory_.size > 0) && !sharedMemory_.name.empty()) { ResetSharedMemory(sharedMemory_); } @@ -99,7 +100,7 @@ Status DaudioInputPlugin::Resume() Status DaudioInputPlugin::GetParameter(Tag tag, ValueType &value) { { - std::lock_guard lock(tagMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); auto iter = tagMap_.find(tag); if (iter != tagMap_.end()) { value = iter->second; @@ -111,7 +112,7 @@ Status DaudioInputPlugin::GetParameter(Tag tag, ValueType &value) Status DaudioInputPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(tagMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); tagMap_.insert(std::make_pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { sharedMemory_ = UnmarshalSharedMemory(Media::Plugin::AnyCast(value)); @@ -121,8 +122,9 @@ Status DaudioInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DaudioInputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - std::lock_guard lock(tagMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); TRUE_RETURN_V(buffer == nullptr, Status::ERROR_NULL_POINTER); + if (buffer->IsEmpty()) { AVTRANS_LOGE("bufferData is Empty."); return Status::ERROR_INVALID_PARAMETER; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h index 396d9e8c..3596e470 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/daudio_input/daudio_input_plugin.h @@ -63,8 +63,8 @@ public: private: std::atomic frameNumber_; - std::mutex tagMapMutex_; std::map tagMap_; + OSAL::Mutex operationMutes_ {}; AVTransSharedMemory sharedMemory_ = AVTransSharedMemory{ 0, 0, "" }; }; } // namespace DistributedHardware diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp index d24a8aa9..35e914de 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.cpp @@ -74,7 +74,7 @@ Status DscreenInputPlugin::Deinit() Status DscreenInputPlugin::Reset() { AVTRANS_LOGI("Reset"); - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); paramsMap_.clear(); frameNumber_.store(0); return Status::OK; @@ -94,7 +94,7 @@ Status DscreenInputPlugin::Resume() Status DscreenInputPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -105,7 +105,7 @@ Status DscreenInputPlugin::GetParameter(Tag tag, ValueType &value) Status DscreenInputPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); paramsMap_.insert(std::pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { sharedMemory_ = UnmarshalSharedMemory(Media::Plugin::AnyCast(value)); @@ -115,7 +115,7 @@ Status DscreenInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DscreenInputPlugin::PushData(const std::string& inPort, std::shared_ptr buffer, int32_t offset) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (!buffer || buffer->IsEmpty()) { AVTRANS_LOGE("buffer is nullptr or empty."); return Status::ERROR_NULL_POINTER; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h index 9e00b5dc..f8b8f110 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dscreen_input/dscreen_input_plugin.h @@ -57,7 +57,7 @@ public: private: std::atomic frameNumber_; - std::mutex paramsMapMutex_; + Media::OSAL::Mutex operationMutes_ {}; std::map paramsMap_; AVTransSharedMemory sharedMemory_ = AVTransSharedMemory{ 0, 0, "" }; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp index 61785037..30cc3c2f 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp @@ -74,9 +74,10 @@ DsoftbusInputPlugin::~DsoftbusInputPlugin() Status DsoftbusInputPlugin::Init() { AVTRANS_LOGI("Init"); + Media::OSAL::ScopedLock lock(operationMutes_); dumpFlag_.store(false); reDumpFlag_.store(false); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } @@ -89,7 +90,8 @@ Status DsoftbusInputPlugin::Deinit() Status DsoftbusInputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - if (GetCurrentState() != State::INITIALIZED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -109,16 +111,16 @@ Status DsoftbusInputPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { HandleData(); }); } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; return Status::OK; } Status DsoftbusInputPlugin::Reset() { AVTRANS_LOGI("Reset"); + Media::OSAL::ScopedLock lock(operationMutes_); eventsCb_ = nullptr; dataCb_ = nullptr; - std::lock_guard lock(paramsMapMutex_); paramsMap_.clear(); if (bufferPopTask_) { bufferPopTask_->Stop(); @@ -127,31 +129,33 @@ Status DsoftbusInputPlugin::Reset() DataQueueClear(dataQueue_); SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } Status DsoftbusInputPlugin::Start() { AVTRANS_LOGI("Start"); - if (GetCurrentState() != State::PREPARED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; return Status::OK; } Status DsoftbusInputPlugin::Stop() { AVTRANS_LOGI("Stop"); - if (GetCurrentState() != State::RUNNING) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; bufferPopTask_->Stop(); DataQueueClear(dataQueue_); return Status::OK; @@ -171,7 +175,6 @@ Status DsoftbusInputPlugin::Resume() Status DsoftbusInputPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramsMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -182,7 +185,7 @@ Status DsoftbusInputPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusInputPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -198,7 +201,7 @@ Status DsoftbusInputPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusInputPlugin::SetCallback(Callback *cb) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -210,7 +213,7 @@ Status DsoftbusInputPlugin::SetCallback(Callback *cb) Status DsoftbusInputPlugin::SetDataCallback(AVDataCallback callback) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -298,7 +301,6 @@ void DsoftbusInputPlugin::DataEnqueue(std::shared_ptr &buffer) } else { AVTRANS_LOGE("DumpFlag = false."); } - std::lock_guard lock(dataQueueMtx_); while (dataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("Data queue overflow."); dataQueue_.pop(); @@ -309,13 +311,13 @@ void DsoftbusInputPlugin::DataEnqueue(std::shared_ptr &buffer) void DsoftbusInputPlugin::HandleData() { - while (GetCurrentState() == State::RUNNING) { + while (state_ == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -334,7 +336,6 @@ void DsoftbusInputPlugin::HandleData() void DsoftbusInputPlugin::DataQueueClear(std::queue> &queue) { - std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h index ff5c8605..99147b6a 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h @@ -81,17 +81,6 @@ private: void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: std::atomic dumpFlag_ ; @@ -101,12 +90,11 @@ private: std::string peerDevId_; std::condition_variable dataCond_; std::shared_ptr bufferPopTask_; - std::mutex stateMutex_; std::mutex dataQueueMtx_; - std::mutex paramsMapMutex_; + Media::OSAL::Mutex operationMutes_ {}; std::queue> dataQueue_; std::map paramsMap_; - std::atomic state_ = State::CREATED; + State state_ {State::CREATED}; Callback* eventsCb_ = nullptr; AVDataCallback dataCb_; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp index c13fb2fa..9c6a470c 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp @@ -63,7 +63,8 @@ DsoftbusInputAudioPlugin::~DsoftbusInputAudioPlugin() Status DsoftbusInputAudioPlugin::Init() { AVTRANS_LOGI("Init"); - SetCurrentState(State::INITIALIZED); + Media::OSAL::ScopedLock lock(operationMutes_); + state_ = State::INITIALIZED; return Status::OK; } @@ -76,7 +77,8 @@ Status DsoftbusInputAudioPlugin::Deinit() Status DsoftbusInputAudioPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - if (GetCurrentState() != State::INITIALIZED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -97,19 +99,17 @@ Status DsoftbusInputAudioPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { HandleData(); }); } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; return Status::OK; } Status DsoftbusInputAudioPlugin::Reset() { AVTRANS_LOGI("Reset"); + Media::OSAL::ScopedLock lock(operationMutes_); eventsCb_ = nullptr; dataCb_ = nullptr; - { - std::lock_guard lock(paramsMapMutex_); - paramsMap_.clear(); - } + paramsMap_.clear(); if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -117,33 +117,35 @@ Status DsoftbusInputAudioPlugin::Reset() DataQueueClear(dataQueue_); SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } Status DsoftbusInputAudioPlugin::Start() { AVTRANS_LOGI("Start"); - if (GetCurrentState() != State::PREPARED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; return Status::OK; } Status DsoftbusInputAudioPlugin::Stop() { AVTRANS_LOGI("Stop"); - if (GetCurrentState() != State::RUNNING) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - SetCurrentState(State::PREPARED); - DataQueueClear(dataQueue_); + state_ = State::PREPARED; bufferPopTask_->Stop(); + DataQueueClear(dataQueue_); return Status::OK; } @@ -161,7 +163,6 @@ Status DsoftbusInputAudioPlugin::Resume() Status DsoftbusInputAudioPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramsMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -172,7 +173,7 @@ Status DsoftbusInputAudioPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusInputAudioPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -182,7 +183,7 @@ Status DsoftbusInputAudioPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusInputAudioPlugin::SetCallback(Callback *cb) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -194,7 +195,7 @@ Status DsoftbusInputAudioPlugin::SetCallback(Callback *cb) Status DsoftbusInputAudioPlugin::SetDataCallback(AVDataCallback callback) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -263,7 +264,6 @@ std::shared_ptr DsoftbusInputAudioPlugin::CreateBuffer(uint32_t metaType void DsoftbusInputAudioPlugin::DataEnqueue(std::shared_ptr &buffer) { - std::lock_guard lock(dataQueueMtx_); while (dataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("Data queue overflow."); dataQueue_.pop(); @@ -274,13 +274,13 @@ void DsoftbusInputAudioPlugin::DataEnqueue(std::shared_ptr &buffer) void DsoftbusInputAudioPlugin::HandleData() { - while (GetCurrentState() == State::RUNNING) { + while (state_ == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -299,7 +299,6 @@ void DsoftbusInputAudioPlugin::HandleData() void DsoftbusInputAudioPlugin::DataQueueClear(std::queue> &queue) { - std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h index 9ea9d88c..0828187e 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h @@ -75,17 +75,6 @@ private: void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: std::string ownerName_; @@ -93,13 +82,11 @@ private: std::string peerDevId_; std::condition_variable dataCond_; std::shared_ptr bufferPopTask_; - std::mutex stateMutex_; std::mutex dataQueueMtx_; - std::mutex paramsMapMutex_; Media::OSAL::Mutex operationMutes_ {}; std::queue> dataQueue_; std::map paramsMap_; - std::atomic state_ = State::CREATED; + State state_ {State::CREATED}; Callback* eventsCb_ = nullptr; AVDataCallback dataCb_; }; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp index d8fee901..adbc026c 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.cpp @@ -64,7 +64,8 @@ DaudioOutputPlugin::~DaudioOutputPlugin() Status DaudioOutputPlugin::Init() { AVTRANS_LOGI("Init."); - SetCurrentState(State::INITIALIZED); + OSAL::ScopedLock lock(operationMutes_); + state_ = State::INITIALIZED; return Status::OK; } @@ -94,7 +95,8 @@ void DaudioOutputPlugin::RampleInit(uint32_t channels, uint32_t sampleRate, uint Status DaudioOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - if (GetCurrentState() != State::INITIALIZED) { + OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -124,13 +126,14 @@ Status DaudioOutputPlugin::Prepare() } AVTRANS_LOGI("channels = %d, sampleRate = %d, channelLayout = %d.", channels, sampleRate, channelsLayout); RampleInit(channels, sampleRate, channelsLayout); - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; return Status::OK; } Status DaudioOutputPlugin::Reset() { AVTRANS_LOGI("Reset enter"); + OSAL::ScopedLock lock(operationMutes_); eventcallback_ = nullptr; if (sendPlayTask_) { sendPlayTask_->Stop(); @@ -140,18 +143,14 @@ Status DaudioOutputPlugin::Reset() resample_.reset(); } smIndex_ = 0; - { - std::lock_guard lock(paramsMapMutex_); - paramsMap_.clear(); - } + paramsMap_.clear(); DataQueueClear(outputBuffer_); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } Status DaudioOutputPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramsMapMutex_); auto iter = paramsMap_.find(tag); if (iter != paramsMap_.end()) { value = iter->second; @@ -162,7 +161,7 @@ Status DaudioOutputPlugin::GetParameter(Tag tag, ValueType &value) Status DaudioOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramsMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); paramsMap_.insert(std::make_pair(tag, value)); if (tag == Plugin::Tag::USER_SHARED_MEMORY_FD) { std::unique_lock lock(sharedMemMtx_); @@ -178,24 +177,26 @@ Status DaudioOutputPlugin::SetParameter(Tag tag, const ValueType &value) Status DaudioOutputPlugin::Start() { AVTRANS_LOGI("Start enter"); - if (GetCurrentState() != State::PREPARED) { + OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } DataQueueClear(outputBuffer_); sendPlayTask_->Start(); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; return Status::OK; } Status DaudioOutputPlugin::Stop() { AVTRANS_LOGI("Stop enter"); - if (GetCurrentState() != State::RUNNING) { + OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; sendPlayTask_->Stop(); DataQueueClear(outputBuffer_); return Status::OK; @@ -203,6 +204,7 @@ Status DaudioOutputPlugin::Stop() Status DaudioOutputPlugin::SetCallback(Callback *cb) { + OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallBack failed, cb is nullptr."); return Status::ERROR_NULL_POINTER; @@ -214,6 +216,7 @@ Status DaudioOutputPlugin::SetCallback(Callback *cb) Status DaudioOutputPlugin::SetDataCallback(AVDataCallback callback) { + OSAL::ScopedLock lock(operationMutes_); if (callback == nullptr) { AVTRANS_LOGE("SetCallBack failed, callback is nullptr."); return Status::ERROR_NULL_POINTER; @@ -239,7 +242,8 @@ Status DaudioOutputPlugin::PushData(const std::string &inPort, std::shared_ptr

GetMemory()->GetSize()); } - std::lock_guard lock(dataQueueMtx_); + + OSAL::ScopedLock lock(operationMutes_); while (outputBuffer_.size() >= DATA_QUEUE_MAX_SIZE) { AVTRANS_LOGE("outputBuffer_ queue overflow."); outputBuffer_.pop(); @@ -251,13 +255,13 @@ Status DaudioOutputPlugin::PushData(const std::string &inPort, std::shared_ptr

buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !outputBuffer_.empty(); }); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { return; } if (outputBuffer_.empty()) { @@ -308,7 +312,6 @@ void DaudioOutputPlugin::WriteMasterClockToMemory(const std::shared_ptr> &q) { - std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, q); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h index cb71fded..38ba0e28 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/daudio_output/daudio_output_plugin.h @@ -72,27 +72,16 @@ private: void DataQueueClear(std::queue> &q); void RampleInit(uint32_t channels, uint32_t sampleRate, uint32_t channelLayout); void WriteMasterClockToMemory(const std::shared_ptr &buffer); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: std::condition_variable dataCond_; - std::mutex paramsMapMutex_; std::map paramsMap_; std::mutex dataQueueMtx_; std::queue> outputBuffer_; std::shared_ptr sendPlayTask_; - std::mutex stateMutex_; - std::atomic state_ = State::CREATED; + Media::OSAL::Mutex operationMutes_ {}; + State state_ {State::CREATED}; Callback *eventcallback_ = nullptr; AVDataCallback datacallback_ = nullptr; std::shared_ptr resample_ {nullptr}; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp index 6426d0e3..8ca47a61 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.cpp @@ -59,8 +59,9 @@ DscreenOutputPlugin::~DscreenOutputPlugin() Status DscreenOutputPlugin::Init() { AVTRANS_LOGI("Init."); + Media::OSAL::ScopedLock lock(operationMutes_); InitOutputController(); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } @@ -74,10 +75,10 @@ Status DscreenOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); { - TRUE_RETURN_V_MSG_E((GetCurrentState() != State::INITIALIZED), Status::ERROR_WRONG_STATE, - "The state is wrong."); + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((state_ != State::INITIALIZED), Status::ERROR_WRONG_STATE, "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; } controller_->PrepareControl(); return Status::OK; @@ -87,7 +88,8 @@ Status DscreenOutputPlugin::Reset() { AVTRANS_LOGI("Reset"); { - SetCurrentState(State::INITIALIZED); + Media::OSAL::ScopedLock lock(operationMutes_); + state_ = State::INITIALIZED; eventsCb_ = nullptr; dataCb_ = nullptr; } @@ -101,13 +103,19 @@ Status DscreenOutputPlugin::Reset() Status DscreenOutputPlugin::GetParameter(Tag tag, ValueType &value) { - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + { + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + } return controller_->GetParameter(tag, value); } Status DscreenOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + { + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + } return controller_->SetParameter(tag, value); } @@ -115,9 +123,10 @@ Status DscreenOutputPlugin::Start() { AVTRANS_LOGI("Start"); { - TRUE_RETURN_V_MSG_E((GetCurrentState() != State::PREPARED), Status::ERROR_WRONG_STATE, "The state is wrong."); + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((state_ != State::PREPARED), Status::ERROR_WRONG_STATE, "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; } controller_->StartControl(); return Status::OK; @@ -127,9 +136,10 @@ Status DscreenOutputPlugin::Stop() { AVTRANS_LOGI("Stop"); { - TRUE_RETURN_V_MSG_E((GetCurrentState() != State::RUNNING), Status::ERROR_WRONG_STATE, "The state is wrong."); + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((state_ != State::RUNNING), Status::ERROR_WRONG_STATE, "The state is wrong."); TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; } controller_->StopControl(); return Status::OK; @@ -137,6 +147,7 @@ Status DscreenOutputPlugin::Stop() Status DscreenOutputPlugin::SetCallback(Callback *cb) { + Media::OSAL::ScopedLock lock(operationMutes_); TRUE_RETURN_V_MSG_E((!cb), Status::ERROR_NULL_POINTER, "SetCallback failed, cb is nullptr."); eventsCb_ = cb; AVTRANS_LOGI("SetCallback success."); @@ -145,6 +156,7 @@ Status DscreenOutputPlugin::SetCallback(Callback *cb) Status DscreenOutputPlugin::SetDataCallback(AVDataCallback callback) { + Media::OSAL::ScopedLock lock(operationMutes_); dataCb_ = callback; AVTRANS_LOGI("SetDataCallback success."); return Status::OK; @@ -152,9 +164,12 @@ Status DscreenOutputPlugin::SetDataCallback(AVDataCallback callback) Status DscreenOutputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - TRUE_RETURN_V_MSG_E((buffer == nullptr || buffer->IsEmpty()), Status::ERROR_NULL_POINTER, - "AVBuffer is nullptr."); - TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + { + Media::OSAL::ScopedLock lock(operationMutes_); + TRUE_RETURN_V_MSG_E((buffer == nullptr || buffer->IsEmpty()), Status::ERROR_NULL_POINTER, + "AVBuffer is nullptr."); + TRUE_RETURN_V_MSG_E((!controller_), Status::ERROR_NULL_POINTER, "Controller is nullptr."); + } controller_->PushData(buffer); return Status::OK; } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h index 93b8ba4b..7ff6534c 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dscreen_output/dscreen_output_plugin.h @@ -68,21 +68,10 @@ public: private: void InitOutputController(); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: - std::mutex stateMutex_; - std::atomic state_ = State::CREATED; + Media::OSAL::Mutex operationMutes_ {}; + State state_ {State::CREATED}; Callback *eventsCb_ = nullptr; AVDataCallback dataCb_; std::condition_variable dataCond_; diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp index 6fa15b8f..dae28ff0 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp @@ -71,9 +71,10 @@ DsoftbusOutputPlugin::~DsoftbusOutputPlugin() Status DsoftbusOutputPlugin::Init() { AVTRANS_LOGI("Init Dsoftbus Output Plugin."); + Media::OSAL::ScopedLock lock(operationMutes_); dumpFlag_.store(false); reDumpFlag_.store(false); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } @@ -86,7 +87,8 @@ Status DsoftbusOutputPlugin::Deinit() Status DsoftbusOutputPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - if (GetCurrentState() != State::INITIALIZED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -96,17 +98,15 @@ Status DsoftbusOutputPlugin::Prepare() bufferPopTask_ = std::make_shared("videoBufferQueuePopThread"); bufferPopTask_->RegisterHandler([this] { FeedChannelData(); }); } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; return Status::OK; } Status DsoftbusOutputPlugin::Reset() { AVTRANS_LOGI("Reset"); - { - std::lock_guard lock(paramMapMutex_); - paramsMap_.clear(); - } + Media::OSAL::ScopedLock lock(operationMutes_); + paramsMap_.clear(); if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -114,14 +114,15 @@ Status DsoftbusOutputPlugin::Reset() DataQueueClear(dataQueue_); eventsCb_ = nullptr; SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } Status DsoftbusOutputPlugin::Start() { AVTRANS_LOGI("Dsoftbus Output Plugin start."); - if (GetCurrentState() != State::PREPARED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -132,18 +133,18 @@ Status DsoftbusOutputPlugin::Start() } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; return Status::OK; } Status DsoftbusOutputPlugin::Stop() { AVTRANS_LOGI("Dsoftbus Output Plugin stop."); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; bufferPopTask_->Stop(); DataQueueClear(dataQueue_); CloseSoftbusChannel(); @@ -152,7 +153,6 @@ Status DsoftbusOutputPlugin::Stop() Status DsoftbusOutputPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -163,7 +163,7 @@ Status DsoftbusOutputPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusOutputPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -179,6 +179,7 @@ Status DsoftbusOutputPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusOutputPlugin::SetCallback(Callback *cb) { + Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallback failed, cb is nullptr."); return Status::ERROR_INVALID_OPERATION; @@ -245,7 +246,7 @@ void DsoftbusOutputPlugin::OnStreamReceived(const StreamData *data, const Stream Status DsoftbusOutputPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - std::lock_guard lock(dataQueueMtx_); + Media::OSAL::ScopedLock lock(operationMutes_); if (buffer == nullptr || buffer->IsEmpty()) { AVTRANS_LOGE("Buffer is nullptr."); return Status::ERROR_NULL_POINTER; @@ -272,13 +273,13 @@ Status DsoftbusOutputPlugin::PushData(const std::string &inPort, std::shared_ptr void DsoftbusOutputPlugin::FeedChannelData() { - while (GetCurrentState() == State::RUNNING) { + while (state_ == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -334,7 +335,6 @@ void DsoftbusOutputPlugin::SendDataToSoftbus(std::shared_ptr &buffer) void DsoftbusOutputPlugin::DataQueueClear(std::queue> &queue) { - std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h index c4c15196..9fa03f39 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.h @@ -79,17 +79,6 @@ private: void DataQueueClear(std::queue> &queue); void CloseSoftbusChannel(); void FeedChannelData(); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: std::atomic dumpFlag_ ; @@ -98,13 +87,12 @@ private: std::string sessionName_; std::string peerDevId_; std::condition_variable dataCond_; - std::mutex stateMutex_; std::mutex dataQueueMtx_; - std::mutex paramMapMutex_; + Media::OSAL::Mutex operationMutes_ {}; std::shared_ptr bufferPopTask_; std::queue> dataQueue_; std::map paramsMap_; - std::atomic state_ = State::CREATED; + State state_ {State::CREATED}; Callback* eventsCb_ = nullptr; }; } // namespace DistributedHardware diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp index 8d7b7f62..853b802a 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp @@ -63,7 +63,8 @@ DsoftbusOutputAudioPlugin::~DsoftbusOutputAudioPlugin() Status DsoftbusOutputAudioPlugin::Init() { AVTRANS_LOGI("Init."); - SetCurrentState(State::INITIALIZED); + Media::OSAL::ScopedLock lock(operationMutes_); + state_ = State::INITIALIZED; return Status::OK; } @@ -76,7 +77,8 @@ Status DsoftbusOutputAudioPlugin::Deinit() Status DsoftbusOutputAudioPlugin::Prepare() { AVTRANS_LOGI("Prepare"); - if (GetCurrentState() != State::INITIALIZED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::INITIALIZED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -98,17 +100,15 @@ Status DsoftbusOutputAudioPlugin::Prepare() sampleRate_ = static_cast(Plugin::AnyCast(sampleRateValue)); AVTRANS_LOGI("channels_ = %u, sampleRate_ = %u.", channels_, sampleRate_); - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; return Status::OK; } Status DsoftbusOutputAudioPlugin::Reset() { AVTRANS_LOGI("Reset"); - { - std::lock_guard lock(paramMapMutex_); - paramsMap_.clear(); - } + Media::OSAL::ScopedLock lock(operationMutes_); + paramsMap_.clear(); if (bufferPopTask_) { bufferPopTask_->Stop(); bufferPopTask_.reset(); @@ -117,14 +117,15 @@ Status DsoftbusOutputAudioPlugin::Reset() eventsCb_ = nullptr; SoftbusChannelAdapter::GetInstance().RemoveChannelServer(TransName2PkgName(ownerName_), sessionName_); SoftbusChannelAdapter::GetInstance().UnRegisterChannelListener(sessionName_, peerDevId_); - SetCurrentState(State::INITIALIZED); + state_ = State::INITIALIZED; return Status::OK; } Status DsoftbusOutputAudioPlugin::Start() { AVTRANS_LOGI("Dsoftbus Output Plugin start."); - if (GetCurrentState() != State::PREPARED) { + Media::OSAL::ScopedLock lock(operationMutes_); + if (state_ != State::PREPARED) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } @@ -135,18 +136,18 @@ Status DsoftbusOutputAudioPlugin::Start() } DataQueueClear(dataQueue_); bufferPopTask_->Start(); - SetCurrentState(State::RUNNING); + state_ = State::RUNNING; return Status::OK; } Status DsoftbusOutputAudioPlugin::Stop() { AVTRANS_LOGI("Dsoftbus Output Plugin stop."); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { AVTRANS_LOGE("The state is wrong."); return Status::ERROR_WRONG_STATE; } - SetCurrentState(State::PREPARED); + state_ = State::PREPARED; bufferPopTask_->Stop(); DataQueueClear(dataQueue_); CloseSoftbusChannel(); @@ -155,7 +156,6 @@ Status DsoftbusOutputAudioPlugin::Stop() Status DsoftbusOutputAudioPlugin::GetParameter(Tag tag, ValueType &value) { - std::lock_guard lock(paramMapMutex_); auto res = paramsMap_.find(tag); if (res != paramsMap_.end()) { value = res->second; @@ -166,7 +166,7 @@ Status DsoftbusOutputAudioPlugin::GetParameter(Tag tag, ValueType &value) Status DsoftbusOutputAudioPlugin::SetParameter(Tag tag, const ValueType &value) { - std::lock_guard lock(paramMapMutex_); + Media::OSAL::ScopedLock lock(operationMutes_); if (tag == Tag::MEDIA_DESCRIPTION) { ParseChannelDescription(Plugin::AnyCast(value), ownerName_, peerDevId_); } @@ -176,6 +176,7 @@ Status DsoftbusOutputAudioPlugin::SetParameter(Tag tag, const ValueType &value) Status DsoftbusOutputAudioPlugin::SetCallback(Callback *cb) { + Media::OSAL::ScopedLock lock(operationMutes_); if (cb == nullptr) { AVTRANS_LOGE("SetCallback failed, cb is nullptr."); return Status::ERROR_INVALID_OPERATION; @@ -242,7 +243,7 @@ void DsoftbusOutputAudioPlugin::OnStreamReceived(const StreamData *data, const S Status DsoftbusOutputAudioPlugin::PushData(const std::string &inPort, std::shared_ptr buffer, int32_t offset) { - std::lock_guard lock(dataQueueMtx_); + Media::OSAL::ScopedLock lock(operationMutes_); if (buffer == nullptr || buffer->IsEmpty()) { AVTRANS_LOGE("Buffer is nullptr."); return Status::ERROR_NULL_POINTER; @@ -265,13 +266,13 @@ Status DsoftbusOutputAudioPlugin::PushData(const std::string &inPort, std::share void DsoftbusOutputAudioPlugin::FeedChannelData() { - while (GetCurrentState() == State::RUNNING) { + while (state_ == State::RUNNING) { std::shared_ptr buffer; { std::unique_lock lock(dataQueueMtx_); dataCond_.wait_for(lock, std::chrono::milliseconds(PLUGIN_TASK_WAIT_TIME), [this]() { return !dataQueue_.empty(); }); - if (GetCurrentState() != State::RUNNING) { + if (state_ != State::RUNNING) { return; } if (dataQueue_.empty()) { @@ -328,7 +329,6 @@ void DsoftbusOutputAudioPlugin::SendDataToSoftbus(std::shared_ptr &buffe void DsoftbusOutputAudioPlugin::DataQueueClear(std::queue> &queue) { - std::lock_guard lock(dataQueueMtx_); std::queue> empty; swap(empty, queue); } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h index 02246647..77ee3492 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.h @@ -75,30 +75,18 @@ private: void DataQueueClear(std::queue> &queue); void CloseSoftbusChannel(); void FeedChannelData(); - State GetCurrentState() - { - std::lock_guard lock(stateMutex_); - return state_; - } - - void SetCurrentState(State state) - { - std::lock_guard lock(stateMutex_); - state_ = state; - } private: std::string ownerName_; std::string sessionName_; std::string peerDevId_; std::condition_variable dataCond_; - std::mutex stateMutex_; std::mutex dataQueueMtx_; - std::mutex paramMapMutex_; + Media::OSAL::Mutex operationMutes_ {}; std::shared_ptr bufferPopTask_; std::queue> dataQueue_; std::map paramsMap_; - std::atomic state_ = State::CREATED; + State state_ {State::CREATED}; Callback* eventsCb_ = nullptr; uint32_t sampleRate_ {0}; uint32_t channels_ {0}; diff --git a/av_transport/common/include/av_trans_constants.h b/av_transport/common/include/av_trans_constants.h index 8673cdb1..022a5853 100644 --- a/av_transport/common/include/av_trans_constants.h +++ b/av_transport/common/include/av_trans_constants.h @@ -30,7 +30,7 @@ const int32_t INVALID_SESSION_ID = -1; const int32_t SESSION_WAIT_SECONDS = 5; const int32_t INTERCERT_STRING_LENGTH = 20; const int32_t PLUGIN_RANK = 100; -const int32_t PLUGIN_TASK_WAIT_TIME = 10; +const int32_t PLUGIN_TASK_WAIT_TIME = 20; const uint32_t VIDEO_H264_LEVEL = 32; const uint32_t MAX_DEVICE_ID_LEN = 100; diff --git a/av_transport/common/src/softbus_channel_adapter.cpp b/av_transport/common/src/softbus_channel_adapter.cpp index 6345aa1c..c0f3a180 100644 --- a/av_transport/common/src/softbus_channel_adapter.cpp +++ b/av_transport/common/src/softbus_channel_adapter.cpp @@ -333,13 +333,7 @@ int32_t SoftbusChannelAdapter::SendBytesData(const std::string& sessName, const TRUE_RETURN_V_MSG_E(peerDevId.empty(), ERR_DH_AVT_INVALID_PARAM, "input peerDevId is empty."); TRUE_RETURN_V_MSG_E(data.empty(), ERR_DH_AVT_INVALID_PARAM, "input data string is empty."); - int32_t existSessId = GetSessIdBySessName(sessName, peerDevId); - if (existSessId < 0) { - AVTRANS_LOGI("Can not find sessionId for mySessName:%s, peerDevId:%s.", - sessName.c_str(), GetAnonyString(peerDevId).c_str()); - return ERR_DH_AVT_SEND_DATA_FAILED; - } - int32_t ret = SendBytes(existSessId, data.c_str(), strlen(data.c_str())); + int32_t ret = SendBytes(GetSessIdBySessName(sessName, peerDevId), data.c_str(), strlen(data.c_str())); if (ret != DH_AVT_SUCCESS) { AVTRANS_LOGE("Send bytes data failed ret:%" PRId32, ret); return ERR_DH_AVT_SEND_DATA_FAILED; @@ -356,13 +350,7 @@ int32_t SoftbusChannelAdapter::SendStreamData(const std::string& sessName, const TRUE_RETURN_V_MSG_E(ext == nullptr, ERR_DH_AVT_INVALID_PARAM, "input ext data is nullptr."); StreamFrameInfo frameInfo = {0}; - int32_t existSessId = GetSessIdBySessName(sessName, peerDevId); - if (existSessId < 0) { - AVTRANS_LOGI("Can not find sessionId for mySessName:%s, peerDevId:%s.", - sessName.c_str(), GetAnonyString(peerDevId).c_str()); - return ERR_DH_AVT_SEND_DATA_FAILED; - } - int32_t ret = SendStream(existSessId, data, ext, &frameInfo); + int32_t ret = SendStream(GetSessIdBySessName(sessName, peerDevId), data, ext, &frameInfo); if (ret != DH_AVT_SUCCESS) { AVTRANS_LOGE("Send stream data failed ret:%" PRId32, ret); return ERR_DH_AVT_SEND_DATA_FAILED; @@ -505,6 +493,7 @@ void SoftbusChannelAdapter::OnSoftbusChannelClosed(int32_t sessionId, ShutdownRe for (auto it = devId2SessIdMap_.begin(); it != devId2SessIdMap_.end();) { if (it->second == sessionId) { event.content = GetOwnerFromSessName(it->first); + AVTRANS_LOGI("find sessName is: %s", it->first.c_str()); std::thread(&SoftbusChannelAdapter::SendChannelEvent, this, it->first, event).detach(); it = devId2SessIdMap_.erase(it); } else { @@ -607,7 +596,7 @@ std::string SoftbusChannelAdapter::GetOwnerFromSessName(const std::string &sessN void SoftbusChannelAdapter::SendChannelEvent(const std::string &sessName, const AVTransEvent event) { - AVTRANS_LOGI("SendChannelEvent event.type_%" PRId32, event.type); + AVTRANS_LOGI("SendChannelEvent event.type_%" PRId32", sessName: %s", event.type, sessName.c_str()); pthread_setname_np(pthread_self(), SEND_CHANNEL_EVENT); ISoftbusChannelListener *listener = nullptr; -- Gitee