android Audio设置音量流程及其binder通讯原理
16lz
2021-01-24
Audio java部分代码流程(4.1.2 version):
在frameworks/base/media/java/android/media中:IAudioService.aidl提供了所有对外的接口函数,如下:
[cpp] view plain copy
- interfaceIAudioService{
- voidadjustVolume(intdirection,intflags);
- voidadjustSuggestedStreamVolume(intdirection,intsuggestedStreamType,intflags);
- voidadjustStreamVolume(intstreamType,intdirection,intflags);
- voidsetStreamVolume(intstreamType,intindex,intflags);
- voidsetStreamSolo(intstreamType,booleanstate,IBindercb);
- voidsetStreamMute(intstreamType,booleanstate,IBindercb);
- booleanisStreamMute(intstreamType);
- intgetStreamVolume(intstreamType);
- intgetStreamMaxVolume(intstreamType);
- intgetLastAudibleStreamVolume(intstreamType);
- voidsetRingerMode(intringerMode);
- .......................
- }
[cpp] view plain copy
- publicclassAudioServiceextendsIAudioService.Stub{
- ........
- }
[cpp] view plain copy
- 492try{
- 493Slog.i(TAG,"AudioService");
- 494ServiceManager.addService(Context.AUDIO_SERVICE,newAudioService(context));
- 495}catch(Throwablee){
- 496reportWtf("startingAudioService",e);
- 497}
而AudioManager.java则实现这些函数了对外应用的接口,比如:
[cpp] view plain copy
- 496publicvoidadjustVolume(intdirection,intflags){
- 497IAudioServiceservice=getService();//
- 498try{
- 499service.adjustVolume(direction,flags);
- 500}catch(RemoteExceptione){
- 501Log.e(TAG,"DeadobjectinadjustVolume",e);
- 502}
- 503}
[cpp] view plain copy
- 365privatestaticIAudioServicegetService()
- 366{
- 367if(sService!=null){
- 368returnsService;
- 369}
- 370IBinderb=ServiceManager.getService(Context.AUDIO_SERVICE);
- 371sService=IAudioService.Stub.asInterface(b);
- 372returnsService;
- 373}
[cpp] view plain copy
- 281registerService(AUDIO_SERVICE,newServiceFetcher(){
- 282publicObjectcreateService(ContextImplctx){
- 283returnnewAudioManager(ctx);
- 284}});
[cpp] view plain copy
- 10353publicclassAudioManager{
- 10354methodpublicintabandonAudioFocus(android.media.AudioManager.OnAudioFocusChangeListener);
- 10355methodpublicvoidadjustStreamVolume(int,int,int);
- 10356methodpublicvoidadjustSuggestedStreamVolume(int,int,int);
- 10357methodpublicvoidadjustVolume(int,int);
- 10358methodpublicintgetMode();
- 10359methodpublicjava.lang.StringgetParameters(java.lang.String);
- 10360methodpublicintgetRingerMode();
- 10361methodpublicdeprecatedintgetRouting(int);
- 10362methodpublicintgetStreamMaxVolume(int);
- 10363methodpublicintgetStreamVolume(int);
- 10364methodpublicintgetVibrateSetting(int);
- 10365methodpublicbooleanisBluetoothA2dpOn();
1、应用调用比如mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
2、AudioManager.java调用public void setStreamVolume(int streamType, int index, int flags){};
3、AudioService.java调用public void setStreamVolume(int streamType, int index, int flags){},在该函数中调用
setStreamVolumeInt(mStreamVolumeAlias[streamType], 831 index, 832 device, 833 false, 834 true); ---------> 1002 sendMsg(mAudioHandler, 1003 MSG_PERSIST_VOLUME, 1004 SENDMSG_QUEUE, 1005 PERSIST_LAST_AUDIBLE, 1006 device, 1007 streamState, 1008 PERSIST_DELAY);
而mAudioHandler = new AudioHandler(),看看这个AudioHandler类:private class AudioHandler extends Handler {};-------->
所以调用该类重写的handleMessage方法--------->
[cpp] view plain copy
- 2940@Override
- 2941publicvoidhandleMessage(Messagemsg){
- 2942
- 2943switch(msg.what){
- 2944
- 2945caseMSG_SET_DEVICE_VOLUME:
- 2946setDeviceVolume((VolumeStreamState)msg.obj,msg.arg1);
- 2947break;
setDeviceVolume---------> mStreamStates[streamType].applyDeviceVolume(getDeviceForStream(streamType));---------->AudioSystem.setStreamVolumeIndex--------->
setStreamVolumeIndex的定义在AudioSystem.java中-------->public static native int setStreamVolumeIndex(int stream, int index, int device);
setStreamVolumeIndex的JNI实现在base/core/jni/android_media_AudioSystem.cpp中---------->[cpp] view plain copy
- 178staticint
- 179android_media_AudioSystem_setStreamVolumeIndex(JNIEnv*env,
- 180jobjectthiz,
- 181jintstream,
- 182jintindex,
- 183jintdevice)
- 184{
- 185returncheck_AudioSystem_Command(
- 186AudioSystem::setStreamVolumeIndex(static_cast<audio_stream_type_t>(stream),
- 187index,
- 188(audio_devices_t)device));
- 189}
[cpp] view plain copy
- 666status_tAudioSystem::setStreamVolumeIndex(audio_stream_type_tstream,
- 667intindex,
- 668audio_devices_tdevice)
- 669{
- 670constsp<IAudioPolicyService>&aps=AudioSystem::get_audio_policy_service();
- 671if(aps==0)returnPERMISSION_DENIED;
- 672returnaps->setStreamVolumeIndex(stream,index,device);
- 673}
[cpp] view plain copy
- 514constsp<IAudioPolicyService>&AudioSystem::get_audio_policy_service()
- 515{
- 516gLock.lock();
- 517if(gAudioPolicyService==0){
- 518sp<IServiceManager>sm=defaultServiceManager();//取得BpServiceManager,通过这个代理可以调用BnServiceManager::onTransact,最后和ServiceManager通讯
- 519sp<IBinder>binder;
- 520do{
- 521binder=sm->getService(String16("media.audio_policy"));//从ServiceManager获得远程AudioPolicyService的句柄
- 522if(binder!=0)
- 523break;
- 524ALOGW("AudioPolicyServicenotpublished,waiting...");
- 525usleep(500000);//0.5s
- 526}while(true);
- 527if(gAudioPolicyServiceClient==NULL){
- 528gAudioPolicyServiceClient=newAudioPolicyServiceClient();
- 529}
- 530binder->linkToDeath(gAudioPolicyServiceClient);
- 531gAudioPolicyService=interface_cast<IAudioPolicyService>(binder);
- 532gLock.unlock();
- 533}else{
- 534gLock.unlock();
- 535}
- 536returngAudioPolicyService;
- 537}
[cpp] view plain copy
- 42inlinesp<INTERFACE>interface_cast(constsp<IBinder>&obj)
- 43{
- 44returnINTERFACE::asInterface(obj);
- 45}
[cpp] view plain copy
- DECLARE_META_INTERFACE(AudioPolicyService)展开后得到的,DECLARE_META_INTERFACE定义在IInterface.h中:
- 74#defineDECLARE_META_INTERFACE(INTERFACE)\
- 75staticconstandroid::String16descriptor;\
- 76staticandroid::sp<I##INTERFACE>asInterface(\
- 77constandroid::sp<android::IBinder>&obj);\
- 78virtualconstandroid::String16&getInterfaceDescriptor()const;\
- 79I##INTERFACE();\
- 80virtual~I##INTERFACE();
[cpp] view plain copy
- #defineIMPLEMENT_META_INTERFACE(INTERFACE,NAME)
- ........................
- 89android::sp<I##INTERFACE>I##INTERFACE::asInterface(\
- 90constandroid::sp<android::IBinder>&obj)\
- 91{\
- 92android::sp<I##INTERFACE>intr;\
- 93if(obj!=NULL){\
- 94intr=static_cast<I##INTERFACE*>(\
- 95obj->queryLocalInterface(\
- 96I##INTERFACE::descriptor).get());\
- 97if(intr==NULL){\
- 98intr=newBp##INTERFACE(obj);\
- 99}\
- 100}\
- 101returnintr;\
- 102}
展开后最终是生成调用new BpAudioPolicyService(new BpBinder(handle)),这里的handle是一个句柄;这样我们最终得到了AudioPolicyService的代理BpAudioPolicyService,通过它就可以和 AudioPolicyService的本地接口BnAudioPolicyService通讯了。
回到前面的AudioSystem.cpp,取得代理BpAudioPolicyService后调用aps->setStreamVolumeIndex,所以进入IAudioPolicyService.cpp:
class BpAudioPolicyService : public BpInterface<IAudioPolicyService>:
[cpp] view plain copy
- 233virtualstatus_tsetStreamVolumeIndex(audio_stream_type_tstream,
- 234intindex,
- 235audio_devices_tdevice)
- 236{
- 237Parceldata,reply;
- 238data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
- 239data.writeInt32(static_cast<uint32_t>(stream));
- 240data.writeInt32(index);
- 241data.writeInt32(static_cast<uint32_t>(device));
- 242remote()->transact(SET_STREAM_VOLUME,data,&reply);
- 243returnstatic_cast<status_t>(reply.readInt32());
- 244}
[cpp] view plain copy
- inlineIBinder*remote(){returnmRemote;}
- IBinder*constmRemote;
[cpp] view plain copy
- 159status_tBpBinder::transact(
- 160uint32_tcode,constParcel&data,Parcel*reply,uint32_tflags)
- 161{
- 162//Onceabinderhasdied,itwillnevercomebacktolife.
- 163if(mAlive){
- 164status_tstatus=IPCThreadState::self()->transact(
- 165mHandle,code,data,reply,flags);
- 166if(status==DEAD_OBJECT)mAlive=0;
- 167returnstatus;
- 168}
- 169
- 170returnDEAD_OBJECT;
- 171}
[cpp] view plain copy
- 34intmain(intargc,char**argv)
- 35{
- 36sp<ProcessState>proc(ProcessState::self());
- 37sp<IServiceManager>sm=defaultServiceManager();
- 38ALOGI("ServiceManager:%p",sm.get());
- 39AudioFlinger::instantiate();
- 40MediaPlayerService::instantiate();
- 41CameraService::instantiate();
- 42AudioPolicyService::instantiate();
- 43ProcessState::self()->startThreadPool();
- 44IPCThreadState::self()->joinThreadPool();
- 45}
frameworks/native/libs/binder/ProcessState.cpp:
[cpp] view plain copy
- 74sp<ProcessState>ProcessState::self()
- 75{
- 76Mutex::Autolock_l(gProcessMutex);
- 77if(gProcess!=NULL){
- 78returngProcess;
- 79}
- 80gProcess=newProcessState;
- 81returngProcess;
- 82}
[cpp] view plain copy
- 335ProcessState::ProcessState()
- 336:mDriverFD(open_driver())
- 337,mVMStart(MAP_FAILED)
- 338,mManagesContexts(false)
- 339,mBinderContextCheckFunc(NULL)
- 340,mBinderContextUserData(NULL)
- 341,mThreadPoolStarted(false)
- 342,mThreadPoolSeq(1)
- 343{
- 344if(mDriverFD>=0){
- 345//XXXIdeally,thereshouldbeaspecificdefineforwhetherwe
- 346//havemmap(orwhetherwecouldpossiblyhavethekernelmodule
- 347//availabla).
- 348#if!defined(HAVE_WIN32_IPC)
- 349//mmapthebinder,providingachunkofvirtualaddressspacetoreceivetransactions.
- 350mVMStart=mmap(0,BINDER_VM_SIZE,PROT_READ,MAP_PRIVATE|MAP_NORESERVE,mDriverFD,0);
- .................................
- }
回到main_mediaserver.cpp,37行defaultServiceManager(),在frameworks/native/libs/binder/IServiceManager.cpp中:
[cpp] view plain copy
- 34sp<IServiceManager>defaultServiceManager()
- 35{
- 36if(gDefaultServiceManager!=NULL)returngDefaultServiceManager;
- 37
- 38{
- 39AutoMutex_l(gDefaultServiceManagerLock);
- 40if(gDefaultServiceManager==NULL){
- 41gDefaultServiceManager=interface_cast<IServiceManager>(
- 42ProcessState::self()->getContextObject(NULL));
- 43}
- 44}
- 45
- 46returngDefaultServiceManager;
- 47}
[cpp] view plain copy
- 89sp<IBinder>ProcessState::getContextObject(constsp<IBinder>&caller)
- 90{
- 91returngetStrongProxyForHandle(0);
- 92}
[cpp] view plain copy
- 37classAudioPolicyService:
- 38publicBinderService<AudioPolicyService>,
- 39publicBnAudioPolicyService,
- 40//publicAudioPolicyClientInterface,
- 41publicIBinder::DeathRecipient
[cpp] view plain copy
- 33template<typenameSERVICE>
- 34classBinderService
- 35{
- 36public:
- 37staticstatus_tpublish(boolallowIsolated=false){
- 38sp<IServiceManager>sm(defaultServiceManager());
- 39returnsm->addService(String16(SERVICE::getServiceName()),newSERVICE(),allowIsolated);
- 40}
- 41
- 42staticvoidpublishAndJoinThreadPool(boolallowIsolated=false){
- 43sp<IServiceManager>sm(defaultServiceManager());
- 44sm->addService(String16(SERVICE::getServiceName()),newSERVICE(),allowIsolated);
- 45ProcessState::self()->startThreadPool();
- 46IPCThreadState::self()->joinThreadPool();
- 47}
- 48
- 49staticvoidinstantiate(){publish();}
- 50
- 51staticstatus_tshutdown(){
- 52returnNO_ERROR;
- 53}
- 54};
BpServiceManager(new BpBinder(0))->addService(String16("media.audio_policy", new AudioPolicyService(), false),进入BpServiceManger::addService的实现,这个 函数实现在frameworks/native/libs/binder/IServiceManager.cpp:
[cpp] view plain copy
- 154virtualstatus_taddService(constString16&name,constsp<IBinder>&service,boolallowIsolated)
- 156{
- 157Parceldata,reply;
- 158data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
- 159data.writeString16(name);
- 160data.writeStrongBinder(service);
- 161data.writeInt32(allowIsolated?1:0);
- 162status_terr=remote()->transact(ADD_SERVICE_TRANSACTION,data,&reply);
- 163returnerr==NO_ERROR?reply.readExceptionCode():err;
- 164}
[cpp] view plain copy
- 136voidProcessState::startThreadPool()
- 137{
- 138AutoMutex_l(mLock);
- 139if(!mThreadPoolStarted){
- 140mThreadPoolStarted=true;
- 141spawnPooledThread(true);
- 142}
- 143}
[cpp] view plain copy
- 286voidProcessState::spawnPooledThread(boolisMain)
- 287{
- 288if(mThreadPoolStarted){
- 289int32_ts=android_atomic_add(1,&mThreadPoolSeq);
- 290charbuf[16];
- 291snprintf(buf,sizeof(buf),"Binder_%X",s);
- 292ALOGV("Spawningnewpooledthread,name=%s\n",buf);
- 293sp<Thread>t=newPoolThread(isMain);
- 294t->run(buf);
- 295}
- 296}
[cpp] view plain copy
- 56classPoolThread:publicThread
- 57{
- 58public:
- 59PoolThread(boolisMain)
- 60:mIsMain(isMain)
- 61{
- 62}
- 63
- 64protected:
- 65virtualboolthreadLoop()
- 66{
- 67IPCThreadState::self()->joinThreadPool(mIsMain);
- 68returnfalse;
- 69}
- 70
- 71constboolmIsMain;
- 72};
[cpp] view plain copy
- 97status_tBBinder::transact(
- 98uint32_tcode,constParcel&data,Parcel*reply,uint32_tflags)
- 99{
- 100data.setDataPosition(0);
- 101
- 102status_terr=NO_ERROR;
- 103switch(code){
- 104casePING_TRANSACTION:
- 105reply->writeInt32(pingBinder());
- 106break;
- 107default:
- 108err=onTransact(code,data,reply,flags);
- 109break;
- 110}
- 111
- 112if(reply!=NULL){
- 113reply->setDataPosition(0);
- 114}
- 115
- 116returnerr;
- 117}
[cpp] view plain copy
- 610status_tAudioPolicyService::onTransact(uint32_tcode,constParcel&data,Parcel*reply,uint32_tflags)
- 612{
- 613returnBnAudioPolicyService::onTransact(code,data,reply,flags);
- 614}
到这里BpAudioPolicyService和BnAudioPolicyService的binder通讯关系就完成了,回到前面IAudioPolicyService.cpp中的242行 remote()->transact(SET_STREAM_VOLUME, data, &reply);这里传进来的是SET_STREAM_VOLUME,所以调用BnAudioPolicyService::onTransact:
[cpp] view plain copy
- 512caseSET_STREAM_VOLUME:{
- 513CHECK_INTERFACE(IAudioPolicyService,data,reply);
- 514audio_stream_type_tstream=
- 515static_cast<audio_stream_type_t>(data.readInt32());
- 516intindex=data.readInt32();
- 517audio_devices_tdevice=static_cast<audio_devices_t>(data.readInt32());
- 518reply->writeInt32(static_cast<uint32_t>(setStreamVolumeIndex(stream,
- 519index,
- 520device)));
- 521returnNO_ERROR;
- 522}break;
[cpp] view plain copy
- 380status_tAudioPolicyService::setStreamVolumeIndex(audio_stream_type_tstream,
- 381intindex,
- 382audio_devices_tdevice)
- 383{
- 384if(mpAudioPolicy==NULL){
- 385returnNO_INIT;
- 386}
- 387if(!settingsAllowed()){
- 388returnPERMISSION_DENIED;
- 389}
- 390if(uint32_t(stream)>=AUDIO_STREAM_CNT){
- 391returnBAD_VALUE;
- 392}
- 393
- 394if(mpAudioPolicy->set_stream_volume_index_for_device){
- 395returnmpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
- 396stream,
- 397index,
- 398device);
- 399}else{
- 400returnmpAudioPolicy->set_stream_volume_index(mpAudioPolicy,stream,index);
- 401}
- 402}
[cpp] view plain copy
- 58AudioPolicyService::AudioPolicyService()
- 59:BnAudioPolicyService(),mpAudioPolicyDev(NULL),mpAudioPolicy(NULL)
- 60{
- 61charvalue[PROPERTY_VALUE_MAX];
- 62conststructhw_module_t*module;
- 63intforced_val;
- 64intrc;
- 65
- 66Mutex::Autolock_l(mLock);
- 67
- 68//starttoneplaybackthread
- 69mTonePlaybackThread=newAudioCommandThread(String8(""));
- 70//startaudiocommandsthread
- 71mAudioCommandThread=newAudioCommandThread(String8("ApmCommand"));
- 72
- 73/*instantiatetheaudiopolicymanager*/
- 74rc=hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID,&module);
- 75if(rc)
- 76return;
- 77
- 78rc=audio_policy_dev_open(module,&mpAudioPolicyDev);
- 79ALOGE_IF(rc,"couldn'topenaudiopolicydevice(%s)",strerror(-rc));
- 80if(rc)
- 81return;
- 82
- 83rc=mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev,&aps_ops,this,
- 84&mpAudioPolicy);
- 85ALOGE_IF(rc,"couldn'tcreateaudiopolicy(%s)",strerror(-rc));
- 86if(rc)
- 87return;
- 88
- 89rc=mpAudioPolicy->init_check(mpAudioPolicy);
- 90ALOGE_IF(rc,"couldn'tinit_checktheaudiopolicy(%s)",strerror(-rc));
- 91if(rc)
- 92return;
- .......................
- }
#define AUDIO_POLICY_HARDWARE_MODULE_ID "audio_policy"
根据hw_get_module的判断关系,看hardware/libhardware_legacy/audio/Android.mk可知,最终调用的是audio_policy.default.so,通过hw_get_module函数的 load(class_id, path, module)打开audio_policy.default.so并返回句柄,接着78行audio_policy_dev_open,在libhardware/include/hardware/audio_policy.h
[cpp] view plain copy
- 424staticinlineintaudio_policy_dev_open(consthw_module_t*module,
- 425structaudio_policy_device**device)
- 426{
- 427returnmodule->methods->open(module,AUDIO_POLICY_INTERFACE,
- 428(hw_device_t**)device);
- 429}
[cpp] view plain copy
- 406staticintlegacy_ap_dev_open(consthw_module_t*module,constchar*name,hw_device_t**device)
- 408{
- 409structlegacy_ap_device*dev;
- 411if(strcmp(name,AUDIO_POLICY_INTERFACE)!=0)
- 412return-EINVAL;
- 414dev=(structlegacy_ap_device*)calloc(1,sizeof(*dev));
- 415if(!dev)
- 416return-ENOMEM;
- 418dev->device.common.tag=HARDWARE_DEVICE_TAG;
- 419dev->device.common.version=0;
- 420dev->device.common.module=const_cast<hw_module_t*>(module);
- 421dev->device.common.close=legacy_ap_dev_close;
- 422dev->device.create_audio_policy=create_legacy_ap;
- 423dev->device.destroy_audio_policy=destroy_legacy_ap;
- 425*device=&dev->device.common;
- 427return0;
- 428}
[cpp] view plain copy
- 40structlegacy_ap_device{
- 41structaudio_policy_devicedevice;
- 42};
[cpp] view plain copy
- 410structaudio_policy_device{
- 411structhw_device_tcommon;
- 412
- 413int(*create_audio_policy)(conststructaudio_policy_device*device,
- 414structaudio_policy_service_ops*aps_ops,
- 415void*service,
- 416structaudio_policy**ap);
- 417
- 418int(*destroy_audio_policy)(conststructaudio_policy_device*device,
- 419structaudio_policy*ap);
- 420};
[cpp] view plain copy
- 311staticintcreate_legacy_ap(conststructaudio_policy_device*device,
- 312structaudio_policy_service_ops*aps_ops,
- 313void*service,
- 314structaudio_policy**ap)
- 315{
- 316structlegacy_audio_policy*lap;
- 317intret;
- 318
- 319if(!service||!aps_ops)
- 320return-EINVAL;
- 321
- 322lap=(structlegacy_audio_policy*)calloc(1,sizeof(*lap));
- 323if(!lap)
- 324return-ENOMEM;
- 325
- 326lap->policy.set_device_connection_state=ap_set_device_connection_state;
- 327lap->policy.get_device_connection_state=ap_get_device_connection_state;
- 328lap->policy.set_phone_state=ap_set_phone_state;
- 329lap->policy.set_ringer_mode=ap_set_ringer_mode;
- 330lap->policy.set_force_use=ap_set_force_use;
- 331lap->policy.get_force_use=ap_get_force_use;
- 332lap->policy.set_can_mute_enforced_audible=ap_set_can_mute_enforced_audible;
- 334lap->policy.init_check=ap_init_check;
- 335lap->policy.get_output=ap_get_output;
- 336lap->policy.start_output=ap_start_output;
- 337lap->policy.stop_output=ap_stop_output;
- 338lap->policy.release_output=ap_release_output;
- 339lap->policy.get_input=ap_get_input;
- 340lap->policy.start_input=ap_start_input;
- 341lap->policy.stop_input=ap_stop_input;
- 342lap->policy.release_input=ap_release_input;
- 343lap->policy.init_stream_volume=ap_init_stream_volume;
- 344lap->policy.set_stream_volume_index=ap_set_stream_volume_index;
- 345lap->policy.get_stream_volume_index=ap_get_stream_volume_index;
- 346lap->policy.set_stream_volume_index_for_device=ap_set_stream_volume_index_for_device;
- 347lap->policy.get_stream_volume_index_for_device=ap_get_stream_volume_index_for_device;
- 348lap->policy.get_strategy_for_stream=ap_get_strategy_for_stream;
- 349lap->policy.get_devices_for_stream=ap_get_devices_for_stream;
- 350lap->policy.get_output_for_effect=ap_get_output_for_effect;
- 351lap->policy.register_effect=ap_register_effect;
- 352lap->policy.unregister_effect=ap_unregister_effect;
- 353lap->policy.set_effect_enabled=ap_set_effect_enabled;
- 354lap->policy.is_stream_active=ap_is_stream_active;
- 355lap->policy.dump=ap_dump;
- 356
- 357lap->service=service;
- 358lap->aps_ops=aps_ops;
- 359lap->service_client=
- 360newAudioPolicyCompatClient(aps_ops,service);
- 361if(!lap->service_client){
- 362ret=-ENOMEM;
- 363gotoerr_new_compat_client;
- 364}
- 365
- 366lap->apm=createAudioPolicyManager(lap->service_client);
- 367if(!lap->apm){
- 368ret=-ENOMEM;
- 369gotoerr_create_apm;
- 370}
- 371
- 372*ap=&lap->policy;
- 373return0;
- 374
- 375err_create_apm:
- 376deletelap->service_client;
- 377err_new_compat_client:
- 378free(lap);
- 379*ap=NULL;
- 380returnret;
- 381}
[cpp] view plain copy
- 232staticintap_set_stream_volume_index_for_device(structaudio_policy*pol,
- 233audio_stream_type_tstream,
- 234intindex,
- 235audio_devices_tdevice)
- 236{
- 237structlegacy_audio_policy*lap=to_lap(pol);
- 238returnlap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream,
- 239index,
- 240device);
- 241}
[cpp] view plain copy
- 44structlegacy_audio_policy{
- 45structaudio_policypolicy;//即为mpAudioPolicy
- 46
- 47void*service;
- 48structaudio_policy_service_ops*aps_ops;
- 49AudioPolicyCompatClient*service_client;
- 50AudioPolicyInterface*apm;
- 51};
[cpp] view plain copy
- 24extern"C"AudioPolicyInterface*createAudioPolicyManager(AudioPolicyClientInterface*clientInterface)
- 25{
- 26returnnewAudioPolicyManagerDefault(clientInterface);
- 27}
[cpp] view plain copy
- 25classAudioPolicyManagerDefault:publicAudioPolicyManagerBase
- 26{
- 28public:
- 29AudioPolicyManagerDefault(AudioPolicyClientInterface*clientInterface)
- 30:AudioPolicyManagerBase(clientInterface){}
- 31
- 32virtual~AudioPolicyManagerDefault(){}
- 33
- 34};
在AudioPolicyManagerBase.cpp中:
953 status_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, int index, audio_devices_t device)956 { 958 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { 959 return BAD_VALUE; 960 } 961 if (!audio_is_output_device(device)) { 962 return BAD_VALUE; 963 } 965 // Force max volume if stream cannot be muted 966 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; 971 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and 972 // clear all device specific values 973 if (device == AUDIO_DEVICE_OUT_DEFAULT) { 974 mStreams[stream].mIndexCur.clear(); 975 } 976 mStreams[stream].mIndexCur.add(device, index); 978 // compute and apply stream volume on all outputs according to connected device 979 status_t status = NO_ERROR; 980 for (size_t i = 0; i < mOutputs.size(); i++) { 981 audio_devices_t curDevice = 982 getDeviceForVolume((audio_devices_t)mOutputs.valueAt(i)->device()); 983 if (device == curDevice) { 984 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); 985 if (volStatus != NO_ERROR) { 986 status = volStatus; 987 } 988 } 989 } 990 return status; 991 }
看984行checkAndSetVolume函数:
[cpp] view plain copy
- 2683status_tAudioPolicyManagerBase::checkAndSetVolume(intstream,
- 2684intindex,
- 2685audio_io_handle_toutput,
- 2686audio_devices_tdevice,
- 2687intdelayMs,
- 2688boolforce)
- 2689{
- ....................
- 2722if(stream==AudioSystem::BLUETOOTH_SCO){
- 2723mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL,volume,output,delayMs);
- 2724}
- 2725}
- 2726
- 2727mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream,volume,output,delayMs);
- ...................
- 2747}
[cpp] view plain copy
- 120status_tAudioPolicyCompatClient::setStreamVolume(
- 121AudioSystem::stream_typestream,
- 122floatvolume,
- 123audio_io_handle_toutput,
- 124intdelayMs)
- 125{
- 126returnmServiceOps->set_stream_volume(mService,(audio_stream_type_t)stream,
- 127volume,output,delayMs);
- 128}
[cpp] view plain copy
- 32classAudioPolicyCompatClient:publicAudioPolicyClientInterface{
- 33public:
- 34AudioPolicyCompatClient(structaudio_policy_service_ops*serviceOps,
- 35void*service):
- 36mServiceOps(serviceOps),
- mService(service){}
[cpp] view plain copy
- 1538structaudio_policy_service_opsaps_ops={
- 1539open_output:aps_open_output,
- 1540open_duplicate_output:aps_open_dup_output,
- 1541close_output:aps_close_output,
- 1542suspend_output:aps_suspend_output,
- 1543restore_output:aps_restore_output,
- 1544open_input:aps_open_input,
- 1545close_input:aps_close_input,
- 1546set_stream_volume:aps_set_stream_volume,
- 1547set_stream_output:aps_set_stream_output,
- 1548set_parameters:aps_set_parameters,
- 1549get_parameters:aps_get_parameters,
- 1550start_tone:aps_start_tone,
- 1551stop_tone:aps_stop_tone,
- 1552set_voice_volume:aps_set_voice_volume,
- 1553move_effects:aps_move_effects,
- 1554load_hw_module:aps_load_hw_module,
- 1555open_output_on_module:aps_open_output_on_module,
- 1556open_input_on_module:aps_open_input_on_module,
- 1557};
[cpp] view plain copy
- 1503staticintaps_set_stream_volume(void*service,audio_stream_type_tstream,
- 1504floatvolume,audio_io_handle_toutput,
- 1505intdelay_ms)
- 1506{
- 1507AudioPolicyService*audioPolicyService=(AudioPolicyService*)service;
- 1508
- 1509returnaudioPolicyService->setStreamVolume(stream,volume,output,
- 1510delay_ms);
- 1511}
[cpp] view plain copy
- 1006intAudioPolicyService::setStreamVolume(audio_stream_type_tstream,
- 1007floatvolume,
- 1008audio_io_handle_toutput,
- 1009intdelayMs)
- 1010{
- 1011return(int)mAudioCommandThread->volumeCommand(stream,volume,
- 1012output,delayMs);
- 1013}
[cpp] view plain copy
- 800status_tAudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_tstream,
- 801floatvolume,
- 802audio_io_handle_toutput,
- 803intdelayMs)
- 804{
- 805status_tstatus=NO_ERROR;
- 806
- 807AudioCommand*command=newAudioCommand();
- 808command->mCommand=SET_VOLUME;
- 809VolumeData*data=newVolumeData();
- 810data->mStream=stream;
- 811data->mVolume=volume;
- 812data->mIO=output;
- 813command->mParam=data;
- 814if(delayMs==0){
- 815command->mWaitStatus=true;
- 816}else{
- 817command->mWaitStatus=false;
- 818}
- 819Mutex::Autolock_l(mLock);
- 820insertCommand_l(command,delayMs);
- 821ALOGV("AudioCommandThread()addingsetvolumestream%d,volume%f,output%d",
- 822stream,volume,output);
- 823mWaitWorkCV.signal();
- 824if(command->mWaitStatus){
- 825command->mCond.wait(mLock);
- 826status=command->mStatus;
- 827mWaitWorkCV.signal();
- 828}
- 829returnstatus;
- 830}
[cpp] view plain copy
- boolAudioPolicyService::AudioCommandThread::threadLoop()
- {
- 681caseSET_VOLUME:{
- 682VolumeData*data=(VolumeData*)command->mParam;
- 683ALOGV("AudioCommandThread()processingsetvolumestream%d,\
- 684volume%f,output%d",data->mStream,data->mVolume,data->mIO);
- 685command->mStatus=AudioSystem::setStreamVolume(data->mStream,
- 686data->mVolume,
- 687data->mIO);
- 688if(command->mWaitStatus){
- 689command->mCond.signal();
- 690mWaitWorkCV.wait(mLock);
- 691}
- 692deletedata;
- 693}break;
- }
[cpp] view plain copy
- 123status_tAudioSystem::setStreamVolume(audio_stream_type_tstream,floatvalue,
- 124audio_io_handle_toutput)
- 125{
- 126if(uint32_t(stream)>=AUDIO_STREAM_CNT)returnBAD_VALUE;
- 127constsp<IAudioFlinger>&af=AudioSystem::get_audio_flinger();
- 128if(af==0)returnPERMISSION_DENIED;
- 129af->setStreamVolume(stream,value,output);
- 130returnNO_ERROR;
- 131}
[cpp] view plain copy
- 49constsp<IAudioFlinger>&AudioSystem::get_audio_flinger()
- 50{
- 51Mutex::Autolock_l(gLock);
- 52if(gAudioFlinger==0){
- 53sp<IServiceManager>sm=defaultServiceManager();
- 54sp<IBinder>binder;
- 55do{
- 56binder=sm->getService(String16("media.audio_flinger"));
- 57if(binder!=0)
- 58break;
- 59ALOGW("AudioFlingernotpublished,waiting...");
- 60usleep(500000);//0.5s
- 61}while(true);
- 62if(gAudioFlingerClient==NULL){
- 63gAudioFlingerClient=newAudioFlingerClient();
- 64}else{
- 65if(gAudioErrorCallback){
- 66gAudioErrorCallback(NO_ERROR);
- 67}
- 68}
- 69binder->linkToDeath(gAudioFlingerClient);
- 70gAudioFlinger=interface_cast<IAudioFlinger>(binder);
- 71gAudioFlinger->registerClient(gAudioFlingerClient);
- 72}
- 73ALOGE_IF(gAudioFlinger==0,"noAudioFlinger!?");
- 74
- 75returngAudioFlinger;
- 76}
[cpp] view plain copy
- 255virtualstatus_tsetStreamVolume(audio_stream_type_tstream,floatvalue,
- 256audio_io_handle_toutput)
- 257{
- 258Parceldata,reply;
- 259data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
- 260data.writeInt32((int32_t)stream);
- 261data.writeFloat(value);
- 262data.writeInt32((int32_t)output);
- 263remote()->transact(SET_STREAM_VOLUME,data,&reply);
- 264returnreply.readInt32();
- 265}
BnAudioFlinger::onTransact函数:
[cpp] view plain copy
- 780caseSET_STREAM_VOLUME:{
- 781CHECK_INTERFACE(IAudioFlinger,data,reply);
- 782intstream=data.readInt32();
- 783floatvolume=data.readFloat();
- 784audio_io_handle_toutput=(audio_io_handle_t)data.readInt32();
- 785reply->writeInt32(setStreamVolume((audio_stream_type_t)stream,volume,output));
- 786returnNO_ERROR;
[cpp] view plain copy
- 761status_tAudioFlinger::setStreamVolume(audio_stream_type_tstream,floatvalue,
- 762audio_io_handle_toutput)
- 763{
- 764//checkcallingpermissions
- 765if(!settingsAllowed()){
- 766returnPERMISSION_DENIED;
- 767}
- 769if(uint32_t(stream)>=AUDIO_STREAM_CNT){//定义在system/core/include/system/audio.h
- 771returnBAD_VALUE;
- 772}
- 774AutoMutexlock(mLock);
- 775PlaybackThread*thread=NULL;
- 776if(output){
- 777thread=checkPlaybackThread_l(output);
- 778if(thread==NULL){
- 779returnBAD_VALUE;
- 780}
- 781}
- 783mStreamTypes[stream].volume=value;
- 785if(thread==NULL){
- 786for(size_ti=0;i<mPlaybackThreads.size();i++){
- 787mPlaybackThreads.valueAt(i)->setStreamVolume(stream,value);
- 788}
- 789}else{
- 790thread->setStreamVolume(stream,value);
- 791}
- 793returnNO_ERROR;
- 794}
更多相关文章
- C语言函数的递归(上)
- Android(安卓)分辨率适配方法
- LocalBroadcastManager原理解析
- 自定义Dialog UI
- 安卓AIDL跨进程间通信
- Android(安卓)studio 点击按钮跳转到新的Activity
- 15个Android很有用的代码片段
- 修改版本号
- Android应用程序组件Content Provider在应用程序之间共享数据的