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> 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 72Android <a href="http://en.wikipedia.org/wiki/Mobile_device">device</a> 73or 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> 78A critical host role is <i>enumeration</i>: 79the process of detecting which peripherals are connected to the bus, 80and querying their properties expressed via <i>descriptors</i>. 81</p> 82 83<p> 84A peripheral may be one physical object 85but actually implement multiple logical <i>functions</i>. 86For example, a webcam peripheral could have both a camera function and a 87microphone audio function. 88</p> 89 90<p> 91Each peripheral function has an <i>interface</i> that 92defines the protocol to communicate with that function. 93</p> 94 95<p> 96The host communicates with a peripheral over a 97<a href="http://en.wikipedia.org/wiki/Stream_(computing)">pipe</a> 98to an <a href="http://en.wikipedia.org/wiki/Communication_endpoint">endpoint</a>, 99a data source or sink 100provided by one of the peripheral's functions. 101</p> 102 103<p> 104There are two kinds of pipes: <i>message</i> and <i>stream</i>. 105A message pipe is used for bi-directional control and status. 106A stream pipe is used for uni-directional data transfer. 107</p> 108 109<p> 110The host initiates all data transfers, 111hence the terms <i>input</i> and <i>output</i> are expressed relative to the host. 112An input operation transfers data from the peripheral to the host, 113while an output operation transfers data from the host to the peripheral. 114</p> 115 116<p> 117There are three major data transfer modes: 118<i>interrupt</i>, <i>bulk</i>, and <i>isochronous</i>. 119Isochronous mode will be discussed further in the context of audio. 120</p> 121 122<p> 123The peripheral may have <i>terminals</i> that connect to the outside world, 124beyond the peripheral itself. In this way, the peripheral serves 125to translate between USB protocol and "real world" signals. 126The 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. 135The Android device appears as a USB peripheral 136to a host PC running a desktop operating system such as Linux, 137Mac 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> 139or 140<a href="http://developer.android.com/tools/help/adb.html">Android Debug Bridge (adb)</a>. 141The 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> 151As the Android device must act as host, and most Android devices include 152a micro-USB connector that does not directly permit host operation, 153an on-the-go (<a href="http://en.wikipedia.org/wiki/USB_On-The-Go">OTG</a>) adapter 154such as this is usually required: 155</p> 156 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 162 163<p> 164An Android device might not provide sufficient power to operate a 165particular peripheral, depending on how much power the peripheral needs, 166and how much the Android device is capable of supplying. Even if 167adequate power is available, the Android device battery charge may 168be 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 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> 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. 181In this mode, the Android device operates as a USB peripheral, 182under the control of another device such as a dock that serves as host. 183The difference between development mode and accessory mode 184is that additional USB functions are visible to the host, beyond adb. 185The Android device begins in development mode and then 186transitions to accessory mode via a re-negotiation process. 187</p> 188 189<p> 190Accessory mode was extended with additional features in Android 4.1, 191in 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> 199Each peripheral function has an associated <i>device class</i> document 200that specifies the standard protocol for that function. 201This enables <i>class compliant</i> hosts and peripheral functions 202to inter-operate, without detailed knowledge of each other's workings. 203Class compliance is critical if the host and peripheral are provided by 204different entities. 205</p> 206 207<p> 208The term <i>driverless</i> is a common synonym for <i>class compliant</i>, 209indicating that it is possible to use the standard features of such a 210peripheral without requiring an operating-system specific 211<a href="http://en.wikipedia.org/wiki/Device_driver">driver</a> to be installed. 212One can assume that a peripheral advertised as "no driver needed" 213for major desktop operating systems 214will be class compliant, though there may be exceptions. 215</p> 216 217<h3 id="audioClass">USB audio class</h3> 218 219<p> 220Here we concern ourselves only with peripherals that implement 221audio functions, and thus adhere to the audio device class. There are two 222editions 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> 228USB includes many other device classes, some of which may be confused 229with 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 232sector-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. 235Both MSC and MTP may be used for transferring audio files, 236but only USB audio class is suitable for real-time streaming. 237</p> 238 239<h3 id="audioTerminals">Audio terminals</h3> 240 241<p> 242The terminals of an audio peripheral are typically analog. 243The 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), 246and is carried over USB protocol to be consumed by 247the host. The ADC is a data <i>source</i> 248for the host. Similarly, the host sends a 249digital 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) 252converts and presents to an analog output terminal. 253The DAC is a <i>sink</i> for the host. 254</p> 255 256<h3 id="channels">Channels</h3> 257 258<p> 259A peripheral with audio function can include a source terminal, sink terminal, or both. 260Each direction may have one channel (<i>mono</i>), two channels 261(<i>stereo</i>), or more. 262Peripherals with more than two channels are called <i>multichannel</i>. 263It 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 265spatial 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>) 268to not assign any particular 269standard spatial meaning to each channel. In this case, it is up to the 270application and user to define how each channel is used. 271For example, a four-channel USB input stream might have the first three 272channels attached to various microphones within a room, and the final 273channel receiving input from an AM radio. 274</p> 275 276<h3 id="isochronous">Isochronous transfer mode</h3> 277 278<p> 279USB audio uses isochronous transfer mode for its real-time characteristics, 280at the expense of error recovery. 281In isochronous mode, bandwidth is guaranteed, and data transmission 282errors are detected using a cyclic redundancy check (CRC). But there is 283no packet acknowledgement or re-transmission in the event of error. 284</p> 285 286<p> 287Isochronous transmissions occur each Start Of Frame (SOF) period. 288The SOF period is one millisecond for full-speed, and 125 microseconds for 289high-speed. Each full-speed frame carries up to 1023 bytes of payload, 290and a high-speed frame carries up to 1024 bytes. Putting these together, 291we calculate the maximum transfer rate as 1,023,000 or 8,192,000 bytes 292per second. This sets a theoretical upper limit on the combined audio 293sample rate, channel count, and bit depth. The practical limit is lower. 294</p> 295 296<p> 297Within 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> 307In adaptive sub-mode, the peripheral sink or source adapts to a potentially varying sample rate 308of the host. 309</p> 310 311<p> 312In asynchronous (also called implicit feedback) sub-mode, 313the sink or source determines the sample rate, and the host accomodates. 314The primary theoretical advantage of asynchronous sub-mode is that the source 315or sink USB clock is physically and electrically closer to (and indeed may 316be the same as, or derived from) the clock that drives the DAC or ADC. 317This proximity means that asynchronous sub-mode should be less susceptible 318to clock jitter. In addition, the clock used by the DAC or ADC may be 319designed for higher accuracy and lower drift than the host clock. 320</p> 321 322<p> 323In synchronous sub-mode, a fixed number of bytes is transferred each SOF period. 324The audio sample rate is effectively derived from the USB clock. 325Synchronous sub-mode is not commonly used with audio because both 326host and peripheral are at the mercy of the USB clock. 327</p> 328 329<p> 330The 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> 361In practice, the sub-mode does of course matter, but other factors 362should 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> 370USB audio is not supported in development mode. 371</p> 372 373<h3 id="hostAudio">Host mode</h3> 374 375<p> 376Android 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 38324 bits of useful audio data are left-justified within the most significant 384bits 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> 390Perusal of the Android framework source code may show additional code 391beyond the minimum needed to support these features. But this code 392has not been validated, so more advanced features are not yet claimed. 393</p> 394 395<h3 id="accessoryAudio">Accessory mode</h3> 396 397<p> 398Android 4.1 (API level 16) added limited support for audio playback to the host. 399While in accessory mode, Android automatically routes its audio output to USB. 400That is, the Android device serves as a data source to the host, for example a dock. 401</p> 402 403<p> 404Accessory mode audio has these features: 405</p> 406 407<ul> 408<li> 409The Android device must be controlled by a knowledgeable host that 410can first transition the Android device from development mode to accessory mode, 411and then the host must transfer audio data from the appropriate endpoint. 412Thus 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> 421Accessory mode audio has not been widely adopted, 422and is not currently recommended for new designs. 423</p> 424 425<h2 id="applications">Applications of USB digital audio</h2> 426 427<p> 428As the name indicates, the USB digital audio signal is represented 429by a <a href="http://en.wikipedia.org/wiki/Digital_data">digital</a> data stream 430rather than the <a href="http://en.wikipedia.org/wiki/Analog_signal">analog</a> 431signal used by the common TRS mini 432<a href=" http://en.wikipedia.org/wiki/Phone_connector_(audio)">headset connector</a>. 433Eventually any digital signal must be converted to analog before it can be heard. 434There 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> 440In the example diagram below, we compare two designs. First we have a 441mobile device with Application Processor (AP), on-board DAC, amplifier, 442and analog TRS connector attached to headphones. We also consider a 443mobile device with USB connected to external USB DAC and amplifier, 444also with headphones. 445</p> 446 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> 451 452<p> 453Which design is better? The answer depends on your needs. 454Each has advantages and disadvantages. 455<b>Note:</b> 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> 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<pre>device/oem/codename/audio_policy.conf</pre> 576The pathname component "oem" should be replaced by the name 577of the OEM who manufactures the Android device, 578and "codename" should be replaced by the device code name. 579</p> 580 581<p> 582An example entry is shown here: 583</p> 584 585<pre> 586audio_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> 619The audio Hardware Abstraction Layer (HAL) 620implementation for USB audio is located here: 621<pre>hardware/libhardware/modules/usbaudio/</pre> 622The USB audio HAL relies heavily on 623<i>tinyalsa</i>, described at <a href="terminology.html">Audio Terminology</a>. 624Though USB audio relies on isochronous transfers, 625this is abstracted away by the ALSA implementation. 626So the USB audio HAL and tinyalsa do not need to concern 627themselves with this part of USB protocol. 628</p> 629