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