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

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

Powered by Google App Engine
This is Rietveld 408576698