Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qaudiosystem.cpp
Go to the documentation of this file.
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
5
6#include <QtCore/qdebug.h>
7#include <QtMultimedia/qaudiosink.h>
8#include <QtMultimedia/qaudiosource.h>
9#include <QtMultimedia/private/qplatformaudiodevices_p.h>
10
12
13QPlatformAudioEndpointBase::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
20QPlatformAudioEndpointBase::~QPlatformAudioEndpointBase()
21 = default;
22
23void QPlatformAudioEndpointBase::setError(QAudio::Error err)
24{
25 if (err == m_error)
26 return;
27 m_error = err;
28}
29
30bool QPlatformAudioEndpointBase::isFormatSupported(const QAudioFormat &format) const
31{
32 return m_audioDevice.isFormatSupported(format);
33}
34
35void QPlatformAudioEndpointBase::updateStreamState(QAudio::State state)
36{
37 if (m_streamState == state)
38 return;
39
40 m_streamState = state;
41 inferState();
42}
43
44void QPlatformAudioEndpointBase::updateStreamIdle(bool idle, EmitStateSignal emitStateSignal)
45{
46 if (idle == m_streamIsIdle)
47 return;
48 m_streamIsIdle = idle;
49
50 if (emitStateSignal == EmitStateSignal::True)
51 inferState();
52}
53
54void QPlatformAudioEndpointBase::inferState()
55{
56 // The "state" is derived from two sources:
57 // * the m_streamState, as changed by start/stop/suspend/resume
58 // * the "idle" state of the stream, as detected by the ringbuffer level
59 //
60 // we combine these two sources to infer a user-visible "state"
61 using State = QtAudio::State;
62
63 State oldState = m_inferredState;
64
65 switch (m_streamState) {
66 case State::StoppedState:
67 m_inferredState = State::StoppedState;
68 break;
69 case State::SuspendedState:
70 m_inferredState = State::SuspendedState;
71 break;
72 case State::ActiveState:
73 m_inferredState = m_streamIsIdle ? State::IdleState : State::ActiveState;
74 break;
75
76 case State::IdleState:
77 qCritical() << "Users should not be able to set the state to Idle!";
78 Q_UNREACHABLE_RETURN();
79 }
80
81 if (oldState != m_inferredState)
82 emit stateChanged(m_inferredState);
83}
84
85QPlatformAudioSink::QPlatformAudioSink(QAudioDevice device, const QAudioFormat &format,
86 QObject *parent)
87 : QPlatformAudioEndpointBase(std::move(device), format, parent)
88{
89}
90
91QPlatformAudioSink::~QPlatformAudioSink()
92 = default;
93
94QPlatformAudioSink *QPlatformAudioSink::get(const QAudioSink &sink)
95{
96 return sink.d;
97}
98
99QPlatformAudioSource::QPlatformAudioSource(QAudioDevice device, const QAudioFormat &format,
100 QObject *parent)
101 : QPlatformAudioEndpointBase(std::move(device), format, parent)
102{
103}
104
105QPlatformAudioSource::~QPlatformAudioSource()
106 = default;
107
108QPlatformAudioSource *QPlatformAudioSource::get(const QAudioSource &source)
109{
110 return source.d;
111}
112
113QT_END_NAMESPACE
114
115#include "moc_qaudiosystem_p.cpp"
Combined button and popup list for selecting options.