Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 1 | page.title=Audio Latency |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Clay Murphy | 768b82a | 2013-11-12 11:32:41 -0800 | [diff] [blame] | 5 | Copyright 2013 The Android Open Source Project |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 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> |
| 28 | This page describes common methods for measuring input and output latency. |
| 29 | </p> |
| 30 | |
| 31 | |
| 32 | |
| 33 | <h2 id="measuringOutput">Measuring Output Latency</h2> |
| 34 | |
| 35 | <p> |
| 36 | There are several techniques available to measure output latency, |
| 37 | with varying degrees of accuracy and ease of running, described below. Also |
| 38 | see the <a href="testing_circuit.html">Testing circuit</a> for an example test environment. |
| 39 | </p> |
| 40 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 41 | <h3 id="ledTest">LED and oscilloscope test</h3> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 42 | <p> |
| 43 | This test measures latency in relation to the device's LED indicator. |
| 44 | If your production device does not have an LED, you can install the |
| 45 | LED on a prototype form factor device. For even better accuracy |
| 46 | on prototype devices with exposed circuity, connect one |
| 47 | oscilloscope probe to the LED directly to bypass the light |
| 48 | sensor latency. |
| 49 | </p> |
| 50 | |
| 51 | <p> |
| 52 | If you cannot install an LED on either your production or prototype device, |
| 53 | try the following workarounds: |
| 54 | </p> |
| 55 | |
| 56 | <ul> |
| 57 | <li>Use a General Purpose Input/Output (GPIO) pin for the same purpose.</li> |
| 58 | <li>Use JTAG or another debugging port.</li> |
| 59 | <li>Use the screen backlight. This might be risky as the |
| 60 | backlight may have a non-neglible latency, and can contribute to |
| 61 | an inaccurate latency reading. |
| 62 | </li> |
| 63 | </ul> |
| 64 | |
| 65 | <p>To conduct this test:</p> |
| 66 | |
| 67 | <ol> |
| 68 | <li>Run an app that periodically pulses the LED at |
| 69 | the same time it outputs audio. |
| 70 | |
| 71 | <p class="note"><b>Note:</b> To get useful results, it is crucial to use the correct |
| 72 | APIs in the test app so that you're exercising the fast audio output path. |
| 73 | See <a href="latency_design.html">Design For Reduced Latency</a> for |
| 74 | background. |
| 75 | </p> |
| 76 | </li> |
| 77 | <li>Place a light sensor next to the LED.</li> |
| 78 | <li>Connect the probes of a dual-channel oscilloscope to both the wired headphone |
| 79 | jack (line output) and light sensor.</li> |
| 80 | <li>Use the oscilloscope to measure |
| 81 | the time difference between observing the line output signal versus the light |
| 82 | sensor signal.</li> |
| 83 | </ol> |
| 84 | |
| 85 | <p>The difference in time is the approximate audio output latency, |
| 86 | assuming that the LED latency and light sensor latency are both zero. |
| 87 | Typically, the LED and light sensor each have a relatively low latency |
| 88 | on the order of one millisecond or less, which is sufficiently low enough |
| 89 | to ignore.</p> |
| 90 | |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 91 | <h2 id="measuringRoundTrip">Measuring Round-Trip Latency</h2> |
| 92 | |
| 93 | <p> |
| 94 | Round-trip latency is the sum of output latency and input latency. |
| 95 | </p> |
| 96 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 97 | <h3 id="larsenTest">Larsen test</h3> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 98 | <p> |
| 99 | One of the easiest latency tests is an audio feedback |
| 100 | (Larsen effect) test. This provides a crude measure of combined output |
| 101 | and input latency by timing an impulse response loop. This test is not very useful |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 102 | for detailed analysis |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 103 | by itself because of the nature of the test, but it can be useful for calibrating |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 104 | other tests, and for establishing an upper bound.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 105 | |
| 106 | <p>To conduct this test:</p> |
| 107 | <ol> |
| 108 | <li>Run an app that captures audio from the microphone and immediately plays the |
| 109 | captured data back over the speaker.</li> |
| 110 | <li>Create a sound externally, |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 111 | such as tapping a pencil by the microphone. This noise generates a feedback loop. |
| 112 | Alternatively, one can inject an impulse into the loop using software.</li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 113 | <li>Measure the time between feedback pulses to get the sum of the output latency, input latency, and application overhead.</li> |
| 114 | </ol> |
| 115 | |
| 116 | <p>This method does not break down the |
| 117 | component times, which is important when the output latency |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 118 | and input latency are independent. So this method is not recommended for measuring |
| 119 | precise output latency or input latency values in isolation, but might be useful |
| 120 | for establishing rough estimates.</p> |
| 121 | |
Glenn Kasten | 7351200 | 2015-01-15 10:06:31 -0800 | [diff] [blame] | 122 | <p> |
| 123 | We have published an example implementation at |
| 124 | <a href="https://android.googlesource.com/platform/frameworks/wilhelm/+/master/tests/examples/slesTestFeedback.cpp">slesTestFeedback.cpp</a>. |
| 125 | This is a command-line app and is built using the platform build environment; |
| 126 | however it should be straightforward to adopt the code for other environments. |
| 127 | You will also need the <a href="avoiding_pi.html#nonBlockingAlgorithms">non-blocking</a> FIFO code |
| 128 | located in the <code>audio_utils</code> library. |
| 129 | </p> |
| 130 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 131 | <h3 id="funplug">FunPlug</h3> |
Glenn Kasten | 9f5130c | 2014-11-10 14:36:08 -0800 | [diff] [blame] | 132 | |
| 133 | <p> |
| 134 | The <a href="funplug.html">FunPlug</a> dongle is handy for |
| 135 | measuring round-trip latency over the headset connector. |
| 136 | The image below demonstrates the result of injecting an impulse |
| 137 | into the loop once, and then allowing the feedback loop to oscillate. |
| 138 | The period of the oscillations is the round-trip latency. |
| 139 | The specific device, software release, and |
| 140 | test conditions are not specified here. The results shown |
| 141 | should not be extrapolated. |
| 142 | </p> |
| 143 | |
Clay Murphy | 1b77cc2 | 2014-12-17 18:20:06 -0800 | [diff] [blame] | 144 | <img src="images/round_trip.png" alt="round-trip measurement" id="figure1" /> |
| 145 | <p class="img-caption"> |
| 146 | <strong>Figure 1.</strong> Round-trip measurement |
| 147 | </p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 148 | |
| 149 | <h2 id="measuringInput">Measuring Input Latency</h2> |
| 150 | |
| 151 | <p> |
| 152 | Input latency is more difficult to measure than output latency. The following |
| 153 | tests might help. |
| 154 | </p> |
| 155 | |
| 156 | <p> |
| 157 | One approach is to first determine the output latency |
| 158 | using the LED and oscilloscope method and then use |
| 159 | the audio feedback (Larsen) test to determine the sum of output |
| 160 | latency and input latency. The difference between these two |
| 161 | measurements is the input latency. |
| 162 | </p> |
| 163 | |
| 164 | <p> |
| 165 | Another technique is to use a GPIO pin on a prototype device. |
| 166 | Externally, pulse a GPIO input at the same time that you present |
| 167 | an audio signal to the device. Run an app that compares the |
| 168 | difference in arrival times of the GPIO signal and audio data. |
| 169 | </p> |
| 170 | |
| 171 | <h2 id="reducing">Reducing Latency</h2> |
| 172 | |
| 173 | <p>To achieve low audio latency, pay special attention throughout the |
| 174 | system to scheduling, interrupt handling, power management, and device |
| 175 | driver design. Your goal is to prevent any part of the platform from |
| 176 | blocking a <code>SCHED_FIFO</code> audio thread for more than a couple |
| 177 | of milliseconds. By adopting such a systematic approach, you can reduce |
| 178 | audio latency and get the side benefit of more predictable performance |
| 179 | overall. |
| 180 | </p> |
| 181 | |
| 182 | |
| 183 | <p> |
| 184 | Audio underruns, when they do occur, are often detectable only under certain |
| 185 | conditions or only at the transitions. Try stressing the system by launching |
| 186 | new apps and scrolling quickly through various displays. But be aware |
| 187 | that some test conditions are so stressful as to be beyond the design |
| 188 | goals. For example, taking a bugreport puts such enormous load on the |
| 189 | system that it may be acceptable to have an underrun in that case. |
| 190 | </p> |
| 191 | |
| 192 | <p> |
| 193 | When testing for underruns: |
| 194 | </p> |
| 195 | <ul> |
| 196 | <li>Configure any DSP after the app processor so that it adds |
| 197 | minimal latency.</li> |
| 198 | <li>Run tests under different conditions |
| 199 | such as having the screen on or off, USB plugged in or unplugged, |
| 200 | WiFi on or off, Bluetooth on or off, and telephony and data radios |
| 201 | on or off.</li> |
| 202 | <li>Select relatively quiet music that you're very familiar with, and which is easy |
| 203 | to hear underruns in.</li> |
| 204 | <li>Use wired headphones for extra sensitivity.</li> |
| 205 | <li>Give yourself breaks so that you don't experience "ear fatigue."</li> |
| 206 | </ul> |
| 207 | |
| 208 | <p> |
| 209 | Once you find the underlying causes of underruns, reduce |
| 210 | the buffer counts and sizes to take advantage of this. |
| 211 | The eager approach of reducing buffer counts and sizes <i>before</i> |
| 212 | analyzing underruns and fixing the causes of underruns only |
| 213 | results in frustration. |
| 214 | </p> |
| 215 | |
| 216 | <h3 id="tools">Tools</h3> |
| 217 | <p> |
| 218 | <code>systrace</code> is an excellent general-purpose tool |
| 219 | for diagnosing system-level performance glitches. |
| 220 | </p> |
| 221 | |
| 222 | <p> |
| 223 | The output of <code>dumpsys media.audio_flinger</code> also contains a |
| 224 | useful section called "simple moving statistics." This has a summary |
| 225 | of the variability of elapsed times for each audio mix and I/O cycle. |
| 226 | Ideally, all the time measurements should be about equal to the mean or |
| 227 | nominal cycle time. If you see a very low minimum or high maximum, this is an |
| 228 | indication of a problem, likely a high scheduling latency or interrupt |
| 229 | disable time. The <i>tail</i> part of the output is especially helpful, |
| 230 | as it highlights the variability beyond +/- 3 standard deviations. |
| 231 | </p> |