DeviceDescriptor.cpp revision 98cc191247388132b6fd8a4ecd07abd6e4c5a0ed
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 NULL), 34 mDeviceType(type), mAddress("") 35{ 36 37} 38 39bool DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const 40{ 41 // Devices are considered equal if they: 42 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) 43 // - have the same address or one device does not specify the address 44 // - have the same channel mask or one device does not specify the channel mask 45 return (mDeviceType == other->mDeviceType) && 46 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && 47 (mChannelMask == 0 || other->mChannelMask == 0 || 48 mChannelMask == other->mChannelMask); 49} 50 51void DeviceDescriptor::loadGains(cnode *root) 52{ 53 AudioPort::loadGains(root); 54 if (mGains.size() > 0) { 55 mGains[0]->getDefaultConfig(&mGain); 56 } 57} 58 59void DeviceVector::refreshTypes() 60{ 61 mDeviceTypes = AUDIO_DEVICE_NONE; 62 for(size_t i = 0; i < size(); i++) { 63 mDeviceTypes |= itemAt(i)->mDeviceType; 64 } 65 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); 66} 67 68ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const 69{ 70 for(size_t i = 0; i < size(); i++) { 71 if (item->equals(itemAt(i))) { 72 return i; 73 } 74 } 75 return -1; 76} 77 78ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item) 79{ 80 ssize_t ret = indexOf(item); 81 82 if (ret < 0) { 83 ret = SortedVector::add(item); 84 if (ret >= 0) { 85 refreshTypes(); 86 } 87 } else { 88 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType); 89 ret = -1; 90 } 91 return ret; 92} 93 94ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item) 95{ 96 size_t i; 97 ssize_t ret = indexOf(item); 98 99 if (ret < 0) { 100 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType); 101 } else { 102 ret = SortedVector::removeAt(ret); 103 if (ret >= 0) { 104 refreshTypes(); 105 } 106 } 107 return ret; 108} 109 110void DeviceVector::loadDevicesFromType(audio_devices_t types) 111{ 112 DeviceVector deviceList; 113 114 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; 115 types &= ~role_bit; 116 117 while (types) { 118 uint32_t i = 31 - __builtin_clz(types); 119 uint32_t type = 1 << i; 120 types &= ~type; 121 add(new DeviceDescriptor(String8("device_type"), type | role_bit)); 122 } 123} 124 125void DeviceVector::loadDevicesFromName(char *name, 126 const DeviceVector& declaredDevices) 127{ 128 char *devName = strtok(name, "|"); 129 while (devName != NULL) { 130 if (strlen(devName) != 0) { 131 audio_devices_t type = ConfigParsingUtils::stringToEnum(sDeviceNameToEnumTable, 132 ARRAY_SIZE(sDeviceNameToEnumTable), 133 devName); 134 if (type != AUDIO_DEVICE_NONE) { 135 sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(name), type); 136 if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX || 137 type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) { 138 dev->mAddress = String8("0"); 139 } 140 add(dev); 141 } else { 142 sp<DeviceDescriptor> deviceDesc = 143 declaredDevices.getDeviceFromName(String8(devName)); 144 if (deviceDesc != 0) { 145 add(deviceDesc); 146 } 147 } 148 } 149 devName = strtok(NULL, "|"); 150 } 151} 152 153sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, String8 address) const 154{ 155 sp<DeviceDescriptor> device; 156 for (size_t i = 0; i < size(); i++) { 157 if (itemAt(i)->mDeviceType == type) { 158 if (address == "" || itemAt(i)->mAddress == address) { 159 device = itemAt(i); 160 if (itemAt(i)->mAddress == address) { 161 break; 162 } 163 } 164 } 165 } 166 ALOGV("DeviceVector::getDevice() for type %08x address %s found %p", 167 type, address.string(), device.get()); 168 return device; 169} 170 171sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const 172{ 173 sp<DeviceDescriptor> device; 174 for (size_t i = 0; i < size(); i++) { 175 if (itemAt(i)->getHandle() == id) { 176 device = itemAt(i); 177 break; 178 } 179 } 180 return device; 181} 182 183DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const 184{ 185 DeviceVector devices; 186 bool isOutput = audio_is_output_devices(type); 187 type &= ~AUDIO_DEVICE_BIT_IN; 188 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { 189 bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType); 190 audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN; 191 if ((isOutput == curIsOutput) && ((type & curType) != 0)) { 192 devices.add(itemAt(i)); 193 type &= ~curType; 194 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", 195 itemAt(i)->mDeviceType, itemAt(i).get()); 196 } 197 } 198 return devices; 199} 200 201DeviceVector DeviceVector::getDevicesFromTypeAddr( 202 audio_devices_t type, String8 address) const 203{ 204 DeviceVector devices; 205 for (size_t i = 0; i < size(); i++) { 206 if (itemAt(i)->mDeviceType == type) { 207 if (itemAt(i)->mAddress == address) { 208 devices.add(itemAt(i)); 209 } 210 } 211 } 212 return devices; 213} 214 215sp<DeviceDescriptor> DeviceVector::getDeviceFromName(const String8& name) const 216{ 217 sp<DeviceDescriptor> device; 218 for (size_t i = 0; i < size(); i++) { 219 if (itemAt(i)->mName == name) { 220 device = itemAt(i); 221 break; 222 } 223 } 224 return device; 225} 226 227void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig, 228 const struct audio_port_config *srcConfig) const 229{ 230 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; 231 if (srcConfig != NULL) { 232 dstConfig->config_mask |= srcConfig->config_mask; 233 } 234 235 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); 236 237 dstConfig->id = mId; 238 dstConfig->role = audio_is_output_device(mDeviceType) ? 239 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; 240 dstConfig->type = AUDIO_PORT_TYPE_DEVICE; 241 dstConfig->ext.device.type = mDeviceType; 242 243 //TODO Understand why this test is necessary. i.e. why at boot time does it crash 244 // without the test? 245 // This has been demonstrated to NOT be true (at start up) 246 // ALOG_ASSERT(mModule != NULL); 247 dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE; 248 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 249} 250 251void DeviceDescriptor::toAudioPort(struct audio_port *port) const 252{ 253 ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); 254 AudioPort::toAudioPort(port); 255 port->id = mId; 256 toAudioPortConfig(&port->active_config); 257 port->ext.device.type = mDeviceType; 258 port->ext.device.hw_module = mModule->mHandle; 259 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 260} 261 262status_t DeviceDescriptor::dump(int fd, int spaces, int index) const 263{ 264 const size_t SIZE = 256; 265 char buffer[SIZE]; 266 String8 result; 267 268 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); 269 result.append(buffer); 270 if (mId != 0) { 271 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); 272 result.append(buffer); 273 } 274 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", 275 ConfigParsingUtils::enumToString(sDeviceNameToEnumTable, 276 ARRAY_SIZE(sDeviceNameToEnumTable), 277 mDeviceType)); 278 result.append(buffer); 279 if (mAddress.size() != 0) { 280 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); 281 result.append(buffer); 282 } 283 write(fd, result.string(), result.size()); 284 AudioPort::dump(fd, spaces); 285 286 return NO_ERROR; 287} 288 289}; // namespace android 290