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 #include "media/audio/win/audio_low_latency_input_win.h" | 5 #include "media/audio/win/audio_low_latency_input_win.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "media/audio/win/audio_manager_win.h" | 10 #include "media/audio/win/audio_manager_win.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 started_(false), | 44 started_(false), |
45 frame_size_(0), | 45 frame_size_(0), |
46 packet_size_frames_(0), | 46 packet_size_frames_(0), |
47 packet_size_bytes_(0), | 47 packet_size_bytes_(0), |
48 endpoint_buffer_size_frames_(0), | 48 endpoint_buffer_size_frames_(0), |
49 effects_(params.effects()), | 49 effects_(params.effects()), |
50 device_id_(device_id), | 50 device_id_(device_id), |
51 perf_count_to_100ns_units_(0.0), | 51 perf_count_to_100ns_units_(0.0), |
52 ms_to_frame_count_(0.0), | 52 ms_to_frame_count_(0.0), |
53 sink_(NULL), | 53 sink_(NULL), |
54 com_stream_(NULL), | |
tommi (sloooow) - chröme
2015/04/22 10:31:52
necessary?
DaleCurtis
2015/04/22 17:48:54
Done.
| |
54 audio_bus_(media::AudioBus::Create(params)) { | 55 audio_bus_(media::AudioBus::Create(params)) { |
55 DCHECK(manager_); | 56 DCHECK(manager_); |
56 | 57 |
57 // Load the Avrt DLL if not already loaded. Required to support MMCSS. | 58 // Load the Avrt DLL if not already loaded. Required to support MMCSS. |
58 bool avrt_init = avrt::Initialize(); | 59 bool avrt_init = avrt::Initialize(); |
59 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; | 60 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; |
60 | 61 |
61 // Set up the desired capture format specified by the client. | 62 // Set up the desired capture format specified by the client. |
62 format_.nSamplesPerSec = params.sample_rate(); | 63 format_.nSamplesPerSec = params.sample_rate(); |
63 format_.wFormatTag = WAVE_FORMAT_PCM; | 64 format_.wFormatTag = WAVE_FORMAT_PCM; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 if (started_) | 152 if (started_) |
152 return; | 153 return; |
153 | 154 |
154 DCHECK(!sink_); | 155 DCHECK(!sink_); |
155 sink_ = callback; | 156 sink_ = callback; |
156 | 157 |
157 // Starts periodic AGC microphone measurements if the AGC has been enabled | 158 // Starts periodic AGC microphone measurements if the AGC has been enabled |
158 // using SetAutomaticGainControl(). | 159 // using SetAutomaticGainControl(). |
159 StartAgc(); | 160 StartAgc(); |
160 | 161 |
162 if (!MarshalComPointers()) { | |
163 HandleError(S_FALSE); | |
164 return; | |
165 } | |
166 | |
161 // Create and start the thread that will drive the capturing by waiting for | 167 // Create and start the thread that will drive the capturing by waiting for |
162 // capture events. | 168 // capture events. |
163 capture_thread_ = | 169 capture_thread_ = |
164 new base::DelegateSimpleThread(this, "wasapi_capture_thread"); | 170 new base::DelegateSimpleThread(this, "wasapi_capture_thread"); |
165 capture_thread_->Start(); | 171 capture_thread_->Start(); |
166 | 172 |
167 // Start streaming data between the endpoint buffer and the audio engine. | 173 // Start streaming data between the endpoint buffer and the audio engine. |
168 HRESULT hr = audio_client_->Start(); | 174 HRESULT hr = audio_client_->Start(); |
169 DLOG_IF(ERROR, FAILED(hr)) << "Failed to start input streaming."; | 175 DLOG_IF(ERROR, FAILED(hr)) << "Failed to start input streaming."; |
170 | 176 |
171 if (SUCCEEDED(hr) && audio_render_client_for_loopback_.get()) | 177 if (SUCCEEDED(hr) && audio_render_client_for_loopback_.get()) |
172 hr = audio_render_client_for_loopback_->Start(); | 178 hr = audio_render_client_for_loopback_->Start(); |
173 | 179 |
174 started_ = SUCCEEDED(hr); | 180 started_ = SUCCEEDED(hr); |
181 if (!started_) | |
182 HandleError(hr); | |
175 } | 183 } |
176 | 184 |
177 void WASAPIAudioInputStream::Stop() { | 185 void WASAPIAudioInputStream::Stop() { |
178 DCHECK(CalledOnValidThread()); | 186 DCHECK(CalledOnValidThread()); |
179 DVLOG(1) << "WASAPIAudioInputStream::Stop()"; | 187 DVLOG(1) << "WASAPIAudioInputStream::Stop()"; |
180 if (!started_) | 188 if (!started_) |
181 return; | 189 return; |
182 | 190 |
183 // Stops periodic AGC microphone measurements. | 191 // Stops periodic AGC microphone measurements. |
184 StopAgc(); | 192 StopAgc(); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 | 351 |
344 ScopedComPtr<IAudioClient> audio_client; | 352 ScopedComPtr<IAudioClient> audio_client; |
345 hr = endpoint_device->Activate(__uuidof(IAudioClient), | 353 hr = endpoint_device->Activate(__uuidof(IAudioClient), |
346 CLSCTX_INPROC_SERVER, | 354 CLSCTX_INPROC_SERVER, |
347 NULL, | 355 NULL, |
348 audio_client.ReceiveVoid()); | 356 audio_client.ReceiveVoid()); |
349 return SUCCEEDED(hr) ? audio_client->GetMixFormat(device_format) : hr; | 357 return SUCCEEDED(hr) ? audio_client->GetMixFormat(device_format) : hr; |
350 } | 358 } |
351 | 359 |
352 void WASAPIAudioInputStream::Run() { | 360 void WASAPIAudioInputStream::Run() { |
353 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); | 361 ScopedCOMInitializer com_init; |
354 | 362 |
355 // Increase the thread priority. | 363 // Increase the thread priority. |
356 capture_thread_->SetThreadPriority(base::ThreadPriority::REALTIME_AUDIO); | 364 capture_thread_->SetThreadPriority(base::ThreadPriority::REALTIME_AUDIO); |
357 | 365 |
358 // Enable MMCSS to ensure that this thread receives prioritized access to | 366 // Enable MMCSS to ensure that this thread receives prioritized access to |
359 // CPU resources. | 367 // CPU resources. |
360 DWORD task_index = 0; | 368 DWORD task_index = 0; |
361 HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio", | 369 HANDLE mm_task = avrt::AvSetMmThreadCharacteristics(L"Pro Audio", |
362 &task_index); | 370 &task_index); |
363 bool mmcss_is_ok = | 371 bool mmcss_is_ok = |
364 (mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL)); | 372 (mm_task && avrt::AvSetMmThreadPriority(mm_task, AVRT_PRIORITY_CRITICAL)); |
365 if (!mmcss_is_ok) { | 373 if (!mmcss_is_ok) { |
366 // Failed to enable MMCSS on this thread. It is not fatal but can lead | 374 // Failed to enable MMCSS on this thread. It is not fatal but can lead |
367 // to reduced QoS at high load. | 375 // to reduced QoS at high load. |
368 DWORD err = GetLastError(); | 376 DWORD err = GetLastError(); |
369 LOG(WARNING) << "Failed to enable MMCSS (error code=" << err << ")."; | 377 LOG(WARNING) << "Failed to enable MMCSS (error code=" << err << ")."; |
370 } | 378 } |
371 | 379 |
380 // Retrieve COM pointers from the main thread. | |
381 ScopedComPtr<IAudioCaptureClient> thread_audio_capture_client; | |
382 | |
383 bool error = !UnmarshalComPointers(&thread_audio_capture_client); | |
384 | |
372 // Allocate a buffer with a size that enables us to take care of cases like: | 385 // Allocate a buffer with a size that enables us to take care of cases like: |
373 // 1) The recorded buffer size is smaller, or does not match exactly with, | 386 // 1) The recorded buffer size is smaller, or does not match exactly with, |
374 // the selected packet size used in each callback. | 387 // the selected packet size used in each callback. |
375 // 2) The selected buffer size is larger than the recorded buffer size in | 388 // 2) The selected buffer size is larger than the recorded buffer size in |
376 // each event. | 389 // each event. |
377 size_t buffer_frame_index = 0; | 390 size_t buffer_frame_index = 0; |
378 size_t capture_buffer_size = std::max( | 391 size_t capture_buffer_size = std::max( |
379 2 * endpoint_buffer_size_frames_ * frame_size_, | 392 2 * endpoint_buffer_size_frames_ * frame_size_, |
380 2 * packet_size_frames_ * frame_size_); | 393 2 * packet_size_frames_ * frame_size_); |
381 scoped_ptr<uint8[]> capture_buffer(new uint8[capture_buffer_size]); | 394 scoped_ptr<uint8[]> capture_buffer(new uint8[capture_buffer_size]); |
382 | 395 |
383 LARGE_INTEGER now_count; | 396 LARGE_INTEGER now_count; |
384 bool recording = true; | 397 bool recording = true; |
385 bool error = false; | 398 double volume = 0; |
386 double volume = GetVolume(); | |
387 HANDLE wait_array[2] = | 399 HANDLE wait_array[2] = |
388 { stop_capture_event_.Get(), audio_samples_ready_event_.Get() }; | 400 { stop_capture_event_.Get(), audio_samples_ready_event_.Get() }; |
389 | 401 |
390 while (recording && !error) { | 402 while (recording && !error) { |
391 HRESULT hr = S_FALSE; | 403 HRESULT hr = S_FALSE; |
392 | 404 |
393 // Wait for a close-down event or a new capture event. | 405 // Wait for a close-down event or a new capture event. |
394 DWORD wait_result = WaitForMultipleObjects(2, wait_array, FALSE, INFINITE); | 406 DWORD wait_result = WaitForMultipleObjects(2, wait_array, FALSE, INFINITE); |
395 switch (wait_result) { | 407 switch (wait_result) { |
396 case WAIT_FAILED: | 408 case WAIT_FAILED: |
397 error = true; | 409 error = true; |
398 break; | 410 break; |
399 case WAIT_OBJECT_0 + 0: | 411 case WAIT_OBJECT_0 + 0: |
400 // |stop_capture_event_| has been set. | 412 // |stop_capture_event_| has been set. |
401 recording = false; | 413 recording = false; |
402 break; | 414 break; |
403 case WAIT_OBJECT_0 + 1: | 415 case WAIT_OBJECT_0 + 1: |
404 { | 416 { |
405 // |audio_samples_ready_event_| has been set. | 417 // |audio_samples_ready_event_| has been set. |
406 BYTE* data_ptr = NULL; | 418 BYTE* data_ptr = NULL; |
407 UINT32 num_frames_to_read = 0; | 419 UINT32 num_frames_to_read = 0; |
408 DWORD flags = 0; | 420 DWORD flags = 0; |
409 UINT64 device_position = 0; | 421 UINT64 device_position = 0; |
410 UINT64 first_audio_frame_timestamp = 0; | 422 UINT64 first_audio_frame_timestamp = 0; |
411 | 423 |
412 // Retrieve the amount of data in the capture endpoint buffer, | 424 // Retrieve the amount of data in the capture endpoint buffer, |
413 // replace it with silence if required, create callbacks for each | 425 // replace it with silence if required, create callbacks for each |
414 // packet and store non-delivered data for the next event. | 426 // packet and store non-delivered data for the next event. |
415 hr = audio_capture_client_->GetBuffer(&data_ptr, | 427 hr = thread_audio_capture_client->GetBuffer( |
416 &num_frames_to_read, | 428 &data_ptr, &num_frames_to_read, &flags, &device_position, |
417 &flags, | 429 &first_audio_frame_timestamp); |
418 &device_position, | |
419 &first_audio_frame_timestamp); | |
420 if (FAILED(hr)) { | 430 if (FAILED(hr)) { |
421 DLOG(ERROR) << "Failed to get data from the capture buffer"; | 431 DLOG(ERROR) << "Failed to get data from the capture buffer"; |
422 continue; | 432 continue; |
423 } | 433 } |
424 | 434 |
425 if (num_frames_to_read != 0) { | 435 if (num_frames_to_read != 0) { |
426 size_t pos = buffer_frame_index * frame_size_; | 436 size_t pos = buffer_frame_index * frame_size_; |
427 size_t num_bytes = num_frames_to_read * frame_size_; | 437 size_t num_bytes = num_frames_to_read * frame_size_; |
428 DCHECK_GE(capture_buffer_size, pos + num_bytes); | 438 DCHECK_GE(capture_buffer_size, pos + num_bytes); |
429 | 439 |
430 if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { | 440 if (flags & AUDCLNT_BUFFERFLAGS_SILENT) { |
431 // Clear out the local buffer since silence is reported. | 441 // Clear out the local buffer since silence is reported. |
432 memset(&capture_buffer[pos], 0, num_bytes); | 442 memset(&capture_buffer[pos], 0, num_bytes); |
433 } else { | 443 } else { |
434 // Copy captured data from audio engine buffer to local buffer. | 444 // Copy captured data from audio engine buffer to local buffer. |
435 memcpy(&capture_buffer[pos], data_ptr, num_bytes); | 445 memcpy(&capture_buffer[pos], data_ptr, num_bytes); |
436 } | 446 } |
437 | 447 |
438 buffer_frame_index += num_frames_to_read; | 448 buffer_frame_index += num_frames_to_read; |
439 } | 449 } |
440 | 450 |
441 hr = audio_capture_client_->ReleaseBuffer(num_frames_to_read); | 451 hr = thread_audio_capture_client->ReleaseBuffer(num_frames_to_read); |
442 DLOG_IF(ERROR, FAILED(hr)) << "Failed to release capture buffer"; | 452 DLOG_IF(ERROR, FAILED(hr)) << "Failed to release capture buffer"; |
443 | 453 |
444 // Derive a delay estimate for the captured audio packet. | 454 // Derive a delay estimate for the captured audio packet. |
445 // The value contains two parts (A+B), where A is the delay of the | 455 // The value contains two parts (A+B), where A is the delay of the |
446 // first audio frame in the packet and B is the extra delay | 456 // first audio frame in the packet and B is the extra delay |
447 // contained in any stored data. Unit is in audio frames. | 457 // contained in any stored data. Unit is in audio frames. |
448 QueryPerformanceCounter(&now_count); | 458 QueryPerformanceCounter(&now_count); |
449 double audio_delay_frames = | 459 double audio_delay_frames = |
450 ((perf_count_to_100ns_units_ * now_count.QuadPart - | 460 ((perf_count_to_100ns_units_ * now_count.QuadPart - |
451 first_audio_frame_timestamp) / 10000.0) * ms_to_frame_count_ + | 461 first_audio_frame_timestamp) / 10000.0) * ms_to_frame_count_ + |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 // that glitches do not occur between the periodic processing passes. | 679 // that glitches do not occur between the periodic processing passes. |
670 // This setting should lead to lowest possible latency. | 680 // This setting should lead to lowest possible latency. |
671 HRESULT hr = audio_client_->Initialize( | 681 HRESULT hr = audio_client_->Initialize( |
672 AUDCLNT_SHAREMODE_SHARED, | 682 AUDCLNT_SHAREMODE_SHARED, |
673 flags, | 683 flags, |
674 0, // hnsBufferDuration | 684 0, // hnsBufferDuration |
675 0, | 685 0, |
676 &format_, | 686 &format_, |
677 (effects_ & AudioParameters::DUCKING) ? &kCommunicationsSessionId : NULL); | 687 (effects_ & AudioParameters::DUCKING) ? &kCommunicationsSessionId : NULL); |
678 | 688 |
679 if (FAILED(hr)) | 689 if (FAILED(hr)) { |
690 PLOG(ERROR) << "Failed to initalize IAudioClient: " << std::hex << hr | |
691 << " : "; | |
tommi (sloooow) - chröme
2015/04/22 10:31:51
is this line left over from some previous code? (o
DaleCurtis
2015/04/22 17:48:54
It was the method that was silently failing during
| |
680 return hr; | 692 return hr; |
693 } | |
681 | 694 |
682 // Retrieve the length of the endpoint buffer shared between the client | 695 // Retrieve the length of the endpoint buffer shared between the client |
683 // and the audio engine. The buffer length determines the maximum amount | 696 // and the audio engine. The buffer length determines the maximum amount |
684 // of capture data that the audio engine can read from the endpoint buffer | 697 // of capture data that the audio engine can read from the endpoint buffer |
685 // during a single processing pass. | 698 // during a single processing pass. |
686 // A typical value is 960 audio frames <=> 20ms @ 48kHz sample rate. | 699 // A typical value is 960 audio frames <=> 20ms @ 48kHz sample rate. |
687 hr = audio_client_->GetBufferSize(&endpoint_buffer_size_frames_); | 700 hr = audio_client_->GetBufferSize(&endpoint_buffer_size_frames_); |
688 if (FAILED(hr)) | 701 if (FAILED(hr)) |
689 return hr; | 702 return hr; |
690 | 703 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
761 if (FAILED(hr)) | 774 if (FAILED(hr)) |
762 return hr; | 775 return hr; |
763 | 776 |
764 // Obtain a reference to the ISimpleAudioVolume interface which enables | 777 // Obtain a reference to the ISimpleAudioVolume interface which enables |
765 // us to control the master volume level of an audio session. | 778 // us to control the master volume level of an audio session. |
766 hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume), | 779 hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume), |
767 simple_audio_volume_.ReceiveVoid()); | 780 simple_audio_volume_.ReceiveVoid()); |
768 return hr; | 781 return hr; |
769 } | 782 } |
770 | 783 |
784 bool WASAPIAudioInputStream::MarshalComPointers() { | |
785 HRESULT hr = CoMarshalInterThreadInterfaceInStream( | |
786 __uuidof(IAudioCaptureClient), audio_capture_client_.get(), | |
787 com_stream_.Receive()); | |
788 if (FAILED(hr)) | |
789 DLOG(ERROR) << "Marshal failed for IAudioCaptureClient: " << std::hex << hr; | |
790 DCHECK(com_stream_); | |
791 return SUCCEEDED(hr); | |
792 } | |
793 | |
794 bool WASAPIAudioInputStream::UnmarshalComPointers( | |
795 ScopedComPtr<IAudioCaptureClient>* audio_capture_client) { | |
796 HRESULT hr = CoGetInterfaceAndReleaseStream( | |
797 com_stream_.Detach(), __uuidof(IAudioCaptureClient), | |
798 audio_capture_client->ReceiveVoid()); | |
799 if (FAILED(hr)) | |
tommi (sloooow) - chröme
2015/04/22 10:31:52
Should we have a DCHECK here that hr either succee
DaleCurtis
2015/04/22 17:48:54
IStream is always released according to the API do
| |
800 DLOG(ERROR) << "Unmarshal failed IAudioCaptureClient: " << std::hex << hr; | |
801 return SUCCEEDED(hr); | |
802 } | |
803 | |
771 } // namespace media | 804 } // namespace media |
OLD | NEW |