blob: 82cb111be28350d86b6e35c355813844b7709ab0 [file] [log] [blame]
Clay Murphy47b1d3f2013-10-03 10:02:22 -07001page.title=Audio
2@jd:body
3
4<!--
Rom Lemarchand44dda732015-04-03 15:10:10 -07005 Copyright 2015 The Android Open Source Project
Clay Murphy47b1d3f2013-10-03 10:02:22 -07006
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 Miller54362a62014-10-24 16:00:04 -070027<p>This page explains how to implement the audio Hardware Abstraction Layer (HAL) and configure the
28shared library.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070029
Heidi Miller54362a62014-10-24 16:00:04 -070030<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 Murphy47b1d3f2013-10-03 10:02:22 -070034<ul>
Heidi Miller54362a62014-10-24 16:00:04 -070035<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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070041</ul>
Heidi Miller54362a62014-10-24 16:00:04 -070042
43<p>For an example, refer to the implementation for the Galaxy Nexus at
44<code>device/samsung/tuna/audio</code>.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070045
46<p>In addition to implementing the HAL, you need to create a
Heidi Miller54362a62014-10-24 16:00:04 -070047<code>device/&lt;company_name&gt;/&lt;device_name&gt;/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
Rom Lemarchand44dda732015-04-03 15:10:10 -070050audio 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 Murphy47b1d3f2013-10-03 10:02:22 -070059
Heidi Miller54362a62014-10-24 16:00:04 -070060<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
Clay Murphy47b1d3f2013-10-03 10:02:22 -070079<pre>
80audio_hw_modules {
81 primary {
82 outputs {
83 ...
Heidi Miller54362a62014-10-24 16:00:04 -070084 hdmi {
Clay Murphy47b1d3f2013-10-03 10:02:22 -070085 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 Miller54362a62014-10-24 16:00:04 -070099<p>AudioFlinger's mixer downmixes the content to stereo automatically when sent to an audio device
100that does not support multichannel audio.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700101
102<h3 id="codecs">Media codecs</h3>
103
Heidi Miller54362a62014-10-24 16:00:04 -0700104<p>Ensure the audio codecs your hardware and drivers support are properly declared for your
Clay Murphy714cd072014-12-01 13:07:52 -0800105product. For details on declaring supported codecs, see <a href="{@docRoot}devices/media.html#expose">Exposing Codecs
Heidi Miller54362a62014-10-24 16:00:04 -0700106to the Framework</a>.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700107
Heidi Miller54362a62014-10-24 16:00:04 -0700108<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
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700113<ol>
Heidi Miller54362a62014-10-24 16:00:04 -0700114<li>Create a <code>device/&lt;company_name&gt;/&lt;device_name&gt;/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:
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700118<pre>
119LOCAL_MODULE := audio.primary.&lt;device_name&gt;
120</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700121
122<p>Notice your library must be named <code>audio_primary.&lt;device_name&gt;.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.&lt;device_name&gt;</code> and
126<code>audio.usb.&lt;device_name&gt;</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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700131LOCAL_PATH := $(call my-dir)
132
133include $(CLEAR_VARS)
134
135LOCAL_MODULE := audio.primary.tuna
Clay Murphyccbdc672014-03-11 20:41:29 +0000136LOCAL_MODULE_RELATIVE_PATH := hw
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700137LOCAL_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>
Heidi Miller54362a62014-10-24 16:00:04 -0700147
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/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile:
153
154<pre>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700155PRODUCT_COPY_FILES := ...
156
157PRODUCT_COPY_FILES += \
158frameworks/native/data/etc/android.android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \
159</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700160
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/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile. For example:
166
167<pre>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700168PRODUCT_COPY_FILES += \
169 device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf
170</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700171
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/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile. For
176example, the Galaxy Nexus requires the primary and bluetooth audio HAL modules:
177
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700178<pre>
179PRODUCT_PACKAGES += \
180 audio.primary.tuna \
181 audio.a2dp.default
182</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700183
184</li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700185</ol>
186
187<h2 id="preprocessing">Audio pre-processing effects</h2>
Heidi Miller54362a62014-10-24 16:00:04 -0700188
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
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700194<ul>
Heidi Miller54362a62014-10-24 16:00:04 -0700195<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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700204</ul>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700205
206
Heidi Miller54362a62014-10-24 16:00:04 -0700207<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
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700214<ul>
Glenn Kasten795a9de2014-01-24 08:58:56 -0800215<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 Miller54362a62014-10-24 16:00:04 -0700222<li><code>android.media.MediaRecorder.AudioSource.DEFAULT</code></li> </ul>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700223
Heidi Miller54362a62014-10-24 16:00:04 -0700224<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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700231
Heidi Miller54362a62014-10-24 16:00:04 -0700232<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
Clay Murphy714cd072014-12-01 13:07:52 -0800236href="{@docRoot}compatibility/index.html"> compatibility requirement</a> regardless of whether this was on by
Heidi Miller54362a62014-10-24 16:00:04 -0700237default due to configuration file , or the audio HAL implementation's default behavior.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700238
Heidi Miller54362a62014-10-24 16:00:04 -0700239<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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700242
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>
Heidi Miller54362a62014-10-24 16:00:04 -0700256
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>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700259
Clay Murphy5d83ab42014-09-09 17:29:09 -0700260<p>The requirements for voice recognition are:</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700261
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 Miller54362a62014-10-24 16:00:04 -0700268<li>Effects/pre-processing must be disabled by default</li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700269</ul>
270
271<p>Examples of tuning different effects for different sources are:</p>
272
273<ul>
Heidi Miller54362a62014-10-24 16:00:04 -0700274<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 Murphy47b1d3f2013-10-03 10:02:22 -0700286</ul>
287
288<h3 id="more">More information</h3>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700289
Heidi Miller54362a62014-10-24 16:00:04 -0700290<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>
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
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700301<li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li>
302</ul>