• 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 <map>
23 #include <set>
24 #include <vector>
25 
26 #include <media/TypeConverter.h>
27 #include <system/audio.h>
28 
29 namespace android {
30 
31 using ChannelMaskSet = std::set<audio_channel_mask_t>;
32 using DeviceTypeSet = std::set<audio_devices_t>;
33 using FormatSet = std::set<audio_format_t>;
34 using SampleRateSet = std::set<uint32_t>;
35 using MixerBehaviorSet = std::set<audio_mixer_behavior_t>;
36 
37 using FormatVector = std::vector<audio_format_t>;
38 using AudioProfileAttributesMultimap =
39         std::multimap<audio_format_t, std::pair<SampleRateSet, ChannelMaskSet>>;
40 
41 const DeviceTypeSet& getAudioDeviceOutAllSet();
42 const DeviceTypeSet& getAudioDeviceOutAllA2dpSet();
43 const DeviceTypeSet& getAudioDeviceOutAllScoSet();
44 const DeviceTypeSet& getAudioDeviceOutAllUsbSet();
45 const DeviceTypeSet& getAudioDeviceInAllSet();
46 const DeviceTypeSet& getAudioDeviceInAllUsbSet();
47 const DeviceTypeSet& getAudioDeviceOutAllBleSet();
48 const DeviceTypeSet& getAudioDeviceOutLeAudioUnicastSet();
49 const DeviceTypeSet& getAudioDeviceOutLeAudioBroadcastSet();
50 
51 template<typename T>
Intersection(const std::set<T> & a,const std::set<T> & b)52 static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
53     std::vector<T> intersection;
54     std::set_intersection(a.begin(), a.end(),
55                           b.begin(), b.end(),
56                           std::back_inserter(intersection));
57     return intersection;
58 }
59 
60 template<typename T>
SetIntersection(const std::set<T> & a,const std::set<T> b)61 static std::set<T> SetIntersection(const std::set<T>& a, const std::set<T> b) {
62     std::set<T> intersection;
63     std::set_intersection(a.begin(), a.end(),
64                           b.begin(), b.end(),
65                           std::inserter(intersection, intersection.begin()));
66     return intersection;
67 }
68 
asInMask(const ChannelMaskSet & channelMasks)69 static inline ChannelMaskSet asInMask(const ChannelMaskSet& channelMasks) {
70     ChannelMaskSet inMaskSet;
71     for (const auto &channel : channelMasks) {
72         if (audio_channel_mask_out_to_in(channel) != AUDIO_CHANNEL_INVALID) {
73             inMaskSet.insert(audio_channel_mask_out_to_in(channel));
74         } else if (audio_channel_mask_get_representation(channel)
75                     == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
76             inMaskSet.insert(channel);
77         }
78     }
79     return inMaskSet;
80 }
81 
asOutMask(const ChannelMaskSet & channelMasks)82 static inline ChannelMaskSet asOutMask(const ChannelMaskSet& channelMasks) {
83     ChannelMaskSet outMaskSet;
84     for (const auto &channel : channelMasks) {
85         if (audio_channel_mask_in_to_out(channel) != AUDIO_CHANNEL_INVALID) {
86             outMaskSet.insert(audio_channel_mask_in_to_out(channel));
87         } else if (audio_channel_mask_get_representation(channel)
88                     == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
89             outMaskSet.insert(channel);
90         }
91     }
92     return outMaskSet;
93 }
94 
isSingleDeviceType(const DeviceTypeSet & deviceTypes,audio_devices_t deviceType)95 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
96                                       audio_devices_t deviceType) {
97     return deviceTypes.size() == 1 && *(deviceTypes.begin()) == deviceType;
98 }
99 
100 typedef bool (*DeviceTypeUnaryPredicate)(audio_devices_t);
isSingleDeviceType(const DeviceTypeSet & deviceTypes,DeviceTypeUnaryPredicate p)101 static inline bool isSingleDeviceType(const DeviceTypeSet& deviceTypes,
102                                       DeviceTypeUnaryPredicate p) {
103     return deviceTypes.size() == 1 && p(*(deviceTypes.begin()));
104 }
105 
areAllOfSameDeviceType(const DeviceTypeSet & deviceTypes,std::function<bool (audio_devices_t)> p)106 static inline bool areAllOfSameDeviceType(const DeviceTypeSet& deviceTypes,
107                                           std::function<bool(audio_devices_t)> p) {
108     return std::all_of(deviceTypes.begin(), deviceTypes.end(), p);
109 }
110 
resetDeviceTypes(DeviceTypeSet & deviceTypes,audio_devices_t typeToAdd)111 static inline void resetDeviceTypes(DeviceTypeSet& deviceTypes, audio_devices_t typeToAdd) {
112     deviceTypes.clear();
113     deviceTypes.insert(typeToAdd);
114 }
115 
116 // FIXME: This is temporary helper function. Remove this when getting rid of all
117 //  bit mask usages of audio device types.
deviceTypesToBitMask(const DeviceTypeSet & deviceTypes)118 static inline audio_devices_t deviceTypesToBitMask(const DeviceTypeSet& deviceTypes) {
119     audio_devices_t types = AUDIO_DEVICE_NONE;
120     for (auto deviceType : deviceTypes) {
121         types = static_cast<audio_devices_t>(types | deviceType);
122     }
123     return types;
124 }
125 
126 std::string deviceTypesToString(const DeviceTypeSet& deviceTypes);
127 
128 bool deviceTypesToString(const DeviceTypeSet& deviceTypes, std::string &str);
129 
130 std::string dumpDeviceTypes(const DeviceTypeSet& deviceTypes);
131 
132 std::string dumpMixerBehaviors(const MixerBehaviorSet& mixerBehaviors);
133 
134 /**
135  * Return human readable string for device types.
136  */
toString(const DeviceTypeSet & deviceTypes)137 inline std::string toString(const DeviceTypeSet& deviceTypes) {
138     return deviceTypesToString(deviceTypes);
139 }
140 
141 /**
142  * Create audio profile attributes map by given audio profile array from the range of [first, last).
143  *
144  * @param profiles the array of audio profiles.
145  * @param first the first index of the profile.
146  * @param last the last index of the profile.
147  * @return a multipmap of audio format to pair of corresponding sample rates and channel masks set.
148  */
149 AudioProfileAttributesMultimap createAudioProfilesAttrMap(audio_profile profiles[],
150                                                           uint32_t first,
151                                                           uint32_t last);
152 
153 /**
154  * Populate audio profiles according to given profile attributes, format, channel masks and
155  * sample rates.
156  *
157  * The function will first go over all pairs of channel masks and sample rates that are present in
158  * the profile attributes of the given map. Note that the channel masks and the sample rates that
159  * are not present in the collections of all valid channel masks and all valid sample rates will be
160  * excluded. After that, if there are channel masks and sample rates that present in the all values
161  * collection but not in profile attributes, they will also be place in a new audio profile in the
162  * profile array.
163  *
164  * Note that if the resulting index of the audio profile exceeds the maximum, no new audio profiles
165  * will be placed in the array.
166  *
167  * @param profileAttrs a multimap that contains format and its corresponding channel masks and
168  *                     sample rates.
169  * @param format the targeted audio format.
170  * @param allChannelMasks all valid channel masks for the format.
171  * @param allSampleRates all valid sample rates for the format.
172  * @param audioProfiles the audio profile array.
173  * @param numAudioProfiles the start index to put audio profile in the array. The value will be
174  *                         updated if there is new audio profile placed.
175  * @param maxAudioProfiles the maximum number of audio profile.
176  */
177 void populateAudioProfiles(const AudioProfileAttributesMultimap& profileAttrs,
178                            audio_format_t format,
179                            ChannelMaskSet allChannelMasks,
180                            SampleRateSet allSampleRates,
181                            audio_profile audioProfiles[],
182                            uint32_t* numAudioProfiles,
183                            uint32_t maxAudioProfiles = AUDIO_PORT_MAX_AUDIO_PROFILES);
184 
185 
186 } // namespace android
187