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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // by the AudioManager. | 105 // by the AudioManager. |
106 // - It is a requirement to call the following methods on the same audio | 106 // - It is a requirement to call the following methods on the same audio |
107 // thread: Open(), Start(), Stop(), and Close(). | 107 // thread: Open(), Start(), Stop(), and Close(). |
108 // - Audio rendering is performed on the audio render thread, owned by this | 108 // - Audio rendering is performed on the audio render thread, owned by this |
109 // class, and the AudioSourceCallback::OnMoreData() method will be called | 109 // class, and the AudioSourceCallback::OnMoreData() method will be called |
110 // from this thread. Stream switching also takes place on the audio-render | 110 // from this thread. Stream switching also takes place on the audio-render |
111 // thread. | 111 // thread. |
112 // - All callback methods from the IMMNotificationClient interface will be | 112 // - All callback methods from the IMMNotificationClient interface will be |
113 // called on a Windows-internal MMDevice thread. | 113 // called on a Windows-internal MMDevice thread. |
114 // | 114 // |
| 115 // Experimental exclusive mode: |
| 116 // |
| 117 // - It is possible to open up a stream in exclusive mode by using the |
| 118 // --enable-exclusive-mode command line flag. |
| 119 // - The internal buffering scheme is less flexible for exclusive streams. |
| 120 // Hence, some manual tuning will be required before deciding what frame |
| 121 // size to use. See the WinAudioOutputTest unit test for more details. |
| 122 // - If an application opens a stream in exclusive mode, the application has |
| 123 // exclusive use of the audio endpoint device that plays the stream. |
| 124 // - Exclusive-mode should only be utilized when the lowest possible latency |
| 125 // is important. |
| 126 // - In exclusive mode, the client can choose to open the stream in any audio |
| 127 // format that the endpoint device supports, i.e. not limited to the device's |
| 128 // current (default) configuration. |
| 129 // - Initial measurements on Windows 7 (HP Z600 workstation) have shown that |
| 130 // the lowest possible latencies we can achieve on this machine are: |
| 131 // o ~3.3333ms @ 48kHz <=> 160 audio frames per buffer. |
| 132 // o ~3.6281ms @ 44.1kHz <=> 160 audio frames per buffer. |
| 133 // - See http://msdn.microsoft.com/en-us/library/windows/desktop/dd370844(v=vs.8
5).aspx |
| 134 // for more details. |
| 135 |
115 #ifndef MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ | 136 #ifndef MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ |
116 #define MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ | 137 #define MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ |
117 | 138 |
118 #include <Audioclient.h> | 139 #include <Audioclient.h> |
119 #include <audiopolicy.h> | 140 #include <audiopolicy.h> |
120 #include <MMDeviceAPI.h> | 141 #include <MMDeviceAPI.h> |
121 | 142 |
122 #include <string> | 143 #include <string> |
123 | 144 |
124 #include "base/compiler_specific.h" | 145 #include "base/compiler_specific.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // Implementation of AudioOutputStream. | 177 // Implementation of AudioOutputStream. |
157 virtual bool Open() OVERRIDE; | 178 virtual bool Open() OVERRIDE; |
158 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 179 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
159 virtual void Stop() OVERRIDE; | 180 virtual void Stop() OVERRIDE; |
160 virtual void Close() OVERRIDE; | 181 virtual void Close() OVERRIDE; |
161 virtual void SetVolume(double volume) OVERRIDE; | 182 virtual void SetVolume(double volume) OVERRIDE; |
162 virtual void GetVolume(double* volume) OVERRIDE; | 183 virtual void GetVolume(double* volume) OVERRIDE; |
163 | 184 |
164 // Retrieves the stream format that the audio engine uses for its internal | 185 // Retrieves the stream format that the audio engine uses for its internal |
165 // processing/mixing of shared-mode streams. | 186 // processing/mixing of shared-mode streams. |
| 187 // This method should not be used in combination with exclusive-mode streams. |
166 static int HardwareSampleRate(ERole device_role); | 188 static int HardwareSampleRate(ERole device_role); |
167 | 189 |
| 190 // Returns AUDCLNT_SHAREMODE_EXCLUSIVE if --enable-exclusive-mode is used |
| 191 // as command-line flag and AUDCLNT_SHAREMODE_SHARED otherwise (default). |
| 192 static AUDCLNT_SHAREMODE GetShareMode(); |
| 193 |
168 bool started() const { return started_; } | 194 bool started() const { return started_; } |
169 | 195 |
170 private: | 196 private: |
171 // Implementation of IUnknown (trivial in this case). See | 197 // Implementation of IUnknown (trivial in this case). See |
172 // msdn.microsoft.com/en-us/library/windows/desktop/dd371403(v=vs.85).aspx | 198 // msdn.microsoft.com/en-us/library/windows/desktop/dd371403(v=vs.85).aspx |
173 // for details regarding why proper implementations of AddRef(), Release() | 199 // for details regarding why proper implementations of AddRef(), Release() |
174 // and QueryInterface() are not needed here. | 200 // and QueryInterface() are not needed here. |
175 STDMETHOD_(ULONG, AddRef)(); | 201 STDMETHOD_(ULONG, AddRef)(); |
176 STDMETHOD_(ULONG, Release)(); | 202 STDMETHOD_(ULONG, Release)(); |
177 STDMETHOD(QueryInterface)(REFIID iid, void** object); | 203 STDMETHOD(QueryInterface)(REFIID iid, void** object); |
(...skipping 21 matching lines...) Expand all Loading... |
199 return S_OK; | 225 return S_OK; |
200 } | 226 } |
201 | 227 |
202 // DelegateSimpleThread::Delegate implementation. | 228 // DelegateSimpleThread::Delegate implementation. |
203 virtual void Run() OVERRIDE; | 229 virtual void Run() OVERRIDE; |
204 | 230 |
205 // Issues the OnError() callback to the |sink_|. | 231 // Issues the OnError() callback to the |sink_|. |
206 void HandleError(HRESULT err); | 232 void HandleError(HRESULT err); |
207 | 233 |
208 // The Open() method is divided into these sub methods. | 234 // The Open() method is divided into these sub methods. |
209 HRESULT SetRenderDevice(ERole device_role); | 235 HRESULT SetRenderDevice(); |
210 HRESULT ActivateRenderDevice(); | 236 HRESULT ActivateRenderDevice(); |
211 HRESULT GetAudioEngineStreamFormat(); | |
212 bool DesiredFormatIsSupported(); | 237 bool DesiredFormatIsSupported(); |
213 HRESULT InitializeAudioEngine(); | 238 HRESULT InitializeAudioEngine(); |
214 | 239 |
| 240 // Called when the device will be opened in shared mode and use the |
| 241 // internal audio engine's mix format. |
| 242 HRESULT SharedModeInitialization(); |
| 243 |
| 244 // Called when the device will be opened in exclusive mode and use the |
| 245 // application specified format. |
| 246 HRESULT ExclusiveModeInitialization(); |
| 247 |
215 // Converts unique endpoint ID to user-friendly device name. | 248 // Converts unique endpoint ID to user-friendly device name. |
216 std::string GetDeviceName(LPCWSTR device_id) const; | 249 std::string GetDeviceName(LPCWSTR device_id) const; |
217 | 250 |
218 // Called on the audio render thread when the current audio stream must | 251 // Called on the audio render thread when the current audio stream must |
219 // be re-initialized because the default audio device has changed. This | 252 // be re-initialized because the default audio device has changed. This |
220 // method: stops the current renderer, releases and re-creates all WASAPI | 253 // method: stops the current renderer, releases and re-creates all WASAPI |
221 // interfaces, creates a new IMMDevice and re-starts rendering using the | 254 // interfaces, creates a new IMMDevice and re-starts rendering using the |
222 // new default audio device. | 255 // new default audio device. |
223 bool RestartRenderingUsingNewDefaultDevice(); | 256 bool RestartRenderingUsingNewDefaultDevice(); |
224 | 257 |
| 258 AUDCLNT_SHAREMODE share_mode() const { return share_mode_; } |
| 259 |
225 // Initializes the COM library for use by the calling thread and sets the | 260 // Initializes the COM library for use by the calling thread and sets the |
226 // thread's concurrency model to multi-threaded. | 261 // thread's concurrency model to multi-threaded. |
227 base::win::ScopedCOMInitializer com_init_; | 262 base::win::ScopedCOMInitializer com_init_; |
228 | 263 |
229 // Contains the thread ID of the creating thread. | 264 // Contains the thread ID of the creating thread. |
230 base::PlatformThreadId creating_thread_id_; | 265 base::PlatformThreadId creating_thread_id_; |
231 | 266 |
232 // Our creator, the audio manager needs to be notified when we close. | 267 // Our creator, the audio manager needs to be notified when we close. |
233 AudioManagerWin* manager_; | 268 AudioManagerWin* manager_; |
234 | 269 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 304 |
270 // Size in milliseconds of each audio packet. | 305 // Size in milliseconds of each audio packet. |
271 float packet_size_ms_; | 306 float packet_size_ms_; |
272 | 307 |
273 // Length of the audio endpoint buffer. | 308 // Length of the audio endpoint buffer. |
274 size_t endpoint_buffer_size_frames_; | 309 size_t endpoint_buffer_size_frames_; |
275 | 310 |
276 // Defines the role that the system has assigned to an audio endpoint device. | 311 // Defines the role that the system has assigned to an audio endpoint device. |
277 ERole device_role_; | 312 ERole device_role_; |
278 | 313 |
| 314 // The sharing mode for the connection. |
| 315 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE |
| 316 // where AUDCLNT_SHAREMODE_SHARED is the default. |
| 317 AUDCLNT_SHAREMODE share_mode_; |
| 318 |
279 // Counts the number of audio frames written to the endpoint buffer. | 319 // Counts the number of audio frames written to the endpoint buffer. |
280 UINT64 num_written_frames_; | 320 UINT64 num_written_frames_; |
281 | 321 |
282 // Pointer to the client that will deliver audio samples to be played out. | 322 // Pointer to the client that will deliver audio samples to be played out. |
283 AudioSourceCallback* source_; | 323 AudioSourceCallback* source_; |
284 | 324 |
285 // An IMMDeviceEnumerator interface which represents a device enumerator. | 325 // An IMMDeviceEnumerator interface which represents a device enumerator. |
286 base::win::ScopedComPtr<IMMDeviceEnumerator> device_enumerator_; | 326 base::win::ScopedComPtr<IMMDeviceEnumerator> device_enumerator_; |
287 | 327 |
288 // An IMMDevice interface which represents an audio endpoint device. | 328 // An IMMDevice interface which represents an audio endpoint device. |
(...skipping 16 matching lines...) Expand all Loading... |
305 | 345 |
306 // This event will be signaled when stream switching shall take place. | 346 // This event will be signaled when stream switching shall take place. |
307 base::win::ScopedHandle stream_switch_event_; | 347 base::win::ScopedHandle stream_switch_event_; |
308 | 348 |
309 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); | 349 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); |
310 }; | 350 }; |
311 | 351 |
312 } // namespace media | 352 } // namespace media |
313 | 353 |
314 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ | 354 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ |
OLD | NEW |