File: audio_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,504 bytes parent folder | download | duplicates (5)
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
137
138
139
140
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/ash/audio/audio_handler.h"

#include <string>
#include <tuple>
#include <utility>

#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"

namespace ash {

AudioHandler::AudioHandler(
    mojo::PendingReceiver<audio::mojom::PageHandler> receiver,
    mojo::PendingRemote<audio::mojom::Page> page)
    : page_(std::move(page)), receiver_(this, std::move(receiver)) {
  observation_.Observe(CrasAudioHandler::Get());
}

AudioHandler::~AudioHandler() = default;

void AudioHandler::GetAudioDeviceInfo() {
  UpdateAudioDeviceInfo();
}

void AudioHandler::GetActiveOutputDeviceName(
    audio::mojom::PageHandler::GetActiveOutputDeviceNameCallback callback) {
  const uint64_t output_id =
      ash::CrasAudioHandler::Get()->GetPrimaryActiveOutputNode();
  const ash::AudioDevice* output_device =
      ash::CrasAudioHandler::Get()->GetDeviceFromId(output_id);
  if (output_device) {
    std::move(callback).Run(output_device->display_name);
  } else {
    std::move(callback).Run(std::nullopt);
  }
}

void AudioHandler::GetActiveInputDeviceName(
    audio::mojom::PageHandler::GetActiveInputDeviceNameCallback callback) {
  const uint64_t input_id =
      ash::CrasAudioHandler::Get()->GetPrimaryActiveInputNode();
  const ash::AudioDevice* input_device =
      ash::CrasAudioHandler::Get()->GetDeviceFromId(input_id);
  if (input_device) {
    std::move(callback).Run(input_device->display_name);
  } else {
    std::move(callback).Run(std::nullopt);
  }
}

void AudioHandler::OpenFeedbackDialog() {
  chrome::OpenFeedbackDialog(chrome::FindBrowserWithActiveWindow(),
                             feedback::kFeedbackSourceMdSettingsAboutPage);
}

void AudioHandler::OnAudioNodesChanged() {
  UpdateAudioDeviceInfo();
}

void AudioHandler::OnOutputNodeVolumeChanged(uint64_t node_id, int volume) {
  page_->UpdateDeviceVolume(node_id, volume);
}

void AudioHandler::OnInputNodeGainChanged(uint64_t node_id, int gain) {
  page_->UpdateDeviceVolume(node_id, gain);
}

void AudioHandler::OnOutputMuteChanged(bool mute) {
  const uint64_t output_id =
      ash::CrasAudioHandler::Get()->GetPrimaryActiveOutputNode();
  page_->UpdateDeviceMute(output_id, mute);
}

void AudioHandler::OnInputMuteChanged(
    bool mute,
    CrasAudioHandler::InputMuteChangeMethod method) {
  const uint64_t input_id =
      ash::CrasAudioHandler::Get()->GetPrimaryActiveInputNode();
  page_->UpdateDeviceMute(input_id, mute);
}

void AudioHandler::OnInputMutedByMicrophoneMuteSwitchChanged(bool mute) {
  const uint64_t input_id =
      ash::CrasAudioHandler::Get()->GetPrimaryActiveInputNode();
  page_->UpdateDeviceMute(input_id, mute);
}

void AudioHandler::OnActiveOutputNodeChanged() {
  UpdateAudioDeviceInfo();
}

void AudioHandler::OnActiveInputNodeChanged() {
  UpdateAudioDeviceInfo();
}

void AudioHandler::UpdateAudioDeviceInfo() {
  ash::AudioDeviceList devices;
  ash::CrasAudioHandler::Get()->GetAudioDevices(&devices);
  base::flat_map<uint64_t, audio::mojom::DeviceDataPtr> device_map;

  for (ash::AudioDeviceList::const_iterator it = devices.begin();
       it != devices.end(); ++it) {
    device_map[it->id] = CreateDeviceData(&(*it));
  }
  page_->UpdateDeviceInfo(std::move(device_map));
}

audio::mojom::DeviceDataPtr AudioHandler::CreateDeviceData(
    const ash::AudioDevice* item) const {
  auto device_data = audio::mojom::DeviceData::New();

  device_data->type = item->GetTypeString(item->type);
  device_data->id = item->id;
  device_data->display_name = item->display_name;
  device_data->is_active = item->active;
  device_data->is_input = item->is_input;
  std::tie(device_data->volume_gain_percent, device_data->is_muted) =
      GetDeviceVolGain(item->id, item->is_input);
  return device_data;
}

std::tuple<int, bool> AudioHandler::GetDeviceVolGain(uint64_t id,
                                                     bool is_input) const {
  if (is_input) {
    return std::make_tuple(
        ash::CrasAudioHandler::Get()->GetInputGainPercentForDevice(id),
        ash::CrasAudioHandler::Get()->IsInputMutedForDevice(id));
  } else {
    return std::make_tuple(
        ash::CrasAudioHandler::Get()->GetOutputVolumePercentForDevice(id),
        ash::CrasAudioHandler::Get()->IsOutputMutedForDevice(id));
  }
}

}  // namespace ash