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
37 : 1024;
38
39 // NOTE: AAudio doesn't support UINT8, so convert to INT16 if that's requested
43 }
44
45 // Set builder parameters for audio source
48
49 // TODO: Set input preset based on device
50
51 builder.userData = this;
52 builder.callback = [](AAudioStream *, void *userData, void *audioData,
53 int32_t numFrames) -> int {
54 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
59 };
61 auto *stream = reinterpret_cast<QAndroidAudioSourceStream *>(userData);
64 };
65
67
68 if (!QtJniTypes::QtAudioDeviceManager::callStaticMethod<jboolean>("prepareAudioInput",
70 qCWarning(qLcAndroidAudioSource) << "Preparation failed for device:" << m_audioDevice.id().toInt();
71
74 // Original sample format unsupported, so doing sample format conversion
77 }
78}
79
85
87{
89
91 if (!permitted) {
92 qWarning("Missing microphone permission!");
94 return false;
95 }
96
97 if (!m_stream->isOpen()) {
98 qCWarning(qLcAndroidAudioSource) << "Stream null";
100 return false;
101 }
102
104 qCWarning(qLcAndroidAudioSource) << "Stream parameters not correct";
105
106 return true;
107}
108
110{
114
115 if (!m_stream->start()) {
116 requestStop();
117 return false;
118 }
119
120 return true;
121}
122
128
130{
133
134 if (!m_stream->start()) {
135 requestStop();
136 return false;
137 }
138
139 return true;
140}
141
147
153
167
173
174QSpan<const std::byte>
176 int numFrames) const noexcept QT_MM_NONBLOCKING
177{
180 return QSpan{ reinterpret_cast<const std::byte *>(audioData), byteAmount };
181}
182
185 int numFrames) noexcept QT_MM_NONBLOCKING
186{
192
193 if (framesWritten != static_cast<uint64_t>(numFrames) && isStopRequested())
195
197}
198
201{
202 if (isStopRequested())
204
205 if (m_hostFormat)
207 *m_hostFormat);
208 else
210
212}
213
215{
216 // Handle as IO error which closes the stream
217 requestStop();
218 invokeOnAppThread([this] {
219 // clang-format off
221 // clang-format on
222 });
223}
224
230
232 = default;
233
234} // namespace QtAAudio
235
236QT_END_NAMESPACE