From d111ce083880951229ee08a8f924544ef8072022 Mon Sep 17 00:00:00 2001 From: Tigran Soghbatyan Date: Tue, 30 Nov 2021 11:47:54 +0100 Subject: [PATCH] [BH-1273] Fix audio asset playback stop Make sure apps are only stopping their own audio playbacks --- products/BellHybrid/apps/Application.cpp | 1 - .../ApplicationBellBackgroundSounds.cpp | 6 ++++++ .../ApplicationBellBackgroundSounds.hpp | 1 + .../widgets/BGSoundsPlayer.cpp | 2 +- .../ApplicationBellPowerNap.cpp | 6 ++++++ .../ApplicationBellPowerNap.hpp | 1 + .../presenter/PowerNapProgressPresenter.cpp | 2 +- .../ApplicationBellSettings.cpp | 6 ++++++ .../ApplicationBellSettings.hpp | 1 + .../presenter/BedtimeSettingsPresenter.cpp | 2 +- .../alarm_settings/AlarmSettingsPresenter.cpp | 2 +- .../alarm_settings/PrewakeUpPresenter.cpp | 2 +- .../alarm_settings/SnoozePresenter.cpp | 2 +- .../common/models/AbstractAudioModel.hpp | 3 ++- .../include/common/models/AudioModel.hpp | 5 ++++- .../BellHybrid/apps/common/src/AudioModel.cpp | 19 +++++++++++++++---- .../BellHybrid/apps/include/Application.hpp | 2 +- .../services/audio/ServiceAudio.cpp | 7 ++++++- 18 files changed, 55 insertions(+), 15 deletions(-) diff --git a/products/BellHybrid/apps/Application.cpp b/products/BellHybrid/apps/Application.cpp index 39f9a31f0ce0214f5016a2b9a97c6471bca8bb36..65222d5cf0a5ab4aa6a8c3ddb15dfd1bbe1a8e1a 100644 --- a/products/BellHybrid/apps/Application.cpp +++ b/products/BellHybrid/apps/Application.cpp @@ -117,7 +117,6 @@ namespace app void Application::onStop() { stopIdleTimer(); - stopAllAudio(); } void Application::stopAllAudio() diff --git a/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp b/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp index 337249b04a0ece008b56a40584b33714336fa0df..2bfe1f1a94aee0624ace8643f80fd7763c183390 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp @@ -108,4 +108,10 @@ namespace app } return ApplicationCommon::handleSwitchWindow(msgl); } + + void ApplicationBellBackgroundSounds::onStop() + { + Application::onStop(); + audioModel->stopPlayedByThis({}); + } } // namespace app diff --git a/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp b/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp index 0ab0e5732062e860c8eb3dfe265a59c864d710b3..f043c02b91c1ab5fe54c6fce94e2114e2dea8b67 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp @@ -26,6 +26,7 @@ namespace app std::unique_ptr audioModel; std::unique_ptr player; + void onStop() override; sys::MessagePointer handleSwitchWindow(sys::Message *msgl) override; public: diff --git a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp index c999fc19cf6a6c724b4a51ae836193f9f8a0755b..19ebf5316bf54a915e03fe33a663748d55f95ed1 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp @@ -28,7 +28,7 @@ namespace app::bgSounds void BGSoundsPlayer::stop(AbstractAudioModel::OnStateChangeCallback &&callback) { paused = false; - audioModel.stop(std::move(callback)); + audioModel.stopPlayedByThis(std::move(callback)); } void BGSoundsPlayer::pause(AbstractAudioModel::OnStateChangeCallback &&callback) { diff --git a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp index f787015f01b95dad4123b5b3f813837f962a075d..9a42ae3c044c3f098dec6a5b6e54700700f978ea 100644 --- a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp +++ b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp @@ -73,4 +73,10 @@ namespace app return handleAsyncResponse(resp); } + + void ApplicationBellPowerNap::onStop() + { + Application::onStop(); + audioModel->stopPlayedByThis({}); + } } // namespace app diff --git a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp index 1842af2fa2a0cbeaf7b3bcfeff203c9bbd654be7..c16ba93463af61748965207bd7e6b68f4d8ce51b 100644 --- a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp +++ b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp @@ -24,6 +24,7 @@ namespace app { private: std::unique_ptr audioModel; + void onStop() override; public: ApplicationBellPowerNap(std::string name = applicationBellPowerNapName, diff --git a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp index bc2b5363978e5ba0c56e7f1b207a774962920942..d01562eb7686b13c68f101d6b132af8e6c2e4aa3 100644 --- a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp @@ -65,7 +65,7 @@ namespace app::powernap } void PowerNapProgressPresenter::onNapAlarmFinished() { - audioModel.stop({}); + audioModel.stopPlayedByThis({}); getView()->napEnded(); } diff --git a/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp b/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp index 6b1b588cc412fba81cd1e2e7e5d5b9cb7a088c47..aabd3e79c46dae6e52ddfb13b8ae94a1cc26855d 100644 --- a/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp +++ b/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp @@ -239,4 +239,10 @@ namespace app } return handleAsyncResponse(resp); } + + void ApplicationBellSettings::onStop() + { + Application::onStop(); + audioModel->stopPlayedByThis({}); + } } // namespace app diff --git a/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp b/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp index 60158c3392d5140394d33b300e62a770f876bf3d..17ac347252dfe465e855f75c147e9043f9f14453 100644 --- a/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp +++ b/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp @@ -29,6 +29,7 @@ namespace app { private: std::unique_ptr audioModel; + void onStop() override; public: ApplicationBellSettings(std::string name = applicationBellSettingsName, diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp index 8914feb4bfb74beb89608c28bfd8982babdb6c54..ceec7f9c61c90af1a02ce9b8f54f5029dfe4bf6e 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp @@ -61,7 +61,7 @@ namespace app::bell_settings void BedtimeSettingsPresenter::stopSound() { - this->audioModel.stop({}); + this->audioModel.stopPlayedByThis({}); } void BedtimeSettingsPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp index 898ec5fd2d59fb03e229dbcbff3accf5858ed3a4..ebb5113b0b414f7cc5df2d9d31ad879fab4ee0f7 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp @@ -57,7 +57,7 @@ namespace app::bell_settings } void AlarmSettingsPresenter::stopSound() { - this->audioModel.stop({}); + this->audioModel.stopPlayedByThis({}); } void AlarmSettingsPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp index b7f5ac96e3db96ac2033b8c182b61a6fe4164d89..fe33c1e5d1349e4452e9f1910705310119f59028 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp @@ -61,7 +61,7 @@ namespace app::bell_settings } void PrewakeUpWindowPresenter::stopSound() { - this->audioModel.stop({}); + this->audioModel.stopPlayedByThis({}); } void PrewakeUpWindowPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp index c81a678e98930ceb4bc96f9f15c1e94a68a63b20..7a1c886454877a6c1b181ec5a1fafdefa641d30a 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp @@ -58,7 +58,7 @@ namespace app::bell_settings } void SnoozePresenter::stopSound() { - audioModel.stop({}); + audioModel.stopPlayedByThis({}); } void SnoozePresenter::eraseProviderData() { diff --git a/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp b/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp index c0d5f54630d757af239613a1afa29f6e8b242d0c..d0c07f93b94bbd15841c146a4f59d04ff04782a1 100644 --- a/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp +++ b/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp @@ -36,7 +36,8 @@ namespace app virtual std::optional getVolume(PlaybackType playbackType) = 0; virtual void getVolume(PlaybackType playbackType, OnGetValueCallback &&callback) = 0; virtual void play(const std::string &filePath, PlaybackType type, OnStateChangeCallback &&callback) = 0; - virtual void stop(OnStateChangeCallback &&callback) = 0; + virtual void stopAny(OnStateChangeCallback &&callback) = 0; + virtual void stopPlayedByThis(OnStateChangeCallback &&callback) = 0; virtual void pause(OnStateChangeCallback &&callback) = 0; virtual void resume(OnStateChangeCallback &&callback) = 0; virtual void setPlaybackFinishedCb(OnPlaybackFinishedCallback &&callback) = 0; diff --git a/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp b/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp index 57b3bd131b235d1153a2328d1d69ea93e4da2689..56d65b3f66e327e27a902ef105d74acb6473a8fd 100644 --- a/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp +++ b/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp @@ -18,13 +18,16 @@ namespace app std::optional getVolume(PlaybackType playbackType) override; void getVolume(PlaybackType playbackType, OnGetValueCallback &&callback) override; void play(const std::string &filePath, PlaybackType type, OnStateChangeCallback &&callback) override; - void stop(OnStateChangeCallback &&callback) override; + void stopAny(OnStateChangeCallback &&callback) override; + void stopPlayedByThis(OnStateChangeCallback &&callback) override; void pause(OnStateChangeCallback &&callback) override; void resume(OnStateChangeCallback &&callback) override; void setPlaybackFinishedCb(OnPlaybackFinishedCallback &&callback) override; bool hasPlaybackFinished() override; private: + audio::Token lastPlayedToken = audio::Token::MakeBadToken(); + void stop(audio::Token token, OnStateChangeCallback &&callback); std::function playbackFinishedCallback{nullptr}; bool playbackFinishedFlag{false}; ApplicationCommon *app{}; diff --git a/products/BellHybrid/apps/common/src/AudioModel.cpp b/products/BellHybrid/apps/common/src/AudioModel.cpp index 1595001b3c3c5a44e9dd59d7837bf6004ef51e06..2e83cd8f59cf94d04cddd7b6d413e6432760c4cd 100644 --- a/products/BellHybrid/apps/common/src/AudioModel.cpp +++ b/products/BellHybrid/apps/common/src/AudioModel.cpp @@ -76,8 +76,9 @@ namespace app playbackFinishedFlag = false; auto msg = std::make_unique(filePath, convertPlaybackType(type)); auto task = app::AsyncRequest::createFromMessage(std::move(msg), service::audioServiceName); - auto cb = [_callback = callback](auto response) { - auto result = dynamic_cast(response); + auto cb = [_callback = callback, this](auto response) { + auto result = dynamic_cast(response); + lastPlayedToken = result->token; if (result == nullptr) { return false; } @@ -91,9 +92,14 @@ namespace app task->execute(app, this, std::move(cb)); } - void AudioModel::stop(OnStateChangeCallback &&callback) + void AudioModel::stopAny(OnStateChangeCallback &&callback) + { + stop(audio::Token(), std::move(callback)); + } + + void AudioModel::stop(audio::Token token, OnStateChangeCallback &&callback) { - auto msg = std::make_unique(); + auto msg = std::make_unique(token); auto task = app::AsyncRequest::createFromMessage(std::move(msg), service::audioServiceName); auto cb = [_callback = callback](auto response) { auto result = dynamic_cast(response); @@ -109,6 +115,11 @@ namespace app task->execute(app, this, std::move(cb)); } + void AudioModel::stopPlayedByThis(OnStateChangeCallback &&callback) + { + stop(lastPlayedToken, std::move(callback)); + } + void AudioModel::setVolume(AbstractAudioModel::Volume volume, PlaybackType playbackType, OnStateChangeCallback &&callback) diff --git a/products/BellHybrid/apps/include/Application.hpp b/products/BellHybrid/apps/include/Application.hpp index af16292038c225774fb33b3f32c7341f128b30eb..12b860af87ec189851eacd4a50443e6b565a13b3 100644 --- a/products/BellHybrid/apps/include/Application.hpp +++ b/products/BellHybrid/apps/include/Application.hpp @@ -21,6 +21,7 @@ namespace app void restartIdleTimer(); void stopIdleTimer(); void stopAllAudio(); + virtual void onStop(); private: sys::MessagePointer handleKBDKeyEvent(sys::Message *msgl) override; @@ -31,7 +32,6 @@ namespace app virtual void onKeyPressed(); virtual void onStart(); - virtual void onStop(); bool idleTimerActiveFlag = true; }; } // namespace app diff --git a/products/BellHybrid/services/audio/ServiceAudio.cpp b/products/BellHybrid/services/audio/ServiceAudio.cpp index 0149c040b8c49243775745c12a4defc6a29a40e3..37a08051953d7c99d1e086c7f16b25c22eab3f2d 100644 --- a/products/BellHybrid/services/audio/ServiceAudio.cpp +++ b/products/BellHybrid/services/audio/ServiceAudio.cpp @@ -157,7 +157,12 @@ namespace service for (auto &input : audioMux.GetAllInputs()) { auto t = input.token; - retCodes.emplace_back(t, stopInput(&input)); + if (token.IsValid() && t == token) { + retCodes.emplace_back(t, stopInput(&input)); + } + if (token.IsUninitialized()) { + retCodes.emplace_back(t, stopInput(&input)); + } } // on failure return first false code