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
) {
20 case AUDIO_TYPE_HEADPHONE
:
23 case AUDIO_TYPE_BLUETOOTH
:
27 case AUDIO_TYPE_INTERNAL_SPEAKER
:
28 case AUDIO_TYPE_INTERNAL_MIC
:
30 case AUDIO_TYPE_KEYBOARD_MIC
:
32 case AUDIO_TYPE_POST_MIX_LOOPBACK
:
33 case AUDIO_TYPE_POST_DSP_LOOPBACK
:
34 case AUDIO_TYPE_OTHER
:
43 std::string
AudioDevice::GetTypeString(AudioDeviceType type
) {
45 case AUDIO_TYPE_HEADPHONE
:
51 case AUDIO_TYPE_BLUETOOTH
:
55 case AUDIO_TYPE_INTERNAL_SPEAKER
:
56 return "INTERNAL_SPEAKER";
57 case AUDIO_TYPE_INTERNAL_MIC
:
58 return "INTERNAL_MIC";
59 case AUDIO_TYPE_KEYBOARD_MIC
:
60 return "KEYBOARD_MIC";
63 case AUDIO_TYPE_POST_MIX_LOOPBACK
:
64 return "POST_MIX_LOOPBACK";
65 case AUDIO_TYPE_POST_DSP_LOOPBACK
:
66 return "POST_DSP_LOOPBACK";
67 case AUDIO_TYPE_OTHER
:
74 AudioDeviceType
AudioDevice::GetAudioType(
75 const std::string
& node_type
) {
76 if (node_type
.find("HEADPHONE") != std::string::npos
)
77 return AUDIO_TYPE_HEADPHONE
;
78 else if (node_type
.find("INTERNAL_MIC") != std::string::npos
)
79 return AUDIO_TYPE_INTERNAL_MIC
;
80 else if (node_type
.find("KEYBOARD_MIC") != std::string::npos
)
81 return AUDIO_TYPE_KEYBOARD_MIC
;
82 else if (node_type
.find("MIC") != std::string::npos
)
83 return AUDIO_TYPE_MIC
;
84 else if (node_type
.find("USB") != std::string::npos
)
85 return AUDIO_TYPE_USB
;
86 else if (node_type
.find("BLUETOOTH") != std::string::npos
)
87 return AUDIO_TYPE_BLUETOOTH
;
88 else if (node_type
.find("HDMI") != std::string::npos
)
89 return AUDIO_TYPE_HDMI
;
90 else if (node_type
.find("INTERNAL_SPEAKER") != std::string::npos
)
91 return AUDIO_TYPE_INTERNAL_SPEAKER
;
92 else if (node_type
.find("AOKR") != std::string::npos
)
93 return AUDIO_TYPE_AOKR
;
94 else if (node_type
.find("POST_MIX_LOOPBACK") != std::string::npos
)
95 return AUDIO_TYPE_POST_MIX_LOOPBACK
;
96 else if (node_type
.find("POST_DSP_LOOPBACK") != std::string::npos
)
97 return AUDIO_TYPE_POST_DSP_LOOPBACK
;
99 return AUDIO_TYPE_OTHER
;
102 AudioDevice::AudioDevice()
106 type(AUDIO_TYPE_OTHER
),
112 AudioDevice::AudioDevice(const AudioNode
& node
) {
113 is_input
= node
.is_input
;
115 type
= GetAudioType(node
.type
);
116 if (!node
.name
.empty() && node
.name
!= "(default)")
117 display_name
= node
.name
;
119 display_name
= node
.device_name
;
120 device_name
= node
.device_name
;
121 priority
= GetDevicePriority(type
);
122 active
= node
.active
;
123 plugged_time
= node
.plugged_time
;
126 std::string
AudioDevice::ToString() const {
128 base::StringAppendF(&result
,
130 is_input
? "true" : "false");
131 base::StringAppendF(&result
,
132 "id = 0x%" PRIx64
" ",
134 base::StringAppendF(&result
,
135 "display_name = %s ",
136 display_name
.c_str());
137 base::StringAppendF(&result
,
139 device_name
.c_str());
140 base::StringAppendF(&result
,
142 GetTypeString(type
).c_str());
143 base::StringAppendF(&result
,
145 active
? "true" : "false");
146 base::StringAppendF(&result
,
148 base::Uint64ToString(plugged_time
).c_str());
153 } // namespace chromeos