1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <algorithm>
20 #include <functional>
21 #include <iterator>
22 #include <set>
23 #include <vector>
24
25 #include <media/TypeConverter.h>
26 #include <system/audio.h>
27
28 namespace android {
29
30 using ChannelMaskSet = std::set<audio_channel_mask_t>;
31 using DeviceTypeSet = std::set<audio_devices_t>;
32 using FormatSet = std::set<audio_format_t>;
33 using SampleRateSet = std::set<uint32_t>;
34
35 using FormatVector = std::vector<audio_format_t>;
36
37 const DeviceTypeSet& getAudioDeviceOutAllSet();
38 const DeviceTypeSet& getAudioDeviceOutAllA2dpSet();
39 const DeviceTypeSet& getAudioDeviceOutAllScoSet();
40 const DeviceTypeSet& getAudioDeviceOutAllUsbSet();
41 const DeviceTypeSet& getAudioDeviceInAllSet();
42 const DeviceTypeSet& getAudioDeviceInAllUsbSet();
43
44 template<typename T>
Intersection(const std::set<T> & a,const std::set<T> & b)45 static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
46 std::vector<T> intersection;
47 std::set_intersection(a.begin(), a.end(),
48 b.begin(), b.end(),
49 std::back_inserter(intersection));
50 return intersection;
51 }
52
asInMask(const ChannelMaskSet & channelMasks)53 static inline ChannelMaskSet asInMask(const ChannelMaskSet& channelMasks) {
54 ChannelMaskSet inMaskSet;
55 for (const auto &channel : channelMasks) {
56 if (audio_channel_mask_out_to_in(channel) != AUDIO_CHANNEL_INVALID) {
57 inMaskSet.insert(audio_channel_mask_out_to_in(channel));
58 }
59 }
60 return inMaskSet;
61 }
62
asOutMask(const ChannelMaskSet & channelMasks)63 static inline ChannelMaskSet asOutMask(const ChannelMaskSet& channelMasks) {
64 ChannelMaskSet outMaskSet;
65 for (const auto &channel : channelMasks) {
66 if (audio_channel_mask_in_to_out(channel) != AUDIO_CHANNEL_INVALID) {
67 outMaskSet.insert(audio_channel_mask_in_to_out(channel));
68 }
69 }
70 return outMaskSet;
71 }
72
isSingleDeviceType(const DeviceTypeSet & deviceTypes,audio_devices_t deviceType)73 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
74 audio_devices_t deviceType) {
75 return deviceTypes.size() == 1 && *(deviceTypes.begin()) == deviceType;
76 }
77
78 typedef bool (*DeviceTypeUnaryPredicate)(audio_devices_t);
isSingleDeviceType(const DeviceTypeSet & deviceTypes,DeviceTypeUnaryPredicate p)79 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
80 DeviceTypeUnaryPredicate p) {
81 return deviceTypes.size() == 1 && p(*(deviceTypes.begin()));
82 }
83
areAllOfSameDeviceType(const DeviceTypeSet & deviceTypes,std::function<bool (audio_devices_t)> p)84 static inline bool areAllOfSameDeviceType(const DeviceTypeSet& deviceTypes,
85 std::function<bool(audio_devices_t)> p) {
86 return std::all_of(deviceTypes.begin(), deviceTypes.end(), p);
87 }
88
resetDeviceTypes(DeviceTypeSet & deviceTypes,audio_devices_t typeToAdd)89 static inline void resetDeviceTypes(DeviceTypeSet& deviceTypes, audio_devices_t typeToAdd) {
90 deviceTypes.clear();
91 deviceTypes.insert(typeToAdd);
92 }
93
94 // FIXME: This is temporary helper function. Remove this when getting rid of all
95 // bit mask usages of audio device types.
deviceTypesToBitMask(const DeviceTypeSet & deviceTypes)96 static inline audio_devices_t deviceTypesToBitMask(const DeviceTypeSet& deviceTypes) {
97 audio_devices_t types = AUDIO_DEVICE_NONE;
98 for (auto deviceType : deviceTypes) {
99 types |= deviceType;
100 }
101 return types;
102 }
103
104 // FIXME: This is temporary helper function. Remove this when getting rid of all
105 // bit mask usages of audio device types.
deviceTypesFromBitMask(audio_devices_t types)106 static inline DeviceTypeSet deviceTypesFromBitMask(audio_devices_t types) {
107 DeviceTypeSet deviceTypes;
108 if ((types & AUDIO_DEVICE_BIT_IN) == 0) {
109 for (auto deviceType : AUDIO_DEVICE_OUT_ALL_ARRAY) {
110 if ((types & deviceType) == deviceType) {
111 deviceTypes.insert(deviceType);
112 }
113 }
114 } else {
115 for (auto deviceType : AUDIO_DEVICE_IN_ALL_ARRAY) {
116 if ((types & deviceType) == deviceType) {
117 deviceTypes.insert(deviceType);
118 }
119 }
120 }
121 return deviceTypes;
122 }
123
124 bool deviceTypesToString(const DeviceTypeSet& deviceTypes, std::string &str);
125
126 std::string dumpDeviceTypes(const DeviceTypeSet& deviceTypes);
127
128 /**
129 * Return human readable string for device types.
130 */
131 std::string toString(const DeviceTypeSet& deviceTypes);
132
133
134 } // namespace android