diff --git a/media/libsydneyaudio/src/sydney_audio_waveapi.c b/media/libsydneyaudio/src/sydney_audio_waveapi.c --- a/media/libsydneyaudio/src/sydney_audio_waveapi.c +++ b/media/libsydneyaudio/src/sydney_audio_waveapi.c @@ -416,29 +416,34 @@ int openAudio(sa_stream_t *s) { wfx.nBlockAlign = (wfx.wBitsPerSample * wfx.nChannels) >> 3; wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; supported = waveOutOpen(NULL, WAVE_MAPPER, &wfx, (DWORD_PTR)0, (DWORD_PTR)0, WAVE_FORMAT_QUERY); if (supported == MMSYSERR_NOERROR) { // audio device opened sucessfully status = waveOutOpen((LPHWAVEOUT)&(s->hWaveOut), WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc, (DWORD_PTR)s, CALLBACK_FUNCTION); - HANDLE_WAVE_ERROR(status, "opening audio device for playback"); - printf("Audio device sucessfully opened\n"); + if (status != MMSYSERR_NOERROR) { + freeBlocks(s->waveBlocks); + s->waveBlocks = NULL; + HANDLE_WAVE_ERROR(status, "opening audio device for playback"); + } } else if (supported == WAVERR_BADFORMAT) { - printf("Requested format not supported...\n"); - // clean up the memory - freeBlocks(s->waveBlocks); + printf("Requested format not supported.\n"); + // clean up the memory + freeBlocks(s->waveBlocks); + s->waveBlocks = NULL; return SA_ERROR_NOT_SUPPORTED; } else { - printf("Error opening default audio device. Exiting...\n"); - // clean up the memory - freeBlocks(s->waveBlocks); + printf("Error opening default audio device.\n"); + // clean up the memory + freeBlocks(s->waveBlocks); + s->waveBlocks = NULL; return SA_ERROR_SYSTEM; } // create notification for data written to a device s->callbackEvent = CreateEvent(0, FALSE, FALSE, 0); // initialise critical section for operations on waveFreeBlockCound variable InitializeCriticalSection(&(s->waveCriticalSection)); return SA_SUCCESS; @@ -454,40 +459,43 @@ int closeAudio(sa_stream_t * s) { result = SA_SUCCESS; // reseting audio device and flushing buffers status = waveOutReset(s->hWaveOut); if (status != MMSYSERR_NOERROR) { result = getSAErrorCode(status); } - /* wait for all blocks to complete */ - while(s->waveFreeBlockCount < BLOCK_COUNT) { - Sleep(10); + if (s->waveBlocks) { + /* wait for all blocks to complete */ + while(s->waveFreeBlockCount < BLOCK_COUNT) { + Sleep(10); + } + + /* unprepare any blocks that are still prepared */ + for(i = 0; i < s->waveFreeBlockCount; i++) { + if(s->waveBlocks[i].dwFlags & WHDR_PREPARED) { + status = waveOutUnprepareHeader(s->hWaveOut, &(s->waveBlocks[i]), sizeof(WAVEHDR)); + if (status != MMSYSERR_NOERROR) { + result = getSAErrorCode(status); + } + } + } + + freeBlocks(s->waveBlocks); + s->waveBlocks = NULL; } - /* unprepare any blocks that are still prepared */ - for(i = 0; i < s->waveFreeBlockCount; i++) { - if(s->waveBlocks[i].dwFlags & WHDR_PREPARED) { - status = waveOutUnprepareHeader(s->hWaveOut, &(s->waveBlocks[i]), sizeof(WAVEHDR)); - if (status != MMSYSERR_NOERROR) { - result = getSAErrorCode(status); - } - } - } - - freeBlocks(s->waveBlocks); status = waveOutClose(s->hWaveOut); if (status != MMSYSERR_NOERROR) { result = getSAErrorCode(status); } DeleteCriticalSection(&(s->waveCriticalSection)); CloseHandle(s->callbackEvent); - printf("[audio] audio resources cleanup completed\n"); return result; } /** * \brief - writes PCM audio samples to audio device * \param s - valid handle to opened sydney stream * \param data - pointer to memory storing audio samples to be played * \param nsamples - number of samples in the memory pointed by previous parameter