Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
12
QT_BEGIN_NAMESPACE
13
14
namespace
QtAAudio
{
15
16
Q_STATIC_LOGGING_CATEGORY(
qLcAndroidAudioSource
,
"qt.multimedia.android.audiosource"
)
17
18
QAndroidAudioSourceStream
::
QAndroidAudioSourceStream
(
QAudioDevice
device
,
19
const
QAudioFormat
&
format
,
20
std
::
optional
<
int
>
ringbufferSize
,
21
QAndroidAudioSource
*
parent
,
float
volume
,
22
std
::
optional
<
NativePeriodFrames
>
nativePeriodFrames
)
23
:
QtMultimediaPrivate
::
QPlatformAudioSourceStream
(
std
::
move
(
device
),
format
,
ringbufferSize
,
24
nativePeriodFrames
,
volume
),
25
m_parent
(
parent
)
26
{
27
QtAAudio
::
StreamBuilder
builder
(
format
);
28
29
qCDebug
(
qLcAndroidAudioSource
) <<
"Creating source for device id:"
<<
m_audioDevice
.
id
()
30
<<
", description:"
<<
m_audioDevice
.
description
();
31
32
builder
.
deviceId
=
m_audioDevice
.
id
().
toInt
();
33
builder
.
deviceIsBluetooth
=
QAndroidAudioUtil
::
isBluetoothDevice
(
m_audioDevice
);
34
35
// Set buffer parameters
36
builder
.
bufferCapacity
=
m_nativePeriodFrames
37
?
qToUnderlying
(*
m_nativePeriodFrames
)
38
: 1024;
39
40
// NOTE: AAudio doesn't support UINT8, so convert to INT16 if that's requested
41
if
(
format
.
sampleFormat
() ==
QAudioFormat
::
UInt8
) {
42
m_hostFormat
=
format
;
43
m_hostFormat
->
setSampleFormat
(
QAudioFormat
::
Int16
);
44
}
45
46
// Set builder parameters for audio source
47
builder
.
params
.
sharingMode
=
AAUDIO_SHARING_MODE_SHARED
;
48
builder
.
params
.
direction
=
AAUDIO_DIRECTION_INPUT
;
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
);
56
Q_ASSERT
(
stream
);
57
auto
audioSpan
=
stream
->
getHostSpan
(
audioData
,
numFrames
);
58
return
stream
->
m_audioCallback
?
stream
->
processCallback
(
audioSpan
)
59
:
stream
->
processRingbuffer
(
audioSpan
,
numFrames
);
60
};
61
builder
.
errorCallback
= [](
AAudioStream
*,
void
*
userData
,
aaudio_result_t
error
) ->
void
{
62
auto
*
stream
=
reinterpret_cast
<
QAndroidAudioSourceStream
*>(
userData
);
63
Q_ASSERT
(
stream
);
64
stream
->
handleError
(
error
);
65
};
66
67
builder
.
setupBuilder
();
68
69
if
(!
QtJniTypes
::
QtAudioDeviceManager
::
callStaticMethod
<
jboolean
>(
"prepareAudioInput"
,
70
m_audioDevice
.
id
().
toInt
()))
71
qCWarning
(
qLcAndroidAudioSource
) <<
"Preparation failed for device:"
<<
m_audioDevice
.
id
().
toInt
();
72
73
m_stream
=
std
::
make_unique
<
QtAAudio
::
Stream
>(
builder
);
74
if
(
builder
.
format
.
sampleFormat
() !=
format
.
sampleFormat
()) {
75
// Original sample format unsupported, so doing sample format conversion
76
Q_ASSERT
(
builder
.
format
.
sampleFormat
() ==
QAudioFormat
::
Float
);
77
m_hostFormat
=
builder
.
format
;
78
}
79
}
80
81
QAndroidAudioSourceStream
::~
QAndroidAudioSourceStream
()
82
{
83
QtJniTypes
::
QtAudioDeviceManager
::
callStaticMethod
<
void
>(
"releaseAudioDevice"
,
84
m_audioDevice
.
id
().
toInt
());
85
}
86
87
bool
QAndroidAudioSourceStream
::
open
()
88
{
89
QMicrophonePermission
permission
;
90
91
const
bool
permitted
=
qApp
->
checkPermission
(
permission
) ==
Qt
::
PermissionStatus
::
Granted
;
92
if
(!
permitted
) {
93
qWarning
(
"Missing microphone permission!"
);
94
requestStop
();
95
return
false
;
96
}
97
98
if
(!
m_stream
->
isOpen
()) {
99
qCWarning
(
qLcAndroidAudioSource
) <<
"Stream null"
;
100
requestStop
();
101
return
false
;
102
}
103
104
if
(!
m_stream
->
areStreamParametersRespected
())
105
qCWarning
(
qLcAndroidAudioSource
) <<
"Stream parameters not correct"
;
106
107
return
true
;
108
}
109
110
bool
QAndroidAudioSourceStream
::
start
(
QIODevice
*
device
)
111
{
112
Q_ASSERT
(
thread
()->
isCurrentThread
());
113
setQIODevice
(
device
);
114
createQIODeviceConnections
(
device
);
115
116
if
(!
m_stream
->
start
()) {
117
requestStop
();
118
return
false
;
119
}
120
121
return
true
;
122
}
123
124
QIODevice
*
QAndroidAudioSourceStream
::
start
()
125
{
126
auto
*
device
=
createRingbufferReaderDevice
();
127
return
start
(
device
) ?
device
:
nullptr
;
128
}
129
130
bool
QAndroidAudioSourceStream
::
start
(
AudioCallback
&&
callback
)
131
{
132
Q_ASSERT
(
thread
()->
isCurrentThread
());
133
m_audioCallback
=
std
::
move
(
callback
);
134
135
if
(!
m_stream
->
start
()) {
136
requestStop
();
137
return
false
;
138
}
139
140
return
true
;
141
}
142
143
void
QAndroidAudioSourceStream
::
suspend
()
144
{
145
Q_ASSERT
(
thread
()->
isCurrentThread
());
146
m_stream
->
stop
();
147
}
148
149
void
QAndroidAudioSourceStream
::
resume
()
150
{
151
Q_ASSERT
(
thread
()->
isCurrentThread
());
152
m_stream
->
start
();
153
}
154
155
void
QAndroidAudioSourceStream
::
stop
(
ShutdownPolicy
policy
)
156
{
157
Q_ASSERT
(
thread
()->
isCurrentThread
());
158
requestStop
();
159
160
m_stream
->
stop
();
161
162
disconnectQIODeviceConnections
();
163
finalizeQIODevice
(
policy
);
164
165
if
(
policy
==
ShutdownPolicy
::
DiscardRingbuffer
)
166
emptyRingbuffer
();
167
}
168
169
void
QAndroidAudioSourceStream
::
updateStreamIdle
(
bool
idle
)
170
{
171
if
(
m_parent
)
172
m_parent
->
updateStreamIdle
(
idle
);
173
}
174
175
QSpan
<
const
std
::
byte
>
176
QAndroidAudioSourceStream
::
getHostSpan
(
void
*
audioData
,
177
int
numFrames
)
const
noexcept
Q_DECL_NONBLOCKING_FUNCTION
178
{
179
qsizetype
byteAmount
=
m_hostFormat
?
m_hostFormat
->
bytesForFrames
(
numFrames
)
180
:
m_format
.
bytesForFrames
(
numFrames
);
181
return
QSpan
{
reinterpret_cast
<
const
std
::
byte
*>(
audioData
),
byteAmount
};
182
}
183
184
aaudio_data_callback_result_t
185
QAndroidAudioSourceStream
::
processRingbuffer
(
QSpan
<
const
std
::
byte
>
audioSpan
,
186
int
numFrames
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
187
{
188
auto
framesWritten
=
m_hostFormat
189
?
QPlatformAudioSourceStream
::
process
(
190
audioSpan
,
numFrames
,
191
QAudioHelperInternal
::
toNativeSampleFormat
(
m_hostFormat
->
sampleFormat
()))
192
:
QPlatformAudioSourceStream
::
process
(
audioSpan
,
numFrames
);
193
194
if
(
framesWritten
!=
static_cast
<
uint64_t
>(
numFrames
) &&
isStopRequested
())
195
return
AAUDIO_CALLBACK_RESULT_STOP
;
196
197
return
AAUDIO_CALLBACK_RESULT_CONTINUE
;
198
}
199
200
aaudio_data_callback_result_t
201
QAndroidAudioSourceStream
::
processCallback
(
QSpan
<
const
std
::
byte
>
audioSpan
)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
202
{
203
if
(
isStopRequested
())
204
return
AAUDIO_CALLBACK_RESULT_STOP
;
205
206
if
(
m_hostFormat
)
207
QtMultimediaPrivate
::
runAudioCallback
(*
m_audioCallback
,
audioSpan
,
m_format
,
volume
(),
208
*
m_hostFormat
);
209
else
210
QtMultimediaPrivate
::
runAudioCallback
(*
m_audioCallback
,
audioSpan
,
m_format
,
volume
());
211
212
return
AAUDIO_CALLBACK_RESULT_CONTINUE
;
213
}
214
215
void
QAndroidAudioSourceStream
::
handleError
(
aaudio_result_t
)
216
{
217
// Handle as IO error which closes the stream
218
requestStop
();
219
invokeOnAppThread
([
this
] {
220
// clang-format off
221
handleIOError
(
m_parent
);
222
// clang-format on
223
});
224
}
225
226
QAndroidAudioSource
::
QAndroidAudioSource
(
QAudioDevice
device
,
const
QAudioFormat
&
format
,
227
QObject
*
parent
)
228
:
BaseClass
(
std
::
move
(
device
),
format
,
parent
)
229
{
230
}
231
232
QAndroidAudioSource
::~
QAndroidAudioSource
()
233
=
default
;
234
235
}
// namespace QtAAudio
236
237
QT_END_NAMESPACE
QtAAudio
Definition
qaaudiostream.cpp:60
qtmultimedia
src
multimedia
android
qandroidaudiosource.cpp
Generated on
for Qt by
1.18.0