blob: 7964e6df4bfa90dac36ff29e013377e014e5d347 [file] [log] [blame]
jiabin9a3361e2019-10-01 09:38:30 -07001/*
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
22namespace android {
23
24const 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
31const 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
38const 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
jiabindaf49952019-11-22 14:10:57 -080045const 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
jiabin9a3361e2019-10-01 09:38:30 -070052const 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
jiabindaf49952019-11-22 14:10:57 -080059const 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
Pattydd807582021-11-04 21:01:03 +080066const 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
Eric Laurent96d1dda2022-03-14 17:14:19 +010073const 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
Patty Huangf5082dd2022-07-06 00:14:12 +080080const 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
Mikhail Naganovfcfb5162021-12-02 01:35:15 +000087std::string deviceTypesToString(const DeviceTypeSet &deviceTypes) {
jiabinc52b1ff2019-10-31 17:20:42 -070088 if (deviceTypes.empty()) {
Mikhail Naganovfcfb5162021-12-02 01:35:15 +000089 return "Empty device types";
jiabinc52b1ff2019-10-31 17:20:42 -070090 }
Mikhail Naganovfcfb5162021-12-02 01:35:15 +000091 std::stringstream ss;
92 for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
93 if (it != deviceTypes.begin()) {
94 ss << ", ";
jiabin9a3361e2019-10-01 09:38:30 -070095 }
Mikhail Naganovfcfb5162021-12-02 01:35:15 +000096 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;
jiabin9a3361e2019-10-01 09:38:30 -0700101 }
102 }
Mikhail Naganovfcfb5162021-12-02 01:35:15 +0000103 return ss.str();
jiabin9a3361e2019-10-01 09:38:30 -0700104}
105
Hongguang48dc8712021-12-15 15:35:08 -0800106bool deviceTypesToString(const DeviceTypeSet &deviceTypes, std::string &str) {
107 str = deviceTypesToString(deviceTypes);
108 return true;
109}
110
jiabin9a3361e2019-10-01 09:38:30 -0700111std::string dumpDeviceTypes(const DeviceTypeSet &deviceTypes) {
Mikhail Naganovfcfb5162021-12-02 01:35:15 +0000112 std::stringstream ss;
113 for (auto it = deviceTypes.begin(); it != deviceTypes.end(); ++it) {
114 if (it != deviceTypes.begin()) {
115 ss << ", ";
jiabin9a3361e2019-10-01 09:38:30 -0700116 }
Mikhail Naganovfcfb5162021-12-02 01:35:15 +0000117 ss << "0x" << std::hex << (*it);
jiabin9a3361e2019-10-01 09:38:30 -0700118 }
Mikhail Naganovfcfb5162021-12-02 01:35:15 +0000119 return ss.str();
jiabinc52b1ff2019-10-31 17:20:42 -0700120}
121
jiabinf591cd52023-09-13 22:42:54 +0000122std::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
Robert Wu9c70c662024-10-30 18:27:47 +0000133std::string toString(const DeviceIdSet& deviceIds) {
134 if (deviceIds.empty()) {
Robert Wu45113402024-11-04 23:47:47 +0000135 return "AUDIO_PORT_HANDLE_NONE";
Robert Wu9c70c662024-10-30 18:27:47 +0000136 }
137 std::stringstream ss;
138 for (auto it = deviceIds.begin(); it != deviceIds.end(); ++it) {
139 if (it != deviceIds.begin()) {
140 ss << ", ";
141 }
142 ss << *it;
143 }
144 return ss.str();
145}
146
147audio_port_handle_t getFirstDeviceId(const DeviceIdSet& deviceIds) {
148 if (deviceIds.empty()) {
149 return AUDIO_PORT_HANDLE_NONE;
150 }
151 return *(deviceIds.begin());
152}
153
jiabin12537fc2023-10-12 17:56:08 +0000154AudioProfileAttributesMultimap createAudioProfilesAttrMap(audio_profile profiles[],
155 uint32_t first,
156 uint32_t last) {
157 AudioProfileAttributesMultimap result;
158 for (uint32_t i = first; i < last; ++i) {
159 SampleRateSet sampleRates(profiles[i].sample_rates,
160 profiles[i].sample_rates + profiles[i].num_sample_rates);
161 ChannelMaskSet channelMasks(profiles[i].channel_masks,
162 profiles[i].channel_masks + profiles[i].num_channel_masks);
163 result.emplace(profiles[i].format, std::make_pair(sampleRates, channelMasks));
164 }
165 return result;
166}
167
168namespace {
169
170void populateAudioProfile(audio_format_t format,
171 const ChannelMaskSet& channelMasks,
172 const SampleRateSet& samplingRates,
173 audio_profile* profile) {
174 profile->format = format;
175 profile->num_channel_masks = 0;
176 for (auto it = channelMasks.begin();
177 it != channelMasks.end() && profile->num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS;
178 ++it) {
179 profile->channel_masks[profile->num_channel_masks++] = *it;
180 }
181 profile->num_sample_rates = 0;
182 for (auto it = samplingRates.begin();
183 it != samplingRates.end() && profile->num_sample_rates < AUDIO_PORT_MAX_SAMPLING_RATES;
184 ++it) {
185 profile->sample_rates[profile->num_sample_rates++] = *it;
186 }
187}
188
189} // namespace
190
191void populateAudioProfiles(const AudioProfileAttributesMultimap& profileAttrs,
192 audio_format_t format,
193 ChannelMaskSet allChannelMasks,
194 SampleRateSet allSampleRates,
195 audio_profile audioProfiles[],
196 uint32_t* numAudioProfiles,
197 uint32_t maxAudioProfiles) {
198 if (*numAudioProfiles >= maxAudioProfiles) {
199 return;
200 }
201
202 const auto lower= profileAttrs.lower_bound(format);
203 const auto upper = profileAttrs.upper_bound(format);
204 SampleRateSet sampleRatesPresent;
205 ChannelMaskSet channelMasksPresent;
206 for (auto it = lower; it != upper && *numAudioProfiles < maxAudioProfiles; ++it) {
207 SampleRateSet srs;
208 std::set_intersection(it->second.first.begin(), it->second.first.end(),
209 allSampleRates.begin(), allSampleRates.end(),
210 std::inserter(srs, srs.begin()));
211 if (srs.empty()) {
212 continue;
213 }
214 ChannelMaskSet cms;
215 std::set_intersection(it->second.second.begin(), it->second.second.end(),
216 allChannelMasks.begin(), allChannelMasks.end(),
217 std::inserter(cms, cms.begin()));
218 if (cms.empty()) {
219 continue;
220 }
221 sampleRatesPresent.insert(srs.begin(), srs.end());
222 channelMasksPresent.insert(cms.begin(), cms.end());
223 populateAudioProfile(it->first, cms, srs,
224 &audioProfiles[(*numAudioProfiles)++]);
225 }
226 if (*numAudioProfiles >= maxAudioProfiles) {
227 ALOGW("%s, too many audio profiles", __func__);
228 return;
229 }
230
231 SampleRateSet srs;
232 std::set_difference(allSampleRates.begin(), allSampleRates.end(),
233 sampleRatesPresent.begin(), sampleRatesPresent.end(),
234 std::inserter(srs, srs.begin()));
235 if (!srs.empty()) {
236 populateAudioProfile(format, allChannelMasks, srs,
237 &audioProfiles[(*numAudioProfiles)++]);
238 }
239 if (*numAudioProfiles >= maxAudioProfiles) {
240 ALOGW("%s, too many audio profiles", __func__);
241 return;
242 }
243 ChannelMaskSet cms;
244 std::set_difference(allChannelMasks.begin(), allChannelMasks.end(),
245 channelMasksPresent.begin(), channelMasksPresent.end(),
246 std::inserter(cms, cms.begin()));
247 if (!cms.empty()) {
248 populateAudioProfile(format, cms, allSampleRates,
249 &audioProfiles[(*numAudioProfiles)++]);
250 }
251
252}
253
jiabin9a3361e2019-10-01 09:38:30 -0700254} // namespace android