Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qaudiostatemachine.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qaudiosystem_p.h"
6#include <qpointer.h>
7#include <qdebug.h>
8
10
12using namespace AudioStateMachineUtils;
13
17
19
21{
22 return toAudioState(m_state.load(std::memory_order_acquire));
23}
24
26{
27 return toAudioError(m_state.load(std::memory_order_acquire));
28}
29
30template <typename StatesChecker, typename NewState>
31Notifier QAudioStateMachine::changeState(const StatesChecker &checker, const NewState &newState)
32{
33 if constexpr (std::is_same_v<RawState, NewState>)
34 return changeState(checker, [newState](RawState) { return newState; });
35 else {
36 RawState prevState = m_state.load(std::memory_order_relaxed);
37 const auto exchanged = multipleCompareExchange(m_state, prevState, checker, newState);
38
39 if (Q_LIKELY(exchanged))
40 return { this, newState(prevState), prevState };
41
42 return {};
43 }
44}
45
46Notifier QAudioStateMachine::stop(QAudio::Error error, bool shouldDrain, bool forceUpdateError)
47{
48 auto statesChecker =
50 forceUpdateError ? QAudio::StoppedState : QAudio::ActiveState);
51
53 auto getNewState = [&](RawState prevState) {
54 const bool shouldAddFlag = shouldDrain && toAudioState(prevState) == QAudio::ActiveState;
55 return shouldAddFlag ? addDrainingFlag(state) : state;
56 };
57
58 return changeState(statesChecker, getNewState);
59}
60
66
68{
69 const auto state = this->state();
71}
72
77
79{
80 return isDrainingState(m_state.load(std::memory_order_acquire));
81}
82
83std::pair<bool, bool> QAudioStateMachine::getDrainedAndStopped() const
84{
85 const auto state = m_state.load(std::memory_order_acquire);
87}
88
90{
91 // Due to the current documentation, we set QAudio::NoError.
92 // TBD: leave the previous error should be more reasonable (IgnoreError)
93 const auto error = QAudio::NoError;
96
97 if (result)
98 m_suspendedInState = result.prevAudioState();
99
100 return result;
101}
102
104{
105 // Due to the current documentation, we set QAudio::NoError.
106 // TBD: leave the previous error should be more reasonable (IgnoreError)
107 const auto error = QAudio::NoError;
108 return changeState(makeStatesChecker(QAudio::SuspendedState),
109 toRawState(m_suspendedInState, error));
110}
111
116
123
125{
126 auto fixState = [error](RawState prevState) { return setStateError(prevState, error); };
127 return changeState([](RawState) { return true; }, fixState);
128}
129
134
135void QAudioStateMachine::reset(RawState state, RawState prevState)
136{
137 auto notifier = m_notifier;
138
139 const auto audioState = toAudioState(state);
140 const auto audioError = toAudioError(state);
141
142 if (toAudioState(prevState) != audioState && notifier)
143 emit notifier->stateChanged(audioState);
144
145 // check the notifier in case the object was deleted in
146 if (toAudioError(prevState) != audioError && notifier)
147 emit notifier->errorChanged(audioError);
148}
149
DarwinBluetooth::LECBManagerNotifier * notifier
bool isActive
QAudioStateMachine(QAudioStateChangeNotifier &notifier)
QAudio::Error error() const
Notifier start(bool isActive=true)
Notifier updateActiveOrIdle(bool isActive, QAudio::Error error=QAudio::NoError)
QAudio::State state() const
Notifier setError(QAudio::Error error)
Notifier stop(QAudio::Error error=QAudio::NoError, bool shouldDrain=false, bool forceUpdateError=false)
Notifier forceSetState(QAudio::State state, QAudio::Error error=QAudio::NoError)
AudioStateMachineUtils::RawState RawState
std::pair< bool, bool > getDrainedAndStopped() const
else opt state
[0]
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
constexpr bool isDrainingState(RawState state)
constexpr auto makeStatesChecker(States... states)
constexpr RawState removeDrainingFlag(RawState state)
constexpr QAudio::State toAudioState(RawState state)
constexpr RawState setStateError(RawState state, QAudio::Error error)
constexpr RawState toRawState(QAudio::State state, QAudio::Error error=QAudio::NoError)
constexpr RawState addDrainingFlag(RawState state)
constexpr QAudio::Error toAudioError(RawState state)
bool multipleCompareExchange(std::atomic< T > &target, T &prevValue, Predicate predicate, NewValueGetter newValueGetter)
State
Definition qaudio.h:29
@ StoppedState
Definition qaudio.h:29
@ SuspendedState
Definition qaudio.h:29
@ IdleState
Definition qaudio.h:29
@ ActiveState
Definition qaudio.h:29
Error
Definition qaudio.h:28
@ NoError
Definition qaudio.h:28
Combined button and popup list for selecting options.
#define Q_LIKELY(x)
DBusConnection const char DBusError * error
GLuint64EXT * result
[6]
#define emit