| 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); | |
| 164 | 161 |
| 165 // Called when the device will be opened in exclusive mode and use the | 162 // Called when the device will be opened in exclusive mode and use the |
| 166 // application specified format. | 163 // application specified format. |
| 167 // TODO(henrika): rewrite and move to CoreAudioUtil when removing flag | 164 // TODO(henrika): rewrite and move to CoreAudioUtil when removing flag |
| 168 // for exclusive audio mode. | 165 // for exclusive audio mode. |
| 169 HRESULT ExclusiveModeInitialization(IAudioClient* client, | 166 HRESULT ExclusiveModeInitialization(IAudioClient* client, |
| 170 HANDLE event_handle, | 167 HANDLE event_handle, |
| 171 uint32* endpoint_buffer_size); | 168 uint32* endpoint_buffer_size); |
| 172 | 169 |
| 173 // If |render_thread_| is valid, sets |stop_render_event_| and blocks until | 170 // If |render_thread_| is valid, sets |stop_render_event_| and blocks until |
| 174 // the thread has stopped. |stop_render_event_| is reset after the call. | 171 // the thread has stopped. |stop_render_event_| is reset after the call. |
| 175 // |source_| is set to NULL. | 172 // |source_| is set to NULL. |
| 176 void StopThread(); | 173 void StopThread(); |
| 177 | 174 |
| 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 void UnmarshalComPointers( | |
| 183 base::win::ScopedComPtr<IAudioClient>* audio_client, | |
| 184 base::win::ScopedComPtr<IAudioRenderClient>* audio_render_client, | |
| 185 base::win::ScopedComPtr<IAudioClock>* audio_clock); | |
| 186 | |
| 187 // Contains the thread ID of the creating thread. | 175 // Contains the thread ID of the creating thread. |
| 188 base::PlatformThreadId creating_thread_id_; | 176 base::PlatformThreadId creating_thread_id_; |
| 189 | 177 |
| 190 // Our creator, the audio manager needs to be notified when we close. | 178 // Our creator, the audio manager needs to be notified when we close. |
| 191 AudioManagerWin* manager_; | 179 AudioManagerWin* manager_; |
| 192 | 180 |
| 193 // Rendering is driven by this thread (which has no message loop). | 181 // Rendering is driven by this thread (which has no message loop). |
| 194 // All OnMoreData() callbacks will be called from this thread. | 182 // All OnMoreData() callbacks will be called from this thread. |
| 195 scoped_ptr<base::DelegateSimpleThread> render_thread_; | 183 scoped_ptr<base::DelegateSimpleThread> render_thread_; |
| 196 | 184 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 226 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE | 214 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE |
| 227 // where AUDCLNT_SHAREMODE_SHARED is the default. | 215 // where AUDCLNT_SHAREMODE_SHARED is the default. |
| 228 AUDCLNT_SHAREMODE share_mode_; | 216 AUDCLNT_SHAREMODE share_mode_; |
| 229 | 217 |
| 230 // Counts the number of audio frames written to the endpoint buffer. | 218 // Counts the number of audio frames written to the endpoint buffer. |
| 231 UINT64 num_written_frames_; | 219 UINT64 num_written_frames_; |
| 232 | 220 |
| 233 // Pointer to the client that will deliver audio samples to be played out. | 221 // Pointer to the client that will deliver audio samples to be played out. |
| 234 AudioSourceCallback* source_; | 222 AudioSourceCallback* source_; |
| 235 | 223 |
| 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 | |
| 248 // An IAudioClient interface which enables a client to create and initialize | 224 // An IAudioClient interface which enables a client to create and initialize |
| 249 // an audio stream between an audio application and the audio engine. | 225 // an audio stream between an audio application and the audio engine. |
| 250 base::win::ScopedComPtr<IAudioClient> audio_client_; | 226 base::win::ScopedComPtr<IAudioClient> audio_client_; |
| 251 | 227 |
| 252 // The IAudioRenderClient interface enables a client to write output | 228 // The IAudioRenderClient interface enables a client to write output |
| 253 // data to a rendering endpoint buffer. | 229 // data to a rendering endpoint buffer. |
| 254 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_; | 230 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_; |
| 255 | 231 |
| 256 base::win::ScopedComPtr<IAudioClock> audio_clock_; | |
| 257 // ------------------------------------------------------ | |
| 258 | |
| 259 // The audio engine will signal this event each time a buffer becomes | 232 // The audio engine will signal this event each time a buffer becomes |
| 260 // ready to be filled by the client. | 233 // ready to be filled by the client. |
| 261 base::win::ScopedHandle audio_samples_render_event_; | 234 base::win::ScopedHandle audio_samples_render_event_; |
| 262 | 235 |
| 263 // This event will be signaled when rendering shall stop. | 236 // This event will be signaled when rendering shall stop. |
| 264 base::win::ScopedHandle stop_render_event_; | 237 base::win::ScopedHandle stop_render_event_; |
| 265 | 238 |
| 266 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 239 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
| 267 scoped_ptr<AudioBus> audio_bus_; | 240 scoped_ptr<AudioBus> audio_bus_; |
| 268 | 241 |
| 242 base::win::ScopedComPtr<IAudioClock> audio_clock_; |
| 243 |
| 269 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); | 244 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); |
| 270 }; | 245 }; |
| 271 | 246 |
| 272 } // namespace media | 247 } // namespace media |
| 273 | 248 |
| 274 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ | 249 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ |
| OLD | NEW |