From edaa4e2ebb65056f466b2ae1b84be0bf55ed1b46 Mon Sep 17 00:00:00 2001 From: ustc_cg Date: Wed, 20 Aug 2025 02:01:23 +0800 Subject: [PATCH] background mute in scene audio crash Signed-off-by: ustc_cg Change-Id: Iaa506f170fe764c0a56b9c2d208b6af6bae2a203 --- .../include/audio_background_manager.h | 2 + .../src/audio_background_manager.cpp | 50 +++++++++++++++++++ .../service_main/src/audio_policy_service.cpp | 1 + .../audio_background_manager_unit_test.cpp | 40 +++++++++++++++ 4 files changed, 93 insertions(+) diff --git a/services/audio_policy/server/infra/appclient/include/audio_background_manager.h b/services/audio_policy/server/infra/appclient/include/audio_background_manager.h index f2da3516e5..09c7475c31 100644 --- a/services/audio_policy/server/infra/appclient/include/audio_background_manager.h +++ b/services/audio_policy/server/infra/appclient/include/audio_background_manager.h @@ -59,6 +59,8 @@ public: int32_t NotifyFreezeStateChange(const std::set &pidList, const bool isFreeze); int32_t ResetAllProxy(); void HandleFreezeStateChange(const int32_t pid, bool isFreeze); + void WriteAppStateChangeSysEvent(int32_t pid, AppState appState, bool isAdd); + void RecoryAppState(); private: AudioBackgroundManager() : streamCollector_(AudioStreamCollector::GetAudioStreamCollector()) {} diff --git a/services/audio_policy/server/infra/appclient/src/audio_background_manager.cpp b/services/audio_policy/server/infra/appclient/src/audio_background_manager.cpp index 66890864a9..a364ef2345 100644 --- a/services/audio_policy/server/infra/appclient/src/audio_background_manager.cpp +++ b/services/audio_policy/server/infra/appclient/src/audio_background_manager.cpp @@ -28,6 +28,7 @@ #include "background_task_listener.h" #include "background_task_subscriber.h" #include "background_task_mgr_helper.h" +#include "media_monitor_manager.h" namespace OHOS { namespace AudioStandard { @@ -133,6 +134,7 @@ void AudioBackgroundManager::NotifyAppStateChange(const int32_t uid, const int32 AUDIO_INFO_LOG("OnBackground with uid: %{public}d", uid); backgroundMuteListener_->OnBackgroundMute(uid); } + WriteAppStateChangeSysEvent(pid, appStatesMap_[pid], true); } void AudioBackgroundManager::NotifyBackgroundTaskStateChange(const int32_t uid, const int32_t pid, bool hasBackgroundTask) @@ -154,6 +156,7 @@ void AudioBackgroundManager::NotifyBackgroundTaskStateChange(const int32_t uid, streamCollector_.HandleBackTaskStateChange(uid, appState.hasSession); } } + WriteAppStateChangeSysEvent(pid, appStatesMap_[pid], true); } int32_t AudioBackgroundManager::NotifySessionStateChange(const int32_t uid, const int32_t pid, const bool hasSession) @@ -173,6 +176,7 @@ int32_t AudioBackgroundManager::NotifySessionStateChange(const int32_t uid, cons appState.hasBackTask, appState.isFreeze); HandleSessionStateChange(uid, pid); } + WriteAppStateChangeSysEvent(pid, appStatesMap_[pid], true); return SUCCESS; } @@ -212,6 +216,7 @@ int32_t AudioBackgroundManager::NotifyFreezeStateChange(const std::set appState.hasBackTask, appState.isFreeze); HandleFreezeStateChange(pid, isFreeze); } + WriteAppStateChangeSysEvent(pid, appStatesMap_[pid], true); } return SUCCESS; } @@ -241,6 +246,24 @@ void AudioBackgroundManager::HandleFreezeStateChange(const int32_t pid, bool isF } } +void AudioBackgroundManager::WriteAppStateChangeSysEvent(int32_t pid, AppState appState, bool isAdd) +{ + AUDIO_INFO_LOG("pid %{public}d is add %{public}d, isFreeze %{public}d, isBack %{public}d, hasSession %{public}d," + "hasBackTask %{public}d, isBinder %{public}d", pid, isAdd, appState.isFreeze, appState.isBack, + appState.hasSession, appState.hasBackTask, appState.isBinder); + std::shared_ptr bean = std::make_shared( + Media::MediaMonitor::ModuleId::AUDIO, Media::MediaMonitor::APP_BACKGROUND_STATE, + Media::MediaMonitor::BEHAVIOR_EVENT); + bean->Add("PID", static_cast(pid)); + bean->Add("IS_FREEZE", static_cast(appState.isFreeze)); + bean->Add("IS_BACK", static_cast(appState.isBack)); + bean->Add("HAS_SESSION", static_cast(appState.hasSession)); + bean->Add("HAS_BACK_TASK", static_cast(appState.hasBackTask)); + bean->Add("IS_BINDER", static_cast(appState.isBinder)); + bean->Add("IS_ADD", isAdd); + Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean); +} + void AudioBackgroundManager::InsertIntoAppStatesMap(int32_t pid, AppState appState) { appStatesMap_.insert(std::make_pair(pid, appState)); @@ -249,10 +272,37 @@ void AudioBackgroundManager::InsertIntoAppStatesMap(int32_t pid, AppState appSta appState.hasBackTask, appState.isFreeze); } +void AudioBackgroundManager::RecoryAppState() +{ + std::lock_guard lock(appStatesMapMutex_); + AUDIO_INFO_LOG("Start recovery app state."); + std::map> appStateMap; + Media::MediaMonitor::MediaMonitorManager::GetInstance().GetAudioAppStateMsg(appStateMap); + if (appStateMap.size() == 0) { + AUDIO_INFO_LOG("the length of appStateMap is 0 and does not need to recory"); + } else { + for (auto &appStateInfo : appStateMap) { + std::shared_ptr info = appStateInfo.second; + AppState appState; + appState.isFreeze = info->isFreeze_; + appState.isBack = info->isBack_; + appState.hasSession = info->hasSession_; + appState.hasBackTask = info->hasBackTask_; + appState.isBinder = info->isBinder_; + appStatesMap_.emplace(appStateInfo.first, appState); + AUDIO_INFO_LOG("pid %{public}d, isFreeze %{public}d, isBack %{public}d," + "hasSession %{public}d, hasBackTask %{public}d, isBinder %{public}d", appStateInfo.first, + appState.isFreeze, appState.isBack, appState.hasSession, appState.hasBackTask, appState.isBinder); + } + } +} + void AudioBackgroundManager::DeleteFromMap(int32_t pid) { if (FindKeyInMap(pid)) { + AppState appState = appStatesMap_[pid]; appStatesMap_.erase(pid); + WriteAppStateChangeSysEvent(pid, appStatesMap_[pid], false); AUDIO_INFO_LOG("Delete pid: %{public}d success.", pid); } else { AUDIO_DEBUG_LOG("Delete pid: %{public}d failed. It does nt exist", pid); diff --git a/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp b/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp index f4ccb2cb60..5fc0f49e59 100644 --- a/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_policy_service.cpp @@ -131,6 +131,7 @@ void AudioPolicyService::CreateRecoveryThread() RecoveryDevicesThread_ = std::make_unique([this] { audioRecoveryDevice_.RecoverExcludedOutputDevices(); audioRecoveryDevice_.RecoveryPreferredDevices(); + audioBackgroundManager_.RecoryAppState(); }); pthread_setname_np(RecoveryDevicesThread_->native_handle(), "APSRecovery"); } diff --git a/services/audio_policy/test/unittest/audio_background_manager_unit_test/src/audio_background_manager_unit_test.cpp b/services/audio_policy/test/unittest/audio_background_manager_unit_test/src/audio_background_manager_unit_test.cpp index cc04c6b9a0..38c39f0981 100644 --- a/services/audio_policy/test/unittest/audio_background_manager_unit_test/src/audio_background_manager_unit_test.cpp +++ b/services/audio_policy/test/unittest/audio_background_manager_unit_test/src/audio_background_manager_unit_test.cpp @@ -1102,5 +1102,45 @@ HWTEST(AudioBackgroundManagerUnitTest, AudioBackgroundManager_044, TestSize.Leve EXPECT_EQ(ret, false); EXPECT_EQ(audioBackgroundManagerTest_->appStatesMap_.empty(), false); } + +/** + * @tc.name : Test WriteAppStateChangeSysEvent API + * @tc.type : FUNC + * @tc.number: AudioBackgroundManager_045 + * @tc.desc : Test WriteAppStateChangeSysEvent + */ +HWTEST(AudioBackgroundManagerUnitTest, AudioBackgroundManager_045, TestSize.Level1) +{ + AudioBackgroundManager* audioBackgroundManagerTest_ = nullptr; + audioBackgroundManagerTest_ = &AudioBackgroundManager::GetInstance(); + ASSERT_TRUE(audioBackgroundManagerTest_ != nullptr); + + int32_t pid = 0; + AppState appState; + appState.isFreeze = 1; + appState.isBack = 1; + appState.hasSession = 1; + appState.hasBackTask = 1; + appState.isBinder = 1; + + audioBackgroundManagerTest_->appStatesMap_.clear(); + EXPECT_EQ(audioBackgroundManagerTest_->appStatesMap_.empty(), true); + + audioBackgroundManagerTest_->WriteAppStateChangeSysEvent(pid, appState, 1); + sleep(1000); + + audioBackgroundManagerTest_->RecoryAppState(); + EXPECT_EQ(appStatesMap_.size(), 1); + + auto it = appStatesMap_.find(pid); + EXPECT_EQ(it != appStatesMap_.end(), true); + + AppState recoryState = it->second; + EXPECT_EQ(appState.isFreeze, recoryState.isFreeze); + EXPECT_EQ(appState.isBack, recoryState.isBack); + EXPECT_EQ(appState.hasSession, recoryState.hasSession); + EXPECT_EQ(appState.hasBackTask, recoryState.hasBackTask); + EXPECT_EQ(appState.isBinder, recoryState.isBinder); +} } // namespace AudioStandard } // namespace OHOS -- Gitee