AudioTrack之getMinFrameCount
2013年07月14日 19:23:14 bob_fly1984 阅读数:1069
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bob_fly1984/article/details/9325759
-
status_t AudioTrack::getMinFrameCount(
-
int* frameCount,
-
audio_stream_type_t streamType,
-
uint32_t sampleRate)
-
{
-
if (frameCount == NULL) return BAD_VALUE;
-
// default to 0 in case of error
-
*frameCount = 0;
-
// FIXME merge with similar code in createTrack_l(), except we're missing
-
// some information here that is available in createTrack_l():
-
// audio_io_handle_t output
-
// audio_format_t format
-
// audio_channel_mask_t channelMask
-
// audio_output_flags_t flags
-
int afSampleRate;
-
if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
-
return NO_INIT;
-
}
-
int afFrameCount;
-
if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
-
return NO_INIT;
-
}
-
uint32_t afLatency;
-
if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
-
return NO_INIT;
-
}
-
// Ensure that buffer depth covers at least audio hardware latency
-
uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
-
if (minBufCount < 2) minBufCount = 2;
-
*frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
-
afFrameCount * minBufCount * sampleRate / afSampleRate;
-
ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
-
*frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
-
return NO_ERROR;
-
}
afFrameCount--音频硬件BUFFER的大小
afFrameCount/afSampleRate--播放完音频硬件BUFFER中的数据需要多长时间,单位是秒。
忽视声道数,单位时间内的frame count值与单位时间内的采样率值是一样的。
((1000 * afFrameCount) / afSampleRate)--转换为毫秒
afLatency--硬件延迟的最大时间
什么是硬件延迟?
硬件播放硬件BUFFER里的数据,导致硬件可能有一段时间不能从客户端进程取得数据。具体表现是:audio hal层write函数阻塞不能返回,导致audioflinger的播放线程阻塞,不能消耗共享内存里的数据。客户端进程维护了一个BUFFER,它正在不停的往这个BUFFER里写数据,为了保证数据不被溢出(丢失),需要BUFFER足够大。
多大呢?
就是在硬件延迟的最大时间内不取数据的情况下,确保客户端进程往这个BUFFER写的数据不被溢出。
可见客户端进程的BUFFER大小事音频硬件BUFFER的整数倍。
计算出的frame count与传入的采样率参数有关系。
-
*frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
-
afFrameCount * minBufCount * sampleRate / afSampleRate;
如果上层传入的采样率为0或和硬件的最大采样率一样,那frame count等于afFrameCount * minBufCount。
如果上层传入的采样率和硬件支持的最大采样率不一样,那frame count等于afFrameCount * minBufCount乘以一个系数,这个系数就是sampleRate / afSampleRate。其中afSampleRate是音频硬件支持的最大采样率。