1 <html devsite> 2 <head> 3 <title>Audio Latency for App Developers</title> 4 <meta name="project_path" value="/_project.yaml" /> 5 <meta name="book_path" value="/_book.yaml" /> 6 </head> 7 <body> 8 <!-- 9 Copyright 2017 The Android Open Source Project 10 11 Licensed under the Apache License, Version 2.0 (the "License"); 12 you may not use this file except in compliance with the License. 13 You may obtain a copy of the License at 14 15 http://www.apache.org/licenses/LICENSE-2.0 16 17 Unless required by applicable law or agreed to in writing, software 18 distributed under the License is distributed on an "AS IS" BASIS, 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 See the License for the specific language governing permissions and 21 limitations under the License. 22 --> 23 24 25 26 <p>For the lowest audio latency possible, we recommend you use Android native audio 27 based on OpenSL ES 1.0.1.</p> 28 29 <h2 id="implementation">Implementation checklist</h2> 30 31 <p>To use Android native audio:</p> 32 33 <ol> 34 35 <li> 36 Download and install the 37 <a href="https://developer.android.com/tools/sdk/ndk/index.html">Android NDK</a>. 38 In the rest of this document, we'll assume <code>NDKroot</code> is the 39 directory where you installed NDK. 40 </li> 41 42 <li> 43 Read the <a href="#supporting">supporting documentation.</a> 44 </li> 45 46 <li> 47 Check for API level 9 or higher. 48 </li> 49 50 <li> 51 Check for feature 52 <a href="http://developer.android.com/guide/topics/manifest/uses-feature-element.html#hw-features">android.hardware.audio.low_latency.</a> 53 </li> 54 55 <li> 56 Use the recommended native buffer size and sample rate returned by 57 <a href="http://developer.android.com/reference/android/media/AudioManager.html#getProperty(java.lang.String)">android.media.AudioManager.getProperty(java.lang.String)</a> 58 <p> <strong>Note</strong>: the same buffer size and sample rate should also be used for input.</p> 59 </li> 60 61 <li> 62 Usually an OpenSL ES buffer count of 1 is sufficient. 63 </li> 64 65 <li> 66 Keep your callback handlers short, without bursty CPU usage or unbounded blocking. Avoid 67 <a href="avoiding_pi.html">priority inversion.</a> 68 </li> 69 70 <li> 71 Consider using 72 <a href="avoiding_pi.html#nonBlockingAlgorithms">non-blocking algorithms</a> 73 to communicate between input and output callback handlers, 74 and between the callback handlers and the rest of your application. 75 </li> 76 77 </ol> 78 79 <h2 id="supporting">Supporting documentation</h2> 80 81 <h3 id="opensl_es_1_0_1">OpenSL ES 1.0.1</h3> 82 83 <p> 84 Use a PDF viewer to review the 85 <a href="https://www.khronos.org/registry/sles/specs/OpenSL_ES_Specification_1.0.1.pdf">OpenSL 1.0.1 Specification.</a> 86 This is a rather long reference, and not all of it will be relevant to you; but you 87 will need to consult it for details on the API. 88 </p> 89 90 <p class="note"> 91 <strong>Note</strong>: this document describes the full OpenSL ES 1.0.1, but Android 92 native audio is actually based on a subset of OpenSL ES 1.0.1 with some Android-specific extensions. 93 </p> 94 95 <p> 96 Documents describing later versions of OpenSL ES, such as 1.1, 97 are not relevant to Android. 98 </p> 99 100 <h3 id="opensl_es_for_android">OpenSL ES for Android</h3> 101 102 <p> 103 The document "OpenSL ES for Android" is provided in the NDK installation, 104 and is not currently available online. Open this link in a browser: 105 </p> 106 107 <pre class="devsite-click-to-copy"> 108 NDKroot/docs/Additional_library_docs/opensles/index.html 109 </pre> 110 111 <p> 112 You'll want to skim the whole 113 document, but pay special attention to the "Performance" subsection of the 114 "Programming notes" section. 115 </p> 116 117 <p> 118 Section "Supported features from OpenSL ES 1.0.1" 119 describes the subset supported by Android. 120 </p> 121 122 <p> 123 Section "Android extensions" describes Android-specific extensions 124 that aren't included in base OpenSL ES 1.0.1. 125 </p> 126 127 <h3 id="relationship">Relationship with OpenSL ES 1.0.1</h3> 128 129 <p> 130 This Venn diagram shows the relationship between 131 Android native audio and OpenSL ES 1.0.1. 132 </p> 133 134 <img src="images/venn.png" alt="Venn diagram" id="figure1" /> 135 <p class="img-caption"> 136 <strong>Figure 1.</strong> Venn diagram 137 </p> 138 139 <h2 id="resources">Other resources</h2> 140 141 <h3 id="source_android_com">source.android.com</h3> 142 143 <p> 144 The site <a href="/">source.android.com</a> 145 is primarily designed for OEMs building Android 146 devices, and the SoC vendors who supply components to these OEMs. 147 </p> 148 149 <p> 150 However, there is a wealth of useful information about latency at this site, so 151 you may want to review it. See the articles at 152 <a href="latency.html">Audio Latency.</a> 153 </p> 154 155 <h3 id="android_ndk">android-ndk</h3> 156 157 <p> 158 If you have questions about how to use Android native audio, you can ask at the discussion group 159 <a href="https://groups.google.com/forum/#!forum/android-ndk">android-ndk.</a> 160 </p> 161 162 <h3 id="videos">Videos</h3> 163 164 <dl> 165 166 <dt><a href="https://youtu.be/d3kfEeMZ65c">High performance audio on Android</a> 167 (Google I/O 2013)</dt> 168 <dd>The whole video is about latency.</dd> 169 170 <dt><a href="https://youtu.be/92fgcUNCHic">Building great multi-media experiences on Android</a> 171 (Google I/O 2014)</dt> 172 <dd>The first 14 minutes are about audio in general and input latency in particular.</dd> 173 174 <dt><a href="https://youtu.be/PnDK17zP9BI">Audio latency: buffer sizes</a> 175 (100 Days of Google Dev)</dt> 176 <dd>Describes the relationship between audio latency, buffer sizes, and task scheduling.</dd> 177 178 </dl> 179 180 </body> 181 </html> 182