AudioTrack之getMinFrameCount

AudioTrack之getMinFrameCount

2013年07月14日 19:23:14 bob_fly1984 阅读数:1069

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bob_fly1984/article/details/9325759

 
  1. status_t AudioTrack::getMinFrameCount(

  2. int* frameCount,

  3. audio_stream_type_t streamType,

  4. uint32_t sampleRate)

  5. {

  6. if (frameCount == NULL) return BAD_VALUE;

  7.  
  8. // default to 0 in case of error

  9. *frameCount = 0;

  10.  
  11. // FIXME merge with similar code in createTrack_l(), except we're missing

  12. // some information here that is available in createTrack_l():

  13. // audio_io_handle_t output

  14. // audio_format_t format

  15. // audio_channel_mask_t channelMask

  16. // audio_output_flags_t flags

  17. int afSampleRate;

  18. if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {

  19. return NO_INIT;

  20. }

  21. int afFrameCount;

  22. if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {

  23. return NO_INIT;

  24. }

  25. uint32_t afLatency;

  26. if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {

  27. return NO_INIT;

  28. }

  29.  
  30. // Ensure that buffer depth covers at least audio hardware latency

  31. uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);

  32. if (minBufCount < 2) minBufCount = 2;

  33.  
  34. *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :

  35. afFrameCount * minBufCount * sampleRate / afSampleRate;

  36. ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",

  37. *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);

  38. return NO_ERROR;

  39. }

afFrameCount--音频硬件BUFFER的大小

afFrameCount/afSampleRate--播放完音频硬件BUFFER中的数据需要多长时间,单位是秒。

忽视声道数,单位时间内的frame count值与单位时间内的采样率值是一样的。

((1000 * afFrameCount) / afSampleRate)--转换为毫秒

afLatency--硬件延迟的最大时间

什么是硬件延迟?

硬件播放硬件BUFFER里的数据,导致硬件可能有一段时间不能从客户端进程取得数据。具体表现是:audio hal层write函数阻塞不能返回,导致audioflinger的播放线程阻塞,不能消耗共享内存里的数据。客户端进程维护了一个BUFFER,它正在不停的往这个BUFFER里写数据,为了保证数据不被溢出(丢失),需要BUFFER足够大。

多大呢?

就是在硬件延迟的最大时间内不取数据的情况下,确保客户端进程往这个BUFFER写的数据不被溢出。

可见客户端进程的BUFFER大小事音频硬件BUFFER的整数倍。

计算出的frame count与传入的采样率参数有关系。

 
  1. *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :

  2. afFrameCount * minBufCount * sampleRate / afSampleRate;


 

如果上层传入的采样率为0或和硬件的最大采样率一样,那frame count等于afFrameCount * minBufCount。

如果上层传入的采样率和硬件支持的最大采样率不一样,那frame count等于afFrameCount * minBufCount乘以一个系数,这个系数就是sampleRate / afSampleRate。其中afSampleRate是音频硬件支持的最大采样率。

猜你喜欢

转载自blog.csdn.net/weixin_42082222/article/details/84335814