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