Android的Audio音频系统

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

音频系统JNIJNIJNI初始化在system_init(运行在Simulator上)或者main_Mediaserver中,AudioFlinger被创建,会生成一个AudioHardwareInterface实例(Android定义的音频设备的一个抽象层),并且初始化音频系统的模式和路由信息如下:mHardwareStatus=AUDIO_HW_IDLE;mAudioHardware=AudioHardwareInterface::create();mHardwareStatus=AUDIO_HW_INIT;if(mAudioHardware-initCheck()==NO_ERROR){//open16-bitoutputstreamfors/wmixermHardwareStatus=AUDIO_HW_OUTPUT_OPEN;mOutput=mAudioHardware-openOutputStream(AudioSystem::PCM_16_BIT);mHardwareStatus=AUDIO_HW_IDLE;if(mOutput){mSampleRate=mOutput-sampleRate();mChannelCount=mOutput-channelCount();mFormat=mOutput-format();mMixBufferSize=mOutput-bufferSize();mFrameCount=mMixBufferSize/mChannelCount/sizeof(int16_t);mMixBuffer=newint16_t[mFrameCount*mChannelCount];memset(mMixBuffer,0,mMixBufferSize);mAudioMixer=newAudioMixer(mFrameCount,mSampleRate);//FIXME-thisshouldcomefromsettingssetMasterVolume(1.0f);setRouting(AudioSystem::MODE_NORMAL,AudioSystem::ROUTE_SPEAKER,AudioSystem::ROUTE_ALL);setRouting(AudioSystem::MODE_RINGTONE,AudioSystem::ROUTE_SPEAKER,AudioSystem::ROUTE_ALL);setRouting(AudioSystem::MODE_IN_CALL,AudioSystem::ROUTE_EARPIECE,AudioSystem::ROUTE_ALL);setMode(AudioSystem::MODE_NORMAL);mMasterMute=false;}else{LOGE(Failedtoinitializeoutputstream);}}else{LOGE(Couldn'teveninitializethestubbedaudiohardware!);}在SystemServer启动的时候,会生成一个AudioService的实例,try{Log.i(TAG,StartingAudioService);ServiceManager.addService(Context.AUDIO_SERVICE,newAudioService(context));}catch(Throwablee){Log.e(TAG,FailurestartingVolumeService,e);}AudioService的构造函数会读取一些关于音频的配置信息,比如Ringer和vibrate信息,privatevoidreadPersistedSettings(){finalContentResolvercr=mContentResolver;mRingerMode=System.getInt(cr,System.MODE_RINGER,AudioManager.RINGER_MODE_NORMAL);mRingerModeAffectedStreams=System.getInt(mContentResolver,System.MODE_RINGER_STREAMS_AFFECTED,1AudioSystem.STREAM_RING);mVibrateSetting=System.getInt(cr,System.VIBRATE_ON,0);mMuteAffectedStreams=System.getInt(cr,System.MUTE_STREAMS_AFFECTED,((1AudioSystem.STREAM_MUSIC)|(1AudioSystem.STREAM_RING)|(1AudioSystem.STREAM_SYSTEM)));//Eachstreamwillreaditsownpersistedsettings//BroadcastthestickyintentbroadcastRingerMode();//BroadcastvibratesettingsbroadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION);}同时也会从底层音频系统读取模式和路由信息:privatevoidreadAudioSettings(){synchronized(mSettingsLock){mMicMute=AudioSystem.isMicrophoneMuted();mMode=AudioSystem.getMode();for(intmode=0;modeAudioSystem.NUM_MODES;mode++){mRoutes[mode]=AudioSystem.getRouting(mode);}}}在AudioSystem.cpp第一次调用get_audio_flinger成功后,它会通过binder来监听运行在media_server进程中的AudioFlinger是否活着。//establishbinderinterfacetoAudioFlingerserviceconstspIAudioFlinger&AudioSystem::get_audio_flinger(){Mutex::Autolock_l(gLock);if(gAudioFlinger.get()==0){spIServiceManagersm=defaultServiceManager();spIBinderbinder;do{binder=sm-getService(String16(media.audio_flinger));if(binder!=0)break;LOGW(AudioFlingernotpublished,waiting...);usleep(500000);//0.5s}while(true);if(gDeathNotifier==NULL){gDeathNotifier=newDeathNotifier();}else{if(gAudioErrorCallback){gAudioErrorCallback(NO_ERROR);}}binder-linkToDeath(gDeathNotifier);gAudioFlinger=interface_castIAudioFlinger(binder);}LOGE_IF(gAudioFlinger==0,noAudioFlinger!?);returngAudioFlinger;}到此,整个音频系统初始化完毕。重新启动如果AudioFlinger运行的media_server进程异常死掉,AudioSystem会收到一个事件通知,voidAudioSystem::DeathNotifier::binderDied(constwpIBinder&who){Mutex::Autolock_l(AudioSystem::gLock);AudioSystem::gAudioFlinger.clear();if(gAudioErrorCallback){gAudioErrorCallback(DEAD_OBJECT);}LOGW(AudioFlingerserverdied!);}从而调用android_media_AudioSystem.cpp注册下来的回调函数,该函数又是通过JNI来调用AudioService.java注册下来的回调函数,在该函数中会发送MSG_MEDIA_SERVER_DIED消息,AudioService会监听这个消息,这样AudioService就能知道AudioFlinger已不工作,它就接着调用getMode来尝试连接到重启后的AudioFlinger。caseMSG_MEDIA_SERVER_DIED:Log.e(TAG,Mediaserverdied.);//ForcecreationofnewIAudioflingerinterfacemMediaServerOk=false;AudioSystem.getMode();break;当连接成功后,AudioFlinger会调用android_media_AudioSystem.cpp注册下来的回调函数,该函数又是通过JNI来调用AudioService.java注册下来的回调函数,在该函数中会发送MSG_MEDIA_SERVER_STARTED消息。接着AudioService就去配置底层音频系统,包括模式、路由、每一路流的音量大小和Ringer状态。caseMSG_MEDIA_SERVER_STARTED://RestoreaudioroutingandstreamvolumesapplyAudioSettings();for(intstreamType=AudioSystem.NUM_STREAMS-1;streamType=0;streamType--){intvolume;VolumeStreamStatestreamState=mStreamStates[streamType];if(streamState.muteCount()==0){volume=streamState.mVolumes[streamState.mIndex];}else{volume=streamState.mVolumes[0];}AudioSystem.setVolume(streamType,volume);}setRingerMode();Note:AudioSystem的Native实现在device/libs/android_runtime/android_media_AudioSystem.cpp中。模式初始的时候音频系统是处于MODE_NORMAL模式的,下面是其模式状态变迁图:问题:当一个Ringtone放完了之后,理论上系统是否要自动切换回NORMAL模式而不是必须要主动调用stopRing?我没找到相关code。路由信息1.当HeadsetObserver检测到有耳机插上来的时候,它会把音频系统的路由设置成均使用该耳机;当耳机被拔下来后,它会把音频系统的路由设置成缺省配置(即都通过扬声器)。privatesynchronizedfinalvoidupdate(StringnewName,intnewState){if(newName!=mHeadsetName||newState!=mHeadsetState){mHeadsetName=newName;mHeadsetState=newState;AudioManageraudioManager=(AudioManager)mContext.getSys

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功