diff options
Diffstat (limited to 'src/multimedia/audio/qaudiooutput_win32_p.cpp')
-rw-r--r-- | src/multimedia/audio/qaudiooutput_win32_p.cpp | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/src/multimedia/audio/qaudiooutput_win32_p.cpp b/src/multimedia/audio/qaudiooutput_win32_p.cpp index b92565d..533583a 100644 --- a/src/multimedia/audio/qaudiooutput_win32_p.cpp +++ b/src/multimedia/audio/qaudiooutput_win32_p.cpp @@ -56,8 +56,6 @@ QT_BEGIN_NAMESPACE -static const int minimumIntervalTime = 50; - QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device, const QAudioFormat& audioFormat): settings(audioFormat) { @@ -73,17 +71,15 @@ QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device, const QAudioF audioSource = 0; pullMode = true; finished = false; - InitializeCriticalSection(&waveOutCriticalSection); } QAudioOutputPrivate::~QAudioOutputPrivate() { - EnterCriticalSection(&waveOutCriticalSection); + mutex.lock(); finished = true; - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); close(); - DeleteCriticalSection(&waveOutCriticalSection); } void CALLBACK QAudioOutputPrivate::waveOutProc( HWAVEOUT hWaveOut, UINT uMsg, @@ -98,6 +94,8 @@ void CALLBACK QAudioOutputPrivate::waveOutProc( HWAVEOUT hWaveOut, UINT uMsg, if(!qAudio) return; + QMutexLocker(&qAudio->mutex); + switch(uMsg) { case WOM_OPEN: qAudio->feedback(); @@ -105,16 +103,13 @@ void CALLBACK QAudioOutputPrivate::waveOutProc( HWAVEOUT hWaveOut, UINT uMsg, case WOM_CLOSE: return; case WOM_DONE: - EnterCriticalSection(&qAudio->waveOutCriticalSection); if(qAudio->finished || qAudio->buffer_size == 0 || qAudio->period_size == 0) { - LeaveCriticalSection(&qAudio->waveOutCriticalSection); return; } qAudio->waveFreeBlockCount++; if(qAudio->waveFreeBlockCount >= qAudio->buffer_size/qAudio->period_size) qAudio->waveFreeBlockCount = qAudio->buffer_size/qAudio->period_size; qAudio->feedback(); - LeaveCriticalSection(&qAudio->waveOutCriticalSection); break; default: return; @@ -150,8 +145,7 @@ void QAudioOutputPrivate::freeBlocks(WAVEHDR* blockArray) int count = buffer_size/period_size; for(int i = 0; i < count; i++) { - if (blocks->dwFlags & WHDR_PREPARED) - waveOutUnprepareHeader(hWaveOut,blocks, sizeof(WAVEHDR)); + waveOutUnprepareHeader(hWaveOut,blocks, sizeof(WAVEHDR)); blocks+=sizeof(WAVEHDR); } HeapFree(GetProcessHeap(), 0, blockArray); @@ -226,9 +220,9 @@ bool QAudioOutputPrivate::open() } waveBlocks = allocateBlocks(period_size, buffer_size/period_size); - EnterCriticalSection(&waveOutCriticalSection); + mutex.lock(); waveFreeBlockCount = buffer_size/period_size; - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); waveCurrentBlock = 0; @@ -334,10 +328,7 @@ int QAudioOutputPrivate::bufferSize() const void QAudioOutputPrivate::setNotifyInterval(int ms) { - if(ms >= minimumIntervalTime) - intervalTime = ms; - else - intervalTime = minimumIntervalTime; + intervalTime = qMax(0, ms); } int QAudioOutputPrivate::notifyInterval() const @@ -369,12 +360,12 @@ qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) int remain; current = &waveBlocks[waveCurrentBlock]; while(l > 0) { - EnterCriticalSection(&waveOutCriticalSection); + mutex.lock(); if(waveFreeBlockCount==0) { - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); break; } - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); if(current->dwFlags & WHDR_PREPARED) waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR)); @@ -391,15 +382,13 @@ qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR)); waveOutWrite(hWaveOut, current, sizeof(WAVEHDR)); - EnterCriticalSection(&waveOutCriticalSection); + mutex.lock(); waveFreeBlockCount--; - LeaveCriticalSection(&waveOutCriticalSection); #ifdef DEBUG_AUDIO - EnterCriticalSection(&waveOutCriticalSection); qDebug("write out l=%d, waveFreeBlockCount=%d", current->dwBufferLength,waveFreeBlockCount); - LeaveCriticalSection(&waveOutCriticalSection); #endif + mutex.unlock(); totalTimeValue += current->dwBufferLength; waveCurrentBlock++; waveCurrentBlock %= buffer_size/period_size; @@ -454,7 +443,7 @@ void QAudioOutputPrivate::feedback() bool QAudioOutputPrivate::deviceReady() { - if(deviceState == QAudio::StoppedState) + if(deviceState == QAudio::StoppedState || deviceState == QAudio::SuspendedState) return false; if(pullMode) { @@ -468,14 +457,16 @@ bool QAudioOutputPrivate::deviceReady() startup = true; bool full=false; - EnterCriticalSection(&waveOutCriticalSection); + + mutex.lock(); if(waveFreeBlockCount==0) full = true; - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); + if (full){ #ifdef DEBUG_AUDIO qDebug() << "Skipping data as unable to write"; #endif - if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime ) { + if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime ) { emit notify(); elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; timeStamp.restart(); @@ -505,12 +496,14 @@ bool QAudioOutputPrivate::deviceReady() bytesAvailable = bytesFree(); int check = 0; - EnterCriticalSection(&waveOutCriticalSection); + + mutex.lock(); check = waveFreeBlockCount; - LeaveCriticalSection(&waveOutCriticalSection); + mutex.unlock(); + if(check == buffer_size/period_size) { - errorState = QAudio::UnderrunError; if (deviceState != QAudio::IdleState) { + errorState = QAudio::UnderrunError; deviceState = QAudio::IdleState; emit stateChanged(deviceState); } @@ -522,19 +515,23 @@ bool QAudioOutputPrivate::deviceReady() } } else { int buffered; - EnterCriticalSection(&waveOutCriticalSection); + + mutex.lock(); buffered = waveFreeBlockCount; - LeaveCriticalSection(&waveOutCriticalSection); - errorState = QAudio::UnderrunError; + mutex.unlock(); + if (buffered >= buffer_size/period_size && deviceState == QAudio::ActiveState) { - deviceState = QAudio::IdleState; - emit stateChanged(deviceState); + if (deviceState != QAudio::IdleState) { + errorState = QAudio::UnderrunError; + deviceState = QAudio::IdleState; + emit stateChanged(deviceState); + } } } if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) return true; - if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { + if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { emit notify(); elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; timeStamp.restart(); |