DeviceDescriptor.cpp revision 322b4d25387a04c9afebe998326d005bbdf17ede
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 "AudioGain.h" 22#include "HwModule.h" 23#include "ConfigParsingUtils.h" 24 25namespace android { 26 27String8 DeviceDescriptor::emptyNameStr = String8(""); 28 29DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) : 30 AudioPort(name, AUDIO_PORT_TYPE_DEVICE, 31 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : 32 AUDIO_PORT_ROLE_SOURCE), 33 mAddress(""), mDeviceType(type), mId(0) 34{ 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 return (mDeviceType == other->mDeviceType) && 56 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && 57 (mChannelMask == 0 || other->mChannelMask == 0 || 58 mChannelMask == other->mChannelMask); 59} 60 61void DeviceDescriptor::loadGains(cnode *root) 62{ 63 AudioPort::loadGains(root); 64 if (mGains.size() > 0) { 65 mGains[0]->getDefaultConfig(&mGain); 66 } 67} 68 69void DeviceVector::refreshTypes() 70{ 71 mDeviceTypes = AUDIO_DEVICE_NONE; 72 for(size_t i = 0; i < size(); i++) { 73 mDeviceTypes |= itemAt(i)->type(); 74 } 75 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); 76} 77 78ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const 79{ 80 for(size_t i = 0; i < size(); i++) { 81 if (item->equals(itemAt(i))) { 82 return i; 83 } 84 } 85 return -1; 86} 87 88ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item) 89{ 90 ssize_t ret = indexOf(item); 91 92 if (ret < 0) { 93 ret = SortedVector::add(item); 94 if (ret >= 0) { 95 refreshTypes(); 96 } 97 } else { 98 ALOGW("DeviceVector::add device %08x already in", item->type()); 99 ret = -1; 100 } 101 return ret; 102} 103 104ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item) 105{ 106 size_t i; 107 ssize_t ret = indexOf(item); 108 109 if (ret < 0) { 110 ALOGW("DeviceVector::remove device %08x not in", item->type()); 111 } else { 112 ret = SortedVector::removeAt(ret); 113 if (ret >= 0) { 114 refreshTypes(); 115 } 116 } 117 return ret; 118} 119 120audio_devices_t DeviceVector::getDevicesFromHwModule(audio_module_handle_t moduleHandle) const 121{ 122 audio_devices_t devices = AUDIO_DEVICE_NONE; 123 for (size_t i = 0; i < size(); i++) { 124 if (itemAt(i)->getModuleHandle() == moduleHandle) { 125 devices |= itemAt(i)->type(); 126 } 127 } 128 return devices; 129} 130 131void DeviceVector::loadDevicesFromType(audio_devices_t types) 132{ 133 DeviceVector deviceList; 134 135 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; 136 types &= ~role_bit; 137 138 while (types) { 139 uint32_t i = 31 - __builtin_clz(types); 140 uint32_t type = 1 << i; 141 types &= ~type; 142 add(new DeviceDescriptor(String8("device_type"), type | role_bit)); 143 } 144} 145 146void DeviceVector::loadDevicesFromName(char *name, 147 const DeviceVector& declaredDevices) 148{ 149 char *devName = strtok(name, "|"); 150 while (devName != NULL) { 151 if (strlen(devName) != 0) { 152 audio_devices_t type = ConfigParsingUtils::stringToEnum(sDeviceTypeToEnumTable, 153 ARRAY_SIZE(sDeviceTypeToEnumTable), 154 devName); 155 if (type != AUDIO_DEVICE_NONE) { 156 devName = (char *)ConfigParsingUtils::enumToString(sDeviceNameToEnumTable, 157 ARRAY_SIZE(sDeviceNameToEnumTable), 158 type); 159 sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(devName), type); 160 if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX || 161 type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) { 162 dev->mAddress = String8("0"); 163 } 164 add(dev); 165 } else { 166 sp<DeviceDescriptor> deviceDesc = 167 declaredDevices.getDeviceFromName(String8(devName)); 168 if (deviceDesc != 0) { 169 add(deviceDesc); 170 } 171 } 172 } 173 devName = strtok(NULL, "|"); 174 } 175} 176 177sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, String8 address) const 178{ 179 sp<DeviceDescriptor> device; 180 for (size_t i = 0; i < size(); i++) { 181 if (itemAt(i)->type() == type) { 182 if (address == "" || itemAt(i)->mAddress == address) { 183 device = itemAt(i); 184 if (itemAt(i)->mAddress == address) { 185 break; 186 } 187 } 188 } 189 } 190 ALOGV("DeviceVector::getDevice() for type %08x address %s found %p", 191 type, address.string(), device.get()); 192 return device; 193} 194 195sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const 196{ 197 sp<DeviceDescriptor> device; 198 for (size_t i = 0; i < size(); i++) { 199 if (itemAt(i)->getId() == id) { 200 device = itemAt(i); 201 break; 202 } 203 } 204 return device; 205} 206 207DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const 208{ 209 DeviceVector devices; 210 bool isOutput = audio_is_output_devices(type); 211 type &= ~AUDIO_DEVICE_BIT_IN; 212 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { 213 bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType); 214 audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN; 215 if ((isOutput == curIsOutput) && ((type & curType) != 0)) { 216 devices.add(itemAt(i)); 217 type &= ~curType; 218 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", 219 itemAt(i)->type(), itemAt(i).get()); 220 } 221 } 222 return devices; 223} 224 225DeviceVector DeviceVector::getDevicesFromTypeAddr( 226 audio_devices_t type, String8 address) const 227{ 228 DeviceVector devices; 229 for (size_t i = 0; i < size(); i++) { 230 if (itemAt(i)->type() == type) { 231 if (itemAt(i)->mAddress == address) { 232 devices.add(itemAt(i)); 233 } 234 } 235 } 236 return devices; 237} 238 239sp<DeviceDescriptor> DeviceVector::getDeviceFromName(const String8& name) const 240{ 241 sp<DeviceDescriptor> device; 242 for (size_t i = 0; i < size(); i++) { 243 if (itemAt(i)->mName == name) { 244 device = itemAt(i); 245 break; 246 } 247 } 248 return device; 249} 250 251 252status_t DeviceVector::dump(int fd, const String8 &direction) const 253{ 254 const size_t SIZE = 256; 255 char buffer[SIZE]; 256 257 snprintf(buffer, SIZE, "\n Available %s devices:\n", direction.string()); 258 write(fd, buffer, strlen(buffer)); 259 for (size_t i = 0; i < size(); i++) { 260 itemAt(i)->dump(fd, 2, i); 261 } 262 return NO_ERROR; 263} 264 265audio_policy_dev_state_t DeviceVector::getDeviceConnectionState(const sp<DeviceDescriptor> &devDesc) const 266{ 267 ssize_t index = indexOf(devDesc); 268 return index >= 0 ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 269} 270 271void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig, 272 const struct audio_port_config *srcConfig) const 273{ 274 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; 275 if (srcConfig != NULL) { 276 dstConfig->config_mask |= srcConfig->config_mask; 277 } 278 279 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); 280 281 dstConfig->id = mId; 282 dstConfig->role = audio_is_output_device(mDeviceType) ? 283 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; 284 dstConfig->type = AUDIO_PORT_TYPE_DEVICE; 285 dstConfig->ext.device.type = mDeviceType; 286 287 //TODO Understand why this test is necessary. i.e. why at boot time does it crash 288 // without the test? 289 // This has been demonstrated to NOT be true (at start up) 290 // ALOG_ASSERT(mModule != NULL); 291 dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE; 292 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 293} 294 295void DeviceDescriptor::toAudioPort(struct audio_port *port) const 296{ 297 ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); 298 AudioPort::toAudioPort(port); 299 port->id = mId; 300 toAudioPortConfig(&port->active_config); 301 port->ext.device.type = mDeviceType; 302 port->ext.device.hw_module = mModule->mHandle; 303 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 304} 305 306status_t DeviceDescriptor::dump(int fd, int spaces, int index) const 307{ 308 const size_t SIZE = 256; 309 char buffer[SIZE]; 310 String8 result; 311 312 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); 313 result.append(buffer); 314 if (mId != 0) { 315 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); 316 result.append(buffer); 317 } 318 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", 319 ConfigParsingUtils::enumToString(sDeviceTypeToEnumTable, 320 ARRAY_SIZE(sDeviceTypeToEnumTable), 321 mDeviceType)); 322 result.append(buffer); 323 if (mAddress.size() != 0) { 324 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); 325 result.append(buffer); 326 } 327 write(fd, result.string(), result.size()); 328 AudioPort::dump(fd, spaces); 329 330 return NO_ERROR; 331} 332 333void DeviceDescriptor::log() const 334{ 335 ALOGI("Device id:%d type:0x%X:%s, addr:%s", 336 mId, 337 mDeviceType, 338 ConfigParsingUtils::enumToString( 339 sDeviceNameToEnumTable, ARRAY_SIZE(sDeviceNameToEnumTable), mDeviceType), 340 mAddress.string()); 341 342 AudioPort::log(" "); 343} 344 345}; // namespace android 346