blob: 754601443e01c6645904eb0b339dc1ad4e71f257 [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 }
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
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
jiabin17058fa2019-10-08 17:33:38 -0700108status_t AudioPort::writeToParcel(Parcel *parcel) const
109{
110 status_t status = NO_ERROR;
111 if ((status = parcel->writeUtf8AsUtf16(mName)) != NO_ERROR) return status;
112 if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status;
113 if ((status = parcel->writeUint32(mRole)) != NO_ERROR) return status;
114 if ((status = parcel->writeParcelable(mProfiles)) != NO_ERROR) return status;
115 if ((status = parcel->writeParcelable(mGains)) != NO_ERROR) return status;
116 return status;
117}
118
119status_t AudioPort::readFromParcel(const Parcel *parcel)
120{
121 status_t status = NO_ERROR;
122 if ((status = parcel->readUtf8FromUtf16(&mName)) != NO_ERROR) return status;
123 static_assert(sizeof(mType) == sizeof(uint32_t));
124 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mType))) != NO_ERROR) {
125 return status;
126 }
127 static_assert(sizeof(mRole) == sizeof(uint32_t));
128 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mRole))) != NO_ERROR) {
129 return status;
130 }
131 mProfiles.clear();
132 if ((status = parcel->readParcelable(&mProfiles)) != NO_ERROR) return status;
133 mGains.clear();
134 if ((status = parcel->readParcelable(&mGains)) != NO_ERROR) return status;
135 return status;
136}
137
jiabin4ef93452019-09-10 14:29:54 -0700138// --- AudioPortConfig class implementation
139
140status_t AudioPortConfig::applyAudioPortConfig(
141 const struct audio_port_config *config,
142 struct audio_port_config *backupConfig __unused)
143{
144 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
145 mSamplingRate = config->sample_rate;
146 }
147 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
148 mChannelMask = config->channel_mask;
149 }
150 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
151 mFormat = config->format;
152 }
153 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
154 mGain = config->gain;
155 }
156
157 return NO_ERROR;
158}
159
160namespace {
161
162template<typename T>
163void updateField(
164 const T& portConfigField, T audio_port_config::*port_config_field,
165 struct audio_port_config *dstConfig, const struct audio_port_config *srcConfig,
166 unsigned int configMask, T defaultValue)
167{
168 if (dstConfig->config_mask & configMask) {
169 if ((srcConfig != nullptr) && (srcConfig->config_mask & configMask)) {
170 dstConfig->*port_config_field = srcConfig->*port_config_field;
171 } else {
172 dstConfig->*port_config_field = portConfigField;
173 }
174 } else {
175 dstConfig->*port_config_field = defaultValue;
176 }
177}
178
179} // namespace
180
181void AudioPortConfig::toAudioPortConfig(
182 struct audio_port_config *dstConfig,
183 const struct audio_port_config *srcConfig) const
184{
185 updateField(mSamplingRate, &audio_port_config::sample_rate,
186 dstConfig, srcConfig, AUDIO_PORT_CONFIG_SAMPLE_RATE, 0u);
187 updateField(mChannelMask, &audio_port_config::channel_mask,
188 dstConfig, srcConfig, AUDIO_PORT_CONFIG_CHANNEL_MASK,
189 (audio_channel_mask_t)AUDIO_CHANNEL_NONE);
190 updateField(mFormat, &audio_port_config::format,
191 dstConfig, srcConfig, AUDIO_PORT_CONFIG_FORMAT, AUDIO_FORMAT_INVALID);
192 dstConfig->id = mId;
193
194 sp<AudioPort> audioport = getAudioPort();
195 if ((dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) && audioport != NULL) {
196 dstConfig->gain = mGain;
197 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)
198 && audioport->checkGain(&srcConfig->gain, srcConfig->gain.index) == OK) {
199 dstConfig->gain = srcConfig->gain;
200 }
201 } else {
202 dstConfig->gain.index = -1;
203 }
204 if (dstConfig->gain.index != -1) {
205 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
206 } else {
207 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
208 }
209}
210
211bool AudioPortConfig::hasGainController(bool canUseForVolume) const
212{
213 sp<AudioPort> audioport = getAudioPort();
214 if (!audioport) {
215 return false;
216 }
217 return canUseForVolume ? audioport->getGains().canUseForVolume()
218 : audioport->getGains().size() > 0;
219}
220
jiabin17058fa2019-10-08 17:33:38 -0700221status_t AudioPortConfig::writeToParcel(Parcel *parcel) const
222{
223 status_t status = NO_ERROR;
224 if ((status = parcel->writeUint32(mSamplingRate)) != NO_ERROR) return status;
225 if ((status = parcel->writeUint32(mFormat)) != NO_ERROR) return status;
226 if ((status = parcel->writeUint32(mChannelMask)) != NO_ERROR) return status;
227 if ((status = parcel->writeInt32(mId)) != NO_ERROR) return status;
228 // Write mGain to parcel.
229 if ((status = parcel->writeInt32(mGain.index)) != NO_ERROR) return status;
230 if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
231 if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
232 if ((status = parcel->writeUint32(mGain.ramp_duration_ms)) != NO_ERROR) return status;
233 std::vector<int> values(std::begin(mGain.values), std::end(mGain.values));
234 if ((status = parcel->writeInt32Vector(values)) != NO_ERROR) return status;
235 return status;
jiabine1284852019-09-11 10:15:46 -0700236}
jiabin17058fa2019-10-08 17:33:38 -0700237
238status_t AudioPortConfig::readFromParcel(const Parcel *parcel)
239{
240 status_t status = NO_ERROR;
241 if ((status = parcel->readUint32(&mSamplingRate)) != NO_ERROR) return status;
242 static_assert(sizeof(mFormat) == sizeof(uint32_t));
243 if ((status = parcel->readUint32(reinterpret_cast<uint32_t*>(&mFormat))) != NO_ERROR) {
244 return status;
245 }
246 if ((status = parcel->readUint32(&mChannelMask)) != NO_ERROR) return status;
247 if ((status = parcel->readInt32(&mId)) != NO_ERROR) return status;
248 // Read mGain from parcel.
249 if ((status = parcel->readInt32(&mGain.index)) != NO_ERROR) return status;
250 if ((status = parcel->readUint32(&mGain.mode)) != NO_ERROR) return status;
251 if ((status = parcel->readUint32(&mGain.channel_mask)) != NO_ERROR) return status;
252 if ((status = parcel->readUint32(&mGain.ramp_duration_ms)) != NO_ERROR) return status;
253 std::vector<int> values;
254 if ((status = parcel->readInt32Vector(&values)) != NO_ERROR) return status;
255 if (values.size() != std::size(mGain.values)) {
256 return BAD_VALUE;
257 }
258 std::copy(values.begin(), values.end(), mGain.values);
259 return status;
260}
261
262} // namespace android