blob: 20d8632464e51c397d4d499cb7157128262066e7 [file] [log] [blame]
jiabin5740f082019-08-19 15:08: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 */
jiabine1284852019-09-11 10:15:46 -070016#define LOG_TAG "AudioPort"
jiabin5740f082019-08-19 15:08:30 -070017
18#include <algorithm>
19
20#include <android-base/stringprintf.h>
jiabine1284852019-09-11 10:15:46 -070021#include <media/AudioPort.h>
jiabin5740f082019-08-19 15:08:30 -070022#include <utils/Log.h>
23
24namespace android {
25
jiabin4ef93452019-09-10 14:29:54 -070026void AudioPort::importAudioPort(const sp<AudioPort>& port, bool force __unused)
27{
28 for (const auto& profileToImport : port->mProfiles) {
29 // Import only valid port, i.e. valid format, non empty rates and channels masks
30 if (!profileToImport->isValid()) {
31 continue;
32 }
33 if (std::find_if(mProfiles.begin(), mProfiles.end(),
34 [profileToImport](const auto &profile) {
35 return *profile == *profileToImport; }) == mProfiles.end()) {
36 addAudioProfile(profileToImport);
37 }
38 }
39}
40
jiabinb4fed192020-09-22 14:45:40 -070041void AudioPort::importAudioPort(const audio_port_v7 &port) {
42 for (size_t i = 0; i < port.num_audio_profiles; ++i) {
43 sp<AudioProfile> profile = new AudioProfile(port.audio_profiles[i].format,
44 ChannelMaskSet(port.audio_profiles[i].channel_masks,
45 port.audio_profiles[i].channel_masks +
46 port.audio_profiles->num_channel_masks),
47 SampleRateSet(port.audio_profiles[i].sample_rates,
48 port.audio_profiles[i].sample_rates +
49 port.audio_profiles[i].num_sample_rates));
50 if (!mProfiles.contains(profile)) {
51 addAudioProfile(profile);
52 }
53 }
54}
55
jiabin4ef93452019-09-10 14:29:54 -070056void AudioPort::toAudioPort(struct audio_port *port) const {
jiabin5740f082019-08-19 15:08:30 -070057 // TODO: update this function once audio_port structure reflects the new profile definition.
58 // For compatibility reason: flatening the AudioProfile into audio_port structure.
59 FormatSet flatenedFormats;
60 SampleRateSet flatenedRates;
61 ChannelMaskSet flatenedChannels;
jiabin3e277cc2019-09-10 14:27:34 -070062 for (const auto& profile : mProfiles) {
jiabin5740f082019-08-19 15:08:30 -070063 if (profile->isValid()) {
64 audio_format_t formatToExport = profile->getFormat();
65 const SampleRateSet &ratesToExport = profile->getSampleRates();
66 const ChannelMaskSet &channelsToExport = profile->getChannels();
67
68 flatenedFormats.insert(formatToExport);
69 flatenedRates.insert(ratesToExport.begin(), ratesToExport.end());
70 flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end());
71
72 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
73 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
74 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) {
75 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
76 return;
77 }
78 }
79 }
jiabinb4fed192020-09-22 14:45:40 -070080 toAudioPortBase(port);
jiabin5740f082019-08-19 15:08:30 -070081 port->num_sample_rates = flatenedRates.size();
82 port->num_channel_masks = flatenedChannels.size();
83 port->num_formats = flatenedFormats.size();
84 std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates);
85 std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks);
86 std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats);
jiabinb4fed192020-09-22 14:45:40 -070087}
jiabin5740f082019-08-19 15:08:30 -070088
jiabinb4fed192020-09-22 14:45:40 -070089void AudioPort::toAudioPort(struct audio_port_v7 *port) const {
90 toAudioPortBase(port);
91 port->num_audio_profiles = 0;
92 for (const auto& profile : mProfiles) {
93 if (profile->isValid()) {
94 const SampleRateSet &sampleRates = profile->getSampleRates();
95 const ChannelMaskSet &channelMasks = profile->getChannels();
jiabin5740f082019-08-19 15:08:30 -070096
jiabinb4fed192020-09-22 14:45:40 -070097 if (sampleRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
98 channelMasks.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
99 port->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
100 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
101 return;
102 }
103
104 auto& dstProfile = port->audio_profiles[port->num_audio_profiles++];
105 dstProfile.format = profile->getFormat();
106 dstProfile.num_sample_rates = sampleRates.size();
107 std::copy(sampleRates.begin(), sampleRates.end(),
108 std::begin(dstProfile.sample_rates));
109 dstProfile.num_channel_masks = channelMasks.size();
110 std::copy(channelMasks.begin(), channelMasks.end(),
111 std::begin(dstProfile.channel_masks));
112 }
jiabin5740f082019-08-19 15:08:30 -0700113 }
114}
115
jiabin4ef93452019-09-10 14:29:54 -0700116void AudioPort::dump(std::string *dst, int spaces, bool verbose) const {
jiabin5740f082019-08-19 15:08:30 -0700117 if (!mName.empty()) {
118 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
119 }
120 if (verbose) {
121 std::string profilesStr;
jiabin3e277cc2019-09-10 14:27:34 -0700122 mProfiles.dump(&profilesStr, spaces);
jiabin5740f082019-08-19 15:08:30 -0700123 dst->append(profilesStr);
124
125 if (mGains.size() != 0) {
126 dst->append(base::StringPrintf("%*s- gains:\n", spaces, ""));
127 for (size_t i = 0; i < mGains.size(); i++) {
128 std::string gainStr;
129 mGains[i]->dump(&gainStr, spaces + 2, i);
130 dst->append(gainStr);
131 }
132 }
133 }
134}
135
jiabin4ef93452019-09-10 14:29:54 -0700136void AudioPort::log(const char* indent) const
137{
138 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
139}
140
jiabin49e69a12019-10-15 16:04:13 -0700141bool AudioPort::equals(const sp<AudioPort> &other) const
142{
143 return other != nullptr &&
144 mGains.equals(other->getGains()) &&
145 mName.compare(other->getName()) == 0 &&
146 mType == other->getType() &&
147 mRole == other->getRole() &&
148 mProfiles.equals(other->getAudioProfiles());
149}
150
jiabin17058fa2019-10-08 17:33:38 -0700151status_t AudioPort::writeToParcel(Parcel *parcel) const
152{
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800153 media::AudioPort parcelable;
154 return writeToParcelable(&parcelable)
155 ?: parcelable.writeToParcel(parcel);
jiabin17058fa2019-10-08 17:33:38 -0700156}
157
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800158status_t AudioPort::writeToParcelable(media::AudioPort* parcelable) const {
159 parcelable->name = mName;
160 parcelable->type = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_type_t_AudioPortType(mType));
161 parcelable->role = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_role_t_AudioPortRole(mRole));
162 parcelable->profiles = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioProfileVector(mProfiles));
163 parcelable->gains = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioGains(mGains));
164 return OK;
165}
166
167status_t AudioPort::readFromParcel(const Parcel *parcel) {
168 media::AudioPort parcelable;
169 return parcelable.readFromParcel(parcel)
170 ?: readFromParcelable(parcelable);
171}
172
173status_t AudioPort::readFromParcelable(const media::AudioPort& parcelable) {
174 mName = parcelable.name;
175 mType = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortType_audio_port_type_t(parcelable.type));
176 mRole = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPortRole_audio_port_role_t(parcelable.role));
177 mProfiles = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioProfileVector(parcelable.profiles));
178 mGains = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioGains(parcelable.gains));
179 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700180}
181
jiabin4ef93452019-09-10 14:29:54 -0700182// --- AudioPortConfig class implementation
183
184status_t AudioPortConfig::applyAudioPortConfig(
185 const struct audio_port_config *config,
186 struct audio_port_config *backupConfig __unused)
187{
188 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
189 mSamplingRate = config->sample_rate;
190 }
191 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
192 mChannelMask = config->channel_mask;
193 }
194 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
195 mFormat = config->format;
196 }
197 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
198 mGain = config->gain;
199 }
200
201 return NO_ERROR;
202}
203
204namespace {
205
206template<typename T>
207void updateField(
208 const T& portConfigField, T audio_port_config::*port_config_field,
209 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
210 unsigned int configMask, T defaultValue)
211{
212 if (dstConfig->config_mask & configMask) {
213 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
214 dstConfig->*port_config_field = srcConfig->*port_config_field;
215 } else {
216 dstConfig->*port_config_field = portConfigField;
217 }
218 } else {
219 dstConfig->*port_config_field = defaultValue;
220 }
221}
222
223} // namespace
224
225void AudioPortConfig::toAudioPortConfig(
226 struct audio_port_config *dstConfig,
227 const struct audio_port_config *srcConfig) const
228{
229 updateField(mSamplingRate, &audio_port_config::sample_rate,
230 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
231 updateField(mChannelMask, &audio_port_config::channel_mask,
232 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
233 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
234 updateField(mFormat, &audio_port_config::format,
235 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
236 dstConfig->id = mId;
237
238 sp<AudioPort> audioport = getAudioPort();
239 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
240 dstConfig->gain = mGain;
241 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
242 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
243 dstConfig->gain = srcConfig->gain;
244 }
245 } else {
246 dstConfig->gain.index = -1;
247 }
248 if (dstConfig->gain.index != -1) {
249 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
250 } else {
251 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
252 }
253}
254
255bool AudioPortConfig::hasGainController(bool canUseForVolume) const
256{
257 sp<AudioPort> audioport = getAudioPort();
258 if (!audioport) {
259 return false;
260 }
261 return canUseForVolume ? audioport->getGains().canUseForVolume()
262 : audioport->getGains().size() > 0;
263}
264
jiabin49e69a12019-10-15 16:04:13 -0700265bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
266{
267 return other != nullptr &&
268 mSamplingRate == other->getSamplingRate() &&
269 mFormat == other->getFormat() &&
270 mChannelMask == other->getChannelMask() &&
271 // Compare audio gain config
272 mGain.index == other->mGain.index &&
273 mGain.mode == other->mGain.mode &&
274 mGain.channel_mask == other->mGain.channel_mask &&
275 std::equal(std::begin(mGain.values), std::end(mGain.values),
276 std::begin(other->mGain.values)) &&
277 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
278}
279
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800280status_t AudioPortConfig::writeToParcel(Parcel *parcel) const {
281 media::AudioPortConfig parcelable;
282 return writeToParcelable(&parcelable)
283 ?: parcelable.writeToParcel(parcel);
jiabine1284852019-09-11 10:15:46 -0700284}
jiabin17058fa2019-10-08 17:33:38 -0700285
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800286status_t AudioPortConfig::writeToParcelable(media::AudioPortConfig* parcelable) const {
287 parcelable->sampleRate = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mSamplingRate));
288 parcelable->format = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_format_t_AudioFormat(mFormat));
289 parcelable->channelMask = VALUE_OR_RETURN_STATUS(
290 legacy2aidl_audio_channel_mask_t_int32_t(mChannelMask));
291 parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
292 parcelable->gain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(mGain.index));
293 parcelable->gain.mode = VALUE_OR_RETURN_STATUS(
Andy Hung973638a2020-12-08 20:47:45 -0800294 legacy2aidl_audio_gain_mode_t_int32_t_mask(mGain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800295 parcelable->gain.channelMask = VALUE_OR_RETURN_STATUS(
296 legacy2aidl_audio_channel_mask_t_int32_t(mGain.channel_mask));
297 parcelable->gain.rampDurationMs = VALUE_OR_RETURN_STATUS(
298 convertIntegral<int32_t>(mGain.ramp_duration_ms));
299 parcelable->gain.values = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<int32_t>>(
300 mGain.values, convertIntegral<int32_t, int>));
301 return OK;
302}
303
304status_t AudioPortConfig::readFromParcel(const Parcel *parcel) {
305 media::AudioPortConfig parcelable;
306 return parcelable.readFromParcel(parcel)
307 ?: readFromParcelable(parcelable);
308}
309
310status_t AudioPortConfig::readFromParcelable(const media::AudioPortConfig& parcelable) {
311 mSamplingRate = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(parcelable.sampleRate));
312 mFormat = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioFormat_audio_format_t(parcelable.format));
313 mChannelMask = VALUE_OR_RETURN_STATUS(
314 aidl2legacy_int32_t_audio_channel_mask_t(parcelable.channelMask));
315 mId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(parcelable.id));
316 mGain.index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.index));
317 mGain.mode = VALUE_OR_RETURN_STATUS(
Ytai Ben-Tsvi0cf92652020-11-23 13:23:00 -0800318 aidl2legacy_int32_t_audio_gain_mode_t_mask(parcelable.gain.mode));
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800319 mGain.channel_mask = VALUE_OR_RETURN_STATUS(
320 aidl2legacy_int32_t_audio_channel_mask_t(parcelable.gain.channelMask));
321 mGain.ramp_duration_ms = VALUE_OR_RETURN_STATUS(
322 convertIntegral<unsigned int>(parcelable.gain.rampDurationMs));
323 if (parcelable.gain.values.size() > std::size(mGain.values)) {
jiabin17058fa2019-10-08 17:33:38 -0700324 return BAD_VALUE;
325 }
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800326 for (size_t i = 0; i < parcelable.gain.values.size(); ++i) {
327 mGain.values[i] = VALUE_OR_RETURN_STATUS(convertIntegral<int>(parcelable.gain.values[i]));
328 }
329 return OK;
jiabin17058fa2019-10-08 17:33:38 -0700330}
331
332} // namespace android