1 /*
2 * Copyright (c) 2023 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 #ifndef AUDIO_DEVICE_INFO_H
16 #define AUDIO_DEVICE_INFO_H
17
18 #include <parcel.h>
19 #include <audio_stream_info.h>
20 #include <set>
21 #include <limits>
22 #include <unordered_set>
23
24 namespace OHOS {
25 namespace AudioStandard {
26 constexpr size_t AUDIO_DEVICE_INFO_SIZE_LIMIT = 30;
27 constexpr int32_t INVALID_GROUP_ID = -1;
28
29 const std::string LOCAL_NETWORK_ID = "LocalDevice";
30 const std::string REMOTE_NETWORK_ID = "RemoteDevice";
31
32 enum API_VERSION {
33 API_7 = 7,
34 API_8,
35 API_9,
36 API_10,
37 API_11,
38 API_MAX = 1000
39 };
40
41 enum DeviceFlag {
42 /**
43 * Device flag none.
44 */
45 NONE_DEVICES_FLAG = 0,
46 /**
47 * Indicates all output audio devices.
48 */
49 OUTPUT_DEVICES_FLAG = 1,
50 /**
51 * Indicates all input audio devices.
52 */
53 INPUT_DEVICES_FLAG = 2,
54 /**
55 * Indicates all audio devices.
56 */
57 ALL_DEVICES_FLAG = 3,
58 /**
59 * Indicates all distributed output audio devices.
60 */
61 DISTRIBUTED_OUTPUT_DEVICES_FLAG = 4,
62 /**
63 * Indicates all distributed input audio devices.
64 */
65 DISTRIBUTED_INPUT_DEVICES_FLAG = 8,
66 /**
67 * Indicates all distributed audio devices.
68 */
69 ALL_DISTRIBUTED_DEVICES_FLAG = 12,
70 /**
71 * Indicates all local and distributed audio devices.
72 */
73 ALL_L_D_DEVICES_FLAG = 15,
74 /**
75 * Device flag max count.
76 */
77 DEVICE_FLAG_MAX
78 };
79
80 enum DeviceRole {
81 /**
82 * Device role none.
83 */
84 DEVICE_ROLE_NONE = -1,
85 /**
86 * Input device role.
87 */
88 INPUT_DEVICE = 1,
89 /**
90 * Output device role.
91 */
92 OUTPUT_DEVICE = 2,
93 /**
94 * Device role max count.
95 */
96 DEVICE_ROLE_MAX
97 };
98
99 enum DeviceType {
100 /**
101 * Indicates device type none.
102 */
103 DEVICE_TYPE_NONE = -1,
104 /**
105 * Indicates invalid device
106 */
107 DEVICE_TYPE_INVALID = 0,
108 /**
109 * Indicates a built-in earpiece device
110 */
111 DEVICE_TYPE_EARPIECE = 1,
112 /**
113 * Indicates a speaker built in a device.
114 */
115 DEVICE_TYPE_SPEAKER = 2,
116 /**
117 * Indicates a headset, which is the combination of a pair of headphones and a microphone.
118 */
119 DEVICE_TYPE_WIRED_HEADSET = 3,
120 /**
121 * Indicates a pair of wired headphones.
122 */
123 DEVICE_TYPE_WIRED_HEADPHONES = 4,
124 /**
125 * Indicates a Bluetooth device used for telephony.
126 */
127 DEVICE_TYPE_BLUETOOTH_SCO = 7,
128 /**
129 * Indicates a Bluetooth device supporting the Advanced Audio Distribution Profile (A2DP).
130 */
131 DEVICE_TYPE_BLUETOOTH_A2DP = 8,
132 /**
133 * Indicates a microphone built in a device.
134 */
135 DEVICE_TYPE_MIC = 15,
136 /**
137 * Indicates a microphone built in a device.
138 */
139 DEVICE_TYPE_WAKEUP = 16,
140 /**
141 * Indicates a microphone built in a device.
142 */
143 DEVICE_TYPE_USB_HEADSET = 22,
144 /**
145 * Indicates a display device.
146 */
147 DEVICE_TYPE_DP = 23,
148 /**
149 * Indicates a virtual remote cast device
150 */
151 DEVICE_TYPE_REMOTE_CAST = 24,
152 /**
153 * Indicates a debug sink device
154 */
155 DEVICE_TYPE_FILE_SINK = 50,
156 /**
157 * Indicates a debug source device
158 */
159 DEVICE_TYPE_FILE_SOURCE = 51,
160 /**
161 * Indicates any headset/headphone for disconnect
162 */
163 DEVICE_TYPE_EXTERN_CABLE = 100,
164 /**
165 * Indicates default device
166 */
167 DEVICE_TYPE_DEFAULT = 1000,
168 /**
169 * Indicates a usb-arm device.
170 */
171 DEVICE_TYPE_USB_ARM_HEADSET = 1001,
172 /**
173 * Indicates device type max count.
174 */
175 DEVICE_TYPE_MAX
176 };
177
178 inline const std::unordered_set<DeviceType> INPUT_DEVICE_TYPE_SET = {
179 DeviceType::DEVICE_TYPE_WIRED_HEADSET,
180 DeviceType::DEVICE_TYPE_BLUETOOTH_SCO,
181 DeviceType::DEVICE_TYPE_MIC,
182 DeviceType::DEVICE_TYPE_WAKEUP,
183 DeviceType::DEVICE_TYPE_USB_HEADSET,
184 DeviceType::DEVICE_TYPE_USB_ARM_HEADSET,
185 DeviceType::DEVICE_TYPE_FILE_SOURCE,
186 };
187
IsInputDevice(DeviceType deviceType)188 inline bool IsInputDevice(DeviceType deviceType)
189 {
190 return INPUT_DEVICE_TYPE_SET.count(deviceType) > 0;
191 }
192
IsInputDevice(DeviceType deviceType,DeviceRole deviceRole)193 inline bool IsInputDevice(DeviceType deviceType, DeviceRole deviceRole)
194 {
195 // Arm usb device distinguishes input and output through device roles.
196 if (deviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
197 return deviceRole == INPUT_DEVICE || deviceRole == DEVICE_ROLE_MAX;
198 } else {
199 return INPUT_DEVICE_TYPE_SET.count(deviceType) > 0;
200 }
201 }
202
203 inline const std::unordered_set<DeviceType> OUTPUT_DEVICE_TYPE_SET = {
204 DeviceType::DEVICE_TYPE_EARPIECE,
205 DeviceType::DEVICE_TYPE_SPEAKER,
206 DeviceType::DEVICE_TYPE_WIRED_HEADSET,
207 DeviceType::DEVICE_TYPE_WIRED_HEADPHONES,
208 DeviceType::DEVICE_TYPE_BLUETOOTH_SCO,
209 DeviceType::DEVICE_TYPE_BLUETOOTH_A2DP,
210 DeviceType::DEVICE_TYPE_USB_HEADSET,
211 DeviceType::DEVICE_TYPE_DP,
212 DeviceType::DEVICE_TYPE_USB_ARM_HEADSET,
213 DeviceType::DEVICE_TYPE_FILE_SINK,
214 DeviceType::DEVICE_TYPE_REMOTE_CAST,
215 };
216
IsOutputDevice(DeviceType deviceType)217 inline bool IsOutputDevice(DeviceType deviceType)
218 {
219 return OUTPUT_DEVICE_TYPE_SET.count(deviceType) > 0;
220 }
221
IsOutputDevice(DeviceType deviceType,DeviceRole deviceRole)222 inline bool IsOutputDevice(DeviceType deviceType, DeviceRole deviceRole)
223 {
224 // Arm usb device distinguishes input and output through device roles.
225 if (deviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
226 return deviceRole == OUTPUT_DEVICE || deviceRole == DEVICE_ROLE_MAX;
227 } else {
228 return OUTPUT_DEVICE_TYPE_SET.count(deviceType) > 0;
229 }
230 }
231
232 enum DeviceBlockStatus {
233 DEVICE_UNBLOCKED = 0,
234 DEVICE_BLOCKED = 1,
235 };
236
237 enum DeviceChangeType {
238 CONNECT = 0,
239 DISCONNECT = 1,
240 };
241
242 enum DeviceVolumeType {
243 EARPIECE_VOLUME_TYPE = 0,
244 SPEAKER_VOLUME_TYPE = 1,
245 HEADSET_VOLUME_TYPE = 2,
246 };
247
248 inline const std::unordered_set<DeviceType> ACTIVE_DEVICE_TYPE_SET = {
249 DeviceType::DEVICE_TYPE_EARPIECE,
250 DeviceType::DEVICE_TYPE_SPEAKER,
251 DeviceType::DEVICE_TYPE_BLUETOOTH_SCO,
252 DeviceType::DEVICE_TYPE_USB_HEADSET,
253 DeviceType::DEVICE_TYPE_FILE_SINK,
254 };
255
IsActiveDeviceType(DeviceType deviceType)256 inline bool IsActiveDeviceType(DeviceType deviceType)
257 {
258 return ACTIVE_DEVICE_TYPE_SET.count(deviceType) > 0;
259 }
260
261 inline const std::unordered_set<DeviceType> COMMUNICATION_DEVICE_TYPE_SET = {
262 DeviceType::DEVICE_TYPE_SPEAKER,
263 };
264
IsCommunicationDeviceType(DeviceType deviceType)265 inline bool IsCommunicationDeviceType(DeviceType deviceType)
266 {
267 return COMMUNICATION_DEVICE_TYPE_SET.count(deviceType) > 0;
268 }
269
270 enum AudioDeviceManagerType {
271 DEV_MGR_UNKNOW = 0,
272 LOCAL_DEV_MGR,
273 REMOTE_DEV_MGR,
274 BLUETOOTH_DEV_MGR,
275 };
276
277 enum AudioDevicePrivacyType {
278 TYPE_PRIVACY,
279 TYPE_PUBLIC,
280 TYPE_NEGATIVE,
281 };
282
283 enum DeviceCategory {
284 CATEGORY_DEFAULT = 0,
285 BT_HEADPHONE = 1 << 0,
286 BT_SOUNDBOX = 1 << 1,
287 BT_CAR = 1 << 2,
288 BT_GLASSES = 1 << 3,
289 BT_WATCH = 1 << 4,
290 BT_HEARAID = 1 << 5,
291 BT_UNWEAR_HEADPHONE = 1 << 6,
292 };
293
294 enum DeviceUsage {
295 MEDIA = 1 << 0,
296 VOICE = 1 << 1,
297 RECOGNITION = 1 << 2,
298 ALL_USAGE = (1 << 3) - 1, // Represents the bitwise OR of all the above usages.
299 };
300
301 enum DeviceInfoUpdateCommand {
302 CATEGORY_UPDATE = 1,
303 CONNECTSTATE_UPDATE,
304 ENABLE_UPDATE,
305 EXCEPTION_FLAG_UPDATE,
306 };
307
308 enum ConnectState {
309 CONNECTED,
310 SUSPEND_CONNECTED,
311 VIRTUAL_CONNECTED,
312 DEACTIVE_CONNECTED
313 };
314
315 enum PreferredType {
316 AUDIO_MEDIA_RENDER = 0,
317 AUDIO_CALL_RENDER = 1,
318 AUDIO_CALL_CAPTURE = 2,
319 AUDIO_RING_RENDER = 3,
320 AUDIO_RECORD_CAPTURE = 4,
321 AUDIO_TONE_RENDER = 5,
322 };
323
324 enum BluetoothOffloadState {
325 NO_A2DP_DEVICE = 0,
326 A2DP_NOT_OFFLOAD = 1,
327 A2DP_OFFLOAD = 2,
328 };
329
330 struct DevicePrivacyInfo {
331 std::string deviceName;
332 DeviceType deviceType;
333 DeviceRole deviceRole;
334 DeviceCategory deviceCategory;
335 DeviceUsage deviceUsage;
336 };
337
MarshallingSetInt32(const std::set<T> & value,Parcel & parcel)338 template<typename T> bool MarshallingSetInt32(const std::set<T> &value, Parcel &parcel)
339 {
340 size_t size = value.size();
341 if (!parcel.WriteUint64(size)) {
342 return false;
343 }
344 for (const auto &i : value) {
345 if (!parcel.WriteInt32(i)) {
346 return false;
347 }
348 }
349 return true;
350 }
351
352 template<typename T> std::set<T> UnmarshallingSetInt32(Parcel &parcel,
353 const size_t maxSize = std::numeric_limits<size_t>::max())
354 {
355 size_t size = parcel.ReadUint64();
356 // due to security concerns, sizelimit has been imposed
357 if (size > maxSize) {
358 size = maxSize;
359 }
360
361 std::set<T> res;
362 for (size_t i = 0; i < size; i++) {
363 res.insert(static_cast<T>(parcel.ReadInt32()));
364 }
365 return res;
366 }
367
368 struct DeviceStreamInfo {
369 AudioEncodingType encoding = AudioEncodingType::ENCODING_PCM;
370 AudioSampleFormat format = AudioSampleFormat::INVALID_WIDTH;
371 AudioChannelLayout channelLayout = AudioChannelLayout::CH_LAYOUT_UNKNOWN;
372 std::set<AudioSamplingRate> samplingRate;
373 std::set<AudioChannel> channels;
374
DeviceStreamInfoDeviceStreamInfo375 DeviceStreamInfo(AudioSamplingRate samplingRate_, AudioEncodingType encoding_, AudioSampleFormat format_,
376 AudioChannel channels_) : encoding(encoding_), format(format_),
377 samplingRate({samplingRate_}), channels({channels_})
378 {}
DeviceStreamInfoDeviceStreamInfo379 DeviceStreamInfo(AudioStreamInfo audioStreamInfo) : DeviceStreamInfo(audioStreamInfo.samplingRate,
380 audioStreamInfo.encoding, audioStreamInfo.format, audioStreamInfo.channels)
381 {}
382 DeviceStreamInfo() = default;
383
MarshallingDeviceStreamInfo384 bool Marshalling(Parcel &parcel) const
385 {
386 return parcel.WriteInt32(static_cast<int32_t>(encoding))
387 && parcel.WriteInt32(static_cast<int32_t>(format))
388 && MarshallingSetInt32(samplingRate, parcel)
389 && MarshallingSetInt32(channels, parcel);
390 }
UnmarshallingDeviceStreamInfo391 void Unmarshalling(Parcel &parcel)
392 {
393 encoding = static_cast<AudioEncodingType>(parcel.ReadInt32());
394 format = static_cast<AudioSampleFormat>(parcel.ReadInt32());
395 samplingRate = UnmarshallingSetInt32<AudioSamplingRate>(parcel, AUDIO_DEVICE_INFO_SIZE_LIMIT);
396 channels = UnmarshallingSetInt32<AudioChannel>(parcel, AUDIO_DEVICE_INFO_SIZE_LIMIT);
397 }
398
CheckParamsDeviceStreamInfo399 bool CheckParams()
400 {
401 if (samplingRate.size() == 0) {
402 return false;
403 }
404 if (channels.size() == 0) {
405 return false;
406 }
407 return true;
408 }
409 };
410
411 enum class AudioStreamDeviceChangeReason {
412 UNKNOWN = 0,
413 NEW_DEVICE_AVAILABLE = 1,
414 OLD_DEVICE_UNAVALIABLE = 2,
415 OVERRODE = 3
416 };
417
418 class AudioStreamDeviceChangeReasonExt {
419 public:
420 enum class ExtEnum {
421 UNKNOWN = 0,
422 NEW_DEVICE_AVAILABLE = 1,
423 OLD_DEVICE_UNAVALIABLE = 2,
424 OVERRODE = 3,
425 MIN = 1000,
426 OLD_DEVICE_UNAVALIABLE_EXT = 1000,
427 SET_AUDIO_SCENE = 1001,
428 SET_DEFAULT_OUTPUT_DEVICE = 1002
429 };
430
AudioStreamDeviceChangeReason()431 operator AudioStreamDeviceChangeReason() const
432 {
433 if (reason_ < ExtEnum::MIN) {
434 return static_cast<AudioStreamDeviceChangeReason>(reason_);
435 } else {
436 return AudioStreamDeviceChangeReason::UNKNOWN;
437 }
438 }
439
440 operator int() const
441 {
442 return static_cast<int>(reason_);
443 }
444
AudioStreamDeviceChangeReasonExt(const AudioStreamDeviceChangeReason & reason)445 AudioStreamDeviceChangeReasonExt(const AudioStreamDeviceChangeReason &reason)
446 : reason_(static_cast<ExtEnum>(reason)) {}
447
AudioStreamDeviceChangeReasonExt(const ExtEnum & reason)448 AudioStreamDeviceChangeReasonExt(const ExtEnum &reason) : reason_(reason) {}
449
IsOldDeviceUnavaliable()450 bool IsOldDeviceUnavaliable() const
451 {
452 return reason_ == ExtEnum::OLD_DEVICE_UNAVALIABLE;
453 }
454
IsOldDeviceUnavaliableExt()455 bool IsOldDeviceUnavaliableExt() const
456 {
457 return reason_ == ExtEnum::OLD_DEVICE_UNAVALIABLE_EXT;
458 }
459
isOverride()460 bool isOverride() const
461 {
462 return reason_ == ExtEnum::OVERRODE;
463 }
464
isSetAudioScene()465 bool isSetAudioScene() const
466 {
467 return reason_ == ExtEnum::SET_AUDIO_SCENE;
468 }
469
isSetDefaultOutputDevice()470 bool isSetDefaultOutputDevice() const
471 {
472 return reason_ == ExtEnum::SET_DEFAULT_OUTPUT_DEVICE;
473 }
474
475 private:
476 ExtEnum reason_;
477 };
478 } // namespace AudioStandard
479 } // namespace OHOS
480 #endif // AUDIO_DEVICE_INFO_H