diff --git a/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp b/frameworks/native/audio_haptic/audio_haptic_player_impl.cpp index c283f55fc9d0abced3862e4e95dfc90135110fc7..54ab6e0601b3b6e3273c847a08fce284eeb8603b 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 52bbe91827a4dd54b6049d026a67a2c3e130e5b4..f496910102fa871b260f4349c2138a42684f6d59 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 28a674b0c4329f46ef6ec705caabb952d1c1b68b..3339032c968291063d54f424fb8550020e1cd595 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 d02d14882d3b412d9c75a5cfa8e471651ba9ac47..74eb404ae20a9c8ec4e01eb64f8d2dc08e5975fc 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 9372f8fd0823675f611f268f9d56b8c2c1023ee6..7565419b5913696c1c7455dc925c05d473ee53d3 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