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
17 #define LOG_TAG "DeviceDescriptorBase"
18 //#define LOG_NDEBUG 0
19
20 #include <android-base/stringprintf.h>
21 #include <audio_utils/string.h>
22 #include <media/AidlConversion.h>
23 #include <media/DeviceDescriptorBase.h>
24 #include <media/TypeConverter.h>
25
26 namespace android {
27
DeviceDescriptorBase(audio_devices_t type)28 DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) :
29 DeviceDescriptorBase(type, "")
30 {
31 }
32
DeviceDescriptorBase(audio_devices_t type,const std::string & address,const FormatVector & encodedFormats)33 DeviceDescriptorBase::DeviceDescriptorBase(
34 audio_devices_t type, const std::string& address,
35 const FormatVector &encodedFormats) :
36 DeviceDescriptorBase(AudioDeviceTypeAddr(type, address), encodedFormats)
37 {
38 }
39
DeviceDescriptorBase(const AudioDeviceTypeAddr & deviceTypeAddr,const FormatVector & encodedFormats)40 DeviceDescriptorBase::DeviceDescriptorBase(
41 const AudioDeviceTypeAddr &deviceTypeAddr, const FormatVector &encodedFormats) :
42 AudioPort("", AUDIO_PORT_TYPE_DEVICE,
43 audio_is_output_device(deviceTypeAddr.mType) ? AUDIO_PORT_ROLE_SINK :
44 AUDIO_PORT_ROLE_SOURCE),
45 mDeviceTypeAddr(deviceTypeAddr),
46 mEncodedFormats(encodedFormats)
47 {
48 if (mDeviceTypeAddr.address().empty() && audio_is_remote_submix_device(mDeviceTypeAddr.mType)) {
49 mDeviceTypeAddr.setAddress("0");
50 }
51 }
52
setAddress(const std::string & address)53 void DeviceDescriptorBase::setAddress(const std::string &address) {
54 mDeviceTypeAddr.setAddress(address);
55 }
56
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const57 void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig,
58 const struct audio_port_config *srcConfig) const
59 {
60 dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN;
61 if (mSamplingRate != 0) {
62 dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
63 }
64 if (mChannelMask != AUDIO_CHANNEL_NONE) {
65 dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
66 }
67 if (mFormat != AUDIO_FORMAT_INVALID) {
68 dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
69 }
70
71 if (srcConfig != NULL) {
72 dstConfig->config_mask |= srcConfig->config_mask;
73 }
74
75 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
76
77 dstConfig->role = audio_is_output_device(mDeviceTypeAddr.mType) ?
78 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
79 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
80 dstConfig->ext.device.type = mDeviceTypeAddr.mType;
81
82 (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mDeviceTypeAddr.getAddress());
83 }
84
toAudioPort(struct audio_port * port) const85 void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const
86 {
87 ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceTypeAddr.mType);
88 toAudioPortInternal(port);
89 }
90
toAudioPort(struct audio_port_v7 * port) const91 void DeviceDescriptorBase::toAudioPort(struct audio_port_v7 *port) const {
92 ALOGV("DeviceDescriptorBase::toAudioPort() v7 handle %d type %08x", mId, mDeviceTypeAddr.mType);
93 toAudioPortInternal(port);
94 }
95
setEncapsulationModes(uint32_t encapsulationModes)96 status_t DeviceDescriptorBase::setEncapsulationModes(uint32_t encapsulationModes) {
97 if ((encapsulationModes & ~AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS) != 0) {
98 return BAD_VALUE;
99 }
100 mEncapsulationModes = encapsulationModes & ~(1 << AUDIO_ENCAPSULATION_MODE_NONE);
101 return NO_ERROR;
102 }
103
setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes)104 status_t DeviceDescriptorBase::setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes) {
105 if ((encapsulationMetadataTypes & ~AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS) != 0) {
106 return BAD_VALUE;
107 }
108 mEncapsulationMetadataTypes =
109 encapsulationMetadataTypes & ~(1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE);
110 return NO_ERROR;
111 }
112
dump(std::string * dst,int spaces,const char * extraInfo,bool verbose) const113 void DeviceDescriptorBase::dump(std::string *dst, int spaces,
114 const char* extraInfo, bool verbose) const
115 {
116 if (mId != 0) {
117 dst->append(base::StringPrintf("Port ID: %d; ", mId));
118 }
119 if (extraInfo != nullptr) {
120 dst->append(base::StringPrintf("%s; ", extraInfo));
121 }
122 dst->append(base::StringPrintf("{%s}\n",
123 mDeviceTypeAddr.toString(true /*includeSensitiveInfo*/).c_str()));
124
125 dst->append(base::StringPrintf(
126 "%*sEncapsulation modes: %u, metadata types: %u\n", spaces, "",
127 mEncapsulationModes, mEncapsulationMetadataTypes));
128 if (!mEncodedFormats.empty()) {
129 std::string s;
130 for (const auto& format : mEncodedFormats) {
131 if (!s.empty()) s.append(", ");
132 s.append(audio_format_to_string(format));
133 }
134 dst->append(base::StringPrintf(
135 "%*sEncoded formats: %s\n", spaces, "", s.c_str()));
136 }
137
138 std::string portStr;
139 AudioPort::dump(&portStr, spaces, nullptr, verbose);
140 if (!portStr.empty()) {
141 if (!mName.empty()) {
142 dst->append(base::StringPrintf("%*s", spaces, ""));
143 }
144 dst->append(portStr);
145 }
146 }
147
toString(bool includeSensitiveInfo) const148 std::string DeviceDescriptorBase::toString(bool includeSensitiveInfo) const
149 {
150 return mDeviceTypeAddr.toString(includeSensitiveInfo);
151 }
152
log() const153 void DeviceDescriptorBase::log() const
154 {
155 ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceTypeAddr.mType,
156 ::android::toString(mDeviceTypeAddr.mType).c_str(),
157 mDeviceTypeAddr.getAddress());
158
159 AudioPort::log(" ");
160 }
161
162 template<typename T>
checkEqual(const T & f1,const T & f2)163 bool checkEqual(const T& f1, const T& f2)
164 {
165 std::set<typename T::value_type> s1(f1.begin(), f1.end());
166 std::set<typename T::value_type> s2(f2.begin(), f2.end());
167 return s1 == s2;
168 }
169
equals(const sp<DeviceDescriptorBase> & other) const170 bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
171 {
172 return other != nullptr &&
173 static_cast<const AudioPort*>(this)->equals(other) &&
174 static_cast<const AudioPortConfig*>(this)->equals(other, useInputChannelMask()) &&
175 mDeviceTypeAddr.equals(other->mDeviceTypeAddr) &&
176 checkEqual(mEncodedFormats, other->mEncodedFormats);
177 }
178
supportsFormat(audio_format_t format)179 bool DeviceDescriptorBase::supportsFormat(audio_format_t format)
180 {
181 if (mEncodedFormats.empty()) {
182 return true;
183 }
184
185 for (const auto& devFormat : mEncodedFormats) {
186 if (devFormat == format) {
187 return true;
188 }
189 }
190 return false;
191 }
192
writeToParcelable(media::AudioPortFw * parcelable) const193 status_t DeviceDescriptorBase::writeToParcelable(media::AudioPortFw* parcelable) const {
194 AudioPort::writeToParcelable(parcelable);
195 AudioPortConfig::writeToParcelable(&parcelable->sys.activeConfig.hal, useInputChannelMask());
196 parcelable->hal.id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
197 parcelable->sys.activeConfig.hal.portId = parcelable->hal.id;
198
199 media::audio::common::AudioPortDeviceExt deviceExt;
200 deviceExt.device = VALUE_OR_RETURN_STATUS(
201 legacy2aidl_AudioDeviceTypeAddress(mDeviceTypeAddr));
202 deviceExt.encodedFormats = VALUE_OR_RETURN_STATUS(
203 convertContainer<std::vector<media::audio::common::AudioFormatDescription>>(
204 mEncodedFormats, legacy2aidl_audio_format_t_AudioFormatDescription));
205 deviceExt.encapsulationModes = VALUE_OR_RETURN_STATUS(
206 legacy2aidl_AudioEncapsulationMode_mask(mEncapsulationModes));
207 deviceExt.encapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
208 legacy2aidl_AudioEncapsulationMetadataType_mask(mEncapsulationMetadataTypes));
209 UNION_SET(parcelable->hal.ext, device, deviceExt);
210 media::AudioPortDeviceExtSys deviceSys;
211 UNION_SET(parcelable->sys.ext, device, deviceSys);
212 return OK;
213 }
214
readFromParcelable(const media::AudioPortFw & parcelable)215 status_t DeviceDescriptorBase::readFromParcelable(const media::AudioPortFw& parcelable) {
216 if (parcelable.sys.type != media::AudioPortType::DEVICE) {
217 return BAD_VALUE;
218 }
219 status_t status = AudioPort::readFromParcelable(parcelable)
220 ?: AudioPortConfig::readFromParcelable(
221 parcelable.sys.activeConfig.hal, useInputChannelMask());
222 if (status != OK) {
223 return status;
224 }
225
226 media::audio::common::AudioPortDeviceExt deviceExt = VALUE_OR_RETURN_STATUS(
227 UNION_GET(parcelable.hal.ext, device));
228 mDeviceTypeAddr = VALUE_OR_RETURN_STATUS(
229 aidl2legacy_AudioDeviceTypeAddress(deviceExt.device));
230 mEncodedFormats = VALUE_OR_RETURN_STATUS(
231 convertContainer<FormatVector>(deviceExt.encodedFormats,
232 aidl2legacy_AudioFormatDescription_audio_format_t));
233 mEncapsulationModes = VALUE_OR_RETURN_STATUS(
234 aidl2legacy_AudioEncapsulationMode_mask(deviceExt.encapsulationModes));
235 mEncapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
236 aidl2legacy_AudioEncapsulationMetadataType_mask(deviceExt.encapsulationMetadataTypes));
237 media::AudioPortDeviceExtSys deviceSys = VALUE_OR_RETURN_STATUS(
238 UNION_GET(parcelable.sys.ext, device));
239 return OK;
240 }
241
toString(const DeviceDescriptorBaseVector & devices)242 std::string toString(const DeviceDescriptorBaseVector& devices)
243 {
244 std::string ret;
245 for (const auto& device : devices) {
246 if (device != *devices.begin()) {
247 ret += ";";
248 }
249 ret += device->toString();
250 }
251 return ret;
252 }
253
deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector & devices)254 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices)
255 {
256 AudioDeviceTypeAddrVector deviceTypeAddrs;
257 for (const auto& device : devices) {
258 deviceTypeAddrs.push_back(device->getDeviceTypeAddr());
259 }
260 return deviceTypeAddrs;
261 }
262
263 ConversionResult<sp<DeviceDescriptorBase>>
aidl2legacy_DeviceDescriptorBase(const media::AudioPortFw & aidl)264 aidl2legacy_DeviceDescriptorBase(const media::AudioPortFw& aidl) {
265 sp<DeviceDescriptorBase> result = new DeviceDescriptorBase(AUDIO_DEVICE_NONE);
266 status_t status = result->readFromParcelable(aidl);
267 if (status != OK) {
268 return base::unexpected(status);
269 }
270 return result;
271 }
272
273 ConversionResult<media::AudioPortFw>
legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase> & legacy)274 legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy) {
275 media::AudioPortFw aidl;
276 status_t status = legacy->writeToParcelable(&aidl);
277 if (status != OK) {
278 return base::unexpected(status);
279 }
280 return aidl;
281 }
282
283 } // namespace android
284