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