Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chromeos / audio / cras_audio_handler.cc
index de6313f..295f280 100644 (file)
@@ -104,6 +104,10 @@ void CrasAudioHandler::RemoveAudioObserver(AudioObserver* observer) {
   observers_.RemoveObserver(observer);
 }
 
+bool CrasAudioHandler::HasKeyboardMic() {
+  return GetKeyboardMic() != NULL;
+}
+
 bool CrasAudioHandler::IsOutputMuted() {
   return output_mute_on_;
 }
@@ -112,10 +116,11 @@ bool CrasAudioHandler::IsOutputMutedForDevice(uint64 device_id) {
   const AudioDevice* device = GetDeviceFromId(device_id);
   if (!device)
     return false;
+  DCHECK(!device->is_input);
   return audio_pref_handler_->GetMuteValue(*device);
 }
 
-bool CrasAudioHandler::IsOutputVolumeBelowDefaultMuteLvel() {
+bool CrasAudioHandler::IsOutputVolumeBelowDefaultMuteLevel() {
   return output_volume_ <= kMuteThresholdPercent;
 }
 
@@ -127,7 +132,16 @@ bool CrasAudioHandler::IsInputMutedForDevice(uint64 device_id) {
   const AudioDevice* device = GetDeviceFromId(device_id);
   if (!device)
     return false;
-  return audio_pref_handler_->GetMuteValue(*device);
+  DCHECK(device->is_input);
+  // We don't record input mute state for each device in the prefs,
+  // for any non-active input device, we assume mute is off.
+  if (device->id == active_input_node_id_)
+    return input_mute_on_;
+  return false;
+}
+
+int CrasAudioHandler::GetOutputDefaultVolumeMuteThreshold() {
+  return kMuteThresholdPercent;
 }
 
 int CrasAudioHandler::GetOutputVolumePercent() {
@@ -179,6 +193,19 @@ bool CrasAudioHandler::GetActiveOutputDevice(AudioDevice* device) const {
   return true;
 }
 
+void CrasAudioHandler::SetKeyboardMicActive(bool active) {
+  const AudioDevice* keyboard_mic = GetKeyboardMic();
+  if (!keyboard_mic)
+    return;
+  if (active) {
+    chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
+        AddActiveInputNode(keyboard_mic->id);
+  } else {
+    chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
+        RemoveActiveInputNode(keyboard_mic->id);
+  }
+}
+
 bool CrasAudioHandler::has_alternative_input() const {
   return has_alternative_input_;
 }
@@ -221,8 +248,10 @@ void CrasAudioHandler::SetOutputMute(bool mute_on) {
   if (!SetOutputMuteInternal(mute_on))
     return;
 
-  if (const AudioDevice* device = GetDeviceFromId(active_output_node_id_))
+  if (const AudioDevice* device = GetDeviceFromId(active_output_node_id_)) {
+    DCHECK(!device->is_input);
     audio_pref_handler_->SetMuteValue(*device, output_mute_on_);
+  }
 
   FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged());
 }
@@ -239,11 +268,6 @@ void CrasAudioHandler::SetInputMute(bool mute_on) {
   if (!SetInputMuteInternal(mute_on))
     return;
 
-  AudioDevice device;
-  if (const AudioDevice* device = GetDeviceFromId(active_input_node_id_))
-    audio_pref_handler_->SetMuteValue(*device, input_mute_on_);
-
-
   FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged());
 }
 
@@ -284,12 +308,15 @@ void CrasAudioHandler::SetMuteForDevice(uint64 device_id, bool mute_on) {
     SetOutputMute(mute_on);
     return;
   } else if (device_id == active_input_node_id_) {
+    VLOG(1) << "SetMuteForDevice sets active input device id="
+            << "0x" << std::hex << device_id << " mute=" << mute_on;
     SetInputMute(mute_on);
     return;
   }
 
-  AudioDevice device;
-  if (const AudioDevice* device = GetDeviceFromId(device_id))
+  const AudioDevice* device = GetDeviceFromId(device_id);
+  // Input device's mute state is not recorded in the pref. crbug.com/365050.
+  if (device && !device->is_input)
     audio_pref_handler_->SetMuteValue(*device, mute_on);
 }
 
@@ -401,6 +428,15 @@ const AudioDevice* CrasAudioHandler::GetDeviceFromId(uint64 device_id) const {
   return &(it->second);
 }
 
+const AudioDevice* CrasAudioHandler::GetKeyboardMic() const {
+  for (AudioDeviceMap::const_iterator it = audio_devices_.begin();
+       it != audio_devices_.end(); it++) {
+    if (it->second.is_input && it->second.type == AUDIO_TYPE_KEYBOARD_MIC)
+      return &(it->second);
+  }
+  return NULL;
+}
+
 void CrasAudioHandler::SetupAudioInputState() {
   // Set the initial audio state to the ones read from audio prefs.
   const AudioDevice* device = GetDeviceFromId(active_input_node_id_);
@@ -410,8 +446,9 @@ void CrasAudioHandler::SetupAudioInputState() {
         << "0x" << std::hex << active_input_node_id_;
     return;
   }
-  input_mute_on_ = audio_pref_handler_->GetMuteValue(*device);
   input_gain_ = audio_pref_handler_->GetInputGainValue(device);
+  VLOG(1) << "SetupAudioInputState for active device id="
+          << "0x" << std::hex << device->id << " mute=" << input_mute_on_;
   SetInputMuteInternal(input_mute_on_);
   // TODO(rkc,jennyz): Set input gain once we decide on how to store
   // the gain values since the range and step are both device specific.
@@ -425,6 +462,7 @@ void CrasAudioHandler::SetupAudioOutputState() {
         << "0x" << std::hex << active_output_node_id_;
     return;
   }
+  DCHECK(!device->is_input);
   output_mute_on_ = audio_pref_handler_->GetMuteValue(*device);
   output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device);
 
@@ -452,12 +490,13 @@ void CrasAudioHandler::ApplyAudioPolicy() {
 
   input_mute_locked_ = false;
   if (audio_pref_handler_->GetAudioCaptureAllowedValue()) {
-    // Set input mute if we have discovered active input device.
-    const AudioDevice* device = GetDeviceFromId(active_input_node_id_);
-    if (device)
-      SetInputMuteInternal(false);
+    VLOG(1) << "Audio input allowed by policy, sets input id="
+            << "0x" << std::hex << active_input_node_id_ << " mute=false";
+    SetInputMuteInternal(false);
   } else {
-    SetInputMute(true);
+    VLOG(0) << "Audio input NOT allowed by policy, sets input id="
+            << "0x" << std::hex << active_input_node_id_ << " mute=true";
+    SetInputMuteInternal(true);
     input_mute_locked_ = true;
   }
 }
@@ -486,6 +525,8 @@ bool CrasAudioHandler::SetInputMuteInternal(bool mute_on) {
   if (input_mute_locked_)
     return false;
 
+  VLOG(1) << "SetInputMuteInternal sets active input device id="
+          << "0x" << std::hex << active_input_node_id_ << " mute=" << mute_on;
   input_mute_on_ = mute_on;
   chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->
       SetInputMute(mute_on);
@@ -529,7 +570,7 @@ bool CrasAudioHandler::NonActiveDeviceUnplugged(
     size_t old_devices_size,
     size_t new_devices_size,
     uint64 current_active_node) {
-  return (new_devices_size <= old_devices_size &&
+  return (new_devices_size < old_devices_size &&
           GetDeviceFromId(current_active_node));
 }
 
@@ -647,7 +688,8 @@ void CrasAudioHandler::UpdateDevicesAndSwitchActive(
 
     if (!has_alternative_input_ &&
         device.is_input &&
-        device.type != AUDIO_TYPE_INTERNAL_MIC) {
+        device.type != AUDIO_TYPE_INTERNAL_MIC &&
+        device.type != AUDIO_TYPE_KEYBOARD_MIC) {
       has_alternative_input_ = true;
     } else if (!has_alternative_output_ &&
                !device.is_input &&