利於理解新Android聲音架構_ADROID 2.1 架構解析 10 聲音


參照Android2.1源碼理解:

10.1 音頻管理

文件: frameworks/base/media/java/android/media/AudioManager.java

10.1.1 音量控制

/**  @hide Default volume index values for audio streams */

    public static final int[] DEFAULT_STREAM_VOLUME = new int[] {

        4,  // STREAM_VOICE_CALL

         7,  // STREAM_SYSTEM

        5,  // STREAM_RING

        11, // STREAM_MUSIC

        6,  // STREAM_ALARM

        5,  // STREAM_NOTIFICATION

        7,  // STREAM_BLUETOOTH_SCO

        7,  // STREAM_SYSTEM_ENFORCED

        11, // STREAM_DTMF

        11  // STREAM_TTS

};

以上結構定義了默認音量。

public void adjustStreamVolume(int streamType, int direction, int flags) {

        IAudioService service = getService();

        try {

            service.adjustStreamVolume(streamType, direction, flags);

        } catch (RemoteException e) {

            Log.e(TAG, "Dead object in adjustStreamVolume", e);

        }

}

public void setStreamVolume(int streamType, int index, int flags) {

        IAudioService service = getService();

        try {

            service.setStreamVolume(streamType, index, flags);

        } catch (RemoteException e) {

            Log.e(TAG, "Dead object in setStreamVolume", e);

        }

    }

將音量控制參數轉給音頻服務去處理。

10.1.2 播放控制

@Deprecated public void setParameter(String key, String value) {

        setParameters(key+"="+value);

    }

    /**

     * Sets a variable number of parameter values to audio hardware.

     *

* @param keyValuePairs list of parameters key value pairs in the form:

     *    key1=value1;key2=value2;...

     *

     */

    public void setParameters(String keyValuePairs) {

        AudioSystem.setParameters(keyValuePairs);

}

cupcake 的播放控制采用的是 setrouting() 方式進行, eclaire 的播放控制改用 setParameter 方式,用參數設置的方式進行。

以上代碼的功能是:將參數傳給音頻系統去進行處理。

10.2 音頻服務

文件: frameworks/base/media/java/android/media/AudioService.java

10.2.1 音效

private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/";

    private static final String[] SOUND_EFFECT_FILES = new String[] {

        "Effect_Tick.ogg",

        "KeypressStandard.ogg",

        "KeypressSpacebar.ogg",

        "KeypressDelete.ogg",

        "KeypressReturn.ogg"

};

public boolean loadSoundEffects() {

        ...

String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + SOUND_EFFECT_FILES[SOUND_EFFECT_FILES_MAP[effect][0]];

              ...

        return true;

}

音效文件放在: /system/media/audio/ui/ 目錄下。

10.2.2 音量

public void setStreamVolume(int streamType, int index, int flags) {

        ensureValidStreamType(streamType);

        index = rescaleIndex(index * 10, streamType, STREAM_VOLUME_ALIAS[streamType]);

        setStreamVolumeInt(STREAM_VOLUME_ALIAS[streamType], index, false, true);

        // UI, etc.

        mVolumePanel.postVolumeChanged(streamType, flags);

        // Broadcast Intent

        sendVolumeUpdate(streamType);

}

private void setStreamVolumeInt(int streamType, int index, boolean force, boolean lastAudible) {

        VolumeStreamState streamState = mStreamStates[streamType];

        if (streamState.setIndex(index, lastAudible) || force) {

            // Post message to set system volume (it in turn will post a message

            // to persist). Do not change volume if stream is muted.

            if (streamState.muteCount() == 0) {

                 sendMsg(mAudioHandler, MSG_SET_SYSTEM_VOLUME, streamType, SENDMSG_NOOP, 0, 0,

                        streamState, 0);

            }

        }

}

private class AudioHandler extends Handler {

private void setStreamVolumeIndex(int stream, int index) {

         AudioSystem.setStreamVolumeIndex(s tream, (index + 5)/10);

}

        private void setSystemVolume(VolumeStreamState streamState) {

            // Adjust volume

            setStreamVolumeIndex (streamState.mStreamType, streamState.mIndex);

...

         }

public void handleMessage(Message msg) {

            int baseMsgWhat = getMsgBase(msg.what);

            switch (baseMsgWhat) {

                case MSG_SET_SYSTEM_VOLUME:

                    setSystemVolume((VolumeStreamState) msg.obj);

                     break;

...

音量設置通過消息發給消息處理,然后再轉向音頻系統進行處理:

AudioSystem.setStreamVolumeIndex

10.3 音頻系統

文件: frameworks/base/media/java/android/media/AudioSystem.java

10.3.1 音頻類型

* The audio stream for phone calls */

    public static final int STREAM_VOICE_CALL = 0;

     /* The audio stream for system sounds */

    public static final int STREAM_SYSTEM = 1;

    /* The audio stream for the phone ring and message alerts */

    public static final int STREAM_RING = 2;

    /* The audio stream for music playback */

    public static final int STREAM_MUSIC = 3;

    /* The audio stream for alarms */

    public static final int STREAM_ALARM = 4;

    /* The audio stream for notifications */

    public static final int STREAM_NOTIFICATION = 5;

    /* @hide The audio stream for phone calls when connected on bluetooth */

    public static final int STREAM_BLUETOOTH_SCO = 6;

    /* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */

    public static final int STREAM_SYSTEM_ENFORCED = 7;

    /* @hide The audio stream for DTMF tones */

     public static final int STREAM_DTMF = 8;

    /* @hide The audio stream for text to speech (TTS) */

public static final int STREAM_TTS = 9;

10.3.2 輸入輸出設備

// output devices

    public static final int DEVICE_OUT_EARPIECE = 0x1;

    public static final int DEVICE_OUT_SPEAKER = 0x2;

    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;

    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;

    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;

     public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;

    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;

    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;

    public static final int DEVICE_OUT_DEFAULT = 0x8000;

    // input devices

    public static final int DEVICE_IN_COMMUNICATION = 0x10000;

    public static final int DEVICE_IN_AMBIENT = 0x20000;

    public static final int DEVICE_IN_BUILTIN_MIC1 = 0x40000;

    public static final int DEVICE_IN_BUILTIN_MIC2 = 0x80000;

    public static final int DEVICE_IN_MIC_ARRAY = 0x100000;

    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x200000;

    public static final int DEVICE_IN_WIRED_HEADSET = 0x400000;

    public static final int DEVICE_IN_AUX_DIGITAL = 0x800000;

public static final int DEVICE_IN_DEFAULT = 0x80000000;

10.3.3 轉向底層處理

public static native int setParameters(String keyValuePairs);

public static native int setStreamVolumeIndex(int stream, int index);

對於參數設置和音量設置都轉向 C/C++ 文件進行處理。

10.4 音頻系統底層處理

文件: frameworks/base/media/libmedia/AudioSystem.cpp

10.4.1 音量設置

const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()

{

    ...

        sp<IServiceManager> sm = defaultServiceManager();

              ...

        binder = sm->getService(String16("media.audio_policy"));

           ...

}

status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)

{

    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();

    if (aps == 0) return PERMISSION_DENIED;

     return aps->setStreamVolumeIndex(stream, index);

}

將參數傳給音頻策略進行處理。

10.4.1 參數設置

const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()

{

    ...

        sp<IServiceManager> sm = defaultServiceManager();

       ..

            binder = sm->getService(String16("media.audio_flinger"));

        ...

}

status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {

    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();

    if (af == 0) return PERMISSION_DENIED;

    return af->setParameters(ioHandle, keyValuePairs);

}

將參數傳給音頻投擲進行處理。

主參數:

const char *AudioParameter::keyRouting = "routing";

const char *AudioParameter::keySamplingRate = "sampling_rate";

const char *AudioParameter::keyFormat = "format";

const char *AudioParameter::keyChannels = "channels";

const char *AudioParameter::keyFrameCount = "frame_count";

10.5 音頻策略

文件: frameworks/base/libs/audioflinger/AudioPolicyService.cpp

void AudioPolicyService::instantiate() {

    defaultServiceManager()->addService(

            String16("media.audio_policy"), new AudioPolicyService());

}

注冊名為 media.audio_policy 的服務。

AudioPolicyService::AudioPolicyService()

    : BnAudioPolicyService() , mpPolicyManager(NULL)

{

    ...

        mpPolicyManager = createAudioPolicyManager(this);

    ...

}

調用音頻策略管理器。

status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{

    ...

    return mpPolicyManager->setStreamVolumeIndex(stream, index);

}

將音量控制參數傳給音頻策略管理器。

10.6 音頻投擲

文件: frameworks/base/libs/audioflinger/AudioFlinger.cpp

void AudioFlinger::instantiate() {

    defaultServiceManager()->addService(

            String16("media.audio_flinger"), new AudioFlinger());

}

注冊名為 media.audio_flinger 的服務

AudioFlinger::AudioFlinger()

    : BnAudioFlinger(),

        mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)

{

    ...

mAudioHardware = AudioHardwareInterface::create();

...

}

status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)

{

    ...

        result = mAudioHardware->setParameters(keyValuePairs);

    ...

}

將參數轉向硬件接口去進行處理。

10.6.1 硬件接口

文件: frameworks/base/libs/audioflinger/AudioHardwareInterface.cpp

AudioHardwareInterface* AudioHardwareInterface::create()

{

   ...

   hw = createAudioHardware();

   ...

}

調用音頻設備。

10.7 音頻策略管理器

文件: hardware/msm7k/libaudio/AudioPolicyManager.cpp

extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)

{

    return new AudioPolicyManager(clientInterface);

}

status_t AudioPolicyManager::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{

   ...

    for (size_t i = 0; i < mOutputs.size(); i++) {

         status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());

        if (volStatus != NO_ERROR) {

            status = volStatus;

        }

    }

    return status;

}

設置音量。

10.8 音頻設備

文件: hardware/msm7k/libaudio/AudioHardware.cpp

extern "C" AudioHardwareInterface* createAudioHardware (void) {

    return new AudioHardware();

}

status_t AudioHardware::setParameters(const String8& keyValuePairs)

{

    AudioParameter param = AudioParameter(keyValuePairs);

    String8 value;

    String8 key;

...

    key = String8(BT_NAME_KEY);

    if (param.get(key, value) == NO_ERROR) {

       ...

    }

    return NO_ERROR;

}

處理 setParameters 參數。

10.1 音頻管理

文件: frameworks/base/media/java/android/media/AudioManager.java

10.1.1 音量控制

/**  @hide Default volume index values for audio streams */

    public static final int[] DEFAULT_STREAM_VOLUME = new int[] {

        4,  // STREAM_VOICE_CALL

         7,  // STREAM_SYSTEM

        5,  // STREAM_RING

        11, // STREAM_MUSIC

        6,  // STREAM_ALARM

        5,  // STREAM_NOTIFICATION

        7,  // STREAM_BLUETOOTH_SCO

        7,  // STREAM_SYSTEM_ENFORCED

        11, // STREAM_DTMF

        11  // STREAM_TTS

};

以上結構定義了默認音量。

public void adjustStreamVolume(int streamType, int direction, int flags) {

        IAudioService service = getService();

        try {

            service.adjustStreamVolume(streamType, direction, flags);

        } catch (RemoteException e) {

            Log.e(TAG, "Dead object in adjustStreamVolume", e);

        }

}

public void setStreamVolume(int streamType, int index, int flags) {

        IAudioService service = getService();

        try {

            service.setStreamVolume(streamType, index, flags);

        } catch (RemoteException e) {

            Log.e(TAG, "Dead object in setStreamVolume", e);

        }

    }

將音量控制參數轉給音頻服務去處理。

10.1.2 播放控制

@Deprecated public void setParameter(String key, String value) {

        setParameters(key+"="+value);

    }

    /**

     * Sets a variable number of parameter values to audio hardware.

     *

* @param keyValuePairs list of parameters key value pairs in the form:

     *    key1=value1;key2=value2;...

     *

     */

    public void setParameters(String keyValuePairs) {

        AudioSystem.setParameters(keyValuePairs);

}

cupcake 的播放控制采用的是 setrouting() 方式進行, eclaire 的播放控制改用 setParameter 方式,用參數設置的方式進行。

以上代碼的功能是:將參數傳給音頻系統去進行處理。

10.2 音頻服務

文件: frameworks/base/media/java/android/media/AudioService.java

10.2.1 音效

private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/";

    private static final String[] SOUND_EFFECT_FILES = new String[] {

        "Effect_Tick.ogg",

        "KeypressStandard.ogg",

        "KeypressSpacebar.ogg",

        "KeypressDelete.ogg",

        "KeypressReturn.ogg"

};

public boolean loadSoundEffects() {

        ...

String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + SOUND_EFFECT_FILES[SOUND_EFFECT_FILES_MAP[effect][0]];

              ...

        return true;

}

音效文件放在: /system/media/audio/ui/ 目錄下。

10.2.2 音量

public void setStreamVolume(int streamType, int index, int flags) {

        ensureValidStreamType(streamType);

        index = rescaleIndex(index * 10, streamType, STREAM_VOLUME_ALIAS[streamType]);

        setStreamVolumeInt(STREAM_VOLUME_ALIAS[streamType], index, false, true);

        // UI, etc.

        mVolumePanel.postVolumeChanged(streamType, flags);

        // Broadcast Intent

        sendVolumeUpdate(streamType);

}

private void setStreamVolumeInt(int streamType, int index, boolean force, boolean lastAudible) {

        VolumeStreamState streamState = mStreamStates[streamType];

        if (streamState.setIndex(index, lastAudible) || force) {

            // Post message to set system volume (it in turn will post a message

            // to persist). Do not change volume if stream is muted.

            if (streamState.muteCount() == 0) {

                 sendMsg(mAudioHandler, MSG_SET_SYSTEM_VOLUME, streamType, SENDMSG_NOOP, 0, 0,

                        streamState, 0);

            }

        }

}

private class AudioHandler extends Handler {

private void setStreamVolumeIndex(int stream, int index) {

         AudioSystem.setStreamVolumeIndex(s tream, (index + 5)/10);

}

        private void setSystemVolume(VolumeStreamState streamState) {

            // Adjust volume

            setStreamVolumeIndex (streamState.mStreamType, streamState.mIndex);

...

         }

public void handleMessage(Message msg) {

            int baseMsgWhat = getMsgBase(msg.what);

            switch (baseMsgWhat) {

                case MSG_SET_SYSTEM_VOLUME:

                    setSystemVolume((VolumeStreamState) msg.obj);

                     break;

...

音量設置通過消息發給消息處理,然后再轉向音頻系統進行處理:

AudioSystem.setStreamVolumeIndex

10.3 音頻系統

文件: frameworks/base/media/java/android/media/AudioSystem.java

10.3.1 音頻類型

* The audio stream for phone calls */

    public static final int STREAM_VOICE_CALL = 0;

     /* The audio stream for system sounds */

    public static final int STREAM_SYSTEM = 1;

    /* The audio stream for the phone ring and message alerts */

    public static final int STREAM_RING = 2;

    /* The audio stream for music playback */

    public static final int STREAM_MUSIC = 3;

    /* The audio stream for alarms */

    public static final int STREAM_ALARM = 4;

    /* The audio stream for notifications */

    public static final int STREAM_NOTIFICATION = 5;

    /* @hide The audio stream for phone calls when connected on bluetooth */

    public static final int STREAM_BLUETOOTH_SCO = 6;

    /* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */

    public static final int STREAM_SYSTEM_ENFORCED = 7;

    /* @hide The audio stream for DTMF tones */

     public static final int STREAM_DTMF = 8;

    /* @hide The audio stream for text to speech (TTS) */

public static final int STREAM_TTS = 9;

10.3.2 輸入輸出設備

// output devices

    public static final int DEVICE_OUT_EARPIECE = 0x1;

    public static final int DEVICE_OUT_SPEAKER = 0x2;

    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;

    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;

    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;

     public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;

    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;

    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;

    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;

    public static final int DEVICE_OUT_DEFAULT = 0x8000;

    // input devices

    public static final int DEVICE_IN_COMMUNICATION = 0x10000;

    public static final int DEVICE_IN_AMBIENT = 0x20000;

    public static final int DEVICE_IN_BUILTIN_MIC1 = 0x40000;

    public static final int DEVICE_IN_BUILTIN_MIC2 = 0x80000;

    public static final int DEVICE_IN_MIC_ARRAY = 0x100000;

    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x200000;

    public static final int DEVICE_IN_WIRED_HEADSET = 0x400000;

    public static final int DEVICE_IN_AUX_DIGITAL = 0x800000;

public static final int DEVICE_IN_DEFAULT = 0x80000000;

10.3.3 轉向底層處理

public static native int setParameters(String keyValuePairs);

public static native int setStreamVolumeIndex(int stream, int index);

對於參數設置和音量設置都轉向 C/C++ 文件進行處理。

10.4 音頻系統底層處理

文件: frameworks/base/media/libmedia/AudioSystem.cpp

10.4.1 音量設置

const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()

{

    ...

        sp<IServiceManager> sm = defaultServiceManager();

              ...

        binder = sm->getService(String16("media.audio_policy"));

           ...

}

status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)

{

    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();

    if (aps == 0) return PERMISSION_DENIED;

     return aps->setStreamVolumeIndex(stream, index);

}

將參數傳給音頻策略進行處理。

10.4.1 參數設置

const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()

{

    ...

        sp<IServiceManager> sm = defaultServiceManager();

       ..

            binder = sm->getService(String16("media.audio_flinger"));

        ...

}

status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {

    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();

    if (af == 0) return PERMISSION_DENIED;

    return af->setParameters(ioHandle, keyValuePairs);

}

將參數傳給音頻投擲進行處理。

主參數:

const char *AudioParameter::keyRouting = "routing";

const char *AudioParameter::keySamplingRate = "sampling_rate";

const char *AudioParameter::keyFormat = "format";

const char *AudioParameter::keyChannels = "channels";

const char *AudioParameter::keyFrameCount = "frame_count";

10.5 音頻策略

文件: frameworks/base/libs/audioflinger/AudioPolicyService.cpp

void AudioPolicyService::instantiate() {

    defaultServiceManager()->addService(

            String16("media.audio_policy"), new AudioPolicyService());

}

注冊名為 media.audio_policy 的服務。

AudioPolicyService::AudioPolicyService()

    : BnAudioPolicyService() , mpPolicyManager(NULL)

{

    ...

        mpPolicyManager = createAudioPolicyManager(this);

    ...

}

調用音頻策略管理器。

status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{

    ...

    return mpPolicyManager->setStreamVolumeIndex(stream, index);

}

將音量控制參數傳給音頻策略管理器。

10.6 音頻投擲

文件: frameworks/base/libs/audioflinger/AudioFlinger.cpp

void AudioFlinger::instantiate() {

    defaultServiceManager()->addService(

            String16("media.audio_flinger"), new AudioFlinger());

}

注冊名為 media.audio_flinger 的服務

AudioFlinger::AudioFlinger()

    : BnAudioFlinger(),

        mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)

{

    ...

mAudioHardware = AudioHardwareInterface::create();

...

}

status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)

{

    ...

        result = mAudioHardware->setParameters(keyValuePairs);

    ...

}

將參數轉向硬件接口去進行處理。

10.6.1 硬件接口

文件: frameworks/base/libs/audioflinger/AudioHardwareInterface.cpp

AudioHardwareInterface* AudioHardwareInterface::create()

{

   ...

   hw = createAudioHardware();

   ...

}

調用音頻設備。

10.7 音頻策略管理器

文件: hardware/msm7k/libaudio/AudioPolicyManager.cpp

extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)

{

    return new AudioPolicyManager(clientInterface);

}

status_t AudioPolicyManager::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{

   ...

    for (size_t i = 0; i < mOutputs.size(); i++) {

         status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());

        if (volStatus != NO_ERROR) {

            status = volStatus;

        }

    }

    return status;

}

設置音量。

10.8 音頻設備

文件: hardware/msm7k/libaudio/AudioHardware.cpp

extern "C" AudioHardwareInterface* createAudioHardware (void) {

    return new AudioHardware();

}

status_t AudioHardware::setParameters(const String8& keyValuePairs)

{

    AudioParameter param = AudioParameter(keyValuePairs);

    String8 value;

    String8 key;

...

    key = String8(BT_NAME_KEY);

    if (param.get(key, value) == NO_ERROR) {

       ...

    }

    return NO_ERROR;

}

處理 setParameters 參數。

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com