|
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 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 #include "qaudiooutput_symbian_p.h" |
|
43 |
|
44 QT_BEGIN_NAMESPACE |
|
45 |
|
46 //----------------------------------------------------------------------------- |
|
47 // Constants |
|
48 //----------------------------------------------------------------------------- |
|
49 |
|
50 const int UnderflowTimerInterval = 50; // ms |
|
51 |
|
52 |
|
53 //----------------------------------------------------------------------------- |
|
54 // Private class |
|
55 //----------------------------------------------------------------------------- |
|
56 |
|
57 SymbianAudioOutputPrivate::SymbianAudioOutputPrivate( |
|
58 QAudioOutputPrivate *audioDevice) |
|
59 : m_audioDevice(audioDevice) |
|
60 { |
|
61 |
|
62 } |
|
63 |
|
64 SymbianAudioOutputPrivate::~SymbianAudioOutputPrivate() |
|
65 { |
|
66 |
|
67 } |
|
68 |
|
69 qint64 SymbianAudioOutputPrivate::readData(char *data, qint64 len) |
|
70 { |
|
71 Q_UNUSED(data) |
|
72 Q_UNUSED(len) |
|
73 return 0; |
|
74 } |
|
75 |
|
76 qint64 SymbianAudioOutputPrivate::writeData(const char *data, qint64 len) |
|
77 { |
|
78 qint64 totalWritten = 0; |
|
79 |
|
80 if (m_audioDevice->state() == QAudio::ActiveState || |
|
81 m_audioDevice->state() == QAudio::IdleState) { |
|
82 |
|
83 while (totalWritten < len) { |
|
84 const qint64 written = m_audioDevice->pushData(data + totalWritten, |
|
85 len - totalWritten); |
|
86 if (written > 0) |
|
87 totalWritten += written; |
|
88 else |
|
89 break; |
|
90 } |
|
91 } |
|
92 |
|
93 return totalWritten; |
|
94 } |
|
95 |
|
96 |
|
97 //----------------------------------------------------------------------------- |
|
98 // Public functions |
|
99 //----------------------------------------------------------------------------- |
|
100 |
|
101 QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device, |
|
102 const QAudioFormat &format) |
|
103 : m_device(device) |
|
104 , m_format(format) |
|
105 , m_clientBufferSize(SymbianAudio::DefaultBufferSize) |
|
106 , m_notifyInterval(SymbianAudio::DefaultNotifyInterval) |
|
107 , m_notifyTimer(new QTimer(this)) |
|
108 , m_error(QAudio::NoError) |
|
109 , m_internalState(SymbianAudio::ClosedState) |
|
110 , m_externalState(QAudio::StoppedState) |
|
111 , m_pullMode(false) |
|
112 , m_source(0) |
|
113 , m_devSoundBuffer(0) |
|
114 , m_devSoundBufferSize(0) |
|
115 , m_bytesWritten(0) |
|
116 , m_pushDataReady(false) |
|
117 , m_bytesPadding(0) |
|
118 , m_underflow(false) |
|
119 , m_lastBuffer(false) |
|
120 , m_underflowTimer(new QTimer(this)) |
|
121 , m_samplesPlayed(0) |
|
122 , m_totalSamplesPlayed(0) |
|
123 { |
|
124 connect(m_notifyTimer.data(), SIGNAL(timeout()), this, SIGNAL(notify())); |
|
125 |
|
126 SymbianAudio::Utils::formatQtToNative(m_format, m_nativeFourCC, |
|
127 m_nativeFormat); |
|
128 |
|
129 m_underflowTimer->setInterval(UnderflowTimerInterval); |
|
130 connect(m_underflowTimer.data(), SIGNAL(timeout()), this, |
|
131 SLOT(underflowTimerExpired())); |
|
132 } |
|
133 |
|
134 QAudioOutputPrivate::~QAudioOutputPrivate() |
|
135 { |
|
136 close(); |
|
137 } |
|
138 |
|
139 QIODevice* QAudioOutputPrivate::start(QIODevice *device) |
|
140 { |
|
141 stop(); |
|
142 |
|
143 // We have to set these before the call to open() because of the |
|
144 // logic in initializingState() |
|
145 if (device) { |
|
146 m_pullMode = true; |
|
147 m_source = device; |
|
148 } |
|
149 |
|
150 open(); |
|
151 |
|
152 if (SymbianAudio::ClosedState != m_internalState) { |
|
153 if (device) { |
|
154 connect(m_source, SIGNAL(readyRead()), this, SLOT(dataReady())); |
|
155 } else { |
|
156 m_source = new SymbianAudioOutputPrivate(this); |
|
157 m_source->open(QIODevice::WriteOnly | QIODevice::Unbuffered); |
|
158 } |
|
159 |
|
160 m_elapsed.restart(); |
|
161 } |
|
162 |
|
163 return m_source; |
|
164 } |
|
165 |
|
166 void QAudioOutputPrivate::stop() |
|
167 { |
|
168 close(); |
|
169 } |
|
170 |
|
171 void QAudioOutputPrivate::reset() |
|
172 { |
|
173 m_totalSamplesPlayed += getSamplesPlayed(); |
|
174 m_devSound->Stop(); |
|
175 m_bytesPadding = 0; |
|
176 startPlayback(); |
|
177 } |
|
178 |
|
179 void QAudioOutputPrivate::suspend() |
|
180 { |
|
181 if (SymbianAudio::ActiveState == m_internalState |
|
182 || SymbianAudio::IdleState == m_internalState) { |
|
183 m_notifyTimer->stop(); |
|
184 m_underflowTimer->stop(); |
|
185 |
|
186 const qint64 samplesWritten = SymbianAudio::Utils::bytesToSamples( |
|
187 m_format, m_bytesWritten); |
|
188 |
|
189 const qint64 samplesPlayed = getSamplesPlayed(); |
|
190 |
|
191 m_bytesWritten = 0; |
|
192 |
|
193 // CMMFDevSound::Pause() is not guaranteed to work correctly in all |
|
194 // implementations, for play-mode DevSound sessions. We therefore |
|
195 // have to implement suspend() by calling CMMFDevSound::Stop(). |
|
196 // Because this causes buffered data to be dropped, we replace the |
|
197 // lost data with silence following a call to resume(), in order to |
|
198 // ensure that processedUSecs() returns the correct value. |
|
199 m_devSound->Stop(); |
|
200 m_totalSamplesPlayed += samplesPlayed; |
|
201 |
|
202 // Calculate the amount of data dropped |
|
203 const qint64 paddingSamples = samplesWritten - samplesPlayed; |
|
204 m_bytesPadding = SymbianAudio::Utils::samplesToBytes(m_format, |
|
205 paddingSamples); |
|
206 |
|
207 setState(SymbianAudio::SuspendedState); |
|
208 } |
|
209 } |
|
210 |
|
211 void QAudioOutputPrivate::resume() |
|
212 { |
|
213 if (SymbianAudio::SuspendedState == m_internalState) |
|
214 startPlayback(); |
|
215 } |
|
216 |
|
217 int QAudioOutputPrivate::bytesFree() const |
|
218 { |
|
219 int result = 0; |
|
220 if (m_devSoundBuffer) { |
|
221 const TDes8 &outputBuffer = m_devSoundBuffer->Data(); |
|
222 result = outputBuffer.MaxLength() - outputBuffer.Length(); |
|
223 } |
|
224 return result; |
|
225 } |
|
226 |
|
227 int QAudioOutputPrivate::periodSize() const |
|
228 { |
|
229 return bufferSize(); |
|
230 } |
|
231 |
|
232 void QAudioOutputPrivate::setBufferSize(int value) |
|
233 { |
|
234 // Note that DevSound does not allow its client to specify the buffer size. |
|
235 // This functionality is available via custom interfaces, but since these |
|
236 // cannot be guaranteed to work across all DevSound implementations, we |
|
237 // do not use them here. |
|
238 // In order to comply with the expected bevahiour of QAudioOutput, we store |
|
239 // the value and return it from bufferSize(), but the underlying DevSound |
|
240 // buffer size remains unchanged. |
|
241 if (value > 0) |
|
242 m_clientBufferSize = value; |
|
243 } |
|
244 |
|
245 int QAudioOutputPrivate::bufferSize() const |
|
246 { |
|
247 return m_devSoundBufferSize ? m_devSoundBufferSize : m_clientBufferSize; |
|
248 } |
|
249 |
|
250 void QAudioOutputPrivate::setNotifyInterval(int ms) |
|
251 { |
|
252 if (ms > 0) { |
|
253 const int oldNotifyInterval = m_notifyInterval; |
|
254 m_notifyInterval = ms; |
|
255 if (m_notifyTimer->isActive() && ms != oldNotifyInterval) |
|
256 m_notifyTimer->start(m_notifyInterval); |
|
257 } |
|
258 } |
|
259 |
|
260 int QAudioOutputPrivate::notifyInterval() const |
|
261 { |
|
262 return m_notifyInterval; |
|
263 } |
|
264 |
|
265 qint64 QAudioOutputPrivate::processedUSecs() const |
|
266 { |
|
267 int samplesPlayed = 0; |
|
268 if (m_devSound && SymbianAudio::SuspendedState != m_internalState) |
|
269 samplesPlayed = getSamplesPlayed(); |
|
270 |
|
271 // Protect against division by zero |
|
272 Q_ASSERT_X(m_format.frequency() > 0, Q_FUNC_INFO, "Invalid frequency"); |
|
273 |
|
274 const qint64 result = qint64(1000000) * |
|
275 (samplesPlayed + m_totalSamplesPlayed) |
|
276 / m_format.frequency(); |
|
277 |
|
278 return result; |
|
279 } |
|
280 |
|
281 qint64 QAudioOutputPrivate::elapsedUSecs() const |
|
282 { |
|
283 const qint64 result = (QAudio::StoppedState == state()) ? |
|
284 0 : m_elapsed.elapsed() * 1000; |
|
285 return result; |
|
286 } |
|
287 |
|
288 QAudio::Error QAudioOutputPrivate::error() const |
|
289 { |
|
290 return m_error; |
|
291 } |
|
292 |
|
293 QAudio::State QAudioOutputPrivate::state() const |
|
294 { |
|
295 return m_externalState; |
|
296 } |
|
297 |
|
298 QAudioFormat QAudioOutputPrivate::format() const |
|
299 { |
|
300 return m_format; |
|
301 } |
|
302 |
|
303 //----------------------------------------------------------------------------- |
|
304 // MDevSoundObserver implementation |
|
305 //----------------------------------------------------------------------------- |
|
306 |
|
307 void QAudioOutputPrivate::InitializeComplete(TInt aError) |
|
308 { |
|
309 Q_ASSERT_X(SymbianAudio::InitializingState == m_internalState, |
|
310 Q_FUNC_INFO, "Invalid state"); |
|
311 |
|
312 if (KErrNone == aError) |
|
313 startPlayback(); |
|
314 } |
|
315 |
|
316 void QAudioOutputPrivate::ToneFinished(TInt aError) |
|
317 { |
|
318 Q_UNUSED(aError) |
|
319 // This class doesn't use DevSound's tone playback functions, so should |
|
320 // never receive this callback. |
|
321 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
322 } |
|
323 |
|
324 void QAudioOutputPrivate::BufferToBeFilled(CMMFBuffer *aBuffer) |
|
325 { |
|
326 // Following receipt of this callback, DevSound should not provide another |
|
327 // buffer until we have returned the current one. |
|
328 Q_ASSERT_X(!m_devSoundBuffer, Q_FUNC_INFO, "Buffer already held"); |
|
329 |
|
330 // Will be returned to DevSound by bufferFilled(). |
|
331 m_devSoundBuffer = static_cast<CMMFDataBuffer*>(aBuffer); |
|
332 |
|
333 if (!m_devSoundBufferSize) |
|
334 m_devSoundBufferSize = m_devSoundBuffer->Data().MaxLength(); |
|
335 |
|
336 writePaddingData(); |
|
337 |
|
338 if (m_pullMode && isDataReady() && !m_bytesPadding) |
|
339 pullData(); |
|
340 } |
|
341 |
|
342 void QAudioOutputPrivate::PlayError(TInt aError) |
|
343 { |
|
344 switch (aError) { |
|
345 case KErrUnderflow: |
|
346 m_underflow = true; |
|
347 if (m_pullMode && !m_lastBuffer) |
|
348 setError(QAudio::UnderrunError); |
|
349 else |
|
350 setState(SymbianAudio::IdleState); |
|
351 break; |
|
352 default: |
|
353 setError(QAudio::IOError); |
|
354 break; |
|
355 } |
|
356 } |
|
357 |
|
358 void QAudioOutputPrivate::BufferToBeEmptied(CMMFBuffer *aBuffer) |
|
359 { |
|
360 Q_UNUSED(aBuffer) |
|
361 // This class doesn't use DevSound in record mode, so should never receive |
|
362 // this callback. |
|
363 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
364 } |
|
365 |
|
366 void QAudioOutputPrivate::RecordError(TInt aError) |
|
367 { |
|
368 Q_UNUSED(aError) |
|
369 // This class doesn't use DevSound in record mode, so should never receive |
|
370 // this callback. |
|
371 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
372 } |
|
373 |
|
374 void QAudioOutputPrivate::ConvertError(TInt aError) |
|
375 { |
|
376 Q_UNUSED(aError) |
|
377 // This class doesn't use DevSound's format conversion functions, so |
|
378 // should never receive this callback. |
|
379 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
380 } |
|
381 |
|
382 void QAudioOutputPrivate::DeviceMessage(TUid aMessageType, const TDesC8 &aMsg) |
|
383 { |
|
384 Q_UNUSED(aMessageType) |
|
385 Q_UNUSED(aMsg) |
|
386 // Ignore this callback. |
|
387 } |
|
388 |
|
389 //----------------------------------------------------------------------------- |
|
390 // Private functions |
|
391 //----------------------------------------------------------------------------- |
|
392 |
|
393 void QAudioOutputPrivate::dataReady() |
|
394 { |
|
395 // Client-provided QIODevice has data ready to read. |
|
396 |
|
397 Q_ASSERT_X(m_source->bytesAvailable(), Q_FUNC_INFO, |
|
398 "readyRead signal received, but no data available"); |
|
399 |
|
400 if (!m_bytesPadding) |
|
401 pullData(); |
|
402 } |
|
403 |
|
404 void QAudioOutputPrivate::underflowTimerExpired() |
|
405 { |
|
406 const TInt samplesPlayed = getSamplesPlayed(); |
|
407 if (m_samplesPlayed && (samplesPlayed == m_samplesPlayed)) { |
|
408 setError(QAudio::UnderrunError); |
|
409 } else { |
|
410 m_samplesPlayed = samplesPlayed; |
|
411 m_underflowTimer->start(); |
|
412 } |
|
413 } |
|
414 |
|
415 void QAudioOutputPrivate::open() |
|
416 { |
|
417 Q_ASSERT_X(SymbianAudio::ClosedState == m_internalState, |
|
418 Q_FUNC_INFO, "DevSound already opened"); |
|
419 |
|
420 QT_TRAP_THROWING( m_devSound.reset(CMMFDevSound::NewL()) ) |
|
421 |
|
422 QScopedPointer<SymbianAudio::DevSoundCapabilities> caps( |
|
423 new SymbianAudio::DevSoundCapabilities(*m_devSound, |
|
424 QAudio::AudioOutput)); |
|
425 |
|
426 int err = SymbianAudio::Utils::isFormatSupported(m_format, *caps) ? |
|
427 KErrNone : KErrNotSupported; |
|
428 |
|
429 if (KErrNone == err) { |
|
430 setState(SymbianAudio::InitializingState); |
|
431 TRAP(err, m_devSound->InitializeL(*this, m_nativeFourCC, |
|
432 EMMFStatePlaying)); |
|
433 } |
|
434 |
|
435 if (KErrNone != err) { |
|
436 setError(QAudio::OpenError); |
|
437 m_devSound.reset(); |
|
438 } |
|
439 } |
|
440 |
|
441 void QAudioOutputPrivate::startPlayback() |
|
442 { |
|
443 TRAPD(err, startDevSoundL()); |
|
444 if (KErrNone == err) { |
|
445 if (isDataReady()) |
|
446 setState(SymbianAudio::ActiveState); |
|
447 else |
|
448 setState(SymbianAudio::IdleState); |
|
449 |
|
450 m_notifyTimer->start(m_notifyInterval); |
|
451 m_underflow = false; |
|
452 |
|
453 Q_ASSERT(m_devSound->SamplesPlayed() == 0); |
|
454 |
|
455 writePaddingData(); |
|
456 |
|
457 if (m_pullMode && m_source->bytesAvailable() && !m_bytesPadding) |
|
458 dataReady(); |
|
459 } else { |
|
460 setError(QAudio::OpenError); |
|
461 close(); |
|
462 } |
|
463 } |
|
464 |
|
465 void QAudioOutputPrivate::startDevSoundL() |
|
466 { |
|
467 TMMFCapabilities nativeFormat = m_devSound->Config(); |
|
468 m_nativeFormat.iBufferSize = nativeFormat.iBufferSize; |
|
469 m_devSound->SetConfigL(m_nativeFormat); |
|
470 m_devSound->PlayInitL(); |
|
471 } |
|
472 |
|
473 void QAudioOutputPrivate::writePaddingData() |
|
474 { |
|
475 // See comments in suspend() |
|
476 |
|
477 while (m_devSoundBuffer && m_bytesPadding) { |
|
478 if (SymbianAudio::IdleState == m_internalState) |
|
479 setState(SymbianAudio::ActiveState); |
|
480 |
|
481 TDes8 &outputBuffer = m_devSoundBuffer->Data(); |
|
482 const qint64 outputBytes = bytesFree(); |
|
483 const qint64 paddingBytes = outputBytes < m_bytesPadding ? |
|
484 outputBytes : m_bytesPadding; |
|
485 unsigned char *ptr = const_cast<unsigned char*>(outputBuffer.Ptr()); |
|
486 Mem::FillZ(ptr, paddingBytes); |
|
487 outputBuffer.SetLength(outputBuffer.Length() + paddingBytes); |
|
488 m_bytesPadding -= paddingBytes; |
|
489 |
|
490 if (m_pullMode && m_source->atEnd()) |
|
491 lastBufferFilled(); |
|
492 if (paddingBytes == outputBytes) |
|
493 bufferFilled(); |
|
494 } |
|
495 } |
|
496 |
|
497 qint64 QAudioOutputPrivate::pushData(const char *data, qint64 len) |
|
498 { |
|
499 // Data has been written to SymbianAudioOutputPrivate |
|
500 |
|
501 Q_ASSERT_X(!m_pullMode, Q_FUNC_INFO, |
|
502 "pushData called when in pull mode"); |
|
503 |
|
504 const unsigned char *const inputPtr = |
|
505 reinterpret_cast<const unsigned char*>(data); |
|
506 qint64 bytesWritten = 0; |
|
507 |
|
508 if (SymbianAudio::IdleState == m_internalState) |
|
509 setState(SymbianAudio::ActiveState); |
|
510 |
|
511 while (m_devSoundBuffer && (bytesWritten < len)) { |
|
512 // writePaddingData() is called from BufferToBeFilled(), so we should |
|
513 // never have any padding data left at this point. |
|
514 Q_ASSERT_X(0 == m_bytesPadding, Q_FUNC_INFO, |
|
515 "Padding bytes remaining in pushData"); |
|
516 |
|
517 TDes8 &outputBuffer = m_devSoundBuffer->Data(); |
|
518 |
|
519 const qint64 outputBytes = bytesFree(); |
|
520 const qint64 inputBytes = len - bytesWritten; |
|
521 const qint64 copyBytes = outputBytes < inputBytes ? |
|
522 outputBytes : inputBytes; |
|
523 |
|
524 outputBuffer.Append(inputPtr + bytesWritten, copyBytes); |
|
525 bytesWritten += copyBytes; |
|
526 |
|
527 bufferFilled(); |
|
528 } |
|
529 |
|
530 m_pushDataReady = (bytesWritten < len); |
|
531 |
|
532 // If DevSound is still initializing (m_internalState == InitializingState), |
|
533 // we cannot transition m_internalState to ActiveState, but we must emit |
|
534 // an (external) state change from IdleState to ActiveState. The following |
|
535 // call triggers this signal. |
|
536 setState(m_internalState); |
|
537 |
|
538 return bytesWritten; |
|
539 } |
|
540 |
|
541 void QAudioOutputPrivate::pullData() |
|
542 { |
|
543 Q_ASSERT_X(m_pullMode, Q_FUNC_INFO, |
|
544 "pullData called when in push mode"); |
|
545 |
|
546 if (m_bytesPadding) |
|
547 m_bytesPadding = 1; |
|
548 |
|
549 // writePaddingData() is called by BufferToBeFilled() before pullData(), |
|
550 // so we should never have any padding data left at this point. |
|
551 Q_ASSERT_X(0 == m_bytesPadding, Q_FUNC_INFO, |
|
552 "Padding bytes remaining in pullData"); |
|
553 |
|
554 qint64 inputBytes = m_source->bytesAvailable(); |
|
555 while (m_devSoundBuffer && inputBytes) { |
|
556 if (SymbianAudio::IdleState == m_internalState) |
|
557 setState(SymbianAudio::ActiveState); |
|
558 |
|
559 TDes8 &outputBuffer = m_devSoundBuffer->Data(); |
|
560 |
|
561 const qint64 outputBytes = bytesFree(); |
|
562 const qint64 copyBytes = outputBytes < inputBytes ? |
|
563 outputBytes : inputBytes; |
|
564 |
|
565 char *outputPtr = (char*)(outputBuffer.Ptr() + outputBuffer.Length()); |
|
566 const qint64 bytesCopied = m_source->read(outputPtr, copyBytes); |
|
567 Q_ASSERT(bytesCopied == copyBytes); |
|
568 outputBuffer.SetLength(outputBuffer.Length() + bytesCopied); |
|
569 inputBytes -= bytesCopied; |
|
570 |
|
571 if (m_source->atEnd()) |
|
572 lastBufferFilled(); |
|
573 else if (copyBytes == outputBytes) |
|
574 bufferFilled(); |
|
575 } |
|
576 } |
|
577 |
|
578 void QAudioOutputPrivate::bufferFilled() |
|
579 { |
|
580 Q_ASSERT_X(m_devSoundBuffer, Q_FUNC_INFO, "No buffer to return"); |
|
581 |
|
582 const TDes8 &outputBuffer = m_devSoundBuffer->Data(); |
|
583 m_bytesWritten += outputBuffer.Length(); |
|
584 |
|
585 m_devSoundBuffer = 0; |
|
586 |
|
587 m_samplesPlayed = getSamplesPlayed(); |
|
588 m_underflowTimer->start(); |
|
589 |
|
590 if (QAudio::UnderrunError == m_error) |
|
591 m_error = QAudio::NoError; |
|
592 |
|
593 m_devSound->PlayData(); |
|
594 } |
|
595 |
|
596 void QAudioOutputPrivate::lastBufferFilled() |
|
597 { |
|
598 Q_ASSERT_X(m_devSoundBuffer, Q_FUNC_INFO, "No buffer to fill"); |
|
599 Q_ASSERT_X(!m_lastBuffer, Q_FUNC_INFO, "Last buffer already sent"); |
|
600 m_lastBuffer = true; |
|
601 m_devSoundBuffer->SetLastBuffer(ETrue); |
|
602 bufferFilled(); |
|
603 } |
|
604 |
|
605 void QAudioOutputPrivate::close() |
|
606 { |
|
607 m_notifyTimer->stop(); |
|
608 m_underflowTimer->stop(); |
|
609 |
|
610 m_error = QAudio::NoError; |
|
611 |
|
612 if (m_devSound) |
|
613 m_devSound->Stop(); |
|
614 m_devSound.reset(); |
|
615 m_devSoundBuffer = 0; |
|
616 m_devSoundBufferSize = 0; |
|
617 |
|
618 if (!m_pullMode) // m_source is owned |
|
619 delete m_source; |
|
620 m_pullMode = false; |
|
621 m_source = 0; |
|
622 |
|
623 m_bytesWritten = 0; |
|
624 m_pushDataReady = false; |
|
625 m_bytesPadding = 0; |
|
626 m_underflow = false; |
|
627 m_lastBuffer = false; |
|
628 m_samplesPlayed = 0; |
|
629 m_totalSamplesPlayed = 0; |
|
630 |
|
631 setState(SymbianAudio::ClosedState); |
|
632 } |
|
633 |
|
634 qint64 QAudioOutputPrivate::getSamplesPlayed() const |
|
635 { |
|
636 qint64 result = 0; |
|
637 if (m_devSound) { |
|
638 const qint64 samplesWritten = SymbianAudio::Utils::bytesToSamples( |
|
639 m_format, m_bytesWritten); |
|
640 |
|
641 if (m_underflow) { |
|
642 result = samplesWritten; |
|
643 } else { |
|
644 // This is necessary because some DevSound implementations report |
|
645 // that they have played more data than has actually been provided to them |
|
646 // by the client. |
|
647 const qint64 devSoundSamplesPlayed(m_devSound->SamplesPlayed()); |
|
648 result = qMin(devSoundSamplesPlayed, samplesWritten); |
|
649 } |
|
650 } |
|
651 return result; |
|
652 } |
|
653 |
|
654 void QAudioOutputPrivate::setError(QAudio::Error error) |
|
655 { |
|
656 m_error = error; |
|
657 |
|
658 // Although no state transition actually occurs here, a stateChanged event |
|
659 // must be emitted to inform the client that the call to start() was |
|
660 // unsuccessful. |
|
661 if (QAudio::OpenError == error) |
|
662 emit stateChanged(QAudio::StoppedState); |
|
663 |
|
664 if (QAudio::UnderrunError == error) |
|
665 setState(SymbianAudio::IdleState); |
|
666 else |
|
667 // Close the DevSound instance. This causes a transition to |
|
668 // StoppedState. This must be done asynchronously in case the |
|
669 // current function was called from a DevSound event handler, in which |
|
670 // case deleting the DevSound instance may cause an exception. |
|
671 QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection); |
|
672 } |
|
673 |
|
674 void QAudioOutputPrivate::setState(SymbianAudio::State newInternalState) |
|
675 { |
|
676 const QAudio::State oldExternalState = m_externalState; |
|
677 m_internalState = newInternalState; |
|
678 m_externalState = SymbianAudio::Utils::stateNativeToQt( |
|
679 m_internalState, initializingState()); |
|
680 |
|
681 if (m_externalState != oldExternalState) |
|
682 emit stateChanged(m_externalState); |
|
683 } |
|
684 |
|
685 bool QAudioOutputPrivate::isDataReady() const |
|
686 { |
|
687 return (m_source && m_source->bytesAvailable()) |
|
688 || m_bytesPadding |
|
689 || m_pushDataReady; |
|
690 } |
|
691 |
|
692 QAudio::State QAudioOutputPrivate::initializingState() const |
|
693 { |
|
694 return isDataReady() ? QAudio::ActiveState : QAudio::IdleState; |
|
695 } |
|
696 |
|
697 QT_END_NAMESPACE |