1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128

ash / system / audio / mic_gain_slider_controller.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/audio/mic_gain_slider_controller.h"

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/system/audio/mic_gain_slider_view.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"

namespace ash {

namespace {

MicGainSliderController::MapDeviceSliderCallback* g_map_slider_device_callback =
    nullptr;

}  // namespace

MicGainSliderController::MicGainSliderController()
    : input_gain_metric_delay_timer_(
          FROM_HERE,
          CrasAudioHandler::kMetricsDelayTimerInterval,
          /*receiver=*/this,
          &MicGainSliderController::RecordGainChanged) {}

MicGainSliderController::~MicGainSliderController() = default;

std::unique_ptr<MicGainSliderView> MicGainSliderController::CreateMicGainSlider(
    uint64_t device_id,
    bool internal) {
  std::unique_ptr<MicGainSliderView> slider =
      std::make_unique<MicGainSliderView>(this, device_id, internal);
  if (g_map_slider_device_callback) {
    g_map_slider_device_callback->Run(device_id, slider.get());
  }
  return slider;
}

// static
void MicGainSliderController::SetMapDeviceSliderCallbackForTest(
    MapDeviceSliderCallback* map_slider_device_callback) {
  g_map_slider_device_callback = map_slider_device_callback;
}

std::unique_ptr<UnifiedSliderView> MicGainSliderController::CreateView() {
#if DCHECK_IS_ON()
  DCHECK(!created_view_);
  created_view_ = true;
#endif
  return std::make_unique<MicGainSliderView>(this);
}

QsSliderCatalogName MicGainSliderController::GetCatalogName() {
  return QsSliderCatalogName::kMicGain;
}

void MicGainSliderController::SliderValueChanged(
    views::Slider* sender,
    float value,
    float old_value,
    views::SliderChangeReason reason) {
  if (reason != views::SliderChangeReason::kByUser) {
    return;
  }

  // Unmute if muted.
  if (CrasAudioHandler::Get()->IsInputMuted()) {
    CrasAudioHandler::Get()->SetMuteForDevice(
        CrasAudioHandler::Get()->GetPrimaryActiveInputNode(),
        /*mute_on=*/false);
  }

  const int level = value * 100;
  if (level != CrasAudioHandler::Get()->GetInputGainPercent()) {
    TrackValueChangeUMA(/*going_up=*/level >
                        CrasAudioHandler::Get()->GetInputGainPercent());
  }

  // Manually sets the mute state since we don't distinguish muted and level is
  // 0 state.
  if (level == 0) {
    CrasAudioHandler::Get()->SetMuteForDevice(
        CrasAudioHandler::Get()->GetPrimaryActiveInputNode(), /*mute_on=*/true);
  }

  CrasAudioHandler::Get()->SetInputGainPercent(level);

  input_gain_metric_delay_timer_.Reset();
}

void MicGainSliderController::SliderButtonPressed() {
  auto* const audio_handler = CrasAudioHandler::Get();
  const bool mute = !audio_handler->IsInputMuted();

  // If the level is 0, this slider is still muted, and nothing needs to be
  // done.
  if (audio_handler->GetInputGainPercent() == 0) {
    return;
  }

  TrackToggleUMA(/*target_toggle_state=*/mute);

  audio_handler->SetMuteForDevice(
      audio_handler->GetPrimaryActiveInputNode(), mute,
      CrasAudioHandler::AudioSettingsChangeSource::kSystemTray);
}

void MicGainSliderController::RecordGainChanged() {
  base::UmaHistogramEnumeration(
      CrasAudioHandler::kInputGainChangedSourceHistogramName,
      CrasAudioHandler::AudioSettingsChangeSource::kSystemTray);

  CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
  CHECK(audio_handler);
  if (!audio_handler->GetForceRespectUiGainsState()) {
    base::UmaHistogramEnumeration(
        CrasAudioHandler::kInputGainChangedHistogramName,
        CrasAudioHandler::AudioSettingsChangeSource::kSystemTray);
  }
}

}  // namespace ash