summaryrefslogtreecommitdiffstats
path: root/src/multimedia/audio/qaudiostatemachine.cpp
blob: 83716efd2e8e1b35303868bbd43409576a4fcc7d (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 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;

struct QAudioStateMachine::Synchronizer {
    QMutex m_mutex;
    QWaitCondition m_condition;

    template <typename Changer>
    void changeState(Changer&& changer) {
        {
            QMutexLocker locker(&m_mutex);
            changer();
        }

        m_condition.notify_all();
    }

    void waitForOperationFinished(std::atomic<RawState>& state)
    {
        QMutexLocker locker(&m_mutex);
        while (isWaitingState(state))
            m_condition.wait(&m_mutex);
    }

    void waitForDrained(std::atomic<RawState>& state, std::chrono::milliseconds timeout) {
        QMutexLocker locker(&m_mutex);
        if (isDrainingState(state))
            m_condition.wait(&m_mutex, timeout.count());
    }
};

QAudioStateMachine::QAudioStateMachine(QAudioStateChangeNotifier &notifier, bool synchronize) :
    m_notifier(&notifier),
    m_sychronizer(synchronize ? std::make_unique<Synchronizer>() : nullptr)
{
}

QAudioStateMachine::~QAudioStateMachine() = default;

QAudio::State QAudioStateMachine::state() const
{
    return toAudioState(m_state);
}

QAudio::Error QAudioStateMachine::error() const
{
    return m_error;
}

Notifier QAudioStateMachine::changeState(std::pair<RawState, uint32_t> prevStatesSet,
                                         RawState newState, QAudio::Error error, bool shouldDrain)
{
    auto checkState = [flags = prevStatesSet.second](RawState state) {
        return (flags >> state) & 1;
    };

    if (!m_sychronizer) {
        RawState prevState = prevStatesSet.first;
        const auto exchanged = multipleCompareExchange(
                m_state, prevState, newState, checkState);

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

        return {};
    }

    while (true) {
        RawState prevState = prevStatesSet.first;

        const auto newWaitingState = newState | (shouldDrain ? WaitingFlags : InProgressFlag);

        const auto exchanged = multipleCompareExchange(
                m_state, prevState, newWaitingState, [checkState](RawState state) {
                    return !isWaitingState(state) && checkState(state);
                });

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

        if (!isWaitingState(prevState))
            return {};

        if (!checkState(fromWaitingState(prevState)))
            return {};

        m_sychronizer->waitForOperationFinished(m_state);
    }
}

Notifier QAudioStateMachine::stop(QAudio::Error error, bool shouldDrain, bool forceUpdateError)
{
    auto result = changeState(
            makeStatesSet(QAudio::ActiveState, QAudio::IdleState, QAudio::SuspendedState),
            QAudio::StoppedState, error, shouldDrain);

    if (!result && forceUpdateError)
        setError(error);

    return result;
}

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

void QAudioStateMachine::waitForDrained(std::chrono::milliseconds timeout)
{
    if (m_sychronizer)
        m_sychronizer->waitForDrained(m_state, timeout);
}

void QAudioStateMachine::onDrained()
{
    if (m_sychronizer)
        m_sychronizer->changeState([this]() { m_state &= ~DrainingFlag; });
}

bool QAudioStateMachine::isDraining() const
{
    return isDrainingState(m_state);
}

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

std::pair<bool, bool> QAudioStateMachine::getDrainedAndStopped() const
{
    const RawState state = m_state;
    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(makeStatesSet(QAudio::ActiveState, QAudio::IdleState),
                              QAudio::SuspendedState, error);

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

    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(makeStatesSet(QAudio::SuspendedState), m_suspendedInState, error);
}

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

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

void QAudioStateMachine::setError(QAudio::Error error)
{
    if (m_error.exchange(error) != error && m_notifier)
        emit m_notifier->errorChanged(error);
}

Notifier QAudioStateMachine::forceSetState(QAudio::State state, QAudio::Error error)
{
    return changeState(makeStatesSet(QAudio::ActiveState, QAudio::IdleState, QAudio::SuspendedState,
                                     QAudio::StoppedState),
                       state, error);
}

void QAudioStateMachine::reset(RawState state, RawState prevState, QAudio::Error error)
{
    Q_ASSERT(!isWaitingState(state));

    if (!m_sychronizer && m_state != state)
        return;

    const auto isErrorChanged = m_error.exchange(error) != error;

    if (m_sychronizer)
        m_sychronizer->changeState([&](){ m_state = state; });

    auto notifier = m_notifier;

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

    // check the notifier in case the object was deleted in
    if (isErrorChanged && notifier)
        emit notifier->errorChanged(error);
}

QT_END_NAMESPACE