jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 1 | /* |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
jiabin | 2177b12 | 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 | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DeviceDescriptorBase" |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 21 | #include <audio_utils/string.h> |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 22 | #include <media/DeviceDescriptorBase.h> |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 23 | #include <media/TypeConverter.h> |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 27 | DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) : |
jiabin | 7946fbe | 2019-11-07 15:47:01 -0800 | [diff] [blame] | 28 | DeviceDescriptorBase(type, "") |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 29 | { |
jiabin | 7946fbe | 2019-11-07 15:47:01 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type, const std::string& address) : |
| 33 | DeviceDescriptorBase(AudioDeviceTypeAddr(type, address)) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | DeviceDescriptorBase::DeviceDescriptorBase(const AudioDeviceTypeAddr &deviceTypeAddr) : |
| 38 | AudioPort("", AUDIO_PORT_TYPE_DEVICE, |
| 39 | audio_is_output_device(deviceTypeAddr.mType) ? AUDIO_PORT_ROLE_SINK : |
| 40 | AUDIO_PORT_ROLE_SOURCE), |
| 41 | mDeviceTypeAddr(deviceTypeAddr) |
| 42 | { |
| 43 | if (mDeviceTypeAddr.mAddress.empty() && audio_is_remote_submix_device(mDeviceTypeAddr.mType)) { |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 44 | mDeviceTypeAddr.mAddress = "0"; |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 48 | void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig, |
| 49 | const struct audio_port_config *srcConfig) const |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 50 | { |
| 51 | dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN; |
| 52 | if (mSamplingRate != 0) { |
| 53 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE; |
| 54 | } |
| 55 | if (mChannelMask != AUDIO_CHANNEL_NONE) { |
| 56 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK; |
| 57 | } |
| 58 | if (mFormat != AUDIO_FORMAT_INVALID) { |
| 59 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT; |
| 60 | } |
| 61 | |
| 62 | if (srcConfig != NULL) { |
| 63 | dstConfig->config_mask |= srcConfig->config_mask; |
| 64 | } |
| 65 | |
| 66 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 67 | |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 68 | dstConfig->role = audio_is_output_device(mDeviceTypeAddr.mType) ? |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 69 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
| 70 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 71 | dstConfig->ext.device.type = mDeviceTypeAddr.mType; |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 72 | |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 73 | (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mDeviceTypeAddr.getAddress()); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 74 | } |
| 75 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 76 | void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 77 | { |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 78 | ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceTypeAddr.mType); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 79 | AudioPort::toAudioPort(port); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 80 | toAudioPortConfig(&port->active_config); |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 81 | port->id = mId; |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 82 | port->ext.device.type = mDeviceTypeAddr.mType; |
| 83 | (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mDeviceTypeAddr.getAddress()); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 84 | } |
| 85 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 86 | void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index, |
| 87 | const char* extraInfo, bool verbose) const |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 88 | { |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 89 | dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1)); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 90 | if (mId != 0) { |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 91 | dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId)); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 92 | } |
| 93 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 94 | if (extraInfo != nullptr) { |
| 95 | dst->append(extraInfo); |
| 96 | } |
| 97 | |
| 98 | dst->append(base::StringPrintf("%*s- type: %-48s\n", |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 99 | spaces, "", ::android::toString(mDeviceTypeAddr.mType).c_str())); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 100 | |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 101 | if (mDeviceTypeAddr.mAddress.size() != 0) { |
| 102 | dst->append(base::StringPrintf( |
| 103 | "%*s- address: %-32s\n", spaces, "", mDeviceTypeAddr.getAddress())); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 104 | } |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 105 | AudioPort::dump(dst, spaces, verbose); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 106 | } |
| 107 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 108 | std::string DeviceDescriptorBase::toString() const |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 109 | { |
| 110 | std::stringstream sstream; |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 111 | sstream << "type:0x" << std::hex << type() << ",@:" << mDeviceTypeAddr.mAddress; |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 112 | return sstream.str(); |
| 113 | } |
| 114 | |
jiabin | 6713a38 | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 115 | void DeviceDescriptorBase::log() const |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 116 | { |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 117 | ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceTypeAddr.mType, |
| 118 | ::android::toString(mDeviceTypeAddr.mType).c_str(), |
| 119 | mDeviceTypeAddr.getAddress()); |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 120 | |
| 121 | AudioPort::log(" "); |
| 122 | } |
| 123 | |
jiabin | 0da24a3 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 124 | bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const |
| 125 | { |
| 126 | return other != nullptr && |
| 127 | static_cast<const AudioPort*>(this)->equals(other) && |
| 128 | static_cast<const AudioPortConfig*>(this)->equals(other) && |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 129 | mDeviceTypeAddr.equals(other->mDeviceTypeAddr); |
jiabin | 0da24a3 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
jiabin | d19519a | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 132 | status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const |
| 133 | { |
| 134 | status_t status = NO_ERROR; |
| 135 | if ((status = AudioPort::writeToParcel(parcel)) != NO_ERROR) return status; |
| 136 | if ((status = AudioPortConfig::writeToParcel(parcel)) != NO_ERROR) return status; |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 137 | if ((status = parcel->writeParcelable(mDeviceTypeAddr)) != NO_ERROR) return status; |
jiabin | d19519a | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 138 | return status; |
| 139 | } |
| 140 | |
| 141 | status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel) |
| 142 | { |
| 143 | status_t status = NO_ERROR; |
| 144 | if ((status = AudioPort::readFromParcel(parcel)) != NO_ERROR) return status; |
| 145 | if ((status = AudioPortConfig::readFromParcel(parcel)) != NO_ERROR) return status; |
jiabin | 020b0dc | 2019-11-05 11:59:53 -0800 | [diff] [blame] | 146 | if ((status = parcel->readParcelable(&mDeviceTypeAddr)) != NO_ERROR) return status; |
jiabin | d19519a | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 147 | return status; |
| 148 | } |
| 149 | |
jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 150 | std::string toString(const DeviceDescriptorBaseVector& devices) |
| 151 | { |
| 152 | std::string ret; |
| 153 | for (const auto& device : devices) { |
| 154 | if (device != *devices.begin()) { |
| 155 | ret += ";"; |
| 156 | } |
| 157 | ret += device->toString(); |
| 158 | } |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices) |
| 163 | { |
| 164 | AudioDeviceTypeAddrVector deviceTypeAddrs; |
| 165 | for (const auto& device : devices) { |
| 166 | deviceTypeAddrs.push_back(device->getDeviceTypeAddr()); |
| 167 | } |
| 168 | return deviceTypeAddrs; |
| 169 | } |
| 170 | |
jiabin | 2177b12 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 171 | } // namespace android |