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> |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 22 | #include <media/DeviceDescriptorBase.h> |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 23 | #include <media/TypeConverter.h> |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 27 | DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) : |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 28 | AudioPort("", AUDIO_PORT_TYPE_DEVICE, |
| 29 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : |
| 30 | AUDIO_PORT_ROLE_SOURCE), |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 31 | mDeviceType(type) |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 32 | { |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 33 | if (audio_is_remote_submix_device(type)) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 34 | mAddress = "0"; |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 38 | void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig, |
| 39 | const struct audio_port_config *srcConfig) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 40 | { |
| 41 | dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN; |
| 42 | if (mSamplingRate != 0) { |
| 43 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE; |
| 44 | } |
| 45 | if (mChannelMask != AUDIO_CHANNEL_NONE) { |
| 46 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK; |
| 47 | } |
| 48 | if (mFormat != AUDIO_FORMAT_INVALID) { |
| 49 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT; |
| 50 | } |
| 51 | |
| 52 | if (srcConfig != NULL) { |
| 53 | dstConfig->config_mask |= srcConfig->config_mask; |
| 54 | } |
| 55 | |
| 56 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 57 | |
| 58 | dstConfig->role = audio_is_output_device(mDeviceType) ? |
| 59 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
| 60 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; |
| 61 | dstConfig->ext.device.type = mDeviceType; |
| 62 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 63 | (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mAddress.c_str()); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 64 | } |
| 65 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 66 | void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 67 | { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 68 | ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceType); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 69 | AudioPort::toAudioPort(port); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 70 | toAudioPortConfig(&port->active_config); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 71 | port->id = mId; |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 72 | port->ext.device.type = mDeviceType; |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 73 | (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mAddress.c_str()); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 74 | } |
| 75 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 76 | void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index, |
| 77 | const char* extraInfo, bool verbose) const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 78 | { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 79 | dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1)); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 80 | if (mId != 0) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 81 | dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId)); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 82 | } |
| 83 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 84 | if (extraInfo != nullptr) { |
| 85 | dst->append(extraInfo); |
| 86 | } |
| 87 | |
| 88 | dst->append(base::StringPrintf("%*s- type: %-48s\n", |
| 89 | spaces, "", ::android::toString(mDeviceType).c_str())); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 90 | |
| 91 | if (mAddress.size() != 0) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 92 | dst->append(base::StringPrintf("%*s- address: %-32s\n", spaces, "", mAddress.c_str())); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 93 | } |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 94 | AudioPort::dump(dst, spaces, verbose); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 95 | } |
| 96 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 97 | std::string DeviceDescriptorBase::toString() const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 98 | { |
| 99 | std::stringstream sstream; |
| 100 | sstream << "type:0x" << std::hex << type() << ",@:" << mAddress; |
| 101 | return sstream.str(); |
| 102 | } |
| 103 | |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 104 | void DeviceDescriptorBase::log() const |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 105 | { |
| 106 | ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceType, |
| 107 | ::android::toString(mDeviceType).c_str(), |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 108 | mAddress.c_str()); |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 109 | |
| 110 | AudioPort::log(" "); |
| 111 | } |
| 112 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 113 | bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const |
| 114 | { |
| 115 | return other != nullptr && |
| 116 | static_cast<const AudioPort*>(this)->equals(other) && |
| 117 | static_cast<const AudioPortConfig*>(this)->equals(other) && |
| 118 | mAddress.compare(other->address()) == 0 && |
| 119 | mDeviceType == other->type(); |
| 120 | } |
| 121 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 122 | status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const |
| 123 | { |
| 124 | status_t status = NO_ERROR; |
| 125 | if ((status = AudioPort::writeToParcel(parcel)) != NO_ERROR) return status; |
| 126 | if ((status = AudioPortConfig::writeToParcel(parcel)) != NO_ERROR) return status; |
| 127 | if ((status = parcel->writeUtf8AsUtf16(mAddress)) != NO_ERROR) return status; |
| 128 | if ((status = parcel->writeUint32(mDeviceType)) != NO_ERROR) return status; |
| 129 | return status; |
| 130 | } |
| 131 | |
| 132 | status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel) |
| 133 | { |
| 134 | status_t status = NO_ERROR; |
| 135 | if ((status = AudioPort::readFromParcel(parcel)) != NO_ERROR) return status; |
| 136 | if ((status = AudioPortConfig::readFromParcel(parcel)) != NO_ERROR) return status; |
| 137 | if ((status = parcel->readUtf8FromUtf16(&mAddress)) != NO_ERROR) return status; |
| 138 | if ((status = parcel->readUint32(&mDeviceType)) != NO_ERROR) return status; |
| 139 | return status; |
| 140 | } |
| 141 | |
jiabin | 6ae65d8 | 2019-09-11 10:16:33 -0700 | [diff] [blame] | 142 | } // namespace android |