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
129 AudioPort::dump(dst, spaces, nullptr, verbose);
130 }
131
toString(bool includeSensitiveInfo) const132 std::string DeviceDescriptorBase::toString(bool includeSensitiveInfo) const
133 {
134 return mDeviceTypeAddr.toString(includeSensitiveInfo);
135 }
136
log() const137 void DeviceDescriptorBase::log() const
138 {
139 ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceTypeAddr.mType,
140 ::android::toString(mDeviceTypeAddr.mType).c_str(),
141 mDeviceTypeAddr.getAddress());
142
143 AudioPort::log(" ");
144 }
145
146 template<typename T>
checkEqual(const T & f1,const T & f2)147 bool checkEqual(const T& f1, const T& f2)
148 {
149 std::set<typename T::value_type> s1(f1.begin(), f1.end());
150 std::set<typename T::value_type> s2(f2.begin(), f2.end());
151 return s1 == s2;
152 }
153
equals(const sp<DeviceDescriptorBase> & other) const154 bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
155 {
156 return other != nullptr &&
157 static_cast<const AudioPort*>(this)->equals(other) &&
158 static_cast<const AudioPortConfig*>(this)->equals(other, useInputChannelMask()) &&
159 mDeviceTypeAddr.equals(other->mDeviceTypeAddr) &&
160 checkEqual(mEncodedFormats, other->mEncodedFormats);
161 }
162
supportsFormat(audio_format_t format)163 bool DeviceDescriptorBase::supportsFormat(audio_format_t format)
164 {
165 if (mEncodedFormats.empty()) {
166 return true;
167 }
168
169 for (const auto& devFormat : mEncodedFormats) {
170 if (devFormat == format) {
171 return true;
172 }
173 }
174 return false;
175 }
176
writeToParcelable(media::AudioPort * parcelable) const177 status_t DeviceDescriptorBase::writeToParcelable(media::AudioPort* parcelable) const {
178 AudioPort::writeToParcelable(parcelable);
179 AudioPortConfig::writeToParcelable(&parcelable->sys.activeConfig.hal, useInputChannelMask());
180 parcelable->hal.id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
181 parcelable->sys.activeConfig.hal.portId = parcelable->hal.id;
182
183 media::audio::common::AudioPortDeviceExt deviceExt;
184 deviceExt.device = VALUE_OR_RETURN_STATUS(
185 legacy2aidl_AudioDeviceTypeAddress(mDeviceTypeAddr));
186 deviceExt.encodedFormats = VALUE_OR_RETURN_STATUS(
187 convertContainer<std::vector<media::audio::common::AudioFormatDescription>>(
188 mEncodedFormats, legacy2aidl_audio_format_t_AudioFormatDescription));
189 UNION_SET(parcelable->hal.ext, device, deviceExt);
190 media::AudioPortDeviceExtSys deviceSys;
191 deviceSys.encapsulationModes = VALUE_OR_RETURN_STATUS(
192 legacy2aidl_AudioEncapsulationMode_mask(mEncapsulationModes));
193 deviceSys.encapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
194 legacy2aidl_AudioEncapsulationMetadataType_mask(mEncapsulationMetadataTypes));
195 UNION_SET(parcelable->sys.ext, device, deviceSys);
196 return OK;
197 }
198
readFromParcelable(const media::AudioPort & parcelable)199 status_t DeviceDescriptorBase::readFromParcelable(const media::AudioPort& parcelable) {
200 if (parcelable.sys.type != media::AudioPortType::DEVICE) {
201 return BAD_VALUE;
202 }
203 status_t status = AudioPort::readFromParcelable(parcelable)
204 ?: AudioPortConfig::readFromParcelable(
205 parcelable.sys.activeConfig.hal, useInputChannelMask());
206 if (status != OK) {
207 return status;
208 }
209
210 media::audio::common::AudioPortDeviceExt deviceExt = VALUE_OR_RETURN_STATUS(
211 UNION_GET(parcelable.hal.ext, device));
212 mDeviceTypeAddr = VALUE_OR_RETURN_STATUS(
213 aidl2legacy_AudioDeviceTypeAddress(deviceExt.device));
214 mEncodedFormats = VALUE_OR_RETURN_STATUS(
215 convertContainer<FormatVector>(deviceExt.encodedFormats,
216 aidl2legacy_AudioFormatDescription_audio_format_t));
217 media::AudioPortDeviceExtSys deviceSys = VALUE_OR_RETURN_STATUS(
218 UNION_GET(parcelable.sys.ext, device));
219 mEncapsulationModes = VALUE_OR_RETURN_STATUS(
220 aidl2legacy_AudioEncapsulationMode_mask(deviceSys.encapsulationModes));
221 mEncapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
222 aidl2legacy_AudioEncapsulationMetadataType_mask(deviceSys.encapsulationMetadataTypes));
223 return OK;
224 }
225
toString(const DeviceDescriptorBaseVector & devices)226 std::string toString(const DeviceDescriptorBaseVector& devices)
227 {
228 std::string ret;
229 for (const auto& device : devices) {
230 if (device != *devices.begin()) {
231 ret += ";";
232 }
233 ret += device->toString();
234 }
235 return ret;
236 }
237
deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector & devices)238 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices)
239 {
240 AudioDeviceTypeAddrVector deviceTypeAddrs;
241 for (const auto& device : devices) {
242 deviceTypeAddrs.push_back(device->getDeviceTypeAddr());
243 }
244 return deviceTypeAddrs;
245 }
246
247 ConversionResult<sp<DeviceDescriptorBase>>
aidl2legacy_DeviceDescriptorBase(const media::AudioPort & aidl)248 aidl2legacy_DeviceDescriptorBase(const media::AudioPort& aidl) {
249 sp<DeviceDescriptorBase> result = new DeviceDescriptorBase(AUDIO_DEVICE_NONE);
250 status_t status = result->readFromParcelable(aidl);
251 if (status != OK) {
252 return base::unexpected(status);
253 }
254 return result;
255 }
256
257 ConversionResult<media::AudioPort>
legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase> & legacy)258 legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy) {
259 media::AudioPort aidl;
260 status_t status = legacy->writeToParcelable(&aidl);
261 if (status != OK) {
262 return base::unexpected(status);
263 }
264 return aidl;
265 }
266
267 } // namespace android
268