jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 1 | /* |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 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 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DeviceDescriptorBase" |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 21 | #include <audio_utils/string.h> |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 22 | #include <media/AidlConversion.h> |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 23 | #include <media/DeviceDescriptorBase.h> |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 24 | #include <media/TypeConverter.h> |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 28 | DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) : |
jiabin | 36e2dea | 2019-11-07 15:47:01 -0800 | [diff] [blame] | 29 | DeviceDescriptorBase(type, "") |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 30 | { |
jiabin | 36e2dea | 2019-11-07 15:47:01 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type, const std::string& address) : |
| 34 | DeviceDescriptorBase(AudioDeviceTypeAddr(type, address)) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | DeviceDescriptorBase::DeviceDescriptorBase(const AudioDeviceTypeAddr &deviceTypeAddr) : |
| 39 | AudioPort("", AUDIO_PORT_TYPE_DEVICE, |
| 40 | audio_is_output_device(deviceTypeAddr.mType) ? AUDIO_PORT_ROLE_SINK : |
| 41 | AUDIO_PORT_ROLE_SOURCE), |
| 42 | mDeviceTypeAddr(deviceTypeAddr) |
| 43 | { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 44 | if (mDeviceTypeAddr.address().empty() && audio_is_remote_submix_device(mDeviceTypeAddr.mType)) { |
| 45 | mDeviceTypeAddr.setAddress("0"); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 46 | } |
jiabin | 827df1b | 2020-07-29 15:09:52 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void DeviceDescriptorBase::setAddress(const std::string &address) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 50 | mDeviceTypeAddr.setAddress(address); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 53 | void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig, |
| 54 | const struct audio_port_config *srcConfig) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 55 | { |
| 56 | dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN; |
| 57 | if (mSamplingRate != 0) { |
| 58 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE; |
| 59 | } |
| 60 | if (mChannelMask != AUDIO_CHANNEL_NONE) { |
| 61 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK; |
| 62 | } |
| 63 | if (mFormat != AUDIO_FORMAT_INVALID) { |
| 64 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT; |
| 65 | } |
| 66 | |
| 67 | if (srcConfig != NULL) { |
| 68 | dstConfig->config_mask |= srcConfig->config_mask; |
| 69 | } |
| 70 | |
| 71 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 72 | |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 73 | dstConfig->role = audio_is_output_device(mDeviceTypeAddr.mType) ? |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 74 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
| 75 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 76 | dstConfig->ext.device.type = mDeviceTypeAddr.mType; |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 77 | |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 78 | (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mDeviceTypeAddr.getAddress()); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 79 | } |
| 80 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 81 | void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 82 | { |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 83 | ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceTypeAddr.mType); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 84 | toAudioPortInternal(port); |
| 85 | } |
| 86 | |
| 87 | void DeviceDescriptorBase::toAudioPort(struct audio_port_v7 *port) const { |
| 88 | ALOGV("DeviceDescriptorBase::toAudioPort() v7 handle %d type %08x", mId, mDeviceTypeAddr.mType); |
| 89 | toAudioPortInternal(port); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 90 | } |
| 91 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 92 | status_t DeviceDescriptorBase::setEncapsulationModes(uint32_t encapsulationModes) { |
| 93 | if ((encapsulationModes & ~AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS) != 0) { |
| 94 | return BAD_VALUE; |
| 95 | } |
| 96 | mEncapsulationModes = encapsulationModes & ~(1 << AUDIO_ENCAPSULATION_MODE_NONE); |
| 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
| 100 | status_t DeviceDescriptorBase::setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes) { |
| 101 | if ((encapsulationMetadataTypes & ~AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS) != 0) { |
| 102 | return BAD_VALUE; |
| 103 | } |
| 104 | mEncapsulationMetadataTypes = |
| 105 | encapsulationMetadataTypes & ~(1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE); |
| 106 | return NO_ERROR; |
| 107 | } |
| 108 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 109 | void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index, |
| 110 | const char* extraInfo, bool verbose) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 111 | { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 112 | dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1)); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 113 | if (mId != 0) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 114 | dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId)); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 115 | } |
| 116 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 117 | if (extraInfo != nullptr) { |
| 118 | dst->append(extraInfo); |
| 119 | } |
| 120 | |
| 121 | dst->append(base::StringPrintf("%*s- type: %-48s\n", |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 122 | spaces, "", ::android::toString(mDeviceTypeAddr.mType).c_str())); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 123 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 124 | dst->append(base::StringPrintf( |
| 125 | "%*s- supported encapsulation modes: %u", spaces, "", mEncapsulationModes)); |
| 126 | dst->append(base::StringPrintf( |
| 127 | "%*s- supported encapsulation metadata types: %u", |
| 128 | spaces, "", mEncapsulationMetadataTypes)); |
| 129 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 130 | if (mDeviceTypeAddr.address().size() != 0) { |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 131 | dst->append(base::StringPrintf( |
| 132 | "%*s- address: %-32s\n", spaces, "", mDeviceTypeAddr.getAddress())); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 133 | } |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 134 | AudioPort::dump(dst, spaces, verbose); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 135 | } |
| 136 | |
jiabin | 827df1b | 2020-07-29 15:09:52 -0700 | [diff] [blame] | 137 | std::string DeviceDescriptorBase::toString(bool includeSensitiveInfo) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 138 | { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 139 | return mDeviceTypeAddr.toString(includeSensitiveInfo); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 140 | } |
| 141 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 142 | void DeviceDescriptorBase::log() const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 143 | { |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 144 | ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceTypeAddr.mType, |
| 145 | ::android::toString(mDeviceTypeAddr.mType).c_str(), |
| 146 | mDeviceTypeAddr.getAddress()); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 147 | |
| 148 | AudioPort::log(" "); |
| 149 | } |
| 150 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 151 | bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const |
| 152 | { |
| 153 | return other != nullptr && |
| 154 | static_cast<const AudioPort*>(this)->equals(other) && |
| 155 | static_cast<const AudioPortConfig*>(this)->equals(other) && |
jiabin | 5d51ca9 | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 156 | mDeviceTypeAddr.equals(other->mDeviceTypeAddr); |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 159 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 160 | status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const |
| 161 | { |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 162 | media::AudioPort parcelable; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 163 | return writeToParcelable(&parcelable) |
| 164 | ?: parcelable.writeToParcel(parcel); |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 167 | status_t DeviceDescriptorBase::writeToParcelable(media::AudioPort* parcelable) const { |
| 168 | AudioPort::writeToParcelable(parcelable); |
| 169 | AudioPortConfig::writeToParcelable(&parcelable->activeConfig); |
| 170 | parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId)); |
| 171 | |
| 172 | media::AudioPortDeviceExt ext; |
| 173 | ext.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(mDeviceTypeAddr)); |
| 174 | ext.encapsulationModes = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 175 | legacy2aidl_AudioEncapsulationMode_mask(mEncapsulationModes)); |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 176 | ext.encapsulationMetadataTypes = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 177 | legacy2aidl_AudioEncapsulationMetadataType_mask(mEncapsulationMetadataTypes)); |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 178 | UNION_SET(parcelable->ext, device, std::move(ext)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel) { |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 183 | media::AudioPort parcelable; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 184 | return parcelable.readFromParcel(parcel) |
| 185 | ?: readFromParcelable(parcelable); |
| 186 | } |
| 187 | |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 188 | status_t DeviceDescriptorBase::readFromParcelable(const media::AudioPort& parcelable) { |
| 189 | if (parcelable.type != media::AudioPortType::DEVICE) { |
| 190 | return BAD_VALUE; |
| 191 | } |
| 192 | status_t status = AudioPort::readFromParcelable(parcelable) |
| 193 | ?: AudioPortConfig::readFromParcelable(parcelable.activeConfig); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 194 | if (status != OK) { |
| 195 | return status; |
| 196 | } |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 197 | |
| 198 | media::AudioPortDeviceExt ext = VALUE_OR_RETURN_STATUS(UNION_GET(parcelable.ext, device)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 199 | mDeviceTypeAddr = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 200 | aidl2legacy_AudioDeviceTypeAddress(ext.device)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 201 | mEncapsulationModes = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 202 | aidl2legacy_AudioEncapsulationMode_mask(ext.encapsulationModes)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 203 | mEncapsulationMetadataTypes = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 204 | aidl2legacy_AudioEncapsulationMetadataType_mask(ext.encapsulationMetadataTypes)); |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 205 | return OK; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 206 | } |
| 207 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 208 | std::string toString(const DeviceDescriptorBaseVector& devices) |
| 209 | { |
| 210 | std::string ret; |
| 211 | for (const auto& device : devices) { |
| 212 | if (device != *devices.begin()) { |
| 213 | ret += ";"; |
| 214 | } |
| 215 | ret += device->toString(); |
| 216 | } |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices) |
| 221 | { |
| 222 | AudioDeviceTypeAddrVector deviceTypeAddrs; |
| 223 | for (const auto& device : devices) { |
| 224 | deviceTypeAddrs.push_back(device->getDeviceTypeAddr()); |
| 225 | } |
| 226 | return deviceTypeAddrs; |
| 227 | } |
| 228 | |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 229 | ConversionResult<sp<DeviceDescriptorBase>> |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 230 | aidl2legacy_DeviceDescriptorBase(const media::AudioPort& aidl) { |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 231 | sp<DeviceDescriptorBase> result = new DeviceDescriptorBase(AUDIO_DEVICE_NONE); |
| 232 | status_t status = result->readFromParcelable(aidl); |
| 233 | if (status != OK) { |
| 234 | return base::unexpected(status); |
| 235 | } |
| 236 | return result; |
| 237 | } |
| 238 | |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 239 | ConversionResult<media::AudioPort> |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 240 | legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy) { |
Ytai Ben-Tsvi | f9dd924 | 2020-11-23 15:44:10 -0800 | [diff] [blame] | 241 | media::AudioPort aidl; |
Ytai Ben-Tsvi | 50e016a | 2020-11-12 14:26:12 -0800 | [diff] [blame] | 242 | status_t status = legacy->writeToParcelable(&aidl); |
| 243 | if (status != OK) { |
| 244 | return base::unexpected(status); |
| 245 | } |
| 246 | return aidl; |
| 247 | } |
| 248 | |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 249 | } // namespace android |