【0006】Android音频设备解析

【1】函数loadHwModule_l解析
 1     
 2 // loadHwModule_l() must be called with AudioFlinger::mLock held
 3 audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
 4 {
 5 //【1】判断是否添加interface?
 6     for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
 7         if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
 8             ALOGW("loadHwModule() module %s already loaded", name);
 9             return mAudioHwDevs.keyAt(i);
10         }
11     }
12 
13     audio_hw_device_t *dev;
14 //【2】加载audio interface
15     int rc = load_audio_interface(name, &dev);
16     if (rc) {
17         ALOGI("loadHwModule() error %d loading module %s ", rc, name);
18         return 0;
19     }
20 //【3】初始化
21     mHardwareStatus = AUDIO_HW_INIT;
22     rc = dev->init_check(dev);
23     //每次初始化都需要改变mHardwareStatus的值,为了正确dump出输出内部状态;
24     mHardwareStatus = AUDIO_HW_IDLE;
25     if (rc) {
26         ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
27         return 0;
28     }
29 
30     // Check and cache this HAL's level of support for master mute and master
31     // volume.  If this is the first HAL opened, and it supports the get
32     // methods, use the initial values provided by the HAL as the current
33     // master mute and volume settings.
34 
35     AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
36     {  // scope for auto-lock pattern
37         AutoMutex lock(mHardwareLock);
38 
39         if (0 == mAudioHwDevs.size()) {
40             mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
41             if (NULL != dev->get_master_volume) {
42                 float mv;
43                 if (OK == dev->get_master_volume(dev, &mv)) {
44                     mMasterVolume = mv;
45                 }
46             }
47 
48             mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
49             if (NULL != dev->get_master_mute) {
50                 bool mm;
51                 if (OK == dev->get_master_mute(dev, &mm)) {
52                     mMasterMute = mm;
53                 }
54             }
55         }
56 
57         mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
58         if ((NULL != dev->set_master_volume) &&
59             (OK == dev->set_master_volume(dev, mMasterVolume))) {
60             flags = static_cast<AudioHwDevice::Flags>(flags |
61                     AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
62         }
63 
64         mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
65         if ((NULL != dev->set_master_mute) &&
66             (OK == dev->set_master_mute(dev, mMasterMute))) {
67             flags = static_cast<AudioHwDevice::Flags>(flags |
68                     AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
69         }
70 
71         mHardwareStatus = AUDIO_HW_IDLE;
72     }
73 //【4】添加到全局变量中:将设备添加到mAudioHwDevs键值对中,
74 //key:handle = nextUniqueId(),保证audio_interface具有全局唯一的id;
75     audio_module_handle_t handle = nextUniqueId();
76     mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
77 
78     ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
79           name, dev->common.module->name, dev->common.module->id, handle);
80 
81     return handle;
}
2.打开音频设备
 1 sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
 2                                                             audio_io_handle_t *output,
 3                                                             audio_config_t *config,
 4                                                             audio_devices_t devices,
 5                                                             const String8& address,
 6                                                             audio_output_flags_t flags)
 7 {
 8 /* 【1】查找相应的audio_interface*/
 9     AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
10     if (outHwDev == NULL) {
11         return 0;
12     }
13 /*【2】为设备打开一个输入流 */
14     audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
15     if (*output == AUDIO_IO_HANDLE_NONE) {
16         *output = nextUniqueId();
17     }
18 
19     mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
20 
21     audio_stream_out_t *outStream = NULL;
22 
23     // FOR TESTING ONLY:
24     // This if statement allows overriding the audio policy settings
25     // and forcing a specific format or channel mask to the HAL/Sink device for testing.
26     if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
27         // Check only for Normal Mixing mode
28         if (kEnableExtendedPrecision) {
29             // Specify format (uncomment one below to choose)
30             //config->format = AUDIO_FORMAT_PCM_FLOAT;
31             //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
32             //config->format = AUDIO_FORMAT_PCM_32_BIT;
33             //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
34             // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
35         }
36         if (kEnableExtendedChannels) {
37             // Specify channel mask (uncomment one below to choose)
38             //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch
39             //config->channel_mask = audio_channel_mask_from_representation_and_bits(
40             //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example
41         }
42     }
43 
44     status_t status = hwDevHal->open_output_stream(hwDevHal,
45                                                    *output,
46                                                    devices,
47                                                    flags,
48                                                    config,
49                                                    &outStream,
50                                                    address.string());
51 
52     mHardwareStatus = AUDIO_HW_IDLE;
53     ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
54             "channelMask %#x, status %d",
55             outStream,
56             config->sample_rate,
57             config->format,
58             config->channel_mask,
59             status);
60 
61     if (status == NO_ERROR && outStream != NULL) {
62         /* 【3】生成AudioStreamOut*/
63         AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
64         /*【4】创建PlaybackThreadOut*/
65         PlaybackThread *thread;
66         if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
67             thread = new OffloadThread(this, outputStream, *output, devices);
68             ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
69         } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
70                 || !isValidPcmSinkFormat(config->format)
71                 || !isValidPcmSinkChannelMask(config->channel_mask)) {
72                //***new DirectOutputThread
73             thread = new DirectOutputThread(this, outputStream, *output, devices);
74             ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
75         } else {
76             //***new MixerThread
77             thread = new MixerThread(this, outputStream, *output, devices);
78             ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
79         }
80         /*【5】 添加播放线程*/
81         mPlaybackThreads.add(*output, thread);
82         return thread;
83     }
84 
85     return 0;
86 }

所做的工作:

【1】 查找合适的音频接口与设备;

【2】创建音频输出流(open_output_stream 获得一个audio_stream_out_t)

【3】利用AudioStreamOut封转audio_stream_out_t和audio_hw_device_t;

【4】创建播放线程;

【5】如果当前设备是主设备,则还需要进行相应的设置;

3.findSuitableHwDev_l 查找当前系统的设备

 1 AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
 2         audio_module_handle_t module,
 3         audio_devices_t devices)
 4 {
 5     // if module is 0, the request comes from an old policy manager and we should load
 6     // well known modules
 7     /* 特殊的module值,此时需要加载所有interface*/
 8     if (module == 0) {
 9         ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
10         for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
11             loadHwModule_l(audio_interfaces[i]);
12         }
13         // then try to find a module supporting the requested device.
14         /* 然后再查找符合要求的device*/
15         for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
16             AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
17             audio_hw_device_t *dev = audioHwDevice->hwDevice();
18             if ((dev->get_supported_devices != NULL) &&
19                     (dev->get_supported_devices(dev) & devices) == devices)
20                 return audioHwDevice;
21         }
22     } else {/* 直接从记录中记录device*/
23         // check a match for the requested module handle
24         AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
25         if (audioHwDevice != NULL) {
26             return audioHwDevice;
27         }
28     }
29 
30     return NULL;
31 }

 

 

 
posted @ 2017-08-01 09:12  OzTaking  阅读(668)  评论(0编辑  收藏  举报