深入剖析Android音频之AudioTrack

转自:http://www.it165.net/pro/html/201410/23660.html

  • 播放声音可以用MediaPlayer和AudioTrack,两者都提供了java API供应用开发者使用。虽然都可以播放声音,但两者还是有很大的区别的。其中最大的区别是MediaPlayer可以播放多种格式的声音文件,例如MP3,AAC,WAV,OGG,MIDI等。MediaPlayer会在framework层创建对应的音频解码器。而AudioTrack只能播放已经解码的PCM流,如果是文件的话只支持wav格式的音频文件,因为wav格式的音频文件大部分都是PCM流。AudioTrack不创建解码器,所以只能播放不需要解码的wav文件。当然两者之间还是有紧密的联系,MediaPlayer在framework层还是会创建AudioTrack,把解码后的PCM数流传递给AudioTrack,AudioTrack再传递给AudioFlinger进行混音,然后才传递给硬件播放,所以是MediaPlayer包含了AudioTrack。使用AudioTrack播放音乐示例:

    01. AudioTrack audio = new AudioTrack(
    02. AudioManager.STREAM_MUSIC, // 指定流的类型
    03. 32000, // 设置音频数据的采样率 32k,如果是44.1k就是44100
    04. AudioFormat.CHANNEL_OUT_STEREO, // 设置输出声道为双声道立体声,而CHANNEL_OUT_MONO类型是单声道
    05. AudioFormat.ENCODING_PCM_16BIT, // 设置音频数据块是8位还是16位,这里设置为16位。好像现在绝大多数的音频都是16位的了
    06. AudioTrack.MODE_STREAM // 设置模式类型,在这里设置为流类型,另外一种MODE_STATIC貌似没有什么效果
    07. );
    08. audio.play(); // 启动音频设备,下面就可以真正开始音频数据的播放了
    09. // 打开mp3文件,读取数据,解码等操作省略 ...
    10. byte[] buffer = new buffer[4096];
    11. int count;
    12. while(true)
    13. {
    14. // 最关键的是将解码后的数据,从缓冲区写入到AudioTrack对象中
    15. audio.write(buffer, 0, 4096);
    16. if(文件结束) break;
    17. }
    18. //关闭并释放资源
    19. audio.stop();
    20. audio.release();

    AudioTrack构造过程

     

    每一个音频流对应着一个AudioTrack类的一个实例,每个AudioTrack会在创建时注册到 AudioFlinger中,由AudioFlinger把所有的AudioTrack进行混合(Mixer),然后输送到AudioHardware中进行播放,目前Android同时最多可以创建32个音频流,也就是说,Mixer最多会同时处理32个AudioTrack的数据流。

    frameworksasemediajavaandroidmediaAudioTrack.java

    01. /**
    02. * streamType:音频流类型
    03. * sampleRateInHz:采样率
    04. * channelConfig:音频声道
    05. * audioFormat:音频格式
    06. * bufferSizeInBytes缓冲区大小:
    07. * mode:音频数据加载模式
    08. * sessionId:会话id
    09. */
    10. public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
    11. int bufferSizeInBytes, int mode, int sessionId)
    12. throws IllegalArgumentException {
    13. // mState already == STATE_UNINITIALIZED
    14.  
    15. // remember which looper is associated with the AudioTrack instantiation
    16. Looper looper;
    17. if ((looper = Looper.myLooper()) == null) {
    18. looper = Looper.getMainLooper();
    19. }
    20. mInitializationLooper = looper;
    21. /**
    22. * 参数检查
    23. * 1.检查streamType是否为:STREAM_ALARM、STREAM_MUSIC、STREAM_RING、STREAM_SYSTEM、STREAM_VOICE_CALL、
    24. *  STREAM_NOTIFICATION、STREAM_BLUETOOTH_SCO、STREAM_BLUETOOTH_SCO,并赋值给mStreamType
    25. * 2.检查sampleRateInHz是否在4000到48000之间,并赋值给mSampleRate
    26. * 3.设置mChannels:
    27. *      CHANNEL_OUT_DEFAULT、CHANNEL_OUT_MONO、CHANNEL_CONFIGURATION_MONO ---> CHANNEL_OUT_MONO
    28. *      CHANNEL_OUT_STEREO、CHANNEL_CONFIGURATION_STEREO                  ---> CHANNEL_OUT_STEREO
    29. * 4.设置mAudioFormat:
    30. *      ENCODING_PCM_16BIT、ENCODING_DEFAULT ---> ENCODING_PCM_16BIT
    31. *      ENCODING_PCM_8BIT ---> ENCODING_PCM_8BIT
    32. * 5.设置mDataLoadMode:
    33. *      MODE_STREAM
    34. *      MODE_STATIC
    35. */
    36. audioParamCheck(streamType, sampleRateInHz, channelConfig, audioFormat, mode);
    37. /**
    38. * buffer大小检查,计算每帧字节大小,如果是ENCODING_PCM_16BIT,则为mChannelCount * 2
    39. * mNativeBufferSizeInFrames为帧数
    40. */
    41. audioBuffSizeCheck(bufferSizeInBytes);
    42. if (sessionId < 0) {
    43. throw new IllegalArgumentException("Invalid audio session ID: "+sessionId);
    44. }
    45. //进入native层初始化
    46. int[] session = new int[1];
    47. session[0] = sessionId;
    48. // native initialization
    49. int initResult = native_setup(new WeakReference<AudioTrack>(this),
    50. mStreamType, mSampleRate, mChannels, mAudioFormat,
    51. mNativeBufferSizeInBytes, mDataLoadMode, session);
    52. if (initResult != SUCCESS) {
    53. loge("Error code "+initResult+" when initializing AudioTrack.");
    54. return; // with mState == STATE_UNINITIALIZED
    55. }
    56. mSessionId = session[0];
    57. if (mDataLoadMode == MODE_STATIC) {
    58. mState = STATE_NO_STATIC_DATA;
    59. } else {
    60. mState = STATE_INITIALIZED;
    61. }
    62. }

    with audio session. Use this constructor when the AudioTrack must be attached to a particular audio session. The primary use of the audio session ID is to associate audio effects to a particular instance of AudioTrack: if an audio session ID is provided when creating an AudioEffect, this effect will be applied only to audio tracks and media players in the same session and not to the output mix. When an AudioTrack is created without specifying a session, it will create its own session which can be retreived by calling the getAudioSessionId() method. If a non-zero session ID is provided, this AudioTrack will share effects attached to this session with all other media players or audio tracks in the same session, otherwise a new session will be created for this track if none is supplied.

streamType

the type of the audio stream. See STREAM_VOICE_CALL,STREAM_SYSTEM,STREAM_RING,STREAM_MUSIC,STREAM_ALARM, andSTREAM_NOTIFICATION.

sampleRateInHz

the sample rate expressed in Hertz.

channelConfig

describes the configuration of the audio channels. SeeCHANNEL_OUT_MONO andCHANNEL_OUT_STEREO

audioFormat

the format in which the audio data is represented. SeeENCODING_PCM_16BIT andENCODING_PCM_8BIT

bufferSizeInBytes

the total size (in bytes) of the buffer where audio data is read from for playback. If using the AudioTrack in streaming mode, you can write data into this buffer in smaller chunks than this size. If using the AudioTrack in static mode, this is the maximum size of the sound that will be played for this instance. SeegetMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioTrack instance in streaming mode. Using values smaller than getMinBufferSize() will result in an initialization failure.

mode

streaming or static buffer. See MODE_STATIC andMODE_STREAM

sessionId

Id of audio session the AudioTrack must be attached to

AudioTrack有两种数据加载模式:

MODE_STREAM

在这种模式下,应用程序持续地write音频数据流到AudioTrack中,并且write动作将阻塞直到数据流从Java层传输到native层,同时加入到播放队列中。这种模式适用于播放大音频数据,但该模式也造成了一定的延时;

  1. MODE_STATIC

    在播放之前,先把所有数据一次性write到AudioTrack的内部缓冲区中。适用于播放内存占用小、延时要求较高的音频数据。

    frameworksasecorejniandroid_media_AudioTrack.cpp

    001. static int android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,jint streamType, jint sampleRateInHertz, jint javaChannelMask,
    002. jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession)
    003. {
    004. ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d",
    005. sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes);
    006. int afSampleRate;//采样率
    007. int afFrameCount;//帧数
    008. //通过AudioSystem从AudioPolicyService中读取对应音频流类型的帧数
    009. if (AudioSystem::getOutputFrameCount(&afFrameCount, (audio_stream_type_t) streamType) != NO_ERROR) {
    010. ALOGE("Error creating AudioTrack: Could not get AudioSystem frame count.");
    011. return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
    012. }
    013. //通过AudioSystem从AudioPolicyService中读取对应音频流类型的采样率
    014. if (AudioSystem::getOutputSamplingRate(&afSampleRate, (audio_stream_type_t) streamType) != NO_ERROR) {
    015. ALOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate.");
    016. return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
    017. }
    018. // Java channel masks don't map directly to the native definition, but it's a simple shift
    019. // to skip the two deprecated channel configurations "default" and "mono".
    020. uint32_t nativeChannelMask = ((uint32_t)javaChannelMask) >> 2;
    021. //判断是否为输出通道
    022. if (!audio_is_output_channel(nativeChannelMask)) {
    023. ALOGE("Error creating AudioTrack: invalid channel mask.");
    024. return AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK;
    025. }
    026. //得到通道个数,popcount函数用于统计一个整数中有多少位为1
    027. int nbChannels = popcount(nativeChannelMask);
    028. // check the stream type
    029. audio_stream_type_t atStreamType;
    030. switch (streamType) {
    031. case AUDIO_STREAM_VOICE_CALL:
    032. case AUDIO_STREAM_SYSTEM:
    033. case AUDIO_STREAM_RING:
    034. case AUDIO_STREAM_MUSIC:
    035. case AUDIO_STREAM_ALARM:
    036. case AUDIO_STREAM_NOTIFICATION:
    037. case AUDIO_STREAM_BLUETOOTH_SCO:
    038. case AUDIO_STREAM_DTMF:
    039. atStreamType = (audio_stream_type_t) streamType;
    040. break;
    041. default:
    042. ALOGE("Error creating AudioTrack: unknown stream type.");
    043. return AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE;
    044. }
    045. // This function was called from Java, so we compare the format against the Java constants
    046. if ((audioFormat != javaAudioTrackFields.PCM16) && (audioFormat != javaAudioTrackFields.PCM8)) {
    047. ALOGE("Error creating AudioTrack: unsupported audio format.");
    048. return AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT;
    049. }
    050. // for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled in android_media_AudioTrack_native_write_byte()
    051. if ((audioFormat == javaAudioTrackFields.PCM8)
    052. && (memoryMode == javaAudioTrackFields.MODE_STATIC)) {
    053. ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit
    054. buff size of %dbytes, switching to 16bit, buff size of %dbytes",
    055. buffSizeInBytes, 2*buffSizeInBytes);
    056. audioFormat = javaAudioTrackFields.PCM16;
    057. // we will need twice the memory to store the data
    058. buffSizeInBytes *= 2;
    059. }
    060. //根据不同的采样方式得到一个采样点的字节数
    061. int bytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1;
    062. audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ?
    063. AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;
    064. //根据buffer大小反向计算帧数  , 一帧大小=一个采样点字节数 * 声道数
    065. int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample);
    066. //判断参数的合法性
    067. jclass clazz = env->GetObjectClass(thiz);
    068. if (clazz == NULL) {
    069. ALOGE("Can't find %s when setting up callback.", kClassPathName);
    070. return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
    071. }
    072. if (jSession == NULL) {
    073. ALOGE("Error creating AudioTrack: invalid session ID pointer");
    074. return AUDIOTRACK_ERROR;
    075. }
    076. jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
    077. if (nSession == NULL) {
    078. ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
    079. return AUDIOTRACK_ERROR;
    080. }
    081. int sessionId = nSession[0];
    082. env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
    083. nSession = NULL;
    084. // create the native AudioTrack object
    085. sp<AudioTrack> lpTrack = new AudioTrack();
    086. if (lpTrack == NULL) {
    087. ALOGE("Error creating uninitialized AudioTrack");
    088. return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
    089. }
    090. // 创建存储音频数据的容器
    091. AudioTrackJniStorage* lpJniStorage = new AudioTrackJniStorage();
    092. lpJniStorage->mStreamType = atStreamType;
    093. //将Java层的AudioTrack引用保存到AudioTrackJniStorage中
    094. lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz);
    095. // we use a weak reference so the AudioTrack object can be garbage collected.
    096. lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this);
    097. lpJniStorage->mCallbackData.busy = false;
    098. //初始化不同模式下的native AudioTrack对象
    099. if (memoryMode == javaAudioTrackFields.MODE_STREAM) { //stream模式
    100. lpTrack->set(
    101. atStreamType,// stream type
    102. sampleRateInHertz,
    103. format,// word length, PCM
    104. nativeChannelMask,
    105. frameCount,
    106. AUDIO_OUTPUT_FLAG_NONE,
    107. audioCallback,
    108. &(lpJniStorage->mCallbackData),//callback, callback data (user)
    109. 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
    110. 0,//stream模式下的共享内存在AudioFlinger中创建
    111. true,// thread can call Java
    112. sessionId);// audio session ID
    113. } else if (memoryMode == javaAudioTrackFields.MODE_STATIC) {//static模式
    114. // 为AudioTrack分配共享内存区域
    115. if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) {
    116. ALOGE("Error creating AudioTrack in static mode: error creating mem heap base");
    117. goto native_init_failure;
    118. }
    119. lpTrack->set(
    120. atStreamType,// stream type
    121. sampleRateInHertz,
    122. format,// word length, PCM
    123. nativeChannelMask,
    124. frameCount,
    125. AUDIO_OUTPUT_FLAG_NONE,
    126. audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user));
    127. 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
    128. lpJniStorage->mMemBase,// shared mem
    129. true,// thread can call Java
    130. sessionId);// audio session ID
    131. }
    132. if (lpTrack->initCheck() != NO_ERROR) {
    133. ALOGE("Error initializing AudioTrack");
    134. goto native_init_failure;
    135. }
    136. nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
    137. if (nSession == NULL) {
    138. ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
    139. goto native_init_failure;
    140. }
    141. // read the audio session ID back from AudioTrack in case we create a new session
    142. nSession[0] = lpTrack->getSessionId();
    143. env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
    144. nSession = NULL;
    145. {   // scope for the lock
    146. Mutex::Autolock l(sLock);
    147. sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData);
    148. }
    149. // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field
    150. // of the Java object (in mNativeTrackInJavaObj)
    151. setAudioTrack(env, thiz, lpTrack);
    152. // save the JNI resources so we can free them later
    153. //ALOGV("storing lpJniStorage: %x
    154. ", (int)lpJniStorage);
    155. env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage);
    156. return AUDIOTRACK_SUCCESS;
    157. // failures:
    158. native_init_failure:
    159. if (nSession != NULL) {
    160. env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
    161. }
    162. env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class);
    163. env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref);
    164. delete lpJniStorage;
    165. env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
    166. return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
    167. }

    1. 检查音频参数;

    2. 创建一个AudioTrack(native)对象;

    3. 创建一个AudioTrackJniStorage对象;

    4. 调用set函数初始化AudioTrack;

    buffersize = frameCount * 每帧数据量 = frameCount * (Channel数 * 每个Channel数据量)

    构造native AudioTrack

    frameworksavmedialibmediaAudioTrack.cpp

    1. AudioTrack::AudioTrack(): mStatus(NO_INIT),
    2. mIsTimed(false),
    3. mPreviousPriority(ANDROID_PRIORITY_NORMAL),
    4. mPreviousSchedulingGroup(SP_DEFAULT),
    5. mCblk(NULL)
    6. {
    7. }

    构造AudioTrackJniStorage

    AudioTrackJniStorage是音频数据存储的容器,是对匿名共享内存的封装。

    01. struct audiotrack_callback_cookie {
    02. jclass      audioTrack_class;
    03. jobject     audioTrack_ref;//Java层AudioTrack对象引用
    04. bool        busy;//忙判断
    05. Condition   cond;//互斥量
    06. };
    07.  
    08. class AudioTrackJniStorage {
    09. public:
    10. sp<MemoryHeapBase>         mMemHeap;
    11. sp<MemoryBase>             mMemBase;
    12. audiotrack_callback_cookie mCallbackData;
    13. audio_stream_type_t        mStreamType;
    14.  
    15. AudioTrackJniStorage() {
    16. mCallbackData.audioTrack_class = 0;
    17. mCallbackData.audioTrack_ref = 0;
    18. mStreamType = AUDIO_STREAM_DEFAULT;
    19. }
    20.  
    21. ~AudioTrackJniStorage() {
    22. mMemBase.clear();
    23. mMemHeap.clear();
    24. }
    25. /**
    26. * 分配一块指定大小的匿名共享内存
    27. * @param sizeInBytes:匿名共享内存大小
    28. * @return
    29. */
    30. bool allocSharedMem(int sizeInBytes) {
    31. //创建一个匿名共享内存
    32. mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base");
    33. if (mMemHeap->getHeapID() < 0) {
    34. return false;
    35. }
    36. mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes);
    37. return true;
    38. }
    39. };
    40.  
    41. /**
    42. * 创建匿名共享内存区域
    43. * @param size:匿名共享内存大小
    44. * @param flags:创建标志位
    45. * @param name:匿名共享内存名称
    46. */
    47. MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
    48. : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
    49. mDevice(0), mNeedUnmap(false), mOffset(0)
    50. {
    51. //获取内存页大小
    52. const size_t pagesize = getpagesize();
    53. //字节对齐
    54. size = ((size + pagesize-1) & ~(pagesize-1));
    55. /* 创建共享内存,打开/dev/ashmem设备,得到一个文件描述符 */
    56. int fd = ashmem_create_region(name == NULL ? "MemoryHeapBase" : name, size);
    57. ALOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno));
    58. if (fd >= 0) {
    59. //通过mmap将匿名共享内存映射到当前进程地址空间
    60. if (mapfd(fd, size) == NO_ERROR) {
    61. if (flags & READ_ONLY) {
    62. ashmem_set_prot_region(fd, PROT_READ);
    63. }
    64. }
    65. }
    66. }

    初始化AudioTrack

    为AudioTrack设置音频参数信息,在Android4.4中,增加了一个参数transfer_type用于指定音频数据的传输方式,Android4.4定义了4种音频数据传输方式:

    enum transfer_type {

    TRANSFER_DEFAULT, // not specified explicitly; determine from the other parameters

    TRANSFER_CALLBACK, // callback EVENT_MORE_DATA

    TRANSFER_OBTAIN, // FIXME deprecated: call obtainBuffer() and releaseBuffer()

    TRANSFER_SYNC, // synchronous write()

    TRANSFER_SHARED, // shared memory

    };

    001. /**
    002. * 初始化AudioTrack
    003. * @param streamType  音频流类型
    004. * @param sampleRate  采样率
    005. * @param format      音频格式
    006. * @param channelMask 输出声道
    007. * @param frameCount  帧数
    008. * @param flags       输出标志位
    009. * @param cbf   Callback function. If not null, this function is called periodically
    010. *   to provide new data and inform of marker, position updates, etc.
    011. * @param user   Context for use by the callback receiver.
    012. * @param notificationFrames   The callback function is called each time notificationFrames          *  PCM frames have been consumed from track input buffer.
    013. * @param sharedBuffer 共享内存
    014. * @param threadCanCallJava
    015. * @param sessionId                
    016. * @return
    017. */
    018. status_t AudioTrack::set(
    019. audio_stream_type_t streamType,
    020. uint32_t sampleRate,
    021. audio_format_t format,
    022. audio_channel_mask_t channelMask,
    023. int frameCountInt,
    024. audio_output_flags_t flags,
    025. callback_t cbf,
    026. void* user,
    027. int notificationFrames,
    028. const sp<IMemory>& sharedBuffer,
    029. bool threadCanCallJava,
    030. int sessionId,
    031. transfer_type transferType,
    032. const audio_offload_info_t *offloadInfo,
    033. int uid)
    034. {
    035. //设置音频数据传输类型
    036. switch (transferType) {
    037. case TRANSFER_DEFAULT:
    038. if (sharedBuffer != 0) {
    039. transferType = TRANSFER_SHARED;
    040. } else if (cbf == NULL || threadCanCallJava) {
    041. transferType = TRANSFER_SYNC;
    042. } else {
    043. transferType = TRANSFER_CALLBACK;
    044. }
    045. break;
    046. case TRANSFER_CALLBACK:
    047. if (cbf == NULL || sharedBuffer != 0) {
    048. ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
    049. return BAD_VALUE;
    050. }
    051. break;
    052. case TRANSFER_OBTAIN:
    053. case TRANSFER_SYNC:
    054. if (sharedBuffer != 0) {
    055. ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
    056. return BAD_VALUE;
    057. }
    058. break;
    059. case TRANSFER_SHARED:
    060. if (sharedBuffer == 0) {
    061. ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
    062. return BAD_VALUE;
    063. }
    064. break;
    065. default:
    066. ALOGE("Invalid transfer type %d", transferType);
    067. return BAD_VALUE;
    068. }
    069. mTransfer = transferType;
    070. // FIXME "int" here is legacy and will be replaced by size_t later
    071. if (frameCountInt < 0) {
    072. ALOGE("Invalid frame count %d", frameCountInt);
    073. return BAD_VALUE;
    074. }
    075. size_t frameCount = frameCountInt;
    076. ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
    077. sharedBuffer->size());
    078. ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
    079. AutoMutex lock(mLock);
    080. // invariant that mAudioTrack != 0 is true only after set() returns successfully
    081. if (mAudioTrack != 0) {
    082. ALOGE("Track already in use");
    083. return INVALID_OPERATION;
    084. }
    085. mOutput = 0;
    086. // 音频流类型设置
    087. if (streamType == AUDIO_STREAM_DEFAULT) {
    088. streamType = AUDIO_STREAM_MUSIC;
    089. }
    090. //根据音频流类型从AudioPolicyService中得到对应的音频采样率
    091. if (sampleRate == 0) {
    092. uint32_t afSampleRate;
    093. if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
    094. return NO_INIT;
    095. }
    096. sampleRate = afSampleRate;
    097. }
    098. mSampleRate = sampleRate;
    099. //音频格式设置
    100. if (format == AUDIO_FORMAT_DEFAULT) {
    101. format = AUDIO_FORMAT_PCM_16_BIT;
    102. }
    103. //如果没有设置声道,则默认设置为立体声通道
    104. if (channelMask == 0) {
    105. channelMask = AUDIO_CHANNEL_OUT_STEREO;
    106. }
    107. // validate parameters
    108. if (!audio_is_valid_format(format)) {
    109. ALOGE("Invalid format %d", format);
    110. return BAD_VALUE;
    111. }
    112. // AudioFlinger does not currently support 8-bit data in shared memory
    113. if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
    114. ALOGE("8-bit data in shared memory is not supported");
    115. return BAD_VALUE;
    116. }
    117. // force direct flag if format is not linear PCM
    118. // or offload was requested
    119. if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
    120. || !audio_is_linear_pcm(format)) {
    121. ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
    122. ? "Offload request, forcing to Direct Output"
    123. : "Not linear PCM, forcing to Direct Output");
    124. flags = (audio_output_flags_t)
    125. // FIXME why can't we allow direct AND fast?
    126. ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
    127. }
    128. // only allow deep buffering for music stream type
    129. if (streamType != AUDIO_STREAM_MUSIC) {
    130. flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
    131. }
    132. //输出声道合法性检查
    133. if (!audio_is_output_channel(channelMask)) {
    134. ALOGE("Invalid channel mask %#x", channelMask);
    135. return BAD_VALUE;
    136. }
    137. mChannelMask = channelMask;
    138. //计算声道个数
    139. uint32_t channelCount = popcount(channelMask);
    140. mChannelCount = channelCount;
    141. if (audio_is_linear_pcm(format)) {
    142. mFrameSize = channelCount * audio_bytes_per_sample(format);
    143. mFrameSizeAF = channelCount * sizeof(int16_t);
    144. } else {
    145. mFrameSize = sizeof(uint8_t);
    146. mFrameSizeAF = sizeof(uint8_t);
    147. }
    148. /**
    149. * audio_io_handle_t是一个整形值,用于标示音频播放线程,这里更加音频参数
    150. * 从AudioFlinger中查找用于播放此音频的播放线程,并返回该播放线程的ID值
    151. */
    152. audio_io_handle_t output = AudioSystem::getOutput(
    153. streamType,
    154. sampleRate, format, channelMask,
    155. flags,
    156. offloadInfo);
    157. if (output == 0) {
    158. ALOGE("Could not get audio output for stream type %d", streamType);
    159. return BAD_VALUE;
    160. }
    161. //AudioTrack初始化
    162. mVolume[LEFT] = 1.0f;
    163. mVolume[RIGHT] = 1.0f;
    164. mSendLevel = 0.0f;
    165. mFrameCount = frameCount;
    166. mReqFrameCount = frameCount;
    167. mNotificationFramesReq = notificationFrames;
    168. mNotificationFramesAct = 0;
    169. mSessionId = sessionId;
    170. if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
    171. mClientUid = IPCThreadState::self()->getCallingUid();
    172. } else {
    173. mClientUid = uid;
    174. }
    175. mAuxEffectId = 0;
    176. mFlags = flags;
    177. mCbf = cbf;
    178. //如果设置了提供音频数据的回调函数,则启动AudioTrackThread线程来提供音频数据
    179. if (cbf != NULL) {
    180. mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
    181. mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
    182. }
    183. // create the IAudioTrack
    184. status_t status = createTrack_l(streamType,
    185. sampleRate,
    186. format,
    187. frameCount,
    188. flags,
    189. sharedBuffer,
    190. output,
    191. 0 /*epoch*/);
    192. if (status != NO_ERROR) {
    193. if (mAudioTrackThread != 0) {
    194. mAudioTrackThread->requestExit();   // see comment in AudioTrack.h
    195. mAudioTrackThread->requestExitAndWait();
    196. mAudioTrackThread.clear();
    197. }
    198. //Use of direct and offloaded output streams is ref counted by audio policy manager.
    199. // As getOutput was called above and resulted in an output stream to be opened,
    200. // we need to release it.
    201. AudioSystem::releaseOutput(output);
    202. return status;
    203. }
    204. mStatus = NO_ERROR;
    205. mStreamType = streamType;
    206. mFormat = format;
    207. mSharedBuffer = sharedBuffer;
    208. mState = STATE_STOPPED;
    209. mUserData = user;
    210. mLoopPeriod = 0;
    211. mMarkerPosition = 0;
    212. mMarkerReached = false;
    213. mNewPosition = 0;
    214. mUpdatePeriod = 0;
    215. AudioSystem::acquireAudioSessionId(mSessionId);
    216. mSequence = 1;
    217. mObservedSequence = mSequence;
    218. mInUnderrun = false;
    219. mOutput = output;
    220. return NO_ERROR;
    221. }

    我们知道,AudioPolicyService启动时加载了系统支持的所有音频接口,并且打开了默认的音频输出,打开音频输出时,调用AudioFlinger::openOutput()函数为当前打开的音频输出接口创建一个PlaybackThread线程,同时为该线程分配一个全局唯一的audio_io_handle_t值,并以键值对的形式保存在AudioFlinger的成员变量mPlaybackThreads中。在这里首先根据音频参数通过调用AudioSystem::getOutput()函数得到当前音频输出接口的PlaybackThread线程id号,同时传递给createTrack函数用于创建Track。AudioTrack在AudioFlinger中是以Track来管理的。不过因为它们之间是跨进程的关系,因此需要一个“桥梁”来维护,这个沟通的媒介是IAudioTrack。函数createTrack_l除了为AudioTrack在AudioFlinger中申请一个Track外,还会建立两者间IAudioTrack桥梁。

    获取音频输出

    获取音频输出就是根据音频参数如采样率、声道、格式等从已经打开的音频输出描述符列表中查找合适的音频输出AudioOutputDescriptor,并返回该音频输出在AudioFlinger中创建的播放线程id号,如果没有合适当前音频输出参数的AudioOutputDescriptor,则请求AudioFlinger打开一个新的音频输出通道,并为当前音频输出创建对应的播放线程,返回该播放线程的id号。具体过程请参考Android AudioPolicyService服务启动过程中的打开输出小节。

    创建AudioTrackThread线程

    初始化AudioTrack时,如果audioCallback为Null,就会创建AudioTrackThread线程。

    AudioTrack支持两种数据输入方式:

    1) Push方式:用户主动write,MediaPlayerService通常采用此方式;

    2) Pull方式: AudioTrackThread线程通过audioCallback回调函数主动从用户那里获取数据,ToneGenerator就是采用这种方式;

    01. bool AudioTrack::AudioTrackThread::threadLoop()
    02. {
    03. {
    04. AutoMutex _l(mMyLock);
    05. if (mPaused) {
    06. mMyCond.wait(mMyLock);
    07. // caller will check for exitPending()
    08. return true;
    09. }
    10. }
    11. //调用创建当前AudioTrackThread线程的AudioTrack的processAudioBuffer函数
    12. if (!mReceiver.processAudioBuffer(this)) {
    13. pause();
    14. }
    15. return true;
    16. }
    申请Track

    音频播放需要AudioTrack写入音频数据,同时需要AudioFlinger完成混音,因此需要在AudioTrack与AudioFlinger之间建立数据通道,而AudioTrack与AudioFlinger又分属不同的进程空间,Android系统采用Binder通信方式来搭建它们之间的桥梁。

    001. status_t AudioTrack::createTrack_l(
    002. audio_stream_type_t streamType,
    003. uint32_t sampleRate,
    004. audio_format_t format,
    005. size_t frameCount,
    006. audio_output_flags_t flags,
    007. const sp<IMemory>& sharedBuffer,
    008. audio_io_handle_t output,
    009. size_t epoch)
    010. {
    011. status_t status;
    012. //得到AudioFlinger的代理对象
    013. const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
    014. if (audioFlinger == 0) {
    015. ALOGE("Could not get audioflinger");
    016. return NO_INIT;
    017. }
    018. //得到输出时延
    019. uint32_t afLatency;
    020. status = AudioSystem::getLatency(output, streamType, &afLatency);
    021. if (status != NO_ERROR) {
    022. ALOGE("getLatency(%d) failed status %d", output, status);
    023. return NO_INIT;
    024. }
    025. //得到音频帧数
    026. size_t afFrameCount;
    027. status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
    028. if (status != NO_ERROR) {
    029. ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
    030. return NO_INIT;
    031. }
    032. //得到采样率
    033. uint32_t afSampleRate;
    034. status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
    035. if (status != NO_ERROR) {
    036. ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
    037. return NO_INIT;
    038. }
    039. // Client decides whether the track is TIMED (see below), but can only express a preference
    040. // for FAST.  Server will perform additional tests.
    041. if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
    042. // either of these use cases:
    043. // use case 1: shared buffer
    044. (sharedBuffer != 0) ||
    045. // use case 2: callback handler
    046. (mCbf != NULL))) {
    047. ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
    048. // once denied, do not request again if IAudioTrack is re-created
    049. flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
    050. mFlags = flags;
    051. }
    052. ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
    053. // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
    054. //  n = 1   fast track; nBuffering is ignored
    055. //  n = 2   normal track, no sample rate conversion
    056. //  n = 3   normal track, with sample rate conversion
    057. //          (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
    058. //  n > 3   very high latency or very small notification interval; nBuffering is ignored
    059. const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
    060. mNotificationFramesAct = mNotificationFramesReq;
    061. if (!audio_is_linear_pcm(format)) {
    062. if (sharedBuffer != 0) {//static模式
    063. // Same comment as below about ignoring frameCount parameter for set()
    064. frameCount = sharedBuffer->size();
    065. } else if (frameCount == 0) {
    066. frameCount = afFrameCount;
    067. }
    068. if (mNotificationFramesAct != frameCount) {
    069. mNotificationFramesAct = frameCount;
    070. }
    071. } else if (sharedBuffer != 0) {// static模式
    072. // Ensure that buffer alignment matches channel count
    073. // 8-bit data in shared memory is not currently supported by AudioFlinger
    074. size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
    075. if (mChannelCount > 1) {
    076. // More than 2 channels does not require stronger alignment than stereo
    077. alignment <<= 1;
    078. }
    079. if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
    080. ALOGE("Invalid buffer alignment: address %p, channel count %u",
    081. sharedBuffer->pointer(), mChannelCount);
    082. return BAD_VALUE;
    083. }
    084. // When initializing a shared buffer AudioTrack via constructors,
    085. // there's no frameCount parameter.
    086. // But when initializing a shared buffer AudioTrack via set(),
    087. // there _is_ a frameCount parameter.  We silently ignore it.
    088. frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
    089. } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
    090. // FIXME move these calculations and associated checks to server
    091. // Ensure that buffer depth covers at least audio hardware latency
    092. uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
    093. ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
    094. afFrameCount, minBufCount, afSampleRate, afLatency);
    095. if (minBufCount <= nBuffering) {
    096. minBufCount = nBuffering;
    097. }
    098. size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
    099. ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"", afLatency=%d",minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
    100. if (frameCount == 0) {
    101. frameCount = minFrameCount;
    102. } else if (frameCount < minFrameCount) {
    103. // not ALOGW because it happens all the time when playing key clicks over A2DP
    104. ALOGV("Minimum buffer size corrected from %d to %d",
    105. frameCount, minFrameCount);
    106. frameCount = minFrameCount;
    107. }
    108. // Make sure that application is notified with sufficient margin before underrun
    109. if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
    110. mNotificationFramesAct = frameCount/nBuffering;
    111. }
    112. } else {
    113. // For fast tracks, the frame count calculations and checks are done by server
    114. }
    115. IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
    116. if (mIsTimed) {
    117. trackFlags |= IAudioFlinger::TRACK_TIMED;
    118. }
    119. pid_t tid = -1;
    120. if (flags & AUDIO_OUTPUT_FLAG_FAST) {
    121. trackFlags |= IAudioFlinger::TRACK_FAST;
    122. if (mAudioTrackThread != 0) {
    123. tid = mAudioTrackThread->getTid();
    124. }
    125. }
    126. if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
    127. trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
    128. }
    129. //向AudioFlinger发送createTrack请求,在stream模式下sharedBuffer为空,output为AudioFlinger中播放线程的id号
    130. sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
    131. sampleRate,
    132. // AudioFlinger only sees 16-bit PCM
    133. format == AUDIO_FORMAT_PCM_8_BIT ?
    134. AUDIO_FORMAT_PCM_16_BIT : format,
    135. mChannelMask,
    136. frameCount,
    137. &trackFlags,
    138. sharedBuffer,
    139. output,
    140. tid,
    141. &mSessionId,
    142. mName,
    143. mClientUid,
    144. &status);
    145. if (track == 0) {
    146. ALOGE("AudioFlinger could not create track, status: %d", status);
    147. return status;
    148. }
    149. //AudioFlinger创建Tack对象时会分配一块共享内存,这里得到这块共享内存的代理对象BpMemory
    150. sp<IMemory> iMem = track->getCblk();
    151. if (iMem == 0) {
    152. ALOGE("Could not get control block");
    153. return NO_INIT;
    154. }
    155. // invariant that mAudioTrack != 0 is true only after set() returns successfully
    156. if (mAudioTrack != 0) {
    157. mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
    158. mDeathNotifier.clear();
    159. }
    160. //将创建的Track代理对象、匿名共享内存代理对象保存到AudioTrack的成员变量中
    161. mAudioTrack = track;
    162. mCblkMemory = iMem;
    163. //保存匿名共享内存的首地址,在匿名共享内存的头部存放了一个audio_track_cblk_t对象
    164. audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
    165. mCblk = cblk;
    166. size_t temp = cblk->frameCount_;
    167. if (temp < frameCount || (frameCount == 0 && temp == 0)) {
    168. // In current design, AudioTrack client checks and ensures frame count validity before
    169. // passing it to AudioFlinger so AudioFlinger should not return a different value except
    170. // for fast track as it uses a special method of assigning frame count.
    171. ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
    172. }
    173. frameCount = temp;
    174. mAwaitBoost = false;
    175. if (flags & AUDIO_OUTPUT_FLAG_FAST) {
    176. if (trackFlags & IAudioFlinger::TRACK_FAST) {
    177. ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
    178. mAwaitBoost = true;
    179. if (sharedBuffer == 0) {
    180. // double-buffering is not required for fast tracks, due to tighter scheduling
    181. if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
    182. mNotificationFramesAct = frameCount;
    183. }
    184. }
    185. } else {
    186. ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
    187. // once denied, do not request again if IAudioTrack is re-created
    188. flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
    189. mFlags = flags;
    190. if (sharedBuffer == 0) {//stream模式
    191. if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
    192. mNotificationFramesAct = frameCount/nBuffering;
    193. }
    194. }
    195. }
    196. }
    197. if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
    198. if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
    199. ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
    200. } else {
    201. ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
    202. flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
    203. mFlags = flags;
    204. return NO_INIT;
    205. }
    206. }
    207. mRefreshRemaining = true;
    208. // Starting address of buffers in shared memory.  If there is a shared buffer, buffers
    209. // is the value of pointer() for the shared buffer, otherwise buffers points
    210. // immediately after the control block.  This address is for the mapping within client
    211. // address space.  AudioFlinger::TrackBase::mBuffer is for the server address space.
    212. void* buffers;
    213. if (sharedBuffer == 0) {//stream模式
    214. buffers = (char*)cblk + sizeof(audio_track_cblk_t);
    215. } else {
    216. buffers = sharedBuffer->pointer();
    217. }
    218. mAudioTrack->attachAuxEffect(mAuxEffectId);
    219. // FIXME don't believe this lie
    220. mLatency = afLatency + (1000*frameCount) / sampleRate;
    221. mFrameCount = frameCount;
    222. // If IAudioTrack is re-created, don't let the requested frameCount
    223. // decrease.  This can confuse clients that cache frameCount().
    224. if (frameCount > mReqFrameCount) {
    225. mReqFrameCount = frameCount;
    226. }
    227. // update proxy
    228. if (sharedBuffer == 0) {
    229. mStaticProxy.clear();
    230. mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
    231. } else {
    232. mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
    233. mProxy = mStaticProxy;
    234. }
    235. mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
    236. uint16_t(mVolume[LEFT] * 0x1000));
    237. mProxy->setSendLevel(mSendLevel);
    238. mProxy->setSampleRate(mSampleRate);
    239. mProxy->setEpoch(epoch);
    240. mProxy->setMinimum(mNotificationFramesAct);
    241. mDeathNotifier = new DeathNotifier(this);
    242. mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
    243. return NO_ERROR;
    244. }

    IAudioTrack建立了AudioTrack与AudioFlinger之间的关系,在static模式下,用于存放音频数据的匿名共享内存在AudioTrack这边创建,在stream播放模式下,匿名共享内存却是在AudioFlinger这边创建。这两种播放模式下创建的匿名共享内存是有区别的,stream模式下的匿名共享内存头部会创建一个audio_track_cblk_t对象,用于协调生产者AudioTrack和消费者AudioFlinger之间的步调。createTrack就是在AudioFlinger中创建一个Track对象。

    frameworksavservicesaudioflinger AudioFlinger.cpp

    001. sp<IAudioTrack> AudioFlinger::createTrack(
    002. audio_stream_type_t streamType,
    003. uint32_t sampleRate,
    004. audio_format_t format,
    005. audio_channel_mask_t channelMask,
    006. size_t frameCount,
    007. IAudioFlinger::track_flags_t *flags,
    008. const sp<IMemory>& sharedBuffer,
    009. audio_io_handle_t output,
    010. pid_t tid,
    011. int *sessionId,
    012. String8& name,
    013. int clientUid,
    014. status_t *status)
    015. {
    016. sp<PlaybackThread::Track> track;
    017. sp<TrackHandle> trackHandle;
    018. sp<Client> client;
    019. status_t lStatus;
    020. int lSessionId;
    021. // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
    022. // but if someone uses binder directly they could bypass that and cause us to crash
    023. if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
    024. ALOGE("createTrack() invalid stream type %d", streamType);
    025. lStatus = BAD_VALUE;
    026. goto Exit;
    027. }
    028. // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
    029. // and we don't yet support 8.24 or 32-bit PCM
    030. if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
    031. ALOGE("createTrack() invalid format %d", format);
    032. lStatus = BAD_VALUE;
    033. goto Exit;
    034. }
    035. {
    036. Mutex::Autolock _l(mLock);
    037. //根据播放线程ID号查找出对应的PlaybackThread,在openout时,播放线程以key/value形式保存在AudioFlinger的mPlaybackThreads中
    038. PlaybackThread *thread = checkPlaybackThread_l(output);
    039. PlaybackThread *effectThread = NULL;
    040. if (thread == NULL) {
    041. ALOGE("no playback thread found for output handle %d", output);
    042. lStatus = BAD_VALUE;
    043. goto Exit;
    044. }
    045. pid_t pid = IPCThreadState::self()->getCallingPid();
    046. //根据客户端进程pid查找是否已经为该客户进程创建了Client对象,如果没有,则创建一个Client对象
    047. client = registerPid_l(pid);
    048. ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
    049. if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
    050. // check if an effect chain with the same session ID is present on another
    051. // output thread and move it here.
    052. //遍历所有的播放线程,不包括输出线程,如果该线程中Track的sessionId与当前相同,则取出该线程作为当前Track的effectThread。
    053. for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
    054. sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
    055. if (mPlaybackThreads.keyAt(i) != output) {
    056. uint32_t sessions = t->hasAudioSession(*sessionId);
    057. if (sessions & PlaybackThread::EFFECT_SESSION) {
    058. effectThread = t.get();
    059. break;
    060. }
    061. }
    062. }
    063. lSessionId = *sessionId;
    064. } else {
    065. // if no audio session id is provided, create one here
    066. lSessionId = nextUniqueId();
    067. if (sessionId != NULL) {
    068. *sessionId = lSessionId;
    069. }
    070. }
    071. ALOGV("createTrack() lSessionId: %d", lSessionId);
    072. //在找到的PlaybackThread线程中创建Track
    073. track = thread->createTrack_l(client, streamType, sampleRate, format,
    074. channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
    075. // move effect chain to this output thread if an effect on same session was waiting
    076. // for a track to be created
    077. if (lStatus == NO_ERROR && effectThread != NULL) {
    078. Mutex::Autolock _dl(thread->mLock);
    079. Mutex::Autolock _sl(effectThread->mLock);
    080. moveEffectChain_l(lSessionId, effectThread, thread, true);
    081. }
    082. // Look for sync events awaiting for a session to be used.
    083. for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
    084. if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
    085. if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
    086. if (lStatus == NO_ERROR) {
    087. (void) track->setSyncEvent(mPendingSyncEvents[i]);
    088. } else {
    089. mPendingSyncEvents[i]->cancel();
    090. }
    091. mPendingSyncEvents.removeAt(i);
    092. i--;
    093. }
    094. }
    095. }
    096. }
    097. //此时Track已成功创建,还需要为该Track创建代理对象TrackHandle
    098. if (lStatus == NO_ERROR) {
    099. // s for server's pid, n for normal mixer name, f for fast index
    100. name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,track->fastIndex());
    101. trackHandle = new TrackHandle(track);
    102. } else {
    103. // remove local strong reference to Client before deleting the Track so that the Client destructor is called by the TrackBase destructor with mLock held
    104. client.clear();
    105. track.clear();
    106. }
    107. Exit:
    108. if (status != NULL) {
    109. *status = lStatus;
    110. }
    111. /**
    112. * 向客户进程返回IAudioTrack的代理对象,这样客户进程就可以跨进程访问创建的Track了,
    113. * 访问方式如下:BpAudioTrack --> BnAudioTrack --> TrackHandle --> Track
    114. */
    115. return trackHandle;
    116. }

    该函数首先以单例模式为应用程序进程创建一个Client对象,直接对话某个客户进程。然后根据播放线程ID找出对应的PlaybackThread,并将创建Track的任务转交给它,PlaybackThread完成Track创建后,由于Track没有通信功能,因此还需要为其创建一个代理通信业务的TrackHandle对象。

    构造Client对象

    根据进程pid,为请求播放音频的客户端创建一个Client对象。

    01. sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
    02. {
    03. // If pid is already in the mClients wp<> map, then use that entry
    04. // (for which promote() is always != 0), otherwise create a new entry and Client.
    05. sp<Client> client = mClients.valueFor(pid).promote();
    06. if (client == 0) {
    07. client = new Client(this, pid);
    08. mClients.add(pid, client);
    09. }
    10. return client;
    11. }

    AudioFlinger的成员变量mClients以键值对的形式保存pid和Client对象,这里首先取出pid对应的Client对象,如果该对象为空,则为客户端进程创建一个新的Client对象。

    01. AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
    02. : RefBase(),mAudioFlinger(audioFlinger),
    03. // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
    04. mMemoryDealer(new MemoryDealer(2*1024*1024, "AudioFlinger::Client")),
    05. mPid(pid),
    06. mTimedTrackCount(0)
    07. {
    08. // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
    09. }

    构造Client对象时,创建了一个MemoryDealer对象,该对象用于分配共享内存。

    frameworks ativelibsinder MemoryDealer.cpp

    1. MemoryDealer::MemoryDealer(size_t size, const char* name)
    2. : mHeap(new MemoryHeapBase(size, 0, name)),//创建指定大小的共享内存
    3. mAllocator(new SimpleBestFitAllocator(size))//创建内存分配器
    4. {   
    5. }

    MemoryDealer是个工具类,用于分配共享内存,每一个Client都拥有一个MemoryDealer对象,这就意味着每个客户端进程都是在自己独有的内存空间中分配共享内存。MemoryDealer构造时创建了一个大小为2*1024*1024的匿名共享内存,该客户进程所有的AudioTrack在AudioFlinger中创建的Track都是在这块共享内存中分配buffer。

    1. SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size)
    2. {
    3. size_t pagesize = getpagesize();
    4. mHeapSize = ((size + pagesize-1) & ~(pagesize-1));//页对齐
    5. chunk_t* node = new chunk_t(0, mHeapSize / kMemoryAlign);
    6. mList.insertHead(node);
    7. }

    由此可知,当应用程序进程中的AudioTrack请求AudioFlinger在某个PlaybackThread中创建Track对象时,AudioFlinger首先会为应用程序进程创建一个Client对象,同时创建一块大小为2M的共享内存。在创建Track时,Track将在2M共享内存中分配buffer用于音频播放。

    创建Track对象
    001. sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
    002. const sp<AudioFlinger::Client>& client,
    003. audio_stream_type_t streamType,
    004. uint32_t sampleRate,
    005. audio_format_t format,
    006. audio_channel_mask_t channelMask,
    007. size_t frameCount,
    008. const sp<IMemory>& sharedBuffer,
    009. int sessionId,
    010. IAudioFlinger::track_flags_t *flags,
    011. pid_t tid,
    012. int uid,
    013. status_t *status)
    014. {
    015. sp<Track> track;
    016. status_t lStatus;
    017. bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0;
    018. // client expresses a preference for FAST, but we get the final say
    019. if (*flags & IAudioFlinger::TRACK_FAST) {
    020. if (
    021. // not timed
    022. (!isTimed) &&
    023. // either of these use cases:
    024. (
    025. // use case 1: shared buffer with any frame count
    026. (
    027. (sharedBuffer != 0)
    028. ) ||
    029. // use case 2: callback handler and frame count is default or at least as large as HAL
    030. (
    031. (tid != -1) &&
    032. ((frameCount == 0) ||
    033. (frameCount >= (mFrameCount * kFastTrackMultiplier)))
    034. )
    035. ) &&
    036. // PCM data
    037. audio_is_linear_pcm(format) &&
    038. // mono or stereo
    039. ( (channelMask == AUDIO_CHANNEL_OUT_MONO) ||
    040. (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) &&
    041. #ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE
    042. // hardware sample rate
    043. (sampleRate == mSampleRate) &&
    044. #endif
    045. // normal mixer has an associated fast mixer
    046. hasFastMixer() &&
    047. // there are sufficient fast track slots available
    048. (mFastTrackAvailMask != 0)
    049. // FIXME test that MixerThread for this fast track has a capable output HAL
    050. // FIXME add a permission test also?
    051. ) {
    052. // if frameCount not specified, then it defaults to fast mixer (HAL) frame count
    053. if (frameCount == 0) {
    054. frameCount = mFrameCount * kFastTrackMultiplier;
    055. }
    056. ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d",
    057. frameCount, mFrameCount);
    058. } else {
    059. ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d "
    060. "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u ""hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
    061. isTimed, sharedBuffer.get(), frameCount, mFrameCount, format,
    062. audio_is_linear_pcm(format),
    063. channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
    064. *flags &= ~IAudioFlinger::TRACK_FAST;
    065. // For compatibility with AudioTrack calculation, buffer depth is forced
    066. // to be at least 2 x the normal mixer frame count and cover audio hardware latency.
    067. // This is probably too conservative, but legacy application code may depend on it.
    068. // If you change this calculation, also review the start threshold which is related.
    069. uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream);
    070. uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate);
    071. if (minBufCount < 2) {
    072. minBufCount = 2;
    073. }
    074. size_t minFrameCount = mNormalFrameCount * minBufCount;
    075. if (frameCount < minFrameCount) {
    076. frameCount = minFrameCount;
    077. }
    078. }
    079. }
    080. if (mType == DIRECT) {
    081. if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
    082. if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
    083. ALOGE("createTrack_l() Bad parameter: sampleRate %u format %d, channelMask 0x%08x ""for output %p with format %d",sampleRate, format, channelMask, mOutput, mFormat);
    084. lStatus = BAD_VALUE;
    085. goto Exit;
    086. }
    087. }
    088. } else if (mType == OFFLOAD) {
    089. if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
    090. ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x """for output %p with format %d",sampleRate, format, channelMask, mOutput, mFormat);
    091. lStatus = BAD_VALUE;
    092. goto Exit;
    093. }
    094. } else {
    095. if ((format & AUDIO_FORMAT_MAIN_MASK) != AUDIO_FORMAT_PCM) {
    096. ALOGE("createTrack_l() Bad parameter: format %d ""
    097. "for output %p with format %d",format, mOutput, mFormat);
    098. lStatus = BAD_VALUE;
    099. goto Exit;
    100. }
    101. // Resampler implementation limits input sampling rate to 2 x output sampling rate.
    102. if (sampleRate > mSampleRate*2) {
    103. ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
    104. lStatus = BAD_VALUE;
    105. goto Exit;
    106. }
    107. }
    108. lStatus = initCheck();
    109. if (lStatus != NO_ERROR) {
    110. ALOGE("Audio driver not initialized.");
    111. goto Exit;
    112. }
    113. { // scope for mLock
    114. Mutex::Autolock _l(mLock);
    115. ALOGD("ceateTrack_l() got lock"); // SPRD: Add some log
    116. // all tracks in same audio session must share the same routing strategy otherwise
    117. // conflicts will happen when tracks are moved from one output to another by audio policy
    118. // manager
    119. uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
    120. for (size_t i = 0; i < mTracks.size(); ++i) {
    121. sp<Track> t = mTracks[i];
    122. if (t != 0 && !t->isOutputTrack()) {
    123. uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
    124. if (sessionId == t->sessionId() && strategy != actual) {
    125. ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
    126. strategy, actual);
    127. lStatus = BAD_VALUE;
    128. goto Exit;
    129. }
    130. }
    131. }
    132. if (!isTimed) {
    133. track = new Track(this, client, streamType, sampleRate, format,
    134. channelMask, frameCount, sharedBuffer, sessionId, uid, *flags);
    135. } else {
    136. track = TimedTrack::create(this, client, streamType, sampleRate, format,
    137. channelMask, frameCount, sharedBuffer, sessionId, uid);
    138. }
    139. if (track == 0 || track->getCblk() == NULL || track->name() < 0) {
    140. lStatus = NO_MEMORY;
    141. goto Exit;
    142. }
    143. mTracks.add(track);
    144. sp<EffectChain> chain = getEffectChain_l(sessionId);
    145. if (chain != 0) {
    146. ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
    147. track->setMainBuffer(chain->inBuffer());
    148. chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
    149. chain->incTrackCnt();
    150. }
    151. if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
    152. pid_t callingPid = IPCThreadState::self()->getCallingPid();
    153. // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
    154. // so ask activity manager to do this on our behalf
    155. sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);
    156. }
    157. }
    158. lStatus = NO_ERROR;
    159. Exit:
    160. if (status) {
    161. *status = lStatus;
    162. }
    163. return track;
    164. }

    这里就为AudioTrack创建了一个Track对象。Track继承于TrackBase,因此构造Track时,首先执行TrackBase的构造函数。

     

    001. AudioFlinger::ThreadBase::TrackBase::TrackBase(
    002. ThreadBase *thread,//所属的播放线程
    003. const sp<Client>& client,//所属的Client
    004. uint32_t sampleRate,//采样率
    005. audio_format_t format,//音频格式
    006. audio_channel_mask_t channelMask,//声道
    007. size_t frameCount,//音频帧个数
    008. const sp<IMemory>& sharedBuffer,//共享内存
    009. int sessionId,
    010. int clientUid,
    011. bool isOut)
    012. :   RefBase(),
    013. mThread(thread),
    014. mClient(client),
    015. mCblk(NULL),
    016. // mBuffer
    017. mState(IDLE),
    018. mSampleRate(sampleRate),
    019. mFormat(format),
    020. mChannelMask(channelMask),
    021. mChannelCount(popcount(channelMask)),
    022. mFrameSize(audio_is_linear_pcm(format) ?
    023. mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
    024. mFrameCount(frameCount),
    025. mSessionId(sessionId),
    026. mIsOut(isOut),
    027. mServerProxy(NULL),
    028. mId(android_atomic_inc(&nextTrackId)),
    029. mTerminated(false)
    030. {
    031. // if the caller is us, trust the specified uid
    032. if (IPCThreadState::self()->getCallingPid() != getpid_cached || clientUid == -1) {
    033. int newclientUid = IPCThreadState::self()->getCallingUid();
    034. if (clientUid != -1 && clientUid != newclientUid) {
    035. ALOGW("uid %d tried to pass itself off as %d", newclientUid, clientUid);
    036. }
    037. clientUid = newclientUid;
    038. }
    039. // clientUid contains the uid of the app that is responsible for this track, so we can blame
    040. //得到应用进程uid
    041. mUid = clientUid;
    042. // client == 0 implies sharedBuffer == 0
    043. ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
    044. ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
    045. sharedBuffer->size());
    046. //计算audio_track_cblk_t大小
    047. size_t size = sizeof(audio_track_cblk_t);
    048. //计算存放音频数据的buffer大小,= frameCount*mFrameSize
    049. size_t bufferSize = (sharedBuffer == 0 ? roundup(frameCount) : frameCount) * mFrameSize;
    050. /**
    051. * stream模式下,需要audio_track_cblk_t来协调生成者和消费者,计算共享内存大小  
    052. *  --------------------------------------------------------
    053. * | audio_track_cblk_t |               buffer                   |
    054. *  --------------------------------------------------------
    055. */
    056. if (sharedBuffer == 0) {//stream模式下
    057. size += bufferSize;
    058. }
    059. //如果Client不为空,就通过Client来分配buffer
    060. if (client != 0) {
    061. //请求Client中的MemoryDealer工具类来分配buffer
    062. mCblkMemory = client->heap()->allocate(size);
    063. //分配成功
    064. if (mCblkMemory != 0) {
    065. //将共享内存的指针强制转换为audio_track_cblk_t
    066. mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
    067. // can't assume mCblk != NULL
    068. } else {
    069. ALOGE("not enough memory for AudioTrack size=%u", size);
    070. client->heap()->dump("AudioTrack");
    071. return;
    072. }
    073. } else {//Client为空,使用数组方式分配内存空间
    074. // this syntax avoids calling the audio_track_cblk_t constructor twice
    075. mCblk = (audio_track_cblk_t *) new uint8_t[size];
    076. // assume mCblk != NULL
    077. }
    078. /**
    079. * 当为应用进程创建了Client对象,则通过Client来分配音频数据buffer,否则通过数组方式分配buffer。 
    080. * stream模式下,在分配好的buffer头部创建audio_track_cblk_t对象,而static模式下,创建单独的
    081. * audio_track_cblk_t对象。
    082. */
    083. if (mCblk != NULL) {
    084. // construct the shared structure in-place.
    085. new(mCblk) audio_track_cblk_t();
    086. // clear all buffers
    087. mCblk->frameCount_ = frameCount;
    088. if (sharedBuffer == 0) {//stream模式
    089. //将mBuffer指向数据buffer的首地址
    090. mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
    091. //清空数据buffer
    092. memset(mBuffer, 0, bufferSize);
    093. } else {//static模式
    094. mBuffer = sharedBuffer->pointer();
    095. #if 0
    096. mCblk->mFlags = CBLK_FORCEREADY;    // FIXME hack, need to fix the track ready logic
    097. #endif
    098. }
    099. #ifdef TEE_SINK
    100.
    101. #endif
    102. ALOGD("TrackBase constructed"); // SPRD: add some log
    103. }
    104. }

    TrackBase构造过程主要是为音频播放分配共享内存,在static模式下,共享内存由应用进程自身分配,但在stream模式,共享内存由AudioFlinger分配,static和stream模式下,都会创建audio_track_cblk_t对象,唯一的区别在于,在stream模式下,audio_track_cblk_t对象创建在共享内存的头部。

    static模式:

    stream模式:

    接下来继续分析Track的构造过程:

    01. AudioFlinger::PlaybackThread::Track::Track(
    02. PlaybackThread *thread, //所属的播放线程
    03. const sp<Client>& client, //所属的Client
    04. audio_stream_type_t streamType,//音频流类型
    05. uint32_t sampleRate, //采样率
    06. audio_format_t format, //音频格式
    07. audio_channel_mask_t channelMask, //声道
    08. size_t frameCount, //音频帧个数
    09. const sp<IMemory>& sharedBuffer, //共享内存
    10. int sessionId,
    11. int uid,
    12. IAudioFlinger::track_flags_t flags)
    13. :   TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer,sessionId, uid, true /*isOut*/),
    14. mFillingUpStatus(FS_INVALID),
    15. // mRetryCount initialized later when needed
    16. mSharedBuffer(sharedBuffer),
    17. mStreamType(streamType),
    18. mName(-1),  // see note below
    19. mMainBuffer(thread->mixBuffer()),
    20. mAuxBuffer(NULL),
    21. mAuxEffectId(0), mHasVolumeController(false),
    22. mPresentationCompleteFrames(0),
    23. mFlags(flags),
    24. mFastIndex(-1),
    25. mCachedVolume(1.0),
    26. mIsInvalid(false),
    27. mAudioTrackServerProxy(NULL),
    28. mResumeToStopping(false)
    29. {
    30. if (mCblk != NULL) {//audio_track_cblk_t对象不为空
    31. if (sharedBuffer == 0) {//stream模式
    32. mAudioTrackServerProxy = new AudioTrackServerProx              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值