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