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