Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / audio / audio_device.cc
blobfd6ae45e42d878958b8a746ee7f6c90bcf6cf119
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, bool is_input) {
19 // Lower the priority of bluetooth mic to prevent unexpected bad eperience
20 // to user because of bluetooth audio profile switching. Make priority to
21 // zero so this mic will never be automatically chosen.
22 if (type == AUDIO_TYPE_BLUETOOTH && is_input)
23 return 0;
24 switch (type) {
25 case AUDIO_TYPE_HEADPHONE:
26 case AUDIO_TYPE_MIC:
27 case AUDIO_TYPE_USB:
28 case AUDIO_TYPE_BLUETOOTH:
29 return 3;
30 case AUDIO_TYPE_HDMI:
31 return 2;
32 case AUDIO_TYPE_INTERNAL_SPEAKER:
33 case AUDIO_TYPE_INTERNAL_MIC:
34 return 1;
35 case AUDIO_TYPE_KEYBOARD_MIC:
36 case AUDIO_TYPE_AOKR:
37 case AUDIO_TYPE_POST_MIX_LOOPBACK:
38 case AUDIO_TYPE_POST_DSP_LOOPBACK:
39 case AUDIO_TYPE_OTHER:
40 default:
41 return 0;
45 } // namespace
47 // static
48 std::string AudioDevice::GetTypeString(AudioDeviceType type) {
49 switch (type) {
50 case AUDIO_TYPE_HEADPHONE:
51 return "HEADPHONE";
52 case AUDIO_TYPE_MIC:
53 return "MIC";
54 case AUDIO_TYPE_USB:
55 return "USB";
56 case AUDIO_TYPE_BLUETOOTH:
57 return "BLUETOOTH";
58 case AUDIO_TYPE_HDMI:
59 return "HDMI";
60 case AUDIO_TYPE_INTERNAL_SPEAKER:
61 return "INTERNAL_SPEAKER";
62 case AUDIO_TYPE_INTERNAL_MIC:
63 return "INTERNAL_MIC";
64 case AUDIO_TYPE_KEYBOARD_MIC:
65 return "KEYBOARD_MIC";
66 case AUDIO_TYPE_AOKR:
67 return "AOKR";
68 case AUDIO_TYPE_POST_MIX_LOOPBACK:
69 return "POST_MIX_LOOPBACK";
70 case AUDIO_TYPE_POST_DSP_LOOPBACK:
71 return "POST_DSP_LOOPBACK";
72 case AUDIO_TYPE_OTHER:
73 default:
74 return "OTHER";
78 // static
79 AudioDeviceType AudioDevice::GetAudioType(
80 const std::string& node_type) {
81 if (node_type.find("HEADPHONE") != std::string::npos)
82 return AUDIO_TYPE_HEADPHONE;
83 else if (node_type.find("INTERNAL_MIC") != std::string::npos)
84 return AUDIO_TYPE_INTERNAL_MIC;
85 else if (node_type.find("KEYBOARD_MIC") != std::string::npos)
86 return AUDIO_TYPE_KEYBOARD_MIC;
87 else if (node_type.find("MIC") != std::string::npos)
88 return AUDIO_TYPE_MIC;
89 else if (node_type.find("USB") != std::string::npos)
90 return AUDIO_TYPE_USB;
91 else if (node_type.find("BLUETOOTH") != std::string::npos)
92 return AUDIO_TYPE_BLUETOOTH;
93 else if (node_type.find("HDMI") != std::string::npos)
94 return AUDIO_TYPE_HDMI;
95 else if (node_type.find("INTERNAL_SPEAKER") != std::string::npos)
96 return AUDIO_TYPE_INTERNAL_SPEAKER;
97 else if (node_type.find("AOKR") != std::string::npos)
98 return AUDIO_TYPE_AOKR;
99 else if (node_type.find("POST_MIX_LOOPBACK") != std::string::npos)
100 return AUDIO_TYPE_POST_MIX_LOOPBACK;
101 else if (node_type.find("POST_DSP_LOOPBACK") != std::string::npos)
102 return AUDIO_TYPE_POST_DSP_LOOPBACK;
103 else
104 return AUDIO_TYPE_OTHER;
107 AudioDevice::AudioDevice()
108 : is_input(false),
109 id(0),
110 display_name(""),
111 type(AUDIO_TYPE_OTHER),
112 priority(0),
113 active(false),
114 plugged_time(0) {
117 AudioDevice::AudioDevice(const AudioNode& node) {
118 is_input = node.is_input;
119 id = node.id;
120 type = GetAudioType(node.type);
121 if (!node.name.empty() && node.name != "(default)")
122 display_name = node.name;
123 else
124 display_name = node.device_name;
125 device_name = node.device_name;
126 priority = GetDevicePriority(type, node.is_input);
127 active = node.active;
128 plugged_time = node.plugged_time;
131 std::string AudioDevice::ToString() const {
132 std::string result;
133 base::StringAppendF(&result,
134 "is_input = %s ",
135 is_input ? "true" : "false");
136 base::StringAppendF(&result,
137 "id = 0x%" PRIx64 " ",
138 id);
139 base::StringAppendF(&result,
140 "display_name = %s ",
141 display_name.c_str());
142 base::StringAppendF(&result,
143 "device_name = %s ",
144 device_name.c_str());
145 base::StringAppendF(&result,
146 "type = %s ",
147 GetTypeString(type).c_str());
148 base::StringAppendF(&result,
149 "active = %s ",
150 active ? "true" : "false");
151 base::StringAppendF(&result,
152 "plugged_time= %s ",
153 base::Uint64ToString(plugged_time).c_str());
155 return result;
158 } // namespace chromeos