summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cras/src/common/cras_types.h7
-rw-r--r--cras/src/server/cras_alsa_io.c1
-rw-r--r--cras/src/server/cras_system_state.c6
-rw-r--r--cras/src/tests/system_state_unittest.cc16
4 files changed, 28 insertions, 2 deletions
diff --git a/cras/src/common/cras_types.h b/cras/src/common/cras_types.h
index 12764db0..8fecbf13 100644
--- a/cras/src/common/cras_types.h
+++ b/cras/src/common/cras_types.h
@@ -254,6 +254,12 @@ struct __attribute__ ((__packed__)) audio_debug_info {
* mute_locked - 0 = unlocked, 1 = locked.
* suspended - 1 = suspended, 0 = resumed.
* capture_gain - Capture gain in dBFS * 100.
+ * capture_gain_target - Target capture gain in dBFS * 100. The actual
+ * capture gain will be subjected to current
+ * supported range. When active device/node changes,
+ * supported range changes accordingly. System state
+ * should try to re-apply target gain subjected to new
+ * range.
* capture_mute - 0 = unmuted, 1 = muted.
* capture_mute_locked - 0 = unlocked, 1 = locked.
* min_capture_gain - Min allowed capture gain in dBFS * 100.
@@ -290,6 +296,7 @@ struct __attribute__ ((packed, aligned(4))) cras_server_state {
int32_t mute_locked;
int32_t suspended;
int32_t capture_gain;
+ int32_t capture_gain_target;
int32_t capture_mute;
int32_t capture_mute_locked;
int32_t min_capture_gain;
diff --git a/cras/src/server/cras_alsa_io.c b/cras/src/server/cras_alsa_io.c
index fe4c1d35..c0434d6b 100644
--- a/cras/src/server/cras_alsa_io.c
+++ b/cras/src/server/cras_alsa_io.c
@@ -2126,6 +2126,7 @@ static int alsa_iodev_set_active_node(struct cras_iodev *iodev,
if (iodev->active_node == ionode) {
enable_active_ucm(aio, dev_enabled);
+ init_device_settings(aio);
return 0;
}
diff --git a/cras/src/server/cras_system_state.c b/cras/src/server/cras_system_state.c
index a51aacd6..3483ebcb 100644
--- a/cras/src/server/cras_system_state.c
+++ b/cras/src/server/cras_system_state.c
@@ -98,6 +98,7 @@ void cras_system_state_init(const char *device_config_dir)
exp_state->mute_locked = 0;
exp_state->suspended = 0;
exp_state->capture_gain = DEFAULT_CAPTURE_GAIN;
+ exp_state->capture_gain_target = DEFAULT_CAPTURE_GAIN;
exp_state->capture_mute = 0;
exp_state->capture_mute_locked = 0;
exp_state->min_volume_dBFS = DEFAULT_MIN_VOLUME_DBFS;
@@ -171,6 +172,7 @@ size_t cras_system_get_volume()
void cras_system_set_capture_gain(long gain)
{
/* Adjust targeted gain to be in supported range. */
+ state.exp_state->capture_gain_target = gain;
gain = MAX(gain, state.exp_state->min_capture_gain);
gain = MIN(gain, state.exp_state->max_capture_gain);
state.exp_state->capture_gain = gain;
@@ -301,8 +303,8 @@ void cras_system_set_capture_gain_limits(long min, long max)
{
state.exp_state->min_capture_gain = MAX(min, DEFAULT_MIN_CAPTURE_GAIN);
state.exp_state->max_capture_gain = max;
- /* Current gain needs to be in the supported range. */
- cras_system_set_capture_gain(state.exp_state->capture_gain);
+ /* Re-apply target gain subjected to the new supported range. */
+ cras_system_set_capture_gain(state.exp_state->capture_gain_target);
}
long cras_system_get_min_capture_gain()
diff --git a/cras/src/tests/system_state_unittest.cc b/cras/src/tests/system_state_unittest.cc
index 8a952110..5816b771 100644
--- a/cras/src/tests/system_state_unittest.cc
+++ b/cras/src/tests/system_state_unittest.cc
@@ -115,6 +115,22 @@ TEST(SystemStateSuite, SetCaptureVolume) {
EXPECT_EQ(3, cras_observer_notify_capture_gain_called);
}
+TEST(SystemStateSuite, SetCaptureVolumeStoreTarget) {
+ cras_system_state_init(device_config_dir);
+ cras_system_set_capture_gain_limits(-2000, 2000);
+ cras_system_set_capture_gain(3000);
+ // Gain is within the limit.
+ EXPECT_EQ(2000, cras_system_get_capture_gain());
+
+ // Assume the range is changed.
+ cras_system_set_capture_gain_limits(-4000, 4000);
+
+ // Gain is also changed because target gain is re-applied.
+ EXPECT_EQ(3000, cras_system_get_capture_gain());
+
+ cras_system_state_deinit();
+}
+
TEST(SystemStateSuite, SetMinMaxCaptureGain) {
cras_system_state_init(device_config_dir);
cras_system_set_capture_gain(3000);