Commit 400ece34 authored by Bailey Berro's avatar Bailey Berro Committed by Commit Bot

Create new pref to store input gain

This change creates a new pref, prefs::kAudioDevicesGainPercent, to
store the gain values for each of a user's input devices. A new pref is
created because the default value for input gain was changed to 50
rather than 75, and any users of mic gain before the switch will have
the old value stored in prefs rather than the new one.

Bug: b:158677629
Change-Id: I536b375dba02c00fa021688be09881eb59a81adf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2246231
Commit-Queue: Bailey Berro <baileyberro@chromium.org>
Reviewed-by: default avatarHsinyu Chao <hychao@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#778913}
parent 79f42287
......@@ -99,17 +99,26 @@ double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue(
if (!device)
return kDefaultOutputVolumePercent;
else
return GetVolumeGainPrefValue(*device);
return GetOutputVolumePrefValue(*device);
}
double AudioDevicesPrefHandlerImpl::GetInputGainValue(
const AudioDevice* device) {
DCHECK(device);
return GetVolumeGainPrefValue(*device);
return GetInputGainPrefValue(*device);
}
void AudioDevicesPrefHandlerImpl::SetVolumeGainValue(
const AudioDevice& device, double value) {
// TODO(baileyberro): Refactor public interface to use two explicit methods.
device.is_input ? SetInputGainPrefValue(device, value)
: SetOutputVolumePrefValue(device, value);
}
void AudioDevicesPrefHandlerImpl::SetOutputVolumePrefValue(
const AudioDevice& device,
double value) {
DCHECK(!device.is_input);
// Use this opportunity to remove device record under deprecated device ID,
// if one exists.
if (device.stable_device_id_version == 2) {
......@@ -121,6 +130,25 @@ void AudioDevicesPrefHandlerImpl::SetVolumeGainValue(
SaveDevicesVolumePref();
}
void AudioDevicesPrefHandlerImpl::SetInputGainPrefValue(
const AudioDevice& device,
double value) {
DCHECK(device.is_input);
const std::string device_id = GetDeviceIdString(device);
// Use this opportunity to remove input device record from
// |device_volume_settings_|.
// TODO(baileyberro): Remove this check in M94.
if (device_volume_settings_->HasKey(device_id)) {
device_volume_settings_->Remove(device_id, nullptr);
SaveDevicesVolumePref();
}
device_gain_settings_->SetDouble(device_id, value);
SaveDevicesGainPref();
}
bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) {
std::string device_id_str = GetDeviceIdString(device);
if (!device_mute_settings_->HasKey(device_id_str))
......@@ -148,7 +176,7 @@ void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device,
void AudioDevicesPrefHandlerImpl::SetDeviceActive(const AudioDevice& device,
bool active,
bool activate_by_user) {
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
auto dict = std::make_unique<base::DictionaryValue>();
dict->SetBoolean(kActiveKey, active);
if (active)
dict->SetBoolean(kActivateByUserKey, activate_by_user);
......@@ -206,8 +234,9 @@ void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver(
observers_.RemoveObserver(observer);
}
double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue(
double AudioDevicesPrefHandlerImpl::GetOutputVolumePrefValue(
const AudioDevice& device) {
DCHECK(!device.is_input);
std::string device_id_str = GetDeviceIdString(device);
if (!device_volume_settings_->HasKey(device_id_str))
MigrateDeviceVolumeGainSettings(device_id_str, device);
......@@ -218,6 +247,19 @@ double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue(
return value;
}
double AudioDevicesPrefHandlerImpl::GetInputGainPrefValue(
const AudioDevice& device) {
DCHECK(device.is_input);
std::string device_id_str = GetDeviceIdString(device);
if (!device_gain_settings_->HasKey(device_id_str))
SetInputGainPrefValue(device, kDefaultInputGainPercent);
double value;
device_gain_settings_->GetDouble(device_id_str, &value);
return value;
}
double AudioDevicesPrefHandlerImpl::GetDeviceDefaultOutputVolume(
const AudioDevice& device) {
if (device.type == AUDIO_TYPE_HDMI)
......@@ -228,14 +270,16 @@ double AudioDevicesPrefHandlerImpl::GetDeviceDefaultOutputVolume(
AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl(
PrefService* local_state)
: device_mute_settings_(new base::DictionaryValue()),
device_volume_settings_(new base::DictionaryValue()),
device_state_settings_(new base::DictionaryValue()),
: device_mute_settings_(std::make_unique<base::DictionaryValue>()),
device_volume_settings_(std::make_unique<base::DictionaryValue>()),
device_gain_settings_(std::make_unique<base::DictionaryValue>()),
device_state_settings_(std::make_unique<base::DictionaryValue>()),
local_state_(local_state) {
InitializePrefObservers();
LoadDevicesMutePref();
LoadDevicesVolumePref();
LoadDevicesGainPref();
LoadDevicesStatePref();
}
......@@ -276,6 +320,20 @@ void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() {
dict_update->MergeDictionary(device_volume_settings_.get());
}
void AudioDevicesPrefHandlerImpl::LoadDevicesGainPref() {
const base::DictionaryValue* gain_prefs =
local_state_->GetDictionary(prefs::kAudioDevicesGainPercent);
if (gain_prefs)
device_gain_settings_.reset(gain_prefs->DeepCopy());
}
void AudioDevicesPrefHandlerImpl::SaveDevicesGainPref() {
DictionaryPrefUpdate dict_update(local_state_,
prefs::kAudioDevicesGainPercent);
dict_update->Clear();
dict_update->MergeDictionary(device_gain_settings_.get());
}
void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() {
const base::DictionaryValue* state_prefs =
local_state_->GetDictionary(prefs::kAudioDevicesState);
......@@ -317,14 +375,12 @@ void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings(
void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeGainSettings(
const std::string& device_key,
const AudioDevice& device) {
DCHECK(!device.is_input);
if (!MigrateDeviceIdInSettings(device_volume_settings_.get(), device_key,
device)) {
// If there was no recorded value for deprecated device ID, use value from
// global vloume pref. For input devices, use the DefaultInputGainPercent
// rather than the kAudioVolumePercent.
double old_volume =
device.is_input ? kDefaultInputGainPercent
: local_state_->GetDouble(prefs::kAudioVolumePercent);
// global vloume pref.
double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent);
device_volume_settings_->SetDouble(device_key, old_volume);
}
SaveDevicesVolumePref();
......@@ -338,6 +394,7 @@ void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() {
// static
void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent);
registry->RegisterDictionaryPref(prefs::kAudioDevicesGainPercent);
registry->RegisterDictionaryPref(prefs::kAudioDevicesMute);
registry->RegisterDictionaryPref(prefs::kAudioDevicesState);
......
......@@ -62,6 +62,10 @@ class COMPONENT_EXPORT(CHROMEOS_AUDIO) AudioDevicesPrefHandlerImpl
void LoadDevicesMutePref();
void SaveDevicesMutePref();
// Load and save methods for the gain preferences for all devices.
void LoadDevicesGainPref();
void SaveDevicesGainPref();
// Load and save methods for the volume preferences for all devices.
void LoadDevicesVolumePref();
void SaveDevicesVolumePref();
......@@ -70,9 +74,13 @@ class COMPONENT_EXPORT(CHROMEOS_AUDIO) AudioDevicesPrefHandlerImpl
void LoadDevicesStatePref();
void SaveDevicesStatePref();
double GetVolumeGainPrefValue(const AudioDevice& device);
double GetOutputVolumePrefValue(const AudioDevice& device);
double GetInputGainPrefValue(const AudioDevice& device);
double GetDeviceDefaultOutputVolume(const AudioDevice& device);
void SetOutputVolumePrefValue(const AudioDevice& device, double value);
void SetInputGainPrefValue(const AudioDevice& device, double value);
// Migrates devices state pref for an audio device. Device settings are
// saved under device stable device ID - this method migrates device state
// for a device that is saved under key derived from v1 stable device ID to
......@@ -102,6 +110,7 @@ class COMPONENT_EXPORT(CHROMEOS_AUDIO) AudioDevicesPrefHandlerImpl
std::unique_ptr<base::DictionaryValue> device_mute_settings_;
std::unique_ptr<base::DictionaryValue> device_volume_settings_;
std::unique_ptr<base::DictionaryValue> device_gain_settings_;
std::unique_ptr<base::DictionaryValue> device_state_settings_;
PrefService* local_state_; // not owned
......
......@@ -137,32 +137,33 @@ class AudioDevicesPrefHandlerTest : public testing::TestWithParam<bool> {
}
AudioDevice GetDeviceWithVersion(int version) {
return CreateAudioDevice(GetParam() ? kInternalMic : kHeadphone, version);
return CreateAudioDevice(IsInputTest() ? kInternalMic : kHeadphone,
version);
}
std::string GetPresetDeviceDeprecatedPrefKey() {
return GetParam() ? kPresetInputDeprecatedPrefKey
: kPresetOutputDeprecatedPrefKey;
return IsInputTest() ? kPresetInputDeprecatedPrefKey
: kPresetOutputDeprecatedPrefKey;
}
AudioDevice GetPresetDeviceWithVersion(int version) {
return CreateAudioDevice(GetParam() ? kPresetInput : kPresetOutput,
return CreateAudioDevice(IsInputTest() ? kPresetInput : kPresetOutput,
version);
}
AudioDevice GetSecondaryDeviceWithVersion(int version) {
return CreateAudioDevice(GetParam() ? kUSBMic : kHDMIOutput, version);
return CreateAudioDevice(IsInputTest() ? kUSBMic : kHDMIOutput, version);
}
AudioDevice GetDeviceWithSpecialCharactersWithVersion(int version) {
return CreateAudioDevice(GetParam() ? kInputDeviceWithSpecialCharacters
: kOutputDeviceWithSpecialCharacters,
return CreateAudioDevice(IsInputTest() ? kInputDeviceWithSpecialCharacters
: kOutputDeviceWithSpecialCharacters,
version);
}
double GetSoundLevelValue(const AudioDevice& device) {
return GetParam() ? audio_pref_handler_->GetInputGainValue(&device)
: audio_pref_handler_->GetOutputVolumeValue(&device);
return IsInputTest() ? audio_pref_handler_->GetInputGainValue(&device)
: audio_pref_handler_->GetOutputVolumeValue(&device);
}
void SetSoundLevelValue(const AudioDevice& device, double value) {
......@@ -202,10 +203,12 @@ class AudioDevicesPrefHandlerTest : public testing::TestWithParam<bool> {
}
double GetDefaultSoundLevelValue() {
return GetParam() ? AudioDevicesPrefHandler::kDefaultInputGainPercent
: AudioDevicesPrefHandler::kDefaultOutputVolumePercent;
return IsInputTest() ? AudioDevicesPrefHandler::kDefaultInputGainPercent
: AudioDevicesPrefHandler::kDefaultOutputVolumePercent;
}
bool IsInputTest() const { return GetParam(); }
scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
......@@ -265,6 +268,11 @@ TEST_P(AudioDevicesPrefHandlerTest, SoundLevel) {
}
TEST_P(AudioDevicesPrefHandlerTest, SoundLevelMigratedFromV1StableId) {
if (IsInputTest()) {
// Input gain does not need to be migrated since a new pref is used.
return;
}
AudioDevice device_v1 = GetPresetDeviceWithVersion(1);
AudioDevice device_v2 = GetPresetDeviceWithVersion(2);
......@@ -285,6 +293,10 @@ TEST_P(AudioDevicesPrefHandlerTest, SoundLevelMigratedFromV1StableId) {
}
TEST_P(AudioDevicesPrefHandlerTest, SettingV2DeviceSoundLevelRemovesV1Entry) {
if (IsInputTest()) {
// Input gain does not need to be migrated since a new pref is used.
return;
}
AudioDevice device_v1 = GetDeviceWithVersion(1);
AudioDevice device_v2 = GetDeviceWithVersion(2);
......@@ -302,7 +314,7 @@ TEST_P(AudioDevicesPrefHandlerTest, SettingV2DeviceSoundLevelRemovesV1Entry) {
}
TEST_P(AudioDevicesPrefHandlerTest, MigrateFromGlobalSoundLevelPref) {
if (GetParam()) {
if (IsInputTest()) {
// For any new input devices, the default volume is set from a constant
// rather than the default kAudioVolumePercent.
return;
......
......@@ -19,8 +19,12 @@ const char kAssistPersonalInfoEnabled[] =
// audio devices.
const char kAudioDevicesMute[] = "settings.audio.devices.mute";
// A dictionary pref storing the gain settings for all the currently known
// audio input devices.
const char kAudioDevicesGainPercent[] = "settings.audio.devices.gain_percent";
// A dictionary pref storing the volume settings for all the currently known
// audio devices.
// audio output devices.
const char kAudioDevicesVolumePercent[] =
"settings.audio.devices.volume_percent";
......
......@@ -16,6 +16,8 @@ COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const char kAssistPersonalInfoEnabled[];
COMPONENT_EXPORT(CHROMEOS_CONSTANTS) extern const char kAudioDevicesMute[];
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const char kAudioDevicesGainPercent[];
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const char kAudioDevicesVolumePercent[];
COMPONENT_EXPORT(CHROMEOS_CONSTANTS) extern const char kAudioMute[];
COMPONENT_EXPORT(CHROMEOS_CONSTANTS) extern const char kAudioOutputAllowed[];
......
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