Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / chromeos / audio / audio_device.cc
blob32012a12aa5166341069b88ee3e0efaece5a77f4
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"
12 namespace chromeos {
14 namespace {
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) {
19 switch (type) {
20 case AUDIO_TYPE_HEADPHONE:
21 case AUDIO_TYPE_MIC:
22 case AUDIO_TYPE_USB:
23 case AUDIO_TYPE_BLUETOOTH:
24 return 3;
25 case AUDIO_TYPE_HDMI:
26 return 2;
27 case AUDIO_TYPE_INTERNAL_SPEAKER:
28 case AUDIO_TYPE_INTERNAL_MIC:
29 return 1;
30 case AUDIO_TYPE_KEYBOARD_MIC:
31 case AUDIO_TYPE_AOKR:
32 case AUDIO_TYPE_POST_MIX_LOOPBACK:
33 case AUDIO_TYPE_POST_DSP_LOOPBACK:
34 case AUDIO_TYPE_OTHER:
35 default:
36 return 0;
40 } // namespace
42 // static
43 std::string AudioDevice::GetTypeString(AudioDeviceType type) {
44 switch (type) {
45 case AUDIO_TYPE_HEADPHONE:
46 return "HEADPHONE";
47 case AUDIO_TYPE_MIC:
48 return "MIC";
49 case AUDIO_TYPE_USB:
50 return "USB";
51 case AUDIO_TYPE_BLUETOOTH:
52 return "BLUETOOTH";
53 case AUDIO_TYPE_HDMI:
54 return "HDMI";
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";
61 case AUDIO_TYPE_AOKR:
62 return "AOKR";
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:
68 default:
69 return "OTHER";
73 // static
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;
98 else
99 return AUDIO_TYPE_OTHER;
102 AudioDevice::AudioDevice()
103 : is_input(false),
104 id(0),
105 display_name(""),
106 type(AUDIO_TYPE_OTHER),
107 priority(0),
108 active(false),
109 plugged_time(0) {
112 AudioDevice::AudioDevice(const AudioNode& node) {
113 is_input = node.is_input;
114 id = node.id;
115 type = GetAudioType(node.type);
116 if (!node.name.empty() && node.name != "(default)")
117 display_name = node.name;
118 else
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 {
127 std::string result;
128 base::StringAppendF(&result,
129 "is_input = %s ",
130 is_input ? "true" : "false");
131 base::StringAppendF(&result,
132 "id = 0x%" PRIx64 " ",
133 id);
134 base::StringAppendF(&result,
135 "display_name = %s ",
136 display_name.c_str());
137 base::StringAppendF(&result,
138 "device_name = %s ",
139 device_name.c_str());
140 base::StringAppendF(&result,
141 "type = %s ",
142 GetTypeString(type).c_str());
143 base::StringAppendF(&result,
144 "active = %s ",
145 active ? "true" : "false");
146 base::StringAppendF(&result,
147 "plugged_time= %s ",
148 base::Uint64ToString(plugged_time).c_str());
150 return result;
153 } // namespace chromeos