summaryrefslogtreecommitdiffstats
path: root/chromeos/audio/audio_device.cc
blob: b6945e9dfe55a2d0a579174695597653c1ce5fdf (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
// 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 "base/format_macros.h"
#include "base/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"

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 GetDevicePriority(chromeos::AudioDeviceType type) {
  switch (type) {
    // Fall through.
    case chromeos::AUDIO_TYPE_HEADPHONE:
    case chromeos::AUDIO_TYPE_MIC:
    case chromeos::AUDIO_TYPE_USB:
    case chromeos::AUDIO_TYPE_BLUETOOTH:
      return 3;
    case chromeos::AUDIO_TYPE_HDMI:
      return 2;
      // Fall through.
    case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER:
    case chromeos::AUDIO_TYPE_INTERNAL_MIC:
      return 1;
      // Fall through.
    case chromeos::AUDIO_TYPE_OTHER:
    default:
      return 0;
  }
}

std::string GetTypeString(chromeos::AudioDeviceType type) {
  switch (type) {
    case chromeos::AUDIO_TYPE_HEADPHONE:
      return "HEADPHONE";
    case chromeos::AUDIO_TYPE_MIC:
      return "MIC";
    case chromeos::AUDIO_TYPE_USB:
      return "USB";
    case chromeos::AUDIO_TYPE_BLUETOOTH:
      return "BLUETOOTH";
    case chromeos::AUDIO_TYPE_HDMI:
      return "HDMI";
    case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER:
      return "INTERNAL_SPEAKER";
    case chromeos::AUDIO_TYPE_INTERNAL_MIC:
      return "INTERNAL_MIC";
    case chromeos::AUDIO_TYPE_OTHER:
    default:
      return "OTHER";
  }
}

chromeos::AudioDeviceType GetAudioType(const std::string& node_type) {
  if (node_type.find("HEADPHONE") != std::string::npos)
    return chromeos::AUDIO_TYPE_HEADPHONE;
  else if (node_type.find("INTERNAL_MIC") != std::string::npos)
    return chromeos::AUDIO_TYPE_INTERNAL_MIC;
  else if (node_type.find("MIC") != std::string::npos)
    return chromeos::AUDIO_TYPE_MIC;
  else if (node_type.find("USB") != std::string::npos)
    return chromeos::AUDIO_TYPE_USB;
  else if (node_type.find("BLUETOOTH") != std::string::npos)
    return chromeos::AUDIO_TYPE_BLUETOOTH;
  else if (node_type.find("HDMI") != std::string::npos)
    return chromeos::AUDIO_TYPE_HDMI;
  else if (node_type.find("INTERNAL_SPEAKER") != std::string::npos)
    return chromeos::AUDIO_TYPE_INTERNAL_SPEAKER;
  else
    return chromeos::AUDIO_TYPE_OTHER;
}

}  // namespace

namespace chromeos {

AudioDevice::AudioDevice()
    : is_input(false),
      id(0),
      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 = UTF8ToUTF16(node.name);
  else
    display_name = UTF8ToUTF16(node.device_name);
  priority = GetDevicePriority(type);
  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 = %s ",
                      base::Uint64ToString(id).c_str());
  base::StringAppendF(&result,
                      "display_name = %s ",
                      UTF16ToUTF8(display_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());

  return result;
}

}  // namespace chromeos