HwModule.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::HwModule" 18//#define LOG_NDEBUG 0 19 20#include "HwModule.h" 21#include "IOProfile.h" 22#include "AudioGain.h" 23#include "ConfigParsingUtils.h" 24#include "audio_policy_conf.h" 25#include <hardware/audio.h> 26#include <policy.h> 27 28namespace android { 29 30HwModule::HwModule(const char *name) 31 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), 32 mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0) 33{ 34} 35 36HwModule::~HwModule() 37{ 38 for (size_t i = 0; i < mOutputProfiles.size(); i++) { 39 mOutputProfiles[i]->mSupportedDevices.clear(); 40 } 41 for (size_t i = 0; i < mInputProfiles.size(); i++) { 42 mInputProfiles[i]->mSupportedDevices.clear(); 43 } 44 free((void *)mName); 45} 46 47status_t HwModule::loadInput(cnode *root) 48{ 49 cnode *node = root->first_child; 50 51 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK); 52 53 while (node) { 54 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { 55 profile->loadSamplingRates((char *)node->value); 56 } else if (strcmp(node->name, FORMATS_TAG) == 0) { 57 profile->loadFormats((char *)node->value); 58 } else if (strcmp(node->name, CHANNELS_TAG) == 0) { 59 profile->loadInChannels((char *)node->value); 60 } else if (strcmp(node->name, DEVICES_TAG) == 0) { 61 profile->mSupportedDevices.loadDevicesFromName((char *)node->value, 62 mDeclaredDevices); 63 } else if (strcmp(node->name, FLAGS_TAG) == 0) { 64 profile->mFlags = ConfigParsingUtils::parseInputFlagNames((char *)node->value); 65 } else if (strcmp(node->name, GAINS_TAG) == 0) { 66 profile->loadGains(node); 67 } 68 node = node->next; 69 } 70 ALOGW_IF(profile->mSupportedDevices.isEmpty(), 71 "loadInput() invalid supported devices"); 72 ALOGW_IF(profile->mChannelMasks.size() == 0, 73 "loadInput() invalid supported channel masks"); 74 ALOGW_IF(profile->mSamplingRates.size() == 0, 75 "loadInput() invalid supported sampling rates"); 76 ALOGW_IF(profile->mFormats.size() == 0, 77 "loadInput() invalid supported formats"); 78 if (!profile->mSupportedDevices.isEmpty() && 79 (profile->mChannelMasks.size() != 0) && 80 (profile->mSamplingRates.size() != 0) && 81 (profile->mFormats.size() != 0)) { 82 83 ALOGV("loadInput() adding input Supported Devices %04x", 84 profile->mSupportedDevices.types()); 85 86 profile->attach(this); 87 mInputProfiles.add(profile); 88 return NO_ERROR; 89 } else { 90 return BAD_VALUE; 91 } 92} 93 94status_t HwModule::loadOutput(cnode *root) 95{ 96 cnode *node = root->first_child; 97 98 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE); 99 100 while (node) { 101 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { 102 profile->loadSamplingRates((char *)node->value); 103 } else if (strcmp(node->name, FORMATS_TAG) == 0) { 104 profile->loadFormats((char *)node->value); 105 } else if (strcmp(node->name, CHANNELS_TAG) == 0) { 106 profile->loadOutChannels((char *)node->value); 107 } else if (strcmp(node->name, DEVICES_TAG) == 0) { 108 profile->mSupportedDevices.loadDevicesFromName((char *)node->value, 109 mDeclaredDevices); 110 } else if (strcmp(node->name, FLAGS_TAG) == 0) { 111 profile->mFlags = ConfigParsingUtils::parseOutputFlagNames((char *)node->value); 112 } else if (strcmp(node->name, GAINS_TAG) == 0) { 113 profile->loadGains(node); 114 } 115 node = node->next; 116 } 117 ALOGW_IF(profile->mSupportedDevices.isEmpty(), 118 "loadOutput() invalid supported devices"); 119 ALOGW_IF(profile->mChannelMasks.size() == 0, 120 "loadOutput() invalid supported channel masks"); 121 ALOGW_IF(profile->mSamplingRates.size() == 0, 122 "loadOutput() invalid supported sampling rates"); 123 ALOGW_IF(profile->mFormats.size() == 0, 124 "loadOutput() invalid supported formats"); 125 if (!profile->mSupportedDevices.isEmpty() && 126 (profile->mChannelMasks.size() != 0) && 127 (profile->mSamplingRates.size() != 0) && 128 (profile->mFormats.size() != 0)) { 129 130 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x", 131 profile->mSupportedDevices.types(), profile->mFlags); 132 profile->attach(this); 133 mOutputProfiles.add(profile); 134 return NO_ERROR; 135 } else { 136 return BAD_VALUE; 137 } 138} 139 140status_t HwModule::loadDevice(cnode *root) 141{ 142 cnode *node = root->first_child; 143 144 audio_devices_t type = AUDIO_DEVICE_NONE; 145 while (node) { 146 if (strcmp(node->name, APM_DEVICE_TYPE) == 0) { 147 type = ConfigParsingUtils::parseDeviceNames((char *)node->value); 148 break; 149 } 150 node = node->next; 151 } 152 if (type == AUDIO_DEVICE_NONE || 153 (!audio_is_input_device(type) && !audio_is_output_device(type))) { 154 ALOGW("loadDevice() bad type %08x", type); 155 return BAD_VALUE; 156 } 157 sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type); 158 159 node = root->first_child; 160 while (node) { 161 if (strcmp(node->name, APM_DEVICE_ADDRESS) == 0) { 162 deviceDesc->mAddress = String8((char *)node->value); 163 } else if (strcmp(node->name, CHANNELS_TAG) == 0) { 164 if (audio_is_input_device(type)) { 165 deviceDesc->loadInChannels((char *)node->value); 166 } else { 167 deviceDesc->loadOutChannels((char *)node->value); 168 } 169 } else if (strcmp(node->name, GAINS_TAG) == 0) { 170 deviceDesc->loadGains(node); 171 } 172 node = node->next; 173 } 174 175 ALOGV("loadDevice() adding device name %s type %08x address %s", 176 deviceDesc->mName.string(), type, deviceDesc->mAddress.string()); 177 178 mDeclaredDevices.add(deviceDesc); 179 180 return NO_ERROR; 181} 182 183status_t HwModule::addOutputProfile(String8 name, const audio_config_t *config, 184 audio_devices_t device, String8 address) 185{ 186 sp<IOProfile> profile = new IOProfile(name, AUDIO_PORT_ROLE_SOURCE); 187 188 profile->mSamplingRates.add(config->sample_rate); 189 profile->mChannelMasks.add(config->channel_mask); 190 profile->mFormats.add(config->format); 191 192 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(name, device); 193 devDesc->mAddress = address; 194 profile->mSupportedDevices.add(devDesc); 195 196 profile->attach(this); 197 mOutputProfiles.add(profile); 198 199 return NO_ERROR; 200} 201 202status_t HwModule::removeOutputProfile(String8 name) 203{ 204 for (size_t i = 0; i < mOutputProfiles.size(); i++) { 205 if (mOutputProfiles[i]->mName == name) { 206 mOutputProfiles.removeAt(i); 207 break; 208 } 209 } 210 211 return NO_ERROR; 212} 213 214status_t HwModule::addInputProfile(String8 name, const audio_config_t *config, 215 audio_devices_t device, String8 address) 216{ 217 sp<IOProfile> profile = new IOProfile(name, AUDIO_PORT_ROLE_SINK); 218 219 profile->mSamplingRates.add(config->sample_rate); 220 profile->mChannelMasks.add(config->channel_mask); 221 profile->mFormats.add(config->format); 222 223 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(name, device); 224 devDesc->mAddress = address; 225 profile->mSupportedDevices.add(devDesc); 226 227 ALOGV("addInputProfile() name %s rate %d mask 0x08", name.string(), config->sample_rate, config->channel_mask); 228 229 profile->attach(this); 230 mInputProfiles.add(profile); 231 232 return NO_ERROR; 233} 234 235status_t HwModule::removeInputProfile(String8 name) 236{ 237 for (size_t i = 0; i < mInputProfiles.size(); i++) { 238 if (mInputProfiles[i]->mName == name) { 239 mInputProfiles.removeAt(i); 240 break; 241 } 242 } 243 244 return NO_ERROR; 245} 246 247 248void HwModule::dump(int fd) 249{ 250 const size_t SIZE = 256; 251 char buffer[SIZE]; 252 String8 result; 253 254 snprintf(buffer, SIZE, " - name: %s\n", mName); 255 result.append(buffer); 256 snprintf(buffer, SIZE, " - handle: %d\n", mHandle); 257 result.append(buffer); 258 snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF); 259 result.append(buffer); 260 write(fd, result.string(), result.size()); 261 if (mOutputProfiles.size()) { 262 write(fd, " - outputs:\n", strlen(" - outputs:\n")); 263 for (size_t i = 0; i < mOutputProfiles.size(); i++) { 264 snprintf(buffer, SIZE, " output %zu:\n", i); 265 write(fd, buffer, strlen(buffer)); 266 mOutputProfiles[i]->dump(fd); 267 } 268 } 269 if (mInputProfiles.size()) { 270 write(fd, " - inputs:\n", strlen(" - inputs:\n")); 271 for (size_t i = 0; i < mInputProfiles.size(); i++) { 272 snprintf(buffer, SIZE, " input %zu:\n", i); 273 write(fd, buffer, strlen(buffer)); 274 mInputProfiles[i]->dump(fd); 275 } 276 } 277 if (mDeclaredDevices.size()) { 278 write(fd, " - devices:\n", strlen(" - devices:\n")); 279 for (size_t i = 0; i < mDeclaredDevices.size(); i++) { 280 mDeclaredDevices[i]->dump(fd, 4, i); 281 } 282 } 283} 284 285sp <HwModule> HwModuleCollection::getModuleFromName(const char *name) const 286{ 287 sp <HwModule> module; 288 289 for (size_t i = 0; i < size(); i++) 290 { 291 if (strcmp(itemAt(i)->mName, name) == 0) { 292 return itemAt(i); 293 } 294 } 295 return module; 296} 297 298 299sp <HwModule> HwModuleCollection::getModuleForDevice(audio_devices_t device) const 300{ 301 sp <HwModule> module; 302 303 for (size_t i = 0; i < size(); i++) { 304 if (itemAt(i)->mHandle == 0) { 305 continue; 306 } 307 if (audio_is_output_device(device)) { 308 for (size_t j = 0; j < itemAt(i)->mOutputProfiles.size(); j++) 309 { 310 if (itemAt(i)->mOutputProfiles[j]->mSupportedDevices.types() & device) { 311 return itemAt(i); 312 } 313 } 314 } else { 315 for (size_t j = 0; j < itemAt(i)->mInputProfiles.size(); j++) { 316 if (itemAt(i)->mInputProfiles[j]->mSupportedDevices.types() & 317 device & ~AUDIO_DEVICE_BIT_IN) { 318 return itemAt(i); 319 } 320 } 321 } 322 } 323 return module; 324} 325 326sp<DeviceDescriptor> HwModuleCollection::getDeviceDescriptor(const audio_devices_t device, 327 const char *device_address, 328 const char *device_name) const 329{ 330 String8 address = (device_address == NULL) ? String8("") : String8(device_address); 331 // handle legacy remote submix case where the address was not always specified 332 if (device_distinguishes_on_address(device) && (address.length() == 0)) { 333 address = String8("0"); 334 } 335 336 for (size_t i = 0; i < size(); i++) { 337 const sp<HwModule> hwModule = itemAt(i); 338 if (hwModule->mHandle == 0) { 339 continue; 340 } 341 DeviceVector deviceList = 342 hwModule->mDeclaredDevices.getDevicesFromTypeAddr(device, address); 343 if (!deviceList.isEmpty()) { 344 return deviceList.itemAt(0); 345 } 346 deviceList = hwModule->mDeclaredDevices.getDevicesFromType(device); 347 if (!deviceList.isEmpty()) { 348 return deviceList.itemAt(0); 349 } 350 } 351 352 sp<DeviceDescriptor> devDesc = 353 new DeviceDescriptor(String8(device_name != NULL ? device_name : ""), device); 354 devDesc->mAddress = address; 355 return devDesc; 356} 357 358status_t HwModuleCollection::dump(int fd) const 359{ 360 const size_t SIZE = 256; 361 char buffer[SIZE]; 362 363 snprintf(buffer, SIZE, "\nHW Modules dump:\n"); 364 write(fd, buffer, strlen(buffer)); 365 for (size_t i = 0; i < size(); i++) { 366 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); 367 write(fd, buffer, strlen(buffer)); 368 itemAt(i)->dump(fd); 369 } 370 return NO_ERROR; 371} 372 373} //namespace android 374