Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 1 | page.title=Audio |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Rom Lemarchand | 44dda73 | 2015-04-03 15:10:10 -0700 | [diff] [blame] | 5 | Copyright 2015 The Android Open Source Project |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 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 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 27 | <p>This page explains how to implement the audio Hardware Abstraction Layer (HAL) and configure the |
| 28 | shared library.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 29 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 34 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 35 | <li><code>hardware/libhardware/include/hardware/audio.h</code> - represents the main functions |
| 36 | of an audio device.</li> |
| 37 | <li><code>hardware/libhardware/include/hardware/audio_policy.h</code> - represents the audio policy |
| 38 | manager, 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 |
| 40 | be applied to audio such as downmixing, echo, or noise suppression.</li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 41 | </ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 42 | |
| 43 | <p>For an example, refer to the implementation for the Galaxy Nexus at |
| 44 | <code>device/samsung/tuna/audio</code>.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 45 | |
| 46 | <p>In addition to implementing the HAL, you need to create a |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 47 | <code>device/<company_name>/<device_name>/audio/audio_policy.conf</code> file that |
| 48 | declares the audio devices present on your product. For an example, see the file for the Galaxy |
| 49 | Nexus audio hardware in <code>device/samsung/tuna/audio/audio_policy.conf</code>. Also, see the |
Rom Lemarchand | 44dda73 | 2015-04-03 15:10:10 -0700 | [diff] [blame] | 50 | audio 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> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 59 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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 |
| 63 | stream directly to the audio hardware. This bypasses the AudioFlinger mixer so it doesn't get |
| 64 | downmixed to two channels.</p> |
| 65 | |
| 66 | <p>The audio HAL must expose whether an output stream profile supports multichannel audio |
| 67 | capabilities. If the HAL exposes its capabilities, the default policy manager allows multichannel |
| 68 | playback over HDMI.</p> |
| 69 | |
| 70 | <p>For more implementation details, see the <code>device/samsung/tuna/audio/audio_hw.c</code> in |
| 71 | the 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 |
| 75 | following is an example from the Galaxy Nexus that shows a "dynamic" channel mask, which means the |
| 76 | audio policy manager queries the actual channel masks supported by the HDMI sink after connection. |
| 77 | You can also specify a static channel mask like <code>AUDIO_CHANNEL_OUT_5POINT1</code>.</p> |
| 78 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 79 | <pre> |
| 80 | audio_hw_modules { |
| 81 | primary { |
| 82 | outputs { |
| 83 | ... |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 84 | hdmi { |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 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 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 99 | <p>AudioFlinger's mixer downmixes the content to stereo automatically when sent to an audio device |
| 100 | that does not support multichannel audio.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 101 | |
| 102 | <h3 id="codecs">Media codecs</h3> |
| 103 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 104 | <p>Ensure the audio codecs your hardware and drivers support are properly declared for your |
Clay Murphy | 714cd07 | 2014-12-01 13:07:52 -0800 | [diff] [blame] | 105 | product. For details on declaring supported codecs, see <a href="{@docRoot}devices/media.html#expose">Exposing Codecs |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 106 | to the Framework</a>.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 107 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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 |
| 111 | location by creating an <code>Android.mk</code> file:</p> |
| 112 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 113 | <ol> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 114 | <li>Create a <code>device/<company_name>/<device_name>/audio</code> directory to |
| 115 | contain your library's source files.</li> |
| 116 | <li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the Makefile |
| 117 | contains the following line: |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 118 | <pre> |
| 119 | LOCAL_MODULE := audio.primary.<device_name> |
| 120 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 121 | |
| 122 | <p>Notice your library must be named <code>audio_primary.<device_name>.so</code> so |
| 123 | that Android can correctly load the library. The "<code>primary</code>" portion of this filename |
| 124 | indicates that this shared library is for the primary audio hardware located on the device. The |
| 125 | module names <code>audio.a2dp.<device_name></code> and |
| 126 | <code>audio.usb.<device_name></code> are also available for bluetooth and USB audio |
| 127 | interfaces. Here is an example of an <code>Android.mk</code> from the Galaxy Nexus audio hardware: |
| 128 | </p> |
| 129 | |
| 130 | <pre> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 131 | LOCAL_PATH := $(call my-dir) |
| 132 | |
| 133 | include $(CLEAR_VARS) |
| 134 | |
| 135 | LOCAL_MODULE := audio.primary.tuna |
Clay Murphy | ccbdc67 | 2014-03-11 20:41:29 +0000 | [diff] [blame] | 136 | LOCAL_MODULE_RELATIVE_PATH := hw |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 137 | LOCAL_SRC_FILES := audio_hw.c ril_interface.c |
| 138 | LOCAL_C_INCLUDES += \ |
| 139 | external/tinyalsa/include \ |
| 140 | $(call include-path-for, audio-utils) \ |
| 141 | $(call include-path-for, audio-effects) |
| 142 | LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils libdl |
| 143 | LOCAL_MODULE_TAGS := optional |
| 144 | |
| 145 | include $(BUILD_SHARED_LIBRARY) |
| 146 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 147 | |
| 148 | </li> |
| 149 | |
| 150 | <li>If your product supports low latency audio as specified by the Android CDD, copy the |
| 151 | corresponding 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> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 155 | PRODUCT_COPY_FILES := ... |
| 156 | |
| 157 | PRODUCT_COPY_FILES += \ |
| 158 | frameworks/native/data/etc/android.android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \ |
| 159 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 168 | PRODUCT_COPY_FILES += \ |
| 169 | device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf |
| 170 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 171 | |
| 172 | </li> |
| 173 | |
| 174 | <li>Declare the shared modules of your audio HAL that are required by your product in the |
| 175 | product's <code>device/<company_name>/<device_name>/device.mk</code> Makefile. For |
| 176 | example, the Galaxy Nexus requires the primary and bluetooth audio HAL modules: |
| 177 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 178 | <pre> |
| 179 | PRODUCT_PACKAGES += \ |
| 180 | audio.primary.tuna \ |
| 181 | audio.a2dp.default |
| 182 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 183 | |
| 184 | </li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 185 | </ol> |
| 186 | |
| 187 | <h2 id="preprocessing">Audio pre-processing effects</h2> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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 |
| 192 | following pre-processing effects are supported:</p> |
| 193 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 194 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 195 | <li> |
| 196 | <a href="http://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler.html"> |
| 197 | Acoustic Echo Cancellation</a></li> |
| 198 | <li> |
| 199 | <a href="http://developer.android.com/reference/android/media/audiofx/AutomaticGainControl.html"> |
| 200 | Automatic Gain Control</a></li> |
| 201 | <li> |
| 202 | <a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> |
| 203 | Noise Suppression</a></li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 204 | </ul> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 205 | |
| 206 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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 |
| 209 | developers request to use the <code>AudioSource</code> abstraction instead of the actual audio |
| 210 | hardware device. The Android Audio Policy Manager maps an <code>AudioSource</code> to the actual |
| 211 | hardware with <code>AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)</code>. The |
| 212 | following sources are exposed to developers:</p> |
| 213 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 214 | <ul> |
Glenn Kasten | 795a9de | 2014-01-24 08:58:56 -0800 | [diff] [blame] | 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> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 222 | <li><code>android.media.MediaRecorder.AudioSource.DEFAULT</code></li> </ul> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 223 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 224 | <p>The default pre-processing effects applied for each <code>AudioSource</code> are specified in |
| 225 | the <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 |
| 227 | specify the pre-processing effects to turn on. For an example, see the implementation for the Nexus |
| 228 | 10 in <code>device/samsung/manta/audio_effects.conf</code>. AudioEffect instances acquire and |
| 229 | release a session when created and destroyed, enabling the effects (such as the Loudness Enhancer) |
| 230 | to persist throughout the duration of the session. </p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 231 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 232 | <p class="warning"><strong>Warning:</strong> For the <code>VOICE_RECOGNITION</code> use case, do |
| 233 | not enable the noise suppression pre-processing effect. It should not be turned on by default when |
| 234 | recording from this audio source, and you should not enable it in your own audio_effects.conf file. |
| 235 | Turning on the effect by default will cause the device to fail the <a |
Clay Murphy | 714cd07 | 2014-12-01 13:07:52 -0800 | [diff] [blame] | 236 | href="{@docRoot}compatibility/index.html"> compatibility requirement</a> regardless of whether this was on by |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 237 | default due to configuration file , or the audio HAL implementation's default behavior.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 238 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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, |
| 241 | the framework will automatically request from the audio HAL the use of those effects.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 242 | |
| 243 | <pre> |
| 244 | pre_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> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 256 | |
| 257 | <p>For <code>AudioSource</code> tuning, there are no explicit requirements on audio gain or audio |
| 258 | processing with the exception of voice recognition (<code>VOICE_RECOGNITION</code>).</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 259 | |
Clay Murphy | 5d83ab4 | 2014-09-09 17:29:09 -0700 | [diff] [blame] | 260 | <p>The requirements for voice recognition are:</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 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> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 268 | <li>Effects/pre-processing must be disabled by default</li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 269 | </ul> |
| 270 | |
| 271 | <p>Examples of tuning different effects for different sources are:</p> |
| 272 | |
| 273 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 286 | </ul> |
| 287 | |
| 288 | <h3 id="more">More information</h3> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 289 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame] | 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"> |
| 295 | audiofx package</a> |
| 296 | |
| 297 | <li>Android documentation for |
| 298 | <a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> |
| 299 | Noise Suppression audio effect</a></li> |
| 300 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 301 | <li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li> |
| 302 | </ul> |