Android 音频子系统(2) ---- AudioServer

打印 上一主题 下一主题

主题 804|帖子 804|积分 2412






前言

本系列文章基于Android14源码进行分析解读,部分框图直接使用了原作者的图片,侵权必删。
我们既生成文章,也是各路文章的搬运工。

一、What&Why AudioFlinger

        AudioHAL(AHAL)提供隔离Vendor硬件差异的抽象,通过tinyalsa实现对底层硬件声卡节点的利用。AHAL作为一个独立的service,在Android系统中肯定有client通过binder与其通信,实现数据流与控制流的交互,完成对声卡节点数据的访问。
        AudioTrack.java和AudioRecord.java是应用层的API,透过GNI访问了Native层的AudioTrack.cpp和AudioRecord.cpp,AudioTrack和AudioRecord又是如何把数据写进和读出底层声卡的呢?
        Linux声卡设备节点是一个互斥的节点,每次只能被一个应用打开,其他应用尝试open一个已经打开的节点会返回device busy的错误。但是在实际Android使用过程中又存在多个音频数据流同时播放的场景,这是如何做到的呢?
        所有这些疑问都会在AudioFlinger服务中解决。AudioFlinger向下访问AudioHardware,实现输出音频数据,控制音频参数;AudioFlinger向上通过IAudioFinger接口对AudioTrack和AudioRecord供服务。AudioFlinger在Android的音频系统框架中起着承上启下的作用,地位相当告急。
   AudioFlinger创建PlaybackThread/RecordThread服务线程,实现对AHAL层音频节点的独占利用。多个AudioTrack/AudioRecord作为Clinet端同PlaybackThread/RecordThread服务线程交互,Thread实现音频流的mix/snoop,来实现整个音频框架。
  下面通过分析代码,完成对AudioFlinger的深入明白。
二、AudioFlinger初始化

AudioFlinger在audioserver中启动,代码在:
frameworks/av/media/audioserver/main_audioserver.cpp
  1. #define LOG_TAG "audioserver"
  2. int main(int argc __unused, char **argv)
  3. {
  4.      ......
  5.     这里修改了AudioFling和AudioPolicyService的初始化方式,以解决独立初始化模式下引起的
  6.     服务启动时间差,这个时间差会导致部分服务时间检测异常而强行退出。
  7.     // Instantiating AudioFlinger (making it public, e.g. through ::initialize())
  8.     // and then instantiating AudioPolicy (and making it public)
  9.     // leads to situations where AudioFlinger is accessed remotely before
  10.     // AudioPolicy is initialized.  Not only might this
  11.     // cause inaccurate results, but if AudioPolicy has slow audio HAL
  12.     // initialization, it can cause a TimeCheck abort to occur on an AudioFlinger
  13.     // call which tries to access AudioPolicy.
  14.     //
  15.     // We create AudioFlinger and AudioPolicy locally then make it public to ServiceManager.
  16.     // This requires both AudioFlinger and AudioPolicy to be in-proc.
  17.     //
  18.     /* 1、创建AudioFling实例,必须在AudioPolicyService之前,因为aps会访问af中的接口 */
  19.     const auto af = sp<AudioFlinger>::make();
  20.     const auto afAdapter = sp<AudioFlingerServerAdapter>::make(af);
  21.    
  22.     /* 2、创建AudioPolicyService实例 */
  23.     const auto aps = sp<AudioPolicyService>::make();
  24.     /* 3、添加AudioFlinger和AudioPolicyService服务到ServiceManager */   
  25.     // Add AudioFlinger and AudioPolicy to ServiceManager.
  26.     sp<IServiceManager> sm = defaultServiceManager();
  27.     sm->addService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME), afAdapter,
  28.             false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
  29.     sm->addService(String16(AudioPolicyService::getServiceName()), aps,
  30.             false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
  31.    
  32.     // AAudioService should only be used in OC-MR1 and later.
  33.     // And only enable the AAudioService if the system MMAP policy explicitly allows it.
  34.     std::vector<AudioMMapPolicyInfo> policyInfos;
  35.     status_t status = af->getMmapPolicyInfos(
  36.             AudioMMapPolicyType::DEFAULT, &policyInfos);
  37.     // Initialize aaudio service when querying mmap policy succeeds and
  38.     // any of the policy supports MMAP.
  39.     if (status == NO_ERROR &&
  40.         std::any_of(policyInfos.begin(), policyInfos.end(), [](const auto& info) {
  41.                 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
  42.                        info.mmapPolicy == AudioMMapPolicy::ALWAYS;
  43.         })) {
  44.         
  45.         /* 4、创建AAudioService实例 */
  46.         AAudioService::instantiate();
  47.     }
  48.     ......
  49.     IPCThreadState::self()->joinThreadPool();
  50.     }
  51. }
复制代码
        主函数里面没有做太多的任务,创建了AudioFlinger和AudioPolicyService两个实例,并将服务注册到servicemanager中,除此之外还检测并创建了AAudioService。

1、AudioFlinger实例构建

  1. AudioFlinger::AudioFlinger() {
  2.     ......
  3.     // reset battery stats.
  4.     // if the audio service has crashed, battery stats could be left
  5.     // in bad state, reset the state upon service start.
  6.     BatteryNotifier::getInstance().noteResetAudio();
  7.     /* 构建音频device和音效effect实例 */
  8.     mDevicesFactoryHal = DevicesFactoryHalInterface::create();
  9.     mEffectsFactoryHal = EffectsFactoryHalInterface::create();
  10. }
复制代码
AudioFlinger的实例没有做太多的工作:

  • 完成电池状态的复位;
  • 构建音频设备Device工厂实例;
  • 构建音效Effect工厂实例;
  1. sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() {
  2.       return createPreferredImpl<DevicesFactoryHalInterface>(true /* isDevice */);
  3. }
  4. void *createPreferredImpl(bool isDevice) {
  5.     ......
  6.     if (...) {
  7.        .....
  8.         /*  1、创建AudioHal服务  */
  9.         if (createHalService(std::max(*ifaceVersionIt, *siblingVersionIt), isDevice,
  10.                              &rawInterface)) {
  11.             return rawInterface;
  12.         }
  13.     return nullptr;
  14. }
  15. bool createHalService(const AudioHalVersionInfo& version, bool isDevice, void** rawInterface) {
  16.     const std::string libName = "libaudiohal@" + version.toVersionString() + ".so";
  17.     const std::string factoryFunctionName =
  18.             isDevice ? "createIDevicesFactory" : "createIEffectsFactory";
  19.     constexpr int dlMode = RTLD_LAZY;
  20.     void* handle = nullptr;
  21.     dlerror(); // clear
  22.     /*  2、打开 libaudiohal@version.so 动态库 */
  23.     handle = dlopen(libName.c_str(), dlMode);
  24.     ......
  25.     void* (*factoryFunction)();
  26.     /*3、函数进来后首先初始化factoryFunctionName为createIDevicesFactory, 所以这里获取到的factoryFunction句柄为createIDevicesFactory函数 */
  27.     *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str());
  28.     if (!factoryFunction) {
  29.        ......
  30.     }
  31.     /* 4、调用libaudiohal动态库的createIDevicesFactory函数,完成AHAL句柄的创建 */
  32.     *rawInterface = (*factoryFunction)();
  33.     return true;
  34. }
  35. extern "C" __attribute__((visibility("default"))) void* createIDevicesFactory() {
  36.     return createIDevicesFactoryImpl();
  37. }
  38. extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
  39.     auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
  40.     return service ? new DevicesFactoryHalHidl(service) : nullptr;
  41. }
复制代码
  1. DevicesFactoryHalEntry.cpp::createIDevicesFactory() ->
  2.     DevicesFactoryHalHidl.cpp::createIDevicesFactoryImpl() ->         
  3.          DevicesFactoryHalHidl.cpp::DevicesFactoryHalHidl()
复制代码
具体逻辑这里不做深入的分析了,后面有机会再展开。
        整个AudioFlinger完成初始化之后,并没有做太多的任务。而是作为一个服务运行在系统中,等候客户端与其进行通信:这也符合AF的功能,我只做一个执行者,不做决策。那谁优势决策者呢?别忘了我们还没有分析AudioPolicyService呢。
2、AudioPolicyService实例构建

  1. AudioPolicyService::AudioPolicyService()
  2.     : BnAudioPolicyService(),
  3.       mAudioPolicyManager(NULL),
  4.       mAudioPolicyClient(NULL),
  5.       mPhoneState(AUDIO_MODE_INVALID),
  6.       mCaptureStateNotifier(false),
  7.      /* 1 - 初始化mCreateAudioPolicyManager为createAudioPolicyManager */
  8.       mCreateAudioPolicyManager(createAudioPolicyManager),
  9.       mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
  10.       setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
  11. }
  12. /* 在完成实例后,首先调用的函数就是onFirstRef,具体原因请搜索其他文章 */
  13. void AudioPolicyService::onFirstRef()
  14. {
  15.      {
  16.         Mutex::Autolock _l(mLock);
  17.         /* 2 - 创建两个命令线程,用于接收并处理其他服务发送的命令 */
  18.         // start audio commands thread
  19.         mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
  20.         // start output activity command thread
  21.         mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
  22.         
  23.         /* 3 - 构造AudioPolicyClient实例:APC是AF的clinet端,很多任务都是通过跨进程调用到AF中 */
  24.         mAudioPolicyClient = new AudioPolicyClient(this);
  25.         /* 4 - 加载用户自定义的的AudioPolicyManager实现:libaudiopolicymanagercustom.so */
  26.         loadAudioPolicyManager();
  27.         /* 5 - 调用createAudioPolicyManager接口 */
  28.         mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
  29.     }
复制代码
2.1、createAudioPolicyManager

  1. static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
  2. {
  3.     AudioPolicyManager *apm = nullptr;
  4.     media::AudioPolicyConfig apmConfig;
  5.     /* 1、与AHAL交互获取modules信息,以确定是否存在用户自定义的configurable AudioPolicyConfig */
  6.     if (status_t status = clientInterface->getAudioPolicyConfig(&apmConfig); status == OK) {
  7.         auto config = AudioPolicyConfig::loadFromApmAidlConfigWithFallback(apmConfig);
  8.         LOG_ALWAYS_FATAL_IF(config->getEngineLibraryNameSuffix() !=
  9.                 AudioPolicyConfig::kDefaultEngineLibraryNameSuffix,
  10.                 "Only default engine is currently supported with the AIDL HAL");
  11.         apm = new AudioPolicyManager(config,
  12.                 loadApmEngineLibraryAndCreateEngine(
  13.                         config->getEngineLibraryNameSuffix(), apmConfig.engineConfig),
  14.                 clientInterface);
  15.     } else {
  16.         /* 2、加载系统预定义的audio_policy_configuration配置 */
  17.         auto config = AudioPolicyConfig::loadFromApmXmlConfigWithFallback();
  18.          /* 3、config->getEngineLibraryNameSuffix()返回的
  19.              字符串为“default”(kDefaultEngineLibraryNameSuffix),
  20.              加载libaudiopolicyengine.default.so并完成AudioPolicyManager实例构造 */
  21.         apm = new AudioPolicyManager(config,
  22.                 loadApmEngineLibraryAndCreateEngine(config->getEngineLibraryNameSuffix()),
  23.                 clientInterface);
  24.     }
  25.     /* 4、初始化AudioPolicyManager */
  26.     status_t status = apm->initialize();
  27.     if (status != NO_ERROR) {
  28.         delete apm;
  29.         apm = nullptr;
  30.     }
  31.     return apm;
  32. }
  33. // static
  34. sp<const AudioPolicyConfig> AudioPolicyConfig::loadFromApmXmlConfigWithFallback(
  35.         const std::string& xmlFilePath) {
  36.     /* 2.1 - xmlFilePath为空,audio_get_audio_policy_config_file获取到的路径为/vendor/etc/audio_policy_configuration.xml */
  37.     const std::string filePath =
  38.             xmlFilePath.empty() ? audio_get_audio_policy_config_file() : xmlFilePath;
  39.     auto config = sp<AudioPolicyConfig>::make();
  40.     /* 2.2 - 解析audio_policy_configuration.xml 文件并获得音频策略配置信息 */
  41.     if (status_t status = config->loadFromXml(filePath, false /*forVts*/); status == NO_ERROR) {
  42.         return config;
  43.     }
  44.      /* 2.3 - 不会运行到这里 */
  45.     return createDefault();
  46. }
  47. status_t AudioPolicyConfig::loadFromXml(const std::string& xmlFilePath, bool forVts) {
  48.     ...
  49.     status_t status = deserializeAudioPolicyFile(xmlFilePath.c_str(), this);
  50.     ...
  51. }
  52. status_t deserializeAudioPolicyFile(const char *fileName, AudioPolicyConfig *config)
  53. {
  54.     PolicySerializer serializer;
  55.     return status = serializer.deserialize(fileName, config);
  56. }
  57. status_t PolicySerializer::deserialize(const char *configFile, AudioPolicyConfig *config,
  58.                                        bool ignoreVendorExtensions)
  59. {
  60.     /* 完成audio_policy_configuration.xml的解析*/
  61.     ......
  62. }
复制代码
以Android的car/emulator的audio_policy_configuration.xml文件为例,下图是xml文件剖析后得到的结果,整个剖析按照mixPort,devicePort和route的属性剖析并终极形成HwModule,条理比力简单,读者自行撸代码也能看懂。
 

得益于Android丰富的工具,可以通过 dumpsys media.audio_policy 命令查看终极配置的路由计谋。
  1. dumpsys media.audio_policy
  2. Supported System Usages:
  3.   AUDIO_USAGE_CALL_ASSISTANT, AUDIO_USAGE_EMERGENCY, AUDIO_USAGE_SAFETY, AUDIO_USAGE_VEHICLE_STATUS, AUDIO_USAGE_ANNOUNCEMENT
  4. UID Policy:
  5.         mObserverRegistered=True
  6.         Assistants UIDs:
  7.                 No UIDs present.
  8.         Active Assistants UIDs:
  9.                 No UIDs present.
  10.         Accessibility UIDs:
  11.                 No UIDs present.
  12.         Input Method Service UID=1010067
  13.         Is RTT Enabled: False
  14. AudioCommandThread: 0xb400007416624090
  15. - Commands:
  16.    Command Time        Wait pParam
  17.   Last Command
  18.    05      092141.687  0    0xb40000739666f2e0
  19. OutputCommandThread: 0xb400007416625950
  20. - Commands:
  21.    Command Time        Wait pParam
  22.   Last Command
  23.    09      092141.966  0    0xb400007306620340
  24. AudioPolicyManager Dump: 0xb4000074466246b0
  25. Primary Output I/O handle: 13
  26. Phone state: AUDIO_MODE_NORMAL
  27. Force use for communications: 0
  28. Force use for media: 10
  29. Force use for record: 0
  30. Force use for dock: 9
  31. Force use for system: 0
  32. Force use for HDMI system audio: 0
  33. Force use for encoded surround output: 0
  34. Force use for vibrate ringing: 0
  35. TTS output not available
  36. Master mono: off
  37. Communication Strategy id: 14
  38. Config source: /vendor/etc/audio_policy_configuration.xml
  39. Available output devices (17):
  40.   1. Port ID: 50; "bus1000_mirror_device"; {AUDIO_DEVICE_OUT_BUS, @:bus1000_mirror_device}
  41.      Encapsulation modes: 0, metadata types: 0
  42.    - Profiles (1):
  43.       1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  44.          sampling rates: 48000
  45.          channel masks: 0x0003
  46.          AUDIO_ENCAPSULATION_TYPE_NONE
  47.      - gains:
  48.        Gain 1:
  49.        - mode: 00000001
  50.        - channel_mask: 00000000
  51.        - min_value: -3200 mB
  52.        - max_value: 600 mB
  53.        - default_value: 0 mB
  54.        - step_value: 100 mB
  55.        - min_ramp_ms: 0 ms
  56.        - max_ramp_ms: 0 ms
  57.   2. Port ID: 47; "bus211_audio_zone_2"; {AUDIO_DEVICE_OUT_BUS, @:bus211_audio_zone_2}
  58.      Encapsulation modes: 0, metadata types: 0
  59.    - Profiles (1):
  60.       1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  61.          sampling rates: 48000
  62.          channel masks: 0x0003
  63.          AUDIO_ENCAPSULATION_TYPE_NONE
  64.      - gains:
  65.        Gain 1:
  66.        - mode: 00000001
  67.        - channel_mask: 00000000
  68.        - min_value: -3200 mB
  69.        - max_value: 600 mB
  70.        - default_value: 0 mB
  71.        - step_value: 100 mB
  72.        - min_ramp_ms: 0 ms
  73.        - max_ramp_ms: 0 ms
  74.   ...
  75.   6. Port ID: 35; "bus111_audio_zone_1"; {AUDIO_DEVICE_OUT_BUS, @:bus111_audio_zone_1}
  76.      Encapsulation modes: 0, metadata types: 0
  77.    - Profiles (1):
  78.       1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  79.          sampling rates: 48000
  80.          channel masks: 0x0003
  81.          AUDIO_ENCAPSULATION_TYPE_NONE
  82.      - gains:
  83.        Gain 1:
  84.        - mode: 00000001
  85.        - channel_mask: 00000000
  86.        - min_value: -3200 mB
  87.        - max_value: 600 mB
  88.        - default_value: 0 mB
  89.        - step_value: 100 mB
  90.        - min_ramp_ms: 0 ms
  91.        - max_ramp_ms: 0 ms
  92.    ...
  93.   10. Port ID: 23; "bus7_system_sound_out"; {AUDIO_DEVICE_OUT_BUS, @:bus7_system_sound_out}
  94.       Encapsulation modes: 0, metadata types: 0
  95.     - Profiles (1):
  96.        1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  97.           sampling rates: 48000
  98.           channel masks: 0x0003
  99.           AUDIO_ENCAPSULATION_TYPE_NONE
  100.       - gains:
  101.         Gain 1:
  102.         - mode: 00000001
  103.         - channel_mask: 00000000
  104.         - min_value: -3200 mB
  105.         - max_value: 600 mB
  106.         - default_value: 0 mB
  107.         - step_value: 100 mB
  108.         - min_ramp_ms: 0 ms
  109.         - max_ramp_ms: 0 ms
  110.   17. Port ID: 2; "bus0_media_out"; {AUDIO_DEVICE_OUT_BUS, @:bus0_media_out}
  111.       Encapsulation modes: 0, metadata types: 0
  112.     - Profiles (1):
  113.        1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  114.           sampling rates: 48000
  115.           channel masks: 0x0003
  116.           AUDIO_ENCAPSULATION_TYPE_NONE
  117.       - gains:
  118.         Gain 1:
  119.         - mode: 00000001
  120.         - channel_mask: 00000000
  121.         - min_value: -3200 mB
  122.         - max_value: 600 mB
  123.         - default_value: 0 mB
  124.         - step_value: 100 mB
  125.         - min_ramp_ms: 0 ms
  126.         - max_ramp_ms: 0 ms
  127. Available input devices (7):
  128.   1. Port ID: 53; "Built-In Mic"; {AUDIO_DEVICE_IN_BUILTIN_MIC, @:Built-In Mic}
  129.      Encapsulation modes: 0, metadata types: 0
  130.    - Profiles (1):
  131.       1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  132.          sampling rates: 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  133.          channel masks: 0x000c, 0x0010, 0x0030
  134.          AUDIO_ENCAPSULATION_TYPE_NONE
  135.   2. Port ID: 54; "Built-In Back Mic"; {AUDIO_DEVICE_IN_BACK_MIC, @:Built-In Back Mic}
  136.      Encapsulation modes: 0, metadata types: 0
  137.    - Profiles (1):
  138.       1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  139.          sampling rates: 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  140.          channel masks: 0x000c, 0x0010, 0x0030
  141.          AUDIO_ENCAPSULATION_TYPE_NONE
  142.      ...
  143. Hardware modules (3):
  144.   1. Handle: 10; "primary"
  145.    - Output MixPorts (17):
  146.       1. "mixport_bus0_media_out"; 0x0002 (AUDIO_OUTPUT_FLAG_PRIMARY)
  147.        - Profiles (1):
  148.           1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  149.              sampling rates: 48000
  150.              channel masks: 0x0003
  151.              AUDIO_ENCAPSULATION_TYPE_NONE
  152.        - Supported devices (1):
  153.         1. Port ID: 2; "bus0_media_out"; {AUDIO_DEVICE_OUT_BUS, @:bus0_media_out}
  154.            Encapsulation modes: 0, metadata types: 0
  155.        - maxOpenCount: 1; curOpenCount: 1
  156.        - maxActiveCount: 1; curActiveCount: 0
  157.        - recommendedMuteDurationMs: 0 ms
  158.       2. "mixport_bus1_navigation_out"; 0x0000 (AUDIO_OUTPUT_FLAG_NONE)
  159.        - Profiles (1):
  160.           1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  161.              sampling rates: 48000
  162.              channel masks: 0x0003
  163.              AUDIO_ENCAPSULATION_TYPE_NONE
  164.        - Supported devices (1):
  165.         1. Port ID: 5; "bus1_navigation_out"; {AUDIO_DEVICE_OUT_BUS, @:bus1_navigation_out}
  166.            Encapsulation modes: 0, metadata types: 0
  167.        - maxOpenCount: 1; curOpenCount: 1
  168.        - maxActiveCount: 1; curActiveCount: 0
  169.        - recommendedMuteDurationMs: 0 ms
  170.       ...
  171.    - Input MixPorts (4):
  172.       1. "primary input"; 0x0000 (AUDIO_INPUT_FLAG_NONE)
  173.        - Profiles (1):
  174.           1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  175.              sampling rates: 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  176.              channel masks: 0x000c, 0x0010, 0x0030
  177.              AUDIO_ENCAPSULATION_TYPE_NONE
  178.        - Supported devices (3):
  179.         1. Port ID: 53; "Built-In Mic"; {AUDIO_DEVICE_IN_BUILTIN_MIC, @:Built-In Mic}
  180.            Encapsulation modes: 0, metadata types: 0
  181.         2. Port ID: 54; "Built-In Back Mic"; {AUDIO_DEVICE_IN_BACK_MIC, @:Built-In Back Mic}
  182.            Encapsulation modes: 0, metadata types: 0
  183.         3. Port ID: 55; "Echo-Reference Mic"; {AUDIO_DEVICE_IN_ECHO_REFERENCE, @:Echo-Reference Mic}
  184.            Encapsulation modes: 0, metadata types: 0
  185.        - maxOpenCount: 1; curOpenCount: 0
  186.        - maxActiveCount: 1; curActiveCount: 0
  187.        - recommendedMuteDurationMs: 0 ms
  188.       ...
  189.    - Declared devices (23):
  190.     1. Port ID: 50; "bus1000_mirror_device"; {AUDIO_DEVICE_OUT_BUS, @:bus1000_mirror_device}
  191.        Encapsulation modes: 0, metadata types: 0
  192.      - Profiles (1):
  193.         1. ""; AUDIO_FORMAT_PCM_16_BIT (0x1)
  194.            sampling rates: 48000
  195.            channel masks: 0x0003
  196.            AUDIO_ENCAPSULATION_TYPE_NONE
  197.        - gains:
  198.          Gain 1:
  199.          - mode: 00000001
  200.          - channel_mask: 00000000
  201.          - min_value: -3200 mB
  202.          - max_value: 600 mB
  203.          - default_value: 0 mB
  204.          - step_value: 100 mB
  205.          - min_ramp_ms: 0 ms
  206.          - max_ramp_ms: 0 ms
  207.     ...
  208.    - Audio Routes (21):
  209.       1. Mix; Sink: "bus0_media_out"
  210.          Sources: "mixport_bus0_media_out"
  211.       2. Mix; Sink: "bus1_navigation_out"
  212.          Sources: "mixport_bus1_navigation_out"
  213.       3. Mix; Sink: "bus2_voice_command_out"
  214.          Sources: "mixport_bus2_voice_command_out"
  215.      ...
  216.   
复制代码
2.2 AudioPolicyManager实例构建 

  1. static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
  2. {
  3.     ...
  4.         apm = new AudioPolicyManager(config,
  5.                 loadApmEngineLibraryAndCreateEngine(config->getEngineLibraryNameSuffix()),
  6.                 clientInterface);
  7. }
复制代码
2.2.1  loadApmEngineLibraryAndCreateEngine 

在2.1章的第3步中config->getEngineLibraryNameSuffix()返回的字符串为“default”(kDefaultEngineLibraryNameSuffix),以是loadApmEngineLibraryAndCreateEngine传入的参数为("default”,null)。
  1. EngineInstance loadApmEngineLibraryAndCreateEngine(const std::string& librarySuffix,
  2.         const std::string& configXmlFilePath)
  3. {
  4.     /* 1 - 加载libaudiopolicyengine.default.so,获取createEngineInstance句柄并赋值给mCreateEngineInstance */
  5.     auto engLib = EngineLibrary::load(librarySuffix);
  6.     if (!engLib) {
  7.         ALOGE("%s: Failed to load the engine library, suffix "%s"",
  8.                 __func__, librarySuffix.c_str());
  9.         return nullptr;
  10.     }
  11.     /* 2 - 创建ape,这里的configXmlFilePath为空 */
  12.     auto engine = engLib->createEngineUsingXmlConfig(configXmlFilePath);
  13.     if (engine == nullptr) {
  14.         ALOGE("%s: Failed to instantiate the APM engine", __func__);
  15.         return nullptr;
  16.     }
  17.     return engine;
  18. }
  19. EngineInstance EngineLibrary::createEngineUsingXmlConfig(const std::string& xmlFilePath)
  20. {
  21.     auto instance = createEngine();
  22.     if (instance != nullptr) {
  23.         /* 2.1 - xmlFilePath为空,加载engine 配置文件*/
  24.         if (status_t status = instance->loadFromXmlConfigWithFallback(xmlFilePath);
  25.                 status == OK) {
  26.             return instance;
  27.         } else {
  28.             ALOGE("%s: loading of the engine config with XML configuration file "%s" failed: %d",
  29.                     __func__, xmlFilePath.empty() ? "default" : xmlFilePath.c_str(), status);
  30.         }
  31.     }
  32.     return nullptr;
  33. }
  34. status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
  35.     return loadWithFallback(xmlFilePath);
  36. }
  37. template<typename T>
  38. status_t Engine::loadWithFallback(const T& configSource) {
  39.     /* 2.1.1 - 加载engineConfig::DEFAULT_PATH("/vendor/etc/audio_policy_engine_configuration.xml") 下的ape配置文件 */
  40.     auto result = EngineBase::loadAudioPolicyEngineConfig(configSource);
  41.     auto legacyStrategy = getLegacyStrategy();
  42.     for (const auto &strategy : legacyStrategy) {
  43.         mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
  44.     }
  45.     return OK;
  46. }
复制代码
        APE的调用逻辑比力繁琐,但是原理很清楚:起首确定是否存在用户自定义的configurable engine,如果不存在则使用default engine;确认之后渐渐调用并剖析相干的APE配置文件。譬如车机的APE配置如下:/frameworks/av/services/audiopolicy/engineconfigurable/config/example/automotive/audio_policy_engine_configuration.xml
  1. <configuration version="1.0" xmlns:xi="http://www.w3.org/2001/XInclude">
  2.     <xi:include href="audio_policy_engine_product_strategies.xml"/>
  3.     <xi:include href="audio_policy_engine_criterion_types.xml"/>
  4.     <xi:include href="audio_policy_engine_criteria.xml"/>
  5.     <xi:include href="audio_policy_engine_volumes.xml"/>
  6. </configuration>
复制代码
具体内容在后续使用的过程中分析。
2.3、AudioPolicyManager::initialize

        回到2.1的第4步,也是最后一步:AudioPolicyManager的initialize。
  1. status_t AudioPolicyManager::initialize() {
  2.     /* 1 - 这里的mEngine就是上面分析的APE,肯定非空了 */
  3.     if (mEngine == nullptr) {
  4.         return NO_INIT;
  5.     }
  6.     /* 2 - 将APM设置为Engine的观察器(mApmObserver=APM),
  7.            允许Engine检索Devices、Streams、HwModules的信息:
  8.            Engine通过APM获取/设置相关信息 */
  9.     mEngine->setObserver(this);
  10.    
  11.     /* 3 - 检测Engine是否被正确初始化:mApmObserver!=null, 这不是废话吗?
  12.            前面刚设置完成*/
  13.     status_t status = mEngine->initCheck();
  14.     if (status != NO_ERROR) {
  15.         LOG_FATAL("Policy engine not initialized(err=%d)", status);
  16.         return status;
  17.     }
  18.     // The actual device selection cache will be updated when calling `updateDevicesAndOutputs`
  19.     // at the end of this function.
  20.     /* 4 - 使用产品策略初始化设备,这些设备的策略会在音频模块初始化后更新。*/
  21.     mEngine->initializeDeviceSelectionCache();
  22.     mCommunnicationStrategy = mEngine->getProductStrategyForAttributes(
  23.         mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL));
  24.      /* 5 - 有音频模块可用(实际上是首次初始化),完成模块的初始化。*/
  25.     // after parsing the config, mConfig contain all known devices;
  26.     // open all output streams needed to access attached devices
  27.     onNewAudioModulesAvailableInt(nullptr /*newDevices*/);
  28.     // make sure default device is reachable
  29.     if (const auto defaultOutputDevice = mConfig->getDefaultOutputDevice();
  30.             defaultOutputDevice == nullptr ||
  31.             !mAvailableOutputDevices.contains(defaultOutputDevice)) {
  32.         ALOGE_IF(defaultOutputDevice != nullptr, "Default device %s is unreachable",
  33.                  defaultOutputDevice->toString().c_str());
  34.         status = NO_INIT;
  35.     }
  36.       
  37.     /* 6 - 第5步中完成了整个音频模块的初始化,跟新相关信息到Cache缓存中。
  38.        在音频系统稳定后,为了加快处理速度,引擎会缓存基于AudioAttribute/Streams的设备选择。当一个设备连接时,Android的模式会发生变化,引擎会收到通知并更新缓存。如果某个输出上音频流的启动/停止可能影响通知时,引擎需要通过updateDevicesAndOutputs函数更新缓存。*/
  39.     updateDevicesAndOutputs();
  40.     return status;
  41. }
复制代码
2.3.1、AudioPolicyManager:nNewAudioModulesAvailableInt

        经过2.2章节的分析,我们已经清楚系统已经完成xml配置文件的剖析并形成HwModule的多维数据,该函数就是根据先前分析的数据完成音频系统的最告急的一步:调用AF->AHAL完成设备节
点的open,并在AF中创建数据流播放/录制线程,为后面AudioTrack做好预备
  1. void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices)
  2. {
  3.     ALOGD("AudioAnt: onNewAudioModulesAvailableInt enter ......");
  4.     /* 1. 遍历audio_policy_configuration.xml中配置的所有模块 */
  5.     for (const auto& hwModule : mConfig->getHwModules()) {
  6.         if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) {
  7.             continue;
  8.         }
  9.         if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) {
  10.             ALOGD("AudioAnt: loadHwModule with module name %s", hwModule->getName());
  11.             /* 2. 加载相关module,获取module的句柄handle */
  12.             if (audio_module_handle_t handle = mpClientInterface->loadHwModule(hwModule->getName());
  13.                     handle != AUDIO_MODULE_HANDLE_NONE) {
  14.                 hwModule->setHandle(handle);
  15.             } else {
  16.                 ALOGW("could not load HW module %s", hwModule->getName());
  17.                 continue;
  18.             }
  19.         }
  20.         /* 3. 如果xml中配置的module存在,则记录到mHwModules容器中以便后面访问使用 */
  21.         mHwModules.push_back(hwModule);
  22.         /* 4. 遍历当前模块支持的profiles */
  23.         for (const auto& outProfile : hwModule->getOutputProfiles()) {
  24.             ALOGD("AudioAnt: outProfile with name: %s", outProfile->getTagName().c_str());
  25.             
  26.             /* 5. 获取当前profile支持的设备 */
  27.             const DeviceVector &supportedDevices = outProfile->getSupportedDevices();
  28.             for(auto& device: supportedDevices) {
  29.                 ALOGD("AudioAnt: supportedDevices with name: %s", device->getTagName().c_str());
  30.             }
  31.             DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getOutputDevices());
  32.             sp<DeviceDescriptor> supportedDevice = 0;
  33.             if (supportedDevices.contains(mConfig->getDefaultOutputDevice())) {
  34.                 supportedDevice = mConfig->getDefaultOutputDevice();
  35.             } else {
  36.                 // choose first device present in profile's SupportedDevices also part of
  37.                 // mAvailableOutputDevices.
  38.                 if (availProfileDevices.isEmpty()) {
  39.                     continue;
  40.                 }
  41.                 supportedDevice = availProfileDevices.itemAt(0);
  42.             }
  43.             if (!mConfig->getOutputDevices().contains(supportedDevice)) {
  44.                 continue;
  45.             }
  46.             sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, mpClientInterface);
  47.             audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
  48.             
  49.             /* 6. 打开设备,涉及AHAL的open操作,并在AF中创建相关播放/录制线程 */
  50.             status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */,
  51.                                                DeviceVector(supportedDevice),
  52.                                                AUDIO_STREAM_DEFAULT,
  53.                                                AUDIO_OUTPUT_FLAG_NONE, &output);
  54.             if (status != NO_ERROR) {
  55.                 ALOGW("Cannot open output stream for devices %s on hw module %s",
  56.                       supportedDevice->toString().c_str(), hwModule->getName());
  57.                 continue;
  58.             }
  59.             for (const auto &device : availProfileDevices) {
  60.                 ALOGD("AudioAnt: availProfileDevices with name: %s", device->getTagName().c_str());
  61.                 // give a valid ID to an attached device once confirmed it is reachable
  62.                 if (!device->isAttached()) {
  63.                     device->attach(hwModule);
  64.                     mAvailableOutputDevices.add(device);
  65.                     device->setEncapsulationInfoFromHal(mpClientInterface);
  66.                     if (newDevices) newDevices->add(device);
  67.                     setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE);
  68.                 }
  69.             }
  70.             if (mPrimaryOutput == nullptr &&
  71.                     outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) {
  72.                 mPrimaryOutput = outputDesc;
  73.             }
  74.             if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
  75.                 outputDesc->close();
  76.             } else {
  77.                 addOutput(output, outputDesc);
  78.                 setOutputDevices(outputDesc,
  79.                                  DeviceVector(supportedDevice),
  80.                                  true,
  81.                                  0,
  82.                                  NULL);
  83.             }
  84.         }
  85.         
  86.         /* 7. 同输出设置一样的操作逻辑,不赘述 */
  87.         // open input streams needed to access attached devices to validate
  88.         // mAvailableInputDevices list
  89.         for (const auto& inProfile : hwModule->getInputProfiles()) {
  90.         }
  91.     }
  92.     /* 8. 如果有声场相关输出,在使用之前先关闭它 */
  93.     // Check if spatializer outputs can be closed until used.
  94.     // mOutputs vector never contains duplicated outputs at this point.
  95.     std::vector<audio_io_handle_t> outputsClosed;
  96.     for (size_t i = 0; i < mOutputs.size(); i++) {
  97.         sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i);
  98.         if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0
  99.                 && !isOutputOnlyAvailableRouteToSomeDevice(desc)) {
  100.             outputsClosed.push_back(desc->mIoHandle);
  101.             desc->close();
  102.         }
  103.     }
  104.     for (auto output : outputsClosed) {
  105.         removeOutput(output);
  106.     }
  107. }
复制代码
        终极的利用输出如下:
  1. 06-06 21:51:06.077 16876 16876 D APM_AudioPolicyManager: AudioAnt: onNewAudioModulesAvailableInt enter ......
  2. 06-06 21:51:06.077 16876 16876 D APM_AudioPolicyManager: AudioAnt: loadHwModule with module name primary
  3. 06-06 21:51:09.092 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus0_media_out
  4. 06-06 21:51:09.092 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus0_media_out
  5. 06-06 21:51:09.096 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus0_media_out, SamplingRate
  6. 48000, Format 0x000001, Channels 0x3, flags 0x2
  7. 06-06 21:51:09.266 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus0_media_out
  8. 06-06 21:51:09.316 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus1_navigation_out
  9. 06-06 21:51:09.316 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus1_navigation_out
  10. 06-06 21:51:09.316 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus1_navigation_out, Samplin
  11. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  12. 06-06 21:51:09.327 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus1_navigation_out
  13. 06-06 21:51:09.354 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus2_voice_command_out
  14. 06-06 21:51:09.354 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus2_voice_command_out
  15. 06-06 21:51:09.354 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus2_voice_command_out, Samp
  16. lingRate 48000, Format 0x000001, Channels 0x3, flags 0
  17. 06-06 21:51:09.365 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus2_voice_command_out
  18. 06-06 21:51:09.391 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus3_call_ring_out
  19. 06-06 21:51:09.392 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus3_call_ring_out
  20. 06-06 21:51:09.392 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus3_call_ring_out, Sampling
  21. Rate 48000, Format 0x000001, Channels 0x3, flags 0
  22. 06-06 21:51:09.403 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus3_call_ring_out
  23. 06-06 21:51:09.416 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus4_call_out
  24. 06-06 21:51:09.416 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus4_call_out
  25. 06-06 21:51:09.416 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus4_call_out, SamplingRate
  26. 48000, Format 0x000001, Channels 0x3, flags 0
  27. 06-06 21:51:09.423 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus4_call_out
  28. 06-06 21:51:09.441 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus5_alarm_out
  29. 06-06 21:51:09.442 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus5_alarm_out
  30. 06-06 21:51:09.442 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus5_alarm_out, SamplingRate
  31. 48000, Format 0x000001, Channels 0x3, flags 0
  32. 06-06 21:51:09.451 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus5_alarm_out
  33. 06-06 21:51:09.463 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus6_notification_out
  34. 06-06 21:51:09.463 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus6_notification_out
  35. 06-06 21:51:09.463 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus6_notification_out, Sampl
  36. ingRate 48000, Format 0x000001, Channels 0x3, flags 0
  37. 06-06 21:51:09.471 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus6_notification_out
  38. 06-06 21:51:09.491 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus7_system_sound_out
  39. 06-06 21:51:09.491 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus7_system_sound_out
  40. 06-06 21:51:09.491 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus7_system_sound_out, Sampl
  41. ingRate 48000, Format 0x000001, Channels 0x3, flags 0
  42. 06-06 21:51:09.499 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus7_system_sound_out
  43. 06-06 21:51:09.519 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus100_audio_zone_1
  44. 06-06 21:51:09.519 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus100_audio_zone_1
  45. 06-06 21:51:09.520 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus100_audio_zone_1, Samplin
  46. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  47. 06-06 21:51:09.530 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus100_audio_zone_1
  48. 06-06 21:51:09.557 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus101_audio_zone_1
  49. 06-06 21:51:09.558 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus101_audio_zone_1
  50. 06-06 21:51:09.558 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus101_audio_zone_1, Samplin
  51. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  52. 06-06 21:51:09.569 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus101_audio_zone_1
  53. 06-06 21:51:09.598 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus110_audio_zone_1
  54. 06-06 21:51:09.598 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus110_audio_zone_1
  55. 06-06 21:51:09.598 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus110_audio_zone_1, Samplin
  56. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  57. 06-06 21:51:09.604 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus110_audio_zone_1
  58. 06-06 21:51:09.630 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus111_audio_zone_1
  59. 06-06 21:51:09.630 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus111_audio_zone_1
  60. 06-06 21:51:09.631 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus111_audio_zone_1, Samplin
  61. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  62. 06-06 21:51:09.642 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus111_audio_zone_1
  63. 06-06 21:51:09.678 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus200_audio_zone_2
  64. 06-06 21:51:09.679 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus200_audio_zone_2
  65. 06-06 21:51:09.679 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus200_audio_zone_2, Samplin
  66. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  67. 06-06 21:51:09.690 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus200_audio_zone_2
  68. 06-06 21:51:09.713 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus201_audio_zone_2
  69. 06-06 21:51:09.713 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus201_audio_zone_2
  70. 06-06 21:51:09.713 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus201_audio_zone_2, Samplin
  71. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  72. 06-06 21:51:09.720 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus201_audio_zone_2
  73. 06-06 21:51:09.742 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus210_audio_zone_2
  74. 06-06 21:51:09.742 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus210_audio_zone_2
  75. 06-06 21:51:09.742 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus210_audio_zone_2, Samplin
  76. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  77. 06-06 21:51:09.747 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus210_audio_zone_2
  78. 06-06 21:51:09.773 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus211_audio_zone_2
  79. 06-06 21:51:09.773 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus211_audio_zone_2
  80. 06-06 21:51:09.773 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus211_audio_zone_2, Samplin
  81. gRate 48000, Format 0x000001, Channels 0x3, flags 0
  82. 06-06 21:51:09.779 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus211_audio_zone_2
  83. 06-06 21:51:09.810 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: mixport_bus1000_mirror_device
  84. 06-06 21:51:09.810 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: bus1000_mirror_device
  85. 06-06 21:51:09.810 16876 16876 I AudioFlinger: AudioAnt: openOutput() this 0xb40000738661f010, module 10 Device AUDIO_DEVICE_OUT_BUS, @:bus1000_mirror_device, Sampl
  86. ingRate 48000, Format 0x000001, Channels 0x3, flags 0
  87. 06-06 21:51:09.817 16876 16876 D APM_AudioPolicyManager: AudioAnt: availProfileDevices with name: bus1000_mirror_device
  88. 06-06 21:51:09.881 16876 16876 D APM_AudioPolicyManager: AudioAnt: loadHwModule with module name a2dp
  89. 06-06 21:51:09.882 16876 16876 D APM_AudioPolicyManager: AudioAnt: loadHwModule with module name usb
  90. 06-06 21:51:09.884 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: usb_accessory output
  91. 06-06 21:51:09.884 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: USB Host Out
  92. 06-06 21:51:09.884 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: usb_device output
  93. 06-06 21:51:09.884 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: USB Device Out
  94. 06-06 21:51:09.885 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: USB Headset Out
  95. 06-06 21:51:09.885 16876 16876 D APM_AudioPolicyManager: AudioAnt: loadHwModule with module name r_submix
  96. 06-06 21:51:09.888 16876 16876 D APM_AudioPolicyManager: AudioAnt: outProfile with name: r_submix output
  97. 06-06 21:51:09.888 16876 16876 D APM_AudioPolicyManager: AudioAnt: supportedDevices with name: Remote Submix Out
复制代码
        可以看到此时系统已经创建了多个AudioOut线程(主历程为audioservice),随后如果应用创建了AudioTrack,就可以根据strategy绑定到相干线程,触发或者加入到播放/录制线程。
  1. auto8678p1_64_bsp_vm:/ # ps -ef | grep audioserver
  2. audioserver  16876     1 0 21:51:04 ?     00:00:02 audioserver
  3. auto8678p1_64_bsp_vm:/ # ps -T -p 16876
  4. USER           PID   TID  PPID        VSZ    RSS WCHAN            ADDR S CMD
  5. audioserver  16876 16876     1   12552792  50996 binder_th+          0 S audioserver
  6. audioserver  16876 16884     1   12552792  50996 binder_th+          0 S binder:16876_1
  7. audioserver  16876 16885     1   12552792  50996 futex_wai+          0 S AudioFlinger_Pa
  8. audioserver  16876 16886     1   12552792  50996 binder_th+          0 S binder:16876_2
  9. audioserver  16876 16887     1   12552792  50996 binder_th+          0 S HwBinder:16876_
  10. audioserver  16876 16888     1   12552792  50996 binder_th+          0 S HwBinder:16876_
  11. audioserver  16876 16889     1   12552792  50996 binder_th+          0 S HwBinder:16876_
  12. audioserver  16876 16891     1   12552792  50996 futex_wai+          0 S AudioUtilThread
  13. audioserver  16876 16892     1   12552792  50996 futex_wai+          0 S ApmAudio
  14. audioserver  16876 16893     1   12552792  50996 futex_wai+          0 S ApmOutput
  15. audioserver  16876 16913     1   12552792  50996 futex_wai+          0 S TimerThread
  16. audioserver  16876 16914     1   12552792  50996 futex_wai+          0 S AudioOut_D
  17. audioserver  16876 16915     1   12552792  50996 futex_wai+          0 S AudioOut_15
  18. audioserver  16876 16916     1   12552792  50996 futex_wai+          0 S AudioOut_1D
  19. audioserver  16876 16917     1   12552792  50996 futex_wai+          0 S AudioOut_25
  20. audioserver  16876 16918     1   12552792  50996 futex_wai+          0 S AudioOut_2D
  21. audioserver  16876 16919     1   12552792  50996 futex_wai+          0 S AudioOut_35
  22. audioserver  16876 16920     1   12552792  50996 futex_wai+          0 S AudioOut_3D
  23. audioserver  16876 16921     1   12552792  50996 futex_wai+          0 S AudioOut_45
  24. audioserver  16876 16922     1   12552792  50996 futex_wai+          0 S AudioOut_4D
  25. audioserver  16876 16923     1   12552792  50996 futex_wai+          0 S AudioOut_55
  26. audioserver  16876 16924     1   12552792  50996 futex_wai+          0 S AudioOut_5D
  27. audioserver  16876 16925     1   12552792  50996 futex_wai+          0 S AudioOut_65
  28. audioserver  16876 16926     1   12552792  50996 futex_wai+          0 S AudioOut_6D
  29. audioserver  16876 16927     1   12552792  50996 futex_wai+          0 S AudioOut_75
  30. audioserver  16876 16928     1   12552792  50996 futex_wai+          0 S AudioOut_7D
  31. audioserver  16876 16929     1   12552792  50996 futex_wai+          0 S AudioOut_85
  32. audioserver  16876 16930     1   12552792  50996 futex_wai+          0 S AudioOut_8D
  33. audioserver  16876 16945     1   12552792  50996 binder_th+          0 S binder:16876_3
  34. audioserver  16876 16948     1   12552792  50996 binder_th+          0 S binder:16876_4
  35. audioserver  16876 17743     1   12552792  50996 binder_th+          0 S binder:16876_5
复制代码
总结

团体音频链路的预备工作暂时画上一个句号,复杂但是容易明白。音频服务端已经预备停当,等候音频客户端(AudioTrack/AudioRecord)的加入并开始奥妙的音频之旅。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表