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