src/plugins/audio/symbian/symbianaudiooutput.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 22 79de32ba3296
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
     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 "symbianaudiooutput.h"
       
    43 #include "symbianaudioutils.h"
       
    44 
       
    45 QT_BEGIN_NAMESPACE
       
    46 
       
    47 //-----------------------------------------------------------------------------
       
    48 // Constants
       
    49 //-----------------------------------------------------------------------------
       
    50 
       
    51 const int UnderflowTimerInterval = 50; // ms
       
    52 
       
    53 
       
    54 //-----------------------------------------------------------------------------
       
    55 // Private class
       
    56 //-----------------------------------------------------------------------------
       
    57 
       
    58 SymbianAudioOutputPrivate::SymbianAudioOutputPrivate(
       
    59                                SymbianAudioOutput *audioDevice)
       
    60     :   m_audioDevice(audioDevice)
       
    61 {
       
    62 
       
    63 }
       
    64 
       
    65 SymbianAudioOutputPrivate::~SymbianAudioOutputPrivate()
       
    66 {
       
    67 
       
    68 }
       
    69 
       
    70 qint64 SymbianAudioOutputPrivate::readData(char *data, qint64 len)
       
    71 {
       
    72     Q_UNUSED(data)
       
    73     Q_UNUSED(len)
       
    74     return 0;
       
    75 }
       
    76 
       
    77 qint64 SymbianAudioOutputPrivate::writeData(const char *data, qint64 len)
       
    78 {
       
    79     qint64 totalWritten = 0;
       
    80 
       
    81     if (m_audioDevice->state() == QAudio::ActiveState ||
       
    82         m_audioDevice->state() == QAudio::IdleState) {
       
    83 
       
    84         while (totalWritten < len) {
       
    85             const qint64 written = m_audioDevice->pushData(data + totalWritten,
       
    86                                                            len - totalWritten);
       
    87             if (written > 0)
       
    88                 totalWritten += written;
       
    89             else
       
    90                 break;
       
    91         }
       
    92     }
       
    93 
       
    94     return totalWritten;
       
    95 }
       
    96 
       
    97 
       
    98 //-----------------------------------------------------------------------------
       
    99 // Public functions
       
   100 //-----------------------------------------------------------------------------
       
   101 
       
   102 SymbianAudioOutput::SymbianAudioOutput(const QByteArray &device,
       
   103                                        const QAudioFormat &format)
       
   104     :   m_device(device)
       
   105     ,   m_format(format)
       
   106     ,   m_clientBufferSize(SymbianAudio::DefaultBufferSize)
       
   107     ,   m_notifyInterval(SymbianAudio::DefaultNotifyInterval)
       
   108     ,   m_notifyTimer(new QTimer(this))
       
   109     ,   m_error(QAudio::NoError)
       
   110     ,   m_internalState(SymbianAudio::ClosedState)
       
   111     ,   m_externalState(QAudio::StoppedState)
       
   112     ,   m_pullMode(false)
       
   113     ,   m_source(0)
       
   114     ,   m_devSoundBuffer(0)
       
   115     ,   m_devSoundBufferSize(0)
       
   116     ,   m_bytesWritten(0)
       
   117     ,   m_pushDataReady(false)
       
   118     ,   m_bytesPadding(0)
       
   119     ,   m_underflow(false)
       
   120     ,   m_lastBuffer(false)
       
   121     ,   m_underflowTimer(new QTimer(this))
       
   122     ,   m_samplesPlayed(0)
       
   123     ,   m_totalSamplesPlayed(0)
       
   124 {
       
   125     connect(m_notifyTimer.data(), SIGNAL(timeout()), this, SIGNAL(notify()));
       
   126 
       
   127     SymbianAudio::Utils::formatQtToNative(m_format, m_nativeFourCC,
       
   128                                           m_nativeFormat);
       
   129 
       
   130     m_underflowTimer->setInterval(UnderflowTimerInterval);
       
   131     connect(m_underflowTimer.data(), SIGNAL(timeout()), this,
       
   132             SLOT(underflowTimerExpired()));
       
   133 }
       
   134 
       
   135 SymbianAudioOutput::~SymbianAudioOutput()
       
   136 {
       
   137     close();
       
   138 }
       
   139 
       
   140 QIODevice* SymbianAudioOutput::start(QIODevice *device)
       
   141 {
       
   142     stop();
       
   143 
       
   144     // We have to set these before the call to open() because of the
       
   145     // logic in initializingState()
       
   146     if (device) {
       
   147         m_pullMode = true;
       
   148         m_source = device;
       
   149     }
       
   150 
       
   151     open();
       
   152 
       
   153     if (SymbianAudio::ClosedState != m_internalState) {
       
   154         if (device) {
       
   155             connect(m_source, SIGNAL(readyRead()), this, SLOT(dataReady()));
       
   156         } else {
       
   157             m_source = new SymbianAudioOutputPrivate(this);
       
   158             m_source->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
       
   159         }
       
   160 
       
   161         m_elapsed.restart();
       
   162     }
       
   163 
       
   164     return m_source;
       
   165 }
       
   166 
       
   167 void SymbianAudioOutput::stop()
       
   168 {
       
   169     close();
       
   170 }
       
   171 
       
   172 void SymbianAudioOutput::reset()
       
   173 {
       
   174     m_totalSamplesPlayed += getSamplesPlayed();
       
   175     m_devSound->Stop();
       
   176     m_bytesPadding = 0;
       
   177     startPlayback();
       
   178 }
       
   179 
       
   180 void SymbianAudioOutput::suspend()
       
   181 {
       
   182     if (SymbianAudio::ActiveState == m_internalState
       
   183         || SymbianAudio::IdleState == m_internalState) {
       
   184         m_notifyTimer->stop();
       
   185         m_underflowTimer->stop();
       
   186 
       
   187         const qint64 samplesWritten = SymbianAudio::Utils::bytesToSamples(
       
   188                                           m_format, m_bytesWritten);
       
   189         m_bytesWritten = 0;
       
   190 
       
   191         const qint64 samplesPlayed = getSamplesPlayed();
       
   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 SymbianAudioOutput::resume()
       
   212 {
       
   213     if (SymbianAudio::SuspendedState == m_internalState)
       
   214         startPlayback();
       
   215 }
       
   216 
       
   217 int SymbianAudioOutput::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 SymbianAudioOutput::periodSize() const
       
   228 {
       
   229     return bufferSize();
       
   230 }
       
   231 
       
   232 void SymbianAudioOutput::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 SymbianAudioOutput::bufferSize() const
       
   246 {
       
   247     return m_devSoundBufferSize ? m_devSoundBufferSize : m_clientBufferSize;
       
   248 }
       
   249 
       
   250 void SymbianAudioOutput::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 SymbianAudioOutput::notifyInterval() const
       
   261 {
       
   262     return m_notifyInterval;
       
   263 }
       
   264 
       
   265 qint64 SymbianAudioOutput::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 SymbianAudioOutput::elapsedUSecs() const
       
   282 {
       
   283     const qint64 result = (QAudio::StoppedState == state()) ?
       
   284                               0 : m_elapsed.elapsed() * 1000;
       
   285     return result;
       
   286 }
       
   287 
       
   288 QAudio::Error SymbianAudioOutput::error() const
       
   289 {
       
   290     return m_error;
       
   291 }
       
   292 
       
   293 QAudio::State SymbianAudioOutput::state() const
       
   294 {
       
   295     return m_externalState;
       
   296 }
       
   297 
       
   298 QAudioFormat SymbianAudioOutput::format() const
       
   299 {
       
   300     return m_format;
       
   301 }
       
   302 
       
   303 //-----------------------------------------------------------------------------
       
   304 // MDevSoundObserver implementation
       
   305 //-----------------------------------------------------------------------------
       
   306 
       
   307 void SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::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 SymbianAudioOutput::isDataReady() const
       
   686 {
       
   687     return (m_source && m_source->bytesAvailable())
       
   688         ||  m_bytesPadding
       
   689         ||  m_pushDataReady;
       
   690 }
       
   691 
       
   692 QAudio::State SymbianAudioOutput::initializingState() const
       
   693 {
       
   694     return isDataReady() ? QAudio::ActiveState : QAudio::IdleState;
       
   695 }
       
   696 
       
   697 QT_END_NAMESPACE