lic void setSpeakerphoneOnboolean on if checkAudioSettingsPermissionquotsetSpeakerphoneOnquot return if on //看到这里是不是明白十之八九了下面这个调用是 //强制通话使用speaker原来是这么个意思 AudioSystem.setForceUseAudioSystem.FOR_COMMUNICATION AudioSystem.FORCE_SPEAKER mForcedUseForComm AudioSystem.FORCE_SPEAKER else AudioSystem.setForceUseAudioSystem.FOR_COMMUNICATION AudioSystem.FORCE_NONE mForcedUseForComm AudioSystem.FORCE_NONE 好了说点题外话既然
Android源码都放开给我们了有什么理由我们不去多搜搜呢上网google也是搜查源代码也是一样吗。
不过我们要有目的就是找到一个合适的使用场景。
force_use和force_config就不用我再解释了吧 ---gtAudioPolicyManagerBase::AudioPolicyManagerBase ... //下面这个意思就是把几种for_use的情况使用的设备全部置为NONE。
//比如设置FOR_MEDIA的场景使用的设备就是FORCE_NONE for int i 0 i lt AudioSystem::NUM_FORCE_USE i mForceUsei AudioSystem::FORCE_NONE // 目前可以的输出设备耳机和外放 mAvailableOutputDevices AudioSystem::DEVICE_OUT_EARPIECE AudioSystem::DEVICE_OUT_SPEAKER //目前可用的输入设备内置MIC mAvailableInputDevices AudioSystem::DEVICE_IN_BUILTIN_MIC 又得来看看AudioSystem是怎么定义输入输出设备的了。
---gtmAvailableOutputDevices AudioSystem::DEVICE_OUT_EARPIECE enum audio_devices // output devices DEVICE_OUT_EARPIECE 0x1 DEVICE_OUT_SPEAKER 0x2 DEVICE_OUT_WIRED_HEADSET 0x4 DEVICE_OUT_WIRED_HEADPHONE 0x8 DEVICE_OUT_BLUETOOTH_SCO 0x10 DEVICE_OUT_BLUETOOTH_SCO_HEADSET 0x20 DEVICE_OUT_BLUETOOTH_SCO_CARKIT 0x40 DEVICE_OUT_BLUETOOTH_A2DP 0x80 DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES 0x100 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER 0x200 DEVICE_OUT_AUX_DIGITAL 0x400 DEVICE_OUT_DEFAULT 0x8000 DEVICE_OUT_ALL DEVICE_OUT_EARPIECE DEVICE_OUT_SPEAKER DEVICE_OUT_WIRED_HEADSET DEVICE_OUT_WIRED_HEADPHONE DEVICE_OUT_BLUETOOTH_SCO DEVICE_OUT_BLUETOOTH_SCO_HEADSET DEVICE_OUT_BLUETOOTH_SCO_CARKIT DEVICE_OUT_BLUETOOTH_A2DP DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER DEVICE_OUT_AUX_DIGITAL DEVICE_OUT_DEFAULT DEVICE_OUT_ALL_A2DP DEVICE_OUT_BLUETOOTH_A2DP DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER // input devices DEVICE_IN_COMMUNICATION 0x10000 DEVICE_IN_AMBIENT 0x20000 DEVICE_IN_BUILTIN_MIC 0x40000 DEVICE_IN_BLUETOOTH_SCO_HEADSET 0x80000 DEVICE_IN_WIRED_HEADSET 0x100000 DEVICE_IN_AUX_DIGITAL 0x200000 DEVICE_IN_VOICE_CALL 0x400000 DEVICE_IN_BACK_MIC 0x800000 DEVICE_IN_DEFAULT 0x80000000 DEVICE_IN_ALL DEVICE_IN_COMMUNICATION DEVICE_IN_AMBIENT DEVICE_IN_BUILTIN_MIC DEVICE_IN_BLUETOOTH_SCO_HEADSET DEVICE_IN_WIRED_HEADSET DEVICE_IN_AUX_DIGITAL DEVICE_IN_VOICE_CALL DEVICE_IN_BACK_MIC DEVICE_IN_DEFAULT 一些比较容易眼花的东西我标成红色的了。
这么多东西不过没什么我们不明白的了。
得嘞继续走。
---gtAudioPolicyManagerBase::AudioPolicyManagerBase // 目前可以的输出设备又有耳机又有外放配置很强悍啊。
//注意这里是OR操作符最终mAvailableOutputDevices 0X3 mAvailableOutputDevices AudioSystem::DEVICE_OUT_EARPIECE AudioSystem::DEVICE_OUT_SPEAKER //目前可用的输入设备内置MICmAvailableInputDevices为0x4000不过我们不关注input mAvailableInputDevices AudioSystem::DEVICE_IN_BUILTIN_MIC ... 下面东西就很少了我们一气呵成。
//创建一个AudioOutputDescriptor并设置它的device为外设0x2 AudioOutputDescriptor outputDesc new AudioOutputDescriptor outputDesc-gtmDevice uint32_tAudioSystem::DEVICE_OUT_SPEAKER //调用APS的openOutput得到一个mHardwareOutput东东。
这是个int型 //不过保不准是一个指针也不一定喔。
//而且下面的参数都是指针类型flags除外难道有人会改value吗 mHardwareOutput mpClientInterface-gtopenOutputampoutputDesc-gtmDevice ampoutputDesc-gtmSamplingRate ampoutputDesc-gtmFormat ampoutputDesc-gtmChannels ampoutputDesc-gtmLatency outputDesc-gtmFlags //这个...估计是把int和指针加入到一个map了方便管理。
addOutputmHardwareOutput outputDesc //不知道干嘛待会看。
setOutputDevicemHardwareOutput uint32_tAudioSystem::DEVICE_OUT_SPEAKER true //不知道干嘛待会看。
updateDeviceForStrategy 好了上面还有一系列函数等着我们调用呢。
我们一个一个看。
提前说一下这块可是AudioManagerBase的核心喔。
----gtAudioOutputDescriptor outputDesc new AudioOutputDescriptor AudioOutputDescriptor是个什么我不是神我也得看注释。
// descriptor for audio outputs. Used to maintain current configuration of each opened audio output // and keep track of the usage of this output by each audio stream type. 明白了么大概意思就是它是这么一个东西 描述audio输出的可以用来保存一些配置信息。
跟踪音频stream类型使用这个output的一些情况。
没明白吧以后碰到场景就明白了。
它的构造函数干了如下勾当 AudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor : mId0 mSamplingRate0 mFormat0 mChannels0 mLatency0 mFlagsAudioSystem::output_flags0 mDevice0 mOutput10 mOutput20 //很好统统都置零了。
上面这些东西不用我解释了吧命名规则也可以看出来。
OKgo on. ---gtmHardwareOutput mpClientInterface-gtopenOutput: 这里调用的是APS的openOutput看看去 ---gtAudioPolicyService::openOutput audio_io_handle_t AudioPolicyService::openOutputuint32_t pDevices uint32_t pSamplingRate uint32_t pFormat uint32_t pChannels uint32_t pLatencyMs AudioSystem::output_flags flags spltIAudioFlingergt af AudioSystem::get_audio_flinger //娘希匹搞到AF去了 return af-gtopenOutputpDevices pSamplingRate uint32_t pFormat pChannels pLatencyMs flags -----gtAudioFlinger::openOutput int AudioFlinger::openOutputuint32_t pDevices uint32_t pSamplingRate uint32_t pFormat uint32_t pChannels uint32_t pLatencyMs uint32_t flags //我们思考下传进来的值吧 //pDevices0x2代表外放 //其他都是0。
嘿嘿有了值这不就知道下面该怎么走了吗 status_t status PlaybackThread thread NULL mHardwareStatus AUDIO_HW_OUTPUT_OPEN uint32_t samplingRate pSamplingRate pSamplingRate : 0 uint32_t format pFormat pFormat : 0 uint32_t channels pChannels pChannels : 0 uint32_t latency pLatencyMs pLatencyMs : 0 Mutex::Autolock _lmLock //HAL对象得到一个AudioStreamOut传进去的值会改吗 AudioStreamOut output mAudioHardware-gtopenOutputStreampDevices int ampformat ampchannels ampsamplingRate ampstatus mHardwareStatus AUDIO_HW_IDLE if output 0 //走哪个分支我把答案告诉大家吧。
//刚才那个mAudioHardware-gtopenOutputStream确实会更改指针对应的value。
//当然我们说了AF使用的是GENERIC的Audio硬件。
大家有兴趣可以去看看它的实现。
//我待会再贴出它的内容。
反正到这里。
//那几个值变成format为PCM_16_BITchannels为2samplingRate为44100 //这样的话那只能走else分支了。
if flags amp AudioSystem::OUTPUT_FLAG_DIRECT format AudioSystem::PCM_16_BIT channels AudioSystem::CHANNEL_OUT_STEREO thread new DirectOutputThreadthis output mNextThreadId else //还记得前两节分析的同学看到这里是不是明白了恩原来 //open一个Output就会在AF中创建一个混音线程。
设计得真好。
//想象下所有设置为外放的程序它的输出都是这个外放stream混音线程来工作 //所有设置为耳机的程序它的输出都是这个耳机stream混音线程来完成。
//为什么对stream特加强调呢没看见 //我们调用的是mAudioHardware-gtopenOutputStream0x2嘛。
返回的 //是一个AudioStreamOut可不是设备喔。
Android把这些个东西都交给HAL层去实现了。
//不用自己来管理系统上有什么耳机外设蓝牙真实设备之类的东东它反正用AudioStreamOut来表示它想要的就可以了。
例如Generic的Audio Hal只支持一个OutputStream。
--gt only my opinion thread new MixerThreadthis output mNextThreadId //好了又多得了一个线程 mPlaybackThreads.addmNextThreadId thread if pSamplingRate pSamplingRate samplingRate if pFormat pFormat format if pChannels pChannels channels if pLatencyMs pLatencyMs thread-gtlatency //从这里返回的是混音线程的索引。
return mNextThreadId return 0//如果没创建成功线程则返回零。
好我们回到AudioManagerBase中。
---gtAudioPolicyManagerBase::AudioPolicyManagerBase mHardwareOutput mpClientInterface-gtopenOutputampoutputDesc-gtmDevice ampoutputDesc-gtmSamplingRate ampoutputDesc-gtmFormat ampoutputDesc-gtmChannels ampoutputDesc-gtmLatency outputDesc-gtmFlags //上面实际就返回一个线程index。
我有点疑惑难道APS就只这么一个实际是线程index的东西就就行了吗虽然它把这个index当成hardware的标识了。
//这个...估计是把int和指针加入到一个map了方便管理。
不看了.
上一篇:
Android开发指南
下一篇:
7*30m公路预应力混凝土连续梁桥上部结构设计