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_OTHER
:
41 std::string
AudioDevice::GetTypeString(AudioDeviceType type
) {
43 case AUDIO_TYPE_HEADPHONE
:
49 case AUDIO_TYPE_BLUETOOTH
:
53 case AUDIO_TYPE_INTERNAL_SPEAKER
:
54 return "INTERNAL_SPEAKER";
55 case AUDIO_TYPE_INTERNAL_MIC
:
56 return "INTERNAL_MIC";
57 case AUDIO_TYPE_KEYBOARD_MIC
:
58 return "KEYBOARD_MIC";
61 case AUDIO_TYPE_OTHER
:
68 AudioDeviceType
AudioDevice::GetAudioType(
69 const std::string
& node_type
) {
70 if (node_type
.find("HEADPHONE") != std::string::npos
)
71 return AUDIO_TYPE_HEADPHONE
;
72 else if (node_type
.find("INTERNAL_MIC") != std::string::npos
)
73 return AUDIO_TYPE_INTERNAL_MIC
;
74 else if (node_type
.find("KEYBOARD_MIC") != std::string::npos
)
75 return AUDIO_TYPE_KEYBOARD_MIC
;
76 else if (node_type
.find("MIC") != std::string::npos
)
77 return AUDIO_TYPE_MIC
;
78 else if (node_type
.find("USB") != std::string::npos
)
79 return AUDIO_TYPE_USB
;
80 else if (node_type
.find("BLUETOOTH") != std::string::npos
)
81 return AUDIO_TYPE_BLUETOOTH
;
82 else if (node_type
.find("HDMI") != std::string::npos
)
83 return AUDIO_TYPE_HDMI
;
84 else if (node_type
.find("INTERNAL_SPEAKER") != std::string::npos
)
85 return AUDIO_TYPE_INTERNAL_SPEAKER
;
86 else if (node_type
.find("AOKR") != std::string::npos
)
87 return AUDIO_TYPE_AOKR
;
89 return AUDIO_TYPE_OTHER
;
92 AudioDevice::AudioDevice()
96 type(AUDIO_TYPE_OTHER
),
102 AudioDevice::AudioDevice(const AudioNode
& node
) {
103 is_input
= node
.is_input
;
105 type
= GetAudioType(node
.type
);
106 if (!node
.name
.empty() && node
.name
!= "(default)")
107 display_name
= node
.name
;
109 display_name
= node
.device_name
;
110 device_name
= node
.device_name
;
111 priority
= GetDevicePriority(type
);
112 active
= node
.active
;
113 plugged_time
= node
.plugged_time
;
116 std::string
AudioDevice::ToString() const {
118 base::StringAppendF(&result
,
120 is_input
? "true" : "false");
121 base::StringAppendF(&result
,
122 "id = 0x%" PRIx64
" ",
124 base::StringAppendF(&result
,
125 "display_name = %s ",
126 display_name
.c_str());
127 base::StringAppendF(&result
,
129 device_name
.c_str());
130 base::StringAppendF(&result
,
132 GetTypeString(type
).c_str());
133 base::StringAppendF(&result
,
135 active
? "true" : "false");
136 base::StringAppendF(&result
,
138 base::Uint64ToString(plugged_time
).c_str());
143 } // namespace chromeos