From a6dcc4395b160d75a8b013440689605366e6b016 Mon Sep 17 00:00:00 2001 From: WangWanliang Date: Mon, 21 Jul 2025 19:27:59 +0800 Subject: [PATCH] Fix the Vibrator repeatedly release crash Signed-off-by: WangWanliang --- .../audio_haptic/audio_haptic_player_impl.cpp | 12 +++++-- .../audio_haptic/audio_haptic_player_impl.h | 1 + .../audio_haptic_vibrator_impl.cpp | 5 ++- .../system_sound_vibrator.cpp | 7 +++-- .../audio_haptic_player_impl_unit_test.cpp | 31 +++++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp b/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp index c283f55fc..54ab6e060 100644 --- a/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp +++ b/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp @@ -143,8 +143,7 @@ AudioHapticPlayerImpl::AudioHapticPlayerImpl() AudioHapticPlayerImpl::~AudioHapticPlayerImpl() { if (playerState_ != AudioHapticPlayerState::STATE_RELEASED) { - ReleaseVibrator(); - ReleaseSound(); + (void)ReleaseVibratorInternal(); } } @@ -254,6 +253,13 @@ int32_t AudioHapticPlayerImpl::Stop() int32_t AudioHapticPlayerImpl::Release() { + MEDIA_LOGI("Enter Release()."); + return ReleaseVibratorInternal(); +} + +int32_t AudioHapticPlayerImpl::ReleaseVibratorInternal() +{ + MEDIA_LOGI("Enter ReleaseSoundInternal()."); std::lock_guard lock(audioHapticPlayerLock_); CHECK_AND_RETURN_RET_LOG(playerState_ != AudioHapticPlayerState::STATE_RELEASED, MSERR_OK, "The audio haptic player has been released."); @@ -272,6 +278,7 @@ int32_t AudioHapticPlayerImpl::Release() void AudioHapticPlayerImpl::ReleaseVibrator() { + MEDIA_LOGI("Enter ReleaseVibrator()."); if (audioHapticVibrator_ != nullptr) { audioHapticVibrator_->StopVibrate(); } @@ -294,6 +301,7 @@ void AudioHapticPlayerImpl::ReleaseVibrator() void AudioHapticPlayerImpl::ReleaseSound() { + MEDIA_LOGI("Enter ReleaseSound()."); if (audioHapticSound_ != nullptr) { (void)audioHapticSound_->ReleaseSound(); audioHapticSound_ = nullptr; diff --git a/frameworks/native/audio_haptic/audio_haptic_player_impl.h b/frameworks/native/audio_haptic/audio_haptic_player_impl.h index 52bbe9182..f49691010 100644 --- a/frameworks/native/audio_haptic/audio_haptic_player_impl.h +++ b/frameworks/native/audio_haptic/audio_haptic_player_impl.h @@ -66,6 +66,7 @@ private: int32_t StartVibrate(); void StopVibrate(); void ResetVibrateState(); + int32_t ReleaseVibratorInternal(); void ReleaseVibrator(); int32_t GetDelayTime(int32_t playedTimes); diff --git a/frameworks/native/audio_haptic/audio_haptic_vibrator_impl.cpp b/frameworks/native/audio_haptic/audio_haptic_vibrator_impl.cpp index 28a674b0c..3339032c9 100644 --- a/frameworks/native/audio_haptic/audio_haptic_vibrator_impl.cpp +++ b/frameworks/native/audio_haptic/audio_haptic_vibrator_impl.cpp @@ -314,11 +314,14 @@ int32_t AudioHapticVibratorImpl::PreLoad(const HapticSource &hapticSource, return MSERR_OPEN_FILE_FAILED; } + CHECK_AND_RETURN_RET_LOG(vibratorFD_ != nullptr, ERROR, "vibratorFD_ is null."); + int32_t fd = vibratorFD_->fd; int32_t result = Sensors::PreProcess(*vibratorFD_, *vibratorPkg_); if (result != 0) { + close(fd); return MSERR_UNSUPPORT_FILE; } - close(vibratorFD_->fd); + close(fd); #endif return MSERR_OK; } diff --git a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp index d02d14882..74eb404ae 100644 --- a/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp +++ b/frameworks/native/system_sound_manager/system_sound_vibrator/system_sound_vibrator.cpp @@ -194,9 +194,11 @@ int32_t SystemSoundVibrator::VibrateForRingtone(const std::string hapticUri) int32_t result = VibrateLoopFunc(lock, fd); if (result != MSERR_OK) { + close(fd); MEDIA_LOGE("Failed to start vibrator!"); return MSERR_INVALID_OPERATION; } + close(fd); return result; } @@ -215,7 +217,6 @@ int32_t SystemSoundVibrator::VibrateLoopFunc(std::unique_lock &lock, vibratorFd->offset = 0; vibratorFd->length = statbuf.st_size; } else { - close(fd); return MSERR_OPEN_FILE_FAILED; } @@ -242,7 +243,6 @@ int32_t SystemSoundVibrator::VibrateLoopFunc(std::unique_lock &lock, []() { return !g_isRunning; }); CHECK_AND_RETURN_RET_LOG(g_isRunning, result, "RunVibrationPatterns: Stop() is call when waiting"); } - close(vibratorFd->fd); #endif return result; } @@ -301,6 +301,7 @@ int32_t SystemSoundVibrator::GetVibratorDuration(const std::string &hapticUri) int32_t result = Sensors::PreProcess(vibratorFD, vibratorPkg); if (result != 0) { + close(fd); MEDIA_LOGE("Failed to pre-process hapticUri!"); Sensors::FreeVibratorPackage(vibratorPkg); return ret; @@ -313,7 +314,7 @@ int32_t SystemSoundVibrator::GetVibratorDuration(const std::string &hapticUri) vibratorPkg.patterns[patternMaxIndex].events[eventMaxIndex].time + vibratorPkg.patterns[patternMaxIndex].events[eventMaxIndex].duration; Sensors::FreeVibratorPackage(vibratorPkg); - close(vibratorFD.fd); + close(fd); #endif return ret; } diff --git a/test/unittest/audio_haptic_test/audio_haptic_player_impl_unit_test.cpp b/test/unittest/audio_haptic_test/audio_haptic_player_impl_unit_test.cpp index 9372f8fd0..7565419b5 100644 --- a/test/unittest/audio_haptic_test/audio_haptic_player_impl_unit_test.cpp +++ b/test/unittest/audio_haptic_test/audio_haptic_player_impl_unit_test.cpp @@ -1314,5 +1314,36 @@ HWTEST_F(AudioHapticPlayerImplUnitTest, AudioHapticPlayerImpl_062, TestSize.Leve EXPECT_EQ(audioHapticPlayerImpl->GetDelayTime(0), 80); EXPECT_EQ(audioHapticPlayerImpl->GetDelayTime(0), 40); } + +/** + * @tc.name : Test AudioHapticPlayerImpl API + * @tc.number: AudioHapticPlayerImpl_063 + * @tc.desc : Test AudioHapticPlayerImpl::ReleaseVibratorInternal() + */ +HWTEST_F(AudioHapticPlayerImplUnitTest, AudioHapticPlayerImpl_063, TestSize.Level1) +{ + auto audioHapticPlayerImpl = std::make_shared(); + + EXPECT_NE(audioHapticPlayerImpl, nullptr); + + AudioHapticPlayerImpl audioHapticPlayerImpl2; + audioHapticPlayerImpl->audioHapticVibrator_ = std::make_shared(audioHapticPlayerImpl2); + EXPECT_NE(audioHapticPlayerImpl->audioHapticVibrator_, nullptr); + + audioHapticPlayerImpl->vibrateThread_ = std::make_shared(); + EXPECT_NE(audioHapticPlayerImpl->vibrateThread_, nullptr); + + AudioSource audioSource = {.audioUri = "123"}; + bool muteAudio = true; + AudioStandard::StreamUsage streamUsage = AudioStandard::StreamUsage::STREAM_USAGE_UNKNOWN; + auto audioHapticSoundNormalImpl = + std::make_shared(audioSource, muteAudio, streamUsage); + + EXPECT_NE(audioHapticSoundNormalImpl, nullptr); + + audioHapticPlayerImpl->audioHapticSound_ = audioHapticSoundNormalImpl; + int32_t result = audioHapticPlayerImpl->ReleaseVibratorInternal(); + EXPECT_EQ(result, MSERR_OK); +} } // namespace Media } // namespace OHOS \ No newline at end of file -- Gitee