| 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 <QtCore/qdebug.h> |
| 7 | #include <QtMultimedia/qaudiosink.h> |
| 8 | #include <QtMultimedia/qaudiosource.h> |
| 9 | #include <QtMultimedia/private/qplatformaudiodevices_p.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | QPlatformAudioEndpointBase::QPlatformAudioEndpointBase(QAudioDevice device, |
| 14 | const QAudioFormat &format, QObject *parent) |
| 15 | : QObject{ parent }, m_audioDevice{ std::move(device) }, m_format{ format } |
| 16 | { |
| 17 | Q_ASSERT(parent && "QPlatformAudioEndpointBase requires the QAudioSink/QAudioSource as parent" ); |
| 18 | } |
| 19 | |
| 20 | void QPlatformAudioEndpointBase::setError(QAudio::Error err) |
| 21 | { |
| 22 | if (err == m_error) |
| 23 | return; |
| 24 | m_error = err; |
| 25 | } |
| 26 | |
| 27 | bool QPlatformAudioEndpointBase::isFormatSupported(const QAudioFormat &format) const |
| 28 | { |
| 29 | return m_audioDevice.isFormatSupported(format); |
| 30 | } |
| 31 | |
| 32 | void QPlatformAudioEndpointBase::updateStreamState(QAudio::State state) |
| 33 | { |
| 34 | if (m_streamState == state) |
| 35 | return; |
| 36 | |
| 37 | m_streamState = state; |
| 38 | inferState(); |
| 39 | } |
| 40 | |
| 41 | void QPlatformAudioEndpointBase::updateStreamIdle(bool idle, EmitStateSignal emitStateSignal) |
| 42 | { |
| 43 | if (idle == m_streamIsIdle) |
| 44 | return; |
| 45 | m_streamIsIdle = idle; |
| 46 | |
| 47 | if (emitStateSignal == EmitStateSignal::True) |
| 48 | inferState(); |
| 49 | } |
| 50 | |
| 51 | void QPlatformAudioEndpointBase::inferState() |
| 52 | { |
| 53 | // The "state" is derived from two sources: |
| 54 | // * the m_streamState, as changed by start/stop/suspend/resume |
| 55 | // * the "idle" state of the stream, as detected by the ringbuffer level |
| 56 | // |
| 57 | // we combine these two sources to infer a user-visible "state" |
| 58 | using State = QtAudio::State; |
| 59 | |
| 60 | State oldState = m_inferredState; |
| 61 | |
| 62 | switch (m_streamState) { |
| 63 | case State::StoppedState: |
| 64 | m_inferredState = State::StoppedState; |
| 65 | break; |
| 66 | case State::SuspendedState: |
| 67 | m_inferredState = State::SuspendedState; |
| 68 | break; |
| 69 | case State::ActiveState: |
| 70 | m_inferredState = m_streamIsIdle ? State::IdleState : State::ActiveState; |
| 71 | break; |
| 72 | |
| 73 | case State::IdleState: |
| 74 | qCritical() << "Users should not be able to set the state to Idle!" ; |
| 75 | Q_UNREACHABLE_RETURN(); |
| 76 | } |
| 77 | |
| 78 | if (oldState != m_inferredState) |
| 79 | emit stateChanged(m_inferredState); |
| 80 | } |
| 81 | |
| 82 | QPlatformAudioSink::QPlatformAudioSink(QAudioDevice device, const QAudioFormat &format, |
| 83 | QObject *parent) |
| 84 | : QPlatformAudioEndpointBase(std::move(device), format, parent) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | QPlatformAudioSink *QPlatformAudioSink::get(const QAudioSink &sink) |
| 89 | { |
| 90 | return sink.d; |
| 91 | } |
| 92 | |
| 93 | QPlatformAudioSource::QPlatformAudioSource(QAudioDevice device, const QAudioFormat &format, |
| 94 | QObject *parent) |
| 95 | : QPlatformAudioEndpointBase(std::move(device), format, parent) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | QPlatformAudioSource *QPlatformAudioSource::get(const QAudioSource &source) |
| 100 | { |
| 101 | return source.d; |
| 102 | } |
| 103 | |
| 104 | QT_END_NAMESPACE |
| 105 | |
| 106 | #include "moc_qaudiosystem_p.cpp" |
| 107 | |