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

Unified Diff: media/audio/cras/audio_manager_cras.cc

Issue 1275783003: Add a virtual beamforming audio device on ChromeOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use gfx::Point3F. Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: media/audio/cras/audio_manager_cras.cc
diff --git a/media/audio/cras/audio_manager_cras.cc b/media/audio/cras/audio_manager_cras.cc
index 31a1780330096be8b663b0cab4687a33bfa75240..683010e05d812943b0adb80985ea6ec2c7512e8b 100644
--- a/media/audio/cras/audio_manager_cras.cc
+++ b/media/audio/cras/audio_manager_cras.cc
@@ -23,28 +23,56 @@
#undef max
namespace media {
+namespace {
-static void AddDefaultDevice(AudioDeviceNames* device_names) {
- DCHECK(device_names->empty());
+// Maximum number of output streams that can be open simultaneously.
+const int kMaxOutputStreams = 50;
+
+// Default sample rate for input and output streams.
+const int kDefaultSampleRate = 48000;
+
+// Define bounds for the output buffer size.
+const int kMinimumOutputBufferSize = 512;
+const int kMaximumOutputBufferSize = 8192;
+// Default input buffer size.
+const int kDefaultInputBufferSize = 1024;
+
+void AddDefaultDevice(AudioDeviceNames* device_names) {
// Cras will route audio from a proper physical device automatically.
device_names->push_back(
AudioDeviceName(AudioManagerBase::kDefaultDeviceName,
AudioManagerBase::kDefaultDeviceId));
}
-// Maximum number of output streams that can be open simultaneously.
-static const int kMaxOutputStreams = 50;
+// Returns a mic positions string if the machine has a beamforming capable
+// internal mic and otherwise an empty string.
+std::string MicPositions() {
+ // Get the list of devices from CRAS. An internal mic with a non-empty
+ // positions field indicates the machine has a beamforming capable mic array.
+ chromeos::AudioDeviceList devices;
+ chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
+ for (const auto& device : devices) {
+ if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) {
+ // There should be only one internal mic device.
+ return device.mic_positions;
+ }
+ }
+ return "";
+}
-// Default sample rate for input and output streams.
-static const int kDefaultSampleRate = 48000;
+// Returns the name and ID of the virtual beamforming device.
+AudioDeviceName BeamformingDeviceName() {
+ static const char kBeamformingDeviceNameSuffix[] = " with beamforming";
+ static const char kBeamformingDeviceIdSuffix[] = "-beamforming";
-// Define bounds for the output buffer size.
-static const int kMinimumOutputBufferSize = 512;
-static const int kMaximumOutputBufferSize = 8192;
+ return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) +
+ kBeamformingDeviceNameSuffix,
+ std::string(AudioManagerBase::kDefaultDeviceId) +
+ kBeamformingDeviceIdSuffix);
+}
-// Default input buffer size.
-static const int kDefaultInputBufferSize = 1024;
+} // namespace
bool AudioManagerCras::HasAudioOutputDevices() {
return true;
@@ -62,7 +90,8 @@ bool AudioManagerCras::HasAudioInputDevices() {
AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory)
: AudioManagerBase(audio_log_factory),
- has_keyboard_mic_(false) {
+ has_keyboard_mic_(false),
+ beamforming_device_name_(BeamformingDeviceName()) {
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
}
@@ -76,6 +105,13 @@ void AudioManagerCras::ShowAudioInputSettings() {
void AudioManagerCras::GetAudioInputDeviceNames(
AudioDeviceNames* device_names) {
+ mic_positions_ = ParsePointsFromString(MicPositions());
+ // At least two mic positions indicates we have a beamforming capable mic
+ // array. Add the virtual beamforming device to the list. When this device is
+ // queried through GetInputStreamParameters, provide the cached mic positions.
+ if (mic_positions_.size() > 1)
+ device_names->push_back(beamforming_device_name_);
+
AddDefaultDevice(device_names);
}
@@ -95,12 +131,18 @@ AudioParameters AudioManagerCras::GetInputStreamParameters(
has_keyboard_mic_ ? AudioParameters::KEYBOARD_MIC
: AudioParameters::NO_EFFECTS;
+ // Return the cached mic positions in the case of the beamforming device.
+ const std::vector<Point>& mic_positions =
+ device_id == beamforming_device_name_.unique_id ? mic_positions_
+ : std::vector<Point>();
+
// TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal
// parameters for the loopback stream may differ from the default.
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16,
buffer_size);
params.set_effects(effects);
+ params.set_mic_positions(mic_positions);
return params;
}

Powered by Google App Engine
This is Rietveld 408576698