From 208982bda607ccd341ac1b6e1b729976daba1d73 Mon Sep 17 00:00:00 2001 From: "hezhiqiang19@huawei.com" Date: Mon, 16 Sep 2024 14:41:05 +0000 Subject: [PATCH] lock current input device operation Signed-off-by: hezhiqiang19@huawei.com Change-Id: I0c8e3bc14fa48e2e4e1f27401032df06faaad378 --- .../include/service/audio_policy_service.h | 11 +- .../src/service/audio_policy_service.cpp | 102 +++++++++++------- 2 files changed, 76 insertions(+), 37 deletions(-) diff --git a/services/audio_policy/server/include/service/audio_policy_service.h b/services/audio_policy/server/include/service/audio_policy_service.h index 49a8abe826..6bf5cd770e 100644 --- a/services/audio_policy/server/include/service/audio_policy_service.h +++ b/services/audio_policy/server/include/service/audio_policy_service.h @@ -169,7 +169,7 @@ public: unique_ptr GetActiveOutputDeviceDescriptor() const; - DeviceType GetActiveInputDevice() const; + DeviceType GetActiveInputDevice(); int32_t SetRingerMode(AudioRingerMode ringMode); @@ -1038,6 +1038,14 @@ private: bool IsA2dpOffloadConnected(); + void SetCurrenInputDevice(const AudioDeviceDescriptor &desc); + + AudioDeviceDescriptor GetCurrentInputDevice(); + + DeviceType GetCurrentInputDeviceType(); + + void SetCurrentInputDeviceType(DeviceType deviceType); + void SendA2dpConnectedWhileRunning(const RendererState &rendererState, const uint32_t &sessionId); int32_t ConnectVirtualDevice(sptr &desc); @@ -1090,6 +1098,7 @@ private: std::mutex serviceFlagMutex_; DeviceType effectActiveDevice_ = DEVICE_TYPE_NONE; AudioDeviceDescriptor currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); + std::mutex curInputDevice_; // lock this mutex to operate currentActiveInputDevice_ AudioDeviceDescriptor currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_NONE, DEVICE_ROLE_NONE); std::vector> pnpDeviceList_; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index c044956d99..714c7d26c2 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -2112,13 +2112,38 @@ std::vector> AudioPolicyService::GetPreferredOutputD return deviceList; } +void AudioPolicyService::SetCurrenInputDevice(const AudioDeviceDescriptor &desc) +{ + std::lock_guard lock(curInputDevice_); + currentActiveInputDevice_ = AudioDeviceDescriptor(desc); +} + +AudioDeviceDescriptor AudioPolicyService::GetCurrentInputDevice() +{ + std::lock_guard lock(curInputDevice_); + return currentActiveInputDevice_; +} + + +DeviceType AudioPolicyService::GetCurrentInputDeviceType() +{ + std::lock_guard lock(curInputDevice_); + return currentActiveInputDevice_.deviceType_; +} + +void AudioPolicyService::SetCurrentInputDeviceType(DeviceType deviceType) +{ + std::lock_guard lock(curInputDevice_); + currentActiveInputDevice_.deviceType_ = deviceType; +} + std::vector> AudioPolicyService::GetPreferredInputDeviceDescInner( AudioCapturerInfo &captureInfo, std::string networkId) { std::vector> deviceList = {}; if (captureInfo.sourceType <= SOURCE_TYPE_INVALID || captureInfo.sourceType > SOURCE_TYPE_MAX) { - sptr devDesc = new(std::nothrow) AudioDeviceDescriptor(currentActiveInputDevice_); + sptr devDesc = new(std::nothrow) AudioDeviceDescriptor(GetCurrentInputDevice()); deviceList.push_back(devDesc); return deviceList; } @@ -2392,16 +2417,18 @@ void AudioPolicyService::FetchOutputDeviceWhenNoRunningStream() void AudioPolicyService::FetchInputDeviceWhenNoRunningStream() { unique_ptr desc = audioRouterCenter_.FetchInputDevice(SOURCE_TYPE_MIC, -1); - if (desc->deviceType_ == DEVICE_TYPE_NONE || IsSameDevice(desc, currentActiveInputDevice_)) { + AudioDeviceDescriptor tempDesc = GetCurrentInputDevice(); + if (desc->deviceType_ == DEVICE_TYPE_NONE || IsSameDevice(desc, tempDesc)) { AUDIO_DEBUG_LOG("input device is not change"); return; } - currentActiveInputDevice_ = AudioDeviceDescriptor(*desc); + SetCurrenInputDevice(*desc); if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { activeBTDevice_ = desc->macAddress_; } - AUDIO_DEBUG_LOG("currentActiveInputDevice update %{public}d", currentActiveInputDevice_.deviceType_); - OnPreferredInputDeviceUpdated(currentActiveInputDevice_.deviceType_, currentActiveInputDevice_.networkId_); + DeviceType deviceType = GetCurrentInputDeviceType(); + AUDIO_DEBUG_LOG("currentActiveInputDevice update %{public}d", deviceType); + OnPreferredInputDeviceUpdated(deviceType, ""); // networkId is not used } int32_t AudioPolicyService::ActivateA2dpDevice(unique_ptr &desc, @@ -2849,8 +2876,8 @@ void AudioPolicyService::FetchInputDevice(vectordeviceType_ != preferredDesc->deviceType_) || ((preferredDesc->deviceType_ == DEVICE_TYPE_NONE) && !IsSameDevice(desc, currentActiveDevice_))) { WriteInputRouteChangeEvent(desc, reason); - currentActiveInputDevice_ = AudioDeviceDescriptor(*desc); - AUDIO_DEBUG_LOG("currentActiveInputDevice update %{public}d", currentActiveInputDevice_.deviceType_); + SetCurrenInputDevice(*desc); + AUDIO_DEBUG_LOG("currentActiveInputDevice update %{public}d", GetCurrentInputDeviceType()); isUpdateActiveDevice = true; } needUpdateActiveDevice = false; @@ -2861,7 +2888,7 @@ void AudioPolicyService::FetchInputDevice(vectorsessionId, desc->deviceType_); } if (isUpdateActiveDevice) { - OnPreferredInputDeviceUpdated(currentActiveInputDevice_.deviceType_, currentActiveInputDevice_.networkId_); + OnPreferredInputDeviceUpdated(GetCurrentInputDeviceType(), ""); // networkId is not used. } if (runningStreamCount == 0) { FetchInputDeviceWhenNoRunningStream(); @@ -2879,9 +2906,9 @@ int32_t AudioPolicyService::HandleDeviceChangeForFetchInputDevice(unique_ptrdeviceType_ != DEVICE_TYPE_NONE) && !IsSameDevice(desc, currentActiveDevice_) && desc->deviceType_ != preferredDesc->deviceType_) || ((preferredDesc->deviceType_ == DEVICE_TYPE_NONE) && !IsSameDevice(desc, currentActiveDevice_))) { - currentActiveInputDevice_ = AudioDeviceDescriptor(*desc); - OnPreferredInputDeviceUpdated(currentActiveInputDevice_.deviceType_, currentActiveInputDevice_.networkId_); - UpdateActiveDeviceRoute(currentActiveInputDevice_.deviceType_, DeviceFlag::INPUT_DEVICES_FLAG); + SetCurrenInputDevice(*desc); + OnPreferredInputDeviceUpdated(GetCurrentInputDeviceType(), ""); // networkId is not used. + UpdateActiveDeviceRoute(GetCurrentInputDeviceType(), DeviceFlag::INPUT_DEVICES_FLAG); } return ERR_NEED_NOT_SWITCH_DEVICE; } @@ -2912,14 +2939,14 @@ void AudioPolicyService::BluetoothScoFetch(unique_ptr &de void AudioPolicyService::BluetoothScoDisconectForRecongnition() { + AudioDeviceDescriptor tempDesc = GetCurrentInputDevice(); AUDIO_INFO_LOG("Recongnition scoCategory: %{public}d, deviceType: %{public}d, scoState: %{public}d", - Bluetooth::AudioHfpManager::GetScoCategory(), currentActiveInputDevice_.deviceType_, + Bluetooth::AudioHfpManager::GetScoCategory(), tempDesc.deviceType_, audioDeviceManager_.GetScoState()); - if (currentActiveInputDevice_.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { - int32_t ret = ScoInputDeviceFetchedForRecongnition(false, currentActiveInputDevice_.macAddress_, - currentActiveInputDevice_.connectState_); + if (tempDesc.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { + int32_t ret = ScoInputDeviceFetchedForRecongnition(false, tempDesc.macAddress_, tempDesc.connectState_); CHECK_AND_RETURN_LOG(ret == SUCCESS, "sco [%{public}s] disconnected failed", - GetEncryptAddr(currentActiveInputDevice_.macAddress_).c_str()); + GetEncryptAddr(tempDesc.macAddress_).c_str()); } } @@ -2932,7 +2959,7 @@ void AudioPolicyService::WriteInputRouteChangeEvent(unique_ptrAdd("REASON", static_cast(reason)); bean->Add("TIMESTAMP", static_cast(timeStamp)); - bean->Add("DEVICE_TYPE_BEFORE_CHANGE", currentActiveInputDevice_.deviceType_); + bean->Add("DEVICE_TYPE_BEFORE_CHANGE", GetCurrentInputDeviceType()); bean->Add("DEVICE_TYPE_AFTER_CHANGE", desc->deviceType_); Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean); } @@ -3514,9 +3541,9 @@ unique_ptr AudioPolicyService::GetActiveOutputDeviceDescr return make_unique(currentActiveDevice_); } -DeviceType AudioPolicyService::GetActiveInputDevice() const +DeviceType AudioPolicyService::GetActiveInputDevice() { - return currentActiveInputDevice_.deviceType_; + return GetCurrentInputDeviceType(); } int32_t AudioPolicyService::SetRingerMode(AudioRingerMode ringMode) @@ -3576,7 +3603,7 @@ int32_t AudioPolicyService::SetAudioScene(AudioScene audioScene) result = gsp->SetAudioScene(audioScene, activeOutputDevices, DEVICE_TYPE_USB_ARM_HEADSET, a2dpOffloadFlag_); } else { - result = gsp->SetAudioScene(audioScene, activeOutputDevices, currentActiveInputDevice_.deviceType_, + result = gsp->SetAudioScene(audioScene, activeOutputDevices, GetCurrentInputDeviceType(), a2dpOffloadFlag_); } IPCSkeleton::SetCallingIdentity(identity); @@ -4780,9 +4807,9 @@ void AudioPolicyService::OnServiceConnected(AudioServiceIndex serviceIndex) unique_ptr outDevice = audioDeviceManager_.GetRenderDefaultDevice(); currentActiveDevice_ = AudioDeviceDescriptor(*outDevice); unique_ptr inDevice = audioDeviceManager_.GetCaptureDefaultDevice(); - currentActiveInputDevice_ = AudioDeviceDescriptor(*inDevice); + SetCurrenInputDevice(*inDevice); SetVolumeForSwitchDevice(currentActiveDevice_.deviceType_); - OnPreferredDeviceUpdated(currentActiveDevice_, currentActiveInputDevice_.deviceType_); + OnPreferredDeviceUpdated(currentActiveDevice_, GetCurrentInputDeviceType()); AddEarpiece(); for (auto it = pnpDeviceList_.begin(); it != pnpDeviceList_.end(); ++it) { OnPnpDeviceStatusUpdated((*it).first, (*it).second); @@ -5254,7 +5281,7 @@ int32_t AudioPolicyService::RegisterTracker(AudioMode &mode, AudioStreamChangeIn AddAudioCapturerMicrophoneDescriptor(streamChangeInfo.audioCapturerChangeInfo.sessionId, DEVICE_TYPE_NONE); if (apiVersion > 0 && apiVersion < API_11) { UpdateDeviceInfo(streamChangeInfo.audioCapturerChangeInfo.inputDeviceInfo, - new AudioDeviceDescriptor(currentActiveInputDevice_), false, false); + new AudioDeviceDescriptor(GetCurrentInputDevice()), false, false); } } else if (apiVersion > 0 && apiVersion < API_11) { UpdateDeviceInfo(streamChangeInfo.audioRendererChangeInfo.outputDeviceInfo, @@ -5397,7 +5424,7 @@ int32_t AudioPolicyService::GetCurrentCapturerChangeInfos(vector> inputDevices = GetDevicesInner(INPUT_DEVICES_FLAG); - DeviceType activeDeviceType = currentActiveInputDevice_.deviceType_; + DeviceType activeDeviceType = GetCurrentInputDeviceType(); DeviceRole activeDeviceRole = INPUT_DEVICE; for (sptr desc : inputDevices) { if ((desc->deviceType_ == activeDeviceType) && (desc->deviceRole_ == activeDeviceRole)) { @@ -5671,7 +5698,7 @@ void AudioPolicyService::UpdateTrackerDeviceChange(const vectordeviceRole_ == INPUT_DEVICE) { - activeDevice = currentActiveInputDevice_.deviceType_; + activeDevice = GetCurrentInputDeviceType(); auto itr = std::find_if(connectedDevices_.begin(), connectedDevices_.end(), isInputDevicePresent); if (itr != connectedDevices_.end()) { DeviceInfo inputDevice = {}; @@ -6335,30 +6362,33 @@ int32_t AudioPolicyService::GetPreferredInputStreamTypeInner(SourceType sourceTy void AudioPolicyService::UpdateInputDeviceInfo(DeviceType deviceType) { - AUDIO_DEBUG_LOG("Current input device is %{public}d", currentActiveInputDevice_.deviceType_); + DeviceType curType = GetCurrentInputDeviceType(); + AUDIO_DEBUG_LOG("Current input device is %{public}d", curType); switch (deviceType) { case DEVICE_TYPE_EARPIECE: case DEVICE_TYPE_SPEAKER: case DEVICE_TYPE_BLUETOOTH_A2DP: - currentActiveInputDevice_.deviceType_ = DEVICE_TYPE_MIC; + curType = DEVICE_TYPE_MIC; break; case DEVICE_TYPE_FILE_SINK: - currentActiveInputDevice_.deviceType_ = DEVICE_TYPE_FILE_SOURCE; + curType = DEVICE_TYPE_FILE_SOURCE; break; case DEVICE_TYPE_USB_ARM_HEADSET: - currentActiveInputDevice_.deviceType_ = DEVICE_TYPE_USB_HEADSET; + curType = DEVICE_TYPE_USB_HEADSET; break; case DEVICE_TYPE_WIRED_HEADSET: case DEVICE_TYPE_USB_HEADSET: case DEVICE_TYPE_BLUETOOTH_SCO: - currentActiveInputDevice_.deviceType_ = deviceType; + curType = deviceType; break; default: break; } - AUDIO_DEBUG_LOG("Input device updated to %{public}d", currentActiveInputDevice_.deviceType_); + SetCurrentInputDeviceType(curType); + + AUDIO_INFO_LOG("Input device updated to %{public}d", curType); } int32_t AudioPolicyService::ResumeStreamState() @@ -6485,10 +6515,10 @@ int32_t AudioPolicyService::GetProcessDeviceInfo(const AudioProcessConfig &confi if (config.capturerInfo.sourceType == SOURCE_TYPE_VOICE_COMMUNICATION) { return GetVoipRecordDeviceInfo(config, deviceInfo); } - deviceInfo.deviceId = currentActiveInputDevice_.deviceId_; + deviceInfo.deviceId = GetCurrentInputDevice().deviceId_; deviceInfo.networkId = LOCAL_NETWORK_ID; deviceInfo.deviceRole = INPUT_DEVICE; - deviceInfo.deviceType = currentActiveInputDevice_.deviceType_; + deviceInfo.deviceType = GetCurrentInputDeviceType(); } // todo @@ -7331,7 +7361,7 @@ int32_t AudioPolicyService::OnCapturerSessionAdded(uint64_t sessionID, SessionIn moduleInfo.rate.c_str(), moduleInfo.channels.c_str(), moduleInfo.bufferSize.c_str(), moduleInfo.format.c_str(), moduleInfo.sourceType.c_str()); OpenPortAndInsertIOHandle(moduleInfo.name, moduleInfo); - audioPolicyManager_.SetDeviceActive(currentActiveInputDevice_.deviceType_, + audioPolicyManager_.SetDeviceActive(GetCurrentInputDeviceType(), moduleInfo.name, true, INPUT_DEVICES_FLAG); } sessionWithNormalSourceType_[sessionID] = sessionInfo; @@ -7937,9 +7967,9 @@ float AudioPolicyService::GetMaxAmplitude(const int32_t deviceId) return outputMaxAmplitude; } - if (deviceId == currentActiveInputDevice_.deviceId_) { + if (deviceId == GetCurrentInputDevice().deviceId_) { std::string identity = IPCSkeleton::ResetCallingIdentity(); - float inputMaxAmplitude = gsp->GetMaxAmplitude(false, currentActiveInputDevice_.deviceType_); + float inputMaxAmplitude = gsp->GetMaxAmplitude(false, GetCurrentInputDeviceType()); IPCSkeleton::SetCallingIdentity(identity); return inputMaxAmplitude; } -- Gitee