~aleteoryx/muditaos

7f72e4c55787a5a42daa31cbf71e1967c5490e9e — Adam Dobrowolski 4 years ago ddf5342
[EGD-8208] Audio try to handle actual stop

Stopping all audio seems excessive
M module-services/service-cellular/CallAudio.cpp => module-services/service-cellular/CallAudio.cpp +29 -5
@@ 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 <service-audio/AudioServiceAPI.hpp>
#include <Timers/TimerFactory.hpp>

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<service::AudioStartPlaybackResponse *>(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);
}

M module-services/service-cellular/CallAudio.hpp => module-services/service-cellular/CallAudio.hpp +2 -0
@@ 10,7 10,9 @@ namespace sys

class CallRingAudio
{
    struct CallMeta;
    sys::Service &owner;
    CallMeta *meta;

  public:
    explicit CallRingAudio(sys::Service &);