在创建 AudioTrack 时有一个缓冲区大小的参数,最小缓冲区参数通过 AudioTrack.getMinBufferSize() 获取。
一、最小缓冲区
为了让音频数据通路能正常运转,共享FIFO必须达到最小缓冲区的大小。如果数据缓冲区分配得过小,那么播放声音会频繁遭遇 underrun,underrun 是消费者(AudioFlinger::PlaybackThread)不能及时从缓冲区拿到数据,造成的后果就是声音断续卡顿。
1、获取方法
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
从函数参数来看,返回值取决于采样率、音频格式、声道数这三个属性。
2、AudioTrack
源码位置:/frameworks/base/media/java/android/media/AudioTrack.java
getMinBufferSize
static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {int channelCount = 0;switch(channelConfig) {case AudioFormat.CHANNEL_OUT_MONO:case AudioFormat.CHANNEL_CONFIGURATION_MONO:channelCount = 1; //单声道break;case AudioFormat.CHANNEL_OUT_STEREO:case AudioFormat.CHANNEL_CONFIGURATION_STEREO:channelCount = 2; //双声道break;default:if (!isMultichannelConfigSupported(channelConfig, audioFormat)) {loge("getMinBufferSize(): Invalid channel configuration.");return ERROR_BAD_VALUE;} else {channelCount = AudioFormat.channelCountFromOutChannelMask(channelConfig);}}if (!AudioFormat.isPublicEncoding(audioFormat)) {loge("getMinBufferSize(): Invalid audio format.");return ERROR_BAD_VALUE;}// 采样率// 注意: AudioFormat.SAMPLE_RATE_UNSPECIFIED不允许if ( (sampleRateInHz < AudioFormat.SAMPLE_RATE_HZ_MIN) || (sampleRateInHz > AudioFormat.SAMPLE_RATE_HZ_MAX) ) {loge("getMinBufferSize(): " + sampleRateInHz + " Hz is not a supported sample rate.");return ERROR_BAD_VALUE; //采样率支持:4KHz ~ 192KHz}// 进入Native层int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);if (size <= 0) {loge("getMinBufferSize(): error querying hardware");return ERROR;} else {return size;}
}
下面看一下对应的 native 方法 native_get_min_buff_size()。
3、android_media_AudioTrack.cpp
源码位置:/frameworks/base/core/jni/android_media_AudioTrack.cpp
native_get_min_buff_size
// 返回创建AudioTrack所需的最小buffer大小
// 如果查询硬件出错,返回-1
static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz,jint sampleRateInHertz, jint channelCount, jint audioFormat) {size_t frameCount;// 获取最低帧数,也就是确定至少设置多少个frame才能保证声音正常播放const status_t status = AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, sampleRateInHertz);if (status != NO_ERROR) {ALOGE("AudioTrack::getMinFrameCount() for sample rate %d failed with status %d", sampleRateInHertz, status);return -1;}const audio_format_t format = audioFormatToNative(audioFormat);if (audio_has_proportional_frames(format)) {const size_t bytesPerSample = audio_bytes_per_sample(format);// PCM 数据最小缓冲区大小return frameCount * channelCount * bytesPerSample;} else {return frameCount;}
}
最小缓冲区的大小 = 最低帧数 * 帧大小(声道数 * 位宽),(位宽以字节为单位)。
二、最低帧数
1、基础概述
在分析获取最小帧数前,我们先来了解几个相关的概念。
帧
帧(frame):表示一个完整的声音单元,所谓的声音单元是指一个采样样本。如果是双声道,那么一个完整的声音单元就是 2 个样本,如果是 5.1 声道,那么一个完整的声音单元就是 6 个样本了。帧的大小(一个完整的声音单元的数据量)等于声道数乘以采样位宽,即 frameSize = channelCount * bytesPerSample。无论是框架层还是内核层,都是以帧为单位去管理音频数据缓冲区的。在音频开发领域通常也会说采样点来对应帧这个概念。因为将帧的个数除以采样率就可以直接获得对应音频数据的时长。(PCM格式)
传输延迟
传输延迟(latency):一个处理单元引入的delay。
周期
周期(period):Linux ALSA 把数据缓冲区划分为若干个块,dma 每传输完一个块上的数据即发出一个硬件中断,CPU 收到中断信号后,再配置 dma 去传输下一个块上的数据。一个块即是一个周期。
周期大小
周期大小(periodSize):即是一个数据块的帧数。
再回到传输延迟(latency),每次传输产生的延迟等于周期大小除以采样率,即 latency = periodSize / sampleRate。
音频重采样
音频重采样是指这样的一个过程——把一个采样率的数据转换为另一个采样率的数据。Android 原生系统上,音频硬件设备一般都工作在一个固定的采样率上(如 48 KHz),因此所有音轨数据都需要重采样到这个固定的采样率上,然后再输出。因为系统中可能存在多个音轨同时播放,而每个音轨的采样率可能是不一致的,比如在播放音乐的过程中,来了一个提示音,这时需要把音乐和提示音混音并输出到硬件设备,而音乐的采样率和提示音的采样率不一致,问题来了,如果硬件设备工作的采样率设置为音乐的采样率的话,那么提示音就会失真,因此最简单见效的解决方法是:硬件设备工作的采样率固定一个值,所有音轨在 AudioFlinger 都重采样到这个采样率上,混音后输出到硬件设备,保证所有音轨听起来都不失真。 sample、frame、period、latency 这些概念与 Linux ALSA 及硬件设备的关系非常密切,在了解这些前置知识后,我们再分析 AudioTrack::getMinFrameCount() 这个函数。
2、AudioTrack.cpp
源码位置:/frameworks/av/media/libaudioclient/AudioTrack.cpp
getMinFrameCount
status_t AudioTrack::getMinFrameCount(size_t* frameCount, audio_stream_type_t streamType, uint32_t sampleRate)
{if (frameCount == NULL) {return BAD_VALUE;}// 通过binder调用到AudioFlinger::sampleRate()获取硬件设备的采样率uint32_t afSampleRate;status_t status;status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);if (status != NO_ERROR) {ALOGE("%s(): Unable to query output sample rate for stream type %d; status %d",__func__, streamType, status);return status;}// 通过binder调用到AudioFlinger::frameCount()获取硬件设备的周期大小size_t afFrameCount;status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);if (status != NO_ERROR) {ALOGE("%s(): Unable to query output frame count for stream type %d; status %d",__func__, streamType, status);return status;}// 通过binder调用到AudioFlinger::latency()获取硬件设备的传输延时uint32_t afLatency;status = AudioSystem::getOutputLatency(&afLatency, streamType);if (status != NO_ERROR) {ALOGE("%s(): Unable to query output latency for stream type %d; status %d",__func__, streamType, status);return status;}// 根据afLatency、afFrameCount、afSampleRate计算出一个最低帧数*frameCount = AudioSystem::calculateMinFrameCount(afLatency, afFrameCount, afSampleRate, sampleRate, 1.0f /*, 0 notificationsPerBufferReq*/);// 正常情况下应始终产生一个非零值if (*frameCount == 0) {ALOGE("%s(): failed for streamType %d, sampleRate %u", __func__, streamType, sampleRate);return BAD_VALUE;}ALOGV("%s(): getMinFrameCount=%zu: afFrameCount=%zu, afSampleRate=%u, afLatency=%u",__func__, *frameCount, afFrameCount, afSampleRate, afLatency);return NO_ERROR;
}
这里比较重要的就是调用 calculateMinFrameCount() 方法计算最低帧数。
3、AudioSystem.cpp
源码位置:/frameworks/av/media/libaudioclient/AudioSystem.cpp
我个人的理解:audiosystem就是native层的audiomanager。
calculateMinFrameCount
/* static */ size_t AudioSystem::calculateMinFrameCount(uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) {// 确保缓冲区深度至少覆盖音频硬件延迟uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);if (minBufCount < 2) {minBufCount = 2;}
#if 0// The notificationsPerBufferReq parameter is not yet used for non-fast tracks,// but keeping the code here to make it easier to add later.if (minBufCount < notificationsPerBufferReq) {minBufCount = notificationsPerBufferReq;}
#endifALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u ""sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount /*, notificationsPerBufferReq*/);return minBufCount * sourceFramesNeededWithTimestretch(sampleRate, afFrameCount, afSampleRate, speed);
}
这个函数根据硬件设备的配置信息(采样率、周期大小、传输延迟)和音轨的采样率,计算出一个最低帧数。
背后的设计思想是:
如果硬件延迟大于>2倍periodsize,则用硬件延迟作为最小buffersize。否则用2倍的periodsize。
保证缓冲区的数据量是每个周期数据量的两倍,这样可以保证进行乒乓操作。
4、AudioResamplerPublic.h
源码位置:/frameworks/av/include/media/AudioResamplerPublic.h
sourceFramesNeededWithTimestretch
static inline size_t sourceFramesNeededWithTimestretch( uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate, float speed) {size_t required = sourceFramesNeeded(srcSampleRate, dstFramesRequired, dstSampleRate);return required * (double)speed + 1 + 1; // accounting for rounding dependencies
}
sourceFramesNeeded
static inline size_t sourceFramesNeeded(uint32_t srcSampleRate, size_t dstFramesRequired, uint32_t dstSampleRate) { // +1 for rounding - always do this even if matched ratio (resampler may use phases not ratio) // +1 for additional sample needed for interpolation return srcSampleRate == dstSampleRate ? dstFramesRequired : size_t((uint64_t)dstFramesRequired * srcSampleRate / dstSampleRate + 1 + 1);}
calculateMinFrameCount() 这个函数根据硬件设备的配置信息(采样率、周期大小、传输延迟)和音轨的采样率,计算出一个最低帧数。 注意,getMinBufferSize() 方法获取到的是保证音频播放时缓冲区的最小值。很多应用都会大于这个最小值。此外,这个最小值只适用于PCM数据播放,PCM数据播放,PCM数据播放。如果是编码后的音频数据(DD,DDP,DTS)需要应用自己计算最小值!!!!!!!!!!!!!!!!!!!!!!
用一张图概括整篇文章。
记忆要点:缓冲区至少两倍于每个中断搬运的数据。