• 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 
deviceTypesToString(const DeviceTypeSet & deviceTypes)87 std::string deviceTypesToString(const DeviceTypeSet &deviceTypes) {
88     if (deviceTypes.empty()) {
89         return "Empty device types";
90     }
91     std::stringstream ss;
92     for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
93         if (it != deviceTypes.begin()) {
94             ss << ", ";
95         }
96         const char* strType = audio_device_to_string(*it);
97         if (strlen(strType) != 0) {
98             ss << strType;
99         } else {
100             ss << "unknown type:0x" << std::hex << *it;
101         }
102     }
103     return ss.str();
104 }
105 
deviceTypesToString(const DeviceTypeSet & deviceTypes,std::string & str)106 bool deviceTypesToString(const DeviceTypeSet &deviceTypes, std::string &str) {
107     str = deviceTypesToString(deviceTypes);
108     return true;
109 }
110 
dumpDeviceTypes(const DeviceTypeSet & deviceTypes)111 std::string dumpDeviceTypes(const DeviceTypeSet &deviceTypes) {
112     std::stringstream ss;
113     for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
114         if (it != deviceTypes.begin()) {
115             ss << ", ";
116         }
117         ss << "0x" << std::hex << (*it);
118     }
119     return ss.str();
120 }
121 
dumpMixerBehaviors(const MixerBehaviorSet & mixerBehaviors)122 std::string dumpMixerBehaviors(const MixerBehaviorSet& mixerBehaviors) {
123     std::stringstream ss;
124     for (auto it = mixerBehaviors.begin(); it != mixerBehaviors.end(); ++it) {
125         if (it != mixerBehaviors.begin()) {
126             ss << ", ";
127         }
128         ss << (*it);
129     }
130     return ss.str();
131 }
132 
createAudioProfilesAttrMap(audio_profile profiles[],uint32_t first,uint32_t last)133 AudioProfileAttributesMultimap createAudioProfilesAttrMap(audio_profile profiles[],
134                                                           uint32_t first,
135                                                           uint32_t last) {
136     AudioProfileAttributesMultimap result;
137     for (uint32_t i = first; i < last; ++i) {
138         SampleRateSet sampleRates(profiles[i].sample_rates,
139                                   profiles[i].sample_rates + profiles[i].num_sample_rates);
140         ChannelMaskSet channelMasks(profiles[i].channel_masks,
141                                     profiles[i].channel_masks + profiles[i].num_channel_masks);
142         result.emplace(profiles[i].format, std::make_pair(sampleRates, channelMasks));
143     }
144     return result;
145 }
146 
147 namespace {
148 
populateAudioProfile(audio_format_t format,const ChannelMaskSet & channelMasks,const SampleRateSet & samplingRates,audio_profile * profile)149 void populateAudioProfile(audio_format_t format,
150                           const ChannelMaskSet& channelMasks,
151                           const SampleRateSet& samplingRates,
152                           audio_profile* profile) {
153     profile->format = format;
154     profile->num_channel_masks = 0;
155     for (auto it = channelMasks.begin();
156          it != channelMasks.end() && profile->num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS;
157          ++it) {
158         profile->channel_masks[profile->num_channel_masks++] = *it;
159     }
160     profile->num_sample_rates = 0;
161     for (auto it = samplingRates.begin();
162          it != samplingRates.end() && profile->num_sample_rates < AUDIO_PORT_MAX_SAMPLING_RATES;
163          ++it) {
164         profile->sample_rates[profile->num_sample_rates++] = *it;
165     }
166 }
167 
168 } // namespace
169 
populateAudioProfiles(const AudioProfileAttributesMultimap & profileAttrs,audio_format_t format,ChannelMaskSet allChannelMasks,SampleRateSet allSampleRates,audio_profile audioProfiles[],uint32_t * numAudioProfiles,uint32_t maxAudioProfiles)170 void populateAudioProfiles(const AudioProfileAttributesMultimap& profileAttrs,
171                            audio_format_t format,
172                            ChannelMaskSet allChannelMasks,
173                            SampleRateSet allSampleRates,
174                            audio_profile audioProfiles[],
175                            uint32_t* numAudioProfiles,
176                            uint32_t maxAudioProfiles) {
177     if (*numAudioProfiles >= maxAudioProfiles) {
178         return;
179     }
180 
181     const auto lower= profileAttrs.lower_bound(format);
182     const auto upper = profileAttrs.upper_bound(format);
183     SampleRateSet sampleRatesPresent;
184     ChannelMaskSet channelMasksPresent;
185     for (auto it = lower; it != upper && *numAudioProfiles < maxAudioProfiles; ++it) {
186         SampleRateSet srs;
187         std::set_intersection(it->second.first.begin(), it->second.first.end(),
188                               allSampleRates.begin(), allSampleRates.end(),
189                               std::inserter(srs, srs.begin()));
190         if (srs.empty()) {
191             continue;
192         }
193         ChannelMaskSet cms;
194         std::set_intersection(it->second.second.begin(), it->second.second.end(),
195                               allChannelMasks.begin(), allChannelMasks.end(),
196                               std::inserter(cms, cms.begin()));
197         if (cms.empty()) {
198             continue;
199         }
200         sampleRatesPresent.insert(srs.begin(), srs.end());
201         channelMasksPresent.insert(cms.begin(), cms.end());
202         populateAudioProfile(it->first, cms, srs,
203                              &audioProfiles[(*numAudioProfiles)++]);
204     }
205     if (*numAudioProfiles >= maxAudioProfiles) {
206         ALOGW("%s, too many audio profiles", __func__);
207         return;
208     }
209 
210     SampleRateSet srs;
211     std::set_difference(allSampleRates.begin(), allSampleRates.end(),
212                         sampleRatesPresent.begin(), sampleRatesPresent.end(),
213                         std::inserter(srs, srs.begin()));
214     if (!srs.empty()) {
215         populateAudioProfile(format, allChannelMasks, srs,
216                              &audioProfiles[(*numAudioProfiles)++]);
217     }
218     if (*numAudioProfiles >= maxAudioProfiles) {
219         ALOGW("%s, too many audio profiles", __func__);
220         return;
221     }
222     ChannelMaskSet cms;
223     std::set_difference(allChannelMasks.begin(), allChannelMasks.end(),
224                         channelMasksPresent.begin(), channelMasksPresent.end(),
225                         std::inserter(cms, cms.begin()));
226     if (!cms.empty()) {
227         populateAudioProfile(format, cms, allSampleRates,
228                              &audioProfiles[(*numAudioProfiles)++]);
229     }
230 
231 }
232 
233 } // namespace android
234