blob: 5adbdf1ef05448160bc39ea3f8a156fa9f6d6926 [file] [log] [blame]
Clay Murphy47b1d3f2013-10-03 10:02:22 -07001page.title=Audio
2@jd:body
3
4<!--
Heidi Miller54362a62014-10-24 16:00:04 -07005 Copyright 2014 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
50<code>system/core/include/system/audio.h</code> and
51<code>system/core/include/system/audio_policy.h</code> header files for a reference of the
52properties that you can define.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070053
Heidi Miller54362a62014-10-24 16:00:04 -070054<h3 id="multichannel">Multi-channel support</h3>
55
56<p>If your hardware and driver supports multichannel audio via HDMI, you can output the audio
57stream directly to the audio hardware. This bypasses the AudioFlinger mixer so it doesn't get
58downmixed to two channels.</p>
59
60<p>The audio HAL must expose whether an output stream profile supports multichannel audio
61capabilities. If the HAL exposes its capabilities, the default policy manager allows multichannel
62playback over HDMI.</p>
63
64<p>For more implementation details, see the <code>device/samsung/tuna/audio/audio_hw.c</code> in
65the Android 4.1 release.</p>
66
67<p>To specify that your product contains a multichannel audio output, edit the
68<code>audio_policy.conf</code> file to describe the multichannel output for your product. The
69following is an example from the Galaxy Nexus that shows a "dynamic" channel mask, which means the
70audio policy manager queries the actual channel masks supported by the HDMI sink after connection.
71You can also specify a static channel mask like <code>AUDIO_CHANNEL_OUT_5POINT1</code>.</p>
72
Clay Murphy47b1d3f2013-10-03 10:02:22 -070073<pre>
74audio_hw_modules {
75 primary {
76 outputs {
77 ...
Heidi Miller54362a62014-10-24 16:00:04 -070078 hdmi {
Clay Murphy47b1d3f2013-10-03 10:02:22 -070079 sampling_rates 44100|48000
80 channel_masks dynamic
81 formats AUDIO_FORMAT_PCM_16_BIT
82 devices AUDIO_DEVICE_OUT_AUX_DIGITAL
83 flags AUDIO_OUTPUT_FLAG_DIRECT
84 }
85 ...
86 }
87 ...
88 }
89 ...
90}
91</pre>
92
Heidi Miller54362a62014-10-24 16:00:04 -070093<p>AudioFlinger's mixer downmixes the content to stereo automatically when sent to an audio device
94that does not support multichannel audio.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -070095
96<h3 id="codecs">Media codecs</h3>
97
Heidi Miller54362a62014-10-24 16:00:04 -070098<p>Ensure the audio codecs your hardware and drivers support are properly declared for your
99product. For details on declaring supported codecs, see <a href="media.html#expose"> Exposing Codecs
100to the Framework</a>.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700101
Heidi Miller54362a62014-10-24 16:00:04 -0700102<h2 id="configuring">Configuring the shared library</h2>
103
104<p>You need to package the HAL implementation into a shared library and copy it to the appropriate
105location by creating an <code>Android.mk</code> file:</p>
106
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700107<ol>
Heidi Miller54362a62014-10-24 16:00:04 -0700108<li>Create a <code>device/&lt;company_name&gt;/&lt;device_name&gt;/audio</code> directory to
109contain your library's source files.</li>
110<li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the Makefile
111contains the following line:
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700112<pre>
113LOCAL_MODULE := audio.primary.&lt;device_name&gt;
114</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700115
116<p>Notice your library must be named <code>audio_primary.&lt;device_name&gt;.so</code> so
117that Android can correctly load the library. The "<code>primary</code>" portion of this filename
118indicates that this shared library is for the primary audio hardware located on the device. The
119module names <code>audio.a2dp.&lt;device_name&gt;</code> and
120<code>audio.usb.&lt;device_name&gt;</code> are also available for bluetooth and USB audio
121interfaces. Here is an example of an <code>Android.mk</code> from the Galaxy Nexus audio hardware:
122</p>
123
124<pre>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700125LOCAL_PATH := $(call my-dir)
126
127include $(CLEAR_VARS)
128
129LOCAL_MODULE := audio.primary.tuna
Clay Murphyccbdc672014-03-11 20:41:29 +0000130LOCAL_MODULE_RELATIVE_PATH := hw
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700131LOCAL_SRC_FILES := audio_hw.c ril_interface.c
132LOCAL_C_INCLUDES += \
133 external/tinyalsa/include \
134 $(call include-path-for, audio-utils) \
135 $(call include-path-for, audio-effects)
136LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils libdl
137LOCAL_MODULE_TAGS := optional
138
139include $(BUILD_SHARED_LIBRARY)
140</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700141
142</li>
143
144<li>If your product supports low latency audio as specified by the Android CDD, copy the
145corresponding XML feature file into your product. For example, in your product's
146<code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile:
147
148<pre>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700149PRODUCT_COPY_FILES := ...
150
151PRODUCT_COPY_FILES += \
152frameworks/native/data/etc/android.android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \
153</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700154
155</li>
156
157<li>Copy the <code>audio_policy.conf</code> file that you created earlier to the
158<code>system/etc/</code> directory in your product's
159<code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile. For example:
160
161<pre>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700162PRODUCT_COPY_FILES += \
163 device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf
164</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700165
166</li>
167
168<li>Declare the shared modules of your audio HAL that are required by your product in the
169product's <code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile. For
170example, the Galaxy Nexus requires the primary and bluetooth audio HAL modules:
171
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700172<pre>
173PRODUCT_PACKAGES += \
174 audio.primary.tuna \
175 audio.a2dp.default
176</pre>
Heidi Miller54362a62014-10-24 16:00:04 -0700177
178</li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700179</ol>
180
181<h2 id="preprocessing">Audio pre-processing effects</h2>
Heidi Miller54362a62014-10-24 16:00:04 -0700182
183<p>The Android platform provides audio effects on supported devices in the
184<a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">audiofx
185</a> package, which is available for developers to access. For example, on the Nexus 10, the
186following pre-processing effects are supported:</p>
187
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700188<ul>
Heidi Miller54362a62014-10-24 16:00:04 -0700189<li>
190<a href="http://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler.html">
191Acoustic Echo Cancellation</a></li>
192<li>
193<a href="http://developer.android.com/reference/android/media/audiofx/AutomaticGainControl.html">
194Automatic Gain Control</a></li>
195<li>
196<a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html">
197Noise Suppression</a></li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700198</ul>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700199
200
Heidi Miller54362a62014-10-24 16:00:04 -0700201<p>Pre-processing effects are paired with the use case mode in which the pre-processing is requested
202. In Android app development, a use case is referred to as an <code>AudioSource</code>; and app
203developers request to use the <code>AudioSource</code> abstraction instead of the actual audio
204hardware device. The Android Audio Policy Manager maps an <code>AudioSource</code> to the actual
205hardware with <code>AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)</code>. The
206following sources are exposed to developers:</p>
207
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700208<ul>
Glenn Kasten795a9de2014-01-24 08:58:56 -0800209<li><code>android.media.MediaRecorder.AudioSource.CAMCORDER</code></li>
210<li><code>android.media.MediaRecorder.AudioSource.VOICE_COMMUNICATION</code></li>
211<li><code>android.media.MediaRecorder.AudioSource.VOICE_CALL</code></li>
212<li><code>android.media.MediaRecorder.AudioSource.VOICE_DOWNLINK</code></li>
213<li><code>android.media.MediaRecorder.AudioSource.VOICE_UPLINK</code></li>
214<li><code>android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION</code></li>
215<li><code>android.media.MediaRecorder.AudioSource.MIC</code></li>
Heidi Miller54362a62014-10-24 16:00:04 -0700216<li><code>android.media.MediaRecorder.AudioSource.DEFAULT</code></li> </ul>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700217
Heidi Miller54362a62014-10-24 16:00:04 -0700218<p>The default pre-processing effects applied for each <code>AudioSource</code> are specified in
219the <code>/system/etc/audio_effects.conf</code> file. To specify your own default effects for every
220<code>AudioSource</code>, create a <code>/system/vendor/etc/audio_effects.conf</code> file and
221specify the pre-processing effects to turn on. For an example, see the implementation for the Nexus
22210 in <code>device/samsung/manta/audio_effects.conf</code>. AudioEffect instances acquire and
223release a session when created and destroyed, enabling the effects (such as the Loudness Enhancer)
224to persist throughout the duration of the session. </p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700225
Heidi Miller54362a62014-10-24 16:00:04 -0700226<p class="warning"><strong>Warning:</strong> For the <code>VOICE_RECOGNITION</code> use case, do
227not enable the noise suppression pre-processing effect. It should not be turned on by default when
228recording from this audio source, and you should not enable it in your own audio_effects.conf file.
229Turning on the effect by default will cause the device to fail the <a
230href="/compatibility/index.html"> compatibility requirement</a> regardless of whether this was on by
231default due to configuration file , or the audio HAL implementation's default behavior.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700232
Heidi Miller54362a62014-10-24 16:00:04 -0700233<p>The following example enables pre-processing for the VoIP <code>AudioSource</code> and Camcorder
234<code>AudioSource</code>. By declaring the <code>AudioSource</code> configuration in this manner,
235the framework will automatically request from the audio HAL the use of those effects.</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700236
237<pre>
238pre_processing {
239 voice_communication {
240 aec {}
241 ns {}
242 }
243 camcorder {
244 agc {}
245 }
246}
247</pre>
248
249<h3 id="tuning">Source tuning</h3>
Heidi Miller54362a62014-10-24 16:00:04 -0700250
251<p>For <code>AudioSource</code> tuning, there are no explicit requirements on audio gain or audio
252processing with the exception of voice recognition (<code>VOICE_RECOGNITION</code>).</p>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700253
254<p>The following are the requirements for voice recognition:</p>
255
256<ul>
257<li>"flat" frequency response (+/- 3dB) from 100Hz to 4kHz</li>
258<li>close-talk config: 90dB SPL reads RMS of 2500 (16bit samples)</li>
259<li>level tracks linearly from -18dB to +12dB relative to 90dB SPL</li>
260<li>THD < 1% (90dB SPL in 100 to 4000Hz range)</li>
261<li>8kHz sampling rate (anti-aliasing)</li>
Heidi Miller54362a62014-10-24 16:00:04 -0700262<li>Effects/pre-processing must be disabled by default</li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700263</ul>
264
265<p>Examples of tuning different effects for different sources are:</p>
266
267<ul>
Heidi Miller54362a62014-10-24 16:00:04 -0700268<li>Noise Suppressor
269<ul>
270<li>Tuned for wind noise suppressor for <code>CAMCORDER</code></li>
271<li>Tuned for stationary noise suppressor for <code>VOICE_COMMUNICATION</code></li>
272</ul>
273</li>
274<li>Automatic Gain Control
275<ul>
276<li>Tuned for close-talk for <code>VOICE_COMMUNICATION</code> and main phone mic</li>
277<li>Tuned for far-talk for <code>CAMCORDER</code></li>
278</ul>
279</li>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700280</ul>
281
282<h3 id="more">More information</h3>
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700283
Heidi Miller54362a62014-10-24 16:00:04 -0700284<p>For more information, see:</p>
285
286<ul>
287<li>Android documentation for
288<a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">
289audiofx package</a>
290
291<li>Android documentation for
292<a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html">
293Noise Suppression audio effect</a></li>
294
Clay Murphy47b1d3f2013-10-03 10:02:22 -0700295<li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li>
Heidi Miller54362a62014-10-24 16:00:04 -0700296</ul>