|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
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 <CoreServices/CoreServices.h> |
|
54 #include <CoreAudio/CoreAudio.h> |
|
55 #include <AudioUnit/AudioUnit.h> |
|
56 #include <AudioToolbox/AudioToolbox.h> |
|
57 |
|
58 #include <QtCore/qendian.h> |
|
59 #include <QtCore/qbuffer.h> |
|
60 #include <QtCore/qtimer.h> |
|
61 #include <QtCore/qdebug.h> |
|
62 |
|
63 #include <qaudiodeviceinfo.h> |
|
64 #include <qaudiooutput.h> |
|
65 |
|
66 #include "qaudio_mac_p.h" |
|
67 #include "qaudiooutput_mac_p.h" |
|
68 |
|
69 |
|
70 QT_BEGIN_NAMESPACE |
|
71 |
|
72 |
|
73 namespace QtMultimediaKitInternal |
|
74 { |
|
75 |
|
76 static const int default_buffer_size = 8 * 1024; |
|
77 |
|
78 |
|
79 class QAudioOutputBuffer : public QObject |
|
80 { |
|
81 Q_OBJECT |
|
82 |
|
83 public: |
|
84 QAudioOutputBuffer(int bufferSize, int maxPeriodSize, QAudioFormat const& audioFormat): |
|
85 m_deviceError(false), |
|
86 m_maxPeriodSize(maxPeriodSize), |
|
87 m_device(0) |
|
88 { |
|
89 m_buffer = new QAudioRingBuffer(bufferSize + (bufferSize % maxPeriodSize == 0 ? 0 : maxPeriodSize - (bufferSize % maxPeriodSize))); |
|
90 m_bytesPerFrame = (audioFormat.sampleSize() / 8) * audioFormat.channels(); |
|
91 m_periodTime = m_maxPeriodSize / m_bytesPerFrame * 1000 / audioFormat.frequency(); |
|
92 |
|
93 m_fillTimer = new QTimer(this); |
|
94 connect(m_fillTimer, SIGNAL(timeout()), SLOT(fillBuffer())); |
|
95 } |
|
96 |
|
97 ~QAudioOutputBuffer() |
|
98 { |
|
99 delete m_buffer; |
|
100 } |
|
101 |
|
102 qint64 readFrames(char* data, qint64 maxFrames) |
|
103 { |
|
104 bool wecan = true; |
|
105 qint64 framesRead = 0; |
|
106 |
|
107 while (wecan && framesRead < maxFrames) { |
|
108 QAudioRingBuffer::Region region = m_buffer->acquireReadRegion((maxFrames - framesRead) * m_bytesPerFrame); |
|
109 |
|
110 if (region.second > 0) { |
|
111 region.second -= region.second % m_bytesPerFrame; |
|
112 memcpy(data + (framesRead * m_bytesPerFrame), region.first, region.second); |
|
113 framesRead += region.second / m_bytesPerFrame; |
|
114 } |
|
115 else |
|
116 wecan = false; |
|
117 |
|
118 m_buffer->releaseReadRegion(region); |
|
119 } |
|
120 |
|
121 if (framesRead == 0 && m_deviceError) |
|
122 framesRead = -1; |
|
123 |
|
124 return framesRead; |
|
125 } |
|
126 |
|
127 qint64 writeBytes(const char* data, qint64 maxSize) |
|
128 { |
|
129 bool wecan = true; |
|
130 qint64 bytesWritten = 0; |
|
131 |
|
132 maxSize -= maxSize % m_bytesPerFrame; |
|
133 while (wecan && bytesWritten < maxSize) { |
|
134 QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(maxSize - bytesWritten); |
|
135 |
|
136 if (region.second > 0) { |
|
137 memcpy(region.first, data + bytesWritten, region.second); |
|
138 bytesWritten += region.second; |
|
139 } |
|
140 else |
|
141 wecan = false; |
|
142 |
|
143 m_buffer->releaseWriteRegion(region); |
|
144 } |
|
145 |
|
146 if (bytesWritten > 0) |
|
147 emit readyRead(); |
|
148 |
|
149 return bytesWritten; |
|
150 } |
|
151 |
|
152 int available() const |
|
153 { |
|
154 return m_buffer->free(); |
|
155 } |
|
156 |
|
157 void reset() |
|
158 { |
|
159 m_buffer->reset(); |
|
160 m_deviceError = false; |
|
161 } |
|
162 |
|
163 void setPrefetchDevice(QIODevice* device) |
|
164 { |
|
165 if (m_device != device) { |
|
166 m_device = device; |
|
167 if (m_device != 0) |
|
168 fillBuffer(); |
|
169 } |
|
170 } |
|
171 |
|
172 void startFillTimer() |
|
173 { |
|
174 if (m_device != 0) |
|
175 m_fillTimer->start(m_buffer->size() / 2 / m_maxPeriodSize * m_periodTime); |
|
176 } |
|
177 |
|
178 void stopFillTimer() |
|
179 { |
|
180 m_fillTimer->stop(); |
|
181 } |
|
182 |
|
183 signals: |
|
184 void readyRead(); |
|
185 |
|
186 private slots: |
|
187 void fillBuffer() |
|
188 { |
|
189 const int free = m_buffer->free(); |
|
190 const int writeSize = free - (free % m_maxPeriodSize); |
|
191 |
|
192 if (writeSize > 0) { |
|
193 bool wecan = true; |
|
194 int filled = 0; |
|
195 |
|
196 while (!m_deviceError && wecan && filled < writeSize) { |
|
197 QAudioRingBuffer::Region region = m_buffer->acquireWriteRegion(writeSize - filled); |
|
198 |
|
199 if (region.second > 0) { |
|
200 region.second = m_device->read(region.first, region.second); |
|
201 if (region.second > 0) |
|
202 filled += region.second; |
|
203 else if (region.second == 0) |
|
204 wecan = false; |
|
205 else if (region.second < 0) { |
|
206 m_fillTimer->stop(); |
|
207 region.second = 0; |
|
208 m_deviceError = true; |
|
209 } |
|
210 } |
|
211 else |
|
212 wecan = false; |
|
213 |
|
214 m_buffer->releaseWriteRegion(region); |
|
215 } |
|
216 |
|
217 if (filled > 0) |
|
218 emit readyRead(); |
|
219 } |
|
220 } |
|
221 |
|
222 private: |
|
223 bool m_deviceError; |
|
224 int m_maxPeriodSize; |
|
225 int m_bytesPerFrame; |
|
226 int m_periodTime; |
|
227 QIODevice* m_device; |
|
228 QTimer* m_fillTimer; |
|
229 QAudioRingBuffer* m_buffer; |
|
230 }; |
|
231 |
|
232 |
|
233 } |
|
234 |
|
235 class MacOutputDevice : public QIODevice |
|
236 { |
|
237 Q_OBJECT |
|
238 |
|
239 public: |
|
240 MacOutputDevice(QtMultimediaKitInternal::QAudioOutputBuffer* audioBuffer, QObject* parent): |
|
241 QIODevice(parent), |
|
242 m_audioBuffer(audioBuffer) |
|
243 { |
|
244 open(QIODevice::WriteOnly | QIODevice::Unbuffered); |
|
245 } |
|
246 |
|
247 qint64 readData(char* data, qint64 len) |
|
248 { |
|
249 Q_UNUSED(data); |
|
250 Q_UNUSED(len); |
|
251 |
|
252 return 0; |
|
253 } |
|
254 |
|
255 qint64 writeData(const char* data, qint64 len) |
|
256 { |
|
257 return m_audioBuffer->writeBytes(data, len); |
|
258 } |
|
259 |
|
260 bool isSequential() const |
|
261 { |
|
262 return true; |
|
263 } |
|
264 |
|
265 private: |
|
266 QtMultimediaKitInternal::QAudioOutputBuffer* m_audioBuffer; |
|
267 }; |
|
268 |
|
269 |
|
270 QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray& device) |
|
271 { |
|
272 QDataStream ds(device); |
|
273 quint32 did, mode; |
|
274 |
|
275 ds >> did >> mode; |
|
276 |
|
277 if (QAudio::Mode(mode) == QAudio::AudioInput) |
|
278 errorCode = QAudio::OpenError; |
|
279 else { |
|
280 isOpen = false; |
|
281 audioDeviceId = AudioDeviceID(did); |
|
282 audioUnit = 0; |
|
283 audioIO = 0; |
|
284 startTime = 0; |
|
285 totalFrames = 0; |
|
286 audioBuffer = 0; |
|
287 internalBufferSize = QtMultimediaKitInternal::default_buffer_size; |
|
288 clockFrequency = AudioGetHostClockFrequency() / 1000; |
|
289 errorCode = QAudio::NoError; |
|
290 stateCode = QAudio::StoppedState; |
|
291 audioThreadState = Stopped; |
|
292 |
|
293 intervalTimer = new QTimer(this); |
|
294 intervalTimer->setInterval(1000); |
|
295 connect(intervalTimer, SIGNAL(timeout()), SIGNAL(notify())); |
|
296 } |
|
297 } |
|
298 |
|
299 QAudioOutputPrivate::~QAudioOutputPrivate() |
|
300 { |
|
301 close(); |
|
302 } |
|
303 |
|
304 bool QAudioOutputPrivate::open() |
|
305 { |
|
306 if (errorCode != QAudio::NoError) |
|
307 return false; |
|
308 |
|
309 if (isOpen) |
|
310 return true; |
|
311 |
|
312 ComponentDescription cd; |
|
313 cd.componentType = kAudioUnitType_Output; |
|
314 cd.componentSubType = kAudioUnitSubType_HALOutput; |
|
315 cd.componentManufacturer = kAudioUnitManufacturer_Apple; |
|
316 cd.componentFlags = 0; |
|
317 cd.componentFlagsMask = 0; |
|
318 |
|
319 // Open |
|
320 Component cp = FindNextComponent(NULL, &cd); |
|
321 if (cp == 0) { |
|
322 qWarning() << "QAudioOutput: Failed to find HAL Output component"; |
|
323 return false; |
|
324 } |
|
325 |
|
326 if (OpenAComponent(cp, &audioUnit) != noErr) { |
|
327 qWarning() << "QAudioOutput: Unable to Open Output Component"; |
|
328 return false; |
|
329 } |
|
330 |
|
331 // register callback |
|
332 AURenderCallbackStruct cb; |
|
333 cb.inputProc = renderCallback; |
|
334 cb.inputProcRefCon = this; |
|
335 |
|
336 if (AudioUnitSetProperty(audioUnit, |
|
337 kAudioUnitProperty_SetRenderCallback, |
|
338 kAudioUnitScope_Global, |
|
339 0, |
|
340 &cb, |
|
341 sizeof(cb)) != noErr) { |
|
342 qWarning() << "QAudioOutput: Failed to set AudioUnit callback"; |
|
343 return false; |
|
344 } |
|
345 |
|
346 // Set Audio Device |
|
347 if (AudioUnitSetProperty(audioUnit, |
|
348 kAudioOutputUnitProperty_CurrentDevice, |
|
349 kAudioUnitScope_Global, |
|
350 0, |
|
351 &audioDeviceId, |
|
352 sizeof(audioDeviceId)) != noErr) { |
|
353 qWarning() << "QAudioOutput: Unable to use configured device"; |
|
354 return false; |
|
355 } |
|
356 |
|
357 // Set stream format |
|
358 streamFormat = toAudioStreamBasicDescription(audioFormat); |
|
359 |
|
360 UInt32 size = sizeof(streamFormat); |
|
361 if (AudioUnitSetProperty(audioUnit, |
|
362 kAudioUnitProperty_StreamFormat, |
|
363 kAudioUnitScope_Input, |
|
364 0, |
|
365 &streamFormat, |
|
366 sizeof(streamFormat)) != noErr) { |
|
367 qWarning() << "QAudioOutput: Unable to Set Stream information"; |
|
368 return false; |
|
369 } |
|
370 |
|
371 // Allocate buffer |
|
372 UInt32 numberOfFrames = 0; |
|
373 size = sizeof(UInt32); |
|
374 if (AudioUnitGetProperty(audioUnit, |
|
375 kAudioDevicePropertyBufferFrameSize, |
|
376 kAudioUnitScope_Global, |
|
377 0, |
|
378 &numberOfFrames, |
|
379 &size) != noErr) { |
|
380 qWarning() << "QAudioInput: Failed to get audio period size"; |
|
381 return false; |
|
382 } |
|
383 |
|
384 periodSizeBytes = numberOfFrames * streamFormat.mBytesPerFrame; |
|
385 if (internalBufferSize < periodSizeBytes * 2) |
|
386 internalBufferSize = periodSizeBytes * 2; |
|
387 else |
|
388 internalBufferSize -= internalBufferSize % streamFormat.mBytesPerFrame; |
|
389 |
|
390 audioBuffer = new QtMultimediaKitInternal::QAudioOutputBuffer(internalBufferSize, periodSizeBytes, audioFormat); |
|
391 connect(audioBuffer, SIGNAL(readyRead()), SLOT(inputReady())); // Pull |
|
392 |
|
393 audioIO = new MacOutputDevice(audioBuffer, this); |
|
394 |
|
395 // Init |
|
396 if (AudioUnitInitialize(audioUnit)) { |
|
397 qWarning() << "QAudioOutput: Failed to initialize AudioUnit"; |
|
398 return false; |
|
399 } |
|
400 |
|
401 isOpen = true; |
|
402 |
|
403 return true; |
|
404 } |
|
405 |
|
406 void QAudioOutputPrivate::close() |
|
407 { |
|
408 if (audioUnit != 0) { |
|
409 AudioOutputUnitStop(audioUnit); |
|
410 AudioUnitUninitialize(audioUnit); |
|
411 CloseComponent(audioUnit); |
|
412 } |
|
413 |
|
414 delete audioBuffer; |
|
415 } |
|
416 |
|
417 QAudioFormat QAudioOutputPrivate::format() const |
|
418 { |
|
419 return audioFormat; |
|
420 } |
|
421 |
|
422 void QAudioOutputPrivate::setFormat(const QAudioFormat& fmt) |
|
423 { |
|
424 if (stateCode == QAudio::StoppedState) |
|
425 audioFormat = fmt; |
|
426 } |
|
427 |
|
428 void QAudioOutputPrivate::start(QIODevice* device) |
|
429 { |
|
430 QIODevice* op = device; |
|
431 |
|
432 if (!audioFormat.isValid() || !open()) { |
|
433 stateCode = QAudio::StoppedState; |
|
434 errorCode = QAudio::OpenError; |
|
435 } |
|
436 |
|
437 reset(); |
|
438 audioBuffer->reset(); |
|
439 audioBuffer->setPrefetchDevice(op); |
|
440 |
|
441 if (op == 0) { |
|
442 op = audioIO; |
|
443 stateCode = QAudio::IdleState; |
|
444 } |
|
445 else |
|
446 stateCode = QAudio::ActiveState; |
|
447 |
|
448 // Start |
|
449 errorCode = QAudio::NoError; |
|
450 totalFrames = 0; |
|
451 startTime = AudioGetCurrentHostTime(); |
|
452 |
|
453 if (stateCode == QAudio::ActiveState) |
|
454 audioThreadStart(); |
|
455 |
|
456 emit stateChanged(stateCode); |
|
457 } |
|
458 |
|
459 QIODevice* QAudioOutputPrivate::start() |
|
460 { |
|
461 QIODevice* op = 0; |
|
462 |
|
463 if (!audioFormat.isValid() || !open()) { |
|
464 stateCode = QAudio::StoppedState; |
|
465 errorCode = QAudio::OpenError; |
|
466 return audioIO; |
|
467 } |
|
468 |
|
469 reset(); |
|
470 audioBuffer->reset(); |
|
471 audioBuffer->setPrefetchDevice(op); |
|
472 |
|
473 if (op == 0) { |
|
474 op = audioIO; |
|
475 stateCode = QAudio::IdleState; |
|
476 } |
|
477 else |
|
478 stateCode = QAudio::ActiveState; |
|
479 |
|
480 // Start |
|
481 errorCode = QAudio::NoError; |
|
482 totalFrames = 0; |
|
483 startTime = AudioGetCurrentHostTime(); |
|
484 |
|
485 if (stateCode == QAudio::ActiveState) |
|
486 audioThreadStart(); |
|
487 |
|
488 emit stateChanged(stateCode); |
|
489 |
|
490 return op; |
|
491 } |
|
492 |
|
493 void QAudioOutputPrivate::stop() |
|
494 { |
|
495 QMutexLocker lock(&mutex); |
|
496 if (stateCode != QAudio::StoppedState) { |
|
497 audioThreadDrain(); |
|
498 |
|
499 stateCode = QAudio::StoppedState; |
|
500 errorCode = QAudio::NoError; |
|
501 QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
502 } |
|
503 } |
|
504 |
|
505 void QAudioOutputPrivate::reset() |
|
506 { |
|
507 QMutexLocker lock(&mutex); |
|
508 if (stateCode != QAudio::StoppedState) { |
|
509 audioThreadStop(); |
|
510 |
|
511 stateCode = QAudio::StoppedState; |
|
512 errorCode = QAudio::NoError; |
|
513 QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
514 } |
|
515 } |
|
516 |
|
517 void QAudioOutputPrivate::suspend() |
|
518 { |
|
519 QMutexLocker lock(&mutex); |
|
520 if (stateCode == QAudio::ActiveState || stateCode == QAudio::IdleState) { |
|
521 audioThreadStop(); |
|
522 |
|
523 stateCode = QAudio::SuspendedState; |
|
524 errorCode = QAudio::NoError; |
|
525 QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
526 } |
|
527 } |
|
528 |
|
529 void QAudioOutputPrivate::resume() |
|
530 { |
|
531 QMutexLocker lock(&mutex); |
|
532 if (stateCode == QAudio::SuspendedState) { |
|
533 audioThreadStart(); |
|
534 |
|
535 stateCode = QAudio::ActiveState; |
|
536 errorCode = QAudio::NoError; |
|
537 QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
538 } |
|
539 } |
|
540 |
|
541 int QAudioOutputPrivate::bytesFree() const |
|
542 { |
|
543 return audioBuffer->available(); |
|
544 } |
|
545 |
|
546 int QAudioOutputPrivate::periodSize() const |
|
547 { |
|
548 return periodSizeBytes; |
|
549 } |
|
550 |
|
551 void QAudioOutputPrivate::setBufferSize(int bs) |
|
552 { |
|
553 if (stateCode == QAudio::StoppedState) |
|
554 internalBufferSize = bs; |
|
555 } |
|
556 |
|
557 int QAudioOutputPrivate::bufferSize() const |
|
558 { |
|
559 return internalBufferSize; |
|
560 } |
|
561 |
|
562 void QAudioOutputPrivate::setNotifyInterval(int milliSeconds) |
|
563 { |
|
564 if (intervalTimer->interval() == milliSeconds) |
|
565 return; |
|
566 |
|
567 if (milliSeconds <= 0) |
|
568 milliSeconds = 0; |
|
569 |
|
570 intervalTimer->setInterval(milliSeconds); |
|
571 } |
|
572 |
|
573 int QAudioOutputPrivate::notifyInterval() const |
|
574 { |
|
575 return intervalTimer->interval(); |
|
576 } |
|
577 |
|
578 qint64 QAudioOutputPrivate::processedUSecs() const |
|
579 { |
|
580 return totalFrames * 1000000 / audioFormat.frequency(); |
|
581 } |
|
582 |
|
583 qint64 QAudioOutputPrivate::elapsedUSecs() const |
|
584 { |
|
585 if (stateCode == QAudio::StoppedState) |
|
586 return 0; |
|
587 |
|
588 return (AudioGetCurrentHostTime() - startTime) / (clockFrequency / 1000); |
|
589 } |
|
590 |
|
591 QAudio::Error QAudioOutputPrivate::error() const |
|
592 { |
|
593 return errorCode; |
|
594 } |
|
595 |
|
596 QAudio::State QAudioOutputPrivate::state() const |
|
597 { |
|
598 return stateCode; |
|
599 } |
|
600 |
|
601 void QAudioOutputPrivate::audioThreadStart() |
|
602 { |
|
603 startTimers(); |
|
604 audioThreadState = Running; |
|
605 AudioOutputUnitStart(audioUnit); |
|
606 } |
|
607 |
|
608 void QAudioOutputPrivate::audioThreadStop() |
|
609 { |
|
610 stopTimers(); |
|
611 if (audioThreadState.testAndSetAcquire(Running, Stopped)) |
|
612 threadFinished.wait(&mutex); |
|
613 } |
|
614 |
|
615 void QAudioOutputPrivate::audioThreadDrain() |
|
616 { |
|
617 stopTimers(); |
|
618 if (audioThreadState.testAndSetAcquire(Running, Draining)) |
|
619 threadFinished.wait(&mutex); |
|
620 } |
|
621 |
|
622 void QAudioOutputPrivate::audioDeviceStop() |
|
623 { |
|
624 AudioOutputUnitStop(audioUnit); |
|
625 audioThreadState = Stopped; |
|
626 threadFinished.wakeOne(); |
|
627 } |
|
628 |
|
629 void QAudioOutputPrivate::audioDeviceIdle() |
|
630 { |
|
631 QMutexLocker lock(&mutex); |
|
632 if (stateCode == QAudio::ActiveState) { |
|
633 audioDeviceStop(); |
|
634 |
|
635 errorCode = QAudio::UnderrunError; |
|
636 stateCode = QAudio::IdleState; |
|
637 QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection); |
|
638 } |
|
639 } |
|
640 |
|
641 void QAudioOutputPrivate::audioDeviceError() |
|
642 { |
|
643 QMutexLocker lock(&mutex); |
|
644 if (stateCode == QAudio::ActiveState) { |
|
645 audioDeviceStop(); |
|
646 |
|
647 errorCode = QAudio::IOError; |
|
648 stateCode = QAudio::StoppedState; |
|
649 QMetaObject::invokeMethod(this, "deviceStopped", Qt::QueuedConnection); |
|
650 } |
|
651 } |
|
652 |
|
653 void QAudioOutputPrivate::startTimers() |
|
654 { |
|
655 audioBuffer->startFillTimer(); |
|
656 if (intervalTimer->interval() > 0) |
|
657 intervalTimer->start(); |
|
658 } |
|
659 |
|
660 void QAudioOutputPrivate::stopTimers() |
|
661 { |
|
662 audioBuffer->stopFillTimer(); |
|
663 intervalTimer->stop(); |
|
664 } |
|
665 |
|
666 |
|
667 void QAudioOutputPrivate::deviceStopped() |
|
668 { |
|
669 intervalTimer->stop(); |
|
670 emit stateChanged(stateCode); |
|
671 } |
|
672 |
|
673 void QAudioOutputPrivate::inputReady() |
|
674 { |
|
675 QMutexLocker lock(&mutex); |
|
676 if (stateCode == QAudio::IdleState) { |
|
677 audioThreadStart(); |
|
678 |
|
679 stateCode = QAudio::ActiveState; |
|
680 errorCode = QAudio::NoError; |
|
681 |
|
682 QMetaObject::invokeMethod(this, "stateChanged", Qt::QueuedConnection, Q_ARG(QAudio::State, stateCode)); |
|
683 } |
|
684 } |
|
685 |
|
686 |
|
687 OSStatus QAudioOutputPrivate::renderCallback(void* inRefCon, |
|
688 AudioUnitRenderActionFlags* ioActionFlags, |
|
689 const AudioTimeStamp* inTimeStamp, |
|
690 UInt32 inBusNumber, |
|
691 UInt32 inNumberFrames, |
|
692 AudioBufferList* ioData) |
|
693 { |
|
694 Q_UNUSED(ioActionFlags) |
|
695 Q_UNUSED(inTimeStamp) |
|
696 Q_UNUSED(inBusNumber) |
|
697 Q_UNUSED(inNumberFrames) |
|
698 |
|
699 QAudioOutputPrivate* d = static_cast<QAudioOutputPrivate*>(inRefCon); |
|
700 |
|
701 const int threadState = d->audioThreadState.fetchAndAddAcquire(0); |
|
702 if (threadState == Stopped) { |
|
703 ioData->mBuffers[0].mDataByteSize = 0; |
|
704 d->audioDeviceStop(); |
|
705 } |
|
706 else { |
|
707 const UInt32 bytesPerFrame = d->streamFormat.mBytesPerFrame; |
|
708 qint64 framesRead; |
|
709 |
|
710 framesRead = d->audioBuffer->readFrames((char*)ioData->mBuffers[0].mData, |
|
711 ioData->mBuffers[0].mDataByteSize / bytesPerFrame); |
|
712 |
|
713 if (framesRead > 0) { |
|
714 ioData->mBuffers[0].mDataByteSize = framesRead * bytesPerFrame; |
|
715 d->totalFrames += framesRead; |
|
716 } |
|
717 else { |
|
718 ioData->mBuffers[0].mDataByteSize = 0; |
|
719 if (framesRead == 0) { |
|
720 if (threadState == Draining) |
|
721 d->audioDeviceStop(); |
|
722 else |
|
723 d->audioDeviceIdle(); |
|
724 } |
|
725 else |
|
726 d->audioDeviceError(); |
|
727 } |
|
728 } |
|
729 |
|
730 return noErr; |
|
731 } |
|
732 |
|
733 |
|
734 QT_END_NAMESPACE |
|
735 |
|
736 #include "qaudiooutput_mac_p.moc" |
|
737 |