From fbc141fe5293355b75429c99112d3fa81c52e674 Mon Sep 17 00:00:00 2001 From: songshenke Date: Sat, 13 Apr 2024 07:38:43 +0000 Subject: [PATCH] Optimize AudioHapticPlayer and use it for SystemTonePlayer Signed-off-by: songshenke Change-Id: I2dad7f0dbf626880f769f05e0ab5a644c9bd9035 --- .../native/system_sound_manager/BUILD.gn | 2 - .../ringtone_player/ringtone_player_impl.cpp | 3 +- .../ringtone_player/ringtone_player_impl.h | 3 - .../system_sound_manager_impl.cpp | 5 +- .../system_sound_manager_impl.h | 3 - .../system_tone_player_impl.cpp | 275 ++++++++---------- .../system_tone_player_impl.h | 39 ++- .../include/system_tone_player.h | 17 ++ 8 files changed, 160 insertions(+), 187 deletions(-) diff --git a/frameworks/native/system_sound_manager/BUILD.gn b/frameworks/native/system_sound_manager/BUILD.gn index 5a5c3f467..ff996a47d 100644 --- a/frameworks/native/system_sound_manager/BUILD.gn +++ b/frameworks/native/system_sound_manager/BUILD.gn @@ -35,7 +35,6 @@ ohos_shared_library("system_sound_client") { install_enable = true sources = [ "./ringtone_player/ringtone_player_impl.cpp", - "./system_sound_vibrator/system_sound_vibrator.cpp", "./system_tone_player/system_tone_player_impl.cpp", "system_sound_manager_impl.cpp", ] @@ -43,7 +42,6 @@ ohos_shared_library("system_sound_client") { include_dirs = [ "./", "./ringtone_player", - "./system_sound_vibrator", "./system_tone_player", "./../../../interfaces/inner_api/native/audio_haptic/include", "./../../../interfaces/inner_api/native/soundpool/include", diff --git a/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.cpp b/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.cpp index 71c493f71..427d4535e 100644 --- a/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.cpp +++ b/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.cpp @@ -49,10 +49,9 @@ RingtonePlayerImpl::~RingtonePlayerImpl() { if (player_ != nullptr) { player_->Release(); - (void)SystemSoundVibrator::StopVibrator(); player_ = nullptr; - callback_ = nullptr; } + callback_ = nullptr; } void RingtonePlayerImpl::InitPlayer(std::string &audioUri) diff --git a/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.h b/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.h index a8a62e316..d193f943c 100644 --- a/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.h +++ b/frameworks/native/system_sound_manager/ringtone_player/ringtone_player_impl.h @@ -17,10 +17,7 @@ #define RINGTONE_PLAYER_IMPL_H #include "audio_haptic_manager.h" -#include "player.h" #include "system_sound_manager_impl.h" -#include "system_sound_vibrator.h" - namespace OHOS { namespace Media { diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp index 9c906cbed..296842266 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp +++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp @@ -394,7 +394,10 @@ void RingerModeCallbackImpl::OnRingerModeUpdated(const AudioStandard::AudioRinge ringerMode_ = ringerMode; int32_t result = sysSoundMgr_.SetRingerMode(ringerMode_); if (result == MSERR_OK && ringerMode_ == AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) { - SystemSoundVibrator::StopVibrator(); + + // to do: Stop vibrator!!! + + // SystemSoundVibrator::StopVibrator(); } } } // namesapce AudioStandard diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.h b/frameworks/native/system_sound_manager/system_sound_manager_impl.h index ee8bf94f0..32167e89f 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_impl.h +++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.h @@ -18,10 +18,7 @@ #include -#include "data_ability_helper.h" #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" -#include "uri.h" -#include "want.h" #include "audio_system_manager.h" diff --git a/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.cpp b/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.cpp index f8fb1a72b..183110d94 100644 --- a/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.cpp +++ b/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.cpp @@ -32,49 +32,74 @@ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "SystemTone namespace OHOS { namespace Media { -const int32_t MAX_STREAMS = 1; // ensure that only one system tone is playing. -const int32_t LOAD_WAIT_SECONDS = 2; -const std::string DEFAULT_SYSTEM_TONE_URI_1 = - "sys_prod/resource/media/audio/notifications/Rise.ogg"; -const std::string DEFAULT_SYSTEM_TONE_URI_2 = - "sys_prod/variant/region_comm/china/resource/media/audio/notifications/Rise.ogg"; - SystemTonePlayerImpl::SystemTonePlayerImpl(const shared_ptr &context, SystemSoundManagerImpl &systemSoundMgr, SystemToneType systemToneType) : context_(context), systemSoundMgr_(systemSoundMgr), systemToneType_(systemToneType) { - InitPlayer(); + audioHapticManager_ = AudioHapticManagerFactory::CreateAudioHapticManager(); + CHECK_AND_RETURN_LOG(audioHapticManager_ != nullptr, "Failed to get audio haptic manager"); + + std::string systemToneUri = systemSoundMgr_.GetSystemToneUri(context_, systemToneType_); + InitPlayer(systemToneUri); } SystemTonePlayerImpl::~SystemTonePlayerImpl() { - if (fileDes_ != -1) { - (void)close(fileDes_); + if (player_ != nullptr) { + (void)player_->Release(); + player_ = nullptr; } - - player_ = nullptr; callback_ = nullptr; } -void SystemTonePlayerImpl::InitPlayer() +int32_t SystemTonePlayerImpl::InitPlayer(const std::string &audioUri) { - MEDIA_LOGI("Enter InitPlayer()"); + MEDIA_LOGI("Enter InitPlayer() with audio uri %{public}s", audioUri.c_str()); + + if (sourceId_ != -1) { + (void)audioHapticManager_->UnregisterSource(sourceId_); + sourceId_ = -1; + } + // Determine whether vibration is needed + muteHaptics_ = GetMuteHapticsValue(); + std::string hapticUri = ""; + if (!muteHaptics_) { + // Get the default haptic source uri. + std::string defaultSystemToneUri = systemSoundMgr_.GetDefaultSystemToneUri(systemToneType_); + if (defaultSystemToneUri == "") { + MEDIA_LOGW("Default system tone uri is empty. Play system tone without vibration"); + muteHaptics_ = true; + } else { + // the size of "ogg" is 3 and the size of ".ogg" is 4. + hapticUri = defaultSystemToneUri.replace(defaultSystemToneUri.find_last_of(".ogg") - 3, 4, ".json"); + } + } - AudioStandard::AudioRendererInfo audioRendererInfo; - audioRendererInfo.contentType = AudioStandard::ContentType::CONTENT_TYPE_UNKNOWN; - audioRendererInfo.streamUsage = AudioStandard::StreamUsage::STREAM_USAGE_NOTIFICATION; - audioRendererInfo.rendererFlags = 0; + sourceId_ = audioHapticManager_->RegisterSource(audioUri, hapticUri); + CHECK_AND_RETURN_RET_LOG(sourceId_ != -1, MSERR_OPEN_FILE_FAILED, + "Failed to register source for audio haptic manager"); + (void)audioHapticManager_->SetAudioLatencyMode(sourceId_, AUDIO_LATENCY_MODE_NORMAL); + (void)audioHapticManager_->SetStreamUsage(sourceId_, AudioStandard::StreamUsage::STREAM_USAGE_NOTIFICATION); - player_ = SoundPoolFactory::CreateSoundPool(MAX_STREAMS, audioRendererInfo); - CHECK_AND_RETURN_LOG(player_ != nullptr, "Failed to create system tone player instance"); + player_ = audioHapticManager_->CreatePlayer(sourceId_, {muteAudio_, muteHaptics_}); + CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_OPEN_FILE_FAILED, + "Failed to create system tone player instance"); + int32_t result = player_->Prepare(); + CHECK_AND_RETURN_RET_LOG(result == MSERR_OK, result, + "Failed to load source for system tone player"); + configuredUri_ = audioUri; - callback_ = std::make_shared(*this); - CHECK_AND_RETURN_LOG(callback_ != nullptr, "Failed to create callback object"); - player_->SetSoundPoolCallback(callback_); + if (callback_ == nullptr) { + callback_ = std::make_shared(shared_from_this()); + } + CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, MSERR_OPEN_FILE_FAILED, + "Failed to create system tone player callback object"); + (void)player_->SetAudioHapticPlayerCallback(callback_); - configuredUri_ = ""; + systemToneState_ = SystemToneState::STATE_PREPARED; + return MSERR_OK; } int32_t SystemTonePlayerImpl::Prepare() @@ -83,107 +108,39 @@ int32_t SystemTonePlayerImpl::Prepare() std::lock_guard lock(systemTonePlayerMutex_); CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_INVALID_STATE, "System tone player instance is null"); - auto systemToneUri = systemSoundMgr_.GetSystemToneUri(context_, systemToneType_); - if (!configuredUri_.empty() && configuredUri_ == systemToneUri) { - MEDIA_LOGI("Prepare: The system tone uri has been loaded. Return directly."); + std::string systemToneUri = systemSoundMgr_.GetSystemToneUri(context_, systemToneType_); + bool muteHaptics = GetMuteHapticsValue(); + if (!configuredUri_.empty() && configuredUri_ == systemToneUri && muteHaptics_ == muteHaptics) { + MEDIA_LOGI("Prepare: The system tone uri has been loaded and the vibration mode is right. Return directly."); return MSERR_OK; } - if (soundID_ != -1) { - (void)player_->Unload(soundID_); - soundID_ = -1; - loadCompleted_ = false; - } - if (fileDes_ != -1) { - (void)close(fileDes_); - fileDes_ = -1; - } - - fileDes_ = open(systemToneUri.c_str(), O_RDONLY); - if (fileDes_ == -1) { - // open file failed, try to use default path. - int32_t ret = ApplyDefaultSystemToneUri(systemToneUri); - if (ret == MSERR_OK) { - systemSoundMgr_.SetSystemToneUri(context_, systemToneUri, systemToneType_); - } else { - return ret; - } - } - std::string uri = "fd://" + to_string(fileDes_); - - int32_t soundID = player_->Load(uri); - if (soundID < 0) { - MEDIA_LOGE("Prepare: Failed to load system tone uri."); - return MSERR_OPEN_FILE_FAILED; - } - std::unique_lock lockWait(loadUriMutex_); - condLoadUri_.wait_for(lockWait, std::chrono::seconds(LOAD_WAIT_SECONDS), - [this]() { return loadCompleted_ || isReleased_; }); - if (isReleased_) { - MEDIA_LOGE("Prepare: The system tone player is released when it is preparing."); - return MSERR_INVALID_OPERATION; - } - if (!loadCompleted_) { - MEDIA_LOGE("Prepare: Failed to load system tone uri (time out)."); - return MSERR_OPEN_FILE_FAILED; - } - - soundID_ = soundID; - configuredUri_ = systemToneUri; - - return MSERR_OK; + int32_t result = InitPlayer(systemToneUri); + CHECK_AND_RETURN_RET_LOG(result == MSERR_OK, result, "Failed to initialize the audio haptic player!"); + systemToneState_ = SystemToneState::STATE_PREPARED; + return result; } -int32_t SystemTonePlayerImpl::ApplyDefaultSystemToneUri(std::string &defaultUri) +bool SystemTonePlayerImpl::GetMuteHapticsValue() { - // systemToneUri == "", try to use default system tone uri 1. - fileDes_ = open(DEFAULT_SYSTEM_TONE_URI_1.c_str(), O_RDONLY); - if (fileDes_ != -1) { - MEDIA_LOGI("ApplyDefaultSystemToneUri: Set source to default system tone uri 1."); - defaultUri = DEFAULT_SYSTEM_TONE_URI_1; - return MSERR_OK; + bool muteHaptics = false; + if (systemSoundMgr_.GetRingerMode() == AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) { + muteHaptics = true; } - - // try to use default system tone uri 2. - fileDes_ = open(DEFAULT_SYSTEM_TONE_URI_2.c_str(), O_RDONLY); - if (fileDes_ != -1) { - MEDIA_LOGI("ApplyDefaultSystemToneUri: Set source to default system tone uri 2."); - defaultUri = DEFAULT_SYSTEM_TONE_URI_2; - return MSERR_OK; - } - - return MSERR_OPEN_FILE_FAILED; + return muteHaptics; } -int32_t SystemTonePlayerImpl::NotifyLoadCompleted() +void SystemTonePlayerImpl::NotifyEndOfStream() { - std::lock_guard lockWait(loadUriMutex_); - loadCompleted_ = true; - condLoadUri_.notify_one(); - return MSERR_OK; + std::lock_guard lock(systemTonePlayerMutex_); + systemToneState_ = SystemToneState::STATE_STOPPED; } int32_t SystemTonePlayerImpl::Start() { MEDIA_LOGI("Enter Start()"); - std::lock_guard lock(systemTonePlayerMutex_); - CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_INVALID_STATE, "System tone player instance is null"); - - PlayParams playParams { - .loop = 0, - .rate = 0, // default AudioRendererRate::RENDER_RATE_NORMAL - .leftVolume = 1.0, - .rightVolume = 1.0, - .priority = 0, - .parallelPlayFlag = false, - }; - - int32_t streamID = player_->Play(soundID_, playParams); - if (systemSoundMgr_.GetRingerMode() != AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) { - (void)SystemSoundVibrator::StartVibrator(VibrationType::VIBRATION_SYSTEM_TONE); - } - - return streamID; + SystemToneOptions options = {false, false}; + return Start(options); } int32_t SystemTonePlayerImpl::Start(const SystemToneOptions &systemToneOptions) @@ -193,23 +150,31 @@ int32_t SystemTonePlayerImpl::Start(const SystemToneOptions &systemToneOptions) std::lock_guard lock(systemTonePlayerMutex_); CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_INVALID_STATE, "System tone player instance is null"); - int32_t streamID = -1; - if (!systemToneOptions.muteAudio) { - PlayParams playParams { - .loop = 0, - .rate = 0, // default AudioRendererRate::RENDER_RATE_NORMAL - .leftVolume = 1.0, - .rightVolume = 1.0, - .priority = 0, - .parallelPlayFlag = false, - }; - - streamID = player_->Play(soundID_, playParams); - } - if (!systemToneOptions.muteHaptics && - systemSoundMgr_.GetRingerMode() != AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) { - (void)SystemSoundVibrator::StartVibrator(VibrationType::VIBRATION_SYSTEM_TONE); + // to do: Use one Soundpool for all SystemTopePlayer + if (systemToneOptions.muteAudio != muteAudio_ || systemToneOptions.muteHaptics != muteHaptics_) { + // reset audio haptic player } + + + + + int32_t streamID = -1; + // if (!systemToneOptions.muteAudio) { + // PlayParams playParams { + // .loop = 0, + // .rate = 0, // default AudioRendererRate::RENDER_RATE_NORMAL + // .leftVolume = 1.0, + // .rightVolume = 1.0, + // .priority = 0, + // .parallelPlayFlag = false, + // }; + + // streamID = player_->Play(soundID_, playParams); + // } + // if (!systemToneOptions.muteHaptics && + // systemSoundMgr_.GetRingerMode() != AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) { + // (void)SystemSoundVibrator::StartVibrator(VibrationType::VIBRATION_SYSTEM_TONE); + // } return streamID; } @@ -219,12 +184,18 @@ int32_t SystemTonePlayerImpl::Stop(const int32_t &streamID) std::lock_guard lock(systemTonePlayerMutex_); CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_INVALID_STATE, "System tone player instance is null"); - if (streamID > 0) { - (void)player_->Stop(streamID); - (void)SystemSoundVibrator::StopVibrator(); - } else { - MEDIA_LOGW("The streamID %{public}d is invalid!", streamID); - } + // to do: streamId + int32_t result = player_->Stop(); + CHECK_AND_RETURN_RET_LOG(result == MSERR_OK, result, "Error %{public}d to stop audio haptic player!", result); + + systemToneState_ = SystemToneState::STATE_STOPPED; + + // if (streamID > 0) { + // (void)player_->Stop(streamID); + // (void)SystemSoundVibrator::StopVibrator(); + // } else { + // MEDIA_LOGW("The streamID %{public}d is invalid!", streamID); + // } return MSERR_OK; } @@ -232,22 +203,14 @@ int32_t SystemTonePlayerImpl::Stop(const int32_t &streamID) int32_t SystemTonePlayerImpl::Release() { MEDIA_LOGI("Enter Release()"); - { - std::lock_guard lockWait(loadUriMutex_); - isReleased_ = true; - condLoadUri_.notify_all(); - } std::lock_guard lock(systemTonePlayerMutex_); CHECK_AND_RETURN_RET_LOG(player_ != nullptr, MSERR_INVALID_STATE, "System tone player instance is null"); - (void)player_->Unload(soundID_); - soundID_ = -1; - loadCompleted_ = false; - if (fileDes_ != -1) { - (void)close(fileDes_); - fileDes_ = -1; - } + int32_t result = player_->Release(); + CHECK_AND_RETURN_RET_LOG(result == MSERR_OK, result, "Error %{public}d to release audio haptic player!", result); + + systemToneState_ = SystemToneState::STATE_RELEASED; player_ = nullptr; callback_ = nullptr; @@ -262,23 +225,23 @@ std::string SystemTonePlayerImpl::GetTitle() const } // Callback class symbols -SystemTonePlayerCallback::SystemTonePlayerCallback(SystemTonePlayerImpl &systemTonePlayerImpl) +SystemTonePlayerCallback::SystemTonePlayerCallback(std::shared_ptr systemTonePlayerImpl) : systemTonePlayerImpl_(systemTonePlayerImpl) {} -void SystemTonePlayerCallback::OnLoadCompleted(int32_t soundId) +void SystemTonePlayerCallback::OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) { - MEDIA_LOGI("OnLoadCompleted reported from sound pool."); - systemTonePlayerImpl_.NotifyLoadCompleted(); + // Audio interrupt callback is not supported for system tone player + MEDIA_LOGI("OnInterrupt reported from audio haptic player."); } - -void SystemTonePlayerCallback::OnPlayFinished() -{ - MEDIA_LOGI("OnPlayFinished reported from sound pool."); -} - -void SystemTonePlayerCallback::OnError(int32_t errorCode) +void SystemTonePlayerCallback::OnEndOfStream(void) { - MEDIA_LOGE("Error reported from sound pool: %{public}d", errorCode); + MEDIA_LOGI("OnEndOfStream reported from audio haptic player."); + std::shared_ptr player = systemTonePlayerImpl_.lock(); + if (player != nullptr) { + player->NotifyEndOfStream(); + } else { + MEDIA_LOGE("The system tone player has been released!"); + } } } // namesapce AudioStandard } // namespace OHOS diff --git a/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.h b/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.h index da8f576c9..4477ee993 100644 --- a/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.h +++ b/frameworks/native/system_sound_manager/system_tone_player/system_tone_player_impl.h @@ -19,15 +19,14 @@ #include #include -#include "isoundpool.h" +#include "audio_haptic_manager.h" #include "system_sound_manager_impl.h" -#include "system_sound_vibrator.h" namespace OHOS { namespace Media { class SystemTonePlayerCallback; -class SystemTonePlayerImpl : public SystemTonePlayer { +class SystemTonePlayerImpl : public SystemTonePlayer, public std::enable_shared_from_this { public: SystemTonePlayerImpl(const std::shared_ptr &context, SystemSoundManagerImpl &systemSoundMgr, SystemToneType systemToneType); @@ -41,37 +40,37 @@ public: int32_t Stop(const int32_t &streamID) override; int32_t Release() override; - int32_t NotifyLoadCompleted(); + void NotifyEndOfStream(); private: - void InitPlayer(); - int32_t ApplyDefaultSystemToneUri(std::string &defaultUri); + int32_t InitPlayer(const std::string &audioUri); + bool GetMuteHapticsValue(); - std::shared_ptr player_ = nullptr; + std::shared_ptr audioHapticManager_ = nullptr; + std::shared_ptr player_ = nullptr; std::shared_ptr callback_ = nullptr; + bool muteAudio_ = false; + bool muteHaptics_ = false; + int32_t sourceId_ = -1; + std::string configuredUri_ = ""; + std::shared_ptr context_; SystemSoundManagerImpl &systemSoundMgr_; SystemToneType systemToneType_; - std::string configuredUri_ = ""; - int32_t soundID_ = -1; - int32_t fileDes_ = -1; - bool loadCompleted_ = false; - bool isReleased_ = false; + SystemToneState systemToneState_ = SystemToneState::STATE_NEW; std::mutex systemTonePlayerMutex_; - std::mutex loadUriMutex_; - std::condition_variable condLoadUri_; }; -class SystemTonePlayerCallback : public ISoundPoolCallback { +class SystemTonePlayerCallback : public AudioHapticPlayerCallback { public: - explicit SystemTonePlayerCallback(SystemTonePlayerImpl &systemTonePlayerImpl); + explicit SystemTonePlayerCallback(std::shared_ptr systemTonePlayerImpl); virtual ~SystemTonePlayerCallback() = default; - void OnLoadCompleted(int32_t soundId) override; - void OnPlayFinished() override; - void OnError(int32_t errorCode) override; + + void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent); + void OnEndOfStream(void); private: - SystemTonePlayerImpl &systemTonePlayerImpl_; + std::weak_ptr systemTonePlayerImpl_; }; } // namespace Media } // namespace OHOS diff --git a/interfaces/inner_api/native/system_sound_manager/include/system_tone_player.h b/interfaces/inner_api/native/system_sound_manager/include/system_tone_player.h index 48a52cdcc..aba2f04eb 100644 --- a/interfaces/inner_api/native/system_sound_manager/include/system_tone_player.h +++ b/interfaces/inner_api/native/system_sound_manager/include/system_tone_player.h @@ -27,6 +27,23 @@ struct SystemToneOptions { bool muteHaptics; }; +enum class SystemToneState { + /** INVALID state */ + STATE_INVALID = -1, + /** Create New instance */ + STATE_NEW, + /** Prepared state */ + STATE_PREPARED, + /** Running state */ + STATE_RUNNING, + /** Stopped state */ + STATE_STOPPED, + /** Released state */ + STATE_RELEASED, + /** Paused state */ + STATE_PAUSED +}; + class SystemTonePlayer { public: virtual ~SystemTonePlayer() = default; -- Gitee