blob: 1846a6bdf97f98bf8a40501082165ae6176bfadf [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
41void AudioPort::toAudioPort(struct audio_port *port) const {
jiabin5740f082019-08-19 15:08:30 -070042 // 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;
jiabin3e277cc2019-09-10 14:27:34 -070047 for (const auto& profile : mProfiles) {
jiabin5740f082019-08-19 15:08:30 -070048 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 }
Jiabin Huang3a0cf472020-11-03 17:29:35 +000065 port->role = mRole;
66 port->type = mType;
67 strlcpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN);
jiabin5740f082019-08-19 15:08:30 -070068 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
Jiabin Huang3a0cf472020-11-03 17:29:35 +000075 ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size());
jiabin5740f082019-08-19 15:08:30 -070076
Jiabin Huang3a0cf472020-11-03 17:29:35 +000077 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();
jiabin5740f082019-08-19 15:08:30 -070080 }
81}
82
jiabin4ef93452019-09-10 14:29:54 -070083void AudioPort::dump(std::string *dst, int spaces, bool verbose) const {
jiabin5740f082019-08-19 15:08:30 -070084 if (!mName.empty()) {
85 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
86 }
87 if (verbose) {
88 std::string profilesStr;
jiabin3e277cc2019-09-10 14:27:34 -070089 mProfiles.dump(&profilesStr, spaces);
jiabin5740f082019-08-19 15:08:30 -070090 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
jiabin4ef93452019-09-10 14:29:54 -0700103void 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
jiabin49e69a12019-10-15 16:04:13 -0700108bool 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
jiabin17058fa2019-10-08 17:33:38 -0700118status_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
129status_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
jiabin4ef93452019-09-10 14:29:54 -0700148// --- AudioPortConfig class implementation
149
150status_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
170namespace {
171
172template<typename T>
173void 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
191void 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
221bool 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
jiabin49e69a12019-10-15 16:04:13 -0700231bool 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
jiabin17058fa2019-10-08 17:33:38 -0700246status_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;
jiabine1284852019-09-11 10:15:46 -0700261}
jiabin17058fa2019-10-08 17:33:38 -0700262
263status_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 }
Mikhail Naganov55773032020-10-01 15:08:13 -0700271 uint32_t rawChannelMask;
272 if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status;
273 mChannelMask = static_cast<audio_channel_mask_t>(rawChannelMask);
jiabin17058fa2019-10-08 17:33:38 -0700274 if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status;
275 // Read mGain from parcel.
276 if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status;
Mikhail Naganov55773032020-10-01 15:08:13 -0700277 uint32_t rawGainMode;
278 if ((status = parcel->readUint32(&rawGainMode)) != NO_ERROR) return status;
279 mGain.mode = static_cast<audio_gain_mode_t>(rawGainMode);
280 if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status;
281 mGain.channel_mask = static_cast<audio_channel_mask_t>(rawChannelMask);
jiabin17058fa2019-10-08 17:33:38 -0700282 if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status;
283 std::vector<int> values;
284 if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
285 if (values.size() != std::size(mGain.values)) {
286 return BAD_VALUE;
287 }
288 std::copy(values.begin(), values.end(), mGain.values);
289 return status;
290}
291
292} // namespace android