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 priority
= GetDevicePriority(type
, node
.is_input
);
127 active
= node
.active
;
128 plugged_time
= node
.plugged_time
;
131 std::string
AudioDevice::ToString() const {
133 base::StringAppendF(&result
,
135 is_input
? "true" : "false");
136 base::StringAppendF(&result
,
137 "id = 0x%" PRIx64
" ",
139 base::StringAppendF(&result
,
140 "display_name = %s ",
141 display_name
.c_str());
142 base::StringAppendF(&result
,
144 device_name
.c_str());
145 base::StringAppendF(&result
,
147 GetTypeString(type
).c_str());
148 base::StringAppendF(&result
,
150 active
? "true" : "false");
151 base::StringAppendF(&result
,
153 base::Uint64ToString(plugged_time
).c_str());
158 } // namespace chromeos