| 1 | // Copyright (C) 2022 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 | |
| 4 | #include "qaudiosystem_p.h" |
| 5 | |
| 6 | #include <private/qplatformaudiodevices_p.h> |
| 7 | |
| 8 | #include <QtCore/qdebug.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QAudioStateChangeNotifier::QAudioStateChangeNotifier(QObject *parent) : QObject(parent) { } |
| 13 | |
| 14 | QPlatformAudioEndpointBase::QPlatformAudioEndpointBase(QObject *parent) |
| 15 | : QAudioStateChangeNotifier(parent) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void QPlatformAudioEndpointBase::setError(QAudio::Error err) |
| 20 | { |
| 21 | if (err == m_error) |
| 22 | return; |
| 23 | m_error = err; |
| 24 | emit errorChanged(error: err); |
| 25 | } |
| 26 | |
| 27 | void QPlatformAudioEndpointBase::updateStreamState(QAudio::State state) |
| 28 | { |
| 29 | if (m_streamState == state) |
| 30 | return; |
| 31 | |
| 32 | m_streamState = state; |
| 33 | inferState(); |
| 34 | } |
| 35 | |
| 36 | void QPlatformAudioEndpointBase::updateStreamIdle(bool idle, EmitStateSignal emitStateSignal) |
| 37 | { |
| 38 | if (idle == m_streamIsIdle) |
| 39 | return; |
| 40 | m_streamIsIdle = idle; |
| 41 | |
| 42 | if (emitStateSignal == EmitStateSignal::True) |
| 43 | inferState(); |
| 44 | } |
| 45 | |
| 46 | void QPlatformAudioEndpointBase::inferState() |
| 47 | { |
| 48 | // The "state" is derived from two sources: |
| 49 | // * the m_streamState, as changed by start/stop/suspend/resume |
| 50 | // * the "idle" state of the stream, as detected by the ringbuffer level |
| 51 | // |
| 52 | // we combine these two sources to infer a user-visible "state" |
| 53 | using State = QtAudio::State; |
| 54 | |
| 55 | State oldState = m_inferredState; |
| 56 | |
| 57 | switch (m_streamState) { |
| 58 | case State::StoppedState: |
| 59 | m_inferredState = State::StoppedState; |
| 60 | break; |
| 61 | case State::SuspendedState: |
| 62 | m_inferredState = State::SuspendedState; |
| 63 | break; |
| 64 | case State::ActiveState: |
| 65 | m_inferredState = m_streamIsIdle ? State::IdleState : State::ActiveState; |
| 66 | break; |
| 67 | |
| 68 | case State::IdleState: |
| 69 | qCritical() << "Users should not be able to set the state to Idle!"; |
| 70 | Q_UNREACHABLE_RETURN(); |
| 71 | } |
| 72 | |
| 73 | if (oldState != m_inferredState) |
| 74 | emit stateChanged(state: m_inferredState); |
| 75 | } |
| 76 | |
| 77 | QPlatformAudioSink::QPlatformAudioSink(QObject *parent) : QPlatformAudioEndpointBase(parent) { } |
| 78 | |
| 79 | qreal QPlatformAudioSink::volume() const |
| 80 | { |
| 81 | return 1.0; |
| 82 | } |
| 83 | |
| 84 | QPlatformAudioSource::QPlatformAudioSource(QObject *parent) : QPlatformAudioEndpointBase(parent) { } |
| 85 | |
| 86 | QT_END_NAMESPACE |
| 87 | |
| 88 | #include "moc_qaudiosystem_p.cpp" |
| 89 |
