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