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
qandroidaudiosource.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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 <QtMultimedia/private/qandroidaudiosource_p.h>
5
6#include <QtCore/qcoreapplication.h>
7#include <QtCore/qpermissions.h>
8
9#include <QtMultimedia/private/qandroidaudiojnitypes_p.h>
10#include <QtMultimedia/private/qandroidaudioutil_p.h>
11
12QT_BEGIN_NAMESPACE
13
14namespace QtAAudio {
15
16Q_STATIC_LOGGING_CATEGORY(qLcAndroidAudioSource, "qt.multimedia.android.audiosource")
17
19 const QAudioFormat &format,
26{
28
29 qCDebug(qLcAndroidAudioSource) << "Creating source for device id:" << m_audioDevice.id()
30 << ", description:" << m_audioDevice.description();
31
34
35 // Set buffer parameters
38 : 1024;
39
40 // NOTE: AAudio doesn't support UINT8, so convert to INT16 if that's requested
44 }
45
46 // Set builder parameters for audio source
49
50 // TODO: Set input preset based on device
51
52 builder.userData = this;
53 builder.callback = [](AAudioStream *, void *userData, void *audioData,
54 int32_t numFrames) -> int {
55 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
60 };
62 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
65 };
66
68
69 if (!QtJniTypes::QtAudioDeviceManager::callStaticMethod<jboolean>("prepareAudioInput",
71 qCWarning(qLcAndroidAudioSource) << "Preparation failed for device:" << m_audioDevice.id().toInt();
72
75 // Original sample format unsupported, so doing sample format conversion
78 }
79}
80
86
88{
90
92 if (!permitted) {
93 qWarning("Missing microphone permission!");
95 return false;
96 }
97
98 if (!m_stream->isOpen()) {
99 qCWarning(qLcAndroidAudioSource) << "Stream null";
100 requestStop();
101 return false;
102 }
103
105 qCWarning(qLcAndroidAudioSource) << "Stream parameters not correct";
106
107 return true;
108}
109
111{
115
116 if (!m_stream->start()) {
117 requestStop();
118 return false;
119 }
120
121 return true;
122}
123
129
131{
134
135 if (!m_stream->start()) {
136 requestStop();
137 return false;
138 }
139
140 return true;
141}
142
148
154
168
174
175QSpan<const std::byte>
177 int numFrames) const noexcept Q_DECL_NONBLOCKING_FUNCTION
178{
181 return QSpan{ reinterpret_cast<const std::byte *>(audioData), byteAmount };
182}
183
187{
193
194 if (framesWritten != static_cast<uint64_t>(numFrames) && isStopRequested())
196
198}
199
202{
203 if (isStopRequested())
205
206 if (m_hostFormat)
208 *m_hostFormat);
209 else
211
213}
214
216{
217 // Handle as IO error which closes the stream
218 requestStop();
219 invokeOnAppThread([this] {
220 // clang-format off
222 // clang-format on
223 });
224}
225
231
233 = default;
234
235} // namespace QtAAudio
236
237QT_END_NAMESPACE