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.
4
5 #ifndef CHROMEOS_AUDIO_AUDIO_DEVICE_H_
6 #define CHROMEOS_AUDIO_AUDIO_DEVICE_H_
7
8 #include <stdint.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "base/component_export.h"
15 #include "chromeos/dbus/audio/audio_node.h"
16
17 namespace chromeos {
18
19 // Ordered from the highest priority to the lowest.
20 enum AudioDeviceType {
21 AUDIO_TYPE_HEADPHONE,
22 AUDIO_TYPE_MIC,
23 AUDIO_TYPE_USB,
24 AUDIO_TYPE_BLUETOOTH,
25 AUDIO_TYPE_BLUETOOTH_NB_MIC,
26 AUDIO_TYPE_HDMI,
27 AUDIO_TYPE_INTERNAL_SPEAKER,
28 AUDIO_TYPE_INTERNAL_MIC,
29 AUDIO_TYPE_FRONT_MIC,
30 AUDIO_TYPE_REAR_MIC,
31 AUDIO_TYPE_KEYBOARD_MIC,
32 AUDIO_TYPE_HOTWORD,
33 AUDIO_TYPE_LINEOUT,
34 AUDIO_TYPE_POST_MIX_LOOPBACK,
35 AUDIO_TYPE_POST_DSP_LOOPBACK,
36 AUDIO_TYPE_ALSA_LOOPBACK,
37 AUDIO_TYPE_OTHER,
38 };
39
COMPONENT_EXPORT(CHROMEOS_AUDIO)40 struct COMPONENT_EXPORT(CHROMEOS_AUDIO) AudioDevice {
41 AudioDevice();
42 explicit AudioDevice(const AudioNode& node);
43 AudioDevice(const AudioDevice& other);
44 std::string ToString() const;
45
46 // Converts between the string type sent via D-Bus and AudioDeviceType.
47 // Static so they can be used by tests.
48 static std::string GetTypeString(chromeos::AudioDeviceType type);
49 static chromeos::AudioDeviceType GetAudioType(const std::string& node_type);
50
51 // Indicates that an input or output audio device is for simple usage like
52 // playback or recording for user. In contrast, audio device such as
53 // loopback, always on keyword recognition (HOTWORD), and keyboard mic are
54 // not for simple usage.
55 // One special case is ALSA loopback device, which will only exist under
56 // testing, and we want it visible to users for e2e tests.
57 bool is_for_simple_usage() const {
58 return (type == AUDIO_TYPE_HEADPHONE ||
59 type == AUDIO_TYPE_INTERNAL_MIC ||
60 type == AUDIO_TYPE_FRONT_MIC ||
61 type == AUDIO_TYPE_REAR_MIC ||
62 type == AUDIO_TYPE_MIC ||
63 type == AUDIO_TYPE_USB ||
64 type == AUDIO_TYPE_BLUETOOTH ||
65 type == AUDIO_TYPE_BLUETOOTH_NB_MIC ||
66 type == AUDIO_TYPE_HDMI ||
67 type == AUDIO_TYPE_INTERNAL_SPEAKER ||
68 type == AUDIO_TYPE_LINEOUT ||
69 type == AUDIO_TYPE_ALSA_LOOPBACK);
70 }
71
72 bool IsExternalDevice() const;
73
74 bool IsInternalMic() const;
75
76 bool is_input = false;
77
78 // Id of this audio device. The legacy |id| is assigned to be unique everytime
79 // when each device got plugged, so that the same physical device will have
80 // a different id after unplug then re-plug.
81 // The |stable_device_id| is designed to be persistent across system reboot
82 // and plug/unplug for the same physical device. It is guaranteed that
83 // different type of hardware has different |stable_device_id|, but not
84 // guaranteed to be different between the same kind of audio device, e.g
85 // USB headset. |id| and |stable_device_id| can be used together to achieve
86 // various goals.
87 // Note that because algorithm used to determine |stable_device_id| changed in
88 // system code, |stable_device_id_version| and |deprecated_stable_device_id|
89 // have been introduced - to ensure backward compatibility until persisted
90 // references to stable device ID have been updated where needed.
91 // |stable_device_id_version| is the version of stable device ID set in
92 // |stable_device_id|. If version is set to 2, |deprecated_stable_device_id|
93 // will contain deprecated, v1 stable device id version.
94 uint64_t id = 0;
95 int stable_device_id_version = 0;
96 uint64_t stable_device_id = 0;
97 uint64_t deprecated_stable_device_id = 0;
98 std::string display_name;
99 std::string device_name;
100 AudioDeviceType type = AUDIO_TYPE_OTHER;
101 uint8_t priority = 0;
102 bool active = false;
103 uint64_t plugged_time = 0;
104 uint32_t max_supported_channels = 0;
105 };
106
107 typedef std::vector<AudioDevice> AudioDeviceList;
108 typedef std::map<uint64_t, AudioDevice> AudioDeviceMap;
109
110 struct AudioDeviceCompare {
111 // Rules used to discern which device is higher,
112 // 1.) Device Type:
113 // [Headphones/USB/Bluetooh > HDMI > Internal Speakers]
114 // [External Mic/USB Mic/Bluetooth > Internal Mic]
115 // 2.) Device Plugged in Time:
116 // [Later > Earlier]
operatorAudioDeviceCompare117 bool operator()(const chromeos::AudioDevice& a,
118 const chromeos::AudioDevice& b) const {
119 if (a.priority < b.priority) {
120 return true;
121 } else if (b.priority < a.priority) {
122 return false;
123 } else if (a.plugged_time < b.plugged_time) {
124 return true;
125 } else {
126 return false;
127 }
128 }
129 };
130
131 } // namespace chromeos
132
133 #endif // CHROMEOS_AUDIO_AUDIO_DEVICE_H_
134