Skip to content

Latest commit

 

History

History
628 lines (506 loc) · 19.1 KB

SDL_openslES.c

File metadata and controls

628 lines (506 loc) · 19.1 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Simple DirectMedia Layer
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "SDL_openslES.h"
Jan 14, 2019
Jan 14, 2019
29
/* for native audio */
30
31
32
33
34
35
36
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <android/log.h>
#define LOG_TAG "SDL_openslES"
Jan 14, 2019
Jan 14, 2019
37
38
39
40
41
42
#if 0
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
// #define LOGI(...) do {} while (0)
// #define LOGE(...) do {} while (0)
#else
43
44
#define LOGI(...)
#define LOGE(...)
Jan 14, 2019
Jan 14, 2019
45
#endif
Jan 14, 2019
Jan 14, 2019
47
/* engine interfaces */
48
49
50
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
Jan 14, 2019
Jan 14, 2019
51
/* output mix interfaces */
52
static SLObjectItf outputMixObject = NULL;
Jan 14, 2019
Jan 14, 2019
53
// static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
Jan 14, 2019
Jan 14, 2019
55
56
/* aux effect on the output mix, used by the buffer queue player */
/* static const SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR; */
Jan 14, 2019
Jan 14, 2019
58
/* buffer queue player interfaces */
Jan 14, 2019
Jan 14, 2019
59
60
61
62
63
64
static SLObjectItf bqPlayerObject = NULL;
static SLPlayItf bqPlayerPlay = NULL;
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue = NULL;
/*static SLEffectSendItf bqPlayerEffectSend = NULL; */
static SLMuteSoloItf bqPlayerMuteSolo = NULL;
static SLVolumeItf bqPlayerVolume = NULL;
Jan 14, 2019
Jan 14, 2019
66
#if 0
Jan 14, 2019
Jan 14, 2019
67
/* recorder interfaces TODO */
Jan 14, 2019
Jan 14, 2019
68
69
70
71
static SLObjectItf recorderObject = NULL;
static SLRecordItf recorderRecord;
static SLAndroidSimpleBufferQueueItf recorderBufferQueue;
#endif
Jan 14, 2019
Jan 14, 2019
73
/* pointer and size of the next player buffer to enqueue, and number of remaining buffers */
Jan 14, 2019
Jan 14, 2019
74
75
76
77
78
#if 0
static short *nextBuffer;
static unsigned nextSize;
static int nextCount;
#endif
Jan 14, 2019
Jan 14, 2019
80
// static SDL_AudioDevice* audioDevice = NULL;
Jan 14, 2019
Jan 14, 2019
83
84
85
86
87
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
88
89
static void openslES_DetectDevices( int iscapture )
{
Jan 14, 2019
Jan 14, 2019
90
LOGI( "openSLES_DetectDevices()" );
91
92
if ( iscapture )
addfn( SLES_DEV_AUDIO_RECORDER );
Jan 14, 2019
Jan 14, 2019
93
else
94
addfn( SLES_DEV_AUDIO_PLAYER );
Jan 14, 2019
Jan 14, 2019
95
return;
Jan 14, 2019
Jan 14, 2019
99
static void openslES_DestroyEngine();
Jan 14, 2019
Jan 14, 2019
101
static int
Jan 14, 2019
Jan 14, 2019
102
openslES_CreateEngine()
Jan 14, 2019
Jan 14, 2019
104
SLresult result;
Jan 14, 2019
Jan 14, 2019
106
LOGI("openSLES_CreateEngine()");
Jan 14, 2019
Jan 14, 2019
108
/* create engine */
Jan 14, 2019
Jan 14, 2019
109
110
111
112
113
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
if (SL_RESULT_SUCCESS != result) {
LOGE("slCreateEngine failed");
goto error;
}
Jan 14, 2019
Jan 14, 2019
115
LOGI("slCreateEngine OK");
Jan 14, 2019
Jan 14, 2019
117
/* realize the engine */
Jan 14, 2019
Jan 14, 2019
118
119
120
121
122
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
LOGE("RealizeEngine failed");
goto error;
}
Jan 14, 2019
Jan 14, 2019
124
LOGI("RealizeEngine OK");
Jan 14, 2019
Jan 14, 2019
126
/* get the engine interface, which is needed in order to create other objects */
Jan 14, 2019
Jan 14, 2019
127
128
129
130
131
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
if (SL_RESULT_SUCCESS != result) {
LOGE("EngineGetInterface failed");
goto error;
}
Jan 14, 2019
Jan 14, 2019
133
LOGI("EngineGetInterface OK");
Jan 14, 2019
Jan 14, 2019
135
136
137
/* create output mix, with environmental reverb specified as a non-required interface */
/* const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB }; */
/* const SLboolean req[1] = { SL_BOOLEAN_FALSE }; */
Jan 14, 2019
Jan 14, 2019
139
140
141
const SLInterfaceID ids[1] = { SL_IID_VOLUME };
const SLboolean req[1] = { SL_BOOLEAN_FALSE };
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
Jan 14, 2019
Jan 14, 2019
143
144
145
146
147
if (SL_RESULT_SUCCESS != result) {
LOGE("CreateOutputMix failed");
goto error;
}
LOGI("CreateOutputMix OK");
Jan 14, 2019
Jan 14, 2019
149
/* realize the output mix */
Jan 14, 2019
Jan 14, 2019
150
151
152
153
154
155
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
LOGE("RealizeOutputMix failed");
goto error;
}
return 1;
Jan 14, 2019
Jan 14, 2019
157
158
159
error:
openslES_DestroyEngine();
return 0;
Jan 14, 2019
Jan 14, 2019
162
163
static void openslES_DestroyPCMPlayer(_THIS);
static void openslES_DestroyPCMRecorder(_THIS);
Jan 14, 2019
Jan 14, 2019
165
static void openslES_DestroyEngine()
Jan 14, 2019
Jan 14, 2019
167
LOGI("openslES_DestroyEngine()");
Jan 14, 2019
Jan 14, 2019
168
Jan 14, 2019
Jan 14, 2019
169
170
// openslES_DestroyPCMPlayer(this);
// openslES_DestroyPCMRecorder(this);
Jan 14, 2019
Jan 14, 2019
171
Jan 14, 2019
Jan 14, 2019
172
/* destroy output mix object, and invalidate all associated interfaces */
Jan 14, 2019
Jan 14, 2019
173
174
175
if (outputMixObject != NULL) {
(*outputMixObject)->Destroy(outputMixObject);
outputMixObject = NULL;
Jan 14, 2019
Jan 14, 2019
176
/* outputMixEnvironmentalReverb = NULL; */
Jan 14, 2019
Jan 14, 2019
177
}
Jan 14, 2019
Jan 14, 2019
179
/* destroy engine object, and invalidate all associated interfaces */
Jan 14, 2019
Jan 14, 2019
180
181
182
183
184
if (engineObject != NULL) {
(*engineObject)->Destroy(engineObject);
engineObject = NULL;
engineEngine = NULL;
}
Jan 14, 2019
Jan 14, 2019
186
return;
Jan 14, 2019
Jan 14, 2019
189
/* this callback handler is called every time a buffer finishes playing */
Jan 14, 2019
Jan 14, 2019
190
191
static void
bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
Jan 14, 2019
Jan 14, 2019
193
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) context;
Jan 14, 2019
Jan 14, 2019
194
195
static int t = 0;
Jan 14, 2019
Jan 14, 2019
196
197
/* assert(bq == bqPlayerBufferQueue); */
/* assert(NULL == context); */
Jan 14, 2019
Jan 14, 2019
199
/* for streaming playback, replace this test by logic to find and fill the next buffer */
Jan 14, 2019
Jan 14, 2019
201
202
203
204
if (--nextCount > 0 && NULL != nextBuffer && 0 != nextSize)
{
SLresult result;
Jan 14, 2019
Jan 14, 2019
205
/* enqueue another buffer */
Jan 14, 2019
Jan 14, 2019
206
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
Jan 14, 2019
Jan 14, 2019
207
208
/* the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT, */
/* which for this code example would indicate a programming error */
Jan 14, 2019
Jan 14, 2019
209
210
211
assert(SL_RESULT_SUCCESS == result);
(void) result;
}
Jan 14, 2019
Jan 14, 2019
213
LOGI("SLES: Playback Callmeback %u", t++);
Jan 14, 2019
Jan 14, 2019
214
SDL_SemPost(audiodata->playsem);
Jan 14, 2019
Jan 14, 2019
215
return;
Jan 14, 2019
Jan 14, 2019
218
219
static int
openslES_CreatePCMRecorder(_THIS)
Jan 14, 2019
Jan 14, 2019
221
222
/* struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
Jan 14, 2019
Jan 14, 2019
223
224
LOGE("openslES_CreatePCMRecorder not implimented yet!");
return SDL_SetError("openslES_CreatePCMRecorder not implimented yet!");
Jan 14, 2019
Jan 14, 2019
227
static void
Jan 14, 2019
Jan 14, 2019
228
openslES_DestroyPCMRecorder(_THIS)
Jan 14, 2019
Jan 14, 2019
230
231
/* struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
Jan 14, 2019
Jan 14, 2019
232
return;
Jan 14, 2019
Jan 14, 2019
235
static int
Jan 14, 2019
Jan 14, 2019
236
openslES_CreatePCMPlayer(_THIS)
Jan 14, 2019
Jan 14, 2019
238
239
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
Jan 14, 2019
Jan 14, 2019
240
SLDataFormat_PCM format_pcm;
Jan 14, 2019
Jan 14, 2019
242
SDL_AudioFormat test_format = 0;
Jan 14, 2019
Jan 14, 2019
243
244
SLresult result;
int i;
Jan 14, 2019
Jan 14, 2019
246
#if 0
Jan 14, 2019
Jan 14, 2019
247
Jan 14, 2019
Jan 14, 2019
248
test_format = SDL_FirstAudioFormat( this->spec.format );
Jan 14, 2019
Jan 14, 2019
250
while (test_format != 0) {
Jan 14, 2019
Jan 14, 2019
252
253
254
255
256
if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
break;
}
test_format = SDL_NextAudioFormat();
}
Jan 14, 2019
Jan 14, 2019
258
if ( test_format == 0 ) {
Jan 14, 2019
Jan 14, 2019
259
/* Didn't find a compatible format : */
Jan 14, 2019
Jan 14, 2019
260
261
262
LOGI( "No compatible audio format!" );
return SDL_SetError("No compatible audio format!");
}
Jan 14, 2019
Jan 14, 2019
264
this->spec.format = test_format;
Jan 14, 2019
Jan 14, 2019
265
#endif
Jan 14, 2019
Jan 14, 2019
267
/* Update the fragment size as size in bytes */
Jan 14, 2019
Jan 14, 2019
268
SDL_CalculateAudioSpec(&this->spec);
Jan 14, 2019
Jan 14, 2019
270
271
272
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, (test_format & 0x1000) ? "BE" : "LE", this->spec.samples);
Jan 14, 2019
Jan 14, 2019
274
/* configure audio source */
Feb 5, 2019
Feb 5, 2019
275
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS };
Jan 14, 2019
Jan 14, 2019
277
278
format_pcm.formatType = SL_DATAFORMAT_PCM;
format_pcm.numChannels = this->spec.channels;
Jan 14, 2019
Jan 14, 2019
279
format_pcm.samplesPerSec = this->spec.freq * 1000; /* / kilo Hz to milli Hz */
Jan 14, 2019
Jan 14, 2019
280
281
format_pcm.bitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format);
format_pcm.containerSize = SDL_AUDIO_BITSIZE(this->spec.format);
Jan 14, 2019
Jan 14, 2019
283
284
285
286
287
if (SDL_AUDIO_ISBIGENDIAN(this->spec.format)) {
format_pcm.endianness = SL_BYTEORDER_BIGENDIAN;
} else {
format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
}
Jan 14, 2019
Jan 14, 2019
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#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)
Jan 14, 2019
Jan 14, 2019
310
311
312
313
314
315
316
317
318
319
320
321
322
if (this->spec.channels == 1) {
format_pcm.channelMask = SL_SPEAKER_FRONT_CENTER;
} else if (this->spec.channels == 2) {
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
} else if (this->spec.channels == 3) {
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER;
} else if (this->spec.channels == 4) {
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
} else {
format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT |
SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT | SL_SPEAKER_FRONT_CENTER;
}
Jan 14, 2019
Jan 14, 2019
324
SLDataSource audioSrc = { &loc_bufq, &format_pcm };
Jan 14, 2019
Jan 14, 2019
326
/* configure audio sink */
Jan 14, 2019
Jan 14, 2019
327
328
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
SLDataSink audioSnk = { &loc_outmix, NULL };
Jan 14, 2019
Jan 14, 2019
330
/* create audio player */
Jan 14, 2019
Jan 14, 2019
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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) {
LOGE("CreateAudioPlayer failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
347
/* realize the player */
Jan 14, 2019
Jan 14, 2019
348
349
350
351
352
result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
LOGE("RealizeAudioPlayer failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
354
/* get the play interface */
Jan 14, 2019
Jan 14, 2019
355
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
Jan 14, 2019
Jan 14, 2019
356
357
358
359
if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_PLAY interface get failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
361
/* get the buffer queue interface */
Jan 14, 2019
Jan 14, 2019
362
363
364
365
366
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &bqPlayerBufferQueue);
if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_BUFFERQUEUE interface get failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
368
369
370
/* 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
371
372
373
374
if (SL_RESULT_SUCCESS != result) {
LOGE("RegisterCallback failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
377
/* get the effect send interface */
Jan 14, 2019
Jan 14, 2019
378
379
380
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend);
if (SL_RESULT_SUCCESS != result)
{
Jan 14, 2019
Jan 14, 2019
382
383
384
LOGE("SL_IID_EFFECTSEND interface get failed");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
387
388
#if 0 /* mute/solo is not supported for sources that are known to be mono, as this is */
/* get the mute/solo interface */
389
390
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_MUTESOLO, &bqPlayerMuteSolo);
assert(SL_RESULT_SUCCESS == result);
Jan 14, 2019
Jan 14, 2019
391
(void) result;
Jan 14, 2019
Jan 14, 2019
394
/* get the volume interface */
Jan 14, 2019
Jan 14, 2019
395
396
397
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_VOLUME interface get failed");
Jan 14, 2019
Jan 14, 2019
398
/* goto failed; */
Jan 14, 2019
Jan 14, 2019
399
}
400
401
/* Create the audio buffer semaphore */
Jan 14, 2019
Jan 14, 2019
402
403
audiodata->playsem = SDL_CreateSemaphore(NUM_BUFFERS - 1);
if (!audiodata->playsem) {
Jan 14, 2019
Jan 14, 2019
404
405
406
LOGE("cannot create Semaphore!");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
408
/* Create the sound buffers */
Jan 14, 2019
Jan 14, 2019
409
410
audiodata->mixbuff = (Uint8 *) SDL_malloc(NUM_BUFFERS * this->spec.size);
if (audiodata->mixbuff == NULL) {
Jan 14, 2019
Jan 14, 2019
411
412
413
LOGE("mixbuffer allocate - out of memory");
goto failed;
}
Jan 14, 2019
Jan 14, 2019
415
for (i = 0; i < NUM_BUFFERS; i++) {
Jan 14, 2019
Jan 14, 2019
416
audiodata->pmixbuff[i] = audiodata->mixbuff + i * this->spec.size;
Jan 14, 2019
Jan 14, 2019
417
}
Jan 14, 2019
Jan 14, 2019
419
/* set the player's state to playing */
Jan 14, 2019
Jan 14, 2019
420
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
Jan 14, 2019
Jan 14, 2019
421
422
423
424
425
if (SL_RESULT_SUCCESS != result) {
LOGE("Play set state failed");
goto failed;
}
426
427
return 0;
Jan 14, 2019
Jan 14, 2019
428
failed:
Jan 14, 2019
Jan 14, 2019
430
openslES_DestroyPCMPlayer(this);
Jan 14, 2019
Jan 14, 2019
432
return SDL_SetError("Open device failed!");
Jan 14, 2019
Jan 14, 2019
435
static void
Jan 14, 2019
Jan 14, 2019
436
openslES_DestroyPCMPlayer(_THIS)
Jan 14, 2019
Jan 14, 2019
438
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
Jan 14, 2019
Jan 14, 2019
439
440
441
SLresult result;
/* set the player's state to 'stopped' */
Jan 14, 2019
Jan 14, 2019
442
443
444
445
446
if (bqPlayerPlay != NULL) {
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("Stopped set state failed");
}
Jan 14, 2019
Jan 14, 2019
447
}
Jan 14, 2019
Jan 14, 2019
448
449
/* destroy buffer queue audio player object, and invalidate all associated interfaces */
Jan 14, 2019
Jan 14, 2019
450
if (bqPlayerObject != NULL) {
Jan 14, 2019
Jan 14, 2019
452
(*bqPlayerObject)->Destroy(bqPlayerObject);
Jan 14, 2019
Jan 14, 2019
454
bqPlayerObject = NULL;
Jan 14, 2019
Jan 14, 2019
455
bqPlayerPlay = NULL;
456
bqPlayerBufferQueue = NULL;
Jan 14, 2019
Jan 14, 2019
457
/* bqPlayerEffectSend = NULL; */
Jan 14, 2019
Jan 14, 2019
458
459
bqPlayerMuteSolo = NULL;
bqPlayerVolume = NULL;
Jan 14, 2019
Jan 14, 2019
462
463
464
if (audiodata->playsem) {
SDL_DestroySemaphore(audiodata->playsem);
audiodata->playsem = NULL;
Jan 14, 2019
Jan 14, 2019
465
}
Jan 14, 2019
Jan 14, 2019
467
468
if (audiodata->mixbuff) {
SDL_free(audiodata->mixbuff);
Jan 14, 2019
Jan 14, 2019
469
}
Jan 14, 2019
Jan 14, 2019
471
return;
Jan 14, 2019
Jan 14, 2019
474
475
static int
openslES_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
Jan 14, 2019
Jan 14, 2019
477
478
479
480
481
this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
Jan 14, 2019
Jan 14, 2019
482
483
484
485
486
487
488
if (iscapture) {
LOGI("openslES_OpenDevice( ) %s for capture", devname);
return openslES_CreatePCMRecorder(this);
} else {
LOGI("openslES_OpenDevice( ) %s for playing", devname);
return openslES_CreatePCMPlayer(this);
}
Jan 14, 2019
Jan 14, 2019
491
492
static void
openslES_CloseDevice(_THIS)
Jan 14, 2019
Jan 14, 2019
494
495
/* struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; */
Jan 14, 2019
Jan 14, 2019
496
497
if (this->iscapture) {
LOGI("openslES_CloseDevice( ) for capture");
Jan 14, 2019
Jan 14, 2019
498
openslES_DestroyPCMRecorder(this);
Jan 14, 2019
Jan 14, 2019
499
500
} else {
LOGI("openslES_CloseDevice( ) for playing");
Jan 14, 2019
Jan 14, 2019
501
openslES_DestroyPCMPlayer(this);
Jan 14, 2019
Jan 14, 2019
502
}
Jan 14, 2019
Jan 14, 2019
503
Jan 14, 2019
Jan 14, 2019
504
505
SDL_free(this->hidden);
Jan 14, 2019
Jan 14, 2019
506
return;
Jan 14, 2019
Jan 14, 2019
509
510
static void
openslES_WaitDevice(_THIS)
Jan 14, 2019
Jan 14, 2019
512
513
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
Jan 14, 2019
Jan 14, 2019
514
LOGI("openslES_WaitDevice( )");
515
516
/* Wait for an audio chunk to finish */
Jan 14, 2019
Jan 14, 2019
517
518
/* WaitForSingleObject(this->hidden->audio_sem, INFINITE); */
SDL_SemWait(audiodata->playsem);
Jan 14, 2019
Jan 14, 2019
520
return;
Jan 14, 2019
Jan 14, 2019
523
524
525
526
527
528
529
530
531
532
533
/*/ 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
535
536
static Uint8 *
openslES_GetDeviceBuf(_THIS)
Jan 14, 2019
Jan 14, 2019
538
539
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
Jan 14, 2019
Jan 14, 2019
540
LOGI("openslES_GetDeviceBuf( )");
Jan 14, 2019
Jan 14, 2019
541
return audiodata->pmixbuff[audiodata->next_buffer];
Jan 14, 2019
Jan 14, 2019
544
545
static void
openslES_PlayDevice(_THIS)
Jan 14, 2019
Jan 14, 2019
547
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
Jan 14, 2019
Jan 14, 2019
548
SLresult result;
Jan 14, 2019
Jan 14, 2019
550
LOGI("======openslES_PlayDevice( )======");
Feb 5, 2019
Feb 5, 2019
552
/* Queue it up */
Jan 14, 2019
Jan 14, 2019
553
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, audiodata->pmixbuff[audiodata->next_buffer], this->spec.size);
Jan 14, 2019
Jan 14, 2019
555
556
557
audiodata->next_buffer++;
if (audiodata->next_buffer >= NUM_BUFFERS) {
audiodata->next_buffer = 0;
Jan 14, 2019
Jan 14, 2019
558
}
Feb 5, 2019
Feb 5, 2019
560
561
562
563
564
565
/* 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
566
return;
Jan 14, 2019
Jan 14, 2019
569
570
static int
openslES_Init(SDL_AudioDriverImpl * impl)
Jan 14, 2019
Jan 14, 2019
572
LOGI("openslES_Init() called");
Jan 14, 2019
Jan 14, 2019
574
575
576
if (!openslES_CreateEngine()) {
return 0;
}
Jan 14, 2019
Jan 14, 2019
578
LOGI("openslES_Init() - set pointers");
Jan 14, 2019
Jan 14, 2019
580
/* Set the function pointers */
Jan 14, 2019
Jan 14, 2019
581
/* impl->DetectDevices = openslES_DetectDevices; */
Jan 14, 2019
Jan 14, 2019
582
impl->OpenDevice = openslES_OpenDevice;
Jan 14, 2019
Jan 14, 2019
583
impl->CloseDevice = openslES_CloseDevice;
Jan 14, 2019
Jan 14, 2019
584
585
586
587
impl->PlayDevice = openslES_PlayDevice;
impl->GetDeviceBuf = openslES_GetDeviceBuf;
impl->Deinitialize = openslES_DestroyEngine;
impl->WaitDevice = openslES_WaitDevice;
Jan 14, 2019
Jan 14, 2019
589
590
591
/* and the capabilities */
impl->HasCaptureSupport = 0; /* TODO */
impl->OnlyHasDefaultOutputDevice = 1;
Jan 14, 2019
Jan 14, 2019
592
/* impl->OnlyHasDefaultInputDevice = 1; */
Jan 14, 2019
Jan 14, 2019
594
LOGI("openslES_Init() - succes");
Jan 14, 2019
Jan 14, 2019
596
597
/* this audio target is available. */
return 1;
598
599
600
}
AudioBootStrap openslES_bootstrap = {
Jan 14, 2019
Jan 14, 2019
601
"openslES", "opensl ES audio driver", openslES_Init, 0
Jan 14, 2019
Jan 14, 2019
604
605
void openslES_ResumeDevices()
{
Jan 14, 2019
Jan 14, 2019
606
607
608
609
610
611
if (bqPlayerPlay != NULL) {
/* set the player's state to 'playing' */
SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("openslES_ResumeDevices failed");
}
Jan 14, 2019
Jan 14, 2019
612
613
614
615
616
}
}
void openslES_PauseDevices()
{
Jan 14, 2019
Jan 14, 2019
617
618
619
620
621
622
if (bqPlayerPlay != NULL) {
/* set the player's state to 'paused' */
SLresult result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PAUSED);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("openslES_PauseDevices failed");
}
Jan 14, 2019
Jan 14, 2019
623
624
625
}
}
626
627
628
#endif /* SDL_AUDIO_DRIVER_OPENSLES */
/* vi: set ts=4 sw=4 expandtab: */