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