DeviceDescriptor.cpp revision 1d4481fb9cb1ea149f79f2a5d22110af84fbc90b
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 &direction) const 211{ 212 const size_t SIZE = 256; 213 char buffer[SIZE]; 214 215 snprintf(buffer, SIZE, "\n Available %s devices:\n", direction.string()); 216 write(fd, buffer, strlen(buffer)); 217 for (size_t i = 0; i < size(); i++) { 218 itemAt(i)->dump(fd, 2, i); 219 } 220 return NO_ERROR; 221} 222 223audio_policy_dev_state_t DeviceVector::getDeviceConnectionState(const sp<DeviceDescriptor> &devDesc) const 224{ 225 ssize_t index = indexOf(devDesc); 226 return index >= 0 ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 227} 228 229void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig, 230 const struct audio_port_config *srcConfig) const 231{ 232 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; 233 if (srcConfig != NULL) { 234 dstConfig->config_mask |= srcConfig->config_mask; 235 } 236 237 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); 238 239 dstConfig->id = mId; 240 dstConfig->role = audio_is_output_device(mDeviceType) ? 241 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; 242 dstConfig->type = AUDIO_PORT_TYPE_DEVICE; 243 dstConfig->ext.device.type = mDeviceType; 244 245 //TODO Understand why this test is necessary. i.e. why at boot time does it crash 246 // without the test? 247 // This has been demonstrated to NOT be true (at start up) 248 // ALOG_ASSERT(mModule != NULL); 249 dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE; 250 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 251} 252 253void DeviceDescriptor::toAudioPort(struct audio_port *port) const 254{ 255 ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); 256 AudioPort::toAudioPort(port); 257 port->id = mId; 258 toAudioPortConfig(&port->active_config); 259 port->ext.device.type = mDeviceType; 260 port->ext.device.hw_module = mModule->mHandle; 261 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); 262} 263 264void DeviceDescriptor::importAudioPort(const sp<AudioPort> port) { 265 AudioPort::importAudioPort(port); 266 mSamplingRate = port->pickSamplingRate(); 267 mFormat = port->pickFormat(); 268 mChannelMask = port->pickChannelMask(); 269} 270 271status_t DeviceDescriptor::dump(int fd, int spaces, int index) const 272{ 273 const size_t SIZE = 256; 274 char buffer[SIZE]; 275 String8 result; 276 277 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); 278 result.append(buffer); 279 if (mId != 0) { 280 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); 281 result.append(buffer); 282 } 283 std::string deviceLiteral; 284 if (DeviceConverter::toString(mDeviceType, deviceLiteral)) { 285 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", deviceLiteral.c_str()); 286 result.append(buffer); 287 } 288 if (mAddress.size() != 0) { 289 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); 290 result.append(buffer); 291 } 292 write(fd, result.string(), result.size()); 293 AudioPort::dump(fd, spaces); 294 295 return NO_ERROR; 296} 297 298void DeviceDescriptor::log() const 299{ 300 std::string device; 301 DeviceConverter::toString(mDeviceType, device); 302 ALOGI("Device id:%d type:0x%X:%s, addr:%s", mId, mDeviceType, device.c_str(), 303 mAddress.string()); 304 305 AudioPort::log(" "); 306} 307 308}; // namespace android 309