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
  129
  130
  131
  132
  133
  134
  135
  136

ash / system / video_conference / bubble / mic_indicator.cc [blame]

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ash/system/video_conference/bubble/mic_indicator.h"

#include <cmath>
#include <iomanip>
#include <iostream>

#include "ash/style/ash_color_id.h"
#include "ash/system/video_conference/video_conference_tray_controller.h"
#include "base/timer/timer.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"

namespace ash::video_conference {

namespace {

const int kIndicatorLines = 4;
const int kIndicatorSpace = 2;
const int kIndicatorWidth = 2;
const int kIndicatorTotalWidth =
    kIndicatorLines * kIndicatorWidth + (kIndicatorLines - 1) * kIndicatorSpace;
const float kIndicatorLengths[] = {0.4, 0.9, 0.6, 0.85};

// Powers above kLogEwmaMax will be restricted to this value.
const float kLogEwmaMax = std::log(0.02);
// Powers below kLogEwmaMin will be restricted to this value.
const float kLogEwmaMin = std::log(0.00002);
const float kLogEwmaDiff = kLogEwmaMax - kLogEwmaMin;

constexpr int kMaxStep = 8;
constexpr float phaseLengths[] = {1.0, 1.1, 1.3, 1.1, 1.0, 0.9, 0.7, 0.9};
constexpr auto kMicIndicatorInsets = gfx::Insets::TLBR(10, 10, 10, 10);

float ScalePower(float power) {
  // Adjust the power on a logarithmic scale, allowing for more noticeable
  // changes at lower volumes.
  float log_value = std::log(power);
  if (log_value > kLogEwmaMax) {
    log_value = kLogEwmaMax;
  }
  if (log_value < kLogEwmaMin) {
    log_value = kLogEwmaMin;
  }
  float normalized = (log_value - kLogEwmaMin) / (kLogEwmaDiff);
  return 0.1 + normalized * (1.0 - 0.1);
}

}  // namespace

MicIndicator::MicIndicator() {
  VideoConferenceTrayController::Get()->SetEwmaPowerReportEnabled(true);
  SetInsideBorderInsets(kMicIndicatorInsets);

  power_ = VideoConferenceTrayController::Get()->GetEwmaPower();
  step_ = 0;
  color_ = cros_tokens::kCrosSysOnSurface;

  timer_ = std::make_unique<base::RepeatingTimer>();
  timer_->Start(FROM_HERE, base::Milliseconds(30),
                base::BindRepeating(&MicIndicator::UpdateProgress,
                                    base::Unretained(this)));
}

MicIndicator::~MicIndicator() {
  // Disable ewma power reporting when the view is destructed, so CRAS
  // doesn't report unnecessary data.
  VideoConferenceTrayController::Get()->SetEwmaPowerReportEnabled(false);
}

void MicIndicator::UpdateProgress() {
  step_ = (step_ + 1) % kMaxStep;
  if (step_ == 0) {
    power_ = VideoConferenceTrayController::Get()->GetEwmaPower();
  }
  SchedulePaint();
}

void MicIndicator::OnPaint(gfx::Canvas* canvas) {
  const float multiplier = ScalePower(power_);

  cc::PaintFlags flags;
  flags.setAntiAlias(true);
  flags.setStrokeWidth(kIndicatorWidth);
  flags.setColor(GetColorProvider()->GetColor(color_));
  flags.setStyle(cc::PaintFlags::kStroke_Style);
  SkPath path;

  const int view_height = GetContentsBounds().height();
  const int view_width = GetContentsBounds().width();
  float x = (view_width - kIndicatorTotalWidth) / 2;
  for (int i = 0; i < kIndicatorLines; i++) {
    float length = view_height *
                   // Set each line's animation cycle based on the previous
                   // line, creating a wave-like effect.
                   phaseLengths[(step_ + kMaxStep - i) % kMaxStep] *
                   kIndicatorLengths[i];

    length = length * multiplier;

    float y0 = (view_height - length) / 2;
    float y1 = y0 + length;
    path.moveTo(x, y0);
    path.lineTo(x, y1);
    canvas->DrawPath(path, flags);

    x += kIndicatorSpace + static_cast<int>(flags.getStrokeWidth());
  }
}

void MicIndicator::OnThemeChanged() {
  views::View::OnThemeChanged();

  bool sidetone_enabled =
      VideoConferenceTrayController::Get()->GetSidetoneEnabled();
  color_ = sidetone_enabled ? cros_tokens::kCrosSysSystemOnPrimaryContainer
                            : cros_tokens::kCrosSysOnSurface;
}

BEGIN_METADATA(MicIndicator)
END_METADATA

}  // namespace ash::video_conference