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_OTHER
:
37 std::string
GetTypeString(chromeos::AudioDeviceType type
) {
39 case chromeos::AUDIO_TYPE_HEADPHONE
:
41 case chromeos::AUDIO_TYPE_MIC
:
43 case chromeos::AUDIO_TYPE_USB
:
45 case chromeos::AUDIO_TYPE_BLUETOOTH
:
47 case chromeos::AUDIO_TYPE_HDMI
:
49 case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
:
50 return "INTERNAL_SPEAKER";
51 case chromeos::AUDIO_TYPE_INTERNAL_MIC
:
52 return "INTERNAL_MIC";
53 case chromeos::AUDIO_TYPE_OTHER
:
59 chromeos::AudioDeviceType
GetAudioType(const std::string
& node_type
) {
60 if (node_type
.find("HEADPHONE") != std::string::npos
)
61 return chromeos::AUDIO_TYPE_HEADPHONE
;
62 else if (node_type
.find("INTERNAL_MIC") != std::string::npos
)
63 return chromeos::AUDIO_TYPE_INTERNAL_MIC
;
64 else if (node_type
.find("MIC") != std::string::npos
)
65 return chromeos::AUDIO_TYPE_MIC
;
66 else if (node_type
.find("USB") != std::string::npos
)
67 return chromeos::AUDIO_TYPE_USB
;
68 else if (node_type
.find("BLUETOOTH") != std::string::npos
)
69 return chromeos::AUDIO_TYPE_BLUETOOTH
;
70 else if (node_type
.find("HDMI") != std::string::npos
)
71 return chromeos::AUDIO_TYPE_HDMI
;
72 else if (node_type
.find("INTERNAL_SPEAKER") != std::string::npos
)
73 return chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
;
75 return chromeos::AUDIO_TYPE_OTHER
;
82 AudioDevice::AudioDevice()
86 type(AUDIO_TYPE_OTHER
),
92 AudioDevice::AudioDevice(const AudioNode
& node
) {
93 is_input
= node
.is_input
;
95 type
= GetAudioType(node
.type
);
96 if (!node
.name
.empty() && node
.name
!= "(default)")
97 display_name
= node
.name
;
99 display_name
= node
.device_name
;
100 device_name
= node
.device_name
;
101 priority
= GetDevicePriority(type
);
102 active
= node
.active
;
103 plugged_time
= node
.plugged_time
;
106 std::string
AudioDevice::ToString() const {
108 base::StringAppendF(&result
,
110 is_input
? "true" : "false");
111 base::StringAppendF(&result
,
112 "id = 0x%" PRIx64
" ",
114 base::StringAppendF(&result
,
115 "display_name = %s ",
116 display_name
.c_str());
117 base::StringAppendF(&result
,
119 device_name
.c_str());
120 base::StringAppendF(&result
,
122 GetTypeString(type
).c_str());
123 base::StringAppendF(&result
,
125 active
? "true" : "false");
126 base::StringAppendF(&result
,
128 base::Uint64ToString(plugged_time
).c_str());
133 } // namespace chromeos