diff --git a/services/audio_policy/server/domain/device/include/audio_usr_select_manager.h b/services/audio_policy/server/domain/device/include/audio_usr_select_manager.h index 9a66160355c3a9eedfed3b3542db4fdd0e57c192..a1b1c00e53932f362bde3826a5c32d33620db441 100644 --- a/services/audio_policy/server/domain/device/include/audio_usr_select_manager.h +++ b/services/audio_policy/server/domain/device/include/audio_usr_select_manager.h @@ -31,6 +31,23 @@ namespace OHOS { namespace AudioStandard { typedef std::shared_ptr AudioDevicePtr; +enum UpdateType { + START_CLIENT, + APP_SELECT, + SYSTEM_SELECT, + APP_PREFER, + STOP_CLIENT, + RELEASE_CLIENT, +}; + +struct RecordDeviceInfo { + int32_t uid_; + SourceType sourceType_{SourceType::SOURCE_TYPE_INVALID}; + AudioDevicePtr selectedDevice_{std::make_shared()}; + AudioDevicePtr activeSelectedDevice_{std::make_shared()}; + std::list>> appPreferredDevices_{}; +}; + class AudioUsrSelectManager { public: static AudioUsrSelectManager& GetAudioUsrSelectManager() @@ -42,29 +59,27 @@ public: // Set media render device selected by the user bool SelectInputDeviceByUid(const std::shared_ptr &deviceDescriptor, int32_t uid); std::shared_ptr GetSelectedInputDeviceByUid(int32_t uid); - void ClearSelectedInputDeviceByUid(int32_t uid); void PreferBluetoothAndNearlinkRecordByUid(int32_t uid, BluetoothAndNearlinkPreferredRecordCategory category); BluetoothAndNearlinkPreferredRecordCategory GetPreferBluetoothAndNearlinkRecordByUid(int32_t uid); - void EnableSelectInputDevice(const std::vector> &inputStreamDescs); - void DisableSelectInputDevice(); - std::shared_ptr GetCapturerDevice(int32_t uid, SourceType sourceType); + std::shared_ptr GetCapturerDevice(int32_t uid, int32_t sessionId, SourceType sourceType); + void UpdateRecordDeviceInfo(UpdateType updateType, int32_t uid, int32_t sessionId, SourceType sourceType, + const std::shared_ptr &desc); private: AudioUsrSelectManager() {}; ~AudioUsrSelectManager() {}; std::list>::iterator findDevice(int32_t uid); - int32_t GetRealUid(const std::shared_ptr &streamDesc); std::shared_ptr JudgeFinalSelectDevice(const std::shared_ptr &desc, SourceType sourceType, BluetoothAndNearlinkPreferredRecordCategory category); std::shared_ptr GetPreferDevice(); + int32_t GetIdFromRecordDeviceInfoList(int32_t uid); std::list> selectedDevices_; std::list isPreferredBluetoothAndNearlinkRecord_; std::unordered_map categoryMap_; - AudioDevicePtr capturerDevice_ = nullptr; - bool isEnabled_ = false; std::mutex mutex_; + std::vector recordDeviceInfoList_; }; } // namespace AudioStandard diff --git a/services/audio_policy/server/domain/device/src/audio_device_manager.cpp b/services/audio_policy/server/domain/device/src/audio_device_manager.cpp index 76aef69e3b33f6bcc2e4135b4809e6acb5e9f20d..b59cbe82e67a48cab2c9c3b3f7cffd20f7140b3e 100644 --- a/services/audio_policy/server/domain/device/src/audio_device_manager.cpp +++ b/services/audio_policy/server/domain/device/src/audio_device_manager.cpp @@ -806,14 +806,14 @@ void AudioDeviceManager::AddAvailableDevicesByUsage(const AudioDeviceUsage usage std::shared_ptr AudioDeviceManager::GetExistedDevice( const std::shared_ptr &device) { - CHECK_AND_RETURN_RET_LOG(device != nullptr, nullptr, "device is nullptr"); + CHECK_AND_RETURN_RET_LOG(device->deviceType_ != DEVICE_TYPE_NONE, device, "device is nullptr"); std::lock_guard currentActiveDevicesLock(currentActiveDevicesMutex_); for (const auto &dev : connectedDevices_) { if (dev->IsSameDeviceInfo(*device)) { return make_shared(dev); } } - return nullptr; + return make_shared(); } bool AudioDeviceManager::IsExistedDevice(const std::shared_ptr &device, @@ -1558,7 +1558,7 @@ int32_t AudioDeviceManager::RemoveSelectedInputDevice(const uint32_t sessionID) shared_ptr AudioDeviceManager::GetSelectedCaptureDevice(const uint32_t sessionID) { - shared_ptr devDesc = nullptr; + shared_ptr devDesc = make_shared();; if (sessionID == 0 || !selectedInputDeviceInfo_.count(sessionID)) { return devDesc; } diff --git a/services/audio_policy/server/domain/device/src/audio_usr_select_manager.cpp b/services/audio_policy/server/domain/device/src/audio_usr_select_manager.cpp index ad0507d54c0611dda8abf09032ae20553004f949..d46f8170de42fe2aa1ee6df84160998ee7960893 100644 --- a/services/audio_policy/server/domain/device/src/audio_usr_select_manager.cpp +++ b/services/audio_policy/server/domain/device/src/audio_usr_select_manager.cpp @@ -47,13 +47,7 @@ bool AudioUsrSelectManager::SelectInputDeviceByUid(const std::shared_ptrmacAddress_).c_str()); } - std::pair devicePair{uid, desc}; - auto it = findDevice(uid); - if (it != selectedDevices_.end()) { - selectedDevices_.erase(it); - } - selectedDevices_.push_front(devicePair); - + UpdateRecordDeviceInfo(UpdateType::APP_SELECT, uid, -1, SourceType::SOURCE_TYPE_INVALID, desc); return !isVirtualDevice; } @@ -61,21 +55,8 @@ std::shared_ptr AudioUsrSelectManager::GetSelectedInputDe { std::lock_guard lock(mutex_); auto invalidDesc = std::make_shared(AudioDeviceDescriptor::DEVICE_INFO); - auto it = findDevice(uid); - if (it == selectedDevices_.end()) { - AUDIO_ERR_LOG("AudioUsrSelectManager::GetSelectedInputDeviceByUid no selected device. uid:%{public}d", uid); - return invalidDesc; - } - return it->second; -} - -void AudioUsrSelectManager::ClearSelectedInputDeviceByUid(int32_t uid) -{ - std::lock_guard lock(mutex_); - auto it = findDevice(uid); - if (it != selectedDevices_.end()) { - selectedDevices_.erase(it); - } + int32_t index = GetIdFromRecordDeviceInfoList(uid); + return index > -1 ? recordDeviceInfoList_[index].selectedDevice_ : invalidDesc; } void AudioUsrSelectManager::PreferBluetoothAndNearlinkRecordByUid(int32_t uid, @@ -110,98 +91,24 @@ BluetoothAndNearlinkPreferredRecordCategory AudioUsrSelectManager::GetPreferBlue return BluetoothAndNearlinkPreferredRecordCategory::PREFERRED_NONE; } -void AudioUsrSelectManager::EnableSelectInputDevice( - const std::vector> &inputStreamDescs) +std::shared_ptr AudioUsrSelectManager::GetCapturerDevice( + int32_t uid, int32_t sessionId, SourceType sourceType) { std::lock_guard lock(mutex_); - isEnabled_ = true; - std::unordered_map uidMap; - for (size_t i = 0; i < inputStreamDescs.size(); ++i) { - auto &streamDesc = inputStreamDescs[i]; - if (streamDesc->streamStatus_ != STREAM_STATUS_STARTED) { - continue; + std::shared_ptr capturerDevice = std::make_shared(); + CHECK_AND_RETURN_RET(!recordDeviceInfoList_.empty(), capturerDevice); + std::shared_ptr appPreferredDevice = std::make_shared(); + for (auto preferredDevice : recordDeviceInfoList_[0].appPreferredDevices_) { + auto it = preferredDevice.find(sessionId); + if (it != preferredDevice.end()) { + appPreferredDevice = it->second; } - - int32_t uid = GetRealUid(streamDesc); - uidMap[uid] = i; - } - AUDIO_INFO_LOG("running stream size: %{public}zu", uidMap.size()); - - // use selected rules first - for (const auto &device : selectedDevices_) { - CHECK_AND_CONTINUE(uidMap.find(device.first) != uidMap.end()); - auto desc = device.second; - - int32_t index = uidMap[device.first]; - auto &streamDesc = inputStreamDescs[index]; - capturerDevice_ = JudgeFinalSelectDevice(desc, streamDesc->capturerInfo_.sourceType, - categoryMap_[device.first]); - CHECK_AND_CONTINUE(capturerDevice_ != nullptr); - return; } - AUDIO_WARNING_LOG("no select device, use prefer settings"); - if (isPreferredBluetoothAndNearlinkRecord_.size() == 0) { - AUDIO_WARNING_LOG("AudioUsrSelectManager::EnableSelectInputDevice no prefer settings"); - return; - } - - // then use prefer rules - // According to the device connection time, obtain the most recently connected Bluetooth/Nearlink device - auto preferDevice = GetPreferDevice(); - CHECK_AND_RETURN(preferDevice != nullptr); - for (const int32_t uid : isPreferredBluetoothAndNearlinkRecord_) { - CHECK_AND_CONTINUE_LOG(uidMap.find(uid) != uidMap.end(), "uid: %{public}d has no running stream", uid); - CHECK_AND_CONTINUE_LOG(categoryMap_[uid] != PREFERRED_NONE, "uid: %{public}d prefer setting 0", uid); - - int32_t index = uidMap[uid]; - auto &streamDesc = inputStreamDescs[index]; - capturerDevice_ = JudgeFinalSelectDevice(preferDevice, streamDesc->capturerInfo_.sourceType, categoryMap_[uid]); - CHECK_AND_CONTINUE(capturerDevice_ != nullptr); - break; - } -} - -void AudioUsrSelectManager::DisableSelectInputDevice() -{ - std::lock_guard lock(mutex_); - isEnabled_ = false; - capturerDevice_ = nullptr; -} - -std::shared_ptr AudioUsrSelectManager::GetCapturerDevice(int32_t uid, SourceType sourceType) -{ - std::lock_guard lock(mutex_); - // If marked, directly return the selection result. - CHECK_AND_RETURN_RET_LOG(!isEnabled_, capturerDevice_, "select enabled, return capturerDevice_"); - AUDIO_INFO_LOG("select disabled, search selected device. uid: %{public}d, sourceType: %{public}d", - uid, sourceType); - - std::shared_ptr capturerDevice = nullptr; - // If not marked, first find the selection of the current UID - auto deviceIt = findDevice(uid); - if (deviceIt != selectedDevices_.end()) { - // Based on the sourceType, determine the final input device - auto desc = JudgeFinalSelectDevice(deviceIt->second, sourceType, categoryMap_[uid]); - CHECK_AND_RETURN_RET_LOG(desc == nullptr, desc, - "AudioUsrSelectManager::GetCapturerDevice has selected device."); - AUDIO_WARNING_LOG("selected device no longer available"); - } - - AUDIO_WARNING_LOG("no selected device, use prefer settings"); - // If the current UID has no selection, then apply the preference setting - auto it = - std::find(isPreferredBluetoothAndNearlinkRecord_.begin(), isPreferredBluetoothAndNearlinkRecord_.end(), uid); - // If the current UID has no preference setting - CHECK_AND_RETURN_RET_LOG(it != isPreferredBluetoothAndNearlinkRecord_.end(), nullptr, - "AudioUsrSelectManager::GetCapturerDevice no prefer data"); - CHECK_AND_RETURN_RET_LOG(categoryMap_[uid] != PREFERRED_NONE, nullptr, - "AudioUsrSelectManager::GetCapturerDevice prefer setting 0"); - // According to the device connection time, obtain the most recently connected Bluetooth/Nearlink device - auto preferDevice = GetPreferDevice(); - CHECK_AND_RETURN_RET(preferDevice != nullptr, nullptr); - return JudgeFinalSelectDevice(preferDevice, sourceType, categoryMap_[uid]); + capturerDevice = recordDeviceInfoList_[0].activeSelectedDevice_->deviceType_ == DEVICE_TYPE_NONE ? + appPreferredDevice : recordDeviceInfoList_[0].activeSelectedDevice_; + return JudgeFinalSelectDevice(capturerDevice, sourceType, categoryMap_[uid]); } std::list>::iterator AudioUsrSelectManager::findDevice(int32_t uid) @@ -212,14 +119,6 @@ std::list>::iterator AudioUsrSelectManager::f }); } -int32_t AudioUsrSelectManager::GetRealUid(const std::shared_ptr &streamDesc) -{ - if (streamDesc->callerUid_ == MEDIA_SERVICE_UID) { - return streamDesc->appInfo_.appUid; - } - return streamDesc->callerUid_; -} - std::shared_ptr AudioUsrSelectManager::JudgeFinalSelectDevice( const std::shared_ptr &desc, SourceType sourceType, BluetoothAndNearlinkPreferredRecordCategory category) @@ -228,7 +127,7 @@ std::shared_ptr AudioUsrSelectManager::JudgeFinalSelectDe bool isConnected = AudioDeviceManager::GetAudioDeviceManager().IsConnectedDevices(desc); if (desc->deviceType_ != DEVICE_TYPE_BLUETOOTH_SCO || category == PREFERRED_LOW_LATENCY) { - return isConnected ? desc : nullptr; + return isConnected ? desc : std::make_shared(); } // 如果是直播或录像且设备为sco,需要判断是否存在可用的高清设备 @@ -239,7 +138,7 @@ std::shared_ptr AudioUsrSelectManager::JudgeFinalSelectDe CHECK_AND_RETURN_RET(!isA2dpinConnected, a2dpin); } - return isConnected ? desc : nullptr; + return isConnected ? desc : std::make_shared(); } std::shared_ptr AudioUsrSelectManager::GetPreferDevice() @@ -256,5 +155,110 @@ std::shared_ptr AudioUsrSelectManager::GetPreferDevice() }); return audioDeviceDescriptors.back(); } + +int32_t AudioUsrSelectManager::GetIdFromRecordDeviceInfoList(int32_t uid) +{ + int32_t index = 0; + for (auto recordDeviceInfo : recordDeviceInfoList_) { + if (recordDeviceInfo.uid_ == uid) { + return index; + } + index++; + } + return -1; +} + +void AudioUsrSelectManager::UpdateRecordDeviceInfo(UpdateType updateType, int32_t uid, int32_t sessionId, + SourceType sourceType, const std::shared_ptr &desc) +{ + int32_t index = GetIdFromRecordDeviceInfoList(uid); + AUDIO_INFO_LOG("UpdateRecordDeviceInfo updateType:%{public}d", updateType); + switch (updateType) { + case UpdateType::START_CLIENT: + if (index < 0) { + RecordDeviceInfo recordDeviceInfo { + .uid_ = uid, + .sourceType_ = sourceType, + .activeSelectedDevice_ = + AudioStateManager::GetAudioStateManager().GetPreferredRecordCaptureDevice(), + .appPreferredDevices_ = {{{sessionId, desc}}} + }; + recordDeviceInfoList_.emplace(recordDeviceInfoList_.begin(), recordDeviceInfo); + } else { + for (auto &recordInfo : recordDeviceInfoList_) { + if (recordInfo.uid_ == uid) { + recordInfo.sourceType_ = sourceType; + } + } + } + break; + case UpdateType::APP_SELECT: + if (index < 0) { + if (desc->deviceType_ != DEVICE_TYPE_NONE) { + RecordDeviceInfo recordDeviceInfo { + .uid_ = uid, + .sourceType_ = SourceType::SOURCE_TYPE_INVALID, + .selectedDevice_ = desc, + .activeSelectedDevice_ = std::make_shared() + }; + recordDeviceInfoList_.push_back(recordDeviceInfo); + } + } else { + recordDeviceInfoList_[index].selectedDevice_ = desc; + recordDeviceInfoList_[index].activeSelectedDevice_ = desc; + if (recordDeviceInfoList_[index].sourceType_ != SourceType::SOURCE_TYPE_INVALID) { + std::rotate(recordDeviceInfoList_.begin(), recordDeviceInfoList_.begin() + index, + recordDeviceInfoList_.begin() + index + 1); + } + } + break; + case UpdateType::SYSTEM_SELECT: + if (desc->deviceType_ != DEVICE_TYPE_NONE) { + for (auto &recordDeviceInfo : recordDeviceInfoList_) { + recordDeviceInfo.activeSelectedDevice_ = desc; + } + } else { + for (auto &recordDeviceInfo : recordDeviceInfoList_) { + recordDeviceInfo.activeSelectedDevice_ = recordDeviceInfo.selectedDevice_; + } + } + break; + case UpdateType::APP_PREFER: + if (index < 0) { + RecordDeviceInfo recordDeviceInfo { + .uid_ = uid, + .appPreferredDevices_ = {{{sessionId, desc}}} + }; + recordDeviceInfoList_.push_back(recordDeviceInfo); + } else { + for (auto &appPreferredDevices : recordDeviceInfoList_[index].appPreferredDevices_) { + auto it = appPreferredDevices.find(sessionId); + if (it != appPreferredDevices.end()) { + it->second = desc; + } + } + } + break; + case UpdateType::STOP_CLIENT: + if (index >= 0) { + if (recordDeviceInfoList_[index].appPreferredDevices_.size() > 0 || + recordDeviceInfoList_[index].selectedDevice_->deviceType_ != DEVICE_TYPE_NONE) { + recordDeviceInfoList_[index].sourceType_ = SourceType::SOURCE_TYPE_INVALID; + std::rotate(recordDeviceInfoList_.begin() + index, recordDeviceInfoList_.begin() + index + 1, + recordDeviceInfoList_.end()); + } else { + recordDeviceInfoList_.erase(recordDeviceInfoList_.begin() + index); + } + } + break; + case UpdateType::RELEASE_CLIENT: + if (index >= 0) { + recordDeviceInfoList_.erase(recordDeviceInfoList_.begin() + index); + } + break; + default: + return; + } +} } // namespace AudioStandard } // namespace OHOS diff --git a/services/audio_policy/server/domain/pipe/include/audio_pipe_manager.h b/services/audio_policy/server/domain/pipe/include/audio_pipe_manager.h index e4417897203dfbbd866dffecf116ef80381eb70f..577c59167ac4bc9d47e557107b4d36ce7a6c6a1f 100644 --- a/services/audio_policy/server/domain/pipe/include/audio_pipe_manager.h +++ b/services/audio_policy/server/domain/pipe/include/audio_pipe_manager.h @@ -67,6 +67,7 @@ public: void UpdateRendererPipeInfos(std::vector> &pipeInfos); void UpdateCapturerPipeInfos(std::vector> &pipeInfos); uint32_t PcmOffloadSessionCount(); + int32_t GetClientUidBySessionId(uint32_t sessionId); void Dump(std::string &dumpString); bool IsModemCommunicationIdExist(); diff --git a/services/audio_policy/server/domain/pipe/src/audio_pipe_manager.cpp b/services/audio_policy/server/domain/pipe/src/audio_pipe_manager.cpp index 4814d5a90959c75ada1d00a76591cc86bff0dd97..3a6a3f41cdcecae45484e97bc0b79874db91f13c 100644 --- a/services/audio_policy/server/domain/pipe/src/audio_pipe_manager.cpp +++ b/services/audio_policy/server/domain/pipe/src/audio_pipe_manager.cpp @@ -22,6 +22,7 @@ namespace OHOS { namespace AudioStandard { const uint32_t FIRST_SESSIONID = 100000; +const int32_t MEDIA_SERVICE_UID = 1013; constexpr uint32_t MAX_VALID_SESSIONID = UINT32_MAX - FIRST_SESSIONID; AudioPipeManager::AudioPipeManager() { @@ -308,6 +309,24 @@ std::shared_ptr AudioPipeManager::GetStreamDescByIdInner( return nullptr; } +int32_t AudioPipeManager::GetClientUidBySessionId(uint32_t sessionId) +{ + std::shared_lock pLock(pipeListLock_); + for (auto &pipeInfo : curPipeList_) { + CHECK_AND_CONTINUE_LOG(pipeInfo != nullptr, "pipeInfo is nullptr"); + for (auto &desc : pipeInfo->streamDescriptors_) { + CHECK_AND_CONTINUE_LOG(desc != nullptr, "desc is nullptr"); + if (desc->sessionId_ == sessionId) { + if (desc->callerUid_ == MEDIA_SERVICE_UID) { + return desc->appInfo_.appUid; + } + return desc->callerUid_; + } + } + } + return -1; +} + int32_t AudioPipeManager::GetStreamCount(const std::string adapterName, const uint32_t routeFlag) { std::shared_lock pLock(pipeListLock_); diff --git a/services/audio_policy/server/domain/router/app_select_router.cpp b/services/audio_policy/server/domain/router/app_select_router.cpp index 853b7bc98fc4587e1ff6030e81c2fd51a4bee2a2..4ad0c6fc5415e3b2c3d602207f398b3a48807d0f 100644 --- a/services/audio_policy/server/domain/router/app_select_router.cpp +++ b/services/audio_policy/server/domain/router/app_select_router.cpp @@ -43,9 +43,8 @@ shared_ptr AppSelectRouter::GetCallCaptureDevice(SourceTy { shared_ptr device = AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionID); - if (device == nullptr) { - device = AudioAffinityManager::GetAudioAffinityManager().GetCapturerDevice(clientUID); - } + CHECK_AND_RETURN_RET(device == nullptr || device->deviceType_ == DEVICE_TYPE_NONE, device); + device = AudioAffinityManager::GetAudioAffinityManager().GetCapturerDevice(clientUID); return device; } @@ -61,12 +60,10 @@ shared_ptr AppSelectRouter::GetRecordCaptureDevice(Source { shared_ptr device = AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionID); - if (device == nullptr) { - device = AudioUsrSelectManager::GetAudioUsrSelectManager().GetCapturerDevice(clientUID, sourceType); - } - if (device == nullptr) { - device = AudioAffinityManager::GetAudioAffinityManager().GetCapturerDevice(clientUID); - } + CHECK_AND_RETURN_RET(device == nullptr || device->deviceType_ == DEVICE_TYPE_NONE, device); + device = AudioUsrSelectManager::GetAudioUsrSelectManager().GetCapturerDevice(clientUID, sessionID, sourceType); + CHECK_AND_RETURN_RET(device == nullptr || device->deviceType_ == DEVICE_TYPE_NONE, device); + device = AudioAffinityManager::GetAudioAffinityManager().GetCapturerDevice(clientUID); return device; } diff --git a/services/audio_policy/server/infra/datashare/src/audio_policy_utils.cpp b/services/audio_policy/server/infra/datashare/src/audio_policy_utils.cpp index 3c4c7f74e2fc2677ea16be8a8f5e8b75154710fc..203a01b486548e82a219144ed3b886d04456a19d 100644 --- a/services/audio_policy/server/infra/datashare/src/audio_policy_utils.cpp +++ b/services/audio_policy/server/infra/datashare/src/audio_policy_utils.cpp @@ -142,6 +142,8 @@ int32_t AudioPolicyUtils::SetPreferredDevice(const PreferredType preferredType, break; case AUDIO_RECORD_CAPTURE: audioStateManager_.SetPreferredRecordCaptureDevice(desc); + AudioUsrSelectManager::GetAudioUsrSelectManager().UpdateRecordDeviceInfo(UpdateType::APP_PREFER, uid, -1, + SourceType::SOURCE_TYPE_INVALID, desc); break; case AUDIO_RING_RENDER: case AUDIO_TONE_RENDER: diff --git a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp index ddeb3bf03694c1c7e7a9d5d956346f65532b4564..30a80e44e2c3a08f0dc65ea64c6c6bda812b4e77 100644 --- a/services/audio_policy/server/service/service_main/src/audio_core_service.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_core_service.cpp @@ -517,6 +517,9 @@ int32_t AudioCoreService::StartClient(uint32_t sessionId) } else { int32_t inputRet = ActivateInputDevice(streamDesc); CHECK_AND_RETURN_RET_LOG(inputRet == SUCCESS, inputRet, "Activate input device failed"); + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::START_CLIENT, GetRealUid(streamDesc), + streamDesc->sessionId_, streamDesc->capturerInfo_.sourceType, + AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionId)); CheckAndSetCurrentInputDevice(deviceDesc); audioActiveDevice_.UpdateActiveDeviceRoute( streamDesc->newDeviceDescs_[0]->deviceType_, DeviceFlag::INPUT_DEVICES_FLAG, @@ -533,12 +536,24 @@ int32_t AudioCoreService::StartClient(uint32_t sessionId) int32_t AudioCoreService::PauseClient(uint32_t sessionId) { pipeManager_->PauseClient(sessionId); + std::shared_ptr streamDesc = pipeManager_->GetStreamDescById(sessionId); + if (streamDesc != nullptr && streamDesc->audioMode_ == AUDIO_MODE_RECORD) { + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::STOP_CLIENT, GetRealUid(streamDesc), + streamDesc->sessionId_, streamDesc->capturerInfo_.sourceType, + AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionId)); + } return SUCCESS; } int32_t AudioCoreService::StopClient(uint32_t sessionId) { pipeManager_->StopClient(sessionId); + std::shared_ptr streamDesc = pipeManager_->GetStreamDescById(sessionId); + if (streamDesc != nullptr && streamDesc->audioMode_ == AUDIO_MODE_RECORD) { + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::STOP_CLIENT, GetRealUid(streamDesc), + streamDesc->sessionId_, streamDesc->capturerInfo_.sourceType, + AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionId)); + } return SUCCESS; } @@ -552,6 +567,12 @@ int32_t AudioCoreService::ReleaseClient(uint32_t sessionId, SessionOperationMsg pipeManager_->RemoveModemCommunicationId(sessionId); return SUCCESS; } + std::shared_ptr streamDesc = pipeManager_->GetStreamDescById(sessionId); + if (streamDesc != nullptr && streamDesc->audioMode_ == AUDIO_MODE_RECORD) { + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::RELEASE_CLIENT, GetRealUid(streamDesc), + streamDesc->sessionId_, streamDesc->capturerInfo_.sourceType, + AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionId)); + } pipeManager_->RemoveClient(sessionId); audioOffloadStream_.UnsetOffloadStatus(sessionId); RemoveUnusedPipe(); @@ -621,6 +642,9 @@ int32_t AudioCoreService::SetInputDevice(const DeviceType deviceType, const uint const SourceType sourceType, bool isRunning) { int32_t ret = audioDeviceManager_.SetInputDevice(deviceType, sessionID, sourceType, isRunning); + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::APP_PREFER, + pipeManager_->GetClientUidBySessionId(sessionID), sessionID, sourceType, + AudioDeviceManager::GetAudioDeviceManager().GetSelectedCaptureDevice(sessionID)); if (ret == NEED_TO_FETCH) { FetchInputDeviceAndRoute("SetInputDevice"); return SUCCESS; @@ -1118,7 +1142,8 @@ std::shared_ptr AudioCoreService::GetSelectedInputDeviceB int32_t AudioCoreService::ClearSelectedInputDeviceByUid(int32_t uid) { - audioUsrSelectManager_.ClearSelectedInputDeviceByUid(uid); + audioUsrSelectManager_.UpdateRecordDeviceInfo(UpdateType::APP_SELECT, uid, -1, SourceType::SOURCE_TYPE_INVALID, + std::make_shared()); return SUCCESS; } @@ -1353,8 +1378,6 @@ int32_t AudioCoreService::FetchInputDeviceAndRoute(std::string caller, const Aud return HandleFetchInputWhenNoRunningStream(); } - AudioUsrSelectManager::GetAudioUsrSelectManager().EnableSelectInputDevice(inputStreamDescs); - bool needUpdateActiveDevice = true; bool isUpdateActiveDevice = false; for (auto streamDesc : inputStreamDescs) { @@ -1383,8 +1406,6 @@ int32_t AudioCoreService::FetchInputDeviceAndRoute(std::string caller, const Aud } } - AudioUsrSelectManager::GetAudioUsrSelectManager().DisableSelectInputDevice(); - int32_t ret = FetchCapturerPipesAndExecute(inputStreamDescs); if (isUpdateActiveDevice) { // networkId is not used. diff --git a/services/audio_policy/test/unittest/audio_usr_select_manager_unit_test/src/audio_usr_select_manager_unit_test.cpp b/services/audio_policy/test/unittest/audio_usr_select_manager_unit_test/src/audio_usr_select_manager_unit_test.cpp index 9f961eeb5c871e857706ee6c708f75d9b2fce1ff..2cd29a02ecbec1e20809245c6e4c989b2c433339 100644 --- a/services/audio_policy/test/unittest/audio_usr_select_manager_unit_test/src/audio_usr_select_manager_unit_test.cpp +++ b/services/audio_policy/test/unittest/audio_usr_select_manager_unit_test/src/audio_usr_select_manager_unit_test.cpp @@ -123,23 +123,6 @@ HWTEST_F(AudioUsrSelectManagerUnitTest, AudioUsrSelectManager_GetSelectedInputDe EXPECT_EQ(desc->deviceType_, DeviceType(0)); } -/** -* @tc.name : Test AudioUsrSelectManager. -* @tc.number: AudioUsrSelectManager_ClearSelectedInputDeviceByUid_001 -* @tc.desc : Test ClearSelectedInputDeviceByUid interface. -*/ -HWTEST_F(AudioUsrSelectManagerUnitTest, AudioUsrSelectManager_ClearSelectedInputDeviceByUid_001, TestSize.Level1) -{ - AudioUsrSelectManager &audioUsrSelectManager = AudioUsrSelectManager::GetAudioUsrSelectManager(); - int32_t uid = 321; - audioUsrSelectManager.ClearSelectedInputDeviceByUid(uid); - EXPECT_EQ(audioUsrSelectManager.selectedDevices_.size(), 1); - - uid = 123; - audioUsrSelectManager.ClearSelectedInputDeviceByUid(uid); - EXPECT_EQ(audioUsrSelectManager.selectedDevices_.size(), 0); -} - /** * @tc.name : Test AudioUsrSelectManager. * @tc.number: AudioUsrSelectManager_PreferBluetoothAndNearlinkRecordByUid_001 @@ -171,25 +154,6 @@ HWTEST_F(AudioUsrSelectManagerUnitTest, AudioUsrSelectManager_GetPreferBluetooth EXPECT_EQ(audioUsrSelectManager.GetPreferBluetoothAndNearlinkRecordByUid(321), PREFERRED_NONE); } -/** -* @tc.name : Test AudioUsrSelectManager. -* @tc.number: AudioUsrSelectManager_GetRealUid_001 -* @tc.desc : Test GetRealUid interface. -*/ -HWTEST_F(AudioUsrSelectManagerUnitTest, AudioUsrSelectManager_GetRealUid_001, TestSize.Level1) -{ - AudioUsrSelectManager &audioUsrSelectManager = AudioUsrSelectManager::GetAudioUsrSelectManager(); - - auto stream = std::make_shared(); - stream->streamStatus_ = STREAM_STATUS_STARTED; - stream->callerUid_ = 123; - EXPECT_EQ(audioUsrSelectManager.GetRealUid(stream), 123); - - stream->callerUid_ = 1013; - stream->appInfo_.appUid = 321; - EXPECT_EQ(audioUsrSelectManager.GetRealUid(stream), 321); -} - /** * @tc.name : Test AudioUsrSelectManager. * @tc.number: AudioUsrSelectManager_JudgeFinalSelectDevice_001