Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / chromeos / audio / audio_device.cc
blob4f4b18cc7326b1b1c99c98e4ea353c6b89cd80cb
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_OTHER:
33 default:
34 return 0;
38 } // namespace
40 // static
41 std::string AudioDevice::GetTypeString(AudioDeviceType type) {
42 switch (type) {
43 case AUDIO_TYPE_HEADPHONE:
44 return "HEADPHONE";
45 case AUDIO_TYPE_MIC:
46 return "MIC";
47 case AUDIO_TYPE_USB:
48 return "USB";
49 case AUDIO_TYPE_BLUETOOTH:
50 return "BLUETOOTH";
51 case AUDIO_TYPE_HDMI:
52 return "HDMI";
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";
59 case AUDIO_TYPE_AOKR:
60 return "AOKR";
61 case AUDIO_TYPE_OTHER:
62 default:
63 return "OTHER";
67 // static
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;
88 else
89 return AUDIO_TYPE_OTHER;
92 AudioDevice::AudioDevice()
93 : is_input(false),
94 id(0),
95 display_name(""),
96 type(AUDIO_TYPE_OTHER),
97 priority(0),
98 active(false),
99 plugged_time(0) {
102 AudioDevice::AudioDevice(const AudioNode& node) {
103 is_input = node.is_input;
104 id = node.id;
105 type = GetAudioType(node.type);
106 if (!node.name.empty() && node.name != "(default)")
107 display_name = node.name;
108 else
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 {
117 std::string result;
118 base::StringAppendF(&result,
119 "is_input = %s ",
120 is_input ? "true" : "false");
121 base::StringAppendF(&result,
122 "id = 0x%" PRIx64 " ",
123 id);
124 base::StringAppendF(&result,
125 "display_name = %s ",
126 display_name.c_str());
127 base::StringAppendF(&result,
128 "device_name = %s ",
129 device_name.c_str());
130 base::StringAppendF(&result,
131 "type = %s ",
132 GetTypeString(type).c_str());
133 base::StringAppendF(&result,
134 "active = %s ",
135 active ? "true" : "false");
136 base::StringAppendF(&result,
137 "plugged_time= %s ",
138 base::Uint64ToString(plugged_time).c_str());
140 return result;
143 } // namespace chromeos