Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: media/audio/win/audio_low_latency_output_win.h

Issue 12049070: Avoids irregular OnMoreData callbacks on Windows using Core Audio (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Non trivial rebase Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 // factory. 11 // factory.
12 // - Next some thread will call Open(), at that point the underlying 12 // - Next some thread will call Open(), at that point the underlying
13 // Core Audio APIs are utilized to create two WASAPI interfaces called 13 // Core Audio APIs are utilized to create two WASAPI interfaces called
14 // IAudioClient and IAudioRenderClient. 14 // IAudioClient and IAudioRenderClient.
15 // - Then some thread will call Start(source). 15 // - Then some thread will call Start(source).
16 // A thread called "wasapi_render_thread" is started and this thread listens 16 // A thread called "wasapi_render_thread" is started and this thread listens
17 // on an event signal which is set periodically by the audio engine to signal 17 // on an event signal which is set periodically by the audio engine to signal
18 // render events. As a result, OnMoreData() will be called and the registered 18 // render events. As a result, OnMoreData() will be called and the registered
19 // client is then expected to provide data samples to be played out. 19 // client is then expected to provide data samples to be played out.
20 // - At some point, a thread will call Stop(), which stops and joins the 20 // - At some point, a thread will call Stop(), which stops and joins the
21 // render thread and at the same time stops audio streaming. 21 // render thread and at the same time stops audio streaming.
22 // - The same thread that called stop will call Close() where we cleanup 22 // - The same thread that called stop will call Close() where we cleanup
23 // and notify the audio manager, which likely will destroy this object. 23 // and notify the audio manager, which likely will destroy this object.
24 // - Initial tests on Windows 7 shows that this implementation results in a
25 // latency of approximately 35 ms if the selected packet size is less than
26 // or equal to 20 ms. Using a packet size of 10 ms does not result in a
27 // lower latency but only affects the size of the data buffer in each
28 // OnMoreData() callback.
29 // - A total typical delay of 35 ms contains three parts: 24 // - A total typical delay of 35 ms contains three parts:
30 // o Audio endpoint device period (~10 ms). 25 // o Audio endpoint device period (~10 ms).
31 // o Stream latency between the buffer and endpoint device (~5 ms). 26 // o Stream latency between the buffer and endpoint device (~5 ms).
32 // o Endpoint buffer (~20 ms to ensure glitch-free rendering). 27 // o Endpoint buffer (~20 ms to ensure glitch-free rendering).
33 // - Note that, if the user selects a packet size of e.g. 100 ms, the total
34 // delay will be approximately 115 ms (10 + 5 + 100).
35 // 28 //
36 // Implementation notes: 29 // Implementation notes:
37 // 30 //
38 // - The minimum supported client is Windows Vista. 31 // - The minimum supported client is Windows Vista.
39 // - This implementation is single-threaded, hence: 32 // - This implementation is single-threaded, hence:
40 // o Construction and destruction must take place from the same thread. 33 // o Construction and destruction must take place from the same thread.
41 // o All APIs must be called from the creating thread as well. 34 // o All APIs must be called from the creating thread as well.
42 // - It is recommended to first acquire the native sample rate of the default 35 // - It is required to first acquire the native audio parameters of the default
43 // input device and then use the same rate when creating this object. Use 36 // output device and then use the same rate when creating this object. Use
44 // WASAPIAudioOutputStream::HardwareSampleRate() to retrieve the sample rate. 37 // e.g. WASAPIAudioOutputStream::HardwareSampleRate() to retrieve the sample
38 // rate. Open() will fail unless "perfect" audio parameters are utilized.
45 // - Calling Close() also leads to self destruction. 39 // - Calling Close() also leads to self destruction.
46 // - Stream switching is not supported if the user shifts the audio device
47 // after Open() is called but before Start() has been called.
48 // - Stream switching can fail if streaming starts on one device with a
49 // supported format (X) and the new default device - to which we would like
50 // to switch - uses another format (Y), which is not supported given the
51 // configured audio parameters.
52 // - The audio device must be opened with the same number of channels as it
53 // supports natively (see HardwareChannelCount()) otherwise Open() will fail.
54 // - Support for 8-bit audio has not yet been verified and tested. 40 // - Support for 8-bit audio has not yet been verified and tested.
55 // 41 //
56 // Core Audio API details: 42 // Core Audio API details:
57 // 43 //
58 // - The public API methods (Open(), Start(), Stop() and Close()) must be 44 // - The public API methods (Open(), Start(), Stop() and Close()) must be
59 // called on constructing thread. The reason is that we want to ensure that 45 // called on constructing thread. The reason is that we want to ensure that
60 // the COM environment is the same for all API implementations. 46 // the COM environment is the same for all API implementations.
61 // - Utilized MMDevice interfaces: 47 // - Utilized MMDevice interfaces:
62 // o IMMDeviceEnumerator 48 // o IMMDeviceEnumerator
63 // o IMMDevice 49 // o IMMDevice
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 143
158 // Retrieves the channel layout the audio engine uses for its internal 144 // Retrieves the channel layout the audio engine uses for its internal
159 // processing/mixing of shared-mode streams for the default endpoint device. 145 // processing/mixing of shared-mode streams for the default endpoint device.
160 // Note that we convert an internal channel layout mask (see ChannelMask()) 146 // Note that we convert an internal channel layout mask (see ChannelMask())
161 // into a Chrome-specific channel layout enumerator in this method, hence 147 // into a Chrome-specific channel layout enumerator in this method, hence
162 // the match might not be perfect. 148 // the match might not be perfect.
163 static ChannelLayout HardwareChannelLayout(); 149 static ChannelLayout HardwareChannelLayout();
164 150
165 // Retrieves the sample rate the audio engine uses for its internal 151 // Retrieves the sample rate the audio engine uses for its internal
166 // processing/mixing of shared-mode streams for the default endpoint device. 152 // processing/mixing of shared-mode streams for the default endpoint device.
167 static int HardwareSampleRate(ERole device_role); 153 static int HardwareSampleRate();
168 154
169 // Returns AUDCLNT_SHAREMODE_EXCLUSIVE if --enable-exclusive-mode is used 155 // Returns AUDCLNT_SHAREMODE_EXCLUSIVE if --enable-exclusive-mode is used
170 // as command-line flag and AUDCLNT_SHAREMODE_SHARED otherwise (default). 156 // as command-line flag and AUDCLNT_SHAREMODE_SHARED otherwise (default).
171 static AUDCLNT_SHAREMODE GetShareMode(); 157 static AUDCLNT_SHAREMODE GetShareMode();
172 158
173 bool started() const { return render_thread_.get() != NULL; } 159 bool started() const { return render_thread_.get() != NULL; }
174 160
175 // Returns the number of channels the audio engine uses for its internal 161 // Returns the number of channels the audio engine uses for its internal
176 // processing/mixing of shared-mode streams for the default endpoint device. 162 // processing/mixing of shared-mode streams for the default endpoint device.
177 int GetEndpointChannelCountForTesting() { return format_.Format.nChannels; } 163 int GetEndpointChannelCountForTesting() { return format_.Format.nChannels; }
178 164
179 private: 165 private:
180 // DelegateSimpleThread::Delegate implementation. 166 // DelegateSimpleThread::Delegate implementation.
181 virtual void Run() OVERRIDE; 167 virtual void Run() OVERRIDE;
182 168
183 // Issues the OnError() callback to the |sink_|. 169 // Issues the OnError() callback to the |sink_|.
184 void HandleError(HRESULT err); 170 void HandleError(HRESULT err);
185 171
186 // The Open() method is divided into these sub methods.
187 HRESULT SetRenderDevice();
188 HRESULT ActivateRenderDevice();
189 bool DesiredFormatIsSupported();
190 HRESULT InitializeAudioEngine();
191
192 // Called when the device will be opened in shared mode and use the
193 // internal audio engine's mix format.
194 HRESULT SharedModeInitialization();
195
196 // Called when the device will be opened in exclusive mode and use the 172 // Called when the device will be opened in exclusive mode and use the
197 // application specified format. 173 // application specified format.
198 HRESULT ExclusiveModeInitialization(); 174 // TODO(henrika): rewrite and move to CoreAudioUtil when removing flag
199 175 // for exclusive audio mode.
200 // Converts unique endpoint ID to user-friendly device name. 176 HRESULT ExclusiveModeInitialization(IAudioClient* client,
201 std::string GetDeviceName(LPCWSTR device_id) const; 177 HANDLE event_handle,
178 size_t* endpoint_buffer_size);
202 179
203 // Contains the thread ID of the creating thread. 180 // Contains the thread ID of the creating thread.
204 base::PlatformThreadId creating_thread_id_; 181 base::PlatformThreadId creating_thread_id_;
205 182
206 // Our creator, the audio manager needs to be notified when we close. 183 // Our creator, the audio manager needs to be notified when we close.
207 AudioManagerWin* manager_; 184 AudioManagerWin* manager_;
208 185
209 // Rendering is driven by this thread (which has no message loop). 186 // Rendering is driven by this thread (which has no message loop).
210 // All OnMoreData() callbacks will be called from this thread. 187 // All OnMoreData() callbacks will be called from this thread.
211 scoped_ptr<base::DelegateSimpleThread> render_thread_; 188 scoped_ptr<base::DelegateSimpleThread> render_thread_;
212 189
213 // Contains the desired audio format which is set up at construction. 190 // Contains the desired audio format which is set up at construction.
214 // Extended PCM waveform format structure based on WAVEFORMATEXTENSIBLE. 191 // Extended PCM waveform format structure based on WAVEFORMATEXTENSIBLE.
215 // Use this for multiple channel and hi-resolution PCM data. 192 // Use this for multiple channel and hi-resolution PCM data.
216 WAVEFORMATPCMEX format_; 193 WAVEFORMATPCMEX format_;
217 194
218 // Copy of the audio format which we know the audio engine supports. 195 // Set to true when stream is successfully opened.
219 // It is recommended to ensure that the sample rate in |format_| is identical
220 // to the sample rate in |audio_engine_mix_format_|.
221 base::win::ScopedCoMem<WAVEFORMATPCMEX> audio_engine_mix_format_;
222
223 bool opened_; 196 bool opened_;
224 197
225 // Set to true as soon as a new default device is detected, and cleared when 198 // We check if the input audio parameters are identical (bit depth is
226 // the streaming has switched from using the old device to the new device. 199 // excluded) to the preferred (native) audio parameters during construction.
227 // All additional device detections during an active state are ignored to 200 // Open() will fail if |audio_parmeters_are_valid_| is false.
228 // ensure that the ongoing switch can finalize without disruptions. 201 bool audio_parmeters_are_valid_;
229 bool restart_rendering_mode_;
230 202
231 // Volume level from 0 to 1. 203 // Volume level from 0 to 1.
232 float volume_; 204 float volume_;
233 205
234 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM).
235 size_t frame_size_;
236
237 // Size in audio frames of each audio packet where an audio packet 206 // Size in audio frames of each audio packet where an audio packet
238 // is defined as the block of data which the source is expected to deliver 207 // is defined as the block of data which the source is expected to deliver
239 // in each OnMoreData() callback. 208 // in each OnMoreData() callback.
240 size_t packet_size_frames_; 209 size_t packet_size_frames_;
241 210
242 // Size in bytes of each audio packet. 211 // Size in bytes of each audio packet.
243 size_t packet_size_bytes_; 212 size_t packet_size_bytes_;
244 213
245 // Size in milliseconds of each audio packet. 214 // Size in milliseconds of each audio packet.
246 float packet_size_ms_; 215 float packet_size_ms_;
247 216
248 // Length of the audio endpoint buffer. 217 // Length of the audio endpoint buffer.
249 uint32 endpoint_buffer_size_frames_; 218 uint32 endpoint_buffer_size_frames_;
250 219
251 // Defines the role that the system has assigned to an audio endpoint device. 220 // Defines the role that the system has assigned to an audio endpoint device.
252 ERole device_role_; 221 ERole device_role_;
253 222
254 // The sharing mode for the connection. 223 // The sharing mode for the connection.
255 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE 224 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE
256 // where AUDCLNT_SHAREMODE_SHARED is the default. 225 // where AUDCLNT_SHAREMODE_SHARED is the default.
257 AUDCLNT_SHAREMODE share_mode_; 226 AUDCLNT_SHAREMODE share_mode_;
258 227
259 // The channel count set by the client in |params| which is provided to the
260 // constructor. The client must feed the AudioSourceCallback::OnMoreData()
261 // callback with PCM-data that contains this number of channels.
262 int client_channel_count_;
263
264 // Counts the number of audio frames written to the endpoint buffer. 228 // Counts the number of audio frames written to the endpoint buffer.
265 UINT64 num_written_frames_; 229 UINT64 num_written_frames_;
266 230
267 // Pointer to the client that will deliver audio samples to be played out. 231 // Pointer to the client that will deliver audio samples to be played out.
268 AudioSourceCallback* source_; 232 AudioSourceCallback* source_;
269 233
270 // An IMMDeviceEnumerator interface which represents a device enumerator. 234 // An IMMDeviceEnumerator interface which represents a device enumerator.
271 base::win::ScopedComPtr<IMMDeviceEnumerator> device_enumerator_; 235 base::win::ScopedComPtr<IMMDeviceEnumerator> device_enumerator_;
272 236
273 // An IMMDevice interface which represents an audio endpoint device.
274 base::win::ScopedComPtr<IMMDevice> endpoint_device_;
275
276 // An IAudioClient interface which enables a client to create and initialize 237 // An IAudioClient interface which enables a client to create and initialize
277 // an audio stream between an audio application and the audio engine. 238 // an audio stream between an audio application and the audio engine.
278 base::win::ScopedComPtr<IAudioClient> audio_client_; 239 base::win::ScopedComPtr<IAudioClient> audio_client_;
279 240
280 // The IAudioRenderClient interface enables a client to write output 241 // The IAudioRenderClient interface enables a client to write output
281 // data to a rendering endpoint buffer. 242 // data to a rendering endpoint buffer.
282 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_; 243 base::win::ScopedComPtr<IAudioRenderClient> audio_render_client_;
283 244
284 // The audio engine will signal this event each time a buffer becomes 245 // The audio engine will signal this event each time a buffer becomes
285 // ready to be filled by the client. 246 // ready to be filled by the client.
286 base::win::ScopedHandle audio_samples_render_event_; 247 base::win::ScopedHandle audio_samples_render_event_;
287 248
288 // This event will be signaled when rendering shall stop. 249 // This event will be signaled when rendering shall stop.
289 base::win::ScopedHandle stop_render_event_; 250 base::win::ScopedHandle stop_render_event_;
290 251
291 // Container for retrieving data from AudioSourceCallback::OnMoreData(). 252 // Container for retrieving data from AudioSourceCallback::OnMoreData().
292 scoped_ptr<AudioBus> audio_bus_; 253 scoped_ptr<AudioBus> audio_bus_;
293 254
294 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream); 255 DISALLOW_COPY_AND_ASSIGN(WASAPIAudioOutputStream);
295 }; 256 };
296 257
297 } // namespace media 258 } // namespace media
298 259
299 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_ 260 #endif // MEDIA_AUDIO_WIN_AUDIO_LOW_LATENCY_OUTPUT_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698