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是音频硬件支持的最大采样率。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值