diff --git a/interfaces/inner_api/native/audiocommon/include/audio_info.h b/interfaces/inner_api/native/audiocommon/include/audio_info.h index 652a277..ac5c0bc 100644 --- a/interfaces/inner_api/native/audiocommon/include/audio_info.h +++ b/interfaces/inner_api/native/audiocommon/include/audio_info.h @@ -33,7 +33,6 @@ constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5; constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20; constexpr int32_t MIN_SERVICE_COUNT = 2; - enum DeviceFlag { /** * Device flag none. diff --git a/services/include/audio_policy/server/service/audio_policy_service.h b/services/include/audio_policy/server/service/audio_policy_service.h index ac69a32..ba10fe5 100644 --- a/services/include/audio_policy/server/service/audio_policy_service.h +++ b/services/include/audio_policy/server/service/audio_policy_service.h @@ -118,6 +118,7 @@ private: void TriggerDeviceChangedCallback(const std::vector> &devChangeDesc, bool connection); bool interruptEnabled_ = true; + int32_t mDefaultDeviceCount = 0; std::bitset serviceFlag_; DeviceType mCurrentActiveDevice = DEVICE_TYPE_NONE; IAudioPolicyInterface& mAudioPolicyManager; diff --git a/services/src/audio_policy/server/service/audio_policy_service.cpp b/services/src/audio_policy/server/service/audio_policy_service.cpp index cef8ad9..b67c978 100644 --- a/services/src/audio_policy/server/service/audio_policy_service.cpp +++ b/services/src/audio_policy/server/service/audio_policy_service.cpp @@ -306,12 +306,17 @@ int32_t AudioPolicyService::SetDeviceActive(InternalDeviceType deviceType, bool int32_t result = SUCCESS; if (!active) { - deviceType = FetchHighPriorityDevice(); + CHECK_AND_RETURN_RET_LOG(deviceType == mCurrentActiveDevice, SUCCESS, "This device is not active"); + if (mConnectedDevices.size() == mDefaultDeviceCount && deviceType == DEVICE_TYPE_SPEAKER) { + deviceType = DEVICE_TYPE_BLUETOOTH_SCO; + } else { + deviceType = FetchHighPriorityDevice(); + } } if (deviceType == mCurrentActiveDevice) { - MEDIA_ERR_LOG("Device already activated %{public}d", mCurrentActiveDevice); - return ERR_INVALID_OPERATION; + MEDIA_ERR_LOG("Device already activated %{public}d. No need to activate again", mCurrentActiveDevice); + return SUCCESS; } if (deviceType == DEVICE_TYPE_SPEAKER) { @@ -493,12 +498,13 @@ void AudioPolicyService::OnServiceConnected(AudioServiceIndex serviceIndex) MEDIA_ERR_LOG("[module_load]::Device failed %{public}d", devType); break; } - // add new device into active device list - sptr audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(devType, - GetDeviceRole(moduleInfo.role)); - mConnectedDevices.insert(mConnectedDevices.begin(), audioDescriptor); } + // add new device into active device list + sptr audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(devType, + GetDeviceRole(moduleInfo.role)); + mConnectedDevices.insert(mConnectedDevices.begin(), audioDescriptor); + mDefaultDeviceCount++; mIOHandles[moduleInfo.name] = ioHandle; } } @@ -561,12 +567,16 @@ AudioIOHandle AudioPolicyService::GetAudioIOHandle(InternalDeviceType deviceType InternalDeviceType AudioPolicyService::GetDeviceType(const std::string &deviceName) { - if (deviceName == "Speaker") - return InternalDeviceType::DEVICE_TYPE_SPEAKER; - if (deviceName == "Built_in_mic") - return InternalDeviceType::DEVICE_TYPE_MIC; + InternalDeviceType devType = InternalDeviceType::DEVICE_TYPE_NONE; + if (deviceName == "Speaker") { + devType = InternalDeviceType::DEVICE_TYPE_SPEAKER; + } else if (deviceName == "Built_in_mic") { + devType = InternalDeviceType::DEVICE_TYPE_MIC; + } else if (deviceName == "fifo_output" || deviceName == "fifo_input") { + devType = DEVICE_TYPE_BLUETOOTH_SCO; + } - return InternalDeviceType::DEVICE_TYPE_NONE; + return devType; } void AudioPolicyService::TriggerDeviceChangedCallback(const vector> &desc, bool isConnected) diff --git a/services/src/audio_policy/server/service/manager/audio_adapter_manager.cpp b/services/src/audio_policy/server/service/manager/audio_adapter_manager.cpp index 1d61287..f99fd30 100644 --- a/services/src/audio_policy/server/service/manager/audio_adapter_manager.cpp +++ b/services/src/audio_policy/server/service/manager/audio_adapter_manager.cpp @@ -158,13 +158,9 @@ AudioIOHandle AudioAdapterManager::OpenAudioPort(const AudioModuleInfo &audioMod if (!audioModuleInfo.fileName.empty() && access(audioModuleInfo.fileName.c_str(), F_OK) == 0) { int32_t ret = std::remove(audioModuleInfo.fileName.c_str()); if (ret) { - MEDIA_ERR_LOG("[AudioAdapterManager] Error Removing file: %{public}s Failed! ret val: %{public}d", - audioModuleInfo.fileName.c_str(), ret); + MEDIA_ERR_LOG("Removing pipe file failed!. Ret val: %{public}d", ret); return ERR_OPERATION_FAILED; } - } else { - MEDIA_ERR_LOG("[AudioAdapterManager] Error audioModuleInfo.fileName is null! or file not exists"); - return ERR_OPERATION_FAILED; } } diff --git a/test/unittest/manager_test/src/audio_manager_unit_test.cpp b/test/unittest/manager_test/src/audio_manager_unit_test.cpp index 8dd69ef..10f3752 100644 --- a/test/unittest/manager_test/src/audio_manager_unit_test.cpp +++ b/test/unittest/manager_test/src/audio_manager_unit_test.cpp @@ -82,23 +82,61 @@ HWTEST(AudioManagerUnitTest, GetConnectedDevicesList_003, TestSize.Level0) /** * @tc.name : Test SetDeviceActive API * @tc.number: SetDeviceActive_001 -* @tc.desc : Test SetDeviceActive interface. Deactivate default speaker should not be success +* @tc.desc : Test SetDeviceActive interface. Activate bluetooth sco device by deactivating speaker */ HWTEST(AudioManagerUnitTest, SetDeviceActive_001, TestSize.Level0) { - auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::SPEAKER, false); - EXPECT_NE(SUCCESS, ret); + auto isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER); + EXPECT_TRUE(isActive); + + auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::BLUETOOTH_SCO, true); + EXPECT_EQ(SUCCESS, ret); + + isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::BLUETOOTH_SCO); + EXPECT_TRUE(isActive); } /** -* @tc.name : Test IsDeviceActive API -* @tc.number: IsDeviceActive_001 -* @tc.desc : Test IsDeviceActive interface. Returns whether the mentioned device is currently active +* @tc.name : Test SetDeviceActive API +* @tc.number: SetDeviceActive_002 +* @tc.desc : Test SetDeviceActive interface. Activate speaker device */ -HWTEST(AudioManagerUnitTest, IsDeviceActive_001, TestSize.Level0) +HWTEST(AudioManagerUnitTest, SetDeviceActive_002, TestSize.Level0) { - auto isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER); + auto isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::BLUETOOTH_SCO); EXPECT_TRUE(isActive); + + auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::SPEAKER, true); + EXPECT_EQ(SUCCESS, ret); + + isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER); + EXPECT_TRUE(isActive); +} + +/** +* @tc.name : Test SetDeviceActive API +* @tc.number: SetDeviceActive_003 +* @tc.desc : Test SetDeviceActive interface. Switch between SPEAKER and BT SCO automatically +*/ +HWTEST(AudioManagerUnitTest, SetDeviceActive_003, TestSize.Level0) +{ + auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::SPEAKER, false); + EXPECT_EQ(SUCCESS, ret); + + auto isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER); + EXPECT_FALSE(isActive); + + isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::BLUETOOTH_SCO); + EXPECT_TRUE(isActive); + + ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::BLUETOOTH_SCO, false); + EXPECT_EQ(SUCCESS, ret); + + isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER); + EXPECT_TRUE(isActive); + + isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::BLUETOOTH_SCO); + EXPECT_FALSE(isActive); } } // namespace AudioStandard } // namespace OHOS