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 // Implementation of AudioOutputStream for Windows using Windows Core Audio | 5 // Implementation of AudioOutputStream for Windows using Windows Core Audio |
6 // WASAPI for low latency rendering. | 6 // WASAPI for low latency rendering. |
7 // | 7 // |
8 // Overview of operation and performance: | 8 // Overview of operation and performance: |
9 // | 9 // |
10 // - An object of WASAPIAudioOutputStream is created by the AudioManager | 10 // - An object of WASAPIAudioOutputStream is created by the AudioManager |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 bool started() const { return render_thread_.get() != NULL; } | 150 bool started() const { return render_thread_.get() != NULL; } |
151 | 151 |
152 private: | 152 private: |
153 // DelegateSimpleThread::Delegate implementation. | 153 // DelegateSimpleThread::Delegate implementation. |
154 virtual void Run() override; | 154 virtual void Run() override; |
155 | 155 |
156 // Core part of the thread loop which controls the actual rendering. | 156 // Core part of the thread loop which controls the actual rendering. |
157 // Checks available amount of space in the endpoint buffer and reads | 157 // Checks available amount of space in the endpoint buffer and reads |
158 // data from the client to fill up the buffer without causing audio | 158 // data from the client to fill up the buffer without causing audio |
159 // glitches. | 159 // glitches. |
160 bool RenderAudioFromSource(UINT64 device_frequency); | 160 bool RenderAudioFromSource(UINT64 device_frequency, |
| 161 IAudioClient* thread_audio_client, |
| 162 IAudioRenderClient* thread_audio_render_client, |
| 163 IAudioClock* thread_audio_clock); |
161 | 164 |
162 // Called when the device will be opened in exclusive mode and use the | 165 // Called when the device will be opened in exclusive mode and use the |
163 // application specified format. | 166 // application specified format. |
164 // TODO(henrika): rewrite and move to CoreAudioUtil when removing flag | 167 // TODO(henrika): rewrite and move to CoreAudioUtil when removing flag |
165 // for exclusive audio mode. | 168 // for exclusive audio mode. |
166 HRESULT ExclusiveModeInitialization(IAudioClient* client, | 169 HRESULT ExclusiveModeInitialization(IAudioClient* client, |
167 HANDLE event_handle, | 170 HANDLE event_handle, |
168 uint32* endpoint_buffer_size); | 171 uint32* endpoint_buffer_size); |
169 | 172 |
170 // If |render_thread_| is valid, sets |stop_render_event_| and blocks until | 173 // If |render_thread_| is valid, sets |stop_render_event_| and blocks until |
171 // the thread has stopped. |stop_render_event_| is reset after the call. | 174 // the thread has stopped. |stop_render_event_| is reset after the call. |
172 // |source_| is set to NULL. | 175 // |source_| is set to NULL. |
173 void StopThread(); | 176 void StopThread(); |
174 | 177 |
| 178 // Handles sharing of COM pointers between the audio and render threads. The |
| 179 // marshal method must be called on the audio manager thread, while unmarshal |
| 180 // must be called on |render_thread_|. |
| 181 bool MarshalComPointers(); |
| 182 bool UnmarshalComPointers( |
| 183 base::win::ScopedComPtr<IAudioClient>* audio_client, |
| 184 base::win::ScopedComPtr<IAudioRenderClient>* audio_render_client, |
| 185 base::win::ScopedComPtr<IAudioClock>* audio_clock); |
| 186 |
175 // Contains the thread ID of the creating thread. | 187 // Contains the thread ID of the creating thread. |
176 base::PlatformThreadId creating_thread_id_; | 188 base::PlatformThreadId creating_thread_id_; |
177 | 189 |
178 // Our creator, the audio manager needs to be notified when we close. | 190 // Our creator, the audio manager needs to be notified when we close. |
179 AudioManagerWin* manager_; | 191 AudioManagerWin* manager_; |
180 | 192 |
181 // Rendering is driven by this thread (which has no message loop). | 193 // Rendering is driven by this thread (which has no message loop). |
182 // All OnMoreData() callbacks will be called from this thread. | 194 // All OnMoreData() callbacks will be called from this thread. |
183 scoped_ptr<base::DelegateSimpleThread> render_thread_; | 195 scoped_ptr<base::DelegateSimpleThread> render_thread_; |
184 | 196 |
(...skipping 29 matching lines...) Expand all Loading... |
214 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE | 226 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE |
215 // where AUDCLNT_SHAREMODE_SHARED is the default. | 227 // where AUDCLNT_SHAREMODE_SHARED is the default. |
216 AUDCLNT_SHAREMODE share_mode_; | 228 AUDCLNT_SHAREMODE share_mode_; |
217 | 229 |
218 // Counts the number of audio frames written to the endpoint buffer. | 230 // Counts the number of audio frames written to the endpoint buffer. |
219 UINT64 num_written_frames_; | 231 UINT64 num_written_frames_; |
220 | 232 |
221 // Pointer to the client that will deliver audio samples to be played out. | 233 // Pointer to the client that will deliver audio samples to be played out. |
222 AudioSourceCallback* source_; | 234 AudioSourceCallback* source_; |
223 | 235 |
| 236 // ------------------------------------------------------ |
| 237 // Warning: COM pointers must be marshaled from the audio thread to be used |
| 238 // on any other thread. Do not use any of these interfaces on a thread other |
| 239 // than the audio thread. See MarshalComPointers() and UnmarshalComPointers() |
| 240 // for marshaling pointers to the render thread. |
| 241 |
| 242 // Stream into which the COM pointers below are marshaled so that they can |
| 243 // be used on another thread. |
| 244 base::win::ScopedComPtr<IStream> com_stream_; |
| 245 |
| 246 // Windows Audio Session API (WASAPI) interfaces. |
| 247 |
224 // An IAudioClient interface which enables a client to create and initialize | 248 // An IAudioClient interface which enables a client to create and initialize |
225 // an audio stream between an audio application and the audio engine. | 249 // an audio stream between an audio application and the audio engine. |
226 base::win::ScopedComPtr<IAudioClient> audio_client_; | 250 base::win::ScopedComPtr<IAudioClient> audio_client_; |
227 | 251 |
228 // The IAudioRenderClient interface enables a client to write output | 252 // The IAudioRenderClient interface enables a client to write output |
229 // data to a rendering endpoint buffer. | 253 // data to a rendering endpoint buffer. |
230 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_; | 254 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_; |
231 | 255 |
| 256 base::win::ScopedComPtr<IAudioClock> audio_clock_; |
| 257 // ------------------------------------------------------ |
| 258 |
232 // The audio engine will signal this event each time a buffer becomes | 259 // The audio engine will signal this event each time a buffer becomes |
233 // ready to be filled by the client. | 260 // ready to be filled by the client. |
234 base::win::ScopedHandle audio_samples_render_event_; | 261 base::win::ScopedHandle audio_samples_render_event_; |
235 | 262 |
236 // This event will be signaled when rendering shall stop. | 263 // This event will be signaled when rendering shall stop. |
237 base::win::ScopedHandle stop_render_event_; | 264 base::win::ScopedHandle stop_render_event_; |
238 | 265 |
239 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 266 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
240 scoped_ptr<AudioBus> audio_bus_; | 267 scoped_ptr<AudioBus> audio_bus_; |
241 | 268 |
242 base::win::ScopedComPtr<IAudioClock> audio_clock_; | |
243 | |
244 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); | 269 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); |
245 }; | 270 }; |
246 | 271 |
247 } // namespace media | 272 } // namespace media |
248 | 273 |
249 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ | 274 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ |
OLD | NEW |