上一篇《AudioPolicy之输出音频策略》通过改变底层输出策略
下面介绍通过上层传递参数来修改音频输出,相比底层修改而言这种方法更加灵活,改动的风险更小,不会影响底层音频输出策略。
这里我们还是采用上篇文章的例子:
要求来电时,打开应用层开关,来电铃声只从耳机端发出。
关闭应用层开关,恢复默认情况,声音同时从耳机端和Speaker发出
这里以MTK平台为例,高通平台路径会有一些差异
应用层:
打开开关时,调用AudioManager.setParameter("AudioRingSpeakerOff",1);
开关关闭时,调用AudioManager.setParameter("AudioRingSpeakerOff",0);
Step1:
/frameworks/av/media/libmedia/AudioSystem.cpp
static String8 key_ring_speaker_on =String8("AudioRingSpeakerOff");//Add this
status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
......
else if (param.getInt(keyFmRXForceDisableFMTX, value) == NO_ERROR){
const sp& aps = AudioSystem::get_audio_policy_service();
if(aps != 0)
{
aps->SetPolicyManagerParameters (POLICY_SET_FM_RX_FORCE_DISABLE_TX,value,0,0);
}
}
//add begin
else if (param.getInt(key_ring_speaker_off, value) == NO_ERROR){
const sp& aps = AudioSystem::get_audio_policy_service();
if(aps != 0)
{
aps->SetPolicyManagerParameters (POLICY_SET_RING_SPEAKER_OFF,value,0,0);
}
}
//add end
Step2:
/vendor/mediatek/proprietary/hardware/audio/common/include/AudioPolicyParameters.h
enum PolicyParameters
{
POLICY_LOAD_VOLUME = 0,
...
POLICY_SET_CROSSMOUNT_LOCAL_PLAYBACK, //For MTK_CROSSMOUNT_SUPPORT
POLICY_SET_CROSSMOUNT_MIC_LOCAL_PLAYBACK, //For MTK_CROSSMOUNT_SUPPORT
POLICY_SET_RING_SPEAKER_OFF, //Add this
};
Step3:
/frameworks/av/services/audiopolicy/engine/interface/AudioPolicyManagerInterface.h
virtual void setringspeakeroff(int a) = 0;//Add this
Step4:
/frameworks/av/services/audiopolicy/enginedefault/src/Engine.h
....
virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy, audio_output_flags_t flags) const
{
return mPolicyEngine->getDeviceForStrategy(strategy, flags);
}
//Add begin
virtual void setringspeakeroff(int a)
{
return mPolicyEngine->setringspeakeroff(a);
}
//Add end
audio_devices_t getDeviceForStrategy(routing_strategy strategy, audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE) const;
void setringspeakeroff(int a); //Add
...
audio_mode_t mPhoneState; /**< current phone state. */
int ring_speaker_off;//Add
Step5:
/frameworks/av/services/audiopolicy/enginedefault/src/Engine.cpp
Engine::Engine()
: mManagerInterface(this),
mPhoneState(AUDIO_MODE_NORMAL),
mApmObserver(NULL)
{
ring_speaker_off = 0;//Add 在这里初始化
for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
}
}
//Add begin
void Engine::setringspeakeroff(int a)
{
ring_speaker_off = a;
}
//Add end
routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream)
case AUDIO_STREAM_RING:
case AUDIO_STREAM_ALARM:
return STRATEGY_SONIFICATION;
//铃声对应的strategy是STRATEGY_SONIFICATION,
在getDeviceForStrategy中修改STRATEGY_SONIFICATION下的相关内容
audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy, audio_output_flags_t flags) const
//Add begin
if ((strategy == STRATEGY_SONIFICATION) || (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED))
{
//在这里加变量控制
if(ring_speaker_off)
{
device &= ~AUDIO_DEVICE_OUT_SPEAKER;
}
if (device == AUDIO_DEVICE_NONE)
{
ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
}
}
//Add end
参考文章:
https://onlinesso.mediatek.com/Pages/FAQ.aspx?list=SW&FAQID=FAQ19632
网友评论