~aleteoryx/muditaos

ref: 7f72e4c55787a5a42daa31cbf71e1967c5490e9e muditaos/module-services/service-cellular/CallAudio.cpp -rw-r--r-- 1.7 KiB
7f72e4c5 — Adam Dobrowolski [EGD-8208] Audio try to handle actual stop 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// 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>

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()
{
    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);
}