summaryrefslogtreecommitdiffstats
path: root/src/multimedia/audio/qaudiostatemachine.cpp
blob: 2d42200e6b434af55238e39a80277e0a4590d424 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qaudiostatemachine_p.h"
#include "qaudiosystem_p.h"
#include <qpointer.h>
#include <qdebug.h>

QT_BEGIN_NAMESPACE

using Notifier = QAudioStateMachine::Notifier;
using namespace AudioStateMachineUtils;

QAudioStateMachine::QAudioStateMachine(QAudioStateChangeNotifier &notifier) : m_notifier(&notifier)
{
}

QAudioStateMachine::~QAudioStateMachine() = default;

QAudio::State QAudioStateMachine::state() const
{
    return toAudioState(m_state.load(std::memory_order_acquire));
}

QAudio::Error QAudioStateMachine::error() const
{
    return toAudioError(m_state.load(std::memory_order_acquire));
}

template <typename StatesChecker, typename NewState>
Notifier QAudioStateMachine::changeState(const StatesChecker &checker, const NewState &newState)
{
    if constexpr (std::is_same_v<RawState, NewState>)
        return changeState(checker, [newState](RawState) { return newState; });
    else {
        RawState prevState = m_state.load(std::memory_order_relaxed);
        const auto exchanged = multipleCompareExchange(m_state, prevState, checker, newState);

        if (Q_LIKELY(exchanged))
            return { this, newState(prevState), prevState };

        return {};
    }
}

Notifier QAudioStateMachine::stop(QAudio::Error error, bool shouldDrain, bool forceUpdateError)
{
    auto statesChecker =
            makeStatesChecker(QAudio::ActiveState, QAudio::IdleState, QAudio::SuspendedState,
                              forceUpdateError ? QAudio::StoppedState : QAudio::ActiveState);

    const auto state = toRawState(QAudio::StoppedState, error);
    auto getNewState = [&](RawState prevState) {
        const bool shouldAddFlag = shouldDrain && toAudioState(prevState) == QAudio::ActiveState;
        return shouldAddFlag ? addDrainingFlag(state) : state;
    };

    return changeState(statesChecker, getNewState);
}

Notifier QAudioStateMachine::start(bool active)
{
    return changeState(makeStatesChecker(QAudio::StoppedState),
                       toRawState(active ? QAudio::ActiveState : QAudio::IdleState));
}

bool QAudioStateMachine::isActiveOrIdle() const
{
    const auto state = this->state();
    return state == QAudio::ActiveState || state == QAudio::IdleState;
}

bool QAudioStateMachine::onDrained()
{
    return changeState(isDrainingState, removeDrainingFlag);
}

bool QAudioStateMachine::isDraining() const
{
    return isDrainingState(m_state.load(std::memory_order_acquire));
}

std::pair<bool, bool> QAudioStateMachine::getDrainedAndStopped() const
{
    const auto state = m_state.load(std::memory_order_acquire);
    return { !isDrainingState(state), toAudioState(state) == QAudio::StoppedState };
}

Notifier QAudioStateMachine::suspend()
{
    // Due to the current documentation, we set QAudio::NoError.
    // TBD: leave the previous error should be more reasonable (IgnoreError)
    const auto error = QAudio::NoError;
    auto result = changeState(makeStatesChecker(QAudio::ActiveState, QAudio::IdleState),
                              toRawState(QAudio::SuspendedState, error));

    if (result)
        m_suspendedInState = result.prevAudioState();

    return result;
}

Notifier QAudioStateMachine::resume()
{
    // Due to the current documentation, we set QAudio::NoError.
    // TBD: leave the previous error should be more reasonable (IgnoreError)
    const auto error = QAudio::NoError;
    return changeState(makeStatesChecker(QAudio::SuspendedState),
                       toRawState(m_suspendedInState, error));
}

Notifier QAudioStateMachine::activateFromIdle()
{
    return changeState(makeStatesChecker(QAudio::IdleState), toRawState(QAudio::ActiveState));
}

Notifier QAudioStateMachine::updateActiveOrIdle(bool isActive, QAudio::Error error)
{
    const auto state = isActive ? QAudio::ActiveState : QAudio::IdleState;
    return changeState(makeStatesChecker(QAudio::ActiveState, QAudio::IdleState),
                       toRawState(state, error));
}

Notifier QAudioStateMachine::setError(QAudio::Error error)
{
    auto fixState = [error](RawState prevState) { return setStateError(prevState, error); };
    return changeState([](RawState) { return true; }, fixState);
}

Notifier QAudioStateMachine::forceSetState(QAudio::State state, QAudio::Error error)
{
    return changeState([](RawState) { return true; }, toRawState(state, error));
}

void QAudioStateMachine::reset(RawState state, RawState prevState)
{
    auto notifier = m_notifier;

    const auto audioState = toAudioState(state);
    const auto audioError = toAudioError(state);

    if (toAudioState(prevState) != audioState && notifier)
        emit notifier->stateChanged(audioState);

    // check the notifier in case the object was deleted in
    if (toAudioError(prevState) != audioError && notifier)
        emit notifier->errorChanged(audioError);
}

QT_END_NAMESPACE