Android10 车载音频架构之AudioFlinger

xref: /frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.cpp

4315  status_t AudioPolicyManager::initialize() {

4316      // Once policy config has been parsed, retrieve an instance of the engine and initialize it.

4317      audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();

4318      if (!engineInstance) {

4319          ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__);

4320          return NO_INIT;

4321      }

4322      // Retrieve the Policy Manager Interface

4323      mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();

4324      if (mEngine == NULL) {

4325          ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);

4326          return NO_INIT;

4327      }

4328      mEngine->setObserver(this);

4329      status_t status = mEngine->initCheck();

4330      if (status != NO_ERROR) {

4331          LOG_FATAL("Policy engine not initialized(err=%d)", status);

4332          return status;

4333      }

4334  

4335      // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices

4336      // open all output streams needed to access attached devices

4337      for (const auto& hwModule : mHwModulesAll) {

4338          hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName()));

4339          if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) {

4340              ALOGW("could not open HW module %s", hwModule->getName());

4341              continue;

4342          }

4343          mHwModules.push_back(hwModule);

4344          // open all output streams needed to access attached devices

4345          // except for direct output streams that are only opened when they are actually

4346          // required by an app.

4347          // This also validates mAvailableOutputDevices list

4348          for (const auto& outProfile : hwModule->getOutputProfiles()) {

4349              if (!outProfile->canOpenNewIo()) {

4350                  ALOGE("Invalid Output profile max open count %u for profile %s",

4351                        outProfile->maxOpenCount, outProfile->getTagName().c_str());

4352                  continue;

4353              }

4354              if (!outProfile->hasSupportedDevices()) {

4355                  ALOGW("Output profile contains no device on module %s", hwModule->getName());

4356                  continue;

4357              }

4358              if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) {

4359                  mTtsOutputAvailable = true;

4360              }

4361  

4362              if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {

4363                  continue;

4364              }

4365              const DeviceVector &supportedDevices = outProfile->getSupportedDevices();

4366              DeviceVector availProfileDevices = supportedDevices.filter(mAvailableOutputDevices);

4367              sp<DeviceDescriptor> supportedDevice = 0;

4368              if (supportedDevices.contains(mDefaultOutputDevice)) {

4369                  supportedDevice = mDefaultOutputDevice;

4370              } else {

4371                  // choose first device present in profile's SupportedDevices also part of

4372                  // mAvailableOutputDevices.

4373                  if (availProfileDevices.isEmpty()) {

4374                      continue;

4375                  }

4376                  supportedDevice = availProfileDevices.itemAt(0);

4377              }

4378              if (!mAvailableOutputDevices.contains(supportedDevice)) {

4379                  continue;

4380              }

4381              sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile,

4382                                                                                   mpClientInterface);

4383              audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;

4384              status_t status = outputDesc->open(nullptr, DeviceVector(supportedDevice),

4385                                                 AUDIO_STREAM_DEFAULT,

4386                                                 AUDIO_OUTPUT_FLAG_NONE, &output);

4387              if (status != NO_ERROR) {

4388                  ALOGW("Cannot open output stream for devices %s on hw module %s",

4389                        supportedDevice->toString().c_str(), hwModule->getName());

4390                  continue;

4391              }

4392              for (const auto &device : availProfileDevices) {

4393                  // give a valid ID to an attached device once confirmed it is reachable

4394                  if (!device->isAttached()) {

4395                      device->attach(hwModule);

4396                  }

4397              }

4398              if (mPrimaryOutput == 0 &&

4399                      outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {

4400                  mPrimaryOutput = outputDesc;

4401              }

4402              addOutput(output, outputDesc);

4403              setOutputDevices(outputDesc,

4404                               DeviceVector(supportedDevice),

4405                               true,

4406                               0,

4407                               NULL);

4408          }

4409          // open input streams needed to access attached devices to validate

4410          // mAvailableInputDevices list

4411          for (const auto& inProfile : hwModule->getInputProfiles()) {

4412              if (!inProfile->canOpenNewIo()) {

4413                  ALOGE("Invalid Input profile max open count %u for profile %s",

4414                        inProfile->maxOpenCount, inProfile->getTagName().c_str());

4415                  continue;

4416              }

4417              if (!inProfile->hasSupportedDevices()) {

4418                  ALOGW("Input profile contains no device on module %s", hwModule->getName());

4419                  continue;

4420              }

4421              // chose first device present in profile's SupportedDevices also part of

4422              // available input devices

4423              const DeviceVector &supportedDevices = inProfile->getSupportedDevices();

4424              DeviceVector availProfileDevices = supportedDevices.filter(mAvailableInputDevices);

4425              if (availProfileDevices.isEmpty()) {

4426                  ALOGE("%s: Input device list is empty!", __FUNCTION__);

4427                  continue;

4428              }

4429              sp<AudioInputDescriptor> inputDesc =

4430                      new AudioInputDescriptor(inProfile, mpClientInterface);

4431  

4432              audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;

4433              status_t status = inputDesc->open(nullptr,

4434                                                availProfileDevices.itemAt(0),

4435                                                AUDIO_SOURCE_MIC,

4436                                                AUDIO_INPUT_FLAG_NONE,

4437                                                &input);

4438              if (status != NO_ERROR) {

4439                  ALOGW("Cannot open input stream for device %s on hw module %s",

4440                        availProfileDevices.toString().c_str(),

4441                        hwModule->getName());

4442                  continue;

4443              }

4444              for (const auto &device : availProfileDevices) {

4445                  // give a valid ID to an attached device once confirmed it is reachable

4446                  if (!device->isAttached()) {

4447                      device->attach(hwModule);

4448                      device->importAudioPort(inProfile, true);

4449                  }

4450              }

4451              inputDesc->close();

4452          }

4453      }

4454      // make sure all attached devices have been allocated a unique ID

4455      auto checkAndSetAvailable = [this](auto& devices) {

4456          for (size_t i = 0; i < devices.size();) {

4457              const auto &device = devices[i];

4458              if (!device->isAttached()) {

4459                  ALOGW("device %s is unreachable", device->toString().c_str());

4460                  devices.remove(device);

4461                  continue;

4462              }

4463              // Device is now validated and can be appended to the available devices of the engine

4464              setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE);

4465              i++;

4466          }

4467      };

4468      checkAndSetAvailable(mAvailableOutputDevices);

4469      checkAndSetAvailable(mAvailableInputDevices);

4470  

4471      // make sure default device is reachable

4472      if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) {

4473          ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable",

4474                   mDefaultOutputDevice->toString().c_str());

4475          status = NO_INIT;

4476      }

4477      // If microphones address is empty, set it according to device type

4478      for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {

4479          if (mAvailableInputDevices[i]->address().isEmpty()) {

4480              if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) {

4481                  mAvailableInputDevices[i]->setAddress(String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS));

4482              } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) {

4483                  mAvailableInputDevices[i]->setAddress(String8(AUDIO_BACK_MICROPHONE_ADDRESS));

4484              }

4485          }

4486      }

4487  

4488      if (mPrimaryOutput == 0) {

4489          ALOGE("Failed to open primary output");

4490          status = NO_INIT;

4491      }

4492  

4493      // Silence ALOGV statements

4494      property_set("log.tag." LOG_TAG, "D");

4495  

4496      updateDevicesAndOutputs();

4497      return status;

4498  }

xref: /frameworks/av/services/audioflinger/AudioFlinger.cpp

1994  audio_module_handle_t AudioFlinger::loadHwModule(const char *name)

1995  {

1996      if (name == NULL) {

1997          return AUDIO_MODULE_HANDLE_NONE;

1998      }

1999      if (!settingsAllowed()) {

2000          return AUDIO_MODULE_HANDLE_NONE;

2001      }

2002      Mutex::Autolock _l(mLock);

2003      return loadHwModule_l(name);

2004  }

2005  

2006  // loadHwModule_l() must be called with AudioFlinger::mLock held

2007  audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)

2008  {

2009      for (size_t i = 0; i < mAudioHwDevs.size(); i++) {

2010          if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {

2011              ALOGW("loadHwModule() module %s already loaded", name);

2012              return mAudioHwDevs.keyAt(i);

2013          }

2014      }

2015  

2016      sp<DeviceHalInterface> dev;

2017  

2018      int rc = mDevicesFactoryHal->openDevice(name, &dev);

2019      if (rc) {

2020          ALOGE("loadHwModule() error %d loading module %s", rc, name);

2021          return AUDIO_MODULE_HANDLE_NONE;

2022      }

2023  

2024      mHardwareStatus = AUDIO_HW_INIT;

2025      rc = dev->initCheck();

2026      mHardwareStatus = AUDIO_HW_IDLE;

2027      if (rc) {

2028          ALOGE("loadHwModule() init check error %d for module %s", rc, name);

2029          return AUDIO_MODULE_HANDLE_NONE;

2030      }

2031  

2032      // Check and cache this HAL's level of support for master mute and master

2033      // volume.  If this is the first HAL opened, and it supports the get

2034      // methods, use the initial values provided by the HAL as the current

2035      // master mute and volume settings.

2036  

2037      AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);

2038      {  // scope for auto-lock pattern

2039          AutoMutex lock(mHardwareLock);

2040  

2041          if (0 == mAudioHwDevs.size()) {

2042              mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;

2043              float mv;

2044              if (OK == dev->getMasterVolume(&mv)) {

2045                  mMasterVolume = mv;

2046              }

2047  

2048              mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;

2049              bool mm;

2050              if (OK == dev->getMasterMute(&mm)) {

2051                  mMasterMute = mm;

2052              }

2053          }

2054  

2055          mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;

2056          if (OK == dev->setMasterVolume(mMasterVolume)) {

2057              flags = static_cast<AudioHwDevice::Flags>(flags |

2058                      AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);

2059          }

2060  

2061          mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;

2062          if (OK == dev->setMasterMute(mMasterMute)) {

2063              flags = static_cast<AudioHwDevice::Flags>(flags |

2064                      AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);

2065          }

2066  

2067          mHardwareStatus = AUDIO_HW_IDLE;

2068      }

2069      if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_MSD) == 0) {

2070          // An MSD module is inserted before hardware modules in order to mix encoded streams.

2071          flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT);

2072      }

2073  

2074      audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);

2075      mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));

2076  

2077      ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle);

2078  

2079      return handle;

2080  

2081  }

2082  

1.加载厂家提供的so文件

通过mpClientInterface->loadHwModule调用AudioFlinger里面的loadHwModule加载厂家提供的so文件

2.重要的对应关系

       a. AudioFlinger中的AudioHwDevice是对audio_hw_device的封装。

            b. audio_hw_hal.cpp中的audio_hw_device结构体的实现要通过厂家提供的AudioHardware

   3.使用底层的audio_hw_device结构体,构建上层的AudioHwDevice对象

int rc = mDevicesFactoryHal->openDevice(name, &dev);

mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));

xref: /frameworks/av/services/audioflinger/AudioFlinger.cpp

2276  sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module,

2277                                                              audio_io_handle_t *output,

2278                                                              audio_config_t *config,

2279                                                              audio_devices_t devices,

2280                                                              const String8& address,

2281                                                              audio_output_flags_t flags)

2282  {

2283      AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);

2284      if (outHwDev == NULL) {

2285          return 0;

2286      }

2287  

2288      if (*output == AUDIO_IO_HANDLE_NONE) {

2289          *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);

2290      } else {

2291          // Audio Policy does not currently request a specific output handle.

2292          // If this is ever needed, see openInput_l() for example code.

2293          ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);

2294          return 0;

2295      }

2296  

2297      mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;

2298  

2299      // FOR TESTING ONLY:

2300      // This if statement allows overriding the audio policy settings

2301      // and forcing a specific format or channel mask to the HAL/Sink device for testing.

2302      if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {

2303          // Check only for Normal Mixing mode

2304          if (kEnableExtendedPrecision) {

2305              // Specify format (uncomment one below to choose)

2306              //config->format = AUDIO_FORMAT_PCM_FLOAT;

2307              //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;

2308              //config->format = AUDIO_FORMAT_PCM_32_BIT;

2309              //config->format = AUDIO_FORMAT_PCM_8_24_BIT;

2310              // ALOGV("openOutput_l() upgrading format to %#08x", config->format);

2311          }

2312          if (kEnableExtendedChannels) {

2313              // Specify channel mask (uncomment one below to choose)

2314              //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch

2315              //config->channel_mask = audio_channel_mask_from_representation_and_bits(

2316              //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example

2317          }

2318      }

2319  

2320      AudioStreamOut *outputStream = NULL;

2321      status_t status = outHwDev->openOutputStream(

2322              &outputStream,

2323              *output,

2324              devices,

2325              flags,

2326              config,

2327              address.string());

2328  

2329      mHardwareStatus = AUDIO_HW_IDLE;

2330  

2331      if (status == NO_ERROR) {

2332          if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {

2333              sp<MmapPlaybackThread> thread =

2334                      new MmapPlaybackThread(this, *output, outHwDev, outputStream,

2335                                            devices, AUDIO_DEVICE_NONE, mSystemReady);

2336              mMmapThreads.add(*output, thread);

2337              ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p",

2338                    *output, thread.get());

2339              return thread;

2340          } else {

2341              sp<PlaybackThread> thread;

2342              if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {

2343                  thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);

2344                  ALOGV("openOutput_l() created offload output: ID %d thread %p",

2345                        *output, thread.get());

2346              } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)

2347                      || !isValidPcmSinkFormat(config->format)

2348                      || !isValidPcmSinkChannelMask(config->channel_mask)) {

2349                  thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);

2350                  ALOGV("openOutput_l() created direct output: ID %d thread %p",

2351                        *output, thread.get());

2352              } else {

2353                  thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);

2354                  ALOGV("openOutput_l() created mixer output: ID %d thread %p",

2355                        *output, thread.get());

2356              }

2357              mPlaybackThreads.add(*output, thread);

2358              mPatchPanel.notifyStreamOpened(outHwDev, *output);

2359              return thread;

2360          }

2361      }

2362  

2363      return 0;

2364  }

根据flag创建线程

openOutputStream之后会根据flag创建线程,一般创建MixerThread线程,flag来自于Audio_policy_configuration.xml文件。MixerThread表示声音来源于多个应用程序,会把声音混合在一起播放,最后将线程添加到mPlaybackThreads数组里面。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值