1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef AUDIO_DEVICE_DESCRIPTOR_H
17 #define AUDIO_DEVICE_DESCRIPTOR_H
18
19 #include <memory>
20 #include <map>
21 #include "parcel.h"
22 #include "audio_device_info.h"
23 #include "audio_info.h"
24
25 namespace OHOS {
26 namespace AudioStandard {
27
IsUsb(DeviceType type)28 inline bool IsUsb(DeviceType type)
29 {
30 return type == DEVICE_TYPE_USB_HEADSET || type == DEVICE_TYPE_USB_ARM_HEADSET;
31 }
32
33 /**
34 * @brief The AudioDeviceDescriptor provides
35 * different sets of audio devices and their roles
36 */
37 class AudioDeviceDescriptor : public Parcelable {
38 friend class AudioSystemManager;
39 public:
40 enum {
41 AUDIO_DEVICE_DESCRIPTOR,
42 DEVICE_INFO,
43 };
44
45 class ClientInfo {
46 public:
47 bool hasBTPermission_ = false;
48 bool hasSystemPermission_ = false;
49 int32_t apiVersion_ = 0;
50
51 ClientInfo() = default;
ClientInfo(int32_t apiVersion)52 ClientInfo(int32_t apiVersion)
53 : apiVersion_(apiVersion) {}
ClientInfo(bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion)54 ClientInfo(bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion)
55 : hasBTPermission_(hasBTPermission), hasSystemPermission_(hasSystemPermission), apiVersion_(apiVersion) {}
56 };
57
58 AudioDeviceDescriptor(int32_t descriptorType = AUDIO_DEVICE_DESCRIPTOR);
59
60 AudioDeviceDescriptor(DeviceType type, DeviceRole role);
61
62 AudioDeviceDescriptor(DeviceType type, DeviceRole role, int32_t interruptGroupId, int32_t volumeGroupId,
63 std::string networkId);
64
65 AudioDeviceDescriptor(const AudioDeviceDescriptor &deviceDescriptor);
66
67 AudioDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
68
69 virtual ~AudioDeviceDescriptor();
70
71 DeviceType getType() const;
72
73 DeviceRole getRole() const;
74
75 DeviceCategory GetDeviceCategory() const;
76
77 bool IsAudioDeviceDescriptor() const;
78
79 bool Marshalling(Parcel &parcel) const override;
80
81 void UnmarshallingSelf(Parcel &parcel);
82
83 static AudioDeviceDescriptor *Unmarshalling(Parcel &parcel);
84
85 void SetDeviceInfo(std::string deviceName, std::string macAddress);
86
87 void SetDeviceCapability(const std::list<DeviceStreamInfo> &audioStreamInfo, int32_t channelMask,
88 int32_t channelIndexMasks = 0);
89
90 bool IsSameDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const;
91
92 bool IsSameDeviceDescPtr(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor) const;
93
94 bool IsSameDeviceInfo(const AudioDeviceDescriptor &deviceInfo) const;
95
96 bool IsPairedDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const;
97
98 bool IsDistributedSpeaker() const;
99
100 DeviceType MapInternalToExternalDeviceType(int32_t apiVersion) const;
101
102 DeviceStreamInfo GetDeviceStreamInfo(void) const;
103
104 void Dump(std::string &dumpString);
105
106 std::string GetDeviceTypeString();
107
108 std::string GetKey();
109
110 struct AudioDeviceDescriptorHash {
operatorAudioDeviceDescriptorHash111 size_t operator()(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor) const
112 {
113 if (deviceDescriptor == nullptr) {
114 return 0;
115 }
116 return std::hash<int32_t>{}(static_cast<int32_t>(deviceDescriptor->deviceType_)) ^
117 std::hash<int32_t>{}(static_cast<int32_t>(deviceDescriptor->deviceRole_)) ^
118 std::hash<std::string>{}(deviceDescriptor->macAddress_) ^
119 std::hash<std::string>{}(deviceDescriptor->networkId_);
120 }
121 };
122
123 struct AudioDeviceDescriptorEqual {
operatorAudioDeviceDescriptorEqual124 bool operator()(const std::shared_ptr<AudioDeviceDescriptor> &lhs,
125 const std::shared_ptr<AudioDeviceDescriptor> &rhs) const
126 {
127 if (lhs == nullptr && rhs == nullptr) {
128 return true;
129 }
130 if (lhs == nullptr || rhs == nullptr) {
131 return false;
132 }
133 return lhs->deviceType_ == rhs->deviceType_ &&
134 lhs->macAddress_ == rhs->macAddress_ &&
135 lhs->networkId_ == rhs->networkId_;
136 }
137 };
138
139 void SetClientInfo(std::shared_ptr<ClientInfo> clientInfo) const;
140 private:
141 static void FixApiCompatibility(int apiVersion, DeviceRole deviceRole,
142 DeviceType &deviceType, int32_t &deviceId, std::list<DeviceStreamInfo> &streamInfo);
143
144 bool MarshallingInner(Parcel &parcel) const;
145
146 bool MarshallingToDeviceInfo(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission,
147 int32_t apiVersion) const;
148 public:
149 DeviceType deviceType_ = DEVICE_TYPE_NONE;
150 DeviceRole deviceRole_ = DEVICE_ROLE_NONE;
151 int32_t deviceId_ = 0;
152 int32_t channelMasks_ = 0;
153 int32_t channelIndexMasks_ = 0;
154 std::string deviceName_;
155 std::string macAddress_;
156 int32_t interruptGroupId_ = 0;
157 int32_t volumeGroupId_ = 0;
158 std::string networkId_;
159 uint16_t dmDeviceType_{0};
160 std::string displayName_;
161 std::list<DeviceStreamInfo> audioStreamInfo_;
162 DeviceCategory deviceCategory_ = CATEGORY_DEFAULT;
163 ConnectState connectState_ = CONNECTED;
164 DeviceUsage deviceUsage_ = ALL_USAGE;
165 // AudioDeviceDescriptor
166 bool exceptionFlag_ = false;
167 int64_t connectTimeStamp_ = 0;
168 std::shared_ptr<AudioDeviceDescriptor> pairDeviceDescriptor_;
169 bool isScoRealConnected_ = false;
170 bool isEnable_ = true;
171 int32_t mediaVolume_ = 0;
172 int32_t callVolume_ = 0;
173 // DeviceInfo
174 bool isLowLatencyDevice_ = false;
175 int32_t a2dpOffloadFlag_ = NO_A2DP_DEVICE;
176 // Other
177 int32_t descriptorType_ = AUDIO_DEVICE_DESCRIPTOR;
178 bool spatializationSupported_ = false;
179 bool hasPair_{false};
180 RouterType routerType_ = ROUTER_TYPE_NONE;
181 bool isVrSupported_ = true;
182 mutable std::shared_ptr<ClientInfo> clientInfo_ = nullptr;
183 VolumeBehavior volumeBehavior_;
184
185 private:
IsOutput()186 bool IsOutput()
187 {
188 return deviceRole_ == OUTPUT_DEVICE;
189 }
190 static const std::map<DeviceType, std::string> deviceTypeStringMap;
191 };
192 } // namespace AudioStandard
193 } // namespace OHOS
194 #endif // AUDIO_DEVICE_DESCRIPTOR_H