Commit 7e77e368 authored by Jenny Zhang's avatar Jenny Zhang Committed by Commit Bot

Fix the issue of usb headphone is not active when plugged in after rebooting.

Bug: 767245
Change-Id: I059d4e783510b9dd3b5db5e8cfd7312e9cd43146
Reviewed-on: https://chromium-review.googlesource.com/679934Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarJenny Zhang <jennyz@chromium.org>
Commit-Queue: Jenny Zhang <jennyz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504429}
parent 1a04551f
...@@ -1310,9 +1310,42 @@ void CrasAudioHandler::HandleHotPlugDevice( ...@@ -1310,9 +1310,42 @@ void CrasAudioHandler::HandleHotPlugDevice(
SwitchToDevice(hotplug_device, true, ACTIVATE_BY_RESTORE_PREVIOUS_STATE); SwitchToDevice(hotplug_device, true, ACTIVATE_BY_RESTORE_PREVIOUS_STATE);
} else { } else {
// Do not active the device if its previous state is inactive. // The hot plugged device was not active last time it was plugged in.
VLOG(1) << "Hotplug device remains inactive as its previous state:" // Let's check how the current active device is activated, if it is not
<< hotplug_device.ToString(); // activated by user choice, then select the hot plugged device it is of
// higher priority.
uint64_t& active_node_id = hotplug_device.is_input ? active_input_node_id_
: active_output_node_id_;
const AudioDevice* active_device = GetDeviceFromId(active_node_id);
if (!active_device) {
// Can't find any current active device.
// This is an odd case, but if it happens, switch to hotplug device.
LOG(ERROR) << "Can not find current active device when the device is"
<< " hot plugged: " << hotplug_device.ToString();
SwitchToDevice(hotplug_device, true, ACTIVATE_BY_PRIORITY);
return;
}
bool activate_by_user = false;
bool state_active = false;
bool found_active_state = audio_pref_handler_->GetDeviceActive(
*active_device, &state_active, &activate_by_user);
DCHECK(found_active_state && state_active);
if (!found_active_state || !state_active) {
LOG(ERROR) << "Cannot retrieve current active device's state in prefs: "
<< active_device->ToString();
return;
}
if (!activate_by_user &&
device_priority_queue.top().id == hotplug_device.id) {
SwitchToDevice(hotplug_device, true, ACTIVATE_BY_PRIORITY);
} else {
// Do not active the hotplug device. Either the current device is
// expliciltly activated by user, or the hotplug device is of lower
// priority.
VLOG(1) << "Hotplug device remains inactive as its previous state:"
<< hotplug_device.ToString();
}
} }
} }
......
...@@ -4155,10 +4155,48 @@ TEST_P(CrasAudioHandlerTest, ...@@ -4155,10 +4155,48 @@ TEST_P(CrasAudioHandlerTest,
cras_audio_handler_->GetAudioDevices(&audio_devices); cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size()); EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
// Verify the active input device is unchanged. // Verify the active input device is changed to usb mic 2.
EXPECT_EQ(0, test_observer_->active_input_node_changed_count()); EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId1, cras_audio_handler_->GetPrimaryActiveInputNode()); EXPECT_EQ(kUSBMicId2, cras_audio_handler_->GetPrimaryActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
} }
TEST_P(CrasAudioHandlerTest, PlugInUSBHeadphoneAfterLastUnplugNotActive) {
// Set up initial audio devices.
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(3u, audio_devices.size());
// 35mm Headphone is active, but USB headphone is not.
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
// Unplug both 35mm headphone and USB headphone.
audio_nodes.clear();
audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
ChangeAudioNodes(audio_nodes);
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(1u, audio_devices.size());
// Internal speaker is active.
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
// Plug in USB headphone.
audio_nodes.clear();
AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
internal_speaker.active = true;
audio_nodes.push_back(internal_speaker);
AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
usb_headphone.plugged_time = 80000000;
audio_nodes.push_back(usb_headphone);
ChangeAudioNodes(audio_nodes);
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(2u, audio_devices.size());
// USB headphone is active.
EXPECT_EQ(kUSBHeadphone1->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
}
} // namespace chromeos } // namespace chromeos
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