Android Framework Audio Subsystem (02) Audio System Framework

This series of articles Master link: Thematic sub-directory Android Framework Class Audio Subsystem


Summary and description of key points in this chapter:

This chapter mainly focuses on ➕ the audio system framework part below the above mind map. Mainly has a basic understanding of the audio system framework.


1 Introduction to Android Audio System Framework

The entire frame diagram of the audio system is as follows:

For this frame picture. Explain several key audio classes:

  1. AudioFlinger: Receive data from multiple apps, merge and deliver them; be the executor of the strategy, such as how to communicate with audio devices, how to maintain the audio devices in the existing system, and how to handle the mixing of multiple audio streams, etc. It must be done by it.
  2. AudioPolicyService: Decide which device to choose for output, connect the headset for headphones, and connect the Bluetooth device for Bluetooth; it is the policy maker, such as when to open the audio interface device, which device corresponds to a certain stream type of audio, etc.
  3. AudioTrack: An agent, APP accesses AudioFlinger through AudioTrack (binder communication, divided into Java layer and C ++ layer).

Next, from the perspective of AndioTrack, AudioFlinger, AudioHAL, interpret the Audio framework in more detail

From this figure we must understand the following basic knowledge:

  1. One sound card corresponds to one or more devices. (Device refers to: speakers, microphones, headphones, etc.)
  2. One or more devices correspond to one device node.
  3. One or more device nodes (such as / dev / snd / pcmc0d0p) correspond to an output, and output is a combination of multiple devices; these devices belong to different ports on the same hardware. These devices support the same parameters, sampling rate, and channels.
  4. One output corresponds to one thread thread.
  5. There are one or more tracks in a thread. When playing a sound, there are multiple sound sources.
  6. One track corresponds to one / multiple AudioTrack.

2 Basic Concepts of Audio Framework System

@ 1 device: For example, the speakers on the sound card, headphones or Bluetooth, etc., these are called devices. The output device device in the Java layer AudioSystem is defined as follows:

public class AudioSystem
{
    //...
    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; //蓝牙,面向 SCO方式,主要用于话音传输
    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;		//HDMI相关
	//...
}

@ 2 output: For ease of management, a group of devices with the same parameters on a device is called output. It describes some actually supported devices (with actual hardware)

@ 3 module: A hardware module is used to access the hardware (sound card) in the Android system. The module is a hardware operation library. Which output can a module support and which devices can an output support? Use the configuration file /system/etc/audio_policy.conf to description

@ 4 profile: configuration, used to describe the output, which devices can be supported (only supported logically, not necessarily with actual hardware, such as headset output is supported for a long time after plugging in the headset, it is not supported without the headset, but the profile has always been supported) .

@ 5 out flag: For example, for a professional APP, it only plays sound from HDMI. At this time, you can specify the out flag as AUDIO_OUTPUT_FLAG_DIRECT, which will cause the final sound to be output directly to the corresponding device without mixing.

@ 6 stream type: app wants to play sounds, you need to specify the sound type, these sound types are called stream type. Its value is implemented in the C ++ layer (AudioTrack.cpp) as follows:

typedef enum {
    /* These values must kept in sync with
     * frameworks/base/media/java/android/media/AudioSystem.java
     */
    AUDIO_STREAM_DEFAULT          = -1,
    AUDIO_STREAM_MIN              = 0,
    AUDIO_STREAM_VOICE_CALL       = 0,//电话语音
    AUDIO_STREAM_SYSTEM           = 1,//系统声音
    AUDIO_STREAM_RING             = 2,//铃声 声音
    AUDIO_STREAM_MUSIC            = 3,//音乐铃声
    AUDIO_STREAM_ALARM            = 4,//警告音
    AUDIO_STREAM_NOTIFICATION     = 5,//通知音
    AUDIO_STREAM_BLUETOOTH_SCO    = 6,
    AUDIO_STREAM_ENFORCED_AUDIBLE = 7, /* Sounds that cannot be muted by user
                                        * and must be routed to speaker
                                        */
    AUDIO_STREAM_DTMF             = 8,//DTMF 键盘拨号音
    AUDIO_STREAM_TTS              = 9,  /* Transmitted Through Speaker.
                                         * Plays over speaker only, silent on other devices.
                                         */
    AUDIO_STREAM_ACCESSIBILITY    = 10, /* For accessibility talk back prompts */
    AUDIO_STREAM_REROUTING        = 11, /* For dynamic policy output mixes */
    AUDIO_STREAM_PATCH            = 12, /* For internal audio flinger tracks. Fixed volume */
    AUDIO_STREAM_PUBLIC_CNT       = AUDIO_STREAM_TTS + 1,
    AUDIO_STREAM_CNT              = AUDIO_STREAM_PATCH + 1,
} audio_stream_type_t;

Its value is implemented in the Java (AudioTrack.java) layer as follows:

public class AudioSystem
{
    /* These values must be kept in sync with system/audio.h */
    /*
     * If these are modified, please also update Settings.System.VOLUME_SETTINGS
     * and attrs.xml and AudioManager.java.
     */
    /* The default audio stream */
    public static final int STREAM_DEFAULT = -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;//DTMF键盘拨号音
    /* @hide The audio stream for text to speech (TTS) */
    public static final int STREAM_TTS = 9;
    /**
     * @deprecated Use {@link #numStreamTypes() instead}
     */
    public static final int NUM_STREAMS = 5;
	//...
}

@ 7 strategy: There are many types of stream type, depending on which type (strategy) it belongs to use strategy. At the same time, it is also necessary to determine what device to play based on strategy, whether it is a speaker, headphones or Bluetooth. Set output according to device. The acquisition method of strategy is implemented as follows:

AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
        AudioSystem::stream_type stream) {
    // stream to strategy mapping
    switch (stream) {
    case AudioSystem::VOICE_CALL:
    case AudioSystem::BLUETOOTH_SCO:
        return STRATEGY_PHONE;
    case AudioSystem::RING:
    case AudioSystem::ALARM:
        return STRATEGY_SONIFICATION;
    case AudioSystem::NOTIFICATION:
        return STRATEGY_SONIFICATION_RESPECTFUL;
    case AudioSystem::DTMF:
        return STRATEGY_DTMF;
    default:
        ALOGE("unknown stream type");
    case AudioSystem::SYSTEM:
        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
        // while key clicks are played produces a poor result
    case AudioSystem::TTS:
    case AudioSystem::MUSIC:
        return STRATEGY_MEDIA;
    case AudioSystem::ENFORCED_AUDIBLE:
        return STRATEGY_ENFORCED_AUDIBLE;
    }
}

@ 8 policy: How a stream finally selects a device, how do these streams affect each other (a high-priority sound will silence other sounds), etc., collectively referred to as policy.

@ 9 Several common terms in audio output devices:

  • headset: indicates both earpiece and Mic
  • headPhone: indicates that there is only earpiece, no Mic
  • lineOut: is to output analog signals to the speaker device

3 Brief description of the documents involved

The code above is mainly divided into 3 parts, AudioFlinger, AudioPolicyService, application part (Java part & C ++ part)

The relevant files and directory locations of some AudioFlinger are as follows:

  • AudioFlinger.cpp (frameworks / av / services / audioflinger / AudioFlinger.cpp)
  • Threads.cpp         (frameworks/av/services/audioflinger/Threads.cpp)
  • Tracks.cpp            (frameworks/av/services/audioflinger/Tracks.cpp)
  • audio_hw_hal.cpp (hardware / libhardware_legacy / audio / Audio_hw_hal.cpp)
  • AudioHardware.cpp (device/friendly-arm/common/libaudio/AudioHardware.cpp)

The relevant files and directory locations of the AudioPolicyService section are as follows:

  • AudioPolicyService.cpp      (frameworks/av/services/audiopolicy/AudioPolicyService.cpp)
  • AudioPolicyClientImpl.cpp  (frameworks/av/services/audiopolicy/AudioPolicyClientImpl.cpp)
  • AudioPolicyInterfaceImpl.cpp (frameworks/av/services/audiopolicy/AudioPolicyInterfaceImpl.cpp)
  • AudioPolicyManager.cpp    (frameworks/av/services/audiopolicy/AudioPolicyManager.cpp)

The relevant files and directory locations of the application APP are as follows:

  • AudioTrack.java     (frameworks/base/media/java/android/media/AudioTrack.java)
  • android_media_AudioTrack.cpp (frameworks/base/core/jni/android_media_AudioTrack.cpp)
  • AudioTrack.cpp      (frameworks/av/media/libmedia/AudioTrack.cpp)
  • AudioSystem.cpp (frameworks / av / media / libmedia / AudioSystem.cpp)
Published 289 original articles · praised 47 · 30,000+ views

Guess you like

Origin blog.csdn.net/vviccc/article/details/105265359