summaryrefslogtreecommitdiffstats
path: root/chromeos/audio/audio_device.cc
blob: 5374194edd0eb1d7f3d1f01cc792941167ded10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/audio/audio_device.h"

#include <stdint.h>

#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"

namespace chromeos {

namespace {

// Get the priority for a particular device type. The priority returned
// will be between 0 to 3, the higher number meaning a higher priority.
uint8_t GetDevicePriority(AudioDeviceType type, bool is_input) {
  // Lower the priority of bluetooth mic to prevent unexpected bad eperience
  // to user because of bluetooth audio profile switching. Make priority to
  // zero so this mic will never be automatically chosen.
  if (type == AUDIO_TYPE_BLUETOOTH && is_input)
    return 0;
  switch (type) {
    case AUDIO_TYPE_HEADPHONE:
    case AUDIO_TYPE_MIC:
    case AUDIO_TYPE_USB:
    case AUDIO_TYPE_BLUETOOTH:
      return 3;
    case AUDIO_TYPE_HDMI:
      return 2;
    case AUDIO_TYPE_INTERNAL_SPEAKER:
    case AUDIO_TYPE_INTERNAL_MIC:
      return 1;
    case AUDIO_TYPE_KEYBOARD_MIC:
    case AUDIO_TYPE_AOKR:
    case AUDIO_TYPE_POST_MIX_LOOPBACK:
    case AUDIO_TYPE_POST_DSP_LOOPBACK:
    case AUDIO_TYPE_OTHER:
    default:
      return 0;
  }
}

}  // namespace

// static
std::string AudioDevice::GetTypeString(AudioDeviceType type) {
  switch (type) {
    case AUDIO_TYPE_HEADPHONE:
      return "HEADPHONE";
    case AUDIO_TYPE_MIC:
      return "MIC";
    case AUDIO_TYPE_USB:
      return "USB";
    case AUDIO_TYPE_BLUETOOTH:
      return "BLUETOOTH";
    case AUDIO_TYPE_HDMI:
      return "HDMI";
    case AUDIO_TYPE_INTERNAL_SPEAKER:
      return "INTERNAL_SPEAKER";
    case AUDIO_TYPE_INTERNAL_MIC:
      return "INTERNAL_MIC";
    case AUDIO_TYPE_KEYBOARD_MIC:
      return "KEYBOARD_MIC";
    case AUDIO_TYPE_AOKR:
      return "AOKR";
    case AUDIO_TYPE_POST_MIX_LOOPBACK:
      return "POST_MIX_LOOPBACK";
    case AUDIO_TYPE_POST_DSP_LOOPBACK:
      return "POST_DSP_LOOPBACK";
    case AUDIO_TYPE_OTHER:
    default:
      return "OTHER";
  }
}

// static
AudioDeviceType AudioDevice::GetAudioType(
    const std::string& node_type) {
  if (node_type.find("HEADPHONE") != std::string::npos)
    return AUDIO_TYPE_HEADPHONE;
  else if (node_type.find("INTERNAL_MIC") != std::string::npos)
    return AUDIO_TYPE_INTERNAL_MIC;
  else if (node_type.find("KEYBOARD_MIC") != std::string::npos)
    return AUDIO_TYPE_KEYBOARD_MIC;
  else if (node_type.find("MIC") != std::string::npos)
    return AUDIO_TYPE_MIC;
  else if (node_type.find("USB") != std::string::npos)
    return AUDIO_TYPE_USB;
  else if (node_type.find("BLUETOOTH") != std::string::npos)
    return AUDIO_TYPE_BLUETOOTH;
  else if (node_type.find("HDMI") != std::string::npos)
    return AUDIO_TYPE_HDMI;
  else if (node_type.find("INTERNAL_SPEAKER") != std::string::npos)
    return AUDIO_TYPE_INTERNAL_SPEAKER;
  else if (node_type.find("AOKR") != std::string::npos)
    return AUDIO_TYPE_AOKR;
  else if (node_type.find("POST_MIX_LOOPBACK") != std::string::npos)
    return AUDIO_TYPE_POST_MIX_LOOPBACK;
  else if (node_type.find("POST_DSP_LOOPBACK") != std::string::npos)
    return AUDIO_TYPE_POST_DSP_LOOPBACK;
  else
    return AUDIO_TYPE_OTHER;
}

AudioDevice::AudioDevice()
    : is_input(false),
      id(0),
      display_name(""),
      type(AUDIO_TYPE_OTHER),
      priority(0),
      active(false),
      plugged_time(0) {
}

AudioDevice::AudioDevice(const AudioNode& node) {
  is_input = node.is_input;
  id = node.id;
  type = GetAudioType(node.type);
  if (!node.name.empty() && node.name != "(default)")
    display_name = node.name;
  else
    display_name = node.device_name;
  device_name = node.device_name;
  mic_positions = node.mic_positions;
  priority = GetDevicePriority(type, node.is_input);
  active = node.active;
  plugged_time = node.plugged_time;
}

std::string AudioDevice::ToString() const {
  std::string result;
  base::StringAppendF(&result,
                      "is_input = %s ",
                      is_input ? "true" : "false");
  base::StringAppendF(&result,
                      "id = 0x%" PRIx64 " ",
                      id);
  base::StringAppendF(&result,
                      "display_name = %s ",
                      display_name.c_str());
  base::StringAppendF(&result,
                      "device_name = %s ",
                      device_name.c_str());
  base::StringAppendF(&result,
                      "type = %s ",
                      GetTypeString(type).c_str());
  base::StringAppendF(&result,
                      "active = %s ",
                      active ? "true" : "false");
  base::StringAppendF(&result,
                      "plugged_time= %s ",
                      base::Uint64ToString(plugged_time).c_str());
  base::StringAppendF(&result, "mic_positions = %s ", mic_positions.c_str());

  return result;
}

}  // namespace chromeos