xref: /aosp_15_r20/external/oboe/docs/OpenSLESMigration.md (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1OpenSLES Migration Guide
2===
3
4# Introduction
5
6This guide will show you how to migrate your code from [OpenSL ES for Android](https://developer.android.com/ndk/guides/audio/opensl/opensl-for-android) (just OpenSL from now on) to Oboe.
7
8To familiarise yourself with Oboe, please read the [Getting Started guide](https://github.com/google/oboe/blob/main/docs/GettingStarted.md) and ensure that Oboe has been added as a dependency in your project.
9
10
11# Concepts
12
13At a high level, OpenSL and Oboe have some similarities. They both create objects which communicate with an audio device capable of playing or recording audio samples. They also use a callback mechanism to read data from or write data to that audio device.
14
15This is where the similarities end.
16
17Oboe has been designed to be a simpler, easier to use API than OpenSL. It aims to reduce the amount of boilerplate code and guesswork associated with recording and playing audio.
18
19
20# Key differences
21
22
23## Object mappings
24
25OpenSL uses an audio engine object, created using `slCreateEngine`, to create other objects. Oboe's equivalent object is `AudioStreamBuilder`, although it will only create an `AudioStream`.
26
27OpenSL uses audio player and audio recorder objects to communicate with audio devices. In Oboe an `AudioStream` is used.
28
29In OpenSL the audio callback mechanism is a user-defined function which is called each time a buffer is enqueued. In Oboe you construct an `AudioStreamDataCallback` object, and its `onAudioReady` method is called each time audio data is ready to be read or written.
30
31Here's a table which summarizes the object mappings:
32
33
34<table>
35  <tr>
36   <td><strong>OpenSL</strong>
37   </td>
38   <td><strong>Oboe </strong>(all classes are in the <code>oboe</code> namespace)
39   </td>
40  </tr>
41  <tr>
42   <td>Audio engine (an <code>SLObjectItf</code>)
43   </td>
44   <td><code>AudioStreamBuilder</code>
45   </td>
46  </tr>
47  <tr>
48   <td>Audio player
49   </td>
50   <td><code>AudioStream</code> configured for output
51   </td>
52  </tr>
53  <tr>
54   <td>Audio recorder
55   </td>
56   <td><code>AudioStream</code> configured for input
57   </td>
58  </tr>
59  <tr>
60   <td>Callback function
61   </td>
62   <td><code>AudioStreamDataCallback::onAudioReady</code>
63   </td>
64  </tr>
65</table>
66
67
68
69## Buffers and callbacks
70
71In OpenSL your app must create and manage a queue of buffers. Each time a buffer is dequeued, the callback function is called and your app must enqueue a new buffer.
72
73In Oboe, rather than owning and enqueuing buffers, you are given direct access to the `AudioStream`'s buffer through the `audioData` parameter of `onAudioReady`.
74
75This is a container array which you can read audio data from when recording, or write data into when playing. The `numFrames` parameter tells you how many frames to read/write. Here's the method signature of `onAudioReady`:
76
77
78```
79DataCallbackResult onAudioReady(
80    AudioStream *oboeStream,
81    void *audioData,
82    int32_t numFrames
83)
84```
85
86
87You supply your implementation of `onAudioReady` when building the audio stream by constructing an `AudioStreamDataCallback` object. [Here's an example.](https://github.com/google/oboe/blob/main/docs/GettingStarted.md#creating-an-audio-stream)
88
89
90### Buffer sizes
91
92In OpenSL you cannot specify the size of the internal buffers of the audio player/recorder because your app is supplying them so they can have arbitrary size. You can only specify the _number of buffers_ through the `SLDataLocator_AndroidSimpleBufferQueue.numBuffers` field.
93
94By contrast, Oboe will use the information it has about the current audio device to configure its buffer size. It will determine the optimal number of audio frames which should be read/written in a single callback. This is known as a _burst_, and usually represents the minimum possible buffer size. Typical values are 96, 128, 192 and 240 frames.
95
96An audio stream's burst size, given by `AudioStream::getFramesPerBurst()`, is important because it is used when configuring the buffer size. Here's an example which uses two bursts for the buffer size, which usually represents a good tradeoff between latency and glitch protection:
97
98
99```
100audioStream.setBufferSizeInFrames(audioStream.getFramesPerBurst() * 2);
101```
102
103
104**Note:** because Oboe uses OpenSL under-the-hood on older devices which does not provide the same information about audio devices, it still needs to know [sensible default values for the burst to be used with OpenSL](https://github.com/google/oboe/blob/main/docs/GettingStarted.md#obtaining-optimal-latency).
105
106
107## Audio stream properties
108
109In OpenSL you must explicitly specify various properties, including the sample rate and audio format, when opening an audio player or audio recorder.
110
111In Oboe, you do not need to specify any properties to open a stream. For example, this will open a valid output `AudioStream` with sensible default values.
112
113
114```
115AudioStreamBuilder builder;
116builder.openStream(myStream);
117```
118
119
120However, you may want to specify some properties. These are set using the `AudioStreamBuilder` ([example](https://github.com/google/oboe/blob/main/docs/FullGuide.md#set-the-audio-stream-configuration-using-an-audiostreambuilder)).
121
122
123## Stream disconnection
124
125OpenSL has no mechanism, other than stopping callbacks, to indicate that an audio device has been disconnected - for example, when headphones are unplugged.
126
127In Oboe, you can be notified of stream disconnection by overriding one of the `onError` methods in `AudioStreamErrorCallback`. This allows you to clean up any resources associated with the audio stream and create a new stream with optimal properties for the current audio device ([more info](https://github.com/google/oboe/blob/main/docs/FullGuide.md#disconnected-audio-stream)).
128
129
130# Unsupported features
131
132
133## Formats
134
135Oboe audio streams only accept [PCM](https://en.wikipedia.org/wiki/Pulse-code_modulation) data in float or signed 16-bit ints. Additional formats including 8-bit unsigned, 24-bit packed, 8.24 and 32-bit are not supported.
136
137Compressed audio, such as MP3, is not supported for a number of reasons but chiefly:
138
139
140
141*   The OpenSL ES implementation has performance and reliability issues.
142*   It keeps the Oboe API and the underlying implementation simple.
143
144Extraction and decoding can be done either through the NDK [Media APIs](https://developer.android.com/ndk/reference/group/media) or by using a third party library like [FFmpeg](https://ffmpeg.org/). An example of both these approaches can be seen in the [RhythmGame sample](https://github.com/google/oboe/tree/main/samples/RhythmGame).
145
146
147## Miscellaneous features
148
149Oboe does **not** support the following features:
150
151
152
153*   Channel masks - only [indexed channel masks](https://developer.android.com/reference/kotlin/android/media/AudioFormat#channel-index-masks) are supported.
154*   Playing audio content from a file pathname or [URI](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier).
155*   Notification callbacks for position updates.
156*   Platform output effects on API 27 and below. [They are supported from API 28 and above.](https://github.com/google/oboe/wiki/TechNote_Effects)
157
158
159# Summary
160
161
162
163*   Replace your audio player or recorder with an `AudioStream` created using an `AudioStreamBuilder`.
164*   Use your value for `numBuffers` to set the audio stream's buffer size as a multiple of the burst size. For example: `audioStream.setBufferSizeInFrames(audioStream.getFramesPerBurst * numBuffers)`.
165*   Create an `AudioStreamDataCallback` object and move your OpenSL callback code inside the `onAudioReady` method.
166*   Handle stream disconnect events by creating an `AudioStreamErrorCallback` object and overriding one of its `onError` methods.
167*   Pass sensible default sample rate and buffer size values to Oboe from `AudioManager` [using this method](https://github.com/google/oboe/blob/main/docs/GettingStarted.md#obtaining-optimal-latency) so that your app is still performant on older devices.
168
169For more information please read the [Full Guide to Oboe](https://github.com/google/oboe/blob/main/docs/FullGuide.md).
170