• 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 #include <sstream>
18 #include <string>
19 
20 #include <media/AudioContainers.h>
21 
22 namespace android {
23 
getAudioDeviceOutAllSet()24 const DeviceTypeSet& getAudioDeviceOutAllSet() {
25     static const DeviceTypeSet audioDeviceOutAllSet = DeviceTypeSet(
26             std::begin(AUDIO_DEVICE_OUT_ALL_ARRAY),
27             std::end(AUDIO_DEVICE_OUT_ALL_ARRAY));
28     return audioDeviceOutAllSet;
29 }
30 
getAudioDeviceOutAllA2dpSet()31 const DeviceTypeSet& getAudioDeviceOutAllA2dpSet() {
32     static const DeviceTypeSet audioDeviceOutAllA2dpSet = DeviceTypeSet(
33             std::begin(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY),
34             std::end(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY));
35     return audioDeviceOutAllA2dpSet;
36 }
37 
getAudioDeviceOutAllScoSet()38 const DeviceTypeSet& getAudioDeviceOutAllScoSet() {
39     static const DeviceTypeSet audioDeviceOutAllScoSet = DeviceTypeSet(
40             std::begin(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY),
41             std::end(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY));
42     return audioDeviceOutAllScoSet;
43 }
44 
getAudioDeviceOutAllUsbSet()45 const DeviceTypeSet& getAudioDeviceOutAllUsbSet() {
46     static const DeviceTypeSet audioDeviceOutAllUsbSet = DeviceTypeSet(
47             std::begin(AUDIO_DEVICE_OUT_ALL_USB_ARRAY),
48             std::end(AUDIO_DEVICE_OUT_ALL_USB_ARRAY));
49     return audioDeviceOutAllUsbSet;
50 }
51 
getAudioDeviceInAllSet()52 const DeviceTypeSet& getAudioDeviceInAllSet() {
53     static const DeviceTypeSet audioDeviceInAllSet = DeviceTypeSet(
54             std::begin(AUDIO_DEVICE_IN_ALL_ARRAY),
55             std::end(AUDIO_DEVICE_IN_ALL_ARRAY));
56     return audioDeviceInAllSet;
57 }
58 
getAudioDeviceInAllUsbSet()59 const DeviceTypeSet& getAudioDeviceInAllUsbSet() {
60     static const DeviceTypeSet audioDeviceInAllUsbSet = DeviceTypeSet(
61             std::begin(AUDIO_DEVICE_IN_ALL_USB_ARRAY),
62             std::end(AUDIO_DEVICE_IN_ALL_USB_ARRAY));
63     return audioDeviceInAllUsbSet;
64 }
65 
getAudioDeviceOutAllBleSet()66 const DeviceTypeSet& getAudioDeviceOutAllBleSet() {
67     static const DeviceTypeSet audioDeviceOutAllBleSet = DeviceTypeSet(
68             std::begin(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY),
69             std::end(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY));
70     return audioDeviceOutAllBleSet;
71 }
72 
getAudioDeviceOutLeAudioUnicastSet()73 const DeviceTypeSet& getAudioDeviceOutLeAudioUnicastSet() {
74     static const DeviceTypeSet audioDeviceOutLeAudioUnicastSet = DeviceTypeSet(
75             std::begin(AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY),
76             std::end(AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY));
77     return audioDeviceOutLeAudioUnicastSet;
78 }
79 
getAudioDeviceOutLeAudioBroadcastSet()80 const DeviceTypeSet& getAudioDeviceOutLeAudioBroadcastSet() {
81     static const DeviceTypeSet audioDeviceOutLeAudioUnicastSet = DeviceTypeSet(
82             std::begin(AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY),
83             std::end(AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY));
84     return audioDeviceOutLeAudioUnicastSet;
85 }
86 
getAudioDeviceOutPickForVolumeSet()87 const DeviceTypeSet& getAudioDeviceOutPickForVolumeSet() {
88     static const DeviceTypeSet audioDevicePickForVolumeSet = DeviceTypeSet(
89             std::begin(AUDIO_DEVICE_OUT_PICK_FOR_VOLUME_ARRAY),
90             std::end(AUDIO_DEVICE_OUT_PICK_FOR_VOLUME_ARRAY));
91     return audioDevicePickForVolumeSet;
92 }
93 
deviceTypesToString(const DeviceTypeSet & deviceTypes)94 std::string deviceTypesToString(const DeviceTypeSet &deviceTypes) {
95     if (deviceTypes.empty()) {
96         return "Empty device types";
97     }
98     std::stringstream ss;
99     for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
100         if (it != deviceTypes.begin()) {
101             ss << ", ";
102         }
103         const char* strType = audio_device_to_string(*it);
104         if (strlen(strType) != 0) {
105             ss << strType;
106         } else {
107             ss << "unknown type:0x" << std::hex << *it;
108         }
109     }
110     return ss.str();
111 }
112 
deviceTypesToString(const DeviceTypeSet & deviceTypes,std::string & str)113 bool deviceTypesToString(const DeviceTypeSet &deviceTypes, std::string &str) {
114     str = deviceTypesToString(deviceTypes);
115     return true;
116 }
117 
dumpDeviceTypes(const DeviceTypeSet & deviceTypes)118 std::string dumpDeviceTypes(const DeviceTypeSet &deviceTypes) {
119     std::stringstream ss;
120     for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
121         if (it != deviceTypes.begin()) {
122             ss << ", ";
123         }
124         ss << "0x" << std::hex << (*it);
125     }
126     return ss.str();
127 }
128 
dumpMixerBehaviors(const MixerBehaviorSet & mixerBehaviors)129 std::string dumpMixerBehaviors(const MixerBehaviorSet& mixerBehaviors) {
130     std::stringstream ss;
131     for (auto it = mixerBehaviors.begin(); it != mixerBehaviors.end(); ++it) {
132         if (it != mixerBehaviors.begin()) {
133             ss << ", ";
134         }
135         ss << (*it);
136     }
137     return ss.str();
138 }
139 
toString(const DeviceIdVector & deviceIds)140 std::string toString(const DeviceIdVector& deviceIds) {
141     if (deviceIds.empty()) {
142         return "AUDIO_PORT_HANDLE_NONE";
143     }
144     std::stringstream ss;
145     for (auto it = deviceIds.begin(); it != deviceIds.end(); ++it) {
146         if (it != deviceIds.begin()) {
147             ss << ", ";
148         }
149         ss << *it;
150     }
151     return ss.str();
152 }
153 
getFirstDeviceId(const DeviceIdVector & deviceIds)154 audio_port_handle_t getFirstDeviceId(const DeviceIdVector& deviceIds) {
155     if (deviceIds.empty()) {
156         return AUDIO_PORT_HANDLE_NONE;
157     }
158     return deviceIds[0];
159 }
160 
areDeviceIdsEqual(const DeviceIdVector & first,const DeviceIdVector & second)161 bool areDeviceIdsEqual(const DeviceIdVector& first, const DeviceIdVector& second) {
162     const std::set<audio_port_handle_t> firstSet(first.begin(), first.end());
163     const std::set<audio_port_handle_t> secondSet(second.begin(), second.end());
164     return firstSet == secondSet;
165 }
166 
createAudioProfilesAttrMap(audio_profile profiles[],uint32_t first,uint32_t last)167 AudioProfileAttributesMultimap createAudioProfilesAttrMap(audio_profile profiles[],
168                                                           uint32_t first,
169                                                           uint32_t last) {
170     AudioProfileAttributesMultimap result;
171     for (uint32_t i = first; i < last; ++i) {
172         SampleRateSet sampleRates(profiles[i].sample_rates,
173                                   profiles[i].sample_rates + profiles[i].num_sample_rates);
174         ChannelMaskSet channelMasks(profiles[i].channel_masks,
175                                     profiles[i].channel_masks + profiles[i].num_channel_masks);
176         result.emplace(profiles[i].format, std::make_pair(sampleRates, channelMasks));
177     }
178     return result;
179 }
180 
181 namespace {
182 
populateAudioProfile(audio_format_t format,const ChannelMaskSet & channelMasks,const SampleRateSet & samplingRates,audio_profile * profile)183 void populateAudioProfile(audio_format_t format,
184                           const ChannelMaskSet& channelMasks,
185                           const SampleRateSet& samplingRates,
186                           audio_profile* profile) {
187     profile->format = format;
188     profile->num_channel_masks = 0;
189     for (auto it = channelMasks.begin();
190          it != channelMasks.end() && profile->num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS;
191          ++it) {
192         profile->channel_masks[profile->num_channel_masks++] = *it;
193     }
194     profile->num_sample_rates = 0;
195     for (auto it = samplingRates.begin();
196          it != samplingRates.end() && profile->num_sample_rates < AUDIO_PORT_MAX_SAMPLING_RATES;
197          ++it) {
198         profile->sample_rates[profile->num_sample_rates++] = *it;
199     }
200 }
201 
202 } // namespace
203 
populateAudioProfiles(const AudioProfileAttributesMultimap & profileAttrs,audio_format_t format,ChannelMaskSet allChannelMasks,SampleRateSet allSampleRates,audio_profile audioProfiles[],uint32_t * numAudioProfiles,uint32_t maxAudioProfiles)204 void populateAudioProfiles(const AudioProfileAttributesMultimap& profileAttrs,
205                            audio_format_t format,
206                            ChannelMaskSet allChannelMasks,
207                            SampleRateSet allSampleRates,
208                            audio_profile audioProfiles[],
209                            uint32_t* numAudioProfiles,
210                            uint32_t maxAudioProfiles) {
211     if (*numAudioProfiles >= maxAudioProfiles) {
212         return;
213     }
214 
215     const auto lower= profileAttrs.lower_bound(format);
216     const auto upper = profileAttrs.upper_bound(format);
217     SampleRateSet sampleRatesPresent;
218     ChannelMaskSet channelMasksPresent;
219     for (auto it = lower; it != upper && *numAudioProfiles < maxAudioProfiles; ++it) {
220         SampleRateSet srs;
221         std::set_intersection(it->second.first.begin(), it->second.first.end(),
222                               allSampleRates.begin(), allSampleRates.end(),
223                               std::inserter(srs, srs.begin()));
224         if (srs.empty()) {
225             continue;
226         }
227         ChannelMaskSet cms;
228         std::set_intersection(it->second.second.begin(), it->second.second.end(),
229                               allChannelMasks.begin(), allChannelMasks.end(),
230                               std::inserter(cms, cms.begin()));
231         if (cms.empty()) {
232             continue;
233         }
234         sampleRatesPresent.insert(srs.begin(), srs.end());
235         channelMasksPresent.insert(cms.begin(), cms.end());
236         populateAudioProfile(it->first, cms, srs,
237                              &audioProfiles[(*numAudioProfiles)++]);
238     }
239     if (*numAudioProfiles >= maxAudioProfiles) {
240         ALOGW("%s, too many audio profiles", __func__);
241         return;
242     }
243 
244     SampleRateSet srs;
245     std::set_difference(allSampleRates.begin(), allSampleRates.end(),
246                         sampleRatesPresent.begin(), sampleRatesPresent.end(),
247                         std::inserter(srs, srs.begin()));
248     if (!srs.empty()) {
249         populateAudioProfile(format, allChannelMasks, srs,
250                              &audioProfiles[(*numAudioProfiles)++]);
251     }
252     if (*numAudioProfiles >= maxAudioProfiles) {
253         ALOGW("%s, too many audio profiles", __func__);
254         return;
255     }
256     ChannelMaskSet cms;
257     std::set_difference(allChannelMasks.begin(), allChannelMasks.end(),
258                         channelMasksPresent.begin(), channelMasksPresent.end(),
259                         std::inserter(cms, cms.begin()));
260     if (!cms.empty()) {
261         populateAudioProfile(format, cms, allSampleRates,
262                              &audioProfiles[(*numAudioProfiles)++]);
263     }
264 
265 }
266 
267 } // namespace android
268