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

Side by Side Diff: media/audio/win/audio_low_latency_input_win.cc

Issue 8283032: Low-latency AudioInputStream implementation based on WASAPI for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 2 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "media/audio/win/audio_low_latency_input_win.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/logging.h"
9 #include "media/audio/audio_util.h"
10 #include "media/audio/win/audio_manager_win.h"
11
12 using base::win::ScopedComPtr;
13
14 #ifndef NDEBUG
15 static void DLogFormat(const char* str, const WAVEFORMATEX* format) {
16 DLOG(INFO) << str << std::endl
17 << " wFormatTag : " << format->wFormatTag << std::endl
18 << " nChannels : " << format->nChannels << std::endl
19 << " nSamplesPerSec : " << format->nSamplesPerSec << std::endl
20 << " nAvgBytesPerSec: " << format->nAvgBytesPerSec << std::endl
21 << " wBitsPerSample : " << format->wBitsPerSample << std::endl
22 << " nBlockAlign : " << format->nBlockAlign << std::endl
23 << " cbSize : " << format->cbSize << std::endl;
24 }
25 #endif
26
27 WASAPIAudioInputStream::WASAPIAudioInputStream(
28 AudioManagerWin* manager, const AudioParameters& params, ERole device_role)
29 : manager_(manager),
30 capture_thread_(NULL),
31 opened_(false),
32 started_(false),
33 endpoint_buffer_size_frames_(0),
34 device_role_(device_role),
35 sink_(NULL) {
36 DCHECK(manager_);
37
38 // Set up the desired capture format specified by the client.
39 format_.nSamplesPerSec = params.sample_rate;
40 format_.wFormatTag = WAVE_FORMAT_PCM;
41 format_.wBitsPerSample = params.bits_per_sample;
42 format_.nChannels = params.channels;
43 format_.nBlockAlign = (format_.wBitsPerSample / 8) * format_.nChannels;
44 format_.nAvgBytesPerSec = format_.nSamplesPerSec * format_.nBlockAlign;
45 format_.cbSize = 0;
46 #ifndef NDEBUG
47 DLogFormat("Desired capture format:", &format_);
48 #endif
49
50 // Size in bytes of each audio frame.
51 frame_size_ = format_.nBlockAlign;
52 // Store size of audio packets which we expect to get from the audio
53 // endpoint device in each capture event.
54 packet_size_frames_ = params.GetPacketSize() / format_.nBlockAlign;
55 packet_size_bytes_ = params.GetPacketSize();
56 packet_size_ms_ = (1000.0 * packet_size_frames_) / params.sample_rate;
Niklas Enbom 2011/10/14 14:46:51 ever used?
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
57 DLOG(INFO) << "Number of bytes per audio frame : " << frame_size_;
58 DLOG(INFO) << "Number of audio frames per packet: " << packet_size_frames_;
59
60 // All events are auto-reset events and non-signaled initially.
61
62 // Create the event which the audio engine will signal each time
63 // a buffer becomes ready to be processed by the client.
64 audio_samples_ready_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
65 DCHECK(audio_samples_ready_event_.IsValid());
66
67 // Create the event which will be set in Stop() when capturing shall stop.
68 stop_capture_event_.Set(CreateEvent(NULL, FALSE, FALSE, NULL));
69 DCHECK(stop_capture_event_.IsValid());
70
71 ms_to_frame_count_ = static_cast<double>(params.sample_rate) / 1000.0;
72
73 LARGE_INTEGER performance_frequency;
74 if (QueryPerformanceFrequency(&performance_frequency)) {
75 perf_count_to_100ns_units_ =
76 (10000000.0 / static_cast<double>(performance_frequency.QuadPart));
77 } else {
78 LOG(ERROR) << "High-resolution performance counters are not supported.";
79 perf_count_to_100ns_units_ = 0.0;
80 }
81 }
82
83 WASAPIAudioInputStream::~WASAPIAudioInputStream() {
84 }
85
86 bool WASAPIAudioInputStream::Open() {
87 // Verify that we are not already opened.
88 if (opened_)
89 return false;
90
91 // Obtain a reference to the IMMDevice interface of the default capturing
92 // device with the specified role.
93 HRESULT hr = SetCaptureDevice(device_role_);
94 if (FAILED(hr)) {
95 HandleError(hr);
96 return false;
97 }
98
99 // Obtain an IAudioClient interface which enables us to create and initialize
100 // an audio stream between an audio application and the audio engine.
101 hr = ActivateCaptureDevice();
102 if (FAILED(hr)) {
103 HandleError(hr);
104 return false;
105 }
106
107 // Retrieve the stream format which the audio engine uses for its internal
108 // processing/mixing of shared-mode streams.
109 hr = GetAudioEngineStreamFormat();
110 if (FAILED(hr)) {
111 HandleError(hr);
112 return false;
113 }
114
115 // Verify that the selected audio endpoint supports the specified format
116 // set during construction.
117 if (!DesiredFormatIsSupported()) {
118 hr = E_INVALIDARG;
119 HandleError(hr);
120 return false;
121 }
122
123 // Initialize the audio stream between the client and the device using
124 // shared mode and a lowest possible glitch-free latency.
125 hr = InitializeAudioEngine();
126 if (FAILED(hr)) {
127 HandleError(hr);
128 return false;
129 }
130
131 opened_ = true;
132
133 return true;
134 }
135
136 void WASAPIAudioInputStream::Start(AudioInputCallback* callback) {
137 DCHECK(callback);
138
139 if (!opened_)
tommi (sloooow) - chröme 2011/10/14 14:31:04 since this function doesn't return a value, it's e
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Added DCHECK(opened_).
140 return;
141
142 if (started_)
143 return;
144
145 sink_ = callback;
146
147 // Create and start the thread will drive the capturing by waiting for
tommi (sloooow) - chröme 2011/10/14 14:31:04 the thread _that_ will drive
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
148 // capture events.
149 capture_thread_ =
150 new base::DelegateSimpleThread(this, "wasapi_capture_thread");
151 capture_thread_->Start();
152
153 // Start streaming data between the endpoint buffer and the audio engine.
154 HRESULT hr = audio_client_->Start();
155 if (FAILED(hr)) {
156 LOG(ERROR) << "Failed to start input streaming.";
tommi (sloooow) - chröme 2011/10/14 14:31:04 fyi - could use LOG_IF(FAILED(hr), ERROR) << ...
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
157 }
158
159 started_ = true;
tommi (sloooow) - chröme 2011/10/14 14:31:04 do you want to set started_ to true even if Start
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Thanks. My bad.
160 }
161
162 void WASAPIAudioInputStream::Stop() {
163 if (!started_)
164 return;
165
166 // Shut down the capture thread.
167 if (stop_capture_event_.IsValid()) {
168 SetEvent(stop_capture_event_.Get());
169 }
170
171 // Stop the input audio streaming.
172 HRESULT hr = audio_client_->Stop();
173 if (FAILED(hr)) {
174 LOG(ERROR) << "Failed to stop input streaming.";
175 }
176
177 // Wait until the thread completes and perform cleanup.
178 if (capture_thread_) {
179 SetEvent(stop_capture_event_.Get());
180 capture_thread_->Join();
181 capture_thread_ = NULL;
182 }
183
184 started_ = false;
185 }
186
187 void WASAPIAudioInputStream::Close() {
188 // It is valid to call Close() before calling open or Start().
189 // It is also valid to call Close() after Start() has been called.
190 Stop();
191 if (sink_) {
192 sink_->OnClose(this);
193 sink_ = NULL;
194 }
195
196 // Inform the audio manager that we have been closed. This can cause our
197 // destruction.
198 manager_->ReleaseInputStream(this);
199 }
200
201 // static
202 double WASAPIAudioInputStream::HardwareSampleRate(ERole device_role) {
203 ScopedCOMInitializer com_init;
tommi (sloooow) - chröme 2011/10/14 14:31:04 It's a bit confusing that this scoped com initiali
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
204 ScopedComPtr<IMMDeviceEnumerator> enumerator;
205 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
206 NULL,
207 CLSCTX_INPROC_SERVER,
208 __uuidof(IMMDeviceEnumerator),
209 enumerator.ReceiveVoid());
210 if (FAILED(hr))
211 return 0.0;
tommi (sloooow) - chröme 2011/10/14 14:31:04 NOTREACHED or LOG(ERROR)?
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
212
213 ScopedComPtr<IMMDevice> endpoint_device;
214 hr = enumerator->GetDefaultAudioEndpoint(eCapture,
215 device_role,
216 endpoint_device.Receive());
217 if (FAILED(hr))
tommi (sloooow) - chröme 2011/10/14 14:31:04 NOTREACHED?
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
218 return 0.0;
219
220 ScopedComPtr<IAudioClient> audio_client;
221 hr = endpoint_device->Activate(__uuidof(IAudioClient),
222 CLSCTX_INPROC_SERVER,
223 NULL,
224 audio_client.ReceiveVoid());
225 if (FAILED(hr))
226 return 0.0;
tommi (sloooow) - chröme 2011/10/14 14:31:04 same here and the return statement below.
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
227
228 ScopedComMem<WAVEFORMATEX> audio_engine_mix_format;
229 hr = audio_client->GetMixFormat(audio_engine_mix_format.Receive());
230 if (FAILED(hr))
231 return 0.0;
232
233 return static_cast<double>(audio_engine_mix_format->nSamplesPerSec);
234 }
235
236 void WASAPIAudioInputStream::Run() {
237 ScopedCOMInitializer com_init;
238
239 capture_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);
240
241 // TODO(henrika): add MMCSS support.
242
243 // Allocate a buffer with a size that enables us to take care of cases like:
244 // 1) The recorded buffer size is smaller, or does not match exactly with,
245 // the selected packet size used in each callback.
246 // 2) The selected buffer size is larger than the recorded buffer size in
247 // each event.
248 size_t buffer_frame_index = 0;
249 size_t capture_buffer_size = std::max(
250 2 * endpoint_buffer_size_frames_ * frame_size_,
251 2 * packet_size_frames_ * frame_size_);
252 scoped_array<BYTE> capture_buffer(new BYTE[capture_buffer_size]);
tommi (sloooow) - chröme 2011/10/14 14:31:04 BYTE -> uint8
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
253
254 LARGE_INTEGER perf_count;
255 bool recording = true;
256 HANDLE wait_array[2] = {stop_capture_event_, audio_samples_ready_event_};
257
258 while (recording) {
259 HRESULT hr = S_FALSE;
260
261 // Wait for a close-down event or a new capture event.
262 DWORD wait_result = WaitForMultipleObjects(2, wait_array, FALSE, INFINITE);
263 switch (wait_result) {
264 case WAIT_FAILED:
265 recording = false;
266 LOG(ERROR) << "WSAPI capturing failed with error code "
Niklas Enbom 2011/10/14 14:46:51 WASAPI
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
267 << GetLastError();
268 break;
269 case WAIT_OBJECT_0 + 0:
270 // |stop_capture_event_| has been set.
271 recording = false;
272 break;
273 case WAIT_OBJECT_0 + 1:
274 // |audio_samples_ready_event_| has been set.
275 BYTE* data_ptr = NULL;
276 UINT32 num_frames_to_read = 0;
277 DWORD flags = 0;
278 UINT64 device_position = 0;
279 UINT64 first_audio_frame_position = 0;
280
281 // Retrieve the amount of data in the capture endpoint buffer,
282 // replace it with silence if required, create callbacks for each
283 // packet and store non-delivered data for the next event.
284 hr = audio_capture_client_->GetBuffer(&data_ptr,
285 &num_frames_to_read,
286 &flags,
287 &device_position,
288 &first_audio_frame_position);
289 if (SUCCEEDED(hr)) {
290 if (num_frames_to_read != 0) {
291 size_t pos = buffer_frame_index * frame_size_;
292 size_t num_bytes = num_frames_to_read * frame_size_;
293 DCHECK_GE(capture_buffer_size, pos + num_bytes);
294
295 if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
296 // Clear out the local buffer since silence is reported.
297 ZeroMemory(&capture_buffer[pos], num_bytes);
298 } else {
299 // Copy captured data from audio engine buffer to local buffer.
300 CopyMemory(&capture_buffer[pos], data_ptr, num_bytes);
301 }
302
303 buffer_frame_index += num_frames_to_read;
304 }
305
306 hr = audio_capture_client_->ReleaseBuffer(num_frames_to_read);
307 if FAILED(hr)
tommi (sloooow) - chröme 2011/10/14 14:31:04 missing ()
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
308 HandleError(hr);
309
310 // Derive a delay estimate for the captured audio packet.
311 // The value contains two parts (A+B), where A is the delay of the
312 // first audio frame in the packet and B is the extra delay contained
313 // in any stored data. Unit is in audio frames.
Niklas Enbom 2011/10/14 14:46:51 If someone else than you and I should understand t
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Thanks. Done.
314 QueryPerformanceCounter(&perf_count);
315 double audio_delay_frames =
316 ((perf_count_to_100ns_units_ * perf_count.QuadPart -
317 first_audio_frame_position) / 10000.0) * ms_to_frame_count_ +
318 buffer_frame_index - num_frames_to_read;
319
320 // Deliver captured data to the registered consumer using a packet
321 // size which was specified at construction.
322 uint32 delay_frames = static_cast<uint32> (audio_delay_frames + 0.5);
323 while (buffer_frame_index >= packet_size_frames_) {
324 uint8* audio_data = reinterpret_cast<uint8*>(capture_buffer.get());
325
326 // Deliver data packet and delay estimation to the user.
327 sink_->OnData(this,
328 audio_data,
329 packet_size_bytes_,
330 delay_frames * frame_size_);
331
332 // Store parts of the recorded data which can't be delivered
333 // using the current packet size. The stored section will be used
334 // either in the next while-loop iteration or in the next
335 // capture event.
336 MoveMemory(&capture_buffer[0],
tommi (sloooow) - chröme 2011/10/14 14:31:04 memmove?
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
337 &capture_buffer[packet_size_bytes_],
338 (buffer_frame_index - packet_size_frames_) * frame_size_);
339
340 buffer_frame_index -= packet_size_frames_;
341 delay_frames -= packet_size_frames_;
342 }
343 }
344 break;
345 }
346 }
347 }
348
349 void WASAPIAudioInputStream::HandleError(HRESULT err) {
350 // TODO(henrika): add COM-specific logging here.
351 NOTREACHED() << "error code: " << err;
352 if (sink_)
353 sink_->OnError(this, static_cast<int>(err));
354 }
355
356 HRESULT WASAPIAudioInputStream::SetCaptureDevice(ERole device_role) {
357 ScopedComPtr<IMMDeviceEnumerator> enumerator;
358 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
359 NULL,
360 CLSCTX_INPROC_SERVER,
361 __uuidof(IMMDeviceEnumerator),
362 enumerator.ReceiveVoid());
363 if (SUCCEEDED(hr)) {
364 // Retrieve the default capture audio endpoint for the specified role.
365 // Note that, in Windows Vista, the MMDevice API supports device roles
366 // but the system-supplied user interface programs do not.
367 hr = enumerator->GetDefaultAudioEndpoint(eCapture,
368 device_role,
369 endpoint_device_.Receive());
370
371 // Verify that the audio endpoint device is active. That is, the audio
372 // adapter that connects to the endpoint device is present and enabled.
373 DWORD state = DEVICE_STATE_DISABLED;
374 hr = endpoint_device_->GetState(&state);
375 if (SUCCEEDED(hr)) {
376 if (!(state & DEVICE_STATE_ACTIVE)) {
377 DLOG(ERROR) << "Selected capture device is not active.";
378 hr = E_ACCESSDENIED;
379 }
380 }
381 }
382
383 return hr;
384 }
385
386 HRESULT WASAPIAudioInputStream::ActivateCaptureDevice() {
387 // Creates and activates an IAudioClient COM object given the selected
388 // capture endpoint device.
389 HRESULT hr = endpoint_device_->Activate(__uuidof(IAudioClient),
390 CLSCTX_INPROC_SERVER,
391 NULL,
392 audio_client_.ReceiveVoid());
393 return hr;
394 }
395
396 HRESULT WASAPIAudioInputStream::GetAudioEngineStreamFormat() {
397 // Retrieve the stream format that the audio engine uses for its internal
398 // processing/mixing of shared-mode streams.
399 HRESULT hr = audio_client_->GetMixFormat(audio_engine_mix_format_.Receive());
400 #ifndef NDEBUG
401 if (SUCCEEDED(hr))
402 DLogFormat("Audio Engine's format:", audio_engine_mix_format_.get());
403 #endif
404 return hr;
405 }
406
407 bool WASAPIAudioInputStream::DesiredFormatIsSupported() {
408 // In shared mode, the audio engine always supports the mix format,
409 // which is stored in the |audio_engine_mix_format_| member. In addition,
410 // the audio engine *might* support similar formats that have the same
411 // sample rate and number of channels as the mix format but differ in
412 // the representation of audio sample values.
413 ScopedComMem<WAVEFORMATEX> closest_match;
414 HRESULT hr = audio_client_->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED,
415 &format_,
416 closest_match.Receive());
417 if (hr == S_FALSE) {
418 DLOG(ERROR) << "Format is not supported but a closest match exists.";
419 #ifndef NDEBUG
420 DLogFormat("Closest suggested capture format:", closest_match.get());
421 #endif
422 return false;
tommi (sloooow) - chröme 2011/10/14 14:31:04 nit: no need for this return statement as the one
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
423 }
424 return (hr == S_OK);
425 }
426
427 HRESULT WASAPIAudioInputStream::InitializeAudioEngine() {
428 // Initialize the audio stream between the client and the device.
429 // We connect indirectly through the audio engine by using shared mode
430 // and WASAPI is initialized in an event driven mode.
431 // Note that, |hnsBufferDuration| is set of 0, which ensures that the
432 // buffer is never smaller than the minimum buffer size needed to ensure
433 // that glitches do not occur between the periodic processing passes.
434 // This setting should lead to lowest possible latency.
435 HRESULT hr = audio_client_->Initialize(AUDCLNT_SHAREMODE_SHARED,
436 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
437 AUDCLNT_STREAMFLAGS_NOPERSIST,
438 0, // hnsBufferDuration
439 0,
440 &format_,
441 NULL);
442 if (FAILED(hr))
443 return hr;
444
445 // Retrieve the length of the endpoint buffer shared between the client
446 // and the audio engine. The buffer length determines the maximum amount
447 // of capture data that the audio engine can read from the endpoint buffer
448 // during a single processing pass.
449 // A typical value is 960 audio frames <=> 20ms @ 48kHz sample rate.
450 hr = audio_client_->GetBufferSize(&endpoint_buffer_size_frames_);
451 if (FAILED(hr))
452 return hr;
453 DLOG(INFO) << "endpoint buffer size: " << endpoint_buffer_size_frames_
454 << " [frames]";
455
456 #ifndef NDEBUG
457 // The period between processing passes by the audio engine is fixed for a
458 // particular audio endpoint device and represents the smallest processing
459 // quantum for the audio engine. This period plus the stream latency between
460 // the buffer and endpoint device represents the minimum possible latency
461 // that an audio application can achieve.
462 REFERENCE_TIME device_period_shared_mode = 0;
463 REFERENCE_TIME device_period_exclusive_mode = 0;
464 HRESULT hr_dbg = audio_client_->GetDevicePeriod(
465 &device_period_shared_mode, &device_period_exclusive_mode);
466 if (SUCCEEDED(hr_dbg)) {
467 DLOG(INFO) << "device period: "
468 << static_cast<double>(device_period_shared_mode / 10000.0)
469 << " [ms]";
470 }
471
472 REFERENCE_TIME latency = 0;
473 hr_dbg = audio_client_->GetStreamLatency(&latency);
474 if (SUCCEEDED(hr_dbg)) {
475 DLOG(INFO) << "stream latency: " << static_cast<double>(latency / 10000.0)
476 << " [ms]";
477 }
478 #endif
479
480 // Set the event handle that the audio engine will signal each time
481 // a buffer becomes ready to be processed by the client.
482 hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get());
483 if (FAILED(hr))
484 return hr;
485
486 // Get access to the IAudioCaptureClient interface. This interface
487 // enables us to read input data from the capture endpoint buffer.
488 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient),
489 audio_capture_client_.ReceiveVoid());
490 if (FAILED(hr))
tommi (sloooow) - chröme 2011/10/14 14:31:04 nit: no need for this if or return statement
henrika (OOO until Aug 14) 2011/10/17 12:08:24 Done.
491 return hr;
492
493 return hr;
494 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698