CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / media / libsydneyaudio / bug495794_closeAudio.patch
blob527f195503e790722343cfc62a8bde7409f48b2c
1 diff --git a/media/libsydneyaudio/src/sydney_audio_waveapi.c b/media/libsydneyaudio/src/sydney_audio_waveapi.c
2 --- a/media/libsydneyaudio/src/sydney_audio_waveapi.c
3 +++ b/media/libsydneyaudio/src/sydney_audio_waveapi.c
4 @@ -416,29 +416,34 @@ int openAudio(sa_stream_t *s) {
5 wfx.nBlockAlign = (wfx.wBitsPerSample * wfx.nChannels) >> 3;
6 wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
8 supported = waveOutOpen(NULL, WAVE_MAPPER, &wfx, (DWORD_PTR)0, (DWORD_PTR)0,
9 WAVE_FORMAT_QUERY);
10 if (supported == MMSYSERR_NOERROR) { // audio device opened sucessfully
11 status = waveOutOpen((LPHWAVEOUT)&(s->hWaveOut), WAVE_MAPPER, &wfx,
12 (DWORD_PTR)waveOutProc, (DWORD_PTR)s, CALLBACK_FUNCTION);
13 - HANDLE_WAVE_ERROR(status, "opening audio device for playback");
14 - printf("Audio device sucessfully opened\n");
15 + if (status != MMSYSERR_NOERROR) {
16 + freeBlocks(s->waveBlocks);
17 + s->waveBlocks = NULL;
18 + HANDLE_WAVE_ERROR(status, "opening audio device for playback");
19 + }
21 else if (supported == WAVERR_BADFORMAT) {
22 - printf("Requested format not supported...\n");
23 - // clean up the memory
24 - freeBlocks(s->waveBlocks);
25 + printf("Requested format not supported.\n");
26 + // clean up the memory
27 + freeBlocks(s->waveBlocks);
28 + s->waveBlocks = NULL;
29 return SA_ERROR_NOT_SUPPORTED;
31 else {
32 - printf("Error opening default audio device. Exiting...\n");
33 - // clean up the memory
34 - freeBlocks(s->waveBlocks);
35 + printf("Error opening default audio device.\n");
36 + // clean up the memory
37 + freeBlocks(s->waveBlocks);
38 + s->waveBlocks = NULL;
39 return SA_ERROR_SYSTEM;
41 // create notification for data written to a device
42 s->callbackEvent = CreateEvent(0, FALSE, FALSE, 0);
43 // initialise critical section for operations on waveFreeBlockCound variable
44 InitializeCriticalSection(&(s->waveCriticalSection));
46 return SA_SUCCESS;
47 @@ -454,40 +459,43 @@ int closeAudio(sa_stream_t * s) {
48 result = SA_SUCCESS;
50 // reseting audio device and flushing buffers
51 status = waveOutReset(s->hWaveOut);
52 if (status != MMSYSERR_NOERROR) {
53 result = getSAErrorCode(status);
56 - /* wait for all blocks to complete */
57 - while(s->waveFreeBlockCount < BLOCK_COUNT) {
58 - Sleep(10);
59 + if (s->waveBlocks) {
60 + /* wait for all blocks to complete */
61 + while(s->waveFreeBlockCount < BLOCK_COUNT) {
62 + Sleep(10);
63 + }
65 + /* unprepare any blocks that are still prepared */
66 + for(i = 0; i < s->waveFreeBlockCount; i++) {
67 + if(s->waveBlocks[i].dwFlags & WHDR_PREPARED) {
68 + status = waveOutUnprepareHeader(s->hWaveOut, &(s->waveBlocks[i]), sizeof(WAVEHDR));
69 + if (status != MMSYSERR_NOERROR) {
70 + result = getSAErrorCode(status);
71 + }
72 + }
73 + }
75 + freeBlocks(s->waveBlocks);
76 + s->waveBlocks = NULL;
79 - /* unprepare any blocks that are still prepared */
80 - for(i = 0; i < s->waveFreeBlockCount; i++) {
81 - if(s->waveBlocks[i].dwFlags & WHDR_PREPARED) {
82 - status = waveOutUnprepareHeader(s->hWaveOut, &(s->waveBlocks[i]), sizeof(WAVEHDR));
83 - if (status != MMSYSERR_NOERROR) {
84 - result = getSAErrorCode(status);
85 - }
86 - }
87 - }
89 - freeBlocks(s->waveBlocks);
90 status = waveOutClose(s->hWaveOut);
91 if (status != MMSYSERR_NOERROR) {
92 result = getSAErrorCode(status);
95 DeleteCriticalSection(&(s->waveCriticalSection));
96 CloseHandle(s->callbackEvent);
97 - printf("[audio] audio resources cleanup completed\n");
99 return result;
102 * \brief - writes PCM audio samples to audio device
103 * \param s - valid handle to opened sydney stream
104 * \param data - pointer to memory storing audio samples to be played
105 * \param nsamples - number of samples in the memory pointed by previous parameter