Android audio切换设备通道流程

来源:互联网 发布:php pack 转换c 编辑:程序博客网 时间:2024/06/15 20:17
Android audio切换设备通道流程
1.frameworks/base/media/java/android/media/AudioManager.java
  public void setMode(int mode) {                                                                                                                                                                           
       IAudioService service = getService();                                                                                                                                                                 
       try {                                                                                                                                                                                                 
            service.setMode(mode, mICallBack, mApplicationContext.getOpPackageName());                                                                                                                        
        } catch (RemoteException e) {                                                                                                                                                                         
            Log.e(TAG, "Dead object in setMode", e);                                                                                                                                                          
        }                                                                                                                                                                                                     
    }  


2.frameworks/base/services/core/java/com/android/server/audio/AudioService.java
  <1>.public void setMode(int mode, IBinder cb, String callingPackage) { 
        newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage);
  }
  <2>.private int setModeInt(int mode, IBinder cb, int pid, String caller) { 
        status = AudioSystem.setPhoneState(mode);
  }


3.frameworks/base/media/java/android/media/AudioSystem.java 
  public static native int setPhoneState(int state); 


4.frameworks/base/core/jni/android_media_AudioSystem.cpp
  <1>.static JNINativeMethod gMethods[] = {
       {"setPhoneState", "(I)I", (void *)android_media_AudioSystem_setPhoneState},
     };
  <2>.static jint android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state){
      return (jint) check_AudioSystem_Command(AudioSystem::setPhoneState((audio_mode_t) state)); 
    }


5.frameworks/av/media/libmedia/AudioSystem.cpp
  status_t AudioSystem::setPhoneState(audio_mode_t state)                                                                                                                                                       
  {                                                                                                                                                                                                             
     if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;                                                                                                                                                  
     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();                                                                                                                             
     if (aps == 0) return PERMISSION_DENIED;                                                                                                                                                                   
                                                                                                                                                                                                               
     return aps->setPhoneState(state);                                                                                                                                                                         
 }


6.frameworks/av/include/media/IAudioPolicyService.h
  class IAudioPolicyService : public IInterface{ 
    public:
          DECLARE_META_INTERFACE(AudioPolicyService);
  };


7.frameworks/native/include/binder/IInterface.h:
  #include <media/IAudioPolicyServiceClient.h>


  #define DECLARE_META_INTERFACE(INTERFACE)                               \                                                                                                                                      
     static const android::String16 descriptor;                          \                                                                                                                                      
     static android::sp<I##INTERFACE> asInterface(                       \                                                                                                                                      
             const android::sp<android::IBinder>& obj);                  \                                                                                                                                      
     virtual const android::String16& getInterfaceDescriptor() const;    \                                                                                                                                      
     I##INTERFACE();                                                     \                                                                                                                                      
     virtual ~I##INTERFACE(); 
8.frameworks/av/services/audiopolicy/service/AudioPolicyInterfaceImplLegacy.cpp
   #include "AudioPolicyService.h"
   status_t AudioPolicyService::setPhoneState(audio_mode_t state){
       AudioSystem::setMode(state); 
       mpAudioPolicy->set_phone_state(mpAudioPolicy, state);//给毁掉函数传state值
       mPhoneState = state;
   }
9.定义和使用
  <1>.hardware/libhardware/include/hardware/audio_policy.h
  //定义回调 
  void (*set_phone_state)(struct audio_policy *pol, audio_mode_t state);


  <2>hardware/libhardware/modules/audio/audio_policy.c
  static int create_legacy_ap(const struct audio_policy_device *device,                                                                                                                                          
                            struct audio_policy_service_ops *aps_ops,                                                                                                                                          
                            void *service,                                                                                                                                                                     
                            struct audio_policy **ap)                                                                                                                                                          
  {
  lap->policy.set_phone_state = ap_set_phone_state;//回调
  }
  
  static void ap_set_phone_state(struct audio_policy *pol, audio_mode_t state)                                                                                                                                   
 {                                                                                                                                                                                                              
     struct legacy_audio_policy *lap = to_lap(pol);                                                                                                                                                             
     // as this is the legacy API, don't change it to use audio_mode_t instead of int                                                                                                                           
     lap->apm->setPhoneState((int) state);                                                                                                                                                                      
 }


10.定义和使用 
  <1>.hardware/qcom/audio/policy_hal/AudioPolicyManager.h
      virtual void setPhoneState(audio_mode_t state); 


  <2>.hardware/libhardware_legacy/include/hardware_legacy/AudioPolicyManagerBase.h
       enum routing_strategy {                                                                                                                                                                                
            STRATEGY_MEDIA,                                                                                                                                                                                    
            STRATEGY_PHONE,                                                                                                                                                                                    
            STRATEGY_SONIFICATION,                                                                                                                                                                             
            STRATEGY_SONIFICATION_RESPECTFUL,                                                                                                                                                                  
            STRATEGY_DTMF,                                                                                                                                                                                     
            STRATEGY_ENFORCED_AUDIBLE,                                                                                                                                                                         
            NUM_STRATEGIES                                                                                                                                                                                     
        };
      
  <3>.hardware/qcom/audio/policy_hal/AudioPolicyManager.cpp
    (1)void AudioPolicyManagerCustom::setPhoneState(audio_mode_t state){
           
        setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));                                                                                                                                         
        setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));                                                                                                                                    
        setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));                                                                                                                                  
        setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
     }
   (2)audio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,bool fromCache)                                                                                                                                  
       {
case STRATEGY_SONIFICATION:                                                                                                                                                                               
                                                                                                                                                                                                              
        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by                                                                                                           
        // handleIncallSonification().                                                                                                                                                                        
        if (isInCall()) {                                                                                                                                                                                     
            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);                                                                                                                               
            break;                                                                                                                                                                                            
        }                                                                                                                                                                                                     
       }

阅读全文
0 0