File: audio_device.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (251 lines) | stat: -rw-r--r-- 8,679 bytes parent folder | download | duplicates (8)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/audio/audio_device.h"

#include <stdint.h>

#include "ash/constants/ash_features.h"
#include "base/containers/contains.h"
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"

namespace ash {

namespace {

// Get the priority for a particular device type. The priority returned
// will be between 0 to 3, the higher number meaning a higher priority.
uint8_t GetDevicePriority(AudioDeviceType type, bool is_input) {
  switch (type) {
    case AudioDeviceType::kHeadphone:
    case AudioDeviceType::kLineout:
    case AudioDeviceType::kMic:
    case AudioDeviceType::kUsb:
    case AudioDeviceType::kBluetooth:
      return 3;
    case AudioDeviceType::kHdmi:
      return 2;
    case AudioDeviceType::kInternalSpeaker:
    case AudioDeviceType::kInternalMic:
    case AudioDeviceType::kFrontMic:
      return 1;
    // Lower the priority of bluetooth mic to prevent unexpected bad eperience
    // to user because of bluetooth audio profile switching. Make priority to
    // zero so this mic will never be automatically chosen.
    case AudioDeviceType::kBluetoothNbMic:
    // Rear mic should have priority lower than front mic to prevent poor
    // quality input caused by accidental selecting to rear side mic.
    case AudioDeviceType::kRearMic:
    case AudioDeviceType::kKeyboardMic:
    case AudioDeviceType::kHotword:
    case AudioDeviceType::kPostMixLoopback:
    case AudioDeviceType::kPostDspLoopback:
    case AudioDeviceType::kAlsaLoopback:
    case AudioDeviceType::kOther:
    default:
      return 0;
  }
}

}  // namespace

// static
std::string AudioDevice::GetTypeString(AudioDeviceType type) {
  switch (type) {
    case AudioDeviceType::kHeadphone:
      return "HEADPHONE";
    case AudioDeviceType::kMic:
      return "MIC";
    case AudioDeviceType::kUsb:
      return "USB";
    case AudioDeviceType::kBluetooth:
      return "BLUETOOTH";
    case AudioDeviceType::kBluetoothNbMic:
      return "BLUETOOTH_NB_MIC";
    case AudioDeviceType::kHdmi:
      return "HDMI";
    case AudioDeviceType::kInternalSpeaker:
      return "INTERNAL_SPEAKER";
    case AudioDeviceType::kInternalMic:
      return "INTERNAL_MIC";
    case AudioDeviceType::kFrontMic:
      return "FRONT_MIC";
    case AudioDeviceType::kRearMic:
      return "REAR_MIC";
    case AudioDeviceType::kKeyboardMic:
      return "KEYBOARD_MIC";
    case AudioDeviceType::kHotword:
      return "HOTWORD";
    case AudioDeviceType::kLineout:
      return "LINEOUT";
    case AudioDeviceType::kPostMixLoopback:
      return "POST_MIX_LOOPBACK";
    case AudioDeviceType::kPostDspLoopback:
      return "POST_DSP_LOOPBACK";
    case AudioDeviceType::kAlsaLoopback:
      return "ALSA_LOOPBACK";
    case AudioDeviceType::kOther:
    default:
      return "OTHER";
  }
}

// static
AudioDeviceType AudioDevice::GetAudioType(const std::string& node_type) {
  if (base::Contains(node_type, "HEADPHONE")) {
    return AudioDeviceType::kHeadphone;
  } else if (base::Contains(node_type, "INTERNAL_MIC")) {
    return AudioDeviceType::kInternalMic;
  } else if (base::Contains(node_type, "FRONT_MIC")) {
    return AudioDeviceType::kFrontMic;
  } else if (base::Contains(node_type, "REAR_MIC")) {
    return AudioDeviceType::kRearMic;
  } else if (base::Contains(node_type, "KEYBOARD_MIC")) {
    return AudioDeviceType::kKeyboardMic;
  } else if (base::Contains(node_type, "BLUETOOTH_NB_MIC")) {
    return AudioDeviceType::kBluetoothNbMic;
  } else if (base::Contains(node_type, "MIC")) {
    return AudioDeviceType::kMic;
  } else if (base::Contains(node_type, "USB")) {
    return AudioDeviceType::kUsb;
  } else if (base::Contains(node_type, "BLUETOOTH")) {
    return AudioDeviceType::kBluetooth;
  } else if (base::Contains(node_type, "HDMI")) {
    return AudioDeviceType::kHdmi;
  } else if (base::Contains(node_type, "INTERNAL_SPEAKER")) {
    return AudioDeviceType::kInternalSpeaker;
  }
  // TODO(hychao): Remove the 'AOKR' matching line after CRAS switches
  // node type naming to 'HOTWORD'.
  else if (base::Contains(node_type, "AOKR")) {
    return AudioDeviceType::kHotword;
  } else if (base::Contains(node_type, "HOTWORD")) {
    return AudioDeviceType::kHotword;
  } else if (base::Contains(node_type, "LINEOUT")) {
    return AudioDeviceType::kLineout;
  } else if (base::Contains(node_type, "POST_MIX_LOOPBACK")) {
    return AudioDeviceType::kPostMixLoopback;
  } else if (base::Contains(node_type, "POST_DSP_LOOPBACK")) {
    return AudioDeviceType::kPostDspLoopback;
  } else if (base::Contains(node_type, "ALSA_LOOPBACK")) {
    return AudioDeviceType::kAlsaLoopback;
  } else {
    return AudioDeviceType::kOther;
  }
}

AudioDevice::AudioDevice() = default;

AudioDevice::AudioDevice(const AudioNode& node) {
  is_input = node.is_input;
  id = node.id;
  stable_device_id_version = node.StableDeviceIdVersion();
  stable_device_id = node.StableDeviceId();
  if (stable_device_id_version == 2) {
    deprecated_stable_device_id = node.stable_device_id_v1;
  }
  type = GetAudioType(node.type);
  if (!node.name.empty() && node.name != "(default)") {
    display_name = node.name;
  } else {
    display_name = node.device_name;
  }
  device_name = node.device_name;
  priority = GetDevicePriority(type, node.is_input);
  active = node.active;
  plugged_time = node.plugged_time;
  max_supported_channels = node.max_supported_channels;
  audio_effect = node.audio_effect;
  number_of_volume_steps = node.number_of_volume_steps;
}

AudioDevice::AudioDevice(const AudioDevice& other) = default;

AudioDevice& AudioDevice::operator=(const AudioDevice& other) = default;

std::string AudioDevice::ToString() const {
  if (stable_device_id_version == 0) {
    return "Null device";
  }

  std::string result;
  base::StringAppendF(&result, "is_input = %s ", is_input ? "true" : "false");
  base::StringAppendF(&result, "id = 0x%" PRIx64 " ", id);
  base::StringAppendF(&result, "stable_device_id_version = %d",
                      stable_device_id_version);
  base::StringAppendF(&result, "stable_device_id = 0x%" PRIx64 " ",
                      stable_device_id);
  base::StringAppendF(&result, "deprecated_stable_device_id = 0x%" PRIx64 " ",
                      deprecated_stable_device_id);
  base::StringAppendF(&result, "display_name = %s ", display_name.c_str());
  base::StringAppendF(&result, "device_name = %s ", device_name.c_str());
  base::StringAppendF(&result, "type = %s ", GetTypeString(type).c_str());
  base::StringAppendF(&result, "priority = %d ", priority);
  base::StringAppendF(&result, "user_priority = %d ", user_priority);
  base::StringAppendF(&result, "active = %s ", active ? "true" : "false");
  base::StringAppendF(&result, "plugged_time = %s ",
                      base::NumberToString(plugged_time).c_str());
  base::StringAppendF(&result, "max_supported_channels = %s ",
                      base::NumberToString(max_supported_channels).c_str());
  base::StringAppendF(&result, "audio_effect = %s ",
                      base::NumberToString(audio_effect).c_str());
  base::StringAppendF(&result, "number_of_volume_steps = %s ",
                      base::NumberToString(number_of_volume_steps).c_str());
  return result;
}

bool AudioDevice::IsExternalDevice() const {
  if (!is_for_simple_usage()) {
    return false;
  }

  return is_input ? !IsInternalMic() : !IsInternalSpeaker();
}

bool AudioDevice::IsInternalMic() const {
  switch (type) {
    case AudioDeviceType::kInternalMic:
    case AudioDeviceType::kFrontMic:
    case AudioDeviceType::kRearMic:
      return true;
    default:
      return false;
  }
}

bool AudioDevice::IsInternalSpeaker() const {
  return type == AudioDeviceType::kInternalSpeaker;
}

bool LessBuiltInPriority(const AudioDevice& a, const AudioDevice& b) {
  if (a.priority < b.priority) {
    return true;
  } else if (b.priority < a.priority) {
    return false;
  } else if (a.plugged_time < b.plugged_time) {
    return true;
  } else {
    return false;
  }
}

bool LessUserPriority(const AudioDevice& a, const AudioDevice& b) {
  if (a.user_priority < b.user_priority) {
    return true;
  } else if (b.user_priority < a.user_priority) {
    return false;
  } else {
    return LessBuiltInPriority(a, b);
  }
}

bool AudioDeviceCompare::operator()(const AudioDevice& a,
                                    const AudioDevice& b) const {
  return LessUserPriority(a, b);
}

}  // namespace ash