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
21 #include "parcel.h"
22 #include "audio_device_info.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
26
IsUsb(DeviceType type)27 inline bool IsUsb(DeviceType type)
28 {
29 return type == DEVICE_TYPE_USB_HEADSET || type == DEVICE_TYPE_USB_ARM_HEADSET;
30 }
31
32 /**
33 * @brief The AudioDeviceDescriptor provides
34 * different sets of audio devices and their roles
35 */
36 class AudioDeviceDescriptor : public Parcelable {
37 friend class AudioSystemManager;
38 public:
39 enum {
40 AUDIO_DEVICE_DESCRIPTOR,
41 DEVICE_INFO,
42 };
43
44 DeviceType deviceType_ = DEVICE_TYPE_NONE;
45 DeviceRole deviceRole_ = DEVICE_ROLE_NONE;
46 int32_t deviceId_ = 0;
47 int32_t channelMasks_ = 0;
48 int32_t channelIndexMasks_ = 0;
49 std::string deviceName_;
50 std::string macAddress_;
51 int32_t interruptGroupId_ = 0;
52 int32_t volumeGroupId_ = 0;
53 std::string networkId_;
54 uint16_t dmDeviceType_{0};
55 std::string displayName_;
56 DeviceStreamInfo audioStreamInfo_ = {};
57 DeviceCategory deviceCategory_ = CATEGORY_DEFAULT;
58 ConnectState connectState_ = CONNECTED;
59 // AudioDeviceDescriptor
60 bool exceptionFlag_ = false;
61 int64_t connectTimeStamp_ = 0;
62 std::shared_ptr<AudioDeviceDescriptor> pairDeviceDescriptor_;
63 bool isScoRealConnected_ = false;
64 bool isEnable_ = true;
65 // DeviceInfo
66 bool isLowLatencyDevice_ = false;
67 int32_t a2dpOffloadFlag_ = NO_A2DP_DEVICE;
68 // Other
69 int32_t descriptorType_ = AUDIO_DEVICE_DESCRIPTOR;
70 bool spatializationSupported_ = false;
71 bool hasPair_{false};
72
73 AudioDeviceDescriptor(int32_t descriptorType = AUDIO_DEVICE_DESCRIPTOR);
74
75 AudioDeviceDescriptor(DeviceType type, DeviceRole role);
76
77 AudioDeviceDescriptor(DeviceType type, DeviceRole role, int32_t interruptGroupId, int32_t volumeGroupId,
78 std::string networkId);
79
80 AudioDeviceDescriptor(const AudioDeviceDescriptor &deviceDescriptor);
81
82 AudioDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor);
83
84 virtual ~AudioDeviceDescriptor();
85
86 DeviceType getType() const;
87
88 DeviceRole getRole() const;
89
90 DeviceCategory GetDeviceCategory() const;
91
92 bool IsAudioDeviceDescriptor() const;
93
94 bool Marshalling(Parcel &parcel) const override;
95
96 bool MarshallingToDeviceDescriptor(Parcel &parcel) const;
97
98 bool MarshallingToDeviceInfo(Parcel &parcel) const;
99
100 bool Marshalling(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion) const;
101
102 bool MarshallingToDeviceInfo(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission,
103 int32_t apiVersion) const;
104
105 void Unmarshalling(Parcel &parcel);
106
107 static std::shared_ptr<AudioDeviceDescriptor> UnmarshallingPtr(Parcel &parcel);
108
109 void UnmarshallingToDeviceDescriptor(Parcel &parcel);
110
111 void UnmarshallingToDeviceInfo(Parcel &parcel);
112
113 void SetDeviceInfo(std::string deviceName, std::string macAddress);
114
115 void SetDeviceCapability(const DeviceStreamInfo &audioStreamInfo, int32_t channelMask,
116 int32_t channelIndexMasks = 0);
117
118 bool IsSameDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const;
119
120 bool IsSameDeviceInfo(const AudioDeviceDescriptor &deviceInfo) const;
121
122 bool IsPairedDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const;
123
124 DeviceType MapInternalToExternalDeviceType() const;
125
126 struct AudioDeviceDescriptorHash {
operatorAudioDeviceDescriptorHash127 size_t operator()(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor) const
128 {
129 if (deviceDescriptor == nullptr) {
130 return 0;
131 }
132 return std::hash<int32_t>{}(static_cast<int32_t>(deviceDescriptor->deviceType_)) ^
133 std::hash<int32_t>{}(static_cast<int32_t>(deviceDescriptor->deviceRole_)) ^
134 std::hash<std::string>{}(deviceDescriptor->macAddress_) ^
135 std::hash<std::string>{}(deviceDescriptor->networkId_);
136 }
137 };
138
139 struct AudioDeviceDescriptorEqual {
operatorAudioDeviceDescriptorEqual140 bool operator()(const std::shared_ptr<AudioDeviceDescriptor> &lhs,
141 const std::shared_ptr<AudioDeviceDescriptor> &rhs) const
142 {
143 if (lhs == nullptr && rhs == nullptr) {
144 return true;
145 }
146 if (lhs == nullptr || rhs == nullptr) {
147 return false;
148 }
149 return lhs->IsSameDeviceDesc(*rhs);
150 }
151 };
152 };
153 } // namespace AudioStandard
154 } // namespace OHOS
155 #endif // AUDIO_DEVICE_DESCRIPTOR_H