Commit 7dc0f0a2 authored by Xiaohui Chen's avatar Xiaohui Chen Committed by Commit Bot

Refactor hotword device detection

Currently there are code duplication that does similar things to detect
hotword input device. Now we move all the logic into cras_audio_handler
to simplify the code.

Bug: None
Test: locally build and run
Change-Id: I08df6cef18a17dd3c5b2eff6f5b4193aecf0b3e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1686951Reviewed-by: default avatarJenny Zhang <jennyz@chromium.org>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Commit-Queue: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#675764}
parent a0d64f3d
......@@ -4,6 +4,8 @@
#include "chrome/browser/ui/webui/chromeos/assistant_optin/assistant_optin_utils.h"
#include <utility>
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/consent_auditor/consent_auditor_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
......@@ -266,16 +268,7 @@ void RecordActivityControlConsent(Profile* profile,
}
bool IsHotwordDspAvailable() {
chromeos::AudioDeviceList devices;
chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
for (const chromeos::AudioDevice& device : devices) {
if (device.type == chromeos::AUDIO_TYPE_HOTWORD) {
VLOG(1) << "Hotword dsp device detected.";
return true;
}
}
VLOG(1) << "No hotword dsp device detected.";
return false;
return chromeos::CrasAudioHandler::Get()->HasHotwordDevice();
}
bool IsVoiceMatchEnabled(const PrefService* prefs) {
......
......@@ -10,7 +10,7 @@
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
......@@ -20,6 +20,7 @@
#include "base/system/sys_info.h"
#include "base/system/system_monitor.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromeos/audio/audio_device.h"
#include "chromeos/audio/audio_devices_pref_handler_stub.h"
#include "services/media_session/public/mojom/constants.mojom.h"
#include "services/media_session/public/mojom/media_controller.mojom.h"
......@@ -242,6 +243,10 @@ bool CrasAudioHandler::HasKeyboardMic() {
return GetKeyboardMic() != nullptr;
}
bool CrasAudioHandler::HasHotwordDevice() {
return GetHotwordDevice() != nullptr;
}
bool CrasAudioHandler::IsOutputMuted() {
return output_mute_on_;
}
......@@ -788,6 +793,15 @@ const AudioDevice* CrasAudioHandler::GetKeyboardMic() const {
return nullptr;
}
const AudioDevice* CrasAudioHandler::GetHotwordDevice() const {
for (const auto& item : audio_devices_) {
const AudioDevice& device = item.second;
if (device.is_input && device.type == AUDIO_TYPE_HOTWORD)
return &device;
}
return nullptr;
}
void CrasAudioHandler::SetupAudioInputState() {
// Set the initial audio state to the ones read from audio prefs.
const AudioDevice* device = GetDeviceFromId(active_input_node_id_);
......
......@@ -9,6 +9,7 @@
#include <stdint.h>
#include <queue>
#include <string>
#include <vector>
#include "base/component_export.h"
......@@ -132,6 +133,9 @@ class COMPONENT_EXPORT(CHROMEOS_AUDIO) CrasAudioHandler
// Returns true if keyboard mic exists.
bool HasKeyboardMic();
// Returns true if hotword input device exists.
bool HasHotwordDevice();
// Returns true if audio output is muted for the system.
bool IsOutputMuted();
......@@ -351,6 +355,8 @@ class COMPONENT_EXPORT(CHROMEOS_AUDIO) CrasAudioHandler
uint64_t stable_device_id) const;
const AudioDevice* GetKeyboardMic() const;
const AudioDevice* GetHotwordDevice() const;
// Initializes audio state, which should only be called when CrasAudioHandler
// is created or cras audio client is restarted.
void InitializeAudioState();
......
......@@ -98,14 +98,7 @@ void Service::RequestAccessToken() {
}
bool Service::ShouldEnableHotword() {
bool dsp_available = false;
chromeos::AudioDeviceList devices;
chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
for (const chromeos::AudioDevice& device : devices) {
if (device.type == chromeos::AUDIO_TYPE_HOTWORD) {
dsp_available = true;
}
}
bool dsp_available = chromeos::CrasAudioHandler::Get()->HasHotwordDevice();
// Disable hotword if hotword is not set to always on and power source is not
// connected.
......
......@@ -134,15 +134,9 @@ void EnableHotwordEffect(const StreamControls& controls, int* effects) {
DCHECK(effects);
if (controls.hotword_enabled) {
#if defined(OS_CHROMEOS)
chromeos::AudioDeviceList devices;
chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
// Only enable if a hotword device exists.
for (const chromeos::AudioDevice& device : devices) {
if (device.type == chromeos::AUDIO_TYPE_HOTWORD) {
DCHECK(device.is_input);
if (chromeos::CrasAudioHandler::Get()->HasHotwordDevice())
*effects |= media::AudioParameters::HOTWORD;
}
}
#endif
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment