1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/audio/audio_device.h"
7 #include "base/format_macros.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
16 // Get the priority for a particular device type. The priority returned
17 // will be between 0 to 3, the higher number meaning a higher priority.
18 uint8
GetDevicePriority(AudioDeviceType type
, bool is_input
) {
19 // Lower the priority of bluetooth mic to prevent unexpected bad eperience
20 // to user because of bluetooth audio profile switching. Make priority to
21 // zero so this mic will never be automatically chosen.
22 if (type
== AUDIO_TYPE_BLUETOOTH
&& is_input
)
25 case AUDIO_TYPE_HEADPHONE
:
28 case AUDIO_TYPE_BLUETOOTH
:
32 case AUDIO_TYPE_INTERNAL_SPEAKER
:
33 case AUDIO_TYPE_INTERNAL_MIC
:
35 case AUDIO_TYPE_KEYBOARD_MIC
:
37 case AUDIO_TYPE_POST_MIX_LOOPBACK
:
38 case AUDIO_TYPE_POST_DSP_LOOPBACK
:
39 case AUDIO_TYPE_OTHER
:
48 std::string
AudioDevice::GetTypeString(AudioDeviceType type
) {
50 case AUDIO_TYPE_HEADPHONE
:
56 case AUDIO_TYPE_BLUETOOTH
:
60 case AUDIO_TYPE_INTERNAL_SPEAKER
:
61 return "INTERNAL_SPEAKER";
62 case AUDIO_TYPE_INTERNAL_MIC
:
63 return "INTERNAL_MIC";
64 case AUDIO_TYPE_KEYBOARD_MIC
:
65 return "KEYBOARD_MIC";
68 case AUDIO_TYPE_POST_MIX_LOOPBACK
:
69 return "POST_MIX_LOOPBACK";
70 case AUDIO_TYPE_POST_DSP_LOOPBACK
:
71 return "POST_DSP_LOOPBACK";
72 case AUDIO_TYPE_OTHER
:
79 AudioDeviceType
AudioDevice::GetAudioType(
80 const std::string
& node_type
) {
81 if (node_type
.find("HEADPHONE") != std::string::npos
)
82 return AUDIO_TYPE_HEADPHONE
;
83 else if (node_type
.find("INTERNAL_MIC") != std::string::npos
)
84 return AUDIO_TYPE_INTERNAL_MIC
;
85 else if (node_type
.find("KEYBOARD_MIC") != std::string::npos
)
86 return AUDIO_TYPE_KEYBOARD_MIC
;
87 else if (node_type
.find("MIC") != std::string::npos
)
88 return AUDIO_TYPE_MIC
;
89 else if (node_type
.find("USB") != std::string::npos
)
90 return AUDIO_TYPE_USB
;
91 else if (node_type
.find("BLUETOOTH") != std::string::npos
)
92 return AUDIO_TYPE_BLUETOOTH
;
93 else if (node_type
.find("HDMI") != std::string::npos
)
94 return AUDIO_TYPE_HDMI
;
95 else if (node_type
.find("INTERNAL_SPEAKER") != std::string::npos
)
96 return AUDIO_TYPE_INTERNAL_SPEAKER
;
97 else if (node_type
.find("AOKR") != std::string::npos
)
98 return AUDIO_TYPE_AOKR
;
99 else if (node_type
.find("POST_MIX_LOOPBACK") != std::string::npos
)
100 return AUDIO_TYPE_POST_MIX_LOOPBACK
;
101 else if (node_type
.find("POST_DSP_LOOPBACK") != std::string::npos
)
102 return AUDIO_TYPE_POST_DSP_LOOPBACK
;
104 return AUDIO_TYPE_OTHER
;
107 AudioDevice::AudioDevice()
111 type(AUDIO_TYPE_OTHER
),
117 AudioDevice::AudioDevice(const AudioNode
& node
) {
118 is_input
= node
.is_input
;
120 type
= GetAudioType(node
.type
);
121 if (!node
.name
.empty() && node
.name
!= "(default)")
122 display_name
= node
.name
;
124 display_name
= node
.device_name
;
125 device_name
= node
.device_name
;
126 mic_positions
= node
.mic_positions
;
127 priority
= GetDevicePriority(type
, node
.is_input
);
128 active
= node
.active
;
129 plugged_time
= node
.plugged_time
;
132 std::string
AudioDevice::ToString() const {
134 base::StringAppendF(&result
,
136 is_input
? "true" : "false");
137 base::StringAppendF(&result
,
138 "id = 0x%" PRIx64
" ",
140 base::StringAppendF(&result
,
141 "display_name = %s ",
142 display_name
.c_str());
143 base::StringAppendF(&result
,
145 device_name
.c_str());
146 base::StringAppendF(&result
,
148 GetTypeString(type
).c_str());
149 base::StringAppendF(&result
,
151 active
? "true" : "false");
152 base::StringAppendF(&result
,
154 base::Uint64ToString(plugged_time
).c_str());
155 base::StringAppendF(&result
, "mic_positions = %s ", mic_positions
.c_str());
160 } // namespace chromeos