OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "media/audio/audio_util.h" | 10 #include "media/audio/audio_util.h" |
11 #include "media/audio/win/audio_manager_win.h" | 11 #include "media/audio/win/audio_manager_win.h" |
12 #include "media/audio/win/avrt_wrapper_win.h" | 12 #include "media/audio/win/avrt_wrapper_win.h" |
13 | 13 |
14 using base::win::ScopedComPtr; | 14 using base::win::ScopedComPtr; |
15 using base::win::ScopedCOMInitializer; | 15 using base::win::ScopedCOMInitializer; |
16 | 16 |
17 WASAPIAudioInputStream::WASAPIAudioInputStream( | 17 WASAPIAudioInputStream::WASAPIAudioInputStream( |
18 AudioManagerWin* manager, const AudioParameters& params, ERole device_role) | 18 AudioManagerWin* manager, const AudioParameters& params, |
| 19 const std::string& device_id) |
19 : com_init_(ScopedCOMInitializer::kMTA), | 20 : com_init_(ScopedCOMInitializer::kMTA), |
20 manager_(manager), | 21 manager_(manager), |
21 capture_thread_(NULL), | 22 capture_thread_(NULL), |
22 opened_(false), | 23 opened_(false), |
23 started_(false), | 24 started_(false), |
24 endpoint_buffer_size_frames_(0), | 25 endpoint_buffer_size_frames_(0), |
25 device_role_(device_role), | 26 device_id_(device_id), |
26 sink_(NULL) { | 27 sink_(NULL) { |
27 DCHECK(manager_); | 28 DCHECK(manager_); |
28 | 29 |
29 // Load the Avrt DLL if not already loaded. Required to support MMCSS. | 30 // Load the Avrt DLL if not already loaded. Required to support MMCSS. |
30 bool avrt_init = avrt::Initialize(); | 31 bool avrt_init = avrt::Initialize(); |
31 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; | 32 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; |
32 | 33 |
33 // Set up the desired capture format specified by the client. | 34 // Set up the desired capture format specified by the client. |
34 format_.nSamplesPerSec = params.sample_rate; | 35 format_.nSamplesPerSec = params.sample_rate; |
35 format_.wFormatTag = WAVE_FORMAT_PCM; | 36 format_.wFormatTag = WAVE_FORMAT_PCM; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 72 } |
72 } | 73 } |
73 | 74 |
74 WASAPIAudioInputStream::~WASAPIAudioInputStream() {} | 75 WASAPIAudioInputStream::~WASAPIAudioInputStream() {} |
75 | 76 |
76 bool WASAPIAudioInputStream::Open() { | 77 bool WASAPIAudioInputStream::Open() { |
77 // Verify that we are not already opened. | 78 // Verify that we are not already opened. |
78 if (opened_) | 79 if (opened_) |
79 return false; | 80 return false; |
80 | 81 |
81 // Obtain a reference to the IMMDevice interface of the default capturing | 82 // Obtain a reference to the IMMDevice interface of the capturing |
82 // device with the specified role. | 83 // device with the specified unique identifier or role which was |
83 HRESULT hr = SetCaptureDevice(device_role_); | 84 // set at construction. |
| 85 HRESULT hr = SetCaptureDevice(); |
84 if (FAILED(hr)) { | 86 if (FAILED(hr)) { |
85 return false; | 87 return false; |
86 } | 88 } |
87 | 89 |
88 // Obtain an IAudioClient interface which enables us to create and initialize | 90 // Obtain an IAudioClient interface which enables us to create and initialize |
89 // an audio stream between an audio application and the audio engine. | 91 // an audio stream between an audio application and the audio engine. |
90 hr = ActivateCaptureDevice(); | 92 hr = ActivateCaptureDevice(); |
91 if (FAILED(hr)) { | 93 if (FAILED(hr)) { |
92 return false; | 94 return false; |
93 } | 95 } |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 PLOG(WARNING) << "Failed to disable MMCSS"; | 374 PLOG(WARNING) << "Failed to disable MMCSS"; |
373 } | 375 } |
374 } | 376 } |
375 | 377 |
376 void WASAPIAudioInputStream::HandleError(HRESULT err) { | 378 void WASAPIAudioInputStream::HandleError(HRESULT err) { |
377 NOTREACHED() << "Error code: " << err; | 379 NOTREACHED() << "Error code: " << err; |
378 if (sink_) | 380 if (sink_) |
379 sink_->OnError(this, static_cast<int>(err)); | 381 sink_->OnError(this, static_cast<int>(err)); |
380 } | 382 } |
381 | 383 |
382 HRESULT WASAPIAudioInputStream::SetCaptureDevice(ERole device_role) { | 384 HRESULT WASAPIAudioInputStream::SetCaptureDevice() { |
383 ScopedComPtr<IMMDeviceEnumerator> enumerator; | 385 ScopedComPtr<IMMDeviceEnumerator> enumerator; |
384 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), | 386 HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), |
385 NULL, | 387 NULL, |
386 CLSCTX_INPROC_SERVER, | 388 CLSCTX_INPROC_SERVER, |
387 __uuidof(IMMDeviceEnumerator), | 389 __uuidof(IMMDeviceEnumerator), |
388 enumerator.ReceiveVoid()); | 390 enumerator.ReceiveVoid()); |
389 if (SUCCEEDED(hr)) { | 391 if (SUCCEEDED(hr)) { |
390 // Retrieve the default capture audio endpoint for the specified role. | 392 // Retrieve the IMMDevice by using the specified role or the specified |
391 // Note that, in Windows Vista, the MMDevice API supports device roles | 393 // unique endpoint device-identification string. |
392 // but the system-supplied user interface programs do not. | 394 // TODO(henrika): possibly add suport for the eCommunications as well. |
393 hr = enumerator->GetDefaultAudioEndpoint(eCapture, | 395 if (device_id_ == AudioManagerBase::kDefaultDeviceId) { |
394 device_role, | 396 // Retrieve the default capture audio endpoint for the specified role. |
395 endpoint_device_.Receive()); | 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 eConsole, |
| 401 endpoint_device_.Receive()); |
| 402 } else { |
| 403 // Retrieve a capture endpoint device that is specified by an endpoint |
| 404 // device-identification string. |
| 405 hr = enumerator->GetDevice(UTF8ToUTF16(device_id_).c_str(), |
| 406 endpoint_device_.Receive()); |
| 407 } |
396 | 408 |
397 // Verify that the audio endpoint device is active. That is, the audio | 409 if (FAILED(hr)) |
| 410 return hr; |
| 411 |
| 412 // Verify that the audio endpoint device is active, i.e., the audio |
398 // adapter that connects to the endpoint device is present and enabled. | 413 // adapter that connects to the endpoint device is present and enabled. |
399 DWORD state = DEVICE_STATE_DISABLED; | 414 DWORD state = DEVICE_STATE_DISABLED; |
400 hr = endpoint_device_->GetState(&state); | 415 hr = endpoint_device_->GetState(&state); |
401 if (SUCCEEDED(hr)) { | 416 if (SUCCEEDED(hr)) { |
402 if (!(state & DEVICE_STATE_ACTIVE)) { | 417 if (!(state & DEVICE_STATE_ACTIVE)) { |
403 DLOG(ERROR) << "Selected capture device is not active."; | 418 DLOG(ERROR) << "Selected capture device is not active."; |
404 hr = E_ACCESSDENIED; | 419 hr = E_ACCESSDENIED; |
405 } | 420 } |
406 } | 421 } |
407 } | 422 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get()); | 514 hr = audio_client_->SetEventHandle(audio_samples_ready_event_.Get()); |
500 if (FAILED(hr)) | 515 if (FAILED(hr)) |
501 return hr; | 516 return hr; |
502 | 517 |
503 // Get access to the IAudioCaptureClient interface. This interface | 518 // Get access to the IAudioCaptureClient interface. This interface |
504 // enables us to read input data from the capture endpoint buffer. | 519 // enables us to read input data from the capture endpoint buffer. |
505 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient), | 520 hr = audio_client_->GetService(__uuidof(IAudioCaptureClient), |
506 audio_capture_client_.ReceiveVoid()); | 521 audio_capture_client_.ReceiveVoid()); |
507 return hr; | 522 return hr; |
508 } | 523 } |
OLD | NEW |