blob: 1192830c1ac72f8d8b267b6c962bb25c275bdecd [file] [log] [blame]
Robert Ly35f2fda2013-01-29 16:27:05 -08001page.title=Audio
2@jd:body
3
4<!--
5 Copyright 2010 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>
Clay Murphyc28f2372013-09-25 16:13:40 -070028 Android's audio Hardware Abstraction Layer (HAL) connects the higher level, audio-specific
Robert Ly35f2fda2013-01-29 16:27:05 -080029 framework APIs in <a href="http://developer.android.com/reference/android/media/package-summary.html">android.media</a>
30 to the underlying audio driver and hardware.
31</p>
32
33<p>
34 The following figure and list describe how audio functionality is implemented and the relevant
35 source code that is involved in the implementation:
36</p>
37<p>
38 <img src="images/audio_hal.png">
39</p>
40<dl>
41 <dt>
42 Application framework
43 </dt>
44 <dd>
45 At the application framework level is the app code, which utilizes the
46 <a href="http://developer.android.com/reference/android/media/package-summary.html">android.media</a>
47 APIs to interact with the audio hardware. Internally, this code calls corresponding JNI glue
Clay Murphyc28f2372013-09-25 16:13:40 -070048 classes to access the native code that interacts with the audio hardware.
Robert Ly35f2fda2013-01-29 16:27:05 -080049 </dd>
50 <dt>
51 JNI
52 </dt>
53 <dd>
54 The JNI code associated with <a href="http://developer.android.com/reference/android/media/package-summary.html">android.media</a> is located in the
55 <code>frameworks/base/core/jni/</code> and <code>frameworks/base/media/jni</code> directories.
56 This code calls the lower level native code to obtain access to the audio hardware.
57 </dd>
58 <dt>
59 Native framework
60 </dt>
61 <dd>
62 The native framework is defined in <code>frameworks/av/media/libmedia</code> and provides a
63 native equivalent to the <a href="http://developer.android.com/reference/android/media/package-summary.html">android.media</a> package. The native framework calls the Binder
64 IPC proxies to obtain access to audio-specific services of the media server.
65 </dd>
66 <dt>
67 Binder IPC
68 </dt>
69 <dd>
70 The Binder IPC proxies facilitate communication over process boundaries. They are located in
71 the <code>frameworks/av/media/libmedia</code> directory and begin with the letter "I".
72 </dd>
73 <dt>
74 Media Server
75 </dt>
76 <dd>
77 The audio services in the media server, located in
78 <code>frameworks/av/services/audioflinger</code>, is the actual code that interacts with your
79 HAL implementations.
80 </dd>
81 <dt>
82 HAL
83 </dt>
84 <dd>
Clay Murphyc28f2372013-09-25 16:13:40 -070085 The HAL defines the standard interface that audio services call into
Robert Ly35f2fda2013-01-29 16:27:05 -080086 and that you must implement to have your audio hardware function correctly. The audio HAL
Clay Murphyc28f2372013-09-25 16:13:40 -070087 interfaces are located in
88<code>hardware/libhardware/include/hardware</code>. See <a
89href="http://source.android.com/devices/reference/audio_8h_source.html">audio.h</a> for additional details.
Robert Ly35f2fda2013-01-29 16:27:05 -080090 </dd>
91 <dt>
92 Kernel Driver
93 </dt>
94 <dd>
95 The audio driver interacts with the hardware and your implementation of the HAL. You can choose
96 to use ALSA, OSS, or a custom driver of your own at this level. The HAL is driver-agnostic.
97 <p>
98 <strong>Note:</strong> If you do choose ALSA, we recommend using <code>external/tinyalsa</code>
99 for the user portion of the driver because of its compatible licensing (The standard user-mode
100 library is GPL licensed).
101</p>
102 </dd>
103</dl>
104<h2 id="implementing">
105 Implementing the HAL
106</h2>
107<p>
108 The audio HAL is composed of three different interfaces that you must implement:
109</p>
110<ul>
111 <li>
112 <code>hardware/libhardware/include/hardware/audio.h</code> - represents the main functions of
113 an audio device.
114 </li>
115 <li>
116 <code>hardware/libhardware/include/hardware/audio_policy.h</code> - represents the audio policy
117 manager, which handles things like audio routing and volume control policies.
118 </li>
119 <li>
120 <code>hardware/libhardware/include/hardware/audio_effect.h</code> - represents effects that can
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700121 be applied to audio such as downmixing, echo, or noise suppression.
Robert Ly35f2fda2013-01-29 16:27:05 -0800122 </li>
123</ul>
124<p>See the implementation for the Galaxy Nexus at <code>device/samsung/tuna/audio</code> for an example.</p>
125
126<p>In addition to implementing the HAL, you need to create a
127 <code>device/&lt;company_name&gt;/&lt;device_name&gt;/audio/audio_policy.conf</code> file
128 that declares the audio devices present on your product. For an example, see the file for
129 the Galaxy Nexus audio hardware in <code>device/samsung/tuna/audio/audio_policy.conf</code>.
130Also, see
131 the <code>system/core/include/system/audio.h</code> and <code>system/core/include/system/audio_policy.h</code>
132 header files for a reference of the properties that you can define.
133</p>
134<h3 id="multichannel">Multi-channel support</h3>
Clay Murphyc28f2372013-09-25 16:13:40 -0700135<p>If your hardware and driver supports multichannel audio via HDMI, you can output the audio stream
Robert Ly35f2fda2013-01-29 16:27:05 -0800136 directly to the audio hardware. This bypasses the AudioFlinger mixer so it doesn't get downmixed to two channels.
137
138 <p>
Clay Murphyc28f2372013-09-25 16:13:40 -0700139 The audio HAL must expose whether an output stream profile supports multichannel audio capabilities.
Robert Ly35f2fda2013-01-29 16:27:05 -0800140 If the HAL exposes its capabilities, the default policy manager allows multichannel playback over
141 HDMI.</p>
Clay Murphyc28f2372013-09-25 16:13:40 -0700142 <p>For more implementation details, see the
143<code>device/samsung/tuna/audio/audio_hw.c</code> in the Android 4.1 release.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800144
145 <p>
146 To specify that your product contains a multichannel audio output, edit the <code>audio_policy.conf</code> file to describe the multichannel
147 output for your product. The following is an example from the Galaxy Nexus that shows a "dynamic" channel mask, which means the audio policy manager
148 queries the actual channel masks supported by the HDMI sink after connection. You can also specify a static channel mask like <code>AUDIO_CHANNEL_OUT_5POINT1</code>
149 </p>
150<pre>
151audio_hw_modules {
152 primary {
153 outputs {
154 ...
155 hdmi {
156 sampling_rates 44100|48000
157 channel_masks dynamic
158 formats AUDIO_FORMAT_PCM_16_BIT
159 devices AUDIO_DEVICE_OUT_AUX_DIGITAL
160 flags AUDIO_OUTPUT_FLAG_DIRECT
161 }
162 ...
163 }
164 ...
165 }
166 ...
167}
168</pre>
169
170
Clay Murphyc28f2372013-09-25 16:13:40 -0700171 <p>AudioFlinger's mixer downmixes the content to stereo
Robert Ly35f2fda2013-01-29 16:27:05 -0800172 automatically when sent to an audio device that does not support multichannel audio.</p>
173</p>
174
Clay Murphyc28f2372013-09-25 16:13:40 -0700175<h3 id="codecs">Media codecs</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800176
Clay Murphyc28f2372013-09-25 16:13:40 -0700177<p>Ensure the audio codecs your hardware and drivers support are properly declared for your product. See
Clay Murphy72bdea02013-06-18 16:44:01 -0700178 <a href="media.html#expose"> Exposing Codecs to the Framework</a> for information on how to do this.
Robert Ly35f2fda2013-01-29 16:27:05 -0800179</p>
180
181<h2 id="configuring">
Clay Murphyc28f2372013-09-25 16:13:40 -0700182 Configuring the shared library
Robert Ly35f2fda2013-01-29 16:27:05 -0800183</h2>
184<p>
185 You need to package the HAL implementation into a shared library and copy it to the
186 appropriate location by creating an <code>Android.mk</code> file:
187</p>
188<ol>
189 <li>Create a <code>device/&lt;company_name&gt;/&lt;device_name&gt;/audio</code> directory
190 to contain your library's source files.
191 </li>
192 <li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the
193 Makefile contains the following line:
194<pre>
195LOCAL_MODULE := audio.primary.&lt;device_name&gt;
196</pre>
197 <p>
Clay Murphyc28f2372013-09-25 16:13:40 -0700198 Notice your library must be named <code>audio_primary.&lt;device_name&gt;.so</code> so
Robert Ly35f2fda2013-01-29 16:27:05 -0800199 that Android can correctly load the library. The "<code>primary</code>" portion of this
200 filename indicates that this shared library is for the primary audio hardware located on the
201 device. The module names <code>audio.a2dp.&lt;device_name&gt;</code> and
202 <code>audio.usb.&lt;device_name&gt;</code> are also available for bluetooth and USB audio
203 interfaces. Here is an example of an <code>Android.mk</code> from the Galaxy
204 Nexus audio hardware:
205 </p>
206 <pre>
207LOCAL_PATH := $(call my-dir)
208
209include $(CLEAR_VARS)
210
211LOCAL_MODULE := audio.primary.tuna
212LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
213LOCAL_SRC_FILES := audio_hw.c ril_interface.c
214LOCAL_C_INCLUDES += \
215 external/tinyalsa/include \
216 $(call include-path-for, audio-utils) \
217 $(call include-path-for, audio-effects)
218LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils libdl
219LOCAL_MODULE_TAGS := optional
220
221include $(BUILD_SHARED_LIBRARY)
222</pre>
223 </li>
224 <li>If your product supports low latency audio as specified by the Android CDD, copy the
225 corresponding XML feature file into your product. For example, in your product's
226 <code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code>
227 Makefile:
228 <pre>
229PRODUCT_COPY_FILES := ...
230
231PRODUCT_COPY_FILES += \
232frameworks/native/data/etc/android.android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \
233</pre>
234 </li>
235
236 <li>Copy the <code>audio_policy.conf</code> file that you created earlier to the <code>system/etc/</code> directory
237 in your product's <code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code>
238 Makefile. For example:
239 <pre>
240PRODUCT_COPY_FILES += \
241 device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf
242</pre>
243 </li>
244 <li>Declare the shared modules of your audio HAL that are required by your product in the product's
245 <code>device/&lt;company_name&gt;/&lt;device_name&gt;/device.mk</code> Makefile. For example, the
246 Galaxy Nexus requires the primary and bluetooth audio HAL modules:
247<pre>
248PRODUCT_PACKAGES += \
249 audio.primary.tuna \
250 audio.a2dp.default
251</pre>
252 </li>
253</ol>
254
Clay Murphyc28f2372013-09-25 16:13:40 -0700255<h2 id="preprocessing">Audio pre-processing effects</h2>
Robert Ly110d6522013-04-10 13:43:54 -0700256<p>
Clay Murphyc28f2372013-09-25 16:13:40 -0700257The Android platform provides audio effects on supported devices in the
Robert Ly110d6522013-04-10 13:43:54 -0700258<a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">audiofx</a>
259package, which is available for developers to access. For example, on the Nexus 10, the following pre-processing effects are supported: </p>
260<ul>
Clay Murphyc28f2372013-09-25 16:13:40 -0700261 <li><a
262href="http://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler.html">Acoustic Echo Cancellation</a></li>
263 <li><a
264href="http://developer.android.com/reference/android/media/audiofx/AutomaticGainControl.html">Automatic Gain Control</a></li>
265 <li><a
266href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html">Noise Suppression</a></li>
Robert Ly110d6522013-04-10 13:43:54 -0700267</ul>
268</p>
269
270
271<p>Pre-processing effects are always paired with the use case mode in which the pre-processing is requested. In Android
Clay Murphyc28f2372013-09-25 16:13:40 -0700272app development, a use case is referred to as an <code>AudioSource</code>; and app developers
273request to use the <code>AudioSource</code> abstraction instead of the actual audio hardware device.
Robert Ly110d6522013-04-10 13:43:54 -0700274The Android Audio Policy Manager maps an <code>AudioSource</code> to the actual hardware with <code>AudioPolicyManagerBase::getDeviceForInputSource(int
Clay Murphyc28f2372013-09-25 16:13:40 -0700275inputSource)</code>. The following sources are exposed to developers:
Robert Ly35f2fda2013-01-29 16:27:05 -0800276</p>
277<ul>
278<code><li>android.media.MediaRecorder.AudioSource.CAMCORDER</li></code>
279<code><li>android.media.MediaRecorder.AudioSource.VOICE_COMMUNICATION</li></code>
280<code><li>android.media.MediaRecorder.AudioSource.VOICE_CALL</li></code>
281<code><li>android.media.MediaRecorder.AudioSource.VOICE_DOWNLINK</li></code>
282<code><li>android.media.MediaRecorder.AudioSource.VOICE_UPLINK</li></code>
283<code><li>android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION</li></code>
284<code><li>android.media.MediaRecorder.AudioSource.MIC</li></code>
285<code><li>android.media.MediaRecorder.AudioSource.DEFAULT</li></code>
286</ul>
287
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700288<p>The default pre-processing effects that are applied for each <code>AudioSource</code> are
Robert Ly35f2fda2013-01-29 16:27:05 -0800289specified in the <code>/system/etc/audio_effects.conf</code> file. To specify
Robert Ly110d6522013-04-10 13:43:54 -0700290your own default effects for every <code>AudioSource</code>, create a <code>/system/vendor/etc/audio_effects.conf</code> file
291and specify any pre-processing effects that you need to turn on. For an example,
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700292see the implementation for the Nexus 10 in <code>device/samsung/manta/audio_effects.conf</code></p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800293
Robert Ly110d6522013-04-10 13:43:54 -0700294<p class="warning"><strong>Warning:</strong> For the <code>VOICE_RECOGNITION</code> use case, do not enable
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700295the noise suppression pre-processing effect. It should not be turned on by default when recording from this audio source,
296and you should not enable it in your own audio_effects.conf file. Turning on the effect by default will cause the device to fail
Clay Murphyc28f2372013-09-25 16:13:40 -0700297the <a href="/compatibility/index.html">compatibility requirement</a> regardless of whether this was on by default due to
298configuration file, or the audio HAL implementation's default behavior.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800299
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700300<p>The following example enables pre-processing for the VoIP <code>AudioSource</code> and Camcorder <code>AudioSource</code>.
Clay Murphyc28f2372013-09-25 16:13:40 -0700301By declaring the <code>AudioSource</code> configuration in this manner, the
302framework will automatically request from the audio HAL the use of those
303effects.</p>
Robert Ly35f2fda2013-01-29 16:27:05 -0800304
305<pre>
Robert Ly35f2fda2013-01-29 16:27:05 -0800306pre_processing {
307 voice_communication {
308 aec {}
309 ns {}
310 }
311 camcorder {
312 agc {}
313 }
314}
315</pre>
316
Clay Murphy1ed64fc2013-06-18 13:45:59 -0700317<h3 id="tuning">Source tuning</h3>
Robert Ly110d6522013-04-10 13:43:54 -0700318<p>For <code>AudioSource</code> tuning, there are no explicit requirements on audio gain or audio processing
Robert Ly35f2fda2013-01-29 16:27:05 -0800319with the exception of voice recognition (<code>VOICE_RECOGNITION</code>).</p>
320
Robert Ly35f2fda2013-01-29 16:27:05 -0800321<p>The following are the requirements for voice recognition:</p>
322
323<ul>
324<li>"flat" frequency response (+/- 3dB) from 100Hz to 4kHz</li>
325<li>close-talk config: 90dB SPL reads RMS of 2500 (16bit samples)</li>
326<li>level tracks linearly from -18dB to +12dB relative to 90dB SPL</li>
327<li>THD < 1% (90dB SPL in 100 to 4000Hz range)</li>
328<li>8kHz sampling rate (anti-aliasing)</li>
329<li>Effects / pre-processing must be disabled by default</li>
330</ul>
331
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700332<p>Examples of tuning different effects for different sources are:</p>
Robert Ly110d6522013-04-10 13:43:54 -0700333
334<ul>
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700335 <li>Noise Suppressor
Robert Ly110d6522013-04-10 13:43:54 -0700336 <ul>
Guru Nagarajan9b1407f2013-04-22 13:54:33 -0700337 <li>Tuned for wind noise suppressor for <code>CAMCORDER</code></li>
338 <li>Tuned for stationary noise suppressor for <code>VOICE_COMMUNICATION</code></li>
Robert Ly110d6522013-04-10 13:43:54 -0700339 </ul>
340 </li>
341 <li>Automatic Gain Control
342 <ul>
343 <li>Tuned for close-talk for <code>VOICE_COMMUNICATION</code> and main phone mic</li>
344 <li>Tuned for far-talk for <code>CAMCORDER</code></li>
345 </ul>
346 </li>
347</ul>
Robert Ly35f2fda2013-01-29 16:27:05 -0800348
Clay Murphy1ed64fc2013-06-18 13:45:59 -0700349<h3 id="more">More information</h3>
Robert Ly35f2fda2013-01-29 16:27:05 -0800350<p>For more information, see:</p>
351<ul>
Robert Ly110d6522013-04-10 13:43:54 -0700352<li>Android documentation for <a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">audiofx
353package</a>
354
355<li>Android documentation for <a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html">Noise Suppression audio effect</a></li>
356<li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li>
Robert Ly35f2fda2013-01-29 16:27:05 -0800357</ul>