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 #define LOG_TAG "AudioPort"
17
18 #include <algorithm>
19
20 #include <android-base/stringprintf.h>
21 #include <media/AudioPort.h>
22 #include <utils/Log.h>
23
24 namespace android {
25
importAudioPort(const sp<AudioPort> & port,bool force __unused)26 void 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
toAudioPort(struct audio_port * port) const41 void AudioPort::toAudioPort(struct audio_port *port) const {
42 // TODO: update this function once audio_port structure reflects the new profile definition.
43 // For compatibility reason: flatening the AudioProfile into audio_port structure.
44 FormatSet flatenedFormats;
45 SampleRateSet flatenedRates;
46 ChannelMaskSet flatenedChannels;
47 for (const auto& profile : mProfiles) {
48 if (profile->isValid()) {
49 audio_format_t formatToExport = profile->getFormat();
50 const SampleRateSet &ratesToExport = profile->getSampleRates();
51 const ChannelMaskSet &channelsToExport = profile->getChannels();
52
53 flatenedFormats.insert(formatToExport);
54 flatenedRates.insert(ratesToExport.begin(), ratesToExport.end());
55 flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end());
56
57 if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES ||
58 flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS ||
59 flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) {
60 ALOGE("%s: bailing out: cannot export profiles to port config", __func__);
61 return;
62 }
63 }
64 }
65 port->role = mRole;
66 port->type = mType;
67 strlcpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN);
68 port->num_sample_rates = flatenedRates.size();
69 port->num_channel_masks = flatenedChannels.size();
70 port->num_formats = flatenedFormats.size();
71 std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates);
72 std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks);
73 std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats);
74
75 ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size());
76
77 port->num_gains = std::min(mGains.size(), (size_t) AUDIO_PORT_MAX_GAINS);
78 for (size_t i = 0; i < port->num_gains; i++) {
79 port->gains[i] = mGains[i]->getGain();
80 }
81 }
82
dump(std::string * dst,int spaces,bool verbose) const83 void AudioPort::dump(std::string *dst, int spaces, bool verbose) const {
84 if (!mName.empty()) {
85 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
86 }
87 if (verbose) {
88 std::string profilesStr;
89 mProfiles.dump(&profilesStr, spaces);
90 dst->append(profilesStr);
91
92 if (mGains.size() != 0) {
93 dst->append(base::StringPrintf("%*s- gains:\n", spaces, ""));
94 for (size_t i = 0; i < mGains.size(); i++) {
95 std::string gainStr;
96 mGains[i]->dump(&gainStr, spaces + 2, i);
97 dst->append(gainStr);
98 }
99 }
100 }
101 }
102
log(const char * indent) const103 void AudioPort::log(const char* indent) const
104 {
105 ALOGI("%s Port[nm:%s, type:%d, role:%d]", indent, mName.c_str(), mType, mRole);
106 }
107
equals(const sp<AudioPort> & other) const108 bool AudioPort::equals(const sp<AudioPort> &other) const
109 {
110 return other != nullptr &&
111 mGains.equals(other->getGains()) &&
112 mName.compare(other->getName()) == 0 &&
113 mType == other->getType() &&
114 mRole == other->getRole() &&
115 mProfiles.equals(other->getAudioProfiles());
116 }
117
writeToParcel(Parcel * parcel) const118 status_t AudioPort::writeToParcel(Parcel *parcel) const
119 {
120 status_t status = NO_ERROR;
121 if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status;
122 if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status;
123 if ((status = parcel->writeUint32(mRole)) != NO_ERROR) return status;
124 if ((status = parcel->writeParcelable(mProfiles)) != NO_ERROR) return status;
125 if ((status = parcel->writeParcelable(mGains)) != NO_ERROR) return status;
126 return status;
127 }
128
readFromParcel(const Parcel * parcel)129 status_t AudioPort::readFromParcel(const Parcel *parcel)
130 {
131 status_t status = NO_ERROR;
132 if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status;
133 static_assert(sizeof(mType) == sizeof(uint32_t));
134 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mType))) != NO_ERROR) {
135 return status;
136 }
137 static_assert(sizeof(mRole) == sizeof(uint32_t));
138 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mRole))) != NO_ERROR) {
139 return status;
140 }
141 mProfiles.clear();
142 if ((status = parcel->readParcelable(&mProfiles)) != NO_ERROR) return status;
143 mGains.clear();
144 if ((status = parcel->readParcelable(&mGains)) != NO_ERROR) return status;
145 return status;
146 }
147
148 // --- AudioPortConfig class implementation
149
applyAudioPortConfig(const struct audio_port_config * config,struct audio_port_config * backupConfig __unused)150 status_t AudioPortConfig::applyAudioPortConfig(
151 const struct audio_port_config *config,
152 struct audio_port_config *backupConfig __unused)
153 {
154 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
155 mSamplingRate = config->sample_rate;
156 }
157 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
158 mChannelMask = config->channel_mask;
159 }
160 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
161 mFormat = config->format;
162 }
163 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
164 mGain = config->gain;
165 }
166
167 return NO_ERROR;
168 }
169
170 namespace {
171
172 template<typename T>
updateField(const T & portConfigField,T audio_port_config::* port_config_field,struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig,unsigned int configMask,T defaultValue)173 void updateField(
174 const T& portConfigField, T audio_port_config::*port_config_field,
175 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
176 unsigned int configMask, T defaultValue)
177 {
178 if (dstConfig->config_mask & configMask) {
179 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
180 dstConfig->*port_config_field = srcConfig->*port_config_field;
181 } else {
182 dstConfig->*port_config_field = portConfigField;
183 }
184 } else {
185 dstConfig->*port_config_field = defaultValue;
186 }
187 }
188
189 } // namespace
190
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const191 void AudioPortConfig::toAudioPortConfig(
192 struct audio_port_config *dstConfig,
193 const struct audio_port_config *srcConfig) const
194 {
195 updateField(mSamplingRate, &audio_port_config::sample_rate,
196 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
197 updateField(mChannelMask, &audio_port_config::channel_mask,
198 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
199 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
200 updateField(mFormat, &audio_port_config::format,
201 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
202 dstConfig->id = mId;
203
204 sp<AudioPort> audioport = getAudioPort();
205 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
206 dstConfig->gain = mGain;
207 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
208 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
209 dstConfig->gain = srcConfig->gain;
210 }
211 } else {
212 dstConfig->gain.index = -1;
213 }
214 if (dstConfig->gain.index != -1) {
215 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
216 } else {
217 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
218 }
219 }
220
hasGainController(bool canUseForVolume) const221 bool AudioPortConfig::hasGainController(bool canUseForVolume) const
222 {
223 sp<AudioPort> audioport = getAudioPort();
224 if (!audioport) {
225 return false;
226 }
227 return canUseForVolume ? audioport->getGains().canUseForVolume()
228 : audioport->getGains().size() > 0;
229 }
230
equals(const sp<AudioPortConfig> & other) const231 bool AudioPortConfig::equals(const sp<AudioPortConfig> &other) const
232 {
233 return other != nullptr &&
234 mSamplingRate == other->getSamplingRate() &&
235 mFormat == other->getFormat() &&
236 mChannelMask == other->getChannelMask() &&
237 // Compare audio gain config
238 mGain.index == other->mGain.index &&
239 mGain.mode == other->mGain.mode &&
240 mGain.channel_mask == other->mGain.channel_mask &&
241 std::equal(std::begin(mGain.values), std::end(mGain.values),
242 std::begin(other->mGain.values)) &&
243 mGain.ramp_duration_ms == other->mGain.ramp_duration_ms;
244 }
245
writeToParcel(Parcel * parcel) const246 status_t AudioPortConfig::writeToParcel(Parcel *parcel) const
247 {
248 status_t status = NO_ERROR;
249 if ((status = parcel->writeUint32(mSamplingRate)) != NO_ERROR) return status;
250 if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status;
251 if ((status = parcel->writeUint32(mChannelMask)) != NO_ERROR) return status;
252 if ((status = parcel->writeInt32(mId)) != NO_ERROR) return status;
253 // Write mGain to parcel.
254 if ((status = parcel->writeInt32(mGain.index)) != NO_ERROR) return status;
255 if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
256 if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
257 if ((status = parcel->writeUint32(mGain.ramp_duration_ms)) != NO_ERROR) return status;
258 std::vector<int> values(std::begin(mGain.values), std::end(mGain.values));
259 if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
260 return status;
261 }
262
readFromParcel(const Parcel * parcel)263 status_t AudioPortConfig::readFromParcel(const Parcel *parcel)
264 {
265 status_t status = NO_ERROR;
266 if ((status = parcel->readUint32(&mSamplingRate)) != NO_ERROR) return status;
267 static_assert(sizeof(mFormat) == sizeof(uint32_t));
268 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) {
269 return status;
270 }
271 if ((status = parcel->readUint32(&mChannelMask)) != NO_ERROR) return status;
272 if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status;
273 // Read mGain from parcel.
274 if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status;
275 if ((status = parcel->readUint32(&mGain.mode)) != NO_ERROR) return status;
276 if ((status = parcel->readUint32(&mGain.channel_mask)) != NO_ERROR) return status;
277 if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status;
278 std::vector<int> values;
279 if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
280 if (values.size() != std::size(mGain.values)) {
281 return BAD_VALUE;
282 }
283 std::copy(values.begin(), values.end(), mGain.values);
284 return status;
285 }
286
287 } // namespace android
288