From 929fda2c4c297c27830b9a9b265feaeea8732f3b Mon Sep 17 00:00:00 2001 From: magekkkk Date: Mon, 28 Oct 2024 12:05:18 +0000 Subject: [PATCH 1/2] adapt to audio device ptr change Signed-off-by: magekkkk --- .../session/test/unittest/avsession_dumper_test.cpp | 10 ++++++---- services/session/server/avsession_service.cpp | 6 +++--- services/session/server/avsession_service.h | 2 +- .../server/test/avsession_service_supplement_test.cpp | 8 ++++---- .../session/server/test/avsession_service_test.cpp | 4 ++-- .../avsessionservice_fuzzer.cpp | 4 ++-- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/frameworks/native/session/test/unittest/avsession_dumper_test.cpp b/frameworks/native/session/test/unittest/avsession_dumper_test.cpp index 5ce9ee9d..253495e2 100644 --- a/frameworks/native/session/test/unittest/avsession_dumper_test.cpp +++ b/frameworks/native/session/test/unittest/avsession_dumper_test.cpp @@ -536,8 +536,9 @@ HWTEST_F(AVSessionDumperTest, SetDeviceInfo001, TestSize.Level1) */ HWTEST_F(AVSessionDumperTest, GetAudioDescriptorByDeviceId001, TestSize.Level1) { - std::vector> castAudioDescriptors; - sptr des = new AudioStandard::AudioDeviceDescriptor(); + std::vector> castAudioDescriptors; + std::shared_ptr des = + std::make_shared(); AudioStandard::AudioDeviceDescriptor res; des->deviceId_ = 12; castAudioDescriptors.push_back(des); @@ -553,8 +554,9 @@ HWTEST_F(AVSessionDumperTest, GetAudioDescriptorByDeviceId001, TestSize.Level1) */ HWTEST_F(AVSessionDumperTest, GetAudioDescriptorByDeviceId002, TestSize.Level1) { - std::vector> castAudioDescriptors; - sptr des = new AudioStandard::AudioDeviceDescriptor(); + std::vector> castAudioDescriptors; + std::shared_ptr des = + std::make_shared(); AudioStandard::AudioDeviceDescriptor res; des->deviceId_ = 11; castAudioDescriptors.push_back(des); diff --git a/services/session/server/avsession_service.cpp b/services/session/server/avsession_service.cpp index dd74670b..23c390d6 100644 --- a/services/session/server/avsession_service.cpp +++ b/services/session/server/avsession_service.cpp @@ -2227,7 +2227,7 @@ void AVSessionService::SetDeviceInfo(const std::vectorSetOutputDevice(outputDeviceInfo); } -bool AVSessionService::GetAudioDescriptorByDeviceId(const std::vector>& +bool AVSessionService::GetAudioDescriptorByDeviceId(const std::vector>& descriptors, const std::string& deviceId, AudioStandard::AudioDeviceDescriptor& audioDescriptor) { @@ -2271,8 +2271,8 @@ int32_t AVSessionService::SelectOutputDevice(const int32_t uid, const AudioDevic audioFilter->rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC; audioFilter->rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA; - std::vector> audioDescriptor; - auto audioDeviceDescriptor = new(std::nothrow) AudioDeviceDescriptor(descriptor); + std::vector> audioDescriptor; + auto audioDeviceDescriptor = std::make_shared(descriptor); CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor != nullptr, ERR_NO_MEMORY, "audioDeviceDescriptor is nullptr"); audioDescriptor.push_back(audioDeviceDescriptor); SLOGI("select the device %{public}s role is %{public}d, networkId is %{public}.6s", diff --git a/services/session/server/avsession_service.h b/services/session/server/avsession_service.h index 6fbf4065..151c5255 100644 --- a/services/session/server/avsession_service.h +++ b/services/session/server/avsession_service.h @@ -383,7 +383,7 @@ private: int32_t GetAudioDescriptor(const std::string deviceId, std::vector& audioDeviceDescriptors); - bool GetAudioDescriptorByDeviceId(const std::vector>& descriptors, + bool GetAudioDescriptorByDeviceId(const std::vector>& descriptors, const std::string& deviceId, AudioStandard::AudioDeviceDescriptor& audioDescriptor); diff --git a/services/session/server/test/avsession_service_supplement_test.cpp b/services/session/server/test/avsession_service_supplement_test.cpp index 609a6c11..23b34650 100644 --- a/services/session/server/test/avsession_service_supplement_test.cpp +++ b/services/session/server/test/avsession_service_supplement_test.cpp @@ -135,8 +135,8 @@ static HWTEST_F(AVSessionServiceSupplementTest, SetDeviceInfo001, TestSize.Level static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId001, TestSize.Level1) { SLOGI("GetAudioDescriptorByDeviceId001 begin!"); - std::vector> descriptors; - sptr descriptor = new AudioStandard::AudioDeviceDescriptor(); + std::vector> descriptors; + std::shared_ptr descriptor = std::make_shared(); descriptors.push_back(descriptor); std::string deviceId = "0"; AudioStandard::AudioDeviceDescriptor audioDescriptor; @@ -153,8 +153,8 @@ static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId001, static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId002, TestSize.Level1) { SLOGI("GetAudioDescriptorByDeviceId002 begin!"); - std::vector> descriptors; - sptr descriptor = new AudioStandard::AudioDeviceDescriptor(); + std::vector> descriptors; + std::shared_ptr descriptor = std::make_shared(); descriptors.push_back(descriptor); std::string deviceId = "1"; AudioStandard::AudioDeviceDescriptor audioDescriptor; diff --git a/services/session/server/test/avsession_service_test.cpp b/services/session/server/test/avsession_service_test.cpp index e2cbaee5..a359a1f6 100644 --- a/services/session/server/test/avsession_service_test.cpp +++ b/services/session/server/test/avsession_service_test.cpp @@ -865,8 +865,8 @@ static HWTEST_F(AVSessionServiceTest, HandleDeviceChange002, TestSize.Level1) { SLOGI("HandleDeviceChange002 begin!"); DeviceChangeAction deviceChange; - std::vector> audioDeviceDescriptors; - OHOS::sptr descriptor = new(std::nothrow) AudioDeviceDescriptor(); + std::vector> audioDeviceDescriptors; + OHOS::std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; deviceChange.type = static_cast(0); deviceChange.flag = static_cast(0); diff --git a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp index 5de0e2e0..004729ca 100644 --- a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp +++ b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp @@ -723,8 +723,8 @@ void HandleDeviceChange002(const uint8_t* data, size_t size) { SLOGI("HandleDeviceChange002 begin!"); DeviceChangeAction deviceChange; - std::vector> audioDeviceDescriptors; - OHOS::sptr descriptor = new(std::nothrow) AudioDeviceDescriptor(); + std::vector> audioDeviceDescriptors; + OHOS::std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; deviceChange.type = static_cast(0); deviceChange.flag = static_cast(0); -- Gitee From 2ff0a41d2b2e1a49f7e6ae298a713ad33936718c Mon Sep 17 00:00:00 2001 From: magekkkk Date: Mon, 28 Oct 2024 12:10:12 +0000 Subject: [PATCH 2/2] fix format Signed-off-by: magekkkk --- services/session/adapter/audio_adapter.cpp | 15 +++++++++------ services/session/adapter/audio_adapter.h | 5 +++-- services/session/server/audio_device_manager.cpp | 4 ++-- services/session/server/avsession_service.cpp | 11 +++++++---- services/session/server/avsession_service.h | 9 +++++---- .../session/server/test/audio_adapter_test.cpp | 2 +- .../test/avsession_service_supplement_test.cpp | 6 ++++-- .../server/test/avsession_service_test.cpp | 8 ++++---- .../avsessionservice_fuzzer.cpp | 8 ++++---- 9 files changed, 39 insertions(+), 29 deletions(-) diff --git a/services/session/adapter/audio_adapter.cpp b/services/session/adapter/audio_adapter.cpp index 7e77a171..20a3dc28 100644 --- a/services/session/adapter/audio_adapter.cpp +++ b/services/session/adapter/audio_adapter.cpp @@ -42,8 +42,8 @@ AudioAdapter::~AudioAdapter() void AudioAdapter::Init() { SLOGI("register audio renderer event listener"); - auto ret = AudioStandard::AudioStreamManager::GetInstance()->RegisterAudioRendererEventListener(getpid(), - shared_from_this()); + auto ret = AudioStandard::AudioStreamManager::GetInstance()->RegisterAudioRendererEventListener( + getpid(), shared_from_this()); if (ret != 0) { SLOGE("register audio renderer event listener failed"); } @@ -69,7 +69,8 @@ void AudioAdapter::AddDeviceChangeListener(const PreferOutputDeviceChangeListene int32_t AudioAdapter::MuteAudioStream(int32_t uid, int32_t pid) { std::vector> audioRendererChangeInfo; - auto ret = AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); + auto ret = + AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); if (ret != 0) { SLOGE("get renderer state failed"); return AVSESSION_ERROR; @@ -97,7 +98,8 @@ int32_t AudioAdapter::MuteAudioStream(int32_t uid, int32_t pid) int32_t AudioAdapter::UnMuteAudioStream(int32_t uid) { std::vector> audioRendererChangeInfo; - auto ret = AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); + auto ret = + AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); if (ret != 0) { SLOGE("get renderer state failed"); return AVSESSION_ERROR; @@ -168,7 +170,7 @@ void AudioAdapter::OnDeviceChange(const DeviceChangeAction& deviceChangeAction) { } -void AudioAdapter::OnPreferredOutputDeviceUpdated(const std::vector> &desc) +void AudioAdapter::OnPreferredOutputDeviceUpdated(const std::vector> &desc) { for (const auto& listener : deviceChangeListeners_) { if (listener) { @@ -180,7 +182,8 @@ void AudioAdapter::OnPreferredOutputDeviceUpdated(const std::vector> audioRendererChangeInfo; - auto ret = AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); + auto ret = + AudioStandard::AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(audioRendererChangeInfo); if (ret != 0) { SLOGE("get renderer state failed"); return false; diff --git a/services/session/adapter/audio_adapter.h b/services/session/adapter/audio_adapter.h index 47199f7e..e792cdcd 100644 --- a/services/session/adapter/audio_adapter.h +++ b/services/session/adapter/audio_adapter.h @@ -34,7 +34,8 @@ class AudioAdapter : public AudioStandard::AudioRendererStateChangeCallback, public: using StateListener = std::function; using DeviceChangeListener = std::function; - using PreferOutputDeviceChangeListener = std::function> &desc)>; + using PreferOutputDeviceChangeListener = + std::function> &desc)>; static AudioAdapter& GetInstance(); AudioAdapter(); @@ -60,7 +61,7 @@ public: void OnDeviceChange(const DeviceChangeAction& deviceChangeAction) override; - void OnPreferredOutputDeviceUpdated(const std::vector> &desc) override; + void OnPreferredOutputDeviceUpdated(const std::vector> &desc) override; bool GetRendererRunning(int32_t uid); diff --git a/services/session/server/audio_device_manager.cpp b/services/session/server/audio_device_manager.cpp index d2a329ad..a909b83b 100644 --- a/services/session/server/audio_device_manager.cpp +++ b/services/session/server/audio_device_manager.cpp @@ -27,7 +27,7 @@ bool AudioDeviceManager::GetVehicleA2dpConnectState() { AudioStandard::AudioSystemManager *audioSystemManager = AudioStandard::AudioSystemManager::GetInstance(); - std::vector> devices = + std::vector> devices = audioSystemManager->GetActiveOutputDeviceDescriptors(); bool isConnected = false; for (auto& device : devices) { @@ -100,7 +100,7 @@ void DeviceChangeCallback::OnDeviceChange(const AudioStandard::DeviceChangeActio return; } if (AudioStandard::DeviceChangeType::DISCONNECT == deviceChangeAction.type) { - std::vector> deviceDescriptors = + std::vector> deviceDescriptors = deviceChangeAction.deviceDescriptors; for (auto &device : deviceDescriptors) { if (device != nullptr && diff --git a/services/session/server/avsession_service.cpp b/services/session/server/avsession_service.cpp index 23c390d6..1ac14f2e 100644 --- a/services/session/server/avsession_service.cpp +++ b/services/session/server/avsession_service.cpp @@ -716,7 +716,8 @@ void AVSessionService::InitAudio() AudioAdapter::GetInstance().AddStreamRendererStateListener([this] (const AudioRendererChangeInfos& infos) { OutputDeviceChangeListener(infos); }); - AudioAdapter::GetInstance().AddDeviceChangeListener([this] (const std::vector> &desc) { + AudioAdapter::GetInstance().AddDeviceChangeListener( + [this] (const std::vector> &desc) { HandleDeviceChange(desc); }); } @@ -2227,8 +2228,9 @@ void AVSessionService::SetDeviceInfo(const std::vectorSetOutputDevice(outputDeviceInfo); } -bool AVSessionService::GetAudioDescriptorByDeviceId(const std::vector>& - descriptors, const std::string& deviceId, +bool AVSessionService::GetAudioDescriptorByDeviceId( + const std::vector>& descriptors, + const std::string& deviceId, AudioStandard::AudioDeviceDescriptor& audioDescriptor) { for (const auto& descriptor : descriptors) { @@ -2896,7 +2898,8 @@ void AVSessionService::NotifyDeviceChange() // LCOV_EXCL_STOP // LCOV_EXCL_START -void AVSessionService::HandleDeviceChange(const std::vector> &desc) +void AVSessionService::HandleDeviceChange( + const std::vector> &desc) { for (auto &audioDeviceDescriptor : desc) { if (audioDeviceDescriptor->deviceType_ == AudioStandard::DEVICE_TYPE_WIRED_HEADSET || diff --git a/services/session/server/avsession_service.h b/services/session/server/avsession_service.h index 151c5255..a42fa6c1 100644 --- a/services/session/server/avsession_service.h +++ b/services/session/server/avsession_service.h @@ -341,7 +341,7 @@ private: void HandleFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info); - void HandleDeviceChange(const std::vector> &desc); + void HandleDeviceChange(const std::vector> &desc); __attribute__((no_sanitize("cfi"))) std::shared_ptr GetService( const std::string& deviceId); @@ -383,9 +383,10 @@ private: int32_t GetAudioDescriptor(const std::string deviceId, std::vector& audioDeviceDescriptors); - bool GetAudioDescriptorByDeviceId(const std::vector>& descriptors, - const std::string& deviceId, - AudioStandard::AudioDeviceDescriptor& audioDescriptor); + bool GetAudioDescriptorByDeviceId( + const std::vector>& descriptors, + const std::string& deviceId, + AudioStandard::AudioDeviceDescriptor& audioDescriptor); void GetDeviceInfo(const sptr& session, const std::vector& descriptors, diff --git a/services/session/server/test/audio_adapter_test.cpp b/services/session/server/test/audio_adapter_test.cpp index 951f364b..f98d7e17 100644 --- a/services/session/server/test/audio_adapter_test.cpp +++ b/services/session/server/test/audio_adapter_test.cpp @@ -248,7 +248,7 @@ static HWTEST(AudioAdapterTest, OnDeviceChange001, TestSize.Level1) SLOGD("PauseAudioStream001 begin!"); bool ret = false; AudioAdapter::GetInstance().AddDeviceChangeListener( - [&ret] (const std::vector> &desc) { + [&ret] (const std::vector> &desc) { ret = desc.empty(); }); AudioAdapter::GetInstance().AddDeviceChangeListener(nullptr); diff --git a/services/session/server/test/avsession_service_supplement_test.cpp b/services/session/server/test/avsession_service_supplement_test.cpp index 23b34650..23a261d8 100644 --- a/services/session/server/test/avsession_service_supplement_test.cpp +++ b/services/session/server/test/avsession_service_supplement_test.cpp @@ -136,7 +136,8 @@ static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId001, { SLOGI("GetAudioDescriptorByDeviceId001 begin!"); std::vector> descriptors; - std::shared_ptr descriptor = std::make_shared(); + std::shared_ptr descriptor = + std::make_shared(); descriptors.push_back(descriptor); std::string deviceId = "0"; AudioStandard::AudioDeviceDescriptor audioDescriptor; @@ -154,7 +155,8 @@ static HWTEST_F(AVSessionServiceSupplementTest, GetAudioDescriptorByDeviceId002, { SLOGI("GetAudioDescriptorByDeviceId002 begin!"); std::vector> descriptors; - std::shared_ptr descriptor = std::make_shared(); + std::shared_ptr descriptor = + std::make_shared(); descriptors.push_back(descriptor); std::string deviceId = "1"; AudioStandard::AudioDeviceDescriptor audioDescriptor; diff --git a/services/session/server/test/avsession_service_test.cpp b/services/session/server/test/avsession_service_test.cpp index a359a1f6..a0ef3962 100644 --- a/services/session/server/test/avsession_service_test.cpp +++ b/services/session/server/test/avsession_service_test.cpp @@ -850,8 +850,8 @@ static HWTEST_F(AVSessionServiceTest, HandleDeviceChange001, TestSize.Level1) { SLOGI("HandleDeviceChange001 begin!"); DeviceChangeAction deviceChange; - std::vector> desc; - OHOS::sptr descriptor = new(std::nothrow) AudioDeviceDescriptor(); + std::vector> desc; + std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; deviceChange.type = static_cast(0); deviceChange.flag = static_cast(0); @@ -865,8 +865,8 @@ static HWTEST_F(AVSessionServiceTest, HandleDeviceChange002, TestSize.Level1) { SLOGI("HandleDeviceChange002 begin!"); DeviceChangeAction deviceChange; - std::vector> audioDeviceDescriptors; - OHOS::std::shared_ptr descriptor = std::make_shared(); + std::vector> audioDeviceDescriptors; + std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; deviceChange.type = static_cast(0); deviceChange.flag = static_cast(0); diff --git a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp index 004729ca..e40a981d 100644 --- a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp +++ b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp @@ -711,8 +711,8 @@ void ConvertKeyCodeToCommand001(const uint8_t* data, size_t size) void HandleDeviceChange001(const uint8_t* data, size_t size) { SLOGI("HandleDeviceChange001 begin!"); - std::vector> audioDeviceDescriptors; - OHOS::sptr descriptor = new(std::nothrow) AudioDeviceDescriptor(); + std::vector> audioDeviceDescriptors; + std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; audioDeviceDescriptors.push_back(descriptor); avsessionService_->HandleDeviceChange(audioDeviceDescriptors); @@ -723,8 +723,8 @@ void HandleDeviceChange002(const uint8_t* data, size_t size) { SLOGI("HandleDeviceChange002 begin!"); DeviceChangeAction deviceChange; - std::vector> audioDeviceDescriptors; - OHOS::std::shared_ptr descriptor = std::make_shared(); + std::vector> audioDeviceDescriptors; + std::shared_ptr descriptor = std::make_shared(); descriptor->deviceType_ = OHOS::AudioStandard::DEVICE_TYPE_WIRED_HEADSET; deviceChange.type = static_cast(0); deviceChange.flag = static_cast(0); -- Gitee