author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtMultimedia module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
// |
|
43 |
// W A R N I N G |
|
44 |
// ------------- |
|
45 |
// |
|
46 |
// This file is not part of the Qt API. It exists for the convenience |
|
47 |
// of other Qt classes. This header file may change from version to |
|
48 |
// version without notice, or even be removed. |
|
49 |
// |
|
50 |
// We mean it. |
|
51 |
// |
|
52 |
||
53 |
#include <QtCore/qendian.h> |
|
54 |
#include <QtCore/qtimer.h> |
|
55 |
#include <QtCore/qdebug.h> |
|
56 |
||
57 |
#include <QtMultimedia/qaudioinput.h> |
|
58 |
||
59 |
#include "qaudio_mac_p.h" |
|
60 |
#include "qaudioinput_mac_p.h" |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
61 |
#include "qaudiodeviceinfo_mac_p.h" |
0 | 62 |
|
63 |
QT_BEGIN_NAMESPACE |
|
64 |
||
65 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
66 |
namespace QtMultimediaInternal |
0 | 67 |
{ |
68 |
||
69 |
static const int default_buffer_size = 4 * 1024; |
|
70 |
||
71 |
class QAudioBufferList |
|
72 |
{ |
|
73 |
public: |
|
74 |
QAudioBufferList(AudioStreamBasicDescription const& streamFormat): |
|
75 |
owner(false), |
|
76 |
sf(streamFormat) |
|
77 |
{ |
|
78 |
const bool isInterleaved = (sf.mFormatFlags & kAudioFormatFlagIsNonInterleaved) == 0; |
|
79 |
const int numberOfBuffers = isInterleaved ? 1 : sf.mChannelsPerFrame; |
|
80 |
||
81 |
dataSize = 0; |
|
82 |
||
83 |
bfs = reinterpret_cast<AudioBufferList*>(qMalloc(sizeof(AudioBufferList) + |
|
84 |
(sizeof(AudioBuffer) * numberOfBuffers))); |
|
85 |
||
86 |
bfs->mNumberBuffers = numberOfBuffers; |
|
87 |
for (int i = 0; i < numberOfBuffers; ++i) { |
|
88 |
bfs->mBuffers[i].mNumberChannels = isInterleaved ? numberOfBuffers : 1; |
|
89 |
bfs->mBuffers[i].mDataByteSize = 0; |
|
90 |
bfs->mBuffers[i].mData = 0; |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
QAudioBufferList(AudioStreamBasicDescription const& streamFormat, char* buffer, int bufferSize): |
|
95 |
owner(false), |
|
96 |
sf(streamFormat), |
|
97 |
bfs(0) |
|
98 |
{ |
|
99 |
dataSize = bufferSize; |
|
100 |
||
101 |
bfs = reinterpret_cast<AudioBufferList*>(qMalloc(sizeof(AudioBufferList) + sizeof(AudioBuffer))); |
|
102 |
||
103 |
bfs->mNumberBuffers = 1; |
|
104 |
bfs->mBuffers[0].mNumberChannels = 1; |
|
105 |
bfs->mBuffers[0].mDataByteSize = dataSize; |
|
106 |
bfs->mBuffers[0].mData = buffer; |
|
107 |
} |
|
108 |
||
109 |
QAudioBufferList(AudioStreamBasicDescription const& streamFormat, int framesToBuffer): |
|
110 |
owner(true), |
|
111 |
sf(streamFormat), |
|
112 |
bfs(0) |
|
113 |
{ |
|
114 |
const bool isInterleaved = (sf.mFormatFlags & kAudioFormatFlagIsNonInterleaved) == 0; |
|
115 |
const int numberOfBuffers = isInterleaved ? 1 : sf.mChannelsPerFrame; |
|
116 |
||
117 |
dataSize = framesToBuffer * sf.mBytesPerFrame; |
|
118 |
||
119 |
bfs = reinterpret_cast<AudioBufferList*>(qMalloc(sizeof(AudioBufferList) + |
|
120 |
(sizeof(AudioBuffer) * numberOfBuffers))); |
|
121 |
bfs->mNumberBuffers = numberOfBuffers; |
|
122 |
for (int i = 0; i < numberOfBuffers; ++i) { |
|
123 |
bfs->mBuffers[i].mNumberChannels = isInterleaved ? numberOfBuffers : 1; |
|
124 |
bfs->mBuffers[i].mDataByteSize = dataSize; |
|
125 |
bfs->mBuffers[i].mData = qMalloc(dataSize); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
~QAudioBufferList() |
|
130 |
{ |
|
131 |
if (owner) { |
|
132 |
for (UInt32 i = 0; i < bfs->mNumberBuffers; ++i) |
|
133 |
qFree(bfs->mBuffers[i].mData); |
|
134 |
} |
|
135 |
||
136 |
qFree(bfs); |
|
137 |
} |
|
138 |
||
139 |
AudioBufferList* audioBufferList() const |
|
140 |
{ |
|
141 |
return bfs; |
|
142 |
} |
|
143 |
||
144 |
char* data(int buffer = 0) const |
|
145 |
{ |
|
146 |
return static_cast<char*>(bfs->mBuffers[buffer].mData); |
|
147 |
} |
|
148 |
||
149 |
qint64 bufferSize(int buffer = 0) const |
|
150 |
{ |
|
151 |
return bfs->mBuffers[buffer].mDataByteSize; |
|
152 |
} |
|
153 |
||
154 |
int frameCount(int buffer = 0) const |
|
155 |
{ |
|
156 |
return bfs->mBuffers[buffer].mDataByteSize / sf.mBytesPerFrame; |
|
157 |
} |
|
158 |
||
159 |
int packetCount(int buffer = 0) const |
|
160 |
{ |
|
161 |
return bfs->mBuffers[buffer].mDataByteSize / sf.mBytesPerPacket; |
|
162 |
} |
|
163 |
||
164 |
int packetSize() const |
|
165 |
{ |
|
166 |
return sf.mBytesPerPacket; |
|
167 |
} |
|
168 |
||
169 |
void reset() |
|
170 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
171 |
for (UInt32 i = 0; i < bfs->mNumberBuffers; ++i) { |
0 | 172 |
bfs->mBuffers[i].mDataByteSize = dataSize; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
173 |
bfs->mBuffers[i].mData = 0; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
174 |
} |
0 | 175 |
} |
176 |
||
177 |
private: |
|
178 |
bool owner; |
|
179 |
int dataSize; |
|
180 |
AudioStreamBasicDescription sf; |
|
181 |
AudioBufferList* bfs; |
|
182 |
}; |
|
183 |
||
184 |
class QAudioPacketFeeder |
|
185 |
{ |
|
186 |
public: |
|
187 |
QAudioPacketFeeder(QAudioBufferList* abl): |
|
188 |
audioBufferList(abl) |
|
189 |
{ |
|
190 |
totalPackets = audioBufferList->packetCount(); |
|
191 |
position = 0; |
|
192 |
} |
|
193 |
||
194 |
bool feed(AudioBufferList& dst, UInt32& packetCount) |
|
195 |
{ |
|
196 |
if (position == totalPackets) { |
|
197 |
dst.mBuffers[0].mDataByteSize = 0; |
|
198 |
packetCount = 0; |
|
199 |
return false; |
|
200 |
} |
|
201 |
||
202 |
if (totalPackets - position < packetCount) |
|
203 |
packetCount = totalPackets - position; |
|
204 |
||
205 |
dst.mBuffers[0].mDataByteSize = packetCount * audioBufferList->packetSize(); |
|
206 |
dst.mBuffers[0].mData = audioBufferList->data() + (position * audioBufferList->packetSize()); |
|
207 |
||
208 |
position += packetCount; |
|
209 |
||
210 |
return true; |
|
211 |
} |
|
212 |
||
213 |
private: |
|
214 |
UInt32 totalPackets; |
|
215 |
UInt32 position; |
|
216 |
QAudioBufferList* audioBufferList; |
|
217 |
}; |
|
218 |
||
219 |
class QAudioInputBuffer : public QObject |
|
220 |
{ |
|
221 |
Q_OBJECT |
|
222 |
||
223 |
public: |
|
224 |
QAudioInputBuffer(int bufferSize, |
|
225 |
int maxPeriodSize, |
|
226 |
AudioStreamBasicDescription const& inputFormat, |
|
227 |
AudioStreamBasicDescription const& outputFormat, |
|
228 |
QObject* parent): |
|
229 |
QObject(parent), |
|
230 |
m_deviceError(false), |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
231 |
m_audioConverter(0), |
0 | 232 |
m_inputFormat(inputFormat), |
233 |
m_outputFormat(outputFormat) |
|
234 |
{ |
|
235 |
m_maxPeriodSize = maxPeriodSize; |
|
236 |
m_periodTime = m_maxPeriodSize / m_outputFormat.mBytesPerFrame * 1000 / m_outputFormat.mSampleRate; |
|
237 |
m_buffer = new QAudioRingBuffer(bufferSize + (bufferSize % maxPeriodSize == 0 ? 0 : maxPeriodSize - (bufferSize % maxPeriodSize))); |
|
238 |
m_inputBufferList = new QAudioBufferList(m_inputFormat); |
|
239 |
||
240 |
m_flushTimer = new QTimer(this); |
|
241 |
connect(m_flushTimer, SIGNAL(timeout()), SLOT(flushBuffer())); |
|
242 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
243 |
if (toQAudioFormat(inputFormat) != toQAudioFormat(outputFormat)) { |
0 | 244 |
if (AudioConverterNew(&m_inputFormat, &m_outputFormat, &m_audioConverter) != noErr) { |
245 |
qWarning() << "QAudioInput: Unable to create an Audio Converter"; |
|
246 |
m_audioConverter = 0; |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
~QAudioInputBuffer() |
|
252 |
{ |
|
253 |
delete m_buffer; |
|
254 |
} |
|
255 |
||
256 |
qint64 renderFromDevice(AudioUnit audioUnit, |
|
257 |
AudioUnitRenderActionFlags* ioActionFlags, |
|
258 |
const AudioTimeStamp* inTimeStamp, |
|
259 |
UInt32 inBusNumber, |
|
260 |
UInt32 inNumberFrames) |
|
261 |
{ |
|
262 |
const bool wasEmpty = m_buffer->used() == 0; |
|
263 |
||
264 |
OSStatus err; |
|
265 |
qint64 framesRendered = 0; |
|
266 |
||
267 |
m_inputBufferList->reset(); |
|
268 |
err = AudioUnitRender(audioUnit, |
|
269 |
ioActionFlags, |
|
270 |
inTimeStamp, |
|
271 |
inBusNumber, |
|
272 |
inNumberFrames, |
|
273 |
m_inputBufferList->audioBufferList()); |
|
274 |
||
275 |
if (m_audioConverter != 0) { |
|
276 |
QAudioPacketFeeder feeder(m_inputBufferList); |
|
277 |
||
278 |
bool wecan = true; |
|
279 |
int copied = 0; |
|
280 |
||
281 |
const int available = m_buffer->free(); |
|
282 |
||
283 |
while (err == noErr && wecan) { |
|
284 |
QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(available); |
|
285 |
||
286 |
if (region.second > 0) { |
|
287 |
AudioBufferList output; |
|
288 |
output.mNumberBuffers = 1; |
|
289 |
output.mBuffers[0].mNumberChannels = 1; |
|
290 |
output.mBuffers[0].mDataByteSize = region.second; |
|
291 |
output.mBuffers[0].mData = region.first; |
|
292 |
||
293 |
UInt32 packetSize = region.second / m_outputFormat.mBytesPerPacket; |
|
294 |
err = AudioConverterFillComplexBuffer(m_audioConverter, |
|
295 |
converterCallback, |
|
296 |
&feeder, |
|
297 |
&packetSize, |
|
298 |
&output, |
|
299 |
0); |
|
300 |
||
301 |
region.second = output.mBuffers[0].mDataByteSize; |
|
302 |
copied += region.second; |
|
303 |
||
304 |
m_buffer->releaseWriteRegion(region); |
|
305 |
} |
|
306 |
else |
|
307 |
wecan = false; |
|
308 |
} |
|
309 |
||
310 |
framesRendered += copied / m_outputFormat.mBytesPerFrame; |
|
311 |
} |
|
312 |
else { |
|
313 |
const int available = m_inputBufferList->bufferSize(); |
|
314 |
bool wecan = true; |
|
315 |
int copied = 0; |
|
316 |
||
317 |
while (wecan && copied < available) { |
|
318 |
QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(available - copied); |
|
319 |
||
320 |
if (region.second > 0) { |
|
321 |
memcpy(region.first, m_inputBufferList->data() + copied, region.second); |
|
322 |
copied += region.second; |
|
323 |
} |
|
324 |
else |
|
325 |
wecan = false; |
|
326 |
||
327 |
m_buffer->releaseWriteRegion(region); |
|
328 |
} |
|
329 |
||
330 |
framesRendered = copied / m_outputFormat.mBytesPerFrame; |
|
331 |
} |
|
332 |
||
333 |
if (wasEmpty && framesRendered > 0) |
|
334 |
emit readyRead(); |
|
335 |
||
336 |
return framesRendered; |
|
337 |
} |
|
338 |
||
339 |
qint64 readBytes(char* data, qint64 len) |
|
340 |
{ |
|
341 |
bool wecan = true; |
|
342 |
qint64 bytesCopied = 0; |
|
343 |
||
344 |
len -= len % m_maxPeriodSize; |
|
345 |
while (wecan && bytesCopied < len) { |
|
346 |
QAudioRingBuffer::Region region = m_buffer->acquireReadRegion(len - bytesCopied); |
|
347 |
||
348 |
if (region.second > 0) { |
|
349 |
memcpy(data + bytesCopied, region.first, region.second); |
|
350 |
bytesCopied += region.second; |
|
351 |
} |
|
352 |
else |
|
353 |
wecan = false; |
|
354 |
||
355 |
m_buffer->releaseReadRegion(region); |
|
356 |
} |
|
357 |
||
358 |
return bytesCopied; |
|
359 |
} |
|
360 |
||
361 |
void setFlushDevice(QIODevice* device) |
|
362 |
{ |
|
363 |
if (m_device != device) |
|
364 |
m_device = device; |
|
365 |
} |
|
366 |
||
367 |
void startFlushTimer() |
|
368 |
{ |
|
369 |
if (m_device != 0) { |
|
370 |
m_flushTimer->start((m_buffer->size() - (m_maxPeriodSize * 2)) / m_maxPeriodSize * m_periodTime); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
void stopFlushTimer() |
|
375 |
{ |
|
376 |
m_flushTimer->stop(); |
|
377 |
} |
|
378 |
||
379 |
void flush(bool all = false) |
|
380 |
{ |
|
381 |
if (m_device == 0) |
|
382 |
return; |
|
383 |
||
384 |
const int used = m_buffer->used(); |
|
385 |
const int readSize = all ? used : used - (used % m_maxPeriodSize); |
|
386 |
||
387 |
if (readSize > 0) { |
|
388 |
bool wecan = true; |
|
389 |
int flushed = 0; |
|
390 |
||
391 |
while (!m_deviceError && wecan && flushed < readSize) { |
|
392 |
QAudioRingBuffer::Region region = m_buffer->acquireReadRegion(readSize - flushed); |
|
393 |
||
394 |
if (region.second > 0) { |
|
395 |
int bytesWritten = m_device->write(region.first, region.second); |
|
396 |
if (bytesWritten < 0) { |
|
397 |
stopFlushTimer(); |
|
398 |
m_deviceError = true; |
|
399 |
} |
|
400 |
else { |
|
401 |
region.second = bytesWritten; |
|
402 |
flushed += bytesWritten; |
|
403 |
wecan = bytesWritten != 0; |
|
404 |
} |
|
405 |
} |
|
406 |
else |
|
407 |
wecan = false; |
|
408 |
||
409 |
m_buffer->releaseReadRegion(region); |
|
410 |
} |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
void reset() |
|
415 |
{ |
|
416 |
m_buffer->reset(); |
|
417 |
m_deviceError = false; |
|
418 |
} |
|
419 |
||
420 |
int available() const |
|
421 |
{ |
|
422 |
return m_buffer->free(); |
|
423 |
} |
|
424 |
||
425 |
int used() const |
|
426 |
{ |
|
427 |
return m_buffer->used(); |
|
428 |
} |
|
429 |
||
430 |
signals: |
|
431 |
void readyRead(); |
|
432 |
||
433 |
private slots: |
|
434 |
void flushBuffer() |
|
435 |
{ |
|
436 |
flush(); |
|
437 |
} |
|
438 |
||
439 |
private: |
|
440 |
bool m_deviceError; |
|
441 |
int m_maxPeriodSize; |
|
442 |
int m_periodTime; |
|
443 |
QIODevice* m_device; |
|
444 |
QTimer* m_flushTimer; |
|
445 |
QAudioRingBuffer* m_buffer; |
|
446 |
QAudioBufferList* m_inputBufferList; |
|
447 |
AudioConverterRef m_audioConverter; |
|
448 |
AudioStreamBasicDescription m_inputFormat; |
|
449 |
AudioStreamBasicDescription m_outputFormat; |
|
450 |
||
451 |
const static OSStatus as_empty = 'qtem'; |
|
452 |
||
453 |
// Converter callback |
|
454 |
static OSStatus converterCallback(AudioConverterRef inAudioConverter, |
|
455 |
UInt32* ioNumberDataPackets, |
|
456 |
AudioBufferList* ioData, |
|
457 |
AudioStreamPacketDescription** outDataPacketDescription, |
|
458 |
void* inUserData) |
|
459 |
{ |
|
460 |
Q_UNUSED(inAudioConverter); |
|
461 |
Q_UNUSED(outDataPacketDescription); |
|
462 |
||
463 |
QAudioPacketFeeder* feeder = static_cast<QAudioPacketFeeder*>(inUserData); |
|
464 |
||
465 |
if (!feeder->feed(*ioData, *ioNumberDataPackets)) |
|
466 |
return as_empty; |
|
467 |
||
468 |
return noErr; |
|
469 |
} |
|
470 |
}; |
|
471 |
||
472 |
||
473 |
class MacInputDevice : public QIODevice |
|
474 |
{ |
|
475 |
Q_OBJECT |
|
476 |
||
477 |
public: |
|
478 |
MacInputDevice(QAudioInputBuffer* audioBuffer, QObject* parent): |
|
479 |
QIODevice(parent), |
|
480 |
m_audioBuffer(audioBuffer) |
|
481 |
{ |
|
482 |
open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
483 |
connect(m_audioBuffer, SIGNAL(readyRead()), SIGNAL(readyRead())); |
|
484 |
} |
|
485 |
||
486 |
qint64 readData(char* data, qint64 len) |
|
487 |
{ |
|
488 |
return m_audioBuffer->readBytes(data, len); |
|
489 |
} |
|
490 |
||
491 |
qint64 writeData(const char* data, qint64 len) |
|
492 |
{ |
|
493 |
Q_UNUSED(data); |
|
494 |
Q_UNUSED(len); |
|
495 |
||
496 |
return 0; |
|
497 |
} |
|
498 |
||
499 |
bool isSequential() const |
|
500 |
{ |
|
501 |
return true; |
|
502 |
} |
|
503 |
||
504 |
private: |
|
505 |
QAudioInputBuffer* m_audioBuffer; |
|
506 |
}; |
|
507 |
||
508 |
} |
|
509 |
||
510 |
||
511 |
QAudioInputPrivate::QAudioInputPrivate(const QByteArray& device, QAudioFormat const& format): |
|
512 |
audioFormat(format) |
|
513 |
{ |
|
514 |
QDataStream ds(device); |
|
515 |
quint32 did, mode; |
|
516 |
||
517 |
ds >> did >> mode; |
|
518 |
||
519 |
if (QAudio::Mode(mode) == QAudio::AudioOutput) |
|
520 |
errorCode = QAudio::OpenError; |
|
521 |
else { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
522 |
audioDeviceInfo = new QAudioDeviceInfoInternal(device, QAudio::AudioInput); |
0 | 523 |
isOpen = false; |
524 |
audioDeviceId = AudioDeviceID(did); |
|
525 |
audioUnit = 0; |
|
526 |
startTime = 0; |
|
527 |
totalFrames = 0; |
|
528 |
audioBuffer = 0; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
529 |
internalBufferSize = QtMultimediaInternal::default_buffer_size; |
0 | 530 |
clockFrequency = AudioGetHostClockFrequency() / 1000; |
531 |
errorCode = QAudio::NoError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
532 |
stateCode = QAudio::StoppedState; |
0 | 533 |
|
534 |
intervalTimer = new QTimer(this); |
|
535 |
intervalTimer->setInterval(1000); |
|
536 |
connect(intervalTimer, SIGNAL(timeout()), SIGNAL(notify())); |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
QAudioInputPrivate::~QAudioInputPrivate() |
|
541 |
{ |
|
542 |
close(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
543 |
delete audioDeviceInfo; |
0 | 544 |
} |
545 |
||
546 |
bool QAudioInputPrivate::open() |
|
547 |
{ |
|
548 |
UInt32 size = 0; |
|
549 |
||
550 |
if (isOpen) |
|
551 |
return true; |
|
552 |
||
553 |
ComponentDescription cd; |
|
554 |
cd.componentType = kAudioUnitType_Output; |
|
555 |
cd.componentSubType = kAudioUnitSubType_HALOutput; |
|
556 |
cd.componentManufacturer = kAudioUnitManufacturer_Apple; |
|
557 |
cd.componentFlags = 0; |
|
558 |
cd.componentFlagsMask = 0; |
|
559 |
||
560 |
// Open |
|
561 |
Component cp = FindNextComponent(NULL, &cd); |
|
562 |
if (cp == 0) { |
|
563 |
qWarning() << "QAudioInput: Failed to find HAL Output component"; |
|
564 |
return false; |
|
565 |
} |
|
566 |
||
567 |
if (OpenAComponent(cp, &audioUnit) != noErr) { |
|
568 |
qWarning() << "QAudioInput: Unable to Open Output Component"; |
|
569 |
return false; |
|
570 |
} |
|
571 |
||
572 |
// Set mode |
|
573 |
// switch to input mode |
|
574 |
UInt32 enable = 1; |
|
575 |
if (AudioUnitSetProperty(audioUnit, |
|
576 |
kAudioOutputUnitProperty_EnableIO, |
|
577 |
kAudioUnitScope_Input, |
|
578 |
1, |
|
579 |
&enable, |
|
580 |
sizeof(enable)) != noErr) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
581 |
qWarning() << "QAudioInput: Unable to switch to input mode (Enable Input)"; |
0 | 582 |
return false; |
583 |
} |
|
584 |
||
585 |
enable = 0; |
|
586 |
if (AudioUnitSetProperty(audioUnit, |
|
587 |
kAudioOutputUnitProperty_EnableIO, |
|
588 |
kAudioUnitScope_Output, |
|
589 |
0, |
|
590 |
&enable, |
|
591 |
sizeof(enable)) != noErr) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
592 |
qWarning() << "QAudioInput: Unable to switch to input mode (Disable output)"; |
0 | 593 |
return false; |
594 |
} |
|
595 |
||
596 |
// register callback |
|
597 |
AURenderCallbackStruct cb; |
|
598 |
cb.inputProc = inputCallback; |
|
599 |
cb.inputProcRefCon = this; |
|
600 |
||
601 |
if (AudioUnitSetProperty(audioUnit, |
|
602 |
kAudioOutputUnitProperty_SetInputCallback, |
|
603 |
kAudioUnitScope_Global, |
|
604 |
0, |
|
605 |
&cb, |
|
606 |
sizeof(cb)) != noErr) { |
|
607 |
qWarning() << "QAudioInput: Failed to set AudioUnit callback"; |
|
608 |
return false; |
|
609 |
} |
|
610 |
||
611 |
// Set Audio Device |
|
612 |
if (AudioUnitSetProperty(audioUnit, |
|
613 |
kAudioOutputUnitProperty_CurrentDevice, |
|
614 |
kAudioUnitScope_Global, |
|
615 |
0, |
|
616 |
&audioDeviceId, |
|
617 |
sizeof(audioDeviceId)) != noErr) { |
|
618 |
qWarning() << "QAudioInput: Unable to use configured device"; |
|
619 |
return false; |
|
620 |
} |
|
621 |
||
622 |
// Set format |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
623 |
// Wanted |
0 | 624 |
streamFormat = toAudioStreamBasicDescription(audioFormat); |
625 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
626 |
// Required on unit |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
627 |
if (audioFormat == audioDeviceInfo->preferredFormat()) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
628 |
deviceFormat = streamFormat; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
629 |
AudioUnitSetProperty(audioUnit, |
0 | 630 |
kAudioUnitProperty_StreamFormat, |
631 |
kAudioUnitScope_Output, |
|
632 |
1, |
|
633 |
&deviceFormat, |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
634 |
sizeof(deviceFormat)); |
0 | 635 |
} |
636 |
else { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
637 |
size = sizeof(deviceFormat); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
638 |
if (AudioUnitGetProperty(audioUnit, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
639 |
kAudioUnitProperty_StreamFormat, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
640 |
kAudioUnitScope_Input, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
641 |
1, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
642 |
&deviceFormat, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
643 |
&size) != noErr) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
644 |
qWarning() << "QAudioInput: Unable to retrieve device format"; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
645 |
return false; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
646 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
647 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
648 |
if (AudioUnitSetProperty(audioUnit, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
649 |
kAudioUnitProperty_StreamFormat, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
650 |
kAudioUnitScope_Output, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
651 |
1, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
652 |
&deviceFormat, |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
653 |
sizeof(deviceFormat)) != noErr) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
654 |
qWarning() << "QAudioInput: Unable to set device format"; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
655 |
return false; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
656 |
} |
0 | 657 |
} |
658 |
||
659 |
// Setup buffers |
|
660 |
UInt32 numberOfFrames; |
|
661 |
size = sizeof(UInt32); |
|
662 |
if (AudioUnitGetProperty(audioUnit, |
|
663 |
kAudioDevicePropertyBufferFrameSize, |
|
664 |
kAudioUnitScope_Global, |
|
665 |
0, |
|
666 |
&numberOfFrames, |
|
667 |
&size) != noErr) { |
|
668 |
qWarning() << "QAudioInput: Failed to get audio period size"; |
|
669 |
return false; |
|
670 |
} |
|
671 |
||
672 |
// Allocate buffer |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
673 |
periodSizeBytes = numberOfFrames * streamFormat.mBytesPerFrame; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
674 |
|
0 | 675 |
if (internalBufferSize < periodSizeBytes * 2) |
676 |
internalBufferSize = periodSizeBytes * 2; |
|
677 |
else |
|
678 |
internalBufferSize -= internalBufferSize % streamFormat.mBytesPerFrame; |
|
679 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
680 |
audioBuffer = new QtMultimediaInternal::QAudioInputBuffer(internalBufferSize, |
0 | 681 |
periodSizeBytes, |
682 |
deviceFormat, |
|
683 |
streamFormat, |
|
684 |
this); |
|
685 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
686 |
audioIO = new QtMultimediaInternal::MacInputDevice(audioBuffer, this); |
0 | 687 |
|
688 |
// Init |
|
689 |
if (AudioUnitInitialize(audioUnit) != noErr) { |
|
690 |
qWarning() << "QAudioInput: Failed to initialize AudioUnit"; |
|
691 |
return false; |
|
692 |
} |
|
693 |
||
694 |
isOpen = true; |
|
695 |
||
696 |
return isOpen; |
|
697 |
} |
|
698 |
||
699 |
void QAudioInputPrivate::close() |
|
700 |
{ |
|
701 |
if (audioUnit != 0) { |
|
702 |
AudioOutputUnitStop(audioUnit); |
|
703 |
AudioUnitUninitialize(audioUnit); |
|
704 |
CloseComponent(audioUnit); |
|
705 |
} |
|
706 |
||
707 |
delete audioBuffer; |
|
708 |
} |
|
709 |
||
710 |
QAudioFormat QAudioInputPrivate::format() const |
|
711 |
{ |
|
712 |
return audioFormat; |
|
713 |
} |
|
714 |
||
715 |
QIODevice* QAudioInputPrivate::start(QIODevice* device) |
|
716 |
{ |
|
717 |
QIODevice* op = device; |
|
718 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
719 |
if (!audioFormat.isValid() || !open()) { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
720 |
stateCode = QAudio::StoppedState; |
0 | 721 |
errorCode = QAudio::OpenError; |
722 |
return audioIO; |
|
723 |
} |
|
724 |
||
725 |
reset(); |
|
726 |
audioBuffer->reset(); |
|
727 |
audioBuffer->setFlushDevice(op); |
|
728 |
||
729 |
if (op == 0) |
|
730 |
op = audioIO; |
|
731 |
||
732 |
// Start |
|
733 |
startTime = AudioGetCurrentHostTime(); |
|
734 |
totalFrames = 0; |
|
735 |
||
736 |
audioThreadStart(); |
|
737 |
||
738 |
stateCode = QAudio::ActiveState; |
|
739 |
errorCode = QAudio::NoError; |
|
740 |
emit stateChanged(stateCode); |
|
741 |
||
742 |
return op; |
|
743 |
} |
|
744 |
||
745 |
void QAudioInputPrivate::stop() |
|
746 |
{ |
|
747 |
QMutexLocker lock(&mutex); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
748 |
if (stateCode != QAudio::StoppedState) { |
0 | 749 |
audioThreadStop(); |
750 |
audioBuffer->flush(true); |
|
751 |
||
752 |
errorCode = QAudio::NoError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
753 |
stateCode = QAudio::StoppedState; |
0 | 754 |
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
755 |
} |
|
756 |
} |
|
757 |
||
758 |
void QAudioInputPrivate::reset() |
|
759 |
{ |
|
760 |
QMutexLocker lock(&mutex); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
761 |
if (stateCode != QAudio::StoppedState) { |
0 | 762 |
audioThreadStop(); |
763 |
||
764 |
errorCode = QAudio::NoError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
765 |
stateCode = QAudio::StoppedState; |
0 | 766 |
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
767 |
} |
|
768 |
} |
|
769 |
||
770 |
void QAudioInputPrivate::suspend() |
|
771 |
{ |
|
772 |
QMutexLocker lock(&mutex); |
|
773 |
if (stateCode == QAudio::ActiveState || stateCode == QAudio::IdleState) { |
|
774 |
audioThreadStop(); |
|
775 |
||
776 |
errorCode = QAudio::NoError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
777 |
stateCode = QAudio::SuspendedState; |
0 | 778 |
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
779 |
} |
|
780 |
} |
|
781 |
||
782 |
void QAudioInputPrivate::resume() |
|
783 |
{ |
|
784 |
QMutexLocker lock(&mutex); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
785 |
if (stateCode == QAudio::SuspendedState) { |
0 | 786 |
audioThreadStart(); |
787 |
||
788 |
errorCode = QAudio::NoError; |
|
789 |
stateCode = QAudio::ActiveState; |
|
790 |
QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
int QAudioInputPrivate::bytesReady() const |
|
795 |
{ |
|
796 |
return audioBuffer->used(); |
|
797 |
} |
|
798 |
||
799 |
int QAudioInputPrivate::periodSize() const |
|
800 |
{ |
|
801 |
return periodSizeBytes; |
|
802 |
} |
|
803 |
||
804 |
void QAudioInputPrivate::setBufferSize(int bs) |
|
805 |
{ |
|
806 |
internalBufferSize = bs; |
|
807 |
} |
|
808 |
||
809 |
int QAudioInputPrivate::bufferSize() const |
|
810 |
{ |
|
811 |
return internalBufferSize; |
|
812 |
} |
|
813 |
||
814 |
void QAudioInputPrivate::setNotifyInterval(int milliSeconds) |
|
815 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
816 |
if (intervalTimer->interval() == milliSeconds) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
817 |
return; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
818 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
819 |
if (milliSeconds <= 0) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
820 |
milliSeconds = 0; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
821 |
|
0 | 822 |
intervalTimer->setInterval(milliSeconds); |
823 |
} |
|
824 |
||
825 |
int QAudioInputPrivate::notifyInterval() const |
|
826 |
{ |
|
827 |
return intervalTimer->interval(); |
|
828 |
} |
|
829 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
830 |
qint64 QAudioInputPrivate::processedUSecs() const |
0 | 831 |
{ |
832 |
return totalFrames * 1000000 / audioFormat.frequency(); |
|
833 |
} |
|
834 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
835 |
qint64 QAudioInputPrivate::elapsedUSecs() const |
0 | 836 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
837 |
if (stateCode == QAudio::StoppedState) |
0 | 838 |
return 0; |
839 |
||
840 |
return (AudioGetCurrentHostTime() - startTime) / (clockFrequency / 1000); |
|
841 |
} |
|
842 |
||
843 |
QAudio::Error QAudioInputPrivate::error() const |
|
844 |
{ |
|
845 |
return errorCode; |
|
846 |
} |
|
847 |
||
848 |
QAudio::State QAudioInputPrivate::state() const |
|
849 |
{ |
|
850 |
return stateCode; |
|
851 |
} |
|
852 |
||
853 |
void QAudioInputPrivate::audioThreadStop() |
|
854 |
{ |
|
855 |
stopTimers(); |
|
856 |
if (audioThreadState.testAndSetAcquire(Running, Stopped)) |
|
857 |
threadFinished.wait(&mutex); |
|
858 |
} |
|
859 |
||
860 |
void QAudioInputPrivate::audioThreadStart() |
|
861 |
{ |
|
862 |
startTimers(); |
|
863 |
audioThreadState = Running; |
|
864 |
AudioOutputUnitStart(audioUnit); |
|
865 |
} |
|
866 |
||
867 |
void QAudioInputPrivate::audioDeviceStop() |
|
868 |
{ |
|
869 |
AudioOutputUnitStop(audioUnit); |
|
870 |
audioThreadState = Stopped; |
|
871 |
threadFinished.wakeOne(); |
|
872 |
} |
|
873 |
||
874 |
void QAudioInputPrivate::audioDeviceFull() |
|
875 |
{ |
|
876 |
QMutexLocker lock(&mutex); |
|
877 |
if (stateCode == QAudio::ActiveState) { |
|
878 |
audioDeviceStop(); |
|
879 |
||
880 |
errorCode = QAudio::UnderrunError; |
|
881 |
stateCode = QAudio::IdleState; |
|
882 |
QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection); |
|
883 |
} |
|
884 |
} |
|
885 |
||
886 |
void QAudioInputPrivate::audioDeviceError() |
|
887 |
{ |
|
888 |
QMutexLocker lock(&mutex); |
|
889 |
if (stateCode == QAudio::ActiveState) { |
|
890 |
audioDeviceStop(); |
|
891 |
||
892 |
errorCode = QAudio::IOError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
893 |
stateCode = QAudio::StoppedState; |
0 | 894 |
QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection); |
895 |
} |
|
896 |
} |
|
897 |
||
898 |
void QAudioInputPrivate::startTimers() |
|
899 |
{ |
|
900 |
audioBuffer->startFlushTimer(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
901 |
if (intervalTimer->interval() > 0) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
902 |
intervalTimer->start(); |
0 | 903 |
} |
904 |
||
905 |
void QAudioInputPrivate::stopTimers() |
|
906 |
{ |
|
907 |
audioBuffer->stopFlushTimer(); |
|
908 |
intervalTimer->stop(); |
|
909 |
} |
|
910 |
||
911 |
void QAudioInputPrivate::deviceStopped() |
|
912 |
{ |
|
913 |
stopTimers(); |
|
914 |
emit stateChanged(stateCode); |
|
915 |
} |
|
916 |
||
917 |
// Input callback |
|
918 |
OSStatus QAudioInputPrivate::inputCallback(void* inRefCon, |
|
919 |
AudioUnitRenderActionFlags* ioActionFlags, |
|
920 |
const AudioTimeStamp* inTimeStamp, |
|
921 |
UInt32 inBusNumber, |
|
922 |
UInt32 inNumberFrames, |
|
923 |
AudioBufferList* ioData) |
|
924 |
{ |
|
925 |
Q_UNUSED(ioData); |
|
926 |
||
927 |
QAudioInputPrivate* d = static_cast<QAudioInputPrivate*>(inRefCon); |
|
928 |
||
929 |
const int threadState = d->audioThreadState.fetchAndAddAcquire(0); |
|
930 |
if (threadState == Stopped) |
|
931 |
d->audioDeviceStop(); |
|
932 |
else { |
|
933 |
qint64 framesWritten; |
|
934 |
||
935 |
framesWritten = d->audioBuffer->renderFromDevice(d->audioUnit, |
|
936 |
ioActionFlags, |
|
937 |
inTimeStamp, |
|
938 |
inBusNumber, |
|
939 |
inNumberFrames); |
|
940 |
||
941 |
if (framesWritten > 0) |
|
942 |
d->totalFrames += framesWritten; |
|
943 |
else if (framesWritten == 0) |
|
944 |
d->audioDeviceFull(); |
|
945 |
else if (framesWritten < 0) |
|
946 |
d->audioDeviceError(); |
|
947 |
} |
|
948 |
||
949 |
return noErr; |
|
950 |
} |
|
951 |
||
952 |
||
953 |
QT_END_NAMESPACE |
|
954 |
||
955 |
#include "qaudioinput_mac_p.moc" |
|
956 |