1page.title=Audio Implementation 2@jd:body 3 4<!-- 5 Copyright 2015 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18--> 19<div id="qv-wrapper"> 20 <div id="qv"> 21 <h2>In this document</h2> 22 <ol id="auto-toc"> 23 </ol> 24 </div> 25</div> 26 27<p>This page explains how to implement the audio Hardware Abstraction Layer (HAL) and configure the 28shared library.</p> 29 30<h2 id="implementing">Implementing the HAL</h2> 31 32<p>The audio HAL is composed of three different interfaces that you must implement:</p> 33 34<ul> 35<li><code>hardware/libhardware/include/hardware/audio.h</code> - represents the main functions 36of an audio device.</li> 37<li><code>hardware/libhardware/include/hardware/audio_policy.h</code> - represents the audio policy 38manager, which handles things like audio routing and volume control policies.</li> 39<li><code>hardware/libhardware/include/hardware/audio_effect.h</code> - represents effects that can 40be applied to audio such as downmixing, echo, or noise suppression.</li> 41</ul> 42 43<p>For an example, refer to the implementation for the Galaxy Nexus at 44<code>device/samsung/tuna/audio</code>.</p> 45 46<p>In addition to implementing the HAL, you need to create a 47<code>device/<company_name>/<device_name>/audio/audio_policy.conf</code> file that 48declares the audio devices present on your product. For an example, see the file for the Galaxy 49Nexus audio hardware in <code>device/samsung/tuna/audio/audio_policy.conf</code>. Also, see the 50audio header files for a reference of the properties that you can define.</p> 51 52<p>In the Android M release and later, the paths are:<br /> 53<code>system/media/audio/include/system/audio.h</code><br /> 54<code>system/media/audio/include/system/audio_policy.h</code></p> 55 56<p>In Android 5.1 and earlier, the paths are:<br /> 57<code>system/core/include/system/audio.h</code><br /> 58<code>system/core/include/system/audio_policy.h</code></p> 59 60<h3 id="multichannel">Multi-channel support</h3> 61 62<p>If your hardware and driver supports multichannel audio via HDMI, you can output the audio 63stream directly to the audio hardware. This bypasses the AudioFlinger mixer so it doesn't get 64downmixed to two channels.</p> 65 66<p>The audio HAL must expose whether an output stream profile supports multichannel audio 67capabilities. If the HAL exposes its capabilities, the default policy manager allows multichannel 68playback over HDMI.</p> 69 70<p>For more implementation details, see the <code>device/samsung/tuna/audio/audio_hw.c</code> in 71the Android 4.1 release.</p> 72 73<p>To specify that your product contains a multichannel audio output, edit the 74<code>audio_policy.conf</code> file to describe the multichannel output for your product. The 75following is an example from the Galaxy Nexus that shows a "dynamic" channel mask, which means the 76audio policy manager queries the actual channel masks supported by the HDMI sink after connection. 77You can also specify a static channel mask like <code>AUDIO_CHANNEL_OUT_5POINT1</code>.</p> 78 79<pre> 80audio_hw_modules { 81 primary { 82 outputs { 83 ... 84 hdmi { 85 sampling_rates 44100|48000 86 channel_masks dynamic 87 formats AUDIO_FORMAT_PCM_16_BIT 88 devices AUDIO_DEVICE_OUT_AUX_DIGITAL 89 flags AUDIO_OUTPUT_FLAG_DIRECT 90 } 91 ... 92 } 93 ... 94 } 95 ... 96} 97</pre> 98 99<p>AudioFlinger's mixer downmixes the content to stereo automatically when sent to an audio device 100that does not support multichannel audio.</p> 101 102<h3 id="codecs">Media codecs</h3> 103 104<p>Ensure the audio codecs your hardware and drivers support are properly declared for your 105product. For details on declaring supported codecs, see <a href="{@docRoot}devices/media.html#expose">Exposing Codecs 106to the Framework</a>.</p> 107 108<h2 id="configuring">Configuring the shared library</h2> 109 110<p>You need to package the HAL implementation into a shared library and copy it to the appropriate 111location by creating an <code>Android.mk</code> file:</p> 112 113<ol> 114<li>Create a <code>device/<company_name>/<device_name>/audio</code> directory to 115contain your library's source files.</li> 116<li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the Makefile 117contains the following line: 118<pre> 119LOCAL_MODULE := audio.primary.<device_name> 120</pre> 121 122<p>Notice your library must be named <code>audio.primary.<device_name>.so</code> so 123that Android can correctly load the library. The "<code>primary</code>" portion of this filename 124indicates that this shared library is for the primary audio hardware located on the device. The 125module names <code>audio.a2dp.<device_name></code> and 126<code>audio.usb.<device_name></code> are also available for bluetooth and USB audio 127interfaces. Here is an example of an <code>Android.mk</code> from the Galaxy Nexus audio hardware: 128</p> 129 130<pre> 131LOCAL_PATH := $(call my-dir) 132 133include $(CLEAR_VARS) 134 135LOCAL_MODULE := audio.primary.tuna 136LOCAL_MODULE_RELATIVE_PATH := hw 137LOCAL_SRC_FILES := audio_hw.c ril_interface.c 138LOCAL_C_INCLUDES += \ 139 external/tinyalsa/include \ 140 $(call include-path-for, audio-utils) \ 141 $(call include-path-for, audio-effects) 142LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils libdl 143LOCAL_MODULE_TAGS := optional 144 145include $(BUILD_SHARED_LIBRARY) 146</pre> 147 148</li> 149 150<li>If your product supports low latency audio as specified by the Android CDD, copy the 151corresponding XML feature file into your product. For example, in your product's 152<code>device/<company_name>/<device_name>/device.mk</code> Makefile: 153 154<pre> 155PRODUCT_COPY_FILES := ... 156 157PRODUCT_COPY_FILES += \ 158frameworks/native/data/etc/android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \ 159</pre> 160 161</li> 162 163<li>Copy the <code>audio_policy.conf</code> file that you created earlier to the 164<code>system/etc/</code> directory in your product's 165<code>device/<company_name>/<device_name>/device.mk</code> Makefile. For example: 166 167<pre> 168PRODUCT_COPY_FILES += \ 169 device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf 170</pre> 171 172</li> 173 174<li>Declare the shared modules of your audio HAL that are required by your product in the 175product's <code>device/<company_name>/<device_name>/device.mk</code> Makefile. For 176example, the Galaxy Nexus requires the primary and bluetooth audio HAL modules: 177 178<pre> 179PRODUCT_PACKAGES += \ 180 audio.primary.tuna \ 181 audio.a2dp.default 182</pre> 183 184</li> 185</ol> 186 187<h2 id="preprocessing">Audio pre-processing effects</h2> 188 189<p>The Android platform provides audio effects on supported devices in the 190<a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">audiofx 191</a> package, which is available for developers to access. For example, on the Nexus 10, the 192following pre-processing effects are supported:</p> 193 194<ul> 195<li> 196<a href="http://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler.html"> 197Acoustic Echo Cancellation</a></li> 198<li> 199<a href="http://developer.android.com/reference/android/media/audiofx/AutomaticGainControl.html"> 200Automatic Gain Control</a></li> 201<li> 202<a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> 203Noise Suppression</a></li> 204</ul> 205 206 207<p>Pre-processing effects are paired with the use case mode in which the pre-processing is requested 208. In Android app development, a use case is referred to as an <code>AudioSource</code>; and app 209developers request to use the <code>AudioSource</code> abstraction instead of the actual audio 210hardware device. The Android Audio Policy Manager maps an <code>AudioSource</code> to the actual 211hardware with <code>AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)</code>. The 212following sources are exposed to developers:</p> 213 214<ul> 215<li><code>android.media.MediaRecorder.AudioSource.CAMCORDER</code></li> 216<li><code>android.media.MediaRecorder.AudioSource.VOICE_COMMUNICATION</code></li> 217<li><code>android.media.MediaRecorder.AudioSource.VOICE_CALL</code></li> 218<li><code>android.media.MediaRecorder.AudioSource.VOICE_DOWNLINK</code></li> 219<li><code>android.media.MediaRecorder.AudioSource.VOICE_UPLINK</code></li> 220<li><code>android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION</code></li> 221<li><code>android.media.MediaRecorder.AudioSource.MIC</code></li> 222<li><code>android.media.MediaRecorder.AudioSource.DEFAULT</code></li> </ul> 223 224<p>The default pre-processing effects applied for each <code>AudioSource</code> are specified in 225the <code>/system/etc/audio_effects.conf</code> file. To specify your own default effects for every 226<code>AudioSource</code>, create a <code>/system/vendor/etc/audio_effects.conf</code> file and 227specify the pre-processing effects to turn on. For an example, see the implementation for the Nexus 22810 in <code>device/samsung/manta/audio_effects.conf</code>. AudioEffect instances acquire and 229release a session when created and destroyed, enabling the effects (such as the Loudness Enhancer) 230to persist throughout the duration of the session. </p> 231 232<p class="warning"><strong>Warning:</strong> For the <code>VOICE_RECOGNITION</code> use case, do 233not enable the noise suppression pre-processing effect. It should not be turned on by default when 234recording from this audio source, and you should not enable it in your own audio_effects.conf file. 235Turning on the effect by default will cause the device to fail the <a 236href="{@docRoot}compatibility/index.html"> compatibility requirement</a> regardless of whether this was on by 237default due to configuration file , or the audio HAL implementation's default behavior.</p> 238 239<p>The following example enables pre-processing for the VoIP <code>AudioSource</code> and Camcorder 240<code>AudioSource</code>. By declaring the <code>AudioSource</code> configuration in this manner, 241the framework will automatically request from the audio HAL the use of those effects.</p> 242 243<pre> 244pre_processing { 245 voice_communication { 246 aec {} 247 ns {} 248 } 249 camcorder { 250 agc {} 251 } 252} 253</pre> 254 255<h3 id="tuning">Source tuning</h3> 256 257<p>For <code>AudioSource</code> tuning, there are no explicit requirements on audio gain or audio 258processing with the exception of voice recognition (<code>VOICE_RECOGNITION</code>).</p> 259 260<p>The requirements for voice recognition are:</p> 261 262<ul> 263<li>"flat" frequency response (+/- 3dB) from 100Hz to 4kHz</li> 264<li>close-talk config: 90dB SPL reads RMS of 2500 (16bit samples)</li> 265<li>level tracks linearly from -18dB to +12dB relative to 90dB SPL</li> 266<li>THD < 1% (90dB SPL in 100 to 4000Hz range)</li> 267<li>8kHz sampling rate (anti-aliasing)</li> 268<li>Effects/pre-processing must be disabled by default</li> 269</ul> 270 271<p>Examples of tuning different effects for different sources are:</p> 272 273<ul> 274<li>Noise Suppressor 275<ul> 276<li>Tuned for wind noise suppressor for <code>CAMCORDER</code></li> 277<li>Tuned for stationary noise suppressor for <code>VOICE_COMMUNICATION</code></li> 278</ul> 279</li> 280<li>Automatic Gain Control 281<ul> 282<li>Tuned for close-talk for <code>VOICE_COMMUNICATION</code> and main phone mic</li> 283<li>Tuned for far-talk for <code>CAMCORDER</code></li> 284</ul> 285</li> 286</ul> 287 288<h3 id="more">More information</h3> 289 290<p>For more information, see:</p> 291 292<ul> 293<li>Android documentation for 294<a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html"> 295audiofx package</a></li> 296 297<li>Android documentation for 298<a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> 299Noise Suppression audio effect</a></li> 300 301<li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li> 302</ul> 303