Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 1 | page.title=USB Digital Audio |
| 2 | @jd:body |
| 3 | |
Clay Murphy | bc92aea | 2014-10-16 10:13:18 -0700 | [diff] [blame] | 4 | <!-- |
| 5 | Copyright 2014 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 | --> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 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 article reviews Android support for USB digital audio and related |
| 29 | USB-based protocols. |
| 30 | </p> |
| 31 | |
| 32 | <h3 id="audience">Audience</h3> |
| 33 | |
| 34 | <p> |
| 35 | The target audience of this article is Android device OEMs, SoC vendors, |
| 36 | USB audio peripheral suppliers, advanced audio application developers, |
| 37 | and others seeking detailed understanding of USB digital audio internals on Android. |
| 38 | </p> |
| 39 | |
| 40 | <p> |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 41 | End users of Nexus devices should see the article |
| 42 | <a href="https://support.google.com/nexus/answer/6127700">Record and play back audio using USB host mode</a> |
| 43 | at the |
| 44 | <a href="https://support.google.com/nexus/">Nexus Help Center</a> instead. |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 45 | Though this article is not oriented towards end users, |
| 46 | certain audiophile consumers may find portions of interest. |
| 47 | </p> |
| 48 | |
| 49 | <h2 id="overview">Overview of USB</h2> |
| 50 | |
| 51 | <p> |
| 52 | Universal Serial Bus (USB) is informally described in the Wikipedia article |
| 53 | <a href="http://en.wikipedia.org/wiki/USB">USB</a>, |
| 54 | and is formally defined by the standards published by the |
| 55 | <a href="http://www.usb.org/">USB Implementers Forum, Inc</a>. |
| 56 | For convenience, we summarize the key USB concepts here, |
| 57 | but the standards are the authoritative reference. |
| 58 | </p> |
| 59 | |
| 60 | <h3 id="terminology">Basic concepts and terminology</h3> |
| 61 | |
| 62 | <p> |
| 63 | USB is a <a href="http://en.wikipedia.org/wiki/Bus_(computing)">bus</a> |
| 64 | with a single initiator of data transfer operations, called the <i>host</i>. |
| 65 | The host communicates with |
| 66 | <a href="http://en.wikipedia.org/wiki/Peripheral">peripherals</a> via the bus. |
| 67 | </p> |
| 68 | |
| 69 | <p> |
| 70 | <b>Note:</b> the terms <i>device</i> or <i>accessory</i> are common synonyms for |
| 71 | <i>peripheral</i>. We avoid those terms here, as they could be confused with |
| 72 | Android <a href="http://en.wikipedia.org/wiki/Mobile_device">device</a> |
| 73 | or the Android-specific concept called |
| 74 | <a href="http://developer.android.com/guide/topics/connectivity/usb/accessory.html">accessory mode</a>. |
| 75 | </p> |
| 76 | |
| 77 | <p> |
| 78 | A critical host role is <i>enumeration</i>: |
| 79 | the process of detecting which peripherals are connected to the bus, |
| 80 | and querying their properties expressed via <i>descriptors</i>. |
| 81 | </p> |
| 82 | |
| 83 | <p> |
| 84 | A peripheral may be one physical object |
| 85 | but actually implement multiple logical <i>functions</i>. |
| 86 | For example, a webcam peripheral could have both a camera function and a |
| 87 | microphone audio function. |
| 88 | </p> |
| 89 | |
| 90 | <p> |
| 91 | Each peripheral function has an <i>interface</i> that |
| 92 | defines the protocol to communicate with that function. |
| 93 | </p> |
| 94 | |
| 95 | <p> |
| 96 | The host communicates with a peripheral over a |
| 97 | <a href="http://en.wikipedia.org/wiki/Stream_(computing)">pipe</a> |
| 98 | to an <a href="http://en.wikipedia.org/wiki/Communication_endpoint">endpoint</a>, |
| 99 | a data source or sink |
| 100 | provided by one of the peripheral's functions. |
| 101 | </p> |
| 102 | |
| 103 | <p> |
| 104 | There are two kinds of pipes: <i>message</i> and <i>stream</i>. |
| 105 | A message pipe is used for bi-directional control and status. |
| 106 | A stream pipe is used for uni-directional data transfer. |
| 107 | </p> |
| 108 | |
| 109 | <p> |
| 110 | The host initiates all data transfers, |
| 111 | hence the terms <i>input</i> and <i>output</i> are expressed relative to the host. |
| 112 | An input operation transfers data from the peripheral to the host, |
| 113 | while an output operation transfers data from the host to the peripheral. |
| 114 | </p> |
| 115 | |
| 116 | <p> |
| 117 | There are three major data transfer modes: |
| 118 | <i>interrupt</i>, <i>bulk</i>, and <i>isochronous</i>. |
| 119 | Isochronous mode will be discussed further in the context of audio. |
| 120 | </p> |
| 121 | |
| 122 | <p> |
| 123 | The peripheral may have <i>terminals</i> that connect to the outside world, |
| 124 | beyond the peripheral itself. In this way, the peripheral serves |
| 125 | to translate between USB protocol and "real world" signals. |
| 126 | The terminals are logical objects of the function. |
| 127 | </p> |
| 128 | |
| 129 | <h2 id="androidModes">Android USB modes</h2> |
| 130 | |
| 131 | <h3 id="developmentMode">Development mode</h3> |
| 132 | |
| 133 | <p> |
| 134 | <i>Development mode</i> has been present since the initial release of Android. |
| 135 | The Android device appears as a USB peripheral |
| 136 | to a host PC running a desktop operating system such as Linux, |
| 137 | Mac OS X, or Windows. The only visible peripheral function is either |
| 138 | <a href="http://en.wikipedia.org/wiki/Android_software_development#Fastboot">Android fastboot</a> |
| 139 | or |
| 140 | <a href="http://developer.android.com/tools/help/adb.html">Android Debug Bridge (adb)</a>. |
| 141 | The fastboot and adb protocols are layered over USB bulk data transfer mode. |
| 142 | </p> |
| 143 | |
| 144 | <h3 id="hostMode">Host mode</h3> |
| 145 | |
| 146 | <p> |
| 147 | <i>Host mode</i> is introduced in Android 3.1 (API level 12). |
| 148 | </p> |
| 149 | |
| 150 | <p> |
| 151 | As the Android device must act as host, and most Android devices include |
| 152 | a micro-USB connector that does not directly permit host operation, |
| 153 | an on-the-go (<a href="http://en.wikipedia.org/wiki/USB_On-The-Go">OTG</a>) adapter |
| 154 | such as this is usually required: |
| 155 | </p> |
| 156 | |
Clay Murphy | 1b77cc2 | 2014-12-17 18:20:06 -0800 | [diff] [blame] | 157 | <img src="images/otg.jpg" style="image-orientation: 90deg;" height="50%" width="50%" alt="OTG" id="figure1" /> |
| 158 | <p class="img-caption"> |
| 159 | <strong>Figure 1.</strong> On-the-go (OTG) adapter |
| 160 | </p> |
| 161 | |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 162 | |
| 163 | <p> |
| 164 | An Android device might not provide sufficient power to operate a |
| 165 | particular peripheral, depending on how much power the peripheral needs, |
| 166 | and how much the Android device is capable of supplying. Even if |
| 167 | adequate power is available, the Android device battery charge may |
| 168 | be significantly shortened. For these situations, use a powered |
| 169 | <a href="http://en.wikipedia.org/wiki/USB_hub">hub</a> such as this: |
| 170 | </p> |
| 171 | |
Clay Murphy | 1b77cc2 | 2014-12-17 18:20:06 -0800 | [diff] [blame] | 172 | <img src="images/hub.jpg" alt="Powered hub" id="figure2" /> |
| 173 | <p class="img-caption"> |
| 174 | <strong>Figure 2.</strong> Powered hub |
| 175 | </p> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 176 | |
| 177 | <h3 id="accessoryMode">Accessory mode</h3> |
| 178 | |
| 179 | <p> |
| 180 | <i>Accessory mode</i> was introduced in Android 3.1 (API level 12) and back-ported to Android 2.3.4. |
| 181 | In this mode, the Android device operates as a USB peripheral, |
| 182 | under the control of another device such as a dock that serves as host. |
| 183 | The difference between development mode and accessory mode |
| 184 | is that additional USB functions are visible to the host, beyond adb. |
| 185 | The Android device begins in development mode and then |
| 186 | transitions to accessory mode via a re-negotiation process. |
| 187 | </p> |
| 188 | |
| 189 | <p> |
| 190 | Accessory mode was extended with additional features in Android 4.1, |
| 191 | in particular audio described below. |
| 192 | </p> |
| 193 | |
| 194 | <h2 id="audioClass">USB audio</h2> |
| 195 | |
| 196 | <h3 id="class">USB classes</h3> |
| 197 | |
| 198 | <p> |
| 199 | Each peripheral function has an associated <i>device class</i> document |
| 200 | that specifies the standard protocol for that function. |
| 201 | This enables <i>class compliant</i> hosts and peripheral functions |
| 202 | to inter-operate, without detailed knowledge of each other's workings. |
| 203 | Class compliance is critical if the host and peripheral are provided by |
| 204 | different entities. |
| 205 | </p> |
| 206 | |
| 207 | <p> |
| 208 | The term <i>driverless</i> is a common synonym for <i>class compliant</i>, |
| 209 | indicating that it is possible to use the standard features of such a |
| 210 | peripheral without requiring an operating-system specific |
| 211 | <a href="http://en.wikipedia.org/wiki/Device_driver">driver</a> to be installed. |
| 212 | One can assume that a peripheral advertised as "no driver needed" |
| 213 | for major desktop operating systems |
| 214 | will be class compliant, though there may be exceptions. |
| 215 | </p> |
| 216 | |
| 217 | <h3 id="audioClass">USB audio class</h3> |
| 218 | |
| 219 | <p> |
| 220 | Here we concern ourselves only with peripherals that implement |
| 221 | audio functions, and thus adhere to the audio device class. There are two |
| 222 | editions of the USB audio class specification: class 1 (UAC1) and 2 (UAC2). |
| 223 | </p> |
| 224 | |
| 225 | <h3 id="otherClasses">Comparison with other classes</h3> |
| 226 | |
| 227 | <p> |
| 228 | USB includes many other device classes, some of which may be confused |
| 229 | with the audio class. The |
| 230 | <a href="http://en.wikipedia.org/wiki/USB_mass_storage_device_class">mass storage class</a> |
| 231 | (MSC) is used for |
| 232 | sector-oriented access to media, while |
| 233 | <a href="http://en.wikipedia.org/wiki/Media_Transfer_Protocol">Media Transfer Protocol</a> |
| 234 | (MTP) is for full file access to media. |
| 235 | Both MSC and MTP may be used for transferring audio files, |
| 236 | but only USB audio class is suitable for real-time streaming. |
| 237 | </p> |
| 238 | |
| 239 | <h3 id="audioTerminals">Audio terminals</h3> |
| 240 | |
| 241 | <p> |
| 242 | The terminals of an audio peripheral are typically analog. |
| 243 | The analog signal presented at the peripheral's input terminal is converted to digital by an |
| 244 | <a href="http://en.wikipedia.org/wiki/Analog-to-digital_converter">analog-to-digital converter</a> |
| 245 | (ADC), |
| 246 | and is carried over USB protocol to be consumed by |
| 247 | the host. The ADC is a data <i>source</i> |
| 248 | for the host. Similarly, the host sends a |
| 249 | digital audio signal over USB protocol to the peripheral, where a |
| 250 | <a href="http://en.wikipedia.org/wiki/Digital-to-analog_converter">digital-to-analog converter</a> |
| 251 | (DAC) |
| 252 | converts and presents to an analog output terminal. |
| 253 | The DAC is a <i>sink</i> for the host. |
| 254 | </p> |
| 255 | |
| 256 | <h3 id="channels">Channels</h3> |
| 257 | |
| 258 | <p> |
| 259 | A peripheral with audio function can include a source terminal, sink terminal, or both. |
| 260 | Each direction may have one channel (<i>mono</i>), two channels |
| 261 | (<i>stereo</i>), or more. |
| 262 | Peripherals with more than two channels are called <i>multichannel</i>. |
| 263 | It is common to interpret a stereo stream as consisting of |
| 264 | <i>left</i> and <i>right</i> channels, and by extension to interpret a multichannel stream as having |
| 265 | spatial locations corresponding to each channel. However, it is also quite appropriate |
| 266 | (especially for USB audio more so than |
| 267 | <a href="http://en.wikipedia.org/wiki/HDMI">HDMI</a>) |
| 268 | to not assign any particular |
| 269 | standard spatial meaning to each channel. In this case, it is up to the |
| 270 | application and user to define how each channel is used. |
| 271 | For example, a four-channel USB input stream might have the first three |
| 272 | channels attached to various microphones within a room, and the final |
| 273 | channel receiving input from an AM radio. |
| 274 | </p> |
| 275 | |
| 276 | <h3 id="isochronous">Isochronous transfer mode</h3> |
| 277 | |
| 278 | <p> |
| 279 | USB audio uses isochronous transfer mode for its real-time characteristics, |
| 280 | at the expense of error recovery. |
| 281 | In isochronous mode, bandwidth is guaranteed, and data transmission |
| 282 | errors are detected using a cyclic redundancy check (CRC). But there is |
| 283 | no packet acknowledgement or re-transmission in the event of error. |
| 284 | </p> |
| 285 | |
| 286 | <p> |
| 287 | Isochronous transmissions occur each Start Of Frame (SOF) period. |
| 288 | The SOF period is one millisecond for full-speed, and 125 microseconds for |
| 289 | high-speed. Each full-speed frame carries up to 1023 bytes of payload, |
| 290 | and a high-speed frame carries up to 1024 bytes. Putting these together, |
| 291 | we calculate the maximum transfer rate as 1,023,000 or 8,192,000 bytes |
| 292 | per second. This sets a theoretical upper limit on the combined audio |
| 293 | sample rate, channel count, and bit depth. The practical limit is lower. |
| 294 | </p> |
| 295 | |
| 296 | <p> |
| 297 | Within isochronous mode, there are three sub-modes: |
| 298 | </p> |
| 299 | |
| 300 | <ul> |
| 301 | <li>Adaptive</li> |
| 302 | <li>Asynchronous</li> |
| 303 | <li>Synchronous</li> |
| 304 | </ul> |
| 305 | |
| 306 | <p> |
| 307 | In adaptive sub-mode, the peripheral sink or source adapts to a potentially varying sample rate |
| 308 | of the host. |
| 309 | </p> |
| 310 | |
| 311 | <p> |
| 312 | In asynchronous (also called implicit feedback) sub-mode, |
| 313 | the sink or source determines the sample rate, and the host accomodates. |
| 314 | The primary theoretical advantage of asynchronous sub-mode is that the source |
| 315 | or sink USB clock is physically and electrically closer to (and indeed may |
| 316 | be the same as, or derived from) the clock that drives the DAC or ADC. |
| 317 | This proximity means that asynchronous sub-mode should be less susceptible |
| 318 | to clock jitter. In addition, the clock used by the DAC or ADC may be |
| 319 | designed for higher accuracy and lower drift than the host clock. |
| 320 | </p> |
| 321 | |
| 322 | <p> |
| 323 | In synchronous sub-mode, a fixed number of bytes is transferred each SOF period. |
| 324 | The audio sample rate is effectively derived from the USB clock. |
| 325 | Synchronous sub-mode is not commonly used with audio because both |
| 326 | host and peripheral are at the mercy of the USB clock. |
| 327 | </p> |
| 328 | |
| 329 | <p> |
| 330 | The table below summarizes the isochronous sub-modes: |
| 331 | </p> |
| 332 | |
| 333 | <table> |
| 334 | <tr> |
| 335 | <th>Sub-mode</th> |
| 336 | <th>Byte count<br \>per packet</th> |
| 337 | <th>Sample rate<br \>determined by</th> |
| 338 | <th>Used for audio</th> |
| 339 | </tr> |
| 340 | <tr> |
| 341 | <td>adaptive</td> |
| 342 | <td>variable</td> |
| 343 | <td>host</td> |
| 344 | <td>yes</td> |
| 345 | </tr> |
| 346 | <tr> |
| 347 | <td>asynchronous</td> |
| 348 | <td>variable</td> |
| 349 | <td>peripheral</td> |
| 350 | <td>yes</td> |
| 351 | </tr> |
| 352 | <tr> |
| 353 | <td>synchronous</td> |
| 354 | <td>fixed</td> |
| 355 | <td>USB clock</td> |
| 356 | <td>no</td> |
| 357 | </tr> |
| 358 | </table> |
| 359 | |
| 360 | <p> |
| 361 | In practice, the sub-mode does of course matter, but other factors |
| 362 | should also be considered. |
| 363 | </p> |
| 364 | |
| 365 | <h2 id="androidSupport">Android support for USB audio class</h2> |
| 366 | |
| 367 | <h3 id="developmentAudio">Development mode</h3> |
| 368 | |
| 369 | <p> |
| 370 | USB audio is not supported in development mode. |
| 371 | </p> |
| 372 | |
| 373 | <h3 id="hostAudio">Host mode</h3> |
| 374 | |
| 375 | <p> |
| 376 | Android 5.0 (API level 21) and above supports a subset of USB audio class 1 (UAC1) features: |
| 377 | </p> |
| 378 | |
| 379 | <ul> |
| 380 | <li>The Android device must act as host</li> |
| 381 | <li>The audio format must be PCM (interface type I)</li> |
| 382 | <li>The bit depth must be 16-bits, 24-bits, or 32-bits where |
| 383 | 24 bits of useful audio data are left-justified within the most significant |
| 384 | bits of the 32-bit word</li> |
| 385 | <li>The sample rate must be either 48, 44.1, 32, 24, 22.05, 16, 12, 11.025, or 8 kHz</li> |
| 386 | <li>The channel count must be 1 (mono) or 2 (stereo)</li> |
| 387 | </ul> |
| 388 | |
| 389 | <p> |
| 390 | Perusal of the Android framework source code may show additional code |
| 391 | beyond the minimum needed to support these features. But this code |
| 392 | has not been validated, so more advanced features are not yet claimed. |
| 393 | </p> |
| 394 | |
| 395 | <h3 id="accessoryAudio">Accessory mode</h3> |
| 396 | |
| 397 | <p> |
| 398 | Android 4.1 (API level 16) added limited support for audio playback to the host. |
| 399 | While in accessory mode, Android automatically routes its audio output to USB. |
| 400 | That is, the Android device serves as a data source to the host, for example a dock. |
| 401 | </p> |
| 402 | |
| 403 | <p> |
| 404 | Accessory mode audio has these features: |
| 405 | </p> |
| 406 | |
| 407 | <ul> |
| 408 | <li> |
| 409 | The Android device must be controlled by a knowledgeable host that |
| 410 | can first transition the Android device from development mode to accessory mode, |
| 411 | and then the host must transfer audio data from the appropriate endpoint. |
| 412 | Thus the Android device does not appear "driverless" to the host. |
| 413 | </li> |
| 414 | <li>The direction must be <i>input</i>, expressed relative to the host</li> |
| 415 | <li>The audio format must be 16-bit PCM</li> |
| 416 | <li>The sample rate must be 44.1 kHz</li> |
| 417 | <li>The channel count must be 2 (stereo)</li> |
| 418 | </ul> |
| 419 | |
| 420 | <p> |
| 421 | Accessory mode audio has not been widely adopted, |
| 422 | and is not currently recommended for new designs. |
| 423 | </p> |
| 424 | |
| 425 | <h2 id="applications">Applications of USB digital audio</h2> |
| 426 | |
| 427 | <p> |
| 428 | As the name indicates, the USB digital audio signal is represented |
| 429 | by a <a href="http://en.wikipedia.org/wiki/Digital_data">digital</a> data stream |
| 430 | rather than the <a href="http://en.wikipedia.org/wiki/Analog_signal">analog</a> |
| 431 | signal used by the common TRS mini |
| 432 | <a href=" http://en.wikipedia.org/wiki/Phone_connector_(audio)">headset connector</a>. |
| 433 | Eventually any digital signal must be converted to analog before it can be heard. |
| 434 | There are tradeoffs in choosing where to place that conversion. |
| 435 | </p> |
| 436 | |
| 437 | <h3 id="comparison">A tale of two DACs</h3> |
| 438 | |
| 439 | <p> |
| 440 | In the example diagram below, we compare two designs. First we have a |
| 441 | mobile device with Application Processor (AP), on-board DAC, amplifier, |
| 442 | and analog TRS connector attached to headphones. We also consider a |
| 443 | mobile device with USB connected to external USB DAC and amplifier, |
| 444 | also with headphones. |
| 445 | </p> |
| 446 | |
Clay Murphy | 1b77cc2 | 2014-12-17 18:20:06 -0800 | [diff] [blame] | 447 | <img src="images/dac.png" alt="DAC comparison" id="figure3" /> |
| 448 | <p class="img-caption"> |
| 449 | <strong>Figure 3.</strong> Comparison of two DACs |
| 450 | </p> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 451 | |
| 452 | <p> |
| 453 | Which design is better? The answer depends on your needs. |
| 454 | Each has advantages and disadvantages. |
| 455 | <b>Note:</b> this is an artificial comparison, since |
| 456 | a real Android device would probably have both options available. |
| 457 | </p> |
| 458 | |
| 459 | <p> |
| 460 | The first design A is simpler, less expensive, uses less power, |
| 461 | and will be a more reliable design assuming otherwise equally reliable components. |
| 462 | However, there are usually audio quality tradeoffs vs. other requirements. |
| 463 | For example, if this is a mass-market device, it may be designed to fit |
| 464 | the needs of the general consumer, not for the audiophile. |
| 465 | </p> |
| 466 | |
| 467 | <p> |
| 468 | In the second design, the external audio peripheral C can be designed for |
| 469 | higher audio quality and greater power output without impacting the cost of |
| 470 | the basic mass market Android device B. Yes, it is a more expensive design, |
| 471 | but the cost is absorbed only by those who want it. |
| 472 | </p> |
| 473 | |
| 474 | <p> |
| 475 | Mobile devices are notorious for having high-density |
| 476 | circuit boards, which can result in more opportunities for |
| 477 | <a href="http://en.wikipedia.org/wiki/Crosstalk_(electronics)">crosstalk</a> |
| 478 | that degrades adjacent analog signals. Digital communication is less susceptible to |
| 479 | <a href="http://en.wikipedia.org/wiki/Noise_(electronics)">noise</a>, |
| 480 | so moving the DAC from the Android device A to an external circuit board |
| 481 | C allows the final analog stages to be physically and electrically |
| 482 | isolated from the dense and noisy circuit board, resulting in higher fidelity audio. |
| 483 | </p> |
| 484 | |
| 485 | <p> |
| 486 | On the other hand, |
| 487 | the second design is more complex, and with added complexity come more |
| 488 | opportunities for things to fail. There is also additional latency |
| 489 | from the USB controllers. |
| 490 | </p> |
| 491 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 492 | <h3 id="hostApplications">Host mode applications</h3> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 493 | |
| 494 | <p> |
| 495 | Typical USB host mode audio applications include: |
| 496 | </p> |
| 497 | |
| 498 | <ul> |
| 499 | <li>music listening</li> |
| 500 | <li>telephony</li> |
| 501 | <li>instant messaging and voice chat</li> |
| 502 | <li>recording</li> |
| 503 | </ul> |
| 504 | |
| 505 | <p> |
| 506 | For all of these applications, Android detects a compatible USB digital |
| 507 | audio peripheral, and automatically routes audio playback and capture |
| 508 | appropriately, based on the audio policy rules. |
| 509 | Stereo content is played on the first two channels of the peripheral. |
| 510 | </p> |
| 511 | |
| 512 | <p> |
| 513 | There are no APIs specific to USB digital audio. |
| 514 | For advanced usage, the automatic routing may interfere with applications |
| 515 | that are USB-aware. For such applications, disable automatic routing |
| 516 | via the corresponding control in the Media section of |
| 517 | <a href="http://developer.android.com/tools/index.html">Settings / Developer Options</a>. |
| 518 | </p> |
| 519 | |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 520 | <h3 id="hostDebugging">Debugging while in host mode</h3> |
| 521 | |
| 522 | <p> |
| 523 | While in USB host mode, adb debugging over USB is unavailable. |
| 524 | See section <a href="http://developer.android.com/tools/help/adb.html#wireless">Wireless usage</a> |
| 525 | of |
| 526 | <a href="http://developer.android.com/tools/help/adb.html">Android Debug Bridge</a> |
| 527 | for an alternative. |
| 528 | </p> |
| 529 | |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 530 | <h2 id="compatibility">Implementing USB audio</h2> |
| 531 | |
| 532 | <h3 id="recommendationsPeripheral">Recommendations for audio peripheral vendors</h3> |
| 533 | |
| 534 | <p> |
| 535 | In order to inter-operate with Android devices, audio peripheral vendors should: |
| 536 | </p> |
| 537 | |
| 538 | <ul> |
| 539 | <li>design for audio class compliance; |
| 540 | currently Android targets class 1, but it is wise to plan for class 2</li> |
| 541 | <li>avoid <a href="http://en.wiktionary.org/wiki/quirk">quirks</a> |
| 542 | <li>test for inter-operability with reference and popular Android devices</li> |
| 543 | <li>clearly document supported features, audio class compliance, power requirements, etc. |
| 544 | so that consumers can make informed decisions</li> |
| 545 | </ul> |
| 546 | |
| 547 | <h3 id="recommendationsAndroid">Recommendations for Android device OEMs and SoC vendors</h3> |
| 548 | |
| 549 | <p> |
| 550 | In order to support USB digital audio, device OEMs and SoC vendors should: |
| 551 | </p> |
| 552 | |
| 553 | <ul> |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 554 | <li>design hardware to support USB host mode</li> |
Glenn Kasten | 0c48401 | 2015-02-06 09:48:25 -0800 | [diff] [blame] | 555 | <li>enable generic USB host support at the framework level |
| 556 | via the <code>android.hardware.usb.host.xml</code> feature flag</li> |
| 557 | <li>enable all kernel features needed: USB host mode, USB audio, isochronous transfer mode; |
Clay Murphy | c0a12ef | 2015-02-17 14:57:14 -0800 | [diff] [blame] | 558 | see <a href="{@docRoot}devices/tech/kernel.html">Android Kernel Configuration</a></li> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 559 | <li>keep up-to-date with recent kernel releases and patches; |
| 560 | despite the noble goal of class compliance, there are extant audio peripherals |
| 561 | with <a href="http://en.wiktionary.org/wiki/quirk">quirks</a>, |
| 562 | and recent kernels have workarounds for such quirks |
| 563 | </li> |
| 564 | <li>enable USB audio policy as described below</li> |
Glenn Kasten | 978bec8 | 2014-12-23 15:15:20 -0800 | [diff] [blame] | 565 | <li>add audio.usb.default to PRODUCT_PACKAGES in device.mk</li> |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 566 | <li>test for inter-operability with common USB audio peripherals</li> |
| 567 | </ul> |
| 568 | |
| 569 | <h3 id="enable">How to enable USB audio policy</h3> |
| 570 | |
| 571 | <p> |
| 572 | To enable USB audio, add an entry to the |
| 573 | audio policy configuration file. This is typically |
| 574 | located here: |
| 575 | <pre>device/oem/codename/audio_policy.conf</pre> |
| 576 | The pathname component "oem" should be replaced by the name |
| 577 | of the OEM who manufactures the Android device, |
| 578 | and "codename" should be replaced by the device code name. |
| 579 | </p> |
| 580 | |
| 581 | <p> |
| 582 | An example entry is shown here: |
| 583 | </p> |
| 584 | |
| 585 | <pre> |
| 586 | audio_hw_modules { |
| 587 | ... |
| 588 | usb { |
| 589 | outputs { |
| 590 | usb_accessory { |
| 591 | sampling_rates 44100 |
| 592 | channel_masks AUDIO_CHANNEL_OUT_STEREO |
| 593 | formats AUDIO_FORMAT_PCM_16_BIT |
| 594 | devices AUDIO_DEVICE_OUT_USB_ACCESSORY |
| 595 | } |
| 596 | usb_device { |
| 597 | sampling_rates dynamic |
| 598 | channel_masks dynamic |
| 599 | formats dynamic |
| 600 | devices AUDIO_DEVICE_OUT_USB_DEVICE |
| 601 | } |
| 602 | } |
| 603 | inputs { |
| 604 | usb_device { |
| 605 | sampling_rates dynamic |
| 606 | channel_masks AUDIO_CHANNEL_IN_STEREO |
| 607 | formats AUDIO_FORMAT_PCM_16_BIT |
| 608 | devices AUDIO_DEVICE_IN_USB_DEVICE |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | ... |
| 613 | } |
| 614 | </pre> |
| 615 | |
| 616 | <h3 id="sourceCode">Source code</h3> |
| 617 | |
| 618 | <p> |
| 619 | The audio Hardware Abstraction Layer (HAL) |
| 620 | implementation for USB audio is located here: |
| 621 | <pre>hardware/libhardware/modules/usbaudio/</pre> |
| 622 | The USB audio HAL relies heavily on |
Clay Murphy | b8249cb | 2014-12-03 14:07:50 -0800 | [diff] [blame] | 623 | <i>tinyalsa</i>, described at <a href="terminology.html">Audio Terminology</a>. |
Glenn Kasten | 949ae0b | 2014-07-31 06:57:49 -0700 | [diff] [blame] | 624 | Though USB audio relies on isochronous transfers, |
| 625 | this is abstracted away by the ALSA implementation. |
| 626 | So the USB audio HAL and tinyalsa do not need to concern |
| 627 | themselves with this part of USB protocol. |
| 628 | </p> |