From 7f72e4c55787a5a42daa31cbf71e1967c5490e9e Mon Sep 17 00:00:00 2001 From: Adam Dobrowolski Date: Fri, 28 Jan 2022 08:43:51 +0100 Subject: [PATCH] [EGD-8208] Audio try to handle actual stop Stopping all audio seems excessive --- .../service-cellular/CallAudio.cpp | 34 ++++++++++++++++--- .../service-cellular/CallAudio.hpp | 2 ++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/module-services/service-cellular/CallAudio.cpp b/module-services/service-cellular/CallAudio.cpp index 33217b33ac80073276c445ef12de4a1a6489cdb4..b2eaff73cdb520d5a5059e4f26c2c6799df67dc0 100644 --- a/module-services/service-cellular/CallAudio.cpp +++ b/module-services/service-cellular/CallAudio.cpp @@ -2,21 +2,45 @@ // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "CallAudio.hpp" +#include "Service/Message.hpp" +#include "products/BellHybrid/services/audio/include/audio/AudioMessage.hpp" #include +#include -CallRingAudio::CallRingAudio(sys::Service &s) : owner(s) +struct CallRingAudio::CallMeta +{ + audio::Token token; + sys::TimerHandle retryTimer; +}; + +CallRingAudio::CallRingAudio(sys::Service &s) : owner(s), meta(new CallRingAudio::CallMeta) {} void CallRingAudio::play() { const auto filePath = AudioServiceAPI::GetSound(&owner, audio::PlaybackType::CallRingtone); AudioServiceAPI::PlaybackStart(&owner, audio::PlaybackType::CallRingtone, filePath); + owner.connect(typeid(service::AudioStartPlaybackResponse), [this](sys::Message *msg) -> sys::MessagePointer { + auto *message = dynamic_cast(msg); + assert(meta); + meta->token = message->token; + owner.disconnect(typeid(service::AudioStartPlaybackResponse)); + return sys::msgHandled(); + }); } void CallRingAudio::stop() { - /// NOTE: we should fix it to stop actual played ringtone... - /// but we would require async response from PlayBack start or API in audio to be able to - /// just stop Ringtone by path - AudioServiceAPI::StopAll(&owner); + assert(meta && "we have stop without start - this should never happen"); + // there is still a chance we didn't get the token - in this case, maybe timer? :D + if (!meta->token.IsValid() && !meta->retryTimer.isActive()) { + meta->retryTimer = sys::TimerFactory::createPeriodicTimer( + &owner, "retry stop music", std::chrono::seconds{1}, [this](sys::Timer &) { + meta->token.IsValid(); + AudioServiceAPI::Stop(&owner, meta->token); + meta->retryTimer.stop(); + }); + meta->retryTimer.start(); + } + AudioServiceAPI::Stop(&owner, meta->token); } diff --git a/module-services/service-cellular/CallAudio.hpp b/module-services/service-cellular/CallAudio.hpp index 840d99c98ed4132e2be49bd7d8629a7e9afa89b0..933bcf95e680c7b41670e5825df1ea3d213f046e 100644 --- a/module-services/service-cellular/CallAudio.hpp +++ b/module-services/service-cellular/CallAudio.hpp @@ -10,7 +10,9 @@ namespace sys class CallRingAudio { + struct CallMeta; sys::Service &owner; + CallMeta *meta; public: explicit CallRingAudio(sys::Service &);