1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2"http://www.w3.org/TR/html4/loose.dtd">
3<html>
4
5<head>
6<title>OpenSL ES for Android</title>
7</head>
8
9<body>
10
11<h1>OpenSL ES for Android</h1>
12
13This article describes the Android native audio APIs based on the
14Khronos Group OpenSL ES&#8482; 1.0.1 standard.
15<p>
16Unless otherwise noted,
17all features are available at Android API level 9 (Android platform
18version 2.3) and higher.
19Some features are only available at Android API level 14 (Android
20platform version 4.0) and higher; these are noted.
21<p>
22OpenSL ES provides a C language interface that is also callable from C++, and
23exposes features similar to the audio portions of these Android APIs
24callable from Java programming language code:
25<ul>
26<li><a href="http://developer.android.com/reference/android/media/MediaPlayer.html">
27android.media.MediaPlayer</a>
28<li><a href="http://developer.android.com/reference/android/media/MediaRecorder.html">
29android.media.MediaRecorder</a>
30</ul>
31
32As with all of the Android Native Development Kit (NDK), the primary
33purpose of OpenSL ES for Android is to facilitate the implementation
34of shared libraries to be called from Java programming language code via Java Native
35Interface (JNI).  NDK is not intended for writing pure C/C++
36applications.  That said, OpenSL ES is a full-featured API, and we
37expect that you should be able to accomplish most of your audio
38needs using only this API, without up-calls to code running in the Dalvik VM.
39
40<p>
41Note: though based on OpenSL ES, the Android native audio API
42is <i>not</i> a conforming implementation of any OpenSL ES 1.0.1
43profile (game, music, or phone). This is because Android does not
44implement all of the features required by any one of the profiles.
45Any known cases where Android behaves differently than the specification
46are described in section "Android extensions" below.
47
48<h2>Getting started</h2>
49
50<h3>Example code</h3>
51
52<h4>Recommended</h4>
53
54Supported and tested example code, usable as a model
55for your own code, is located in NDK folder
56<code>platforms/android-9/samples/native-audio/</code>.
57
58<h4>Not recommended</h4>
59
60The OpenSL ES 1.0.1 specification contains example code in the
61appendices (see section "References" below for the link to this
62specification).  However, the examples in Appendix B: Sample Code
63and Appendix C: Use Case Sample Code use features
64not supported by Android. Some examples also contain
65typographical errors, or use APIs that are likely to change.
66Proceed with caution in referring to these;
67though the code may be helpful in understanding the full OpenSL ES
68standard, it should not be used as is with Android.
69
70<h3>Adding OpenSL ES to your application source code</h3>
71
72OpenSL ES is a C API, but is callable from both C and C++ code.
73<p>
74At a minimum, add the following line to your code:
75<pre>
76#include &lt;SLES/OpenSLES.h&gt;
77</pre>
78
79If you use Android extensions, also include this header:
80<pre>
81#include &lt;SLES/OpenSLES_Android.h&gt;
82</pre>
83which automatically includes these headers as well (you don't need to
84include these, they are shown as an aid in learning the API):
85<pre>
86#include &lt;SLES/OpenSLES_AndroidConfiguration.h&gt;
87#include &lt;SLES/OpenSLES_AndroidMetadata.h&gt;
88</pre>
89
90<h3>Makefile</h3>
91
92Modify your Android.mk as follows:
93<pre>
94LOCAL_LDLIBS += libOpenSLES
95</pre>
96
97<h3>Audio content</h3>
98
99There are many ways to package audio content for your
100application, including:
101
102<dl>
103
104<dt>Resources</dt>
105<dd>
106By placing your audio files into the <code>res/raw/</code> folder,
107they can be accessed easily by the associated APIs for
108<a href="http://developer.android.com/reference/android/content/res/Resources.html">
109Resources</a>.  However there is no direct native access to resources,
110so you will need to write Java programming language code to copy them out before use.
111</dd>
112
113<dt>Assets</dt>
114<dd>
115By placing your audio files into the <code>assets/</code> folder,
116they will be directly accessible by the Android native asset manager
117APIs.  See the header files <code>android/asset_manager.h</code>
118and <code>android/asset_manager_jni.h</code> for more information
119on these APIs.  The example code
120located in NDK folder
121<code>platforms/android-9/samples/native-audio/</code> uses these
122native asset manager APIs in conjunction with the Android file
123descriptor data locator.
124</dd>
125
126<dt>Network</dt>
127<dd>
128You can use the URI data locator to play audio content directly from the
129network. However, be sure to read section "Security and permissions" below.
130</dd>
131
132<dt>Local filesystem</dt>
133<dd>
134The URI data locator supports the <code>file:</code> scheme for local files,
135provided the files are accessible by the application.
136Note that the Android security framework restricts file access via
137the Linux user ID and group ID mechanism.
138</dd>
139
140<dt>Recorded</dt>
141<dd>Your application can record audio data from the microphone input,
142store this content, and then play it back later.
143The example code uses this method for the "Playback" clip.
144</dd>
145
146<dt>Compiled and linked inline</dt>
147<dd>
148You can link your audio content directly into the shared library,
149and then play it using an audio player with buffer queue data locator.  This is most
150suitable for short PCM format clips.  The example code uses this
151technique for the "Hello" and "Android" clips. The PCM data was
152converted to hex strings using a <code>bin2c</code> tool (not supplied).
153</dd>
154
155<dt>Real-time synthesis</dt>
156<dd>
157Your application can synthesize PCM data on the fly and then play it
158using an audio player with buffer queue data locator.  This is a
159relatively advanced technique, and the details of audio synthesis
160are beyond the scope of this article.
161</dd>
162
163</dl>
164
165Finding or creating useful audio content for your application is
166beyond the scope of this article, but see the "References" section
167below for some suggested web search terms.
168<p>
169Note that it is your responsibility to ensure that you are legally
170permitted to play or record content, and that there may be privacy
171considerations for recording content.
172
173<h3>Debugging</h3>
174
175For robustness, we recommend that you examine the <code>SLresult</code>
176value which is returned by most APIs. Use of <code>assert</code>
177vs. more advanced error handling logic is a matter of coding style
178and the particular API; see the Wikipedia article on
179<a href="http://en.wikipedia.org/wiki/Assertion_(computing)">assert</a>
180for more information. In the supplied example, we have used <code>assert</code>
181for "impossible" conditions which would indicate a coding error, and
182explicit error handling for others which are more likely to occur
183in production.
184<p>
185Many API errors result in a log entry, in addition to the non-zero
186result code. These log entries provide additional detail which can
187be especially useful for the more complex APIs such as
188<code>Engine::CreateAudioPlayer</code>.
189<p>
190Use <a href="http://developer.android.com/guide/developing/tools/adb.html">
191adb logcat</a>, the
192<a href="http://developer.android.com/guide/developing/eclipse-adt.html">
193Eclipse ADT plugin</a> LogCat pane, or
194<a href="http://developer.android.com/guide/developing/tools/ddms.html#logcat">
195ddms logcat</a> to see the log.
196
197<h2>Supported features from OpenSL ES 1.0.1</h2>
198
199This section summarizes available features. In some
200cases, there are limitations which are described in the next
201sub-section.
202
203<h3>Global entry points</h3>
204
205Supported global entry points:
206<ul>
207<li><code>slCreateEngine</code>
208<li><code>slQueryNumSupportedEngineInterfaces</code>
209<li><code>slQuerySupportedEngineInterfaces</code>
210</ul>
211
212<h3>Objects and interfaces</h3>
213
214The following figure indicates objects and interfaces supported by
215Android's OpenSL ES implementation.  A green cell means the feature
216is supported.
217
218<p>
219<img src="chart1.png" alt="Supported objects and interfaces">
220
221<h3>Limitations</h3>
222
223This section details limitations with respect to the supported
224objects and interfaces from the previous section.
225
226<h4>Buffer queue data locator</h4>
227
228An audio player or recorder with buffer queue data locator supports
229PCM data format only.
230
231<h4>Device data locator</h4>
232
233The only supported use of an I/O device data locator is when it is
234specified as the data source for <code>Engine::CreateAudioRecorder</code>.
235It should be initialized using these values, as shown in the example:
236<pre>
237SLDataLocator_IODevice loc_dev =
238  {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
239  SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
240</pre>
241
242<h4>Dynamic interface management</h4>
243
244<code>RemoveInterface</code> and <code>ResumeInterface</code> are not supported.
245
246<h4>Effect combinations</h4>
247
248It is meaningless to have both environmental reverb and preset
249reverb on the same output mix.
250<p>
251The platform may ignore effect requests if it estimates that the
252CPU load would be too high.
253
254<h4>Effect send</h4>
255
256<code>SetSendLevel</code> supports a single send level per audio player.
257
258<h4>Environmental reverb</h4>
259
260Environmental reverb does not support the <code>reflectionsDelay</code>,
261<code>reflectionsLevel</code>, or <code>reverbDelay</code> fields of
262<code>struct SLEnvironmentalReverbSettings</code>.
263
264<h4>MIME data format</h4>
265
266The MIME data format can be used with URI data locator only, and only
267for player (not recorder).
268<p>
269The Android implementation of OpenSL ES requires that <code>mimeType</code>
270be initialized to either <code>NULL</code> or a valid UTF-8 string,
271and that <code>containerType</code> be initialized to a valid value.
272In the absence of other considerations, such as portability to other
273implementations, or content format which cannot be identified by header,
274we recommend that you
275set the <code>mimeType</code> to <code>NULL</code> and <code>containerType</code>
276to <code>SL_CONTAINERTYPE_UNSPECIFIED</code>.
277<p>
278Supported formats include WAV PCM, WAV alaw, WAV ulaw, MP3, Ogg
279Vorbis, AAC LC, HE-AACv1 (aacPlus), HE-AACv2 (enhanced aacPlus),
280AMR, and FLAC [provided these are supported by the overall platform,
281and AAC formats must be located within an MP4 or ADTS container].
282MIDI is not supported.
283WMA is not part of the open source release, and compatibility
284with Android OpenSL ES has not been verified.
285<p>
286The Android implementation of OpenSL ES does not support direct
287playback of DRM or encrypted content; if you want to play this, you
288will need to convert to cleartext in your application before playing,
289and enforce any DRM restrictions in your application.
290
291<h4>Object</h4>
292
293<code>Resume</code>, <code>RegisterCallback</code>,
294<code>AbortAsyncOperation</code>, <code>SetPriority</code>,
295<code>GetPriority</code>, and <code>SetLossOfControlInterfaces</code>
296are not supported.
297
298<h4>PCM data format</h4>
299
300The PCM data format can be used with buffer queues only. Supported PCM
301playback configurations are 8-bit unsigned or 16-bit signed, mono
302or stereo, little endian byte ordering, and these sample rates:
3038000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, or 48000 Hz.
304For recording, the supported configurations are device-dependent,
305however generally 16000 Hz mono 16-bit signed is usually available.
306<p>
307Note that the field <code>samplesPerSec</code> is actually in
308units of milliHz, despite the misleading name. To avoid accidentally
309using the wrong value, you should initialize this field using one
310of the symbolic constants defined for this purpose (such as
311<code>SL_SAMPLINGRATE_44_1</code> etc.)
312
313<h4>Playback rate</h4>
314
315The supported playback rate range(s) and capabilities may vary depending
316on the platform version and implementation, and so should be determined
317at runtime by querying with <code>PlaybackRate::GetRateRange</code>
318or <code>PlaybackRate::GetCapabilitiesOfRate</code>.
319<p>
320That said, some guidance on typical rate ranges may be useful:
321In Android 2.3 a single playback rate range from 500 per mille to 2000 per mille
322inclusive is typically supported, with property
323<code>SL_RATEPROP_NOPITCHCORAUDIO</code>.
324In Android 4.0 the same rate range is typically supported for a data source
325in PCM format, and a unity rate range for other formats.
326
327<h4>Record</h4>
328
329The <code>SL_RECORDEVENT_HEADATLIMIT</code> and
330<code>SL_RECORDEVENT_HEADMOVING</code> events are not supported.
331
332<h4>Seek</h4>
333
334<code>SetLoop</code> enables whole file looping. The <code>startPos</code>
335parameter should be zero and the <code>endPos</code> parameter should
336be <code>SL_TIME_UNKNOWN</code>.
337
338<h4>URI data locator</h4>
339
340The URI data locator can be used with MIME data format only, and
341only for an audio player (not audio recorder). Supported schemes
342are <code>http:</code> and <code>file:</code>.
343A missing scheme defaults to the <code>file:</code> scheme. Other
344schemes such as <code>https:</code>, <code>ftp:</code>, and
345<code>content:</code> are not supported.
346<code>rtsp:</code> is not verified.
347
348<h3>Data structures</h3>
349
350Android supports these OpenSL ES 1.0.1 data structures:
351<ul>
352<li>SLDataFormat_MIME
353<li>SLDataFormat_PCM
354<li>SLDataLocator_BufferQueue
355<li>SLDataLocator_IODevice
356<li>SLDataLocator_OutputMix
357<li>SLDataLocator_URI
358<li>SLDataSink
359<li>SLDataSource
360<li>SLEngineOption
361<li>SLEnvironmentalReverbSettings
362<li>SLInterfaceID
363</ul>
364
365<h3>Platform configuration</h3>
366
367OpenSL ES for Android is designed for multi-threaded applications,
368and is thread-safe.
369<p>
370OpenSL ES for Android supports a single engine per application, and
371up to 32 objects. Available device memory and CPU may further
372restrict the usable number of objects.
373<p>
374<code>slCreateEngine</code> recognizes, but ignores, these engine options:
375<ul>
376<li><code>SL_ENGINEOPTION_THREADSAFE</code>
377<li><code>SL_ENGINEOPTION_LOSSOFCONTROL</code>
378</ul>
379
380OpenMAX AL and OpenSL ES may be used together in the same application.
381In this case, there is internally a single shared engine object,
382and the 32 object limit is shared between OpenMAX AL and OpenSL ES.
383The application should first create both engines, then use both engines,
384and finally destroy both engines.  The implementation maintains a
385reference count on the shared engine, so that it is correctly destroyed
386at the second destroy.
387
388<h2>Planning for future versions of OpenSL ES</h2>
389
390The Android native audio APIs are based on Khronos
391Group OpenSL ES 1.0.1 (see section "References" below).
392As of the time of this writing, Khronos has recently released
393a revised version 1.1 of the standard. The revised version
394includes new features, clarifications, correction of
395typographical errors, and some incompatibilities. Most of the expected
396incompatibilities are relatively minor, or are in areas of OpenSL ES
397not supported by Android. However, even a small change
398can be significant for an application developer, so it is important
399to prepare for this.
400<p>
401The Android team is committed to preserving future API binary
402compatibility for developers to the extent feasible. It is our
403intention to continue to support future binary compatibility of the
4041.0.1-based API, even as we add support for later versions of the
405standard. An application developed with this version should
406work on future versions of the Android platform, provided that
407you follow the guidelines listed in section "Planning for
408binary compatibility" below.
409<p>
410Note that future source compatibility will <i>not</i> be a goal. That is,
411if you upgrade to a newer version of the NDK, you may need to modify
412your application source code to conform to the new API. We expect
413that most such changes will be minor; see details below.
414
415<h3>Planning for binary compatibility</h3>
416
417We recommend that your application follow these guidelines,
418to improve future binary compatibility:
419<ul>
420<li>
421Use only the documented subset of Android-supported features from
422OpenSL ES 1.0.1.
423<li>
424Do not depend on a particular result code for an unsuccessful
425operation; be prepared to deal with a different result code.
426<li>
427Application callback handlers generally run in a restricted context,
428and should be written to perform their work quickly and then return
429as soon as possible. Do not do complex operations within a callback
430handler. For example, within a buffer queue completion callback,
431you can enqueue another buffer, but do not create an audio player.
432<li>
433Callback handlers should be prepared to be called more or less
434frequently, to receive additional event types, and should ignore
435event types that they do not recognize. Callbacks that are configured
436with an event mask of enabled event types should be prepared to be
437called with multiple event type bits set simultaneously.
438Use "&amp;" to test for each event bit rather than a switch case.
439<li>
440Use prefetch status and callbacks as a general indication of progress, but do
441not depend on specific hard-coded fill levels or callback sequence.
442The meaning of the prefetch status fill level, and the behavior for
443errors that are detected during prefetch, may change.
444<li>
445See section "Buffer queue behavior" below.
446</ul>
447
448<h3>Planning for source compatibility</h3>
449
450As mentioned, source code incompatibilities are expected in the next
451version of OpenSL ES from Khronos Group. Likely areas of change include:
452
453<ul>
454<li>The buffer queue interface is expected to have significant changes,
455especially in the areas of <code>BufferQueue::Enqueue</code>, the parameter
456list for <code>slBufferQueueCallback</code>,
457and the name of field <code>SLBufferQueueState.playIndex</code>.
458We recommend that your application code use Android simple buffer
459queues instead, because we do not plan to change that API.
460In the example code supplied with the NDK, we have used
461Android simple buffer queues for playback for this reason.
462(We also use Android simple buffer queue for recording and decode to PCM, but
463that is because standard OpenSL ES 1.0.1 does not support record or decode to
464a buffer queue data sink.)
465<li>Addition of <code>const</code> to input parameters passed by reference,
466and to <code>SLchar *</code> struct fields used as input values.
467This should not require any changes to your code.
468<li>Substitution of unsigned types for some parameters that are
469currently signed.  You may need to change a parameter type from
470<code>SLint32</code> to <code>SLuint32</code> or similar, or add a cast.
471<li><code>Equalizer::GetPresetName</code> will copy the string to
472application memory instead of returning a pointer to implementation
473memory. This will be a significant change, so we recommend that you
474either avoid calling this method, or isolate your use of it.
475<li>Additional fields in struct types. For output parameters, these
476new fields can be ignored, but for input parameters the new fields
477will need to be initialized. Fortunately, these are expected to all
478be in areas not supported by Android.
479<li>Interface
480<a href="http://en.wikipedia.org/wiki/Globally_unique_identifier">
481GUIDs</a> will change. Refer to interfaces by symbolic name rather than GUID
482to avoid a dependency.
483<li><code>SLchar</code> will change from <code>unsigned char</code>
484to <code>char</code>. This primarily affects the URI data locator
485and MIME data format.
486<li><code>SLDataFormat_MIME.mimeType</code> will be renamed to <code>pMimeType</code>,
487and <code>SLDataLocator_URI.URI</code> will be renamed to <code>pURI</code>.
488We recommend that you initialize the <code>SLDataFormat_MIME</code>
489and <code>SLDataLocator_URI</code>
490data structures using a brace-enclosed comma-separated list of values,
491rather than by field name, to isolate your code from this change.
492In the example code we have used this technique.
493<li><code>SL_DATAFORMAT_PCM</code> does not permit the application
494to specify the representation of the data as signed integer, unsigned
495integer, or floating-point. The Android implementation assumes that
4968-bit data is unsigned integer and 16-bit is signed integer.  In
497addition, the field <code>samplesPerSec</code> is a misnomer, as
498the actual units are milliHz. These issues are expected to be
499addressed in the next OpenSL ES version, which will introduce a new
500extended PCM data format that permits the application to explicitly
501specify the representation, and corrects the field name.  As this
502will be a new data format, and the current PCM data format will
503still be available (though deprecated), it should not require any
504immediate changes to your code.
505</ul>
506
507<h2>Android extensions</h2>
508
509The API for Android extensions is defined in <code>SLES/OpenSLES_Android.h</code>
510and the header files that it includes.
511Consult that file for details on these extensions. Unless otherwise
512noted, all interfaces are "explicit".
513<p>
514Note that use these extensions will limit your application's
515portability to other OpenSL ES implementations. If this is a concern,
516we advise that you avoid using them, or isolate your use of these
517with <code>#ifdef</code> etc.
518<p>
519The following figure shows which Android-specific interfaces and
520data locators are available for each object type.
521
522<p>
523<img src="chart2.png" alt="Android extensions">
524
525<h3>Android configuration interface</h3>
526
527The Android configuration interface provides a means to set
528platform-specific parameters for objects. Unlike other OpenSL ES
5291.0.1 interfaces, the Android configuration interface is available
530prior to object realization. This permits the object to be configured
531and then realized. Header file <code>SLES/OpenSLES_AndroidConfiguration.h</code>
532documents the available configuration keys and values:
533<ul>
534<li>stream type for audio players (default <code>SL_ANDROID_STREAM_MEDIA</code>)
535<li>record profile for audio recorders (default <code>SL_ANDROID_RECORDING_PRESET_GENERIC</code>)
536</ul>
537Here is an example code fragment that sets the Android audio stream type on an audio player:
538<pre>
539// CreateAudioPlayer and specify SL_IID_ANDROIDCONFIGURATION
540// in the required interface ID array. Do not realize player yet.
541// ...
542SLAndroidConfigurationItf playerConfig;
543result = (*playerObject)-&gt;GetInterface(playerObject,
544    SL_IID_ANDROIDCONFIGURATION, &amp;playerConfig);
545assert(SL_RESULT_SUCCESS == result);
546SLint32 streamType = SL_ANDROID_STREAM_ALARM;
547result = (*playerConfig)-&gt;SetConfiguration(playerConfig,
548    SL_ANDROID_KEY_STREAM_TYPE, &amp;streamType, sizeof(SLint32));
549assert(SL_RESULT_SUCCESS == result);
550// ...
551// Now realize the player here.
552</pre>
553Similar code can be used to configure the preset for an audio recorder.
554
555<h3>Android effects interfaces</h3>
556
557The Android effect, effect send, and effect capabilities interfaces provide
558a generic mechanism for an application to query and use device-specific
559audio effects. A device manufacturer should document any available
560device-specific audio effects.
561<p>
562Portable applications should use the OpenSL ES 1.0.1 APIs
563for audio effects instead of the Android effect extensions.
564
565<h3>Android file descriptor data locator</h3>
566
567The Android file descriptor data locator permits the source for an
568audio player to be specified as an open file descriptor with read
569access. The data format must be MIME.
570<p>
571This is especially useful in conjunction with the native asset manager.
572
573<h3>Android simple buffer queue data locator and interface</h3>
574
575The Android simple buffer queue data locator and interface are
576identical to the OpenSL ES 1.0.1 buffer queue locator and interface,
577except that Android simple buffer queues may be used with both audio
578players and audio recorders, and are limited to PCM data format.
579[OpenSL ES 1.0.1 buffer queues are for audio players only, and are not
580restricted to PCM data format.]
581<p>
582For recording, the application should enqueue empty buffers. Upon
583notification of completion via a registered callback, the filled
584buffer is available for the application to read.
585<p>
586For playback there is no difference. But for future source code
587compatibility, we suggest that applications use Android simple
588buffer queues instead of OpenSL ES 1.0.1 buffer queues.
589
590<h3>Dynamic interfaces at object creation</h3>
591
592For convenience, the Android implementation of OpenSL ES 1.0.1
593permits dynamic interfaces to be specified at object creation time,
594as an alternative to adding these interfaces after object creation
595with <code>DynamicInterfaceManagement::AddInterface</code>.
596
597<h3>Buffer queue behavior</h3>
598
599The OpenSL ES 1.0.1 specification requires that "On transition to
600the <code>SL_PLAYSTATE_STOPPED</code> state the play cursor is
601returned to the beginning of the currently playing buffer." The
602Android implementation does not necessarily conform to this
603requirement. For Android, it is unspecified whether a transition
604to <code>SL_PLAYSTATE_STOPPED</code> operates as described, or
605leaves the play cursor unchanged.
606<p>
607We recommend that you do not rely on either behavior; after a
608transition to <code>SL_PLAYSTATE_STOPPED</code>, you should explicitly
609call <code>BufferQueue::Clear</code>. This will place the buffer
610queue into a known state.
611<p>
612A corollary is that it is unspecified whether buffer queue callbacks
613are called upon transition to <code>SL_PLAYSTATE_STOPPED</code> or by
614<code>BufferQueue::Clear</code>.
615We recommend that you do not rely on either behavior; be prepared
616to receive a callback in these cases, but also do not depend on
617receiving one.
618<p>
619It is expected that a future version of OpenSL ES will clarify these
620issues. However, upgrading to that version would result in source
621code incompatibilities (see section "Planning for source compatibility"
622above).
623
624<h3>Reporting of extensions</h3>
625
626<code>Engine::QueryNumSupportedExtensions</code>,
627<code>Engine::QuerySupportedExtension</code>,
628<code>Engine::IsExtensionSupported</code> report these extensions:
629<ul>
630<li><code>ANDROID_SDK_LEVEL_#</code>
631where # is the platform API level, 9 or higher
632</ul>
633
634<h3>Decode audio to PCM</h3>
635
636Note: this feature is available at API level 14 and higher.
637<p>
638A standard audio player plays back to an audio device, and the data sink
639is specified as an output mix.
640However, as an Android extension, an audio player instead
641acts as a decoder if the data source is specified as a URI or Android
642file descriptor data locator with MIME data format, and the data sink is
643an Android simple buffer queue data locator with PCM data format.
644<p>
645This feature is primarily intended for games to pre-load their
646audio assets when changing to a new game level, similar to
647<code>android.media.SoundPool</code>.
648<p>
649The application should initially enqueue a set of empty buffers to the Android simple
650buffer queue, which will be filled with PCM data.  The Android simple
651buffer queue callback is invoked after each buffer is filled. The
652callback handler should process the PCM data, re-enqueue the
653now-empty buffer, and then return.  The application is responsible for
654keeping track of decoded buffers; the callback parameter list does not include
655sufficient information to indicate which buffer was filled or which buffer to enqueue next.
656<p>
657The end of stream is determined implicitly by the data source.
658At the end of stream a <code>SL_PLAYEVENT_HEADATEND</code> event is
659delivered. The Android simple buffer queue callback will no longer
660be called after all consumed data is decoded.
661<p>
662The sink's PCM data format typically matches that of the encoded data source
663with respect to sample rate, channel count, and bit depth. However, the platform
664implementation is permitted to decode to a different sample rate, channel count, or bit depth.
665There is a provision to detect the actual PCM format; see section "Determining
666the format of decoded PCM data via metadata" below.
667<p>
668Decode to PCM supports pause and initial seek.  Volume control, effects,
669looping, and playback rate are not supported.
670<p>
671Depending on the platform implementation, decoding may require resources
672that cannot be left idle.  Therefore it is not recommended to starve the
673decoder by failing to provide a sufficient number of empty PCM buffers,
674e.g. by returning from the Android simple buffer queue callback without
675enqueueing another empty buffer.  The result of decoder starvation is
676unspecified; the implementation may choose to either drop the decoded
677PCM data, pause the decoding process, or in severe cases terminate
678the decoder.
679
680<h3>Decode streaming ADTS AAC to PCM</h3>
681
682Note: this feature is available at API level 14 and higher.
683<p>
684An audio player acts as a streaming decoder if the data source is an
685Android buffer queue data locator with MIME data format, and the data
686sink is an Android simple buffer queue data locator with PCM data format.
687The MIME data format should be configured as:
688<dl>
689<dt>container</dt>
690<dd><code>SL_CONTAINERTYPE_RAW</code>
691<dt>MIME type string
692<dd><code>"audio/vnd.android.aac-adts"</code> (macro <code>SL_ANDROID_MIME_AACADTS</code>)
693</dl>
694<p>
695This feature is primarily intended for streaming media applications that
696deal with AAC audio, but need to apply custom processing of the audio
697prior to playback.  Most applications that need to decode audio to PCM
698should use the method of the previous section "Decode audio to PCM",
699as it is simpler and handles more audio formats.  The technique described
700here is a more specialized approach, to be used only if both of these
701conditions apply:
702<ul>
703<li>the compressed audio source is a stream of AAC frames contained by ADTS headers
704<li>the application manages this stream, that is the data is <i>not</i> located within
705a network resource identified by URI or within a local file identified by file descriptor.
706</ul>
707The application should initially enqueue a set of filled buffers to the Android buffer queue.
708Each buffer contains one or more complete ADTS AAC frames.
709The Android buffer queue callback is invoked after each buffer is emptied.
710The callback handler should re-fill and re-enqueue the buffer, and then return.
711The application need not keep track of encoded buffers; the callback parameter
712list does include sufficient information to indicate which buffer to enqueue next.
713The end of stream is explicitly marked by enqueuing an EOS item.
714After EOS, no more enqueues are permitted.
715<p>
716It is not recommended to starve the decoder by failing to provide full
717ADTS AAC buffers, e.g. by returning from the Android buffer queue callback
718without enqueueing another full buffer.  The result of decoder starvation
719is unspecified.
720<p>
721In all respects except for the data source, the streaming decode method is similar
722to that of the previous section:
723<ul>
724<li>initially enqueue a set of empty buffers to the Android simple buffer queue
725<li>the Android simple buffer queue callback is invoked after each buffer is filled with PCM data;
726the callback handler should process the PCM data and then re-enqueue another empty buffer
727<li>the <code>SL_PLAYEVENT_HEADATEND</code> event is delivered at end of stream
728<li>the actual PCM format should be detected using metadata rather than by making an assumption
729<li>the same limitations apply with respect to volume control, effects, etc.
730<li>starvation for lack of empty PCM buffers is not recommended
731</ul>
732<p>
733Despite the similarity in names, an Android buffer queue is <i>not</i>
734the same as an Android simple buffer queue.  The streaming decoder
735uses both kinds of buffer queues: an Android buffer queue for the ADTS
736AAC data source, and an Android simple buffer queue for the PCM data
737sink.  The Android simple buffer queue API is described in this document
738in section "Android simple buffer queue data locator and interface".
739The Android buffer queue API is described in the Android native media
740API documentation, located in <a href="../openmaxal/index.html">docs/openmaxal/index.html</a>.
741
742<h3>Determining the format of decoded PCM data via metadata</h3>
743
744The metadata extraction interface <code>SLMetadataExtractionItf</code>
745is a standard OpenSL ES 1.0.1 interface, not an Android extension.
746However, the particular metadata keys that
747indicate the actual format of decoded PCM data are specific to Android,
748and are defined in header <code>SLES/OpenSLES_AndroidMetadata.h</code>.
749<p>
750The metadata key indices are available immediately after
751<code>Object::Realize</code>. Yet the associated values are not
752available until after the first encoded data has been decoded.  A good
753practice is to query for the key indices in the main thread after Realize,
754and to read the PCM format metadata values in the Android simple
755buffer queue callback handler the first time it is called.
756<p>
757The OpenSL ES 1.0.1 metadata extraction interface
758<code>SLMetadataExtractionItf</code> is admittedly cumbersome, as it
759requires a multi-step process to first determine key indices and then
760to get the key values.  Consult the example code for snippets showing
761how to work with this interface.
762<p>
763Metadata key names are stable.  But the key indices are not documented
764and are subject to change.  An application should not assume that indices
765are persistent across different execution runs, and should not assume that
766indices are shared for different object instances within the same run.
767
768<h2>Programming notes</h2>
769
770These notes supplement the OpenSL ES 1.0.1 specification,
771available in the "References" section below.
772
773<h3>Objects and interface initialization</h3>
774
775Two aspects of the OpenSL ES programming model that may be unfamiliar
776to new developers are the distinction between objects and interfaces,
777and the initialization sequence.
778<p>
779Briefly, an OpenSL ES object is similar to the object concept
780in programming languages such as Java and C++, except an OpenSL ES
781object is <i>only</i> visible via its associated interfaces. This
782includes the initial interface for all objects, called
783<code>SLObjectItf</code>.  There is no handle for an object itself,
784only a handle to the <code>SLObjectItf</code> interface of the object.
785<p>
786An OpenSL ES object is first "created", which returns an
787<code>SLObjectItf</code>, then "realized". This is similar to the
788common programming pattern of first constructing an object (which
789should never fail other than for lack of memory or invalid parameters),
790and then completing initialization (which may fail due to lack of
791resources).  The realize step gives the implementation a
792logical place to allocate additional resources if needed.
793<p>
794As part of the API to create an object, an application specifies
795an array of desired interfaces that it plans to acquire later. Note
796that this array does <i>not</i> automatically acquire the interfaces;
797it merely indicates a future intention to acquire them.  Interfaces
798are distinguished as "implicit" or "explicit".  An explicit interface
799<i>must</i> be listed in the array if it will be acquired later.
800An implicit interface need not be listed in the object create array,
801but there is no harm in listing it there.  OpenSL ES has one more
802kind of interface called "dynamic", which does not need to be
803specified in the object create array, and can be added later after
804the object is created.  The Android implementation provides a
805convenience feature to avoid this complexity; see section "Dynamic
806interfaces at object creation" above.
807<p>
808After the object is created and realized, the application should
809acquire interfaces for each feature it needs, using
810<code>GetInterface</code> on the initial <code>SLObjectItf</code>.
811<p>
812Finally, the object is available for use via its interfaces, though
813note that some objects require further setup. In particular, an
814audio player with URI data source needs a bit more preparation in
815order to detect connection errors. See the next section
816"Audio player prefetch" for details.
817<p>
818After your application is done with the object, you should explicitly
819destroy it; see section "Destroy" below.
820
821<h3>Audio player prefetch</h3>
822
823For an audio player with URI data source, <code>Object::Realize</code> allocates resources
824but does not connect to the data source (i.e. "prepare") or begin
825pre-fetching data. These occur once the player state is set to
826either <code>SL_PLAYSTATE_PAUSED</code> or <code>SL_PLAYSTATE_PLAYING</code>.
827<p>
828Note that some information may still be unknown until relatively
829late in this sequence. In particular, initially
830<code>Player::GetDuration</code> will return <code>SL_TIME_UNKNOWN</code>
831and <code>MuteSolo::GetChannelCount</code> will either return successfully
832with channel count zero
833or the error result <code>SL_RESULT_PRECONDITIONS_VIOLATED</code>.
834These APIs will return the proper values once they are known.
835<p>
836Other properties that are initially unknown include the sample rate
837and actual media content type based on examining the content's header
838(as opposed to the application-specified MIME type and container type).
839These too, are determined later during prepare / prefetch, but there are
840no APIs to retrieve them.
841<p>
842The prefetch status interface is useful for detecting when all
843information is available. Or, your application can poll periodically.
844Note that some information may <i>never</i> be known, for example,
845the duration of a streaming MP3.
846<p>
847The prefetch status interface is also useful for detecting errors.
848Register a callback and enable at least the
849<code>SL_PREFETCHEVENT_FILLLEVELCHANGE</code> and
850<code>SL_PREFETCHEVENT_STATUSCHANGE</code> events. If both of these
851events are delivered simultaneously, and
852<code>PrefetchStatus::GetFillLevel</code> reports a zero level, and
853<code>PrefetchStatus::GetPrefetchStatus</code> reports
854<code>SL_PREFETCHSTATUS_UNDERFLOW</code>, then this indicates a
855non-recoverable error in the data source.
856This includes the inability to connect to the data source because
857the local filename does not exist or the network URI is invalid.
858<p>
859The next version of OpenSL ES is expected to add more explicit
860support for handling errors in the data source. However, for future
861binary compatibility, we intend to continue to support the current
862method for reporting a non-recoverable error.
863<p>
864In summary, a recommended code sequence is:
865<ul>
866<li>Engine::CreateAudioPlayer
867<li>Object:Realize
868<li>Object::GetInterface for SL_IID_PREFETCHSTATUS
869<li>PrefetchStatus::SetCallbackEventsMask
870<li>PrefetchStatus::SetFillUpdatePeriod
871<li>PrefetchStatus::RegisterCallback
872<li>Object::GetInterface for SL_IID_PLAY
873<li>Play::SetPlayState to SL_PLAYSTATE_PAUSED or SL_PLAYSTATE_PLAYING
874<li>preparation and prefetching occur here; during this time your
875callback will be called with periodic status updates
876</ul>
877
878<h3>Destroy</h3>
879
880Be sure to destroy all objects on exit from your application.  Objects
881should be destroyed in reverse order of their creation, as it is
882not safe to destroy an object that has any dependent objects.
883For example, destroy in this order: audio players and recorders,
884output mix, then finally the engine.
885<p>
886OpenSL ES does not support automatic garbage collection or
887<a href="http://en.wikipedia.org/wiki/Reference_counting">reference counting</a>
888of interfaces. After you call <code>Object::Destroy</code>, all extant
889interfaces derived from the associated object become <i>undefined</i>.
890<p>
891The Android OpenSL ES implementation does not detect the incorrect
892use of such interfaces.
893Continuing to use such interfaces after the object is destroyed will
894cause your application to crash or behave in unpredictable ways.
895<p>
896We recommend that you explicitly set both the primary object interface
897and all associated interfaces to NULL as part of your object
898destruction sequence, to prevent the accidental misuse of a stale
899interface handle.
900
901<h3>Stereo panning</h3>
902
903When <code>Volume::EnableStereoPosition</code> is used to enable
904stereo panning of a mono source, there is a 3 dB reduction in total
905<a href="http://en.wikipedia.org/wiki/Sound_power_level">
906sound power level</a>.  This is needed to permit the total sound
907power level to remain constant as the source is panned from one
908channel to the other. Therefore, don't enable stereo positioning
909if you don't need it.  See the Wikipedia article on
910<a href="http://en.wikipedia.org/wiki/Panning_(audio)">audio panning</a>
911for more information.
912
913<h3>Callbacks and threads</h3>
914
915Callback handlers are generally called <i>synchronously</i> with
916respect to the event, that is, at the moment and location where the
917event is detected by the implementation. But this point is
918<i>asynchronous</i> with respect to the application. Thus you should
919use a non-blocking synchronization mechanism to control access
920to any variables shared between the application and the callback
921handler. In the example code, such as for buffer queues, we have
922either omitted this synchronization or used blocking synchronization in
923the interest of simplicity. However, proper non-blocking synchronization
924would be critical for any production code.
925<p>
926Callback handlers are called from internal
927non-application thread(s) which are not attached to the Dalvik virtual machine and thus
928are ineligible to use JNI. Because these internal threads are
929critical to the integrity of the OpenSL ES implementation, a callback
930handler should also not block or perform excessive work.
931<p>
932If your callback handler needs to use JNI, or execute work that is
933not proportional to the callback, the handler should instead post an
934event for another thread to process.  Examples of acceptable callback
935workload include rendering and enqueuing the next output buffer (for an
936AudioPlayer), processing the just-filled input buffer and enqueueing the
937next empty buffer (for an AudioRecorder), or simple APIs such as most
938of the "Get" family.  See section "Performance" below regarding the workload.
939<p>
940Note that the converse is safe: a Dalvik application thread which has
941entered JNI is allowed to directly call OpenSL ES APIs, including
942those which block. However, blocking calls are not recommended from
943the main thread, as they may result in the dreaded "Application Not
944Responding" (ANR).
945<p>
946The choice of which thread calls a callback handler is largely left up
947to the implementation.  The reason for this flexibility is to permit
948future optimizations, especially on multi-core devices.
949<p>
950The thread on which the callback handler runs is not guaranteed to have
951the same identity across different calls.  Therefore do not rely on the
952<code>pthread_t</code> returned by <code>pthread_self()</code>, or the
953<code>pid_t</code> returned by <code>gettid()</code>, to be consistent
954across calls.  Don't use the thread local storage (TLS) APIs such as
955<code>pthread_setspecific()</code> and <code>pthread_getspecific()</code>
956from a callback, for the same reason.
957<p>
958The implementation guarantees that concurrent callbacks of the same kind,
959for the same object, will not occur.  However, concurrent callbacks of
960<i>different</i> kinds for the same object are possible, on different threads.
961
962<h3>Performance</h3>
963
964As OpenSL ES is a native C API, non-Dalvik application threads which
965call OpenSL ES have no Dalvik-related overhead such as garbage
966collection pauses. With one exception described below, there is no additional performance
967benefit to the use of OpenSL ES other than this. In particular, use
968of OpenSL ES does not guarantee a lower audio latency, higher scheduling
969priority, etc. than what the platform generally provides.
970On the other hand, as the Android platform and specific device
971implementations continue to evolve, an OpenSL ES application can
972expect to benefit from any future system performance improvements.
973<p>
974One such evolution is support for reduced audio output latency.
975The underpinnings for reduced output latency were first included in
976the Android 4.1 platform release ("Jellybean"), and then continued
977progress occurred in the Android 4.2 platform.  These improvements
978are available via OpenSL ES for device implementations that claim feature
979"android.hardware.audio.low_latency". If the device doesn't claim this
980feature but supports API level 9 (Android platform version 2.3) or later,
981then you can still use the OpenSL ES APIs but the output latency may be higher.
982The lower output latency path is used only if the application requests a
983buffer count of 2 or more, and a buffer size and sample rate that are
984compatible with the device's native output configuration.
985These parameters are device-specific and should be obtained as follows.
986<p>
987Beginning with API level 17 (Android platform version 4.2), an application
988can query for the native or optimal output sample rate and buffer size
989for the device's primary output stream.  When combined with the feature
990test just mentioned, an app can now configure itself appropriately for
991lower latency output on devices that claim support.
992<p>
993The recommended sequence is:
994<ol>
995<li>Check for API level 9 or higher, to confirm use of OpenSL ES.
996<li>Check for feature "android.hardware.audio.low_latency" using code such as this:
997<pre>
998import android.content.pm.PackageManager;
999...
1000PackageManager pm = getContext().getPackageManager();
1001boolean claimsFeature = pm.hasSystemFeature(PackageManager.FEATURE_AUDIO_LOW_LATENCY);
1002</pre>
1003<li>Check for API level 17 or higher, to confirm use of
1004<code>android.media.AudioManager.getProperty()</code>.
1005<li>Get the native or optimal output sample rate and buffer size for this device's primary output
1006stream, using code such as this:
1007<pre>
1008import android.media.AudioManager;
1009...
1010AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
1011String sampleRate = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE));
1012String framesPerBuffer = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
1013</pre>
1014Note that <code>sampleRate</code> and <code>framesPerBuffer</code>
1015are <code>String</code>s.  First check for <code>null</code>
1016and then convert to <code>int</code> using <code>Integer.parseInt()</code>.
1017<li>Now use OpenSL ES to create an AudioPlayer with PCM buffer queue data locator.
1018</ol>
1019The number of lower latency audio players is limited. If your application
1020requires more than a few audio sources, consider mixing your audio at
1021application level.  Be sure to destroy your audio players when your
1022activity is paused, as they are a global resource shared with other apps.
1023<p>
1024To avoid audible glitches, the buffer queue callback handler must execute
1025within a small and predictable time window. This typically implies no
1026unbounded blocking on mutexes, conditions, or I/O operations. Instead consider
1027"try locks", locks and waits with timeouts, and non-blocking algorithms.
1028<p>
1029The computation required to render the next buffer (for AudioPlayer) or
1030consume the previous buffer (for AudioRecord) should take approximately
1031the same amount of time for each callback.  Avoid algorithms that execute in
1032a non-deterministic amount of time, or are "bursty" in their computations.
1033A callback computation is bursty if the CPU time spent in any
1034given callback is significantly larger than the average.
1035In summary, the ideal is for the CPU execution time of the handler to have
1036variance near zero, and for the handler to not block for unbounded times.
1037<p>
1038Lower latency audio is for these outputs only: on-device speaker, wired
1039headphones, wired headset, and line out.
1040
1041<h3>Security and permissions</h3>
1042
1043As far as who can do what, security in Android is done at the
1044process level. Java programming language code can't do anything more than native code, nor
1045can native code do anything more than Java programming language code. The only differences
1046between them are what APIs are available that provide functionality
1047that the platform promises to support in the future and across
1048different devices.
1049<p>
1050Applications using OpenSL ES must request whatever permissions they
1051would need for similar non-native APIs. For example, if your application
1052records audio, then it needs the <code>android.permission.RECORD_AUDIO</code>
1053permission. Applications that use audio effects need
1054<code>android.permission.MODIFY_AUDIO_SETTINGS</code>. Applications that play
1055network URI resources need <code>android.permission.NETWORK</code>.
1056<p>
1057Depending on the platform version and implementation,
1058media content parsers and software codecs may run within the context
1059of the Android application that calls OpenSL ES (hardware codecs
1060are abstracted, but are device-dependent). Malformed content
1061designed to exploit parser and codec vulnerabilities is a known attack
1062vector. We recommend that you play media only from trustworthy
1063sources, or that you partition your application such that code that
1064handles media from untrustworthy sources runs in a relatively
1065sandboxed environment.  For example you could process media from
1066untrustworthy sources in a separate process. Though both processes
1067would still run under the same UID, this separation does make an
1068attack more difficult.
1069
1070<h2>Platform issues</h2>
1071
1072This section describes known issues in the initial platform
1073release which supports these APIs.
1074
1075<h3>Dynamic interface management</h3>
1076
1077<code>DynamicInterfaceManagement::AddInterface</code> does not work.
1078Instead, specify the interface in the array passed to Create, as
1079shown in the example code for environmental reverb.
1080
1081<h2>References and resources</h2>
1082
1083Android:
1084<ul>
1085<li><a href="http://developer.android.com/resources/index.html">
1086Android developer resources</a>
1087<li><a href="http://groups.google.com/group/android-developers">
1088Android developers discussion group</a>
1089<li><a href="http://developer.android.com/sdk/ndk/index.html">Android NDK</a>
1090<li><a href="http://groups.google.com/group/android-ndk">
1091Android NDK discussion group</a> (for developers of native code, including OpenSL ES)
1092<li><a href="http://code.google.com/p/android/issues/">
1093Android open source bug database</a>
1094</ul>
1095
1096Khronos Group:
1097<ul>
1098<li><a href="http://www.khronos.org/opensles/">
1099Khronos Group OpenSL ES Overview</a>
1100<li><a href="http://www.khronos.org/registry/sles/">
1101Khronos Group OpenSL ES 1.0.1 specification</a>
1102<li><a href="http://www.khronos.org/message_boards/viewforum.php?f=15">
1103Khronos Group public message board for OpenSL ES</a>
1104(please limit to non-Android questions)
1105</ul>
1106For convenience, we have included a copy of the OpenSL ES 1.0.1
1107specification with the NDK in
1108<code>docs/opensles/OpenSL_ES_Specification_1.0.1.pdf</code>.
1109
1110<p>
1111Miscellaneous:
1112<ul>
1113<li><a href="http://en.wikipedia.org/wiki/Java_Native_Interface">JNI</a>
1114<li><a href="http://stackoverflow.com/search?q=android+audio">
1115Stack Overflow</a>
1116<li>web search for "interactive audio", "game audio", "sound design",
1117"audio programming", "audio content", "audio formats", etc.
1118<li><a href="http://en.wikipedia.org/wiki/Advanced_Audio_Coding">AAC</a>
1119</ul>
1120
1121</body>
1122</html>
1123