Skip to content

Latest commit

 

History

History
766 lines (637 loc) · 23.9 KB

SDL_openslES.c

File metadata and controls

766 lines (637 loc) · 23.9 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_AUDIO_DRIVER_OPENSLES
Jun 8, 2019
Jun 8, 2019
25
26
27
28
/* For more discussion of low latency audio on Android, see this:
https://googlesamples.github.io/android-audio-high-performance/guides/opensl_es.html
*/
Feb 12, 2020
Feb 12, 2020
29
#include "SDL_assert.h"
30
31
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
Feb 12, 2020
Feb 12, 2020
32
#include "../../core/android/SDL_android.h"
33
34
#include "SDL_openslES.h"
Jan 14, 2019
Jan 14, 2019
35
/* for native audio */
36
37
38
39
40
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <android/log.h>
Jan 14, 2019
Jan 14, 2019
41
#if 0
Aug 30, 2019
Aug 30, 2019
42
#define LOG_TAG "SDL_openslES"
Jan 14, 2019
Jan 14, 2019
43
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
May 23, 2019
May 23, 2019
44
45
46
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
//#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
#define LOGV(...)
Jan 14, 2019
Jan 14, 2019
47
#else
48
#define LOGE(...)
May 23, 2019
May 23, 2019
49
50
#define LOGI(...)
#define LOGV(...)
Jan 14, 2019
Jan 14, 2019
51
#endif
Feb 12, 2020
Feb 12, 2020
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
#define SL_SPEAKER_FRONT_LEFT ((SLuint32) 0x00000001)
#define SL_SPEAKER_FRONT_RIGHT ((SLuint32) 0x00000002)
#define SL_SPEAKER_FRONT_CENTER ((SLuint32) 0x00000004)
#define SL_SPEAKER_LOW_FREQUENCY ((SLuint32) 0x00000008)
#define SL_SPEAKER_BACK_LEFT ((SLuint32) 0x00000010)
#define SL_SPEAKER_BACK_RIGHT ((SLuint32) 0x00000020)
#define SL_SPEAKER_FRONT_LEFT_OF_CENTER ((SLuint32) 0x00000040)
#define SL_SPEAKER_FRONT_RIGHT_OF_CENTER ((SLuint32) 0x00000080)
#define SL_SPEAKER_BACK_CENTER ((SLuint32) 0x00000100)
#define SL_SPEAKER_SIDE_LEFT ((SLuint32) 0x00000200)
#define SL_SPEAKER_SIDE_RIGHT ((SLuint32) 0x00000400)
#define SL_SPEAKER_TOP_CENTER ((SLuint32) 0x00000800)
#define SL_SPEAKER_TOP_FRONT_LEFT ((SLuint32) 0x00001000)
#define SL_SPEAKER_TOP_FRONT_CENTER ((SLuint32) 0x00002000)
#define SL_SPEAKER_TOP_FRONT_RIGHT ((SLuint32) 0x00004000)
#define SL_SPEAKER_TOP_BACK_LEFT ((SLuint32) 0x00008000)
#define SL_SPEAKER_TOP_BACK_CENTER ((SLuint32) 0x00010000)
#define SL_SPEAKER_TOP_BACK_RIGHT ((SLuint32) 0x00020000)
*/
#define SL_ANDROID_SPEAKER_STEREO (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT)
#define SL_ANDROID_SPEAKER_QUAD (SL_ANDROID_SPEAKER_STEREO | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT)
#define SL_ANDROID_SPEAKER_5DOT1 (SL_ANDROID_SPEAKER_QUAD | SL_SPEAKER_FRONT_CENTER | SL_SPEAKER_LOW_FREQUENCY)
#define SL_ANDROID_SPEAKER_7DOT1 (SL_ANDROID_SPEAKER_5DOT1 | SL_SPEAKER_SIDE_LEFT | SL_SPEAKER_SIDE_RIGHT)
Jan 14, 2019
Jan 14, 2019
78
/* engine interfaces */
Feb 12, 2020
Feb 12, 2020
79
80
static SLObjectItf engineObject;
static SLEngineItf engineEngine;
Jan 14, 2019
Jan 14, 2019
82
/* output mix interfaces */
Feb 12, 2020
Feb 12, 2020
83
static SLObjectItf outputMixObject;
Jan 14, 2019
Jan 14, 2019
85
/* buffer queue player interfaces */
Feb 12, 2020
Feb 12, 2020
86
87
88
static SLObjectItf bqPlayerObject;
static SLPlayItf bqPlayerPlay;
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
Feb 5, 2019
Feb 5, 2019
89
#if 0
Feb 12, 2020
Feb 12, 2020
90
static SLVolumeItf bqPlayerVolume;
Feb 5, 2019
Feb 5, 2019
91
#endif
Feb 12, 2020
Feb 12, 2020
93
94
95
/* recorder interfaces */
static SLObjectItf recorderObject;
static SLRecordItf recorderRecord;
Jan 14, 2019
Jan 14, 2019
96
static SLAndroidSimpleBufferQueueItf recorderBufferQueue;
Jan 14, 2019
Jan 14, 2019
99
100
101
102
103
static const char *sldevaudiorecorderstr = "SLES Audio Recorder";
static const char *sldevaudioplayerstr = "SLES Audio Player";
#define SLES_DEV_AUDIO_RECORDER sldevaudiorecorderstr
#define SLES_DEV_AUDIO_PLAYER sldevaudioplayerstr
104
105
static void openslES_DetectDevices( int iscapture )
{
Feb 12, 2020
Feb 12, 2020
106
LOGI( "openSLES_DetectDevices()" );
107
108
if ( iscapture )
addfn( SLES_DEV_AUDIO_RECORDER );
Feb 12, 2020
Feb 12, 2020
109
else
110
111
112
113
addfn( SLES_DEV_AUDIO_PLAYER );
}
#endif
Feb 12, 2020
Feb 12, 2020
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
static void openslES_DestroyEngine(void)
{
LOGI("openslES_DestroyEngine()");
/* destroy output mix object, and invalidate all associated interfaces */
if (outputMixObject != NULL) {
(*outputMixObject)->Destroy(outputMixObject);
outputMixObject = NULL;
}
/* destroy engine object, and invalidate all associated interfaces */
if (engineObject != NULL) {
(*engineObject)->Destroy(engineObject);
engineObject = NULL;
engineEngine = NULL;
}
}
Jan 14, 2019
Jan 14, 2019
132
static int
Feb 12, 2020
Feb 12, 2020
133
openslES_CreateEngine(void)
Jan 14, 2019
Jan 14, 2019
135
SLresult result;
Jan 14, 2019
Jan 14, 2019
137
LOGI("openSLES_CreateEngine()");
Jan 14, 2019
Jan 14, 2019
139
/* create engine */
Jan 14, 2019
Jan 14, 2019
140
141
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
142
LOGE("slCreateEngine failed: %d", result);
Jan 14, 2019
Jan 14, 2019
143
144
145
goto error;
}
LOGI("slCreateEngine OK");
Jan 14, 2019
Jan 14, 2019
147
/* realize the engine */
Jan 14, 2019
Jan 14, 2019
148
149
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
150
LOGE("RealizeEngine failed: %d", result);
Jan 14, 2019
Jan 14, 2019
151
152
153
goto error;
}
LOGI("RealizeEngine OK");
Jan 14, 2019
Jan 14, 2019
155
/* get the engine interface, which is needed in order to create other objects */
Jan 14, 2019
Jan 14, 2019
156
157
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
158
LOGE("EngineGetInterface failed: %d", result);
Jan 14, 2019
Jan 14, 2019
159
160
161
goto error;
}
LOGI("EngineGetInterface OK");
Feb 12, 2020
Feb 12, 2020
163
/* create output mix */
Jan 14, 2019
Jan 14, 2019
164
165
166
167
const SLInterfaceID ids[1] = { SL_IID_VOLUME };
const SLboolean req[1] = { SL_BOOLEAN_FALSE };
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
168
LOGE("CreateOutputMix failed: %d", result);
Jan 14, 2019
Jan 14, 2019
169
170
171
goto error;
}
LOGI("CreateOutputMix OK");
Jan 14, 2019
Jan 14, 2019
173
/* realize the output mix */
Jan 14, 2019
Jan 14, 2019
174
175
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
176
LOGE("RealizeOutputMix failed: %d", result);
Jan 14, 2019
Jan 14, 2019
177
178
179
goto error;
}
return 1;
Jan 14, 2019
Jan 14, 2019
181
182
183
error:
openslES_DestroyEngine();
return 0;
Feb 12, 2020
Feb 12, 2020
186
187
188
189
190
191
192
193
194
/* this callback handler is called every time a buffer finishes recording */
static void
bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) context;
LOGV("SLES: Recording Callback");
SDL_SemPost(audiodata->playsem);
}
Feb 12, 2020
Feb 12, 2020
196
197
static void
openslES_DestroyPCMRecorder(_THIS)
Feb 12, 2020
Feb 12, 2020
199
200
struct SDL_PrivateAudioData *audiodata = this->hidden;
SLresult result;
Jan 14, 2019
Jan 14, 2019
201
Feb 12, 2020
Feb 12, 2020
202
203
204
205
206
207
208
/* stop recording */
if (recorderRecord != NULL) {
result = (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) {
LOGE("SetRecordState stopped: %d", result);
}
}
Jan 14, 2019
Jan 14, 2019
209
Feb 12, 2020
Feb 12, 2020
210
211
212
213
214
215
/* destroy audio recorder object, and invalidate all associated interfaces */
if (recorderObject != NULL) {
(*recorderObject)->Destroy(recorderObject);
recorderObject = NULL;
recorderRecord = NULL;
recorderBufferQueue = NULL;
Jan 14, 2019
Jan 14, 2019
216
}
Feb 12, 2020
Feb 12, 2020
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
if (audiodata->playsem) {
SDL_DestroySemaphore(audiodata->playsem);
audiodata->playsem = NULL;
}
if (audiodata->mixbuff) {
SDL_free(audiodata->mixbuff);
}
}
static int
openslES_CreatePCMRecorder(_THIS)
{
struct SDL_PrivateAudioData *audiodata = this->hidden;
SLDataFormat_PCM format_pcm;
SLresult result;
int i;
if (!Android_JNI_RequestPermission("android.permission.RECORD_AUDIO")) {
Feb 14, 2020
Feb 14, 2020
237
LOGE("This app doesn't have RECORD_AUDIO permission");
Feb 12, 2020
Feb 12, 2020
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
return SDL_SetError("This app doesn't have RECORD_AUDIO permission");
}
/* Just go with signed 16-bit audio as it's the most compatible */
this->spec.format = AUDIO_S16SYS;
this->spec.channels = 1;
/*this->spec.freq = SL_SAMPLINGRATE_16 / 1000;*/
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(&this->spec);
LOGI("Try to open %u hz %u bit chan %u %s samples %u",
this->spec.freq, SDL_AUDIO_BITSIZE(this->spec.format),
this->spec.channels, (this->spec.format & 0x1000) ? "BE" : "LE", this->spec.samples);
/* configure audio source */
SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
SLDataSource audioSrc = {&loc_dev, NULL};
/* configure audio sink */
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS };
format_pcm.formatType = SL_DATAFORMAT_PCM;
format_pcm.numChannels = this->spec.channels;
format_pcm.samplesPerSec = this->spec.freq * 1000; /* / kilo Hz to milli Hz */
format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
format_pcm.containerSize = SDL_AUDIO_BITSIZE(this->spec.format);
format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
SLDataSink audioSnk = { &loc_bufq, &format_pcm };
/* create audio recorder */
/* (requires the RECORD_AUDIO permission) */
const SLInterfaceID ids[1] = {
SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
};
const SLboolean req[1] = {
SL_BOOLEAN_TRUE,
};
result = (*engineEngine)->CreateAudioRecorder(engineEngine, &recorderObject, &audioSrc, &audioSnk, 1, ids, req);
if (SL_RESULT_SUCCESS != result) {
LOGE("CreateAudioRecorder failed: %d", result);
goto failed;
}
/* realize the recorder */
result = (*recorderObject)->Realize(recorderObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
LOGE("RealizeAudioPlayer failed: %d", result);
goto failed;
}
/* get the record interface */
result = (*recorderObject)->GetInterface(recorderObject, SL_IID_RECORD, &recorderRecord);
if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_RECORD interface get failed: %d", result);
goto failed;
}
/* get the buffer queue interface */
result = (*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &recorderBufferQueue);
if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_BUFFERQUEUE interface get failed: %d", result);
goto failed;
}
/* register callback on the buffer queue */
/* context is '(SDL_PrivateAudioData *)this->hidden' */
result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, this->hidden);
if (SL_RESULT_SUCCESS != result) {
LOGE("RegisterCallback failed: %d", result);
goto failed;
}
/* Create the audio buffer semaphore */
audiodata->playsem = SDL_CreateSemaphore(0);
if (!audiodata->playsem) {
LOGE("cannot create Semaphore!");
goto failed;
}
/* Create the sound buffers */
audiodata->mixbuff = (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
if (audiodata->mixbuff == NULL) {
LOGE("mixbuffer allocate - out of memory");
goto failed;
}
for (i = 0; i < NUM_BUFFERS; i++) {
audiodata->pmixbuff[i] = audiodata->mixbuff + i * this->spec.size;
}
/* in case already recording, stop recording and clear buffer queue */
result = (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) {
LOGE("Record set state failed: %d", result);
goto failed;
}
/* enqueue empty buffers to be filled by the recorder */
for (i = 0; i < NUM_BUFFERS; i++) {
result = (*recorderBufferQueue)->Enqueue(recorderBufferQueue, audiodata->pmixbuff[i], this->spec.size);
if (SL_RESULT_SUCCESS != result) {
LOGE("Record enqueue buffers failed: %d", result);
goto failed;
}
Jan 14, 2019
Jan 14, 2019
346
}
Feb 12, 2020
Feb 12, 2020
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* start recording */
result = (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_RECORDING);
if (SL_RESULT_SUCCESS != result) {
LOGE("Record set state failed: %d", result);
goto failed;
}
return 0;
failed:
openslES_DestroyPCMRecorder(this);
return SDL_SetError("Open device failed!");
Jan 14, 2019
Jan 14, 2019
364
/* this callback handler is called every time a buffer finishes playing */
Jan 14, 2019
Jan 14, 2019
365
366
static void
bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
Jan 14, 2019
Jan 14, 2019
368
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) context;
Feb 12, 2020
Feb 12, 2020
369
370
LOGV("SLES: Playback Callback");
Jan 14, 2019
Jan 14, 2019
371
SDL_SemPost(audiodata->playsem);
Feb 12, 2020
Feb 12, 2020
374
375
static void
openslES_DestroyPCMPlayer(_THIS)
Feb 12, 2020
Feb 12, 2020
377
378
struct SDL_PrivateAudioData *audiodata = this->hidden;
SLresult result;
Jan 14, 2019
Jan 14, 2019
379
Feb 12, 2020
Feb 12, 2020
380
381
382
383
384
385
386
/* set the player's state to 'stopped' */
if (bqPlayerPlay != NULL) {
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) {
LOGE("SetPlayState stopped failed: %d", result);
}
}
Feb 12, 2020
Feb 12, 2020
388
389
390
391
/* destroy buffer queue audio player object, and invalidate all associated interfaces */
if (bqPlayerObject != NULL) {
(*bqPlayerObject)->Destroy(bqPlayerObject);
Jan 14, 2019
Jan 14, 2019
392
Feb 12, 2020
Feb 12, 2020
393
394
395
396
397
398
399
400
401
402
403
404
405
bqPlayerObject = NULL;
bqPlayerPlay = NULL;
bqPlayerBufferQueue = NULL;
}
if (audiodata->playsem) {
SDL_DestroySemaphore(audiodata->playsem);
audiodata->playsem = NULL;
}
if (audiodata->mixbuff) {
SDL_free(audiodata->mixbuff);
}
Jan 14, 2019
Jan 14, 2019
408
static int
Jan 14, 2019
Jan 14, 2019
409
openslES_CreatePCMPlayer(_THIS)
Feb 12, 2020
Feb 12, 2020
411
struct SDL_PrivateAudioData *audiodata = this->hidden;
Jan 14, 2019
Jan 14, 2019
412
413
414
SLDataFormat_PCM format_pcm;
SLresult result;
int i;
May 23, 2019
May 23, 2019
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* If we want to add floating point audio support (requires API level 21)
it can be done as described here:
https://developer.android.com/ndk/guides/audio/opensl/android-extensions.html#floating-point
*/
#if 1
/* Just go with signed 16-bit audio as it's the most compatible */
this->spec.format = AUDIO_S16SYS;
#else
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
while (test_format != 0) {
if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
break;
}
test_format = SDL_NextAudioFormat();
}
May 23, 2019
May 23, 2019
432
433
434
435
436
437
if (test_format == 0) {
/* Didn't find a compatible format : */
LOGI( "No compatible audio format, using signed 16-bit audio" );
test_format = AUDIO_S16SYS;
}
this->spec.format = test_format;
Jan 14, 2019
Jan 14, 2019
438
#endif
Jan 14, 2019
Jan 14, 2019
440
/* Update the fragment size as size in bytes */
Jan 14, 2019
Jan 14, 2019
441
SDL_CalculateAudioSpec(&this->spec);
Jan 14, 2019
Jan 14, 2019
443
444
LOGI("Try to open %u hz %u bit chan %u %s samples %u",
this->spec.freq, SDL_AUDIO_BITSIZE(this->spec.format),
May 23, 2019
May 23, 2019
445
this->spec.channels, (this->spec.format & 0x1000) ? "BE" : "LE", this->spec.samples);
Jan 14, 2019
Jan 14, 2019
447
/* configure audio source */
Feb 5, 2019
Feb 5, 2019
448
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS };
Jan 14, 2019
Jan 14, 2019
450
451
format_pcm.formatType = SL_DATAFORMAT_PCM;
format_pcm.numChannels = this->spec.channels;
Jan 14, 2019
Jan 14, 2019
452
format_pcm.samplesPerSec = this->spec.freq * 1000; /* / kilo Hz to milli Hz */
Jan 14, 2019
Jan 14, 2019
453
454
format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
format_pcm.containerSize = SDL_AUDIO_BITSIZE(this->spec.format);
Jan 14, 2019
Jan 14, 2019
456
457
458
459
460
if (SDL_AUDIO_ISBIGENDIAN(this->spec.format)) {
format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
} else {
format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
}
Jun 7, 2019
Jun 7, 2019
462
463
464
465
466
467
switch (this->spec.channels)
{
case 1:
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
break;
case 2:
Jun 8, 2019
Jun 8, 2019
468
format_pcm.channelMask = SL_ANDROID_SPEAKER_STEREO;
Jun 7, 2019
Jun 7, 2019
469
470
break;
case 3:
Jun 8, 2019
Jun 8, 2019
471
format_pcm.channelMask = SL_ANDROID_SPEAKER_STEREO | SL_SPEAKER_FRONT_CENTER;
Jun 7, 2019
Jun 7, 2019
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
break;
case 4:
format_pcm.channelMask = SL_ANDROID_SPEAKER_QUAD;
break;
case 5:
format_pcm.channelMask = SL_ANDROID_SPEAKER_QUAD | SL_SPEAKER_FRONT_CENTER;
break;
case 6:
format_pcm.channelMask = SL_ANDROID_SPEAKER_5DOT1;
break;
case 7:
format_pcm.channelMask = SL_ANDROID_SPEAKER_5DOT1 | SL_SPEAKER_BACK_CENTER;
break;
case 8:
format_pcm.channelMask = SL_ANDROID_SPEAKER_7DOT1;
break;
default:
/* Unknown number of channels, fall back to stereo */
this->spec.channels = 2;
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
break;
Jan 14, 2019
Jan 14, 2019
493
}
Jan 14, 2019
Jan 14, 2019
495
SLDataSource audioSrc = { &loc_bufq, &format_pcm };
Jan 14, 2019
Jan 14, 2019
497
/* configure audio sink */
Jan 14, 2019
Jan 14, 2019
498
499
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
SLDataSink audioSnk = { &loc_outmix, NULL };
Jan 14, 2019
Jan 14, 2019
501
/* create audio player */
Jan 14, 2019
Jan 14, 2019
502
503
504
505
506
507
508
509
510
511
512
513
const SLInterfaceID ids[2] = {
SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
SL_IID_VOLUME
};
const SLboolean req[2] = {
SL_BOOLEAN_TRUE,
SL_BOOLEAN_FALSE,
};
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 2, ids, req);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
514
LOGE("CreateAudioPlayer failed: %d", result);
Jan 14, 2019
Jan 14, 2019
515
516
goto failed;
}
Jan 14, 2019
Jan 14, 2019
518
/* realize the player */
Jan 14, 2019
Jan 14, 2019
519
520
result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
521
LOGE("RealizeAudioPlayer failed: %d", result);
Jan 14, 2019
Jan 14, 2019
522
523
goto failed;
}
Jan 14, 2019
Jan 14, 2019
525
/* get the play interface */
Jan 14, 2019
Jan 14, 2019
526
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
Jan 14, 2019
Jan 14, 2019
527
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
528
LOGE("SL_IID_PLAY interface get failed: %d", result);
Jan 14, 2019
Jan 14, 2019
529
530
goto failed;
}
Jan 14, 2019
Jan 14, 2019
532
/* get the buffer queue interface */
Jan 14, 2019
Jan 14, 2019
533
534
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bqPlayerBufferQueue);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
535
LOGE("SL_IID_BUFFERQUEUE interface get failed: %d", result);
Jan 14, 2019
Jan 14, 2019
536
537
goto failed;
}
Jan 14, 2019
Jan 14, 2019
539
540
541
/* register callback on the buffer queue */
/* context is '(SDL_PrivateAudioData *)this->hidden' */
result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this->hidden);
Jan 14, 2019
Jan 14, 2019
542
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
543
LOGE("RegisterCallback failed: %d", result);
Jan 14, 2019
Jan 14, 2019
544
545
goto failed;
}
Feb 5, 2019
Feb 5, 2019
547
#if 0
Jan 14, 2019
Jan 14, 2019
548
/* get the volume interface */
Jan 14, 2019
Jan 14, 2019
549
550
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
551
LOGE("SL_IID_VOLUME interface get failed: %d", result);
Jan 14, 2019
Jan 14, 2019
552
/* goto failed; */
Jan 14, 2019
Jan 14, 2019
553
}
Feb 5, 2019
Feb 5, 2019
554
#endif
555
556
/* Create the audio buffer semaphore */
Jan 14, 2019
Jan 14, 2019
557
558
audiodata->playsem = SDL_CreateSemaphore(NUM_BUFFERS - 1);
if (!audiodata->playsem) {
Jan 14, 2019
Jan 14, 2019
559
560
561
LOGE("cannot create Semaphore!");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
563
/* Create the sound buffers */
Jan 14, 2019
Jan 14, 2019
564
565
audiodata->mixbuff = (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
if (audiodata->mixbuff == NULL) {
Jan 14, 2019
Jan 14, 2019
566
567
568
LOGE("mixbuffer allocate - out of memory");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
570
for (i = 0; i < NUM_BUFFERS; i++) {
Jan 14, 2019
Jan 14, 2019
571
audiodata->pmixbuff[i] = audiodata->mixbuff + i * this->spec.size;
Jan 14, 2019
Jan 14, 2019
572
}
Jan 14, 2019
Jan 14, 2019
574
/* set the player's state to playing */
Jan 14, 2019
Jan 14, 2019
575
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
Jan 14, 2019
Jan 14, 2019
576
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
577
LOGE("Play set state failed: %d", result);
Jan 14, 2019
Jan 14, 2019
578
579
580
goto failed;
}
581
582
return 0;
Jan 14, 2019
Jan 14, 2019
583
failed:
Jan 14, 2019
Jan 14, 2019
585
openslES_DestroyPCMPlayer(this);
Jan 14, 2019
Jan 14, 2019
587
return SDL_SetError("Open device failed!");
Jan 14, 2019
Jan 14, 2019
590
591
static int
openslES_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
Jan 14, 2019
Jan 14, 2019
593
594
595
596
597
this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
Jan 14, 2019
Jan 14, 2019
598
if (iscapture) {
Feb 12, 2020
Feb 12, 2020
599
LOGI("openslES_OpenDevice() %s for capture", devname);
Jan 14, 2019
Jan 14, 2019
600
601
return openslES_CreatePCMRecorder(this);
} else {
Feb 12, 2020
Feb 12, 2020
602
LOGI("openslES_OpenDevice() %s for playing", devname);
Jan 14, 2019
Jan 14, 2019
603
604
return openslES_CreatePCMPlayer(this);
}
Jan 14, 2019
Jan 14, 2019
607
static void
Feb 12, 2020
Feb 12, 2020
608
openslES_WaitDevice(_THIS)
Feb 12, 2020
Feb 12, 2020
610
struct SDL_PrivateAudioData *audiodata = this->hidden;
Jan 14, 2019
Jan 14, 2019
611
Feb 12, 2020
Feb 12, 2020
612
LOGV("openslES_WaitDevice()");
Jan 14, 2019
Jan 14, 2019
613
Feb 12, 2020
Feb 12, 2020
614
615
/* Wait for an audio chunk to finish */
SDL_SemWait(audiodata->playsem);
Jan 14, 2019
Jan 14, 2019
618
static void
Feb 12, 2020
Feb 12, 2020
619
openslES_PlayDevice(_THIS)
Feb 12, 2020
Feb 12, 2020
621
622
struct SDL_PrivateAudioData *audiodata = this->hidden;
SLresult result;
Jan 14, 2019
Jan 14, 2019
623
Feb 12, 2020
Feb 12, 2020
624
LOGV("======openslES_PlayDevice()======");
Feb 12, 2020
Feb 12, 2020
626
627
/* Queue it up */
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
Feb 12, 2020
Feb 12, 2020
629
630
631
632
633
634
635
636
637
638
audiodata->next_buffer++;
if (audiodata->next_buffer >= NUM_BUFFERS) {
audiodata->next_buffer = 0;
}
/* If Enqueue fails, callback won't be called.
* Post the semphore, not to run out of buffer */
if (SL_RESULT_SUCCESS != result) {
SDL_SemPost(audiodata->playsem);
}
Jan 14, 2019
Jan 14, 2019
641
642
643
644
645
646
647
648
649
650
651
/*/ n playn sem */
/* getbuf 0 - 1 */
/* fill buff 0 - 1 */
/* play 0 - 0 1 */
/* wait 1 0 0 */
/* getbuf 1 0 0 */
/* fill buff 1 0 0 */
/* play 0 0 0 */
/* wait */
/* */
/* okay.. */
Jan 14, 2019
Jan 14, 2019
653
654
static Uint8 *
openslES_GetDeviceBuf(_THIS)
Feb 12, 2020
Feb 12, 2020
656
struct SDL_PrivateAudioData *audiodata = this->hidden;
Jan 14, 2019
Jan 14, 2019
657
Feb 12, 2020
Feb 12, 2020
658
LOGV("openslES_GetDeviceBuf()");
Jan 14, 2019
Jan 14, 2019
659
return audiodata->pmixbuff[audiodata->next_buffer];
Feb 12, 2020
Feb 12, 2020
662
663
static int
openslES_CaptureFromDevice(_THIS, void *buffer, int buflen)
Feb 12, 2020
Feb 12, 2020
665
struct SDL_PrivateAudioData *audiodata = this->hidden;
Jan 14, 2019
Jan 14, 2019
666
SLresult result;
Feb 12, 2020
Feb 12, 2020
668
669
/* Wait for new recorded data */
SDL_SemWait(audiodata->playsem);
Feb 12, 2020
Feb 12, 2020
671
672
673
674
675
676
677
678
679
680
/* Copy it to the output buffer */
SDL_assert(buflen == this->spec.size);
SDL_memcpy(buffer, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
/* Re-enqueue the buffer */
result = (*recorderBufferQueue)->Enqueue(recorderBufferQueue, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
if (SL_RESULT_SUCCESS != result) {
LOGE("Record enqueue buffers failed: %d", result);
return -1;
}
Jan 14, 2019
Jan 14, 2019
682
683
684
audiodata->next_buffer++;
if (audiodata->next_buffer >= NUM_BUFFERS) {
audiodata->next_buffer = 0;
Jan 14, 2019
Jan 14, 2019
685
}
Feb 12, 2020
Feb 12, 2020
687
688
689
690
691
692
693
694
695
696
697
698
699
700
return this->spec.size;
}
static void
openslES_CloseDevice(_THIS)
{
/* struct SDL_PrivateAudioData *audiodata = this->hidden; */
if (this->iscapture) {
LOGI("openslES_CloseDevice() for capture");
openslES_DestroyPCMRecorder(this);
} else {
LOGI("openslES_CloseDevice() for playing");
openslES_DestroyPCMPlayer(this);
Feb 5, 2019
Feb 5, 2019
701
702
}
Feb 12, 2020
Feb 12, 2020
703
SDL_free(this->hidden);
Jan 14, 2019
Jan 14, 2019
706
707
static int
openslES_Init(SDL_AudioDriverImpl * impl)
Jan 14, 2019
Jan 14, 2019
709
LOGI("openslES_Init() called");
Jan 14, 2019
Jan 14, 2019
711
712
713
if (!openslES_CreateEngine()) {
return 0;
}
Jan 14, 2019
Jan 14, 2019
715
LOGI("openslES_Init() - set pointers");
Jan 14, 2019
Jan 14, 2019
717
/* Set the function pointers */
Jan 14, 2019
Jan 14, 2019
718
/* impl->DetectDevices = openslES_DetectDevices; */
Jan 14, 2019
Jan 14, 2019
719
impl->OpenDevice = openslES_OpenDevice;
Feb 12, 2020
Feb 12, 2020
720
impl->WaitDevice = openslES_WaitDevice;
Jan 14, 2019
Jan 14, 2019
721
722
impl->PlayDevice = openslES_PlayDevice;
impl->GetDeviceBuf = openslES_GetDeviceBuf;
Feb 12, 2020
Feb 12, 2020
723
724
impl->CaptureFromDevice = openslES_CaptureFromDevice;
impl->CloseDevice = openslES_CloseDevice;
Jan 14, 2019
Jan 14, 2019
725
impl->Deinitialize = openslES_DestroyEngine;
Jan 14, 2019
Jan 14, 2019
727
/* and the capabilities */
Feb 12, 2020
Feb 12, 2020
728
729
730
impl->HasCaptureSupport = 1;
impl->OnlyHasDefaultOutputDevice = 1;
impl->OnlyHasDefaultCaptureDevice = 1;
Feb 12, 2020
Feb 12, 2020
732
LOGI("openslES_Init() - success");
Jan 14, 2019
Jan 14, 2019
734
735
/* this audio target is available. */
return 1;
736
737
738
}
AudioBootStrap openslES_bootstrap = {
Jan 14, 2019
Jan 14, 2019
739
"openslES", "opensl ES audio driver", openslES_Init, 0
Feb 12, 2020
Feb 12, 2020
742
void openslES_ResumeDevices(void)
Jan 14, 2019
Jan 14, 2019
743
{
Jan 14, 2019
Jan 14, 2019
744
745
746
747
if (bqPlayerPlay != NULL) {
/* set the player's state to 'playing' */
SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
748
LOGE("openslES_ResumeDevices failed: %d", result);
Jan 14, 2019
Jan 14, 2019
749
}
Jan 14, 2019
Jan 14, 2019
750
751
752
}
}
Feb 12, 2020
Feb 12, 2020
753
void openslES_PauseDevices(void)
Jan 14, 2019
Jan 14, 2019
754
{
Jan 14, 2019
Jan 14, 2019
755
756
757
758
if (bqPlayerPlay != NULL) {
/* set the player's state to 'paused' */
SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PAUSED);
if (SL_RESULT_SUCCESS != result) {
Feb 12, 2020
Feb 12, 2020
759
LOGE("openslES_PauseDevices failed: %d", result);
Jan 14, 2019
Jan 14, 2019
760
}
Jan 14, 2019
Jan 14, 2019
761
762
763
}
}
764
765
766
#endif /* SDL_AUDIO_DRIVER_OPENSLES */
/* vi: set ts=4 sw=4 expandtab: */