Android 4.4 Kitkat 音频实现及简要分析

41 篇文章 0 订阅
11 篇文章 0 订阅


androidframework移植调试audio

目录(?)[+]

在 Android 4.4 上实现录放音


背景

Android 自 ICS 开始,音频系统就有了很大的变化,先是抛弃了 alsalib,然后是采用了 AIO,各级框架上,都有了自己的特色,与 Linux 的音频应用渐行渐远,形成了自己独特的音频管理和音频配置功能。总的来说改进还是非常大,至少在用户体验上已经大大的超越了之前的版本。我们就从 4.4 的音频实现上来分析其中的一些变化和实现机制。


要求


首先是硬件功能正常,这个不表。 Linux 支持 alsa 驱动,生成 alsa 子系统,最好是能够在 buildroot 等其他文件系统上事先测试音频的播放和录制。

HAL

音频的 HAL 简单实现,参考 device/asus/grouper/audio , 如果没有太复杂的音频配置,基本上可以在这个代码基础上稍微修改,主要是一些播放和录制的参数。这个 HAL 已经实现了通用的接口,并且调用的也是标准的 tinyalsa 的接口,移植性非常高。我们这里使用的 wm8904,功能不多,直接使用即可。


Android 的配置


主要是4个文件 audio_policy.conf  media_profiles.xml media_codecs.xml mixer_paths.xml 参考 asus ,不必大改,基本照抄,完全可以直接使用,开源万岁。

做好文件系统,这个时候系统应该就不使用 default 的 stub 音频 HAL , 而是用我们添加的 audio HAL 了。 但是能否发声,这个多半还是不能。

调试


audio 系统调用了  libtinyalsa libaudioutils libaudioroute 几个底层库。 这几个移植了一些通用的 alsa 设备打开配置功能,但是具体平台却并不一定都能正常执行,主要是这些库实现都很简单,没有考虑全面,你的硬件细节可能刚好被他们忽略了,同样以我们的 wm8904 来说,我们不支持 time stamp ,但是 tinyalsa 是默认打开的必须将其关掉。
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.     disable tstamp for wm8904.  
  2.       
  3.     Change-Id: Ia22aa6ed39ede6214657487344d0488be93e5544  
  4.   
  5. diff --git a/pcm.c b/pcm.c  
  6. index 4501777..94cf6ee 100644  
  7. --- a/pcm.c  
  8. +++ b/pcm.c  
  9. @@ -691,7 +691,7 @@ struct pcm *pcm_open(unsigned int card, unsigned int device,  
  10.    
  11.    
  12.      memset(&sparams, 0, sizeof(sparams));  
  13. -    sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;  
  14. +    sparams.tstamp_mode = SNDRV_PCM_TSTAMP_NONE;  
  15.      sparams.period_step = 1;  
  16.    
  17.      if (!config->start_threshold) {  

具体哪些参数不对,或者不合适,就需要 Android 驱动工程师根据硬件设计和芯片手册,逐个查证,配置到一个音频系统工作的最佳状态。那么用户体验才能最好。



Android 音频系统分析

以下分析基于 4.4.2

audio HAL


tinyalsa 与 audioroute
Android 音频系统基于 Linux 的 ALSA 驱动, tinyalsa 在 alsa 的驱动基础上封装音频接口,提供给 audio HAL, audio HAL 提供接口给 Android audioflinger 等 framework。
HAL 需要实现 audio 硬件的打开与关闭(这里是 android 认为的硬件)。
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static inline int audio_hw_device_open(const struct hw_module_t* module,  
  2.                                        struct audio_hw_device** device)  
  3. {  
  4.     return module->methods->open(module, AUDIO_HARDWARE_INTERFACE,  
  5.                                  (struct hw_device_t**)device);  
  6. }  
  7.   
  8. static inline int audio_hw_device_close(struct audio_hw_device* device)  
  9. {  
  10.     return device->common.close(&device->common);  
  11. }  

需要实现 in 和 out 的 数据流
struct audio_stream_out   struct audio_stream_in 
in 主要有 read 方法用于读取音频数据,   out 主要有 write 方法,写入数据到设备,分别实现录音和放音。
详见: hardware/libhardware/include/hardware/audio.h

其中的方法又是调用的 tinyalsa 的接口,关于 pcm 的操作:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* Open and close a stream */  
  2. struct pcm *pcm_open(unsigned int card, unsigned int device,  
  3.                      unsigned int flags, struct pcm_config *config);  
  4. int pcm_close(struct pcm *pcm);  
  5. int pcm_is_ready(struct pcm *pcm);  
  6.   
  7. /* Obtain the parameters for a PCM */  
  8. struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,  
  9.                                   unsigned int flags);  
  10. void pcm_params_free(struct pcm_params *pcm_params);  
  11. unsigned int pcm_params_get_min(struct pcm_params *pcm_params,  
  12.                                 enum pcm_param param);  
  13. unsigned int pcm_params_get_max(struct pcm_params *pcm_params,  
  14.                                 enum pcm_param param);  
  15.   
  16. /* Set and get config */  
  17. int pcm_get_config(struct pcm *pcm, struct pcm_config *config);  
  18. int pcm_set_config(struct pcm *pcm, struct pcm_config *config);  
  19.   
  20. /* Returns a human readable reason for the last error */  
  21. const char *pcm_get_error(struct pcm *pcm);  
  22.   
  23. /* Returns the sample size in bits for a PCM format. 
  24.  * As with ALSA formats, this is the storage size for the format, whereas the 
  25.  * format represents the number of significant bits. For example, 
  26.  * PCM_FORMAT_S24_LE uses 32 bits of storage. 
  27.  */  
  28. unsigned int pcm_format_to_bits(enum pcm_format format);  
  29.   
  30. /* Returns the buffer size (int frames) that should be used for pcm_write. */  
  31. unsigned int pcm_get_buffer_size(struct pcm *pcm);  
  32. unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames);  
  33. unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes);  
  34.   
  35. /* Returns the pcm latency in ms */  
  36. unsigned int pcm_get_latency(struct pcm *pcm);  
  37.   
  38. /* Returns available frames in pcm buffer and corresponding time stamp. 
  39.  * The clock is CLOCK_MONOTONIC if flag PCM_MONOTONIC was specified in pcm_open, 
  40.  * otherwise the clock is CLOCK_REALTIME. 
  41.  * For an input stream, frames available are frames ready for the 
  42.  * application to read. 
  43.  * For an output stream, frames available are the number of empty frames available 
  44.  * for the application to write. 
  45.  */  
  46. int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,  
  47.                        struct timespec *tstamp);  
  48.   
  49. /* Write data to the fifo. 
  50.  * Will start playback on the first write or on a write that 
  51.  * occurs after a fifo underrun. 
  52.  */  
  53. int pcm_write(struct pcm *pcm, const void *data, unsigned int count);  
  54. int pcm_read(struct pcm *pcm, void *data, unsigned int count);  
  55.   
  56. /* 
  57.  * mmap() support. 
  58.  */  
  59. int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);  
  60. int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count);  
  61. int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,  
  62.                    unsigned int *frames);  
  63. int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);  
  64.   
  65. /* Start and stop a PCM channel that doesn't transfer data */  
  66. int pcm_start(struct pcm *pcm);  
  67. int pcm_stop(struct pcm *pcm);  
  68.   
  69. /* Interrupt driven API */  
  70. int pcm_wait(struct pcm *pcm, int timeout);  
  71.   
  72. /* Change avail_min after the stream has been opened with no need to stop the stream. 
  73.  * Only accepted if opened with PCM_MMAP and PCM_NOIRQ flags 
  74.  */  
  75. int pcm_set_avail_min(struct pcm *pcm, int avail_min);  


值得一提的是 HAL 现在不包含 route 的操作, audio route 交给了 libaudioroute.so , 它也是调用 tinyalsa 的接口,并包含一个 xml 解析器,解析 mixer_paths.xml 里面的 route 配置数据。这样系统就可以对 alsa 的 pcm 和 mixer 进行操作了,理论上应该可以放音了,使用 tinyalsa 提供的工具可以进行测试,当然无法测试 HAL 的接口。
tinycap      tinymix      tinypcminfo  tinyplay

tinyplay 可以播放 wav 格式的纯音频数据。 tinymix 可以查看和配置音频路径:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. root@sama5d3:/ # tinymix                                                         
  2. Mixer name: 'wm8904 @ SAMA5D3EK'  
  3. Number of controls: 41  
  4. ctl     type    num     name                                     value  
  5. 0       INT     1       EQ1 Volume                               12  
  6. 1       INT     1       EQ2 Volume                               12  
  7. 2       INT     1       EQ3 Volume                               12  
  8. 3       INT     1       EQ4 Volume                               12  
  9. 4       INT     1       EQ5 Volume                               12  
  10. 5       INT     2       Digital Capture Volume                   96 96  
  11. 6       ENUM    1       Left Caputure Mode                       Single-Ended  
  12. 7       ENUM    1       Right Capture Mode                       Single-Ended  
  13. 8       INT     2       Capture Volume                           5 5  
  14. 9       BOOL    2       Capture Switch                           Off Off  
  15. 10      BOOL    1       High Pass Filter Switch                  On  
  16. 11      ENUM    1       High Pass Filter Mode                    Hi-fi  
  17. 12      BOOL    1       ADC 128x OSR Switch                      On  
  18. 13      INT     1       Digital Playback Boost Volume            0  
  19. 14      INT     2       Digital Playback Volume                  96 96  
  20. 15      INT     2       Headphone Volume                         45 45  
  21. 16      BOOL    2       Headphone Switch                         On On  
  22. 17      BOOL    2       Headphone ZC Switch                      On On  
  23. 18      INT     2       Line Output Volume                       57 57  
  24. 19      BOOL    2       Line Output Switch                       On On  
  25. 20      BOOL    2       Line Output ZC Switch                    On On  
  26. 21      BOOL    1       EQ Switch                                Off  
  27. 22      BOOL    1       DRC Switch                               Off  
  28. 23      ENUM    1       DRC Path                                 ADC  
  29. 24      BOOL    1       DAC OSRx2 Switch                         Off  
  30. 25      BOOL    1       DAC Deemphasis Switch                    Off  
  31. 26      INT     2       Digital Sidetone Volume                  0 0  
  32. 27      ENUM    1       LINER Mux                                DAC  
  33. 28      ENUM    1       LINEL Mux                                DAC  
  34. 29      ENUM    1       HPR Mux                                  DAC  
  35. 30      ENUM    1       HPL Mux                                  DAC  
  36. 31      ENUM    1       Right Sidetone                           None  
  37. 32      ENUM    1       Left Sidetone                            None  
  38. 33      ENUM    1       DACR Mux                                 Right  
  39. 34      ENUM    1       DACL Mux                                 Left  
  40. 35      ENUM    1       AIFOUTR Mux                              Right  
  41. 36      ENUM    1       AIFOUTL Mux                              Left  
  42. 37      ENUM    1       Right Capture Inverting Mux              IN1R  
  43. 38      ENUM    1       Right Capture Mux                        IN2R  
  44. 39      ENUM    1       Left Capture Inverting Mux               IN1L  
  45. 40      ENUM    1       Left Capture Mux                         IN2L  


audioflinger

audioflinger 是 audio 音频服务器,它会加载 audio hal ,并处理 audio 应用发出音频请求。这个分析的有很多,参考以下:

主题推荐
android 文件系统 用户体验 application framework
猜你在找
android录音和得到音量
Android 录音(MediaRecorder)与播放(MediaPlayer)
嵌入式的苦逼从何而来
创建mediaserver的过程
切换音效会导致有杂音
PCM数据格式
Android 4.4 Graphic系统详解(4) 一个view的绘制之旅
Android 如何永久性开启adb 的root权限 M
boot loader,Linux 内核,文件系统之间的关系
修改android mediascanner扫描路径
id="ad_frm_0" frameborder="0" scrolling="no" src="http://blog.csdn.net/common/ad.html?t=4&containerId=ad_cen&frmId=ad_frm_0" style="border-width: 0px; overflow: hidden; width: 984px; height: 90px;">
查看评论
1楼  chroman 2014-06-30 13:45发表 [回复] [引用] [举报]
博主你好,我现在在nexus 7上将tinycap放到/system/bin下,按照如下方式在adb shell中运行,一直出现参数不正确的问题。
参数我自己改过各种了,还是这些问题。求教问题可能出现在哪里?
root@flo:/dev/snd # tinycap /storage/sdcard0/aaa.wav -d 0 -c 2 -r 44100 -b 16 
Unable to open PCM device (cannot open device '/dev/snd/pcmC0D0c': Invalid argument)
Re:  无才顽石 2014-07-02 15:09发表 [回复] [引用] [举报]
回复chroman:要查看手机的参数,我这里是可以的,参数是算出来的,你参考了:
​tinycap test.wav -D 0 -d 0 -c 2 -r 44100 -b 16 -p 512 -n 8
root@sama5d3:/proc/asound/card0/pcm0c/sub0 # cat hw_params 
access: RW_INTERLEAVED
format: S16_LE
subformat: STD
channels: 2
rate: 44100 (44100/1)
period_size: 512
buffer_size: 4096
root@sama5d3:/proc/asound/card0/pcm0c/sub0 # cat sw_params 
tstamp_mode: NONE
period_step: 1
avail_min: 1
start_threshold: 1
Re:  chroman 2014-07-04 12:45发表 [回复]
回复jingxia2008:设置相同参数了,还是有问题不能截获音频,但如果用应用录音的话再tinycap就阻塞了。想请问一下从这里截获的音频和用java api AudioRecord获得的音频数据会有很大区别吗?在AudioRecord中感觉会有去噪,AudioRecord是否就是直接把硬件的数据读取后给上层而不做更多的去噪处理?
Re:  chroman 2014-07-04 11:40发表 [回复] [引用] [举报]
回复jingxia2008:好的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值