OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/win/waveout_output_win.h" | 5 #include "media/audio/win/waveout_output_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <mmsystem.h> | 8 #include <mmsystem.h> |
9 #pragma comment(lib, "winmm.lib") | 9 #pragma comment(lib, "winmm.lib") |
10 | 10 |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // of all currently playing streams. Then you don't have to wait | 241 // of all currently playing streams. Then you don't have to wait |
242 // till all callbacks are completed. Of course access to singleton | 242 // till all callbacks are completed. Of course access to singleton |
243 // should be under its own lock, and checking the liveness and | 243 // should be under its own lock, and checking the liveness and |
244 // acquiring the lock on stream should be done atomically. | 244 // acquiring the lock on stream should be done atomically. |
245 void PCMWaveOutAudioOutputStream::Stop() { | 245 void PCMWaveOutAudioOutputStream::Stop() { |
246 if (state_ != PCMA_PLAYING) | 246 if (state_ != PCMA_PLAYING) |
247 return; | 247 return; |
248 state_ = PCMA_STOPPING; | 248 state_ = PCMA_STOPPING; |
249 base::subtle::MemoryBarrier(); | 249 base::subtle::MemoryBarrier(); |
250 | 250 |
251 // Stop watching for buffer event, wait till all the callbacks are complete. | 251 // Stop watching for buffer event, waits until outstanding callbacks finish. |
252 // Should be done before ::waveOutReset() call to avoid race condition when | |
253 // callback that is currently active and already checked that stream is still | |
254 // being played calls ::waveOutWrite() after ::waveOutReset() returns, later | |
255 // causing ::waveOutClose() to fail with WAVERR_STILLPLAYING. | |
256 // TODO(enal): that delays actual stopping of playback. Alternative can be | |
257 // to call ::waveOutReset() twice, once before | |
258 // ::UnregisterWaitEx() and once after. | |
259 if (waiting_handle_) { | 252 if (waiting_handle_) { |
260 if (!::UnregisterWaitEx(waiting_handle_, INVALID_HANDLE_VALUE)) { | 253 if (!::UnregisterWaitEx(waiting_handle_, INVALID_HANDLE_VALUE)) |
261 state_ = PCMA_PLAYING; | 254 HandleError(::GetLastError()); |
262 HandleError(MMSYSERR_ERROR); | |
263 return; | |
264 } | |
265 waiting_handle_ = NULL; | 255 waiting_handle_ = NULL; |
266 } | 256 } |
267 | 257 |
268 // Stop playback. | 258 // Stop playback. |
269 MMRESULT res = ::waveOutReset(waveout_); | 259 MMRESULT res = ::waveOutReset(waveout_); |
270 if (res != MMSYSERR_NOERROR) { | 260 if (res != MMSYSERR_NOERROR) |
271 state_ = PCMA_PLAYING; | |
272 HandleError(res); | 261 HandleError(res); |
273 return; | |
274 } | |
275 | 262 |
276 // Wait for lock to ensure all outstanding callbacks have completed. | 263 // Wait for lock to ensure all outstanding callbacks have completed. |
277 base::AutoLock auto_lock(lock_); | 264 base::AutoLock auto_lock(lock_); |
278 | 265 |
279 // waveOutReset() leaves buffers in the unpredictable state, causing | 266 // waveOutReset() leaves buffers in the unpredictable state, causing |
280 // problems if we want to close, release, or reuse them. Fix the states. | 267 // problems if we want to close, release, or reuse them. Fix the states. |
281 for (int ix = 0; ix != num_buffers_; ++ix) { | 268 for (int ix = 0; ix != num_buffers_; ++ix) |
282 GetBuffer(ix)->dwFlags = WHDR_PREPARED; | 269 GetBuffer(ix)->dwFlags = WHDR_PREPARED; |
283 } | |
284 | 270 |
285 // Don't use callback after Stop(). | 271 // Don't use callback after Stop(). |
286 callback_ = NULL; | 272 callback_ = NULL; |
287 | 273 |
288 state_ = PCMA_READY; | 274 state_ = PCMA_READY; |
289 } | 275 } |
290 | 276 |
291 // We can Close in any state except that trying to close a stream that is | 277 // We can Close in any state except that trying to close a stream that is |
292 // playing Windows generates an error. We cannot propagate it to the source, | 278 // playing Windows generates an error. We cannot propagate it to the source, |
293 // as callback_ is set to NULL. Just print it and hope somebody somehow | 279 // as callback_ is set to NULL. Just print it and hope somebody somehow |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 buffer, | 387 buffer, |
402 sizeof(WAVEHDR)); | 388 sizeof(WAVEHDR)); |
403 if (result != MMSYSERR_NOERROR) | 389 if (result != MMSYSERR_NOERROR) |
404 stream->HandleError(result); | 390 stream->HandleError(result); |
405 stream->pending_bytes_ += buffer->dwBufferLength; | 391 stream->pending_bytes_ += buffer->dwBufferLength; |
406 } | 392 } |
407 } | 393 } |
408 } | 394 } |
409 | 395 |
410 } // namespace media | 396 } // namespace media |
OLD | NEW |