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
33
34 // Set buffer parameters
36
37 // NOTE: AAudio doesn't support UINT8, so convert to INT16 if that's requested
41 }
42
43 // Set builder parameters for audio source
46
47 // TODO: Set input preset based on device
48
49 builder.userData = this;
50 builder.callback = [](AAudioStream *, void *userData, void *audioData,
51 int32_t numFrames) -> int {
52 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
57 };
59 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
62 };
63
65
66 if (!QtJniTypes::QtAudioDeviceManager::callStaticMethod<jboolean>("prepareAudioInput",
68 qCWarning(qLcAndroidAudioSource) << "Preparation failed for device:" << m_audioDevice.id().toInt();
69
72 // Original sample format unsupported, so doing sample format conversion
75 }
76}
77
83
85{
87
89 if (!permitted) {
90 qWarning("Missing microphone permission!");
92 return false;
93 }
94
95 if (!m_stream->isOpen()) {
96 qCWarning(qLcAndroidAudioSource) << "Stream null";
98 return false;
99 }
100
102 qCWarning(qLcAndroidAudioSource) << "Stream parameters not correct";
103
104 return true;
105}
106
108{
112
113 if (!m_stream->start()) {
114 requestStop();
115 return false;
116 }
117
118 return true;
119}
120
126
128{
131
132 if (!m_stream->start()) {
133 requestStop();
134 return false;
135 }
136
137 return true;
138}
139
145
151
165
171
172QSpan<const std::byte>
174 int numFrames) const noexcept QT_MM_NONBLOCKING
175{
178 return QSpan{ reinterpret_cast<const std::byte *>(audioData), byteAmount };
179}
180
183 int numFrames) noexcept QT_MM_NONBLOCKING
184{
190
191 if (framesWritten != static_cast<uint64_t>(numFrames) && isStopRequested())
193
195}
196
199{
200 if (isStopRequested())
202
203 if (m_hostFormat)
205 *m_hostFormat);
206 else
208
210}
211
213{
214 // Handle as IO error which closes the stream
215 requestStop();
216 invokeOnAppThread([this] {
217 // clang-format off
219 // clang-format on
220 });
221}
222
228
230 = default;
231
232} // namespace QtAAudio
233
234QT_END_NAMESPACE