DeviceDescriptor.cpp revision f4ad6e5637b6deccdac4b60615383f290b3806cf
1/* 2 * Copyright (C) 2015 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 "APM::Devices" 18//#define LOG_NDEBUG 0 19 20#include "DeviceDescriptor.h" 21#include "TypeConverter.h" 22#include "AudioGain.h" 23#include "HwModule.h" 24 25namespace android { 26 27DeviceDescriptor::DeviceDescriptor(audio_devices_t type, const String8 &tagName) : 28 AudioPort(String8(""), AUDIO_PORT_TYPE_DEVICE, 29 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : 30 AUDIO_PORT_ROLE_SOURCE), 31 mAddress(""), mTagName(tagName), mDeviceType(type), mId(0) 32{ 33 if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX || type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) { 34 mAddress = String8("0"); 35 } 36} 37 38audio_port_handle_t DeviceDescriptor::getId() const 39{ 40 return mId; 41} 42 43void DeviceDescriptor::attach(const sp<HwModule>& module) 44{ 45 AudioPort::attach(module); 46 mId = getNextUniqueId(); 47} 48 49bool DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const 50{ 51 // Devices are considered equal if they: 52 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) 53 // - have the same address or one device does not specify the address 54 // - have the same channel mask or one device does not specify the channel mask 55 if (other == 0) { 56 return false; 57 } 58 return (mDeviceType == other->mDeviceType) && 59 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && 60 (mChannelMask == 0 || other->mChannelMask == 0 || 61 mChannelMask == other->mChannelMask); 62} 63 64void DeviceVector::refreshTypes() 65{ 66 mDeviceTypes = AUDIO_DEVICE_NONE; 67 for(size_t i = 0; i < size(); i++) { 68 mDeviceTypes |= itemAt(i)->type(); 69 } 70 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); 71} 72 73ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const 74{ 75 for(size_t i = 0; i < size(); i++) { 76 if (item->equals(itemAt(i))) { 77 return i; 78 } 79 } 80 return -1; 81} 82 83void DeviceVector::add(const DeviceVector &devices) 84{ 85 for (size_t i = 0; i < devices.size(); i++) { 86 sp<DeviceDescriptor> device = devices.itemAt(i); 87 if (indexOf(device) < 0 && SortedVector::add(device) >= 0) { 88 refreshTypes(); 89 } 90 } 91} 92 93ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item) 94{ 95 ssize_t ret = indexOf(item); 96 97 if (ret < 0) { 98 ret = SortedVector::add(item); 99 if (ret >= 0) { 100 refreshTypes(); 101 } 102 } else { 103 ALOGW("DeviceVector::add device %08x already in", item->type()); 104 ret = -1; 105 } 106 return ret; 107} 108 109ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item) 110{ 111 size_t i; 112 ssize_t ret = indexOf(item); 113 114 if (ret < 0) { 115 ALOGW("DeviceVector::remove device %08x not in", item->type()); 116 } else { 117 ret = SortedVector::removeAt(ret); 118 if (ret >= 0) { 119 refreshTypes(); 120 } 121 } 122 return ret; 123} 124 125audio_devices_t DeviceVector::getDevicesFromHwModule(audio_module_handle_t moduleHandle) const 126{ 127 audio_devices_t devices = AUDIO_DEVICE_NONE; 128 for (size_t i = 0; i < size(); i++) { 129 if (itemAt(i)->getModuleHandle() == moduleHandle) { 130 devices |= itemAt(i)->type(); 131 } 132 } 133 return devices; 134} 135 136sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, String8 address) const 137{ 138 sp<DeviceDescriptor> device; 139 for (size_t i = 0; i < size(); i++) { 140 if (itemAt(i)->type() == type) { 141 if (address == "" || itemAt(i)->mAddress == address) { 142 device = itemAt(i); 143 if (itemAt(i)->mAddress == address) { 144 break; 145 } 146 } 147 } 148 } 149 ALOGV("DeviceVector::getDevice() for type %08x address %s found %p", 150 type, address.string(), device.get()); 151 return device; 152} 153 154sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const 155{ 156 sp<DeviceDescriptor> device; 157 for (size_t i = 0; i < size(); i++) { 158 if (itemAt(i)->getId() == id) { 159 device = itemAt(i); 160 break; 161 } 162 } 163 return device; 164} 165 166DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const 167{ 168 DeviceVector devices; 169 bool isOutput = audio_is_output_devices(type); 170 type &= ~AUDIO_DEVICE_BIT_IN; 171 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { 172 bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType); 173 audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN; 174 if ((isOutput == curIsOutput) && ((type & curType) != 0)) { 175 devices.add(itemAt(i)); 176 type &= ~curType; 177 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", 178 itemAt(i)->type(), itemAt(i).get()); 179 } 180 } 181 return devices; 182} 183 184DeviceVector DeviceVector::getDevicesFromTypeAddr( 185 audio_devices_t type, String8 address) const 186{ 187 DeviceVector devices; 188 for (size_t i = 0; i < size(); i++) { 189 if (itemAt(i)->type() == type) { 190 if (itemAt(i)->mAddress == address) { 191 devices.add(itemAt(i)); 192 } 193 } 194 } 195 return devices; 196} 197 198sp<DeviceDescriptor> DeviceVector::getDeviceFromTagName(const String8 &tagName) const 199{ 200 sp<DeviceDescriptor> device; 201 for (size_t i = 0; i < size(); i++) { 202 if (itemAt(i)->getTagName() == tagName) { 203 device = itemAt(i); 204 break; 205 } 206 } 207 return device; 208} 209 210status_t DeviceVector::dump(int fd, const String8 &tag, int spaces, bool verbose) const 211{ 212 if (isEmpty()) { 213 return NO_ERROR; 214 } 215 const size_t SIZE = 256; 216 char buffer[SIZE]; 217 218 snprintf(buffer, SIZE, "%*s- %s devices:\n", spaces, "", tag.string()); 219 write(fd, buffer, strlen(buffer)); 220 for (size_t i = 0; i < size(); i++) { 221 itemAt(i)->dump(fd, spaces + 2, i, verbose); 222 } 223 return NO_ERROR; 224} 225 226audio_policy_dev_state_t DeviceVector::getDeviceConnectionState(const sp<DeviceDescriptor> &devDesc) const 227{ 228 ssize_t index = indexOf(devDesc); 229 return index >= 0 ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 230} 231 232void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig, 233 const struct audio_port_config *srcConfig) const 234{ 235 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; 236 if (srcConfig != NULL) { 237 dstConfig->config_mask |= srcConfig->config_mask; 238 } 239 240 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); 241 242 dstConfig->id = mId; 243 dstConfig->role = audio_is_output_device(mDeviceType) ? 244 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; 245 dstConfig->type = AUDIO_PORT_TYPE_DEVICE; 246 dstConfig->ext.device.type = mDeviceType; 247 248 //TODO Understand why this test is necessary. i.e. why at boot time does it crash 249 // without the test? 250 // This has been demonstrated to NOT be true (at start up) 251 // ALOG_ASSERT(mModule != NULL); 252 dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE; 253 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 254} 255 256void DeviceDescriptor::toAudioPort(struct audio_port *port) const 257{ 258 ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); 259 AudioPort::toAudioPort(port); 260 port->id = mId; 261 toAudioPortConfig(&port->active_config); 262 port->ext.device.type = mDeviceType; 263 port->ext.device.hw_module = mModule->mHandle; 264 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 265} 266 267void DeviceDescriptor::importAudioPort(const sp<AudioPort> port) { 268 AudioPort::importAudioPort(port); 269 port->pickAudioProfile(mSamplingRate, mChannelMask, mFormat); 270} 271 272status_t DeviceDescriptor::dump(int fd, int spaces, int index, bool verbose) const 273{ 274 const size_t SIZE = 256; 275 char buffer[SIZE]; 276 String8 result; 277 278 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); 279 result.append(buffer); 280 if (mId != 0) { 281 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); 282 result.append(buffer); 283 } 284 if (!mTagName.isEmpty()) { 285 snprintf(buffer, SIZE, "%*s- tag name: %s\n", spaces, "", mTagName.string()); 286 result.append(buffer); 287 } 288 std::string deviceLiteral; 289 if (DeviceConverter::toString(mDeviceType, deviceLiteral)) { 290 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", deviceLiteral.c_str()); 291 result.append(buffer); 292 } 293 if (mAddress.size() != 0) { 294 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); 295 result.append(buffer); 296 } 297 write(fd, result.string(), result.size()); 298 AudioPort::dump(fd, spaces, verbose); 299 300 return NO_ERROR; 301} 302 303void DeviceDescriptor::log() const 304{ 305 std::string device; 306 DeviceConverter::toString(mDeviceType, device); 307 ALOGI("Device id:%d type:0x%X:%s, addr:%s", mId, mDeviceType, device.c_str(), 308 mAddress.string()); 309 310 AudioPort::log(" "); 311} 312 313}; // namespace android 314