1 /*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cassert>
18
19 #include <SLES/OpenSLES.h>
20 #include <SLES/OpenSLES_Android.h>
21 #include <common/AudioClock.h>
22
23 #include "oboe/AudioStreamBuilder.h"
24 #include "AudioOutputStreamOpenSLES.h"
25 #include "AudioStreamOpenSLES.h"
26 #include "OpenSLESUtilities.h"
27 #include "OutputMixerOpenSLES.h"
28
29 using namespace oboe;
30
OpenSLES_convertOutputUsage(Usage oboeUsage)31 static SLuint32 OpenSLES_convertOutputUsage(Usage oboeUsage) {
32 SLuint32 openslStream = SL_ANDROID_STREAM_MEDIA;
33 switch(oboeUsage) {
34 case Usage::Media:
35 openslStream = SL_ANDROID_STREAM_MEDIA;
36 break;
37 case Usage::VoiceCommunication:
38 case Usage::VoiceCommunicationSignalling:
39 openslStream = SL_ANDROID_STREAM_VOICE;
40 break;
41 case Usage::Alarm:
42 openslStream = SL_ANDROID_STREAM_ALARM;
43 break;
44 case Usage::Notification:
45 case Usage::NotificationRingtone:
46 case Usage::NotificationEvent:
47 openslStream = SL_ANDROID_STREAM_NOTIFICATION;
48 break;
49 case Usage::AssistanceAccessibility:
50 case Usage::AssistanceNavigationGuidance:
51 case Usage::AssistanceSonification:
52 openslStream = SL_ANDROID_STREAM_SYSTEM;
53 break;
54 case Usage::Game:
55 openslStream = SL_ANDROID_STREAM_MEDIA;
56 break;
57 case Usage::Assistant:
58 default:
59 openslStream = SL_ANDROID_STREAM_SYSTEM;
60 break;
61 }
62 return openslStream;
63 }
64
AudioOutputStreamOpenSLES(const AudioStreamBuilder & builder)65 AudioOutputStreamOpenSLES::AudioOutputStreamOpenSLES(const AudioStreamBuilder &builder)
66 : AudioStreamOpenSLES(builder) {
67 }
68
69 // These will wind up in <SLES/OpenSLES_Android.h>
70 constexpr int SL_ANDROID_SPEAKER_STEREO = (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT);
71
72 constexpr int SL_ANDROID_SPEAKER_QUAD = (SL_ANDROID_SPEAKER_STEREO
73 | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT);
74
75 constexpr int SL_ANDROID_SPEAKER_5DOT1 = (SL_ANDROID_SPEAKER_QUAD
76 | SL_SPEAKER_FRONT_CENTER | SL_SPEAKER_LOW_FREQUENCY);
77
78 constexpr int SL_ANDROID_SPEAKER_7DOT1 = (SL_ANDROID_SPEAKER_5DOT1 | SL_SPEAKER_SIDE_LEFT
79 | SL_SPEAKER_SIDE_RIGHT);
80
channelCountToChannelMask(int channelCount) const81 SLuint32 AudioOutputStreamOpenSLES::channelCountToChannelMask(int channelCount) const {
82 SLuint32 channelMask = 0;
83
84 switch (channelCount) {
85 case 1:
86 channelMask = SL_SPEAKER_FRONT_CENTER;
87 break;
88
89 case 2:
90 channelMask = SL_ANDROID_SPEAKER_STEREO;
91 break;
92
93 case 4: // Quad
94 channelMask = SL_ANDROID_SPEAKER_QUAD;
95 break;
96
97 case 6: // 5.1
98 channelMask = SL_ANDROID_SPEAKER_5DOT1;
99 break;
100
101 case 8: // 7.1
102 channelMask = SL_ANDROID_SPEAKER_7DOT1;
103 break;
104
105 default:
106 channelMask = channelCountToChannelMaskDefault(channelCount);
107 break;
108 }
109 return channelMask;
110 }
111
open()112 Result AudioOutputStreamOpenSLES::open() {
113 logUnsupportedAttributes();
114
115 SLAndroidConfigurationItf configItf = nullptr;
116
117
118 if (getSdkVersion() < __ANDROID_API_L__ && mFormat == AudioFormat::Float){
119 // TODO: Allow floating point format on API <21 using float->int16 converter
120 return Result::ErrorInvalidFormat;
121 }
122
123 // If audio format is unspecified then choose a suitable default.
124 // API 21+: FLOAT
125 // API <21: INT16
126 if (mFormat == AudioFormat::Unspecified){
127 mFormat = (getSdkVersion() < __ANDROID_API_L__) ?
128 AudioFormat::I16 : AudioFormat::Float;
129 }
130
131 Result oboeResult = AudioStreamOpenSLES::open();
132 if (Result::OK != oboeResult) return oboeResult;
133
134 SLresult result = OutputMixerOpenSL::getInstance().open();
135 if (SL_RESULT_SUCCESS != result) {
136 AudioStreamOpenSLES::close();
137 return Result::ErrorInternal;
138 }
139
140 SLuint32 bitsPerSample = static_cast<SLuint32>(getBytesPerSample() * kBitsPerByte);
141
142 // configure audio source
143 SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
144 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType
145 static_cast<SLuint32>(kBufferQueueLength)}; // numBuffers
146
147 // Define the audio data format.
148 SLDataFormat_PCM format_pcm = {
149 SL_DATAFORMAT_PCM, // formatType
150 static_cast<SLuint32>(mChannelCount), // numChannels
151 static_cast<SLuint32>(mSampleRate * kMillisPerSecond), // milliSamplesPerSec
152 bitsPerSample, // bitsPerSample
153 bitsPerSample, // containerSize;
154 channelCountToChannelMask(mChannelCount), // channelMask
155 getDefaultByteOrder(),
156 };
157
158 SLDataSource audioSrc = {&loc_bufq, &format_pcm};
159
160 /**
161 * API 21 (Lollipop) introduced support for floating-point data representation and an extended
162 * data format type: SLAndroidDataFormat_PCM_EX. If running on API 21+ use this newer format
163 * type, creating it from our original format.
164 */
165 SLAndroidDataFormat_PCM_EX format_pcm_ex;
166 if (getSdkVersion() >= __ANDROID_API_L__) {
167 SLuint32 representation = OpenSLES_ConvertFormatToRepresentation(getFormat());
168 // Fill in the format structure.
169 format_pcm_ex = OpenSLES_createExtendedFormat(format_pcm, representation);
170 // Use in place of the previous format.
171 audioSrc.pFormat = &format_pcm_ex;
172 }
173
174 result = OutputMixerOpenSL::getInstance().createAudioPlayer(&mObjectInterface,
175 &audioSrc);
176 if (SL_RESULT_SUCCESS != result) {
177 LOGE("createAudioPlayer() result:%s", getSLErrStr(result));
178 goto error;
179 }
180
181 // Configure the stream.
182 result = (*mObjectInterface)->GetInterface(mObjectInterface,
183 SL_IID_ANDROIDCONFIGURATION,
184 (void *)&configItf);
185 if (SL_RESULT_SUCCESS != result) {
186 LOGW("%s() GetInterface(SL_IID_ANDROIDCONFIGURATION) failed with %s",
187 __func__, getSLErrStr(result));
188 } else {
189 result = configurePerformanceMode(configItf);
190 if (SL_RESULT_SUCCESS != result) {
191 goto error;
192 }
193
194 SLuint32 presetValue = OpenSLES_convertOutputUsage(getUsage());
195 result = (*configItf)->SetConfiguration(configItf,
196 SL_ANDROID_KEY_STREAM_TYPE,
197 &presetValue,
198 sizeof(presetValue));
199 if (SL_RESULT_SUCCESS != result) {
200 goto error;
201 }
202 }
203
204 result = (*mObjectInterface)->Realize(mObjectInterface, SL_BOOLEAN_FALSE);
205 if (SL_RESULT_SUCCESS != result) {
206 LOGE("Realize player object result:%s", getSLErrStr(result));
207 goto error;
208 }
209
210 result = (*mObjectInterface)->GetInterface(mObjectInterface, SL_IID_PLAY, &mPlayInterface);
211 if (SL_RESULT_SUCCESS != result) {
212 LOGE("GetInterface PLAY result:%s", getSLErrStr(result));
213 goto error;
214 }
215
216 result = AudioStreamOpenSLES::registerBufferQueueCallback();
217 if (SL_RESULT_SUCCESS != result) {
218 goto error;
219 }
220
221 result = updateStreamParameters(configItf);
222 if (SL_RESULT_SUCCESS != result) {
223 goto error;
224 }
225
226 oboeResult = configureBufferSizes(mSampleRate);
227 if (Result::OK != oboeResult) {
228 goto error;
229 }
230
231 allocateFifo();
232
233 setState(StreamState::Open);
234 return Result::OK;
235
236 error:
237 return Result::ErrorInternal; // TODO convert error from SLES to OBOE
238 }
239
onAfterDestroy()240 Result AudioOutputStreamOpenSLES::onAfterDestroy() {
241 OutputMixerOpenSL::getInstance().close();
242 return Result::OK;
243 }
244
close()245 Result AudioOutputStreamOpenSLES::close() {
246 LOGD("AudioOutputStreamOpenSLES::%s()", __func__);
247 std::lock_guard<std::mutex> lock(mLock);
248 Result result = Result::OK;
249 if (getState() == StreamState::Closed){
250 result = Result::ErrorClosed;
251 } else {
252 requestPause_l();
253 // invalidate any interfaces
254 mPlayInterface = nullptr;
255 result = AudioStreamOpenSLES::close_l();
256 }
257 return result;
258 }
259
setPlayState_l(SLuint32 newState)260 Result AudioOutputStreamOpenSLES::setPlayState_l(SLuint32 newState) {
261
262 LOGD("AudioOutputStreamOpenSLES(): %s() called", __func__);
263 Result result = Result::OK;
264
265 if (mPlayInterface == nullptr){
266 LOGE("AudioOutputStreamOpenSLES::%s() mPlayInterface is null", __func__);
267 return Result::ErrorInvalidState;
268 }
269
270 SLresult slResult = (*mPlayInterface)->SetPlayState(mPlayInterface, newState);
271 if (SL_RESULT_SUCCESS != slResult) {
272 LOGW("AudioOutputStreamOpenSLES(): %s() returned %s", __func__, getSLErrStr(slResult));
273 result = Result::ErrorInternal; // TODO convert slResult to Result::Error
274 }
275 return result;
276 }
277
requestStart()278 Result AudioOutputStreamOpenSLES::requestStart() {
279 LOGD("AudioOutputStreamOpenSLES(): %s() called", __func__);
280
281 mLock.lock();
282 StreamState initialState = getState();
283 switch (initialState) {
284 case StreamState::Starting:
285 case StreamState::Started:
286 mLock.unlock();
287 return Result::OK;
288 case StreamState::Closed:
289 mLock.unlock();
290 return Result::ErrorClosed;
291 default:
292 break;
293 }
294
295 // We use a callback if the user requests one
296 // OR if we have an internal callback to read the blocking IO buffer.
297 setDataCallbackEnabled(true);
298
299 setState(StreamState::Starting);
300 Result result = setPlayState_l(SL_PLAYSTATE_PLAYING);
301 if (result == Result::OK) {
302 setState(StreamState::Started);
303 mLock.unlock();
304 if (getBufferDepth(mSimpleBufferQueueInterface) == 0) {
305 // Enqueue the first buffer if needed to start the streaming.
306 // This might call requestStop() so try to avoid a recursive lock.
307 processBufferCallback(mSimpleBufferQueueInterface);
308 }
309 } else {
310 setState(initialState);
311 mLock.unlock();
312 }
313 return result;
314 }
315
requestPause()316 Result AudioOutputStreamOpenSLES::requestPause() {
317 LOGD("AudioOutputStreamOpenSLES(): %s() called", __func__);
318 std::lock_guard<std::mutex> lock(mLock);
319 return requestPause_l();
320 }
321
322 // Call under mLock
requestPause_l()323 Result AudioOutputStreamOpenSLES::requestPause_l() {
324 StreamState initialState = getState();
325 switch (initialState) {
326 case StreamState::Pausing:
327 case StreamState::Paused:
328 return Result::OK;
329 case StreamState::Closed:
330 return Result::ErrorClosed;
331 default:
332 break;
333 }
334
335 setState(StreamState::Pausing);
336 Result result = setPlayState_l(SL_PLAYSTATE_PAUSED);
337 if (result == Result::OK) {
338 // Note that OpenSL ES does NOT reset its millisecond position when OUTPUT is paused.
339 int64_t framesWritten = getFramesWritten();
340 if (framesWritten >= 0) {
341 setFramesRead(framesWritten);
342 }
343 setState(StreamState::Paused);
344 } else {
345 setState(initialState);
346 }
347 return result;
348 }
349
350 /**
351 * Flush/clear the queue buffers
352 */
requestFlush()353 Result AudioOutputStreamOpenSLES::requestFlush() {
354 std::lock_guard<std::mutex> lock(mLock);
355 return requestFlush_l();
356 }
357
requestFlush_l()358 Result AudioOutputStreamOpenSLES::requestFlush_l() {
359 LOGD("AudioOutputStreamOpenSLES(): %s() called", __func__);
360 if (getState() == StreamState::Closed) {
361 return Result::ErrorClosed;
362 }
363
364 Result result = Result::OK;
365 if (mPlayInterface == nullptr || mSimpleBufferQueueInterface == nullptr) {
366 result = Result::ErrorInvalidState;
367 } else {
368 SLresult slResult = (*mSimpleBufferQueueInterface)->Clear(mSimpleBufferQueueInterface);
369 if (slResult != SL_RESULT_SUCCESS){
370 LOGW("Failed to clear buffer queue. OpenSLES error: %d", result);
371 result = Result::ErrorInternal;
372 }
373 }
374 return result;
375 }
376
requestStop()377 Result AudioOutputStreamOpenSLES::requestStop() {
378 LOGD("AudioOutputStreamOpenSLES(): %s() called", __func__);
379 std::lock_guard<std::mutex> lock(mLock);
380
381 StreamState initialState = getState();
382 switch (initialState) {
383 case StreamState::Stopping:
384 case StreamState::Stopped:
385 return Result::OK;
386 case StreamState::Closed:
387 return Result::ErrorClosed;
388 default:
389 break;
390 }
391
392 setState(StreamState::Stopping);
393
394 Result result = setPlayState_l(SL_PLAYSTATE_STOPPED);
395 if (result == Result::OK) {
396
397 // Also clear the buffer queue so the old data won't be played if the stream is restarted.
398 // Call the _l function that expects to already be under a lock.
399 if (requestFlush_l() != Result::OK) {
400 LOGW("Failed to flush the stream. Error %s", convertToText(flush()));
401 }
402
403 mPositionMillis.reset32(); // OpenSL ES resets its millisecond position when stopped.
404 int64_t framesWritten = getFramesWritten();
405 if (framesWritten >= 0) {
406 setFramesRead(framesWritten);
407 }
408 setState(StreamState::Stopped);
409 } else {
410 setState(initialState);
411 }
412 return result;
413 }
414
setFramesRead(int64_t framesRead)415 void AudioOutputStreamOpenSLES::setFramesRead(int64_t framesRead) {
416 int64_t millisWritten = framesRead * kMillisPerSecond / getSampleRate();
417 mPositionMillis.set(millisWritten);
418 }
419
updateFramesRead()420 void AudioOutputStreamOpenSLES::updateFramesRead() {
421 if (usingFIFO()) {
422 AudioStreamBuffered::updateFramesRead();
423 } else {
424 mFramesRead = getFramesProcessedByServer();
425 }
426 }
427
updateServiceFrameCounter()428 Result AudioOutputStreamOpenSLES::updateServiceFrameCounter() {
429 Result result = Result::OK;
430 // Avoid deadlock if another thread is trying to stop or close this stream
431 // and this is being called from a callback.
432 if (mLock.try_lock()) {
433
434 if (mPlayInterface == nullptr) {
435 mLock.unlock();
436 return Result::ErrorNull;
437 }
438 SLmillisecond msec = 0;
439 SLresult slResult = (*mPlayInterface)->GetPosition(mPlayInterface, &msec);
440 if (SL_RESULT_SUCCESS != slResult) {
441 LOGW("%s(): GetPosition() returned %s", __func__, getSLErrStr(slResult));
442 // set result based on SLresult
443 result = Result::ErrorInternal;
444 } else {
445 mPositionMillis.update32(msec);
446 }
447 mLock.unlock();
448 }
449 return result;
450 }
451