• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=OpenSL ES for Android
2@jd:body
3
4<div id="qv-wrapper">
5    <div id="qv">
6      <h2>On this page</h2>
7
8      <ol>
9        <li><a href="#getstarted">Getting Started</a></li>
10        <li><a href="#inherited">Features Inherited from the Reference Specification</a></li>
11        <li><a href="#planning">Planning for Future Versions of OpenSL ES</a></li>
12        <li><a href="#ae">Android Extensions</a></li>
13        <li><a href="#notes">Programming Notes</a></li>
14        <li><a href="#platform-issues">Platform Issues</a></li>
15      </ol>
16    </div>
17  </div>
18
19<p>
20This page provides details about how the
21<a href="{@docRoot}tools/sdk/ndk/index.html">NDK</a> implementation of OpenSL
22ES™ differs from the reference specification for OpenSL ES 1.0.1. When using sample code from the
23specification, you may need to modify it to work on Android.
24</p>
25
26<p>
27Unless otherwise noted, all features are available at Android 2.3 (API level 9) and higher.
28 Some features are only available for Android 4.0 (API level 14); these are noted.
29</p>
30
31<p class="note"><strong>Note: </strong>
32The Android Compatibility Definition Document (CDD) enumerates the hardware and software
33requirements of a compatible Android device. See
34<a class="external-link" href="https://source.android.com/compatibility/">Android Compatibility</a>
35for more information on the overall compatibility program, and
36<a class="external-link" href="https://static.googleusercontent.com/media/source.android.com/en//compatibility/android-cdd.pdf">
37CDD</a> for the actual CDD document.
38</p>
39
40<p>
41<a class="external-link" href="https://www.khronos.org/opensles/">OpenSL ES</a> provides a C
42language interface that is also accessible using C++. It exposes features similar to the audio
43portions of these Android Java APIs:
44</p>
45
46<ul>
47  <li><a href="{@docRoot}reference/android/media/MediaPlayer.html">
48  android.media.MediaPlayer</a></li>
49  <li><a href="{@docRoot}reference/android/media/MediaRecorder.html">
50  android.media.MediaRecorder</a></li>
51</ul>
52
53<p>
54As with all of the Android Native Development Kit (NDK), the primary purpose of OpenSL ES for
55Android is to facilitate the implementation of shared libraries to be called using the Java Native
56Interface (<a class="external-link" href="https://en.wikipedia.org/wiki/Java_Native_Interface">JNI
57</a>). NDK is not intended for writing pure C/C++ applications. However, OpenSL ES is a
58full-featured API, and we expect that you should be able to accomplish most of your audio needs
59using only this API, without up-calls to code running in the Android runtime.
60</p>
61
62<p class="note"><strong>Note: </strong>
63Though based on OpenSL ES, the Android native audio (high-performance audio) API  is not a
64conforming implementation of any OpenSL ES 1.0.1 profile (game, music, or phone). This is because
65Android does not implement all of the features required by any one of the profiles. Any known cases
66where Android behaves differently than the specification are described in the <a href="#ae">
67Android extensions</a> section below.
68</p>
69
70<h2 id="getstarted">Getting Started</h2>
71
72<p>
73This section provides the information needed to get started using the OpenSL ES APIs.
74</p>
75
76<h3>Example code</h3>
77
78<p>
79We recommend using supported and tested example code that is usable as a model for your own
80code, which is located in the NDK folder {@code platforms/android-9/samples/native-audio/}, as well
81as in the
82<a class="external-link" href="https://github.com/googlesamples/android-ndk/tree/master/audio-echo">audio-echo</a>
83and
84<a class="external-link" href="https://github.com/googlesamples/android-ndk/tree/master/native-audio">native-audio</a>
85folders of the
86<a class="external-link" href="https://github.com/googlesamples/android-ndk">android-ndk</a> GitHub
87repository.
88</p>
89
90<p class="caution"><strong>Caution: </strong>
91The OpenSL ES 1.0.1 specification contains example code in the appendices (see
92<a class="external-link" href="https://www.khronos.org/registry/sles/">Khronos OpenSL ES Registry</a>
93for more details). However, the examples in <em>Appendix B: Sample Code</em> and
94<em>Appendix C: Use Case Sample Code</em> use features that are not supported by Android. Some
95examples also contain typographical errors, or use APIs that are likely to change. Proceed with
96caution when referring to these; though the code may be helpful in understanding the full OpenSL ES
97standard, it should not be used as-is with Android.
98</p>
99
100<h3>Makefile</h3>
101
102<p>
103Modify your {@code Android.mk} file as follows:
104</p>
105<pre>
106LOCAL_LDLIBS += -lOpenSLES
107</pre>
108
109<h3>Audio content</h3>
110
111<p>
112The following are some of the many ways to package audio content for your application:
113</p>
114
115<ul>
116  <li><strong>Resources</strong>: By placing your audio files into the {@code res/raw/} folder,
117  they can be accessed easily by the associated APIs for
118  <a href="{@docRoot}reference/android/content/res/Resources.html">Resources</a>.
119  However, there is no direct native access to resources, so you must write Java
120  programming language code to copy them out before use.</li>
121  <li><strong>Assets</strong>: By placing your audio files into the {@code assets/} folder, they
122  are directly accessible by the Android native asset manager APIs. See the header files {@code
123  android/asset_manager.h} and {@code android/asset_manager_jni.h} for more information on these
124  APIs. The example code located in the NDK folder {@code platforms/android-9/samples/native-audio/}
125  uses these native asset manager APIs in conjunction with the Android file descriptor data
126  locator.</li>
127  <li><strong>Network</strong>: You can use the URI data locator to play audio content directly
128  from the network. However, be sure to read the <a href="#sandp">Security and permissions</a>
129  section below.</li>
130  <li><strong>Local file system</strong>: The URI data locator supports the {@code file:} scheme
131  for local files, provided the files are accessible by the application. Note that the Android
132  security framework restricts file access via the Linux user ID and group ID mechanisms.</li>
133  <li><strong>Recorded</strong>: Your application can record audio data from the microphone input,
134  store this content, and then play it back later. The example code uses this method for the <em>
135  Playback</em> clip.</li>
136  <li><strong>Compiled and linked inline</strong>: You can link your audio content directly into
137  the shared library, and then play it using an audio player with buffer queue data locator. This
138  is most suitable for short PCM format clips. The example code uses this technique for the <em>
139  Hello</em> and <em>Android</em> clips. The PCM data was converted to hex strings using a
140  {@code bin2c} tool (not supplied).</li>
141  <li><strong>Real-time synthesis</strong>: Your application can synthesize PCM data on the fly and
142  then play it using an audio player with buffer queue data locator. This is a relatively advanced
143  technique, and the details of audio synthesis are beyond the scope of this article.</li>
144</ul>
145
146<p class="note"><strong>Note: </strong>
147Finding or creating useful audio content for your application is beyond the scope of this article.
148You can use web search terms such as <em>interactive audio</em>, <em>game audio</em>, <em>sound
149design</em>, and <em>audio programming</em> to locate more information.
150</p>
151<p class="caution"><strong>Caution:</strong> It is your responsibility
152to ensure that you are legally permitted to play or record content. There may be privacy
153considerations for recording content.
154</p>
155
156<h2 id="inherited">Features Inherited from the Reference Specification</h2>
157
158<p>
159The Android NDK implementation of OpenSL ES inherits much of the feature set from
160the reference specification, with certain limitations.
161</p>
162
163<h3>Global entry points</h3>
164
165<p>
166OpenSL ES for Android supports all of the global entry points in the Android specification.
167These entry points include:
168</p>
169
170<ul>
171<li>{@code slCreateEngine}
172</li>
173<li>{@code slQueryNumSupportedEngineInterfaces}</code>
174</li>
175<li>{@code slQuerySupportedEngineInterfaces}</code>
176</li>
177</ul>
178
179<h3>Objects and interfaces</h3>
180
181<p>
182Table 1 shows the objects and interfaces that the Android NDK implementation of
183OpenSL ES supports. If a <em>Yes</em> appears in the cell, then the feature is available in this
184implementation.
185</p>
186
187<p class="table-caption" id="Objects-and-interfaces">
188  <strong>Table 1.</strong> Android NDK support for objects and interfaces.</p>
189<table>
190  <tr>
191    <th scope="col">Feature</th>
192    <th scope="col">Audio player</th>
193    <th scope="col">Audio recorder</th>
194    <th scope="col">Engine</th>
195    <th scope="col">Output mix</th>
196  </tr>
197  <tr>
198    <td>Bass boost</td>
199    <td>Yes</td>
200    <td>No</td>
201    <td>No</td>
202    <td>Yes</td>
203  </tr>
204  <tr>
205    <td>Buffer queue</td>
206    <td>Yes</td>
207    <td>No</td>
208    <td>No</td>
209    <td>No</td>
210  </tr>
211  <tr>
212    <td>Dynamic interface management</td>
213    <td>Yes</td>
214    <td>Yes</td>
215    <td>Yes</td>
216    <td>Yes</td>
217  </tr>
218  <tr>
219    <td>Effect send</td>
220    <td>Yes</td>
221    <td>No</td>
222    <td>No</td>
223    <td>No</td>
224  </tr>
225  <tr>
226    <td>Engine</td>
227    <td>No</td>
228    <td>No</td>
229    <td>Yes</td>
230    <td>No</td>
231  </tr>
232  <tr>
233    <td>Environmental reverb</td>
234    <td>No</td>
235    <td>No</td>
236    <td>No</td>
237    <td>Yes</td>
238  </tr>
239  <tr>
240    <td>Equalizer</td>
241    <td>Yes</td>
242    <td>No</td>
243    <td>No</td>
244    <td>Yes</td>
245  </tr>
246  <tr>
247    <td>Metadata extraction</td>
248    <td>Yes: Decode to PCM</td>
249    <td>No</td>
250    <td>No</td>
251    <td>No</td>
252  </tr>
253  <tr>
254    <td>Mute solo</td>
255    <td>Yes</td>
256    <td>No</td>
257    <td>No</td>
258    <td>No</td>
259  </tr>
260  <tr>
261    <td>Object</td>
262    <td>Yes</td>
263    <td>Yes</td>
264    <td>Yes</td>
265    <td>Yes</td>
266  </tr>
267  <tr>
268    <td>Play</td>
269    <td>Yes</td>
270    <td>No</td>
271    <td>No</td>
272    <td>No</td>
273  </tr>
274  <tr>
275    <td>Playback rate</td>
276    <td>Yes</td>
277    <td>No</td>
278    <td>No</td>
279    <td>No</td>
280  </tr>
281  <tr>
282    <td>Prefetch status</td>
283    <td>Yes</td>
284    <td>No</td>
285    <td>No</td>
286    <td>No</td>
287  </tr>
288  <tr>
289    <td>Preset reverb</td>
290    <td>No</td>
291    <td>No</td>
292    <td>No</td>
293    <td>Yes</td>
294  </tr>
295  <tr>
296    <td>Record</td>
297    <td>No</td>
298    <td>Yes</td>
299    <td>No</td>
300    <td>No</td>
301  </tr>
302  <tr>
303    <td>Seek</td>
304    <td>Yes</td>
305    <td>No</td>
306    <td>No</td>
307    <td>No</td>
308  </tr>
309  <tr>
310    <td>Virtualizer</td>
311    <td>Yes</td>
312    <td>No</td>
313    <td>No</td>
314    <td>Yes</td>
315  </tr>
316  <tr>
317    <td>Volume</td>
318    <td>Yes</td>
319    <td>No</td>
320    <td>No</td>
321    <td>No</td>
322  </tr>
323  <tr>
324    <td>Buffer queue data locator</td>
325    <td>Yes: Source</td>
326    <td>No</td>
327    <td>No</td>
328    <td>No</td>
329  </tr>
330  <tr>
331    <td>I/O device data locator</td>
332    <td>No</td>
333    <td>Yes: Source</td>
334    <td>No</td>
335    <td>No</td>
336  </tr>
337  <tr>
338    <td>Output mix locator</td>
339    <td>Yes: Sink</td>
340    <td>No</td>
341    <td>No</td>
342    <td>No</td>
343  </tr>
344  <tr>
345    <td>URI data locator</td>
346    <td>Yes: Source</td>
347    <td>No</td>
348    <td>No</td>
349    <td>No</td>
350  </tr>
351  </table>
352
353<p>
354The next section explains the limitations for some of these features.
355</p>
356
357<h3>Limitations</h3>
358
359<p>
360Certain limitations apply to the features in Table 1. These limitations
361represent differences from the reference specification. The rest of this section provides
362information about these differences.</p>
363
364<h4>Dynamic interface management</h4>
365
366<p>
367OpenSL ES for Android does not support {@code RemoveInterface} or
368{@code ResumeInterface}.
369</p>
370
371<h4>Effect combinations: environment reverb and preset reverb</h4>
372
373<p>
374You cannot have both environmental reverb and preset reverb on the same output mix.
375</p>
376<p>
377The platform might ignore effect requests if it estimates that the
378CPU load would be too high.
379</p>
380
381<h4>Effect send</h4>
382
383<p>
384<code>SetSendLevel()</code> supports a single send level per audio player.
385</p>
386
387<h4>Environmental reverb</h4>
388
389<p>
390Environmental reverb does not support the <code>reflectionsDelay</code>,
391<code>reflectionsLevel</code>, or <code>reverbDelay</code> fields of
392the <code>SLEnvironmentalReverbSettings</code> struct.
393</p>
394
395<h4>MIME data format</h4>
396
397<p>
398You can use the MIME data format only with the URI data locator, and only for an audio
399player. You cannot use this data format for an audio recorder.
400</p>
401<p>
402The Android implementation of OpenSL ES requires you to initialize <code>mimeType</code>
403to either <code>NULL</code> or a valid UTF-8 string. You must also initialize
404<code>containerType</code> to a valid value.
405In the absence of other considerations, such as portability to other
406implementations or content format that an app cannot identify by header,
407we recommend that you
408set <code>mimeType</code> to <code>NULL</code> and <code>containerType</code>
409to <code>SL_CONTAINERTYPE_UNSPECIFIED</code>.
410</p>
411<p>
412OpenSL ES for Android supports the following audio formats, so long as the
413Android platform supports them as well:</p>
414
415<ul>
416<li><a class="external-link" href="https://en.wikipedia.org/wiki/WAV">WAV</a> PCM.</li>
417<li>WAV alaw.</li>
418<li>WAV ulaw.</li>
419<li>MP3 Ogg Vorbis.</li>
420<li>AAC LC.</li>
421<li>HE-AACv1 (AAC+).</li>
422<li>HE-AACv2 (enhanced AAC+).</li>
423<li>AMR.</li>
424<li>FLAC.</li>
425</ul>
426
427<p class="note"><strong>Note: </strong>
428For a list of audio formats that Android supports, see
429<a href="{@docRoot}guide/appendix/media-formats.html">Supported Media Formats</a>.
430</p>
431
432<p>
433The following limitations apply to the handling of these and other formats in this
434implementation of OpenSL ES:
435</p>
436
437<ul>
438<li><a class="external-link" href="https://en.wikipedia.org/wiki/Advanced_Audio_Coding">AAC</a>
439formats must reside within an MP4 or ADTS container.</li>
440<li>OpenSL ES for Android does not support
441<a class="external-link" href="https://source.android.com/devices/audio/midi.html">MIDI</a>.</li>
442<li>WMA is not part of <a class="external-link" href="https://source.android.com/">AOSP</a>, and we
443have not verified its compatibility with OpenSL ES for Android.</li>
444<li>The Android NDK implementation of OpenSL ES does not support direct
445playback of DRM or encrypted content. To play back protected audio content, you must
446decrypt it in your application before playing, with your app enforcing any DRM
447restrictions.</li>
448</ul>
449
450<h4>Object-related methods</h4>
451
452<p>
453OpenSL ES for Android does not support the following methods for manipulating objects:
454</p>
455
456<ul>
457<li>{@code Resume()}</li>
458<li>{@code RegisterCallback()}</li>
459<li>{@code AbortAsyncOperation()}</li>
460<li>{@code SetPriority()}</li>
461<li>{@code GetPriority()}</li>
462<li>{@code SetLossOfControlInterfaces()}</li>
463</ul>
464
465<h4>PCM data format</h4>
466
467<p>
468PCM is the only data format you can use with buffer queues. Supported PCM
469playback configurations have the following characteristics:
470</p>
471
472<ul>
473<li>8-bit unsigned or 16-bit signed.</li>
474<li>Mono or stereo.</li>
475<li>Little-endian byte ordering.</li>
476<li>Sample rates of:
477  <ul>
478    <li>8,000 Hz.</li>
479    <li>11,025 Hz.</li>
480    <li>12,000 Hz.</li>
481    <li>16,000 Hz.</li>
482    <li>22,050 Hz.</li>
483    <li>24,000 Hz.</li>
484    <li>32,000 Hz.</li>
485    <li>44,100 Hz.</li>
486    <li>48,000 Hz.</li>
487  </ul></li>
488</ul>
489
490<p>
491The configurations that OpenSL ES for Android supports for recording are
492device-dependent; usually, 16,000 Hz mono/16-bit signed is available regardless of the device.
493</p>
494<p>
495The value of the <code>samplesPerSec</code> field is in units of milliHz, despite the misleading
496name. To avoid accidentally using the wrong value, we recommend that you initialize this field using
497one of the symbolic constants defined for this purpose, such as {@code SL_SAMPLINGRATE_44_1}.
498</p>
499<p>
500Android 5.0 (API level 21) and above support <a href="#fp">floating-point data</a>.
501</p>
502
503<h4>Playback rate</h4>
504
505<p>
506An OpenSL ES <i>playback rate</i> indicates the speed at which an
507object presents data, expressed in thousandths of normal speed, or <i>per mille</i>. For example,
508a playback rate of 1,000 per mille is 1,000/1,000, or normal speed.
509A <i>rate range</i> is a closed interval that expresses possible rate ranges.
510</p>
511
512<p>
513Support for playback-rate ranges and other capabilities may vary depending
514on the platform version and implementation. Your app can determine these capabilities at runtime by
515using <code>PlaybackRate::GetRateRange()</code> or
516<code>PlaybackRate::GetCapabilitiesOfRate()</code> to query the device.
517</p>
518
519<p>
520A device typically supports the same rate range for a data source in PCM format, and a unity rate
521range of 1000 per mille to 1000 per mille for other formats: that is, the unity rate range is
522effectively a single value.
523</p>
524
525<h4>Record</h4>
526
527<p>
528OpenSL ES for Android does not support the <code>SL_RECORDEVENT_HEADATLIMIT</code>
529or <code>SL_RECORDEVENT_HEADMOVING</code> events.
530</p>
531
532<h4>Seek</h4>
533
534<p>
535The <code>SetLoop()</code> method enables whole-file looping. To enable looping,
536set the <code>startPos</code> parameter to 0, and the value of the <code>endPos</code> parameter
537to <code>SL_TIME_UNKNOWN</code>.
538</p>
539
540<h4>Buffer queue data locator</h4>
541
542<p>
543An audio player or recorder with a data locator for a buffer queue supports PCM data format only.
544</p>
545
546<h4>I/O device data locator</h4>
547
548<p>
549OpenSL ES for Android only supports use of an I/O device data locator when you have
550specified the locator as the data source for <code>Engine::CreateAudioRecorder()</code>.
551Initialize the device data locator using the values contained in the following code snippet.
552</p>
553
554<pre>
555SLDataLocator_IODevice loc_dev =
556  {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
557  SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
558</pre>
559
560<h4>URI data locator</h4>
561
562<p>
563OpenSL ES for Android can only use the URI data locator with MIME data format,
564and only for an audio player. You cannot use this data format for an audio recorder. It supports
565{@code http:} and {@code file:} schemes. It does not support other schemes, such as {@code https:},
566{@code ftp:}, or
567{@code content:}.
568</p>
569
570<p>
571We have not verified support for {@code rtsp:} with audio on the Android platform.
572</p>
573
574<h4>Data structures</h4>
575
576<p>
577Android supports these OpenSL ES 1.0.1 data structures:
578</p>
579<ul>
580  <li>{@code SLDataFormat_MIME}</li>
581  <li>{@code SLDataFormat_PCM}</li>
582  <li>{@code SLDataLocator_BufferQueue}</li>
583  <li>{@code SLDataLocator_IODevice}</li>
584  <li>{@code SLDataLocator_OutputMix}</li>
585  <li>{@code SLDataLocator_URI}</li>
586  <li>{@code SLDataSink}</li>
587  <li>{@code SLDataSource}</li>
588  <li>{@code SLEngineOption}</li>
589  <li>{@code SLEnvironmentalReverbSettings}</li>
590  <li>{@code SLInterfaceID}</li>
591</ul>
592
593<h4>Platform configuration</h4>
594
595<p>
596OpenSL ES for Android is designed for multi-threaded applications and is thread-safe. It supports a
597single engine per application, and up to 32 objects per engine. Available device memory and CPU may
598further restrict the usable number of objects.
599</p>
600
601<p>
602These engine options are recognized, but ignored by {@code slCreateEngine}:
603</p>
604
605<ul>
606  <li>{@code SL_ENGINEOPTION_THREADSAFE}</li>
607  <li>{@code SL_ENGINEOPTION_LOSSOFCONTROL}</li>
608</ul>
609
610<p>
611OpenMAX AL and OpenSL ES may be used together in the same application. In this case, there is
612a single shared engine object internally, and the 32 object limit is shared between OpenMAX AL
613and OpenSL ES. The application should first create both engines, use both engines, and finally
614destroy both engines. The implementation maintains a reference count on the shared engine so that
615it is correctly destroyed during the second destroy operation.
616</p>
617
618<h2 id="planning">Planning for Future Versions of OpenSL ES</h2>
619
620<p>
621The Android high-performance audio APIs are based on
622<a class="external-link" href="https://www.khronos.org/registry/sles/">Khronos Group OpenSL ES
6231.0.1</a>. Khronos has released a revised version 1.1 of the standard. The
624revised version includes new features, clarifications, corrections of typographical errors, and
625some incompatibilities. Most of the expected incompatibilities are relatively minor or are in
626areas of OpenSL ES that are not supported by Android.
627</p>
628
629<p>
630An application
631developed with this version should work on future versions of the Android platform, provided
632that you follow the guidelines that are outlined in the <a href="#binary-compat">Planning for
633binary compatibility</a> section below.
634</p>
635
636<p class="note"><strong>Note: </strong>
637Future source compatibility is not a goal. That is, if you upgrade to a newer version of the NDK,
638you may need to modify your application source code to conform to the new API. We expect that most
639such changes will be minor; see details below.
640</p>
641
642<h3 id="binary-compat">Planning for binary compatibility</h3>
643
644<p>
645We recommend that your application follow these guidelines to improve future binary compatibility:
646</p>
647
648<ul>
649  <li>Use only the documented subset of Android-supported features from OpenSL ES 1.0.1.</li>
650  <li>Do not depend on a particular result code for an unsuccessful operation; be prepared to deal
651  with a different result code.</li>
652  <li>Application callback handlers generally run in a restricted context. They should be written
653  to perform their work quickly, and then return as soon as possible. Do not run complex operations
654  within a callback handler. For example, within a buffer queue completion callback, you can
655  enqueue another buffer, but do not create an audio player.</li>
656  <li>Callback handlers should be prepared to be called more or less frequently, to receive
657  additional event types, and should ignore event types that they do not recognize. Callbacks that
658  are configured with an event mask made of enabled event types should be prepared to be called
659  with multiple event type bits set simultaneously. Use "&" to test for each event bit rather than
660  a switch case.</li>
661  <li>Use prefetch status and callbacks as a general indication of progress, but do not depend on
662  specific hard-coded fill levels or callback sequences. The meaning of the prefetch status fill
663  level, and the behavior for errors that are detected during prefetch, may change.</li>
664</ul>
665
666<p class="note"><strong>Note: </strong>
667See the <a href="#bq-behavior">Buffer queue behavior</a> section below for more details.
668</p>
669
670<h3>Planning for source compatibility</h3>
671
672<p>
673As mentioned, source code incompatibilities are expected in the next version of OpenSL ES from
674Khronos Group. The likely areas of change include:
675</p>
676
677<ul>
678  <li>The buffer queue interface is expected to have significant changes, especially in the areas
679  of {@code BufferQueue::Enqueue}, the parameter list for {@code slBufferQueueCallback}, and the
680  name of field {@code SLBufferQueueState.playIndex}. We recommend that your application code use
681  Android simple buffer queues instead. In the example
682  code that is supplied with the NDK, we have used Android simple buffer queues for playback for
683  this reason. (We also use Android simple buffer queue for recording and decoding to PCM, but that
684  is because standard OpenSL ES 1.0.1 does not support record or decode to a buffer queue data
685  sink.)</li>
686  <li>There will be an addition of {@code const} to the input parameters passed by reference, and
687  to {@code SLchar *} struct fields used as input values. This should not require any changes to
688  your code.</li>
689  <li>There will be a substitution of unsigned types for some parameters that are currently signed.
690  You may need to change a parameter type from {@code SLint32} to {@code SLuint32} or similar, or
691  add a cast.</li>
692  <li>{@code Equalizer::GetPresetName} copies the string to application memory instead of returning
693  a pointer to implementation memory. This will be a significant change, so we recommend that you
694  either avoid calling this method, or isolate your use of it.</li>
695  <li>There will be additional fields in the struct types. For output parameters, these new fields
696  can be ignored, but for input parameters the new fields will need to be initialized. Fortunately,
697  all of these are expected to be in areas that are not supported by Android.</li>
698  <li>Interface <a class="external-link" href="http://en.wikipedia.org/wiki/Globally_unique_identifier">
699  GUIDs</a> will change. Refer to interfaces by symbolic name rather than GUID to avoid a
700  dependency.</li>
701  <li>{@code SLchar} will change from {@code unsigned char} to {@code char}. This primarily affects
702  the URI data locator and MIME data format.</li>
703  <li>{@code SLDataFormat_MIME.mimeType} will be renamed to {@code pMimeType}, and
704  {@code SLDataLocator_URI.URI} will be renamed to {@code pURI}. We recommend that you initialize
705  the {@code SLDataFormat_MIME} and {@code SLDataLocator_URI} data structures using a
706  brace-enclosed, comma-separated list of values, rather than by field name, to isolate your code
707  from this change. This technique is used in the example code.</li>
708  <li>{@code SL_DATAFORMAT_PCM} does not permit the application to specify the representation of
709  the data as signed integer, unsigned integer, or floating-point. The Android implementation
710  assumes that 8-bit data is unsigned integer and 16-bit is signed integer. In addition, the field
711  {@code samplesPerSec} is a misnomer, as the actual units are milliHz. These issues are expected
712  to be addressed in the next OpenSL ES version, which will introduce a new extended PCM data
713  format that permits the application to explicitly specify the representation and corrects the
714  field name. As this will be a new data format, and the current PCM data format will still be
715  available (though deprecated), it should not require any immediate changes to your code.</li>
716</ul>
717
718<h2 id="ae">Android Extensions</h2>
719
720<p>
721OpenSL ES for Android extends the reference OpenSL ES specification to make it compatible with
722Android, and to take advantage of the power and flexibility of the Android platform.
723</p>
724
725<p>
726The definition of the API for the Android extensions resides in <code>OpenSLES_Android.h</code>
727and the header files that it includes. Consult {@code OpenSLES_Android.h}
728for details about these extensions. This file is located under your installation root, in the
729{@code platforms/android-&lt;version&gt;/&lt;abi&gt;/include/SLES} directory. Unless otherwise
730noted, all interfaces are explicit.
731</p>
732
733<p>
734These extensions limit your application's portability to
735other OpenSL ES implementations, because they are Android-specific. You can mitigate this issue by
736avoiding use of the extensions or by using {@code #ifdef} to exclude them at compile time.
737</p>
738
739<p>
740Table 2 shows the Android-specific interfaces and data locators that Android OpenSL ES supports
741for each object type. The <em>Yes</em> values in the cells indicate the interfaces and data
742locators that are available for each object type.
743</p>
744
745<p class="table-caption" id="Android-extensions">
746  <strong>Table 2.</strong> Interfaces and data locators, by object type.</p>
747<table>
748  <tr>
749    <th scope="col">Feature</th>
750    <th scope="col">Audio player</th>
751    <th scope="col">Audio recorder</th>
752    <th scope="col">Engine</th>
753    <th scope="col">Output mix</th>
754  </tr>
755  <tr>
756    <td>Android buffer queue</td>
757    <td>Yes: Source (decode)</td>
758    <td>No</td>
759    <td>No</td>
760    <td>No</td>
761  </tr>
762  <tr>
763    <td>Android configuration</td>
764    <td>Yes</td>
765    <td>Yes</td>
766    <td>No</td>
767    <td>No</td>
768  </tr>
769  <tr>
770    <td>Android effect</td>
771    <td>Yes</td>
772    <td>No</td>
773    <td>No</td>
774    <td>Yes</td>
775  </tr>
776  <tr>
777    <td>Android effect capabilities</td>
778    <td>No</td>
779    <td>No</td>
780    <td>Yes</td>
781    <td>No</td>
782  </tr>
783  <tr>
784    <td>Android effect send</td>
785    <td>Yes</td>
786    <td>No</td>
787    <td>No</td>
788    <td>No</td>
789  </tr>
790  <tr>
791    <td>Android simple buffer queue</td>
792    <td>Yes: Source (playback) or sink (decode)</td>
793    <td>Yes</td>
794    <td>No</td>
795    <td>No</td>
796  </tr>
797  <tr>
798    <td>Android buffer queue data locator</td>
799    <td>Yes: Source (decode)</td>
800    <td>No</td>
801    <td>No</td>
802    <td>No</td>
803  </tr>
804  <tr>
805    <td>Android file descriptor data locator</td>
806    <td>Yes: Source</td>
807    <td>No</td>
808    <td>No</td>
809    <td>No</td>
810  </tr>
811  <tr>
812    <td>Android simple buffer queue data locator</td>
813    <td>Yes: Source (playback) or sink (decode)</td>
814    <td>Yes: Sink</td>
815    <td>No</td>
816    <td>No</td>
817  </tr>
818</table>
819
820<h3 id="configuration-interface">Android configuration interface</h3>
821
822<p>
823The Android configuration interface provides a means to set
824platform-specific parameters for objects. This interface is different from other OpenSL ES
8251.0.1 interfaces in that your app can use it before instantiating the corresponding object; thus,
826you can configure the object before instantiating it. The
827{@code OpenSLES_AndroidConfiguration.h} header file</code>, which resides at
828{@code platforms/android-&lt;version&gt;/&lt;abi&gt;/include/SLES},
829documents the following available configuration keys and values:
830</p>
831
832<ul>
833<li>Stream type for audio players (default <code>SL_ANDROID_STREAM_MEDIA</code>).</li>
834<li>Record profile for audio recorders (default <code>SL_ANDROID_RECORDING_PRESET_GENERIC</code>).
835</li>
836</ul>
837
838<p>
839The following code snippet shows an example of how to set the Android audio stream type on an audio
840player:
841</p>
842
843<pre>
844// CreateAudioPlayer and specify SL_IID_ANDROIDCONFIGURATION
845// in the required interface ID array. Do not realize player yet.
846// ...
847SLAndroidConfigurationItf playerConfig;
848result = (*playerObject)-&gt;GetInterface(playerObject,
849    SL_IID_ANDROIDCONFIGURATION, &amp;playerConfig);
850assert(SL_RESULT_SUCCESS == result);
851SLint32 streamType = SL_ANDROID_STREAM_ALARM;
852result = (*playerConfig)-&gt;SetConfiguration(playerConfig,
853    SL_ANDROID_KEY_STREAM_TYPE, &amp;streamType, sizeof(SLint32));
854assert(SL_RESULT_SUCCESS == result);
855// ...
856// Now realize the player here.
857</pre>
858
859<p>
860You can use similar code to configure the preset for an audio recorder:
861</p>
862<pre>
863// ... obtain the configuration interface as the first four lines above, then:
864SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
865result = (*playerConfig)-&gt;SetConfiguration(playerConfig,
866    RECORDING_PRESET, &amp;presetValue, sizeof(SLuint32));
867</pre>
868
869<h3>Android effects interfaces</h3>
870
871<p>
872Android's effect, effect send, and effect capabilities interfaces provide
873a generic mechanism for an application to query and use device-specific
874audio effects. Device manufacturers should document any available device-specific audio effects
875that they provide.
876</p>
877
878<p>
879Portable applications should use the OpenSL ES 1.0.1 APIs for audio effects instead of the Android
880effect extensions.
881</p>
882
883<h3>Android file descriptor data locator</h3>
884
885<p>
886The Android file descriptor data locator permits you to specify the source for an
887audio player as an open file descriptor with read access. The data format must be MIME.
888</p>
889<p>
890This extension is especially useful in conjunction with the native asset manager, because
891the app reads assets from the APK via a file descriptor.
892</p>
893
894<h3 id="simple">Android simple buffer queue data locator and interface</h3>
895
896<p>
897The Android simple buffer queue data locator and interface are
898identical to those in the OpenSL ES 1.0.1 reference specification, with two exceptions: You
899can also use Android simple buffer queues with both audio players and audio recorders. Also, PCM
900is the only data format you can use with these queues.
901In the reference specification, buffer queues are for audio players only, but they are
902compatible with data formats beyond PCM.
903</p>
904<p>
905For recording, your app should enqueue empty buffers. When a registered callback sends
906notification that the system has finished writing data to the buffer, the app can
907read the buffer.
908</p>
909<p>
910Playback works in the same way. For future source code
911compatibility, however, we suggest that applications use Android simple
912buffer queues instead of OpenSL ES 1.0.1 buffer queues.
913</p>
914
915<h3 id="dynamic-interfaces">Dynamic interfaces at object creation</h3>
916
917<p>
918For convenience, the Android implementation of OpenSL ES 1.0.1
919permits your app to specify dynamic interfaces when it instantiates an object.
920This is an alternative to using <code>DynamicInterfaceManagement::AddInterface()</code>
921to add these interfaces after instantiation.
922</p>
923
924<h3 id="bq-behavior">Buffer queue behavior</h3>
925
926<p>
927The Android implementation does not include the
928reference specification's requirement that the play cursor return to the beginning
929of the currently playing buffer when playback enters the {@code SL_PLAYSTATE_STOPPED}
930state. This implementation can conform to that behavior, or it can leave the location of the play
931cursor unchanged.
932</p>
933
934<p>
935As a result, your app cannot assume that either behavior occurs. Therefore,
936you should explicitly call the <code>BufferQueue::Clear()</code> method after a transition to
937<code>SL_PLAYSTATE_STOPPED</code>. Doing so sets the buffer queue to a known state.
938</p>
939
940<p>
941Similarly, there is no specification governing whether the trigger for a buffer queue callback must
942be a transition to <code>SL_PLAYSTATE_STOPPED</code> or execution of
943<code>BufferQueue::Clear()</code>. Therefore, we recommend that you do not create a dependency on
944one or the other; instead, your app should be able to handle both.
945</p>
946
947<h3>Reporting of extensions</h3>
948<p>
949There are three methods for querying whether the platform supports the Android extensions. These
950methods are:
951</p>
952
953<ul>
954<li><code>Engine::QueryNumSupportedExtensions()</code></li>
955<li><code>Engine::QuerySupportedExtension()</code></li>
956<li><code>Engine::IsExtensionSupported()</code></li>
957</ul>
958
959<p>
960Any of these methods returns <code>ANDROID_SDK_LEVEL_&lt;API-level&gt;</code>,
961where {@code API-level} is the platform API level; for example, {@code ANDROID_SDK_LEVEL_23}.
962A platform API level of 9 or higher means that the platform supports the extensions.
963</p>
964
965
966<h3 id="da">Decode audio to PCM</h3>
967
968<p>
969This section describes a deprecated Android-specific extension to OpenSL ES 1.0.1
970for decoding an encoded stream to PCM without immediate playback.
971The table below gives recommendations for use of this extension and alternatives.
972</p>
973
974<table>
975<tr>
976  <th>API level</th>
977  <th>Alternatives</th>
978</tr>
979<tr>
980  <td>13 and below</td>
981  <td>An open-source codec with a suitable license</td>
982</tr>
983<tr>
984  <td>14 to 15</td>
985  <td>An open-source codec with a suitable license</td>
986</tr>
987<tr>
988  <td>16 to 20</td>
989  <td>
990    The {@link android.media.MediaCodec} class or an open-source codec with a suitable license
991  </td>
992</tr>
993<tr>
994  <td>21 and above</td>
995  <td>
996    NDK MediaCodec in the {@code &lt;media/NdkMedia*.h&gt;} header files, the
997    {@link android.media.MediaCodec} class, or an open-source codec with a suitable license
998  </td>
999</tr>
1000</table>
1001
1002<p class="note"><strong>Note: </strong>
1003There is currently no documentation for the NDK version of the {@code MediaCodec} API. However,
1004you can refer to the
1005<a class="external-link" href="https://github.com/googlesamples/android-ndk/tree/master/native-codec">
1006native-codec</a> sample code for an example.
1007</p>
1008
1009<p>
1010A standard audio player plays back to an audio device, specifying the output mix as the data sink.
1011The Android extension differs in that an audio player instead
1012acts as a decoder if the app has specified the data source either as a URI or as an Android
1013file descriptor data locator described in MIME data format. In such a case, the data sink is
1014an Android simple buffer queue data locator with PCM data format.
1015</p>
1016
1017<p>
1018This feature is primarily intended for games to pre-load their audio assets when changing to a
1019new game level, which is similar to the functionality that the {@link android.media.SoundPool}
1020class provides.
1021</p>
1022
1023<p>
1024The application should initially enqueue a set of empty buffers in the Android simple
1025buffer queue. After that, the app fills the buffers with PCM data. The Android simple
1026buffer queue callback fires after each buffer is filled. The callback handler processes
1027the PCM data, re-enqueues the now-empty buffer, and then returns. The application is responsible for
1028keeping track of decoded buffers; the callback parameter list does not include
1029sufficient information to indicate the buffer that contains data or the buffer that should be
1030enqueued next.
1031</p>
1032
1033<p>
1034The data source implicitly reports the end of stream (EOS) by delivering a
1035<code>SL_PLAYEVENT_HEADATEND</code> event at the end of the stream. After the app has decoded
1036all of the data it received, it makes no further calls to the Android simple buffer queue callback.
1037</p>
1038<p>
1039The sink's PCM data format typically matches that of the encoded data source
1040with respect to sample rate, channel count, and bit depth. However, you can decode to a different
1041sample rate, channel count, or bit depth.
1042For information about a provision to detect the actual PCM format, see <a href="#meta">
1043Determining the format of decoded PCM data via metadata</a>.
1044</p>
1045<p>
1046OpenSL ES for Android's PCM decoding feature supports pause and initial seek; it does not support
1047volume control, effects, looping, or playback rate.
1048</p>
1049<p>
1050Depending on the platform implementation, decoding may require resources
1051that cannot be left idle.  Therefore, we recommend that you make sure to provide
1052sufficient numbers of empty PCM buffers; otherwise, the decoder starves. This may happen,
1053for example, if your app returns from the Android simple buffer queue callback without
1054enqueueing another empty buffer.  The result of decoder starvation is
1055unspecified, but may include: dropping the decoded
1056PCM data, pausing the decoding process, or terminating the decoder outright.
1057</p>
1058
1059<p class="note"><strong>Note: </strong>
1060To decode an encoded stream to PCM but not play back immediately, for apps running on
1061Android 4.x (API levels 16&ndash;20), we recommend using the {@link android.media.MediaCodec} class.
1062For new applications running on Android 5.0 (API level 21) or higher, we recommend using the NDK
1063equivalent, {@code &lt;NdkMedia*.h&gt;}. These header files reside in
1064the {@code media/} directory under your installation root.
1065</p>
1066
1067<h3>Decode streaming ADTS AAC to PCM</h3>
1068
1069<p>
1070An audio player acts as a streaming decoder if the data source is an
1071Android buffer queue data locator with MIME data format, and the data
1072sink is an Android simple buffer queue data locator with PCM data format.
1073Configure the MIME data format as follows:
1074</p>
1075
1076<ul>
1077<li>Container: {@code SL_CONTAINERTYPE_RAW}</li>
1078<li>MIME type string: {@code SL_ANDROID_MIME_AACADTS}</li>
1079</ul>
1080
1081<p>
1082This feature is primarily intended for streaming media applications that
1083deal with AAC audio but need to perform custom audio processing
1084prior to playback.  Most applications that need to decode audio to PCM
1085should use the method that <a href="#da">Decode audio to PCM</a> describes,
1086as that method is simpler and handles more audio formats.  The technique described
1087here is a more specialized approach, to be used only if both of these
1088conditions apply:
1089</p>
1090
1091<ul>
1092<li>The compressed audio source is a stream of AAC frames contained in ADTS headers.
1093</li>
1094<li>The application manages this stream. The data is <em>not</em> located within
1095a network resource whose identifier is a URI or within a local file whose identifier is
1096a file descriptor.
1097</li>
1098</ul>
1099
1100<p>
1101The application should initially enqueue a set of filled buffers in the Android buffer queue.
1102Each buffer contains one or more complete ADTS AAC frames.
1103The Android buffer queue callback fires after each buffer is emptied.
1104The callback handler should refill and re-enqueue the buffer, and then return.
1105The application need not keep track of encoded buffers; the callback parameter
1106list includes sufficient information to indicate the buffer that should be enqueued next.
1107The end of stream is explicitly marked by enqueuing an EOS item.
1108After EOS, no more enqueues are permitted.
1109</p>
1110
1111<p>
1112We recommend that you make sure to provide full
1113ADTS AAC buffers, to avoid starving the decoder. This may happen, for example, if your app
1114returns from the Android buffer queue callback without enqueueing another full buffer.
1115The result of decoder starvation is unspecified.
1116</p>
1117
1118<p>
1119In all respects except for the data source, the streaming decode method is the same as
1120the one that <a href="#da">Decode audio to PCM</a> describes.
1121</p>
1122
1123<p>
1124Despite the similarity in names, an Android buffer queue is <em>not</em>
1125the same as an <a href="#simple">Android simple buffer queue</a>. The streaming decoder
1126uses both kinds of buffer queues: an Android buffer queue for the ADTS
1127AAC data source, and an Android simple buffer queue for the PCM data
1128sink.  For more information about the Android simple buffer queue API, see <a href="#simple">Android
1129simple buffer queue data locator and interface</a>.
1130For more information about the Android buffer queue API, see the {@code index.html} file in
1131the {@code docs/Additional_library_docs/openmaxal/} directory under the installation root.
1132</p>
1133
1134<h3 id="meta">Determining the format of decoded PCM data via metadata</h3>
1135
1136<p>
1137The <code>SLMetadataExtractionItf</code> interface is part of the reference specification.
1138However, the metadata keys that indicate the actual format of decoded PCM data are specific to
1139Android. The <code>OpenSLES_AndroidMetadata.h</code> header file defines these metadata keys.
1140This header file resides under your installation root, in the
1141{@code platforms/android-&lt;version&gt;/&lt;abi&gt;/include/SLES} directory.
1142</p>
1143
1144<p>
1145The metadata key indices are available immediately after
1146the <code>Object::Realize()</code> method finishes executing. However, the associated values are not
1147available until after the app decodes the first encoded data.  A good
1148practice is to query for the key indices in the main thread after calling the {@code
1149Object::Realize} method, and to read the PCM format metadata values in the Android simple
1150buffer queue callback handler when calling it for the first time. Consult the
1151<a class="external-link" href="https://github.com/googlesamples/android-ndk">example code in the
1152NDK package</a> for examples of working with this interface.
1153</p>
1154
1155<p>
1156Metadata key names are stable, but the key indices are not documented,
1157and are subject to change.  An application should not assume that indices
1158are persistent across different execution runs, and should not assume that
1159multiple object instances share indices within the same run.
1160</p>
1161
1162<h3 id="fp">Floating-point data</h3>
1163
1164<p>
1165An app running on Android 5.0 (API level 21) and higher can supply data to an AudioPlayer in
1166single-precision, floating-point format.
1167</p>
1168<p>
1169In following example code, the {@code Engine::CreateAudioPlayer} method creates an audio player
1170that uses floating-point data:
1171</p>
1172
1173<pre>
1174#include &lt;SLES/OpenSLES_Android.h&gt;
1175...
1176SLAndroidDataFormat_PCM_EX pcm;
1177pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
1178pcm.numChannels = 2;
1179pcm.sampleRate = SL_SAMPLINGRATE_44_1;
1180pcm.bitsPerSample = 32;
1181pcm.containerSize = 32;
1182pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
1183pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
1184pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
1185...
1186SLDataSource audiosrc;
1187audiosrc.pLocator = ...
1188audiosrc.pFormat = &amp;pcm;
1189</pre>
1190
1191<h2 id="notes">Programming Notes</h2>
1192<p><a href="{@docRoot}ndk/guides/audio/opensl-prog-notes.html">OpenSL ES Programming Notes</a>
1193 provides supplemental information to ensure proper implementation of OpenSL ES.</p>
1194<p class="note"><strong>Note: </strong>
1195For your convenience, we have included a copy of the OpenSL ES 1.0.1 specification with the NDK in
1196{@code docs/opensles/OpenSL_ES_Specification_1.0.1.pdf}.
1197</p>
1198
1199<h2 id="platform-issues">Platform Issues</h2>
1200
1201<p>
1202This section describes known issues in the initial platform release that supports these APIs.
1203</p>
1204
1205<h3>Dynamic interface management</h3>
1206
1207<p>
1208{@code DynamicInterfaceManagement::AddInterface} does not work. Instead, specify the interface in
1209the array that is passed to Create, as shown in the example code for environmental reverb.
1210</p>
1211
1212