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
53 template<typename T>
SetIntersection(const std::set<T> & a,const std::set<T> b)54 static std::set<T> SetIntersection(const std::set<T>& a, const std::set<T> b) {
55 std::set<T> intersection;
56 std::set_intersection(a.begin(), a.end(),
57 b.begin(), b.end(),
58 std::inserter(intersection, intersection.begin()));
59 return intersection;
60 }
61
asInMask(const ChannelMaskSet & channelMasks)62 static inline ChannelMaskSet asInMask(const ChannelMaskSet& channelMasks) {
63 ChannelMaskSet inMaskSet;
64 for (const auto &channel : channelMasks) {
65 if (audio_channel_mask_out_to_in(channel) != AUDIO_CHANNEL_INVALID) {
66 inMaskSet.insert(audio_channel_mask_out_to_in(channel));
67 }
68 }
69 return inMaskSet;
70 }
71
asOutMask(const ChannelMaskSet & channelMasks)72 static inline ChannelMaskSet asOutMask(const ChannelMaskSet& channelMasks) {
73 ChannelMaskSet outMaskSet;
74 for (const auto &channel : channelMasks) {
75 if (audio_channel_mask_in_to_out(channel) != AUDIO_CHANNEL_INVALID) {
76 outMaskSet.insert(audio_channel_mask_in_to_out(channel));
77 }
78 }
79 return outMaskSet;
80 }
81
isSingleDeviceType(const DeviceTypeSet & deviceTypes,audio_devices_t deviceType)82 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
83 audio_devices_t deviceType) {
84 return deviceTypes.size() == 1 && *(deviceTypes.begin()) == deviceType;
85 }
86
87 typedef bool (*DeviceTypeUnaryPredicate)(audio_devices_t);
isSingleDeviceType(const DeviceTypeSet & deviceTypes,DeviceTypeUnaryPredicate p)88 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
89 DeviceTypeUnaryPredicate p) {
90 return deviceTypes.size() == 1 && p(*(deviceTypes.begin()));
91 }
92
areAllOfSameDeviceType(const DeviceTypeSet & deviceTypes,std::function<bool (audio_devices_t)> p)93 static inline bool areAllOfSameDeviceType(const DeviceTypeSet& deviceTypes,
94 std::function<bool(audio_devices_t)> p) {
95 return std::all_of(deviceTypes.begin(), deviceTypes.end(), p);
96 }
97
resetDeviceTypes(DeviceTypeSet & deviceTypes,audio_devices_t typeToAdd)98 static inline void resetDeviceTypes(DeviceTypeSet& deviceTypes, audio_devices_t typeToAdd) {
99 deviceTypes.clear();
100 deviceTypes.insert(typeToAdd);
101 }
102
103 // FIXME: This is temporary helper function. Remove this when getting rid of all
104 // bit mask usages of audio device types.
deviceTypesToBitMask(const DeviceTypeSet & deviceTypes)105 static inline audio_devices_t deviceTypesToBitMask(const DeviceTypeSet& deviceTypes) {
106 audio_devices_t types = AUDIO_DEVICE_NONE;
107 for (auto deviceType : deviceTypes) {
108 types = static_cast<audio_devices_t>(types | deviceType);
109 }
110 return types;
111 }
112
113 // FIXME: This is temporary helper function. Remove this when getting rid of all
114 // bit mask usages of audio device types.
deviceTypesFromBitMask(audio_devices_t types)115 static inline DeviceTypeSet deviceTypesFromBitMask(audio_devices_t types) {
116 DeviceTypeSet deviceTypes;
117 if ((types & AUDIO_DEVICE_BIT_IN) == 0) {
118 for (auto deviceType : AUDIO_DEVICE_OUT_ALL_ARRAY) {
119 if ((types & deviceType) == deviceType) {
120 deviceTypes.insert(deviceType);
121 }
122 }
123 } else {
124 for (auto deviceType : AUDIO_DEVICE_IN_ALL_ARRAY) {
125 if ((types & deviceType) == deviceType) {
126 deviceTypes.insert(deviceType);
127 }
128 }
129 }
130 return deviceTypes;
131 }
132
133 bool deviceTypesToString(const DeviceTypeSet& deviceTypes, std::string &str);
134
135 std::string dumpDeviceTypes(const DeviceTypeSet& deviceTypes);
136
137 /**
138 * Return human readable string for device types.
139 */
140 std::string toString(const DeviceTypeSet& deviceTypes);
141
142
143 } // namespace android
144