Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | |
| 12 #include "base/debug/trace_event.h" | 13 #include "base/debug/trace_event.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
| 15 #include "media/audio/audio_util.h" | 16 #include "media/audio/audio_util.h" |
| 16 #include "media/audio/win/audio_manager_win.h" | 17 #include "media/audio/win/audio_manager_win.h" |
| 17 | 18 |
| 18 // Number of times InitializeCriticalSectionAndSpinCount() spins | |
| 19 // before going to sleep. | |
| 20 const DWORD kSpinCount = 2000; | |
| 21 | |
| 22 // Some general thoughts about the waveOut API which is badly documented : | 19 // Some general thoughts about the waveOut API which is badly documented : |
| 23 // - We use CALLBACK_FUNCTION mode in which XP secretly creates two threads | 20 // - We use CALLBACK_FUNCTION mode in which XP secretly creates two threads |
| 24 // named _MixerCallbackThread and _waveThread which have real-time priority. | 21 // named _MixerCallbackThread and _waveThread which have real-time priority. |
| 25 // The callbacks occur in _waveThread. | 22 // The callbacks occur in _waveThread. |
| 26 // - Windows does not provide a way to query if the device is playing or paused | 23 // - Windows does not provide a way to query if the device is playing or paused |
| 27 // thus it forces you to maintain state, which naturally is not exactly | 24 // thus it forces you to maintain state, which naturally is not exactly |
| 28 // synchronized to the actual device state. | 25 // synchronized to the actual device state. |
| 29 // - Some functions, like waveOutReset cannot be called in the callback thread | 26 // - Some functions, like waveOutReset() cannot be called in the callback thread |
| 30 // or called in any random state because they deadlock. This results in a | 27 // or called in any random state because they deadlock. This results in a |
| 31 // non- instantaneous Stop() method. waveOutPrepareHeader seems to be in the | 28 // non-instantaneous Stop() method. waveOutWrite() and waveOutPrepareHeader |
| 32 // same boat. | 29 // seem to be in the same boat. |
| 33 // - waveOutReset() will forcefully kill the _waveThread so it is important | 30 // - We have to use separate "feeder" thread that calls waveOutWrite() to feed |
| 34 // to make sure we are not executing inside the audio source's OnMoreData() | 31 // buffers to driver, cannot do it from the callback. |
| 35 // or that we take locks inside WaveCallback() or QueueNextPacket(). | |
| 36 | |
| 37 // Sixty four MB is the maximum buffer size per AudioOutputStream. | |
| 38 static const uint32 kMaxOpenBufferSize = 1024 * 1024 * 64; | |
| 39 | |
| 40 // Our sound buffers are allocated once and kept in a linked list using the | |
| 41 // the WAVEHDR::dwUser variable. The last buffer points to the first buffer. | |
| 42 static WAVEHDR* GetNextBuffer(WAVEHDR* current) { | |
| 43 return reinterpret_cast<WAVEHDR*>(current->dwUser); | |
| 44 } | |
| 45 | 32 |
| 46 // See Also | 33 // See Also |
| 47 // http://www.thx.com/consumer/home-entertainment/home-theater/surround-sound-sp eaker-set-up/ | 34 // http://www.thx.com/consumer/home-entertainment/home-theater/surround-sound-sp eaker-set-up/ |
| 48 // http://en.wikipedia.org/wiki/Surround_sound | 35 // http://en.wikipedia.org/wiki/Surround_sound |
| 49 | 36 |
| 50 static const int kMaxChannelsToMask = 8; | 37 static const int kMaxChannelsToMask = 8; |
| 51 static const unsigned int kChannelsToMask[kMaxChannelsToMask + 1] = { | 38 static const unsigned int kChannelsToMask[kMaxChannelsToMask + 1] = { |
| 52 0, | 39 0, |
| 53 // 1 = Mono | 40 // 1 = Mono |
| 54 SPEAKER_FRONT_CENTER, | 41 SPEAKER_FRONT_CENTER, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 72 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | | 59 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | |
| 73 SPEAKER_BACK_CENTER, | 60 SPEAKER_BACK_CENTER, |
| 74 // 8 = 7.1 | 61 // 8 = 7.1 |
| 75 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | | 62 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | |
| 76 SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | | 63 SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | |
| 77 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | | 64 SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | |
| 78 SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT | 65 SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT |
| 79 // TODO(fbarchard): Add additional masks for 7.2 and beyond. | 66 // TODO(fbarchard): Add additional masks for 7.2 and beyond. |
| 80 }; | 67 }; |
| 81 | 68 |
| 69 inline size_t PCMWaveOutAudioOutputStream::BufferSize() const { | |
| 70 // Round size of buffer up to the nearest 16 bytes. | |
| 71 return (sizeof(WAVEHDR) + buffer_size_ + 15u) & static_cast<size_t>(~15); | |
|
tommi (sloooow) - chröme
2011/11/19 17:23:27
nit: instead of 15 I find hex better to read for b
| |
| 72 } | |
| 73 | |
| 74 inline WAVEHDR* PCMWaveOutAudioOutputStream::GetBuffer(int n) const { | |
| 75 DCHECK_GE(n, 0); | |
| 76 DCHECK_LT(n, num_buffers_); | |
| 77 return reinterpret_cast<WAVEHDR*>(&buffers_[n * BufferSize()]); | |
| 78 } | |
| 79 | |
| 80 | |
| 82 PCMWaveOutAudioOutputStream::PCMWaveOutAudioOutputStream( | 81 PCMWaveOutAudioOutputStream::PCMWaveOutAudioOutputStream( |
| 83 AudioManagerWin* manager, const AudioParameters& params, int num_buffers, | 82 AudioManagerWin* manager, const AudioParameters& params, int num_buffers, |
| 84 UINT device_id) | 83 UINT device_id) |
| 85 : state_(PCMA_BRAND_NEW), | 84 : state_(PCMA_BRAND_NEW), |
| 86 manager_(manager), | 85 manager_(manager), |
| 87 device_id_(device_id), | 86 device_id_(device_id), |
| 88 waveout_(NULL), | 87 waveout_(NULL), |
| 89 callback_(NULL), | 88 callback_(NULL), |
| 90 num_buffers_(num_buffers), | 89 num_buffers_(num_buffers), |
| 91 buffer_(NULL), | |
| 92 buffer_size_(params.GetPacketSize()), | 90 buffer_size_(params.GetPacketSize()), |
| 93 volume_(1), | 91 volume_(1), |
| 94 channels_(params.channels), | 92 channels_(params.channels), |
| 95 pending_bytes_(0) { | 93 pending_bytes_(0) { |
| 96 ::InitializeCriticalSectionAndSpinCount(&lock_, kSpinCount); | |
| 97 | |
| 98 format_.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; | 94 format_.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; |
| 99 format_.Format.nChannels = params.channels; | 95 format_.Format.nChannels = params.channels; |
| 100 format_.Format.nSamplesPerSec = params.sample_rate; | 96 format_.Format.nSamplesPerSec = params.sample_rate; |
| 101 format_.Format.wBitsPerSample = params.bits_per_sample; | 97 format_.Format.wBitsPerSample = params.bits_per_sample; |
| 102 format_.Format.cbSize = sizeof(format_) - sizeof(WAVEFORMATEX); | 98 format_.Format.cbSize = sizeof(format_) - sizeof(WAVEFORMATEX); |
| 103 // The next are computed from above. | 99 // The next are computed from above. |
| 104 format_.Format.nBlockAlign = (format_.Format.nChannels * | 100 format_.Format.nBlockAlign = (format_.Format.nChannels * |
| 105 format_.Format.wBitsPerSample) / 8; | 101 format_.Format.wBitsPerSample) / 8; |
| 106 format_.Format.nAvgBytesPerSec = format_.Format.nBlockAlign * | 102 format_.Format.nAvgBytesPerSec = format_.Format.nBlockAlign * |
| 107 format_.Format.nSamplesPerSec; | 103 format_.Format.nSamplesPerSec; |
| 108 if (params.channels > kMaxChannelsToMask) { | 104 if (params.channels > kMaxChannelsToMask) { |
| 109 format_.dwChannelMask = kChannelsToMask[kMaxChannelsToMask]; | 105 format_.dwChannelMask = kChannelsToMask[kMaxChannelsToMask]; |
| 110 } else { | 106 } else { |
| 111 format_.dwChannelMask = kChannelsToMask[params.channels]; | 107 format_.dwChannelMask = kChannelsToMask[params.channels]; |
| 112 } | 108 } |
| 113 format_.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; | 109 format_.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; |
| 114 format_.Samples.wValidBitsPerSample = params.bits_per_sample; | 110 format_.Samples.wValidBitsPerSample = params.bits_per_sample; |
| 115 } | 111 } |
| 116 | 112 |
| 117 PCMWaveOutAudioOutputStream::~PCMWaveOutAudioOutputStream() { | 113 PCMWaveOutAudioOutputStream::~PCMWaveOutAudioOutputStream() { |
| 118 DCHECK(NULL == waveout_); | 114 DCHECK(NULL == waveout_); |
| 119 ::DeleteCriticalSection(&lock_); | |
| 120 } | 115 } |
| 121 | 116 |
| 122 bool PCMWaveOutAudioOutputStream::Open() { | 117 bool PCMWaveOutAudioOutputStream::Open() { |
| 123 if (state_ != PCMA_BRAND_NEW) | 118 if (state_ != PCMA_BRAND_NEW) |
| 124 return false; | 119 return false; |
| 125 if (num_buffers_ < 2 || num_buffers_ > 5) | 120 if (num_buffers_ < 2 || num_buffers_ > 5) |
| 126 return false; | 121 return false; |
| 127 // Open the device. We'll be getting callback in WaveCallback function. | 122 // Open the device. We'll be getting callback in WaveCallback function. |
| 128 // They occur in a magic, time-critical thread that windows creates. | 123 // They occur in a magic, time-critical thread that windows creates. |
| 129 MMRESULT result = ::waveOutOpen(&waveout_, device_id_, | 124 MMRESULT result = ::waveOutOpen(&waveout_, device_id_, |
| 130 reinterpret_cast<LPCWAVEFORMATEX>(&format_), | 125 reinterpret_cast<LPCWAVEFORMATEX>(&format_), |
| 131 reinterpret_cast<DWORD_PTR>(WaveCallback), | 126 reinterpret_cast<DWORD_PTR>(WaveCallback), |
| 132 reinterpret_cast<DWORD_PTR>(this), | 127 reinterpret_cast<DWORD_PTR>(this), |
| 133 CALLBACK_FUNCTION); | 128 CALLBACK_FUNCTION); |
| 134 if (result != MMSYSERR_NOERROR) | 129 if (result != MMSYSERR_NOERROR) |
| 135 return false; | 130 return false; |
| 136 | 131 |
| 137 SetupBuffers(); | 132 SetupBuffers(); |
| 138 state_ = PCMA_READY; | 133 state_ = PCMA_READY; |
| 139 return true; | 134 return true; |
| 140 } | 135 } |
| 141 | 136 |
| 142 void PCMWaveOutAudioOutputStream::SetupBuffers() { | 137 void PCMWaveOutAudioOutputStream::SetupBuffers() { |
| 143 WAVEHDR* last = NULL; | 138 buffers_.reset(new char[BufferSize() * num_buffers_]); |
| 144 WAVEHDR* first = NULL; | |
| 145 for (int ix = 0; ix != num_buffers_; ++ix) { | 139 for (int ix = 0; ix != num_buffers_; ++ix) { |
| 146 uint32 sz = sizeof(WAVEHDR) + buffer_size_; | 140 WAVEHDR* buffer = GetBuffer(ix); |
| 147 buffer_ = reinterpret_cast<WAVEHDR*>(new char[sz]); | 141 buffer->lpData = reinterpret_cast<char*>(buffer) + sizeof(WAVEHDR); |
| 148 buffer_->lpData = reinterpret_cast<char*>(buffer_) + sizeof(WAVEHDR); | 142 buffer->dwBufferLength = buffer_size_; |
| 149 buffer_->dwBufferLength = buffer_size_; | 143 buffer->dwBytesRecorded = 0; |
| 150 buffer_->dwBytesRecorded = 0; | 144 buffer->dwFlags = WHDR_DONE; |
| 151 buffer_->dwUser = reinterpret_cast<DWORD_PTR>(last); | 145 buffer->dwLoops = 0; |
| 152 buffer_->dwFlags = WHDR_DONE; | |
| 153 buffer_->dwLoops = 0; | |
| 154 if (ix == 0) | |
| 155 first = buffer_; | |
| 156 last = buffer_; | |
| 157 // Tell windows sound drivers about our buffers. Not documented what | 146 // Tell windows sound drivers about our buffers. Not documented what |
| 158 // this does but we can guess that causes the OS to keep a reference to | 147 // this does but we can guess that causes the OS to keep a reference to |
| 159 // the memory pages so the driver can use them without worries. | 148 // the memory pages so the driver can use them without worries. |
| 160 ::waveOutPrepareHeader(waveout_, buffer_, sizeof(WAVEHDR)); | 149 ::waveOutPrepareHeader(waveout_, buffer, sizeof(WAVEHDR)); |
| 161 } | 150 } |
| 162 // Fix the first buffer to point to the last one. | |
| 163 first->dwUser = reinterpret_cast<DWORD_PTR>(last); | |
| 164 } | 151 } |
| 165 | 152 |
| 166 void PCMWaveOutAudioOutputStream::FreeBuffers() { | 153 void PCMWaveOutAudioOutputStream::FreeBuffers() { |
| 167 WAVEHDR* current = buffer_; | |
| 168 for (int ix = 0; ix != num_buffers_; ++ix) { | 154 for (int ix = 0; ix != num_buffers_; ++ix) { |
| 169 WAVEHDR* next = GetNextBuffer(current); | 155 ::waveOutUnprepareHeader(waveout_, GetBuffer(ix), sizeof(WAVEHDR)); |
| 170 ::waveOutUnprepareHeader(waveout_, current, sizeof(WAVEHDR)); | |
| 171 delete[] reinterpret_cast<char*>(current); | |
| 172 current = next; | |
| 173 } | 156 } |
| 174 buffer_ = NULL; | 157 buffers_.reset(NULL); |
| 175 } | 158 } |
| 176 | 159 |
| 177 // Initially we ask the source to fill up both audio buffers. If we don't do | 160 // Initially we ask the source to fill up all audio buffers. If we don't do |
| 178 // this then we would always get the driver callback when it is about to run | 161 // this then we would always get the driver callback when it is about to run |
| 179 // samples and that would leave too little time to react. | 162 // samples and that would leave too little time to react. |
| 180 void PCMWaveOutAudioOutputStream::Start(AudioSourceCallback* callback) { | 163 void PCMWaveOutAudioOutputStream::Start(AudioSourceCallback* callback) { |
| 181 if (state_ != PCMA_READY) | 164 if (state_ != PCMA_READY) |
| 182 return; | 165 return; |
| 183 callback_ = callback; | 166 callback_ = callback; |
| 184 state_ = PCMA_PLAYING; | 167 state_ = PCMA_PLAYING; |
| 168 | |
| 169 playing_object_ = new PlayingObject(this); | |
| 170 | |
| 171 // Queue the buffers. | |
| 172 // TODO(enal): If there are more than 2, queue only first 2 and schedule | |
| 173 // remaining to be filled in the "feeder" thread. Non-trivial because we have | |
| 174 // to be sure that data is ready. Can be done by storing time of last | |
| 175 // OnMoreData() call and scheduling delayed task in FeedBuffer() if data is | |
| 176 // not ready yet. | |
| 185 pending_bytes_ = 0; | 177 pending_bytes_ = 0; |
| 186 WAVEHDR* buffer = buffer_; | |
| 187 for (int ix = 0; ix != num_buffers_; ++ix) { | 178 for (int ix = 0; ix != num_buffers_; ++ix) { |
| 179 WAVEHDR* buffer = GetBuffer(ix); | |
| 188 // Caller waits for 1st packet to become available, but not for others, | 180 // Caller waits for 1st packet to become available, but not for others, |
| 189 // so we wait for them here. | 181 // so we wait for them here. |
| 190 if (ix != 0) | 182 if (ix != 0) |
| 191 callback_->WaitTillDataReady(); | 183 callback_->WaitTillDataReady(); |
| 192 QueueNextPacket(buffer); // Read more data. | 184 QueueNextPacket(buffer); // Read more data. |
| 193 pending_bytes_ += buffer->dwBufferLength; | 185 pending_bytes_ += buffer->dwBufferLength; |
| 194 buffer = GetNextBuffer(buffer); | |
| 195 } | 186 } |
| 196 buffer = buffer_; | |
| 197 | 187 |
| 198 // From now on |pending_bytes_| would be accessed by callback thread. | 188 // From now on |pending_bytes_| would be accessed by callback thread. |
| 199 // Most likely waveOutPause() or waveOutRestart() has its own memory barrier, | 189 // Most likely waveOutPause() or waveOutRestart() has its own memory barrier, |
| 200 // but issuing our own is safer. | 190 // but issuing our own is safer. |
| 201 MemoryBarrier(); | 191 MemoryBarrier(); |
| 202 | 192 |
| 203 MMRESULT result = ::waveOutPause(waveout_); | 193 MMRESULT result = ::waveOutPause(waveout_); |
| 204 if (result != MMSYSERR_NOERROR) { | 194 if (result != MMSYSERR_NOERROR) { |
| 205 HandleError(result); | 195 HandleError(result); |
| 206 return; | 196 return; |
| 207 } | 197 } |
| 208 | 198 |
| 209 // Send the buffers to the audio driver. Note that the device is paused | 199 // Send the buffers to the audio driver. Note that the device is paused |
| 210 // so we avoid entering the callback method while still here. | 200 // so we avoid entering the callback method while still here. |
| 211 for (int ix = 0; ix != num_buffers_; ++ix) { | 201 for (int ix = 0; ix != num_buffers_; ++ix) { |
| 212 result = ::waveOutWrite(waveout_, buffer, sizeof(WAVEHDR)); | 202 result = ::waveOutWrite(waveout_, GetBuffer(ix), sizeof(WAVEHDR)); |
| 213 if (result != MMSYSERR_NOERROR) { | 203 if (result != MMSYSERR_NOERROR) { |
| 214 HandleError(result); | 204 HandleError(result); |
| 215 break; | 205 break; |
| 216 } | 206 } |
| 217 buffer = GetNextBuffer(buffer); | 207 // Each successfully sent buffer references |playing_object_|. |
| 208 playing_object_->AddRef(); | |
| 218 } | 209 } |
| 219 result = ::waveOutRestart(waveout_); | 210 result = ::waveOutRestart(waveout_); |
| 220 if (result != MMSYSERR_NOERROR) { | 211 if (result != MMSYSERR_NOERROR) { |
| 221 HandleError(result); | 212 HandleError(result); |
| 222 return; | 213 return; |
| 223 } | 214 } |
| 224 } | 215 } |
| 225 | 216 |
| 226 // Stopping is tricky. First, no buffer should be locked by the audio driver | 217 // Stopping is tricky. We want to avoid stopping while feeder thread feeds |
| 227 // or else the waveOutReset() will deadlock and secondly, the callback should | 218 // buffer to driver, so use the lock on the |playing_object_|. |
| 228 // not be inside the AudioSource's OnMoreData because waveOutReset() forcefully | |
| 229 // kills the callback thread after releasing all buffers. | |
| 230 void PCMWaveOutAudioOutputStream::Stop() { | 219 void PCMWaveOutAudioOutputStream::Stop() { |
| 231 if (state_ != PCMA_PLAYING) | 220 if (state_ != PCMA_PLAYING) |
| 232 return; | 221 return; |
| 233 | 222 MMRESULT res = MMSYSERR_NOERROR; |
| 234 // Enter into critical section and call ::waveOutReset(). The fact that we | 223 { |
| 235 // entered critical section means that callback is out of critical section and | 224 base::AutoLock auto_lock(*playing_object_->lock()); |
| 236 // it is safe to reset. | 225 playing_object_->stop_playing(); |
| 237 ::EnterCriticalSection(&lock_); | 226 MemoryBarrier(); |
| 238 MMRESULT res = ::waveOutReset(waveout_); | 227 res = ::waveOutReset(waveout_); |
| 239 ::LeaveCriticalSection(&lock_); | 228 } |
| 240 if (res != MMSYSERR_NOERROR) { | 229 if (res != MMSYSERR_NOERROR) { |
| 241 HandleError(res); | 230 HandleError(res); |
| 242 return; | 231 return; |
| 243 } | 232 } |
| 244 | 233 |
| 234 playing_object_ = NULL; | |
|
tommi (sloooow) - chröme
2011/11/19 17:23:27
While this correctly releases the object, it also
| |
| 235 | |
| 245 // Don't use callback after Stop(). | 236 // Don't use callback after Stop(). |
| 246 callback_ = NULL; | 237 callback_ = NULL; |
| 247 | 238 |
| 248 state_ = PCMA_READY; | 239 state_ = PCMA_READY; |
| 249 } | 240 } |
| 250 | 241 |
| 251 // We can Close in any state except that trying to close a stream that is | 242 // We can Close in any state except that trying to close a stream that is |
| 252 // playing Windows generates an error, which we propagate to the source. | 243 // playing Windows generates an error, which we propagate to the source. |
| 253 void PCMWaveOutAudioOutputStream::Close() { | 244 void PCMWaveOutAudioOutputStream::Close() { |
| 254 if (waveout_) { | 245 if (waveout_) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 volume_); | 301 volume_); |
| 311 } | 302 } |
| 312 } else { | 303 } else { |
| 313 HandleError(0); | 304 HandleError(0); |
| 314 return; | 305 return; |
| 315 } | 306 } |
| 316 buffer->dwFlags = WHDR_PREPARED; | 307 buffer->dwFlags = WHDR_PREPARED; |
| 317 } | 308 } |
| 318 | 309 |
| 319 // Windows call us back in this function when some events happen. Most notably | 310 // Windows call us back in this function when some events happen. Most notably |
| 320 // when it is done playing a buffer. Since we use double buffering it is | 311 // when it is done playing a buffer. Cannot feed freed buffer back to the system |
| 321 // convenient to think of |buffer| as free and GetNextBuffer(buffer) as in | 312 // from here, have to schedule it for the separate thread. |
|
cpu_(ooo_6.6-7.5)
2011/11/21 01:18:43
I don't agree with this approach. We have added an
tommi (sloooow) - chröme
2011/11/22 08:21:45
Hey Carlos - If we use the event model, won't the
| |
| 322 // use by the driver. | |
| 323 void PCMWaveOutAudioOutputStream::WaveCallback(HWAVEOUT hwo, UINT msg, | 313 void PCMWaveOutAudioOutputStream::WaveCallback(HWAVEOUT hwo, UINT msg, |
| 324 DWORD_PTR instance, | 314 DWORD_PTR instance, |
| 325 DWORD_PTR param1, DWORD_PTR) { | 315 DWORD_PTR param1, DWORD_PTR) { |
| 326 TRACE_EVENT0("audio", "PCMWaveOutAudioOutputStream::WaveCallback"); | 316 TRACE_EVENT0("audio", "PCMWaveOutAudioOutputStream::WaveCallback"); |
| 327 | 317 |
| 328 if (msg == WOM_DONE) { | 318 if (msg == WOM_DONE) { |
| 329 // WOM_DONE indicates that the driver is done with our buffer, we can | 319 // WOM_DONE indicates that the driver is done with our buffer, |
| 330 // either ask the source for more data or check if we need to stop playing. | 320 // if still playing ask "feedef" thread to buffer more data. |
| 331 WAVEHDR* buffer = reinterpret_cast<WAVEHDR*>(param1); | 321 WAVEHDR* buffer = reinterpret_cast<WAVEHDR*>(param1); |
| 332 buffer->dwFlags = WHDR_DONE; | 322 buffer->dwFlags = WHDR_DONE; |
| 333 | 323 |
| 334 PCMWaveOutAudioOutputStream* stream = | 324 PCMWaveOutAudioOutputStream* stream = |
| 335 reinterpret_cast<PCMWaveOutAudioOutputStream*>(instance); | 325 reinterpret_cast<PCMWaveOutAudioOutputStream*>(instance); |
| 336 | 326 |
| 337 // Do real work only if main thread has not yet called waveOutReset(). | 327 if (stream->playing_object_->playing()) { |
| 338 if (::TryEnterCriticalSection(&stream->lock_)) { | 328 stream->manager_->GetMessageLoop()->PostTask( |
| 339 // Before we queue the next packet, we need to adjust the number of | 329 FROM_HERE, |
| 340 // pending bytes since the last write to hardware. | 330 base::Bind( |
| 341 stream->pending_bytes_ -= buffer->dwBufferLength; | 331 &PCMWaveOutAudioOutputStream::PlayingObject::FeedBuffer, |
| 342 | 332 stream->playing_object_.get(), |
| 343 stream->QueueNextPacket(buffer); | 333 buffer, |
| 344 | 334 hwo)); |
| 345 // Time to send the buffer to the audio driver. Since we are reusing | |
| 346 // the same buffers we can get away without calling waveOutPrepareHeader. | |
| 347 MMRESULT result = ::waveOutWrite(hwo, buffer, sizeof(WAVEHDR)); | |
| 348 if (result != MMSYSERR_NOERROR) | |
| 349 stream->HandleError(result); | |
| 350 | |
| 351 stream->pending_bytes_ += buffer->dwBufferLength; | |
| 352 ::LeaveCriticalSection(&stream->lock_); | |
| 353 } | 335 } |
| 336 stream->playing_object_->Release(); | |
| 354 } | 337 } |
| 355 } | 338 } |
| 339 | |
| 340 PCMWaveOutAudioOutputStream::PlayingObject::PlayingObject( | |
| 341 PCMWaveOutAudioOutputStream *stream) : | |
| 342 stream_(stream) { | |
| 343 DCHECK(stream_ != NULL); | |
| 344 } | |
| 345 | |
| 346 PCMWaveOutAudioOutputStream::PlayingObject::~PlayingObject() { | |
| 347 DCHECK(stream_ == NULL); | |
| 348 } | |
| 349 | |
| 350 void PCMWaveOutAudioOutputStream::PlayingObject::FeedBuffer(WAVEHDR* buffer, | |
| 351 HWAVEOUT hwo) { | |
| 352 TRACE_EVENT0("audio", | |
| 353 "PCMWaveOutAudioOutputStream::PlayingObject::FeedBuffer"); | |
| 354 // Try to obtain the lock. Minor optimization: if lock is owned, return | |
| 355 // immediately, not wait till PCMWaveOutAudioOutputStream::Stop() release | |
| 356 // it and we will return anyways. | |
| 357 if (!lock_.Try()) | |
|
cpu_(ooo_6.6-7.5)
2011/11/21 01:18:43
I don't completely follow why we need to take this
| |
| 358 return; | |
| 359 if (!playing()) | |
| 360 return; | |
| 361 // Before we queue the next packet, we need to adjust the number of | |
| 362 // pending bytes since the last write to hardware. | |
| 363 stream_->pending_bytes_ -= buffer->dwBufferLength; | |
| 364 stream_->QueueNextPacket(buffer); | |
| 365 // Time to send the buffer to the audio driver. Since we are reusing | |
| 366 // the same buffers we can get away without calling waveOutPrepareHeader. | |
| 367 MMRESULT result = ::waveOutWrite(hwo, buffer, sizeof(WAVEHDR)); | |
| 368 if (result != MMSYSERR_NOERROR) | |
| 369 stream_->HandleError(result); | |
| 370 AddRef(); | |
|
tommi (sloooow) - chröme
2011/11/19 17:23:27
maybe a comment on where this reference is release
tommi (sloooow) - chröme
2011/11/19 21:58:42
Actually, I think this AddRef should not be bere.
| |
| 371 stream_->pending_bytes_ += buffer->dwBufferLength; | |
| 372 lock_.Release(); | |
| 373 } | |
| OLD | NEW |