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"
14 // Get the priority for a particular device type. The priority returned
15 // will be between 0 to 3, the higher number meaning a higher priority.
16 uint8
GetDevicePriority(chromeos::AudioDeviceType type
) {
19 case chromeos::AUDIO_TYPE_HEADPHONE
:
20 case chromeos::AUDIO_TYPE_MIC
:
21 case chromeos::AUDIO_TYPE_USB
:
22 case chromeos::AUDIO_TYPE_BLUETOOTH
:
24 case chromeos::AUDIO_TYPE_HDMI
:
27 case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
:
28 case chromeos::AUDIO_TYPE_INTERNAL_MIC
:
31 case chromeos::AUDIO_TYPE_KEYBOARD_MIC
:
32 case chromeos::AUDIO_TYPE_OTHER
:
38 std::string
GetTypeString(chromeos::AudioDeviceType type
) {
40 case chromeos::AUDIO_TYPE_HEADPHONE
:
42 case chromeos::AUDIO_TYPE_MIC
:
44 case chromeos::AUDIO_TYPE_USB
:
46 case chromeos::AUDIO_TYPE_BLUETOOTH
:
48 case chromeos::AUDIO_TYPE_HDMI
:
50 case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
:
51 return "INTERNAL_SPEAKER";
52 case chromeos::AUDIO_TYPE_INTERNAL_MIC
:
53 return "INTERNAL_MIC";
54 case chromeos::AUDIO_TYPE_KEYBOARD_MIC
:
55 return "KEYBOARD_MIC";
56 case chromeos::AUDIO_TYPE_OTHER
:
62 chromeos::AudioDeviceType
GetAudioType(const std::string
& node_type
) {
63 if (node_type
.find("HEADPHONE") != std::string::npos
)
64 return chromeos::AUDIO_TYPE_HEADPHONE
;
65 else if (node_type
.find("INTERNAL_MIC") != std::string::npos
)
66 return chromeos::AUDIO_TYPE_INTERNAL_MIC
;
67 else if (node_type
.find("KEYBOARD_MIC") != std::string::npos
)
68 return chromeos::AUDIO_TYPE_KEYBOARD_MIC
;
69 else if (node_type
.find("MIC") != std::string::npos
)
70 return chromeos::AUDIO_TYPE_MIC
;
71 else if (node_type
.find("USB") != std::string::npos
)
72 return chromeos::AUDIO_TYPE_USB
;
73 else if (node_type
.find("BLUETOOTH") != std::string::npos
)
74 return chromeos::AUDIO_TYPE_BLUETOOTH
;
75 else if (node_type
.find("HDMI") != std::string::npos
)
76 return chromeos::AUDIO_TYPE_HDMI
;
77 else if (node_type
.find("INTERNAL_SPEAKER") != std::string::npos
)
78 return chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
;
80 return chromeos::AUDIO_TYPE_OTHER
;
87 AudioDevice::AudioDevice()
91 type(AUDIO_TYPE_OTHER
),
97 AudioDevice::AudioDevice(const AudioNode
& node
) {
98 is_input
= node
.is_input
;
100 type
= GetAudioType(node
.type
);
101 if (!node
.name
.empty() && node
.name
!= "(default)")
102 display_name
= node
.name
;
104 display_name
= node
.device_name
;
105 device_name
= node
.device_name
;
106 priority
= GetDevicePriority(type
);
107 active
= node
.active
;
108 plugged_time
= node
.plugged_time
;
111 std::string
AudioDevice::ToString() const {
113 base::StringAppendF(&result
,
115 is_input
? "true" : "false");
116 base::StringAppendF(&result
,
117 "id = 0x%" PRIx64
" ",
119 base::StringAppendF(&result
,
120 "display_name = %s ",
121 display_name
.c_str());
122 base::StringAppendF(&result
,
124 device_name
.c_str());
125 base::StringAppendF(&result
,
127 GetTypeString(type
).c_str());
128 base::StringAppendF(&result
,
130 active
? "true" : "false");
131 base::StringAppendF(&result
,
133 base::Uint64ToString(plugged_time
).c_str());
138 } // namespace chromeos