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" |
11 #include "media/audio/win/avrt_wrapper_win.h" | 11 #include "media/audio/win/avrt_wrapper_win.h" |
| 12 #include "media/base/audio_bus.h" |
12 | 13 |
13 using base::win::ScopedComPtr; | 14 using base::win::ScopedComPtr; |
14 using base::win::ScopedCOMInitializer; | 15 using base::win::ScopedCOMInitializer; |
15 | 16 |
16 namespace media { | 17 namespace media { |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Returns true if |device| represents the default communication capture device. | 20 // Returns true if |device| represents the default communication capture device. |
20 bool IsDefaultCommunicationDevice(IMMDeviceEnumerator* enumerator, | 21 bool IsDefaultCommunicationDevice(IMMDeviceEnumerator* enumerator, |
21 IMMDevice* device) { | 22 IMMDevice* device) { |
22 ScopedComPtr<IMMDevice> communications; | 23 ScopedComPtr<IMMDevice> communications; |
23 if (FAILED(enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, | 24 if (FAILED(enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, |
24 communications.Receive()))) { | 25 communications.Receive()))) { |
25 return false; | 26 return false; |
26 } | 27 } |
27 | 28 |
28 base::win::ScopedCoMem<WCHAR> communications_id, device_id; | 29 base::win::ScopedCoMem<WCHAR> communications_id, device_id; |
29 device->GetId(&device_id); | 30 device->GetId(&device_id); |
30 communications->GetId(&communications_id); | 31 communications->GetId(&communications_id); |
31 return lstrcmpW(communications_id, device_id) == 0; | 32 return lstrcmpW(communications_id, device_id) == 0; |
32 } | 33 } |
33 | 34 |
34 } // namespace | 35 } // namespace |
35 | 36 |
36 WASAPIAudioInputStream::WASAPIAudioInputStream( | 37 WASAPIAudioInputStream::WASAPIAudioInputStream(AudioManagerWin* manager, |
37 AudioManagerWin* manager, | 38 const AudioParameters& params, |
38 const AudioParameters& params, | 39 const std::string& device_id) |
39 const std::string& device_id) | |
40 : manager_(manager), | 40 : manager_(manager), |
41 capture_thread_(NULL), | 41 capture_thread_(NULL), |
42 opened_(false), | 42 opened_(false), |
43 started_(false), | 43 started_(false), |
44 frame_size_(0), | 44 frame_size_(0), |
45 packet_size_frames_(0), | 45 packet_size_frames_(0), |
46 packet_size_bytes_(0), | 46 packet_size_bytes_(0), |
47 endpoint_buffer_size_frames_(0), | 47 endpoint_buffer_size_frames_(0), |
48 effects_(params.effects()), | 48 effects_(params.effects()), |
49 device_id_(device_id), | 49 device_id_(device_id), |
50 perf_count_to_100ns_units_(0.0), | 50 perf_count_to_100ns_units_(0.0), |
51 ms_to_frame_count_(0.0), | 51 ms_to_frame_count_(0.0), |
52 sink_(NULL) { | 52 sink_(NULL), |
| 53 audio_bus_(media::AudioBus::Create(params)) { |
53 DCHECK(manager_); | 54 DCHECK(manager_); |
54 | 55 |
55 // Load the Avrt DLL if not already loaded. Required to support MMCSS. | 56 // Load the Avrt DLL if not already loaded. Required to support MMCSS. |
56 bool avrt_init = avrt::Initialize(); | 57 bool avrt_init = avrt::Initialize(); |
57 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; | 58 DCHECK(avrt_init) << "Failed to load the Avrt.dll"; |
58 | 59 |
59 // Set up the desired capture format specified by the client. | 60 // Set up the desired capture format specified by the client. |
60 format_.nSamplesPerSec = params.sample_rate(); | 61 format_.nSamplesPerSec = params.sample_rate(); |
61 format_.wFormatTag = WAVE_FORMAT_PCM; | 62 format_.wFormatTag = WAVE_FORMAT_PCM; |
62 format_.wBitsPerSample = params.bits_per_sample(); | 63 format_.wBitsPerSample = params.bits_per_sample(); |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 | 430 |
430 // Get a cached AGC volume level which is updated once every second | 431 // Get a cached AGC volume level which is updated once every second |
431 // on the audio manager thread. Note that, |volume| is also updated | 432 // on the audio manager thread. Note that, |volume| is also updated |
432 // each time SetVolume() is called through IPC by the render-side AGC. | 433 // each time SetVolume() is called through IPC by the render-side AGC. |
433 GetAgcVolume(&volume); | 434 GetAgcVolume(&volume); |
434 | 435 |
435 // Deliver captured data to the registered consumer using a packet | 436 // Deliver captured data to the registered consumer using a packet |
436 // size which was specified at construction. | 437 // size which was specified at construction. |
437 uint32 delay_frames = static_cast<uint32>(audio_delay_frames + 0.5); | 438 uint32 delay_frames = static_cast<uint32>(audio_delay_frames + 0.5); |
438 while (buffer_frame_index >= packet_size_frames_) { | 439 while (buffer_frame_index >= packet_size_frames_) { |
439 uint8* audio_data = | 440 // Copy data to audio bus to match the OnData interface. |
440 reinterpret_cast<uint8*>(capture_buffer.get()); | 441 uint8* audio_data = reinterpret_cast<uint8*>(capture_buffer.get()); |
| 442 audio_bus_->FromInterleaved( |
| 443 audio_data, audio_bus_->frames(), format_.wBitsPerSample / 8); |
441 | 444 |
442 // Deliver data packet, delay estimation and volume level to | 445 // Deliver data packet, delay estimation and volume level to |
443 // the user. | 446 // the user. |
444 sink_->OnData(this, | 447 sink_->OnData( |
445 audio_data, | 448 this, audio_bus_.get(), delay_frames * frame_size_, volume); |
446 packet_size_bytes_, | |
447 delay_frames * frame_size_, | |
448 volume); | |
449 | 449 |
450 // Store parts of the recorded data which can't be delivered | 450 // Store parts of the recorded data which can't be delivered |
451 // using the current packet size. The stored section will be used | 451 // using the current packet size. The stored section will be used |
452 // either in the next while-loop iteration or in the next | 452 // either in the next while-loop iteration or in the next |
453 // capture event. | 453 // capture event. |
454 memmove(&capture_buffer[0], | 454 memmove(&capture_buffer[0], |
455 &capture_buffer[packet_size_bytes_], | 455 &capture_buffer[packet_size_bytes_], |
456 (buffer_frame_index - packet_size_frames_) * frame_size_); | 456 (buffer_frame_index - packet_size_frames_) * frame_size_); |
457 | 457 |
458 buffer_frame_index -= packet_size_frames_; | 458 buffer_frame_index -= packet_size_frames_; |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 return hr; | 731 return hr; |
732 | 732 |
733 // Obtain a reference to the ISimpleAudioVolume interface which enables | 733 // Obtain a reference to the ISimpleAudioVolume interface which enables |
734 // us to control the master volume level of an audio session. | 734 // us to control the master volume level of an audio session. |
735 hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume), | 735 hr = audio_client_->GetService(__uuidof(ISimpleAudioVolume), |
736 simple_audio_volume_.ReceiveVoid()); | 736 simple_audio_volume_.ReceiveVoid()); |
737 return hr; | 737 return hr; |
738 } | 738 } |
739 | 739 |
740 } // namespace media | 740 } // namespace media |
OLD | NEW |