blob: d650c67797a522a92f258de131b8c48f491b466b [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{
153 status_t status = NO_ERROR;
154 if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status;
155 if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status;
156 if ((status = parcel->writeUint32(mRole)) != NO_ERROR) return status;
157 if ((status = parcel->writeParcelable(mProfiles)) != NO_ERROR) return status;
158 if ((status = parcel->writeParcelable(mGains)) != NO_ERROR) return status;
159 return status;
160}
161
162status_t AudioPort::readFromParcel(const Parcel *parcel)
163{
164 status_t status = NO_ERROR;
165 if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status;
166 static_assert(sizeof(mType) == sizeof(uint32_t));
167 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mType))) != NO_ERROR) {
168 return status;
169 }
170 static_assert(sizeof(mRole) == sizeof(uint32_t));
171 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mRole))) != NO_ERROR) {
172 return status;
173 }
174 mProfiles.clear();
175 if ((status = parcel->readParcelable(&mProfiles)) != NO_ERROR) return status;
176 mGains.clear();
177 if ((status = parcel->readParcelable(&mGains)) != NO_ERROR) return status;
178 return status;
179}
180
jiabin4ef93452019-09-10 14:29:54 -0700181// --- AudioPortConfig class implementation
182
183status_t AudioPortConfig::applyAudioPortConfig(
184 const struct audio_port_config *config,
185 struct audio_port_config *backupConfig __unused)
186{
187 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
188 mSamplingRate = config->sample_rate;
189 }
190 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
191 mChannelMask = config->channel_mask;
192 }
193 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
194 mFormat = config->format;
195 }
196 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
197 mGain = config->gain;
198 }
199
200 return NO_ERROR;
201}
202
203namespace {
204
205template<typename T>
206void updateField(
207 const T& portConfigField, T audio_port_config::*port_config_field,
208 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
209 unsigned int configMask, T defaultValue)
210{
211 if (dstConfig->config_mask & configMask) {
212 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
213 dstConfig->*port_config_field = srcConfig->*port_config_field;
214 } else {
215 dstConfig->*port_config_field = portConfigField;
216 }
217 } else {
218 dstConfig->*port_config_field = defaultValue;
219 }
220}
221
222} // namespace
223
224void AudioPortConfig::toAudioPortConfig(
225 struct audio_port_config *dstConfig,
226 const struct audio_port_config *srcConfig) const
227{
228 updateField(mSamplingRate, &audio_port_config::sample_rate,
229 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
230 updateField(mChannelMask, &audio_port_config::channel_mask,
231 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
232 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
233 updateField(mFormat, &audio_port_config::format,
234 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
235 dstConfig->id = mId;
236
237 sp<AudioPort> audioport = getAudioPort();
238 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
239 dstConfig->gain = mGain;
240 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
241 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
242 dstConfig->gain = srcConfig->gain;
243 }
244 } else {
245 dstConfig->gain.index = -1;
246 }
247 if (dstConfig->gain.index != -1) {
248 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
249 } else {
250 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
251 }
252}
253
254bool AudioPortConfig::hasGainController(bool canUseForVolume) const
255{
256 sp<AudioPort> audioport = getAudioPort();
257 if (!audioport) {
258 return false;
259 }
260 return canUseForVolume ? audioport->getGains().canUseForVolume()
261 : audioport->getGains().size() > 0;
262}
263
jiabin49e69a12019-10-15 16:04:13 -0700264bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
265{
266 return other != nullptr &&
267 mSamplingRate == other->getSamplingRate() &&
268 mFormat == other->getFormat() &&
269 mChannelMask == other->getChannelMask() &&
270 // Compare audio gain config
271 mGain.index == other->mGain.index &&
272 mGain.mode == other->mGain.mode &&
273 mGain.channel_mask == other->mGain.channel_mask &&
274 std::equal(std::begin(mGain.values), std::end(mGain.values),
275 std::begin(other->mGain.values)) &&
276 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
277}
278
jiabin17058fa2019-10-08 17:33:38 -0700279status_t AudioPortConfig::writeToParcel(Parcel *parcel) const
280{
281 status_t status = NO_ERROR;
282 if ((status = parcel->writeUint32(mSamplingRate)) != NO_ERROR) return status;
283 if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status;
284 if ((status = parcel->writeUint32(mChannelMask)) != NO_ERROR) return status;
285 if ((status = parcel->writeInt32(mId)) != NO_ERROR) return status;
286 // Write mGain to parcel.
287 if ((status = parcel->writeInt32(mGain.index)) != NO_ERROR) return status;
288 if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
289 if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
290 if ((status = parcel->writeUint32(mGain.ramp_duration_ms)) != NO_ERROR) return status;
291 std::vector<int> values(std::begin(mGain.values), std::end(mGain.values));
292 if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
293 return status;
jiabine1284852019-09-11 10:15:46 -0700294}
jiabin17058fa2019-10-08 17:33:38 -0700295
296status_t AudioPortConfig::readFromParcel(const Parcel *parcel)
297{
298 status_t status = NO_ERROR;
299 if ((status = parcel->readUint32(&mSamplingRate)) != NO_ERROR) return status;
300 static_assert(sizeof(mFormat) == sizeof(uint32_t));
301 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) {
302 return status;
303 }
Mikhail Naganov55773032020-10-01 15:08:13 -0700304 uint32_t rawChannelMask;
305 if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status;
306 mChannelMask = static_cast<audio_channel_mask_t>(rawChannelMask);
jiabin17058fa2019-10-08 17:33:38 -0700307 if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status;
308 // Read mGain from parcel.
309 if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status;
Mikhail Naganov55773032020-10-01 15:08:13 -0700310 uint32_t rawGainMode;
311 if ((status = parcel->readUint32(&rawGainMode)) != NO_ERROR) return status;
312 mGain.mode = static_cast<audio_gain_mode_t>(rawGainMode);
313 if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status;
314 mGain.channel_mask = static_cast<audio_channel_mask_t>(rawChannelMask);
jiabin17058fa2019-10-08 17:33:38 -0700315 if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status;
316 std::vector<int> values;
317 if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
318 if (values.size() != std::size(mGain.values)) {
319 return BAD_VALUE;
320 }
321 std::copy(values.begin(), values.end(), mGain.values);
322 return status;
323}
324
325} // namespace android